[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: [PATCH] miscutil: provide getrandom() userland implementation if needed
From:       Thomas Devoogdt <thomas () devoogdt ! com>
Date:       2023-02-26 17:23:40
Message-ID: CACXRmJibS=32MEoaXjCzy4ZKOEhzU176zqpO6=6vZYQp1_zjGA () mail ! gmail ! com
[Download RAW message or body]

Hi,

This solution is equally correct, the most crucial thing here is that
`errno = ENOSYS;` is set so that the /dev/random fallback is used.
All other stuff is coding style, but I would personally already put
the <sys/random.h> include in "random.h". So that only one time the
glibc and kernel version check is needed.

kr,

Thomas

Op vr 24 feb. 2023 om 23:52 schreef Markus Mayer <mmayer@broadcom.com>:
>
> Glibc 2.24 and older do not provide an interface for the getrandom()
> system call. Linux kernels have been implementing this sytem call since
> kernel 3.17.
>
> Let's address the situation where glibc <= 2.24 and Linux >= 3.17 by
> providing the system call interface ourselves when needed. This allows
> users to take advantage of getrandom() as long as the kernel supports
> it, even if glibc doesn't provide access to it.
>
> Signed-off-by: Markus Mayer <mmayer@broadcom.com>
> ---
>  miscutils/getrandom.c | 22 ++++++++++++++++++++++
>  miscutils/random.h    | 19 +++++++++++++++++++
>  miscutils/seedrng.c   |  6 +++++-
>  3 files changed, 46 insertions(+), 1 deletion(-)
>  create mode 100644 miscutils/getrandom.c
>  create mode 100644 miscutils/random.h
>
> diff --git a/miscutils/getrandom.c b/miscutils/getrandom.c
> new file mode 100644
> index 000000000000..ab6a57f8042e
> --- /dev/null
> +++ b/miscutils/getrandom.c
> @@ -0,0 +1,22 @@
> +#include <features.h>
> +
> +#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 24
> +
> +#ifndef __NR_getrandom
> +#include <errno.h>
> +#endif
> +#include <sys/syscall.h>
> +#include "random.h"
> +
> +ssize_t getrandom(void *buffer, size_t length, unsigned int flags)
> +{
> +#ifdef __NR_getrandom
> +       return syscall(__NR_getrandom, buffer, length, flags);
> +#else
> +       /* For kernels that don't support getrandom() (< 3.17). */
> +       errno = ENOSYS;
> +       return -1;
> +#endif
> +}
> +
> +#endif /* __GLIBC__ ... */
> diff --git a/miscutils/random.h b/miscutils/random.h
> new file mode 100644
> index 000000000000..3131df5e8a5d
> --- /dev/null
> +++ b/miscutils/random.h
> @@ -0,0 +1,19 @@
> +#ifndef _RANDOM_H
> +#define _RANDOM_H 1
> +
> +#include <features.h>
> +#include <sys/types.h>
> +
> +#ifndef GRND_NONBLOCK
> +#define GRND_NONBLOCK 0x01
> +#endif
> +#ifndef GRND_RANDOM
> +#define GRND_RANDOM 0x02
> +#endif
> +#ifndef GRND_INSECURE
> +#define GRND_INSECURE 0x04
> +#endif
> +
> +ssize_t getrandom(void *buffer, size_t length, unsigned int flags);
> +
> +#endif /* _RANDOM_H */
> diff --git a/miscutils/seedrng.c b/miscutils/seedrng.c
> index 967741dc72ea..8ce553e460b1 100644
> --- a/miscutils/seedrng.c
> +++ b/miscutils/seedrng.c
> @@ -29,7 +29,7 @@
>
>  //applet:IF_SEEDRNG(APPLET(seedrng, BB_DIR_USR_SBIN, BB_SUID_DROP))
>
> -//kbuild:lib-$(CONFIG_SEEDRNG) += seedrng.o
> +//kbuild:lib-$(CONFIG_SEEDRNG) += getrandom.o seedrng.o
>
>  //usage:#define seedrng_trivial_usage
>  //usage:       "[-d DIR] [-n]"
> @@ -42,7 +42,11 @@
>  #include "libbb.h"
>
>  #include <linux/random.h>
> +#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 24
> +#include "random.h"
> +#else
>  #include <sys/random.h>
> +#endif
>  #include <sys/file.h>
>
>  #ifndef GRND_INSECURE
> --
> 2.34.1
>
>
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic