]> git.pld-linux.org Git - packages/qemu.git/blame - qemu-user-execve.patch
update qemu-user-execve.patch
[packages/qemu.git] / qemu-user-execve.patch
CommitLineData
f1a1313b 1Discussion:
a5affa61 2https://resin.io/blog/building-arm-containers-on-any-x86-machine-even-dockerhub/
f1a1313b 3
a5affa61
ER
4https://github.com/resin-io/qemu/commit/782e5bb77014ff136f7bb6133a911e5f53e914a7
5
102e18f7
ER
6https://github.com/resin-io/qemu/commit/782e5bb77014ff136f7bb6133a911e5f53e914a7#commitcomment-17193923
7It 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.
8
9[1] https://patchwork.ozlabs.org/patch/569452/
10[2] https://patchwork.ozlabs.org/patch/573877/
11[3] https://patchwork.ozlabs.org/patch/582756/
12
13From patchwork Mon Feb 15 05:51:47 2016
14Content-Type: text/plain; charset="utf-8"
15MIME-Version: 1.0
16Content-Transfer-Encoding: 7bit
17Subject: [v3] linux-user: add option to intercept execve() syscalls
a5affa61 18From: Petros Angelatos <petrosagg@resin.io>
102e18f7
ER
19X-Patchwork-Id: 582756
20Message-Id: <1455515507-26877-1-git-send-email-petrosagg@resin.io>
21To: qemu-devel@nongnu.org
22Cc: lucas.kaldstrom@hotmail.co.uk, peter.maydell@linaro.org,
23 riku.voipio@iki.fi,
24 laurent@vivier.eu, Petros Angelatos <petrosagg@resin.io>
25Date: Sun, 14 Feb 2016 21:51:47 -0800
a5affa61
ER
26
27In order for one to use QEMU user mode emulation under a chroot, it is
28required to use binfmt_misc. This can be avoided by QEMU never doing a
29raw execve() to the host system.
30
31Introduce a new option, -execve, that uses the current QEMU interpreter
32to intercept execve().
33
34qemu_execve() will prepend the interpreter path , similar to what
35binfmt_misc would do, and then pass the modified execve() to the host.
36
37It is necessary to parse hashbang scripts in that function otherwise
38the kernel will try to run the interpreter of a script without QEMU and
39get an invalid exec format error.
40
41Signed-off-by: Petros Angelatos <petrosagg@resin.io>
102e18f7
ER
42Tested-by: Laurent Vivier <laurent@vivier.eu>
43Reviewed-by: Laurent Vivier <laurent@vivier.eu>
a5affa61 44---
102e18f7
ER
45v3 changes:
46 - rebase the patchset against current code
47
f1a1313b
ER
48diff --git a/linux-user/main.c b/linux-user/main.c
49index ee12035..5951279 100644
50--- a/linux-user/main.c
51+++ b/linux-user/main.c
52@@ -79,6 +79,7 @@ static void usage(int exitcode);
a5affa61
ER
53
54 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
55 const char *qemu_uname_release;
56+const char *qemu_execve_path;
57
58 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
59 we allocate a bigger stack. Need a better solution, for example
f1a1313b 60@@ -3828,6 +3829,11 @@ static void handle_arg_guest_base(const char *arg)
a5affa61
ER
61 have_guest_base = 1;
62 }
63
64+static void handle_arg_execve(const char *arg)
65+{
f1a1313b 66+ qemu_execve_path = strdup(arg);
a5affa61
ER
67+}
68+
69 static void handle_arg_reserved_va(const char *arg)
70 {
71 char *p;
f1a1313b 72@@ -3913,6 +3919,8 @@ static const struct qemu_argument arg_table[] = {
a5affa61
ER
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'"},
f1a1313b
ER
76+ {"execve", "QEMU_EXECVE", true, handle_arg_execve,
77+ "path", "use interpreter at 'path' when a process calls execve()"},
a5affa61
ER
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,
81diff --git a/linux-user/qemu.h b/linux-user/qemu.h
102e18f7 82index bd90cc3..0d9b058 100644
a5affa61
ER
83--- a/linux-user/qemu.h
84+++ b/linux-user/qemu.h
102e18f7 85@@ -140,6 +140,7 @@ void init_task_state(TaskState *ts);
a5affa61
ER
86 void task_settid(TaskState *);
87 void stop_all_tasks(void);
88 extern const char *qemu_uname_release;
89+extern const char *qemu_execve_path;
90 extern unsigned long mmap_min_addr;
91
92 /* ??? See if we can avoid exposing so much of the loader internals. */
f1a1313b
ER
93diff --git a/linux-user/syscall.c b/linux-user/syscall.c
94index 0cbace4..d0b5442 100644
95--- a/linux-user/syscall.c
96+++ b/linux-user/syscall.c
97@@ -5854,6 +5854,109 @@ static target_timer_t get_timer_id(abi_long arg)
a5affa61
ER
98 return timerid;
99 }
100
f1a1313b
ER
101+#define BINPRM_BUF_SIZE 128
102+
a5affa61
ER
103+/* qemu_execve() Must return target values and target errnos. */
104+static abi_long qemu_execve(char *filename, char *argv[],
105+ char *envp[])
106+{
107+ char *i_arg = NULL, *i_name = NULL;
108+ char **new_argp;
109+ int argc, fd, ret, i, offset = 3;
110+ char *cp;
111+ char buf[BINPRM_BUF_SIZE];
112+
a5affa61
ER
113+ for (argc = 0; argv[argc] != NULL; argc++) {
114+ /* nothing */ ;
115+ }
116+
117+ fd = open(filename, O_RDONLY);
118+ if (fd == -1) {
f1a1313b 119+ return -ENOENT;
a5affa61
ER
120+ }
121+
122+ ret = read(fd, buf, BINPRM_BUF_SIZE);
123+ if (ret == -1) {
124+ close(fd);
f1a1313b 125+ return -ENOENT;
a5affa61
ER
126+ }
127+
128+ close(fd);
129+
130+ /* adapted from the kernel
131+ * https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/binfmt_script.c
132+ */
133+ if ((buf[0] == '#') && (buf[1] == '!')) {
134+ /*
135+ * This section does the #! interpretation.
136+ * Sorta complicated, but hopefully it will work. -TYT
137+ */
138+
139+ buf[BINPRM_BUF_SIZE - 1] = '\0';
140+ cp = strchr(buf, '\n');
141+ if (cp == NULL) {
f1a1313b 142+ cp = buf+BINPRM_BUF_SIZE-1;
a5affa61
ER
143+ }
144+ *cp = '\0';
145+ while (cp > buf) {
146+ cp--;
147+ if ((*cp == ' ') || (*cp == '\t')) {
148+ *cp = '\0';
149+ } else {
150+ break;
151+ }
152+ }
f1a1313b 153+ for (cp = buf+2; (*cp == ' ') || (*cp == '\t'); cp++) {
a5affa61
ER
154+ /* nothing */ ;
155+ }
156+ if (*cp == '\0') {
157+ return -ENOEXEC; /* No interpreter name found */
158+ }
159+ i_name = cp;
160+ i_arg = NULL;
161+ for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
162+ /* nothing */ ;
163+ }
164+ while ((*cp == ' ') || (*cp == '\t')) {
165+ *cp++ = '\0';
166+ }
167+ if (*cp) {
168+ i_arg = cp;
169+ }
170+
171+ if (i_arg) {
172+ offset = 5;
173+ } else {
174+ offset = 4;
175+ }
176+ }
177+
178+ new_argp = alloca((argc + offset + 1) * sizeof(void *));
179+
180+ /* Copy the original arguments with offset */
181+ for (i = 0; i < argc; i++) {
182+ new_argp[i + offset] = argv[i];
183+ }
184+
185+ new_argp[0] = strdup(qemu_execve_path);
186+ new_argp[1] = strdup("-0");
187+ new_argp[offset] = filename;
188+ new_argp[argc + offset] = NULL;
189+
190+ if (i_name) {
191+ new_argp[2] = i_name;
192+ new_argp[3] = i_name;
193+
194+ if (i_arg) {
195+ new_argp[4] = i_arg;
196+ }
197+ } else {
198+ new_argp[2] = argv[0];
199+ }
200+
f1a1313b 201+ return get_errno(execve(qemu_execve_path, new_argp, envp));
a5affa61
ER
202+}
203+
204 /* do_syscall() should always have a single exit point at the end so
205 that actions, such as logging of syscall results, can be performed.
206 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
f1a1313b
ER
207@@ -6113,7 +6216,13 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
208
209 if (!(p = lock_user_string(arg1)))
210 goto execve_efault;
211- ret = get_errno(execve(p, argp, envp));
212+
213+ if (qemu_execve_path && *qemu_execve_path) {
214+ ret = get_errno(qemu_execve(p, argp, envp));
215+ } else {
216+ ret = get_errno(execve(p, argp, envp));
217+ }
218+
a5affa61
ER
219 unlock_user(p, arg1, 0);
220
221 goto execve_end;
This page took 0.085683 seconds and 4 git commands to generate.