From 8adf598f19d5228de6798dd51eb791189d03df48 Mon Sep 17 00:00:00 2001 From: Dillon Nys Date: Wed, 30 Aug 2023 17:11:14 -0700 Subject: [PATCH] fix(auth): Crash with `EXC_BAD_ACCESS` Adds checks to results of `IOServiceGetMatchingService` and `IORegistryEntryCreateCFProperty` FFI calls to prevent trying to access bad data in cast or free calls. --- .../ffigen.macos.yaml | 1 + .../asf/asf_device_info_collector.macos.dart | 20 ++++++++++++++----- .../lib/src/platform/macos_bindings.g.dart | 7 +++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/auth/amplify_auth_cognito_dart/ffigen.macos.yaml b/packages/auth/amplify_auth_cognito_dart/ffigen.macos.yaml index 44bb5da8ab..7ef07bb56a 100644 --- a/packages/auth/amplify_auth_cognito_dart/ffigen.macos.yaml +++ b/packages/auth/amplify_auth_cognito_dart/ffigen.macos.yaml @@ -78,6 +78,7 @@ enums: macros: include: - kIOPlatformUUIDKey + - IO_OBJECT_NULL typedefs: include: - CFTypeRef diff --git a/packages/auth/amplify_auth_cognito_dart/lib/src/asf/asf_device_info_collector.macos.dart b/packages/auth/amplify_auth_cognito_dart/lib/src/asf/asf_device_info_collector.macos.dart index a7da2006cf..52343632d4 100644 --- a/packages/auth/amplify_auth_cognito_dart/lib/src/asf/asf_device_info_collector.macos.dart +++ b/packages/auth/amplify_auth_cognito_dart/lib/src/asf/asf_device_info_collector.macos.dart @@ -93,12 +93,13 @@ final class ASFDeviceInfoMacOS extends ASFDeviceInfoPlatform { Future get deviceOsReleaseVersion async => 'macOS/$_osVersion'; NSScreen? get _mainScreen { - try { - // NSScreen is not linked in Dart-only apps. - return NSScreen.getMainScreen(_native); - } on Exception { + // NSScreen is not linked in Dart-only apps. + if (!zIsFlutter) { return null; } + // TODO(dnys1): https://github.com/dart-lang/ffigen/issues/608 + // return NSScreen.getMainScreen(_native); + return null; } @override @@ -110,12 +111,17 @@ final class ASFDeviceInfoMacOS extends ASFDeviceInfoPlatform { _mainScreen?.visibleFrame.size.width.toInt() ?? 0; String? _ioValueFor(String key) => using((arena) { + const serviceName = 'IOPlatformExpertDevice'; final service = _native.IOServiceGetMatchingService( _native.kIOMasterPortDefault, _native.IOServiceMatching( - 'IOPlatformExpertDevice'.toNativeUtf8(allocator: arena).cast(), + serviceName.toNativeUtf8(allocator: arena).cast(), ), ); + logger.verbose('Found service matching $serviceName: $service'); + if (service == IO_OBJECT_NULL) { + return null; + } arena.onReleaseAll(() => _native.IOObjectRelease(service)); final data = _native.IORegistryEntryCreateCFProperty( service, @@ -123,6 +129,10 @@ final class ASFDeviceInfoMacOS extends ASFDeviceInfoPlatform { _native.kCFAllocatorDefault, 0, ); + logger.verbose('Got data for $key: $data'); + if (data == nullptr) { + return null; + } arena.onReleaseAll(() => _native.CFRelease(data)); return switch (CFType) { const (CFData) => data.cast().toDartString(_native), diff --git a/packages/auth/amplify_auth_cognito_dart/lib/src/platform/macos_bindings.g.dart b/packages/auth/amplify_auth_cognito_dart/lib/src/platform/macos_bindings.g.dart index 3325a73455..0e5b7a5840 100644 --- a/packages/auth/amplify_auth_cognito_dart/lib/src/platform/macos_bindings.g.dart +++ b/packages/auth/amplify_auth_cognito_dart/lib/src/platform/macos_bindings.g.dart @@ -10366,18 +10366,23 @@ class NativeMacOsFramework { } final class utsname extends ffi.Struct { + /// [XSI] Name of OS @ffi.Array.multi([256]) external ffi.Array sysname; + /// [XSI] Name of this network node @ffi.Array.multi([256]) external ffi.Array nodename; + /// [XSI] Release level @ffi.Array.multi([256]) external ffi.Array release; + /// [XSI] Version level @ffi.Array.multi([256]) external ffi.Array version; + /// [XSI] Hardware type @ffi.Array.multi([256]) external ffi.Array machine; } @@ -20023,3 +20028,5 @@ abstract class NSAlignmentOptions { typedef CFTypeRef1 = ffi.Pointer; const String kIOPlatformUUIDKey = 'IOPlatformUUID'; + +const int IO_OBJECT_NULL = 0;