-
Notifications
You must be signed in to change notification settings - Fork 30
/
evtloop.c
1661 lines (1559 loc) · 45.6 KB
/
evtloop.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* 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 "evtloop.h"
#include "auclass.h"
#include "auevent.h"
#include "aupolicy.h"
#include "sys.h"
#include "str.h"
#include "time.h"
#include "os.h"
#include "policy.h"
#include "debug.h"
#include "attrib.h"
#include <sys/ptrace.h>
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
static bool running = true; /* shared */
static int kefd = -1; /* shared */
static FILE *auef = NULL;
static pid_t xnumon_pid;
static uint64_t aupclobbers = 0;
static uint64_t aueunknowns = 0;
static uint64_t failedsyscalls = 0;
static uint64_t radar38845422 = 0;
static uint64_t radar38845422_fatal = 0;
static uint64_t radar38845784 = 0; /* never fatal */
static uint64_t radar39267328 = 0;
static uint64_t radar39267328_fatal = 0;
static uint64_t radar39623812 = 0;
static uint64_t radar39623812_fatal = 0;
static uint64_t radar42770257_fatal = 0; /* always fatal */
static uint64_t radar42783724 = 0;
static uint64_t radar42783724_fatal = 0;
static uint64_t radar42784847 = 0;
static uint64_t radar42784847_fatal = 0;
static uint64_t radar42946744_fatal = 0;
static uint64_t radar43151662_fatal = 0; /* always fatal */
static uint64_t missingtoken = 0;
static uint64_t ooms = 0;
static bool kextloop_running = true;
static pthread_t kextloop_thr;
static int
kefd_readable(int fd, UNUSED void *udata) {
const xnumon_msg_t *msg;
struct timespec tm;
msg = kextctl_recv(fd);
if (!msg)
return -1;
tm.tv_sec = msg->time_s;
tm.tv_nsec = msg->time_ns;
procmon_kern_preexec(&tm, (pid_t)msg->pid, msg->path);
if (kextctl_ack(fd, msg) == -1) {
fprintf(stderr, "Failed to acknowledge message from kext\n");
return -1;
}
return 0;
}
static void *
kextloop_thread(void *arg) {
kqueue_t *kq = (kqueue_t *)arg;
#if 0 /* terra pericolosa */
(void)policy_thread_sched_priority(TP_HIGH);
#endif
(void)policy_thread_diskio_important();
/* event dispatch loop */
kextloop_running = true;
for (;;) {
int rv = kqueue_dispatch(kq);
if (!kextloop_running)
break;
if (rv != 0) {
fprintf(stderr, "kevent_dispatch() failed\n");
running = false; /* stop main loop */
break;
}
}
kqueue_free(kq);
close(kefd);
kefd = -1;
return NULL;
}
static void
kextloop_break(void) {
kextloop_running = false;
if (pthread_join(kextloop_thr, NULL) != 0) {
fprintf(stderr, "Failed to join kextloop thread - exiting\n");
exit(EXIT_FAILURE);
}
}
static int
kextloop_spawn(kevent_ctx_t *ctx) {
kqueue_t *kq = NULL;
if ((kefd = kextctl_open()) == -1) {
fprintf(stderr, "kextctl_open() failed: %s (%i)\n",
strerror(errno), errno);
goto errout;
}
/* from here on the kernel blocks execs until we ACK */
kq = kqueue_new();
if (!kq) {
fprintf(stderr, "kqueue_new() failed: %s (%i)\n",
strerror(errno), errno);
goto errout;
}
if (kqueue_add_fd_read(kq, kefd, ctx) == -1) {
fprintf(stderr, "kqueue_add_fd_read(/dev/xnumon) failed: "
"%s (%i)\n", strerror(errno), errno);
goto errout;
}
if (pthread_create(&kextloop_thr, NULL, kextloop_thread, kq) != 0) {
fprintf(stderr, "pthread_create() failed: "
"%s (%i)\n", strerror(errno), errno);
goto errout;
}
return 0;
errout:
if (kq) {
kqueue_free(kq);
}
if (kefd != -1) {
close(kefd);
kefd = -1;
}
return -1;
}
/*
* Construct an absolute path from a possibly relative path and fully resolve
* all symlinks.
*
* If use_cwd is true, use the current working directory for process pid to
* construct the full path of a relative path. If use_cwd is false, fail if
* path is relative.
*
* Caller must free *path, but not *cwd.
*/
static void NONNULL(1,2,3,5)
path_resolve(char **path, const char **cwd, const char *unrpath,
pid_t pid, struct timespec *tv, bool use_cwd) {
if (use_cwd) {
*cwd = procmon_getcwd(pid, tv);
if (!*cwd && (errno == ENOMEM))
ooms++;
} else {
*cwd = NULL;
}
*path = sys_realpath(unrpath, *cwd);
if (!*path && (errno == ENOMEM))
ooms++;
}
/*
* Variant of path_resolve for symlinks. Fully resolves all directory
* components of the resulting path, but keeps the symlink itself unresolved.
*/
static void NONNULL(1,2,3,5)
path_resolve_symlink(char **path, const char **cwd, const char *unrpath,
pid_t pid, struct timespec *tv, bool use_cwd) {
if (use_cwd) {
*cwd = procmon_getcwd(pid, tv);
if (!*cwd && (errno == ENOMEM))
ooms++;
} else {
*cwd = NULL;
}
*path = sys_realdir(unrpath, *cwd);
if (!*path && (errno == ENOMEM))
ooms++;
}
/*
* Called when the auditpipe file descriptor is readable.
*
* XNU BSD syscalls:
* https://github.com/apple/darwin-xnu/blob/master/bsd/kern/syscalls.master
* XNU Mach syscalls:
* https://github.com/apple/darwin-xnu/blob/master/osfmk/mach/syscall_sw.h
*
* Mostly complete workarounds for the following bugs in audit(4):
* 38845422: audit(4): nonsensical path and missing attr for posix_spawn(2)
* 38845784: audit(4): spurious return value for execve(2)
* 39267328: audit(4): target path not resolved for rename(2) [fixed]
* 39623812: audit(4): path not resolved for utimes(2)
* 42783724: audit(4): target path not resolved for link(2) [fixed]
* 43063872: audit(4): port in wrong byte order for ports on IPv6 sockets
*
* Partial workarounds for the following audit(4) bugs:
* 42784847: audit(4): path not resolved for symlink(2) and symlinkat(2)
* 42770257: audit(4): missing destination path tokens for renameat(2)
* 43151662: audit(4): missing destination path tokens for linkat(2)
*
* Only detection, no workaround for the following bugs in audit(4):
* 42946744: audit(4): missing argv and arge for __mac_execve(2)
*
* Neither detection nor workaround for the following bugs in audit(4):
* 40755284: audit(4): processor_set_tasks() cannot be audited
* 43006946: audit(4): no AUE_CONNECT for connect(2) on non-blocking socket
*
* Presence of bugs without workaround can be detected using the test suite.
*/
#define TOKEN_ASSERT(EVENT, TOKEN, COND) \
if (!(COND)) { \
missingtoken++; \
DEBUG(cfg->debug, "missingtoken", \
"event=" EVENT " token=" TOKEN); \
if (cfg->debug) \
auevent_fprint(stderr, &ev); \
break; \
}
static int
auef_readable(UNUSED int fd, void *udata) {
config_t *cfg = (config_t *)udata;
audit_event_t ev;
const char *cwd;
char *path;
const char *cpath;
bool flag;
int rv;
auevent_create(&ev);
rv = auevent_fread(&ev, NULL, cfg->envlevel /* HACK */, auef);
if (rv == -1 || rv == 0) {
if (ev.flags & AEFLAG_ENOMEM)
ooms++;
auevent_destroy(&ev);
return rv;
}
#ifdef DEBUG_AUDITPIPE
auevent_fprint(stderr, &ev);
#endif
/* avoid reacting on our own close invocations */
if (ev.subject.pid == xnumon_pid)
goto out;
switch (ev.type) {
/*
* Events for process monitoring.
*/
case AUE_FORK:
case AUE_VFORK:
TOKEN_ASSERT("fork", "return", ev.return_present);
if (ev.return_value > INT_MAX) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("fork", "subject", ev.subject_present);
procmon_fork(&ev.tv, &ev.subject, ev.return_value);
break;
case AUE_POSIX_SPAWN:
TOKEN_ASSERT("posix_spawn", "return", ev.return_present);
if (ev.return_value) {
/* posix_spawnp spams an event for each directory in
* $PATH with return_value==2 until it finds the actual
* matching executable (10.11.6) */
failedsyscalls++;
break;
}
TOKEN_ASSERT("posix_spawn", "subject", ev.subject_present);
/*
* On at least 10.11.6 and 10.12.6, the following happens:
* path is /dev/console when launchd spawns xpcproxy,
* path is /dev/null when xpcproxy execs the XPC target,
* path is $CWD/dev/ttysNNN when lldb spawns debug subject;
* in all of these cases, no attr token is provided, and there
* is only one path instead of two.
*
* Reported to Apple as radar 38845422 on 2018-03-25.
*
* As a result, whenever no attr is present or path starts in
* /dev, assume a buggy path. First try the path by pid.
* If that fails, employ less reliable ways to work around the
* issue.
*/
path = (char *)(ev.path[1] ? ev.path[1] : ev.path[0]);
if (ev.attr_count == 0 || !path ||
!str_beginswith(path, "/dev/")) {
radar38845422++;
path = sys_pidpath(ev.args[0].present ?
ev.args[0].value : ev.subject.pid);
if (!path) {
if (!ev.execarg) {
radar38845422_fatal++;
DEBUG(cfg->debug,
"radar38845422_fatal",
"path[0]=%s "
"path[1]=%s "
"args[0]=%i "
"pid=%i "
"sys_pidpath(args[0]||pid)=>%s",
ev.path[0],
ev.path[1],
ev.args[0].present
? (int)ev.args[0].value : -1,
ev.subject.pid,
cwd);
break;
}
/* When launchd spawns the xpcproxy exec
* trampoline, path is /dev/console and argv[0]
* is just xpcproxy; hardcode that.
* This allows a malicious binary to be named
* xpcproxy and triggering this kernel bug to
* hide its hash and codesigning status from
* us unless kextlevel >= 1. */
if (!strcmp(ev.execarg[0], "xpcproxy")) {
path = strdup("/usr/libexec/xpcproxy");
} else {
/* As a last resort, use execarg[0] as
* path; note that this is not always
* absolute, can be just the basename;
* also it is forgeable.
* If we are using the kext, the real
* path will be fetched from the kext.
* If not, there will be no way to
* reliably find the corresponding
* image on disk. */
path = strdup(ev.execarg[0]);
}
if (!path)
ooms++;
}
} else {
path = strdup(path);
if (!path)
ooms++;
}
if (!path)
/* got counted above */
break;
if (!ev.args[0].present) {
/* POSIX_SPAWN_SETEXEC */
procmon_exec(&ev.tv,
&ev.subject,
path,
ev.attr_count > 0 ? &ev.attr[0] : NULL,
ev.execarg,
ev.execenv);
ev.execarg = NULL; /* pass ownership to procmon */
ev.execenv = NULL; /* pass ownership to procmon */
break;
}
TOKEN_ASSERT("execve", "args[0]", ev.args[0].present);
procmon_spawn(&ev.tv,
&ev.subject,
ev.args[0].value,
path,
ev.attr_count > 0 ? &ev.attr[0] : NULL,
ev.execarg,
ev.execenv);
ev.execarg = NULL; /* pass ownership to procmon */
ev.execenv = NULL; /* pass ownership to procmon */
break;
case AUE_EXEC:
case AUE_EXECVE:
case AUE_MAC_EXECVE:
TOKEN_ASSERT("execve", "subject", ev.subject_present);
/*
* On at least 10.11.6, audit records for successful execve
* invocations sometimes have a pid as return value, for
* example when being spawned from make, which does not
* indicate failure; only treat negative values as errors.
*
* Reported to Apple as radar 38845784 on 2018-03-25.
*/
if (ev.return_present) {
if (ev.return_value > INT_MAX) {
failedsyscalls++;
break;
} else if (ev.return_value != 0) {
radar38845784++;
}
}
TOKEN_ASSERT("execve", "path", ev.path[0]);
path = (char *)(ev.path[1] ? ev.path[1] : ev.path[0]);
assert(path);
path = strdup(path);
if (!path) {
ooms++;
break;
}
if (ev.type == AUE_MAC_EXECVE && (
!ev.execarg || ((cfg->envlevel > 0) && !ev.execenv))) {
/*
* On at least 10.11.6, audit records for __mac_execve
* are missing their exec arg and exec env tokens.
*
* Reported to Apple as radar 42946744 on 2018-08-05.
*/
radar42946744_fatal++;
DEBUG(cfg->debug,
"radar42946744_fatal",
"path[0]=%s "
"path[1]=%s "
"argv=%i env=%i "
"pid=%i",
ev.path[0],
ev.path[1],
ev.execarg ? 1 : 0,
ev.execenv ? 1 : 0,
ev.subject.pid);
}
procmon_exec(&ev.tv,
&ev.subject,
path,
ev.attr_count > 0 ? &ev.attr[0] : NULL,
ev.execarg,
ev.execenv);
ev.execarg = NULL; /* pass ownership to procmon */
ev.execenv = NULL; /* pass ownership to procmon */
break;
case AUE_EXIT:
TOKEN_ASSERT("exit", "subject", ev.subject_present);
/* exit never fails; audit event not triggered if process got
* terminated in other ways than calling exit() */
procmon_exit(&ev.tv, ev.subject.pid);
break;
case AUE_WAIT4:
TOKEN_ASSERT("wait4", "return", ev.return_present);
if (ev.return_value == 0 || ev.return_value > INT_MAX) {
failedsyscalls++;
break;
}
/* cannot distinguish terminated and stopped processes */
procmon_wait4(&ev.tv, ev.return_value);
break;
case AUE_CHDIR:
case AUE_FCHDIR:
TOKEN_ASSERT("chdir", "return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("chdir", "subject", ev.subject_present);
TOKEN_ASSERT("chdir", "path", ev.path[0]);
path = (char *)(ev.path[1] ? ev.path[1] : ev.path[0]);
assert(path);
path = strdup(path);
if (!path) {
ooms++;
break;
}
procmon_chdir(&ev.tv, ev.subject.pid, path);
break;
/*
* Events for tracking inter-process access commonly used for
* code injection and other manipulation.
*/
case AUE_TASKFORPID:
if (!LOGEVT_WANT(cfg->events, LOGEVT_HACKMON))
break;
TOKEN_ASSERT("task_for_pid", "return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("task_for_pid", "subject", ev.subject_present);
TOKEN_ASSERT("task_for_pid", "process|args[2](pid)",
ev.process_present || ev.args[2].present);
if (ev.process_present) {
hackmon_taskforpid(&ev.tv, &ev.subject,
&ev.process, ev.process.pid);
} else {
hackmon_taskforpid(&ev.tv, &ev.subject,
NULL, ev.args[2].value);
}
break;
case AUE_PTRACE:
if (!LOGEVT_WANT(cfg->events, LOGEVT_HACKMON))
break;
TOKEN_ASSERT("ptrace", "return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("ptrace", "subject", ev.subject_present);
TOKEN_ASSERT("ptrace", "args[1](request)", ev.args[1].present);
if (ev.args[1].value != PT_ATTACHEXC)
break;
TOKEN_ASSERT("ptrace", "process|args[2](pid)",
ev.process_present || ev.args[2].present);
if (ev.process_present) {
hackmon_ptrace(&ev.tv, &ev.subject,
&ev.process, ev.process.pid);
} else {
hackmon_ptrace(&ev.tv, &ev.subject,
NULL, ev.args[2].value);
}
break;
/*
* Events for tracking file modifications.
*/
case AUE_OPEN_W:
case AUE_OPEN_WC:
case AUE_OPEN_WT:
case AUE_OPEN_WTC:
case AUE_OPEN_RW:
case AUE_OPEN_RWC:
case AUE_OPEN_RWT:
case AUE_OPEN_RWTC:
case AUE_OPEN_EXTENDED_W:
case AUE_OPEN_EXTENDED_WC:
case AUE_OPEN_EXTENDED_WT:
case AUE_OPEN_EXTENDED_WTC:
case AUE_OPEN_EXTENDED_RW:
case AUE_OPEN_EXTENDED_RWC:
case AUE_OPEN_EXTENDED_RWT:
case AUE_OPEN_EXTENDED_RWTC:
case AUE_OPENAT_W:
case AUE_OPENAT_WC:
case AUE_OPENAT_WT:
case AUE_OPENAT_WTC:
case AUE_OPENAT_RW:
case AUE_OPENAT_RWC:
case AUE_OPENAT_RWT:
case AUE_OPENAT_RWTC:
case AUE_OPENBYID_W:
case AUE_OPENBYID_WT:
case AUE_OPENBYID_RW:
case AUE_OPENBYID_RWT:
if (!LOGEVT_WANT(cfg->events, LOGEVT_FILEMON))
break;
TOKEN_ASSERT("open(w)", "return", ev.return_present);
if (ev.return_value > INT_MAX) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("open(w)", "subject", ev.subject_present);
#if 0
TOKEN_ASSERT("open(w)", "arg[2](flags)", ev.args[2].present);
TOKEN_ASSERT("open(w)", "arg[3](mode)", ev.args[3].present);
#endif
TOKEN_ASSERT("open(2)", "path[0]", ev.path[0]);
/* sometimes one, sometimes two path tokens, unsure if bug */
path = (char *)(ev.path[1] ? ev.path[1] : ev.path[0]);
assert(path);
procmon_file_open(&ev.subject, ev.return_value, path, &ev.tv);
break;
case AUE_CLOSE:
if (!LOGEVT_WANT(cfg->events, LOGEVT_FILEMON|LOGEVT_SOCKMON))
break;
TOKEN_ASSERT("close", "return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("close", "subject", ev.subject_present);
TOKEN_ASSERT("close", "arg[2](fd)", ev.args[2].present);
procmon_fd_close(ev.subject.pid, ev.args[2].value);
if (!LOGEVT_WANT(cfg->events, LOGEVT_FILEMON))
break;
if (!ev.path[0]) {
/* closed file descriptor does not point to vnode */
break;
}
path = (char *)(ev.path[1] ? ev.path[1] : ev.path[0]);
assert(path);
path = strdup(path);
if (!path) {
ooms++;
break;
}
filemon_touched(&ev.tv, &ev.subject, path);
break;
case AUE_UTIMES:
case AUE_FUTIMES:
if (!LOGEVT_WANT(cfg->events, LOGEVT_FILEMON))
break;
TOKEN_ASSERT("utimes", "return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("utimes", "subject", ev.subject_present);
/*
* On at least 10.11.6, records include only an unresolved
* path.
*
* Reported to Apple as radar 39623812 on 2018-04-21.
*/
if (ev.path[1]) {
/* two path tokens */
path = strdup(ev.path[1]);
if (!path)
ooms++;
} else if (ev.path[0]) {
/* one path token, assume unresolved if no attr */
if (ev.attr_count > 0) {
path = strdup(ev.path[0]);
if (!path)
ooms++;
} else {
radar39623812++;
path_resolve(&path, &cwd, ev.path[0],
ev.subject.pid, &ev.tv, true);
if (!path && (errno != ENOMEM)) {
radar39623812_fatal++;
DEBUG(cfg->debug, "radar39623812_fatal",
"path[0]=%s pid=%i "
"cwd(pid)=>%s",
ev.path[0], ev.subject.pid, cwd);
}
}
} else {
path = NULL;
missingtoken++;
DEBUG(cfg->debug, "missingtoken",
"event=utimes token=path");
if (cfg->debug)
auevent_fprint(stderr, &ev);
}
if (!path)
/* counted above */
break;
filemon_touched(&ev.tv, &ev.subject, path);
break;
case AUE_RENAMEAT:
case AUE_LINKAT:
case AUE_CLONEFILEAT:
case AUE_FCLONEFILEAT:
flag = false; /* dont resolve relative to cwd on path bugs */
goto rename_et_al;
case AUE_RENAME:
case AUE_LINK:
case AUE_COPYFILE: /* copyfile(2) not copyfile(3) */
flag = true; /* resolve relative to cwd on path bugs */
rename_et_al:
if (!LOGEVT_WANT(cfg->events, LOGEVT_FILEMON))
break;
TOKEN_ASSERT("rename|link|clonefile|copyfile",
"return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("rename|link|clonefile|copyfile",
"subject", ev.subject_present);
/*
* Before 10.14.3/2019-001, AUE_RENAME and AUE_LINK records
* include only an unresolved target path.
*
* Reported to Apple as radar 39267328 on 2018-04-08 and
* radar 42783724 on 2018-07-31 respectively. Fix published by
* Apple on 2019-01-22.
*/
if (ev.path[3]) {
/* four path tokens, as expected */
path = strdup(ev.path[3]);
if (!path)
ooms++;
} else if (ev.path[2] && !ev.path[3]) {
/* three path tokens, assume third unresolved dpath */
switch (ev.type) {
case AUE_RENAME:
radar39267328++;
break;
case AUE_LINK:
radar42783724++;
break;
default:
missingtoken++;
DEBUG(cfg->debug, "missingtoken",
"event=rename|link|clonefile|copyfile "
"token=path");
if (cfg->debug)
auevent_fprint(stderr, &ev);
break;
}
path_resolve(&path, &cwd, ev.path[2],
ev.subject.pid, &ev.tv, flag);
if (!path && (errno != ENOMEM)) {
switch (ev.type) {
case AUE_RENAME:
radar39267328_fatal++;
DEBUG(cfg->debug,
"radar39267328_fatal",
"path[2]=%s pid=%i "
"getcwd(pid)=>%s",
ev.path[2], ev.subject.pid, cwd);
break;
case AUE_LINK:
radar42783724_fatal++;
DEBUG(cfg->debug,
"radar42783724_fatal",
"path[2]=%s pid=%i "
"getcwd(pid)=>%s",
ev.path[2], ev.subject.pid, cwd);
break;
}
/* others already got a "missingtoken" above */
}
} else {
/* less than three path tokens */
path = NULL;
/*
* AUE_RENAMEAT and AUE_LINKAT records sometimes have
* only one or two path tokens instead of four.
*
* Reported to Apple as radar 42770257 and 43151662 on
* 2018-07-31 and 2018-08-10 respectively.
*/
switch (ev.type) {
case AUE_RENAMEAT:
radar42770257_fatal++;
DEBUG(cfg->debug, "radar42770257_fatal",
"event=renameat token=path");
break;
case AUE_LINKAT:
radar43151662_fatal++;
DEBUG(cfg->debug, "radar43151662_fatal",
"event=linkat token=path");
break;
default:
missingtoken++;
DEBUG(cfg->debug, "missingtoken",
"event=rename|link|clonefile|copyfile "
"token=path");
if (cfg->debug)
auevent_fprint(stderr, &ev);
break;
}
}
if (!path)
/* counted above */
break;
filemon_touched(&ev.tv, &ev.subject, path);
break;
case AUE_SYMLINK:
case AUE_SYMLINKAT:
if (!LOGEVT_WANT(cfg->events, LOGEVT_FILEMON))
break;
TOKEN_ASSERT("symlink", "return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("symlink", "subject", ev.subject_present);
/*
* On at least 10.11.6, AUE_SYMLINK and AUE_SYMLINKAT records
* include only an unresolved target path.
*
* Reported to Apple as radar 42784847 on 2018-07-31.
*/
if (ev.path[1]) {
path = strdup(ev.path[1]);
if (!path)
ooms++;
} else if (ev.path[0] && !ev.path[1]) {
/* only an unresolved target path token */
radar42784847++;
path_resolve_symlink(&path, &cwd, ev.path[0],
ev.subject.pid, &ev.tv,
(ev.type == AUE_SYMLINK));
if (!path && (errno != ENOMEM)) {
radar42784847_fatal++;
DEBUG(cfg->debug,
"radar42784847_fatal",
"path[0]=%s pid=%i "
"getcwd(pid)=>%s",
ev.path[0], ev.subject.pid, cwd);
}
} else {
path = NULL;
missingtoken++;
DEBUG(cfg->debug, "missingtoken",
"event=symlink token=path");
if (cfg->debug)
auevent_fprint(stderr, &ev);
}
if (!path)
/* counted above */
break;
filemon_symlink(&ev.tv, &ev.subject, path);
break;
case AUE_UNLINK:
case AUE_UNLINKAT:
if (!LOGEVT_WANT(cfg->events, LOGEVT_FILEMON))
break;
TOKEN_ASSERT("unlink", "return", ev.return_present);
if (ev.return_value) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("unlink", "subject", ev.subject_present);
if (ev.path[1]) {
/* two path tokens */
cpath = ev.path[1];
#if 0
} else if (ev.path[0]) {
/* one path token, assume unresolved if no attr */
if (ev.attr_count > 0) {
path = strdup(ev.path[0]);
if (!path)
ooms++;
} else {
radarXXX++;
path_resolve_symlink(&path, &cwd, ev.path[0],
ev.subject.pid, &ev.tv,
(ev.type == AUE_UNLINK));
if (!path && (errno != ENOMEM)) {
radarXXX_fatal++;
DEBUG(cfg->debug, "radarXXX_fatal",
"path[0]=%s pid=%i "
"cwd(pid)=>%s",
ev.path[0], ev.subject.pid, cwd);
}
}
// XXX free path after call to unlink!
#endif
} else {
cpath = NULL;
missingtoken++;
DEBUG(cfg->debug, "missingtoken",
"event=unlink token=path");
if (cfg->debug)
auevent_fprint(stderr, &ev);
}
if (!cpath)
/* counted above */
break;
filemon_unlink(cpath, ev.attr_count > 0 ? &ev.attr[0] : NULL);
break;
/*
* Events for socket tracking.
*/
case AUE_SOCKET:
if (!LOGEVT_WANT(cfg->events, LOGEVT_SOCKMON))
break;
TOKEN_ASSERT("socket", "return", ev.return_present);
if (ev.return_value > INT_MAX) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("socket", "subject", ev.subject_present);
TOKEN_ASSERT("socket", "arg[1](domain)", ev.args[1].present);
TOKEN_ASSERT("socket", "arg[2](type)", ev.args[2].present);
TOKEN_ASSERT("socket", "arg[3](protocol)", ev.args[3].present);
sockmon_socket(&ev.tv, &ev.subject, ev.return_value,
auevent_sock_domain(ev.args[1].value),
auevent_sock_type(ev.args[2].value),
ev.args[3].value);
break;
case AUE_BIND:
if (!LOGEVT_WANT(cfg->events, LOGEVT_SOCKMON))
break;
TOKEN_ASSERT("bind", "return", ev.return_present);
if (ev.return_value != 0) {
failedsyscalls++;
break;
}
if (!ev.sockinet_present)
/* skip unix socket */
break;
TOKEN_ASSERT("bind", "subject", ev.subject_present);
TOKEN_ASSERT("bind", "arg[1](fd)", ev.args[1].present);
sockmon_bind(&ev.tv, &ev.subject, ev.args[1].value,
&ev.sockinet_addr, ev.sockinet_port);
break;
case AUE_LISTEN:
if (!LOGEVT_WANT(cfg->events,
LOGEVT_FLAG(LOGEVT_SOCKET_LISTEN)))
break;
TOKEN_ASSERT("listen", "return", ev.return_present);
if (ev.return_value != 0) {
failedsyscalls++;
break;
}
TOKEN_ASSERT("listen", "subject", ev.subject_present);
TOKEN_ASSERT("listen", "arg[1](fd)", ev.args[1].present);
sockmon_listen(&ev.tv, &ev.subject, ev.args[1].value);
break;
case AUE_ACCEPT:
if (!LOGEVT_WANT(cfg->events,
LOGEVT_FLAG(LOGEVT_SOCKET_ACCEPT)))
break;
TOKEN_ASSERT("accept", "return", ev.return_present);
if (ev.return_value > INT_MAX) {
failedsyscalls++;
break;
}
if (!ev.sockinet_present)
/* skip unix socket */
break;
TOKEN_ASSERT("accept", "subject", ev.subject_present);
TOKEN_ASSERT("accept", "arg[1](fd)", ev.args[1].present);
sockmon_accept(&ev.tv, &ev.subject, ev.args[1].value,
&ev.sockinet_addr, ev.sockinet_port);
break;
case AUE_CONNECT:
if (!LOGEVT_WANT(cfg->events,
LOGEVT_FLAG(LOGEVT_SOCKET_CONNECT)))
break;
/* While it would be interesting to see failed connects,
* XNU does not seem to provide audit(4) records for them. */
TOKEN_ASSERT("connect", "return", ev.return_present);
if (ev.return_value != 0) {
failedsyscalls++;
break;
}
if (!ev.sockinet_present)
/* unix socket */
break;
TOKEN_ASSERT("connect", "subject", ev.subject_present);
TOKEN_ASSERT("connect", "arg[1](fd)", ev.args[1].present);
sockmon_connect(&ev.tv, &ev.subject, ev.args[1].value,
&ev.sockinet_addr, ev.sockinet_port);
break;
/*
* Unhandled events.
*/
default:
/*
* Some event types seem to be logged regardless of the class
* mask settings. However, their volume seems to be low, so
* far only userspace events have been seen (10.11.6), so we
* just ignore them here instead of filtering on each event.
*/
#ifdef DEBUG_AUDITPIPE
fprintf(stderr, "Unhandled event type=%u\n", ev.type);
#endif
aueunknowns++;
break;
}
out:
auevent_destroy(&ev); /* free all allocated members not NULLed above */
return 0;
}
#undef TOKEN_ASSERT
/*
* Handles SIGTERM, SIGQUIT and SIGINT.
*/
static int
sigquit_arrived(UNUSED int sig, UNUSED void *udata) {
running = false;
fprintf(stderr, "Shutting down, draining queues...\n");
return -1; /* stop processing more events */
}