[v3,2/2] malloc: Do not clobber errno on __getrandom_nocancel (BZ #29624)

Message ID 20220929192756.3310704-2-adhemerval.zanella@linaro.org
State Committed
Headers
Series [1/2] stdlib: Fix __getrandom_nocancel type and arc4random usage (BZ #29638) |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Adhemerval Zanella Netto Sept. 29, 2022, 7:27 p.m. UTC
  Use INTERNAL_SYSCALL_CALL instead of INLINE_SYSCALL_CALL.  This
requires emulate the semantic for hurd call (so __arc4random_buf
uses the fallback).

Checked on x86_64-linux-gnu.
---
 stdlib/arc4random.c                  |  2 +-
 sysdeps/mach/hurd/not-cancel.h       | 12 ++++++++++--
 sysdeps/unix/sysv/linux/not-cancel.h |  2 +-
 3 files changed, 12 insertions(+), 4 deletions(-)
  

Comments

Wilco Dijkstra Sept. 30, 2022, 3:09 p.m. UTC | #1
Hi Adhemerval,

This looks good to me assuming Hurd is 32-bit only.

Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>

Cheers,
Wilco


From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
 
Use INTERNAL_SYSCALL_CALL instead of INLINE_SYSCALL_CALL.  This
requires emulate the semantic for hurd call (so __arc4random_buf
uses the fallback).

Checked on x86_64-linux-gnu.
---
 stdlib/arc4random.c                  |  2 +-
 sysdeps/mach/hurd/not-cancel.h       | 12 ++++++++++--
 sysdeps/unix/sysv/linux/not-cancel.h |  2 +-
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c
index 960a38f295..c126c601bf 100644
--- a/stdlib/arc4random.c
+++ b/stdlib/arc4random.c
@@ -51,7 +51,7 @@ __arc4random_buf (void *p, size_t n)
           n -= l;
           continue; /* Interrupted by a signal; keep going.  */
         }
-      else if (l < 0 && errno == ENOSYS)
+      else if (l == -ENOSYS)
         break; /* No syscall, so fallback to /dev/urandom.  */
       arc4random_getrandom_failure ();
     }
diff --git a/sysdeps/mach/hurd/not-cancel.h b/sysdeps/mach/hurd/not-cancel.h
index ae58b734e3..5d2d2b4b8a 100644
--- a/sysdeps/mach/hurd/not-cancel.h
+++ b/sysdeps/mach/hurd/not-cancel.h
@@ -25,6 +25,7 @@
 #include <sys/wait.h>
 #include <time.h>
 #include <sys/uio.h>
+#include <sys/random.h>
 #include <hurd.h>
 #include <hurd/fd.h>
 
@@ -75,8 +76,15 @@ __typeof (__fcntl) __fcntl_nocancel;
 #define __fcntl64_nocancel(...) \
   __fcntl_nocancel (__VA_ARGS__)
 
-#define __getrandom_nocancel(buf, size, flags) \
-  __getrandom (buf, size, flags)
+static inline int
+__getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
+{
+  int save_errno = errno;
+  int r = __getrandom (buf, buflen, flags);
+  r = r == -1 ? -errno : r;
+  __set_errno (save_errno);
+  return r;
+}
 
 #define __poll_infinity_nocancel(fds, nfds) \
   __poll (fds, nfds, -1)
diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h
index cf35c8bfc9..93615de681 100644
--- a/sysdeps/unix/sysv/linux/not-cancel.h
+++ b/sysdeps/unix/sysv/linux/not-cancel.h
@@ -71,7 +71,7 @@ __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt)
 static inline ssize_t
 __getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
 {
-  return INLINE_SYSCALL_CALL (getrandom, buf, buflen, flags);
+  return INTERNAL_SYSCALL_CALL (getrandom, buf, buflen, flags);
 }
 
 static inline int
  
Yann Droneaud Sept. 30, 2022, 4:29 p.m. UTC | #2
Hi,

Le 30/09/2022 à 17:09, Wilco Dijkstra via Libc-alpha a écrit :
> Hi Adhemerval,
>
> This looks good to me assuming Hurd is 32-bit only.
>
> Reviewed-by: Wilco Dijkstra<Wilco.Dijkstra@arm.com>
>
> Cheers,
> Wilco
>
>
> From: Adhemerval Zanella<adhemerval.zanella@linaro.org>
>   
> Use INTERNAL_SYSCALL_CALL instead of INLINE_SYSCALL_CALL.  This
> requires emulate the semantic for hurd call (so __arc4random_buf
> uses the fallback).
>
> Checked on x86_64-linux-gnu.
> ---
> diff --git a/sysdeps/mach/hurd/not-cancel.h b/sysdeps/mach/hurd/not-cancel.h
> index ae58b734e3..5d2d2b4b8a 100644
> --- a/sysdeps/mach/hurd/not-cancel.h
> +++ b/sysdeps/mach/hurd/not-cancel.h
> @@ -25,6 +25,7 @@
>   #include <sys/wait.h>
>   #include <time.h>
>   #include <sys/uio.h>
> +#include <sys/random.h>
>   #include <hurd.h>
>   #include <hurd/fd.h>
>   
> @@ -75,8 +76,15 @@ __typeof (__fcntl) __fcntl_nocancel;
>   #define __fcntl64_nocancel(...) \
>     __fcntl_nocancel (__VA_ARGS__)
>   
> -#define __getrandom_nocancel(buf, size, flags) \
> -  __getrandom (buf, size, flags)
> +static inline int


