-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathstruct_catalog.c
More file actions
535 lines (485 loc) · 18.2 KB
/
struct_catalog.c
File metadata and controls
535 lines (485 loc) · 18.2 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* Struct catalog and offset mapping for CMP-guided struct filling.
*
* Provides a static catalog of known struct types (with per-field offset
* and size), a table mapping syscall args to those struct types, and a
* fast nr-indexed lookup built at init time.
*
* The field-for-CMP heuristic uses value magnitude to narrow which field
* a kernel CMP constant was most likely comparing against.
*/
#include <stddef.h>
#include <string.h>
#include <signal.h>
#include <sys/timex.h>
#include <sys/resource.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <time.h>
#include <linux/sched.h>
#include <linux/sched/types.h>
#include <linux/io_uring.h>
#include "config.h"
#ifdef USE_BPF
#include <linux/bpf.h>
#endif
#include "struct_catalog.h"
#include "arch.h"
#include "perf_event.h"
#include "random.h"
#include "tables.h"
#include "trinity.h"
#include "utils.h"
/* Helper: build a struct_field entry from struct S, member m. */
#define FIELD(S, m) \
{ .name = #m, \
.offset = offsetof(S, m), \
.size = sizeof(((S *)NULL)->m) }
/* ------------------------------------------------------------------ */
/* struct timex (adjtimex, clock_adjtime) */
/* ------------------------------------------------------------------ */
static const struct struct_field timex_fields[] = {
FIELD(struct timex, modes),
FIELD(struct timex, offset),
FIELD(struct timex, freq),
FIELD(struct timex, maxerror),
FIELD(struct timex, esterror),
FIELD(struct timex, status),
FIELD(struct timex, constant),
FIELD(struct timex, precision),
FIELD(struct timex, tolerance),
FIELD(struct timex, tick),
FIELD(struct timex, ppsfreq),
FIELD(struct timex, jitter),
FIELD(struct timex, shift),
FIELD(struct timex, stabil),
FIELD(struct timex, jitcnt),
FIELD(struct timex, calcnt),
FIELD(struct timex, errcnt),
FIELD(struct timex, stbcnt),
};
/* ------------------------------------------------------------------ */
/* struct sched_attr (sched_setattr, sched_getattr) */
/* ------------------------------------------------------------------ */
static const struct struct_field sched_attr_fields[] = {
FIELD(struct sched_attr, size),
FIELD(struct sched_attr, sched_policy),
FIELD(struct sched_attr, sched_flags),
FIELD(struct sched_attr, sched_nice),
FIELD(struct sched_attr, sched_priority),
FIELD(struct sched_attr, sched_runtime),
FIELD(struct sched_attr, sched_deadline),
FIELD(struct sched_attr, sched_period),
FIELD(struct sched_attr, sched_util_min),
FIELD(struct sched_attr, sched_util_max),
};
/* ------------------------------------------------------------------ */
/* struct clone_args (clone3) */
/* ------------------------------------------------------------------ */
static const struct struct_field clone_args_fields[] = {
FIELD(struct clone_args, flags),
FIELD(struct clone_args, pidfd),
FIELD(struct clone_args, child_tid),
FIELD(struct clone_args, parent_tid),
FIELD(struct clone_args, exit_signal),
FIELD(struct clone_args, stack),
FIELD(struct clone_args, stack_size),
FIELD(struct clone_args, tls),
FIELD(struct clone_args, set_tid),
FIELD(struct clone_args, set_tid_size),
FIELD(struct clone_args, cgroup),
};
/* ------------------------------------------------------------------ */
/* struct io_uring_params (io_uring_setup) */
/* ------------------------------------------------------------------ */
static const struct struct_field io_uring_params_fields[] = {
FIELD(struct io_uring_params, sq_entries),
FIELD(struct io_uring_params, cq_entries),
FIELD(struct io_uring_params, flags),
FIELD(struct io_uring_params, sq_thread_cpu),
FIELD(struct io_uring_params, sq_thread_idle),
FIELD(struct io_uring_params, features),
FIELD(struct io_uring_params, wq_fd),
};
/* ------------------------------------------------------------------ */
/* struct rlimit (setrlimit, getrlimit, prlimit64) */
/* ------------------------------------------------------------------ */
static const struct struct_field rlimit_fields[] = {
FIELD(struct rlimit, rlim_cur),
FIELD(struct rlimit, rlim_max),
};
/* ------------------------------------------------------------------ */
/* struct itimerspec (timer_settime, timerfd_settime) */
/* ------------------------------------------------------------------ */
static const struct struct_field itimerspec_fields[] = {
FIELD(struct itimerspec, it_interval.tv_sec),
FIELD(struct itimerspec, it_interval.tv_nsec),
FIELD(struct itimerspec, it_value.tv_sec),
FIELD(struct itimerspec, it_value.tv_nsec),
};
/* ------------------------------------------------------------------ */
/* struct epoll_event (epoll_ctl) */
/* ------------------------------------------------------------------ */
static const struct struct_field epoll_event_fields[] = {
FIELD(struct epoll_event, events),
};
/* ------------------------------------------------------------------ */
/* struct perf_event_attr (perf_event_open) */
/* ------------------------------------------------------------------ */
/*
* Only the addressable scalar fields are listed; the long bitfield run
* (disabled, inherit, ..., sigtrap, __reserved_1) cannot be referenced
* by offsetof and is intentionally omitted. CMP attribution still
* benefits from every full-byte field below — particularly type, size,
* sample_type, read_format, branch_sample_type, sample_regs_*, and the
* union'd config / bp_addr / bp_len slots that the kernel constantly
* compares against PERF_* constants.
*/
static const struct struct_field perf_event_attr_fields[] = {
FIELD(struct perf_event_attr, type),
FIELD(struct perf_event_attr, size),
FIELD(struct perf_event_attr, config),
FIELD(struct perf_event_attr, sample_period),
FIELD(struct perf_event_attr, sample_type),
FIELD(struct perf_event_attr, read_format),
FIELD(struct perf_event_attr, wakeup_events),
FIELD(struct perf_event_attr, bp_type),
FIELD(struct perf_event_attr, bp_addr),
FIELD(struct perf_event_attr, bp_len),
FIELD(struct perf_event_attr, branch_sample_type),
FIELD(struct perf_event_attr, sample_regs_user),
FIELD(struct perf_event_attr, sample_stack_user),
FIELD(struct perf_event_attr, clockid),
FIELD(struct perf_event_attr, sample_regs_intr),
FIELD(struct perf_event_attr, aux_watermark),
FIELD(struct perf_event_attr, sample_max_stack),
FIELD(struct perf_event_attr, aux_sample_size),
FIELD(struct perf_event_attr, aux_action),
FIELD(struct perf_event_attr, sig_data),
FIELD(struct perf_event_attr, config3),
};
/* ------------------------------------------------------------------ */
/* struct sigaction (rt_sigaction, sigaction) */
/* ------------------------------------------------------------------ */
static const struct struct_field sigaction_fields[] = {
FIELD(struct sigaction, sa_flags),
};
/* ------------------------------------------------------------------ */
/* struct msghdr (sendmsg, recvmsg) */
/* ------------------------------------------------------------------ */
static const struct struct_field msghdr_fields[] = {
FIELD(struct msghdr, msg_name),
FIELD(struct msghdr, msg_namelen),
FIELD(struct msghdr, msg_iov),
FIELD(struct msghdr, msg_iovlen),
FIELD(struct msghdr, msg_control),
FIELD(struct msghdr, msg_controllen),
FIELD(struct msghdr, msg_flags),
};
/* ------------------------------------------------------------------ */
/* struct sockaddr_storage (bind, connect, sendto, ...) */
/* ------------------------------------------------------------------ */
/*
* Only ss_family is interesting from a CMP standpoint — the kernel's
* sockaddr dispatch starts by comparing it against AF_* constants. The
* rest of the buffer is opaque padding whose meaning depends entirely on
* which family the kernel routed to.
*/
static const struct struct_field sockaddr_storage_fields[] = {
FIELD(struct sockaddr_storage, ss_family),
};
/* ------------------------------------------------------------------ */
/* union bpf_attr (bpf) */
/* ------------------------------------------------------------------ */
#ifdef USE_BPF
/*
* union bpf_attr is a tagged union dispatched on the bpf() cmd argument.
* Every command's anonymous struct starts at offset 0, so most of the
* fields below collide on offset — that's intentional. The CMP heuristic
* picks at random across same-size matches, so a u32 CMP hint may resolve
* to map_type, prog_type, attach_type, or any of the other enum-bearing
* slots the kernel constantly compares against the BPF_* constant space.
*
* Only the most-compared scalar fields are listed; the deeper per-cmd
* substructs (test, link_create, link_update, raw_tracepoint, info, ...)
* carry mostly fds and addresses that the kernel passes through rather
* than compares.
*/
static const struct struct_field bpf_attr_fields[] = {
FIELD(union bpf_attr, map_type),
FIELD(union bpf_attr, key_size),
FIELD(union bpf_attr, value_size),
FIELD(union bpf_attr, max_entries),
FIELD(union bpf_attr, map_flags),
FIELD(union bpf_attr, prog_type),
FIELD(union bpf_attr, insn_cnt),
FIELD(union bpf_attr, log_level),
FIELD(union bpf_attr, log_size),
FIELD(union bpf_attr, kern_version),
FIELD(union bpf_attr, prog_flags),
FIELD(union bpf_attr, expected_attach_type),
FIELD(union bpf_attr, attach_type),
FIELD(union bpf_attr, attach_flags),
FIELD(union bpf_attr, start_id),
FIELD(union bpf_attr, prog_id),
FIELD(union bpf_attr, map_id),
};
#endif
/* ------------------------------------------------------------------ */
/* The catalog itself */
/* ------------------------------------------------------------------ */
const struct struct_desc struct_catalog[] = {
{
.name = "timex",
.struct_size = sizeof(struct timex),
.fields = timex_fields,
.num_fields = ARRAY_SIZE(timex_fields),
},
{
.name = "sched_attr",
.struct_size = sizeof(struct sched_attr),
.fields = sched_attr_fields,
.num_fields = ARRAY_SIZE(sched_attr_fields),
},
{
.name = "clone_args",
.struct_size = sizeof(struct clone_args),
.fields = clone_args_fields,
.num_fields = ARRAY_SIZE(clone_args_fields),
},
{
.name = "io_uring_params",
.struct_size = sizeof(struct io_uring_params),
.fields = io_uring_params_fields,
.num_fields = ARRAY_SIZE(io_uring_params_fields),
},
{
.name = "rlimit",
.struct_size = sizeof(struct rlimit),
.fields = rlimit_fields,
.num_fields = ARRAY_SIZE(rlimit_fields),
},
{
.name = "itimerspec",
.struct_size = sizeof(struct itimerspec),
.fields = itimerspec_fields,
.num_fields = ARRAY_SIZE(itimerspec_fields),
},
{
.name = "epoll_event",
.struct_size = sizeof(struct epoll_event),
.fields = epoll_event_fields,
.num_fields = ARRAY_SIZE(epoll_event_fields),
},
{
.name = "perf_event_attr",
.struct_size = sizeof(struct perf_event_attr),
.fields = perf_event_attr_fields,
.num_fields = ARRAY_SIZE(perf_event_attr_fields),
},
{
.name = "sigaction",
.struct_size = sizeof(struct sigaction),
.fields = sigaction_fields,
.num_fields = ARRAY_SIZE(sigaction_fields),
},
{
.name = "msghdr",
.struct_size = sizeof(struct msghdr),
.fields = msghdr_fields,
.num_fields = ARRAY_SIZE(msghdr_fields),
},
{
.name = "sockaddr_storage",
.struct_size = sizeof(struct sockaddr_storage),
.fields = sockaddr_storage_fields,
.num_fields = ARRAY_SIZE(sockaddr_storage_fields),
},
#ifdef USE_BPF
{
.name = "bpf_attr",
.struct_size = sizeof(union bpf_attr),
.fields = bpf_attr_fields,
.num_fields = ARRAY_SIZE(bpf_attr_fields),
},
#endif
};
const unsigned int struct_catalog_count = ARRAY_SIZE(struct_catalog);
/* ------------------------------------------------------------------ */
/* Syscall -> struct arg mapping */
/* ------------------------------------------------------------------ */
/*
* Maps (syscall name, 1-based arg index) to the struct type passed at
* that argument. Only covers args that are struct pointers filled by
* a custom sanitise callback. Terminated by .syscall_name == NULL.
*/
const struct syscall_struct_arg syscall_struct_args[] = {
/* adjtimex(struct timex *) */
{ "adjtimex", 1, &struct_catalog[0] },
/* clock_adjtime(clockid_t, struct timex *) */
{ "clock_adjtime", 2, &struct_catalog[0] },
/* sched_setattr(pid_t, struct sched_attr *, unsigned int) */
{ "sched_setattr", 2, &struct_catalog[1] },
/* sched_getattr(pid_t, struct sched_attr *, unsigned int, unsigned int) */
{ "sched_getattr", 2, &struct_catalog[1] },
/* clone3(struct clone_args *, size_t) */
{ "clone3", 1, &struct_catalog[2] },
/* io_uring_setup(u32, struct io_uring_params *) */
{ "io_uring_setup", 2, &struct_catalog[3] },
/* setrlimit(unsigned int, struct rlimit *) */
{ "setrlimit", 2, &struct_catalog[4] },
/* getrlimit(unsigned int, struct rlimit *) */
{ "getrlimit", 2, &struct_catalog[4] },
/* prlimit64(pid_t, unsigned int, struct rlimit *, struct rlimit *) */
{ "prlimit64", 3, &struct_catalog[4] },
{ "prlimit64", 4, &struct_catalog[4] },
/* timer_settime(timer_t, int, struct itimerspec *, struct itimerspec *) */
{ "timer_settime", 3, &struct_catalog[5] },
/* timerfd_settime(int, int, struct itimerspec *, struct itimerspec *) */
{ "timerfd_settime", 3, &struct_catalog[5] },
/* epoll_ctl(int, int, int, struct epoll_event *) */
{ "epoll_ctl", 4, &struct_catalog[6] },
/* perf_event_open(struct perf_event_attr *, pid_t, int, int, ulong) */
{ "perf_event_open", 1, &struct_catalog[7] },
/* rt_sigaction(int, const struct sigaction *, struct sigaction *, size_t) */
{ "rt_sigaction", 2, &struct_catalog[8] },
{ "rt_sigaction", 3, &struct_catalog[8] },
/* sigaction(int, const struct old_sigaction *, struct old_sigaction *) */
{ "sigaction", 2, &struct_catalog[8] },
{ "sigaction", 3, &struct_catalog[8] },
/* sendmsg(int, const struct msghdr *, int) */
{ "sendmsg", 2, &struct_catalog[9] },
/* recvmsg(int, struct msghdr *, int) */
{ "recvmsg", 2, &struct_catalog[9] },
/* bind(int, struct sockaddr *, socklen_t) */
{ "bind", 2, &struct_catalog[10] },
/* connect(int, struct sockaddr *, socklen_t) */
{ "connect", 2, &struct_catalog[10] },
/* sendto(int, const void *, size_t, int, struct sockaddr *, socklen_t) */
{ "sendto", 5, &struct_catalog[10] },
#ifdef USE_BPF
/* bpf(int, union bpf_attr *, unsigned int) */
{ "bpf", 2, &struct_catalog[11] },
#endif
/* sentinel */
{ NULL, 0, NULL },
};
/* ------------------------------------------------------------------ */
/* Fast nr -> desc lookup table */
/* ------------------------------------------------------------------ */
/*
* desc_by_nr_64[syscall_nr][arg_idx - 1] -> struct_desc* or NULL.
* desc_by_nr_32[syscall_nr][arg_idx - 1] -> struct_desc* or NULL.
* Populated at init time by scanning the active syscall table.
* Split to avoid collisions when biarch builds have different syscall
* numbers for 32-bit and 64-bit that happen to overlap.
*/
static const struct struct_desc *desc_by_nr_64[MAX_NR_SYSCALL][6];
static const struct struct_desc *desc_by_nr_32[MAX_NR_SYSCALL][6];
/* ------------------------------------------------------------------ */
/* API */
/* ------------------------------------------------------------------ */
const struct struct_desc *struct_catalog_lookup(const char *name)
{
unsigned int i;
for (i = 0; i < struct_catalog_count; i++) {
if (strcmp(struct_catalog[i].name, name) == 0)
return &struct_catalog[i];
}
return NULL;
}
const struct struct_desc *struct_arg_lookup(unsigned int nr,
unsigned int arg_idx,
bool do32bit)
{
if (nr >= MAX_NR_SYSCALL || arg_idx < 1 || arg_idx > 6)
return NULL;
if (do32bit)
return desc_by_nr_32[nr][arg_idx - 1];
return desc_by_nr_64[nr][arg_idx - 1];
}
/*
* Return the natural byte width needed to represent val:
* val < 2^8 -> 1, < 2^16 -> 2, < 2^32 -> 4, else 8.
*/
static unsigned int natural_width(unsigned long val)
{
if (val < (1UL << 8))
return 1;
if (val < (1UL << 16))
return 2;
if (val < (1UL << 32))
return 4;
return 8;
}
int struct_field_for_cmp(const struct struct_desc *desc, unsigned long val)
{
unsigned int want = natural_width(val);
unsigned int i, count, pick;
/*
* Two passes: prefer exact size matches, fall back to fields large
* enough to hold the value. Within each pass pick uniformly at random
* so every qualifying field gets exercised.
*/
count = 0;
for (i = 0; i < desc->num_fields; i++) {
if (desc->fields[i].size == want)
count++;
}
if (count) {
pick = rand32() % count;
for (i = 0; i < desc->num_fields; i++) {
if (desc->fields[i].size == want && pick-- == 0)
return (int) i;
}
}
count = 0;
for (i = 0; i < desc->num_fields; i++) {
if (desc->fields[i].size >= want)
count++;
}
if (count) {
pick = rand32() % count;
for (i = 0; i < desc->num_fields; i++) {
if (desc->fields[i].size >= want && pick-- == 0)
return (int) i;
}
}
return -1;
}
void struct_catalog_init(void)
{
const struct syscall_struct_arg *sa;
unsigned int i;
int nr;
memset(desc_by_nr_64, 0, sizeof(desc_by_nr_64));
memset(desc_by_nr_32, 0, sizeof(desc_by_nr_32));
for (sa = syscall_struct_args; sa->syscall_name != NULL; sa++) {
if (sa->arg_idx < 1 || sa->arg_idx > 6)
continue;
/* Search the active syscall table(s) for this name. */
if (biarch) {
nr = search_syscall_table(syscalls_64bit,
max_nr_64bit_syscalls,
sa->syscall_name);
if (nr >= 0 && (unsigned int) nr < MAX_NR_SYSCALL)
desc_by_nr_64[nr][sa->arg_idx - 1] = sa->desc;
nr = search_syscall_table(syscalls_32bit,
max_nr_32bit_syscalls,
sa->syscall_name);
if (nr >= 0 && (unsigned int) nr < MAX_NR_SYSCALL)
desc_by_nr_32[nr][sa->arg_idx - 1] = sa->desc;
} else {
nr = search_syscall_table(syscalls,
max_nr_syscalls,
sa->syscall_name);
if (nr >= 0 && (unsigned int) nr < MAX_NR_SYSCALL)
desc_by_nr_64[nr][sa->arg_idx - 1] = sa->desc;
}
}
for (i = 0; i < struct_catalog_count; i++)
output(0, "struct catalog: registered %s (%u fields, %u bytes)\n",
struct_catalog[i].name,
struct_catalog[i].num_fields,
struct_catalog[i].struct_size);
}