Skip to content

Commit

Permalink
Check status for ALL zygotes
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Aug 9, 2021
1 parent 138bad6 commit cbeee7c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
7 changes: 2 additions & 5 deletions rirud/src/main/java/riru/Daemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public void binderDied() {
systemServerBinder.unlinkToDeath(this, 0);
systemServerBinder = null;

DaemonUtils.setIsLoaded(false, false);
DaemonUtils.setIsLoaded(true, false);
DaemonUtils.clearLoadedProcess();
DaemonUtils.getLoadedModules().clear();

DaemonUtils.writeStatus(R.string.zygote_dead);
Expand Down Expand Up @@ -149,9 +148,7 @@ private void startWait(boolean isFirst) {
}

synchronized (serverThread) {
// TODO: actually only consider one zygote
// how about one loaded Riru but the other (others) did not
if (!DaemonUtils.isLoaded(DaemonUtils.has64Bit())) {
if (!DaemonUtils.isLoaded()) {
onRiruNotLoaded(isFirst);
} else {
onRiruLoad();
Expand Down
11 changes: 6 additions & 5 deletions rirud/src/main/java/riru/DaemonSocketServerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ private void writeToFile(File parent, String name, String content) {
}
}

private void handleWriteStatus(LittleEndianDataInputStream in) throws IOException {
private void handleWriteStatus(LittleEndianDataInputStream in, Credentials credentials) throws IOException {
boolean is64Bit = in.readBoolean();
int count = in.readInt();

DaemonUtils.setIsLoaded(is64Bit, true);
DaemonUtils.recordLoadedProcess(credentials.getPid());

File parent = new File("/dev/riru" + (is64Bit ? "64" : "") + "_" + DaemonUtils.getDevRandom());
File modules = new File(parent, "modules");
Expand Down Expand Up @@ -264,7 +264,7 @@ private void handleReadModules(LittleEndianDataInputStream in, LittleEndianDataO
}
}

private void handleAction(LittleEndianDataInputStream in, LittleEndianDataOutputStream out, int action) throws IOException {
private void handleAction(LittleEndianDataInputStream in, LittleEndianDataOutputStream out, int action, Credentials credentials) throws IOException {
Log.i(TAG, "Action " + action);

switch (action) {
Expand All @@ -280,7 +280,7 @@ private void handleAction(LittleEndianDataInputStream in, LittleEndianDataOutput
}
case ACTION_WRITE_STATUS: {
Log.i(TAG, "Action: write status");
handleWriteStatus(in);
handleWriteStatus(in, credentials);
break;
}
case ACTION_READ_FILE: {
Expand Down Expand Up @@ -309,6 +309,7 @@ private void handleAction(LittleEndianDataInputStream in, LittleEndianDataOutput
private void handleSocket(LocalSocket socket, boolean privateConnection) throws IOException {
int action;
boolean first = true;
var credentials = socket.getPeerCredentials();

try (LittleEndianDataInputStream in = new LittleEndianDataInputStream(socket.getInputStream());
LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(socket.getOutputStream())) {
Expand All @@ -329,7 +330,7 @@ private void handleSocket(LocalSocket socket, boolean privateConnection) throws
Log.e(TAG, "Unauthorized connection using private action");
return;
}
handleAction(in, out, action);
handleAction(in, out, action, credentials);
first = false;
}
}
Expand Down
27 changes: 22 additions & 5 deletions rirud/src/main/java/riru/DaemonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class DaemonUtils {
private static int ppid = -1;
private static int magiskVersionCode = -1;
private static String magiskTmpfsPath;
private static final boolean[] loaded = new boolean[2];

public static Resources res;

Expand All @@ -67,6 +66,8 @@ public class DaemonUtils {
private static boolean isSELinuxEnforcing = false;
private static boolean fileContext = true;

private static final Set<Integer> zygotePid = Collections.newSetFromMap(new ConcurrentHashMap<>());

static {
originalNativeBridge = SystemProperties.get("ro.dalvik.vm.native.bridge");
if (TextUtils.isEmpty(originalNativeBridge)) {
Expand Down Expand Up @@ -123,12 +124,28 @@ public static void init(String[] args) {
magiskTmpfsPath = args[2];
}

public static boolean isLoaded(boolean is64Bit) {
return loaded[is64Bit ? 1 : 0];
public static boolean isLoaded() {
var processes = new File("/proc").listFiles((file, s) -> TextUtils.isDigitsOnly(s));
if (processes == null) {
Log.w(TAG, "Could not list all processes");
return false;
}
for (var process : processes) {
var pid = Integer.parseInt(process.getName());
if (Objects.equals(SELinux.getPidContext(pid), "u:r:zygote:s0") && !zygotePid.contains(pid)) {
Log.w(TAG, "Process " + pid + " has zygote context but did not load riru");
return false;
}
}
return true;
}

public static void clearLoadedProcess() {
zygotePid.clear();
}

public static void setIsLoaded(boolean is64Bit, boolean riruIsLoaded) {
DaemonUtils.loaded[is64Bit ? 1 : 0] = riruIsLoaded;
public static void recordLoadedProcess(int pid) {
zygotePid.add(pid);
}

public static Set<String> getLoadedModules() {
Expand Down

0 comments on commit cbeee7c

Please sign in to comment.