1 /*
2 * Copyright (c) 2007-2021 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $apfw: pf_ruleset.c,v 1.2 2007/08/10 03:00:16 jhw Exp $ */
30 /* $OpenBSD: pf_ruleset.c,v 1.1 2006/10/27 13:56:51 mcbride Exp $ */
31
32 /*
33 * Copyright (c) 2001 Daniel Hartmeier
34 * Copyright (c) 2002,2003 Henning Brauer
35 * NAT64 - Copyright (c) 2010 Viagenie Inc. (http://www.viagenie.ca)
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * - Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * - Redistributions in binary form must reproduce the above
45 * copyright notice, this list of conditions and the following
46 * disclaimer in the documentation and/or other materials provided
47 * with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
52 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
53 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
59 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 *
62 * Effort sponsored in part by the Defense Advanced Research Projects
63 * Agency (DARPA) and Air Force Research Laboratory, Air Force
64 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
65 *
66 */
67
68 #include <sys/param.h>
69 #include <sys/socket.h>
70 #ifdef KERNEL
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <libkern/libkern.h>
74 #endif /* KERNEL */
75 #include <sys/mbuf.h>
76
77 #include <netinet/ip_dummynet.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/tcp.h>
82
83 #include <net/if.h>
84 #include <net/pfvar.h>
85
86 #include <netinet/ip6.h>
87
88
89 #ifdef KERNEL
90 #define DPFPRINTF(format, x ...) \
91 if (pf_status.debug >= PF_DEBUG_NOISY) \
92 printf(format, ##x)
93 #define rs_malloc_data(size) kalloc_data(size, Z_WAITOK)
94 #define rs_malloc_type(type) kalloc_type(type, Z_WAITOK | Z_ZERO)
95 #define rs_free_data kfree_data
96 #define rs_free_type kfree_type
97
98 #else
99 /* Userland equivalents so we can lend code to pfctl et al. */
100
101 #include <arpa/inet.h>
102 #include <errno.h>
103 #include <stdio.h>
104 #include <stdlib.h>
105 #include <string.h>
106 static __inline void*
rs_malloc_data(size_t size)107 rs_malloc_data(size_t size)
108 {
109 void* result = malloc(size);
110 if (result != NULL) {
111 memset(result, 0, size);
112 }
113 return result;
114 }
115 #define rs_malloc_type(type) ((type*) rs_malloc_data(sizeof(type)))
116 #define rs_free_data(ptr, size) free(ptr)
117 #define rs_free_type(type, ptr) free(ptr)
118
119 #ifdef PFDEBUG
120 #include <sys/stdarg.h>
121 #define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
122 #else
123 #define DPFPRINTF(format, x...) ((void)0)
124 #endif /* PFDEBUG */
125 #endif /* KERNEL */
126
127
128 struct pf_anchor_global pf_anchors;
129 struct pf_anchor pf_main_anchor;
130
131 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
132
133 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
134 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
135
136 static __inline int
pf_anchor_compare(struct pf_anchor * a,struct pf_anchor * b)137 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
138 {
139 int c = strbufcmp(a->path, b->path);
140
141 return c ? (c < 0 ? -1 : 1) : 0;
142 }
143
144 int
pf_get_ruleset_number(u_int8_t action)145 pf_get_ruleset_number(u_int8_t action)
146 {
147 switch (action) {
148 case PF_SCRUB:
149 case PF_NOSCRUB:
150 return PF_RULESET_SCRUB;
151 case PF_PASS:
152 case PF_DROP:
153 return PF_RULESET_FILTER;
154 case PF_NAT:
155 case PF_NONAT:
156 return PF_RULESET_NAT;
157 case PF_BINAT:
158 case PF_NOBINAT:
159 return PF_RULESET_BINAT;
160 case PF_RDR:
161 case PF_NORDR:
162 case PF_NAT64:
163 case PF_NONAT64:
164 return PF_RULESET_RDR;
165 #if DUMMYNET
166 case PF_DUMMYNET:
167 case PF_NODUMMYNET:
168 return PF_RULESET_DUMMYNET;
169 #endif /* DUMMYNET */
170 default:
171 return PF_RULESET_MAX;
172 }
173 }
174
175 void
pf_init_ruleset(struct pf_ruleset * ruleset)176 pf_init_ruleset(struct pf_ruleset *ruleset)
177 {
178 int i;
179
180 memset(ruleset, 0, sizeof(struct pf_ruleset));
181 for (i = 0; i < PF_RULESET_MAX; i++) {
182 TAILQ_INIT(&ruleset->rules[i].queues[0]);
183 TAILQ_INIT(&ruleset->rules[i].queues[1]);
184 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
185 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
186 }
187 }
188
189 struct pf_anchor *
pf_find_anchor(const char * path)190 pf_find_anchor(const char *path)
191 {
192 struct pf_anchor *key, *found;
193
194 key = rs_malloc_type(struct pf_anchor);
195 strlcpy(key->path, path, sizeof(key->path));
196 found = RB_FIND(pf_anchor_global, &pf_anchors, key);
197 rs_free_type(struct pf_anchor, key);
198
199 if (found) {
200 pf_reference_anchor(found);
201 }
202 return found;
203 }
204
205 int
pf_reference_anchor(struct pf_anchor * a)206 pf_reference_anchor(struct pf_anchor *a)
207 {
208 ASSERT(a->refcnt >= 0);
209 LCK_MTX_ASSERT(&pf_lock, LCK_MTX_ASSERT_OWNED);
210 return ++a->refcnt;
211 }
212
213 int
pf_release_anchor(struct pf_anchor * a)214 pf_release_anchor(struct pf_anchor *a)
215 {
216 ASSERT(a->refcnt > 0);
217 LCK_MTX_ASSERT(&pf_lock, LCK_MTX_ASSERT_OWNED);
218 int r = --a->refcnt;
219 if (r == 0) {
220 pf_remove_if_empty_ruleset(&a->ruleset);
221 }
222 return r;
223 }
224
225 struct pf_ruleset *
pf_find_ruleset(const char * path)226 pf_find_ruleset(const char *path)
227 {
228 struct pf_anchor *__single anchor;
229
230 while (*path == '/') {
231 path++;
232 }
233 if (!*path) {
234 return &pf_main_ruleset;
235 }
236 anchor = pf_find_anchor(path);
237 if (anchor == NULL) {
238 return NULL;
239 } else {
240 return &anchor->ruleset;
241 }
242 }
243
244 struct pf_ruleset *
pf_find_ruleset_with_owner(const char * path,const char * owner,int is_anchor,int * error)245 pf_find_ruleset_with_owner(const char *path, const char *owner, int is_anchor,
246 int *error)
247 {
248 struct pf_anchor *anchor;
249
250 while (*path == '/') {
251 path++;
252 }
253 if (!*path) {
254 return &pf_main_ruleset;
255 }
256 anchor = pf_find_anchor(path);
257 if (anchor == NULL) {
258 *error = EINVAL;
259 return NULL;
260 } else {
261 if ((owner && (!strlcmp(anchor->owner, owner, sizeof(anchor->owner))))
262 || (is_anchor && !strlcmp(anchor->owner, "", sizeof(anchor->owner)))) {
263 return &anchor->ruleset;
264 }
265 pf_release_anchor(anchor);
266 anchor = NULL;
267 *error = EPERM;
268 return NULL;
269 }
270 }
271
272 int
pf_release_ruleset(struct pf_ruleset * r)273 pf_release_ruleset(struct pf_ruleset *r)
274 {
275 if (r->anchor == NULL) {
276 return 0;
277 }
278 return pf_release_anchor(r->anchor);
279 }
280
281 #define PF_ANCHOR_DELIMITER '/'
282
283 static char *__bidi_indexable
pf_find_next_anchor_delimiter(char * __bidi_indexable str,bool reverse)284 pf_find_next_anchor_delimiter(char *__bidi_indexable str, bool reverse)
285 {
286 const char *__null_terminated str_nt = __unsafe_null_terminated_from_indexable(str);
287 const char *__null_terminated result_nt = NULL;
288 char *__bidi_indexable result = NULL;
289
290 if (reverse) {
291 result_nt = strrchr(str_nt, PF_ANCHOR_DELIMITER);
292 } else {
293 result_nt = strchr(str_nt, PF_ANCHOR_DELIMITER);
294 }
295 if (result_nt == NULL) {
296 goto done;
297 }
298 if (str_nt > result_nt) {
299 goto done;
300 }
301 size_t off = (size_t) (result_nt - str_nt);
302 result = str + off;
303 done:
304 return result;
305 }
306
307 struct pf_ruleset *
pf_find_or_create_ruleset(const char * path)308 pf_find_or_create_ruleset(const char *path)
309 {
310 char *__bidi_indexable p = NULL;
311 char *__bidi_indexable q = NULL;
312 char *__bidi_indexable r = NULL;
313 struct pf_ruleset *__single ruleset;
314 struct pf_anchor *__single anchor = NULL, *__single dup, *__single parent = NULL;
315
316 if (path[0] == 0) {
317 return &pf_main_ruleset;
318 }
319 while (*path == PF_ANCHOR_DELIMITER) {
320 path++;
321 }
322 ruleset = pf_find_ruleset(path);
323 if (ruleset != NULL) {
324 return ruleset;
325 }
326 p = (char *__bidi_indexable) rs_malloc_data(MAXPATHLEN);
327 strlcpy(p, path, MAXPATHLEN);
328 while (parent == NULL) {
329 q = pf_find_next_anchor_delimiter(p, true);
330 if (q == NULL) {
331 break;
332 }
333 *q = 0;
334 if ((ruleset = pf_find_ruleset(__unsafe_null_terminated_from_indexable(p))) != NULL) {
335 parent = ruleset->anchor;
336 break;
337 }
338 }
339 if (q == NULL) {
340 q = p;
341 } else {
342 q++;
343 }
344 strlcpy(p, path, MAXPATHLEN);
345 if (!*q) {
346 rs_free_data(p, MAXPATHLEN);
347 return NULL;
348 }
349 while ((r = pf_find_next_anchor_delimiter(q, false)) != NULL || *q) {
350 if (r != NULL) {
351 *r = 0;
352 }
353 if (!*q || strlen(__unsafe_null_terminated_from_indexable(q)) >= PF_ANCHOR_NAME_SIZE ||
354 (parent != NULL && strbuflen(parent->path, sizeof(parent->path)) >=
355 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
356 rs_free_data(p, MAXPATHLEN);
357 return NULL;
358 }
359 anchor = rs_malloc_type(struct pf_anchor);
360 if (anchor == NULL) {
361 rs_free_data(p, MAXPATHLEN);
362 return NULL;
363 }
364 RB_INIT(&anchor->children);
365 strbufcpy(anchor->name, sizeof(anchor->name), q, strlen(__unsafe_null_terminated_from_indexable(q)));
366 if (parent != NULL) {
367 strbufcpy(anchor->path, parent->path);
368 strlcat(anchor->path, "/", sizeof(anchor->path));
369 }
370 strbufcat(anchor->path, anchor->name);
371 if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) !=
372 NULL) {
373 printf("pf_find_or_create_ruleset: RB_INSERT1 "
374 "'%s' '%s' collides with '%s' '%s'\n",
375 anchor->path, anchor->name, dup->path, dup->name);
376 rs_free_type(struct pf_anchor, anchor);
377 rs_free_data(p, MAXPATHLEN);
378 return NULL;
379 }
380 if (parent != NULL) {
381 /* reference to parent was already taken by pf_find_anchor() */
382 anchor->parent = parent;
383 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
384 anchor)) != NULL) {
385 printf("pf_find_or_create_ruleset: "
386 "RB_INSERT2 '%s' '%s' collides with "
387 "'%s' '%s'\n", anchor->path, anchor->name,
388 dup->path, dup->name);
389 RB_REMOVE(pf_anchor_global, &pf_anchors,
390 anchor);
391 rs_free_type(struct pf_anchor, anchor);
392 rs_free_data(p, MAXPATHLEN);
393 return NULL;
394 }
395 }
396 pf_init_ruleset(&anchor->ruleset);
397 anchor->ruleset.anchor = anchor;
398 pf_reference_anchor(anchor);
399 parent = anchor;
400 if (r != NULL) {
401 q = r + 1;
402 } else {
403 *q = 0;
404 }
405 #if DUMMYNET
406 if (strlcmp(anchor->name, "com.apple.nlc",
407 sizeof(anchor->name)) == 0) {
408 is_nlc_enabled_glb = TRUE;
409 }
410 #endif
411 }
412 rs_free_data(p, MAXPATHLEN);
413 return anchor ? &anchor->ruleset : 0;
414 }
415
416 void
pf_remove_if_empty_ruleset(struct pf_ruleset * ruleset)417 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
418 {
419 struct pf_anchor *parent;
420 int i;
421
422 if (ruleset == NULL) {
423 return;
424 }
425 /* the main ruleset and anchor even if empty */
426 if (ruleset == &pf_main_ruleset) {
427 return;
428 }
429 /* Each rule, child anchor, and table must take a ref count on the anchor */
430 if (ruleset->anchor == NULL || ruleset->anchor->refcnt > 0) {
431 return;
432 }
433 ASSERT(RB_EMPTY(&ruleset->anchor->children) &&
434 ruleset->tables == 0);
435 /* if we have uncommitted change for tables, bail */
436 if (ruleset->topen > 0) {
437 return;
438 }
439
440
441 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
442 !RB_EMPTY(&ruleset->anchor->children) ||
443 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
444 ruleset->topen) {
445 return;
446 }
447 for (i = 0; i < PF_RULESET_MAX; ++i) {
448 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
449 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
450 ruleset->rules[i].inactive.open) {
451 return;
452 }
453 }
454 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
455 #if DUMMYNET
456 if (strlcmp(ruleset->anchor->name, "com.apple.nlc",
457 sizeof(ruleset->anchor->name)) == 0) {
458 struct dummynet_event dn_event;
459 bzero(&dn_event, sizeof(dn_event));
460 dn_event.dn_event_code = DUMMYNET_NLC_DISABLED;
461 dummynet_event_enqueue_nwk_wq_entry(&dn_event);
462 is_nlc_enabled_glb = FALSE;
463 }
464 #endif
465 if ((parent = ruleset->anchor->parent) != NULL) {
466 RB_REMOVE(pf_anchor_node, &parent->children,
467 ruleset->anchor);
468 }
469 rs_free_type(struct pf_anchor, ruleset->anchor);
470 if (parent == NULL) {
471 return;
472 }
473 pf_release_anchor(parent);
474 }
475
476 int
pf_anchor_setup(struct pf_rule * r,const struct pf_ruleset * s,const char * __counted_by (name_len)name,size_t name_len)477 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
478 const char *__counted_by(name_len)name, size_t name_len)
479 {
480 char *__null_terminated p = NULL, *path;
481 struct pf_ruleset *__single ruleset;
482 char const *namebuf = (char const *__bidi_indexable) name;
483
484 r->anchor = NULL;
485 r->anchor_relative = 0;
486 r->anchor_wildcard = 0;
487 if (!namebuf[0]) {
488 return 0;
489 }
490 path = (char *__bidi_indexable)rs_malloc_data(MAXPATHLEN);
491 if (namebuf[0] == '/') {
492 strbufcpy(path, MAXPATHLEN, namebuf + 1, name_len - 1);
493 } else {
494 /* relative path */
495 r->anchor_relative = 1;
496 if (s->anchor == NULL || !s->anchor->path[0]) {
497 path[0] = 0;
498 } else {
499 strbufcpy(path, MAXPATHLEN, s->anchor->path, sizeof(s->anchor->path));
500 }
501 while (namebuf[0] == '.' && namebuf[1] == '.' && namebuf[2] == '/') {
502 if (!path[0]) {
503 printf("pf_anchor_setup: .. beyond root\n");
504 rs_free_data(path, MAXPATHLEN);
505 return 1;
506 }
507 if ((p = strrchr(__unsafe_null_terminated_from_indexable(path), '/')) != NULL) {
508 *p = 0;
509 } else {
510 path[0] = 0;
511 }
512 r->anchor_relative++;
513 namebuf += 3;
514 }
515 if (path[0]) {
516 strlcat(path, "/", MAXPATHLEN);
517 }
518 strbufcat(path, MAXPATHLEN, namebuf, name_len);
519 }
520 if ((p = strrchr(__unsafe_null_terminated_from_indexable(path), '/')) != NULL &&
521 strcmp(p, "/*") == 0) {
522 r->anchor_wildcard = 1;
523 *p = 0;
524 }
525 ruleset = pf_find_or_create_ruleset(__unsafe_null_terminated_from_indexable(path));
526 rs_free_data(path, MAXPATHLEN);
527 if (ruleset == NULL || ruleset->anchor == NULL) {
528 printf("pf_anchor_setup: ruleset\n");
529 return 1;
530 }
531 r->anchor = ruleset->anchor;
532 return 0;
533 }
534
535 int
pf_anchor_copyout(const struct pf_ruleset * rs,const struct pf_rule * r,struct pfioc_rule * pr)536 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
537 struct pfioc_rule *pr)
538 {
539 pr->anchor_call[0] = 0;
540 if (r->anchor == NULL) {
541 return 0;
542 }
543 if (!r->anchor_relative) {
544 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
545 strbufcat(pr->anchor_call, r->anchor->path);
546 } else {
547 char *a, *__null_terminated p = NULL;
548 int i;
549
550 a = (char *__bidi_indexable)rs_malloc_data(MAXPATHLEN);
551 if (rs->anchor == NULL) {
552 a[0] = 0;
553 } else {
554 strbufcpy(a, MAXPATHLEN, rs->anchor->path, sizeof(rs->anchor->path));
555 }
556 for (i = 1; i < r->anchor_relative; ++i) {
557 if ((p = strrchr(__unsafe_null_terminated_from_indexable(a), '/')) == NULL) {
558 p = __unsafe_null_terminated_from_indexable(a);
559 }
560 *p = 0;
561 strlcat(pr->anchor_call, "../",
562 sizeof(pr->anchor_call));
563 }
564 if (strbufcmp(a, strbuflen(a, MAXPATHLEN), r->anchor->path, strbuflen(a, MAXPATHLEN))) {
565 printf("pf_anchor_copyout: '%s' '%s'\n", a,
566 r->anchor->path);
567 rs_free_data(a, MAXPATHLEN);
568 return 1;
569 }
570 if (strbuflen(r->anchor->path, sizeof(r->anchor->path)) > strbuflen(a, MAXPATHLEN)) {
571 strlcat(pr->anchor_call,
572 __unsafe_null_terminated_from_indexable(r->anchor->path + (a[0] ?
573 strbuflen(a, MAXPATHLEN) + 1 : 0)), sizeof(pr->anchor_call));
574 }
575 rs_free_data(a, MAXPATHLEN);
576 }
577 if (r->anchor_wildcard) {
578 strlcat(pr->anchor_call, __unsafe_null_terminated_from_indexable(pr->anchor_call[0] ? "/*" : "*"),
579 sizeof(pr->anchor_call));
580 }
581 return 0;
582 }
583
584 void
pf_anchor_remove(struct pf_rule * r)585 pf_anchor_remove(struct pf_rule *r)
586 {
587 if (r->anchor == NULL) {
588 return;
589 }
590 if (r->anchor->refcnt <= 0) {
591 printf("pf_anchor_remove: broken refcount\n");
592 r->anchor = NULL;
593 return;
594 }
595 pf_release_anchor(r->anchor);
596 r->anchor = NULL;
597 }
598