From: Laurent Vivier <laurent@vivier.eu>
To: Riku Voipio <riku.voipio@iki.fi>
Cc: qemu-devel@nongnu.org, Laurent Vivier <laurent@vivier.eu>
Subject: [Qemu-devel] [PATCH 5/6] linux-user: add support of binfmt_misc 'O' flag
Date: Fri, 30 Aug 2013 01:46:44 +0200 [thread overview]
Message-ID: <1377820005-3835-6-git-send-email-laurent@vivier.eu> (raw)
In-Reply-To: <1377820005-3835-1-git-send-email-laurent@vivier.eu>
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>
---
linux-user/linuxload.c | 8 ++------
linux-user/main.c | 32 +++++++++++++++++++++++++++++++-
linux-user/qemu.h | 2 +-
3 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c
index 5cd6d91..a1fe5ed 100644
--- a/linux-user/linuxload.c
+++ b/linux-user/linuxload.c
@@ -131,7 +131,7 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
return sp;
}
-int loader_exec(const char * filename, char ** argv, char ** envp,
+int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
struct target_pt_regs * regs, struct image_info *infop,
struct linux_binprm *bprm)
{
@@ -140,11 +140,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp,
bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
memset(bprm->page, 0, sizeof(bprm->page));
- retval = open(filename, O_RDONLY);
- if (retval < 0) {
- return -errno;
- }
- bprm->fd = retval;
+ bprm->fd = fdexec;
bprm->filename = (char *)filename;
bprm->argc = count(argv);
bprm->argv = argv;
diff --git a/linux-user/main.c b/linux-user/main.c
index 03859bc..0223b93 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -3532,6 +3532,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 = ®s1;
@@ -3546,6 +3566,7 @@ int main(int argc, char **argv, char **envp)
int target_argc;
int i;
int ret;
+ int execfd;
module_call_init(MODULE_INIT_QOM);
@@ -3721,7 +3742,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));
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 4a16e8f..111251b 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -172,7 +172,7 @@ struct linux_binprm {
void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
abi_ulong stringp, int push_ptr);
-int loader_exec(const char * filename, char ** argv, char ** envp,
+int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
struct target_pt_regs * regs, struct image_info *infop,
struct linux_binprm *);
--
1.8.1.2
next prev parent reply other threads:[~2013-08-29 23:47 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-29 23:46 [Qemu-devel] [PATCH 0/6] linux-user: Misc patches for linux container compatibility Laurent Vivier
2013-08-29 23:46 ` [Qemu-devel] [PATCH 1/6] linux-user: convert /proc/net/route when endianess differs Laurent Vivier
2013-09-06 16:30 ` Peter Maydell
2013-08-29 23:46 ` [Qemu-devel] [PATCH 2/6] linux-user: Add setsockopt(SO_ATTACH_FILTER) Laurent Vivier
2013-09-06 16:30 ` Peter Maydell
2013-08-29 23:46 ` [Qemu-devel] [PATCH 3/6] linux-user: allow use of TIOCGSID Laurent Vivier
2013-09-06 16:31 ` Peter Maydell
2013-08-29 23:46 ` [Qemu-devel] [PATCH 4/6] linux-user: add some IPV6 commands in setsockop() Laurent Vivier
2013-09-06 16:31 ` Peter Maydell
2013-08-29 23:46 ` Laurent Vivier [this message]
2013-09-06 16:17 ` [Qemu-devel] [PATCH 5/6] linux-user: add support of binfmt_misc 'O' flag Peter Maydell
2013-09-06 16:50 ` Laurent Vivier
2013-08-29 23:46 ` [Qemu-devel] [PATCH 6/6] scripts: create a template to use with lxc-create Laurent Vivier
2013-09-06 16:33 ` Peter Maydell
2013-09-06 16:56 ` Laurent Vivier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1377820005-3835-6-git-send-email-laurent@vivier.eu \
--to=laurent@vivier.eu \
--cc=qemu-devel@nongnu.org \
--cc=riku.voipio@iki.fi \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).