-
Notifications
You must be signed in to change notification settings - Fork 30
/
proc.c
281 lines (250 loc) · 5.5 KB
/
proc.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
/*-
* 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 "proc.h"
#include "tommyhash.h"
#include "filemon.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static proc_t *proctab[UINT16_MAX + 1];
uint32_t procs; /* external access from procmap.c */
_Static_assert(sizeof(pid_t) == 4, "pid_t is 32bit");
#define hashpid(P) (uint16_t)tommy_inthash_u32((uint32_t)(P))
/*
* file descriptor tracking
*/
fd_ctx_t *
proc_getfd(proc_t *proc, int fd) {
if (fd < 0)
return NULL;
if (fd < (int)(sizeof(proc->fdlovect)/sizeof(proc->fdlovect[0])))
return proc->fdlovect[fd];
tommy_node *node = tommy_list_head(&proc->fdhilist);
while (node) {
fd_ctx_t *ctx = node->data;
if (ctx->fd == fd)
return ctx;
node = node->next;
}
return NULL;
}
void
proc_setfd(proc_t *proc, fd_ctx_t *ctx) {
if (ctx->fd < 0)
return;
if (ctx->fd < (int)(sizeof(proc->fdlovect)/sizeof(proc->fdlovect[0]))) {
proc->fdlovect[ctx->fd] = ctx;
tommy_list_insert_head(&proc->fdlolist, &ctx->node, ctx);
} else {
tommy_list_insert_head(&proc->fdhilist, &ctx->node, ctx);
}
}
fd_ctx_t *
proc_closefd(proc_t *proc, int fd) {
if (fd < 0)
return NULL;
if (fd < (int)(sizeof(proc->fdlovect)/sizeof(proc->fdlovect[0]))) {
fd_ctx_t *ctx = proc->fdlovect[fd];
proc->fdlovect[fd] = NULL;
if (ctx)
tommy_list_remove_existing(&proc->fdlolist, &ctx->node);
return ctx;
}
tommy_node *node = tommy_list_head(&proc->fdhilist);
while (node) {
fd_ctx_t *ctx = node->data;
if (ctx->fd == fd) {
tommy_list_remove_existing(&proc->fdhilist, node);
return ctx;
}
node = node->next;
}
return NULL;
}
void
proc_triggerfd(fd_ctx_t *ctx, struct timespec *tv) {
if ((ctx->flags & FDFLAG_FILE) && ctx->fi.path) {
assert(ctx->fi.path);
filemon_touched(tv, &ctx->fi.subject, ctx->fi.path);
ctx->fi.path = NULL;
}
}
void
proc_freefd(fd_ctx_t *ctx) {
if ((ctx->flags & FDFLAG_FILE) && ctx->fi.path) {
free(ctx->fi.path);
ctx->fi.path = NULL;
}
free(ctx);
}
/*
* process tracking
*/
static proc_t *
proc_new(void) {
proc_t *proc;
proc = malloc(sizeof(proc_t));
if (!proc)
return NULL;
bzero(proc, sizeof(proc_t));
tommy_list_init(&proc->fdlolist);
tommy_list_init(&proc->fdhilist);
procs++;
return proc;
}
/*
* Timestamp tv is passed down from the event that caused the proc to be
* evicted; used for events triggered by implicit closing of open files.
* Subject and path are stored when setting the file descriptor.
*/
static void
proc_free(proc_t *proc, struct timespec *tv) {
fd_ctx_t *ctx;
assert(proc);
while (!tommy_list_empty(&proc->fdlolist)) {
ctx = tommy_list_remove_existing(&proc->fdlolist,
tommy_list_head(&proc->fdlolist));
if (tv)
proc_triggerfd(ctx, tv);
proc_freefd(ctx);
}
while (!tommy_list_empty(&proc->fdhilist)) {
ctx = tommy_list_remove_existing(&proc->fdhilist,
tommy_list_head(&proc->fdhilist));
if (tv)
proc_triggerfd(ctx, tv);
proc_freefd(ctx);
}
if (proc->image_exec)
image_exec_free(proc->image_exec);
if (proc->cwd)
free(proc->cwd);
assert(procs > 0);
procs--;
free(proc);
}
/*
* Precondition is that pid does not exist in proctab.
*
* Returned pointer points directly into proctab-internal storage.
*/
proc_t *
proctab_create(pid_t pid) {
proc_t *proc;
uint16_t h;
h = hashpid(pid);
proc = proctab[h];
if (!proc) {
proc = proc_new();
if (proc == NULL)
return NULL;
proctab[h] = proc;
} else {
assert(proc->pid != pid);
while (proc->next) {
assert(proc->pid != pid);
proc = proc->next;
}
proc->next = proc_new();
if (proc->next == NULL)
return NULL;
proc = proc->next;
}
assert(proc);
assert(!proc->next);
proc->pid = pid;
return proc;
}
/*
* Returned pointer points directly into proctab-internal storage.
*/
proc_t *
proctab_find(pid_t pid) {
proc_t *proc;
proc = proctab[hashpid(pid)];
if (proc) {
if (proc->pid == pid)
return proc;
while ((proc = proc->next)) {
if (proc->pid == pid)
return proc;
}
}
return NULL;
}
/*
* Returned pointer points directly into proctab-internal storage.
*/
proc_t *
proctab_find_or_create(pid_t pid) {
proc_t *proc;
proc = proctab_find(pid);
if (proc)
return proc;
return proctab_create(pid);
}
/*
* Need to make sure to keep the proc_t accessible in proctab during
* proc_free, because proc_free can trigger a launchd-add event which
* in turn needs to loop up the proc to access the image_exec_t.
*/
void
proctab_remove(pid_t pid, struct timespec *tv) {
proc_t *proc;
uint16_t h;
h = hashpid(pid);
proc = proctab[h];
if (proc) {
/* pid is first in the list */
if (proc->pid == pid) {
proc_t *tmp = proc->next;
proc_free(proc, tv);
proctab[h] = tmp;
return;
}
/* pid is not first in the list */
while (proc->next) {
if (proc->next->pid == pid) {
proc_t *tmp = proc->next;
proc_free(tmp, tv);
proc->next = tmp->next;
return;
}
proc = proc->next;
}
}
}
/*
* Does not trigger any implicit close filemon events anymore.
*/
static void
proctab_flush(void) {
proc_t *proc, *next;
uint16_t h;
for (size_t i = 0; i < (sizeof(proctab)/sizeof(proctab[0])); i++) {
h = (uint16_t)i;
proc = proctab[h];
while (proc) {
next = proc->next;
proc_free(proc, NULL);
proc = next;
}
}
}
void
proctab_init(void) {
procs = 0;
bzero(proctab, sizeof(proctab));
}
void
proctab_fini(void) {
proctab_flush();
assert(procs == 0);
}