ssize_t


> +__getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
> +{
> +  int save_errno = errno;
> +  int r = __getrandom (buf, buflen, flags);

ssize_t r


> +  r = r == -1 ? -errno : r;
> +  __set_errno (save_errno);
> +  return r;
> +}
>   

Regards.
  
Adhemerval Zanella Netto Sept. 30, 2022, 4:37 p.m. UTC | #3
On 30/09/22 13:29, Yann Droneaud wrote:
> Hi,
> 
> Le 30/09/2022 à 17:09, Wilco Dijkstra via Libc-alpha a écrit :
>> Hi Adhemerval,
>>
>> This looks good to me assuming Hurd is 32-bit only.
>>
>> Reviewed-by: Wilco Dijkstra<Wilco.Dijkstra@arm.com>
>>
>> Cheers,
>> Wilco
>>
>>
>> From: Adhemerval Zanella<adhemerval.zanella@linaro.org>
>>   Use INTERNAL_SYSCALL_CALL instead of INLINE_SYSCALL_CALL.  This
>> requires emulate the semantic for hurd call (so __arc4random_buf
>> uses the fallback).
>>
>> Checked on x86_64-linux-gnu.
>> ---
>> diff --git a/sysdeps/mach/hurd/not-cancel.h b/sysdeps/mach/hurd/not-cancel.h
>> index ae58b734e3..5d2d2b4b8a 100644
>> --- a/sysdeps/mach/hurd/not-cancel.h
>> +++ b/sysdeps/mach/hurd/not-cancel.h
>> @@ -25,6 +25,7 @@
>>   #include <sys/wait.h>
>>   #include <time.h>
>>   #include <sys/uio.h>
>> +#include <sys/random.h>
>>   #include <hurd.h>
>>   #include <hurd/fd.h>
>>   @@ -75,8 +76,15 @@ __typeof (__fcntl) __fcntl_nocancel;
>>   #define __fcntl64_nocancel(...) \
>>     __fcntl_nocancel (__VA_ARGS__)
>>   -#define __getrandom_nocancel(buf, size, flags) \
>> -  __getrandom (buf, size, flags)
>> +static inline int
> 
> 
> ssize_t
> 
> 
>> +__getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
>> +{
>> +  int save_errno = errno;
>> +  int r = __getrandom (buf, buflen, flags);
> 
> ssize_t r

Ack.
  
Yu-Chien Peter Lin Oct. 1, 2022, 12:21 p.m. UTC | #4
Hi Adhemerval,

Tested on riscv[32|64]-linux-gnu
LGTM.

Reviewed-by: Yu Chien Peter Lin <peterlin@andestech.com>

Thanks,
Peter Lin
  

Patch

diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c
index 960a38f295..c126c601bf 100644
--- a/stdlib/arc4random.c
+++ b/stdlib/arc4random.c
@@ -51,7 +51,7 @@  __arc4random_buf (void *p, size_t n)
 	  n -= l;
 	  continue; /* Interrupted by a signal; keep going.  */
 	}
-      else if (l < 0 && errno == ENOSYS)
+      else if (l == -ENOSYS)
 	break; /* No syscall, so fallback to /dev/urandom.  */
       arc4random_getrandom_failure ();
     }
diff --git a/sysdeps/mach/hurd/not-cancel.h b/sysdeps/mach/hurd/not-cancel.h
index ae58b734e3..5d2d2b4b8a 100644
--- a/sysdeps/mach/hurd/not-cancel.h
+++ b/sysdeps/mach/hurd/not-cancel.h
@@ -25,6 +25,7 @@ 
 #include <sys/wait.h>
 #include <time.h>
 #include <sys/uio.h>
+#include <sys/random.h>
 #include <hurd.h>
 #include <hurd/fd.h>
 
@@ -75,8 +76,15 @@  __typeof (__fcntl) __fcntl_nocancel;
 #define __fcntl64_nocancel(...) \
   __fcntl_nocancel (__VA_ARGS__)
 
-#define __getrandom_nocancel(buf, size, flags) \
-  __getrandom (buf, size, flags)
+static inline int
+__getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
+{
+  int save_errno = errno;
+  int r = __getrandom (buf, buflen, flags);
+  r = r == -1 ? -errno : r;
+  __set_errno (save_errno);
+  return r;
+}
 
 #define __poll_infinity_nocancel(fds, nfds) \
   __poll (fds, nfds, -1)
diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h
index cf35c8bfc9..93615de681 100644
--- a/sysdeps/unix/sysv/linux/not-cancel.h
+++ b/sysdeps/unix/sysv/linux/not-cancel.h
@@ -71,7 +71,7 @@  __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt)
 static inline ssize_t
 __getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
 {
-  return INLINE_SYSCALL_CALL (getrandom, buf, buflen, flags);
+  return INTERNAL_SYSCALL_CALL (getrandom, buf, buflen, flags);
 }
 
 static inline int