# --- T2-COPYRIGHT-NOTE-BEGIN --- # T2 SDE: package/*/qemu/chroot.patch # Copyright (C) 2022 - 2023 The T2 SDE Project # # This Copyright note is generated by scripts/Create-CopyPatch, # more information can be found in the files COPYING and README. # # This patch file is dual-licensed. It is available under the license the # patched project is licensed under, as long as it is an OpenSource license # as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms # of the GNU General Public License version 2 as used by the T2 SDE. # --- T2-COPYRIGHT-NOTE-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} };