-
Notifications
You must be signed in to change notification settings - Fork 30
/
procmon.c
1441 lines (1324 loc) · 35.9 KB
/
procmon.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.
*/
/*
* Process monitoring core.
*
* Plenty of refactoring opportunities here.
*/
#include "procmon.h"
#include "tommylist.h"
#include "proc.h"
#include "hashes.h"
#include "cachehash.h"
#include "cachecsig.h"
#include "time.h"
#include "work.h"
#include "filemon.h"
#include "atomic.h"
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
static config_t *config;
/* prepq state */
static tommy_list pqlist;
pthread_mutex_t pqmutex; /* protects pqlist */
static uint64_t pqsize; /* current number of elements in pqlist */
static uint64_t pqlookup; /* counts total number of lookups in pq */
static uint64_t pqmiss; /* counts no preloaded image found in pq */
static uint64_t pqdrop; /* counts preloaded imgs removed due max TTL */
static uint64_t pqskip; /* counts non-matching entries skipped in pq */
static atomic32_t images;
static uint64_t liveacq; /* counts live process acquisitions */
static uint64_t miss_bypid; /* counts various miss conditions */
static uint64_t miss_forksubj;
static uint64_t miss_execsubj;
static uint64_t miss_execinterp;
static uint64_t miss_chdirsubj;
static uint64_t miss_getcwd;
static atomic64_t ooms; /* counts events impaired due to OOM */
setstr_t *suppress_image_exec_by_ident;
setstr_t *suppress_image_exec_by_path;
setstr_t *suppress_image_exec_by_ancestor_ident;
setstr_t *suppress_image_exec_by_ancestor_path;
static int image_exec_work(image_exec_t *);
/*
* Ownership of path will be transfered to image_exec; caller must not assume
* that path still exists after calling this function. Path is also freed when
* this function fails and returns NULL.
*
* Thread-safe.
*/
static image_exec_t *
image_exec_new(char *path) {
image_exec_t *image;
assert(path);
image = malloc(sizeof(image_exec_t));
if (!image) {
free(path);
atomic64_inc(&ooms);
return NULL;
}
bzero(image, sizeof(image_exec_t));
pthread_mutex_init(&image->refsmutex, NULL);
image->refs = 1;
#ifdef DEBUG_REFS
fprintf(stderr, "DEBUG_REFS: image_exec_new(%p) refs=%zu\n",
image, image->refs);
#endif
image->path = path;
image->fd = -1;
image->hdr.code = LOGEVT_IMAGE_EXEC;
image->hdr.le_work = (__typeof__(image->hdr.le_work))image_exec_work;
image->hdr.le_free = (__typeof__(image->hdr.le_free))image_exec_free;
atomic32_inc(&images);
return image;
}
/*
* Must not use config because config will be set to NULL before the last
* instances of image_exec are drained out of the log queue.
*/
void
image_exec_free(image_exec_t *image) {
assert(image);
pthread_mutex_lock(&image->refsmutex);
image->refs--;
#ifdef DEBUG_REFS
fprintf(stderr, "DEBUG_REFS: image_exec_free(%p) refs=%zu (was %zu)\n",
image, image->refs, image->refs + 1);
#endif
if (image->refs > 0) {
pthread_mutex_unlock(&image->refsmutex);
return;
}
pthread_mutex_unlock(&image->refsmutex);
pthread_mutex_destroy(&image->refsmutex);
if (image->script)
image_exec_free(image->script);
if (image->prev)
image_exec_free(image->prev);
if (image->argv)
free(image->argv);
if (image->envv)
free(image->envv);
if (image->path)
free(image->path);
if (image->cwd)
free(image->cwd);
if (image->codesign)
codesign_free(image->codesign);
atomic32_dec(&images);
free(image);
}
static void
image_exec_ref(image_exec_t *image) {
assert(image);
pthread_mutex_lock(&image->refsmutex);
image->refs++;
#ifdef DEBUG_REFS
fprintf(stderr, "DEBUG_REFS: image_exec_ref(%p) refs=%zu (was %zu)\n",
image, image->refs, image->refs - 1);
#endif
pthread_mutex_unlock(&image->refsmutex);
}
/*
* Prune history of exec images to config->ancestors previous levels. Go
* back in history and free previous image iff on the whole path, all images
* were exclusively owned by this one, i.e. had a refcount of 1, in order to
* make sure that we are not cutting short another fork's exec history.
*/
static void
image_exec_prune_ancestors(image_exec_t *image, size_t level) {
assert(image);
#ifdef DEBUG_REFS
fprintf(stderr, "DEBUG_REFS: image_exec_prune_ancestors(%p, level %zu) "
"refs=%zu\n", image, level, image->refs);
#endif
if (!image->prev)
return;
if (level >= config->ancestors) {
image_exec_free(image->prev);
image->prev = NULL;
return;
}
if (image->refs == 1)
image_exec_prune_ancestors(image->prev, level + 1);
}
/*
* This function is called multiple times on the same image_exec_t if
* kextlevel > 0.
* Partially thread-safe: only a single thread may call functions on a given
* image_exec_t instance at a time.
* Kern indicates if we are currently handling a kernel module callback.
* Note that attr may be NULL depending on where this function is called from.
*/
static int
image_exec_open(image_exec_t *image, const audit_attr_t *attr, bool kern)
{
char buf[2];
int rv;
int oflag;
if (image->flags & (EIFLAG_STAT|EIFLAG_ATTR)) {
#ifdef DEBUG_EXECIMAGE
fprintf(stderr, "DEBUG_EXECIMAGE: already have stat\n");
#endif
/*
* Switch the file descritor to blocking if we are called on
* an instance that was opened in kext prep queue mode. This
* way we do not have to change all the code to retry all file
* operations when outside of prep queue mode.
*/
if (image->fd != -1 && !kern &&
sys_fd_setblocking(image->fd) == -1)
return -1;
return 0;
}
if (image->flags & EIFLAG_NOPATH) {
if (attr)
goto fallback;
return -1;
}
assert(!!strncmp(image->path, "/dev/", 5));
/*
* Open the image file non-blocking if the kext is waiting for us.
* This is to avoid blocking processes in the kext while a long-running
* low-level disk operation is in progress, such as while Boot Camp
* Assistant is editing partitions or Disk Utility is running fsck.
*/
oflag = O_RDONLY;
if (kern)
oflag |= O_NONBLOCK;
image->fd = open(image->path, oflag);
if (image->fd == -1) {
if (attr)
goto fallback;
return -1;
}
rv = sys_fdattr(&image->stat, image->fd);
if (rv == -1) {
if (attr)
goto fallback;
return -1;
}
if (attr && ((image->stat.mode != attr->mode) ||
(image->stat.uid != attr->uid) ||
(image->stat.gid != attr->gid) ||
(image->stat.dev != attr->dev) ||
(image->stat.ino != attr->ino)))
goto fallback;
/* https://www.in-ulm.de/~mascheck/various/shebang/ */
rv = pread(image->fd, buf, sizeof(buf), 0);
if (rv == -1) {
close(image->fd);
image->fd = -1;
if (attr)
goto fallback;
return -1;
} else if ((rv == 2) && (buf[0] == '#' && buf[1] == '!'))
image->flags |= EIFLAG_SHEBANG;
image->flags |= EIFLAG_STAT;
#ifdef DEBUG_EXECIMAGE
fprintf(stderr, "DEBUG_EXECIMAGE: stat from path=%s\n", image->path);
#endif
return 0;
fallback:
image->stat.mode = attr->mode;
image->stat.uid = attr->uid;
image->stat.gid = attr->gid;
image->stat.dev = attr->dev;
image->stat.ino = attr->ino;
image->flags |= EIFLAG_ATTR;
return 0;
}
/*
* Partially thread-safe: only a single thread may call functions on a given
* image_exec_t instance at a time.
*/
static void
image_exec_close(image_exec_t *image) {
assert(image);
if (image->fd != -1) {
close(image->fd);
image->fd = -1;
}
}
/*
* Kern indicates if we are currently handling a kernel module callback.
*
* Partially thread-safe: only a single thread may call functions on a given
* image_exec_t instance at a time.
*/
static int
image_exec_acquire(image_exec_t *image, bool kern) {
stat_attr_t st;
off_t sz;
bool hit;
int rv;
assert(image);
if (image->flags & EIFLAG_DONE)
return 0;
/* postpone hashes for later offline processing */
if (kern && config->kextlevel < KEXTLEVEL_HASH)
return 0;
/* postpone large binaries for later offline processing */
if (kern && image->stat.size > 1024*1024*8)
return 0;
if (!(image->flags & EIFLAG_HASHES)) {
if (!(image->flags & EIFLAG_STAT) || image->fd == -1) {
image->flags |= EIFLAG_DONE;
return -1;
}
hit = cachehash_get(&image->hashes,
image->stat.dev,
image->stat.ino,
&image->stat.mtime,
&image->stat.ctime,
&image->stat.btime);
if (!hit) {
/* cache miss, calculate hashes */
rv = hashes_fd(&sz, &image->hashes, config->hflags,
image->fd);
if ((rv == -1) || (sz != image->stat.size)) {
close(image->fd);
image->fd = -1;
image->flags |= EIFLAG_DONE;
return -1;
}
/*
* If 2nd stat does not match 1st, invalidate hashes.
*/
rv = sys_fdattr(&st, image->fd);
if (rv == -1) {
close(image->fd);
image->fd = -1;
image->flags |= EIFLAG_DONE;
return -1;
}
/* fd still open, no need to compare dev and ino */
if ((image->stat.size != st.size) ||
(image->stat.mtime.tv_sec != st.mtime.tv_sec) ||
(image->stat.mtime.tv_nsec != st.mtime.tv_nsec) ||
(image->stat.ctime.tv_sec != st.ctime.tv_sec) ||
(image->stat.ctime.tv_nsec != st.ctime.tv_nsec) ||
(image->stat.btime.tv_sec != st.btime.tv_sec) ||
(image->stat.btime.tv_nsec != st.btime.tv_nsec)) {
image->flags &= ~EIFLAG_HASHES;
close(image->fd);
image->fd = -1;
image->flags |= EIFLAG_DONE;
return -1;
}
cachehash_put(image->stat.dev,
image->stat.ino,
&image->stat.mtime,
&image->stat.ctime,
&image->stat.btime,
&image->hashes);
#ifdef DEBUG_EXECIMAGE
fprintf(stderr, "DEBUG_EXECIMAGE: hashes from path=%s\n", image->path);
#endif
}
#ifdef DEBUG_EXECIMAGE
else
fprintf(stderr, "DEBUG_EXECIMAGE: hashes from cache\n");
#endif
image->flags |= EIFLAG_HASHES;
}
#ifdef DEBUG_EXECIMAGE
else
fprintf(stderr, "DEBUG_EXECIMAGE: already have hashes\n");
#endif
/* everything below operates on paths, not open file descriptors */
if (image->fd != -1) {
close(image->fd);
image->fd = -1;
}
/* postpone codesign for later offline processing? */
if (kern && config->kextlevel < KEXTLEVEL_CSIG) {
return 0;
}
/* skip code signing for scripts */
if (image->flags & EIFLAG_SHEBANG) {
image->flags |= EIFLAG_DONE;
return 0;
}
#ifdef DEBUG_EXECIMAGE
if (image->codesign)
fprintf(stderr, "DEBUG_EXECIMAGE: already have codesign\n");
#endif
if (!image->codesign && (image->flags & EIFLAG_HASHES)) {
image->codesign = cachecsig_get(&image->hashes);
if (!image->codesign) {
if (errno == ENOMEM) {
image->flags |= EIFLAG_ENOMEM;
image->flags |= EIFLAG_DONE;
return -1;
}
}
#ifdef DEBUG_EXECIMAGE
else
fprintf(stderr, "DEBUG_EXECIMAGE: codesign from cache\n");
#endif
}
if (!image->codesign && config->codesign) {
/* Postpone codesign verification of processes spawned as part
* of codesign verification during KAuth handling. */
if (kern && (!strcmp(image->path, "/usr/libexec/xpcproxy") ||
!strcmp(image->path, "/usr/sbin/ocspd")))
return 0;
/* Check code signature (can be very slow!) */
image->codesign = codesign_new(image->path, -1);
if (!image->codesign) {
if (errno == ENOMEM)
image->flags |= EIFLAG_ENOMEM;
image->flags |= EIFLAG_DONE;
return -1;
}
/*
* If 3rd stat does not match 1st, invalidate codesign.
* If 3rd stat fails, return error but don't invalidate.
* The codesign routines fail internally if the data is changed
* during signature verification.
*/
rv = sys_pathattr(&st, image->path);
if (rv == -1) {
image->flags |= EIFLAG_DONE;
return -1;
}
if ((image->stat.size != st.size) ||
(image->stat.dev != st.dev) ||
(image->stat.ino != st.ino) ||
(image->stat.mtime.tv_sec != st.mtime.tv_sec) ||
(image->stat.mtime.tv_nsec != st.mtime.tv_nsec) ||
(image->stat.ctime.tv_sec != st.ctime.tv_sec) ||
(image->stat.ctime.tv_nsec != st.ctime.tv_nsec) ||
(image->stat.btime.tv_sec != st.btime.tv_sec) ||
(image->stat.btime.tv_nsec != st.btime.tv_nsec)) {
codesign_free(image->codesign);
image->codesign = NULL;
image->flags |= EIFLAG_DONE;
return -1;
}
cachecsig_put(&image->hashes, image->codesign);
#ifdef DEBUG_EXECIMAGE
fprintf(stderr, "DEBUG_EXECIMAGE: codesign from path=%s\n",
image->path);
#endif
}
image->flags |= EIFLAG_DONE;
return 0;
}
/*
* Return true iff exec image matches either one of the idents in by_ident or
* one of the paths in by_path.
*/
bool
image_exec_match_suppressions(image_exec_t *ie,
setstr_t *by_ident, setstr_t *by_path) {
if (ie->codesign && codesign_is_good(ie->codesign)) {
if (setstr_contains3(by_ident, ie->codesign->ident,
ie->codesign->teamid))
return true;
}
if (ie->path) {
if (setstr_contains(by_path, ie->path))
return true;
}
if (ie->script && ie->script->path) {
if (setstr_contains(by_path, ie->script->path))
return true;
}
return false;
}
/*
* Work function to be executed in the worker thread.
*
* Returning 0 leads to the event being logged, -1 indicates that this event
* should not be logged (may or may not be due to an error).
*
* Partially thread-safe: only a single thread may call functions on a given
* image_exec_t instance at a time.
*/
static int
image_exec_work(image_exec_t *ei) {
#ifdef DEBUG_REFS
fprintf(stderr, "DEBUG_REFS: image_exec_work(%p)\n", ei);
#endif
image_exec_acquire(ei, false);
image_exec_close(ei);
if (ei->script) {
image_exec_acquire(ei->script, false);
image_exec_close(ei->script);
}
if (config->ancestors < SIZE_MAX)
image_exec_prune_ancestors(ei, false);
if (ei->flags & EIFLAG_ENOMEM) {
atomic64_inc(&ooms);
return -1;
}
if (ei->flags & EIFLAG_NOLOG)
return -1;
if (image_exec_match_suppressions(ei, suppress_image_exec_by_ident,
suppress_image_exec_by_path))
return -1;
return 0;
}
/*
* Create new image_exec from pid using runtime lookups.
*/
static image_exec_t *
image_exec_from_pid(pid_t pid) {
image_exec_t *ei;
char *path;
int nopath = 0;
int rv;
path = sys_pidpath(pid);
#ifdef DEBUG_PROCMON
DEBUG(config->debug, "image_exec_from_pid",
"pid=%i path=%s", pid, path);
#endif
if (!path) {
if (errno == ENOMEM) {
atomic64_inc(&ooms);
return NULL;
}
if (asprintf(&path, "<%i>", pid) == -1) {
atomic64_inc(&ooms);
free(path);
return NULL;
}
nopath = 1;
}
ei = image_exec_new(path);
if (!ei)
return NULL;
rv = timespec_nanotime(&ei->hdr.tv);
if (rv == -1) {
image_exec_free(ei);
return NULL;
}
if (nopath)
ei->flags |= EIFLAG_NOPATH;
ei->flags |= EIFLAG_PIDLOOKUP;
ei->pid = pid;
return ei;
}
/*
* Create new proc from pid using runtime lookups. Called after looking up a
* subject in proctab fails and for examination of processes which executed
* before xnumon.
*
* Returns NULL on oom or if the process is not running anymore.
*
* Does oom counting, caller does not need to.
* However, caller needs to count and report miss if this fails.
*/
static proc_t *
procmon_proc_from_pid(pid_t pid, bool log_event, struct timespec *tv) {
proc_t *proc;
pid_t ppid;
proc = proctab_find_or_create(pid);
if (!proc) {
atomic64_inc(&ooms);
return NULL;
}
if (sys_pidbsdinfo(&proc->fork_tv, &ppid, pid) == -1) {
/* process not alive anymore */
proctab_remove(pid, tv);
return NULL;
}
if (proc->cwd) {
free(proc->cwd);
}
proc->cwd = sys_pidcwd(pid);
if (!proc->cwd) {
if (errno == ENOMEM)
atomic64_inc(&ooms);
/* process not alive anymore unless ENOMEM */
proctab_remove(pid, tv);
return NULL;
}
if (proc->image_exec) {
image_exec_free(proc->image_exec);
}
proc->image_exec = image_exec_from_pid(pid);
if (!proc->image_exec) {
/* process not alive anymore unless ENOMEM */
proctab_remove(pid, tv);
return NULL;
}
image_exec_open(proc->image_exec, NULL, false);
/* after acquiring all info from process, go after parent before
* submitting the child into the queues */
if ((ppid >= 0) && (ppid != pid)) {
proc_t *pproc = proctab_find(ppid);
if (!pproc) {
pproc = procmon_proc_from_pid(ppid, log_event, tv);
if (!pproc) {
if (errno == ENOMEM) {
proctab_remove(pid, tv);
return NULL;
}
/* parent not alive anymore */
ppid = -1;
}
}
if (pproc) {
proc->image_exec->prev = pproc->image_exec;
if (proc->image_exec->prev) {
image_exec_ref(proc->image_exec->prev);
}
}
}
if (!log_event || pid == 0)
proc->image_exec->flags |= EIFLAG_NOLOG;
#ifdef DEBUG_REFS
fprintf(stderr, "DEBUG_REFS: work_submit(%p)\n",
proc->image_exec);
#endif
image_exec_ref(proc->image_exec); /* ref is owned by proc */
work_submit(proc->image_exec);
return proc;
}
/*
* Retrieve the current executable image for a given pid.
* Intended to be called from other subsystems when logging process context
* for an event related to a pid.
* Caller must free the returned image_exec_t with image_exec_free().
* On error returns NULL.
* Not thread-safe - must be called from the main thread, not worker or logger!
*
* Caller does error counting and reporting.
*/
image_exec_t *
image_exec_by_pid(pid_t pid, struct timespec *tv) {
proc_t *proc;
proc = proctab_find(pid);
if (!proc) {
proc = procmon_proc_from_pid(pid, true, tv);
if (!proc) {
if (errno != ENOMEM) {
miss_bypid++;
DEBUG(config->debug, "miss_bypid",
"pid=%i", pid);
}
return NULL;
}
liveacq++;
}
image_exec_ref(proc->image_exec);
return proc->image_exec;
}
/*
* Handles fork.
*/
void
procmon_fork(struct timespec *tv,
audit_proc_t *subject, pid_t childpid) {
proc_t *parent, *child;
#ifdef DEBUG_PROCMON
DEBUG(config->debug, "procmon_fork",
"subject->pid=%i childpid=%i\n",
subject->pid, childpid);
#endif
parent = proctab_find(subject->pid);
if (!parent) {
parent = procmon_proc_from_pid(subject->pid, true, tv);
if (!parent) {
if (errno != ENOMEM) {
miss_forksubj++;
DEBUG(config->debug, "miss_forksubj",
"subject.pid=%i childpid=%i",
subject->pid, childpid);
}
return;
}
liveacq++;
}
assert(parent);
proctab_remove(childpid, tv);
child = proctab_create(childpid);
if (!child) {
atomic64_inc(&ooms);
return;
}
child->fork_tv = *tv;
assert(parent->cwd);
child->cwd = strdup(parent->cwd);
if (!child->cwd) {
proctab_remove(childpid, tv);
atomic64_inc(&ooms);
return;
}
assert(parent->image_exec);
child->image_exec = parent->image_exec;
image_exec_ref(child->image_exec);
}
/*
* Only handles true posix_spawn without the POSIX_SPAWN_SETEXEC attribute set.
* POSIX_SPAWN_SETEXEC is treated as regular exec.
*
* Ownership of argv and imagepath is transfered; procmon guarantees that they
* will be freed.
*/
void
procmon_spawn(struct timespec *tv,
audit_proc_t *subject,
pid_t childpid,
char *imagepath, audit_attr_t *attr,
char **argv, char **envv) {
#ifdef DEBUG_PROCMON
DEBUG(config->debug, "procmon_spawn",
"subject->pid=%i childpid=%i imagepath=%s",
subject->pid, childpid, imagepath);
#endif
procmon_fork(tv, subject, childpid);
subject->pid = childpid;
procmon_exec(tv, subject, imagepath, attr, argv, envv);
}
/*
* Append an element to the prepq.
* Called from the kext event handler, if kextlevel is > 0.
*/
static void
prepq_append(image_exec_t *ei) {
pthread_mutex_lock(&pqmutex);
tommy_list_insert_tail(&pqlist, &ei->hdr.node, ei);
pqsize++;
pthread_mutex_unlock(&pqmutex);
}
/*
* Remove an existing (!) element from the prepq.
* Called from the exec handler only (!).
*
* The chosen locking strategy only works if only a single thread is removing
* elements and one or more other threads are only adding elements.
*/
static void
prepq_remove_existing(image_exec_t *ei) {
pthread_mutex_lock(&pqmutex);
tommy_list_remove_existing(&pqlist,
&ei->hdr.node);
pqsize--;
pthread_mutex_unlock(&pqmutex);
}
/*
* Look up the corresponding exec images acquired by kext events before the
* audit event was committed. Linking the audit event to the correct kext
* events even when events are being lost for some reason is probably the most
* tricky part of all of this.
*/
static void
prepq_lookup(image_exec_t **image, image_exec_t **interp,
proc_t *proc, char *imagepath, audit_attr_t *attr, char **argv) {
*image = NULL;
*interp = NULL;
pqlookup++;
for (tommy_node *node = tommy_list_head(&pqlist);
node; node = node->next) {
image_exec_t *ei = node->data;
assert(ei);
if (!*image) {
/*
* Find the image based on (pid,dev,ino) or
* (pid,basename(path)) as a fallback if no attr is
* available from the audit event. When the kernel
* passes a wrong path to the audit framework, it does
* not provide attributes; in that case we have to rely
* on just the pid and the basename.
*/
if (ei->pid == proc->pid &&
((attr && ei->stat.dev == attr->dev &&
ei->stat.ino == attr->ino) ||
(!attr &&
!sys_basenamecmp(ei->path, imagepath)))) {
/* we have a match */
prepq_remove_existing(ei);
*image = ei;
/* script executions always have the
* interpreter as argv[0] and the script file
* as argv[1]. The remaining arguments are the
* arguments passed to the scripts, if any */
if (((*image)->flags & EIFLAG_SHEBANG) &&
argv && argv[0] && argv[1])
continue;
break;
}
} else {
assert(!*interp);
assert(argv && argv[0] && argv[1]);
/* #! can be relative path and we have no attr now.
* Using (pid,basename(path)) is the best we can do
* at this point. */
if (ei->pid == proc->pid &&
!sys_basenamecmp(ei->path, argv[0])) {
/* we have a match */
prepq_remove_existing(ei);
*interp = ei;
break;
}
}
pqskip++;
#if 0
DEBUG(config->debug, "prepq_skip",
"looking for %s[%i]: skipped %s[%i]",
imagepath, proc->pid, ei->path, ei->pid);
#endif
if (++ei->pqttl == MAXPQTTL) {
DEBUG(config->debug, "prepq_drop",
"looking for %s[%i]: dropped %s[%i]",
imagepath, proc->pid, ei->path, ei->pid);
prepq_remove_existing(ei);
image_exec_free(ei);
pqdrop++;
}
}
assert(!(*interp && !*image));
}
/*
* For scripts, this will be called once, with argv[0] as the interpreter and
* argv[1+] as argv[0+] of the script execution, imagepath as the script and
* attr as the file attributes of the script.
*
* Ownership of argv and imagepath is transfered, procmon guarantees that they
* will be freed. Only argv and attr can be NULL.
*/
void
procmon_exec(struct timespec *tv,
audit_proc_t *subject,
char *imagepath, audit_attr_t *attr,
char **argv, char **envv) {
proc_t *proc;
image_exec_t *prev_image_exec;
char *cwd;
#ifdef DEBUG_PROCMON
DEBUG(config->debug, "procmon_exec",
"subject->pid=%i imagepath=%s", subject->pid, imagepath);
#endif
proc = proctab_find(subject->pid);
if (!proc) {
proc = procmon_proc_from_pid(subject->pid, true, tv);
if (!proc) {
if (errno != ENOMEM) {
miss_execsubj++;
DEBUG(config->debug, "miss_execsubj",
"subject.pid=%i imagepath=%s argv[0]=%s",
subject->pid, imagepath,
argv ? argv[0] : NULL);
}
free(imagepath);
if (argv)
free(argv);
if (envv)
free(envv);
return;
}
liveacq++;
}
assert(proc);
image_exec_t *image, *interp;
prepq_lookup(&image, &interp, proc, imagepath, attr, argv);
#if 0
if (image)
fprintf(stderr, "found kext image "
"pid=%i path=%s is_script=%i\n",
image->pid, image->path,
image->flags & EIFLAG_SHEBANG);
if (interp)
fprintf(stderr, "found kext interp "
"pid=%i path=%s is_script=%i\n",
interp->pid, interp->path,
interp->flags & EIFLAG_SHEBANG);
#endif
if (!image) {
DEBUG(config->debug && config->kextlevel > 0,
"prepq_miss",
"looking for %s[%i]: not found (image)",
imagepath, proc->pid);
pqmiss++;
image = image_exec_new(imagepath);
if (!image) {
/* no counter, oom is the only reason this can happen */
if (argv)
free(argv);
if (envv)
free(envv);
assert(!interp);
return;
}
} else {
free(imagepath);
}
assert(image);
image_exec_open(image, attr, false);
/*
* XXX why are we not using the shebang from the script file here if
* argv is unavailable?
*/
if (image->flags & EIFLAG_SHEBANG) {
if (!interp) {
DEBUG(config->debug && config->kextlevel > 0,
"prepq_miss",
"looking for %s[%i]: not found (interp "
"argv[0]=%s)",
imagepath, proc->pid, argv ? argv[0] : NULL);
pqmiss++;
if (!argv) {
miss_execinterp++;
DEBUG(config->debug, "miss_execinterp",
"subject.pid=%i imagepath=%s "
"argv=NULL attr:%s",
subject->pid, imagepath,
attr ? "y" : "n");
image_exec_free(image);
if (envv)
free(envv);
return;
}
if (argv[0][0] == '/' || proc->cwd) {
char *p = sys_realpath(argv[0], proc->cwd);
if (!p) {
if (errno == ENOMEM)
atomic64_inc(&ooms);
miss_execinterp++;
DEBUG(config->debug,
"miss_execinterp",
"subject.pid=%i imagepath=%s "
"argv[0]=%s argv[1]=%s "
"attr:%s",
subject->pid, imagepath,
argv[0], argv[1],
attr ? "y" : "n");
image_exec_free(image);
free(argv);
if (envv)
free(envv);
return;
}
interp = image_exec_new(p);
}
if (!interp) {
miss_execinterp++;
DEBUG(config->debug, "miss_execinterp",
"subject.pid=%i imagepath=%s "
"argv[0]=%s argv[1]=%s attr:%s",
subject->pid, imagepath,
argv[0], argv[1],
attr ? "y" : "n");
image_exec_free(image);
free(argv);
if (envv)