Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secure Folder will fail to create with FBE (Android 11 and lower) #26

Closed
XDABlackMesa123 opened this issue May 17, 2023 · 21 comments
Closed
Assignees

Comments

@XDABlackMesa123
Copy link
Owner

XDABlackMesa123 commented May 17, 2023

Secure Folder bases part of its implementation over the AOSP multi user API's. For this reason, devices with FBE in place will require a user key generated by the keymaster TA, more info here (qualcomm.com) and here (docs.samsungknox.com).

Devices with a legacy One UI version (One UI 3.x, and also 2.x as saw in #23) will refuse to generate the required user key due to additional device integrity checks implemented directly in the SKeyMaster TA.
We can ensure this is the case by following the entire creation process via system logs. The following logs were captured on a Galaxy A52s 5G running its latest Android 11 based firmware (A528BXXS1AUL3).

First off, the Secure Folder app triggers the container creation:

03-09 09:09:19.699  3978  3978 I ProvisioningActivityPresenter: Set Knox container name as : Secure Folder
03-09 09:09:19.700  1061  3824 D KnoxMUMContainerPolicy: updateProvisioningState called: state = 1
03-09 09:09:19.700  1061  3824 I KnoxMUMContainerPolicy: Provisioning started... { isCLType:false type:secure-folder state:1 creatorUid:10126 adminPackageName:com.samsung.knox.securefolder }
03-09 09:09:19.734  1061  2313 I SystemServerTiming: createUser-268566624
03-09 09:09:19.734  1061  2313 D PersonaServiceHelper: creating user owned container

UserManagerService.checkIntegrity() is called to verify whether or not the device can generate a SAK via keymaster, this check is already hooked in the module via KnoxDARHooks. Nothing wrong happens if this is patched:

03-09 09:09:19.767  1142  1142 I keystore: del USRPKEY_KnoxTestKey 1000 0
03-09 09:09:19.768  1142  1142 I keystore: del USRSKEY_KnoxTestKey 1000 0
03-09 09:09:19.769  1142  1142 I keystore: del USRCERT_KnoxTestKey 1000 0
03-09 09:09:19.771  1142  1142 I keystore: del CACERT_KnoxTestKey 1000 0
03-09 09:09:19.771  1142  1142 I keystore: del USRCSR_KnoxTestKey 1000 0
03-09 09:09:19.773  1142  1142 I keystore: del USRPKEY_KnoxTestKey 1000 0
03-09 09:09:19.774  1142  1142 I keystore: del USRSKEY_KnoxTestKey 1000 0
03-09 09:09:19.775  1142  1142 I keystore: del USRCERT_KnoxTestKey 1000 0
03-09 09:09:19.776  1142  1142 I keystore: del CACERT_KnoxTestKey 1000 0
03-09 09:09:19.776  1142  1142 I keystore: del USRCSR_KnoxTestKey 1000 0
03-09 09:09:19.777   600   600 W keymaster_tee: [WRN]start nwd_import_key
03-09 09:09:19.782   600   600 D keymaster_swd: keymaster_swd [ERR] (tz_check_oem:74) Device is compromized: fuse loc=5,status=0,sw_fuse_blown=1
03-09 09:09:19.782   600   600 D keymaster_swd: keymaster_swd [WRN] (swd_run_cb:251) swd_import_key() returns 0
03-09 09:09:19.782   600   600 W keymaster_tee: [WRN]ret OK PARAMS: A32 P1 B32 P1 NAR1 S256 2023.03.09,09:09:19.777 
03-09 09:09:19.783  1142  2403 I keystore: !@ writeBlobs blob user_0/1000_USRPKEY_KnoxTestKey
03-09 09:09:19.824  1142  1142 I keystore: del USRPKEY_KnoxTestKey 1000 0
03-09 09:09:19.824  1142  1142 I keystore: !@ reading blob user_0/1000_USRPKEY_KnoxTestKey
03-09 09:09:19.832  1142  1142 I keystore: del USRCERT_KnoxTestKey 1000 0
03-09 09:09:19.843  1142  1142 I keystore: del CACERT_KnoxTestKey 1000 0
03-09 09:09:19.847  1142  1142 I keystore: del USRCSR_KnoxTestKey 1000 0
03-09 09:09:19.848  1061  2313 I SyntheticPasswordCrypto: SP key deleted: KnoxTestKey

UserManagerService.checkDRK() is called:

03-09 09:09:19.848  1061  2313 I DEVROOT#MGR: [android] create DeviceRootKeyServiceManager.
03-09 09:09:19.848  1061  2313 E DEVROOT#CIF:  KFI_isExistDRK start
03-09 09:09:19.855   886   914 E DEVROOT#VND:  communicate start cmd:0x2041, length:64
03-09 09:09:19.855   886   914 E DEVROOT#VND:  cmd [2041] start...
03-09 09:09:19.856   886   914 E DEVROOT#VND:  DRK file exists. 2517
03-09 09:09:19.856   886   914 E DEVROOT#VND:  loadTA (1) start ...
03-09 09:09:19.921   886   914 E DEVROOT#VND:  loadTA End... 
03-09 09:09:19.976   886   914 E DEVROOT#VND:  cmd [2041] end...[0]
03-09 09:09:19.978   886   914 E DEVROOT#VND:  communicate end 0 / length : 0

Once both checks passes, UserManagerService will begin to generate the user key:

03-09 09:09:19.978  1061  2313 I SystemServerTiming: createUserKey
03-09 09:09:19.979   546   585 D vold    : createUserKey(151)
03-09 09:09:19.979   546   585 D FsCrypt : fscrypt_vold_create_user_key for 151 serial 151
03-09 09:09:19.979   546   585 D FsCrypt : create_and_install_user_keys 151
03-09 09:09:19.979   546   585 E FsCrypt : Preparing: /data/misc/vold/user_keys/ce/151
03-09 09:09:19.979   546   585 D FsCrypt : Skipping non-key .
03-09 09:09:19.979   546   585 D FsCrypt : Skipping non-key ..
03-09 09:09:19.979   546   585 D KeyStorage: Already exists, destroying: /data/misc/vold/user_keys/temp
03-09 09:09:19.980   546   585 D vold    : /system/bin/secdiscard
03-09 09:09:19.980   546   585 D vold    :     --
03-09 09:09:19.980   546   585 D vold    :     /data/misc/vold/user_keys/temp/encrypted_key
03-09 09:09:19.980   546   585 D vold    :     /data/misc/vold/user_keys/temp/secdiscardable
03-09 09:09:20.029   546   585 D vold    : /system/bin/rm
03-09 09:09:20.029   546   585 D vold    :     -rf
03-09 09:09:20.029   546   585 D vold    :     /data/misc/vold/user_keys/temp

Vold will now call keymaster to handle the key generation:

03-09 09:09:20.065   546   585 D KeyStorage: Inside KeyStorage.isKnoxKeyPath() - key_path : /data/misc/vold/user_keys/ce/151/current
03-09 09:09:20.065   546   585 D KeyStorage: Inside KeyStorage.isKnoxKeyPath() - user_de_path_length : 29
03-09 09:09:20.066   546   585 D KeyStorage: Inside KeyStorage.isKnoxKeyPath() - user_ce_path_length : 29
03-09 09:09:20.066   546   585 D KeyStorage: Input path is for Knox user
03-09 09:09:20.066   546   585 E KeyStorage: storeKey /data/misc/vold/user_keys/temp
03-09 09:09:20.106   532   532 I hwservicemanager: getTransport: Cannot find entry android.hardware.keymaster@3.0::IKeymasterDevice/default in either framework or device manifest.
03-09 09:09:20.107   546   585 I vold    : List of Keymaster HALs found:
03-09 09:09:20.107   546   585 I vold    : Keymaster HAL #1: SKeymaster(Keymaster MDFPP) from SKeymaster team SecurityLevel: TRUSTED_ENVIRONMENT HAL: android.hardware.keymaster@4.1::IKeymasterDevice/default
03-09 09:09:20.107   546   585 I vold    : Using SKeymaster(Keymaster MDFPP) from SKeymaster team for encryption.  Security level: TRUSTED_ENVIRONMENT, HAL: android.hardware.keymaster@4.1::IKeymasterDevice/default
03-09 09:09:20.107   546   585 D KeyStorage: Knox protection required in generating keymaster key
03-09 09:09:20.107   546   585 D KeyStorage: Creating key that doesn't need auth token

Keymaster TA starts to generate the key, but it will fail on purpose due to the device having a blown warranty bit and non-trusted boot status (custom binary installed):

03-09 09:09:20.112   600   600 W keymaster_tee: [WRN]start nwd_generate_key
03-09 09:09:20.118   600   600 D keymaster_swd: keymaster_swd [ERR] (tz_check_trust_boot_status:1426) TB is fail:0x1
03-09 09:09:20.118   600   600 D keymaster_swd: keymaster_swd [ERR] (tz_check_trust_boot_status:1437) WB is fail:0x1
03-09 09:09:20.118   600   600 D keymaster_swd: keymaster_swd [ERR] (km_check_trustboot_for_knox:413) tz_check_trust_boot_status fail
03-09 09:09:20.118   600   600 W keymaster_tee: [WRN]Cmd 2, err -45
03-09 09:09:20.118   600   600 W keymaster_tee: [WRN]nwd_generate_key ret -45

...it's a disaster:

03-09 09:09:20.118   546   585 E vold    : generate_key failed, code -45
03-09 09:09:20.119   546   585 E KeyStorage: generateKeymasterKey failed
03-09 09:09:20.119  1061  2313 E StorageManagerService: android.os.ServiceSpecificException:  (code 0)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.Parcel.createExceptionOrNull(Parcel.java:2399)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.Parcel.createException(Parcel.java:2369)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2352)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2294)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.IVold$Stub$Proxy.createUserKey(IVold.java:3248)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at com.android.server.StorageManagerService.createUserKey(StorageManagerService.java:3825)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.storage.StorageManager.createUserKey(StorageManager.java:1621)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUncheckedNoTracing(UserManagerService.java:3844)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUnchecked(UserManagerService.java:3601)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternal(UserManagerService.java:3587)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createProfileForUserWithThrow(UserManagerService.java:3509)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.IUserManager$Stub.onTransact(IUserManager.java:766)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransactInternal(Binder.java:1190)
03-09 09:09:20.119  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransact(Binder.java:1159)
03-09 09:09:20.119  1061  2313 D SystemServerTiming: createUserKey took to complete: 141ms
03-09 09:09:20.119  1061  2313 I SystemServerTiming: prepareUserData
03-09 09:09:20.120   546   585 D vold    : prepareUserStorage(151)
03-09 09:09:20.120   546   585 D FsCrypt : fscrypt_prepare_user_storage for volume null, user 151, serial 151, flags 3
03-09 09:09:20.120   546   585 E FsCrypt : Preparing: /data/system/users/151
03-09 09:09:20.120   546   585 E FsCrypt : Preparing: /data/misc/profiles/cur/151
03-09 09:09:20.120   546   585 E FsCrypt : Preparing: /data/system_de/151
03-09 09:09:20.120   546   585 E FsCrypt : Preparing: /data/misc_de/151
03-09 09:09:20.120   546   585 E FsCrypt : Preparing: /data/vendor_de/151
03-09 09:09:20.120   546   585 E FsCrypt : Preparing: /data/user_de/151
03-09 09:09:20.120   546   585 D FsCrypt : lookup_policy 151
03-09 09:09:20.120   546   585 E FsCrypt : Cannot find key for 151
03-09 09:09:20.121  1061  2313 E StorageManagerService: android.os.ServiceSpecificException:  (code 0)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.Parcel.createExceptionOrNull(Parcel.java:2399)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.Parcel.createException(Parcel.java:2369)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2352)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2294)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.IVold$Stub$Proxy.prepareUserStorage(IVold.java:3433)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.StorageManagerService.prepareUserStorageInternal(StorageManagerService.java:4040)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.StorageManagerService.prepareUserStorage(StorageManagerService.java:4034)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.storage.StorageManager.prepareUserStorage(StorageManager.java:1657)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserDataPreparer.prepareUserDataLI(UserDataPreparer.java:85)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserDataPreparer.prepareUserData(UserDataPreparer.java:74)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUncheckedNoTracing(UserManagerService.java:3848)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUnchecked(UserManagerService.java:3601)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternal(UserManagerService.java:3587)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createProfileForUserWithThrow(UserManagerService.java:3509)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.IUserManager$Stub.onTransact(IUserManager.java:766)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransactInternal(Binder.java:1190)
03-09 09:09:20.121  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransact(Binder.java:1159)
03-09 09:09:20.121  1061  2313 V UserDataPreparer: Found /data/user_de/151 with serial number -1
03-09 09:09:20.121  1061  2313 D UserDataPreparer: Serial number missing on /data/user_de/151; assuming current is valid
03-09 09:09:20.121  1061  2313 V UserDataPreparer: Found /data/system_de/151 with serial number -1
03-09 09:09:20.121  1061  2313 D UserDataPreparer: Serial number missing on /data/system_de/151; assuming current is valid
03-09 09:09:20.121  1061  2313 W PackageManager: Destroying user 151 on volume null because we failed to prepare: java.io.IOException: getxattr failed: ENOENT (No such file or directory)
03-09 09:09:20.123   546   585 D vold    : destroyUserStorage(151)
03-09 09:09:20.123   546   585 D FsCrypt : fscrypt_destroy_user_storage for volume null, user 151, flags 3
03-09 09:09:20.123   546   585 D FsCrypt : prepare_subdirs
03-09 09:09:20.123   546   585 D vold    : /system/bin/vold_prepare_subdirs
03-09 09:09:20.123   546   585 D vold    :     destroy
03-09 09:09:20.123   546   585 D vold    :
03-09 09:09:20.123   546   585 D vold    :     151
03-09 09:09:20.123   546   585 D vold    :     3
03-09 09:09:20.150 10712 10712 E vold_prepare_subdirs: Unable to open directory: /data/misc_ce/151: No such file or directory
03-09 09:09:20.150 10712 10712 E vold_prepare_subdirs: Unable to open directory: /data/vendor_ce/151: No such file or directory
03-09 09:09:20.152   546   585 E vold    : Process exited with code: 255
03-09 09:09:20.152   546   585 E FsCrypt : vold_prepare_subdirs failed
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/media/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/user/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/system_ce/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/misc_ce/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/vendor_ce/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/user_de/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/system/users/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/misc/profiles/cur/151
03-09 09:09:20.152   546   585 E FsCrypt : Destroying: /data/system_de/151
03-09 09:09:20.153   546   585 E FsCrypt : Destroying: /data/misc_de/151
03-09 09:09:20.153   546   585 E FsCrypt : Destroying: /data/vendor_de/151
03-09 09:09:20.155  1061  2313 E StorageManagerService: android.os.ServiceSpecificException:  (code 0)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.Parcel.createExceptionOrNull(Parcel.java:2399)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.Parcel.createException(Parcel.java:2369)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2352)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2294)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.IVold$Stub$Proxy.destroyUserStorage(IVold.java:3454)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.StorageManagerService.destroyUserStorage(StorageManagerService.java:4060)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.storage.StorageManager.destroyUserStorage(StorageManager.java:1666)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserDataPreparer.destroyUserDataLI(UserDataPreparer.java:153)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserDataPreparer.prepareUserDataLI(UserDataPreparer.java:112)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserDataPreparer.prepareUserData(UserDataPreparer.java:74)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUncheckedNoTracing(UserManagerService.java:3848)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUnchecked(UserManagerService.java:3601)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternal(UserManagerService.java:3587)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createProfileForUserWithThrow(UserManagerService.java:3509)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.IUserManager$Stub.onTransact(IUserManager.java:766)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransactInternal(Binder.java:1190)
03-09 09:09:20.155  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransact(Binder.java:1159)

