Skip to content

Commit

Permalink
Resolve /proc/self/exe before executing
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Jun 2, 2022
1 parent cae7a27 commit 8ef7539
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
}

dependencies {
classpath("com.android.tools.build:gradle:7.2.0")
classpath("com.android.tools.build:gradle:7.2.1")

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Binary file modified service/src/main/assets/main.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -161,43 +161,46 @@ private Shell.Task startRootProcess(ComponentName name, String action) {
context.registerReceiver(new ServiceReceiver(), filter);
}

Context de = Utils.getDeContext(context);
File mainJar = new File(de.getCacheDir(), "main.jar");
return (stdin, stdout, stderr) -> {
Context ctx = Utils.getContext();
Context de = Utils.getDeContext(ctx);
File mainJar = new File(de.getCacheDir(), "main.jar");

String env = "";
String params = "";
// Dump main.jar as trampoline
try (InputStream in = ctx.getResources().getAssets().open("main.jar");
OutputStream out = new FileOutputStream(mainJar)) {
Utils.pump(in, out);
}

if (Utils.vLog()) {
env = LOGGING_ENV + "=1 ";
}
String env = "";
String params = "";

// Only support debugging on SDK >= 27
if (Build.VERSION.SDK_INT >= 27 && Debug.isDebuggerConnected()) {
env += DEBUG_ENV + "=1 ";
// Reference of the params to start jdwp:
// https://developer.android.com/ndk/guides/wrap-script#debugging_when_using_wrapsh
if (Build.VERSION.SDK_INT == 27) {
params = API_27_DEBUG;
} else {
params = API_28_DEBUG;
if (Utils.vLog()) {
env = LOGGING_ENV + "=1 ";
}
}

String cmd = String.format(Locale.ROOT,
"(%s CLASSPATH=%s /proc/%d/exe %s /system/bin --nice-name=%s:root " +
"com.topjohnwu.superuser.internal.RootServerMain %s %d %s %s >/dev/null 2>&1)&",
env, mainJar, Process.myPid(), params, context.getPackageName(),
name.flattenToString().replace("$", "\\$"), // args[0]
Process.myUid(), // args[1]
filterAction, // args[2]
action); // args[3]

return (stdin, stdout, stderr) -> {
// Dump main.jar as trampoline
try (InputStream in = context.getResources().getAssets().open("main.jar");
OutputStream out = new FileOutputStream(mainJar)) {
Utils.pump(in, out);
// Only support debugging on SDK >= 27
if (Build.VERSION.SDK_INT >= 27 && Debug.isDebuggerConnected()) {
env += DEBUG_ENV + "=1 ";
// Reference of the params to start jdwp:
// https://developer.android.com/ndk/guides/wrap-script#debugging_when_using_wrapsh
if (Build.VERSION.SDK_INT == 27) {
params = API_27_DEBUG;
} else {
params = API_28_DEBUG;
}
}

String app_process = new File("/proc/self/exe").getCanonicalPath();
String cmd = String.format(Locale.ROOT,
"(%s CLASSPATH=%s %s %s /system/bin --nice-name=%s:root " +
"com.topjohnwu.superuser.internal.RootServerMain %s %d %s %s >/dev/null 2>&1)&",
env, mainJar, app_process, params, ctx.getPackageName(),
name.flattenToString().replace("$", "\\$"), // args[0]
Process.myUid(), // args[1]
filterAction, // args[2]
action); // args[3]

Utils.log(TAG, cmd);
// Write command to stdin
byte[] bytes = cmd.getBytes(StandardCharsets.UTF_8);
Expand Down

0 comments on commit 8ef7539

Please sign in to comment.