Set dumpPriority to 0 in addService

This commit is contained in:
topjohnwu 2022-02-27 04:53:31 -08:00
parent f0d4bc6824
commit dfdb9b687f
4 changed files with 47 additions and 27 deletions

Binary file not shown.

View File

@ -20,6 +20,7 @@ import static com.topjohnwu.superuser.internal.RootServiceManager.TAG;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.os.IBinder;
@ -31,46 +32,68 @@ import java.lang.reflect.Method;
* These methods should only be accessed in the root process, since under normal circumstances
* accessing these internal APIs through reflection will be blocked.
*/
@SuppressLint("PrivateApi,DiscouragedPrivateApi,SoonBlockedPrivateApi")
class HiddenAPIs {
@SuppressLint("StaticFieldLeak")
private static Context systemContext;
private static Method addService;
private static Method attachBaseContext;
private static Method setAppName;
// Set this flag to silence AMS's complaints. Only exist on Android 8.0+
public static final int FLAG_RECEIVER_FROM_SHELL =
Build.VERSION.SDK_INT >= 26 ? 0x00400000 : 0;
static synchronized Context getSystemContext() {
if (systemContext == null)
systemContext = RootServerMain.getSystemContext();
return systemContext;
}
@SuppressLint("PrivateApi,DiscouragedPrivateApi")
static void setAppName(String name) {
static {
try {
Class<?> sm = Class.forName("android.os.ServiceManager");
if (Build.VERSION.SDK_INT >= 28) {
try {
addService = sm.getDeclaredMethod("addService",
String.class, IBinder.class, boolean.class, int.class);
} catch (NoSuchMethodException ignored) {
// Fallback to the 2 argument version
}
}
if (addService == null) {
addService = sm.getDeclaredMethod("addService", String.class, IBinder.class);
}
attachBaseContext = ContextWrapper.class.getDeclaredMethod("attachBaseContext", Context.class);
attachBaseContext.setAccessible(true);
Class<?> ddm = Class.forName("android.ddm.DdmHandleAppName");
Method m = ddm.getDeclaredMethod("setAppName", String.class, int.class);
m.invoke(null, name, 0);
} catch (Exception e) {
throw new RuntimeException(e);
setAppName = ddm.getDeclaredMethod("setAppName", String.class, int.class);
} catch (ReflectiveOperationException e) {
Utils.err(TAG, e);
}
}
static IBinder getService(String name) {
static void setAppName(String name) {
try {
return (IBinder) RootServerMain.getService.invoke(null, name);
} catch (Exception e) {
Utils.err(TAG, e);
return null;
setAppName.invoke(null, name, 0);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
static void addService(String name, IBinder service) {
try {
RootServerMain.addService.invoke(null, name, service);
} catch (Exception e) {
if (addService.getParameterTypes().length == 4) {
// Set dumpPriority to 0 so the service cannot be listed
addService.invoke(null, name, service, false, 0);
} else {
addService.invoke(null, name, service);
}
} catch (ReflectiveOperationException e) {
Utils.err(TAG, e);
}
}
static void attachBaseContext(Object wrapper, Context context) {
if (wrapper instanceof ContextWrapper) {
try {
attachBaseContext.invoke(wrapper, context);
} catch (ReflectiveOperationException ignored) { /* Impossible */ }
}
}
}

View File

@ -51,16 +51,14 @@ class RootServerMain extends ContextWrapper implements Callable<Object[]> {
static final String CMDLINE_START_DAEMON = "daemon";
static final String CMDLINE_STOP_SERVICE = "stop";
static final Method getService;
static final Method addService;
static final Method attachBaseContext;
private static final Method getService;
private static final Method attachBaseContext;
static {
try {
@SuppressLint("PrivateApi")
Class<?> sm = Class.forName("android.os.ServiceManager");
getService = sm.getDeclaredMethod("getService", String.class);
addService = sm.getDeclaredMethod("addService", String.class, IBinder.class);
attachBaseContext = ContextWrapper.class.getDeclaredMethod("attachBaseContext", Context.class);
attachBaseContext.setAccessible(true);
} catch (Exception e) {

View File

@ -16,7 +16,6 @@
package com.topjohnwu.superuser.internal;
import static com.topjohnwu.superuser.internal.RootServerMain.attachBaseContext;
import static com.topjohnwu.superuser.internal.RootServerMain.getServiceName;
import static com.topjohnwu.superuser.internal.RootServiceManager.BUNDLE_BINDER_KEY;
import static com.topjohnwu.superuser.internal.RootServiceManager.LOGGING_ENV;
@ -213,7 +212,7 @@ public class RootServiceServer extends IRootServiceManager.Stub {
Class<?> clz = context.getClassLoader().loadClass(name.getClassName());
Constructor<?> ctor = clz.getDeclaredConstructor();
ctor.setAccessible(true);
attachBaseContext.invoke(ctor.newInstance(), context);
HiddenAPIs.attachBaseContext(ctor.newInstance(), context);
// RootService should be registered after attachBaseContext
s = activeServices.get(name);