2 https://resin.io/blog/building-arm-containers-on-any-x86-machine-even-dockerhub/
4 https://github.com/resin-io/qemu/commit/782e5bb77014ff136f7bb6133a911e5f53e914a7
6 https://github.com/resin-io/qemu/commit/782e5bb77014ff136f7bb6133a911e5f53e914a7#commitcomment-17193923
7 It has gone through review[1][2][3] and I'm waiting for the maintainer of the linux-user subsystem to accept it in his tree.
9 [1] https://patchwork.ozlabs.org/patch/569452/
10 [2] https://patchwork.ozlabs.org/patch/573877/
11 [3] https://patchwork.ozlabs.org/patch/582756/
13 From patchwork Mon Feb 15 05:51:47 2016
14 Content-Type: text/plain; charset="utf-8"
16 Content-Transfer-Encoding: 7bit
17 Subject: [v3] linux-user: add option to intercept execve() syscalls
18 From: Petros Angelatos <petrosagg@resin.io>
19 X-Patchwork-Id: 582756
20 Message-Id: <1455515507-26877-1-git-send-email-petrosagg@resin.io>
21 To: qemu-devel@nongnu.org
22 Cc: lucas.kaldstrom@hotmail.co.uk, peter.maydell@linaro.org,
24 laurent@vivier.eu, Petros Angelatos <petrosagg@resin.io>
25 Date: Sun, 14 Feb 2016 21:51:47 -0800
27 In order for one to use QEMU user mode emulation under a chroot, it is
28 required to use binfmt_misc. This can be avoided by QEMU never doing a
29 raw execve() to the host system.
31 Introduce a new option, -execve, that uses the current QEMU interpreter
32 to intercept execve().
34 qemu_execve() will prepend the interpreter path , similar to what
35 binfmt_misc would do, and then pass the modified execve() to the host.
37 It is necessary to parse hashbang scripts in that function otherwise
38 the kernel will try to run the interpreter of a script without QEMU and
39 get an invalid exec format error.
41 Signed-off-by: Petros Angelatos <petrosagg@resin.io>
42 Tested-by: Laurent Vivier <laurent@vivier.eu>
43 Reviewed-by: Laurent Vivier <laurent@vivier.eu>
46 - rebase the patchset against current code
48 diff --git a/linux-user/main.c b/linux-user/main.c
49 index ee12035..5951279 100644
50 --- a/linux-user/main.c
51 +++ b/linux-user/main.c
52 @@ -123,6 +123,7 @@ static void usage(int exitcode);
54 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
55 const char *qemu_uname_release;
56 +const char *qemu_execve_path;
58 #if !defined(TARGET_DEFAULT_STACK_SIZE)
59 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
60 @@ -362,6 +363,11 @@ static void handle_arg_guest_base(const
61 have_guest_base = true;
64 +static void handle_arg_execve(const char *arg)
66 + qemu_execve_path = strdup(arg);
69 static void handle_arg_reserved_va(const char *arg)
72 @@ -464,6 +470,8 @@ static const struct qemu_argument arg_ta
73 "uname", "set qemu uname release string to 'uname'"},
74 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base,
75 "address", "set guest_base address to 'address'"},
76 + {"execve", "QEMU_EXECVE", true, handle_arg_execve,
77 + "path", "use interpreter at 'path' when a process calls execve()"},
78 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
79 "size", "reserve 'size' bytes for guest virtual address space"},
80 {"d", "QEMU_LOG", true, handle_arg_log,
81 --- qemu-6.2.0/linux-user/user-internals.h.orig
82 +++ qemu-6.2.0/linux-user/user-internals.h
83 @@ -28,6 +28,7 @@ void init_task_state(TaskState *ts);
84 void task_settid(TaskState *);
85 void stop_all_tasks(void);
86 extern const char *qemu_uname_release;
87 +extern const char *qemu_execve_path;
88 extern unsigned long mmap_min_addr;
90 typedef struct IOCTLEntry IOCTLEntry;
91 --- qemu-8.1.5/linux-user/syscall.c.orig 2024-01-29 19:31:43.000000000 +0100
92 +++ qemu-8.1.5/linux-user/syscall.c 2024-05-06 19:05:26.075673131 +0200
93 @@ -8680,6 +8680,109 @@ ssize_t do_guest_readlink(const char *pa
97 +#define BINPRM_BUF_SIZE 128
99 +/* qemu_execve() Must return target values and target errnos. */
100 +static abi_long qemu_execve(char *filename, char *argv[],
103 + char *i_arg = NULL, *i_name = NULL;
105 + int argc, fd, ret, i, offset = 3;
107 + char buf[BINPRM_BUF_SIZE];
109 + for (argc = 0; argv[argc] != NULL; argc++) {
113 + fd = open(filename, O_RDONLY);
118 + ret = read(fd, buf, BINPRM_BUF_SIZE);
126 + /* adapted from the kernel
127 + * https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/binfmt_script.c
129 + if ((buf[0] == '#') && (buf[1] == '!')) {
131 + * This section does the #! interpretation.
132 + * Sorta complicated, but hopefully it will work. -TYT
135 + buf[BINPRM_BUF_SIZE - 1] = '\0';
136 + cp = strchr(buf, '\n');
138 + cp = buf+BINPRM_BUF_SIZE-1;
143 + if ((*cp == ' ') || (*cp == '\t')) {
149 + for (cp = buf+2; (*cp == ' ') || (*cp == '\t'); cp++) {
153 + return -ENOEXEC; /* No interpreter name found */
157 + for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
160 + while ((*cp == ' ') || (*cp == '\t')) {
174 + new_argp = alloca((argc + offset + 1) * sizeof(void *));
176 + /* Copy the original arguments with offset */
177 + for (i = 0; i < argc; i++) {
178 + new_argp[i + offset] = argv[i];
181 + new_argp[0] = strdup(qemu_execve_path);
182 + new_argp[1] = strdup("-0");
183 + new_argp[offset] = filename;
184 + new_argp[argc + offset] = NULL;
187 + new_argp[2] = i_name;
188 + new_argp[3] = i_name;
191 + new_argp[4] = i_arg;
194 + new_argp[2] = argv[0];
197 + return get_errno(safe_execve(qemu_execve_path, new_argp, envp));
200 static int do_execv(CPUArchState *cpu_env, int dirfd,
201 abi_long pathname, abi_long guest_argp,
202 abi_long guest_envp, int flags, bool is_execveat)
203 @@ -8765,9 +8868,12 @@ static int do_execv(CPUArchState *cpu_en
204 if (is_proc_myself(p, "exe")) {
208 - ? safe_execveat(dirfd, exe, argp, envp, flags)
209 - : safe_execve(exe, argp, envp);
211 + ret = safe_execveat(dirfd, exe, argp, envp, flags);
212 + else if (qemu_execve_path && *qemu_execve_path)
213 + ret = qemu_execve(exe, argp, envp);
215 + ret = safe_execve(exe, argp, envp);
216 ret = get_errno(ret);
218 unlock_user(p, pathname, 0);