-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_filters.c
385 lines (319 loc) · 8.65 KB
/
basic_filters.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (c) 2016 Dmitry V. Levin <[email protected]>
* Copyright (c) 2016-2018 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "defs.h"
#include <regex.h>
#include "filter.h"
#include "number_set.h"
#include "xstring.h"
/**
* Checks whether a @-separated personality specification suffix is present.
* Personality suffix is a one of strings stored in personality_designators
* array.
*
* @param[in] s Specification string to check.
* @param[out] p Where to store personality number if it is found.
* @return If personality is found, the provided string is copied without
* suffix and returned as a result (callee should de-alllocate it
* with free() after use), and personality number is written to p.
* Otherwise, NULL is returned and p is untouched.
*/
static char *
qualify_syscall_separate_personality(const char *s, unsigned int *p)
{
char *pos = strchr(s, '@');
if (!pos)
return NULL;
for (unsigned int i = 0; i < SUPPORTED_PERSONALITIES; i++) {
if (!strcmp(pos + 1, personality_designators[i])) {
*p = i;
return xstrndup(s, pos - s);
}
}
error_msg_and_help("incorrect personality designator '%s'"
" in qualification '%s'", pos + 1, s);
}
static bool
qualify_syscall_number_personality(int n, unsigned int p,
struct number_set *set)
{
if ((unsigned int) n >= nsyscall_vec[p])
return false;
add_number_to_set_array(n, set, p);
return true;
}
static bool
qualify_syscall_number(const char *s, struct number_set *set)
{
unsigned int p;
char *num_str = qualify_syscall_separate_personality(s, &p);
int n;
if (num_str) {
n = string_to_uint(num_str);
free(num_str);
if (n < 0)
return false;
return qualify_syscall_number_personality(n, p, set);
}
n = string_to_uint(s);
if (n < 0)
return false;
bool done = false;
for (p = 0; p < SUPPORTED_PERSONALITIES; ++p)
done |= qualify_syscall_number_personality(n, p, set);
return done;
}
static void
regerror_msg_and_die(int errcode, const regex_t *preg,
const char *str, const char *pattern)
{
char buf[512];
regerror(errcode, preg, buf, sizeof(buf));
error_msg_and_die("%s: %s: %s", str, pattern, buf);
}
static bool
qualify_syscall_regex(const char *s, struct number_set *set)
{
regex_t preg;
int rc;
if ((rc = regcomp(&preg, s, REG_EXTENDED | REG_NOSUB)) != 0)
regerror_msg_and_die(rc, &preg, "regcomp", s);
bool found = false;
for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
if (!sysent_vec[p][i].sys_name)
continue;
rc = regexec(&preg, sysent_vec[p][i].sys_name,
0, NULL, 0);
if (rc == REG_NOMATCH) {
char name_buf[128];
char *pos = stpcpy(name_buf,
sysent_vec[p][i].sys_name);
(void) xappendstr(name_buf, pos, "@%s",
personality_designators[p]);
rc = regexec(&preg, name_buf, 0, NULL, 0);
}
if (rc == REG_NOMATCH)
continue;
else if (rc)
regerror_msg_and_die(rc, &preg, "regexec", s);
add_number_to_set_array(i, set, p);
found = true;
}
}
regfree(&preg);
return found;
}
static unsigned int
lookup_class(const char *s)
{
static const struct {
const char *name;
unsigned int value;
} syscall_class[] = {
{ "%desc", TRACE_DESC },
{ "%file", TRACE_FILE },
{ "%memory", TRACE_MEMORY },
{ "%process", TRACE_PROCESS },
{ "%signal", TRACE_SIGNAL },
{ "%ipc", TRACE_IPC },
{ "%net", TRACE_NETWORK },
{ "%network", TRACE_NETWORK },
{ "%stat", TRACE_STAT },
{ "%lstat", TRACE_LSTAT },
{ "%fstat", TRACE_FSTAT },
{ "%%stat", TRACE_STAT_LIKE },
{ "%statfs", TRACE_STATFS },
{ "%fstatfs", TRACE_FSTATFS },
{ "%%statfs", TRACE_STATFS_LIKE },
{ "%pure", TRACE_PURE },
/* legacy class names */
{ "desc", TRACE_DESC },
{ "file", TRACE_FILE },
{ "memory", TRACE_MEMORY },
{ "process", TRACE_PROCESS },
{ "signal", TRACE_SIGNAL },
{ "ipc", TRACE_IPC },
{ "network", TRACE_NETWORK },
};
for (unsigned int i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
if (strcmp(s, syscall_class[i].name) == 0)
return syscall_class[i].value;
}
return 0;
}
static bool
qualify_syscall_class(const char *s, struct number_set *set)
{
const unsigned int n = lookup_class(s);
if (!n)
return false;
for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
if (sysent_vec[p][i].sys_name &&
(sysent_vec[p][i].sys_flags & n) == n)
add_number_to_set_array(i, set, p);
}
}
return true;
}
kernel_long_t
scno_by_name(const char *s, unsigned int p, kernel_long_t start)
{
if (p >= SUPPORTED_PERSONALITIES)
return -1;
for (kernel_ulong_t i = start; i < nsyscall_vec[p]; ++i) {
if (sysent_vec[p][i].sys_name &&
strcmp(s, sysent_vec[p][i].sys_name) == 0)
return i;
}
return -1;
}
static bool
qualify_syscall_name_personality(const char *s, unsigned int p,
struct number_set *set)
{
bool found = false;
for (kernel_long_t scno = 0; (scno = scno_by_name(s, p, scno)) >= 0;
++scno) {
add_number_to_set_array(scno, set, p);
found = true;
}
return found;
}
static bool
qualify_syscall_name(const char *s, struct number_set *set)
{
unsigned int p;
char *name_str = qualify_syscall_separate_personality(s, &p);
bool found = false;
if (name_str) {
found = qualify_syscall_name_personality(name_str, p, set);
free(name_str);
return found;
}
for (p = 0; p < SUPPORTED_PERSONALITIES; ++p)
found |= qualify_syscall_name_personality(s, p, set);
return found;
}
static bool
qualify_syscall(const char *token, struct number_set *set)
{
bool ignore_fail = false;
while (*token == '?') {
token++;
ignore_fail = true;
}
if (*token >= '0' && *token <= '9')
return qualify_syscall_number(token, set) || ignore_fail;
if (*token == '/')
return qualify_syscall_regex(token + 1, set) || ignore_fail;
return qualify_syscall_class(token, set)
|| qualify_syscall_name(token, set)
|| ignore_fail;
}
/*
* Add syscall numbers to SETs for each supported personality
* according to STR specification.
*/
void
qualify_syscall_tokens(const char *const str, struct number_set *const set)
{
/* Clear all sets. */
clear_number_set_array(set, SUPPORTED_PERSONALITIES);
/*
* Each leading ! character means inversion
* of the remaining specification.
*/
const char *s = str;
while (*s == '!') {
invert_number_set_array(set, SUPPORTED_PERSONALITIES);
++s;
}
if (strcmp(s, "none") == 0) {
/*
* No syscall numbers are added to sets.
* Subsequent is_number_in_set* invocations
* will return set[p]->not.
*/
return;
} else if (strcmp(s, "all") == 0) {
/* "all" == "!none" */
invert_number_set_array(set, SUPPORTED_PERSONALITIES);
return;
}
/*
* Split the string into comma separated tokens.
* For each token, call qualify_syscall that will take care
* if adding appropriate syscall numbers to sets.
* The absence of tokens or a negative return code
* from qualify_syscall is a fatal error.
*/
char *copy = xstrdup(s);
char *saveptr = NULL;
bool done = false;
for (const char *token = strtok_r(copy, ",", &saveptr);
token; token = strtok_r(NULL, ",", &saveptr)) {
done = qualify_syscall(token, set);
if (!done)
error_msg_and_die("invalid system call '%s'", token);
}
free(copy);
if (!done)
error_msg_and_die("invalid system call '%s'", str);
}
/*
* Add numbers to SET according to STR specification.
*/
void
qualify_tokens(const char *const str, struct number_set *const set,
string_to_uint_func func, const char *const name)
{
/* Clear the set. */
clear_number_set_array(set, 1);
/*
* Each leading ! character means inversion
* of the remaining specification.
*/
const char *s = str;
while (*s == '!') {
invert_number_set_array(set, 1);
++s;
}
if (strcmp(s, "none") == 0) {
/*
* No numbers are added to the set.
* Subsequent is_number_in_set* invocations
* will return set->not.
*/
return;
} else if (strcmp(s, "all") == 0) {
/* "all" == "!none" */
invert_number_set_array(set, 1);
return;
}
/*
* Split the string into comma separated tokens.
* For each token, find out the corresponding number
* by calling FUNC, and add that number to the set.
* The absence of tokens or a negative answer
* from FUNC is a fatal error.
*/
char *copy = xstrdup(s);
char *saveptr = NULL;
int number = -1;
for (const char *token = strtok_r(copy, ",", &saveptr);
token; token = strtok_r(NULL, ",", &saveptr)) {
number = func(token);
if (number < 0)
error_msg_and_die("invalid %s '%s'", name, token);
add_number_to_set(number, set);
}
free(copy);
if (number < 0)
error_msg_and_die("invalid %s '%s'", name, str);
}