[1/2] stdlib: Fix __getrandom_nocancel type and arc4random usage (BZ #29638)

Message ID 20220929192756.3310704-1-adhemerval.zanella@linaro.org
State Committed
Commit 13db9ee2cb3b77e25f852be7d6952882e1be6f00
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

Commit Message

Adhemerval Zanella Sept. 29, 2022, 7:27 p.m. UTC
  Using an unsigned type prevents the fallback to be used if kernel
does not support getrandom syscall.

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

Comments

H.J. Lu Sept. 29, 2022, 7:39 p.m. UTC | #1
On Thu, Sep 29, 2022 at 12:28 PM Adhemerval Zanella via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> Using an unsigned type prevents the fallback to be used if kernel
> does not support getrandom syscall.
>
> Checked on x86_64-linux-gnu.
> ---
>  stdlib/arc4random.c                  | 2 +-
>  sysdeps/unix/sysv/linux/not-cancel.h | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c
> index e417ef624d..960a38f295 100644
> --- a/stdlib/arc4random.c
> +++ b/stdlib/arc4random.c
> @@ -34,7 +34,7 @@ void
>  __arc4random_buf (void *p, size_t n)
>  {
>    static int seen_initialized;
> -  size_t l;
> +  ssize_t l;
>    int fd;
>
>    if (n == 0)
> diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h
> index a263d294b1..cf35c8bfc9 100644
> --- a/sysdeps/unix/sysv/linux/not-cancel.h
> +++ b/sysdeps/unix/sysv/linux/not-cancel.h
> @@ -68,7 +68,7 @@ __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt)
>    INTERNAL_SYSCALL_CALL (writev, fd, iov, iovcnt);
>  }
>
> -static inline int
> +static inline ssize_t

Shouldn't it be __syscall_slong_t?

>  __getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
>  {
>    return INLINE_SYSCALL_CALL (getrandom, buf, buflen, flags);
> --
> 2.34.1
>
  
Adhemerval Zanella Sept. 29, 2022, 7:54 p.m. UTC | #2
On 29/09/22 16:39, H.J. Lu wrote:
> On Thu, Sep 29, 2022 at 12:28 PM Adhemerval Zanella via Libc-alpha
> <libc-alpha@sourceware.org> wrote:
>>
>> Using an unsigned type prevents the fallback to be used if kernel
>> does not support getrandom syscall.
>>
>> Checked on x86_64-linux-gnu.
>> ---
>>  stdlib/arc4random.c                  | 2 +-
>>  sysdeps/unix/sysv/linux/not-cancel.h | 2 +-
>>  2 files changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c
>> index e417ef624d..960a38f295 100644
>> --- a/stdlib/arc4random.c
>> +++ b/stdlib/arc4random.c
>> @@ -34,7 +34,7 @@ void
>>  __arc4random_buf (void *p, size_t n)
>>  {
>>    static int seen_initialized;
>> -  size_t l;
>> +  ssize_t l;
>>    int fd;
>>
>>    if (n == 0)
>> diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h
>> index a263d294b1..cf35c8bfc9 100644
>> --- a/sysdeps/unix/sysv/linux/not-cancel.h
>> +++ b/sysdeps/unix/sysv/linux/not-cancel.h
>> @@ -68,7 +68,7 @@ __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt)
>>    INTERNAL_SYSCALL_CALL (writev, fd, iov, iovcnt);
>>  }
>>
>> -static inline int
>> +static inline ssize_t
> 
> Shouldn't it be __syscall_slong_t?

Would it make any difference? 'size_t' is still 32 bit on x32, so arc4random_buf
would not be able to handle buffer larger than 32 bits.  Same for getrandom,
which return ssize_t.

> 
>>  __getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
>>  {
>>    return INLINE_SYSCALL_CALL (getrandom, buf, buflen, flags);
>> --
>> 2.34.1
>>
> 
> 
>
  
Andreas Schwab Sept. 29, 2022, 8:01 p.m. UTC | #3
On Sep 29 2022, H.J. Lu via Libc-alpha wrote:

>> diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h
>> index a263d294b1..cf35c8bfc9 100644
>> --- a/sysdeps/unix/sysv/linux/not-cancel.h
>> +++ b/sysdeps/unix/sysv/linux/not-cancel.h
>> @@ -68,7 +68,7 @@ __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt)
>>    INTERNAL_SYSCALL_CALL (writev, fd, iov, iovcnt);
>>  }
>>
>> -static inline int
>> +static inline ssize_t
>
> Shouldn't it be __syscall_slong_t?

getrandom can never return more than the size of the buffer.
  
Wilco Dijkstra Sept. 30, 2022, 3:05 p.m. UTC | #4
Hi Adhemerval,

This looks good to me (and will work on ILP32).

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

Cheers,
Wilco


From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
 
Using an unsigned type prevents the fallback to be used if kernel
does not support getrandom syscall.

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

diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c
index e417ef624d..960a38f295 100644
--- a/stdlib/arc4random.c
+++ b/stdlib/arc4random.c
@@ -34,7 +34,7 @@ void
 __arc4random_buf (void *p, size_t n)
 {
   static int seen_initialized;
-  size_t l;
+  ssize_t l;
   int fd;
 
   if (n == 0)
diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h
index a263d294b1..cf35c8bfc9 100644
--- a/sysdeps/unix/sysv/linux/not-cancel.h
+++ b/sysdeps/unix/sysv/linux/not-cancel.h
@@ -68,7 +68,7 @@ __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt)
   INTERNAL_SYSCALL_CALL (writev, fd, iov, iovcnt);
 }
 
-static inline int
+static inline ssize_t
 __getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
 {
   return INLINE_SYSCALL_CALL (getrandom, buf, buflen, flags);
  
Yu-Chien Peter Lin Oct. 1, 2022, 12:16 p.m. UTC | #5
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 e417ef624d..960a38f295 100644
--- a/stdlib/arc4random.c
+++ b/stdlib/arc4random.c
@@ -34,7 +34,7 @@  void
 __arc4random_buf (void *p, size_t n)
 {
   static int seen_initialized;
-  size_t l;
+  ssize_t l;
   int fd;
 
   if (n == 0)
diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h
index a263d294b1..cf35c8bfc9 100644
--- a/sysdeps/unix/sysv/linux/not-cancel.h
+++ b/sysdeps/unix/sysv/linux/not-cancel.h
@@ -68,7 +68,7 @@  __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt)
   INTERNAL_SYSCALL_CALL (writev, fd, iov, iovcnt);
 }
 
-static inline int
+static inline ssize_t
 __getrandom_nocancel (void *buf, size_t buflen, unsigned int flags)
 {
   return INLINE_SYSCALL_CALL (getrandom, buf, buflen, flags);