-
Notifications
You must be signed in to change notification settings - Fork 30
/
auevent.c
707 lines (679 loc) · 20.5 KB
/
auevent.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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2019, Daniel Roethlisberger <daniel@roe.ch>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
#include "auevent.h"
#include "aupipe.h"
#include "minmax.h"
#include "sys.h"
#include "aev.h"
#include "logutl.h"
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <arpa/inet.h>
#include <bsm/libbsm.h>
#include <bsm/audit_kevents.h>
#include <bsm/audit_domain.h>
#include <bsm/audit_socket_type.h>
/*
* Record token structs in libbsm:
* https://github.com/openbsm/openbsm/blob/master/bsm/libbsm.h
*
* AUT_* token constants in kernel:
* https://github.com/apple/darwin-xnu/blob/master/bsd/bsm/audit_record.h
*
* AUE_* event constants in kernel:
* https://github.com/apple/darwin-xnu/blob/master/bsd/bsm/audit_kevents.h
*/
static dev_t devnull;
int
auevent_init(void) {
devnull = sys_devbypath("/dev/null");
if (devnull == (dev_t)-1)
return -1;
return 0;
}
#define SET_DEV(DST_DEV, SRC_TID) \
DST_DEV = ((dev_t)(SRC_TID).port) == devnull ? -1 : (SRC_TID).port;
#define SET_ADDR(DST_ADDR, SRC_TID) \
if ((SRC_TID).addr != 0) { \
(DST_ADDR).family = AF_INET; \
(DST_ADDR).ev_addr = (SRC_TID).addr; \
}
#define SET_ADDR_EX(DST_ADDR, SRC_TID) \
if ((SRC_TID).type == AU_IPv4) { \
if ((SRC_TID).addr[0] != 0) { \
(DST_ADDR).family = AF_INET; \
(DST_ADDR).ev_addr = (SRC_TID).addr[0]; \
} \
} else if ((SRC_TID).type == AU_IPv6) { \
(DST_ADDR).family = AF_INET6; \
(DST_ADDR).ev6_addr[0] = (SRC_TID).addr[0]; \
(DST_ADDR).ev6_addr[1] = (SRC_TID).addr[1]; \
(DST_ADDR).ev6_addr[2] = (SRC_TID).addr[2]; \
(DST_ADDR).ev6_addr[3] = (SRC_TID).addr[3]; \
}
/*
* While this functionality is still present, it is not currently being used
* by xnumon, so the linear search is not an issue.
*/
static bool
auevent_type_in_typelist(const uint16_t type, const uint16_t typelist[]) {
int i = 0;
if (!typelist)
return true;
while (typelist[i]) {
if (type == typelist[i])
return true;
i++;
}
return false;
}
/*
* ev must be created using auevent_create before every call to
* auevent_fread and destroyed after using the results.
*
* returns 0 to indicate that a record was skipped
* returns 1 to indicate that a record was read into ev
* returns -1 on errors
*/
ssize_t
auevent_fread(audit_event_t *ev, const uint16_t aues[], int flags, FILE *f) {
int rv;
int reclen;
tokenstr_t tok;
size_t textc;
size_t pathc;
assert(ev);
/*
* https://github.com/openbsm/openbsm/blob/master/libbsm/bsm_io.c
*
* au_read_rec always reads a whole record. On read errors or short
* reads due to non-blocking I/O, it returns an error and leaves the
* file pointer dangling where it was without returning the partially
* read buffer. While using blocking file descriptors on a sane
* kernel, this should work for us and read exactly one event from
* the file descriptor per call.
*/
reclen = au_read_rec(f, &ev->recbuf);
if (reclen == -1) {
fprintf(stderr, "au_read_rec(): %s (%i)\n",
strerror(errno), errno);
return -1;
}
if (reclen == 0)
goto skip_rec;
textc = 0;
pathc = 0;
for (int recpos = 0; recpos < reclen;) {
rv = au_fetch_tok(&tok, ev->recbuf+recpos, reclen-recpos);
if (rv == -1) {
/* partial record; libbsm's current implementation
* of au_read_rec never reads a partial record.
* If it would, there would be a need for handling
* partial records gracefully (praudit does not). */
fprintf(stderr, "au_fetch_tok() returns error,"
" skipping partial record\n");
goto skip_rec;
}
/*
* XNU reports subjects and processes not attached to any TTY
* with tty device /dev/null and tty addr 0.0.0.0.
* Translate those here to no device represented by (dev_t)-1
* and no addr represented by address family 0, respectively.
*
* The timestamp in the headers is nanotime() shortly before
* the syscall returns to the calling userspace process.
*/
switch (tok.id) {
/* record header and trailer */
case AUT_HEADER32:
ev->type = tok.tt.hdr32.e_type;
if (aues && !auevent_type_in_typelist(ev->type, aues))
goto skip_rec;
ev->mod = tok.tt.hdr32.e_mod;
ev->tv.tv_sec = (time_t)tok.tt.hdr32.s;
ev->tv.tv_nsec = (long)tok.tt.hdr32.ms*1000000;
/* size, version */
break;
case AUT_HEADER32_EX:
ev->type = tok.tt.hdr32_ex.e_type;
if (aues && !auevent_type_in_typelist(ev->type, aues))
goto skip_rec;
ev->mod = tok.tt.hdr32_ex.e_mod;
ev->tv.tv_sec = (time_t)tok.tt.hdr32_ex.s;
ev->tv.tv_nsec = (long)tok.tt.hdr32_ex.ms*1000000;
/* size, version */
break;
case AUT_HEADER64:
ev->type = tok.tt.hdr64.e_type;
if (aues && !auevent_type_in_typelist(ev->type, aues))
goto skip_rec;
ev->mod = tok.tt.hdr64.e_mod;
ev->tv.tv_sec = (time_t)tok.tt.hdr64.s;
ev->tv.tv_nsec = (long)tok.tt.hdr64.ms;
/* size, version */
break;
case AUT_HEADER64_EX:
ev->type = tok.tt.hdr64_ex.e_type;
if (aues && !auevent_type_in_typelist(ev->type, aues))
goto skip_rec;
ev->mod = tok.tt.hdr64_ex.e_mod;
ev->tv.tv_sec = (time_t)tok.tt.hdr64_ex.s;
ev->tv.tv_nsec = (long)tok.tt.hdr64_ex.ms;
/* size, version */
break;
case AUT_TRAILER:
/* ignore */
break;
/* subject */
case AUT_SUBJECT32:
assert(!ev->subject_present);
ev->subject_present = true;
ev->subject.auid = tok.tt.subj32.auid;
ev->subject.euid = tok.tt.subj32.euid;
ev->subject.egid = tok.tt.subj32.egid;
ev->subject.ruid = tok.tt.subj32.ruid;
ev->subject.rgid = tok.tt.subj32.rgid;
ev->subject.pid = tok.tt.subj32.pid;
ev->subject.sid = tok.tt.subj32.sid;
SET_DEV(ev->subject.dev, tok.tt.subj32.tid);
SET_ADDR(ev->subject.addr, tok.tt.subj32.tid);
break;
case AUT_SUBJECT32_EX:
assert(!ev->subject_present);
ev->subject_present = true;
ev->subject.auid = tok.tt.subj32_ex.auid;
ev->subject.euid = tok.tt.subj32_ex.euid;
ev->subject.egid = tok.tt.subj32_ex.egid;
ev->subject.ruid = tok.tt.subj32_ex.ruid;
ev->subject.rgid = tok.tt.subj32_ex.rgid;
ev->subject.pid = tok.tt.subj32_ex.pid;
ev->subject.sid = tok.tt.subj32_ex.sid;
SET_DEV(ev->subject.dev, tok.tt.subj32_ex.tid);
SET_ADDR_EX(ev->subject.addr, tok.tt.subj32_ex.tid);
break;
case AUT_SUBJECT64:
assert(!ev->subject_present);
ev->subject_present = true;
ev->subject.auid = tok.tt.subj64.auid;
ev->subject.euid = tok.tt.subj64.euid;
ev->subject.egid = tok.tt.subj64.egid;
ev->subject.ruid = tok.tt.subj64.ruid;
ev->subject.rgid = tok.tt.subj64.rgid;
ev->subject.pid = tok.tt.subj64.pid;
ev->subject.sid = tok.tt.subj64.sid;
SET_DEV(ev->subject.dev, tok.tt.subj64.tid);
SET_ADDR(ev->subject.addr, tok.tt.subj64.tid);
break;
case AUT_SUBJECT64_EX:
assert(!ev->subject_present);
ev->subject_present = true;
ev->subject.auid = tok.tt.subj64_ex.auid;
ev->subject.euid = tok.tt.subj64_ex.euid;
ev->subject.egid = tok.tt.subj64_ex.egid;
ev->subject.ruid = tok.tt.subj64_ex.ruid;
ev->subject.rgid = tok.tt.subj64_ex.rgid;
ev->subject.pid = tok.tt.subj64_ex.pid;
ev->subject.sid = tok.tt.subj64_ex.sid;
SET_DEV(ev->subject.dev, tok.tt.subj64_ex.tid);
SET_ADDR_EX(ev->subject.addr, tok.tt.subj64_ex.tid);
break;
/* process (as object, other than subject) */
case AUT_PROCESS32:
assert(!ev->process_present);
ev->process_present = true;
ev->process.auid = tok.tt.proc32.auid;
ev->process.euid = tok.tt.proc32.euid;
ev->process.egid = tok.tt.proc32.egid;
ev->process.ruid = tok.tt.proc32.ruid;
ev->process.rgid = tok.tt.proc32.rgid;
ev->process.pid = tok.tt.proc32.pid;
ev->process.sid = tok.tt.proc32.sid;
SET_DEV(ev->process.dev, tok.tt.proc32.tid);
SET_ADDR(ev->process.addr, tok.tt.proc32.tid);
break;
case AUT_PROCESS32_EX:
assert(!ev->process_present);
ev->process_present = true;
ev->process.auid = tok.tt.proc32_ex.auid;
ev->process.euid = tok.tt.proc32_ex.euid;
ev->process.egid = tok.tt.proc32_ex.egid;
ev->process.ruid = tok.tt.proc32_ex.ruid;
ev->process.rgid = tok.tt.proc32_ex.rgid;
ev->process.pid = tok.tt.proc32_ex.pid;
ev->process.sid = tok.tt.proc32_ex.sid;
SET_DEV(ev->process.dev, tok.tt.proc32_ex.tid);
SET_ADDR_EX(ev->process.addr, tok.tt.proc32_ex.tid);
break;
case AUT_PROCESS64:
assert(!ev->process_present);
ev->process_present = true;
ev->process.auid = tok.tt.proc64.auid;
ev->process.euid = tok.tt.proc64.euid;
ev->process.egid = tok.tt.proc64.egid;
ev->process.ruid = tok.tt.proc64.ruid;
ev->process.rgid = tok.tt.proc64.rgid;
ev->process.pid = tok.tt.proc64.pid;
ev->process.sid = tok.tt.proc64.sid;
SET_DEV(ev->process.dev, tok.tt.proc64.tid);
SET_ADDR(ev->process.addr, tok.tt.proc64.tid);
break;
case AUT_PROCESS64_EX:
assert(!ev->process_present);
ev->process_present = true;
ev->process.auid = tok.tt.proc64_ex.auid;
ev->process.euid = tok.tt.proc64_ex.euid;
ev->process.egid = tok.tt.proc64_ex.egid;
ev->process.ruid = tok.tt.proc64_ex.ruid;
ev->process.rgid = tok.tt.proc64_ex.rgid;
ev->process.pid = tok.tt.proc64_ex.pid;
ev->process.sid = tok.tt.proc64_ex.sid;
SET_DEV(ev->process.dev, tok.tt.proc64_ex.tid);
SET_ADDR_EX(ev->process.addr, tok.tt.proc64_ex.tid);
break;
/* syscall arguments */
case AUT_ARG32:
/* tok.tt.arg32.no is zero-based */
assert(!ev->args[tok.tt.arg32.no].present);
ev->args[tok.tt.arg32.no].present = true;
ev->args[tok.tt.arg32.no].value = tok.tt.arg32.val;
#ifdef DEBUG_AUDITPIPE
ev->args[tok.tt.arg32.no].text =
strdup(tok.tt.arg32.text);
if (!ev->args[tok.tt.arg32.no].text)
ev->flags |= AEFLAG_ENOMEM;
#endif /* DEBUG_AUDITPIPE */
ev->args_count = max(ev->args_count,
(size_t)tok.tt.arg32.no + 1);
break;
case AUT_ARG64:
/* tok.tt.arg64.no is zero-based */
assert(!ev->args[tok.tt.arg64.no].present);
ev->args[tok.tt.arg64.no].present = true;
ev->args[tok.tt.arg64.no].value = tok.tt.arg64.val;
#ifdef DEBUG_AUDITPIPE
ev->args[tok.tt.arg64.no].text =
strdup(tok.tt.arg64.text);
if (!ev->args[tok.tt.arg64.no].text)
ev->flags |= AEFLAG_ENOMEM;
#endif /* DEBUG_AUDITPIPE */
ev->args_count = max(ev->args_count,
(size_t)tok.tt.arg64.no + 1);
break;
/* syscall return value */
case AUT_RETURN32:
assert(!ev->return_present);
ev->return_present = true;
ev->return_error = tok.tt.ret32.status;
ev->return_value = tok.tt.ret32.ret;
break;
case AUT_RETURN64:
assert(!ev->return_present);
ev->return_present = true;
ev->return_error = tok.tt.ret64.err;
ev->return_value = tok.tt.ret64.val;
break;
/* symlink text */
case AUT_TEXT:
if (!(textc < sizeof(ev->text)/sizeof(ev->text[0]))) {
fprintf(stderr, "Too many text tokens, "
"skipping record\n");
goto skip_rec;
}
ev->text[textc] = tok.tt.text.text;
textc++;
break;
/* path */
case AUT_PATH:
/*
* Historically, on other BSM implementations, records
* for syscalls with a single path argument had only
* had a single path token. However, macOS includes an
* unresolved and a resolved version of each token, as
* confirmed by Apple in radar 39267988 on 2018-06-13.
* Since there are syscalls with two path arguments, we
* store a maximum of four path arguments.
*/
if (!(pathc < sizeof(ev->path)/sizeof(ev->path[0]))) {
fprintf(stderr, "Too many path tokens, "
"skipping record\n");
goto skip_rec;
}
ev->path[pathc] = tok.tt.path.path;
pathc++;
break;
/* attr */
case AUT_ATTR32:
if (!(ev->attr_count <
sizeof(ev->attr)/sizeof(ev->attr[0]))) {
fprintf(stderr, "Too many attr tokens, "
"skipping record\n");
goto skip_rec;
}
ev->attr[ev->attr_count].mode = tok.tt.attr32.mode;
ev->attr[ev->attr_count].uid = tok.tt.attr32.uid;
ev->attr[ev->attr_count].gid = tok.tt.attr32.gid;
ev->attr[ev->attr_count].dev = tok.tt.attr32.fsid;
ev->attr[ev->attr_count].ino = tok.tt.attr32.nid;
#if 0
ev->attr[ev->attr_count].rdev = tok.tt.attr32.dev;
#endif
ev->attr_count++;
break;
case AUT_ATTR64:
if (!(ev->attr_count <
sizeof(ev->attr)/sizeof(ev->attr[0]))) {
fprintf(stderr, "Too many attr tokens, "
"skipping record\n");
goto skip_rec;
}
ev->attr[ev->attr_count].mode = tok.tt.attr64.mode;
ev->attr[ev->attr_count].uid = tok.tt.attr64.uid;
ev->attr[ev->attr_count].gid = tok.tt.attr64.gid;
ev->attr[ev->attr_count].dev = tok.tt.attr64.fsid;
ev->attr[ev->attr_count].ino = tok.tt.attr64.nid;
#if 0
ev->attr[ev->attr_count].rdev = tok.tt.attr64.dev;
#endif
ev->attr_count++;
break;
/* exec argv */
case AUT_EXEC_ARGS:
assert(ev->execarg == NULL);
if (ev->execarg)
free(ev->execarg);
ev->execarg = aev_new(tok.tt.execarg.count,
tok.tt.execarg.text);
if (!ev->execarg)
ev->flags |= AEFLAG_ENOMEM;
break;
/* exec env */
case AUT_EXEC_ENV:
if (!(flags & (AUEVENT_FLAG_ENV_DYLD |
AUEVENT_FLAG_ENV_FULL)))
break;
assert(ev->execenv == NULL);
if (ev->execenv)
free(ev->execenv);
if (flags & AUEVENT_FLAG_ENV_DYLD) {
ev->execenv = aev_new_prefix(
tok.tt.execenv.count,
tok.tt.execenv.text,
"DYLD_");
} else {
assert(flags & AUEVENT_FLAG_ENV_FULL);
ev->execenv = aev_new(tok.tt.execenv.count,
tok.tt.execenv.text);
}
if (!ev->execenv && errno == ENOMEM)
ev->flags |= AEFLAG_ENOMEM;
break;
/* process exit status */
case AUT_EXIT:
assert(!ev->exit_present);
ev->exit_present = true;
ev->exit_status = tok.tt.exit.status;
ev->exit_return = tok.tt.exit.ret;
break;
case AUT_SOCKINET32: /* Darwin */
if (tok.tt.sockinet_ex32.family != BSM_PF_INET)
break;
ev->sockinet_addr.family = AF_INET;
ev->sockinet_addr.ev_addr =
tok.tt.sockinet_ex32.addr[0];
ev->sockinet_port = ntohs(tok.tt.sockinet_ex32.port);
break;
case AUT_SOCKINET128: /* Darwin */
if (tok.tt.sockinet_ex32.family != BSM_PF_INET6)
break;
ev->sockinet_addr.family = AF_INET6;
ev->sockinet_addr.ev6_addr[0] =
tok.tt.sockinet_ex32.addr[0];
ev->sockinet_addr.ev6_addr[1] =
tok.tt.sockinet_ex32.addr[1];
ev->sockinet_addr.ev6_addr[2] =
tok.tt.sockinet_ex32.addr[2];
ev->sockinet_addr.ev6_addr[3] =
tok.tt.sockinet_ex32.addr[3];
/* AUT_SOCKINET128 has ports in host byte order.
* Reported to Apple as radar 43063872 on 2018-08-08.
* Need to differentiate here based on record version
* or macOS version once a fix is out. */
#ifdef RADAR43063872_FIXED
if (radar_43063872_present) {
#endif
ev->sockinet_port = tok.tt.sockinet_ex32.port;
#ifdef RADAR43063872_FIXED
} else {
ev->sockinet_port =
ntohs(tok.tt.sockinet_ex32.port);
}
#endif
break;
case AUT_SOCKUNIX: /* Darwin */
/* ignore for now */
break;
/* unhandled tokens */
default:
for (int i = 0; i < 256; i++) {
if (ev->unk_tokids[i] == tok.id)
break;
if (ev->unk_tokids[i] == 0) {
ev->unk_tokids[i] = tok.id;
break;
}
}
break;
}
#ifdef DEBUG_AUDITPIPE
au_print_flags_tok(stderr, &tok, ":", AU_OFLAG_NONE);
fprintf(stderr, "\n");
#endif
recpos += tok.len;
}
return (ev->flags & AEFLAG_ENOMEM) ? -1 : 1;
skip_rec:
return 0;
}
void
auevent_fprint(FILE *f, audit_event_t *ev) {
struct au_event_ent *aue_ent;
assert(ev);
logutl_fwrite_timespec(f, &ev->tv);
aue_ent = getauevnum(ev->type);
fprintf(f, " %s [%i:%i]", aue_ent->ae_name, ev->type, ev->mod);
if (ev->subject_present) {
fprintf(f,
" subject_pid=%i"
" subject_sid=%"PRIu32
" subject_tid=/dev/%s[%s]"
" subject_auid=%u"
" subject_euid=%u"
" subject_egid=%u"
" subject_ruid=%u"
" subject_rgid=%u",
ev->subject.pid,
ev->subject.sid,
ev->subject.dev == -1 ? "-"
: sys_ttydevname(ev->subject.dev),
ipaddrtoa(&ev->subject.addr, "-"),
ev->subject.auid,
ev->subject.euid,
ev->subject.egid,
ev->subject.ruid,
ev->subject.rgid);
}
if (ev->process_present) {
fprintf(f,
" process_pid=%i"
" process_sid=%"PRIu32
" process_tid=/dev/%s[%s]"
" process_auid=%u"
" process_euid=%u"
" process_egid=%u"
" process_ruid=%u"
" process_rgid=%u",
ev->process.pid,
ev->process.sid,
ev->subject.dev == -1 ? "-"
: sys_ttydevname(ev->process.dev),
ipaddrtoa(&ev->process.addr, "-"),
ev->process.auid,
ev->process.euid,
ev->process.egid,
ev->process.ruid,
ev->process.rgid);
}
for (size_t i = 0; i < ev->args_count; i++) {
if (ev->args[i].present) {
#ifdef DEBUG_AUDITPIPE
fprintf(f, " args[%zu:%s]=%"PRIu64, i,
ev->args[i].text,
ev->args[i].value);
#else
fprintf(f, " args[%zu]=%"PRIu64, i,
ev->args[i].value);
#endif
}
}
if (ev->return_present) {
fprintf(f, " return_error=%u return_value=%"PRIu32,
ev->return_error, ev->return_value);
}
if (ev->exit_present) {
fprintf(f, " exit_status=%"PRIu32" exit_return=%"PRIu32,
ev->exit_status, ev->exit_return);
}
for (size_t i = 0; i < sizeof(ev->text)/sizeof(ev->text[0]); i++) {
if (ev->text[i]) {
fprintf(f, " text[%zu]=%s", i, ev->text[i]);
}
}
for (size_t i = 0; i < sizeof(ev->path)/sizeof(ev->path[0]); i++) {
if (ev->path[i]) {
fprintf(f, " path[%zu]='%s'", i, ev->path[i]);
}
}
for (size_t i = 0; i < ev->attr_count; i++) {
fprintf(f, " attr[%zu] mode=%o uid=%u gid=%u",
i,
ev->attr[i].mode,
ev->attr[i].uid,
ev->attr[i].gid);
}
if (ev->execarg) {
fprintf(f, " execarg");
for (size_t i = 0; ev->execarg[i]; i++) {
fprintf(f, "%s'%s'", i ? " ": "=",
ev->execarg[i]);
}
}
if (ev->execenv) {
fprintf(f, " execenv");
for (size_t i = 0; ev->execenv[i]; i++) {
fprintf(f, "%s'%s'", i ? " ": "=",
ev->execenv[i]);
}
}
if (ev->sockinet_present) {
fprintf(f, " sockinet=%s:%i",
ipaddrtoa(&ev->sockinet_addr, "-"),
ev->sockinet_port);
}
if (ev->unk_tokids[0]) {
fprintf(f, " unk_tokids");
for (int i = 0; i < 256; i++) {
if (ev->unk_tokids[i] == 0)
break;
fprintf(f, "%s0x%02x", i ? "," : "=",
ev->unk_tokids[i]);
}
}
fprintf(f, "\n");
}
void
auevent_create(audit_event_t *ev) {
assert(ev);
bzero(ev, sizeof(audit_event_t));
}
void
auevent_destroy(audit_event_t *ev) {
/* free raw event memory */
if (ev->recbuf) {
free(ev->recbuf);
ev->recbuf = NULL;
}
if (ev->execarg) {
free(ev->execarg);
ev->execarg = NULL;
}
if (ev->execenv) {
free(ev->execenv);
ev->execenv = NULL;
}
#ifdef DEBUG_AUDITPIPE
for (size_t i = 0; i < ev->args_count; i++) {
if (ev->args[i].present && ev->args[i].text) {
free(ev->args[i].text);
ev->args[i].text = NULL;
}
}
#endif /* DEBUG_AUDITPIPE */
}
/*
* BSM uses domain/PF/AF and socket type constants derived from Solaris, which
* unfortunately differ from BSD. Hence the need to map them back into BSD
* constants. Cannot do this automatically because the constants are emitted
* as generic arg tokens.
*/
int
auevent_sock_domain(int bsmdomain) {
switch (bsmdomain) {
case BSM_PF_UNSPEC: /* 0 */
return PF_UNSPEC; /* 0 */
case BSM_PF_LOCAL: /* 1 */
return PF_UNIX; /* 1 */
case BSM_PF_INET: /* 2 */
return PF_INET; /* 2 */
case BSM_PF_ROUTE: /* 24 */
return PF_ROUTE; /* 17 */
case BSM_PF_KEY: /* 27 */
return PF_KEY; /* 29 */
case BSM_PF_INET6: /* 26 */
return PF_INET6; /* 30 */
/* ... */
case BSM_PF_UNKNOWN: /* 700 */
default:
return -1;
}
}
int
auevent_sock_type(int bsmtype) {
switch (bsmtype) {
case BSM_SOCK_DGRAM: /* 1 */
return SOCK_DGRAM; /* 2 */
case BSM_SOCK_STREAM: /* 2 */
return SOCK_STREAM; /* 1 */
case BSM_SOCK_RAW: /* 4 */
return SOCK_RAW; /* 3 */
case BSM_SOCK_RDM: /* 5 */
return SOCK_RDM; /* 4 */
case BSM_SOCK_SEQPACKET: /* 6 */
return SOCK_SEQPACKET; /* 5 */
case BSM_SOCK_UNKNOWN: /* 500 */
default:
return -1;
}
}