AUDIT: Really exempt auditd from having its actions audited.
[linux-block.git] / kernel / auditsc.c
CommitLineData
85c8721f 1/* auditsc.c -- System-call auditing support
1da177e4
LT
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>
01116105 37#include <linux/mount.h>
3ec3b2fb 38#include <linux/socket.h>
1da177e4
LT
39#include <linux/audit.h>
40#include <linux/personality.h>
41#include <linux/time.h>
42#include <asm/unistd.h>
43
44/* 0 = no checking
45 1 = put_count checking
46 2 = verbose put_count checking
47*/
48#define AUDIT_DEBUG 0
49
50/* No syscall auditing will take place unless audit_enabled != 0. */
51extern int audit_enabled;
52
53/* AUDIT_NAMES is the number of slots we reserve in the audit_context
54 * for saving names from getname(). */
55#define AUDIT_NAMES 20
56
57/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
58 * audit_context from being used for nameless inodes from
59 * path_lookup. */
60#define AUDIT_NAMES_RESERVED 7
61
62/* At task start time, the audit_state is set in the audit_context using
63 a per-task filter. At syscall entry, the audit_state is augmented by
64 the syscall filter. */
65enum audit_state {
66 AUDIT_DISABLED, /* Do not create per-task audit_context.
67 * No syscall-specific audit records can
68 * be generated. */
69 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
70 * but don't necessarily fill it in at
71 * syscall entry time (i.e., filter
72 * instead). */
73 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
74 * and always fill it in at syscall
75 * entry time. This makes a full
76 * syscall record available if some
77 * other part of the kernel decides it
78 * should be recorded. */
79 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
80 * always fill it in at syscall entry
81 * time, and always write out the audit
82 * record at syscall exit time. */
83};
84
85/* When fs/namei.c:getname() is called, we store the pointer in name and
86 * we don't let putname() free it (instead we free all of the saved
87 * pointers at syscall exit time).
88 *
89 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
90struct audit_names {
91 const char *name;
92 unsigned long ino;
93 dev_t dev;
94 umode_t mode;
95 uid_t uid;
96 gid_t gid;
97 dev_t rdev;
98};
99
100struct audit_aux_data {
101 struct audit_aux_data *next;
102 int type;
103};
104
105#define AUDIT_AUX_IPCPERM 0
106
107struct audit_aux_data_ipcctl {
108 struct audit_aux_data d;
109 struct ipc_perm p;
110 unsigned long qbytes;
111 uid_t uid;
112 gid_t gid;
113 mode_t mode;
114};
115
3ec3b2fb
DW
116struct audit_aux_data_socketcall {
117 struct audit_aux_data d;
118 int nargs;
119 unsigned long args[0];
120};
121
122struct audit_aux_data_sockaddr {
123 struct audit_aux_data d;
124 int len;
125 char a[0];
126};
127
01116105
SS
128struct audit_aux_data_path {
129 struct audit_aux_data d;
130 struct dentry *dentry;
131 struct vfsmount *mnt;
132};
1da177e4
LT
133
134/* The per-task audit context. */
135struct audit_context {
136 int in_syscall; /* 1 if task is in a syscall */
137 enum audit_state state;
138 unsigned int serial; /* serial number for record */
139 struct timespec ctime; /* time of syscall entry */
140 uid_t loginuid; /* login uid (identity) */
141 int major; /* syscall number */
142 unsigned long argv[4]; /* syscall arguments */
143 int return_valid; /* return code is valid */
2fd6f58b 144 long return_code;/* syscall return code */
1da177e4
LT
145 int auditable; /* 1 if record should be written */
146 int name_count;
147 struct audit_names names[AUDIT_NAMES];
8f37d47c
DW
148 struct dentry * pwd;
149 struct vfsmount * pwdmnt;
1da177e4
LT
150 struct audit_context *previous; /* For nested syscalls */
151 struct audit_aux_data *aux;
152
153 /* Save things to print about task_struct */
154 pid_t pid;
155 uid_t uid, euid, suid, fsuid;
156 gid_t gid, egid, sgid, fsgid;
157 unsigned long personality;
2fd6f58b 158 int arch;
1da177e4
LT
159
160#if AUDIT_DEBUG
161 int put_count;
162 int ino_count;
163#endif
164};
165
166 /* Public API */
167/* There are three lists of rules -- one to search at task creation
168 * time, one to search at syscall entry time, and another to search at
169 * syscall exit time. */
0f45aa18
DW
170static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
171 LIST_HEAD_INIT(audit_filter_list[0]),
172 LIST_HEAD_INIT(audit_filter_list[1]),
173 LIST_HEAD_INIT(audit_filter_list[2]),
174 LIST_HEAD_INIT(audit_filter_list[3]),
175 LIST_HEAD_INIT(audit_filter_list[4]),
176#if AUDIT_NR_FILTERS != 5
177#error Fix audit_filter_list initialiser
178#endif
179};
1da177e4
LT
180
181struct audit_entry {
182 struct list_head list;
183 struct rcu_head rcu;
184 struct audit_rule rule;
185};
186
7ca00264
DW
187extern int audit_pid;
188
1da177e4
LT
189/* Check to see if two rules are identical. It is called from
190 * audit_del_rule during AUDIT_DEL. */
191static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
192{
193 int i;
194
195 if (a->flags != b->flags)
196 return 1;
197
198 if (a->action != b->action)
199 return 1;
200
201 if (a->field_count != b->field_count)
202 return 1;
203
204 for (i = 0; i < a->field_count; i++) {
205 if (a->fields[i] != b->fields[i]
206 || a->values[i] != b->values[i])
207 return 1;
208 }
209
210 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
211 if (a->mask[i] != b->mask[i])
212 return 1;
213
214 return 0;
215}
216
217/* Note that audit_add_rule and audit_del_rule are called via
218 * audit_receive() in audit.c, and are protected by
219 * audit_netlink_sem. */
0f45aa18
DW
220static inline void audit_add_rule(struct audit_entry *entry,
221 struct list_head *list)
1da177e4 222{
0f45aa18
DW
223 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
224 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
1da177e4
LT
225 list_add_rcu(&entry->list, list);
226 } else {
227 list_add_tail_rcu(&entry->list, list);
228 }
1da177e4
LT
229}
230
231static void audit_free_rule(struct rcu_head *head)
232{
233 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
234 kfree(e);
235}
236
237/* Note that audit_add_rule and audit_del_rule are called via
238 * audit_receive() in audit.c, and are protected by
239 * audit_netlink_sem. */
240static inline int audit_del_rule(struct audit_rule *rule,
241 struct list_head *list)
242{
243 struct audit_entry *e;
244
245 /* Do not use the _rcu iterator here, since this is the only
246 * deletion routine. */
247 list_for_each_entry(e, list, list) {
248 if (!audit_compare_rule(rule, &e->rule)) {
249 list_del_rcu(&e->list);
250 call_rcu(&e->rcu, audit_free_rule);
251 return 0;
252 }
253 }
0f45aa18 254 return -ENOENT; /* No matching rule */
1da177e4
LT
255}
256
1da177e4
LT
257/* Copy rule from user-space to kernel-space. Called during
258 * AUDIT_ADD. */
259static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
260{
261 int i;
262
263 if (s->action != AUDIT_NEVER
264 && s->action != AUDIT_POSSIBLE
265 && s->action != AUDIT_ALWAYS)
266 return -1;
267 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
268 return -1;
0f45aa18
DW
269 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
270 return -1;
1da177e4
LT
271
272 d->flags = s->flags;
273 d->action = s->action;
274 d->field_count = s->field_count;
275 for (i = 0; i < d->field_count; i++) {
276 d->fields[i] = s->fields[i];
277 d->values[i] = s->values[i];
278 }
279 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
280 return 0;
281}
282
c94c257c
SH
283int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
284 uid_t loginuid)
1da177e4 285{
1da177e4
LT
286 struct audit_entry *entry;
287 int err = 0;
0f45aa18
DW
288 int i;
289 unsigned listnr;
1da177e4
LT
290
291 switch (type) {
292 case AUDIT_LIST:
293 /* The *_rcu iterators not needed here because we are
294 always called with audit_netlink_sem held. */
0f45aa18
DW
295 for (i=0; i<AUDIT_NR_FILTERS; i++) {
296 list_for_each_entry(entry, &audit_filter_list[i], list)
297 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
298 &entry->rule, sizeof(entry->rule));
299 }
1da177e4
LT
300 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
301 break;
302 case AUDIT_ADD:
303 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
304 return -ENOMEM;
305 if (audit_copy_rule(&entry->rule, data)) {
306 kfree(entry);
307 return -EINVAL;
308 }
0f45aa18
DW
309 listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
310 audit_add_rule(entry, &audit_filter_list[listnr]);
c0404993 311 audit_log(NULL, AUDIT_CONFIG_CHANGE,
bccf6ae0 312 "auid=%u added an audit rule\n", loginuid);
1da177e4
LT
313 break;
314 case AUDIT_DEL:
0f45aa18
DW
315 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
316 if (listnr >= AUDIT_NR_FILTERS)
317 return -EINVAL;
318
319 err = audit_del_rule(data, &audit_filter_list[listnr]);
320 if (!err)
321 audit_log(NULL, AUDIT_CONFIG_CHANGE,
322 "auid=%u removed an audit rule\n", loginuid);
1da177e4
LT
323 break;
324 default:
325 return -EINVAL;
326 }
327
328 return err;
329}
1da177e4
LT
330
331/* Compare a task_struct with an audit_rule. Return 1 on match, 0
332 * otherwise. */
333static int audit_filter_rules(struct task_struct *tsk,
334 struct audit_rule *rule,
335 struct audit_context *ctx,
336 enum audit_state *state)
337{
338 int i, j;
339
340 for (i = 0; i < rule->field_count; i++) {
341 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
342 u32 value = rule->values[i];
343 int result = 0;
344
345 switch (field) {
346 case AUDIT_PID:
347 result = (tsk->pid == value);
348 break;
349 case AUDIT_UID:
350 result = (tsk->uid == value);
351 break;
352 case AUDIT_EUID:
353 result = (tsk->euid == value);
354 break;
355 case AUDIT_SUID:
356 result = (tsk->suid == value);
357 break;
358 case AUDIT_FSUID:
359 result = (tsk->fsuid == value);
360 break;
361 case AUDIT_GID:
362 result = (tsk->gid == value);
363 break;
364 case AUDIT_EGID:
365 result = (tsk->egid == value);
366 break;
367 case AUDIT_SGID:
368 result = (tsk->sgid == value);
369 break;
370 case AUDIT_FSGID:
371 result = (tsk->fsgid == value);
372 break;
373 case AUDIT_PERS:
374 result = (tsk->personality == value);
375 break;
2fd6f58b 376 case AUDIT_ARCH:
377 if (ctx)
378 result = (ctx->arch == value);
379 break;
1da177e4
LT
380
381 case AUDIT_EXIT:
382 if (ctx && ctx->return_valid)
383 result = (ctx->return_code == value);
384 break;
385 case AUDIT_SUCCESS:
386 if (ctx && ctx->return_valid)
2fd6f58b 387 result = (ctx->return_valid == AUDITSC_SUCCESS);
1da177e4
LT
388 break;
389 case AUDIT_DEVMAJOR:
390 if (ctx) {
391 for (j = 0; j < ctx->name_count; j++) {
392 if (MAJOR(ctx->names[j].dev)==value) {
393 ++result;
394 break;
395 }
396 }
397 }
398 break;
399 case AUDIT_DEVMINOR:
400 if (ctx) {
401 for (j = 0; j < ctx->name_count; j++) {
402 if (MINOR(ctx->names[j].dev)==value) {
403 ++result;
404 break;
405 }
406 }
407 }
408 break;
409 case AUDIT_INODE:
410 if (ctx) {
411 for (j = 0; j < ctx->name_count; j++) {
412 if (ctx->names[j].ino == value) {
413 ++result;
414 break;
415 }
416 }
417 }
418 break;
419 case AUDIT_LOGINUID:
420 result = 0;
421 if (ctx)
422 result = (ctx->loginuid == value);
423 break;
424 case AUDIT_ARG0:
425 case AUDIT_ARG1:
426 case AUDIT_ARG2:
427 case AUDIT_ARG3:
428 if (ctx)
429 result = (ctx->argv[field-AUDIT_ARG0]==value);
430 break;
431 }
432
433 if (rule->fields[i] & AUDIT_NEGATE)
434 result = !result;
435 if (!result)
436 return 0;
437 }
438 switch (rule->action) {
439 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
440 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
441 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
442 }
443 return 1;
444}
445
446/* At process creation time, we can determine if system-call auditing is
447 * completely disabled for this task. Since we only have the task
448 * structure at this point, we can only check uid and gid.
449 */
450static enum audit_state audit_filter_task(struct task_struct *tsk)
451{
452 struct audit_entry *e;
453 enum audit_state state;
454
455 rcu_read_lock();
0f45aa18 456 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
1da177e4
LT
457 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
458 rcu_read_unlock();
459 return state;
460 }
461 }
462 rcu_read_unlock();
463 return AUDIT_BUILD_CONTEXT;
464}
465
466/* At syscall entry and exit time, this filter is called if the
467 * audit_state is not low enough that auditing cannot take place, but is
23f32d18 468 * also not high enough that we already know we have to write an audit
1da177e4
LT
469 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
470 */
471static enum audit_state audit_filter_syscall(struct task_struct *tsk,
472 struct audit_context *ctx,
473 struct list_head *list)
474{
475 struct audit_entry *e;
476 enum audit_state state;
477 int word = AUDIT_WORD(ctx->major);
478 int bit = AUDIT_BIT(ctx->major);
479
f7056d64
DW
480 if (audit_pid && ctx->pid == audit_pid)
481 return AUDIT_DISABLED;
482
1da177e4
LT
483 rcu_read_lock();
484 list_for_each_entry_rcu(e, list, list) {
485 if ((e->rule.mask[word] & bit) == bit
486 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
487 rcu_read_unlock();
488 return state;
489 }
490 }
491 rcu_read_unlock();
492 return AUDIT_BUILD_CONTEXT;
493}
494
0f45aa18
DW
495int audit_filter_user(struct task_struct *tsk, int type)
496{
497 struct audit_entry *e;
498 enum audit_state state;
499
f7056d64
DW
500 if (audit_pid && tsk->pid == audit_pid)
501 return AUDIT_DISABLED;
502
0f45aa18
DW
503 rcu_read_lock();
504 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
505 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
506 rcu_read_unlock();
507 return state != AUDIT_DISABLED;
508 }
509 }
510 rcu_read_unlock();
511 return 1; /* Audit by default */
512
513}
514
1da177e4
LT
515/* This should be called with task_lock() held. */
516static inline struct audit_context *audit_get_context(struct task_struct *tsk,
517 int return_valid,
518 int return_code)
519{
520 struct audit_context *context = tsk->audit_context;
521
522 if (likely(!context))
523 return NULL;
524 context->return_valid = return_valid;
525 context->return_code = return_code;
526
527 if (context->in_syscall && !context->auditable) {
528 enum audit_state state;
0f45aa18 529 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
1da177e4
LT
530 if (state == AUDIT_RECORD_CONTEXT)
531 context->auditable = 1;
532 }
533
534 context->pid = tsk->pid;
535 context->uid = tsk->uid;
536 context->gid = tsk->gid;
537 context->euid = tsk->euid;
538 context->suid = tsk->suid;
539 context->fsuid = tsk->fsuid;
540 context->egid = tsk->egid;
541 context->sgid = tsk->sgid;
542 context->fsgid = tsk->fsgid;
543 context->personality = tsk->personality;
544 tsk->audit_context = NULL;
545 return context;
546}
547
548static inline void audit_free_names(struct audit_context *context)
549{
550 int i;
551
552#if AUDIT_DEBUG == 2
553 if (context->auditable
554 ||context->put_count + context->ino_count != context->name_count) {
555 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
556 " name_count=%d put_count=%d"
557 " ino_count=%d [NOT freeing]\n",
558 __LINE__,
559 context->serial, context->major, context->in_syscall,
560 context->name_count, context->put_count,
561 context->ino_count);
562 for (i = 0; i < context->name_count; i++)
563 printk(KERN_ERR "names[%d] = %p = %s\n", i,
564 context->names[i].name,
565 context->names[i].name);
566 dump_stack();
567 return;
568 }
569#endif
570#if AUDIT_DEBUG
571 context->put_count = 0;
572 context->ino_count = 0;
573#endif
574
575 for (i = 0; i < context->name_count; i++)
576 if (context->names[i].name)
577 __putname(context->names[i].name);
578 context->name_count = 0;
8f37d47c
DW
579 if (context->pwd)
580 dput(context->pwd);
581 if (context->pwdmnt)
582 mntput(context->pwdmnt);
583 context->pwd = NULL;
584 context->pwdmnt = NULL;
1da177e4
LT
585}
586
587static inline void audit_free_aux(struct audit_context *context)
588{
589 struct audit_aux_data *aux;
590
591 while ((aux = context->aux)) {
01116105
SS
592 if (aux->type == AUDIT_AVC_PATH) {
593 struct audit_aux_data_path *axi = (void *)aux;
594 dput(axi->dentry);
595 mntput(axi->mnt);
596 }
1da177e4
LT
597 context->aux = aux->next;
598 kfree(aux);
599 }
600}
601
602static inline void audit_zero_context(struct audit_context *context,
603 enum audit_state state)
604{
605 uid_t loginuid = context->loginuid;
606
607 memset(context, 0, sizeof(*context));
608 context->state = state;
609 context->loginuid = loginuid;
610}
611
612static inline struct audit_context *audit_alloc_context(enum audit_state state)
613{
614 struct audit_context *context;
615
616 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
617 return NULL;
618 audit_zero_context(context, state);
619 return context;
620}
621
622/* Filter on the task information and allocate a per-task audit context
623 * if necessary. Doing so turns on system call auditing for the
624 * specified task. This is called from copy_process, so no lock is
625 * needed. */
626int audit_alloc(struct task_struct *tsk)
627{
628 struct audit_context *context;
629 enum audit_state state;
630
631 if (likely(!audit_enabled))
632 return 0; /* Return if not auditing. */
633
634 state = audit_filter_task(tsk);
635 if (likely(state == AUDIT_DISABLED))
636 return 0;
637
638 if (!(context = audit_alloc_context(state))) {
639 audit_log_lost("out of memory in audit_alloc");
640 return -ENOMEM;
641 }
642
643 /* Preserve login uid */
644 context->loginuid = -1;
645 if (current->audit_context)
646 context->loginuid = current->audit_context->loginuid;
647
648 tsk->audit_context = context;
649 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
650 return 0;
651}
652
653static inline void audit_free_context(struct audit_context *context)
654{
655 struct audit_context *previous;
656 int count = 0;
657
658 do {
659 previous = context->previous;
660 if (previous || (count && count < 10)) {
661 ++count;
662 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
663 " freeing multiple contexts (%d)\n",
664 context->serial, context->major,
665 context->name_count, count);
666 }
667 audit_free_names(context);
668 audit_free_aux(context);
669 kfree(context);
670 context = previous;
671 } while (context);
672 if (count >= 10)
673 printk(KERN_ERR "audit: freed %d contexts\n", count);
674}
675
219f0817
SS
676static void audit_log_task_info(struct audit_buffer *ab)
677{
678 char name[sizeof(current->comm)];
679 struct mm_struct *mm = current->mm;
680 struct vm_area_struct *vma;
681
682 get_task_comm(name, current);
99e45eea
DW
683 audit_log_format(ab, " comm=");
684 audit_log_untrustedstring(ab, name);
219f0817
SS
685
686 if (!mm)
687 return;
688
689 down_read(&mm->mmap_sem);
690 vma = mm->mmap;
691 while (vma) {
692 if ((vma->vm_flags & VM_EXECUTABLE) &&
693 vma->vm_file) {
694 audit_log_d_path(ab, "exe=",
695 vma->vm_file->f_dentry,
696 vma->vm_file->f_vfsmnt);
697 break;
698 }
699 vma = vma->vm_next;
700 }
701 up_read(&mm->mmap_sem);
702}
703
1da177e4
LT
704static void audit_log_exit(struct audit_context *context)
705{
706 int i;
707 struct audit_buffer *ab;
7551ced3 708 struct audit_aux_data *aux;
1da177e4 709
c0404993 710 ab = audit_log_start(context, AUDIT_SYSCALL);
1da177e4
LT
711 if (!ab)
712 return; /* audit_panic has been called */
bccf6ae0
DW
713 audit_log_format(ab, "arch=%x syscall=%d",
714 context->arch, context->major);
1da177e4
LT
715 if (context->personality != PER_LINUX)
716 audit_log_format(ab, " per=%lx", context->personality);
717 if (context->return_valid)
2fd6f58b 718 audit_log_format(ab, " success=%s exit=%ld",
719 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
720 context->return_code);
1da177e4
LT
721 audit_log_format(ab,
722 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
326e9c8b
SG
723 " pid=%d auid=%u uid=%u gid=%u"
724 " euid=%u suid=%u fsuid=%u"
725 " egid=%u sgid=%u fsgid=%u",
1da177e4
LT
726 context->argv[0],
727 context->argv[1],
728 context->argv[2],
729 context->argv[3],
730 context->name_count,
731 context->pid,
732 context->loginuid,
733 context->uid,
734 context->gid,
735 context->euid, context->suid, context->fsuid,
736 context->egid, context->sgid, context->fsgid);
219f0817 737 audit_log_task_info(ab);
1da177e4 738 audit_log_end(ab);
1da177e4 739
7551ced3 740 for (aux = context->aux; aux; aux = aux->next) {
c0404993
SG
741
742 ab = audit_log_start(context, aux->type);
1da177e4
LT
743 if (!ab)
744 continue; /* audit_panic has been called */
745
1da177e4 746 switch (aux->type) {
c0404993 747 case AUDIT_IPC: {
1da177e4
LT
748 struct audit_aux_data_ipcctl *axi = (void *)aux;
749 audit_log_format(ab,
326e9c8b 750 " qbytes=%lx iuid=%u igid=%u mode=%x",
1da177e4 751 axi->qbytes, axi->uid, axi->gid, axi->mode);
3ec3b2fb
DW
752 break; }
753
754 case AUDIT_SOCKETCALL: {
755 int i;
756 struct audit_aux_data_socketcall *axs = (void *)aux;
757 audit_log_format(ab, "nargs=%d", axs->nargs);
758 for (i=0; i<axs->nargs; i++)
759 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
760 break; }
761
762 case AUDIT_SOCKADDR: {
763 struct audit_aux_data_sockaddr *axs = (void *)aux;
764
765 audit_log_format(ab, "saddr=");
766 audit_log_hex(ab, axs->a, axs->len);
767 break; }
01116105
SS
768
769 case AUDIT_AVC_PATH: {
770 struct audit_aux_data_path *axi = (void *)aux;
771 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
01116105
SS
772 break; }
773
1da177e4
LT
774 }
775 audit_log_end(ab);
1da177e4
LT
776 }
777
8f37d47c
DW
778 if (context->pwd && context->pwdmnt) {
779 ab = audit_log_start(context, AUDIT_CWD);
780 if (ab) {
781 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
782 audit_log_end(ab);
783 }
784 }
1da177e4 785 for (i = 0; i < context->name_count; i++) {
c0404993 786 ab = audit_log_start(context, AUDIT_PATH);
1da177e4
LT
787 if (!ab)
788 continue; /* audit_panic has been called */
8f37d47c 789
1da177e4 790 audit_log_format(ab, "item=%d", i);
83c7d091 791 if (context->names[i].name) {
792 audit_log_format(ab, " name=");
793 audit_log_untrustedstring(ab, context->names[i].name);
794 }
1da177e4
LT
795 if (context->names[i].ino != (unsigned long)-1)
796 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
326e9c8b 797 " ouid=%u ogid=%u rdev=%02x:%02x",
1da177e4
LT
798 context->names[i].ino,
799 MAJOR(context->names[i].dev),
800 MINOR(context->names[i].dev),
801 context->names[i].mode,
802 context->names[i].uid,
803 context->names[i].gid,
804 MAJOR(context->names[i].rdev),
805 MINOR(context->names[i].rdev));
806 audit_log_end(ab);
807 }
808}
809
810/* Free a per-task audit context. Called from copy_process and
811 * __put_task_struct. */
812void audit_free(struct task_struct *tsk)
813{
814 struct audit_context *context;
815
816 task_lock(tsk);
817 context = audit_get_context(tsk, 0, 0);
818 task_unlock(tsk);
819
820 if (likely(!context))
821 return;
822
823 /* Check for system calls that do not go through the exit
824 * function (e.g., exit_group), then free context block. */
f7056d64 825 if (context->in_syscall && context->auditable)
1da177e4
LT
826 audit_log_exit(context);
827
828 audit_free_context(context);
829}
830
1da177e4
LT
831/* Fill in audit context at syscall entry. This only happens if the
832 * audit context was created when the task was created and the state or
833 * filters demand the audit context be built. If the state from the
834 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
835 * then the record will be written at syscall exit time (otherwise, it
836 * will only be written if another part of the kernel requests that it
837 * be written). */
2fd6f58b 838void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
1da177e4
LT
839 unsigned long a1, unsigned long a2,
840 unsigned long a3, unsigned long a4)
841{
842 struct audit_context *context = tsk->audit_context;
843 enum audit_state state;
844
845 BUG_ON(!context);
846
847 /* This happens only on certain architectures that make system
848 * calls in kernel_thread via the entry.S interface, instead of
849 * with direct calls. (If you are porting to a new
850 * architecture, hitting this condition can indicate that you
851 * got the _exit/_leave calls backward in entry.S.)
852 *
853 * i386 no
854 * x86_64 no
855 * ppc64 yes (see arch/ppc64/kernel/misc.S)
856 *
857 * This also happens with vm86 emulation in a non-nested manner
858 * (entries without exits), so this case must be caught.
859 */
860 if (context->in_syscall) {
861 struct audit_context *newctx;
862
863#if defined(__NR_vm86) && defined(__NR_vm86old)
864 /* vm86 mode should only be entered once */
865 if (major == __NR_vm86 || major == __NR_vm86old)
866 return;
867#endif
868#if AUDIT_DEBUG
869 printk(KERN_ERR
870 "audit(:%d) pid=%d in syscall=%d;"
871 " entering syscall=%d\n",
872 context->serial, tsk->pid, context->major, major);
873#endif
874 newctx = audit_alloc_context(context->state);
875 if (newctx) {
876 newctx->previous = context;
877 context = newctx;
878 tsk->audit_context = newctx;
879 } else {
880 /* If we can't alloc a new context, the best we
881 * can do is to leak memory (any pending putname
882 * will be lost). The only other alternative is
883 * to abandon auditing. */
884 audit_zero_context(context, context->state);
885 }
886 }
887 BUG_ON(context->in_syscall || context->name_count);
888
889 if (!audit_enabled)
890 return;
891
2fd6f58b 892 context->arch = arch;
1da177e4
LT
893 context->major = major;
894 context->argv[0] = a1;
895 context->argv[1] = a2;
896 context->argv[2] = a3;
897 context->argv[3] = a4;
898
899 state = context->state;
900 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
0f45aa18 901 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1da177e4
LT
902 if (likely(state == AUDIT_DISABLED))
903 return;
904
905 context->serial = audit_serial();
906 context->ctime = CURRENT_TIME;
907 context->in_syscall = 1;
908 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
909}
910
911/* Tear down after system call. If the audit context has been marked as
912 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
913 * filtering, or because some other part of the kernel write an audit
914 * message), then write out the syscall information. In call cases,
915 * free the names stored from getname(). */
2fd6f58b 916void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1da177e4
LT
917{
918 struct audit_context *context;
919
920 get_task_struct(tsk);
921 task_lock(tsk);
2fd6f58b 922 context = audit_get_context(tsk, valid, return_code);
1da177e4
LT
923 task_unlock(tsk);
924
925 /* Not having a context here is ok, since the parent may have
926 * called __put_task_struct. */
927 if (likely(!context))
928 return;
929
f7056d64 930 if (context->in_syscall && context->auditable)
1da177e4
LT
931 audit_log_exit(context);
932
933 context->in_syscall = 0;
934 context->auditable = 0;
2fd6f58b 935
1da177e4
LT
936 if (context->previous) {
937 struct audit_context *new_context = context->previous;
938 context->previous = NULL;
939 audit_free_context(context);
940 tsk->audit_context = new_context;
941 } else {
942 audit_free_names(context);
943 audit_free_aux(context);
944 audit_zero_context(context, context->state);
945 tsk->audit_context = context;
946 }
947 put_task_struct(tsk);
948}
949
950/* Add a name to the list. Called from fs/namei.c:getname(). */
951void audit_getname(const char *name)
952{
953 struct audit_context *context = current->audit_context;
954
955 if (!context || IS_ERR(name) || !name)
956 return;
957
958 if (!context->in_syscall) {
959#if AUDIT_DEBUG == 2
960 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
961 __FILE__, __LINE__, context->serial, name);
962 dump_stack();
963#endif
964 return;
965 }
966 BUG_ON(context->name_count >= AUDIT_NAMES);
967 context->names[context->name_count].name = name;
968 context->names[context->name_count].ino = (unsigned long)-1;
969 ++context->name_count;
8f37d47c
DW
970 if (!context->pwd) {
971 read_lock(&current->fs->lock);
972 context->pwd = dget(current->fs->pwd);
973 context->pwdmnt = mntget(current->fs->pwdmnt);
974 read_unlock(&current->fs->lock);
975 }
976
1da177e4
LT
977}
978
979/* Intercept a putname request. Called from
980 * include/linux/fs.h:putname(). If we have stored the name from
981 * getname in the audit context, then we delay the putname until syscall
982 * exit. */
983void audit_putname(const char *name)
984{
985 struct audit_context *context = current->audit_context;
986
987 BUG_ON(!context);
988 if (!context->in_syscall) {
989#if AUDIT_DEBUG == 2
990 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
991 __FILE__, __LINE__, context->serial, name);
992 if (context->name_count) {
993 int i;
994 for (i = 0; i < context->name_count; i++)
995 printk(KERN_ERR "name[%d] = %p = %s\n", i,
996 context->names[i].name,
997 context->names[i].name);
998 }
999#endif
1000 __putname(name);
1001 }
1002#if AUDIT_DEBUG
1003 else {
1004 ++context->put_count;
1005 if (context->put_count > context->name_count) {
1006 printk(KERN_ERR "%s:%d(:%d): major=%d"
1007 " in_syscall=%d putname(%p) name_count=%d"
1008 " put_count=%d\n",
1009 __FILE__, __LINE__,
1010 context->serial, context->major,
1011 context->in_syscall, name, context->name_count,
1012 context->put_count);
1013 dump_stack();
1014 }
1015 }
1016#endif
1017}
1018
1019/* Store the inode and device from a lookup. Called from
1020 * fs/namei.c:path_lookup(). */
1021void audit_inode(const char *name, const struct inode *inode)
1022{
1023 int idx;
1024 struct audit_context *context = current->audit_context;
1025
1026 if (!context->in_syscall)
1027 return;
1028 if (context->name_count
1029 && context->names[context->name_count-1].name
1030 && context->names[context->name_count-1].name == name)
1031 idx = context->name_count - 1;
1032 else if (context->name_count > 1
1033 && context->names[context->name_count-2].name
1034 && context->names[context->name_count-2].name == name)
1035 idx = context->name_count - 2;
1036 else {
1037 /* FIXME: how much do we care about inodes that have no
1038 * associated name? */
1039 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1040 return;
1041 idx = context->name_count++;
1042 context->names[idx].name = NULL;
1043#if AUDIT_DEBUG
1044 ++context->ino_count;
1045#endif
1046 }
1047 context->names[idx].ino = inode->i_ino;
1048 context->names[idx].dev = inode->i_sb->s_dev;
1049 context->names[idx].mode = inode->i_mode;
1050 context->names[idx].uid = inode->i_uid;
1051 context->names[idx].gid = inode->i_gid;
1052 context->names[idx].rdev = inode->i_rdev;
1053}
1054
bfb4496e
DW
1055void auditsc_get_stamp(struct audit_context *ctx,
1056 struct timespec *t, unsigned int *serial)
1da177e4 1057{
bfb4496e
DW
1058 t->tv_sec = ctx->ctime.tv_sec;
1059 t->tv_nsec = ctx->ctime.tv_nsec;
1060 *serial = ctx->serial;
1061 ctx->auditable = 1;
1da177e4
LT
1062}
1063
456be6cd 1064int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1da177e4 1065{
456be6cd 1066 if (task->audit_context) {
c0404993
SG
1067 struct audit_buffer *ab;
1068
1069 ab = audit_log_start(NULL, AUDIT_LOGIN);
1070 if (ab) {
1071 audit_log_format(ab, "login pid=%d uid=%u "
326e9c8b 1072 "old auid=%u new auid=%u",
c0404993
SG
1073 task->pid, task->uid,
1074 task->audit_context->loginuid, loginuid);
1075 audit_log_end(ab);
1076 }
456be6cd 1077 task->audit_context->loginuid = loginuid;
1da177e4
LT
1078 }
1079 return 0;
1080}
1081
1082uid_t audit_get_loginuid(struct audit_context *ctx)
1083{
1084 return ctx ? ctx->loginuid : -1;
1085}
1086
1087int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1088{
1089 struct audit_aux_data_ipcctl *ax;
1090 struct audit_context *context = current->audit_context;
1091
1092 if (likely(!context))
1093 return 0;
1094
1095 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1096 if (!ax)
1097 return -ENOMEM;
1098
1099 ax->qbytes = qbytes;
1100 ax->uid = uid;
1101 ax->gid = gid;
1102 ax->mode = mode;
1103
c0404993 1104 ax->d.type = AUDIT_IPC;
1da177e4
LT
1105 ax->d.next = context->aux;
1106 context->aux = (void *)ax;
1107 return 0;
1108}
c2f0c7c3 1109
3ec3b2fb
DW
1110int audit_socketcall(int nargs, unsigned long *args)
1111{
1112 struct audit_aux_data_socketcall *ax;
1113 struct audit_context *context = current->audit_context;
1114
1115 if (likely(!context))
1116 return 0;
1117
1118 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1119 if (!ax)
1120 return -ENOMEM;
1121
1122 ax->nargs = nargs;
1123 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1124
1125 ax->d.type = AUDIT_SOCKETCALL;
1126 ax->d.next = context->aux;
1127 context->aux = (void *)ax;
1128 return 0;
1129}
1130
1131int audit_sockaddr(int len, void *a)
1132{
1133 struct audit_aux_data_sockaddr *ax;
1134 struct audit_context *context = current->audit_context;
1135
1136 if (likely(!context))
1137 return 0;
1138
1139 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1140 if (!ax)
1141 return -ENOMEM;
1142
1143 ax->len = len;
1144 memcpy(ax->a, a, len);
1145
1146 ax->d.type = AUDIT_SOCKADDR;
1147 ax->d.next = context->aux;
1148 context->aux = (void *)ax;
1149 return 0;
1150}
1151
01116105
SS
1152int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1153{
1154 struct audit_aux_data_path *ax;
1155 struct audit_context *context = current->audit_context;
1156
1157 if (likely(!context))
1158 return 0;
1159
1160 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1161 if (!ax)
1162 return -ENOMEM;
1163
1164 ax->dentry = dget(dentry);
1165 ax->mnt = mntget(mnt);
1166
1167 ax->d.type = AUDIT_AVC_PATH;
1168 ax->d.next = context->aux;
1169 context->aux = (void *)ax;
1170 return 0;
1171}
1172
c2f0c7c3
SG
1173void audit_signal_info(int sig, struct task_struct *t)
1174{
1175 extern pid_t audit_sig_pid;
1176 extern uid_t audit_sig_uid;
c2f0c7c3
SG
1177
1178 if (unlikely(audit_pid && t->pid == audit_pid)) {
1179 if (sig == SIGTERM || sig == SIGHUP) {
1180 struct audit_context *ctx = current->audit_context;
1181 audit_sig_pid = current->pid;
1182 if (ctx)
1183 audit_sig_uid = ctx->loginuid;
1184 else
1185 audit_sig_uid = current->uid;
1186 }
1187 }
1188}
1189