qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Helge Deller" <deller@gmx.de>,
	"Daniel P . Berrangé" <berrange@redhat.com>
Subject: [PULL 6/6] linux-user: Fix openat() emulation to correctly detect accesses to /proc
Date: Wed,  9 Aug 2023 10:50:06 -0700	[thread overview]
Message-ID: <20230809175006.242479-7-richard.henderson@linaro.org> (raw)
In-Reply-To: <20230809175006.242479-1-richard.henderson@linaro.org>

From: Helge Deller <deller@gmx.de>

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.

Signed-off-by: Helge Deller <deller@gmx.de>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20230803214450.647040-2-deller@gmx.de>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 linux-user/syscall.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 12ebc70df5..9353268cc1 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -8557,9 +8557,12 @@ static int open_hardware(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)
 {
+    g_autofree char *proc_name = NULL;
+    const char *pathname;
     struct fake_open {
         const char *filename;
         int (*fill)(CPUArchState *cpu_env, int fd);
@@ -8585,6 +8588,14 @@ 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 */
+    proc_name = realpath(fname, NULL);
+    if (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);
-- 
2.34.1



  parent reply	other threads:[~2023-08-09 17:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-09 17:50 [PULL 0/6] linux-user late fixes Richard Henderson
2023-08-09 17:50 ` [PULL 1/6] qemu/osdep: Remove fallback for MAP_FIXED_NOREPLACE Richard Henderson
2023-08-09 17:50 ` [PULL 2/6] linux-user: Split out do_mmap Richard Henderson
2023-08-09 17:50 ` [PULL 3/6] linux-user: Use ARRAY_SIZE with bitmask_transtbl Richard Henderson
2023-08-09 17:50 ` [PULL 4/6] tests/tcg: Disable filename test for info proc mappings Richard Henderson
2023-08-09 17:50 ` [PULL 5/6] util/interval-tree: Check root for null in interval_tree_iter_first Richard Henderson
2023-08-09 17:50 ` Richard Henderson [this message]
2023-08-09 22:04 ` [PULL 0/6] linux-user late fixes Richard Henderson

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=20230809175006.242479-7-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=berrange@redhat.com \
    --cc=deller@gmx.de \
    --cc=qemu-devel@nongnu.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).