public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY
@ 2024-01-04  0:54 Xi Ruoyao
  2024-01-04  9:51 ` Florian Weimer
  0 siblings, 1 reply; 6+ messages in thread
From: Xi Ruoyao @ 2024-01-04  0:54 UTC (permalink / raw)
  To: libc-alpha; +Cc: Adhemerval Zanella Netto, Andreas K . Huettel, Xi Ruoyao

The _nocancel functions returns errors as negative values, i. e. -EINTR
for interrupted system call.  But TEMP_FAILURE_RETRY expects the errors
as errno, thus using TEMP_FAILURE_RETRY is incorrect here.

Add a customized TEMP_FAILURE_RETRY_NEG macro and use it instead of
TEMP_FAILURE_RETRY to fix the issue.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 stdlib/arc4random.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/stdlib/arc4random.c b/stdlib/arc4random.c
index 3ae8fc1302..41f24c124c 100644
--- a/stdlib/arc4random.c
+++ b/stdlib/arc4random.c
@@ -30,6 +30,15 @@ arc4random_getrandom_failure (void)
   __libc_fatal ("Fatal glibc error: cannot get entropy for arc4random\n");
 }
 
+/* Special version of TEMP_FAILURE_RETRY for error values as negative
+   values.  */
+#define TEMP_FAILURE_RETRY_NEG(expression)	\
+  (__extension__				\
+    ({ long int __result;			\
+       do __result = (long int) (expression);	\
+       while (__result == -EINTR);		\
+       __result; }))
+
 void
 __arc4random_buf (void *p, size_t n)
 {
@@ -42,7 +51,7 @@ __arc4random_buf (void *p, size_t n)
 
   for (;;)
     {
-      l = TEMP_FAILURE_RETRY (__getrandom_nocancel (p, n, 0));
+      l = TEMP_FAILURE_RETRY_NEG (__getrandom_nocancel (p, n, 0));
       if (l > 0)
 	{
 	  if ((size_t) l == n)
@@ -60,24 +69,24 @@ __arc4random_buf (void *p, size_t n)
     {
       /* Poll /dev/random as an approximation of RNG initialization.  */
       struct pollfd pfd = { .events = POLLIN };
-      pfd.fd = TEMP_FAILURE_RETRY (
+      pfd.fd = TEMP_FAILURE_RETRY_NEG (
 	  __open64_nocancel ("/dev/random", O_RDONLY | O_CLOEXEC | O_NOCTTY));
       if (pfd.fd < 0)
 	arc4random_getrandom_failure ();
-      if (TEMP_FAILURE_RETRY (__poll_infinity_nocancel (&pfd, 1)) < 0)
+      if (TEMP_FAILURE_RETRY_NEG (__poll_infinity_nocancel (&pfd, 1)) < 0)
 	arc4random_getrandom_failure ();
       if (__close_nocancel (pfd.fd) < 0)
 	arc4random_getrandom_failure ();
       atomic_store_relaxed (&seen_initialized, 1);
     }
 
-  fd = TEMP_FAILURE_RETRY (
+  fd = TEMP_FAILURE_RETRY_NEG (
       __open64_nocancel ("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOCTTY));
   if (fd < 0)
     arc4random_getrandom_failure ();
   for (;;)
     {
-      l = TEMP_FAILURE_RETRY (__read_nocancel (fd, p, n));
+      l = TEMP_FAILURE_RETRY_NEG (__read_nocancel (fd, p, n));
       if (l <= 0)
 	arc4random_getrandom_failure ();
       if ((size_t) l == n)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY
  2024-01-04  0:54 [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY Xi Ruoyao
@ 2024-01-04  9:51 ` Florian Weimer
  2024-01-04 12:13   ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Weimer @ 2024-01-04  9:51 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: libc-alpha, Adhemerval Zanella Netto, Andreas K . Huettel

* Xi Ruoyao:

> The _nocancel functions returns errors as negative values, i. e. -EINTR
> for interrupted system call.  But TEMP_FAILURE_RETRY expects the errors
> as errno, thus using TEMP_FAILURE_RETRY is incorrect here.
>
> Add a customized TEMP_FAILURE_RETRY_NEG macro and use it instead of
> TEMP_FAILURE_RETRY to fix the issue.

Or we could change __getrandom_nocancel to use INLINE_SYSCALL_CALL
instead of INTERNAL_SYCALL_CALL.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY
  2024-01-04  9:51 ` Florian Weimer
@ 2024-01-04 12:13   ` Adhemerval Zanella Netto
  2024-01-04 12:16     ` Florian Weimer
  0 siblings, 1 reply; 6+ messages in thread
From: Adhemerval Zanella Netto @ 2024-01-04 12:13 UTC (permalink / raw)
  To: Florian Weimer, Xi Ruoyao; +Cc: libc-alpha, Andreas K . Huettel



On 04/01/24 06:51, Florian Weimer wrote:
> * Xi Ruoyao:
> 
>> The _nocancel functions returns errors as negative values, i. e. -EINTR
>> for interrupted system call.  But TEMP_FAILURE_RETRY expects the errors
>> as errno, thus using TEMP_FAILURE_RETRY is incorrect here.
>>
>> Add a customized TEMP_FAILURE_RETRY_NEG macro and use it instead of
>> TEMP_FAILURE_RETRY to fix the issue.
> 
> Or we could change __getrandom_nocancel to use INLINE_SYSCALL_CALL
> instead of INTERNAL_SYCALL_CALL.

It follows what other _nocancel functions that return a value do.  The
__getrandom_nocancel is also used on malloc initialization, but I do
not think touching errno should be an issue.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY
  2024-01-04 12:13   ` Adhemerval Zanella Netto