The system will reports back the container creation failed:

03-09 09:09:20.189  1061  2313 D SystemServerTiming: prepareUserData took to complete: 70ms
03-09 09:09:20.190   546   585 I epmd    : Set sdp policy for user 151
03-09 09:09:20.190   546   585 I epmd    : Set sdp policy to /data/user/151
03-09 09:09:20.190   546   585 E epmd    : Failed to open /data/user/151: No such file or directory
03-09 09:09:20.190   546   585 E epmd    : Failed to set sdp policy for user 151
03-09 09:09:20.190   546   585 E vold    : failed: No such file or directory
03-09 09:09:20.190  1061  2313 E StorageManagerService: failed
03-09 09:09:20.190  1061  2313 E StorageManagerService: android.os.ServiceSpecificException: failed (code 2)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.Parcel.createExceptionOrNull(Parcel.java:2399)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.Parcel.createException(Parcel.java:2369)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2352)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.Parcel.readException(Parcel.java:2294)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.IVold$Stub$Proxy.setSdpPolicyCmd(IVold.java:3858)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.StorageManagerService.setSdpPolicyCmd(StorageManagerService.java:2824)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.storage.StorageManager.setSdpPolicy(StorageManager.java:1071)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.EnterprisePartitionManager.setSdpPolicy(EnterprisePartitionManager.java:267)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.SdpManagerService$LocalService.setSdpPolicy(SdpManagerService.java:5006)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.lambda$setSdpPolicy$0(UserManagerService.java:761)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.pm.-$$Lambda$UserManagerService$qu9_cdgz_y0n8pKkAUrYPXZEfbM.apply(Unknown Source:4)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at java.util.Optional.map(Optional.java:211)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.setSdpPolicy(UserManagerService.java:760)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUncheckedNoTracing(UserManagerService.java:3854)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternalUnchecked(UserManagerService.java:3601)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createUserInternal(UserManagerService.java:3587)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at com.android.server.pm.UserManagerService.createProfileForUserWithThrow(UserManagerService.java:3509)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.IUserManager$Stub.onTransact(IUserManager.java:766)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransactInternal(Binder.java:1190)
03-09 09:09:20.190  1061  2313 E StorageManagerService: 	at android.os.Binder.execTransact(Binder.java:1159)
03-09 09:09:20.190  1061  2313 E SdpManagerService: setSdpPolicy failed!
03-09 09:09:20.190  1061  2313 E UserManagerService: Failed to set sdp policy
03-09 09:09:20.190  1061  2313 D SystemServerTiming: createUser-268566624 took to complete: 456ms
03-09 09:09:20.190  1061  2313 D SystemServerTiming: !@Boot_SystemServer: 456ms : createUser-268566624
03-09 09:09:20.190  1061  2313 I SystemServerTiming: !@Boot_EBS:   Took 456ms by 'createUser-268566624'
03-09 09:09:20.197  1061  2313 D KnoxMUMContainerPolicy: updateProvisioningState called: state = 11
03-09 09:09:20.197  1061  2313 I KnoxMUMContainerPolicy: ProvisioningState.update():Bundle[{isCLType=false, pidKnox=-1, type=secure-folder, state=0, containerId=-1, requestId=-1, pidProvision=-1, creatorUid=10126, pwdRstToken=null, adminPackageName=com.samsung.knox.securefolder}]
03-09 09:09:20.197  1061  2313 I KnoxMUMContainerPolicy: ProvisioningState.update(): appying:{ state:11 }
03-09 09:09:20.197  1061  2313 I KnoxMUMContainerPolicy: failed
03-09 09:09:20.206  1061  2313 I KnoxMUMContainerPolicy: provisioningFinished()... { isCLType:false pidKnox:-1 type:secure-folder state:11 containerId:-1 requestId:-1 pidProvision:-1 creatorUid:10126 adminPackageName:com.samsung.knox.securefolder }
03-09 09:09:20.206  1061  2313 I KnoxMUMContainerPolicy: provisioningFinished() not a normal finish, state:11
03-09 09:09:20.207  3978 10689 E CreateProfileTask: CreateProfileTask run. com.samsung.knox.securefolder.presentation.switcher.setupwizard.presenter.provisioningtasks.ProvisioningException: user creation failed
03-09 09:09:20.207  3978 10689 E CreateProfileTask: 	at com.samsung.knox.securefolder.presentation.switcher.setupwizard.presenter.provisioningtasks.task.CreateProfileTask.run(CreateProfileTask.java:63)
03-09 09:09:20.207  3978 10689 E CreateProfileTask: 	at com.samsung.knox.securefolder.presentation.switcher.setupwizard.presenter.provisioningtasks.AbstractProvisioningController$ProvisioningTaskHandler.handleMessage(AbstractProvisioningController.java:161)
03-09 09:09:20.207  3978 10689 E CreateProfileTask: 	at android.os.Handler.dispatchMessage(Handler.java:106)
03-09 09:09:20.207  3978 10689 E CreateProfileTask: 	at android.os.Looper.loop(Looper.java:246)
03-09 09:09:20.207  3978 10689 E CreateProfileTask: 	at android.os.HandlerThread.run(HandlerThread.java:67)
03-09 09:09:20.209  1061  3832 E KnoxMUMContainerPolicy: no ongoing provisioning
@XDABlackMesa123 XDABlackMesa123 self-assigned this May 17, 2023
@XDABlackMesa123
Copy link
Owner Author

