From f082d0735c66ac723de608c09fbdf66dd6814a72 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Tue, 22 Jun 2021 21:00:21 +0800 Subject: [PATCH] Disable profile by setting permission --- app/build.gradle | 1 + .../lspatch/loader/LSPApplication.java | 49 +++++++++++++++++++ .../lspatch/loader/util/FileUtils.java | 8 +-- core | 2 +- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d2b4a48..3e9a85d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -81,6 +81,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(path: ':lspcore') + implementation project(path: ':hiddenapi-bridge') compileOnly project(":hiddenapi-stubs") implementation project(':share') } diff --git a/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java b/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java index b94d573..9b7585f 100644 --- a/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java +++ b/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java @@ -15,6 +15,7 @@ import android.os.Environment; import android.os.IBinder; import android.os.Parcel; import android.os.ParcelFileDescriptor; +import android.system.Os; import android.util.Log; import org.json.JSONArray; @@ -39,7 +40,9 @@ import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.PosixFilePermissions; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -47,6 +50,7 @@ import java.util.Map; import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XposedBridge; import de.robv.android.xposed.XposedHelpers; +import hidden.HiddenApiBridge; /** * Created by Windysha @@ -99,6 +103,7 @@ public class LSPApplication extends ApplicationServiceClient { serviceClient = instance; try { + disableProfile(context); loadModules(context); Main.forkPostCommon(false, context.getDataDir().toString(), ActivityThread.currentProcessName()); doHook(context); @@ -110,6 +115,50 @@ public class LSPApplication extends ApplicationServiceClient { } } + public static void disableProfile(Context context) { + final ArrayList codePaths = new ArrayList<>(); + var appInfo = context.getApplicationInfo(); + var pkgName = context.getPackageName(); + if (appInfo == null) return; + if ((appInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) { + codePaths.add(appInfo.sourceDir); + } + if (appInfo.splitSourceDirs != null) { + Collections.addAll(codePaths, appInfo.splitSourceDirs); + } + + if (codePaths.isEmpty()) { + // If there are no code paths there's no need to setup a profile file and register with + // the runtime, + return; + } + + var profileDir = HiddenApiBridge.Environment_getDataProfilesDePackageDirectory(appInfo.uid / PER_USER_RANGE, pkgName); + + var attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------")); + + for (int i = codePaths.size() - 1; i >= 0; i--) { + String splitName = i == 0 ? null : appInfo.splitNames[i - 1]; + File curProfileFile = new File(profileDir, splitName == null ? "primary.prof" : splitName + ".split.prof").getAbsoluteFile(); + Log.d(TAG, "processing " + curProfileFile.getAbsolutePath()); + try { + if (curProfileFile.exists() && !curProfileFile.delete()) { + try (var writer = new FileOutputStream(curProfileFile)) { + Log.d(TAG, "failed to delete, try to clear content " + curProfileFile.getAbsolutePath()); + } catch (Throwable e) { + Log.e(TAG, "failed to delete and clear profile file " + curProfileFile.getAbsolutePath(), e); + } + Os.chmod(curProfileFile.getAbsolutePath(), 00400); + } else { + Files.createFile(curProfileFile.toPath(), attrs); + } + } catch (Throwable e) { + Log.e(TAG, "failed to disable profile file " + curProfileFile.getAbsolutePath(), e); + } + } + + } + public static void loadModules(Context context) { var configFile = new File(context.getExternalFilesDir(null), "lspatch.json"); var cacheDir = new File(context.getExternalCacheDir(), "modules"); diff --git a/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java b/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java index f6dc78d..43265cf 100644 --- a/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java +++ b/app/src/main/java/org/lsposed/lspatch/loader/util/FileUtils.java @@ -15,9 +15,7 @@ public class FileUtils { } try (InputStream is = context.getAssets().open(assetsFileName)) { return readTextFromInputStream(is); - } - catch (Exception e) { - e.printStackTrace(); + } catch (Throwable ignored) { } return null; } @@ -30,9 +28,7 @@ public class FileUtils { builder.append(str); } return builder.toString(); - } - catch (Exception e) { - e.printStackTrace(); + } catch (Throwable ignored) { } return null; } diff --git a/core b/core index 3acd29f..41d7f45 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 3acd29f99d3c8bd13fbad443dac739fa05bb0c7b +Subproject commit 41d7f456fbc6c14dc3dfe1448b72c9661ebf74ab