1 /* auditsc.c -- System-call auditing support
2 * Handles all system-call specific auditing features.
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
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.
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.
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
21 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23 * Many of the ideas implemented here are from Stephen C. Tweedie,
24 * especially the idea of avoiding a copy by using getname.
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.
32 #include <linux/init.h>
33 #include <asm/types.h>
34 #include <asm/atomic.h>
36 #include <linux/module.h>
37 #include <linux/mount.h>
38 #include <linux/socket.h>
39 #include <linux/audit.h>
40 #include <linux/personality.h>
41 #include <linux/time.h>
42 #include <linux/kthread.h>
43 #include <linux/netlink.h>
44 #include <linux/compiler.h>
45 #include <asm/unistd.h>
48 1 = put_count checking
49 2 = verbose put_count checking
53 /* No syscall auditing will take place unless audit_enabled != 0. */
54 extern int audit_enabled;
56 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
57 * for saving names from getname(). */
58 #define AUDIT_NAMES 20
60 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
61 * audit_context from being used for nameless inodes from
63 #define AUDIT_NAMES_RESERVED 7
65 /* At task start time, the audit_state is set in the audit_context using
66 a per-task filter. At syscall entry, the audit_state is augmented by
67 the syscall filter. */
69 AUDIT_DISABLED, /* Do not create per-task audit_context.
70 * No syscall-specific audit records can
72 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
73 * but don't necessarily fill it in at
74 * syscall entry time (i.e., filter
76 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
77 * and always fill it in at syscall
78 * entry time. This makes a full
79 * syscall record available if some
80 * other part of the kernel decides it
81 * should be recorded. */
82 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
83 * always fill it in at syscall entry
84 * time, and always write out the audit
85 * record at syscall exit time. */
88 /* When fs/namei.c:getname() is called, we store the pointer in name and
89 * we don't let putname() free it (instead we free all of the saved
90 * pointers at syscall exit time).
92 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
104 struct audit_aux_data {
105 struct audit_aux_data *next;
109 #define AUDIT_AUX_IPCPERM 0
111 struct audit_aux_data_ipcctl {
112 struct audit_aux_data d;
114 unsigned long qbytes;
120 struct audit_aux_data_socketcall {
121 struct audit_aux_data d;
123 unsigned long args[0];
126 struct audit_aux_data_sockaddr {
127 struct audit_aux_data d;
132 struct audit_aux_data_path {
133 struct audit_aux_data d;
134 struct dentry *dentry;
135 struct vfsmount *mnt;
138 /* The per-task audit context. */
139 struct audit_context {
140 int in_syscall; /* 1 if task is in a syscall */
141 enum audit_state state;
142 unsigned int serial; /* serial number for record */
143 struct timespec ctime; /* time of syscall entry */
144 uid_t loginuid; /* login uid (identity) */
145 int major; /* syscall number */
146 unsigned long argv[4]; /* syscall arguments */
147 int return_valid; /* return code is valid */
148 long return_code;/* syscall return code */
149 int auditable; /* 1 if record should be written */
151 struct audit_names names[AUDIT_NAMES];
153 struct vfsmount * pwdmnt;
154 struct audit_context *previous; /* For nested syscalls */
155 struct audit_aux_data *aux;
157 /* Save things to print about task_struct */
159 uid_t uid, euid, suid, fsuid;
160 gid_t gid, egid, sgid, fsgid;
161 unsigned long personality;
171 /* There are three lists of rules -- one to search at task creation
172 * time, one to search at syscall entry time, and another to search at
173 * syscall exit time. */
174 static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
175 LIST_HEAD_INIT(audit_filter_list[0]),
176 LIST_HEAD_INIT(audit_filter_list[1]),
177 LIST_HEAD_INIT(audit_filter_list[2]),
178 LIST_HEAD_INIT(audit_filter_list[3]),
179 LIST_HEAD_INIT(audit_filter_list[4]),
180 #if AUDIT_NR_FILTERS != 5
181 #error Fix audit_filter_list initialiser
186 struct list_head list;
188 struct audit_rule rule;
191 extern int audit_pid;
193 /* Copy rule from user-space to kernel-space. Called from
194 * audit_add_rule during AUDIT_ADD. */
195 static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
199 if (s->action != AUDIT_NEVER
200 && s->action != AUDIT_POSSIBLE
201 && s->action != AUDIT_ALWAYS)
203 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
205 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
209 d->action = s->action;
210 d->field_count = s->field_count;
211 for (i = 0; i < d->field_count; i++) {
212 d->fields[i] = s->fields[i];
213 d->values[i] = s->values[i];
215 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
219 /* Check to see if two rules are identical. It is called from
220 * audit_add_rule during AUDIT_ADD and
221 * audit_del_rule during AUDIT_DEL. */
222 static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
226 if (a->flags != b->flags)
229 if (a->action != b->action)
232 if (a->field_count != b->field_count)
235 for (i = 0; i < a->field_count; i++) {
236 if (a->fields[i] != b->fields[i]
237 || a->values[i] != b->values[i])
241 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
242 if (a->mask[i] != b->mask[i])
248 /* Note that audit_add_rule and audit_del_rule are called via
249 * audit_receive() in audit.c, and are protected by
250 * audit_netlink_sem. */
251 static inline int audit_add_rule(struct audit_rule *rule,
252 struct list_head *list)
254 struct audit_entry *entry;
256 /* Do not use the _rcu iterator here, since this is the only
257 * addition routine. */
258 list_for_each_entry(entry, list, list) {
259 if (!audit_compare_rule(rule, &entry->rule)) {
264 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
266 if (audit_copy_rule(&entry->rule, rule)) {
271 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
272 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
273 list_add_rcu(&entry->list, list);
275 list_add_tail_rcu(&entry->list, list);
281 static inline void audit_free_rule(struct rcu_head *head)
283 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
287 /* Note that audit_add_rule and audit_del_rule are called via
288 * audit_receive() in audit.c, and are protected by
289 * audit_netlink_sem. */
290 static inline int audit_del_rule(struct audit_rule *rule,
291 struct list_head *list)
293 struct audit_entry *e;
295 /* Do not use the _rcu iterator here, since this is the only
296 * deletion routine. */
297 list_for_each_entry(e, list, list) {
298 if (!audit_compare_rule(rule, &e->rule)) {
299 list_del_rcu(&e->list);
300 call_rcu(&e->rcu, audit_free_rule);
304 return -ENOENT; /* No matching rule */
307 static int audit_list_rules(void *_dest)
311 struct audit_entry *entry;
318 down(&audit_netlink_sem);
320 /* The *_rcu iterators not needed here because we are
321 always called with audit_netlink_sem held. */
322 for (i=0; i<AUDIT_NR_FILTERS; i++) {
323 list_for_each_entry(entry, &audit_filter_list[i], list)
324 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
325 &entry->rule, sizeof(entry->rule));
327 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
329 up(&audit_netlink_sem);
333 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
336 struct task_struct *tsk;
343 /* We can't just spew out the rules here because we might fill
344 * the available socket buffer space and deadlock waiting for
345 * auditctl to read from it... which isn't ever going to
346 * happen if we're actually running in the context of auditctl
347 * trying to _send_ the stuff */
349 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
355 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
362 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
363 if (listnr >= AUDIT_NR_FILTERS)
366 err = audit_add_rule(data, &audit_filter_list[listnr]);
368 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
369 "auid=%u added an audit rule\n", loginuid);
372 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
373 if (listnr >= AUDIT_NR_FILTERS)
376 err = audit_del_rule(data, &audit_filter_list[listnr]);
378 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
379 "auid=%u removed an audit rule\n", loginuid);
388 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
390 static int audit_filter_rules(struct task_struct *tsk,
391 struct audit_rule *rule,
392 struct audit_context *ctx,
393 enum audit_state *state)
397 for (i = 0; i < rule->field_count; i++) {
398 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
399 u32 value = rule->values[i];
404 result = (tsk->pid == value);
407 result = (tsk->uid == value);
410 result = (tsk->euid == value);
413 result = (tsk->suid == value);
416 result = (tsk->fsuid == value);
419 result = (tsk->gid == value);
422 result = (tsk->egid == value);
425 result = (tsk->sgid == value);
428 result = (tsk->fsgid == value);
431 result = (tsk->personality == value);
435 result = (ctx->arch == value);
439 if (ctx && ctx->return_valid)
440 result = (ctx->return_code == value);
443 if (ctx && ctx->return_valid) {
445 result = (ctx->return_valid == AUDITSC_SUCCESS);
447 result = (ctx->return_valid == AUDITSC_FAILURE);
452 for (j = 0; j < ctx->name_count; j++) {
453 if (MAJOR(ctx->names[j].dev)==value) {
462 for (j = 0; j < ctx->name_count; j++) {
463 if (MINOR(ctx->names[j].dev)==value) {
472 for (j = 0; j < ctx->name_count; j++) {
473 if (ctx->names[j].ino == value) {
483 result = (ctx->loginuid == value);
490 result = (ctx->argv[field-AUDIT_ARG0]==value);
494 if (rule->fields[i] & AUDIT_NEGATE)
499 switch (rule->action) {
500 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
501 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
502 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
507 /* At process creation time, we can determine if system-call auditing is
508 * completely disabled for this task. Since we only have the task
509 * structure at this point, we can only check uid and gid.
511 static enum audit_state audit_filter_task(struct task_struct *tsk)
513 struct audit_entry *e;
514 enum audit_state state;
517 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
518 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
524 return AUDIT_BUILD_CONTEXT;
527 /* At syscall entry and exit time, this filter is called if the
528 * audit_state is not low enough that auditing cannot take place, but is
529 * also not high enough that we already know we have to write an audit
530 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
532 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
533 struct audit_context *ctx,
534 struct list_head *list)
536 struct audit_entry *e;
537 enum audit_state state;
539 if (audit_pid && tsk->tgid == audit_pid)
540 return AUDIT_DISABLED;
543 if (!list_empty(list)) {
544 int word = AUDIT_WORD(ctx->major);
545 int bit = AUDIT_BIT(ctx->major);
547 list_for_each_entry_rcu(e, list, list) {
548 if ((e->rule.mask[word] & bit) == bit
549 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
556 return AUDIT_BUILD_CONTEXT;
559 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
560 struct audit_rule *rule,
561 enum audit_state *state)
565 for (i = 0; i < rule->field_count; i++) {
566 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
567 u32 value = rule->values[i];
572 result = (cb->creds.pid == value);
575 result = (cb->creds.uid == value);
578 result = (cb->creds.gid == value);
581 result = (cb->loginuid == value);
585 if (rule->fields[i] & AUDIT_NEGATE)
590 switch (rule->action) {
591 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
592 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
593 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
598 int audit_filter_user(struct netlink_skb_parms *cb, int type)
600 struct audit_entry *e;
601 enum audit_state state;
605 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
606 if (audit_filter_user_rules(cb, &e->rule, &state)) {
607 if (state == AUDIT_DISABLED)
614 return ret; /* Audit by default */
617 /* This should be called with task_lock() held. */
618 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
622 struct audit_context *context = tsk->audit_context;
624 if (likely(!context))
626 context->return_valid = return_valid;
627 context->return_code = return_code;
629 if (context->in_syscall && !context->auditable) {
630 enum audit_state state;
631 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
632 if (state == AUDIT_RECORD_CONTEXT)
633 context->auditable = 1;
636 context->pid = tsk->pid;
637 context->uid = tsk->uid;
638 context->gid = tsk->gid;
639 context->euid = tsk->euid;
640 context->suid = tsk->suid;
641 context->fsuid = tsk->fsuid;
642 context->egid = tsk->egid;
643 context->sgid = tsk->sgid;
644 context->fsgid = tsk->fsgid;
645 context->personality = tsk->personality;
646 tsk->audit_context = NULL;
650 static inline void audit_free_names(struct audit_context *context)
655 if (context->auditable
656 ||context->put_count + context->ino_count != context->name_count) {
657 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
658 " name_count=%d put_count=%d"
659 " ino_count=%d [NOT freeing]\n",
661 context->serial, context->major, context->in_syscall,
662 context->name_count, context->put_count,
664 for (i = 0; i < context->name_count; i++)
665 printk(KERN_ERR "names[%d] = %p = %s\n", i,
666 context->names[i].name,
667 context->names[i].name);
673 context->put_count = 0;
674 context->ino_count = 0;
677 for (i = 0; i < context->name_count; i++)
678 if (context->names[i].name)
679 __putname(context->names[i].name);
680 context->name_count = 0;
684 mntput(context->pwdmnt);
686 context->pwdmnt = NULL;
689 static inline void audit_free_aux(struct audit_context *context)
691 struct audit_aux_data *aux;
693 while ((aux = context->aux)) {
694 if (aux->type == AUDIT_AVC_PATH) {
695 struct audit_aux_data_path *axi = (void *)aux;
699 context->aux = aux->next;
704 static inline void audit_zero_context(struct audit_context *context,
705 enum audit_state state)
707 uid_t loginuid = context->loginuid;
709 memset(context, 0, sizeof(*context));
710 context->state = state;
711 context->loginuid = loginuid;
714 static inline struct audit_context *audit_alloc_context(enum audit_state state)
716 struct audit_context *context;
718 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
720 audit_zero_context(context, state);
724 /* Filter on the task information and allocate a per-task audit context
725 * if necessary. Doing so turns on system call auditing for the
726 * specified task. This is called from copy_process, so no lock is
728 int audit_alloc(struct task_struct *tsk)
730 struct audit_context *context;
731 enum audit_state state;
733 if (likely(!audit_enabled))
734 return 0; /* Return if not auditing. */
736 state = audit_filter_task(tsk);
737 if (likely(state == AUDIT_DISABLED))
740 if (!(context = audit_alloc_context(state))) {
741 audit_log_lost("out of memory in audit_alloc");
745 /* Preserve login uid */
746 context->loginuid = -1;
747 if (current->audit_context)
748 context->loginuid = current->audit_context->loginuid;
750 tsk->audit_context = context;
751 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
755 static inline void audit_free_context(struct audit_context *context)
757 struct audit_context *previous;
761 previous = context->previous;
762 if (previous || (count && count < 10)) {
764 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
765 " freeing multiple contexts (%d)\n",
766 context->serial, context->major,
767 context->name_count, count);
769 audit_free_names(context);
770 audit_free_aux(context);
775 printk(KERN_ERR "audit: freed %d contexts\n", count);
778 static void audit_log_task_info(struct audit_buffer *ab)
780 char name[sizeof(current->comm)];
781 struct mm_struct *mm = current->mm;
782 struct vm_area_struct *vma;
784 get_task_comm(name, current);
785 audit_log_format(ab, " comm=");
786 audit_log_untrustedstring(ab, name);
791 down_read(&mm->mmap_sem);
794 if ((vma->vm_flags & VM_EXECUTABLE) &&
796 audit_log_d_path(ab, "exe=",
797 vma->vm_file->f_dentry,
798 vma->vm_file->f_vfsmnt);
803 up_read(&mm->mmap_sem);
806 static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
809 struct audit_buffer *ab;
810 struct audit_aux_data *aux;
812 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
814 return; /* audit_panic has been called */
815 audit_log_format(ab, "arch=%x syscall=%d",
816 context->arch, context->major);
817 if (context->personality != PER_LINUX)
818 audit_log_format(ab, " per=%lx", context->personality);
819 if (context->return_valid)
820 audit_log_format(ab, " success=%s exit=%ld",
821 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
822 context->return_code);
824 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
825 " pid=%d auid=%u uid=%u gid=%u"
826 " euid=%u suid=%u fsuid=%u"
827 " egid=%u sgid=%u fsgid=%u",
837 context->euid, context->suid, context->fsuid,
838 context->egid, context->sgid, context->fsgid);
839 audit_log_task_info(ab);
842 for (aux = context->aux; aux; aux = aux->next) {
844 ab = audit_log_start(context, GFP_KERNEL, aux->type);
846 continue; /* audit_panic has been called */
850 struct audit_aux_data_ipcctl *axi = (void *)aux;
852 " qbytes=%lx iuid=%u igid=%u mode=%x",
853 axi->qbytes, axi->uid, axi->gid, axi->mode);
856 case AUDIT_SOCKETCALL: {
858 struct audit_aux_data_socketcall *axs = (void *)aux;
859 audit_log_format(ab, "nargs=%d", axs->nargs);
860 for (i=0; i<axs->nargs; i++)
861 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
864 case AUDIT_SOCKADDR: {
865 struct audit_aux_data_sockaddr *axs = (void *)aux;
867 audit_log_format(ab, "saddr=");
868 audit_log_hex(ab, axs->a, axs->len);
871 case AUDIT_AVC_PATH: {
872 struct audit_aux_data_path *axi = (void *)aux;
873 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
880 if (context->pwd && context->pwdmnt) {
881 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
883 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
887 for (i = 0; i < context->name_count; i++) {
888 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
890 continue; /* audit_panic has been called */
892 audit_log_format(ab, "item=%d", i);
893 if (context->names[i].name) {
894 audit_log_format(ab, " name=");
895 audit_log_untrustedstring(ab, context->names[i].name);
897 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
899 if (context->names[i].ino != (unsigned long)-1)
900 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
901 " ouid=%u ogid=%u rdev=%02x:%02x",
902 context->names[i].ino,
903 MAJOR(context->names[i].dev),
904 MINOR(context->names[i].dev),
905 context->names[i].mode,
906 context->names[i].uid,
907 context->names[i].gid,
908 MAJOR(context->names[i].rdev),
909 MINOR(context->names[i].rdev));
914 /* Free a per-task audit context. Called from copy_process and
915 * __put_task_struct. */
916 void audit_free(struct task_struct *tsk)
918 struct audit_context *context;
921 context = audit_get_context(tsk, 0, 0);
924 if (likely(!context))
927 /* Check for system calls that do not go through the exit
928 * function (e.g., exit_group), then free context block.
929 * We use GFP_ATOMIC here because we might be doing this
930 * in the context of the idle thread */
931 if (context->in_syscall && context->auditable)
932 audit_log_exit(context, GFP_ATOMIC);
934 audit_free_context(context);
937 /* Fill in audit context at syscall entry. This only happens if the
938 * audit context was created when the task was created and the state or
939 * filters demand the audit context be built. If the state from the
940 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
941 * then the record will be written at syscall exit time (otherwise, it
942 * will only be written if another part of the kernel requests that it
944 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
945 unsigned long a1, unsigned long a2,
946 unsigned long a3, unsigned long a4)
948 struct audit_context *context = tsk->audit_context;
949 enum audit_state state;
953 /* This happens only on certain architectures that make system
954 * calls in kernel_thread via the entry.S interface, instead of
955 * with direct calls. (If you are porting to a new
956 * architecture, hitting this condition can indicate that you
957 * got the _exit/_leave calls backward in entry.S.)
961 * ppc64 yes (see arch/ppc64/kernel/misc.S)
963 * This also happens with vm86 emulation in a non-nested manner
964 * (entries without exits), so this case must be caught.
966 if (context->in_syscall) {
967 struct audit_context *newctx;
969 #if defined(__NR_vm86) && defined(__NR_vm86old)
970 /* vm86 mode should only be entered once */
971 if (major == __NR_vm86 || major == __NR_vm86old)
976 "audit(:%d) pid=%d in syscall=%d;"
977 " entering syscall=%d\n",
978 context->serial, tsk->pid, context->major, major);
980 newctx = audit_alloc_context(context->state);
982 newctx->previous = context;
984 tsk->audit_context = newctx;
986 /* If we can't alloc a new context, the best we
987 * can do is to leak memory (any pending putname
988 * will be lost). The only other alternative is
989 * to abandon auditing. */
990 audit_zero_context(context, context->state);
993 BUG_ON(context->in_syscall || context->name_count);
998 context->arch = arch;
999 context->major = major;
1000 context->argv[0] = a1;
1001 context->argv[1] = a2;
1002 context->argv[2] = a3;
1003 context->argv[3] = a4;
1005 state = context->state;
1006 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
1007 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1008 if (likely(state == AUDIT_DISABLED))
1011 context->serial = 0;
1012 context->ctime = CURRENT_TIME;
1013 context->in_syscall = 1;
1014 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1017 /* Tear down after system call. If the audit context has been marked as
1018 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1019 * filtering, or because some other part of the kernel write an audit
1020 * message), then write out the syscall information. In call cases,
1021 * free the names stored from getname(). */
1022 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1024 struct audit_context *context;
1026 get_task_struct(tsk);
1028 context = audit_get_context(tsk, valid, return_code);
1031 /* Not having a context here is ok, since the parent may have
1032 * called __put_task_struct. */
1033 if (likely(!context))
1036 if (context->in_syscall && context->auditable)
1037 audit_log_exit(context, GFP_KERNEL);
1039 context->in_syscall = 0;
1040 context->auditable = 0;
1042 if (context->previous) {
1043 struct audit_context *new_context = context->previous;
1044 context->previous = NULL;
1045 audit_free_context(context);
1046 tsk->audit_context = new_context;
1048 audit_free_names(context);
1049 audit_free_aux(context);
1050 tsk->audit_context = context;
1053 put_task_struct(tsk);
1056 /* Add a name to the list. Called from fs/namei.c:getname(). */
1057 void audit_getname(const char *name)
1059 struct audit_context *context = current->audit_context;
1061 if (!context || IS_ERR(name) || !name)
1064 if (!context->in_syscall) {
1065 #if AUDIT_DEBUG == 2
1066 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1067 __FILE__, __LINE__, context->serial, name);
1072 BUG_ON(context->name_count >= AUDIT_NAMES);
1073 context->names[context->name_count].name = name;
1074 context->names[context->name_count].ino = (unsigned long)-1;
1075 ++context->name_count;
1076 if (!context->pwd) {
1077 read_lock(¤t->fs->lock);
1078 context->pwd = dget(current->fs->pwd);
1079 context->pwdmnt = mntget(current->fs->pwdmnt);
1080 read_unlock(¤t->fs->lock);
1085 /* Intercept a putname request. Called from
1086 * include/linux/fs.h:putname(). If we have stored the name from
1087 * getname in the audit context, then we delay the putname until syscall
1089 void audit_putname(const char *name)
1091 struct audit_context *context = current->audit_context;
1094 if (!context->in_syscall) {
1095 #if AUDIT_DEBUG == 2
1096 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1097 __FILE__, __LINE__, context->serial, name);
1098 if (context->name_count) {
1100 for (i = 0; i < context->name_count; i++)
1101 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1102 context->names[i].name,
1103 context->names[i].name);
1110 ++context->put_count;
1111 if (context->put_count > context->name_count) {
1112 printk(KERN_ERR "%s:%d(:%d): major=%d"
1113 " in_syscall=%d putname(%p) name_count=%d"
1116 context->serial, context->major,
1117 context->in_syscall, name, context->name_count,
1118 context->put_count);
1125 /* Store the inode and device from a lookup. Called from
1126 * fs/namei.c:path_lookup(). */
1127 void audit_inode(const char *name, const struct inode *inode, unsigned flags)
1130 struct audit_context *context = current->audit_context;
1132 if (!context->in_syscall)
1134 if (context->name_count
1135 && context->names[context->name_count-1].name
1136 && context->names[context->name_count-1].name == name)
1137 idx = context->name_count - 1;
1138 else if (context->name_count > 1
1139 && context->names[context->name_count-2].name
1140 && context->names[context->name_count-2].name == name)
1141 idx = context->name_count - 2;
1143 /* FIXME: how much do we care about inodes that have no
1144 * associated name? */
1145 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1147 idx = context->name_count++;
1148 context->names[idx].name = NULL;
1150 ++context->ino_count;
1153 context->names[idx].flags = flags;
1154 context->names[idx].ino = inode->i_ino;
1155 context->names[idx].dev = inode->i_sb->s_dev;
1156 context->names[idx].mode = inode->i_mode;
1157 context->names[idx].uid = inode->i_uid;
1158 context->names[idx].gid = inode->i_gid;
1159 context->names[idx].rdev = inode->i_rdev;
1162 void auditsc_get_stamp(struct audit_context *ctx,
1163 struct timespec *t, unsigned int *serial)
1166 ctx->serial = audit_serial();
1167 t->tv_sec = ctx->ctime.tv_sec;
1168 t->tv_nsec = ctx->ctime.tv_nsec;
1169 *serial = ctx->serial;
1173 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1175 if (task->audit_context) {
1176 struct audit_buffer *ab;
1178 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1180 audit_log_format(ab, "login pid=%d uid=%u "
1181 "old auid=%u new auid=%u",
1182 task->pid, task->uid,
1183 task->audit_context->loginuid, loginuid);
1186 task->audit_context->loginuid = loginuid;
1191 uid_t audit_get_loginuid(struct audit_context *ctx)
1193 return ctx ? ctx->loginuid : -1;
1196 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1198 struct audit_aux_data_ipcctl *ax;
1199 struct audit_context *context = current->audit_context;
1201 if (likely(!context))
1204 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1208 ax->qbytes = qbytes;
1213 ax->d.type = AUDIT_IPC;
1214 ax->d.next = context->aux;
1215 context->aux = (void *)ax;
1219 int audit_socketcall(int nargs, unsigned long *args)
1221 struct audit_aux_data_socketcall *ax;
1222 struct audit_context *context = current->audit_context;
1224 if (likely(!context))
1227 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1232 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1234 ax->d.type = AUDIT_SOCKETCALL;
1235 ax->d.next = context->aux;
1236 context->aux = (void *)ax;
1240 int audit_sockaddr(int len, void *a)
1242 struct audit_aux_data_sockaddr *ax;
1243 struct audit_context *context = current->audit_context;
1245 if (likely(!context))
1248 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1253 memcpy(ax->a, a, len);
1255 ax->d.type = AUDIT_SOCKADDR;
1256 ax->d.next = context->aux;
1257 context->aux = (void *)ax;
1261 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1263 struct audit_aux_data_path *ax;
1264 struct audit_context *context = current->audit_context;
1266 if (likely(!context))
1269 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1273 ax->dentry = dget(dentry);
1274 ax->mnt = mntget(mnt);
1276 ax->d.type = AUDIT_AVC_PATH;
1277 ax->d.next = context->aux;
1278 context->aux = (void *)ax;
1282 void audit_signal_info(int sig, struct task_struct *t)
1284 extern pid_t audit_sig_pid;
1285 extern uid_t audit_sig_uid;
1287 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1288 if (sig == SIGTERM || sig == SIGHUP) {
1289 struct audit_context *ctx = current->audit_context;
1290 audit_sig_pid = current->pid;
1292 audit_sig_uid = ctx->loginuid;
1294 audit_sig_uid = current->uid;