linux-user: Use memfd for open syscall emulation

For certain paths in /proc, the open syscall is intercepted and the
returned file descriptor points to a temporary file with emulated
contents.

If TMPDIR is not accessible or writable for the current user (for
example in a read-only mounted chroot or container) tools such as ps
from procps may fail unexpectedly. Trying to read one of these paths
such as /proc/self/stat would return an error such as ENOENT or EROFS.

To relax the requirement on a writable TMPDIR, use memfd_create()
instead to create an anonymous file and return its file descriptor.

Signed-off-by: Rainer Müller <raimue@codingfarm.de>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20220729154951.76268-1-raimue@codingfarm.de>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
This commit is contained in:
Rainer Müller 2022-07-29 17:49:51 +02:00 committed by Laurent Vivier
parent f71fa4e3bb
commit 5b63de6b54

View file

@ -8260,6 +8260,11 @@ static int do_openat(CPUArchState *cpu_env, int dirfd, const char *pathname, int
char filename[PATH_MAX]; char filename[PATH_MAX];
int fd, r; int fd, r;
fd = memfd_create("qemu-open", 0);
if (fd < 0) {
if (errno != ENOSYS) {
return fd;
}
/* create temporary file to map stat to */ /* create temporary file to map stat to */
tmpdir = getenv("TMPDIR"); tmpdir = getenv("TMPDIR");
if (!tmpdir) if (!tmpdir)
@ -8270,6 +8275,7 @@ static int do_openat(CPUArchState *cpu_env, int dirfd, const char *pathname, int
return fd; return fd;
} }
unlink(filename); unlink(filename);
}
if ((r = fake_open->fill(cpu_env, fd))) { if ((r = fake_open->fill(cpu_env, fd))) {
int e = errno; int e = errno;