-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
151 lines (137 loc) · 2.82 KB
/
main.c
File metadata and controls
151 lines (137 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "types.h"
#include "bpf.h"
#include "loop.h"
void
usage(void)
{
fprintf(stderr, "usage: %s [-hv] [-c count] [-i interface] "
"<command> [args...]\n", getprogname());
}
int
main(int argc, char **argv)
{
int rs, ch;
char err[256];
FILE *log;
struct bpf bpf;
/* defaults */
struct opts opts;
opts.count = 1;
opts.ifname = "pflog0";
opts.verbose = 0;
while ((ch = getopt(argc, argv, "c:hi:v")) != -1) {
switch (ch) {
case 'c':
opts.count = strtoul(optarg, NULL, 10);
if (!opts.count) {
fprintf(stderr, "option 'c' must be greater than zero");
return EXIT_FAILURE;
}
break;
case 'i':
opts.ifname = optarg;
break;
case 'v':
opts.verbose = 1;
break;
case 'h':
default:
usage();
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc) {
opts.path = argv[0];
opts.argv = calloc(1 + argc, sizeof *opts.argv);
if (opts.argv == NULL) {
perror("calloc");
return EXIT_FAILURE;
}
for (int i = 0; i < argc; ++i)
opts.argv[i] = argv[i];
} else {
usage();
return EXIT_FAILURE;
}
if (unveil("/dev/bpf", "r")) {
perror("unveil");
return EXIT_FAILURE;
}
if (unveil("/dev/null", "rw")) {
perror("unveil");
return EXIT_FAILURE;
}
if (unveil(opts.path, "x")) {
perror("unveil");
return EXIT_FAILURE;
}
if (unveil(NULL, NULL)) {
perror("unveil");
return EXIT_FAILURE;
}
if (opts.verbose) {
log = stderr;
}
else {
log = fopen("/dev/null", "r+e");
if (log == NULL) {
perror("failed to open /dev/null");
return EXIT_FAILURE;
}
}
bpf.fd = open("/dev/bpf", O_RDONLY | O_CLOEXEC);
if (bpf.fd == -1) {
perror("failed to open /dev/bpf");
return EXIT_FAILURE;
}
rs = setresuid(getuid(), getuid(), getuid());
if (rs) {
perror("failed to drop setuid privileges");
return EXIT_FAILURE;
}
rs = bpf_setif(bpf.fd, opts.ifname, err, sizeof err);
if (rs) {
fprintf(stderr, "failed to set interface: %s\n", err);
return EXIT_FAILURE;
}
/*
* everything that can be done before BIOCLOCK is now done
*/
rs = bpf_lock(bpf.fd, err, sizeof err);
if (rs) {
fprintf(stderr, "failed to lock bpf: %s\n", err);
return EXIT_FAILURE;
}
rs = bpf_gblen(bpf.fd, &bpf.len, err, sizeof err);
if (rs) {
fprintf(stderr, "failed to get buffer size: %s\n", err);
return EXIT_FAILURE;
}
bpf.buf = malloc(bpf.len);
if (bpf.buf == NULL) {
perror("malloc");
return EXIT_FAILURE;
}
rs = bpf_immediate(bpf.fd, 1, err, sizeof err);
if (rs) {
fprintf(stderr, "failed to set immediate mode: %s\n", err);
return EXIT_FAILURE;
}
rs = pledge("bpf exec proc stdio", NULL);
if (rs) {
perror("pledge");
return EXIT_FAILURE;
}
rs = loop(log, &bpf, &opts);
if (rs)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}