Disable profile by setting permission

This commit is contained in:
LoveSy 2021-06-22 21:00:21 +08:00
parent de1ea7cdf6
commit f082d0735c
4 changed files with 53 additions and 7 deletions

View File

@ -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')
}

View File

@ -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<String> 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");

View File

@ -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;
}

2
core

@ -1 +1 @@
Subproject commit 3acd29f99d3c8bd13fbad443dac739fa05bb0c7b
Subproject commit 41d7f456fbc6c14dc3dfe1448b72c9661ebf74ab