# --- T2-COPYRIGHT-BEGIN --- # t2/package/*/qemu/chroot.patch # Copyright (C) 2022 - 2026 The T2 SDE Project # SPDX-License-Identifier: GPL-2.0 or patched project license # --- T2-COPYRIGHT-END --- Quick and dirty patch for qemu user emulation how to chroot and drop priveledges, so you don't have to clutter a target filesystem with host code, figure out how to build qemu static in order to run a dynamic binary, or set Linux binfmt. - Rene Rebe --- qemu-8.0.0/linux-user/main.c.vanilla 2023-04-20 12:20:12.493209854 +0200 +++ qemu-8.0.0/linux-user/main.c 2023-04-20 12:22:14.849197124 +0200 @@ -422,6 +422,32 @@ } #endif +static void handle_arg_chroot(const char* arg) +{ + if (chdir(arg) || chroot(".")) { + fprintf(stderr, "Can't chroot into '%s'\n", arg); + _exit(1); + } +} + +static void handle_arg_su(const char* arg) +{ + int temp; + char *gid = strchr(arg, ':'); + if (gid) { + temp = atoi(++gid); + if (setresgid(temp, temp, temp)) { + fprintf(stderr, "Can't set gid to %d\n", temp); + _exit(1); + } + } + temp = atoi(arg); + if (setresuid(temp, temp, temp)) { + fprintf(stderr, "Can't set uid to %d\n", temp); + _exit(1); + } +} + struct qemu_argument { const char *argv; const char *env; @@ -548,6 +548,10 @@ "", "Generate a /tmp/perf-${pid}.map file for perf"}, {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump, "", "Generate a jit-${pid}.dump file for perf"}, + {"chroot", "", true, handle_arg_chroot, + "", "chroot into dir"}, + {"su", "", true, handle_arg_su, + "", "set numeric user and group IDs"}, {NULL, NULL, false, NULL, NULL, NULL} };