xref: /xnu-10002.1.13/tests/debug_syscall_rejection_helper.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions #include <errno.h>
2*1031c584SApple OSS Distributions #include <stdbool.h>
3*1031c584SApple OSS Distributions #include <stdio.h>
4*1031c584SApple OSS Distributions #include <stdlib.h>
5*1031c584SApple OSS Distributions #include <string.h>
6*1031c584SApple OSS Distributions #include <unistd.h>
7*1031c584SApple OSS Distributions 
8*1031c584SApple OSS Distributions #include <sys/kern_debug.h>
9*1031c584SApple OSS Distributions 
10*1031c584SApple OSS Distributions int
main(int argc,char * argv[])11*1031c584SApple OSS Distributions main(int argc, char *argv[])
12*1031c584SApple OSS Distributions {
13*1031c584SApple OSS Distributions 	int opt;
14*1031c584SApple OSS Distributions 
15*1031c584SApple OSS Distributions 	syscall_rejection_selector_t masks[16] = { 0 };
16*1031c584SApple OSS Distributions 
17*1031c584SApple OSS Distributions 	int pos = 0;
18*1031c584SApple OSS Distributions 	unsigned char selector = 0;
19*1031c584SApple OSS Distributions 	bool next_is_allow = false;
20*1031c584SApple OSS Distributions 
21*1031c584SApple OSS Distributions 	uint64_t flags = SYSCALL_REJECTION_FLAGS_DEFAULT;
22*1031c584SApple OSS Distributions 
23*1031c584SApple OSS Distributions 	while ((opt = getopt(argc, argv, "ads:i:OF")) != -1) {
24*1031c584SApple OSS Distributions 		switch (opt) {
25*1031c584SApple OSS Distributions 		case 'a':
26*1031c584SApple OSS Distributions 			next_is_allow = true;
27*1031c584SApple OSS Distributions 			break;
28*1031c584SApple OSS Distributions 		case 'd':
29*1031c584SApple OSS Distributions 			next_is_allow = false;
30*1031c584SApple OSS Distributions 			break;
31*1031c584SApple OSS Distributions 		case 's':
32*1031c584SApple OSS Distributions 			selector = (syscall_rejection_selector_t)atoi(optarg);
33*1031c584SApple OSS Distributions 			break;
34*1031c584SApple OSS Distributions 		case 'i':
35*1031c584SApple OSS Distributions 			pos = atoi(optarg);
36*1031c584SApple OSS Distributions 			if (next_is_allow) {
37*1031c584SApple OSS Distributions 				// printf("%i: ALLOW %u\n", pos, (unsigned int)selector);
38*1031c584SApple OSS Distributions 				masks[pos] = SYSCALL_REJECTION_ALLOW(selector);
39*1031c584SApple OSS Distributions 			} else {
40*1031c584SApple OSS Distributions 				// printf("%i: DENY %u\n", pos, (unsigned int)selector);
41*1031c584SApple OSS Distributions 				masks[pos] = SYSCALL_REJECTION_DENY(selector);
42*1031c584SApple OSS Distributions 			}
43*1031c584SApple OSS Distributions 			break;
44*1031c584SApple OSS Distributions 		case 'O':
45*1031c584SApple OSS Distributions 			flags |= SYSCALL_REJECTION_FLAGS_ONCE;
46*1031c584SApple OSS Distributions 			break;
47*1031c584SApple OSS Distributions 		case 'F':
48*1031c584SApple OSS Distributions 			flags |= SYSCALL_REJECTION_FLAGS_FORCE_FATAL;
49*1031c584SApple OSS Distributions 			break;
50*1031c584SApple OSS Distributions 		default:
51*1031c584SApple OSS Distributions 			fprintf(stderr, "unknown option '%c'\n", opt);
52*1031c584SApple OSS Distributions 			exit(2);
53*1031c584SApple OSS Distributions 		}
54*1031c584SApple OSS Distributions 	}
55*1031c584SApple OSS Distributions 
56*1031c584SApple OSS Distributions 	debug_syscall_reject_config(masks, sizeof(masks) / sizeof(masks[0]), flags);
57*1031c584SApple OSS Distributions 
58*1031c584SApple OSS Distributions 	int __unused ret = chdir("/tmp");
59*1031c584SApple OSS Distributions 
60*1031c584SApple OSS Distributions 	syscall_rejection_selector_t all_allow_masks[16] = { 0 };
61*1031c584SApple OSS Distributions 	all_allow_masks[0] = SYSCALL_REJECTION_ALLOW(SYSCALL_REJECTION_ALL);
62*1031c584SApple OSS Distributions 
63*1031c584SApple OSS Distributions 	debug_syscall_reject_config(all_allow_masks, sizeof(all_allow_masks) / sizeof(all_allow_masks[0]), SYSCALL_REJECTION_FLAGS_DEFAULT);
64*1031c584SApple OSS Distributions 
65*1031c584SApple OSS Distributions 	return 0;
66*1031c584SApple OSS Distributions }
67