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 2005 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2005 IBM Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
32 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
35 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
39 #include <linux/init.h>
40 #include <asm/types.h>
41 #include <asm/atomic.h>
42 #include <asm/types.h>
44 #include <linux/namei.h>
46 #include <linux/module.h>
47 #include <linux/mount.h>
48 #include <linux/socket.h>
49 #include <linux/audit.h>
50 #include <linux/personality.h>
51 #include <linux/time.h>
52 #include <linux/kthread.h>
53 #include <linux/netlink.h>
54 #include <linux/compiler.h>
55 #include <asm/unistd.h>
58 1 = put_count checking
59 2 = verbose put_count checking
63 /* No syscall auditing will take place unless audit_enabled != 0. */
64 extern int audit_enabled;
66 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
67 * for saving names from getname(). */
68 #define AUDIT_NAMES 20
70 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
71 * audit_context from being used for nameless inodes from
73 #define AUDIT_NAMES_RESERVED 7
75 /* At task start time, the audit_state is set in the audit_context using
76 a per-task filter. At syscall entry, the audit_state is augmented by
77 the syscall filter. */
79 AUDIT_DISABLED, /* Do not create per-task audit_context.
80 * No syscall-specific audit records can
82 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
83 * but don't necessarily fill it in at
84 * syscall entry time (i.e., filter
86 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
87 * and always fill it in at syscall
88 * entry time. This makes a full
89 * syscall record available if some
90 * other part of the kernel decides it
91 * should be recorded. */
92 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
93 * always fill it in at syscall entry
94 * time, and always write out the audit
95 * record at syscall exit time. */
98 /* When fs/namei.c:getname() is called, we store the pointer in name and
99 * we don't let putname() free it (instead we free all of the saved
100 * pointers at syscall exit time).
102 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
114 struct audit_aux_data {
115 struct audit_aux_data *next;
119 #define AUDIT_AUX_IPCPERM 0
121 struct audit_aux_data_ipcctl {
122 struct audit_aux_data d;
124 unsigned long qbytes;
130 struct audit_aux_data_socketcall {
131 struct audit_aux_data d;
133 unsigned long args[0];
136 struct audit_aux_data_sockaddr {
137 struct audit_aux_data d;
142 struct audit_aux_data_path {
143 struct audit_aux_data d;
144 struct dentry *dentry;
145 struct vfsmount *mnt;
148 /* The per-task audit context. */
149 struct audit_context {
150 int in_syscall; /* 1 if task is in a syscall */
151 enum audit_state state;
152 unsigned int serial; /* serial number for record */
153 struct timespec ctime; /* time of syscall entry */
154 uid_t loginuid; /* login uid (identity) */
155 int major; /* syscall number */
156 unsigned long argv[4]; /* syscall arguments */
157 int return_valid; /* return code is valid */
158 long return_code;/* syscall return code */
159 int auditable; /* 1 if record should be written */
161 struct audit_names names[AUDIT_NAMES];
163 struct vfsmount * pwdmnt;
164 struct audit_context *previous; /* For nested syscalls */
165 struct audit_aux_data *aux;
167 /* Save things to print about task_struct */
169 uid_t uid, euid, suid, fsuid;
170 gid_t gid, egid, sgid, fsgid;
171 unsigned long personality;
181 /* There are three lists of rules -- one to search at task creation
182 * time, one to search at syscall entry time, and another to search at
183 * syscall exit time. */
184 static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
185 LIST_HEAD_INIT(audit_filter_list[0]),
186 LIST_HEAD_INIT(audit_filter_list[1]),
187 LIST_HEAD_INIT(audit_filter_list[2]),
188 LIST_HEAD_INIT(audit_filter_list[3]),
189 LIST_HEAD_INIT(audit_filter_list[4]),
190 #if AUDIT_NR_FILTERS != 5
191 #error Fix audit_filter_list initialiser
196 struct list_head list;
198 struct audit_rule rule;
201 extern int audit_pid;
203 /* Copy rule from user-space to kernel-space. Called from
204 * audit_add_rule during AUDIT_ADD. */
205 static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
209 if (s->action != AUDIT_NEVER
210 && s->action != AUDIT_POSSIBLE
211 && s->action != AUDIT_ALWAYS)
213 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
215 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
219 d->action = s->action;
220 d->field_count = s->field_count;
221 for (i = 0; i < d->field_count; i++) {
222 d->fields[i] = s->fields[i];
223 d->values[i] = s->values[i];
225 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
229 /* Check to see if two rules are identical. It is called from
230 * audit_add_rule during AUDIT_ADD and
231 * audit_del_rule during AUDIT_DEL. */
232 static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
236 if (a->flags != b->flags)
239 if (a->action != b->action)
242 if (a->field_count != b->field_count)
245 for (i = 0; i < a->field_count; i++) {
246 if (a->fields[i] != b->fields[i]
247 || a->values[i] != b->values[i])
251 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
252 if (a->mask[i] != b->mask[i])
258 /* Note that audit_add_rule and audit_del_rule are called via
259 * audit_receive() in audit.c, and are protected by
260 * audit_netlink_sem. */
261 static inline int audit_add_rule(struct audit_rule *rule,
262 struct list_head *list)
264 struct audit_entry *entry;
267 /* Do not use the _rcu iterator here, since this is the only
268 * addition routine. */
269 list_for_each_entry(entry, list, list) {
270 if (!audit_compare_rule(rule, &entry->rule)) {
275 for (i = 0; i < rule->field_count; i++) {
276 if (rule->fields[i] & AUDIT_UNUSED_BITS)
278 if ( rule->fields[i] & AUDIT_NEGATE )
279 rule->fields[i] |= AUDIT_NOT_EQUAL;
280 else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
281 rule->fields[i] |= AUDIT_EQUAL;
282 rule->fields[i] &= (~AUDIT_NEGATE);
285 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
287 if (audit_copy_rule(&entry->rule, rule)) {
292 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
293 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
294 list_add_rcu(&entry->list, list);
296 list_add_tail_rcu(&entry->list, list);
302 static inline void audit_free_rule(struct rcu_head *head)
304 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
308 /* Note that audit_add_rule and audit_del_rule are called via
309 * audit_receive() in audit.c, and are protected by
310 * audit_netlink_sem. */
311 static inline int audit_del_rule(struct audit_rule *rule,
312 struct list_head *list)
314 struct audit_entry *e;
316 /* Do not use the _rcu iterator here, since this is the only
317 * deletion routine. */
318 list_for_each_entry(e, list, list) {
319 if (!audit_compare_rule(rule, &e->rule)) {
320 list_del_rcu(&e->list);
321 call_rcu(&e->rcu, audit_free_rule);
325 return -ENOENT; /* No matching rule */
328 static int audit_list_rules(void *_dest)
332 struct audit_entry *entry;
339 down(&audit_netlink_sem);
341 /* The *_rcu iterators not needed here because we are
342 always called with audit_netlink_sem held. */
343 for (i=0; i<AUDIT_NR_FILTERS; i++) {
344 list_for_each_entry(entry, &audit_filter_list[i], list)
345 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
346 &entry->rule, sizeof(entry->rule));
348 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
350 up(&audit_netlink_sem);
355 * audit_receive_filter - apply all rules to the specified message type
356 * @type: audit message type
357 * @pid: target pid for netlink audit messages
358 * @uid: target uid for netlink audit messages
359 * @seq: netlink audit message sequence (serial) number
360 * @data: payload data
361 * @loginuid: loginuid of sender
363 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
366 struct task_struct *tsk;
373 /* We can't just spew out the rules here because we might fill
374 * the available socket buffer space and deadlock waiting for
375 * auditctl to read from it... which isn't ever going to
376 * happen if we're actually running in the context of auditctl
377 * trying to _send_ the stuff */
379 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
385 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
392 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
393 if (listnr >= AUDIT_NR_FILTERS)
396 err = audit_add_rule(data, &audit_filter_list[listnr]);
398 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
399 "auid=%u added an audit rule\n", loginuid);
402 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
403 if (listnr >= AUDIT_NR_FILTERS)
406 err = audit_del_rule(data, &audit_filter_list[listnr]);
408 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
409 "auid=%u removed an audit rule\n", loginuid);
418 static int audit_comparator(const u32 left, const u32 op, const u32 right)
422 return (left == right);
423 case AUDIT_NOT_EQUAL:
424 return (left != right);
425 case AUDIT_LESS_THAN:
426 return (left < right);
427 case AUDIT_LESS_THAN_OR_EQUAL:
428 return (left <= right);
429 case AUDIT_GREATER_THAN:
430 return (left > right);
431 case AUDIT_GREATER_THAN_OR_EQUAL:
432 return (left >= right);
438 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
440 static int audit_filter_rules(struct task_struct *tsk,
441 struct audit_rule *rule,
442 struct audit_context *ctx,
443 enum audit_state *state)
447 for (i = 0; i < rule->field_count; i++) {
448 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
449 u32 op = rule->fields[i] & AUDIT_OPERATORS;
450 u32 value = rule->values[i];
455 result = audit_comparator(tsk->pid, op, value);
458 result = audit_comparator(tsk->uid, op, value);
461 result = audit_comparator(tsk->euid, op, value);
464 result = audit_comparator(tsk->suid, op, value);
467 result = audit_comparator(tsk->fsuid, op, value);
470 result = audit_comparator(tsk->gid, op, value);
473 result = audit_comparator(tsk->egid, op, value);
476 result = audit_comparator(tsk->sgid, op, value);
479 result = audit_comparator(tsk->fsgid, op, value);
482 result = audit_comparator(tsk->personality, op, value);
486 result = audit_comparator(ctx->arch, op, value);
490 if (ctx && ctx->return_valid)
491 result = audit_comparator(ctx->return_code, op, value);
494 if (ctx && ctx->return_valid) {
496 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
498 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
503 for (j = 0; j < ctx->name_count; j++) {
504 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
513 for (j = 0; j < ctx->name_count; j++) {
514 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
523 for (j = 0; j < ctx->name_count; j++) {
524 if (audit_comparator(ctx->names[j].ino, op, value) ||
525 audit_comparator(ctx->names[j].pino, op, value)) {
535 result = audit_comparator(ctx->loginuid, op, value);
542 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
549 switch (rule->action) {
550 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
551 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
552 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
557 /* At process creation time, we can determine if system-call auditing is
558 * completely disabled for this task. Since we only have the task
559 * structure at this point, we can only check uid and gid.
561 static enum audit_state audit_filter_task(struct task_struct *tsk)
563 struct audit_entry *e;
564 enum audit_state state;
567 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
568 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
574 return AUDIT_BUILD_CONTEXT;
577 /* At syscall entry and exit time, this filter is called if the
578 * audit_state is not low enough that auditing cannot take place, but is
579 * also not high enough that we already know we have to write an audit
580 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
582 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
583 struct audit_context *ctx,
584 struct list_head *list)
586 struct audit_entry *e;
587 enum audit_state state;
589 if (audit_pid && tsk->tgid == audit_pid)
590 return AUDIT_DISABLED;
593 if (!list_empty(list)) {
594 int word = AUDIT_WORD(ctx->major);
595 int bit = AUDIT_BIT(ctx->major);
597 list_for_each_entry_rcu(e, list, list) {
598 if ((e->rule.mask[word] & bit) == bit
599 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
606 return AUDIT_BUILD_CONTEXT;
609 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
610 struct audit_rule *rule,
611 enum audit_state *state)
615 for (i = 0; i < rule->field_count; i++) {
616 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
617 u32 op = rule->fields[i] & AUDIT_OPERATORS;
618 u32 value = rule->values[i];
623 result = audit_comparator(cb->creds.pid, op, value);
626 result = audit_comparator(cb->creds.uid, op, value);
629 result = audit_comparator(cb->creds.gid, op, value);
632 result = audit_comparator(cb->loginuid, op, value);
639 switch (rule->action) {
640 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
641 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
642 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
647 int audit_filter_user(struct netlink_skb_parms *cb, int type)
649 struct audit_entry *e;
650 enum audit_state state;
654 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
655 if (audit_filter_user_rules(cb, &e->rule, &state)) {
656 if (state == AUDIT_DISABLED)
663 return ret; /* Audit by default */
666 /* This should be called with task_lock() held. */
667 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
671 struct audit_context *context = tsk->audit_context;
673 if (likely(!context))
675 context->return_valid = return_valid;
676 context->return_code = return_code;
678 if (context->in_syscall && !context->auditable) {
679 enum audit_state state;
680 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
681 if (state == AUDIT_RECORD_CONTEXT)
682 context->auditable = 1;
685 context->pid = tsk->pid;
686 context->uid = tsk->uid;
687 context->gid = tsk->gid;
688 context->euid = tsk->euid;
689 context->suid = tsk->suid;
690 context->fsuid = tsk->fsuid;
691 context->egid = tsk->egid;
692 context->sgid = tsk->sgid;
693 context->fsgid = tsk->fsgid;
694 context->personality = tsk->personality;
695 tsk->audit_context = NULL;
699 static inline void audit_free_names(struct audit_context *context)
704 if (context->auditable
705 ||context->put_count + context->ino_count != context->name_count) {
706 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
707 " name_count=%d put_count=%d"
708 " ino_count=%d [NOT freeing]\n",
710 context->serial, context->major, context->in_syscall,
711 context->name_count, context->put_count,
713 for (i = 0; i < context->name_count; i++)
714 printk(KERN_ERR "names[%d] = %p = %s\n", i,
715 context->names[i].name,
716 context->names[i].name ?: "(null)");
722 context->put_count = 0;
723 context->ino_count = 0;
726 for (i = 0; i < context->name_count; i++)
727 if (context->names[i].name)
728 __putname(context->names[i].name);
729 context->name_count = 0;
733 mntput(context->pwdmnt);
735 context->pwdmnt = NULL;
738 static inline void audit_free_aux(struct audit_context *context)
740 struct audit_aux_data *aux;
742 while ((aux = context->aux)) {
743 if (aux->type == AUDIT_AVC_PATH) {
744 struct audit_aux_data_path *axi = (void *)aux;
748 context->aux = aux->next;
753 static inline void audit_zero_context(struct audit_context *context,
754 enum audit_state state)
756 uid_t loginuid = context->loginuid;
758 memset(context, 0, sizeof(*context));
759 context->state = state;
760 context->loginuid = loginuid;
763 static inline struct audit_context *audit_alloc_context(enum audit_state state)
765 struct audit_context *context;
767 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
769 audit_zero_context(context, state);
774 * audit_alloc - allocate an audit context block for a task
777 * Filter on the task information and allocate a per-task audit context
778 * if necessary. Doing so turns on system call auditing for the
779 * specified task. This is called from copy_process, so no lock is
782 int audit_alloc(struct task_struct *tsk)
784 struct audit_context *context;
785 enum audit_state state;
787 if (likely(!audit_enabled))
788 return 0; /* Return if not auditing. */
790 state = audit_filter_task(tsk);
791 if (likely(state == AUDIT_DISABLED))
794 if (!(context = audit_alloc_context(state))) {
795 audit_log_lost("out of memory in audit_alloc");
799 /* Preserve login uid */
800 context->loginuid = -1;
801 if (current->audit_context)
802 context->loginuid = current->audit_context->loginuid;
804 tsk->audit_context = context;
805 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
809 static inline void audit_free_context(struct audit_context *context)
811 struct audit_context *previous;
815 previous = context->previous;
816 if (previous || (count && count < 10)) {
818 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
819 " freeing multiple contexts (%d)\n",
820 context->serial, context->major,
821 context->name_count, count);
823 audit_free_names(context);
824 audit_free_aux(context);
829 printk(KERN_ERR "audit: freed %d contexts\n", count);
832 static void audit_log_task_info(struct audit_buffer *ab)
834 char name[sizeof(current->comm)];
835 struct mm_struct *mm = current->mm;
836 struct vm_area_struct *vma;
838 get_task_comm(name, current);
839 audit_log_format(ab, " comm=");
840 audit_log_untrustedstring(ab, name);
845 down_read(&mm->mmap_sem);
848 if ((vma->vm_flags & VM_EXECUTABLE) &&
850 audit_log_d_path(ab, "exe=",
851 vma->vm_file->f_dentry,
852 vma->vm_file->f_vfsmnt);
857 up_read(&mm->mmap_sem);
860 static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
863 struct audit_buffer *ab;
864 struct audit_aux_data *aux;
866 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
868 return; /* audit_panic has been called */
869 audit_log_format(ab, "arch=%x syscall=%d",
870 context->arch, context->major);
871 if (context->personality != PER_LINUX)
872 audit_log_format(ab, " per=%lx", context->personality);
873 if (context->return_valid)
874 audit_log_format(ab, " success=%s exit=%ld",
875 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
876 context->return_code);
878 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
879 " pid=%d auid=%u uid=%u gid=%u"
880 " euid=%u suid=%u fsuid=%u"
881 " egid=%u sgid=%u fsgid=%u",
891 context->euid, context->suid, context->fsuid,
892 context->egid, context->sgid, context->fsgid);
893 audit_log_task_info(ab);
896 for (aux = context->aux; aux; aux = aux->next) {
898 ab = audit_log_start(context, gfp_mask, aux->type);
900 continue; /* audit_panic has been called */
904 struct audit_aux_data_ipcctl *axi = (void *)aux;
906 " qbytes=%lx iuid=%u igid=%u mode=%x",
907 axi->qbytes, axi->uid, axi->gid, axi->mode);
910 case AUDIT_SOCKETCALL: {
912 struct audit_aux_data_socketcall *axs = (void *)aux;
913 audit_log_format(ab, "nargs=%d", axs->nargs);
914 for (i=0; i<axs->nargs; i++)
915 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
918 case AUDIT_SOCKADDR: {
919 struct audit_aux_data_sockaddr *axs = (void *)aux;
921 audit_log_format(ab, "saddr=");
922 audit_log_hex(ab, axs->a, axs->len);
925 case AUDIT_AVC_PATH: {
926 struct audit_aux_data_path *axi = (void *)aux;
927 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
934 if (context->pwd && context->pwdmnt) {
935 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
937 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
941 for (i = 0; i < context->name_count; i++) {
942 unsigned long ino = context->names[i].ino;
943 unsigned long pino = context->names[i].pino;
945 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
947 continue; /* audit_panic has been called */
949 audit_log_format(ab, "item=%d", i);
951 audit_log_format(ab, " name=");
952 if (context->names[i].name)
953 audit_log_untrustedstring(ab, context->names[i].name);
955 audit_log_format(ab, "(null)");
957 if (pino != (unsigned long)-1)
958 audit_log_format(ab, " parent=%lu", pino);
959 if (ino != (unsigned long)-1)
960 audit_log_format(ab, " inode=%lu", ino);
961 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
962 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
963 " ouid=%u ogid=%u rdev=%02x:%02x",
964 MAJOR(context->names[i].dev),
965 MINOR(context->names[i].dev),
966 context->names[i].mode,
967 context->names[i].uid,
968 context->names[i].gid,
969 MAJOR(context->names[i].rdev),
970 MINOR(context->names[i].rdev));
976 * audit_free - free a per-task audit context
977 * @tsk: task whose audit context block to free
979 * Called from copy_process and __put_task_struct.
981 void audit_free(struct task_struct *tsk)
983 struct audit_context *context;
986 context = audit_get_context(tsk, 0, 0);
989 if (likely(!context))
992 /* Check for system calls that do not go through the exit
993 * function (e.g., exit_group), then free context block.
994 * We use GFP_ATOMIC here because we might be doing this
995 * in the context of the idle thread */
996 if (context->in_syscall && context->auditable)
997 audit_log_exit(context, GFP_ATOMIC);
999 audit_free_context(context);
1003 * audit_syscall_entry - fill in an audit record at syscall entry
1004 * @tsk: task being audited
1005 * @arch: architecture type
1006 * @major: major syscall type (function)
1007 * @a1: additional syscall register 1
1008 * @a2: additional syscall register 2
1009 * @a3: additional syscall register 3
1010 * @a4: additional syscall register 4
1012 * Fill in audit context at syscall entry. This only happens if the
1013 * audit context was created when the task was created and the state or
1014 * filters demand the audit context be built. If the state from the
1015 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1016 * then the record will be written at syscall exit time (otherwise, it
1017 * will only be written if another part of the kernel requests that it
1020 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
1021 unsigned long a1, unsigned long a2,
1022 unsigned long a3, unsigned long a4)
1024 struct audit_context *context = tsk->audit_context;
1025 enum audit_state state;
1030 * This happens only on certain architectures that make system
1031 * calls in kernel_thread via the entry.S interface, instead of
1032 * with direct calls. (If you are porting to a new
1033 * architecture, hitting this condition can indicate that you
1034 * got the _exit/_leave calls backward in entry.S.)
1038 * ppc64 yes (see arch/ppc64/kernel/misc.S)
1040 * This also happens with vm86 emulation in a non-nested manner
1041 * (entries without exits), so this case must be caught.
1043 if (context->in_syscall) {
1044 struct audit_context *newctx;
1048 "audit(:%d) pid=%d in syscall=%d;"
1049 " entering syscall=%d\n",
1050 context->serial, tsk->pid, context->major, major);
1052 newctx = audit_alloc_context(context->state);
1054 newctx->previous = context;
1056 tsk->audit_context = newctx;
1058 /* If we can't alloc a new context, the best we
1059 * can do is to leak memory (any pending putname
1060 * will be lost). The only other alternative is
1061 * to abandon auditing. */
1062 audit_zero_context(context, context->state);
1065 BUG_ON(context->in_syscall || context->name_count);
1070 context->arch = arch;
1071 context->major = major;
1072 context->argv[0] = a1;
1073 context->argv[1] = a2;
1074 context->argv[2] = a3;
1075 context->argv[3] = a4;
1077 state = context->state;
1078 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
1079 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1080 if (likely(state == AUDIT_DISABLED))
1083 context->serial = 0;
1084 context->ctime = CURRENT_TIME;
1085 context->in_syscall = 1;
1086 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1090 * audit_syscall_exit - deallocate audit context after a system call
1091 * @tsk: task being audited
1092 * @valid: success/failure flag
1093 * @return_code: syscall return value
1095 * Tear down after system call. If the audit context has been marked as
1096 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1097 * filtering, or because some other part of the kernel write an audit
1098 * message), then write out the syscall information. In call cases,
1099 * free the names stored from getname().
1101 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1103 struct audit_context *context;
1105 get_task_struct(tsk);
1107 context = audit_get_context(tsk, valid, return_code);
1110 /* Not having a context here is ok, since the parent may have
1111 * called __put_task_struct. */
1112 if (likely(!context))
1115 if (context->in_syscall && context->auditable)
1116 audit_log_exit(context, GFP_KERNEL);
1118 context->in_syscall = 0;
1119 context->auditable = 0;
1121 if (context->previous) {
1122 struct audit_context *new_context = context->previous;
1123 context->previous = NULL;
1124 audit_free_context(context);
1125 tsk->audit_context = new_context;
1127 audit_free_names(context);
1128 audit_free_aux(context);
1129 tsk->audit_context = context;
1132 put_task_struct(tsk);
1136 * audit_getname - add a name to the list
1137 * @name: name to add
1139 * Add a name to the list of audit names for this context.
1140 * Called from fs/namei.c:getname().
1142 void audit_getname(const char *name)
1144 struct audit_context *context = current->audit_context;
1146 if (!context || IS_ERR(name) || !name)
1149 if (!context->in_syscall) {
1150 #if AUDIT_DEBUG == 2
1151 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1152 __FILE__, __LINE__, context->serial, name);
1157 BUG_ON(context->name_count >= AUDIT_NAMES);
1158 context->names[context->name_count].name = name;
1159 context->names[context->name_count].ino = (unsigned long)-1;
1160 ++context->name_count;
1161 if (!context->pwd) {
1162 read_lock(¤t->fs->lock);
1163 context->pwd = dget(current->fs->pwd);
1164 context->pwdmnt = mntget(current->fs->pwdmnt);
1165 read_unlock(¤t->fs->lock);
1170 /* audit_putname - intercept a putname request
1171 * @name: name to intercept and delay for putname
1173 * If we have stored the name from getname in the audit context,
1174 * then we delay the putname until syscall exit.
1175 * Called from include/linux/fs.h:putname().
1177 void audit_putname(const char *name)
1179 struct audit_context *context = current->audit_context;
1182 if (!context->in_syscall) {
1183 #if AUDIT_DEBUG == 2
1184 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1185 __FILE__, __LINE__, context->serial, name);
1186 if (context->name_count) {
1188 for (i = 0; i < context->name_count; i++)
1189 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1190 context->names[i].name,
1191 context->names[i].name ?: "(null)");
1198 ++context->put_count;
1199 if (context->put_count > context->name_count) {
1200 printk(KERN_ERR "%s:%d(:%d): major=%d"
1201 " in_syscall=%d putname(%p) name_count=%d"
1204 context->serial, context->major,
1205 context->in_syscall, name, context->name_count,
1206 context->put_count);
1214 * audit_inode - store the inode and device from a lookup
1215 * @name: name being audited
1216 * @inode: inode being audited
1217 * @flags: lookup flags (as used in path_lookup())
1219 * Called from fs/namei.c:path_lookup().
1221 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1224 struct audit_context *context = current->audit_context;
1226 if (!context->in_syscall)
1228 if (context->name_count
1229 && context->names[context->name_count-1].name
1230 && context->names[context->name_count-1].name == name)
1231 idx = context->name_count - 1;
1232 else if (context->name_count > 1
1233 && context->names[context->name_count-2].name
1234 && context->names[context->name_count-2].name == name)
1235 idx = context->name_count - 2;
1237 /* FIXME: how much do we care about inodes that have no
1238 * associated name? */
1239 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1241 idx = context->name_count++;
1242 context->names[idx].name = NULL;
1244 ++context->ino_count;
1247 context->names[idx].dev = inode->i_sb->s_dev;
1248 context->names[idx].mode = inode->i_mode;
1249 context->names[idx].uid = inode->i_uid;
1250 context->names[idx].gid = inode->i_gid;
1251 context->names[idx].rdev = inode->i_rdev;
1252 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1253 (strcmp(name, ".") != 0)) {
1254 context->names[idx].ino = (unsigned long)-1;
1255 context->names[idx].pino = inode->i_ino;
1257 context->names[idx].ino = inode->i_ino;
1258 context->names[idx].pino = (unsigned long)-1;
1263 * audit_inode_child - collect inode info for created/removed objects
1264 * @dname: inode's dentry name
1265 * @inode: inode being audited
1266 * @pino: inode number of dentry parent
1268 * For syscalls that create or remove filesystem objects, audit_inode
1269 * can only collect information for the filesystem object's parent.
1270 * This call updates the audit context with the child's information.
1271 * Syscalls that create a new filesystem object must be hooked after
1272 * the object is created. Syscalls that remove a filesystem object
1273 * must be hooked prior, in order to capture the target inode during
1274 * unsuccessful attempts.
1276 void __audit_inode_child(const char *dname, const struct inode *inode,
1280 struct audit_context *context = current->audit_context;
1282 if (!context->in_syscall)
1285 /* determine matching parent */
1287 for (idx = 0; idx < context->name_count; idx++)
1288 if (context->names[idx].pino == pino) {
1290 const char *name = context->names[idx].name;
1291 int dlen = strlen(dname);
1292 int nlen = name ? strlen(name) : 0;
1297 /* disregard trailing slashes */
1298 n = name + nlen - 1;
1299 while ((*n == '/') && (n > name))
1302 /* find last path component */
1306 else if (n > name) {
1313 if (strncmp(n, dname, dlen) == 0)
1314 goto update_context;
1317 /* catch-all in case match not found */
1318 idx = context->name_count++;
1319 context->names[idx].name = NULL;
1320 context->names[idx].pino = pino;
1322 context->ino_count++;
1327 context->names[idx].ino = inode->i_ino;
1328 context->names[idx].dev = inode->i_sb->s_dev;
1329 context->names[idx].mode = inode->i_mode;
1330 context->names[idx].uid = inode->i_uid;
1331 context->names[idx].gid = inode->i_gid;
1332 context->names[idx].rdev = inode->i_rdev;
1337 * auditsc_get_stamp - get local copies of audit_context values
1338 * @ctx: audit_context for the task
1339 * @t: timespec to store time recorded in the audit_context
1340 * @serial: serial value that is recorded in the audit_context
1342 * Also sets the context as auditable.
1344 void auditsc_get_stamp(struct audit_context *ctx,
1345 struct timespec *t, unsigned int *serial)
1348 ctx->serial = audit_serial();
1349 t->tv_sec = ctx->ctime.tv_sec;
1350 t->tv_nsec = ctx->ctime.tv_nsec;
1351 *serial = ctx->serial;
1356 * audit_set_loginuid - set a task's audit_context loginuid
1357 * @task: task whose audit context is being modified
1358 * @loginuid: loginuid value
1362 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1364 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1366 if (task->audit_context) {
1367 struct audit_buffer *ab;
1369 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1371 audit_log_format(ab, "login pid=%d uid=%u "
1372 "old auid=%u new auid=%u",
1373 task->pid, task->uid,
1374 task->audit_context->loginuid, loginuid);
1377 task->audit_context->loginuid = loginuid;
1383 * audit_get_loginuid - get the loginuid for an audit_context
1384 * @ctx: the audit_context
1386 * Returns the context's loginuid or -1 if @ctx is NULL.
1388 uid_t audit_get_loginuid(struct audit_context *ctx)
1390 return ctx ? ctx->loginuid : -1;
1394 * audit_ipc_perms - record audit data for ipc
1395 * @qbytes: msgq bytes
1396 * @uid: msgq user id
1397 * @gid: msgq group id
1398 * @mode: msgq mode (permissions)
1400 * Returns 0 for success or NULL context or < 0 on error.
1402 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1404 struct audit_aux_data_ipcctl *ax;
1405 struct audit_context *context = current->audit_context;
1407 if (likely(!context))
1410 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1414 ax->qbytes = qbytes;
1419 ax->d.type = AUDIT_IPC;
1420 ax->d.next = context->aux;
1421 context->aux = (void *)ax;
1426 * audit_socketcall - record audit data for sys_socketcall
1427 * @nargs: number of args
1430 * Returns 0 for success or NULL context or < 0 on error.
1432 int audit_socketcall(int nargs, unsigned long *args)
1434 struct audit_aux_data_socketcall *ax;
1435 struct audit_context *context = current->audit_context;
1437 if (likely(!context))
1440 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1445 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1447 ax->d.type = AUDIT_SOCKETCALL;
1448 ax->d.next = context->aux;
1449 context->aux = (void *)ax;
1454 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1455 * @len: data length in user space
1456 * @a: data address in kernel space
1458 * Returns 0 for success or NULL context or < 0 on error.
1460 int audit_sockaddr(int len, void *a)
1462 struct audit_aux_data_sockaddr *ax;
1463 struct audit_context *context = current->audit_context;
1465 if (likely(!context))
1468 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1473 memcpy(ax->a, a, len);
1475 ax->d.type = AUDIT_SOCKADDR;
1476 ax->d.next = context->aux;
1477 context->aux = (void *)ax;
1482 * audit_avc_path - record the granting or denial of permissions
1483 * @dentry: dentry to record
1484 * @mnt: mnt to record
1486 * Returns 0 for success or NULL context or < 0 on error.
1488 * Called from security/selinux/avc.c::avc_audit()
1490 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1492 struct audit_aux_data_path *ax;
1493 struct audit_context *context = current->audit_context;
1495 if (likely(!context))
1498 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1502 ax->dentry = dget(dentry);
1503 ax->mnt = mntget(mnt);
1505 ax->d.type = AUDIT_AVC_PATH;
1506 ax->d.next = context->aux;
1507 context->aux = (void *)ax;
1512 * audit_signal_info - record signal info for shutting down audit subsystem
1513 * @sig: signal value
1514 * @t: task being signaled
1516 * If the audit subsystem is being terminated, record the task (pid)
1517 * and uid that is doing that.
1519 void audit_signal_info(int sig, struct task_struct *t)
1521 extern pid_t audit_sig_pid;
1522 extern uid_t audit_sig_uid;
1524 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1525 if (sig == SIGTERM || sig == SIGHUP) {
1526 struct audit_context *ctx = current->audit_context;
1527 audit_sig_pid = current->pid;
1529 audit_sig_uid = ctx->loginuid;
1531 audit_sig_uid = current->uid;