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