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 LIST_HEAD_INIT(audit_filter_list[5]),
191 #if AUDIT_NR_FILTERS != 6
192 #error Fix audit_filter_list initialiser
197 struct list_head list;
199 struct audit_rule rule;
202 extern int audit_pid;
204 /* Copy rule from user-space to kernel-space. Called from
205 * audit_add_rule during AUDIT_ADD. */
206 static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
210 if (s->action != AUDIT_NEVER
211 && s->action != AUDIT_POSSIBLE
212 && s->action != AUDIT_ALWAYS)
214 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
216 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
220 d->action = s->action;
221 d->field_count = s->field_count;
222 for (i = 0; i < d->field_count; i++) {
223 d->fields[i] = s->fields[i];
224 d->values[i] = s->values[i];
226 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
230 /* Check to see if two rules are identical. It is called from
231 * audit_add_rule during AUDIT_ADD and
232 * audit_del_rule during AUDIT_DEL. */
233 static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
237 if (a->flags != b->flags)
240 if (a->action != b->action)
243 if (a->field_count != b->field_count)
246 for (i = 0; i < a->field_count; i++) {
247 if (a->fields[i] != b->fields[i]
248 || a->values[i] != b->values[i])
252 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
253 if (a->mask[i] != b->mask[i])
259 /* Note that audit_add_rule and audit_del_rule are called via
260 * audit_receive() in audit.c, and are protected by
261 * audit_netlink_sem. */
262 static inline int audit_add_rule(struct audit_rule *rule,
263 struct list_head *list)
265 struct audit_entry *entry;
268 /* Do not use the _rcu iterator here, since this is the only
269 * addition routine. */
270 list_for_each_entry(entry, list, list) {
271 if (!audit_compare_rule(rule, &entry->rule)) {
276 for (i = 0; i < rule->field_count; i++) {
277 if (rule->fields[i] & AUDIT_UNUSED_BITS)
279 if ( rule->fields[i] & AUDIT_NEGATE )
280 rule->fields[i] |= AUDIT_NOT_EQUAL;
281 else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
282 rule->fields[i] |= AUDIT_EQUAL;
283 rule->fields[i] &= (~AUDIT_NEGATE);
286 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
288 if (audit_copy_rule(&entry->rule, rule)) {
293 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
294 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
295 list_add_rcu(&entry->list, list);
297 list_add_tail_rcu(&entry->list, list);
303 static inline void audit_free_rule(struct rcu_head *head)
305 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
309 /* Note that audit_add_rule and audit_del_rule are called via
310 * audit_receive() in audit.c, and are protected by
311 * audit_netlink_sem. */
312 static inline int audit_del_rule(struct audit_rule *rule,
313 struct list_head *list)
315 struct audit_entry *e;
317 /* Do not use the _rcu iterator here, since this is the only
318 * deletion routine. */
319 list_for_each_entry(e, list, list) {
320 if (!audit_compare_rule(rule, &e->rule)) {
321 list_del_rcu(&e->list);
322 call_rcu(&e->rcu, audit_free_rule);
326 return -ENOENT; /* No matching rule */
329 static int audit_list_rules(void *_dest)
333 struct audit_entry *entry;
340 down(&audit_netlink_sem);
342 /* The *_rcu iterators not needed here because we are
343 always called with audit_netlink_sem held. */
344 for (i=0; i<AUDIT_NR_FILTERS; i++) {
345 list_for_each_entry(entry, &audit_filter_list[i], list)
346 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
347 &entry->rule, sizeof(entry->rule));
349 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
351 up(&audit_netlink_sem);
356 * audit_receive_filter - apply all rules to the specified message type
357 * @type: audit message type
358 * @pid: target pid for netlink audit messages
359 * @uid: target uid for netlink audit messages
360 * @seq: netlink audit message sequence (serial) number
361 * @data: payload data
362 * @loginuid: loginuid of sender
364 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
367 struct task_struct *tsk;
374 /* We can't just spew out the rules here because we might fill
375 * the available socket buffer space and deadlock waiting for
376 * auditctl to read from it... which isn't ever going to
377 * happen if we're actually running in the context of auditctl
378 * trying to _send_ the stuff */
380 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
386 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
393 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
394 if (listnr >= AUDIT_NR_FILTERS)
397 err = audit_add_rule(data, &audit_filter_list[listnr]);
399 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
400 "auid=%u added an audit rule\n", loginuid);
403 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
404 if (listnr >= AUDIT_NR_FILTERS)
407 err = audit_del_rule(data, &audit_filter_list[listnr]);
409 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
410 "auid=%u removed an audit rule\n", loginuid);
419 static int audit_comparator(const u32 left, const u32 op, const u32 right)
423 return (left == right);
424 case AUDIT_NOT_EQUAL:
425 return (left != right);
426 case AUDIT_LESS_THAN:
427 return (left < right);
428 case AUDIT_LESS_THAN_OR_EQUAL:
429 return (left <= right);
430 case AUDIT_GREATER_THAN:
431 return (left > right);
432 case AUDIT_GREATER_THAN_OR_EQUAL:
433 return (left >= right);
439 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
441 static int audit_filter_rules(struct task_struct *tsk,
442 struct audit_rule *rule,
443 struct audit_context *ctx,
444 enum audit_state *state)
448 for (i = 0; i < rule->field_count; i++) {
449 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
450 u32 op = rule->fields[i] & AUDIT_OPERATORS;
451 u32 value = rule->values[i];
456 result = audit_comparator(tsk->pid, op, value);
459 result = audit_comparator(tsk->uid, op, value);
462 result = audit_comparator(tsk->euid, op, value);
465 result = audit_comparator(tsk->suid, op, value);
468 result = audit_comparator(tsk->fsuid, op, value);
471 result = audit_comparator(tsk->gid, op, value);
474 result = audit_comparator(tsk->egid, op, value);
477 result = audit_comparator(tsk->sgid, op, value);
480 result = audit_comparator(tsk->fsgid, op, value);
483 result = audit_comparator(tsk->personality, op, value);
487 result = audit_comparator(ctx->arch, op, value);
491 if (ctx && ctx->return_valid)
492 result = audit_comparator(ctx->return_code, op, value);
495 if (ctx && ctx->return_valid) {
497 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
499 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
504 for (j = 0; j < ctx->name_count; j++) {
505 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
514 for (j = 0; j < ctx->name_count; j++) {
515 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
524 for (j = 0; j < ctx->name_count; j++) {
525 if (audit_comparator(ctx->names[j].ino, op, value) ||
526 audit_comparator(ctx->names[j].pino, op, value)) {
536 result = audit_comparator(ctx->loginuid, op, value);
543 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
550 switch (rule->action) {
551 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
552 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
553 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
558 /* At process creation time, we can determine if system-call auditing is
559 * completely disabled for this task. Since we only have the task
560 * structure at this point, we can only check uid and gid.
562 static enum audit_state audit_filter_task(struct task_struct *tsk)
564 struct audit_entry *e;
565 enum audit_state state;
568 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
569 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
575 return AUDIT_BUILD_CONTEXT;
578 /* At syscall entry and exit time, this filter is called if the
579 * audit_state is not low enough that auditing cannot take place, but is
580 * also not high enough that we already know we have to write an audit
581 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
583 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
584 struct audit_context *ctx,
585 struct list_head *list)
587 struct audit_entry *e;
588 enum audit_state state;
590 if (audit_pid && tsk->tgid == audit_pid)
591 return AUDIT_DISABLED;
594 if (!list_empty(list)) {
595 int word = AUDIT_WORD(ctx->major);
596 int bit = AUDIT_BIT(ctx->major);
598 list_for_each_entry_rcu(e, list, list) {
599 if ((e->rule.mask[word] & bit) == bit
600 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
607 return AUDIT_BUILD_CONTEXT;
610 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
611 struct audit_rule *rule,
612 enum audit_state *state)
616 for (i = 0; i < rule->field_count; i++) {
617 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
618 u32 op = rule->fields[i] & AUDIT_OPERATORS;
619 u32 value = rule->values[i];
624 result = audit_comparator(cb->creds.pid, op, value);
627 result = audit_comparator(cb->creds.uid, op, value);
630 result = audit_comparator(cb->creds.gid, op, value);
633 result = audit_comparator(cb->loginuid, op, value);
640 switch (rule->action) {
641 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
642 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
643 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
648 int audit_filter_user(struct netlink_skb_parms *cb, int type)
650 struct audit_entry *e;
651 enum audit_state state;
655 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
656 if (audit_filter_user_rules(cb, &e->rule, &state)) {
657 if (state == AUDIT_DISABLED)
664 return ret; /* Audit by default */
667 int audit_filter_type(int type)
669 struct audit_entry *e;
673 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
674 goto unlock_and_return;
676 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
678 struct audit_rule *rule = &e->rule;
680 for (i = 0; i < rule->field_count; i++) {
681 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
682 u32 op = rule->fields[i] & AUDIT_OPERATORS;
683 u32 value = rule->values[i];
684 if ( field == AUDIT_MSGTYPE ) {
685 result = audit_comparator(type, op, value);
691 goto unlock_and_return;
699 /* This should be called with task_lock() held. */
700 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
704 struct audit_context *context = tsk->audit_context;
706 if (likely(!context))
708 context->return_valid = return_valid;
709 context->return_code = return_code;
711 if (context->in_syscall && !context->auditable) {
712 enum audit_state state;
713 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
714 if (state == AUDIT_RECORD_CONTEXT)
715 context->auditable = 1;
718 context->pid = tsk->pid;
719 context->uid = tsk->uid;
720 context->gid = tsk->gid;
721 context->euid = tsk->euid;
722 context->suid = tsk->suid;
723 context->fsuid = tsk->fsuid;
724 context->egid = tsk->egid;
725 context->sgid = tsk->sgid;
726 context->fsgid = tsk->fsgid;
727 context->personality = tsk->personality;
728 tsk->audit_context = NULL;
732 static inline void audit_free_names(struct audit_context *context)
737 if (context->auditable
738 ||context->put_count + context->ino_count != context->name_count) {
739 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
740 " name_count=%d put_count=%d"
741 " ino_count=%d [NOT freeing]\n",
743 context->serial, context->major, context->in_syscall,
744 context->name_count, context->put_count,
746 for (i = 0; i < context->name_count; i++)
747 printk(KERN_ERR "names[%d] = %p = %s\n", i,
748 context->names[i].name,
749 context->names[i].name ?: "(null)");
755 context->put_count = 0;
756 context->ino_count = 0;
759 for (i = 0; i < context->name_count; i++)
760 if (context->names[i].name)
761 __putname(context->names[i].name);
762 context->name_count = 0;
766 mntput(context->pwdmnt);
768 context->pwdmnt = NULL;
771 static inline void audit_free_aux(struct audit_context *context)
773 struct audit_aux_data *aux;
775 while ((aux = context->aux)) {
776 if (aux->type == AUDIT_AVC_PATH) {
777 struct audit_aux_data_path *axi = (void *)aux;
781 context->aux = aux->next;
786 static inline void audit_zero_context(struct audit_context *context,
787 enum audit_state state)
789 uid_t loginuid = context->loginuid;
791 memset(context, 0, sizeof(*context));
792 context->state = state;
793 context->loginuid = loginuid;
796 static inline struct audit_context *audit_alloc_context(enum audit_state state)
798 struct audit_context *context;
800 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
802 audit_zero_context(context, state);
807 * audit_alloc - allocate an audit context block for a task
810 * Filter on the task information and allocate a per-task audit context
811 * if necessary. Doing so turns on system call auditing for the
812 * specified task. This is called from copy_process, so no lock is
815 int audit_alloc(struct task_struct *tsk)
817 struct audit_context *context;
818 enum audit_state state;
820 if (likely(!audit_enabled))
821 return 0; /* Return if not auditing. */
823 state = audit_filter_task(tsk);
824 if (likely(state == AUDIT_DISABLED))
827 if (!(context = audit_alloc_context(state))) {
828 audit_log_lost("out of memory in audit_alloc");
832 /* Preserve login uid */
833 context->loginuid = -1;
834 if (current->audit_context)
835 context->loginuid = current->audit_context->loginuid;
837 tsk->audit_context = context;
838 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
842 static inline void audit_free_context(struct audit_context *context)
844 struct audit_context *previous;
848 previous = context->previous;
849 if (previous || (count && count < 10)) {
851 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
852 " freeing multiple contexts (%d)\n",
853 context->serial, context->major,
854 context->name_count, count);
856 audit_free_names(context);
857 audit_free_aux(context);
862 printk(KERN_ERR "audit: freed %d contexts\n", count);
865 static void audit_log_task_info(struct audit_buffer *ab)
867 char name[sizeof(current->comm)];
868 struct mm_struct *mm = current->mm;
869 struct vm_area_struct *vma;
871 get_task_comm(name, current);
872 audit_log_format(ab, " comm=");
873 audit_log_untrustedstring(ab, name);
878 down_read(&mm->mmap_sem);
881 if ((vma->vm_flags & VM_EXECUTABLE) &&
883 audit_log_d_path(ab, "exe=",
884 vma->vm_file->f_dentry,
885 vma->vm_file->f_vfsmnt);
890 up_read(&mm->mmap_sem);
893 static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
896 struct audit_buffer *ab;
897 struct audit_aux_data *aux;
899 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
901 return; /* audit_panic has been called */
902 audit_log_format(ab, "arch=%x syscall=%d",
903 context->arch, context->major);
904 if (context->personality != PER_LINUX)
905 audit_log_format(ab, " per=%lx", context->personality);
906 if (context->return_valid)
907 audit_log_format(ab, " success=%s exit=%ld",
908 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
909 context->return_code);
911 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
912 " pid=%d auid=%u uid=%u gid=%u"
913 " euid=%u suid=%u fsuid=%u"
914 " egid=%u sgid=%u fsgid=%u",
924 context->euid, context->suid, context->fsuid,
925 context->egid, context->sgid, context->fsgid);
926 audit_log_task_info(ab);
929 for (aux = context->aux; aux; aux = aux->next) {
931 ab = audit_log_start(context, gfp_mask, aux->type);
933 continue; /* audit_panic has been called */
937 struct audit_aux_data_ipcctl *axi = (void *)aux;
939 " qbytes=%lx iuid=%u igid=%u mode=%x",
940 axi->qbytes, axi->uid, axi->gid, axi->mode);
943 case AUDIT_SOCKETCALL: {
945 struct audit_aux_data_socketcall *axs = (void *)aux;
946 audit_log_format(ab, "nargs=%d", axs->nargs);
947 for (i=0; i<axs->nargs; i++)
948 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
951 case AUDIT_SOCKADDR: {
952 struct audit_aux_data_sockaddr *axs = (void *)aux;
954 audit_log_format(ab, "saddr=");
955 audit_log_hex(ab, axs->a, axs->len);
958 case AUDIT_AVC_PATH: {
959 struct audit_aux_data_path *axi = (void *)aux;
960 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
967 if (context->pwd && context->pwdmnt) {
968 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
970 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
974 for (i = 0; i < context->name_count; i++) {
975 unsigned long ino = context->names[i].ino;
976 unsigned long pino = context->names[i].pino;
978 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
980 continue; /* audit_panic has been called */
982 audit_log_format(ab, "item=%d", i);
984 audit_log_format(ab, " name=");
985 if (context->names[i].name)
986 audit_log_untrustedstring(ab, context->names[i].name);
988 audit_log_format(ab, "(null)");
990 if (pino != (unsigned long)-1)
991 audit_log_format(ab, " parent=%lu", pino);
992 if (ino != (unsigned long)-1)
993 audit_log_format(ab, " inode=%lu", ino);
994 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
995 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
996 " ouid=%u ogid=%u rdev=%02x:%02x",
997 MAJOR(context->names[i].dev),
998 MINOR(context->names[i].dev),
999 context->names[i].mode,
1000 context->names[i].uid,
1001 context->names[i].gid,
1002 MAJOR(context->names[i].rdev),
1003 MINOR(context->names[i].rdev));
1009 * audit_free - free a per-task audit context
1010 * @tsk: task whose audit context block to free
1012 * Called from copy_process and __put_task_struct.
1014 void audit_free(struct task_struct *tsk)
1016 struct audit_context *context;
1019 context = audit_get_context(tsk, 0, 0);
1022 if (likely(!context))
1025 /* Check for system calls that do not go through the exit
1026 * function (e.g., exit_group), then free context block.
1027 * We use GFP_ATOMIC here because we might be doing this
1028 * in the context of the idle thread */
1029 if (context->in_syscall && context->auditable)
1030 audit_log_exit(context, GFP_ATOMIC);
1032 audit_free_context(context);
1036 * audit_syscall_entry - fill in an audit record at syscall entry
1037 * @tsk: task being audited
1038 * @arch: architecture type
1039 * @major: major syscall type (function)
1040 * @a1: additional syscall register 1
1041 * @a2: additional syscall register 2
1042 * @a3: additional syscall register 3
1043 * @a4: additional syscall register 4
1045 * Fill in audit context at syscall entry. This only happens if the
1046 * audit context was created when the task was created and the state or
1047 * filters demand the audit context be built. If the state from the
1048 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1049 * then the record will be written at syscall exit time (otherwise, it
1050 * will only be written if another part of the kernel requests that it
1053 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
1054 unsigned long a1, unsigned long a2,
1055 unsigned long a3, unsigned long a4)
1057 struct audit_context *context = tsk->audit_context;
1058 enum audit_state state;
1063 * This happens only on certain architectures that make system
1064 * calls in kernel_thread via the entry.S interface, instead of
1065 * with direct calls. (If you are porting to a new
1066 * architecture, hitting this condition can indicate that you
1067 * got the _exit/_leave calls backward in entry.S.)
1071 * ppc64 yes (see arch/ppc64/kernel/misc.S)
1073 * This also happens with vm86 emulation in a non-nested manner
1074 * (entries without exits), so this case must be caught.
1076 if (context->in_syscall) {
1077 struct audit_context *newctx;
1081 "audit(:%d) pid=%d in syscall=%d;"
1082 " entering syscall=%d\n",
1083 context->serial, tsk->pid, context->major, major);
1085 newctx = audit_alloc_context(context->state);
1087 newctx->previous = context;
1089 tsk->audit_context = newctx;
1091 /* If we can't alloc a new context, the best we
1092 * can do is to leak memory (any pending putname
1093 * will be lost). The only other alternative is
1094 * to abandon auditing. */
1095 audit_zero_context(context, context->state);
1098 BUG_ON(context->in_syscall || context->name_count);
1103 context->arch = arch;
1104 context->major = major;
1105 context->argv[0] = a1;
1106 context->argv[1] = a2;
1107 context->argv[2] = a3;
1108 context->argv[3] = a4;
1110 state = context->state;
1111 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
1112 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1113 if (likely(state == AUDIT_DISABLED))
1116 context->serial = 0;
1117 context->ctime = CURRENT_TIME;
1118 context->in_syscall = 1;
1119 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1123 * audit_syscall_exit - deallocate audit context after a system call
1124 * @tsk: task being audited
1125 * @valid: success/failure flag
1126 * @return_code: syscall return value
1128 * Tear down after system call. If the audit context has been marked as
1129 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1130 * filtering, or because some other part of the kernel write an audit
1131 * message), then write out the syscall information. In call cases,
1132 * free the names stored from getname().
1134 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1136 struct audit_context *context;
1138 get_task_struct(tsk);
1140 context = audit_get_context(tsk, valid, return_code);
1143 /* Not having a context here is ok, since the parent may have
1144 * called __put_task_struct. */
1145 if (likely(!context))
1148 if (context->in_syscall && context->auditable)
1149 audit_log_exit(context, GFP_KERNEL);
1151 context->in_syscall = 0;
1152 context->auditable = 0;
1154 if (context->previous) {
1155 struct audit_context *new_context = context->previous;
1156 context->previous = NULL;
1157 audit_free_context(context);
1158 tsk->audit_context = new_context;
1160 audit_free_names(context);
1161 audit_free_aux(context);
1162 tsk->audit_context = context;
1165 put_task_struct(tsk);
1169 * audit_getname - add a name to the list
1170 * @name: name to add
1172 * Add a name to the list of audit names for this context.
1173 * Called from fs/namei.c:getname().
1175 void audit_getname(const char *name)
1177 struct audit_context *context = current->audit_context;
1179 if (!context || IS_ERR(name) || !name)
1182 if (!context->in_syscall) {
1183 #if AUDIT_DEBUG == 2
1184 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1185 __FILE__, __LINE__, context->serial, name);
1190 BUG_ON(context->name_count >= AUDIT_NAMES);
1191 context->names[context->name_count].name = name;
1192 context->names[context->name_count].ino = (unsigned long)-1;
1193 ++context->name_count;
1194 if (!context->pwd) {
1195 read_lock(¤t->fs->lock);
1196 context->pwd = dget(current->fs->pwd);
1197 context->pwdmnt = mntget(current->fs->pwdmnt);
1198 read_unlock(¤t->fs->lock);
1203 /* audit_putname - intercept a putname request
1204 * @name: name to intercept and delay for putname
1206 * If we have stored the name from getname in the audit context,
1207 * then we delay the putname until syscall exit.
1208 * Called from include/linux/fs.h:putname().
1210 void audit_putname(const char *name)
1212 struct audit_context *context = current->audit_context;
1215 if (!context->in_syscall) {
1216 #if AUDIT_DEBUG == 2
1217 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1218 __FILE__, __LINE__, context->serial, name);
1219 if (context->name_count) {
1221 for (i = 0; i < context->name_count; i++)
1222 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1223 context->names[i].name,
1224 context->names[i].name ?: "(null)");
1231 ++context->put_count;
1232 if (context->put_count > context->name_count) {
1233 printk(KERN_ERR "%s:%d(:%d): major=%d"
1234 " in_syscall=%d putname(%p) name_count=%d"
1237 context->serial, context->major,
1238 context->in_syscall, name, context->name_count,
1239 context->put_count);
1247 * audit_inode - store the inode and device from a lookup
1248 * @name: name being audited
1249 * @inode: inode being audited
1250 * @flags: lookup flags (as used in path_lookup())
1252 * Called from fs/namei.c:path_lookup().
1254 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1257 struct audit_context *context = current->audit_context;
1259 if (!context->in_syscall)
1261 if (context->name_count
1262 && context->names[context->name_count-1].name
1263 && context->names[context->name_count-1].name == name)
1264 idx = context->name_count - 1;
1265 else if (context->name_count > 1
1266 && context->names[context->name_count-2].name
1267 && context->names[context->name_count-2].name == name)
1268 idx = context->name_count - 2;
1270 /* FIXME: how much do we care about inodes that have no
1271 * associated name? */
1272 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1274 idx = context->name_count++;
1275 context->names[idx].name = NULL;
1277 ++context->ino_count;
1280 context->names[idx].dev = inode->i_sb->s_dev;
1281 context->names[idx].mode = inode->i_mode;
1282 context->names[idx].uid = inode->i_uid;
1283 context->names[idx].gid = inode->i_gid;
1284 context->names[idx].rdev = inode->i_rdev;
1285 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1286 (strcmp(name, ".") != 0)) {
1287 context->names[idx].ino = (unsigned long)-1;
1288 context->names[idx].pino = inode->i_ino;
1290 context->names[idx].ino = inode->i_ino;
1291 context->names[idx].pino = (unsigned long)-1;
1296 * audit_inode_child - collect inode info for created/removed objects
1297 * @dname: inode's dentry name
1298 * @inode: inode being audited
1299 * @pino: inode number of dentry parent
1301 * For syscalls that create or remove filesystem objects, audit_inode
1302 * can only collect information for the filesystem object's parent.
1303 * This call updates the audit context with the child's information.
1304 * Syscalls that create a new filesystem object must be hooked after
1305 * the object is created. Syscalls that remove a filesystem object
1306 * must be hooked prior, in order to capture the target inode during
1307 * unsuccessful attempts.
1309 void __audit_inode_child(const char *dname, const struct inode *inode,
1313 struct audit_context *context = current->audit_context;
1315 if (!context->in_syscall)
1318 /* determine matching parent */
1320 for (idx = 0; idx < context->name_count; idx++)
1321 if (context->names[idx].pino == pino) {
1323 const char *name = context->names[idx].name;
1324 int dlen = strlen(dname);
1325 int nlen = name ? strlen(name) : 0;
1330 /* disregard trailing slashes */
1331 n = name + nlen - 1;
1332 while ((*n == '/') && (n > name))
1335 /* find last path component */
1339 else if (n > name) {
1346 if (strncmp(n, dname, dlen) == 0)
1347 goto update_context;
1350 /* catch-all in case match not found */
1351 idx = context->name_count++;
1352 context->names[idx].name = NULL;
1353 context->names[idx].pino = pino;
1355 context->ino_count++;
1360 context->names[idx].ino = inode->i_ino;
1361 context->names[idx].dev = inode->i_sb->s_dev;
1362 context->names[idx].mode = inode->i_mode;
1363 context->names[idx].uid = inode->i_uid;
1364 context->names[idx].gid = inode->i_gid;
1365 context->names[idx].rdev = inode->i_rdev;
1370 * auditsc_get_stamp - get local copies of audit_context values
1371 * @ctx: audit_context for the task
1372 * @t: timespec to store time recorded in the audit_context
1373 * @serial: serial value that is recorded in the audit_context
1375 * Also sets the context as auditable.
1377 void auditsc_get_stamp(struct audit_context *ctx,
1378 struct timespec *t, unsigned int *serial)
1381 ctx->serial = audit_serial();
1382 t->tv_sec = ctx->ctime.tv_sec;
1383 t->tv_nsec = ctx->ctime.tv_nsec;
1384 *serial = ctx->serial;
1389 * audit_set_loginuid - set a task's audit_context loginuid
1390 * @task: task whose audit context is being modified
1391 * @loginuid: loginuid value
1395 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1397 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1399 if (task->audit_context) {
1400 struct audit_buffer *ab;
1402 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1404 audit_log_format(ab, "login pid=%d uid=%u "
1405 "old auid=%u new auid=%u",
1406 task->pid, task->uid,
1407 task->audit_context->loginuid, loginuid);
1410 task->audit_context->loginuid = loginuid;
1416 * audit_get_loginuid - get the loginuid for an audit_context
1417 * @ctx: the audit_context
1419 * Returns the context's loginuid or -1 if @ctx is NULL.
1421 uid_t audit_get_loginuid(struct audit_context *ctx)
1423 return ctx ? ctx->loginuid : -1;
1427 * audit_ipc_perms - record audit data for ipc
1428 * @qbytes: msgq bytes
1429 * @uid: msgq user id
1430 * @gid: msgq group id
1431 * @mode: msgq mode (permissions)
1433 * Returns 0 for success or NULL context or < 0 on error.
1435 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1437 struct audit_aux_data_ipcctl *ax;
1438 struct audit_context *context = current->audit_context;
1440 if (likely(!context))
1443 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1447 ax->qbytes = qbytes;
1452 ax->d.type = AUDIT_IPC;
1453 ax->d.next = context->aux;
1454 context->aux = (void *)ax;
1459 * audit_socketcall - record audit data for sys_socketcall
1460 * @nargs: number of args
1463 * Returns 0 for success or NULL context or < 0 on error.
1465 int audit_socketcall(int nargs, unsigned long *args)
1467 struct audit_aux_data_socketcall *ax;
1468 struct audit_context *context = current->audit_context;
1470 if (likely(!context))
1473 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1478 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1480 ax->d.type = AUDIT_SOCKETCALL;
1481 ax->d.next = context->aux;
1482 context->aux = (void *)ax;
1487 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1488 * @len: data length in user space
1489 * @a: data address in kernel space
1491 * Returns 0 for success or NULL context or < 0 on error.
1493 int audit_sockaddr(int len, void *a)
1495 struct audit_aux_data_sockaddr *ax;
1496 struct audit_context *context = current->audit_context;
1498 if (likely(!context))
1501 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1506 memcpy(ax->a, a, len);
1508 ax->d.type = AUDIT_SOCKADDR;
1509 ax->d.next = context->aux;
1510 context->aux = (void *)ax;
1515 * audit_avc_path - record the granting or denial of permissions
1516 * @dentry: dentry to record
1517 * @mnt: mnt to record
1519 * Returns 0 for success or NULL context or < 0 on error.
1521 * Called from security/selinux/avc.c::avc_audit()
1523 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1525 struct audit_aux_data_path *ax;
1526 struct audit_context *context = current->audit_context;
1528 if (likely(!context))
1531 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1535 ax->dentry = dget(dentry);
1536 ax->mnt = mntget(mnt);
1538 ax->d.type = AUDIT_AVC_PATH;
1539 ax->d.next = context->aux;
1540 context->aux = (void *)ax;
1545 * audit_signal_info - record signal info for shutting down audit subsystem
1546 * @sig: signal value
1547 * @t: task being signaled
1549 * If the audit subsystem is being terminated, record the task (pid)
1550 * and uid that is doing that.
1552 void audit_signal_info(int sig, struct task_struct *t)
1554 extern pid_t audit_sig_pid;
1555 extern uid_t audit_sig_uid;
1557 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1558 if (sig == SIGTERM || sig == SIGHUP) {
1559 struct audit_context *ctx = current->audit_context;
1560 audit_sig_pid = current->pid;
1562 audit_sig_uid = ctx->loginuid;
1564 audit_sig_uid = current->uid;