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

[CRITICAL BUG] Harmony crashes when trying to load Shared Prefs before user encrypted storage is available #64

Open
teqtic opened this issue Oct 14, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@teqtic
Copy link

teqtic commented Oct 14, 2024

I need to be able to load the shared prefs before the user unlocks the device for the first time. To do this, all you usually need to do is use the "device protected" storage context when creating the sharedPrefs:

try {
            return EncryptedHarmony.getSharedPreferences(
                    context.createDeviceProtectedStorageContext(), // Use device protected storage context so that it works before unlocking
                    PrefKeys.SHARED_PREFERENCES_FILE_NAME,
                    MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
                    EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                    EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
            );
        } catch (GeneralSecurityException e) {
            Utils.logError(TAG, "GeneralSecurityException: " + e);
        } catch (IOException e) {
            Utils.logError(TAG, "IOException: " + e);
        }
        return null;

Moving it to device encrypted storage after being created in the regular context does not seem to work either (may be a separate issue):

final Context deviceContext = context.createDeviceProtectedStorageContext();
        // This will come back as true if already moved (prefs wasn't found in the regular context)
        if (deviceContext.moveSharedPreferencesFrom(context, PrefKeys.SHARED_PREFERENCES_FILE_NAME)) {
            Utils.logDebug(TAG, "Migrated shared preferences to device protected storage, or was already there.");
        } else {
            Utils.logWarn(TAG, "Failed to migrate shared preferences.");
        }

Stack trace of crash below:

FATAL EXCEPTION: main
                                                                            Process: com.teqtic.lockmeout:NotificationHidingService, PID: 4024
                                                                            java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
                                                                            	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:590)
                                                                            	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                            	at java.lang.reflect.Method.invoke(Native Method)
                                                                            	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                                                                            	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878) 
                                                                            Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Unable to create harmony prefs directories
                                                                            	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
                                                                            	at java.util.concurrent.FutureTask.get(FutureTask.java:191)
                                                                            	at com.frybits.harmony.HarmonyImpl.awaitForLoad(Harmony.kt:266)
                                                                            	at com.frybits.harmony.HarmonyImpl.edit(Harmony.kt:242)
                                                                            	at androidx.security.crypto.SecureHarmonyPreferencesImpl.edit(SecureHarmonyPreferences.kt:194)
                                                                            	at com.teqtic.lockmeout.services.NotificationHidingService.onListenerConnected(NotificationHidingService.java:245)
                                                                            	at android.service.notification.NotificationListenerService$MyHandler.handleMessage(NotificationListenerService.java:2418)
                                                                            	at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                            	at android.os.Looper.loopOnce(Looper.java:232)
                                                                            	at android.os.Looper.loop(Looper.java:317)
                                                                            	at android.app.ActivityThread.main(ActivityThread.java:8592)
                                                                            	at java.lang.reflect.Method.invoke(Native Method) 
                                                                            	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580) 
                                                                            	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878) 
                                                                            Caused by: java.io.IOException: Unable to create harmony prefs directories
                                                                            	at com.frybits.harmony.HarmonyImpl.checkForRequiredFiles(Harmony.kt:273)
                                                                            	at com.frybits.harmony.HarmonyImpl.initialLoad(Harmony.kt:298)
                                                                            	at com.frybits.harmony.HarmonyImpl.isLoadedTask$lambda$4(Harmony.kt:171)
                                                                            	at com.frybits.harmony.HarmonyImpl.$r8$lambda$w7RcXw4Qa0Sdpj8Zeizyl--EULA(Unknown Source:0)
                                                                            	at com.frybits.harmony.HarmonyImpl$$ExternalSyntheticLambda6.call(Unknown Source:2)
                                                                            	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
                                                                            	at android.os.Handler.handleCallback(Handler.java:959)
                                                                            	at android.os.Handler.dispatchMessage(Handler.java:100)
                                                                            	at android.os.Looper.loopOnce(Looper.java:232)
                                                                            	at android.os.Looper.loop(Looper.java:317)
                                                                            	at android.os.HandlerThread.run(HandlerThread.java:85)
@teqtic teqtic changed the title [CRITICAL BUG] Harmony crashes when trying to load Shared Prefs before user unlocks [CRITICAL BUG] Harmony crashes when trying to load Shared Prefs before user encrypted storage is available Oct 14, 2024
@pablobaxter pablobaxter self-assigned this Oct 17, 2024
@pablobaxter pablobaxter added the bug Something isn't working label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants