[core] clean

This commit is contained in:
vvb2060 2021-06-29 00:43:21 +08:00
parent 64c89d7fa7
commit 338c73bf10
14 changed files with 43 additions and 227 deletions

View File

@ -367,21 +367,13 @@ public final class XposedInit {
return false;
}
// module can load it's own so
StringBuilder nativePath = new StringBuilder();
// Compatible with applications with 32-bit native libraries only
if (android.os.Process.is64Bit()) {
for (String i : Build.SUPPORTED_64_BIT_ABIS) {
nativePath.append(apk).append("!/lib/").append(i).append(File.pathSeparator);
}
} else {
for (String i : Build.SUPPORTED_32_BIT_ABIS) {
nativePath.append(apk).append("!/lib/").append(i).append(File.pathSeparator);
}
var librarySearchPath = new StringBuilder();
var abis = Process.is64Bit() ? Build.SUPPORTED_64_BIT_ABIS : Build.SUPPORTED_32_BIT_ABIS;
for (String abi : abis) {
librarySearchPath.append(apk).append("!/lib/").append(abi).append(File.pathSeparator);
}
// Log.d(TAG, "Allowed native path" + nativePath.toString());
ClassLoader initLoader = XposedInit.class.getClassLoader();
ClassLoader mcl = new DelegateLastClassLoader(apk, nativePath.toString(), initLoader);
ClassLoader mcl = new DelegateLastClassLoader(apk, librarySearchPath.toString(), initLoader);
try {
if (mcl.loadClass(XposedBridge.class.getName()).getClassLoader() != initLoader) {

View File

@ -41,7 +41,6 @@ import org.lsposed.lspd.hooker.SystemMainHooker;
import org.lsposed.lspd.service.ServiceManager;
import org.lsposed.lspd.util.ModuleLogger;
import org.lsposed.lspd.util.Utils;
import org.lsposed.lspd.util.Versions;
import org.lsposed.lspd.yahfa.hooker.YahfaHooker;
import java.io.File;
@ -70,16 +69,6 @@ public class Main {
new LoadedApkCstrHooker());
}
public static void startSystemServerHook() {
StartBootstrapServicesHooker sbsHooker = new StartBootstrapServicesHooker();
Object[] paramTypesAndCallback = Versions.hasR() ?
new Object[]{"com.android.server.utils.TimingsTraceAndSlog", sbsHooker} :
new Object[]{sbsHooker};
XposedHelpers.findAndHookMethod("com.android.server.SystemServer",
SystemMainHooker.systemServerCL,
"startBootstrapServices", paramTypesAndCallback);
}
private static void installBootstrapHooks(boolean isSystem, String appDataDir) {
// Initialize the Xposed framework
try {

View File

@ -51,6 +51,8 @@ public class HandleBindAppHooker extends XC_MethodHook {
// save app process name here for later use
String appProcessName = (String) XposedHelpers.getObjectField(bindData, "processName");
String reportedPackageName = appInfo.packageName.equals("android") ? "system" : appInfo.packageName;
// Note: packageName="android" -> system_server process, ams pms etc;
// packageName="system" -> android pkg, system dialogues.
Utils.logD("processName=" + appProcessName +
", packageName=" + reportedPackageName + ", appDataDir=" + appDataDir);

View File

@ -32,11 +32,10 @@ import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.XposedInit;
// when a package is loaded for an existing process, trigger the callbacks as well
// ed: remove resources related hooking
public class LoadedApkCstrHooker extends XC_MethodHook {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
protected void afterHookedMethod(MethodHookParam param) {
Hookers.logD("LoadedApk#<init> starts");
try {

View File

@ -93,7 +93,7 @@ public class LoadedApkGetCLHooker extends XC_MethodHook {
IBinder binder = loadedApk.getApplicationInfo() != null ? serviceClient.requestManagerBinder(loadedApk.getApplicationInfo().packageName) : null;
if (binder != null) {
if (InstallerVerifier.verifyInstallerSignature(loadedApk.getApplicationInfo())) {
XposedInstallerHooker.hookXposedInstaller(lpparam.classLoader, binder);
InstallerVerifier.hookXposedInstaller(lpparam.classLoader, binder);
} else {
InstallerVerifier.hookXposedInstaller(classLoader);
}

View File

@ -32,7 +32,7 @@ import de.robv.android.xposed.callbacks.XC_LoadPackage;
public class StartBootstrapServicesHooker extends XC_MethodHook {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
protected void beforeHookedMethod(MethodHookParam param) {
logD("SystemServer#startBootstrapServices() starts");
try {

View File

@ -20,27 +20,33 @@
package org.lsposed.lspd.hooker;
import org.lsposed.lspd.core.Main;
import android.os.Build;
import org.lsposed.lspd.deopt.PrebuiltMethodsDeopter;
import org.lsposed.lspd.util.Hookers;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
// system_server initialization
// ed: only support sdk >= 21 for now
public class SystemMainHooker extends XC_MethodHook {
public static volatile ClassLoader systemServerCL;
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
protected void afterHookedMethod(MethodHookParam param) {
Hookers.logD("ActivityThread#systemMain() starts");
try {
// get system_server classLoader
systemServerCL = Thread.currentThread().getContextClassLoader();
// deopt methods in SYSTEMSERVERCLASSPATH
PrebuiltMethodsDeopter.deoptSystemServerMethods(systemServerCL);
Main.startSystemServerHook();
var sbsHooker = new StartBootstrapServicesHooker();
Object[] paramTypesAndCallback = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ?
new Object[]{"com.android.server.utils.TimingsTraceAndSlog", sbsHooker} :
new Object[]{sbsHooker};
XposedHelpers.findAndHookMethod("com.android.server.SystemServer",
systemServerCL, "startBootstrapServices", paramTypesAndCallback);
} catch (Throwable t) {
Hookers.logE("error when hooking systemMain", t);
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of LSPosed.
*
* LSPosed is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LSPosed is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 EdXposed Contributors
* Copyright (C) 2021 LSPosed Contributors
*/
package org.lsposed.lspd.hooker;
import android.os.IBinder;
import org.lsposed.lspd.util.Utils;
import de.robv.android.xposed.XposedHelpers;
public class XposedInstallerHooker {
public static void hookXposedInstaller(final ClassLoader classLoader, IBinder binder) {
Utils.logI("Found LSPosed Manager, hooking it");
try {
Class<?> serviceClass = XposedHelpers.findClass("org.lsposed.manager.receivers.LSPManagerServiceClient", classLoader);
XposedHelpers.setStaticObjectField(serviceClass, "binder", binder);
Utils.logI("Hooked LSPosed Manager");
} catch (Throwable t) {
Utils.logW("Could not hook LSPosed Manager", t);
}
}
}

View File

@ -26,6 +26,7 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;
import com.android.apksig.ApkVerifier;
@ -72,4 +73,16 @@ public class InstallerVerifier {
Utils.logW("hookXposedInstaller: ", t);
}
}
public static void hookXposedInstaller(final ClassLoader classLoader, IBinder binder) {
Utils.logI("Found LSPosed Manager, hooking it");
try {
Class<?> serviceClass = XposedHelpers.findClass("org.lsposed.manager.receivers.LSPManagerServiceClient", classLoader);
XposedHelpers.setStaticObjectField(serviceClass, "binder", binder);
Utils.logI("Hooked LSPosed Manager");
} catch (Throwable t) {
Utils.logW("Could not hook LSPosed Manager", t);
}
}
}

View File

@ -1,46 +0,0 @@
/*
* <!--This file is part of LSPosed.
*
* LSPosed is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LSPosed is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2021 LSPosed Contributors-->
*/
package org.lsposed.lspd.util;
import android.util.Log;
public class Logger {
private static final String TAG = "LSPosed";
public static int e(String msg) {
return Log.e(TAG, msg);
}
public static int e(String msg, Throwable e) {
return Log.e(TAG, msg, e);
}
public static int e(Throwable e) {
return Log.e(TAG, e.getMessage(), e);
}
public static int d(String msg) {
return Log.d(TAG, msg);
}
public static int w(String msg) {
return Log.w(TAG, msg);
}
}

View File

@ -1,50 +0,0 @@
/*
* This file is part of LSPosed.
*
* LSPosed is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LSPosed is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 EdXposed Contributors
* Copyright (C) 2021 LSPosed Contributors
*/
package org.lsposed.lspd.util;
public class ProxyClassLoader extends ClassLoader {
private final ClassLoader mClassLoader;
public ProxyClassLoader(ClassLoader parentCL, ClassLoader appCL) {
super(parentCL);
mClassLoader = appCL;
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class<?> clazz = null;
try {
clazz = mClassLoader.loadClass(name);
} catch (ClassNotFoundException ignored) {
}
if (clazz == null) {
clazz = super.loadClass(name, resolve);
if (clazz == null) {
throw new ClassNotFoundException();
}
}
return clazz;
}
}

View File

@ -1,34 +0,0 @@
/*
* This file is part of LSPosed.
*
* LSPosed is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LSPosed is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 EdXposed Contributors
* Copyright (C) 2021 LSPosed Contributors
*/
package org.lsposed.lspd.util;
import android.os.Build;
public class Versions {
public static boolean hasR() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R;
}
public static boolean hasQ() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
}
}

View File

@ -20,7 +20,7 @@
package org.lsposed.lspd.yahfa.dexmaker;
import org.lsposed.lspd.util.Logger;
import org.lsposed.lspd.util.Utils;
import java.lang.reflect.Executable;
import java.lang.reflect.InvocationTargetException;
@ -34,21 +34,20 @@ public final class DynamicBridge {
private static final ConcurrentHashMap<Executable, LspHooker> hookedInfo = new ConcurrentHashMap<>();
public static synchronized void hookMethod(Executable hookMethod, XposedBridge.AdditionalHookInfo additionalHookInfo) {
Logger.d("hooking " + hookMethod);
Utils.logD("hooking " + hookMethod);
if (hookedInfo.containsKey(hookMethod)) {
Logger.w("already hook method:" + hookMethod.toString());
Utils.logW("already hook method:" + hookMethod);
return;
}
Logger.d("start to generate class for: " + hookMethod);
Utils.logD("start to generate class for: " + hookMethod);
try {
final HookerDexMaker dexMaker = new HookerDexMaker();
dexMaker.start(hookMethod, additionalHookInfo,
hookMethod.getDeclaringClass().getClassLoader());
dexMaker.start(hookMethod, additionalHookInfo);
hookedInfo.put(hookMethod, dexMaker.getHooker());
} catch (Throwable e) {
Logger.e("error occur when generating dex.", e);
Utils.logE("error occur when generating dex.", e);
}
}
@ -64,5 +63,3 @@ public final class DynamicBridge {
return hooker.invokeOriginalMethod(thisObject, args);
}
}

View File

@ -22,8 +22,7 @@ package org.lsposed.lspd.yahfa.dexmaker;
import org.lsposed.lspd.core.yahfa.HookMain;
import org.lsposed.lspd.nativebridge.Yahfa;
import org.lsposed.lspd.util.Logger;
import org.lsposed.lspd.util.ProxyClassLoader;
import org.lsposed.lspd.util.Utils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
@ -57,7 +56,6 @@ public class HookerDexMaker {
private Executable mMember;
private XposedBridge.AdditionalHookInfo mHookInfo;
private ClassLoader mAppClassLoader;
private LspHooker mHooker;
private static Class<?>[] getParameterTypes(Executable method, boolean isStatic) {
@ -87,8 +85,7 @@ public class HookerDexMaker {
return descriptors;
}
public void start(Executable member, XposedBridge.AdditionalHookInfo hookInfo,
ClassLoader appClassLoader) throws Exception {
public void start(Executable member, XposedBridge.AdditionalHookInfo hookInfo) throws Exception {
if (member instanceof Method) {
Method method = (Method) member;
mReturnType = method.getReturnType();
@ -100,22 +97,15 @@ public class HookerDexMaker {
}
mMember = member;
mHookInfo = hookInfo;
if (appClassLoader == null
|| appClassLoader.getClass().getName().equals("java.lang.BootClassLoader")) {
mAppClassLoader = getClass().getClassLoader();
} else {
mAppClassLoader = appClassLoader;
mAppClassLoader = new ProxyClassLoader(mAppClassLoader, getClass().getClassLoader());
}
long startTime = System.nanoTime();
doMake(member instanceof Constructor ? "constructor" : member.getName());
long endTime = System.nanoTime();
Logger.d("Hook time: " + (endTime - startTime) / 1e6 + "ms");
Utils.logD("Hook time: " + (endTime - startTime) / 1e6 + "ms");
}
private void doMake(String methodName) throws Exception {
Class<?> hookClass = Yahfa.buildHooker(mAppClassLoader, getDescriptor(mReturnType), getDescriptors(mActualParameterTypes), methodName);
Class<?> hookClass = Yahfa.buildHooker(LspHooker.class.getClassLoader(), getDescriptor(mReturnType), getDescriptors(mActualParameterTypes), methodName);
// Execute our newly-generated code in-process.
Method backupMethod = hookClass.getMethod(METHOD_NAME_BACKUP, mActualParameterTypes);
mHooker = new LspHooker(mHookInfo, mMember, backupMethod);