diff --git a/tools/linuxfs/desktop/droiddeck-desktop-gpu b/tools/linuxfs/desktop/droiddeck-desktop-gpu index a0173f7b6d..a25e21e366 100644 --- a/tools/linuxfs/desktop/droiddeck-desktop-gpu +++ b/tools/linuxfs/desktop/droiddeck-desktop-gpu @@ -1,7 +1,6 @@ #!/usr/bin/python3 import os import re -import shutil HOME = os.environ.get("HOME", "/root") SYSTEM_DIR = "/usr/share/applications" @@ -10,8 +9,6 @@ FLATPAK_DIR = os.path.join(HOME, ".local/share/flatpak/exports/share/application USER_DIR = os.path.join(HOME, ".local/share/applications") MARK = "X-DroidDeck-GPU=1" SKIP = re.compile(r"^(steam|droiddeck-game-|droiddeck-steam|steamdeck-)", re.I) -CHROMIUM_ENV = "env -u WAYLAND_DISPLAY XDG_SESSION_TYPE=x11 ELECTRON_DISABLE_SANDBOX=1 " -CHROMIUM_FLAGS = " --no-sandbox --ozone-platform=x11" def enabled(): @@ -27,33 +24,6 @@ def main_group(text): return lines[:start + 1], lines[start + 1:end], lines[end:] -def split_program(exec_line): - if exec_line.startswith('"'): - end = exec_line.find('"', 1) - if end < 0: - return None, None - return exec_line[:end + 1], exec_line[end + 1:] - program, _, rest = exec_line.partition(" ") - return program, (" " + rest) if rest else "" - - -def chromium(exec_line): - program, _ = split_program(exec_line) - if not program: - return False - program = program.strip('"') - path = program if os.path.isabs(program) else shutil.which(program) - if not path: - return False - here = os.path.dirname(os.path.realpath(path)) - return any(os.path.exists(os.path.join(d, "chrome-sandbox")) for d in (here, os.path.dirname(here))) - - -def chromium_exec(exec_line): - program, rest = split_program(exec_line) - return CHROMIUM_ENV + program + CHROMIUM_FLAGS + rest - - def gpu_exec(exec_line): if exec_line.startswith("/usr/bin/flatpak run ") or exec_line.startswith("flatpak run "): exec_line = "/usr/local/bin/droiddeck-flatpak-run " + exec_line.split(" run ", 1)[1] @@ -75,24 +45,8 @@ def wrap(text, gpu_on=True): if not exec_line or exec_line.startswith("droiddeck-gpu "): return None cats = set(filter(None, keys.get("Categories", "").split(";"))) - gpu = gpu_on and bool(cats & {"Game", "Emulator"}) - browser = chromium(exec_line) - if not gpu and not browser: + if not (gpu_on and cats & {"Game", "Emulator"}): return None - if browser: - exec_line = chromium_exec(exec_line) - if not gpu: - def fix(lines): - return ["Exec=" + chromium_exec(l[5:].strip()) if l.startswith("Exec=") and chromium(l[5:].strip()) else l - for l in lines] - out_group = fix(group) - while out_group and not out_group[-1].strip(): - out_group.pop() - out_group.append(MARK) - out_rest = fix(rest) - if out_rest and out_rest[0].strip(): - out_rest.insert(0, "") - return "\n".join(head + out_group + out_rest) + "\n" out_group = [] actions = keys.get("Actions", "") for l in group: @@ -111,8 +65,7 @@ def wrap(text, gpu_on=True): wrapped_rest = [] for l in rest: if l.startswith("Exec=") and not l[5:].startswith("droiddeck-gpu "): - action = l[5:].strip() - l = "Exec=" + gpu_exec(chromium_exec(action) if chromium(action) else action) + l = "Exec=" + gpu_exec(l[5:].strip()) wrapped_rest.append(l) if wrapped_rest and wrapped_rest[0].strip(): wrapped_rest.insert(0, "") diff --git a/tools/linuxfs/preload/binfmt.c b/tools/linuxfs/preload/binfmt.c index 50b5dd616f..484a81094b 100644 --- a/tools/linuxfs/preload/binfmt.c +++ b/tools/linuxfs/preload/binfmt.c @@ -21,6 +21,29 @@ typedef int (*spawn_fn)(pid_t *, const char *, const posix_spawn_file_actions_t __attribute__((visibility("hidden"))) char *const *dd_webhelper_argv(const char *path, char *const argv[], char *const envp[]); +__attribute__((visibility("hidden"))) int dd_program_lookup(const char *file, char *out, size_t size) { + if (!file || !*file) return 0; + if (strchr(file, '/')) return snprintf(out, size, "%s", file) < (int)size; + const char *paths = getenv("PATH"); + if (!paths) paths = "/bin:/usr/bin"; + char candidate[PATH_MAX]; + int found = 0; + for (const char *p = paths;; ) { + const char *end = strchrnul(p, ':'); + int n = end > p ? snprintf(candidate, sizeof candidate, "%.*s/%s", (int)(end - p), p, file) + : snprintf(candidate, sizeof candidate, "%s", file); + struct stat st; + if (n > 0 && n < (int)sizeof candidate && n < (int)size && stat(candidate, &st) == 0 && S_ISREG(st.st_mode)) { + int runs = access(candidate, X_OK) == 0; + if (runs || !found) memcpy(out, candidate, (size_t)n + 1); + found = 1; + if (runs) return 1; + } + if (!*end) return found; + p = end + 1; + } +} + #if defined(__aarch64__) || defined(BINFMT_TEST) static int flag(const char *name) { @@ -67,21 +90,6 @@ static int wanted(const char *path, int error) { return (error == ENOEXEC || error == ENOENT || error == EACCES) && handing_off() && foreign(path, error); } -static int lookup(const char *file, char *out, size_t size) { - if (!file || !*file) return 0; - if (strchr(file, '/')) return snprintf(out, size, "%s", file) < (int)size; - const char *paths = getenv("PATH"); - if (!paths) paths = "/bin:/usr/bin"; - for (const char *p = paths;; ) { - const char *end = strchrnul(p, ':'); - int n = end > p ? snprintf(out, size, "%.*s/%s", (int)(end - p), p, file) : snprintf(out, size, "%s", file); - struct stat st; - if (n > 0 && n < (int)size && stat(out, &st) == 0 && S_ISREG(st.st_mode)) return 1; - if (!*end) return 0; - p = end + 1; - } -} - static int count(char *const argv[]) { int n = 0; while (argv && argv[n]) n++; @@ -126,12 +134,12 @@ __attribute__((visibility("hidden"))) int dd_binfmt_retry(const char *path, char __attribute__((visibility("hidden"))) void dd_binfmt_before_search(const char *file, char *const argv[], char *const envp[]) { char path[PATH_MAX]; - if (handing_off() && lookup(file, path, sizeof path)) dd_binfmt_before(path, argv, envp); + if (handing_off() && dd_program_lookup(file, path, sizeof path)) dd_binfmt_before(path, argv, envp); } __attribute__((visibility("hidden"))) int dd_binfmt_after_search(const char *file, char *const argv[], char *const envp[], int error) { char path[PATH_MAX]; - if (handing_off() && lookup(file, path, sizeof path)) return dd_binfmt_retry(path, argv, envp, error); + if (handing_off() && dd_program_lookup(file, path, sizeof path)) return dd_binfmt_retry(path, argv, envp, error); errno = error; return -1; } @@ -153,7 +161,7 @@ __attribute__((visibility("hidden"))) int dd_binfmt_spawn(pid_t *pid, const char int execvpe(const char *file, char *const argv[], char *const envp[]) { char resolved[PATH_MAX]; - const char *path = lookup(file, resolved, sizeof resolved) ? resolved : file; + const char *path = dd_program_lookup(file, resolved, sizeof resolved) ? resolved : file; char *const *adjusted = dd_webhelper_argv(path, argv, envp); static int (*real)(const char *, char *const[], char *const[]); if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execvpe"); @@ -165,7 +173,7 @@ int execvpe(const char *file, char *const argv[], char *const envp[]) { int posix_spawnp(pid_t *pid, const char *file, const posix_spawn_file_actions_t *fa, const posix_spawnattr_t *attr, char *const argv[], char *const envp[]) { char path[PATH_MAX]; - int found = lookup(file, path, sizeof path); + int found = dd_program_lookup(file, path, sizeof path); char *const *adjusted = dd_webhelper_argv(found ? path : file, argv, envp); static spawn_fn real, spawn; if (!real) real = (spawn_fn)dlsym(RTLD_NEXT, "posix_spawnp"); diff --git a/tools/linuxfs/preload/net.c b/tools/linuxfs/preload/net.c index de3cfdaee7..388b92da6a 100644 --- a/tools/linuxfs/preload/net.c +++ b/tools/linuxfs/preload/net.c @@ -277,6 +277,7 @@ __attribute__((visibility("hidden"))) void dd_binfmt_before_search(const char *f __attribute__((visibility("hidden"))) int dd_binfmt_after_search(const char *file, char *const argv[], char *const envp[], int error); __attribute__((visibility("hidden"))) char *const *dd_webhelper_argv(const char *path, char *const argv[], char *const envp[]); +__attribute__((visibility("hidden"))) int dd_program_lookup(const char *file, char *out, size_t size); __attribute__((visibility("hidden"))) int dd_binfmt_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *fa, const posix_spawnattr_t *attr, char *const argv[], char *const envp[], spawn_fn real); @@ -377,7 +378,8 @@ static int answer_lsof_via_spawn(const char *path, char *const argv[], pid_t *pi } int execvp(const char *file, char *const argv_in[]) { - char *const *argv = dd_webhelper_argv(file, argv_in, environ); + char resolved[PATH_MAX]; + char *const *argv = dd_webhelper_argv(dd_program_lookup(file, resolved, sizeof resolved) ? resolved : file, argv_in, environ); static int (*real)(const char *, char *const[]); if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execvp"); maybe_answer_lsof(file, argv); diff --git a/tools/linuxfs/preload/webhelper.c b/tools/linuxfs/preload/webhelper.c index 81aba8e8a5..5263b0f464 100644 --- a/tools/linuxfs/preload/webhelper.c +++ b/tools/linuxfs/preload/webhelper.c @@ -26,40 +26,64 @@ static char *join_features(const char *have, const char *add) { return strcat(out, add); } -static int electron_node_mode(char *const envp[]) { - static const char variable[] = "ELECTRON_RUN_AS_NODE="; +static const char *env_value(char *const envp[], const char *variable) { + size_t n = strlen(variable); for (int i = 0; envp && envp[i]; i++) - if (!strncmp(envp[i], variable, sizeof variable - 1)) - return envp[i][sizeof variable - 1] != 0; - return 0; + if (!strncmp(envp[i], variable, n) && envp[i][n] == '=') return envp[i] + n + 1; + return NULL; } -static char *const *chromium_argv(const char *path, char *const argv[]) { - const char *slash = path ? strrchr(path, '/') : NULL; - if (!slash || !argv || !argv[0]) return argv; - int argc = 0; - for (; argv[argc]; argc++) - if (argc && (!strncmp(argv[argc], "--type=", 7) || !strcmp(argv[argc], "--no-sandbox"))) return argv; - size_t dir = (size_t)(slash - path) + 1; - char *sandbox = malloc(dir + sizeof "chrome-sandbox"); - if (!sandbox) return argv; - memcpy(sandbox, path, dir); - strcpy(sandbox + dir, "chrome-sandbox"); - int chromium = access(sandbox, F_OK) == 0; - free(sandbox); - if (!chromium) return argv; - char **out = calloc((size_t)argc + 2, sizeof *out); +static int electron_node_mode(char *const envp[]) { + const char *mode = env_value(envp, "ELECTRON_RUN_AS_NODE"); + return mode && *mode; +} + +static int beside_chrome_sandbox(const char *path) { + char *real = realpath(path, NULL); + if (!real) return 0; + int found = 0; + for (int up = 0; up < 2 && !found; up++) { + char *slash = strrchr(real, '/'); + if (!slash) break; + *slash = 0; + size_t dir = strlen(real); + char *sandbox = malloc(dir + sizeof "/chrome-sandbox"); + if (!sandbox) break; + memcpy(sandbox, real, dir); + strcpy(sandbox + dir, "/chrome-sandbox"); + found = access(sandbox, F_OK) == 0; + free(sandbox); + } + free(real); + return found; +} + +static char *const *chromium_argv(const char *path, char *const argv[], char *const envp[]) { + if (!path || !argv || !argv[0]) return argv; + int argc = 0, sandbox = 1, ozone = 0; + for (; argv[argc]; argc++) { + if (!argc) continue; + if (!strncmp(argv[argc], "--type=", 7)) return argv; + if (!strcmp(argv[argc], "--no-sandbox")) sandbox = 0; + if (!strncmp(argv[argc], "--ozone-platform=", 17)) ozone = 1; + } + const char *display = env_value(envp, "DISPLAY"); + int x11 = !ozone && display && *display; + if ((!sandbox && !x11) || !beside_chrome_sandbox(path)) return argv; + char **out = calloc((size_t)argc + 3, sizeof *out); if (!out) return argv; - out[0] = argv[0]; - out[1] = (char *)"--no-sandbox"; - for (int i = 1; i < argc; i++) out[i + 1] = argv[i]; + int n = 0; + out[n++] = argv[0]; + if (sandbox) out[n++] = (char *)"--no-sandbox"; + if (x11) out[n++] = (char *)"--ozone-platform=x11"; + for (int i = 1; i < argc; i++) out[n++] = argv[i]; return out; } __attribute__((visibility("hidden"))) char *const *dd_webhelper_argv(const char *path, char *const argv[], char *const envp[]) { if (electron_node_mode(envp)) return argv; - if (!is_webhelper_browser(path, argv)) return chromium_argv(path, argv); + if (!is_webhelper_browser(path, argv)) return chromium_argv(path, argv, envp); const char *spec = getenv("DD_WEBHELPER_ARGS"); if (!spec || !*spec) spec = WEBHELPER_DEFAULT; if (!strcmp(spec, "stock")) return argv; diff --git a/tools/tests/test_binfmt_preload.py b/tools/tests/test_binfmt_preload.py index 04b21b6021..38c2dc4038 100644 --- a/tools/tests/test_binfmt_preload.py +++ b/tools/tests/test_binfmt_preload.py @@ -209,6 +209,24 @@ class BinfmtPreloadTest(unittest.TestCase): code = self.chromium_script() self.assertEqual(self.run_program("execve", code, "--version"), (0, "--no-sandbox\n--version", None)) + def test_a_chromium_app_named_on_path_receives_no_sandbox(self): + (self.dir / "code").symlink_to(self.chromium_script()) + for how in ("execvp", "execvpe", "spawnp"): + out = self.run_program(how, "code", "--version") + self.assertEqual(out[1].splitlines()[:2], ["--no-sandbox", "--version"], how) + + def test_a_path_match_that_cannot_run_does_not_decide_the_flags(self): + stale = self.dir / "stale" + stale.mkdir() + (stale / "util").write_text("#!/bin/sh\n") + (stale / "chrome-sandbox").touch() + util = self.dir / "util" + util.write_text("#!/bin/sh\nprintf '%s\\n' \"$@\"\n") + util.chmod(0o755) + for how in ("execvp", "execvpe", "spawnp"): + out = self.run_program(how, "util", "--version", PATH=f"{stale}:{self.dir}:/usr/bin:/bin") + self.assertEqual(out[1].splitlines()[:1], ["--version"], how) + def test_the_hand_off_switch_works_without_the_desktop(self): tool = self.x86() for how in ("execve", "execvp"): diff --git a/tools/tests/test_desktop_chromium_apps.py b/tools/tests/test_desktop_chromium_apps.py index f8d6056d21..8f5525d6d3 100644 --- a/tools/tests/test_desktop_chromium_apps.py +++ b/tools/tests/test_desktop_chromium_apps.py @@ -1,11 +1,8 @@ -import os from pathlib import Path import tempfile import unittest SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/desktop/droiddeck-desktop-gpu" -PREFIX = "env -u WAYLAND_DISPLAY XDG_SESSION_TYPE=x11 ELECTRON_DISABLE_SANDBOX=1 " -FLAGS = " --no-sandbox --ozone-platform=x11" def load(): @@ -24,12 +21,6 @@ class DesktopChromiumAppsTest(unittest.TestCase): self.gpu = load() self.electron = self.app("lib/chat/chat", sandbox=True) self.plain = self.app("lib/editor/editor", sandbox=False) - bin_dir = self.root / "bin" - bin_dir.mkdir() - (bin_dir / "chat").symlink_to(self.electron) - old_path = os.environ.get("PATH", "") - os.environ["PATH"] = "%s:%s" % (bin_dir, old_path) - self.addCleanup(os.environ.__setitem__, "PATH", old_path) def app(self, relative, sandbox): path = self.root / relative @@ -49,34 +40,17 @@ class DesktopChromiumAppsTest(unittest.TestCase): def exec_lines(self, text): return [l[5:] for l in text.splitlines() if l.startswith("Exec=")] - def test_an_electron_app_starts_without_the_sandbox_on_x11(self): - out = self.gpu["wrap"](self.entry("%s %%U" % self.electron, actions="%s --new" % self.electron)) - self.assertEqual(self.exec_lines(out), [PREFIX + str(self.electron) + FLAGS + " %U", - PREFIX + str(self.electron) + FLAGS + " --new"]) - self.assertIn("X-DroidDeck-GPU=1", out) - self.assertIn("Actions=new;", out) - self.assertNotIn("droiddeck-window", out) + def test_electron_apps_keep_their_own_entries(self): + self.assertIsNone(self.gpu["wrap"](self.entry("%s %%U" % self.electron, actions="%s --new" % self.electron))) + self.assertIsNone(self.gpu["wrap"](self.entry(str(self.plain)))) - def test_a_command_on_the_path_is_followed_to_its_install_folder(self): - out = self.gpu["wrap"](self.entry("chat %U")) - self.assertEqual(self.exec_lines(out), [PREFIX + "chat" + FLAGS + " %U"]) + def test_an_electron_game_goes_to_the_gpu_as_installed(self): + out = self.gpu["wrap"](self.entry(str(self.electron), categories="Game;", actions="%s --new" % self.electron)) + self.assertEqual(self.exec_lines(out), ["droiddeck-gpu " + str(self.electron), str(self.electron), + "droiddeck-gpu %s --new" % self.electron]) - def test_a_quoted_program_keeps_its_quotes(self): - out = self.gpu["wrap"](self.entry('"%s" %%U' % self.electron)) - self.assertEqual(self.exec_lines(out), [PREFIX + '"%s"' % self.electron + FLAGS + " %U"]) - - def test_other_apps_are_left_alone(self): - self.assertIsNone(self.gpu["wrap"](self.entry("%s %%F" % self.plain))) - self.assertIsNone(self.gpu["wrap"](self.entry("missing-program %F"))) - - def test_an_electron_game_goes_to_the_gpu_and_keeps_a_window_action(self): - out = self.gpu["wrap"](self.entry(str(self.electron), categories="Game;")) - fixed = PREFIX + str(self.electron) + FLAGS - self.assertEqual(self.exec_lines(out), ["droiddeck-gpu " + fixed, fixed]) - - def test_with_the_gpu_off_electron_apps_are_still_fixed(self): - out = self.gpu["wrap"](self.entry(str(self.electron), categories="Game;"), False) - self.assertEqual(self.exec_lines(out), [PREFIX + str(self.electron) + FLAGS]) + def test_with_the_gpu_off_no_entry_is_written(self): + self.assertIsNone(self.gpu["wrap"](self.entry(str(self.electron), categories="Game;"), False)) self.assertIsNone(self.gpu["wrap"](self.entry(str(self.plain), categories="Game;"), False)) def test_games_that_are_not_chromium_are_unchanged(self): diff --git a/tools/tests/test_webhelper_preload.py b/tools/tests/test_webhelper_preload.py index 3847e9988f..24e854dbb8 100644 --- a/tools/tests/test_webhelper_preload.py +++ b/tools/tests/test_webhelper_preload.py @@ -15,8 +15,11 @@ int main(int argc, char **argv) { const char *mode = getenv("TEST_NODE_MODE_CHILD"); static char node[64]; snprintf(node, sizeof node, "ELECTRON_RUN_AS_NODE=%s", mode ? mode : ""); - char *node_env[] = {node, NULL}; - char *plain_env[] = {"PATH=/usr/bin", NULL}; + const char *x = getenv("TEST_DISPLAY"); + static char display[64]; + snprintf(display, sizeof display, "DISPLAY=%s", x ? x : ""); + char *node_env[] = {node, x ? display : NULL, NULL}; + char *plain_env[] = {"PATH=/usr/bin", x ? display : NULL, NULL}; char *const *envp = mode ? node_env : plain_env; char *const *out = dd_webhelper_argv(argv[1], argv + 1, (char *const *)envp); for (int i = 0; out[i]; i++) printf("%s\n", out[i]); @@ -42,8 +45,10 @@ class WebhelperPreloadTest(unittest.TestCase): def tearDownClass(cls): shutil.rmtree(cls.tmp, ignore_errors=True) - def argv(self, *args, spec=None, node_child=False, parent_node=False): - env = {k: v for k, v in os.environ.items() if k != "DD_WEBHELPER_ARGS"} + def argv(self, *args, spec=None, node_child=False, parent_node=False, display=None): + env = {k: v for k, v in os.environ.items() if k not in ("DD_WEBHELPER_ARGS", "TEST_DISPLAY")} + if display is not None: + env["TEST_DISPLAY"] = display env.pop("ELECTRON_RUN_AS_NODE", None) if spec is not None: env["DD_WEBHELPER_ARGS"] = spec @@ -104,6 +109,34 @@ class WebhelperPreloadTest(unittest.TestCase): chrome = self.chromium("chromium-parent-mode", "chrome") self.assertEqual(self.argv(chrome, parent_node=True), [chrome, "--no-sandbox"]) + def test_a_link_to_an_electron_app_gets_the_sandbox_flag(self): + app = self.chromium("vesktop", "vesktop") + link = self.tmp / "bin-vesktop" + link.symlink_to(app) + self.assertEqual(self.argv(str(link), "%U"), [str(link), "--no-sandbox", "%U"]) + + def test_an_app_whose_sandbox_helper_is_one_folder_up_gets_the_flag(self): + root = self.tmp / "nested" + (root / "bin").mkdir(parents=True) + (root / "chrome-sandbox").touch() + (root / "bin/app").touch() + self.assertEqual(self.argv(str(root / "bin/app")), [str(root / "bin/app"), "--no-sandbox"]) + + def test_with_an_x_display_chromium_apps_use_x11(self): + chrome = self.chromium("chromium-x11", "chrome") + self.assertEqual(self.argv(chrome, "--new-window", display=":0"), + [chrome, "--no-sandbox", "--ozone-platform=x11", "--new-window"]) + self.assertEqual(self.argv(chrome, "--no-sandbox", display=":0"), + [chrome, "--ozone-platform=x11", "--no-sandbox"]) + + def test_an_explicit_platform_and_sandbox_choice_is_kept(self): + chrome = self.chromium("chromium-own-flags", "chrome") + own = [chrome, "--no-sandbox", "--ozone-platform=wayland"] + self.assertEqual(self.argv(*own, display=":0"), own) + + def test_other_programs_with_an_x_display_are_left_alone(self): + self.assertEqual(self.argv("/usr/bin/steam", display=":0"), ["/usr/bin/steam"]) + def test_stock_turns_it_off(self): args = [WEBHELPER, "--enable-features=A"] self.assertEqual(self.argv(*args, spec="stock"), args)