mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-02 20:21:59 -05:00
* feat(native): run the desktop, its apps and shells as an ordinary user instead of fake root * fix(native): a script's interpreter takes DDNATV_BECOME, so sudo runs scripts as root * fix(desktop): keep the Plasma start line the pipewire tests slice the script at
173 lines
6.6 KiB
Python
173 lines
6.6 KiB
Python
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
from tools.tests.test_usr2_preload import runner
|
|
|
|
HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh"
|
|
|
|
# After the hook: prints the environment the hook hands the next program
|
|
RECORDER = r"""
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
int execve(const char *path, char *const argv[], char *const envp[]) {
|
|
(void)path; (void)argv;
|
|
for (int i = 0; envp[i]; i++) {
|
|
if (strncmp(envp[i], "DDNATV_USER=", 12) == 0 || strncmp(envp[i], "DDNATV_BECOME=", 14) == 0)
|
|
printf("env\t%d\t%s\n", i, envp[i]);
|
|
}
|
|
printf("exec\n");
|
|
fflush(stdout);
|
|
_exit(0);
|
|
}
|
|
"""
|
|
|
|
PROBE = r"""
|
|
#define _GNU_SOURCE
|
|
#include <errno.h>
|
|
#include <grp.h>
|
|
#include <pwd.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/syscall.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc > 1 && strcmp(argv[1], "exec") == 0) {
|
|
char *envp[] = {"PATH=/bin", argc > 2 ? argv[2] : NULL, NULL};
|
|
const char *program = argc > 3 ? argv[3] : "/bin/true";
|
|
char *args[] = {(char *)program, NULL};
|
|
execve(program, args, envp);
|
|
return 1;
|
|
}
|
|
printf("ids\t%u\t%u\t%u\t%u\n", getuid(), geteuid(), getgid(), getegid());
|
|
printf("syscall\t%ld\n", syscall(SYS_getuid));
|
|
struct passwd *pw = getpwuid(getuid());
|
|
printf("user\t%s\t%s\n", pw ? pw->pw_name : "-", pw ? pw->pw_dir : "-");
|
|
struct passwd *root = getpwnam("root");
|
|
printf("root\t%u\n", root ? root->pw_uid : 999);
|
|
struct group *gr = getgrgid(getgid());
|
|
printf("group\t%s\n", gr ? gr->gr_name : "-");
|
|
struct stat st;
|
|
stat("/bin/true", &st);
|
|
printf("owner\t%d\n", st.st_uid == getuid());
|
|
errno = 0;
|
|
int to_root = setuid(0);
|
|
printf("setuid0\t%d\t%d\n", to_root, errno);
|
|
printf("setuid_own\t%d\n", setuid(getuid()));
|
|
int sv[2];
|
|
socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
|
|
pid_t child = fork();
|
|
if (child == 0) { pause(); _exit(0); }
|
|
struct ucred cred;
|
|
socklen_t len = sizeof(cred);
|
|
getsockopt(sv[0], SOL_SOCKET, SO_PEERCRED, &cred, &len);
|
|
printf("peer\t%d\n", cred.uid == getuid());
|
|
kill(child, 9);
|
|
waitpid(child, NULL, 0);
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
|
|
class NativeHookUserModeTest(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.cc, cls.prefix = runner()
|
|
if cls.cc is None:
|
|
raise unittest.SkipTest("no aarch64 compiler and runner")
|
|
cls.tmp = tempfile.TemporaryDirectory(dir="/tmp")
|
|
tmp = Path(cls.tmp.name)
|
|
cls.hook = tmp / "hook.so"
|
|
subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True)
|
|
(tmp / "recorder.c").write_text(RECORDER)
|
|
cls.recorder = tmp / "recorder.so"
|
|
subprocess.run([cls.cc, "-shared", "-fPIC", "-O1", "-o", str(cls.recorder), str(tmp / "recorder.c")], check=True)
|
|
(tmp / "probe.c").write_text(PROBE)
|
|
cls.probe = tmp / "probe"
|
|
subprocess.run([cls.cc, "-O1", "-o", str(cls.probe), str(tmp / "probe.c")], check=True)
|
|
cls.root = tmp / "root"
|
|
(cls.root / "bin").mkdir(parents=True)
|
|
(cls.root / "etc").mkdir()
|
|
(cls.root / "etc/passwd").write_text(f"root:x:{os.getuid()}:{os.getgid()}:root:/root:/bin/bash\nroot:x:0:0:root:/root:/bin/bash\n")
|
|
(cls.root / "bin/true").write_text("")
|
|
(cls.root / "bin/true").chmod(0o755)
|
|
(cls.root / "bin/script").write_text("#!/bin/true\n")
|
|
(cls.root / "bin/script").chmod(0o755)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.tmp.cleanup()
|
|
|
|
def run_probe(self, *args, user=None):
|
|
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": f"{self.hook}:{self.recorder}"}
|
|
if user:
|
|
hook_env["DDNATV_USER"] = user
|
|
env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_ROOT", "DDNATV_USER")}
|
|
if user:
|
|
# A peer's user is the first entry of /proc/PID/environ, which under qemu is qemu's own
|
|
env = {"DDNATV_USER": user, **env}
|
|
if self.prefix:
|
|
cmd = list(self.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
|
|
else:
|
|
cmd = []
|
|
env.update(hook_env)
|
|
out = subprocess.run(cmd + [str(self.probe), *args], env=env, capture_output=True, text=True, timeout=120)
|
|
result = {}
|
|
for line in out.stdout.splitlines():
|
|
key, *values = line.split("\t")
|
|
result.setdefault(key, []).append(values)
|
|
self.assertTrue(result, out.stdout + out.stderr)
|
|
return result
|
|
|
|
def test_programs_are_root_by_default(self):
|
|
r = self.run_probe()
|
|
self.assertEqual([["0", "0", "0", "0"]], r["ids"])
|
|
self.assertEqual([["0"]], r["syscall"])
|
|
self.assertEqual("root", r["user"][0][0])
|
|
self.assertEqual([["1"]], r["owner"])
|
|
self.assertEqual([["0", "0"]], r["setuid0"])
|
|
self.assertEqual([["1"]], r["peer"])
|
|
|
|
def test_the_app_user_sees_its_real_ids_and_cannot_become_root(self):
|
|
uid, gid = os.getuid(), os.getgid()
|
|
if uid == 0:
|
|
self.skipTest("the app's user is root's uid on this runner, so root's ids are its own")
|
|
r = self.run_probe(user="app")
|
|
self.assertEqual([[str(uid), str(uid), str(gid), str(gid)]], r["ids"])
|
|
self.assertEqual([[str(uid)]], r["syscall"])
|
|
self.assertEqual([["deck", "/root"]], r["user"])
|
|
self.assertEqual([["deck"]], r["group"])
|
|
self.assertEqual([["0"]], r["root"])
|
|
self.assertEqual([["1"]], r["owner"])
|
|
self.assertEqual([["-1", "1"]], r["setuid0"])
|
|
self.assertEqual([["0"]], r["setuid_own"])
|
|
self.assertEqual([["1"]], r["peer"])
|
|
|
|
def test_programs_an_app_user_starts_stay_that_user_whatever_their_environment(self):
|
|
r = self.run_probe("exec", "DDNATV_USER=root", user="app")
|
|
self.assertEqual([["0", "DDNATV_USER=app"]], r["env"])
|
|
|
|
def test_become_switches_the_started_program_once(self):
|
|
self.assertNotIn("env", self.run_probe("exec", "DDNATV_BECOME=root", user="app"))
|
|
self.assertEqual([["0", "DDNATV_USER=app"]], self.run_probe("exec", "DDNATV_BECOME=app")["env"])
|
|
|
|
def test_become_reaches_a_scripts_interpreter(self):
|
|
self.assertNotIn("env", self.run_probe("exec", "DDNATV_BECOME=root", "/bin/script", user="app"))
|
|
self.assertEqual([["0", "DDNATV_USER=app"]], self.run_probe("exec", "DDNATV_BECOME=app", "/bin/script")["env"])
|
|
|
|
def test_root_never_passes_an_app_user_on(self):
|
|
self.assertNotIn("env", self.run_probe("exec", "DDNATV_USER=app"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|