GrapheneOS / os-issue-tracker

Issue tracker for GrapheneOS Android Open Source Project hardening work. Standalone projects like Auditor, AttestationServer and hardened_malloc have their own dedicated trackers.
https://grapheneos.org/
339 stars 18 forks source link

Fails to get ActivityService while debugging app #3518

Open borbmizzet opened 2 months ago

borbmizzet commented 2 months ago

When debugging my app in android studio, it crashes with the following error when creating the application. It does not crash if I am not using the debugger. It also seems like my collogues who do not use GrapheneOS do not have this issue, nor did I, until I switched my Pixel 8 from stock to GrapheneOS a month or two ago. My app uses both Java and C++. When I try to debug the C++ code, the app crashes as soon as the debugger is attached. When I try to debug the java code, I have verified the null value of 'processes' as described in the code I have included for context with the error log: (Built with SDK 34, GrapheneOS build 2024050700, Android Studio 2023.3.1)

2024-04-02 15:13:37.448 12152-12152 AndroidRuntime com...ndroid.apps.[redacted].debug D Shutting down VM 2024-04-02 15:13:37.450 12152-12152 AndroidRuntime com...ndroid.apps.[redacted].debug E FATAL EXCEPTION: main Process: com.[redacted].android.apps.[redacted].debug, PID: 12152 java.lang.RuntimeException: Unable to create application com.[redacted].android.[redacted].MyApplication: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7343) at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2344) 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:8532) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:894) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference at com.[redacted].android.[redacted].utils.AppUtils.isMainAppProcess(AppUtils.kt:yyy)

object AppUtils {
...
   fun cancelRestartIntent(application: MyApplication) {
        PendingIntent.getBroadcast(
            application.applicationContext,
            MyApplication.RESTART_REQUEST_CODE, Intent(application.applicationContext, SplashActivity::class.java), PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )?.apply {
            cancel()
            (application.applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager?)?.cancel(this)
        }
    }
...
  fun isMainAppProcess(context: Context) : Boolean {
    val processes = (context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).runningAppProcesses
    val id = Process.myPid()
    yyy: for (process in processes) {  // 'processes' is null
  ...

at com.[redacted].android.[redacted].MyApplication.onCreate(MyApplication.kt:zzz)

class MyApplication : android.app.Application() {
... 
override fun onCreate() {
  if (BuildConfig.DEBUG) {
    android.os.StrictMode.setVmPolicy(VmPolicy.Builder().penaltyDeathOnCleartextNetwork().build())
    Timber.plant(Timber.DebugTree())
  } else {
    Thread.setDefaultUncaughtExceptionHandler(MyExceptionHandler(this, Thread.getDefaultUncaughtExceptionHandler()))
  }
  super.onCreate()
  AppUtils.cancelRestartIntent(this)
  zzz: if (AppUtils.isMainAppProcess(this)) {

at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1384) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7338) at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2344)  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:8532)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:894)

Things I have tried: Turning off Secure App Spawning Enabling exploit protection compatibility mode for the app Explicitly setting the app to be debugged in Developer Options

thestinger commented 2 months ago

This doesn't really look like a GrapheneOS issue.

Did you disable native debugging in security settings?

borbmizzet commented 2 months ago

Native Code Debugging->"Block for third-party apps by default" is toggled off. I'm not an Android developer by trade. I've got a bit of experience, but I haven't done any native android development in years. I'm primarily a C++ developer, so I posted the relevant code because it looked to me like the issue was at an OS level or SDK level. All the native stuff is done by a different team here, I'm just working on some cross-platform development at the moment that overlaps with the android app.