Skip to contentSkip to conversation

linux-user: Fix openat() emulation to correctly detect accesses to /proc

PATCH In review Helge Deller 2 participants 2 messages Aug 1, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Fix openat() emulation to correctly detect accesses to /proc

In qemu we catch accesses to files like /proc/cpuinfo or /proc/net/route and return to the guest contents which would be visible on a real system (instead what the host would show).

This patch fixes a bug, where for example the accesses
cat /proc////cpuinfo
or

    cd /proc && cat cpuinfo

will not be recognized by qemu and where qemu will wrongly show the contents of the host's /proc/cpuinfo file.

linux-user/syscall.c +10 -1
index 917c388073..bb864c2bb3 100644
@@ -8531,9 +8531,11 @@ static int open_cpuinfo(CPUArchState *cpu_env, int fd)
 }
 #endif
 
-int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
+int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
                     int flags, mode_t mode, bool safe)
 {
+    char proc_name[PATH_MAX];
+    const char *pathname;
     struct fake_open {
         const char *filename;
         int (*fill)(CPUArchState *cpu_env, int fd);
@@ -8560,6 +8562,13 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
         { NULL, NULL, NULL }
     };
 
+    /* if this is a file from /proc/ filesystem, expand full name */
+    if (realpath(fname, proc_name) && strncmp(proc_name, "/proc/", 6) == 0) {
+        pathname = proc_name;
+    } else {
+        pathname = fname;
+    }
+
     if (is_proc_myself(pathname, "exe")) {
         if (safe) {
             return safe_openat(dirfd, exec_path, flags, mode);
#1 lore raw
Quoted 18 lines · Helge Deller

In qemu we catch accesses to files like /proc/cpuinfo or /proc/net/route and return to the guest contents which would be visible on a real system (instead what the host would show).

This patch fixes a bug, where for example the accesses
cat /proc////cpuinfo
or

    cd /proc && cat cpuinfo

will not be recognized by qemu and where qemu will wrongly show the contents of the host's /proc/cpuinfo file.

linux-user/syscall.c +0 -0
index 917c388073..bb864c2bb3 100644

linux-user/syscall.c static int open_cpuinfo(CPUArchState *cpu_env, int fd) · line 8537

Quoted 8 lines
linux-user/syscall.c +2 -1
@@ -8531,9 +8531,11 @@ static int open_cpuinfo(CPUArchState *cpu_env, int fd)
 }
 #endif
 
-int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
+int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
                     int flags, mode_t mode, bool safe)
 {
+    char proc_name[PATH_MAX];

No PATH_MAX buffers declared on the stack please.

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

Quoted 10 lines
linux-user/syscall.c +3 -0
+    const char *pathname;
     struct fake_open {
         const char *filename;
         int (*fill)(CPUArchState *cpu_env, int fd);
@@ -8560,6 +8562,13 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
         { NULL, NULL, NULL }
     };
 
+    /* if this is a file from /proc/ filesystem, expand full name */
+    if (realpath(fname, proc_name) && strncmp(proc_name, "/proc/", 6) == 0) {

QEMU relies on the extended semantics of realpath() where passing NULL for 'proc_name' causes it to allocate a buffer of the correct size and return the new buffer. Using that avoids PATH_MAX variables.

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

Quoted 4 lines
linux-user/syscall.c +4 -0
+        pathname = proc_name;
+    } else {
+        pathname = fname;
+    }

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|