1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 2020 Alex Richardson <arichardson@FreeBSD.org> 5 * 6 * This software was developed by SRI International and the University of 7 * Cambridge Computer Laboratory (Department of Computer Science and 8 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the 9 * DARPA SSITH research programme. 10 * 11 * This work was supported by Innovate UK project 105694, "Digital Security by 12 * Design (DSbD) Technology Platform Prototype". 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are met: 16 * 1. Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright notice, 19 * this list of conditions and the following disclaimer in the documentation 20 * and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 #include <sys/cdefs.h> 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <stdbool.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <spawn.h> 43 #include <sys/module.h> 44 #include <sys/sbuf.h> 45 #include <sys/stat.h> 46 #include <sys/wait.h> 47 48 #include <atf-c.h> 49 50 /* 51 * Tests 0001-0999 are copied from OpenBSD's regress/sbin/pfctl. 52 * Tests 1001-1999 are ours (FreeBSD's own). 53 * 54 * pf: Run pfctl -nv on pfNNNN.in and check that the output matches pfNNNN.ok. 55 * Copied from OpenBSD. Main differences are some things not working 56 * in FreeBSD: 57 * * The action 'match' 58 * * The command 'set reassemble' 59 * * The 'from'/'to' options together with 'route-to' 60 * * The option 'scrub' (it is an action in FreeBSD) 61 * * Accepting undefined routing tables in actions (??: see pf0093.in) 62 * * The 'route' option 63 * * The 'set queue def' option 64 * selfpf: Feed pfctl output through pfctl again and verify it stays the same. 65 * Copied from OpenBSD. 66 */ 67 68 static bool 69 check_pf_module_available(void) 70 { 71 int modid; 72 struct module_stat stat; 73 74 if ((modid = modfind("pf")) < 0) { 75 warn("pf module not found"); 76 return false; 77 } 78 stat.version = sizeof(struct module_stat); 79 if (modstat(modid, &stat) < 0) { 80 warn("can't stat pf module id %d", modid); 81 return false; 82 } 83 return (true); 84 } 85 86 extern char **environ; 87 88 static struct sbuf * 89 read_fd(int fd, size_t sizehint) 90 { 91 struct sbuf *sb; 92 ssize_t count; 93 char buffer[MAXBSIZE]; 94 95 sb = sbuf_new(NULL, NULL, sizehint, SBUF_AUTOEXTEND); 96 errno = 0; 97 while ((count = read(fd, buffer, sizeof(buffer) - 1)) > 0) { 98 sbuf_bcat(sb, buffer, count); 99 } 100 ATF_REQUIRE_ERRNO(0, count == 0 && "Should have reached EOF"); 101 sbuf_finish(sb); /* Ensure NULL-termination */ 102 return (sb); 103 } 104 105 static struct sbuf * 106 read_file(const char *filename) 107 { 108 struct stat s; 109 struct sbuf *result; 110 int fd; 111 112 errno = 0; 113 ATF_REQUIRE_EQ_MSG(stat(filename, &s), 0, "cannot stat %s", filename); 114 fd = open(filename, O_RDONLY); 115 ATF_REQUIRE_ERRNO(0, fd > 0); 116 result = read_fd(fd, s.st_size); 117 ATF_REQUIRE_ERRNO(0, close(fd) == 0); 118 return (result); 119 } 120 121 static void 122 run_pfctl_test(const char *input_path, const char *expected_path, 123 const atf_tc_t *tc) 124 { 125 int status; 126 pid_t pid; 127 int pipefds[2]; 128 char input_files_path[PATH_MAX]; 129 struct sbuf *expected_output; 130 struct sbuf *real_output; 131 posix_spawn_file_actions_t action; 132 133 if (!check_pf_module_available()) 134 atf_tc_skip("pf(4) is not loaded"); 135 136 /* The test inputs need to be able to use relative includes. */ 137 snprintf(input_files_path, sizeof(input_files_path), "%s/files", 138 atf_tc_get_config_var(tc, "srcdir")); 139 ATF_REQUIRE_ERRNO(0, chdir(input_files_path) == 0); 140 141 ATF_REQUIRE_ERRNO(0, pipe(pipefds) == 0); 142 expected_output = read_file(expected_path); 143 144 posix_spawn_file_actions_init(&action); 145 posix_spawn_file_actions_addclose(&action, STDIN_FILENO); 146 posix_spawn_file_actions_addclose(&action, pipefds[1]); 147 posix_spawn_file_actions_adddup2(&action, pipefds[0], STDOUT_FILENO); 148 posix_spawn_file_actions_adddup2(&action, pipefds[0], STDERR_FILENO); 149 150 const char *argv[] = { "pfctl", "-o", "none", "-nvf", input_path, 151 NULL }; 152 printf("Running %s %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3], 153 argv[4]); 154 status = posix_spawnp( 155 &pid, "pfctl", &action, NULL, __DECONST(char **, argv), environ); 156 ATF_REQUIRE_EQ_MSG( 157 status, 0, "posix_spawn failed: %s", strerror(errno)); 158 posix_spawn_file_actions_destroy(&action); 159 close(pipefds[0]); 160 161 real_output = read_fd(pipefds[1], 0); 162 printf("---\n%s---\n", sbuf_data(real_output)); 163 ATF_REQUIRE_EQ(waitpid(pid, &status, 0), pid); 164 ATF_REQUIRE_MSG(WIFEXITED(status), 165 "pfctl returned non-zero! Output:\n %s", sbuf_data(real_output)); 166 167 ATF_CHECK_STREQ(sbuf_data(expected_output), sbuf_data(real_output)); 168 sbuf_delete(expected_output); 169 sbuf_delete(real_output); 170 close(pipefds[1]); 171 } 172 173 static void 174 do_pf_test(const char *number, const atf_tc_t *tc) 175 { 176 char *input_path; 177 char *expected_path; 178 asprintf(&input_path, "%s/files/pf%s.in", 179 atf_tc_get_config_var(tc, "srcdir"), number); 180 asprintf(&expected_path, "%s/files/pf%s.ok", 181 atf_tc_get_config_var(tc, "srcdir"), number); 182 run_pfctl_test(input_path, expected_path, tc); 183 free(input_path); 184 free(expected_path); 185 } 186 187 static void 188 do_selfpf_test(const char *number, const atf_tc_t *tc) 189 { 190 char *expected_path; 191 asprintf(&expected_path, "%s/files/pf%s.ok", 192 atf_tc_get_config_var(tc, "srcdir"), number); 193 run_pfctl_test(expected_path, expected_path, tc); 194 free(expected_path); 195 } 196 197 #define PFCTL_TEST(number, descr) \ 198 ATF_TC(pf##number); \ 199 ATF_TC_HEAD(pf##number, tc) \ 200 { \ 201 atf_tc_set_md_var(tc, "descr", descr); \ 202 } \ 203 ATF_TC_BODY(pf##number, tc) \ 204 { \ 205 do_pf_test(#number, tc); \ 206 } \ 207 ATF_TC(selfpf##number); \ 208 ATF_TC_HEAD(selfpf##number, tc) \ 209 { \ 210 atf_tc_set_md_var(tc, "descr", "Self " descr); \ 211 } \ 212 ATF_TC_BODY(selfpf##number, tc) \ 213 { \ 214 do_selfpf_test(#number, tc); \ 215 } 216 #include "pfctl_test_list.inc" 217 #undef PFCTL_TEST 218 219 ATF_TP_ADD_TCS(tp) 220 { 221 #define PFCTL_TEST(number, descr) \ 222 ATF_TP_ADD_TC(tp, pf##number); \ 223 ATF_TP_ADD_TC(tp, selfpf##number); 224 #include "pfctl_test_list.inc" 225 #undef PFCTL_TEST 226 227 return atf_no_error(); 228 } 229