@ 2024-01-04 12:16     ` Florian Weimer
  2024-01-04 12:31       ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Weimer @ 2024-01-04 12:16 UTC (permalink / raw)
  To: Adhemerval Zanella Netto; +Cc: Xi Ruoyao, libc-alpha, Andreas K . Huettel

* Adhemerval Zanella Netto:

> On 04/01/24 06:51, Florian Weimer wrote:
>> * Xi Ruoyao:
>> 
>>> The _nocancel functions returns errors as negative values, i. e. -EINTR
>>> for interrupted system call.  But TEMP_FAILURE_RETRY expects the errors
>>> as errno, thus using TEMP_FAILURE_RETRY is incorrect here.
>>>
>>> Add a customized TEMP_FAILURE_RETRY_NEG macro and use it instead of
>>> TEMP_FAILURE_RETRY to fix the issue.
>> 
>> Or we could change __getrandom_nocancel to use INLINE_SYSCALL_CALL
>> instead of INTERNAL_SYCALL_CALL.
>
> It follows what other _nocancel functions that return a value do.  The
> __getrandom_nocancel is also used on malloc initialization, but I do
> not think touching errno should be an issue.

It's inconsistent.  Most of the implementations in
sysdeps/unix/sysv/linux/*_nocancel*.c use INLINE_SYSCALL_CALL, and
therefore set errno.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY
  2024-01-04 12:16     ` Florian Weimer
@ 2024-01-04 12:31       ` Adhemerval Zanella Netto
  2024-01-04 12:45         ` Xi Ruoyao
  0 siblings, 1 reply; 6+ messages in thread
From: Adhemerval Zanella Netto @ 2024-01-04 12:31 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Xi Ruoyao, libc-alpha, Andreas K . Huettel



On 04/01/24 09:16, Florian Weimer wrote:
> * Adhemerval Zanella Netto:
> 
>> On 04/01/24 06:51, Florian Weimer wrote:
>>> * Xi Ruoyao:
>>>
>>>> The _nocancel functions returns errors as negative values, i. e. -EINTR
>>>> for interrupted system call.  But TEMP_FAILURE_RETRY expects the errors
>>>> as errno, thus using TEMP_FAILURE_RETRY is incorrect here.
>>>>
>>>> Add a customized TEMP_FAILURE_RETRY_NEG macro and use it instead of
>>>> TEMP_FAILURE_RETRY to fix the issue.
>>>
>>> Or we could change __getrandom_nocancel to use INLINE_SYSCALL_CALL
>>> instead of INTERNAL_SYCALL_CALL.
>>
>> It follows what other _nocancel functions that return a value do.  The
>> __getrandom_nocancel is also used on malloc initialization, but I do
>> not think touching errno should be an issue.
> 
> It's inconsistent.  Most of the implementations in
> sysdeps/unix/sysv/linux/*_nocancel*.c use INLINE_SYSCALL_CALL, and
> therefore set errno.

I meant that *your suggestion* follow what other _nocancel functions that 
return a value do (sorry for the miscommunication). 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY
  2024-01-04 12:31       ` Adhemerval Zanella Netto
@ 2024-01-04 12:45         ` Xi Ruoyao
  0 siblings, 0 replies; 6+ messages in thread
From: Xi Ruoyao @ 2024-01-04 12:45 UTC (permalink / raw)
  To: Adhemerval Zanella Netto, Florian Weimer; +Cc: libc-alpha, Andreas K . Huettel

On Thu, 2024-01-04 at 09:31 -0300, Adhemerval Zanella Netto wrote:
> 
> 
> On 04/01/24 09:16, Florian Weimer wrote:
> > * Adhemerval Zanella Netto:
> > 
> > > On 04/01/24 06:51, Florian Weimer wrote:
> > > > * Xi Ruoyao:
> > > > 
> > > > > The _nocancel functions returns errors as negative values, i. e. -EINTR
> > > > > for interrupted system call.  But TEMP_FAILURE_RETRY expects the errors
> > > > > as errno, thus using TEMP_FAILURE_RETRY is incorrect here.
> > > > > 
> > > > > Add a customized TEMP_FAILURE_RETRY_NEG macro and use it instead of
> > > > > TEMP_FAILURE_RETRY to fix the issue.
> > > > 
> > > > Or we could change __getrandom_nocancel to use INLINE_SYSCALL_CALL
> > > > instead of INTERNAL_SYCALL_CALL.
> > > 
> > > It follows what other _nocancel functions that return a value do.  The
> > > __getrandom_nocancel is also used on malloc initialization, but I do
> > > not think touching errno should be an issue.
> > 
> > It's inconsistent.  Most of the implementations in
> > sysdeps/unix/sysv/linux/*_nocancel*.c use INLINE_SYSCALL_CALL, and
> > therefore set errno.
> 
> I meant that *your suggestion* follow what other _nocancel functions that 
> return a value do (sorry for the miscommunication). 

Phew, I mistakenly assumed all _nocancel functions did not use errno. 
So this patch is wrong and I'll make a V2.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-01-04 12:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-04  0:54 [PATCH] arc4random: Fix incorrect usage of TEMP_FAILURE_RETRY Xi Ruoyao
2024-01-04  9:51 ` Florian Weimer
2024-01-04 12:13   ` Adhemerval Zanella Netto
2024-01-04 12:16     ` Florian Weimer
2024-01-04 12:31       ` Adhemerval Zanella Netto
2024-01-04 12:45         ` Xi Ruoyao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).