qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Vivier <laurent@vivier.eu>
To: Drew DeVault <sir@cmpwn.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH] linux-user: implement execveat
Date: Tue, 1 Nov 2022 21:01:06 +0100	[thread overview]
Message-ID: <01e85f0c-2dbc-7ebe-793a-c05818b8ce8c@vivier.eu> (raw)
In-Reply-To: <20221031084030.2125288-1-sir@cmpwn.com>

Le 31/10/2022 à 09:40, Drew DeVault a écrit :
> References: https://gitlab.com/qemu-project/qemu/-/issues/1007
> Signed-off-by: Drew DeVault <sir@cmpwn.com>
> ---
>   linux-user/syscall.c | 50 ++++++++++++++++++++++++++++++++++++++------
>   1 file changed, 44 insertions(+), 6 deletions(-)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index f55cdebee5..795f7ce4cd 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -634,6 +634,10 @@ safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
>   safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
>                 int, options, struct rusage *, rusage)
>   safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
> +#if defined(TARGET_NR_execveat)
> +safe_syscall5(int, execveat, int, dirfd, const char *, filename,
> +        char **, argv, char **, envp, int, flags)
> +#endif
>   #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
>       defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
>   safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
> @@ -8748,19 +8752,45 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
>           ret = get_errno(unlinkat(arg1, p, arg3));
>           unlock_user(p, arg2, 0);
>           return ret;
> +#endif
> +#if defined(TARGET_NR_execveat)
> +    case TARGET_NR_execveat:
>   #endif
>       case TARGET_NR_execve:
>           {
>               char **argp, **envp;
> -            int argc, envc;
> +            int argc, envc, dirfd, flags;
>               abi_ulong gp;
>               abi_ulong guest_argp;
>               abi_ulong guest_envp;
>               abi_ulong addr;
> +            abi_long path;
>               char **q;
>   
>               argc = 0;
> -            guest_argp = arg2;
> +
> +            switch (num) {
> +            case TARGET_NR_execve:
> +                path = arg1;
> +                guest_argp = arg2;
> +                guest_envp = arg3;
> +                dirfd = AT_FDCWD;
> +                flags = 0;
> +                break;
> +#if defined(TARGET_NR_execveat)
> +            case TARGET_NR_execveat:
> +                dirfd = arg1;
> +                path = arg2;
> +                guest_argp = arg3;
> +                guest_envp = arg4;
> +                flags = arg5;
> +                break;
> +#endif
> +            default:
> +                // squelch uninitialized variable warnings
> +                abort();
> +            }
> +
>               for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
>                   if (get_user_ual(addr, gp))
>                       return -TARGET_EFAULT;
> @@ -8769,7 +8799,6 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
>                   argc++;
>               }
>               envc = 0;
> -            guest_envp = arg3;
>               for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
>                   if (get_user_ual(addr, gp))
>                       return -TARGET_EFAULT;
> @@ -8803,7 +8832,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
>               }
>               *q = NULL;
>   
> -            if (!(p = lock_user_string(arg1)))
> +            if (!(p = lock_user_string(path)))
>                   goto execve_efault;
>               /* Although execve() is not an interruptible syscall it is
>                * a special case where we must use the safe_syscall wrapper:
> @@ -8815,8 +8844,17 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
>                * before the execve completes and makes it the other
>                * program's problem.
>                */
> -            ret = get_errno(safe_execve(p, argp, envp));
> -            unlock_user(p, arg1, 0);
> +            switch (num) {
> +            case TARGET_NR_execve:
> +                ret = get_errno(safe_execve(p, argp, envp));
> +                break;
> +#if defined(TARGET_NR_execveat)
> +            case TARGET_NR_execveat:
> +                ret = get_errno(safe_execveat(dirfd, p, argp, envp, flags));
> +                break;
> +#endif
> +            }
> +            unlock_user(p, path, 0);
>   
>               goto execve_end;
>   


I think it would be clearer to write a common function and to call it from execve and execveat, like 
it's in the kernel:

execve:

   do_execveat(AT_FDCWD, filename, argv, envp, 0);

execeveat:

   do_execveat(fd, filename, argv, envp, flags);

Thanks,
Laurent


  reply	other threads:[~2022-11-01 20:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-31  8:40 [PATCH] linux-user: implement execveat Drew DeVault
2022-11-01 20:01 ` Laurent Vivier [this message]
2022-11-03 14:48   ` Drew DeVault

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=01e85f0c-2dbc-7ebe-793a-c05818b8ce8c@vivier.eu \
    --to=laurent@vivier.eu \
    --cc=qemu-devel@nongnu.org \
    --cc=sir@cmpwn.com \
    /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).