Skip to contentSkip to conversation

linux-user: Fix handling when AT_EXECFD is 0

PATCH In review Vivian Wang 2 participants 4 messages Jul 23, 2024 – Jul 29, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover linux-user: Fix handling when AT_EXECFD is 0

Found when trying to build coreutils under linux-user as binfmt_misc interpreter with "open-binary" flag.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2448

Changes since v1 [1]:
- Removed old glibc check in patch 1
- Added Reviewed-by

[1]: https://lore.kernel.org/qemu-devel/[email protected]/

Vivian Wang (2):
util/getauxval: Ensure setting errno if not found linux-user/main: Check errno when getting AT_EXECFD

 linux-user/main.c | 3 ++-
 util/getauxval.c  | 6 +++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

--
2.45.1

#1 lore raw

PATCH v2 1/2 util/getauxval: Ensure setting errno if not found

Sometimes zero is a valid value for getauxval (e.g. AT_EXECFD). Make sure that we can distinguish between a valid zero value and a not found entry by setting errno.

Assumes that getauxval from sys/auxv.h sets errno correctly.

---
 util/getauxval.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
util/getauxval.c +5 -1
index b124107d61..fad16c406e 100644
@@ -95,6 +95,7 @@ unsigned long qemu_getauxval(unsigned long type)
         }
     }
 
+    errno = ENOENT;
     return 0;
 }
 
@@ -104,7 +105,9 @@ unsigned long qemu_getauxval(unsigned long type)
 unsigned long qemu_getauxval(unsigned long type)
 {
     unsigned long aux = 0;
-    elf_aux_info(type, &aux, sizeof(aux));
+    int ret = elf_aux_info(type, &aux, sizeof(aux));
+    if (ret != 0)
+        errno = ret;
     return aux;
 }
 
@@ -112,6 +115,7 @@ unsigned long qemu_getauxval(unsigned long type)
 
 unsigned long qemu_getauxval(unsigned long type)
 {
+    errno = ENOSYS;
     return 0;
 }
 
Signature · 1 line
2.45.1
#2 lore raw

PATCH v2 2/2 linux-user/main: Check errno when getting AT_EXECFD

In reply to Vivian Wang

It's possible for AT_EXECFD to end up with a valid value of 0. Check errno when using qemu_getauxval instead of return value to handle this case.

Not handling this case leads to a confusing condition where the executable ends up as fd 0, i.e. stdin.

Fixes 0b959cf5e4cc ("linux-user: Use qemu_getauxval for AT_EXECFD")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2448

---
 linux-user/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/main.c +2 -1
index 7d3cf45fa9..8143a0d4b0 100644
@@ -755,8 +755,9 @@ int main(int argc, char **argv, char **envp)
     /*
      * Manage binfmt-misc open-binary flag
      */
+    errno = 0;
     execfd = qemu_getauxval(AT_EXECFD);
-    if (execfd == 0) {
+    if (errno != 0) {
         execfd = open(exec_path, O_RDONLY);
         if (execfd < 0) {
             printf("Error while loading %s: %s\n", exec_path, strerror(errno));
Signature · 1 line
2.45.1
#3 lore raw
In reply to Vivian Wang
Quoted 15 lines · Vivian Wang

Found when trying to build coreutils under linux-user as binfmt_misc interpreter with "open-binary" flag.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2448

Changes since v1 [1]:
- Removed old glibc check in patch 1
- Added Reviewed-by

[1]: https://lore.kernel.org/qemu-devel/[email protected]/

Vivian Wang (2):
util/getauxval: Ensure setting errno if not found linux-user/main: Check errno when getting AT_EXECFD

Quoted 8 lines
  linux-user/main.c | 3 ++-
  util/getauxval.c  | 6 +++++-
  2 files changed, 7 insertions(+), 2 deletions(-)

--
2.45.1

Queued, with the braces in patch 1 fixed.

r~