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/atomic.h>
34 #include <asm/types.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 <asm/unistd.h>
47 1 = put_count checking
48 2 = verbose put_count checking
52 /* No syscall auditing will take place unless audit_enabled != 0. */
53 extern int audit_enabled;
55 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
56 * for saving names from getname(). */
57 #define AUDIT_NAMES 20
59 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
60 * audit_context from being used for nameless inodes from
62 #define AUDIT_NAMES_RESERVED 7
64 /* At task start time, the audit_state is set in the audit_context using
65 a per-task filter. At syscall entry, the audit_state is augmented by
66 the syscall filter. */
68 AUDIT_DISABLED, /* Do not create per-task audit_context.
69 * No syscall-specific audit records can
71 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
72 * but don't necessarily fill it in at
73 * syscall entry time (i.e., filter
75 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
76 * and always fill it in at syscall
77 * entry time. This makes a full
78 * syscall record available if some
79 * other part of the kernel decides it
80 * should be recorded. */
81 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
82 * always fill it in at syscall entry
83 * time, and always write out the audit
84 * record at syscall exit time. */
87 /* When fs/namei.c:getname() is called, we store the pointer in name and
88 * we don't let putname() free it (instead we free all of the saved
89 * pointers at syscall exit time).
91 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
103 struct audit_aux_data {
104 struct audit_aux_data *next;
108 #define AUDIT_AUX_IPCPERM 0
110 struct audit_aux_data_ipcctl {
111 struct audit_aux_data d;
113 unsigned long qbytes;
119 struct audit_aux_data_socketcall {
120 struct audit_aux_data d;
122 unsigned long args[0];
125 struct audit_aux_data_sockaddr {
126 struct audit_aux_data d;
131 struct audit_aux_data_path {
132 struct audit_aux_data d;
133 struct dentry *dentry;
134 struct vfsmount *mnt;
137 /* The per-task audit context. */
138 struct audit_context {
139 int in_syscall; /* 1 if task is in a syscall */
140 enum audit_state state;
141 unsigned int serial; /* serial number for record */
142 struct timespec ctime; /* time of syscall entry */
143 uid_t loginuid; /* login uid (identity) */
144 int major; /* syscall number */
145 unsigned long argv[4]; /* syscall arguments */
146 int return_valid; /* return code is valid */
147 long return_code;/* syscall return code */
148 int auditable; /* 1 if record should be written */
150 struct audit_names names[AUDIT_NAMES];
152 struct vfsmount * pwdmnt;
153 struct audit_context *previous; /* For nested syscalls */
154 struct audit_aux_data *aux;
156 /* Save things to print about task_struct */
158 uid_t uid, euid, suid, fsuid;
159 gid_t gid, egid, sgid, fsgid;
160 unsigned long personality;
170 /* There are three lists of rules -- one to search at task creation
171 * time, one to search at syscall entry time, and another to search at
172 * syscall exit time. */
173 static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
174 LIST_HEAD_INIT(audit_filter_list[0]),
175 LIST_HEAD_INIT(audit_filter_list[1]),
176 LIST_HEAD_INIT(audit_filter_list[2]),
177 LIST_HEAD_INIT(audit_filter_list[3]),
178 LIST_HEAD_INIT(audit_filter_list[4]),
179 #if AUDIT_NR_FILTERS != 5
180 #error Fix audit_filter_list initialiser
185 struct list_head list;
187 struct audit_rule rule;
190 extern int audit_pid;
192 /* Check to see if two rules are identical. It is called from
193 * audit_del_rule during AUDIT_DEL. */
194 static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
198 if (a->flags != b->flags)
201 if (a->action != b->action)
204 if (a->field_count != b->field_count)
207 for (i = 0; i < a->field_count; i++) {
208 if (a->fields[i] != b->fields[i]
209 || a->values[i] != b->values[i])
213 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
214 if (a->mask[i] != b->mask[i])
220 /* Note that audit_add_rule and audit_del_rule are called via
221 * audit_receive() in audit.c, and are protected by
222 * audit_netlink_sem. */
223 static inline void audit_add_rule(struct audit_entry *entry,
224 struct list_head *list)
226 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
227 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
228 list_add_rcu(&entry->list, list);
230 list_add_tail_rcu(&entry->list, list);
234 static void audit_free_rule(struct rcu_head *head)
236 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
240 /* Note that audit_add_rule and audit_del_rule are called via
241 * audit_receive() in audit.c, and are protected by
242 * audit_netlink_sem. */
243 static inline int audit_del_rule(struct audit_rule *rule,
244 struct list_head *list)
246 struct audit_entry *e;
248 /* Do not use the _rcu iterator here, since this is the only
249 * deletion routine. */
250 list_for_each_entry(e, list, list) {
251 if (!audit_compare_rule(rule, &e->rule)) {
252 list_del_rcu(&e->list);
253 call_rcu(&e->rcu, audit_free_rule);
257 return -ENOENT; /* No matching rule */
260 /* Copy rule from user-space to kernel-space. Called during
262 static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
266 if (s->action != AUDIT_NEVER
267 && s->action != AUDIT_POSSIBLE
268 && s->action != AUDIT_ALWAYS)
270 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
272 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
276 d->action = s->action;
277 d->field_count = s->field_count;
278 for (i = 0; i < d->field_count; i++) {
279 d->fields[i] = s->fields[i];
280 d->values[i] = s->values[i];
282 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
286 static int audit_list_rules(void *_dest)
290 struct audit_entry *entry;
297 down(&audit_netlink_sem);
299 /* The *_rcu iterators not needed here because we are
300 always called with audit_netlink_sem held. */
301 for (i=0; i<AUDIT_NR_FILTERS; i++) {
302 list_for_each_entry(entry, &audit_filter_list[i], list)
303 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
304 &entry->rule, sizeof(entry->rule));
306 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
308 up(&audit_netlink_sem);
312 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
315 struct audit_entry *entry;
316 struct task_struct *tsk;
323 /* We can't just spew out the rules here because we might fill
324 * the available socket buffer space and deadlock waiting for
325 * auditctl to read from it... which isn't ever going to
326 * happen if we're actually running in the context of auditctl
327 * trying to _send_ the stuff */
329 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
335 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
342 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
344 if (audit_copy_rule(&entry->rule, data)) {
348 listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
349 audit_add_rule(entry, &audit_filter_list[listnr]);
350 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
351 "auid=%u added an audit rule\n", loginuid);
354 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
355 if (listnr >= AUDIT_NR_FILTERS)
358 err = audit_del_rule(data, &audit_filter_list[listnr]);
360 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
361 "auid=%u removed an audit rule\n", loginuid);
370 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
372 static int audit_filter_rules(struct task_struct *tsk,
373 struct audit_rule *rule,
374 struct audit_context *ctx,
375 enum audit_state *state)
379 for (i = 0; i < rule->field_count; i++) {
380 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
381 u32 value = rule->values[i];
386 result = (tsk->pid == value);
389 result = (tsk->uid == value);
392 result = (tsk->euid == value);
395 result = (tsk->suid == value);
398 result = (tsk->fsuid == value);
401 result = (tsk->gid == value);
404 result = (tsk->egid == value);
407 result = (tsk->sgid == value);
410 result = (tsk->fsgid == value);
413 result = (tsk->personality == value);
417 result = (ctx->arch == value);
421 if (ctx && ctx->return_valid)
422 result = (ctx->return_code == value);
425 if (ctx && ctx->return_valid)
426 result = (ctx->return_valid == AUDITSC_SUCCESS);
430 for (j = 0; j < ctx->name_count; j++) {
431 if (MAJOR(ctx->names[j].dev)==value) {
440 for (j = 0; j < ctx->name_count; j++) {
441 if (MINOR(ctx->names[j].dev)==value) {
450 for (j = 0; j < ctx->name_count; j++) {
451 if (ctx->names[j].ino == value) {
461 result = (ctx->loginuid == value);
468 result = (ctx->argv[field-AUDIT_ARG0]==value);
472 if (rule->fields[i] & AUDIT_NEGATE)
477 switch (rule->action) {
478 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
479 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
480 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
485 /* At process creation time, we can determine if system-call auditing is
486 * completely disabled for this task. Since we only have the task
487 * structure at this point, we can only check uid and gid.
489 static enum audit_state audit_filter_task(struct task_struct *tsk)
491 struct audit_entry *e;
492 enum audit_state state;
495 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
496 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
502 return AUDIT_BUILD_CONTEXT;
505 /* At syscall entry and exit time, this filter is called if the
506 * audit_state is not low enough that auditing cannot take place, but is
507 * also not high enough that we already know we have to write an audit
508 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
510 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
511 struct audit_context *ctx,
512 struct list_head *list)
514 struct audit_entry *e;
515 enum audit_state state;
516 int word = AUDIT_WORD(ctx->major);
517 int bit = AUDIT_BIT(ctx->major);
519 if (audit_pid && tsk->pid == audit_pid)
520 return AUDIT_DISABLED;
523 list_for_each_entry_rcu(e, list, list) {
524 if ((e->rule.mask[word] & bit) == bit
525 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
531 return AUDIT_BUILD_CONTEXT;
534 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
535 struct audit_rule *rule,
536 enum audit_state *state)
540 for (i = 0; i < rule->field_count; i++) {
541 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
542 u32 value = rule->values[i];
547 result = (cb->creds.pid == value);
550 result = (cb->creds.uid == value);
553 result = (cb->creds.gid == value);
556 result = (cb->loginuid == value);
560 if (rule->fields[i] & AUDIT_NEGATE)
565 switch (rule->action) {
566 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
567 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
568 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
573 int audit_filter_user(struct netlink_skb_parms *cb, int type)
575 struct audit_entry *e;
576 enum audit_state state;
580 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
581 if (audit_filter_user_rules(cb, &e->rule, &state)) {
582 if (state == AUDIT_DISABLED)
589 return ret; /* Audit by default */
592 /* This should be called with task_lock() held. */
593 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
597 struct audit_context *context = tsk->audit_context;
599 if (likely(!context))
601 context->return_valid = return_valid;
602 context->return_code = return_code;
604 if (context->in_syscall && !context->auditable) {
605 enum audit_state state;
606 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
607 if (state == AUDIT_RECORD_CONTEXT)
608 context->auditable = 1;
611 context->pid = tsk->pid;
612 context->uid = tsk->uid;
613 context->gid = tsk->gid;
614 context->euid = tsk->euid;
615 context->suid = tsk->suid;
616 context->fsuid = tsk->fsuid;
617 context->egid = tsk->egid;
618 context->sgid = tsk->sgid;
619 context->fsgid = tsk->fsgid;
620 context->personality = tsk->personality;
621 tsk->audit_context = NULL;
625 static inline void audit_free_names(struct audit_context *context)
630 if (context->auditable
631 ||context->put_count + context->ino_count != context->name_count) {
632 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
633 " name_count=%d put_count=%d"
634 " ino_count=%d [NOT freeing]\n",
636 context->serial, context->major, context->in_syscall,
637 context->name_count, context->put_count,
639 for (i = 0; i < context->name_count; i++)
640 printk(KERN_ERR "names[%d] = %p = %s\n", i,
641 context->names[i].name,
642 context->names[i].name);
648 context->put_count = 0;
649 context->ino_count = 0;
652 for (i = 0; i < context->name_count; i++)
653 if (context->names[i].name)
654 __putname(context->names[i].name);
655 context->name_count = 0;
659 mntput(context->pwdmnt);
661 context->pwdmnt = NULL;
664 static inline void audit_free_aux(struct audit_context *context)
666 struct audit_aux_data *aux;
668 while ((aux = context->aux)) {
669 if (aux->type == AUDIT_AVC_PATH) {
670 struct audit_aux_data_path *axi = (void *)aux;
674 context->aux = aux->next;
679 static inline void audit_zero_context(struct audit_context *context,
680 enum audit_state state)
682 uid_t loginuid = context->loginuid;
684 memset(context, 0, sizeof(*context));
685 context->state = state;
686 context->loginuid = loginuid;
689 static inline struct audit_context *audit_alloc_context(enum audit_state state)
691 struct audit_context *context;
693 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
695 audit_zero_context(context, state);
699 /* Filter on the task information and allocate a per-task audit context
700 * if necessary. Doing so turns on system call auditing for the
701 * specified task. This is called from copy_process, so no lock is
703 int audit_alloc(struct task_struct *tsk)
705 struct audit_context *context;
706 enum audit_state state;
708 if (likely(!audit_enabled))
709 return 0; /* Return if not auditing. */
711 state = audit_filter_task(tsk);
712 if (likely(state == AUDIT_DISABLED))
715 if (!(context = audit_alloc_context(state))) {
716 audit_log_lost("out of memory in audit_alloc");
720 /* Preserve login uid */
721 context->loginuid = -1;
722 if (current->audit_context)
723 context->loginuid = current->audit_context->loginuid;
725 tsk->audit_context = context;
726 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
730 static inline void audit_free_context(struct audit_context *context)
732 struct audit_context *previous;
736 previous = context->previous;
737 if (previous || (count && count < 10)) {
739 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
740 " freeing multiple contexts (%d)\n",
741 context->serial, context->major,
742 context->name_count, count);
744 audit_free_names(context);
745 audit_free_aux(context);
750 printk(KERN_ERR "audit: freed %d contexts\n", count);
753 static void audit_log_task_info(struct audit_buffer *ab)
755 char name[sizeof(current->comm)];
756 struct mm_struct *mm = current->mm;
757 struct vm_area_struct *vma;
759 get_task_comm(name, current);
760 audit_log_format(ab, " comm=");
761 audit_log_untrustedstring(ab, name);
766 down_read(&mm->mmap_sem);
769 if ((vma->vm_flags & VM_EXECUTABLE) &&
771 audit_log_d_path(ab, "exe=",
772 vma->vm_file->f_dentry,
773 vma->vm_file->f_vfsmnt);
778 up_read(&mm->mmap_sem);
781 static void audit_log_exit(struct audit_context *context)
784 struct audit_buffer *ab;
785 struct audit_aux_data *aux;
787 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
789 return; /* audit_panic has been called */
790 audit_log_format(ab, "arch=%x syscall=%d",
791 context->arch, context->major);
792 if (context->personality != PER_LINUX)
793 audit_log_format(ab, " per=%lx", context->personality);
794 if (context->return_valid)
795 audit_log_format(ab, " success=%s exit=%ld",
796 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
797 context->return_code);
799 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
800 " pid=%d auid=%u uid=%u gid=%u"
801 " euid=%u suid=%u fsuid=%u"
802 " egid=%u sgid=%u fsgid=%u",
812 context->euid, context->suid, context->fsuid,
813 context->egid, context->sgid, context->fsgid);
814 audit_log_task_info(ab);
817 for (aux = context->aux; aux; aux = aux->next) {
819 ab = audit_log_start(context, GFP_KERNEL, aux->type);
821 continue; /* audit_panic has been called */
825 struct audit_aux_data_ipcctl *axi = (void *)aux;
827 " qbytes=%lx iuid=%u igid=%u mode=%x",
828 axi->qbytes, axi->uid, axi->gid, axi->mode);
831 case AUDIT_SOCKETCALL: {
833 struct audit_aux_data_socketcall *axs = (void *)aux;
834 audit_log_format(ab, "nargs=%d", axs->nargs);
835 for (i=0; i<axs->nargs; i++)
836 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
839 case AUDIT_SOCKADDR: {
840 struct audit_aux_data_sockaddr *axs = (void *)aux;
842 audit_log_format(ab, "saddr=");
843 audit_log_hex(ab, axs->a, axs->len);
846 case AUDIT_AVC_PATH: {
847 struct audit_aux_data_path *axi = (void *)aux;
848 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
855 if (context->pwd && context->pwdmnt) {
856 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
858 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
862 for (i = 0; i < context->name_count; i++) {
863 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
865 continue; /* audit_panic has been called */
867 audit_log_format(ab, "item=%d", i);
868 if (context->names[i].name) {
869 audit_log_format(ab, " name=");
870 audit_log_untrustedstring(ab, context->names[i].name);
872 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
874 if (context->names[i].ino != (unsigned long)-1)
875 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
876 " ouid=%u ogid=%u rdev=%02x:%02x",
877 context->names[i].ino,
878 MAJOR(context->names[i].dev),
879 MINOR(context->names[i].dev),
880 context->names[i].mode,
881 context->names[i].uid,
882 context->names[i].gid,
883 MAJOR(context->names[i].rdev),
884 MINOR(context->names[i].rdev));
889 /* Free a per-task audit context. Called from copy_process and
890 * __put_task_struct. */
891 void audit_free(struct task_struct *tsk)
893 struct audit_context *context;
896 context = audit_get_context(tsk, 0, 0);
899 if (likely(!context))
902 /* Check for system calls that do not go through the exit
903 * function (e.g., exit_group), then free context block. */
904 if (context->in_syscall && context->auditable)
905 audit_log_exit(context);
907 audit_free_context(context);
910 /* Fill in audit context at syscall entry. This only happens if the
911 * audit context was created when the task was created and the state or
912 * filters demand the audit context be built. If the state from the
913 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
914 * then the record will be written at syscall exit time (otherwise, it
915 * will only be written if another part of the kernel requests that it
917 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
918 unsigned long a1, unsigned long a2,
919 unsigned long a3, unsigned long a4)
921 struct audit_context *context = tsk->audit_context;
922 enum audit_state state;
926 /* This happens only on certain architectures that make system
927 * calls in kernel_thread via the entry.S interface, instead of
928 * with direct calls. (If you are porting to a new
929 * architecture, hitting this condition can indicate that you
930 * got the _exit/_leave calls backward in entry.S.)
934 * ppc64 yes (see arch/ppc64/kernel/misc.S)
936 * This also happens with vm86 emulation in a non-nested manner
937 * (entries without exits), so this case must be caught.
939 if (context->in_syscall) {
940 struct audit_context *newctx;
942 #if defined(__NR_vm86) && defined(__NR_vm86old)
943 /* vm86 mode should only be entered once */
944 if (major == __NR_vm86 || major == __NR_vm86old)
949 "audit(:%d) pid=%d in syscall=%d;"
950 " entering syscall=%d\n",
951 context->serial, tsk->pid, context->major, major);
953 newctx = audit_alloc_context(context->state);
955 newctx->previous = context;
957 tsk->audit_context = newctx;
959 /* If we can't alloc a new context, the best we
960 * can do is to leak memory (any pending putname
961 * will be lost). The only other alternative is
962 * to abandon auditing. */
963 audit_zero_context(context, context->state);
966 BUG_ON(context->in_syscall || context->name_count);
971 context->arch = arch;
972 context->major = major;
973 context->argv[0] = a1;
974 context->argv[1] = a2;
975 context->argv[2] = a3;
976 context->argv[3] = a4;
978 state = context->state;
979 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
980 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
981 if (likely(state == AUDIT_DISABLED))
984 context->serial = audit_serial();
985 context->ctime = CURRENT_TIME;
986 context->in_syscall = 1;
987 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
990 /* Tear down after system call. If the audit context has been marked as
991 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
992 * filtering, or because some other part of the kernel write an audit
993 * message), then write out the syscall information. In call cases,
994 * free the names stored from getname(). */
995 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
997 struct audit_context *context;
999 get_task_struct(tsk);
1001 context = audit_get_context(tsk, valid, return_code);
1004 /* Not having a context here is ok, since the parent may have
1005 * called __put_task_struct. */
1006 if (likely(!context))
1009 if (context->in_syscall && context->auditable)
1010 audit_log_exit(context);
1012 context->in_syscall = 0;
1013 context->auditable = 0;
1015 if (context->previous) {
1016 struct audit_context *new_context = context->previous;
1017 context->previous = NULL;
1018 audit_free_context(context);
1019 tsk->audit_context = new_context;
1021 audit_free_names(context);
1022 audit_free_aux(context);
1023 audit_zero_context(context, context->state);
1024 tsk->audit_context = context;
1026 put_task_struct(tsk);
1029 /* Add a name to the list. Called from fs/namei.c:getname(). */
1030 void audit_getname(const char *name)
1032 struct audit_context *context = current->audit_context;
1034 if (!context || IS_ERR(name) || !name)
1037 if (!context->in_syscall) {
1038 #if AUDIT_DEBUG == 2
1039 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1040 __FILE__, __LINE__, context->serial, name);
1045 BUG_ON(context->name_count >= AUDIT_NAMES);
1046 context->names[context->name_count].name = name;
1047 context->names[context->name_count].ino = (unsigned long)-1;
1048 ++context->name_count;
1049 if (!context->pwd) {
1050 read_lock(¤t->fs->lock);
1051 context->pwd = dget(current->fs->pwd);
1052 context->pwdmnt = mntget(current->fs->pwdmnt);
1053 read_unlock(¤t->fs->lock);
1058 /* Intercept a putname request. Called from
1059 * include/linux/fs.h:putname(). If we have stored the name from
1060 * getname in the audit context, then we delay the putname until syscall
1062 void audit_putname(const char *name)
1064 struct audit_context *context = current->audit_context;
1067 if (!context->in_syscall) {
1068 #if AUDIT_DEBUG == 2
1069 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1070 __FILE__, __LINE__, context->serial, name);
1071 if (context->name_count) {
1073 for (i = 0; i < context->name_count; i++)
1074 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1075 context->names[i].name,
1076 context->names[i].name);
1083 ++context->put_count;
1084 if (context->put_count > context->name_count) {
1085 printk(KERN_ERR "%s:%d(:%d): major=%d"
1086 " in_syscall=%d putname(%p) name_count=%d"
1089 context->serial, context->major,
1090 context->in_syscall, name, context->name_count,
1091 context->put_count);
1098 /* Store the inode and device from a lookup. Called from
1099 * fs/namei.c:path_lookup(). */
1100 void audit_inode(const char *name, const struct inode *inode, unsigned flags)
1103 struct audit_context *context = current->audit_context;
1105 if (!context->in_syscall)
1107 if (context->name_count
1108 && context->names[context->name_count-1].name
1109 && context->names[context->name_count-1].name == name)
1110 idx = context->name_count - 1;
1111 else if (context->name_count > 1
1112 && context->names[context->name_count-2].name
1113 && context->names[context->name_count-2].name == name)
1114 idx = context->name_count - 2;
1116 /* FIXME: how much do we care about inodes that have no
1117 * associated name? */
1118 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1120 idx = context->name_count++;
1121 context->names[idx].name = NULL;
1123 ++context->ino_count;
1126 context->names[idx].flags = flags;
1127 context->names[idx].ino = inode->i_ino;
1128 context->names[idx].dev = inode->i_sb->s_dev;
1129 context->names[idx].mode = inode->i_mode;
1130 context->names[idx].uid = inode->i_uid;
1131 context->names[idx].gid = inode->i_gid;
1132 context->names[idx].rdev = inode->i_rdev;
1135 void auditsc_get_stamp(struct audit_context *ctx,
1136 struct timespec *t, unsigned int *serial)
1138 t->tv_sec = ctx->ctime.tv_sec;
1139 t->tv_nsec = ctx->ctime.tv_nsec;
1140 *serial = ctx->serial;
1144 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1146 if (task->audit_context) {
1147 struct audit_buffer *ab;
1149 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1151 audit_log_format(ab, "login pid=%d uid=%u "
1152 "old auid=%u new auid=%u",
1153 task->pid, task->uid,
1154 task->audit_context->loginuid, loginuid);
1157 task->audit_context->loginuid = loginuid;
1162 uid_t audit_get_loginuid(struct audit_context *ctx)
1164 return ctx ? ctx->loginuid : -1;
1167 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1169 struct audit_aux_data_ipcctl *ax;
1170 struct audit_context *context = current->audit_context;
1172 if (likely(!context))
1175 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1179 ax->qbytes = qbytes;
1184 ax->d.type = AUDIT_IPC;
1185 ax->d.next = context->aux;
1186 context->aux = (void *)ax;
1190 int audit_socketcall(int nargs, unsigned long *args)
1192 struct audit_aux_data_socketcall *ax;
1193 struct audit_context *context = current->audit_context;
1195 if (likely(!context))
1198 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1203 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1205 ax->d.type = AUDIT_SOCKETCALL;
1206 ax->d.next = context->aux;
1207 context->aux = (void *)ax;
1211 int audit_sockaddr(int len, void *a)
1213 struct audit_aux_data_sockaddr *ax;
1214 struct audit_context *context = current->audit_context;
1216 if (likely(!context))
1219 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1224 memcpy(ax->a, a, len);
1226 ax->d.type = AUDIT_SOCKADDR;
1227 ax->d.next = context->aux;
1228 context->aux = (void *)ax;
1232 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1234 struct audit_aux_data_path *ax;
1235 struct audit_context *context = current->audit_context;
1237 if (likely(!context))
1240 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1244 ax->dentry = dget(dentry);
1245 ax->mnt = mntget(mnt);
1247 ax->d.type = AUDIT_AVC_PATH;
1248 ax->d.next = context->aux;
1249 context->aux = (void *)ax;
1253 void audit_signal_info(int sig, struct task_struct *t)
1255 extern pid_t audit_sig_pid;
1256 extern uid_t audit_sig_uid;
1258 if (unlikely(audit_pid && t->pid == audit_pid)) {
1259 if (sig == SIGTERM || sig == SIGHUP) {
1260 struct audit_context *ctx = current->audit_context;
1261 audit_sig_pid = current->pid;
1263 audit_sig_uid = ctx->loginuid;
1265 audit_sig_uid = current->uid;