|
| 1 | +/* |
| 2 | +* NPF ruleset tests. |
| 3 | +* |
| 4 | +* Public Domain. |
| 5 | +*/ |
| 6 | + |
| 7 | +#ifdef _KERNEL |
| 8 | +#include <sys/types.h> |
| 9 | +#endif |
| 10 | + |
| 11 | +#include "npf_impl.h" |
| 12 | +#include "npf_test.h" |
| 13 | + |
| 14 | +#include <netinet/in.h> |
| 15 | +#include <sys/socket.h> |
| 16 | +#include <sys/kauth.h> |
| 17 | +#include <sys/socketvar.h> |
| 18 | +#include <sys/lwp.h> |
| 19 | +#include <sys/cpu.h> |
| 20 | + |
| 21 | +#define RESULT_PASS 0 |
| 22 | +#define RESULT_BLOCK ENETUNREACH |
| 23 | + |
| 24 | +/* this port number suitable for testing */ |
| 25 | +#define REMOTE_PORT 65500 |
| 26 | +#define LOCAL_PORT 65000 |
| 27 | +#define LOCAL_IP "127.0.0.1" |
| 28 | +#define REMOTE_IP LOCAL_IP |
| 29 | + |
| 30 | +static const struct test_case { |
| 31 | + int af; |
| 32 | + const char * src; |
| 33 | + uint16_t sport; |
| 34 | + const char * dst; |
| 35 | + uint16_t dport; |
| 36 | + uint32_t uid; |
| 37 | + uint32_t gid; |
| 38 | + const char * ifname; |
| 39 | + int di; |
| 40 | + int ret; |
| 41 | + int stateful_ret; |
| 42 | +} test_cases[] = { |
| 43 | + { |
| 44 | + /* pass in final from $local_ip4 user $Kojo = 1001 group $wheel = 20 */ |
| 45 | + .af = AF_INET, |
| 46 | + .src = "10.1.1.4", .sport = 9000, |
| 47 | + .dst = LOCAL_IP, .dport = LOCAL_PORT, |
| 48 | + .ifname = IFNAME_EXT, .di = PFIL_IN, |
| 49 | + .uid = 1001, .gid = 20, /* matches so pass it */ |
| 50 | + .ret = RESULT_PASS, .stateful_ret = RESULT_PASS |
| 51 | + }, |
| 52 | + { |
| 53 | + /* connect on different UID and block */ |
| 54 | + .af = AF_INET, |
| 55 | + .src = "10.1.1.4", .sport = 9000, |
| 56 | + .dst = LOCAL_IP, .dport = LOCAL_PORT, |
| 57 | + .ifname = IFNAME_EXT, .di = PFIL_IN, |
| 58 | + .uid = 1001, .gid = 10, /* mismatch gid so block it */ |
| 59 | + .ret = RESULT_BLOCK, .stateful_ret = RESULT_BLOCK |
| 60 | + }, |
| 61 | + { |
| 62 | + .af = AF_INET, |
| 63 | + .src = "10.1.1.4", .sport = 9000, |
| 64 | + .dst = LOCAL_IP, .dport = LOCAL_PORT, |
| 65 | + .ifname = IFNAME_EXT, .di = PFIL_IN, |
| 66 | + .uid = 100, .gid = 20, /* mismatch uid so block it */ |
| 67 | + .ret = RESULT_BLOCK, .stateful_ret = RESULT_BLOCK |
| 68 | + }, |
| 69 | + |
| 70 | + |
| 71 | + /* block out final to 127.0.0.1 user > $Kojo( > 1001) group 1 >< $wheel( IRG 1 >< 20) */ |
| 72 | + { |
| 73 | + .af = AF_INET, |
| 74 | + .src = LOCAL_IP, .sport = LOCAL_PORT, |
| 75 | + .dst = REMOTE_IP, .dport = REMOTE_PORT, |
| 76 | + .ifname = IFNAME_EXT, .di = PFIL_OUT, |
| 77 | + .uid = 1005, .gid = 14, /* matches so blocks it */ |
| 78 | + .ret = RESULT_BLOCK, .stateful_ret = RESULT_BLOCK |
| 79 | + }, |
| 80 | + { |
| 81 | + .af = AF_INET, |
| 82 | + .src = LOCAL_IP, .sport = LOCAL_PORT, |
| 83 | + .dst = REMOTE_IP, .dport = REMOTE_PORT, |
| 84 | + .ifname = IFNAME_EXT, .di = PFIL_OUT, |
| 85 | + .uid = 1005, .gid = 30, /* mismatch gid so pass it */ |
| 86 | + .ret = RESULT_PASS, .stateful_ret = RESULT_PASS |
| 87 | + }, |
| 88 | + { |
| 89 | + .af = AF_INET, |
| 90 | + .src = LOCAL_IP, .sport = LOCAL_PORT, |
| 91 | + .dst = REMOTE_IP, .dport = REMOTE_PORT, |
| 92 | + .ifname = IFNAME_EXT, .di = PFIL_OUT, |
| 93 | + .uid = 100, .gid = 15, /* mismatch uid so pass it */ |
| 94 | + .ret = RESULT_PASS, .stateful_ret = RESULT_PASS |
| 95 | + }, |
| 96 | + { |
| 97 | + .af = AF_INET, |
| 98 | + .src = LOCAL_IP, .sport = LOCAL_PORT, |
| 99 | + .dst = REMOTE_IP, .dport = REMOTE_PORT, |
| 100 | + .ifname = IFNAME_EXT, .di = PFIL_OUT, |
| 101 | + .uid = 1010, .gid = 11, /* matches so blocks it */ |
| 102 | + .ret = RESULT_BLOCK, .stateful_ret = RESULT_BLOCK |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +static int |
| 107 | +run_raw_testcase(unsigned i, bool verbose) |
| 108 | +{ |
| 109 | + const struct test_case *t = &test_cases[i]; |
| 110 | + npf_t *npf = npf_getkernctx(); |
| 111 | + npf_cache_t *npc; |
| 112 | + struct mbuf *m; |
| 113 | + npf_rule_t *rl; |
| 114 | + int slock, error; |
| 115 | + |
| 116 | + m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, t->sport, t->dport); |
| 117 | + npc = get_cached_pkt(m, t->ifname); |
| 118 | + |
| 119 | + slock = npf_config_read_enter(npf); |
| 120 | + rl = npf_ruleset_inspect(npc, npf_config_ruleset(npf), t->di, NPF_LAYER_3); |
| 121 | + if (rl) { |
| 122 | + npf_match_info_t mi; |
| 123 | + int id_match; |
| 124 | + |
| 125 | + id_match = npf_rule_match_rid(rl, npc, t->di); |
| 126 | + error = npf_rule_conclude(rl, &mi); |
| 127 | + if (verbose) |
| 128 | + printf("id match is ...%d\n", id_match); |
| 129 | + if (id_match != -1 && !id_match) { |
| 130 | + error = npf_rule_reverse(npc, &mi, error); |
| 131 | + } |
| 132 | + |
| 133 | + } else { |
| 134 | + error = ENOENT; |
| 135 | + } |
| 136 | + npf_config_read_exit(npf, slock); |
| 137 | + |
| 138 | + put_cached_pkt(npc); |
| 139 | + return error; |
| 140 | +} |
| 141 | + |
| 142 | +static int |
| 143 | +run_handler_testcase(unsigned i) |
| 144 | +{ |
| 145 | + const struct test_case *t = &test_cases[i]; |
| 146 | + ifnet_t *ifp = npf_test_getif(t->ifname); |
| 147 | + npf_t *npf = npf_getkernctx(); |
| 148 | + struct mbuf *m; |
| 149 | + int error; |
| 150 | + |
| 151 | + m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, t->sport, t->dport); |
| 152 | + error = npfk_packet_handler(npf, &m, ifp, t->di); |
| 153 | + if (m) { |
| 154 | + m_freem(m); |
| 155 | + } |
| 156 | + return error; |
| 157 | +} |
| 158 | + |
| 159 | +/* |
| 160 | + * we create our specific server socket here which listens on |
| 161 | + * loopback address and port 65000. easier to test pcb lookup here since |
| 162 | + * it will be loaded into the protocol table. |
| 163 | + */ |
| 164 | +static struct socket * |
| 165 | +test_socket(int dir, uid_t uid, gid_t gid) |
| 166 | +{ |
| 167 | + struct sockaddr_in server; |
| 168 | + struct lwp *cur = curlwp; |
| 169 | + void *p, *rp; |
| 170 | + |
| 171 | + memset(&Server, 0, sizeof(server)); |
| 172 | + |
| 173 | + server.sin_len = sizeof(server); |
| 174 | + server.sin_family = AF_INET; |
| 175 | + p = &server.sin_addr.s_addr; |
| 176 | + npf_inet_pton(AF_INET, LOCAL_IP, p); /* we bind to 127.0.0.1 */ |
| 177 | + server.sin_port = htons(LOCAL_PORT); |
| 178 | + |
| 179 | + struct socket *so; |
| 180 | + int error = socreate(AF_INET, &so, SOCK_DGRAM, 0, cur, NULL); |
| 181 | + if (error) { |
| 182 | + printf("socket creation failed: error is %d\n", error); |
| 183 | + return NULL; |
| 184 | + } |
| 185 | + |
| 186 | + solock(so); |
| 187 | + |
| 188 | + kauth_cred_t cred = kauth_cred_alloc(); |
| 189 | + kauth_cred_seteuid(cred, uid); |
| 190 | + kauth_cred_setegid(cred, gid); |
| 191 | + |
| 192 | + kauth_cred_t old = so->so_cred; |
| 193 | + so->so_cred = kauth_cred_dup(cred); |
| 194 | + kauth_cred_free(old); |
| 195 | + |
| 196 | + sounlock(so); |
| 197 | + |
| 198 | + if ((error = sobind(so, (struct sockaddr *)&server, cur)) != 0) { |
| 199 | + printf("bind failed %d\n", error); |
| 200 | + return NULL; |
| 201 | + } |
| 202 | + |
| 203 | + if (dir == PFIL_OUT) { |
| 204 | + /* connect to an additional remote address to set the 4 tuple addr-port state */ |
| 205 | + struct sockaddr_in remote; |
| 206 | + memset(&Remote, 0, sizeof(remote)); |
| 207 | + |
| 208 | + remote.sin_len = sizeof(remote); |
| 209 | + remote.sin_family = AF_INET; |
| 210 | + rp = &remote.sin_addr.s_addr; |
| 211 | + npf_inet_pton(AF_INET, REMOTE_IP, rp); /* we connect to 127.0.0.1 */ |
| 212 | + remote.sin_port = htons(REMOTE_PORT); |
| 213 | + |
| 214 | + solock(so); |
| 215 | + if ((error = soconnect(so, (struct sockaddr *)&remote, cur)) != 0) { |
| 216 | + printf("connect failed :%d\n", error); |
| 217 | + return NULL; |
| 218 | + } |
| 219 | + sounlock(so); |
| 220 | + } |
| 221 | + |
| 222 | + return so; |
| 223 | +} |
| 224 | + |
| 225 | +static bool |
| 226 | +test_static(bool verbose) |
| 227 | +{ |
| 228 | + for (size_t i = 0; i < __arraycount(test_cases); i++) { |
| 229 | + const struct test_case *t = &test_cases[i]; |
| 230 | + int error, serror; |
| 231 | + struct socket *so; |
| 232 | + |
| 233 | + so = test_socket(t->di, t->uid, t->gid); |
| 234 | + if (so == NULL) { |
| 235 | + printf("socket:\n"); |
| 236 | + return false; |
| 237 | + } |
| 238 | + |
| 239 | + if (npf_test_getif(t->ifname) == NULL) { |
| 240 | + printf("Interface %s is not configured.\n", t->ifname); |
| 241 | + return false; |
| 242 | + } |
| 243 | + |
| 244 | + error = run_raw_testcase(i, verbose); |
| 245 | + serror = run_handler_testcase(i); |
| 246 | + |
| 247 | + if (verbose) { |
| 248 | + printf("rule test %d:\texpected %d (stateful) and %d\n" |
| 249 | + "\t\t-> returned %d and %d\n", |
| 250 | + i + 1, t->stateful_ret, t->ret, serror, error); |
| 251 | + } |
| 252 | + CHECK_TRUE(error == t->ret); |
| 253 | + CHECK_TRUE(serror == t->stateful_ret) |
| 254 | + |
| 255 | + soclose(so); |
| 256 | + } |
| 257 | + return true; |
| 258 | +} |
| 259 | + |
| 260 | +bool |
| 261 | +npf_guid_test(bool verbose) |
| 262 | +{ |
| 263 | + soinit1(); |
| 264 | + |
| 265 | + bool ok; |
| 266 | + |
| 267 | + ok = test_static(verbose); |
| 268 | + CHECK_TRUE(ok); |
| 269 | + |
| 270 | + return true; |
| 271 | +} |
0 commit comments