summary refs log tree commit diff stats
path: root/linux-user/main.c
diff options
context:
space:
mode:
authorLaurent Vivier <laurent@vivier.eu>2013-08-30 01:46:44 +0200
committerRiku Voipio <riku.voipio@linaro.org>2013-09-24 10:47:07 +0300
commit03cfd8faa7ffb7201e2949b99c2f35b1fef7078b (patch)
treef6eebab9322436e4bfd6e27de22a4217bcd557df /linux-user/main.c
parent0d78b3b5b1b5c391aa96b481be106de023810b66 (diff)
downloadfocaccia-qemu-03cfd8faa7ffb7201e2949b99c2f35b1fef7078b.tar.gz
focaccia-qemu-03cfd8faa7ffb7201e2949b99c2f35b1fef7078b.zip
linux-user: add support of binfmt_misc 'O' flag
The binfmt_misc module can calculate the credentials and security
token according to the binary instead of to the interpreter if the
'C' flag is enabled.

To be able to execute non-readable binaries, this flag implies 'O'
flag. When 'O' flag is enabled, bintfmt_misc opens the file for
reading and pass the file descriptor to the interpreter.

References:
linux/Documentation/binfmt_misc.txt          ['O' and 'C' description]
linux/fs/binfmt_misc.c linux/fs/binfmt_elf.c [ AT_EXECFD usage ]

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
Signed-off-by: Riku Voipio <riku.voipio@linaro.org>
Diffstat (limited to 'linux-user/main.c')
-rw-r--r--linux-user/main.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/linux-user/main.c b/linux-user/main.c
index 3eed25274d..016e2e1755 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -3618,6 +3618,26 @@ static int parse_args(int argc, char **argv)
     return optind;
 }
 
+static int get_execfd(char **envp)
+{
+    typedef struct {
+        long a_type;
+        long a_val;
+    } auxv_t;
+    auxv_t *auxv;
+
+    while (*envp++ != NULL) {
+        ;
+    }
+
+    for (auxv = (auxv_t *)envp; auxv->a_type != AT_NULL; auxv++) {
+        if (auxv->a_type == AT_EXECFD) {
+            return auxv->a_val;
+        }
+    }
+    return -1;
+}
+
 int main(int argc, char **argv, char **envp)
 {
     struct target_pt_regs regs1, *regs = &regs1;
@@ -3632,6 +3652,7 @@ int main(int argc, char **argv, char **envp)
     int target_argc;
     int i;
     int ret;
+    int execfd;
 
     module_call_init(MODULE_INIT_QOM);
 
@@ -3809,7 +3830,16 @@ int main(int argc, char **argv, char **envp)
     env->opaque = ts;
     task_settid(ts);
 
-    ret = loader_exec(filename, target_argv, target_environ, regs,
+    execfd = get_execfd(envp);
+    if (execfd < 0) {
+        execfd = open(filename, O_RDONLY);
+    }
+    if (execfd < 0) {
+        printf("Error while loading %s: %s\n", filename, strerror(-execfd));
+        _exit(1);
+    }
+
+    ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
         info, &bprm);
     if (ret != 0) {
         printf("Error while loading %s: %s\n", filename, strerror(-ret));