2023-10-25 19:48:07

by Petr Vorel

[permalink] [raw]
Subject: [PATCH 0/3] Add getrandom() fallback, cleanup headers

Hi,

motivation to add this is to allow to compile reexport on systems with
older libc. (getrandom() wrapper is supported on glibc 2.25+ and musl
1.1.20+, uclibc-ng does
not yet support it).

getrandom() syscall is supported Linux 3.17+ (old enough to bother with
a check).

I also wonder why getrandom() syscall does not called with GRND_NONBLOCK
flag. Is it ok/needed to block?

Kind regards,
Petr

Petr Vorel (3):
reexport/fsidd.c: Remove unused headers
support/reexport.c: Remove unused headers
support/backend_sqlite.c: Add getrandom() fallback

Makefile.am | 1 +
aclocal/getrandom.m4 | 16 ++++++++++++++++
configure.ac | 3 +++
support/reexport/backend_sqlite.c | 18 +++++++++++++++++-
support/reexport/fsidd.c | 10 ----------
support/reexport/reexport.c | 7 -------
6 files changed, 37 insertions(+), 18 deletions(-)
create mode 100644 aclocal/getrandom.m4

--
2.42.0


2023-10-25 19:48:07

by Petr Vorel

[permalink] [raw]
Subject: [PATCH 1/3] reexport/fsidd.c: Remove unused headers

Some of them are needed but included elsewhere, e.g. <string.h>
included in xcommon.h, but at least <sys/random.h> is removed due
further code simplification.

Fixes: 6fd2732d ("export: Add fsidd")
Signed-off-by: Petr Vorel <[email protected]>
---
support/reexport/fsidd.c | 10 ----------
1 file changed, 10 deletions(-)

diff --git a/support/reexport/fsidd.c b/support/reexport/fsidd.c
index d4b245e8..3e62b3fc 100644
--- a/support/reexport/fsidd.c
+++ b/support/reexport/fsidd.c
@@ -7,16 +7,6 @@
#include <dlfcn.h>
#endif
#include <event2/event.h>
-#include <limits.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <sys/random.h>
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/un.h>
-#include <sys/vfs.h>
-#include <unistd.h>

#include "conffile.h"
#include "reexport_backend.h"
--
2.42.0

2023-10-25 19:53:42

by Petr Vorel

[permalink] [raw]
Subject: [PATCH 2/3] support/reexport.c: Remove unused headers

Some of them are needed but included elsewhere, e.g. <sys/socket.h>
included in nfslib.h or <string.h> included in xcommon.h, but at least
<sys/random.h> is removed due further code simplification.

Fixes: 878674b3 ("Add reexport helper library")
Signed-off-by: Petr Vorel <[email protected]>
---
support/reexport/reexport.c | 7 -------
1 file changed, 7 deletions(-)

diff --git a/support/reexport/reexport.c b/support/reexport/reexport.c
index d9a700af..78516586 100644
--- a/support/reexport/reexport.c
+++ b/support/reexport/reexport.c
@@ -5,16 +5,9 @@
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif
-#include <stdint.h>
-#include <stdio.h>
-#include <sys/random.h>
-#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
-#include <unistd.h>
#include <errno.h>
-#include <sys/socket.h>
-#include <sys/un.h>

#include "nfsd_path.h"
#include "conffile.h"
--
2.42.0

2023-10-25 19:57:08

by Richard Weinberger

[permalink] [raw]
Subject: Re: [PATCH 2/3] support/reexport.c: Remove unused headers

----- Ursprüngliche Mail -----
> Von: "Petr Vorel" <[email protected]>
> An: "linux-nfs" <[email protected]>
> CC: "Petr Vorel" <[email protected]>, "richard" <[email protected]>, "Steve Dickson" <[email protected]>
> Gesendet: Mittwoch, 25. Oktober 2023 21:47:00
> Betreff: [PATCH 2/3] support/reexport.c: Remove unused headers

> Some of them are needed but included elsewhere, e.g. <sys/socket.h>
> included in nfslib.h or <string.h> included in xcommon.h, but at least
> <sys/random.h> is removed due further code simplification.
>
> Fixes: 878674b3 ("Add reexport helper library")
> Signed-off-by: Petr Vorel <[email protected]>

