diff mbox series

[v2,02/11] linux-user: Fix the execfd case of /proc/self/exe open

Message ID 20210531055019.10149-3-yamamoto@midokura.com (mailing list archive)
State New, archived
Headers show
Series linux-user changes to run docker | expand

Commit Message

Takashi Yamamoto May 31, 2021, 5:50 a.m. UTC
It's problematic to return AT_EXECFD as it is because the user app
would close it.
This patch opens it via /proc/self/fd instead.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
---
 linux-user/syscall.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Comments

Laurent Vivier June 20, 2021, 2:16 p.m. UTC | #1
Le 31/05/2021 à 07:50, YAMAMOTO Takashi a écrit :
> It's problematic to return AT_EXECFD as it is because the user app
> would close it.
> This patch opens it via /proc/self/fd instead.
> 
> Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
> ---
>  linux-user/syscall.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index a2b03ecb8b..14a63518e2 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -8118,7 +8118,17 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,
>  
>      if (is_proc_myself(pathname, "exe")) {
>          int execfd = qemu_getauxval(AT_EXECFD);
> -        return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
> +        if (execfd) {
> +            char filename[PATH_MAX];
> +            int ret;
> +
> +            snprintf(filename, sizeof(filename), "/proc/self/fd/%d", execfd);
> +            ret = safe_openat(dirfd, filename, flags, mode);
> +            if (ret != -1) {
> +                return ret;
> +            }
> +        }
> +        return safe_openat(dirfd, exec_path, flags, mode);
>      }
>  
>      for (fake_open = fakes; fake_open->filename; fake_open++) {
> 

I think a dup() would be more appropriate, or explain why not.

Thanks,
Laurent
Takashi Yamamoto June 21, 2021, 1:19 a.m. UTC | #2
On Sun, Jun 20, 2021 at 11:16 PM Laurent Vivier <laurent@vivier.eu> wrote:
>
> Le 31/05/2021 à 07:50, YAMAMOTO Takashi a écrit :
> > It's problematic to return AT_EXECFD as it is because the user app
> > would close it.
> > This patch opens it via /proc/self/fd instead.
> >
> > Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
> > ---
> >  linux-user/syscall.c | 12 +++++++++++-
> >  1 file changed, 11 insertions(+), 1 deletion(-)
> >
> > diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> > index a2b03ecb8b..14a63518e2 100644
> > --- a/linux-user/syscall.c
> > +++ b/linux-user/syscall.c
> > @@ -8118,7 +8118,17 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,
> >
> >      if (is_proc_myself(pathname, "exe")) {
> >          int execfd = qemu_getauxval(AT_EXECFD);
> > -        return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
> > +        if (execfd) {
> > +            char filename[PATH_MAX];
> > +            int ret;
> > +
> > +            snprintf(filename, sizeof(filename), "/proc/self/fd/%d", execfd);
> > +            ret = safe_openat(dirfd, filename, flags, mode);
> > +            if (ret != -1) {
> > +                return ret;
> > +            }
> > +        }
> > +        return safe_openat(dirfd, exec_path, flags, mode);
> >      }
> >
> >      for (fake_open = fakes; fake_open->filename; fake_open++) {
> >
>
> I think a dup() would be more appropriate, or explain why not.

i did this way because dup() doesn't deal with open flags.

>
> Thanks,
> Laurent
diff mbox series

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index a2b03ecb8b..14a63518e2 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -8118,7 +8118,17 @@  static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,
 
     if (is_proc_myself(pathname, "exe")) {
         int execfd = qemu_getauxval(AT_EXECFD);
-        return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
+        if (execfd) {
+            char filename[PATH_MAX];
+            int ret;
+
+            snprintf(filename, sizeof(filename), "/proc/self/fd/%d", execfd);
+            ret = safe_openat(dirfd, filename, flags, mode);
+            if (ret != -1) {
+                return ret;
+            }
+        }
+        return safe_openat(dirfd, exec_path, flags, mode);
     }
 
     for (fake_open = fakes; fake_open->filename; fake_open++) {