]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* auditsc.c -- System-call auditing support -*- linux-c -*- |
2 | * Handles all system-call specific auditing features. | |
3 | * | |
4 | * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina. | |
5 | * All Rights Reserved. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> | |
22 | * | |
23 | * Many of the ideas implemented here are from Stephen C. Tweedie, | |
24 | * especially the idea of avoiding a copy by using getname. | |
25 | * | |
26 | * The method for actual interception of syscall entry and exit (not in | |
27 | * this file -- see entry.S) is based on a GPL'd patch written by | |
28 | * okir@suse.de and Copyright 2003 SuSE Linux AG. | |
29 | * | |
30 | */ | |
31 | ||
32 | #include <linux/init.h> | |
33 | #include <asm/atomic.h> | |
34 | #include <asm/types.h> | |
35 | #include <linux/mm.h> | |
36 | #include <linux/module.h> | |
37 | ||
38 | #include <linux/audit.h> | |
39 | #include <linux/personality.h> | |
40 | #include <linux/time.h> | |
41 | #include <asm/unistd.h> | |
42 | ||
43 | /* 0 = no checking | |
44 | 1 = put_count checking | |
45 | 2 = verbose put_count checking | |
46 | */ | |
47 | #define AUDIT_DEBUG 0 | |
48 | ||
49 | /* No syscall auditing will take place unless audit_enabled != 0. */ | |
50 | extern int audit_enabled; | |
51 | ||
52 | /* AUDIT_NAMES is the number of slots we reserve in the audit_context | |
53 | * for saving names from getname(). */ | |
54 | #define AUDIT_NAMES 20 | |
55 | ||
56 | /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the | |
57 | * audit_context from being used for nameless inodes from | |
58 | * path_lookup. */ | |
59 | #define AUDIT_NAMES_RESERVED 7 | |
60 | ||
61 | /* At task start time, the audit_state is set in the audit_context using | |
62 | a per-task filter. At syscall entry, the audit_state is augmented by | |
63 | the syscall filter. */ | |
64 | enum audit_state { | |
65 | AUDIT_DISABLED, /* Do not create per-task audit_context. | |
66 | * No syscall-specific audit records can | |
67 | * be generated. */ | |
68 | AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context, | |
69 | * but don't necessarily fill it in at | |
70 | * syscall entry time (i.e., filter | |
71 | * instead). */ | |
72 | AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context, | |
73 | * and always fill it in at syscall | |
74 | * entry time. This makes a full | |
75 | * syscall record available if some | |
76 | * other part of the kernel decides it | |
77 | * should be recorded. */ | |
78 | AUDIT_RECORD_CONTEXT /* Create the per-task audit_context, | |
79 | * always fill it in at syscall entry | |
80 | * time, and always write out the audit | |
81 | * record at syscall exit time. */ | |
82 | }; | |
83 | ||
84 | /* When fs/namei.c:getname() is called, we store the pointer in name and | |
85 | * we don't let putname() free it (instead we free all of the saved | |
86 | * pointers at syscall exit time). | |
87 | * | |
88 | * Further, in fs/namei.c:path_lookup() we store the inode and device. */ | |
89 | struct audit_names { | |
90 | const char *name; | |
91 | unsigned long ino; | |
92 | dev_t dev; | |
93 | umode_t mode; | |
94 | uid_t uid; | |
95 | gid_t gid; | |
96 | dev_t rdev; | |
97 | }; | |
98 | ||
99 | struct audit_aux_data { | |
100 | struct audit_aux_data *next; | |
101 | int type; | |
102 | }; | |
103 | ||
104 | #define AUDIT_AUX_IPCPERM 0 | |
105 | ||
106 | struct audit_aux_data_ipcctl { | |
107 | struct audit_aux_data d; | |
108 | struct ipc_perm p; | |
109 | unsigned long qbytes; | |
110 | uid_t uid; | |
111 | gid_t gid; | |
112 | mode_t mode; | |
113 | }; | |
114 | ||
115 | ||
116 | /* The per-task audit context. */ | |
117 | struct audit_context { | |
118 | int in_syscall; /* 1 if task is in a syscall */ | |
119 | enum audit_state state; | |
120 | unsigned int serial; /* serial number for record */ | |
121 | struct timespec ctime; /* time of syscall entry */ | |
122 | uid_t loginuid; /* login uid (identity) */ | |
123 | int major; /* syscall number */ | |
124 | unsigned long argv[4]; /* syscall arguments */ | |
125 | int return_valid; /* return code is valid */ | |
126 | int return_code;/* syscall return code */ | |
127 | int auditable; /* 1 if record should be written */ | |
128 | int name_count; | |
129 | struct audit_names names[AUDIT_NAMES]; | |
130 | struct audit_context *previous; /* For nested syscalls */ | |
131 | struct audit_aux_data *aux; | |
132 | ||
133 | /* Save things to print about task_struct */ | |
134 | pid_t pid; | |
135 | uid_t uid, euid, suid, fsuid; | |
136 | gid_t gid, egid, sgid, fsgid; | |
137 | unsigned long personality; | |
138 | ||
139 | #if AUDIT_DEBUG | |
140 | int put_count; | |
141 | int ino_count; | |
142 | #endif | |
143 | }; | |
144 | ||
145 | /* Public API */ | |
146 | /* There are three lists of rules -- one to search at task creation | |
147 | * time, one to search at syscall entry time, and another to search at | |
148 | * syscall exit time. */ | |
149 | static LIST_HEAD(audit_tsklist); | |
150 | static LIST_HEAD(audit_entlist); | |
151 | static LIST_HEAD(audit_extlist); | |
152 | ||
153 | struct audit_entry { | |
154 | struct list_head list; | |
155 | struct rcu_head rcu; | |
156 | struct audit_rule rule; | |
157 | }; | |
158 | ||
159 | /* Check to see if two rules are identical. It is called from | |
160 | * audit_del_rule during AUDIT_DEL. */ | |
161 | static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b) | |
162 | { | |
163 | int i; | |
164 | ||
165 | if (a->flags != b->flags) | |
166 | return 1; | |
167 | ||
168 | if (a->action != b->action) | |
169 | return 1; | |
170 | ||
171 | if (a->field_count != b->field_count) | |
172 | return 1; | |
173 | ||
174 | for (i = 0; i < a->field_count; i++) { | |
175 | if (a->fields[i] != b->fields[i] | |
176 | || a->values[i] != b->values[i]) | |
177 | return 1; | |
178 | } | |
179 | ||
180 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
181 | if (a->mask[i] != b->mask[i]) | |
182 | return 1; | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | /* Note that audit_add_rule and audit_del_rule are called via | |
188 | * audit_receive() in audit.c, and are protected by | |
189 | * audit_netlink_sem. */ | |
190 | static inline int audit_add_rule(struct audit_entry *entry, | |
191 | struct list_head *list) | |
192 | { | |
193 | if (entry->rule.flags & AUDIT_PREPEND) { | |
194 | entry->rule.flags &= ~AUDIT_PREPEND; | |
195 | list_add_rcu(&entry->list, list); | |
196 | } else { | |
197 | list_add_tail_rcu(&entry->list, list); | |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
202 | static void audit_free_rule(struct rcu_head *head) | |
203 | { | |
204 | struct audit_entry *e = container_of(head, struct audit_entry, rcu); | |
205 | kfree(e); | |
206 | } | |
207 | ||
208 | /* Note that audit_add_rule and audit_del_rule are called via | |
209 | * audit_receive() in audit.c, and are protected by | |
210 | * audit_netlink_sem. */ | |
211 | static inline int audit_del_rule(struct audit_rule *rule, | |
212 | struct list_head *list) | |
213 | { | |
214 | struct audit_entry *e; | |
215 | ||
216 | /* Do not use the _rcu iterator here, since this is the only | |
217 | * deletion routine. */ | |
218 | list_for_each_entry(e, list, list) { | |
219 | if (!audit_compare_rule(rule, &e->rule)) { | |
220 | list_del_rcu(&e->list); | |
221 | call_rcu(&e->rcu, audit_free_rule); | |
222 | return 0; | |
223 | } | |
224 | } | |
225 | return -EFAULT; /* No matching rule */ | |
226 | } | |
227 | ||
228 | #ifdef CONFIG_NET | |
229 | /* Copy rule from user-space to kernel-space. Called during | |
230 | * AUDIT_ADD. */ | |
231 | static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s) | |
232 | { | |
233 | int i; | |
234 | ||
235 | if (s->action != AUDIT_NEVER | |
236 | && s->action != AUDIT_POSSIBLE | |
237 | && s->action != AUDIT_ALWAYS) | |
238 | return -1; | |
239 | if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS) | |
240 | return -1; | |
241 | ||
242 | d->flags = s->flags; | |
243 | d->action = s->action; | |
244 | d->field_count = s->field_count; | |
245 | for (i = 0; i < d->field_count; i++) { | |
246 | d->fields[i] = s->fields[i]; | |
247 | d->values[i] = s->values[i]; | |
248 | } | |
249 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i]; | |
250 | return 0; | |
251 | } | |
252 | ||
253 | int audit_receive_filter(int type, int pid, int uid, int seq, void *data) | |
254 | { | |
255 | u32 flags; | |
256 | struct audit_entry *entry; | |
257 | int err = 0; | |
258 | ||
259 | switch (type) { | |
260 | case AUDIT_LIST: | |
261 | /* The *_rcu iterators not needed here because we are | |
262 | always called with audit_netlink_sem held. */ | |
263 | list_for_each_entry(entry, &audit_tsklist, list) | |
264 | audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, | |
265 | &entry->rule, sizeof(entry->rule)); | |
266 | list_for_each_entry(entry, &audit_entlist, list) | |
267 | audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, | |
268 | &entry->rule, sizeof(entry->rule)); | |
269 | list_for_each_entry(entry, &audit_extlist, list) | |
270 | audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, | |
271 | &entry->rule, sizeof(entry->rule)); | |
272 | audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0); | |
273 | break; | |
274 | case AUDIT_ADD: | |
275 | if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL))) | |
276 | return -ENOMEM; | |
277 | if (audit_copy_rule(&entry->rule, data)) { | |
278 | kfree(entry); | |
279 | return -EINVAL; | |
280 | } | |
281 | flags = entry->rule.flags; | |
282 | if (!err && (flags & AUDIT_PER_TASK)) | |
283 | err = audit_add_rule(entry, &audit_tsklist); | |
284 | if (!err && (flags & AUDIT_AT_ENTRY)) | |
285 | err = audit_add_rule(entry, &audit_entlist); | |
286 | if (!err && (flags & AUDIT_AT_EXIT)) | |
287 | err = audit_add_rule(entry, &audit_extlist); | |
288 | break; | |
289 | case AUDIT_DEL: | |
290 | flags =((struct audit_rule *)data)->flags; | |
291 | if (!err && (flags & AUDIT_PER_TASK)) | |
292 | err = audit_del_rule(data, &audit_tsklist); | |
293 | if (!err && (flags & AUDIT_AT_ENTRY)) | |
294 | err = audit_del_rule(data, &audit_entlist); | |
295 | if (!err && (flags & AUDIT_AT_EXIT)) | |
296 | err = audit_del_rule(data, &audit_extlist); | |
297 | break; | |
298 | default: | |
299 | return -EINVAL; | |
300 | } | |
301 | ||
302 | return err; | |
303 | } | |
304 | #endif | |
305 | ||
306 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 | |
307 | * otherwise. */ | |
308 | static int audit_filter_rules(struct task_struct *tsk, | |
309 | struct audit_rule *rule, | |
310 | struct audit_context *ctx, | |
311 | enum audit_state *state) | |
312 | { | |
313 | int i, j; | |
314 | ||
315 | for (i = 0; i < rule->field_count; i++) { | |
316 | u32 field = rule->fields[i] & ~AUDIT_NEGATE; | |
317 | u32 value = rule->values[i]; | |
318 | int result = 0; | |
319 | ||
320 | switch (field) { | |
321 | case AUDIT_PID: | |
322 | result = (tsk->pid == value); | |
323 | break; | |
324 | case AUDIT_UID: | |
325 | result = (tsk->uid == value); | |
326 | break; | |
327 | case AUDIT_EUID: | |
328 | result = (tsk->euid == value); | |
329 | break; | |
330 | case AUDIT_SUID: | |
331 | result = (tsk->suid == value); | |
332 | break; | |
333 | case AUDIT_FSUID: | |
334 | result = (tsk->fsuid == value); | |
335 | break; | |
336 | case AUDIT_GID: | |
337 | result = (tsk->gid == value); | |
338 | break; | |
339 | case AUDIT_EGID: | |
340 | result = (tsk->egid == value); | |
341 | break; | |
342 | case AUDIT_SGID: | |
343 | result = (tsk->sgid == value); | |
344 | break; | |
345 | case AUDIT_FSGID: | |
346 | result = (tsk->fsgid == value); | |
347 | break; | |
348 | case AUDIT_PERS: | |
349 | result = (tsk->personality == value); | |
350 | break; | |
351 | ||
352 | case AUDIT_EXIT: | |
353 | if (ctx && ctx->return_valid) | |
354 | result = (ctx->return_code == value); | |
355 | break; | |
356 | case AUDIT_SUCCESS: | |
357 | if (ctx && ctx->return_valid) | |
358 | result = (ctx->return_code >= 0); | |
359 | break; | |
360 | case AUDIT_DEVMAJOR: | |
361 | if (ctx) { | |
362 | for (j = 0; j < ctx->name_count; j++) { | |
363 | if (MAJOR(ctx->names[j].dev)==value) { | |
364 | ++result; | |
365 | break; | |
366 | } | |
367 | } | |
368 | } | |
369 | break; | |
370 | case AUDIT_DEVMINOR: | |
371 | if (ctx) { | |
372 | for (j = 0; j < ctx->name_count; j++) { | |
373 | if (MINOR(ctx->names[j].dev)==value) { | |
374 | ++result; | |
375 | break; | |
376 | } | |
377 | } | |
378 | } | |
379 | break; | |
380 | case AUDIT_INODE: | |
381 | if (ctx) { | |
382 | for (j = 0; j < ctx->name_count; j++) { | |
383 | if (ctx->names[j].ino == value) { | |
384 | ++result; | |
385 | break; | |
386 | } | |
387 | } | |
388 | } | |
389 | break; | |
390 | case AUDIT_LOGINUID: | |
391 | result = 0; | |
392 | if (ctx) | |
393 | result = (ctx->loginuid == value); | |
394 | break; | |
395 | case AUDIT_ARG0: | |
396 | case AUDIT_ARG1: | |
397 | case AUDIT_ARG2: | |
398 | case AUDIT_ARG3: | |
399 | if (ctx) | |
400 | result = (ctx->argv[field-AUDIT_ARG0]==value); | |
401 | break; | |
402 | } | |
403 | ||
404 | if (rule->fields[i] & AUDIT_NEGATE) | |
405 | result = !result; | |
406 | if (!result) | |
407 | return 0; | |
408 | } | |
409 | switch (rule->action) { | |
410 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; | |
411 | case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break; | |
412 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; | |
413 | } | |
414 | return 1; | |
415 | } | |
416 | ||
417 | /* At process creation time, we can determine if system-call auditing is | |
418 | * completely disabled for this task. Since we only have the task | |
419 | * structure at this point, we can only check uid and gid. | |
420 | */ | |
421 | static enum audit_state audit_filter_task(struct task_struct *tsk) | |
422 | { | |
423 | struct audit_entry *e; | |
424 | enum audit_state state; | |
425 | ||
426 | rcu_read_lock(); | |
427 | list_for_each_entry_rcu(e, &audit_tsklist, list) { | |
428 | if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { | |
429 | rcu_read_unlock(); | |
430 | return state; | |
431 | } | |
432 | } | |
433 | rcu_read_unlock(); | |
434 | return AUDIT_BUILD_CONTEXT; | |
435 | } | |
436 | ||
437 | /* At syscall entry and exit time, this filter is called if the | |
438 | * audit_state is not low enough that auditing cannot take place, but is | |
439 | * also not high enough that we already know we have to write and audit | |
440 | * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). | |
441 | */ | |
442 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, | |
443 | struct audit_context *ctx, | |
444 | struct list_head *list) | |
445 | { | |
446 | struct audit_entry *e; | |
447 | enum audit_state state; | |
448 | int word = AUDIT_WORD(ctx->major); | |
449 | int bit = AUDIT_BIT(ctx->major); | |
450 | ||
451 | rcu_read_lock(); | |
452 | list_for_each_entry_rcu(e, list, list) { | |
453 | if ((e->rule.mask[word] & bit) == bit | |
454 | && audit_filter_rules(tsk, &e->rule, ctx, &state)) { | |
455 | rcu_read_unlock(); | |
456 | return state; | |
457 | } | |
458 | } | |
459 | rcu_read_unlock(); | |
460 | return AUDIT_BUILD_CONTEXT; | |
461 | } | |
462 | ||
463 | /* This should be called with task_lock() held. */ | |
464 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, | |
465 | int return_valid, | |
466 | int return_code) | |
467 | { | |
468 | struct audit_context *context = tsk->audit_context; | |
469 | ||
470 | if (likely(!context)) | |
471 | return NULL; | |
472 | context->return_valid = return_valid; | |
473 | context->return_code = return_code; | |
474 | ||
475 | if (context->in_syscall && !context->auditable) { | |
476 | enum audit_state state; | |
477 | state = audit_filter_syscall(tsk, context, &audit_extlist); | |
478 | if (state == AUDIT_RECORD_CONTEXT) | |
479 | context->auditable = 1; | |
480 | } | |
481 | ||
482 | context->pid = tsk->pid; | |
483 | context->uid = tsk->uid; | |
484 | context->gid = tsk->gid; | |
485 | context->euid = tsk->euid; | |
486 | context->suid = tsk->suid; | |
487 | context->fsuid = tsk->fsuid; | |
488 | context->egid = tsk->egid; | |
489 | context->sgid = tsk->sgid; | |
490 | context->fsgid = tsk->fsgid; | |
491 | context->personality = tsk->personality; | |
492 | tsk->audit_context = NULL; | |
493 | return context; | |
494 | } | |
495 | ||
496 | static inline void audit_free_names(struct audit_context *context) | |
497 | { | |
498 | int i; | |
499 | ||
500 | #if AUDIT_DEBUG == 2 | |
501 | if (context->auditable | |
502 | ||context->put_count + context->ino_count != context->name_count) { | |
503 | printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d" | |
504 | " name_count=%d put_count=%d" | |
505 | " ino_count=%d [NOT freeing]\n", | |
506 | __LINE__, | |
507 | context->serial, context->major, context->in_syscall, | |
508 | context->name_count, context->put_count, | |
509 | context->ino_count); | |
510 | for (i = 0; i < context->name_count; i++) | |
511 | printk(KERN_ERR "names[%d] = %p = %s\n", i, | |
512 | context->names[i].name, | |
513 | context->names[i].name); | |
514 | dump_stack(); | |
515 | return; | |
516 | } | |
517 | #endif | |
518 | #if AUDIT_DEBUG | |
519 | context->put_count = 0; | |
520 | context->ino_count = 0; | |
521 | #endif | |
522 | ||
523 | for (i = 0; i < context->name_count; i++) | |
524 | if (context->names[i].name) | |
525 | __putname(context->names[i].name); | |
526 | context->name_count = 0; | |
527 | } | |
528 | ||
529 | static inline void audit_free_aux(struct audit_context *context) | |
530 | { | |
531 | struct audit_aux_data *aux; | |
532 | ||
533 | while ((aux = context->aux)) { | |
534 | context->aux = aux->next; | |
535 | kfree(aux); | |
536 | } | |
537 | } | |
538 | ||
539 | static inline void audit_zero_context(struct audit_context *context, | |
540 | enum audit_state state) | |
541 | { | |
542 | uid_t loginuid = context->loginuid; | |
543 | ||
544 | memset(context, 0, sizeof(*context)); | |
545 | context->state = state; | |
546 | context->loginuid = loginuid; | |
547 | } | |
548 | ||
549 | static inline struct audit_context *audit_alloc_context(enum audit_state state) | |
550 | { | |
551 | struct audit_context *context; | |
552 | ||
553 | if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) | |
554 | return NULL; | |
555 | audit_zero_context(context, state); | |
556 | return context; | |
557 | } | |
558 | ||
559 | /* Filter on the task information and allocate a per-task audit context | |
560 | * if necessary. Doing so turns on system call auditing for the | |
561 | * specified task. This is called from copy_process, so no lock is | |
562 | * needed. */ | |
563 | int audit_alloc(struct task_struct *tsk) | |
564 | { | |
565 | struct audit_context *context; | |
566 | enum audit_state state; | |
567 | ||
568 | if (likely(!audit_enabled)) | |
569 | return 0; /* Return if not auditing. */ | |
570 | ||
571 | state = audit_filter_task(tsk); | |
572 | if (likely(state == AUDIT_DISABLED)) | |
573 | return 0; | |
574 | ||
575 | if (!(context = audit_alloc_context(state))) { | |
576 | audit_log_lost("out of memory in audit_alloc"); | |
577 | return -ENOMEM; | |
578 | } | |
579 | ||
580 | /* Preserve login uid */ | |
581 | context->loginuid = -1; | |
582 | if (current->audit_context) | |
583 | context->loginuid = current->audit_context->loginuid; | |
584 | ||
585 | tsk->audit_context = context; | |
586 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); | |
587 | return 0; | |
588 | } | |
589 | ||
590 | static inline void audit_free_context(struct audit_context *context) | |
591 | { | |
592 | struct audit_context *previous; | |
593 | int count = 0; | |
594 | ||
595 | do { | |
596 | previous = context->previous; | |
597 | if (previous || (count && count < 10)) { | |
598 | ++count; | |
599 | printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" | |
600 | " freeing multiple contexts (%d)\n", | |
601 | context->serial, context->major, | |
602 | context->name_count, count); | |
603 | } | |
604 | audit_free_names(context); | |
605 | audit_free_aux(context); | |
606 | kfree(context); | |
607 | context = previous; | |
608 | } while (context); | |
609 | if (count >= 10) | |
610 | printk(KERN_ERR "audit: freed %d contexts\n", count); | |
611 | } | |
612 | ||
613 | static void audit_log_exit(struct audit_context *context) | |
614 | { | |
615 | int i; | |
616 | struct audit_buffer *ab; | |
617 | ||
618 | ab = audit_log_start(context); | |
619 | if (!ab) | |
620 | return; /* audit_panic has been called */ | |
621 | audit_log_format(ab, "syscall=%d", context->major); | |
622 | if (context->personality != PER_LINUX) | |
623 | audit_log_format(ab, " per=%lx", context->personality); | |
624 | if (context->return_valid) | |
625 | audit_log_format(ab, " exit=%d", context->return_code); | |
626 | audit_log_format(ab, | |
627 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" | |
628 | " pid=%d loginuid=%d uid=%d gid=%d" | |
629 | " euid=%d suid=%d fsuid=%d" | |
630 | " egid=%d sgid=%d fsgid=%d", | |
631 | context->argv[0], | |
632 | context->argv[1], | |
633 | context->argv[2], | |
634 | context->argv[3], | |
635 | context->name_count, | |
636 | context->pid, | |
637 | context->loginuid, | |
638 | context->uid, | |
639 | context->gid, | |
640 | context->euid, context->suid, context->fsuid, | |
641 | context->egid, context->sgid, context->fsgid); | |
642 | audit_log_end(ab); | |
643 | while (context->aux) { | |
644 | struct audit_aux_data *aux; | |
645 | ||
646 | ab = audit_log_start(context); | |
647 | if (!ab) | |
648 | continue; /* audit_panic has been called */ | |
649 | ||
650 | aux = context->aux; | |
651 | context->aux = aux->next; | |
652 | ||
653 | audit_log_format(ab, "auxitem=%d", aux->type); | |
654 | switch (aux->type) { | |
655 | case AUDIT_AUX_IPCPERM: { | |
656 | struct audit_aux_data_ipcctl *axi = (void *)aux; | |
657 | audit_log_format(ab, | |
658 | " qbytes=%lx uid=%d gid=%d mode=%x", | |
659 | axi->qbytes, axi->uid, axi->gid, axi->mode); | |
660 | } | |
661 | } | |
662 | audit_log_end(ab); | |
663 | kfree(aux); | |
664 | } | |
665 | ||
666 | for (i = 0; i < context->name_count; i++) { | |
667 | ab = audit_log_start(context); | |
668 | if (!ab) | |
669 | continue; /* audit_panic has been called */ | |
670 | audit_log_format(ab, "item=%d", i); | |
671 | if (context->names[i].name) | |
672 | audit_log_format(ab, " name=%s", | |
673 | context->names[i].name); | |
674 | if (context->names[i].ino != (unsigned long)-1) | |
675 | audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o" | |
676 | " uid=%d gid=%d rdev=%02x:%02x", | |
677 | context->names[i].ino, | |
678 | MAJOR(context->names[i].dev), | |
679 | MINOR(context->names[i].dev), | |
680 | context->names[i].mode, | |
681 | context->names[i].uid, | |
682 | context->names[i].gid, | |
683 | MAJOR(context->names[i].rdev), | |
684 | MINOR(context->names[i].rdev)); | |
685 | audit_log_end(ab); | |
686 | } | |
687 | } | |
688 | ||
689 | /* Free a per-task audit context. Called from copy_process and | |
690 | * __put_task_struct. */ | |
691 | void audit_free(struct task_struct *tsk) | |
692 | { | |
693 | struct audit_context *context; | |
694 | ||
695 | task_lock(tsk); | |
696 | context = audit_get_context(tsk, 0, 0); | |
697 | task_unlock(tsk); | |
698 | ||
699 | if (likely(!context)) | |
700 | return; | |
701 | ||
702 | /* Check for system calls that do not go through the exit | |
703 | * function (e.g., exit_group), then free context block. */ | |
704 | if (context->in_syscall && context->auditable) | |
705 | audit_log_exit(context); | |
706 | ||
707 | audit_free_context(context); | |
708 | } | |
709 | ||
710 | /* Compute a serial number for the audit record. Audit records are | |
711 | * written to user-space as soon as they are generated, so a complete | |
712 | * audit record may be written in several pieces. The timestamp of the | |
713 | * record and this serial number are used by the user-space daemon to | |
714 | * determine which pieces belong to the same audit record. The | |
715 | * (timestamp,serial) tuple is unique for each syscall and is live from | |
716 | * syscall entry to syscall exit. | |
717 | * | |
718 | * Atomic values are only guaranteed to be 24-bit, so we count down. | |
719 | * | |
720 | * NOTE: Another possibility is to store the formatted records off the | |
721 | * audit context (for those records that have a context), and emit them | |
722 | * all at syscall exit. However, this could delay the reporting of | |
723 | * significant errors until syscall exit (or never, if the system | |
724 | * halts). */ | |
725 | static inline unsigned int audit_serial(void) | |
726 | { | |
727 | static atomic_t serial = ATOMIC_INIT(0xffffff); | |
728 | unsigned int a, b; | |
729 | ||
730 | do { | |
731 | a = atomic_read(&serial); | |
732 | if (atomic_dec_and_test(&serial)) | |
733 | atomic_set(&serial, 0xffffff); | |
734 | b = atomic_read(&serial); | |
735 | } while (b != a - 1); | |
736 | ||
737 | return 0xffffff - b; | |
738 | } | |
739 | ||
740 | /* Fill in audit context at syscall entry. This only happens if the | |
741 | * audit context was created when the task was created and the state or | |
742 | * filters demand the audit context be built. If the state from the | |
743 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, | |
744 | * then the record will be written at syscall exit time (otherwise, it | |
745 | * will only be written if another part of the kernel requests that it | |
746 | * be written). */ | |
747 | void audit_syscall_entry(struct task_struct *tsk, int major, | |
748 | unsigned long a1, unsigned long a2, | |
749 | unsigned long a3, unsigned long a4) | |
750 | { | |
751 | struct audit_context *context = tsk->audit_context; | |
752 | enum audit_state state; | |
753 | ||
754 | BUG_ON(!context); | |
755 | ||
756 | /* This happens only on certain architectures that make system | |
757 | * calls in kernel_thread via the entry.S interface, instead of | |
758 | * with direct calls. (If you are porting to a new | |
759 | * architecture, hitting this condition can indicate that you | |
760 | * got the _exit/_leave calls backward in entry.S.) | |
761 | * | |
762 | * i386 no | |
763 | * x86_64 no | |
764 | * ppc64 yes (see arch/ppc64/kernel/misc.S) | |
765 | * | |
766 | * This also happens with vm86 emulation in a non-nested manner | |
767 | * (entries without exits), so this case must be caught. | |
768 | */ | |
769 | if (context->in_syscall) { | |
770 | struct audit_context *newctx; | |
771 | ||
772 | #if defined(__NR_vm86) && defined(__NR_vm86old) | |
773 | /* vm86 mode should only be entered once */ | |
774 | if (major == __NR_vm86 || major == __NR_vm86old) | |
775 | return; | |
776 | #endif | |
777 | #if AUDIT_DEBUG | |
778 | printk(KERN_ERR | |
779 | "audit(:%d) pid=%d in syscall=%d;" | |
780 | " entering syscall=%d\n", | |
781 | context->serial, tsk->pid, context->major, major); | |
782 | #endif | |
783 | newctx = audit_alloc_context(context->state); | |
784 | if (newctx) { | |
785 | newctx->previous = context; | |
786 | context = newctx; | |
787 | tsk->audit_context = newctx; | |
788 | } else { | |
789 | /* If we can't alloc a new context, the best we | |
790 | * can do is to leak memory (any pending putname | |
791 | * will be lost). The only other alternative is | |
792 | * to abandon auditing. */ | |
793 | audit_zero_context(context, context->state); | |
794 | } | |
795 | } | |
796 | BUG_ON(context->in_syscall || context->name_count); | |
797 | ||
798 | if (!audit_enabled) | |
799 | return; | |
800 | ||
801 | context->major = major; | |
802 | context->argv[0] = a1; | |
803 | context->argv[1] = a2; | |
804 | context->argv[2] = a3; | |
805 | context->argv[3] = a4; | |
806 | ||
807 | state = context->state; | |
808 | if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT) | |
809 | state = audit_filter_syscall(tsk, context, &audit_entlist); | |
810 | if (likely(state == AUDIT_DISABLED)) | |
811 | return; | |
812 | ||
813 | context->serial = audit_serial(); | |
814 | context->ctime = CURRENT_TIME; | |
815 | context->in_syscall = 1; | |
816 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); | |
817 | } | |
818 | ||
819 | /* Tear down after system call. If the audit context has been marked as | |
820 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from | |
821 | * filtering, or because some other part of the kernel write an audit | |
822 | * message), then write out the syscall information. In call cases, | |
823 | * free the names stored from getname(). */ | |
824 | void audit_syscall_exit(struct task_struct *tsk, int return_code) | |
825 | { | |
826 | struct audit_context *context; | |
827 | ||
828 | get_task_struct(tsk); | |
829 | task_lock(tsk); | |
830 | context = audit_get_context(tsk, 1, return_code); | |
831 | task_unlock(tsk); | |
832 | ||
833 | /* Not having a context here is ok, since the parent may have | |
834 | * called __put_task_struct. */ | |
835 | if (likely(!context)) | |
836 | return; | |
837 | ||
838 | if (context->in_syscall && context->auditable) | |
839 | audit_log_exit(context); | |
840 | ||
841 | context->in_syscall = 0; | |
842 | context->auditable = 0; | |
843 | if (context->previous) { | |
844 | struct audit_context *new_context = context->previous; | |
845 | context->previous = NULL; | |
846 | audit_free_context(context); | |
847 | tsk->audit_context = new_context; | |
848 | } else { | |
849 | audit_free_names(context); | |
850 | audit_free_aux(context); | |
851 | audit_zero_context(context, context->state); | |
852 | tsk->audit_context = context; | |
853 | } | |
854 | put_task_struct(tsk); | |
855 | } | |
856 | ||
857 | /* Add a name to the list. Called from fs/namei.c:getname(). */ | |
858 | void audit_getname(const char *name) | |
859 | { | |
860 | struct audit_context *context = current->audit_context; | |
861 | ||
862 | if (!context || IS_ERR(name) || !name) | |
863 | return; | |
864 | ||
865 | if (!context->in_syscall) { | |
866 | #if AUDIT_DEBUG == 2 | |
867 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", | |
868 | __FILE__, __LINE__, context->serial, name); | |
869 | dump_stack(); | |
870 | #endif | |
871 | return; | |
872 | } | |
873 | BUG_ON(context->name_count >= AUDIT_NAMES); | |
874 | context->names[context->name_count].name = name; | |
875 | context->names[context->name_count].ino = (unsigned long)-1; | |
876 | ++context->name_count; | |
877 | } | |
878 | ||
879 | /* Intercept a putname request. Called from | |
880 | * include/linux/fs.h:putname(). If we have stored the name from | |
881 | * getname in the audit context, then we delay the putname until syscall | |
882 | * exit. */ | |
883 | void audit_putname(const char *name) | |
884 | { | |
885 | struct audit_context *context = current->audit_context; | |
886 | ||
887 | BUG_ON(!context); | |
888 | if (!context->in_syscall) { | |
889 | #if AUDIT_DEBUG == 2 | |
890 | printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", | |
891 | __FILE__, __LINE__, context->serial, name); | |
892 | if (context->name_count) { | |
893 | int i; | |
894 | for (i = 0; i < context->name_count; i++) | |
895 | printk(KERN_ERR "name[%d] = %p = %s\n", i, | |
896 | context->names[i].name, | |
897 | context->names[i].name); | |
898 | } | |
899 | #endif | |
900 | __putname(name); | |
901 | } | |
902 | #if AUDIT_DEBUG | |
903 | else { | |
904 | ++context->put_count; | |
905 | if (context->put_count > context->name_count) { | |
906 | printk(KERN_ERR "%s:%d(:%d): major=%d" | |
907 | " in_syscall=%d putname(%p) name_count=%d" | |
908 | " put_count=%d\n", | |
909 | __FILE__, __LINE__, | |
910 | context->serial, context->major, | |
911 | context->in_syscall, name, context->name_count, | |
912 | context->put_count); | |
913 | dump_stack(); | |
914 | } | |
915 | } | |
916 | #endif | |
917 | } | |
918 | ||
919 | /* Store the inode and device from a lookup. Called from | |
920 | * fs/namei.c:path_lookup(). */ | |
921 | void audit_inode(const char *name, const struct inode *inode) | |
922 | { | |
923 | int idx; | |
924 | struct audit_context *context = current->audit_context; | |
925 | ||
926 | if (!context->in_syscall) | |
927 | return; | |
928 | if (context->name_count | |
929 | && context->names[context->name_count-1].name | |
930 | && context->names[context->name_count-1].name == name) | |
931 | idx = context->name_count - 1; | |
932 | else if (context->name_count > 1 | |
933 | && context->names[context->name_count-2].name | |
934 | && context->names[context->name_count-2].name == name) | |
935 | idx = context->name_count - 2; | |
936 | else { | |
937 | /* FIXME: how much do we care about inodes that have no | |
938 | * associated name? */ | |
939 | if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED) | |
940 | return; | |
941 | idx = context->name_count++; | |
942 | context->names[idx].name = NULL; | |
943 | #if AUDIT_DEBUG | |
944 | ++context->ino_count; | |
945 | #endif | |
946 | } | |
947 | context->names[idx].ino = inode->i_ino; | |
948 | context->names[idx].dev = inode->i_sb->s_dev; | |
949 | context->names[idx].mode = inode->i_mode; | |
950 | context->names[idx].uid = inode->i_uid; | |
951 | context->names[idx].gid = inode->i_gid; | |
952 | context->names[idx].rdev = inode->i_rdev; | |
953 | } | |
954 | ||
955 | void audit_get_stamp(struct audit_context *ctx, | |
956 | struct timespec *t, int *serial) | |
957 | { | |
958 | if (ctx) { | |
959 | t->tv_sec = ctx->ctime.tv_sec; | |
960 | t->tv_nsec = ctx->ctime.tv_nsec; | |
961 | *serial = ctx->serial; | |
962 | ctx->auditable = 1; | |
963 | } else { | |
964 | *t = CURRENT_TIME; | |
965 | *serial = 0; | |
966 | } | |
967 | } | |
968 | ||
969 | extern int audit_set_type(struct audit_buffer *ab, int type); | |
970 | ||
971 | int audit_set_loginuid(struct audit_context *ctx, uid_t loginuid) | |
972 | { | |
973 | if (ctx) { | |
974 | struct audit_buffer *ab; | |
975 | ||
976 | ab = audit_log_start(NULL); | |
977 | if (ab) { | |
978 | audit_log_format(ab, "login pid=%d uid=%u " | |
979 | "old loginuid=%u new loginuid=%u", | |
980 | ctx->pid, ctx->uid, ctx->loginuid, loginuid); | |
981 | audit_set_type(ab, AUDIT_LOGIN); | |
982 | audit_log_end(ab); | |
983 | } | |
984 | ctx->loginuid = loginuid; | |
985 | } | |
986 | return 0; | |
987 | } | |
988 | ||
989 | uid_t audit_get_loginuid(struct audit_context *ctx) | |
990 | { | |
991 | return ctx ? ctx->loginuid : -1; | |
992 | } | |
993 | ||
994 | int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode) | |
995 | { | |
996 | struct audit_aux_data_ipcctl *ax; | |
997 | struct audit_context *context = current->audit_context; | |
998 | ||
999 | if (likely(!context)) | |
1000 | return 0; | |
1001 | ||
1002 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); | |
1003 | if (!ax) | |
1004 | return -ENOMEM; | |
1005 | ||
1006 | ax->qbytes = qbytes; | |
1007 | ax->uid = uid; | |
1008 | ax->gid = gid; | |
1009 | ax->mode = mode; | |
1010 | ||
1011 | ax->d.type = AUDIT_AUX_IPCPERM; | |
1012 | ax->d.next = context->aux; | |
1013 | context->aux = (void *)ax; | |
1014 | return 0; | |
1015 | } |