From 734a659ad264ac080457167e845ffabbaaa66d0e Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Thu, 28 Jul 2022 16:14:06 +0100 Subject: [PATCH 1/3] linux-user/flatload.c: Fix setting of image_info::end_code The flatload loader sets the end_code field in the image_info struct incorrectly, due to a typo. This is a very long-standing bug (dating all the way back to when the bFLT loader was added in 2006), but has gone unnoticed because (a) most people don't use bFLT binaries (b) we don't actually do anything with the end_code field, except print it in debugging traces and pass it to TCG plugins Fix the typo. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1119 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-Id: <20220728151406.2262862-1-peter.maydell@linaro.org> Signed-off-by: Laurent Vivier --- linux-user/flatload.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux-user/flatload.c b/linux-user/flatload.c index e4c2f89a22..e99570ca18 100644 --- a/linux-user/flatload.c +++ b/linux-user/flatload.c @@ -808,7 +808,7 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info) /* Stash our initial stack pointer into the mm structure */ info->start_code = libinfo[0].start_code; - info->end_code = libinfo[0].start_code = libinfo[0].text_len; + info->end_code = libinfo[0].start_code + libinfo[0].text_len; info->start_data = libinfo[0].start_data; info->end_data = libinfo[0].end_data; info->start_brk = libinfo[0].start_brk; From f71fa4e3bb22f534ee668e7f4bdf64e59c193afd Mon Sep 17 00:00:00 2001 From: Ilya Leoshkevich Date: Mon, 25 Jul 2022 15:41:00 +0200 Subject: [PATCH 2/3] linux-user: Do not treat madvise()'s advice as a bitmask Advice is enum, not flags. Doing (advice & MADV_DONTNEED) also matches e.g. MADV_MERGEABLE. Signed-off-by: Ilya Leoshkevich Reviewed-by: Laurent Vivier Message-Id: <20220725134100.128035-1-iii@linux.ibm.com> Fixes: 892a4f6a750a ("linux-user: Add partial support for MADV_DONTNEED") Signed-off-by: Laurent Vivier --- linux-user/mmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 4e7a6be6ee..edceaca4a8 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -891,7 +891,7 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) * anonymous mappings. In this case passthrough is safe, so do it. */ mmap_lock(); - if ((advice & MADV_DONTNEED) && + if (advice == MADV_DONTNEED && can_passthrough_madv_dontneed(start, end)) { ret = get_errno(madvise(g2h_untagged(start), len, MADV_DONTNEED)); } From 5b63de6b54add51822db3c89325c6fc05534a54c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rainer=20M=C3=BCller?= Date: Fri, 29 Jul 2022 17:49:51 +0200 Subject: [PATCH 3/3] linux-user: Use memfd for open syscall emulation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Richard Henderson Message-Id: <20220729154951.76268-1-raimue@codingfarm.de> Signed-off-by: Laurent Vivier --- linux-user/syscall.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index b27a6552aa..ef53feb5ab 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8260,16 +8260,22 @@ static int do_openat(CPUArchState *cpu_env, int dirfd, const char *pathname, int char filename[PATH_MAX]; int fd, r; - /* create temporary file to map stat to */ - tmpdir = getenv("TMPDIR"); - if (!tmpdir) - tmpdir = "/tmp"; - snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir); - fd = mkstemp(filename); + fd = memfd_create("qemu-open", 0); if (fd < 0) { - return fd; + if (errno != ENOSYS) { + return fd; + } + /* create temporary file to map stat to */ + tmpdir = getenv("TMPDIR"); + if (!tmpdir) + tmpdir = "/tmp"; + snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir); + fd = mkstemp(filename); + if (fd < 0) { + return fd; + } + unlink(filename); } - unlink(filename); if ((r = fake_open->fill(cpu_env, fd))) { int e = errno;