qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Shu-Chun Weng <scw@google.com>
To: Helge Deller <deller@gmx.de>
Cc: qemu-devel@nongnu.org, Laurent Vivier <laurent@vivier.eu>,
	 Richard Henderson <richard.henderson@linaro.org>
Subject: Re: [PATCH v3 2/2] linux-user: Fix openat() emulation to not modify atime
Date: Thu, 28 Dec 2023 14:15:21 -0800	[thread overview]
Message-ID: <CAF3nBxi2Vbtr-Xj_JimygioYOcSC4YMixS6JdRwHO+rCSrBdjg@mail.gmail.com> (raw)
In-Reply-To: <e7ce2432-aba7-4893-b828-7f5df6cbe9a6@gmx.de>


[-- Attachment #1.1: Type: text/plain, Size: 5071 bytes --]

Yup, reproduced on a system with glibc built with fortified source. Sending
out version 4.

On Thu, Dec 28, 2023 at 7:49 AM Helge Deller <deller@gmx.de> wrote:

> On 12/8/23 23:42, Shu-Chun Weng wrote:
> > Commit b8002058 strengthened openat()'s /proc detection by calling
> > realpath(3) on the given path, which allows various paths and symlinks
> > that points to the /proc file system to be intercepted correctly.
> >
> > Using realpath(3), though, has a side effect that it reads the symlinks
> > along the way, and thus changes their atime. The results in the
> > following code snippet already get ~now instead of the real atime:
> >
> >    int fd = open("/path/to/a/symlink", O_PATH | O_NOFOLLOW);
> >    struct stat st;
> >    fstat(fd, st);
> >    return st.st_atime;
> >
> > This change opens a path that doesn't appear to be part of /proc
> > directly and checks the destination of /proc/self/fd/n to determine if
> > it actually refers to a file in /proc.
> >
> > Neither this nor the existing code works with symlinks or indirect paths
> > (e.g.  /tmp/../proc/self/exe) that points to /proc/self/exe because it
> > is itself a symlink, and both realpath(3) and /proc/self/fd/n will
> > resolve into the location of QEMU.
> >
> > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2004
> > Signed-off-by: Shu-Chun Weng <scw@google.com>
> > ---
> >   linux-user/syscall.c | 47 +++++++++++++++++++++++++++++++++++---------
> >   1 file changed, 38 insertions(+), 9 deletions(-)
> >
> > diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> > index e384e14248..7c3772301f 100644
> > --- a/linux-user/syscall.c
> > +++ b/linux-user/syscall.c
> > @@ -8308,8 +8308,7 @@ static int open_net_route(CPUArchState *cpu_env,
> int fd)
> >   int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char
> *fname,
> >                       int flags, mode_t mode, bool safe)
> >   {
> > -    g_autofree char *proc_name = NULL;
> > -    const char *pathname;
> > +    g_autofree char *pathname = NULL;
> >       struct fake_open {
> >           const char *filename;
> >           int (*fill)(CPUArchState *cpu_env, int fd);
> > @@ -8334,12 +8333,42 @@ int do_guest_openat(CPUArchState *cpu_env, int
> dirfd, const char *fname,
> >           { NULL, NULL, NULL }
> >       };
> >
> > -    /* if this is a file from /proc/ filesystem, expand full name */
> > -    proc_name = realpath(fname, NULL);
> > -    if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
> > -        pathname = proc_name;
> > +    if (strncmp(fname, "/proc/", 6) == 0) {
> > +        pathname = g_strdup(fname);
> >       } else {
> > -        pathname = fname;
> > +        g_autofree char *proc_name = NULL;
> > +        struct stat proc_stat;
> > +        int fd;
> > +
> > +        if (safe) {
> > +            fd = safe_openat(dirfd, path(fname), flags, mode);
> > +        } else {
> > +            fd = openat(dirfd, path(fname), flags, mode);
> > +        }
> > +        if (fd < 0) {
> > +            return fd;
> > +        }
> > +
> > +        /*
> > +         * Try to get the real path of the file we just opened. We
> avoid calling
> > +         * `realpath(3)` because it calls `readlink(2)` on symlinks
> which
> > +         * changes their atime. Note that since `/proc/self/exe` is a
> symlink,
> > +         * `pathname` will never resolve to it (neither will
> `realpath(3)`).
> > +         * That's why we check `fname` against the "/proc/" prefix
> first.
> > +         */
> > +        proc_name = g_strdup_printf("/proc/self/fd/%d", fd);
> > +        if (lstat(proc_name, &proc_stat) < 0 ||
> !S_ISLNK(proc_stat.st_mode)) {
> > +            /* No procfs or something weird. Not going to dig further.
> */
> > +            return fd;
> > +        }
> > +        pathname = g_new(char, proc_stat.st_size + 1);
> > +        readlink(proc_name, pathname, proc_stat.st_size + 1);
>
> this gives a build error for me:
> ./qemu/linux-user/syscall.c:8365:9: error: ignoring return value of
> ‘readlink’ declared with attribute ‘warn_unused_result’
> [-Werror=unused-result]
>
> Other than that, this patch indeed fixes the issue #2004
>
> Reviewed-by: Helge Deller <deller@gmx.de>
>
>
> Helge
>
> > +
> > +        /* if this is not a file from /proc/ filesystem, the fd is good
> as-is */
> > +        if (strncmp(pathname, "/proc/", 6) != 0) {
> > +            return fd;
> > +        }
> > +        close(fd);
> >       }
> >
> >       if (is_proc_myself(pathname, "exe")) {
> > @@ -8390,9 +8419,9 @@ int do_guest_openat(CPUArchState *cpu_env, int
> dirfd, const char *fname,
> >       }
> >
> >       if (safe) {
> > -        return safe_openat(dirfd, path(pathname), flags, mode);
> > +        return safe_openat(dirfd, pathname, flags, mode);
> >       } else {
> > -        return openat(dirfd, path(pathname), flags, mode);
> > +        return openat(dirfd, pathname, flags, mode);
> >       }
> >   }
> >
> >
>
>

[-- Attachment #1.2: Type: text/html, Size: 6424 bytes --]

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4004 bytes --]

      reply	other threads:[~2023-12-28 22:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-08 22:42 [PATCH v3 0/2] linux-user: openat() fixes Shu-Chun Weng
2023-12-08 22:42 ` [PATCH v3 1/2] linux-user: Define TARGET_O_LARGEFILE for aarch64 Shu-Chun Weng
2023-12-08 22:42 ` [PATCH v3 2/2] linux-user: Fix openat() emulation to not modify atime Shu-Chun Weng
2023-12-26 23:05   ` Shu-Chun Weng
2023-12-28 15:49   ` Helge Deller
2023-12-28 22:15     ` Shu-Chun Weng [this message]

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=CAF3nBxi2Vbtr-Xj_JimygioYOcSC4YMixS6JdRwHO+rCSrBdjg@mail.gmail.com \
    --to=scw@google.com \
    --cc=deller@gmx.de \
    --cc=laurent@vivier.eu \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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 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).