Skip to contentSkip to conversation

linux-user: Fix handling when AT_EXECFD is 0

PATCH Superseded Vivian Wang 3 participants 8 messages Jul 21, 2024 – Jul 22, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

I'm unsure whether the version detection in patch 1 makes sense. Please advise on how to handle situations like this.

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

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  | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 3 deletions(-)

--
2.45.1

#1 lore raw

PATCH 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.

Ignore getauxval from sys/auxv.h on glibc < 2.19 because it does not set errno.

---
 util/getauxval.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
util/getauxval.c +12 -2
index b124107d61..f1008bdc59 100644
@@ -24,7 +24,13 @@
 
 #include "qemu/osdep.h"
 
-#ifdef CONFIG_GETAUXVAL
+/* If glibc < 2.19, getauxval can't be used because it does not set errno if
+   entry is not found. */
+#if defined(CONFIG_GETAUXVAL) && \
+    (!defined(__GLIBC__) \
+        || __GLIBC__ > 2 \
+        || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19))
+
 /* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for
    the system declaration of getauxval pulls in the system <elf.h>, which
    conflicts with qemu's version.  */
@@ -95,6 +101,7 @@ unsigned long qemu_getauxval(unsigned long type)
         }
     }
 
+    errno = ENOENT;
     return 0;
 }
 
@@ -104,7 +111,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 +121,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

RE: [PATCH 1/2] util/getauxval: Ensure setting errno if not found

Quoted 29 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Vivian Wang
Sent: Sunday, July 21, 2024 5:08 PM
To: [email protected]
Cc: Vivian Wang <[email protected]>; Richard Henderson <[email protected]>; Laurent Vivier <[email protected]>
Subject: [PATCH 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.

Ignore getauxval from sys/auxv.h on glibc < 2.19 because it does not set errno.

---
 util/getauxval.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
util/getauxval.c +0 -0
index b124107d61..f1008bdc59 100644

util/getauxval.c line 32

Quoted 10 lines
util/getauxval.c +6 -1
@@ -24,7 +24,13 @@
 
 #include "qemu/osdep.h"
 
-#ifdef CONFIG_GETAUXVAL
+/* If glibc < 2.19, getauxval can't be used because it does not set errno if
+   entry is not found. */
+#if defined(CONFIG_GETAUXVAL) && \
+    (!defined(__GLIBC__) \
+        || __GLIBC__ > 2 \
+        || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19))

you can use GLIB_CHECK_VERSION(2, 19, 0) instead

Quoted 34 lines
util/getauxval.c +6 -2
+
 /* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for
    the system declaration of getauxval pulls in the system <elf.h>, which
    conflicts with qemu's version.  */
@@ -95,6 +101,7 @@ unsigned long qemu_getauxval(unsigned long type)
         }
     }
 
+    errno = ENOENT;
     return 0;
 }
 
@@ -104,7 +111,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 +121,7 @@ unsigned long qemu_getauxval(unsigned long type)
 
 unsigned long qemu_getauxval(unsigned long type)
 {
+    errno = ENOSYS;
     return 0;
 }
 
--

2.45.1

#3 lore raw

Re: [PATCH 1/2] util/getauxval: Ensure setting errno if not found

Quoted 31 lines · Xingtao Yao (Fujitsu)
Quoted 31 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Vivian Wang
Sent: Sunday, July 21, 2024 5:08 PM
To: [email protected]
Cc: Vivian Wang <[email protected]>; Richard Henderson <[email protected]>; Laurent Vivier <[email protected]>
Subject: [PATCH 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.

Ignore getauxval from sys/auxv.h on glibc < 2.19 because it does not set errno.

---
 util/getauxval.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
util/getauxval.c +0 -0
index b124107d61..f1008bdc59 100644
@@ -24,7 +24,13 @@
 
 #include "qemu/osdep.h"
 
Quoted 8 lines

util/getauxval.c line 32

Quoted 7 lines
util/getauxval.c +6 -1
-#ifdef CONFIG_GETAUXVAL
+/* If glibc < 2.19, getauxval can't be used because it does not set errno if
+   entry is not found. */
+#if defined(CONFIG_GETAUXVAL) && \
+    (!defined(__GLIBC__) \
+        || __GLIBC__ > 2 \
+        || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19))

you can use GLIB_CHECK_VERSION(2, 19, 0) instead

That wouldn't work. I'm testing for glibc, not glib.

Quoted 34 lines
Quoted 34 lines
util/getauxval.c +6 -2
+
 /* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for
    the system declaration of getauxval pulls in the system <elf.h>, which
    conflicts with qemu's version.  */
@@ -95,6 +101,7 @@ unsigned long qemu_getauxval(unsigned long type)
         }
     }
 
+    errno = ENOENT;
     return 0;
 }
 
