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.
5 * Copyright (C) 2005 IBM Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24 * Many of the ideas implemented here are from Stephen C. Tweedie,
25 * especially the idea of avoiding a copy by using getname.
27 * The method for actual interception of syscall entry and exit (not in
28 * this file -- see entry.S) is based on a GPL'd patch written by
29 * okir@suse.de and Copyright 2003 SuSE Linux AG.
31 * The support of additional filter rules compares (>, <, >=, <=) was
32 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
36 #include <linux/init.h>
37 #include <asm/types.h>
38 #include <asm/atomic.h>
40 #include <linux/module.h>
41 #include <linux/mount.h>
42 #include <linux/socket.h>
43 #include <linux/audit.h>
44 #include <linux/personality.h>
45 #include <linux/time.h>
46 #include <linux/kthread.h>
47 #include <linux/netlink.h>
48 #include <linux/compiler.h>
49 #include <asm/unistd.h>
52 1 = put_count checking
53 2 = verbose put_count checking
57 /* No syscall auditing will take place unless audit_enabled != 0. */
58 extern int audit_enabled;
60 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
61 * for saving names from getname(). */
62 #define AUDIT_NAMES 20
64 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
65 * audit_context from being used for nameless inodes from
67 #define AUDIT_NAMES_RESERVED 7
69 /* At task start time, the audit_state is set in the audit_context using
70 a per-task filter. At syscall entry, the audit_state is augmented by
71 the syscall filter. */
73 AUDIT_DISABLED, /* Do not create per-task audit_context.
74 * No syscall-specific audit records can
76 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
77 * but don't necessarily fill it in at
78 * syscall entry time (i.e., filter
80 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
81 * and always fill it in at syscall
82 * entry time. This makes a full
83 * syscall record available if some
84 * other part of the kernel decides it
85 * should be recorded. */
86 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
87 * always fill it in at syscall entry
88 * time, and always write out the audit
89 * record at syscall exit time. */
92 /* When fs/namei.c:getname() is called, we store the pointer in name and
93 * we don't let putname() free it (instead we free all of the saved
94 * pointers at syscall exit time).
96 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
108 struct audit_aux_data {
109 struct audit_aux_data *next;
113 #define AUDIT_AUX_IPCPERM 0
115 struct audit_aux_data_ipcctl {
116 struct audit_aux_data d;
118 unsigned long qbytes;
124 struct audit_aux_data_socketcall {
125 struct audit_aux_data d;
127 unsigned long args[0];
130 struct audit_aux_data_sockaddr {
131 struct audit_aux_data d;
136 struct audit_aux_data_path {
137 struct audit_aux_data d;
138 struct dentry *dentry;
139 struct vfsmount *mnt;
142 /* The per-task audit context. */
143 struct audit_context {
144 int in_syscall; /* 1 if task is in a syscall */
145 enum audit_state state;
146 unsigned int serial; /* serial number for record */
147 struct timespec ctime; /* time of syscall entry */
148 uid_t loginuid; /* login uid (identity) */
149 int major; /* syscall number */
150 unsigned long argv[4]; /* syscall arguments */
151 int return_valid; /* return code is valid */
152 long return_code;/* syscall return code */
153 int auditable; /* 1 if record should be written */
155 struct audit_names names[AUDIT_NAMES];
157 struct vfsmount * pwdmnt;
158 struct audit_context *previous; /* For nested syscalls */
159 struct audit_aux_data *aux;
161 /* Save things to print about task_struct */
163 uid_t uid, euid, suid, fsuid;
164 gid_t gid, egid, sgid, fsgid;
165 unsigned long personality;
175 /* There are three lists of rules -- one to search at task creation
176 * time, one to search at syscall entry time, and another to search at
177 * syscall exit time. */
178 static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
179 LIST_HEAD_INIT(audit_filter_list[0]),
180 LIST_HEAD_INIT(audit_filter_list[1]),
181 LIST_HEAD_INIT(audit_filter_list[2]),
182 LIST_HEAD_INIT(audit_filter_list[3]),
183 LIST_HEAD_INIT(audit_filter_list[4]),
184 #if AUDIT_NR_FILTERS != 5
185 #error Fix audit_filter_list initialiser
190 struct list_head list;
192 struct audit_rule rule;
195 extern int audit_pid;
197 /* Copy rule from user-space to kernel-space. Called from
198 * audit_add_rule during AUDIT_ADD. */
199 static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
203 if (s->action != AUDIT_NEVER
204 && s->action != AUDIT_POSSIBLE
205 && s->action != AUDIT_ALWAYS)
207 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
209 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
213 d->action = s->action;
214 d->field_count = s->field_count;
215 for (i = 0; i < d->field_count; i++) {
216 d->fields[i] = s->fields[i];
217 d->values[i] = s->values[i];
219 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
223 /* Check to see if two rules are identical. It is called from
224 * audit_add_rule during AUDIT_ADD and
225 * audit_del_rule during AUDIT_DEL. */
226 static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
230 if (a->flags != b->flags)
233 if (a->action != b->action)
236 if (a->field_count != b->field_count)
239 for (i = 0; i < a->field_count; i++) {
240 if (a->fields[i] != b->fields[i]
241 || a->values[i] != b->values[i])
245 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
246 if (a->mask[i] != b->mask[i])
252 /* Note that audit_add_rule and audit_del_rule are called via
253 * audit_receive() in audit.c, and are protected by
254 * audit_netlink_sem. */
255 static inline int audit_add_rule(struct audit_rule *rule,
256 struct list_head *list)
258 struct audit_entry *entry;
261 /* Do not use the _rcu iterator here, since this is the only
262 * addition routine. */
263 list_for_each_entry(entry, list, list) {
264 if (!audit_compare_rule(rule, &entry->rule)) {
269 for (i = 0; i < rule->field_count; i++) {
270 if (rule->fields[i] & AUDIT_UNUSED_BITS)
272 if ( rule->fields[i] & AUDIT_NEGATE )
273 rule->fields[i] |= AUDIT_NOT_EQUAL;
274 else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
275 rule->fields[i] |= AUDIT_EQUAL;
276 rule->fields[i] &= (~AUDIT_NEGATE);
279 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
281 if (audit_copy_rule(&entry->rule, rule)) {
286 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
287 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
288 list_add_rcu(&entry->list, list);
290 list_add_tail_rcu(&entry->list, list);
296 static inline void audit_free_rule(struct rcu_head *head)
298 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
302 /* Note that audit_add_rule and audit_del_rule are called via
303 * audit_receive() in audit.c, and are protected by
304 * audit_netlink_sem. */
305 static inline int audit_del_rule(struct audit_rule *rule,
306 struct list_head *list)
308 struct audit_entry *e;
310 /* Do not use the _rcu iterator here, since this is the only
311 * deletion routine. */
312 list_for_each_entry(e, list, list) {
313 if (!audit_compare_rule(rule, &e->rule)) {
314 list_del_rcu(&e->list);
315 call_rcu(&e->rcu, audit_free_rule);
319 return -ENOENT; /* No matching rule */
322 static int audit_list_rules(void *_dest)
326 struct audit_entry *entry;
333 down(&audit_netlink_sem);
335 /* The *_rcu iterators not needed here because we are
336 always called with audit_netlink_sem held. */
337 for (i=0; i<AUDIT_NR_FILTERS; i++) {
338 list_for_each_entry(entry, &audit_filter_list[i], list)
339 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
340 &entry->rule, sizeof(entry->rule));
342 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
344 up(&audit_netlink_sem);
349 * audit_receive_filter - apply all rules to the specified message type
350 * @type: audit message type
351 * @pid: target pid for netlink audit messages
352 * @uid: target uid for netlink audit messages
353 * @seq: netlink audit message sequence (serial) number
354 * @data: payload data
355 * @loginuid: loginuid of sender
357 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
360 struct task_struct *tsk;
367 /* We can't just spew out the rules here because we might fill
368 * the available socket buffer space and deadlock waiting for
369 * auditctl to read from it... which isn't ever going to
370 * happen if we're actually running in the context of auditctl
371 * trying to _send_ the stuff */
373 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
379 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
386 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
387 if (listnr >= AUDIT_NR_FILTERS)
390 err = audit_add_rule(data, &audit_filter_list[listnr]);
392 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
393 "auid=%u added an audit rule\n", loginuid);
396 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
397 if (listnr >= AUDIT_NR_FILTERS)
400 err = audit_del_rule(data, &audit_filter_list[listnr]);
402 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
403 "auid=%u removed an audit rule\n", loginuid);
412 static int audit_comparator(const u32 left, const u32 op, const u32 right)
416 return (left == right);
417 case AUDIT_NOT_EQUAL:
418 return (left != right);
419 case AUDIT_LESS_THAN:
420 return (left < right);
421 case AUDIT_LESS_THAN_OR_EQUAL:
422 return (left <= right);
423 case AUDIT_GREATER_THAN:
424 return (left > right);
425 case AUDIT_GREATER_THAN_OR_EQUAL:
426 return (left >= right);
432 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
434 static int audit_filter_rules(struct task_struct *tsk,
435 struct audit_rule *rule,
436 struct audit_context *ctx,
437 enum audit_state *state)
441 for (i = 0; i < rule->field_count; i++) {
442 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
443 u32 op = rule->fields[i] & AUDIT_OPERATORS;
444 u32 value = rule->values[i];
449 result = audit_comparator(tsk->pid, op, value);
452 result = audit_comparator(tsk->uid, op, value);
455 result = audit_comparator(tsk->euid, op, value);
458 result = audit_comparator(tsk->suid, op, value);
461 result = audit_comparator(tsk->fsuid, op, value);
464 result = audit_comparator(tsk->gid, op, value);
467 result = audit_comparator(tsk->egid, op, value);
470 result = audit_comparator(tsk->sgid, op, value);
473 result = audit_comparator(tsk->fsgid, op, value);
476 result = audit_comparator(tsk->personality, op, value);
480 result = audit_comparator(ctx->arch, op, value);
484 if (ctx && ctx->return_valid)
485 result = audit_comparator(ctx->return_code, op, value);
488 if (ctx && ctx->return_valid) {
490 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
492 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
497 for (j = 0; j < ctx->name_count; j++) {
498 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
507 for (j = 0; j < ctx->name_count; j++) {
508 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
517 for (j = 0; j < ctx->name_count; j++) {
518 if (audit_comparator(ctx->names[j].ino, op, value)) {
528 result = audit_comparator(ctx->loginuid, op, value);
535 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
542 switch (rule->action) {
543 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
544 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
545 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
550 /* At process creation time, we can determine if system-call auditing is
551 * completely disabled for this task. Since we only have the task
552 * structure at this point, we can only check uid and gid.
554 static enum audit_state audit_filter_task(struct task_struct *tsk)
556 struct audit_entry *e;
557 enum audit_state state;
560 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
561 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
567 return AUDIT_BUILD_CONTEXT;
570 /* At syscall entry and exit time, this filter is called if the
571 * audit_state is not low enough that auditing cannot take place, but is
572 * also not high enough that we already know we have to write an audit
573 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
575 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
576 struct audit_context *ctx,
577 struct list_head *list)
579 struct audit_entry *e;
580 enum audit_state state;
582 if (audit_pid && tsk->tgid == audit_pid)
583 return AUDIT_DISABLED;
586 if (!list_empty(list)) {
587 int word = AUDIT_WORD(ctx->major);
588 int bit = AUDIT_BIT(ctx->major);
590 list_for_each_entry_rcu(e, list, list) {
591 if ((e->rule.mask[word] & bit) == bit
592 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
599 return AUDIT_BUILD_CONTEXT;
602 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
603 struct audit_rule *rule,
604 enum audit_state *state)
608 for (i = 0; i < rule->field_count; i++) {
609 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
610 u32 op = rule->fields[i] & AUDIT_OPERATORS;
611 u32 value = rule->values[i];
616 result = audit_comparator(cb->creds.pid, op, value);
619 result = audit_comparator(cb->creds.uid, op, value);
622 result = audit_comparator(cb->creds.gid, op, value);
625 result = audit_comparator(cb->loginuid, op, value);
632 switch (rule->action) {
633 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
634 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
635 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
640 int audit_filter_user(struct netlink_skb_parms *cb, int type)
642 struct audit_entry *e;
643 enum audit_state state;
647 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
648 if (audit_filter_user_rules(cb, &e->rule, &state)) {
649 if (state == AUDIT_DISABLED)
656 return ret; /* Audit by default */
659 /* This should be called with task_lock() held. */
660 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
664 struct audit_context *context = tsk->audit_context;
666 if (likely(!context))
668 context->return_valid = return_valid;
669 context->return_code = return_code;
671 if (context->in_syscall && !context->auditable) {
672 enum audit_state state;
673 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
674 if (state == AUDIT_RECORD_CONTEXT)
675 context->auditable = 1;
678 context->pid = tsk->pid;
679 context->uid = tsk->uid;
680 context->gid = tsk->gid;
681 context->euid = tsk->euid;
682 context->suid = tsk->suid;
683 context->fsuid = tsk->fsuid;
684 context->egid = tsk->egid;
685 context->sgid = tsk->sgid;
686 context->fsgid = tsk->fsgid;
687 context->personality = tsk->personality;
688 tsk->audit_context = NULL;
692 static inline void audit_free_names(struct audit_context *context)
697 if (context->auditable
698 ||context->put_count + context->ino_count != context->name_count) {
699 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
700 " name_count=%d put_count=%d"
701 " ino_count=%d [NOT freeing]\n",
703 context->serial, context->major, context->in_syscall,
704 context->name_count, context->put_count,
706 for (i = 0; i < context->name_count; i++)
707 printk(KERN_ERR "names[%d] = %p = %s\n", i,
708 context->names[i].name,
709 context->names[i].name);
715 context->put_count = 0;
716 context->ino_count = 0;
719 for (i = 0; i < context->name_count; i++)
720 if (context->names[i].name)
721 __putname(context->names[i].name);
722 context->name_count = 0;
726 mntput(context->pwdmnt);
728 context->pwdmnt = NULL;
731 static inline void audit_free_aux(struct audit_context *context)
733 struct audit_aux_data *aux;
735 while ((aux = context->aux)) {
736 if (aux->type == AUDIT_AVC_PATH) {
737 struct audit_aux_data_path *axi = (void *)aux;
741 context->aux = aux->next;
746 static inline void audit_zero_context(struct audit_context *context,
747 enum audit_state state)
749 uid_t loginuid = context->loginuid;
751 memset(context, 0, sizeof(*context));
752 context->state = state;
753 context->loginuid = loginuid;
756 static inline struct audit_context *audit_alloc_context(enum audit_state state)
758 struct audit_context *context;
760 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
762 audit_zero_context(context, state);
767 * audit_alloc - allocate an audit context block for a task
770 * Filter on the task information and allocate a per-task audit context
771 * if necessary. Doing so turns on system call auditing for the
772 * specified task. This is called from copy_process, so no lock is
775 int audit_alloc(struct task_struct *tsk)
777 struct audit_context *context;
778 enum audit_state state;
780 if (likely(!audit_enabled))
781 return 0; /* Return if not auditing. */
783 state = audit_filter_task(tsk);
784 if (likely(state == AUDIT_DISABLED))
787 if (!(context = audit_alloc_context(state))) {
788 audit_log_lost("out of memory in audit_alloc");
792 /* Preserve login uid */
793 context->loginuid = -1;
794 if (current->audit_context)
795 context->loginuid = current->audit_context->loginuid;
797 tsk->audit_context = context;
798 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
802 static inline void audit_free_context(struct audit_context *context)
804 struct audit_context *previous;
808 previous = context->previous;
809 if (previous || (count && count < 10)) {
811 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
812 " freeing multiple contexts (%d)\n",
813 context->serial, context->major,
814 context->name_count, count);
816 audit_free_names(context);
817 audit_free_aux(context);
822 printk(KERN_ERR "audit: freed %d contexts\n", count);
825 static void audit_log_task_info(struct audit_buffer *ab)
827 char name[sizeof(current->comm)];
828 struct mm_struct *mm = current->mm;
829 struct vm_area_struct *vma;
831 get_task_comm(name, current);
832 audit_log_format(ab, " comm=");
833 audit_log_untrustedstring(ab, name);
838 down_read(&mm->mmap_sem);
841 if ((vma->vm_flags & VM_EXECUTABLE) &&
843 audit_log_d_path(ab, "exe=",
844 vma->vm_file->f_dentry,
845 vma->vm_file->f_vfsmnt);
850 up_read(&mm->mmap_sem);
853 static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
856 struct audit_buffer *ab;
857 struct audit_aux_data *aux;
859 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
861 return; /* audit_panic has been called */
862 audit_log_format(ab, "arch=%x syscall=%d",
863 context->arch, context->major);
864 if (context->personality != PER_LINUX)
865 audit_log_format(ab, " per=%lx", context->personality);
866 if (context->return_valid)
867 audit_log_format(ab, " success=%s exit=%ld",
868 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
869 context->return_code);
871 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
872 " pid=%d auid=%u uid=%u gid=%u"
873 " euid=%u suid=%u fsuid=%u"
874 " egid=%u sgid=%u fsgid=%u",
884 context->euid, context->suid, context->fsuid,
885 context->egid, context->sgid, context->fsgid);
886 audit_log_task_info(ab);
889 for (aux = context->aux; aux; aux = aux->next) {
891 ab = audit_log_start(context, gfp_mask, aux->type);
893 continue; /* audit_panic has been called */
897 struct audit_aux_data_ipcctl *axi = (void *)aux;
899 " qbytes=%lx iuid=%u igid=%u mode=%x",
900 axi->qbytes, axi->uid, axi->gid, axi->mode);
903 case AUDIT_SOCKETCALL: {
905 struct audit_aux_data_socketcall *axs = (void *)aux;
906 audit_log_format(ab, "nargs=%d", axs->nargs);
907 for (i=0; i<axs->nargs; i++)
908 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
911 case AUDIT_SOCKADDR: {
912 struct audit_aux_data_sockaddr *axs = (void *)aux;
914 audit_log_format(ab, "saddr=");
915 audit_log_hex(ab, axs->a, axs->len);
918 case AUDIT_AVC_PATH: {
919 struct audit_aux_data_path *axi = (void *)aux;
920 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
927 if (context->pwd && context->pwdmnt) {
928 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
930 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
934 for (i = 0; i < context->name_count; i++) {
935 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
937 continue; /* audit_panic has been called */
939 audit_log_format(ab, "item=%d", i);
940 if (context->names[i].name) {
941 audit_log_format(ab, " name=");
942 audit_log_untrustedstring(ab, context->names[i].name);
944 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
946 if (context->names[i].ino != (unsigned long)-1)
947 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
948 " ouid=%u ogid=%u rdev=%02x:%02x",
949 context->names[i].ino,
950 MAJOR(context->names[i].dev),
951 MINOR(context->names[i].dev),
952 context->names[i].mode,
953 context->names[i].uid,
954 context->names[i].gid,
955 MAJOR(context->names[i].rdev),
956 MINOR(context->names[i].rdev));
962 * audit_free - free a per-task audit context
963 * @tsk: task whose audit context block to free
965 * Called from copy_process and __put_task_struct.
967 void audit_free(struct task_struct *tsk)
969 struct audit_context *context;
972 context = audit_get_context(tsk, 0, 0);
975 if (likely(!context))
978 /* Check for system calls that do not go through the exit
979 * function (e.g., exit_group), then free context block.
980 * We use GFP_ATOMIC here because we might be doing this
981 * in the context of the idle thread */
982 if (context->in_syscall && context->auditable)
983 audit_log_exit(context, GFP_ATOMIC);
985 audit_free_context(context);
989 * audit_syscall_entry - fill in an audit record at syscall entry
990 * @tsk: task being audited
991 * @arch: architecture type
992 * @major: major syscall type (function)
993 * @a1: additional syscall register 1
994 * @a2: additional syscall register 2
995 * @a3: additional syscall register 3
996 * @a4: additional syscall register 4
998 * Fill in audit context at syscall entry. This only happens if the
999 * audit context was created when the task was created and the state or
1000 * filters demand the audit context be built. If the state from the
1001 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1002 * then the record will be written at syscall exit time (otherwise, it
1003 * will only be written if another part of the kernel requests that it
1006 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
1007 unsigned long a1, unsigned long a2,
1008 unsigned long a3, unsigned long a4)
1010 struct audit_context *context = tsk->audit_context;
1011 enum audit_state state;
1016 * This happens only on certain architectures that make system
1017 * calls in kernel_thread via the entry.S interface, instead of
1018 * with direct calls. (If you are porting to a new
1019 * architecture, hitting this condition can indicate that you
1020 * got the _exit/_leave calls backward in entry.S.)
1024 * ppc64 yes (see arch/ppc64/kernel/misc.S)
1026 * This also happens with vm86 emulation in a non-nested manner
1027 * (entries without exits), so this case must be caught.
1029 if (context->in_syscall) {
1030 struct audit_context *newctx;
1034 "audit(:%d) pid=%d in syscall=%d;"
1035 " entering syscall=%d\n",
1036 context->serial, tsk->pid, context->major, major);
1038 newctx = audit_alloc_context(context->state);
1040 newctx->previous = context;
1042 tsk->audit_context = newctx;
1044 /* If we can't alloc a new context, the best we
1045 * can do is to leak memory (any pending putname
1046 * will be lost). The only other alternative is
1047 * to abandon auditing. */
1048 audit_zero_context(context, context->state);
1051 BUG_ON(context->in_syscall || context->name_count);
1056 context->arch = arch;
1057 context->major = major;
1058 context->argv[0] = a1;
1059 context->argv[1] = a2;
1060 context->argv[2] = a3;
1061 context->argv[3] = a4;
1063 state = context->state;
1064 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
1065 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1066 if (likely(state == AUDIT_DISABLED))
1069 context->serial = 0;
1070 context->ctime = CURRENT_TIME;
1071 context->in_syscall = 1;
1072 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1076 * audit_syscall_exit - deallocate audit context after a system call
1077 * @tsk: task being audited
1078 * @valid: success/failure flag
1079 * @return_code: syscall return value
1081 * Tear down after system call. If the audit context has been marked as
1082 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1083 * filtering, or because some other part of the kernel write an audit
1084 * message), then write out the syscall information. In call cases,
1085 * free the names stored from getname().
1087 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1089 struct audit_context *context;
1091 get_task_struct(tsk);
1093 context = audit_get_context(tsk, valid, return_code);
1096 /* Not having a context here is ok, since the parent may have
1097 * called __put_task_struct. */
1098 if (likely(!context))
1101 if (context->in_syscall && context->auditable)
1102 audit_log_exit(context, GFP_KERNEL);
1104 context->in_syscall = 0;
1105 context->auditable = 0;
1107 if (context->previous) {
1108 struct audit_context *new_context = context->previous;
1109 context->previous = NULL;
1110 audit_free_context(context);
1111 tsk->audit_context = new_context;
1113 audit_free_names(context);
1114 audit_free_aux(context);
1115 tsk->audit_context = context;
1118 put_task_struct(tsk);
1122 * audit_getname - add a name to the list
1123 * @name: name to add
1125 * Add a name to the list of audit names for this context.
1126 * Called from fs/namei.c:getname().
1128 void audit_getname(const char *name)
1130 struct audit_context *context = current->audit_context;
1132 if (!context || IS_ERR(name) || !name)
1135 if (!context->in_syscall) {
1136 #if AUDIT_DEBUG == 2
1137 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1138 __FILE__, __LINE__, context->serial, name);
1143 BUG_ON(context->name_count >= AUDIT_NAMES);
1144 context->names[context->name_count].name = name;
1145 context->names[context->name_count].ino = (unsigned long)-1;
1146 ++context->name_count;
1147 if (!context->pwd) {
1148 read_lock(¤t->fs->lock);
1149 context->pwd = dget(current->fs->pwd);
1150 context->pwdmnt = mntget(current->fs->pwdmnt);
1151 read_unlock(¤t->fs->lock);
1156 /* audit_putname - intercept a putname request
1157 * @name: name to intercept and delay for putname
1159 * If we have stored the name from getname in the audit context,
1160 * then we delay the putname until syscall exit.
1161 * Called from include/linux/fs.h:putname().
1163 void audit_putname(const char *name)
1165 struct audit_context *context = current->audit_context;
1168 if (!context->in_syscall) {
1169 #if AUDIT_DEBUG == 2
1170 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1171 __FILE__, __LINE__, context->serial, name);
1172 if (context->name_count) {
1174 for (i = 0; i < context->name_count; i++)
1175 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1176 context->names[i].name,
1177 context->names[i].name);
1184 ++context->put_count;
1185 if (context->put_count > context->name_count) {
1186 printk(KERN_ERR "%s:%d(:%d): major=%d"
1187 " in_syscall=%d putname(%p) name_count=%d"
1190 context->serial, context->major,
1191 context->in_syscall, name, context->name_count,
1192 context->put_count);
1200 * audit_inode - store the inode and device from a lookup
1201 * @name: name being audited
1202 * @inode: inode being audited
1203 * @flags: lookup flags (as used in path_lookup())
1205 * Called from fs/namei.c:path_lookup().
1207 void audit_inode(const char *name, const struct inode *inode, unsigned flags)
1210 struct audit_context *context = current->audit_context;
1212 if (!context->in_syscall)
1214 if (context->name_count
1215 && context->names[context->name_count-1].name
1216 && context->names[context->name_count-1].name == name)
1217 idx = context->name_count - 1;
1218 else if (context->name_count > 1
1219 && context->names[context->name_count-2].name
1220 && context->names[context->name_count-2].name == name)
1221 idx = context->name_count - 2;
1223 /* FIXME: how much do we care about inodes that have no
1224 * associated name? */
1225 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1227 idx = context->name_count++;
1228 context->names[idx].name = NULL;
1230 ++context->ino_count;
1233 context->names[idx].flags = flags;
1234 context->names[idx].ino = inode->i_ino;
1235 context->names[idx].dev = inode->i_sb->s_dev;
1236 context->names[idx].mode = inode->i_mode;
1237 context->names[idx].uid = inode->i_uid;
1238 context->names[idx].gid = inode->i_gid;
1239 context->names[idx].rdev = inode->i_rdev;
1243 * auditsc_get_stamp - get local copies of audit_context values
1244 * @ctx: audit_context for the task
1245 * @t: timespec to store time recorded in the audit_context
1246 * @serial: serial value that is recorded in the audit_context
1248 * Also sets the context as auditable.
1250 void auditsc_get_stamp(struct audit_context *ctx,
1251 struct timespec *t, unsigned int *serial)
1254 ctx->serial = audit_serial();
1255 t->tv_sec = ctx->ctime.tv_sec;
1256 t->tv_nsec = ctx->ctime.tv_nsec;
1257 *serial = ctx->serial;
1262 * audit_set_loginuid - set a task's audit_context loginuid
1263 * @task: task whose audit context is being modified
1264 * @loginuid: loginuid value
1268 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1270 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1272 if (task->audit_context) {
1273 struct audit_buffer *ab;
1275 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1277 audit_log_format(ab, "login pid=%d uid=%u "
1278 "old auid=%u new auid=%u",
1279 task->pid, task->uid,
1280 task->audit_context->loginuid, loginuid);
1283 task->audit_context->loginuid = loginuid;
1289 * audit_get_loginuid - get the loginuid for an audit_context
1290 * @ctx: the audit_context
1292 * Returns the context's loginuid or -1 if @ctx is NULL.
1294 uid_t audit_get_loginuid(struct audit_context *ctx)
1296 return ctx ? ctx->loginuid : -1;
1300 * audit_ipc_perms - record audit data for ipc
1301 * @qbytes: msgq bytes
1302 * @uid: msgq user id
1303 * @gid: msgq group id
1304 * @mode: msgq mode (permissions)
1306 * Returns 0 for success or NULL context or < 0 on error.
1308 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1310 struct audit_aux_data_ipcctl *ax;
1311 struct audit_context *context = current->audit_context;
1313 if (likely(!context))
1316 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1320 ax->qbytes = qbytes;
1325 ax->d.type = AUDIT_IPC;
1326 ax->d.next = context->aux;
1327 context->aux = (void *)ax;
1332 * audit_socketcall - record audit data for sys_socketcall
1333 * @nargs: number of args
1336 * Returns 0 for success or NULL context or < 0 on error.
1338 int audit_socketcall(int nargs, unsigned long *args)
1340 struct audit_aux_data_socketcall *ax;
1341 struct audit_context *context = current->audit_context;
1343 if (likely(!context))
1346 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1351 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1353 ax->d.type = AUDIT_SOCKETCALL;
1354 ax->d.next = context->aux;
1355 context->aux = (void *)ax;
1360 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1361 * @len: data length in user space
1362 * @a: data address in kernel space
1364 * Returns 0 for success or NULL context or < 0 on error.
1366 int audit_sockaddr(int len, void *a)
1368 struct audit_aux_data_sockaddr *ax;
1369 struct audit_context *context = current->audit_context;
1371 if (likely(!context))
1374 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1379 memcpy(ax->a, a, len);
1381 ax->d.type = AUDIT_SOCKADDR;
1382 ax->d.next = context->aux;
1383 context->aux = (void *)ax;
1388 * audit_avc_path - record the granting or denial of permissions
1389 * @dentry: dentry to record
1390 * @mnt: mnt to record
1392 * Returns 0 for success or NULL context or < 0 on error.
1394 * Called from security/selinux/avc.c::avc_audit()
1396 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1398 struct audit_aux_data_path *ax;
1399 struct audit_context *context = current->audit_context;
1401 if (likely(!context))
1404 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1408 ax->dentry = dget(dentry);
1409 ax->mnt = mntget(mnt);
1411 ax->d.type = AUDIT_AVC_PATH;
1412 ax->d.next = context->aux;
1413 context->aux = (void *)ax;
1418 * audit_signal_info - record signal info for shutting down audit subsystem
1419 * @sig: signal value
1420 * @t: task being signaled
1422 * If the audit subsystem is being terminated, record the task (pid)
1423 * and uid that is doing that.
1425 void audit_signal_info(int sig, struct task_struct *t)
1427 extern pid_t audit_sig_pid;
1428 extern uid_t audit_sig_uid;
1430 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1431 if (sig == SIGTERM || sig == SIGHUP) {
1432 struct audit_context *ctx = current->audit_context;
1433 audit_sig_pid = current->pid;
1435 audit_sig_uid = ctx->loginuid;
1437 audit_sig_uid = current->uid;