Skip to contentSkip to conversation

linux-user: openat() fixes

PATCH Superseded Shu-Chun Weng 2 participants 6 messages Dec 8, 2023 – Dec 28, 2023

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover linux-user: openat() fixes

Since v2:
- More robust handling of readlink()

Since v1:
- Eliminate static buffers in do_guest_openat()

Shu-Chun Weng (2):
linux-user: Define TARGET_O_LARGEFILE for aarch64
linux-user: Fix openat() emulation to not modify atime

 linux-user/aarch64/target_fcntl.h |  1 +
 linux-user/syscall.c              | 47 +++++++++++++++++++++++++------
 2 files changed, 39 insertions(+), 9 deletions(-)
#1 lore raw

PATCH v3 1/2 linux-user: Define TARGET_O_LARGEFILE for aarch64

In 050a1ba, when moving the macros from preprocessor-guarding to file-based definition, TARGET_O_LARGEFILE appeared to have been accidentally left off.

This may have correctness implication, but so far I was only confused by strace's output.

Fixes 050a1ba69a ("linux-user: move arm/aarch64/m68k fcntl definitions to [arm|aarch64|m68k]/target_fcntl.h")
---
 linux-user/aarch64/target_fcntl.h | 1 +
 1 file changed, 1 insertion(+)
linux-user/aarch64/target_fcntl.h +1 -0
index efdf6e5f05..55ab788a7c 100644
@@ -11,6 +11,7 @@
 #define TARGET_O_DIRECTORY      040000 /* must be a directory */
 #define TARGET_O_NOFOLLOW      0100000 /* don't follow links */
 #define TARGET_O_DIRECT        0200000 /* direct disk access hint */
+#define TARGET_O_LARGEFILE     0400000
 
 #include "../generic/fcntl.h"
 #endif
#2 lore raw

PATCH v3 2/2 linux-user: Fix openat() emulation to not modify atime

In reply to Shu-Chun Weng

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

---
 linux-user/syscall.c | 47 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 38 insertions(+), 9 deletions(-)
linux-user/syscall.c +38 -9
index e384e14248..7c3772301f 100644
@@ -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);
+
+        /* 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);
     }
 }
 
#3 lore raw

Re: [PATCH v3 2/2] linux-user: Fix openat() emulation to not modify atime

ping~

Quoted 111 lines · Shu-Chun Weng

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

---
 linux-user/syscall.c | 47 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 38 insertions(+), 9 deletions(-)
linux-user/syscall.c +1 -2
index e384e14248..7c3772301f 100644
@@ -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);
linux-user/syscall.c +17 -5
@@ -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

linux-user/syscall.c +2 -0
+         * `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)).

linux-user/syscall.c +4 -0
+         * 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)) {

linux-user/syscall.c +7 -0
+            /* 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);
+
+        /* if this is not a file from /proc/ filesystem, the fd is good

as-is */

linux-user/syscall.c +6 -2
+        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);
     }
 }
Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH v3 2/2] linux-user: Fix openat() emulation to not modify atime

In reply to Shu-Chun Weng
Quoted 71 lines · Shu-Chun Weng

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

---
  linux-user/syscall.c | 47 +++++++++++++++++++++++++++++++++++---------
  1 file changed, 38 insertions(+), 9 deletions(-)
linux-user/syscall.c +20 -7
index e384e14248..7c3772301f 100644
@@ -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,

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname, · line 8365

Quoted 10 lines
linux-user/syscall.c +10 -0
+         * `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

Helge

Quoted 22 lines
linux-user/syscall.c +8 -2
+
+        /* 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);
      }
  }
 
#5 lore raw

Re: [PATCH v3 2/2] linux-user: Fix openat() emulation to not modify atime

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

Quoted 131 lines · Helge Deller

linux-user/syscall.c static int open_net_route(CPUArchState *cpu_env,

Quoted 33 lines · Shu-Chun Weng

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

---
  linux-user/syscall.c | 47 +++++++++++++++++++++++++++++++++++---------
  1 file changed, 38 insertions(+), 9 deletions(-)
linux-user/syscall.c +0 -0
index e384e14248..7c3772301f 100644
@@ -8308,8 +8308,7 @@ static int open_net_route(CPUArchState *cpu_env,

int fd)

linux-user/syscall.c static int open_net_route(CPUArchState *cpu_env, · line 8308

Quoted 1 line
linux-user/syscall.c +0 -0
  int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char

*fname,

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int

Quoted 9 lines
linux-user/syscall.c +1 -2
                      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,

Quoted 16 lines
linux-user/syscall.c +7 -5
          { 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) {

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,

Quoted 10 lines
linux-user/syscall.c +10 -0
+            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

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int · line 8354

Quoted 1 line
linux-user/syscall.c +1 -0
+         * `realpath(3)` because it calls `readlink(2)` on symlinks

which

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int · line 8355

Quoted 1 line
linux-user/syscall.c +1 -0
+         * changes their atime. Note that since `/proc/self/exe` is a

symlink,

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int · line 8356

Quoted 1 line
linux-user/syscall.c +1 -0
+         * `pathname` will never resolve to it (neither will

realpath(3)).

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int · line 8357

Quoted 1 line
linux-user/syscall.c +1 -0
+         * That's why we check `fname` against the "/proc/" prefix

first.

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,

Quoted 3 lines
linux-user/syscall.c +3 -0
+         */
+        proc_name = g_strdup_printf("/proc/self/fd/%d", fd);
+        if (lstat(proc_name, &proc_stat) < 0 ||

!S_ISLNK(proc_stat.st_mode)) {

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int · line 8361

Quoted 1 line
linux-user/syscall.c +1 -0
+            /* No procfs or something weird. Not going to dig further.

*/

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname, · line 8365

Quoted 4 lines
linux-user/syscall.c +4 -0
+            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

Helge

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int · line 8367

Quoted 2 lines
linux-user/syscall.c +2 -0
+
+        /* if this is not a file from /proc/ filesystem, the fd is good

as-is */

linux-user/syscall.c int do_guest_openat(CPUArchState *cpu_env, int

Quoted 8 lines
linux-user/syscall.c +4 -0
+        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,

Quoted 12 lines
linux-user/syscall.c +2 -2
      }
 
      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);
      }
  }