All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laurent Vivier <laurent@vivier.eu>
To: Matteo Croce <mcroce@linux.microsoft.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH] linux-user: add option to chroot before emulation
Date: Tue, 8 Dec 2020 09:21:16 +0100	[thread overview]
Message-ID: <f37589e0-90d9-a5f0-a5a9-423c583c675d@vivier.eu> (raw)
In-Reply-To: <20201208001727.17433-1-mcroce@linux.microsoft.com>

Le 08/12/2020 à 01:17, Matteo Croce a écrit :
> From: Matteo Croce <mcroce@microsoft.com>
> 
> Add a '-c' option which does a chroot() just before starting the
> emulation. This is useful when the static QEMU user binary can't
> be copied into the target root filesystem, e.g. if it's readonly.

Did you try to use the binfmt_misc 'F' flag (fix binary)?

https://www.kernel.org/doc/Documentation/admin-guide/binfmt-misc.rst

``F`` - fix binary

The usual behaviour of binfmt_misc is to spawn the
binary lazily when the misc format file is invoked.  However,
this doesn``t work very well in the face of mount namespaces and
changeroots, so the ``F`` mode opens the binary as soon as the
emulation is installed and uses the opened image to spawn the
emulator, meaning it is always available once installed,
regardless of how the environment changes.

This can be configured with scripts/qemu-binfmt-conf.sh and
"--persistent yes"" option

