diff options
| author | riastradh <riastradh@NetBSD.org> | 2020-08-14 00:53:15 +0000 |
|---|---|---|
| committer | riastradh <riastradh@NetBSD.org> | 2020-08-14 00:53:15 +0000 |
| commit | e2fa4d60fd222537c72bee82b7b9d8a56f9727ff (patch) | |
| tree | 41a0209dde4f6cd8b4dd8bbcdd74c6d610a641c5 /tests | |
| parent | f1e4c716a0c7708391ccc7132fc073a9cfa7aca4 (diff) | |
New system call getrandom() compatible with Linux and others.
Three ways to call:
getrandom(p, n, 0) Blocks at boot until full entropy.
Returns up to n bytes at p; guarantees
up to 256 bytes even if interrupted
after blocking. getrandom(0,0,0)
serves as an entropy barrier: return
only after system has full entropy.
getrandom(p, n, GRND_INSECURE) Never blocks. Guarantees up to 256
bytes even if interrupted. Equivalent
to /dev/urandom. Safe only after
successful getrandom(...,0),
getrandom(...,GRND_RANDOM), or read
from /dev/random.
getrandom(p, n, GRND_RANDOM) May block at any time. Returns up to n
bytes at p, but no guarantees about how
many -- may return as short as 1 byte.
Equivalent to /dev/random. Legacy.
Provided only for source compatibility
with Linux.
Can also use flags|GRND_NONBLOCK to fail with EWOULDBLOCK/EAGAIN
without producing any output instead of blocking.
- The combination GRND_INSECURE|GRND_NONBLOCK is the same as
GRND_INSECURE, since GRND_INSECURE never blocks anyway.
- The combinations GRND_INSECURE|GRND_RANDOM and
GRND_INSECURE|GRND_RANDOM|GRND_NONBLOCK are nonsensical and fail
with EINVAL.
As proposed on tech-userlevel, tech-crypto, tech-security, and
tech-kern, and subsequently adopted by core (minus the getentropy part
of the proposal, because other operating systems and participants in
the discussion couldn't come to an agreement about getentropy and
blocking semantics):
https://mail-index.netbsd.org/tech-userlevel/2020/05/02/msg012333.html
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/lib/libc/sys/Makefile | 3 | ||||
| -rw-r--r-- | tests/lib/libc/sys/t_getrandom.c | 170 |
2 files changed, 172 insertions, 1 deletions
diff --git a/tests/lib/libc/sys/Makefile b/tests/lib/libc/sys/Makefile index ad87ff9d803..4019e2af385 100644 --- a/tests/lib/libc/sys/Makefile +++ b/tests/lib/libc/sys/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.66 2020/07/17 15:34:16 kamil Exp $ +# $NetBSD: Makefile,v 1.67 2020/08/14 00:53:16 riastradh Exp $ MKMAN= no @@ -25,6 +25,7 @@ TESTS_C+= t_getgroups TESTS_C+= t_getitimer TESTS_C+= t_getlogin TESTS_C+= t_getpid +TESTS_C+= t_getrandom TESTS_C+= t_getrusage TESTS_C+= t_getsid TESTS_C+= t_getsockname diff --git a/tests/lib/libc/sys/t_getrandom.c b/tests/lib/libc/sys/t_getrandom.c new file mode 100644 index 00000000000..b5316517a1b --- /dev/null +++ b/tests/lib/libc/sys/t_getrandom.c @@ -0,0 +1,170 @@ +/* $NetBSD: t_getrandom.c,v 1.1 2020/08/14 00:53:16 riastradh Exp $ */ + +/*- + * Copyright (c) 2020 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Taylor R. Campbell. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_getrandom.c,v 1.1 2020/08/14 00:53:16 riastradh Exp $"); + +#include <sys/random.h> + +#include <atf-c.h> +#include <errno.h> +#include <signal.h> +#include <unistd.h> + +static uint8_t buf[65536]; +static uint8_t zero24[24]; + +static void +alarm_handler(int signo) +{ +} + +ATF_TC(getrandom); +ATF_TC_HEAD(getrandom, tc) +{ + + atf_tc_set_md_var(tc, "descr", "getrandom(2)"); +} + +/* + * Probability of spurious failure is 1/2^192 for each of the memcmps. + * As long as there are fewer than 2^64 of them, the probability of + * spurious failure is at most 1/2^128, which is low enough that we + * don't care about it. + */ + +ATF_TC_BODY(getrandom, tc) +{ + ssize_t n; + + ATF_REQUIRE(signal(SIGALRM, &alarm_handler) != SIG_ERR); + + /* default */ + alarm(1); + memset(buf, 0, sizeof buf); + n = getrandom(buf, sizeof buf, 0); + if (n == -1) { + ATF_CHECK_EQ(errno, EINTR); + } else { + ATF_CHECK_EQ((size_t)n, sizeof buf); + ATF_CHECK(memcmp(buf, zero24, 24) != 0); + ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); + } + alarm(0); + + /* default, nonblocking */ + memset(buf, 0, sizeof buf); + n = getrandom(buf, sizeof buf, GRND_NONBLOCK); + if (n == -1) { + ATF_CHECK_EQ(errno, EAGAIN); + } else { + ATF_CHECK_EQ((size_t)n, sizeof buf); + ATF_CHECK(memcmp(buf, zero24, 24) != 0); + ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); + } + + /* insecure */ + memset(buf, 0, sizeof buf); + n = getrandom(buf, sizeof buf, GRND_INSECURE); + ATF_CHECK(n != -1); + ATF_CHECK_EQ((size_t)n, sizeof buf); + ATF_CHECK(memcmp(buf, zero24, 24) != 0); + ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); + + /* insecure, nonblocking -- same as mere insecure */ + memset(buf, 0, sizeof buf); + n = getrandom(buf, sizeof buf, GRND_INSECURE|GRND_NONBLOCK); + ATF_CHECK(n != -1); + ATF_CHECK_EQ((size_t)n, sizeof buf); + ATF_CHECK(memcmp(buf, zero24, 24) != 0); + ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); + + /* `random' (hokey) */ + alarm(1); + memset(buf, 0, sizeof buf); + n = getrandom(buf, sizeof buf, GRND_RANDOM); + if (n == -1) { + ATF_CHECK_EQ(errno, EINTR); + } else { + ATF_CHECK(n != 0); + ATF_CHECK((size_t)n <= sizeof buf); + if ((size_t)n >= 24) { + ATF_CHECK(memcmp(buf, zero24, 24) != 0); + ATF_CHECK(memcmp(buf + n - 24, zero24, 24) != 0); + } + } + alarm(0); + + /* `random' (hokey), nonblocking */ + memset(buf, 0, sizeof buf); + n = getrandom(buf, sizeof buf, GRND_RANDOM|GRND_NONBLOCK); + if (n == -1) { + ATF_CHECK_EQ(errno, EAGAIN); + } else { + ATF_CHECK(n != 0); + ATF_CHECK((size_t)n <= sizeof buf); + if ((size_t)n >= 24) { + ATF_CHECK(memcmp(buf, zero24, 24) != 0); + ATF_CHECK(memcmp(buf + n - 24, zero24, 24) != 0); + } + } + + /* random and insecure -- nonsensical */ + n = getrandom(buf, sizeof buf, GRND_RANDOM|GRND_INSECURE); + ATF_CHECK_EQ(n, -1); + ATF_CHECK_EQ(errno, EINVAL); + + /* random and insecure, nonblocking -- nonsensical */ + n = getrandom(buf, sizeof buf, + GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK); + ATF_CHECK_EQ(n, -1); + ATF_CHECK_EQ(errno, EINVAL); + + /* invalid flags */ + __CTASSERT(~(GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK)); + n = getrandom(buf, sizeof buf, + ~(GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK)); + ATF_CHECK_EQ(n, -1); + ATF_CHECK_EQ(errno, EINVAL); + + /* unmapped */ + n = getrandom(NULL, sizeof buf, GRND_INSECURE|GRND_NONBLOCK); + ATF_CHECK_EQ(n, -1); + ATF_CHECK_EQ(errno, EFAULT); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, getrandom); + + return atf_no_error(); +} |
