linux-user: add option to intercept execve() syscalls
PATCH v6 1/1 linux-user: add option to intercept execve() syscalls
In order for one to use QEMU user mode emulation under a chroot, it is required to use binfmt_misc. This can be avoided by QEMU never doing a raw execve() to the host system, which is especially useful in environments where binfmt_misc can't be used.
Introduce a new option, -execve, that uses the current QEMU interpreter to intercept execve(). In addition, execve mode can also be en- and disabled using the 'QEMU_EXECVE' env var.
qemu_execve() will prepend the interpreter path, similar to what binfmt_misc would do, and then pass the modified execve() to the host.
It is necessary to parse hashbang scripts in that function otherwise the kernel will try to run the interpreter of a script without QEMU and get an invalid exec format error.
Note that a previous incarnation of this patch was submitted a few years ago (see [0]) by Petros Angelatos as the original author who confirmed that it's OK to resubmit it.
[MH: Adapted to match coding style; Simplified implementation; Added additional comments; Improved env var handling]
[0]: https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/
---
v6:
Rebased on top of 'master'. Thread for v5 is at:
https://lore.kernel.org/qemu-devel/[email protected]/
v5:
This is actually v2 of this re-incarnation. Sending it as v5, so it shows as the most recent version in patchew.
- Simplify implementation by removing possibility to pass script files as 'user executables' to qemu, i.e. run 'qemu-<arch> foo.sh'. It's sufficient (at least for our use-cases) to restrict this to 'execve()' itself to mimic Kernel behaviour with execve mode disabled.
- Add a number of comments to the code to hopefully explain better what's going on (when modifying argv for instance).
- Reworked sign-off statements according to review feedback.
v1:
We've been using this feature internally for at least five years by now. Prior to submission, the code was updated to (hopefully) conform to the current QEMU coding style.
I'd be happy to add test cases for this feature, but I'd need some pointers given that I'm a first-time contributor. Thanks!
--- linux-user/linuxload.c | 97 ++++++++++++++++++++++++ linux-user/loader.h | 1 + linux-user/main.c | 54 +++++++++++++ linux-user/syscall.c | 146 +++++++++++++++++++++++++++++++++--- linux-user/user-internals.h | 1 + 5 files changed, 287 insertions(+), 12 deletions(-)
linux-user/linuxload.c +97 -0
@@ -138,6 +138,103 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,return sp; } +/** +* Prepares script files for execution. +* +* Checks whether the given file is a script, i.e. starts with a shebang (#!). +* If so, the first line is parsed to extract the interpreter name and (if +* given) the single argument and bprm is updated accordingly. +* +* Returns 0 on success, 1 if the file is not a script, <0 for errors. +*/ +int load_script_file (const char *filename,struct linux_binprm *bprm) +{ +int retval, fd; +char *i_name =NULL ; +char **new_argv =NULL ; +char *cp; +char buf[BPRM_BUF_SIZE ]; + +/* Check if it is a script */ + fd =open (filename,O_RDONLY ); +if (fd ==-1 ) { +return fd; + } + + retval =read (fd, buf,BPRM_BUF_SIZE ); +close (fd); + +if (retval ==-1 ) { +return retval; + } + +/* if we have less than 2 bytes, we can guess it is not executable */ +if (retval <2 ) { +return -ENOEXEC ; + } + +/* +* Extract interpreter and argument (if given). +* +* Adapted from the kernel: +* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/binfmt_script.c +*/ +if ((buf[0 ] =='#' ) && (buf[1 ] =='!' )) { +/* Ensure buffer is 0-terminated. */ + buf[BPRM_BUF_SIZE -1 ] ='\0' ; +/* Find end of first line, we might have read more than one... */ + cp =strchr (buf,'\n' ); +/* ... or the first line might have been truncated. */ +if (cp ==NULL ) { + cp = buf +BPRM_BUF_SIZE -1 ; + } +/* Truncate buffer to first line. */ + *cp ='\0' ; +/* Truncate trailing whitespace on the first line. */ +while (cp > buf) { + cp--; +if ((*cp ==' ' ) || (*cp =='\t' )) { + *cp ='\0' ; + }else { +break ; + } + } +/* Skip (optional) whitespace between shebang and interpreter name. */ +for (cp = buf +2 ; (*cp ==' ' ) || (*cp =='\t' ); cp++) { +/* nothing */ ; + } +if (*cp =='\0' ) { +return -ENOEXEC ;/* No interpreter name found */ + } +/* We now have identified the beginning of the interpreter name. */ + i_name = cp; + +/* +* Skip over the interpreter name till we hit end of string or +* whitespace (that may separate an optional argument). +*/ +for ( ; *cp && (*cp !=' ' ) && (*cp !='\t' ); cp++) { +/* nothing */ ; + } +/* Mark the end of the interpreter name and skip whitespace behind it. */ +while ((*cp ==' ' ) || (*cp =='\t' )) { + *cp++ ='\0' ; + } + +/* We now have identified the beginning of the argument. */ +if (*cp) { + new_argv = g_new0(char*, 1 + 1); + new_argv[0 ] =g_strdup (cp); + } + bprm->argv = new_argv; + bprm->filename =g_strdup (i_name); + +return 0 ; + }else { +return 1 ; + } +} +int loader_exec (int fdexec,const char *filename,char **argv,char **envp,struct image_info *infop,struct linux_binprm *bprm) {
linux-user/loader.h +1 -0
@@ -90,6 +90,7 @@ int loader_exec(int fdexec, const char *filename, char **argv, char **envp,uint32_t get_elf_eflags (int fd);int load_elf_binary (struct linux_binprm *bprm,struct image_info *info);int load_flt_binary (struct linux_binprm *bprm,struct image_info *info); +int load_script_file (const char *filename,struct linux_binprm *bprm);abi_long memcpy_to_target (abi_ulong dest,const void *src,unsigned long len);
linux-user/main.c +54 -0
72 lines not on this page · raw message
linux-user/syscall.c +134 -12
170 lines not on this page · raw message
linux-user/user-internals.h +1 -0
@@ -69,6 +69,7 @@ abi_long get_errno(abi_long ret);const char *target_strerror (int err);int get_osversion (void );void init_qemu_uname_release (void ); +extern const char *qemu_execve_path;void fork_start (void );void fork_end (pid_t pid);