Thanks,
Laurent
> 
> Move some code which accesses /proc/sys/vm/mmap_min_addr before
> the chroot, otherwise it would fail.
> 
> Signed-off-by: Matteo Croce <mcroce@microsoft.com>
> ---
>  linux-user/main.c | 128 +++++++++++++++++++++++++++-------------------
>  1 file changed, 75 insertions(+), 53 deletions(-)
> 
> diff --git a/linux-user/main.c b/linux-user/main.c
> index 24d1eb73ad..4788e4b5bc 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -60,6 +60,7 @@ static const char *seed_optarg;
>  unsigned long mmap_min_addr;
>  unsigned long guest_base;
>  bool have_guest_base;
> +static const char *qemu_chroot;
>  
>  /*
>   * Used to implement backwards-compatibility for the `-strace`, and
> @@ -304,6 +305,11 @@ static void handle_arg_pagesize(const char *arg)
>      }
>  }
>  
> +static void handle_arg_chroot(const char *arg)
> +{
> +    qemu_chroot = arg;
> +}
> +
>  static void handle_arg_seed(const char *arg)
>  {
>      seed_optarg = arg;
> @@ -450,6 +456,8 @@ static const struct qemu_argument arg_table[] = {
>       "logfile",     "write logs to 'logfile' (default stderr)"},
>      {"p",          "QEMU_PAGESIZE",    true,  handle_arg_pagesize,
>       "pagesize",   "set the host page size to 'pagesize'"},
> +    {"c",          "QEMU_CHROOT",      true,  handle_arg_chroot,
> +     "chroot",     "chroot to 'chroot' before starting emulation"},
>      {"singlestep", "QEMU_SINGLESTEP",  false, handle_arg_singlestep,
>       "",           "run in singlestep mode"},
>      {"strace",     "QEMU_STRACE",      false, handle_arg_strace,
> @@ -688,6 +696,73 @@ int main(int argc, char **argv, char **envp)
>  
>      init_qemu_uname_release();
>  
> +    /*
> +     * Read in mmap_min_addr kernel parameter.  This value is used
> +     * When loading the ELF image to determine whether guest_base
> +     * is needed.  It is also used in mmap_find_vma.
> +     */
> +    {
> +        FILE *fp;
> +
> +        if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
> +            unsigned long tmp;
> +            if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
> +                mmap_min_addr = tmp;
> +                qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
> +                              mmap_min_addr);
> +            }
> +            fclose(fp);
> +        }
> +    }
> +
> +    /*
> +     * We prefer to not make NULL pointers accessible to QEMU.
> +     * If we're in a chroot with no /proc, fall back to 1 page.
> +     */
> +    if (mmap_min_addr == 0) {
> +        mmap_min_addr = qemu_host_page_size;
> +        qemu_log_mask(CPU_LOG_PAGE,
> +                      "host mmap_min_addr=0x%lx (fallback)\n",
> +                      mmap_min_addr);
> +    }
> +
> +    /*
> +     * Prepare copy of argv vector for target.
> +     */
> +    target_argc = argc - optind;
> +    target_argv = calloc(target_argc + 1, sizeof (char *));
> +    if (target_argv == NULL) {
> +        (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    /*
> +     * If argv0 is specified (using '-0' switch) we replace
> +     * argv[0] pointer with the given one.
> +     */
> +    i = 0;
> +    if (argv0 != NULL) {
> +        target_argv[i++] = strdup(argv0);
> +    }
> +    for (; i < target_argc; i++) {
> +        target_argv[i] = strdup(argv[optind + i]);
> +    }
> +    target_argv[target_argc] = NULL;
> +
> +    /*
> +     * Change root if requested wuth '-c'
> +     */
> +    if (qemu_chroot) {
> +        if (chroot(qemu_chroot) < 0) {
> +            error_report("chroot failed");
> +            exit(1);
> +        }
> +        if (chdir("/")) {
> +            error_report("not able to chdir to /: %s", strerror(errno));
> +            exit(1);
> +        }
> +    }
> +
>      execfd = qemu_getauxval(AT_EXECFD);
>      if (execfd == 0) {
>          execfd = open(exec_path, O_RDONLY);
> @@ -746,59 +821,6 @@ int main(int argc, char **argv, char **envp)
>      target_environ = envlist_to_environ(envlist, NULL);
>      envlist_free(envlist);
>  
> -    /*
> -     * Read in mmap_min_addr kernel parameter.  This value is used
> -     * When loading the ELF image to determine whether guest_base
> -     * is needed.  It is also used in mmap_find_vma.
> -     */
> -    {
> -        FILE *fp;
> -
> -        if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
> -            unsigned long tmp;
> -            if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
> -                mmap_min_addr = tmp;
> -                qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
> -                              mmap_min_addr);
> -            }
> -            fclose(fp);
> -        }
> -    }
> -
> -    /*
> -     * We prefer to not make NULL pointers accessible to QEMU.
> -     * If we're in a chroot with no /proc, fall back to 1 page.
> -     */
> -    if (mmap_min_addr == 0) {
> -        mmap_min_addr = qemu_host_page_size;
> -        qemu_log_mask(CPU_LOG_PAGE,
> -                      "host mmap_min_addr=0x%lx (fallback)\n",
> -                      mmap_min_addr);
> -    }
> -
> -    /*
> -     * Prepare copy of argv vector for target.
> -     */
> -    target_argc = argc - optind;
> -    target_argv = calloc(target_argc + 1, sizeof (char *));
> -    if (target_argv == NULL) {
> -        (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
> -        exit(EXIT_FAILURE);
> -    }
> -
> -    /*
> -     * If argv0 is specified (using '-0' switch) we replace
> -     * argv[0] pointer with the given one.
> -     */
> -    i = 0;
> -    if (argv0 != NULL) {
> -        target_argv[i++] = strdup(argv0);
> -    }
> -    for (; i < target_argc; i++) {
> -        target_argv[i] = strdup(argv[optind + i]);
> -    }
> -    target_argv[target_argc] = NULL;
> -
>      ts = g_new0(TaskState, 1);
>      init_task_state(ts);
>      /* build Task State */
> 



  parent reply	other threads:[~2020-12-08  8:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-08  0:17 [PATCH] linux-user: add option to chroot before emulation Matteo Croce
2020-12-08  2:09 ` no-reply
2020-12-08  8:21 ` Laurent Vivier [this message]
2020-12-08 16:04   ` Matteo Croce
2020-12-08 18:25     ` 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=f37589e0-90d9-a5f0-a5a9-423c583c675d@vivier.eu \
    --to=laurent@vivier.eu \
    --cc=mcroce@linux.microsoft.com \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.