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);
334 * audit_receive_filter - apply all rules to the specified message type
335 * @type: audit message type
336 * @pid: target pid for netlink audit messages
337 * @uid: target uid for netlink audit messages
338 * @seq: netlink audit message sequence (serial) number
339 * @data: payload data
340 * @loginuid: loginuid of sender
342 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
345 struct task_struct *tsk;
352 /* We can't just spew out the rules here because we might fill
353 * the available socket buffer space and deadlock waiting for
354 * auditctl to read from it... which isn't ever going to
355 * happen if we're actually running in the context of auditctl
356 * trying to _send_ the stuff */
358 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
364 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
371 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
372 if (listnr >= AUDIT_NR_FILTERS)
375 err = audit_add_rule(data, &audit_filter_list[listnr]);
377 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
378 "auid=%u added an audit rule\n", loginuid);
381 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
382 if (listnr >= AUDIT_NR_FILTERS)
385 err = audit_del_rule(data, &audit_filter_list[listnr]);
387 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
388 "auid=%u removed an audit rule\n", loginuid);
397 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
399 static int audit_filter_rules(struct task_struct *tsk,
400 struct audit_rule *rule,
401 struct audit_context *ctx,
402 enum audit_state *state)
406 for (i = 0; i < rule->field_count; i++) {
407 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
408 u32 value = rule->values[i];
413 result = (tsk->pid == value);
416 result = (tsk->uid == value);
419 result = (tsk->euid == value);
422 result = (tsk->suid == value);
425 result = (tsk->fsuid == value);
428 result = (tsk->gid == value);
431 result = (tsk->egid == value);
434 result = (tsk->sgid == value);
437 result = (tsk->fsgid == value);
440 result = (tsk->personality == value);
444 result = (ctx->arch == value);
448 if (ctx && ctx->return_valid)
449 result = (ctx->return_code == value);
452 if (ctx && ctx->return_valid) {
454 result = (ctx->return_valid == AUDITSC_SUCCESS);
456 result = (ctx->return_valid == AUDITSC_FAILURE);
461 for (j = 0; j < ctx->name_count; j++) {
462 if (MAJOR(ctx->names[j].dev)==value) {
471 for (j = 0; j < ctx->name_count; j++) {
472 if (MINOR(ctx->names[j].dev)==value) {
481 for (j = 0; j < ctx->name_count; j++) {
482 if (ctx->names[j].ino == value) {
492 result = (ctx->loginuid == value);
499 result = (ctx->argv[field-AUDIT_ARG0]==value);
503 if (rule->fields[i] & AUDIT_NEGATE)
508 switch (rule->action) {
509 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
510 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
511 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
516 /* At process creation time, we can determine if system-call auditing is
517 * completely disabled for this task. Since we only have the task
518 * structure at this point, we can only check uid and gid.
520 static enum audit_state audit_filter_task(struct task_struct *tsk)
522 struct audit_entry *e;
523 enum audit_state state;
526 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
527 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
533 return AUDIT_BUILD_CONTEXT;
536 /* At syscall entry and exit time, this filter is called if the
537 * audit_state is not low enough that auditing cannot take place, but is
538 * also not high enough that we already know we have to write an audit
539 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
541 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
542 struct audit_context *ctx,
543 struct list_head *list)
545 struct audit_entry *e;
546 enum audit_state state;
548 if (audit_pid && tsk->tgid == audit_pid)
549 return AUDIT_DISABLED;
552 if (!list_empty(list)) {
553 int word = AUDIT_WORD(ctx->major);
554 int bit = AUDIT_BIT(ctx->major);
556 list_for_each_entry_rcu(e, list, list) {
557 if ((e->rule.mask[word] & bit) == bit
558 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
565 return AUDIT_BUILD_CONTEXT;
568 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
569 struct audit_rule *rule,
570 enum audit_state *state)
574 for (i = 0; i < rule->field_count; i++) {
575 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
576 u32 value = rule->values[i];
581 result = (cb->creds.pid == value);
584 result = (cb->creds.uid == value);
587 result = (cb->creds.gid == value);
590 result = (cb->loginuid == value);
594 if (rule->fields[i] & AUDIT_NEGATE)
599 switch (rule->action) {
600 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
601 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
602 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
607 int audit_filter_user(struct netlink_skb_parms *cb, int type)
609 struct audit_entry *e;
610 enum audit_state state;
614 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
615 if (audit_filter_user_rules(cb, &e->rule, &state)) {
616 if (state == AUDIT_DISABLED)
623 return ret; /* Audit by default */
626 /* This should be called with task_lock() held. */
627 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
631 struct audit_context *context = tsk->audit_context;
633 if (likely(!context))
635 context->return_valid = return_valid;
636 context->return_code = return_code;
638 if (context->in_syscall && !context->auditable) {
639 enum audit_state state;
640 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
641 if (state == AUDIT_RECORD_CONTEXT)
642 context->auditable = 1;
645 context->pid = tsk->pid;
646 context->uid = tsk->uid;
647 context->gid = tsk->gid;
648 context->euid = tsk->euid;
649 context->suid = tsk->suid;
650 context->fsuid = tsk->fsuid;
651 context->egid = tsk->egid;
652 context->sgid = tsk->sgid;
653 context->fsgid = tsk->fsgid;
654 context->personality = tsk->personality;
655 tsk->audit_context = NULL;
659 static inline void audit_free_names(struct audit_context *context)
664 if (context->auditable
665 ||context->put_count + context->ino_count != context->name_count) {
666 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
667 " name_count=%d put_count=%d"
668 " ino_count=%d [NOT freeing]\n",
670 context->serial, context->major, context->in_syscall,
671 context->name_count, context->put_count,
673 for (i = 0; i < context->name_count; i++)
674 printk(KERN_ERR "names[%d] = %p = %s\n", i,
675 context->names[i].name,
676 context->names[i].name);
682 context->put_count = 0;
683 context->ino_count = 0;
686 for (i = 0; i < context->name_count; i++)
687 if (context->names[i].name)
688 __putname(context->names[i].name);
689 context->name_count = 0;
693 mntput(context->pwdmnt);
695 context->pwdmnt = NULL;
698 static inline void audit_free_aux(struct audit_context *context)
700 struct audit_aux_data *aux;
702 while ((aux = context->aux)) {
703 if (aux->type == AUDIT_AVC_PATH) {
704 struct audit_aux_data_path *axi = (void *)aux;
708 context->aux = aux->next;
713 static inline void audit_zero_context(struct audit_context *context,
714 enum audit_state state)
716 uid_t loginuid = context->loginuid;
718 memset(context, 0, sizeof(*context));
719 context->state = state;
720 context->loginuid = loginuid;
723 static inline struct audit_context *audit_alloc_context(enum audit_state state)
725 struct audit_context *context;
727 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
729 audit_zero_context(context, state);
734 * audit_alloc - allocate an audit context block for a task
737 * Filter on the task information and allocate a per-task audit context
738 * if necessary. Doing so turns on system call auditing for the
739 * specified task. This is called from copy_process, so no lock is
742 int audit_alloc(struct task_struct *tsk)
744 struct audit_context *context;
745 enum audit_state state;
747 if (likely(!audit_enabled))
748 return 0; /* Return if not auditing. */
750 state = audit_filter_task(tsk);
751 if (likely(state == AUDIT_DISABLED))
754 if (!(context = audit_alloc_context(state))) {
755 audit_log_lost("out of memory in audit_alloc");
759 /* Preserve login uid */
760 context->loginuid = -1;
761 if (current->audit_context)
762 context->loginuid = current->audit_context->loginuid;
764 tsk->audit_context = context;
765 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
769 static inline void audit_free_context(struct audit_context *context)
771 struct audit_context *previous;
775 previous = context->previous;
776 if (previous || (count && count < 10)) {
778 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
779 " freeing multiple contexts (%d)\n",
780 context->serial, context->major,
781 context->name_count, count);
783 audit_free_names(context);
784 audit_free_aux(context);
789 printk(KERN_ERR "audit: freed %d contexts\n", count);
792 static void audit_log_task_info(struct audit_buffer *ab)
794 char name[sizeof(current->comm)];
795 struct mm_struct *mm = current->mm;
796 struct vm_area_struct *vma;
798 get_task_comm(name, current);
799 audit_log_format(ab, " comm=");
800 audit_log_untrustedstring(ab, name);
805 down_read(&mm->mmap_sem);
808 if ((vma->vm_flags & VM_EXECUTABLE) &&
810 audit_log_d_path(ab, "exe=",
811 vma->vm_file->f_dentry,
812 vma->vm_file->f_vfsmnt);
817 up_read(&mm->mmap_sem);
820 static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
823 struct audit_buffer *ab;
824 struct audit_aux_data *aux;
826 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
828 return; /* audit_panic has been called */
829 audit_log_format(ab, "arch=%x syscall=%d",
830 context->arch, context->major);
831 if (context->personality != PER_LINUX)
832 audit_log_format(ab, " per=%lx", context->personality);
833 if (context->return_valid)
834 audit_log_format(ab, " success=%s exit=%ld",
835 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
836 context->return_code);
838 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
839 " pid=%d auid=%u uid=%u gid=%u"
840 " euid=%u suid=%u fsuid=%u"
841 " egid=%u sgid=%u fsgid=%u",
851 context->euid, context->suid, context->fsuid,
852 context->egid, context->sgid, context->fsgid);
853 audit_log_task_info(ab);
856 for (aux = context->aux; aux; aux = aux->next) {
858 ab = audit_log_start(context, gfp_mask, aux->type);
860 continue; /* audit_panic has been called */
864 struct audit_aux_data_ipcctl *axi = (void *)aux;
866 " qbytes=%lx iuid=%u igid=%u mode=%x",
867 axi->qbytes, axi->uid, axi->gid, axi->mode);
870 case AUDIT_SOCKETCALL: {
872 struct audit_aux_data_socketcall *axs = (void *)aux;
873 audit_log_format(ab, "nargs=%d", axs->nargs);
874 for (i=0; i<axs->nargs; i++)
875 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
878 case AUDIT_SOCKADDR: {
879 struct audit_aux_data_sockaddr *axs = (void *)aux;
881 audit_log_format(ab, "saddr=");
882 audit_log_hex(ab, axs->a, axs->len);
885 case AUDIT_AVC_PATH: {
886 struct audit_aux_data_path *axi = (void *)aux;
887 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
894 if (context->pwd && context->pwdmnt) {
895 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
897 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
901 for (i = 0; i < context->name_count; i++) {
902 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
904 continue; /* audit_panic has been called */
906 audit_log_format(ab, "item=%d", i);
907 if (context->names[i].name) {
908 audit_log_format(ab, " name=");
909 audit_log_untrustedstring(ab, context->names[i].name);
911 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
913 if (context->names[i].ino != (unsigned long)-1)
914 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
915 " ouid=%u ogid=%u rdev=%02x:%02x",
916 context->names[i].ino,
917 MAJOR(context->names[i].dev),
918 MINOR(context->names[i].dev),
919 context->names[i].mode,
920 context->names[i].uid,
921 context->names[i].gid,
922 MAJOR(context->names[i].rdev),
923 MINOR(context->names[i].rdev));
929 * audit_free - free a per-task audit context
930 * @tsk: task whose audit context block to free
932 * Called from copy_process and __put_task_struct.
934 void audit_free(struct task_struct *tsk)
936 struct audit_context *context;
939 context = audit_get_context(tsk, 0, 0);
942 if (likely(!context))
945 /* Check for system calls that do not go through the exit
946 * function (e.g., exit_group), then free context block.
947 * We use GFP_ATOMIC here because we might be doing this
948 * in the context of the idle thread */
949 if (context->in_syscall && context->auditable)
950 audit_log_exit(context, GFP_ATOMIC);
952 audit_free_context(context);
956 * audit_syscall_entry - fill in an audit record at syscall entry
957 * @tsk: task being audited
958 * @arch: architecture type
959 * @major: major syscall type (function)
960 * @a1: additional syscall register 1
961 * @a2: additional syscall register 2
962 * @a3: additional syscall register 3
963 * @a4: additional syscall register 4
965 * Fill in audit context at syscall entry. This only happens if the
966 * audit context was created when the task was created and the state or
967 * filters demand the audit context be built. If the state from the
968 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
969 * then the record will be written at syscall exit time (otherwise, it
970 * will only be written if another part of the kernel requests that it
973 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
974 unsigned long a1, unsigned long a2,
975 unsigned long a3, unsigned long a4)
977 struct audit_context *context = tsk->audit_context;
978 enum audit_state state;
983 * This happens only on certain architectures that make system
984 * calls in kernel_thread via the entry.S interface, instead of
985 * with direct calls. (If you are porting to a new
986 * architecture, hitting this condition can indicate that you
987 * got the _exit/_leave calls backward in entry.S.)
991 * ppc64 yes (see arch/ppc64/kernel/misc.S)
993 * This also happens with vm86 emulation in a non-nested manner
994 * (entries without exits), so this case must be caught.
996 if (context->in_syscall) {
997 struct audit_context *newctx;
1001 "audit(:%d) pid=%d in syscall=%d;"
1002 " entering syscall=%d\n",
1003 context->serial, tsk->pid, context->major, major);
1005 newctx = audit_alloc_context(context->state);
1007 newctx->previous = context;
1009 tsk->audit_context = newctx;
1011 /* If we can't alloc a new context, the best we
1012 * can do is to leak memory (any pending putname
1013 * will be lost). The only other alternative is
1014 * to abandon auditing. */
1015 audit_zero_context(context, context->state);
1018 BUG_ON(context->in_syscall || context->name_count);
1023 context->arch = arch;
1024 context->major = major;
1025 context->argv[0] = a1;
1026 context->argv[1] = a2;
1027 context->argv[2] = a3;
1028 context->argv[3] = a4;
1030 state = context->state;
1031 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
1032 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1033 if (likely(state == AUDIT_DISABLED))
1036 context->serial = 0;
1037 context->ctime = CURRENT_TIME;
1038 context->in_syscall = 1;
1039 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1043 * audit_syscall_exit - deallocate audit context after a system call
1044 * @tsk: task being audited
1045 * @valid: success/failure flag
1046 * @return_code: syscall return value
1048 * Tear down after system call. If the audit context has been marked as
1049 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1050 * filtering, or because some other part of the kernel write an audit
1051 * message), then write out the syscall information. In call cases,
1052 * free the names stored from getname().
1054 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1056 struct audit_context *context;
1058 get_task_struct(tsk);
1060 context = audit_get_context(tsk, valid, return_code);
1063 /* Not having a context here is ok, since the parent may have
1064 * called __put_task_struct. */
1065 if (likely(!context))
1068 if (context->in_syscall && context->auditable)
1069 audit_log_exit(context, GFP_KERNEL);
1071 context->in_syscall = 0;
1072 context->auditable = 0;
1074 if (context->previous) {
1075 struct audit_context *new_context = context->previous;
1076 context->previous = NULL;
1077 audit_free_context(context);
1078 tsk->audit_context = new_context;
1080 audit_free_names(context);
1081 audit_free_aux(context);
1082 tsk->audit_context = context;
1085 put_task_struct(tsk);
1089 * audit_getname - add a name to the list
1090 * @name: name to add
1092 * Add a name to the list of audit names for this context.
1093 * Called from fs/namei.c:getname().
1095 void audit_getname(const char *name)
1097 struct audit_context *context = current->audit_context;
1099 if (!context || IS_ERR(name) || !name)
1102 if (!context->in_syscall) {
1103 #if AUDIT_DEBUG == 2
1104 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1105 __FILE__, __LINE__, context->serial, name);
1110 BUG_ON(context->name_count >= AUDIT_NAMES);
1111 context->names[context->name_count].name = name;
1112 context->names[context->name_count].ino = (unsigned long)-1;
1113 ++context->name_count;
1114 if (!context->pwd) {
1115 read_lock(¤t->fs->lock);
1116 context->pwd = dget(current->fs->pwd);
1117 context->pwdmnt = mntget(current->fs->pwdmnt);
1118 read_unlock(¤t->fs->lock);
1123 /* audit_putname - intercept a putname request
1124 * @name: name to intercept and delay for putname
1126 * If we have stored the name from getname in the audit context,
1127 * then we delay the putname until syscall exit.
1128 * Called from include/linux/fs.h:putname().
1130 void audit_putname(const char *name)
1132 struct audit_context *context = current->audit_context;
1135 if (!context->in_syscall) {
1136 #if AUDIT_DEBUG == 2
1137 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1138 __FILE__, __LINE__, context->serial, name);
1139 if (context->name_count) {
1141 for (i = 0; i < context->name_count; i++)
1142 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1143 context->names[i].name,
1144 context->names[i].name);
1151 ++context->put_count;
1152 if (context->put_count > context->name_count) {
1153 printk(KERN_ERR "%s:%d(:%d): major=%d"
1154 " in_syscall=%d putname(%p) name_count=%d"
1157 context->serial, context->major,
1158 context->in_syscall, name, context->name_count,
1159 context->put_count);
1167 * audit_inode - store the inode and device from a lookup
1168 * @name: name being audited
1169 * @inode: inode being audited
1170 * @flags: lookup flags (as used in path_lookup())
1172 * Called from fs/namei.c:path_lookup().
1174 void audit_inode(const char *name, const struct inode *inode, unsigned flags)
1177 struct audit_context *context = current->audit_context;
1179 if (!context->in_syscall)
1181 if (context->name_count
1182 && context->names[context->name_count-1].name
1183 && context->names[context->name_count-1].name == name)
1184 idx = context->name_count - 1;
1185 else if (context->name_count > 1
1186 && context->names[context->name_count-2].name
1187 && context->names[context->name_count-2].name == name)
1188 idx = context->name_count - 2;
1190 /* FIXME: how much do we care about inodes that have no
1191 * associated name? */
1192 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1194 idx = context->name_count++;
1195 context->names[idx].name = NULL;
1197 ++context->ino_count;
1200 context->names[idx].flags = flags;
1201 context->names[idx].ino = inode->i_ino;
1202 context->names[idx].dev = inode->i_sb->s_dev;
1203 context->names[idx].mode = inode->i_mode;
1204 context->names[idx].uid = inode->i_uid;
1205 context->names[idx].gid = inode->i_gid;
1206 context->names[idx].rdev = inode->i_rdev;
1210 * auditsc_get_stamp - get local copies of audit_context values
1211 * @ctx: audit_context for the task
1212 * @t: timespec to store time recorded in the audit_context
1213 * @serial: serial value that is recorded in the audit_context
1215 * Also sets the context as auditable.
1217 void auditsc_get_stamp(struct audit_context *ctx,
1218 struct timespec *t, unsigned int *serial)
1221 ctx->serial = audit_serial();
1222 t->tv_sec = ctx->ctime.tv_sec;
1223 t->tv_nsec = ctx->ctime.tv_nsec;
1224 *serial = ctx->serial;
1229 * audit_set_loginuid - set a task's audit_context loginuid
1230 * @task: task whose audit context is being modified
1231 * @loginuid: loginuid value
1235 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1237 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1239 if (task->audit_context) {
1240 struct audit_buffer *ab;
1242 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1244 audit_log_format(ab, "login pid=%d uid=%u "
1245 "old auid=%u new auid=%u",
1246 task->pid, task->uid,
1247 task->audit_context->loginuid, loginuid);
1250 task->audit_context->loginuid = loginuid;
1256 * audit_get_loginuid - get the loginuid for an audit_context
1257 * @ctx: the audit_context
1259 * Returns the context's loginuid or -1 if @ctx is NULL.
1261 uid_t audit_get_loginuid(struct audit_context *ctx)
1263 return ctx ? ctx->loginuid : -1;
1267 * audit_ipc_perms - record audit data for ipc
1268 * @qbytes: msgq bytes
1269 * @uid: msgq user id
1270 * @gid: msgq group id
1271 * @mode: msgq mode (permissions)
1273 * Returns 0 for success or NULL context or < 0 on error.
1275 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1277 struct audit_aux_data_ipcctl *ax;
1278 struct audit_context *context = current->audit_context;
1280 if (likely(!context))
1283 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1287 ax->qbytes = qbytes;
1292 ax->d.type = AUDIT_IPC;
1293 ax->d.next = context->aux;
1294 context->aux = (void *)ax;
1299 * audit_socketcall - record audit data for sys_socketcall
1300 * @nargs: number of args
1303 * Returns 0 for success or NULL context or < 0 on error.
1305 int audit_socketcall(int nargs, unsigned long *args)
1307 struct audit_aux_data_socketcall *ax;
1308 struct audit_context *context = current->audit_context;
1310 if (likely(!context))
1313 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1318 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1320 ax->d.type = AUDIT_SOCKETCALL;
1321 ax->d.next = context->aux;
1322 context->aux = (void *)ax;
1327 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1328 * @len: data length in user space
1329 * @a: data address in kernel space
1331 * Returns 0 for success or NULL context or < 0 on error.
1333 int audit_sockaddr(int len, void *a)
1335 struct audit_aux_data_sockaddr *ax;
1336 struct audit_context *context = current->audit_context;
1338 if (likely(!context))
1341 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1346 memcpy(ax->a, a, len);
1348 ax->d.type = AUDIT_SOCKADDR;
1349 ax->d.next = context->aux;
1350 context->aux = (void *)ax;
1355 * audit_avc_path - record the granting or denial of permissions
1356 * @dentry: dentry to record
1357 * @mnt: mnt to record
1359 * Returns 0 for success or NULL context or < 0 on error.
1361 * Called from security/selinux/avc.c::avc_audit()
1363 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1365 struct audit_aux_data_path *ax;
1366 struct audit_context *context = current->audit_context;
1368 if (likely(!context))
1371 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1375 ax->dentry = dget(dentry);
1376 ax->mnt = mntget(mnt);
1378 ax->d.type = AUDIT_AVC_PATH;
1379 ax->d.next = context->aux;
1380 context->aux = (void *)ax;
1385 * audit_signal_info - record signal info for shutting down audit subsystem
1386 * @sig: signal value
1387 * @t: task being signaled
1389 * If the audit subsystem is being terminated, record the task (pid)
1390 * and uid that is doing that.
1392 void audit_signal_info(int sig, struct task_struct *t)
1394 extern pid_t audit_sig_pid;
1395 extern uid_t audit_sig_uid;
1397 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1398 if (sig == SIGTERM || sig == SIGHUP) {
1399 struct audit_context *ctx = current->audit_context;
1400 audit_sig_pid = current->pid;
1402 audit_sig_uid = ctx->loginuid;
1404 audit_sig_uid = current->uid;