Skip to content

Commit

Permalink
linux-user: Fix openat() emulation to correctly detect accesses to /proc
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
hdeller authored and rth7680 committed Aug 9, 2023
1 parent 47d1e98 commit b800205
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion linux-user/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b800205

Please sign in to comment.