Reviewed-by: Richard Weinberger <[email protected]>

Thanks,
//richard

2023-10-25 20:13:10

by Richard Weinberger

[permalink] [raw]
Subject: Re: [PATCH 0/3] Add getrandom() fallback, cleanup headers

----- Ursprüngliche Mail -----
> Von: "Petr Vorel" <[email protected]>
> I also wonder why getrandom() syscall does not called with GRND_NONBLOCK
> flag. Is it ok/needed to block?

With GRND_NONBLOCK it would return EAGAIN if not enough
randomness is ready. How to handle this then? Aborting the start of the daemon?

Before we other think the whole thing, the sole purpose of the getrandom()
call is seeding libc's PRNG with srand() to give every waiter a different
amount of sleep time upon concurrent database access.
See wait_for_dbaccess() and handling of SQLITE_LOCKED.

I'm pretty sure instead of seeding from getrandom() we can also use the current
time or read a few bytes from /dev/urandom.
Just make sure that every user of sqlite_plug_init() has a different seed.

Thanks,
//richard

2023-10-25 20:56:27

by Petr Vorel

[permalink] [raw]
Subject: Re: [PATCH 0/3] Add getrandom() fallback, cleanup headers

Hi Richard,

> ----- Urspr?ngliche Mail -----
> > Von: "Petr Vorel" <[email protected]>
> > I also wonder why getrandom() syscall does not called with GRND_NONBLOCK
> > flag. Is it ok/needed to block?

> With GRND_NONBLOCK it would return EAGAIN if not enough
> randomness is ready. How to handle this then? Aborting the start of the daemon?

Well, current code uses /dev/urandom and blocks until pool is ready (man
random(7)), which is probably OK (on VM people may need to use haveged to avoid
blocking, but that's known). But even with blocking mode blocking requests of
any size can be interrupted by a signal handler with errno EINTR. That's
probably the reason why people write more robust code. I'm not sure if it's
really needed to be handled in our case.

Nice example is ul_random_get_bytes() in util-linux [1]:

#ifdef HAVE_GETRANDOM
while (n > 0) {
int x;

errno = 0;
x = getrandom(cp, n, GRND_NONBLOCK);
if (x > 0) { /* success */
n -= x;
cp += x;
lose_counter = 0;
errno = 0;
} else if (errno == ENOSYS) { /* kernel without getrandom() */
break;

} else if (errno == EAGAIN && lose_counter < UL_RAND_READ_ATTEMPTS) {
xusleep(UL_RAND_READ_DELAY); /* no entropy, wait and try again */
lose_counter++;
} else
break;
}

if (errno == ENOSYS)
#endif

1) sleep on EAGAIN and try again (needed to be handled due GRND_NONBLOCK).

2) It also handles ENOSYS (run on kernel without getrandom() although it was built
with libc support), which would be very rare (IMHO getrandom() is on all
architectures, but looking into drivers/char/random.c, it would be on kernels
without CONFIG_SYSCTL). Then the code also adds fallback to read
/dev/{u,}random in this case. It could be added to nfs-utils, if anybody really
needs it.

> Before we other think the whole thing, the sole purpose of the getrandom()
> call is seeding libc's PRNG with srand() to give every waiter a different
> amount of sleep time upon concurrent database access.
> See wait_for_dbaccess() and handling of SQLITE_LOCKED.

> I'm pretty sure instead of seeding from getrandom() we can also use the current
> time or read a few bytes from /dev/urandom.

Sure. Current time would work everywhere, but I guess getrandom() with syscall
is good enough. Systems which have /dev/urandom also have getrandom() syscall
(thus will work with my current proposal).

> Just make sure that every user of sqlite_plug_init() has a different seed.

Thanks for info.

Kind regards,
Petr

> Thanks,
> //richard

[1] https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/lib/randutils.c

2023-10-25 20:57:32

by Petr Vorel

[permalink] [raw]
Subject: Re: [PATCH 0/3] Add getrandom() fallback, cleanup headers

Hi all,

> 1) sleep on EAGAIN and try again (needed to be handled due GRND_NONBLOCK).

(man getrandom(2))

Kind regards,
Petr