@@ -104,7 +111,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 +121,7 @@ unsigned long qemu_getauxval(unsigned long type)
 
 unsigned long qemu_getauxval(unsigned long type)
 {
+    errno = ENOSYS;
     return 0;
 }
 
--

2.45.1

#4 lore raw

RE: [PATCH 1/2] util/getauxval: Ensure setting errno if not found

Quoted 50 lines

-----Original Message-----
From: Vivian Wang <[email protected]>
Sent: Monday, July 22, 2024 4:24 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Cc: Richard Henderson <[email protected]>; Laurent Vivier <[email protected]>
Subject: Re: [PATCH 1/2] util/getauxval: Ensure setting errno if not found

Quoted 4 lines · Xingtao Yao (Fujitsu)
Quoted 4 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of

Vivian

Quoted 27 lines
Quoted 27 lines

Wang
Sent: Sunday, July 21, 2024 5:08 PM
To: [email protected]
Cc: Vivian Wang <[email protected]>; Richard Henderson <[email protected]>; Laurent Vivier <[email protected]>
Subject: [PATCH 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.

Ignore getauxval from sys/auxv.h on glibc < 2.19 because it does not set errno.

---
 util/getauxval.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
util/getauxval.c +0 -0
index b124107d61..f1008bdc59 100644
@@ -24,7 +24,13 @@
 
 #include "qemu/osdep.h"
 
Quoted 8 lines

util/getauxval.c line 32

Quoted 7 lines
util/getauxval.c +6 -1
-#ifdef CONFIG_GETAUXVAL
+/* If glibc < 2.19, getauxval can't be used because it does not set errno if
+   entry is not found. */
+#if defined(CONFIG_GETAUXVAL) && \
+    (!defined(__GLIBC__) \
+        || __GLIBC__ > 2 \
+        || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19))

you can use GLIB_CHECK_VERSION(2, 19, 0) instead

Quoted 1 line

That wouldn't work. I'm testing for glibc, not glib.

sorry, I misunderstood.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
util/getauxval.c +6 -2
+
 /* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for
    the system declaration of getauxval pulls in the system <elf.h>, which
    conflicts with qemu's version.  */
@@ -95,6 +101,7 @@ unsigned long qemu_getauxval(unsigned long type)
         }
     }
 
+    errno = ENOENT;
     return 0;
 }
 
@@ -104,7 +111,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 +121,7 @@ unsigned long qemu_getauxval(unsigned long type)
 
 unsigned long qemu_getauxval(unsigned long type)
 {
+    errno = ENOSYS;
     return 0;
 }
 
--

2.45.1

#5 lore raw

Re: [PATCH 1/2] util/getauxval: Ensure setting errno if not found

In reply to Vivian Wang
Quoted 17 lines · Vivian Wang

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.

Ignore getauxval from sys/auxv.h on glibc < 2.19 because it does not set errno.

---
  util/getauxval.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
util/getauxval.c +0 -0
index b124107d61..f1008bdc59 100644

util/getauxval.c line 32

Quoted 10 lines
util/getauxval.c +6 -1
@@ -24,7 +24,13 @@
 
  #include "qemu/osdep.h"
 
-#ifdef CONFIG_GETAUXVAL
+/* If glibc < 2.19, getauxval can't be used because it does not set errno if
+   entry is not found. */
+#if defined(CONFIG_GETAUXVAL) && \
+    (!defined(__GLIBC__) \
+        || __GLIBC__ > 2 \
+        || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19))

Version 2.19 is so long ago that all supported OS releases have something newer. For Alpine Linux using musl, the first commit of getauxval had the errno setting.

Therefore we don't need this check.

util/getauxval.c unsigned long qemu_getauxval(unsigned long type) · line 106

Quoted 7 lines
util/getauxval.c +1 -0
@@ -95,6 +101,7 @@ unsigned long qemu_getauxval(unsigned long type)
          }
      }
 
+    errno = ENOENT;
      return 0;
  }

Ok.

util/getauxval.c unsigned long qemu_getauxval(unsigned long type) · line 116

Quoted 8 lines
util/getauxval.c +3 -1
@@ -104,7 +111,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;

Braces required.

util/getauxval.c unsigned long qemu_getauxval(unsigned long type) · line 126

Quoted 10 lines
util/getauxval.c +1 -0
      return aux;
  }
 
@@ -112,6 +121,7 @@ unsigned long qemu_getauxval(unsigned long type)
 
  unsigned long qemu_getauxval(unsigned long type)
  {
+    errno = ENOSYS;
      return 0;
  }

Ok.

r~

#6 lore raw

PATCH 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
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] linux-user/main: Check errno when getting AT_EXECFD

Quoted 19 lines · 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 +0 -0
index 7d3cf45fa9..8143a0d4b0 100644

linux-user/main.c int main(int argc, char **argv, char **envp) · line 763

Quoted 10 lines
linux-user/main.c +2 -1
@@ -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));

r~