From abbca19c82ea73d885d2675690d7baa36190ed1e Mon Sep 17 00:00:00 2001 From: Nullptr Date: Sun, 22 Oct 2023 00:23:12 +0800 Subject: [PATCH] Implement uid_should_umount for magisk --- zygiskd/src/root_impl/magisk.rs | 41 +++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/zygiskd/src/root_impl/magisk.rs b/zygiskd/src/root_impl/magisk.rs index d5484611..22a5fe32 100644 --- a/zygiskd/src/root_impl/magisk.rs +++ b/zygiskd/src/root_impl/magisk.rs @@ -24,23 +24,40 @@ pub fn get_magisk() -> Option { } pub fn uid_granted_root(uid: i32) -> bool { - let output: Option = Command::new("magisk") + Command::new("magisk") .arg("--sqlite") - .arg("select uid from policies where policy=2") + .arg(format!("select 1 from policies where uid={uid} and policy=2 limit 1")) .stdout(Stdio::piped()) .spawn().ok() .and_then(|child| child.wait_with_output().ok()) - .and_then(|output| String::from_utf8(output.stdout).ok()); - let lines = match &output { - Some(output) => output.lines(), - None => return false, - }; - lines.into_iter().any(|line| { - line.trim().strip_prefix("uid=").and_then(|uid| uid.parse().ok()) == Some(uid) - }) + .and_then(|output| String::from_utf8(output.stdout).ok()) + .map(|output| output.is_empty()) == Some(false) } pub fn uid_should_umount(uid: i32) -> bool { - // TODO: uid_should_umount - return false; + let output = Command::new("pm") + .args(["list", "packages", "--uid", &uid.to_string()]) + .stdout(Stdio::piped()) + .spawn().ok() + .and_then(|child| child.wait_with_output().ok()) + .and_then(|output| String::from_utf8(output.stdout).ok()); + let line = match output { + Some(line) => line, + None => return false, + }; + let pkg = line + .strip_prefix("package:") + .and_then(|line| line.split(' ').next()); + let pkg = match pkg { + Some(pkg) => pkg, + None => return false, + }; + Command::new("magisk") + .arg("--sqlite") + .arg(format!("select 1 from denylist where package_name=\"{pkg}\" limit 1")) + .stdout(Stdio::piped()) + .spawn().ok() + .and_then(|child| child.wait_with_output().ok()) + .and_then(|output| String::from_utf8(output.stdout).ok()) + .map(|output| output.is_empty()) == Some(false) }