Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 8 Jan 2018 16:06:41 -0600
From: William Pitcock <nenolod@...eferenced.org>
To: musl@...ts.openwall.com
Cc: Hauke Mehrtens <hauke@...ke-m.de>
Subject: Re: [PATCH v2] Add getrandom syscall wrapper and getentropy function

Hi,

On Sat, Jan 6, 2018 at 4:08 PM, Hauke Mehrtens <hauke@...ke-m.de> wrote:
> This syscall is available since Linux 3.17 and was also implemented in
> glibc in version 2.25 using the same interfaces.
> The getrandom function is a pure syscall wrapper liker glibc does it.
> getentropy is implemented on top of the getrandom syscall and fills the
> buffer.
>
> Currently no fallback is implemented this could be possible by using
> AT_RANDOM in the future.
> ---
>  include/sys/random.h   | 21 +++++++++++++++++++++
>  src/linux/getentropy.c | 29 +++++++++++++++++++++++++++++
>  src/linux/getrandom.c  |  7 +++++++
>  3 files changed, 57 insertions(+)
>  create mode 100644 include/sys/random.h
>  create mode 100644 src/linux/getentropy.c
>  create mode 100644 src/linux/getrandom.c
>
> diff --git a/include/sys/random.h b/include/sys/random.h
> new file mode 100644
> index 00000000..5b09e394
> --- /dev/null
> +++ b/include/sys/random.h
> @@ -0,0 +1,21 @@
> +#ifndef _SYS_RANDOM_H
> +#define _SYS_RANDOM_H
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
> +#define __NEED_size_t
> +#define __NEED_ssize_t
> +#include <bits/alltypes.h>
> +
> +#define GRND_NONBLOCK  0x0001
> +#define GRND_RANDOM    0x0002
> +
> +ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
> +
> +int getentropy(void *buffer, size_t length);

Function prototypes in headers should be provided without naming the arguments.

> +
> +#ifdef __cplusplus
> +}
> +#endif
> +#endif
> diff --git a/src/linux/getentropy.c b/src/linux/getentropy.c
> new file mode 100644
> index 00000000..48ca3d51
> --- /dev/null
> +++ b/src/linux/getentropy.c
> @@ -0,0 +1,29 @@
> +#include <sys/random.h>
> +#include <errno.h>
> +#include "syscall.h"
> +
> +int getentropy(void *buffer, size_t length)
> +{
> +       int ret;
> +       char *pos = buffer;
> +       size_t rem = length;
> +
> +       if (length > 256) {
> +               return __syscall_ret(-EIO);
> +       }
> +
> +       while (rem) {
> +               ret = __syscall_cp(SYS_getrandom, pos, rem, 0);
> +               if (ret == -EINTR) {
> +                       continue;
> +               } else if (ret == -EFAULT || ret == -ENOSYS) {
> +                       return __syscall_ret(ret);
> +               } else if (ret <= 0) {
> +                       return __syscall_ret(-EIO);
> +               }
> +
> +               pos += ret;
> +               rem -= ret;
> +       }
> +       return 0;
> +}
> diff --git a/src/linux/getrandom.c b/src/linux/getrandom.c
> new file mode 100644
> index 00000000..795b932c
> --- /dev/null
> +++ b/src/linux/getrandom.c
> @@ -0,0 +1,7 @@
> +#include <sys/random.h>
> +#include "syscall.h"
> +
> +ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
> +{
> +       return syscall_cp(SYS_getrandom, buf, buflen, flags);
> +}
> --
> 2.11.0
>

Otherwise this looks okay to me.

William

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.