XDABlackMesa123 commented May 17, 2023

I've decided to analyze the keymaster TA to get a better look at how this check works and how is it triggered. I'm also comparing the different keymaster TA iterations from the various OS of my device to see if anything relevant can be seen. The following TA blobs were checked:

  • Android 11 (version 4.2.19/A528BXXS1AUL3)
  • Android 12 (version 4.3.11/A528BXXS1CVE5)
  • Android 13 (version 4.4.08/A528BXXU3EWE1)

Searching for "tz_check_trust_boot_status" is enough for it to show up. It is referenced in two functions:


Surprisingly this check has been completely removed in the Android 13 TA, so we basically can ignore it.


The function itself is pretty easy to understand: it will retrieve the warranty bit and trustboot status using Iccc_ReadData_TA (probably relies on the tz_iccc TA to get those values from the bootloader) and check both values are 0 (normal). But how is it triggered?


Seems like the check is in place only if the current key params contains a specific tag with value "0x700007d0". A rapid search in system frameworks shows a result in the KeymasterDefs class:

.field public static final blacklist KM_TAG_KNOX_OBJECT_PROTECTION_REQUIRED:I = 0x700007d0

@XDABlackMesa123
Copy link
Owner Author

XDABlackMesa123 commented May 17, 2023

The following hook should completely avoid triggering tz_check_trust_boot_status in keymaster:

