2020-12-29 01:44:02 -08:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2022-02-02 04:58:31 -08:00
|
|
|
import static com.topjohnwu.magisk.BuildConfig.APPLICATION_ID;
|
|
|
|
|
2020-12-29 01:44:02 -08:00
|
|
|
import android.app.AppComponentFactory;
|
|
|
|
import android.app.Application;
|
|
|
|
import android.content.Context;
|
2021-01-26 03:40:25 -08:00
|
|
|
import android.content.ContextWrapper;
|
2022-02-13 06:22:42 -08:00
|
|
|
import android.content.pm.PackageInfo;
|
2021-04-17 22:14:54 -07:00
|
|
|
import android.content.pm.PackageManager;
|
2021-01-26 03:40:25 -08:00
|
|
|
import android.os.Build;
|
2020-12-29 01:44:02 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
2021-12-15 03:18:23 +08:00
|
|
|
import com.topjohnwu.magisk.utils.APKInstall;
|
|
|
|
|
2020-12-29 01:44:02 -08:00
|
|
|
import java.io.File;
|
2021-12-12 23:52:59 -08:00
|
|
|
import java.io.FileInputStream;
|
2020-12-29 01:44:02 -08:00
|
|
|
import java.io.FileOutputStream;
|
2021-12-12 23:52:59 -08:00
|
|
|
import java.io.IOException;
|
2021-01-26 03:40:25 -08:00
|
|
|
import java.lang.reflect.Field;
|
2022-02-01 22:43:44 -08:00
|
|
|
import java.lang.reflect.Method;
|
2022-02-13 06:22:42 -08:00
|
|
|
import java.util.HashMap;
|
2020-12-29 01:44:02 -08:00
|
|
|
|
2021-09-02 21:31:33 -07:00
|
|
|
import io.michaelrocks.paranoid.Obfuscate;
|
|
|
|
|
|
|
|
@Obfuscate
|
2021-12-15 03:18:23 +08:00
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2021-12-13 03:57:27 -08:00
|
|
|
public class DynLoad {
|
2020-12-29 01:44:02 -08:00
|
|
|
|
2021-04-17 22:14:54 -07:00
|
|
|
static Object componentFactory;
|
2022-05-22 19:36:47 -07:00
|
|
|
static ClassLoader activeClassLoader = DynLoad.class.getClassLoader();
|
2022-02-01 22:43:44 -08:00
|
|
|
|
2022-02-13 03:32:11 -08:00
|
|
|
static StubApk.Data createApkData() {
|
|
|
|
var data = new StubApk.Data();
|
|
|
|
data.setVersion(BuildConfig.STUB_VERSION);
|
2022-02-13 07:24:34 -08:00
|
|
|
data.setClassToComponent(new HashMap<>());
|
2022-02-13 03:32:11 -08:00
|
|
|
data.setRootService(DelegateRootService.class);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-02-01 22:43:44 -08:00
|
|
|
static void attachContext(Object o, Context context) {
|
|
|
|
if (!(o instanceof ContextWrapper))
|
|
|
|
return;
|
|
|
|
try {
|
|
|
|
Method m = ContextWrapper.class.getDeclaredMethod("attachBaseContext", Context.class);
|
|
|
|
m.setAccessible(true);
|
|
|
|
m.invoke(o, context);
|
|
|
|
} catch (Exception ignored) { /* Impossible */ }
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:24:34 -08:00
|
|
|
// Dynamically load APK from internal, external storage, or previous app
|
|
|
|
static AppClassLoader loadApk(Context context) {
|
|
|
|
File apk = StubApk.current(context);
|
|
|
|
File update = StubApk.update(context);
|
2021-12-12 23:52:59 -08:00
|
|
|
|
|
|
|
if (update.exists()) {
|
|
|
|
// Rename from update
|
2020-12-29 01:44:02 -08:00
|
|
|
update.renameTo(apk);
|
2021-12-12 23:52:59 -08:00
|
|
|
}
|
|
|
|
|
2022-02-01 22:43:44 -08:00
|
|
|
// Copy from external for easier development
|
2022-02-13 07:24:34 -08:00
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
File external = new File(context.getExternalFilesDir(null), "magisk.apk");
|
2021-12-12 23:52:59 -08:00
|
|
|
if (external.exists()) {
|
|
|
|
try {
|
2021-12-15 03:18:23 +08:00
|
|
|
var in = new FileInputStream(external);
|
|
|
|
var out = new FileOutputStream(apk);
|
|
|
|
try (in; out) {
|
|
|
|
APKInstall.transfer(in, out);
|
|
|
|
}
|
2021-12-12 23:52:59 -08:00
|
|
|
} catch (IOException e) {
|
2021-12-13 03:57:27 -08:00
|
|
|
Log.e(DynLoad.class.getSimpleName(), "", e);
|
2021-12-12 23:52:59 -08:00
|
|
|
apk.delete();
|
|
|
|
} finally {
|
|
|
|
external.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 22:43:44 -08:00
|
|
|
if (apk.exists()) {
|
2022-02-13 07:24:34 -08:00
|
|
|
return new AppClassLoader(apk);
|
2022-02-01 22:43:44 -08:00
|
|
|
}
|
2022-02-02 05:06:12 -08:00
|
|
|
|
|
|
|
// If no APK is loaded, attempt to copy from previous app
|
2022-02-13 07:24:34 -08:00
|
|
|
if (!context.getPackageName().equals(APPLICATION_ID)) {
|
2021-12-12 23:52:59 -08:00
|
|
|
try {
|
2022-02-02 04:58:31 -08:00
|
|
|
var info = context.getPackageManager().getApplicationInfo(APPLICATION_ID, 0);
|
|
|
|
var src = new FileInputStream(info.sourceDir);
|
|
|
|
var out = new FileOutputStream(apk);
|
|
|
|
try (src; out) {
|
|
|
|
APKInstall.transfer(src, out);
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
2022-02-13 07:24:34 -08:00
|
|
|
return new AppClassLoader(apk);
|
2022-02-02 04:58:31 -08:00
|
|
|
} catch (PackageManager.NameNotFoundException ignored) {
|
2021-12-12 23:52:59 -08:00
|
|
|
} catch (IOException e) {
|
2021-12-13 03:57:27 -08:00
|
|
|
Log.e(DynLoad.class.getSimpleName(), "", e);
|
2021-12-12 23:52:59 -08:00
|
|
|
apk.delete();
|
|
|
|
}
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
2021-12-12 23:52:59 -08:00
|
|
|
|
2022-02-13 07:24:34 -08:00
|
|
|
return null;
|
2021-12-13 03:57:27 -08:00
|
|
|
}
|
|
|
|
|
2022-02-13 03:32:11 -08:00
|
|
|
// Dynamically load APK and create the Application instance from the loaded APK
|
2022-02-01 22:43:44 -08:00
|
|
|
static Application createAndSetupApp(Application context) {
|
2022-02-13 03:32:11 -08:00
|
|
|
// On API >= 29, AppComponentFactory will replace the ClassLoader for us
|
2022-02-01 22:43:44 -08:00
|
|
|
if (Build.VERSION.SDK_INT < 29)
|
|
|
|
replaceClassLoader(context);
|
2021-04-17 22:14:54 -07:00
|
|
|
|
2022-03-02 23:16:47 +08:00
|
|
|
// noinspection InlinedApi
|
2022-02-13 06:22:42 -08:00
|
|
|
int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES
|
2022-03-02 23:16:47 +08:00
|
|
|
| PackageManager.GET_PROVIDERS | PackageManager.GET_RECEIVERS
|
|
|
|
| PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
|
2022-02-13 14:23:06 -08:00
|
|
|
var pm = context.getPackageManager();
|
2022-02-13 06:22:42 -08:00
|
|
|
|
|
|
|
final PackageInfo info;
|
|
|
|
try {
|
2022-03-02 23:16:47 +08:00
|
|
|
// noinspection WrongConstant
|
2022-02-13 14:23:06 -08:00
|
|
|
info = pm.getPackageInfo(context.getPackageName(), flags);
|
2022-02-13 06:22:42 -08:00
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
// Impossible
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
2022-02-13 03:32:11 -08:00
|
|
|
File apk = StubApk.current(context);
|
2022-02-13 07:24:34 -08:00
|
|
|
|
|
|
|
final var cl = loadApk(context);
|
|
|
|
if (cl != null) try {
|
2022-03-02 23:16:47 +08:00
|
|
|
// noinspection WrongConstant
|
2022-02-13 14:23:06 -08:00
|
|
|
var pkgInfo = pm.getPackageArchiveInfo(apk.getPath(), flags);
|
2022-02-13 07:24:34 -08:00
|
|
|
cl.updateComponentMap(info, pkgInfo);
|
|
|
|
|
2022-02-13 06:22:42 -08:00
|
|
|
var appInfo = pkgInfo.applicationInfo;
|
|
|
|
|
2022-02-13 07:24:34 -08:00
|
|
|
var data = createApkData();
|
|
|
|
var map = data.getClassToComponent();
|
|
|
|
// Create the inverse mapping (class to component name)
|
|
|
|
for (var e : cl.mapping.entrySet()) {
|
|
|
|
map.put(e.getValue(), e.getKey());
|
|
|
|
}
|
2022-02-13 03:32:11 -08:00
|
|
|
|
|
|
|
// Create the receiver Application
|
2022-02-13 07:24:34 -08:00
|
|
|
var app = (Application) cl.loadClass(appInfo.className)
|
2022-02-13 03:32:11 -08:00
|
|
|
.getConstructor(Object.class)
|
|
|
|
.newInstance(data.getObject());
|
|
|
|
|
|
|
|
// Create the receiver component factory
|
|
|
|
if (Build.VERSION.SDK_INT >= 28 && componentFactory != null) {
|
2022-02-13 07:24:34 -08:00
|
|
|
Object factory = cl.loadClass(appInfo.appComponentFactory).newInstance();
|
2022-02-13 03:32:11 -08:00
|
|
|
var delegate = (DelegateComponentFactory) componentFactory;
|
|
|
|
delegate.receiver = (AppComponentFactory) factory;
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:36:47 -07:00
|
|
|
activeClassLoader = cl;
|
2022-02-13 07:24:34 -08:00
|
|
|
|
2022-02-01 22:43:44 -08:00
|
|
|
// Send real application to attachBaseContext
|
|
|
|
attachContext(app, context);
|
2022-02-13 03:32:11 -08:00
|
|
|
|
|
|
|
return app;
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(DynLoad.class.getSimpleName(), "", e);
|
|
|
|
apk.delete();
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
2022-02-13 03:32:11 -08:00
|
|
|
|
2022-05-22 19:36:47 -07:00
|
|
|
activeClassLoader = new StubClassLoader(info);
|
2022-02-13 03:32:11 -08:00
|
|
|
return null;
|
2022-02-01 22:43:44 -08:00
|
|
|
}
|
2021-04-17 22:14:54 -07:00
|
|
|
|
2021-01-26 03:40:25 -08:00
|
|
|
// Replace LoadedApk mClassLoader
|
2022-02-01 22:43:44 -08:00
|
|
|
private static void replaceClassLoader(Context context) {
|
2021-12-13 03:57:27 -08:00
|
|
|
// Get ContextImpl
|
|
|
|
while (context instanceof ContextWrapper) {
|
|
|
|
context = ((ContextWrapper) context).getBaseContext();
|
|
|
|
}
|
|
|
|
|
2022-02-01 22:43:44 -08:00
|
|
|
try {
|
|
|
|
Field mInfo = context.getClass().getDeclaredField("mPackageInfo");
|
|
|
|
mInfo.setAccessible(true);
|
|
|
|
Object loadedApk = mInfo.get(context);
|
2022-03-02 23:16:47 +08:00
|
|
|
assert loadedApk != null;
|
2022-02-01 22:43:44 -08:00
|
|
|
Field mcl = loadedApk.getClass().getDeclaredField("mClassLoader");
|
|
|
|
mcl.setAccessible(true);
|
|
|
|
mcl.set(loadedApk, new DelegateClassLoader());
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Actually impossible as this method is only called on API < 29,
|
2022-02-02 05:06:12 -08:00
|
|
|
// and API 21 - 28 do not restrict access to these fields.
|
2022-02-01 22:43:44 -08:00
|
|
|
Log.e(DynLoad.class.getSimpleName(), "", e);
|
|
|
|
}
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
|
|
|
}
|