Home | History | Annotate | Line # | Download | only in gen
t_getentropy.c revision 1.1
      1 /*	$NetBSD: t_getentropy.c,v 1.1 2022/05/31 13:42:59 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: t_getentropy.c,v 1.1 2022/05/31 13:42:59 riastradh Exp $");
     31 
     32 #include <sys/mman.h>
     33 
     34 #include <atf-c.h>
     35 #include <errno.h>
     36 #include <limits.h>
     37 #include <stdint.h>
     38 #include <string.h>
     39 #include <unistd.h>
     40 
     41 static uint8_t buf[289];
     42 static uint8_t zero[sizeof(buf)];
     43 
     44 __CTASSERT(GETENTROPY_MAX == 256);
     45 
     46 ATF_TC(getentropy_0);
     47 ATF_TC_HEAD(getentropy_0, tc)
     48 {
     49 	atf_tc_set_md_var(tc, "descr", "getentropy 0 bytes");
     50 	atf_tc_set_md_var(tc, "timeout", "2");
     51 }
     52 ATF_TC_BODY(getentropy_0, tc)
     53 {
     54 
     55 	memset(buf, 0, sizeof(buf));
     56 	if (getentropy(buf, 0) == -1)
     57 		atf_tc_fail("getentropy: %d (%s)", errno, strerror(errno));
     58 	ATF_CHECK(memcmp(buf, zero, sizeof(buf)) == 0);
     59 }
     60 
     61 ATF_TC(getentropy_32);
     62 ATF_TC_HEAD(getentropy_32, tc)
     63 {
     64 	atf_tc_set_md_var(tc, "descr", "getentropy 32 bytes");
     65 	atf_tc_set_md_var(tc, "timeout", "2");
     66 }
     67 ATF_TC_BODY(getentropy_32, tc)
     68 {
     69 
     70 	memset(buf, 0, sizeof(buf));
     71 	if (getentropy(buf + 1, 32) == -1)
     72 		atf_tc_fail("getentropy: %d (%s)", errno, strerror(errno));
     73 	ATF_CHECK(buf[0] == 0);
     74 	ATF_CHECK(memcmp(buf + 1, zero, 32) != 0);
     75 	ATF_CHECK(memcmp(buf + 1 + 32, zero, sizeof(buf) - 1 - 32) == 0);
     76 }
     77 
     78 ATF_TC(getentropy_256);
     79 ATF_TC_HEAD(getentropy_256, tc)
     80 {
     81 	atf_tc_set_md_var(tc, "descr", "getentropy 256 bytes");
     82 	atf_tc_set_md_var(tc, "timeout", "2");
     83 }
     84 ATF_TC_BODY(getentropy_256, tc)
     85 {
     86 
     87 	memset(buf, 0, sizeof(buf));
     88 	if (getentropy(buf + 1, 256) == -1)
     89 		atf_tc_fail("getentropy: %d (%s)", errno, strerror(errno));
     90 	ATF_CHECK(buf[0] == 0);
     91 	ATF_CHECK(memcmp(buf + 1, zero, 256) != 0);
     92 	ATF_CHECK(memcmp(buf + 1 + 256, zero, sizeof(buf) - 1 - 256) == 0);
     93 }
     94 
     95 ATF_TC(getentropy_257);
     96 ATF_TC_HEAD(getentropy_257, tc)
     97 {
     98 	atf_tc_set_md_var(tc, "descr", "getentropy 257 bytes (beyond max)");
     99 	atf_tc_set_md_var(tc, "timeout", "2");
    100 }
    101 ATF_TC_BODY(getentropy_257, tc)
    102 {
    103 
    104 	memset(buf, 0, sizeof(buf));
    105 	ATF_CHECK_ERRNO(EINVAL, getentropy(buf, 257) == -1);
    106 	ATF_CHECK(memcmp(buf, zero, sizeof(buf)) == 0);
    107 }
    108 
    109 ATF_TC(getentropy_null);
    110 ATF_TC_HEAD(getentropy_null, tc)
    111 {
    112 	atf_tc_set_md_var(tc, "descr", "getentropy with null buffer");
    113 	atf_tc_set_md_var(tc, "timeout", "2");
    114 }
    115 ATF_TC_BODY(getentropy_null, tc)
    116 {
    117 
    118 	ATF_CHECK_ERRNO(EFAULT, getentropy(NULL, 32) == -1);
    119 }
    120 
    121 ATF_TC(getentropy_nearnull);
    122 ATF_TC_HEAD(getentropy_nearnull, tc)
    123 {
    124 	atf_tc_set_md_var(tc, "descr", "getentropy with nearly null buffer");
    125 	atf_tc_set_md_var(tc, "timeout", "2");
    126 }
    127 ATF_TC_BODY(getentropy_nearnull, tc)
    128 {
    129 
    130 	ATF_CHECK_ERRNO(EFAULT, getentropy((char *)(uintptr_t)1, 32) == -1);
    131 }
    132 
    133 ATF_TC(getentropy_badaddr);
    134 ATF_TC_HEAD(getentropy_badaddr, tc)
    135 {
    136 	atf_tc_set_md_var(tc, "descr", "getentropy with bad address");
    137 	atf_tc_set_md_var(tc, "timeout", "2");
    138 }
    139 ATF_TC_BODY(getentropy_badaddr, tc)
    140 {
    141 	size_t pagesize = sysconf(_SC_PAGESIZE);
    142 	char *p;
    143 
    144 	/*
    145 	 * Allocate three consecutive pages and make the middle one
    146 	 * nonwritable.
    147 	 */
    148 	p = mmap(NULL, 3*pagesize, PROT_READ|PROT_WRITE,
    149 	    MAP_ANON|MAP_PRIVATE, -1, 0);
    150 	if (p == MAP_FAILED)
    151 		atf_tc_fail("mmap: %d (%s)", errno, strerror(errno));
    152 	if (mprotect(p + pagesize, pagesize, PROT_READ) == -1)
    153 		atf_tc_fail("mprotect: %d (%s)", errno, strerror(errno));
    154 
    155 	/* Verify that writing to the end of the first page works.  */
    156 	if (getentropy(p + pagesize - 25, 25) == -1)
    157 		atf_tc_fail("getentropy 1: %d (%s)", errno, strerror(errno));
    158 	ATF_CHECK(memcmp(p + pagesize - 25, zero, 25) != 0);
    159 
    160 	/*
    161 	 * Verify that writes into the middle page, whether straddling
    162 	 * the surrounding pages or not, fail with EFAULT.
    163 	 */
    164 	ATF_CHECK_ERRNO(EFAULT, getentropy(p + pagesize - 1, 2) == -1);
    165 	ATF_CHECK_ERRNO(EFAULT, getentropy(p + pagesize, 32) == -1);
    166 	ATF_CHECK_ERRNO(EFAULT, getentropy(p + 2*pagesize - 1, 2) == -1);
    167 
    168 	/* Verify that writing to the start of the last page works.  */
    169 	if (getentropy(p + 2*pagesize, 25) == -1)
    170 		atf_tc_fail("getentropy 2: %d (%s)", errno, strerror(errno));
    171 	ATF_CHECK(memcmp(p + pagesize - 25, zero, 25) != 0);
    172 
    173 	if (munmap(p, 3*pagesize) == -1)
    174 		atf_tc_fail("munmap: %d (%s)", errno, strerror(errno));
    175 }
    176 
    177 ATF_TP_ADD_TCS(tp)
    178 {
    179 
    180 	ATF_TP_ADD_TC(tp, getentropy_0);
    181 	ATF_TP_ADD_TC(tp, getentropy_256);
    182 	ATF_TP_ADD_TC(tp, getentropy_257);
    183 	ATF_TP_ADD_TC(tp, getentropy_32);
    184 	ATF_TP_ADD_TC(tp, getentropy_badaddr);
    185 	ATF_TP_ADD_TC(tp, getentropy_nearnull);
    186 	ATF_TP_ADD_TC(tp, getentropy_null);
    187 
    188 	return atf_no_error();
    189 }
    190