loadSystem(object : YukiBaseHooker() {
    private val KM_TAG_KNOX_OBJECT_PROTECTION_REQUIRED = 1879050192

    override fun onHook() {
        findClass("android.security.keymaster.KeymasterArguments").hook {
            injectMember {
                method {
                    name = "addBoolean"
                    param(IntType)
                }
                beforeHook {
                    val tag: Int = args(0).int()
                    if (tag == KM_TAG_KNOX_OBJECT_PROTECTION_REQUIRED) {
                        resultNull()
                    }
                }
            }
        }
    }
})

However, it still won't work since the tag is added anyway. By looking again in the logs, we can see vold prints some interesting debug info:

03-09 09:09:20.065   546   585 D KeyStorage: Inside KeyStorage.isKnoxKeyPath() - key_path : /data/misc/vold/user_keys/ce/151/current
03-09 09:09:20.065   546   585 D KeyStorage: Inside KeyStorage.isKnoxKeyPath() - user_de_path_length : 29
03-09 09:09:20.066   546   585 D KeyStorage: Inside KeyStorage.isKnoxKeyPath() - user_ce_path_length : 29
03-09 09:09:20.066   546   585 D KeyStorage: Input path is for Knox user
03-09 09:09:20.107   546   585 D KeyStorage: Knox protection required in generating keymaster key

