From 69ea87350f8a2c20e00cbc8a5a53bd50702d532a Mon Sep 17 00:00:00 2001 From: dchagin Date: Sat, 18 Mar 2017 18:34:29 +0000 Subject: [PATCH] Implement getrandom() syscall. Note. GRND_RANDOM option is not supported for now. MFC after: 1 month --- sys/amd64/linux/linux_dummy.c | 1 - sys/amd64/linux32/linux32_dummy.c | 1 - sys/compat/linux/linux_misc.c | 26 ++++++++++++++++++++++++++ sys/compat/linux/linux_misc.h | 4 ++++ sys/i386/linux/linux_dummy.c | 1 - 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/sys/amd64/linux/linux_dummy.c b/sys/amd64/linux/linux_dummy.c index 90efd4e1f042..0a34dc121f1d 100644 --- a/sys/amd64/linux/linux_dummy.c +++ b/sys/amd64/linux/linux_dummy.c @@ -135,7 +135,6 @@ DUMMY(sched_getattr); DUMMY(renameat2); /* linux 3.15: */ DUMMY(seccomp); -DUMMY(getrandom); DUMMY(memfd_create); DUMMY(kexec_file_load); /* linux 3.18: */ diff --git a/sys/amd64/linux32/linux32_dummy.c b/sys/amd64/linux32/linux32_dummy.c index da5e7571f08f..2c8d6b579e41 100644 --- a/sys/amd64/linux32/linux32_dummy.c +++ b/sys/amd64/linux32/linux32_dummy.c @@ -144,7 +144,6 @@ DUMMY(sched_getattr); DUMMY(renameat2); /* linux 3.15: */ DUMMY(seccomp); -DUMMY(getrandom); DUMMY(memfd_create); /* linux 3.18: */ DUMMY(bpf); diff --git a/sys/compat/linux/linux_misc.c b/sys/compat/linux/linux_misc.c index 5a4fad2d3b55..2ce3be2b50e8 100644 --- a/sys/compat/linux/linux_misc.c +++ b/sys/compat/linux/linux_misc.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -65,6 +66,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include @@ -2508,3 +2510,27 @@ linux_to_bsd_waitopts(int options, int *bsdopts) if (options & __WCLONE) *bsdopts |= WLINUXCLONE; } + +int +linux_getrandom(struct thread *td, struct linux_getrandom_args *args) +{ + struct uio uio; + struct iovec iov; + + if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM)) + return (EINVAL); + if (args->count > INT_MAX) + args->count = INT_MAX; + + iov.iov_base = args->buf; + iov.iov_len = args->count; + + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + uio.uio_resid = iov.iov_len; + uio.uio_segflg = UIO_USERSPACE; + uio.uio_rw = UIO_READ; + uio.uio_td = td; + + return (read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK)); +} diff --git a/sys/compat/linux/linux_misc.h b/sys/compat/linux/linux_misc.h index 826bd5002640..98ffd2263647 100644 --- a/sys/compat/linux/linux_misc.h +++ b/sys/compat/linux/linux_misc.h @@ -145,6 +145,10 @@ extern int stclohz; #define LINUX_RLIM_INFINITY (~0UL) +/* Linux getrandom flags */ +#define LINUX_GRND_NONBLOCK 0x0001 +#define LINUX_GRND_RANDOM 0x0002 + int linux_common_wait(struct thread *td, int pid, int *status, int options, struct rusage *ru); void linux_to_bsd_waitopts(int options, int *bsdopts); diff --git a/sys/i386/linux/linux_dummy.c b/sys/i386/linux/linux_dummy.c index bb5214205850..c025cecfc42f 100644 --- a/sys/i386/linux/linux_dummy.c +++ b/sys/i386/linux/linux_dummy.c @@ -140,7 +140,6 @@ DUMMY(sched_getattr); DUMMY(renameat2); /* linux 3.15: */ DUMMY(seccomp); -DUMMY(getrandom); DUMMY(memfd_create); /* linux 3.18: */ DUMMY(bpf);