Things become more clear once we check what's happening under the hood:


Looks like vold manually adds the KNOX_OBJECT_PROTECTION_REQUIRED tag if isKnoxKeyPath is true.

@XDABlackMesa123
Copy link
Owner Author

I decided to test if patching vold to ensure the KNOX_OBJECT_PROTECTION tag doesn't gets passed to the key params does fix the key generation. Since I'm unable to test on my Galaxy A52s 5G (I can't downgrade to Android 11 due to an higher RP rev value), I'm gonna use a Galaxy A71 with firmware build A715FXXU8BVA2. Using Binja I'm able to find the check in place here as well:


Let's patch vold to NOP the call to the sub_6fe84 branch:

  • Before A80317B8A9831738E7170094A8C3583888000034A00358F8400000B4F51B0394
  • After A80317B8A98317381F2003D5A8C3583888000034A00358F81F2003D5F51B0394

Surprisingly enough, Secure Folder works normally:


@KikMyaz
Copy link

KikMyaz commented Jul 22, 2023

Let's patch vold to NOP the call to the sub_6fe84 branch:

  • Before A80317B8A9831738E7170094A8C3583888000034A00358F8400000B4F51B0394
  • After A80317B8A98317381F2003D5A8C3583888000034A00358F81F2003D5F51B0394

Sorry this is a noob question - I could see you NOP the call to sub_6fe84 with E7170094 -> 1F2003D5, but may I ask what was the reasoning behind NOP-ing the cbz x0 instruction at 0x69f00? (400000B4 -> 1F2003D5)

@XDABlackMesa123
Copy link
Owner Author

Sorry this is a noob question - I could see you NOP the call to sub_6fe84 with E7170094 -> 1F2003D5, but may I ask what was the reasoning behind NOP-ing the cbz x0 instruction at 0x69f00? (400000B4 -> 1F2003D5)

I've used Binja to automatically patch the binary, I'm not an expert in assembly/pseudocode unfortunately

@KikMyaz
Copy link

KikMyaz commented Jul 22, 2023

Ah - thank you! Sorry this has come across as a really stupid question but may I ask - if it might be possible - to patch the binary using the free/web version of binary ninja? I think if it's just a NOP patch to the bl instruction we might be able to automate it inside a shell script within a recovery zip.

@XDABlackMesa123
Copy link
Owner Author

Sorry this has come across as a really stupid question but may I ask - if it might be possible - to patch the binary using the free/web version of binary ninja?

Binary Ninja is a paid software and iirc there's no free/web version of it. Another disassembler like Ghidra should work the same if it supports aarch64 binaries.

I think if it's just a NOP patch to the bl instruction we might be able to automate it inside a shell script within a recovery zip.

That's the idea, but the issue is I haven't been able to find a common patch that works for all the devices. Maybe the vold/libepm inside the Enhancer works as well with FBE enabled? I could modify the Enhancer zip to extract those fix files when flashing in recovery mode.

@XDABlackMesa123
Copy link
Owner Author

Also these were the original vold files extracted from the firmware (G9700ZCU4DTL2/Android 10) - looks like it's the exact same issue.

The patch for this vold binary is (offset 0x000718F0):

  • Before -> E0 A3 04 91 E1 03 05 91 7A 0F 00 94 0A 00 00 14
  • After -> E0 A3 04 91 E1 03 05 91 1F 20 03 D5 0A 00 00 14

@KikMyaz
Copy link

KikMyaz commented Jul 22, 2023

Sorry just realised I had mixed up the vold files I got from #23 and the stock ones, but good news!

I loaded the new vold binary from stock G9700ZCU4DTL2/Android 10 and something similar was present.
Screen Shot 2023-07-22 at 10 17 24 PM
To confirm the line needed to be patched, 71EEFF97 was the string in assembly. This can be doubled checked at https://armconverter.com/?code=bl%20%230x75dcc&offset=7a408.
Screen Shot 2023-07-22 at 10 18 00 PM
So all I need is to patch 71EEFF97 @ 0x75dcc to 1F2003D5 (NOP).
Screen Shot 2023-07-22 at 10 18 41 PM

And after replacing this patched vold binary in /system/bin/vold in TWRP, secure folder now works correctly on S10 (Android 10) with FBE enabled. No changes were needed for the library files (libepm.so). Thank you!! @blackmesa123

@XDABlackMesa123
Copy link
Owner Author

And after replacing this patched vold binary in /system/bin/vold in TWRP, secure folder now works correctly on S10 (Android 10) with FBE enabled. No changes were needed for the library files (libepm.so). Thank you!! @blackmesa123

So we do now know patching vold is the way to fix Secure Folder on A10/11 FBE-enabled devices. My plan is to integrate this fix either in a separate zip or even better on the Enhancer zip, but since Magisk modules aren't able to replace vold due to the nature of the system service, the fix has to be implemented via a recovery flash:

  • The Enhancer script already has in-system/recovery flashing detection, so we can use that
  • If flashing in-system via the Magisk/KSU app, only install the module (currently only has WSM fix)
  • If flashing in recovery, detect what fix needs to be applied:
    • Device has FBE and is running Android 10/11: vold hex patch
    • Device has no FBE and is running Android 10: replace vold bin with non-FBE version if required
    • Device has no FBE and is running Android 11: no actions required

Again, the main issue is that I haven't figured out yet a patch that works with all the possible vold binaries for both Android 10/11, probably adding multiple hex patches in the script and apply only the one that matches the bin running in the device, patching the function related to adding tags in the key arguments might not be the best idea since it either requires diff patches for each vold bin or might break stuff since I saw it's also used elsewhere in vold.

@XDABlackMesa123
Copy link
Owner Author

@KikMyaz could you test if this alternative hex patch works (by starting from scratch)? From 09FA8052 to 09008052

@XDABlackMesa123
Copy link
Owner Author

If you confirm it works, there are two way to hex patch the vold bin:

  • NOP the instruction that adds the KNOX_OBJECT_PROTECTION_REQUIRED tag:
# Android 10
magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 7A0F0094 1F2003D5
# Android 11
magiskboot hexpatch "$SYSTEM_DIR/bin/vold" E7170094 1F2003D5
  • Change the tag from KNOX_OBJECT_PROTECTION_REQUIRED (KM_BOOL | 2000) to KM_BOOL | 0
# Android 10
magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 09FA80520900AE72 090080520900AE72
# Android 11
magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 08FA805200E4006F 0800805200E4006F

The first one is the best imho, but we need to confirm it works for all the devices.

@XDABlackMesa123
Copy link
Owner Author

XDABlackMesa123 commented Aug 2, 2023

The patch that modifies the tag looks our best bet compared to the branch one (assuming it works), comparing a few vold binaries I was able to find four hexpatches that should make the script work for all the support Android 10/11 devices:

if grep -q 'Knox protection required' $SYSTEM_DIR/bin/vold; then
  magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 00E4006FEA861A11 00E4006FEABE0451
  magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 08FA805200E4006F 0800805200E4006F
  magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 08FA80520800AE72 080080520800AE72
  magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 09FA80520900AE72 090080520900AE72
fi

EDIT: I've tested this new set of patches and they seem to work great on my A71.

@XDABlackMesa123 XDABlackMesa123 pinned this issue Aug 2, 2023
@KikMyaz
Copy link

KikMyaz commented Aug 2, 2023

@KikMyaz could you test if this alternative hex patch works (by starting from scratch)? From 09FA8052 to 09008052

Okay... to test this I kinda wanted to back up the entire device before I flash anything :/

Now this is a little tricky since FBE is in effect and I still want to keep my data (and those in secure folder too). I have twrp but it cannot decrypt /data so I'm not entirely sure how to get that partition backed up... and even if I do a raw image of that partition I don't know which partitions have the keys (presumably I also need to restore the partitions that store the keys later on? also idk but last time when I restored a raw unencrypted userdata.img it somehow broke lockscreen passcode and I had to rebuild that database) Do you know any way of backing up the entire device/internal EMMC to the effect of QPST/EDL but not using EDL? Or any way to back up an FBE device with secure folder data?

@KikMyaz
Copy link

KikMyaz commented Aug 2, 2023

  • NOP the instruction that adds the KNOX_OBJECT_PROTECTION_REQUIRED tag:
# Android 10
magiskboot hexpatch "$SYSTEM_DIR/bin/vold" 7A0F0094 1F2003D5

Also I am not sure "7A0F0094" is correct here. I got the vold files mixed up but basically for S10 on Android 10 the instruction I needed to NOP in the end was "71EEFF97" instead of "7A0F0094" (which I don't think was present in my stock vold binary).

@XDABlackMesa123
Copy link
Owner Author

XDABlackMesa123 commented Aug 3, 2023

Also I am not sure "7A0F0094" is correct here. I got the vold files mixed up but basically for S10 on Android 10 the instruction I needed to NOP in the end was "71EEFF97" instead of "7A0F0094" (which I don't think was present in my stock vold binary).

Got them from #26 (comment), but at this point the best way to patch vold is with the new hex patches I posted previously at #26 (comment).

@XDABlackMesa123
Copy link
Owner Author

Do you know any way of backing up the entire device/internal EMMC to the effect of QPST/EDL but not using EDL? Or any way to back up an FBE device with secure folder data?

I don't know if backing up the raw userdata partition works out of the box, since by restoring it you'd need the encryption keys to be validated by keymaster, you'd also need to backup the metadata partition for the metadata keys. Your best bet is to use a recovery that has support for decryption, which isn't really hard to implement (here's how I did implement it in my device: Mesa-Labs-Archive/android_device_samsung_a52sxq@3b391f5)

@XDABlackMesa123
Copy link
Owner Author

XDABlackMesa123 commented Aug 3, 2023

I managed to implement a "recovery-mode" flashing in the Enhancer script, issue is it will fail to flash on recoveries without crypto support as it requires Magisk specific files in /data/adb. Perhaps a separate zip is required for those devices without a decent custom recovery?

https://github.com/BlackMesa123/KnoxPatch/actions/runs/5754241674

@KikMyaz
Copy link

KikMyaz commented Aug 3, 2023

Got them from #26 (comment), but at this point the best way to patch vold is with the new hex patches I posted previously at #26 (comment).

Yeah that was my fault 😬 I'm sorry I got the files mixed up - this vold.zip was the correct (and patched) vold binary from that firmware. I don't have the stock one handy at the moment but it's basically a matter of flipping the NOP, 1F2003D5 @ address 0x75dcc back to 71EEFF97.

@XDABlackMesa123
Copy link
Owner Author

XDABlackMesa123 commented Aug 4, 2023

Yeah that was my fault grimacing I'm sorry I got the files mixed up - this vold.zip was the correct (and patched) vold binary from that firmware. I don't have the stock one handy at the moment but it's basically a matter of flipping the NOP, 1F2003D5 @ address 0x75dcc back to 71EEFF97.

I decided to use the tag hex patch rather than NOP the call to the branch as I saw it's very difficult to patch that out as the address/patch changes from bin to bin. The tag patch worked fine on my A71 and I've identified only four different patches comparing various Android 10/11 vold binaries.

https://github.com/BlackMesa123/KnoxPatch/blob/485d1a7eed04816670219648cdcd0e268551f3b1/enhancer/customize.sh#L121-L126

@XDABlackMesa123
Copy link
Owner Author

Closing this issue since the fix has been added in Enhancer v0.4. An alternative version of the Enhancer zip is also now available for custom recoveries without crypto support.

XDABlackMesa123 added a commit to XDABlackMesa123/keybuster that referenced this issue May 25, 2024
Deprecated since Android 12, when provided with this tag the KeyMaster TA will check for the current trustboot/warranty bit status and refuse any operation if one of these isn't valid (XDABlackMesa123/KnoxPatch#26 (comment)).
Due to leftover code, we can apply this tag to bypass the KM_TAG_EXPORTABLE check in the TA. Note that EC and RSA generated keys aren't supported.

Signed-off-by: BlackMesa123 <giangrecosalvo9@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants