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, 2006 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 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
35 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
38 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
45 #include <linux/init.h>
46 #include <asm/types.h>
47 #include <asm/atomic.h>
48 #include <asm/types.h>
50 #include <linux/namei.h>
52 #include <linux/module.h>
53 #include <linux/mount.h>
54 #include <linux/socket.h>
55 #include <linux/mqueue.h>
56 #include <linux/audit.h>
57 #include <linux/personality.h>
58 #include <linux/time.h>
59 #include <linux/netlink.h>
60 #include <linux/compiler.h>
61 #include <asm/unistd.h>
62 #include <linux/security.h>
63 #include <linux/list.h>
64 #include <linux/tty.h>
65 #include <linux/selinux.h>
66 #include <linux/binfmts.h>
67 #include <linux/highmem.h>
68 #include <linux/syscalls.h>
72 extern struct list_head audit_filter_list[];
74 /* No syscall auditing will take place unless audit_enabled != 0. */
75 extern int audit_enabled;
77 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
78 * for saving names from getname(). */
79 #define AUDIT_NAMES 20
81 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
82 * audit_context from being used for nameless inodes from
84 #define AUDIT_NAMES_RESERVED 7
86 /* Indicates that audit should log the full pathname. */
87 #define AUDIT_NAME_FULL -1
89 /* number of audit rules */
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. */
99 int name_len; /* number of name's characters to log */
100 unsigned name_put; /* call __putname() for this name */
110 struct audit_aux_data {
111 struct audit_aux_data *next;
115 #define AUDIT_AUX_IPCPERM 0
117 struct audit_aux_data_mq_open {
118 struct audit_aux_data d;
124 struct audit_aux_data_mq_sendrecv {
125 struct audit_aux_data d;
128 unsigned int msg_prio;
129 struct timespec abs_timeout;
132 struct audit_aux_data_mq_notify {
133 struct audit_aux_data d;
135 struct sigevent notification;
138 struct audit_aux_data_mq_getsetattr {
139 struct audit_aux_data d;
141 struct mq_attr mqstat;
144 struct audit_aux_data_ipcctl {
145 struct audit_aux_data d;
147 unsigned long qbytes;
154 struct audit_aux_data_execve {
155 struct audit_aux_data d;
161 struct audit_aux_data_socketcall {
162 struct audit_aux_data d;
164 unsigned long args[0];
167 struct audit_aux_data_sockaddr {
168 struct audit_aux_data d;
173 struct audit_aux_data_path {
174 struct audit_aux_data d;
175 struct dentry *dentry;
176 struct vfsmount *mnt;
179 /* The per-task audit context. */
180 struct audit_context {
181 int dummy; /* must be the first element */
182 int in_syscall; /* 1 if task is in a syscall */
183 enum audit_state state;
184 unsigned int serial; /* serial number for record */
185 struct timespec ctime; /* time of syscall entry */
186 uid_t loginuid; /* login uid (identity) */
187 int major; /* syscall number */
188 unsigned long argv[4]; /* syscall arguments */
189 int return_valid; /* return code is valid */
190 long return_code;/* syscall return code */
191 int auditable; /* 1 if record should be written */
193 struct audit_names names[AUDIT_NAMES];
194 char * filterkey; /* key for rule that triggered record */
196 struct vfsmount * pwdmnt;
197 struct audit_context *previous; /* For nested syscalls */
198 struct audit_aux_data *aux;
200 /* Save things to print about task_struct */
202 uid_t uid, euid, suid, fsuid;
203 gid_t gid, egid, sgid, fsgid;
204 unsigned long personality;
213 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
214 static inline int open_arg(int flags, int mask)
216 int n = ACC_MODE(flags);
217 if (flags & (O_TRUNC | O_CREAT))
218 n |= AUDIT_PERM_WRITE;
222 static int audit_match_perm(struct audit_context *ctx, int mask)
224 unsigned n = ctx->major;
225 switch (audit_classify_syscall(ctx->arch, n)) {
227 if ((mask & AUDIT_PERM_WRITE) &&
228 audit_match_class(AUDIT_CLASS_WRITE, n))
230 if ((mask & AUDIT_PERM_READ) &&
231 audit_match_class(AUDIT_CLASS_READ, n))
233 if ((mask & AUDIT_PERM_ATTR) &&
234 audit_match_class(AUDIT_CLASS_CHATTR, n))
237 case 1: /* 32bit on biarch */
238 if ((mask & AUDIT_PERM_WRITE) &&
239 audit_match_class(AUDIT_CLASS_WRITE_32, n))
241 if ((mask & AUDIT_PERM_READ) &&
242 audit_match_class(AUDIT_CLASS_READ_32, n))
244 if ((mask & AUDIT_PERM_ATTR) &&
245 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
249 return mask & ACC_MODE(ctx->argv[1]);
251 return mask & ACC_MODE(ctx->argv[2]);
252 case 4: /* socketcall */
253 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
255 return mask & AUDIT_PERM_EXEC;
261 /* Determine if any context name data matches a rule's watch data */
262 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
264 static int audit_filter_rules(struct task_struct *tsk,
265 struct audit_krule *rule,
266 struct audit_context *ctx,
267 struct audit_names *name,
268 enum audit_state *state)
270 int i, j, need_sid = 1;
273 for (i = 0; i < rule->field_count; i++) {
274 struct audit_field *f = &rule->fields[i];
279 result = audit_comparator(tsk->pid, f->op, f->val);
284 ctx->ppid = sys_getppid();
285 result = audit_comparator(ctx->ppid, f->op, f->val);
289 result = audit_comparator(tsk->uid, f->op, f->val);
292 result = audit_comparator(tsk->euid, f->op, f->val);
295 result = audit_comparator(tsk->suid, f->op, f->val);
298 result = audit_comparator(tsk->fsuid, f->op, f->val);
301 result = audit_comparator(tsk->gid, f->op, f->val);
304 result = audit_comparator(tsk->egid, f->op, f->val);
307 result = audit_comparator(tsk->sgid, f->op, f->val);
310 result = audit_comparator(tsk->fsgid, f->op, f->val);
313 result = audit_comparator(tsk->personality, f->op, f->val);
317 result = audit_comparator(ctx->arch, f->op, f->val);
321 if (ctx && ctx->return_valid)
322 result = audit_comparator(ctx->return_code, f->op, f->val);
325 if (ctx && ctx->return_valid) {
327 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
329 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
334 result = audit_comparator(MAJOR(name->dev),
337 for (j = 0; j < ctx->name_count; j++) {
338 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
347 result = audit_comparator(MINOR(name->dev),
350 for (j = 0; j < ctx->name_count; j++) {
351 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
360 result = (name->ino == f->val);
362 for (j = 0; j < ctx->name_count; j++) {
363 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
371 if (name && rule->watch->ino != (unsigned long)-1)
372 result = (name->dev == rule->watch->dev &&
373 name->ino == rule->watch->ino);
378 result = audit_comparator(ctx->loginuid, f->op, f->val);
380 case AUDIT_SUBJ_USER:
381 case AUDIT_SUBJ_ROLE:
382 case AUDIT_SUBJ_TYPE:
385 /* NOTE: this may return negative values indicating
386 a temporary error. We simply treat this as a
387 match for now to avoid losing information that
388 may be wanted. An error message will also be
392 selinux_get_task_sid(tsk, &sid);
395 result = selinux_audit_rule_match(sid, f->type,
404 case AUDIT_OBJ_LEV_LOW:
405 case AUDIT_OBJ_LEV_HIGH:
406 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
409 /* Find files that match */
411 result = selinux_audit_rule_match(
412 name->osid, f->type, f->op,
415 for (j = 0; j < ctx->name_count; j++) {
416 if (selinux_audit_rule_match(
425 /* Find ipc objects that match */
427 struct audit_aux_data *aux;
428 for (aux = ctx->aux; aux;
430 if (aux->type == AUDIT_IPC) {
431 struct audit_aux_data_ipcctl *axi = (void *)aux;
432 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
446 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
448 case AUDIT_FILTERKEY:
449 /* ignore this field for filtering */
453 result = audit_match_perm(ctx, f->val);
461 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
462 switch (rule->action) {
463 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
464 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
469 /* At process creation time, we can determine if system-call auditing is
470 * completely disabled for this task. Since we only have the task
471 * structure at this point, we can only check uid and gid.
473 static enum audit_state audit_filter_task(struct task_struct *tsk)
475 struct audit_entry *e;
476 enum audit_state state;
479 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
480 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
486 return AUDIT_BUILD_CONTEXT;
489 /* At syscall entry and exit time, this filter is called if the
490 * audit_state is not low enough that auditing cannot take place, but is
491 * also not high enough that we already know we have to write an audit
492 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
494 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
495 struct audit_context *ctx,
496 struct list_head *list)
498 struct audit_entry *e;
499 enum audit_state state;
501 if (audit_pid && tsk->tgid == audit_pid)
502 return AUDIT_DISABLED;
505 if (!list_empty(list)) {
506 int word = AUDIT_WORD(ctx->major);
507 int bit = AUDIT_BIT(ctx->major);
509 list_for_each_entry_rcu(e, list, list) {
510 if ((e->rule.mask[word] & bit) == bit &&
511 audit_filter_rules(tsk, &e->rule, ctx, NULL,
519 return AUDIT_BUILD_CONTEXT;
522 /* At syscall exit time, this filter is called if any audit_names[] have been
523 * collected during syscall processing. We only check rules in sublists at hash
524 * buckets applicable to the inode numbers in audit_names[].
525 * Regarding audit_state, same rules apply as for audit_filter_syscall().
527 enum audit_state audit_filter_inodes(struct task_struct *tsk,
528 struct audit_context *ctx)
531 struct audit_entry *e;
532 enum audit_state state;
534 if (audit_pid && tsk->tgid == audit_pid)
535 return AUDIT_DISABLED;
538 for (i = 0; i < ctx->name_count; i++) {
539 int word = AUDIT_WORD(ctx->major);
540 int bit = AUDIT_BIT(ctx->major);
541 struct audit_names *n = &ctx->names[i];
542 int h = audit_hash_ino((u32)n->ino);
543 struct list_head *list = &audit_inode_hash[h];
545 if (list_empty(list))
548 list_for_each_entry_rcu(e, list, list) {
549 if ((e->rule.mask[word] & bit) == bit &&
550 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
557 return AUDIT_BUILD_CONTEXT;
560 void audit_set_auditable(struct audit_context *ctx)
565 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
569 struct audit_context *context = tsk->audit_context;
571 if (likely(!context))
573 context->return_valid = return_valid;
574 context->return_code = return_code;
576 if (context->in_syscall && !context->dummy && !context->auditable) {
577 enum audit_state state;
579 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
580 if (state == AUDIT_RECORD_CONTEXT) {
581 context->auditable = 1;
585 state = audit_filter_inodes(tsk, context);
586 if (state == AUDIT_RECORD_CONTEXT)
587 context->auditable = 1;
593 tsk->audit_context = NULL;
597 static inline void audit_free_names(struct audit_context *context)
602 if (context->auditable
603 ||context->put_count + context->ino_count != context->name_count) {
604 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
605 " name_count=%d put_count=%d"
606 " ino_count=%d [NOT freeing]\n",
608 context->serial, context->major, context->in_syscall,
609 context->name_count, context->put_count,
611 for (i = 0; i < context->name_count; i++) {
612 printk(KERN_ERR "names[%d] = %p = %s\n", i,
613 context->names[i].name,
614 context->names[i].name ?: "(null)");
621 context->put_count = 0;
622 context->ino_count = 0;
625 for (i = 0; i < context->name_count; i++) {
626 if (context->names[i].name && context->names[i].name_put)
627 __putname(context->names[i].name);
629 context->name_count = 0;
633 mntput(context->pwdmnt);
635 context->pwdmnt = NULL;
638 static inline void audit_free_aux(struct audit_context *context)
640 struct audit_aux_data *aux;
642 while ((aux = context->aux)) {
643 if (aux->type == AUDIT_AVC_PATH) {
644 struct audit_aux_data_path *axi = (void *)aux;
649 context->aux = aux->next;
654 static inline void audit_zero_context(struct audit_context *context,
655 enum audit_state state)
657 uid_t loginuid = context->loginuid;
659 memset(context, 0, sizeof(*context));
660 context->state = state;
661 context->loginuid = loginuid;
664 static inline struct audit_context *audit_alloc_context(enum audit_state state)
666 struct audit_context *context;
668 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
670 audit_zero_context(context, state);
675 * audit_alloc - allocate an audit context block for a task
678 * Filter on the task information and allocate a per-task audit context
679 * if necessary. Doing so turns on system call auditing for the
680 * specified task. This is called from copy_process, so no lock is
683 int audit_alloc(struct task_struct *tsk)
685 struct audit_context *context;
686 enum audit_state state;
688 if (likely(!audit_enabled))
689 return 0; /* Return if not auditing. */
691 state = audit_filter_task(tsk);
692 if (likely(state == AUDIT_DISABLED))
695 if (!(context = audit_alloc_context(state))) {
696 audit_log_lost("out of memory in audit_alloc");
700 /* Preserve login uid */
701 context->loginuid = -1;
702 if (current->audit_context)
703 context->loginuid = current->audit_context->loginuid;
705 tsk->audit_context = context;
706 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
710 static inline void audit_free_context(struct audit_context *context)
712 struct audit_context *previous;
716 previous = context->previous;
717 if (previous || (count && count < 10)) {
719 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
720 " freeing multiple contexts (%d)\n",
721 context->serial, context->major,
722 context->name_count, count);
724 audit_free_names(context);
725 audit_free_aux(context);
726 kfree(context->filterkey);
731 printk(KERN_ERR "audit: freed %d contexts\n", count);
734 void audit_log_task_context(struct audit_buffer *ab)
739 len = security_getprocattr(current, "current", NULL, 0);
746 ctx = kmalloc(len, GFP_KERNEL);
750 len = security_getprocattr(current, "current", ctx, len);
754 audit_log_format(ab, " subj=%s", ctx);
759 audit_panic("error in audit_log_task_context");
763 EXPORT_SYMBOL(audit_log_task_context);
765 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
767 char name[sizeof(tsk->comm)];
768 struct mm_struct *mm = tsk->mm;
769 struct vm_area_struct *vma;
773 get_task_comm(name, tsk);
774 audit_log_format(ab, " comm=");
775 audit_log_untrustedstring(ab, name);
778 down_read(&mm->mmap_sem);
781 if ((vma->vm_flags & VM_EXECUTABLE) &&
783 audit_log_d_path(ab, "exe=",
784 vma->vm_file->f_dentry,
785 vma->vm_file->f_vfsmnt);
790 up_read(&mm->mmap_sem);
792 audit_log_task_context(ab);
795 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
797 int i, call_panic = 0;
798 struct audit_buffer *ab;
799 struct audit_aux_data *aux;
803 context->pid = tsk->pid;
805 context->ppid = sys_getppid();
806 context->uid = tsk->uid;
807 context->gid = tsk->gid;
808 context->euid = tsk->euid;
809 context->suid = tsk->suid;
810 context->fsuid = tsk->fsuid;
811 context->egid = tsk->egid;
812 context->sgid = tsk->sgid;
813 context->fsgid = tsk->fsgid;
814 context->personality = tsk->personality;
816 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
818 return; /* audit_panic has been called */
819 audit_log_format(ab, "arch=%x syscall=%d",
820 context->arch, context->major);
821 if (context->personality != PER_LINUX)
822 audit_log_format(ab, " per=%lx", context->personality);
823 if (context->return_valid)
824 audit_log_format(ab, " success=%s exit=%ld",
825 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
826 context->return_code);
828 mutex_lock(&tty_mutex);
829 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
830 tty = tsk->signal->tty->name;
834 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
835 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
836 " euid=%u suid=%u fsuid=%u"
837 " egid=%u sgid=%u fsgid=%u tty=%s",
848 context->euid, context->suid, context->fsuid,
849 context->egid, context->sgid, context->fsgid, tty);
851 mutex_unlock(&tty_mutex);
853 audit_log_task_info(ab, tsk);
854 if (context->filterkey) {
855 audit_log_format(ab, " key=");
856 audit_log_untrustedstring(ab, context->filterkey);
858 audit_log_format(ab, " key=(null)");
861 for (aux = context->aux; aux; aux = aux->next) {
863 ab = audit_log_start(context, GFP_KERNEL, aux->type);
865 continue; /* audit_panic has been called */
868 case AUDIT_MQ_OPEN: {
869 struct audit_aux_data_mq_open *axi = (void *)aux;
871 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
872 "mq_msgsize=%ld mq_curmsgs=%ld",
873 axi->oflag, axi->mode, axi->attr.mq_flags,
874 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
875 axi->attr.mq_curmsgs);
878 case AUDIT_MQ_SENDRECV: {
879 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
881 "mqdes=%d msg_len=%zd msg_prio=%u "
882 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
883 axi->mqdes, axi->msg_len, axi->msg_prio,
884 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
887 case AUDIT_MQ_NOTIFY: {
888 struct audit_aux_data_mq_notify *axi = (void *)aux;
890 "mqdes=%d sigev_signo=%d",
892 axi->notification.sigev_signo);
895 case AUDIT_MQ_GETSETATTR: {
896 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
898 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
901 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
902 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
906 struct audit_aux_data_ipcctl *axi = (void *)aux;
908 "ouid=%u ogid=%u mode=%x",
909 axi->uid, axi->gid, axi->mode);
910 if (axi->osid != 0) {
913 if (selinux_sid_to_string(
914 axi->osid, &ctx, &len)) {
915 audit_log_format(ab, " osid=%u",
919 audit_log_format(ab, " obj=%s", ctx);
924 case AUDIT_IPC_SET_PERM: {
925 struct audit_aux_data_ipcctl *axi = (void *)aux;
927 "qbytes=%lx ouid=%u ogid=%u mode=%x",
928 axi->qbytes, axi->uid, axi->gid, axi->mode);
932 struct audit_aux_data_execve *axi = (void *)aux;
935 for (i = 0, p = axi->mem; i < axi->argc; i++) {
936 audit_log_format(ab, "a%d=", i);
937 p = audit_log_untrustedstring(ab, p);
938 audit_log_format(ab, "\n");
942 case AUDIT_SOCKETCALL: {
944 struct audit_aux_data_socketcall *axs = (void *)aux;
945 audit_log_format(ab, "nargs=%d", axs->nargs);
946 for (i=0; i<axs->nargs; i++)
947 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
950 case AUDIT_SOCKADDR: {
951 struct audit_aux_data_sockaddr *axs = (void *)aux;
953 audit_log_format(ab, "saddr=");
954 audit_log_hex(ab, axs->a, axs->len);
957 case AUDIT_AVC_PATH: {
958 struct audit_aux_data_path *axi = (void *)aux;
959 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
966 if (context->pwd && context->pwdmnt) {
967 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
969 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
973 for (i = 0; i < context->name_count; i++) {
974 struct audit_names *n = &context->names[i];
976 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
978 continue; /* audit_panic has been called */
980 audit_log_format(ab, "item=%d", i);
983 switch(n->name_len) {
984 case AUDIT_NAME_FULL:
985 /* log the full path */
986 audit_log_format(ab, " name=");
987 audit_log_untrustedstring(ab, n->name);
990 /* name was specified as a relative path and the
991 * directory component is the cwd */
992 audit_log_d_path(ab, " name=", context->pwd,
996 /* log the name's directory component */
997 audit_log_format(ab, " name=");
998 audit_log_n_untrustedstring(ab, n->name_len,
1002 audit_log_format(ab, " name=(null)");
1004 if (n->ino != (unsigned long)-1) {
1005 audit_log_format(ab, " inode=%lu"
1006 " dev=%02x:%02x mode=%#o"
1007 " ouid=%u ogid=%u rdev=%02x:%02x",
1020 if (selinux_sid_to_string(
1021 n->osid, &ctx, &len)) {
1022 audit_log_format(ab, " osid=%u", n->osid);
1025 audit_log_format(ab, " obj=%s", ctx);
1032 audit_panic("error converting sid to string");
1036 * audit_free - free a per-task audit context
1037 * @tsk: task whose audit context block to free
1039 * Called from copy_process and do_exit
1041 void audit_free(struct task_struct *tsk)
1043 struct audit_context *context;
1045 context = audit_get_context(tsk, 0, 0);
1046 if (likely(!context))
1049 /* Check for system calls that do not go through the exit
1050 * function (e.g., exit_group), then free context block.
1051 * We use GFP_ATOMIC here because we might be doing this
1052 * in the context of the idle thread */
1053 /* that can happen only if we are called from do_exit() */
1054 if (context->in_syscall && context->auditable)
1055 audit_log_exit(context, tsk);
1057 audit_free_context(context);
1061 * audit_syscall_entry - fill in an audit record at syscall entry
1062 * @tsk: task being audited
1063 * @arch: architecture type
1064 * @major: major syscall type (function)
1065 * @a1: additional syscall register 1
1066 * @a2: additional syscall register 2
1067 * @a3: additional syscall register 3
1068 * @a4: additional syscall register 4
1070 * Fill in audit context at syscall entry. This only happens if the
1071 * audit context was created when the task was created and the state or
1072 * filters demand the audit context be built. If the state from the
1073 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1074 * then the record will be written at syscall exit time (otherwise, it
1075 * will only be written if another part of the kernel requests that it
1078 void audit_syscall_entry(int arch, int major,
1079 unsigned long a1, unsigned long a2,
1080 unsigned long a3, unsigned long a4)
1082 struct task_struct *tsk = current;
1083 struct audit_context *context = tsk->audit_context;
1084 enum audit_state state;
1089 * This happens only on certain architectures that make system
1090 * calls in kernel_thread via the entry.S interface, instead of
1091 * with direct calls. (If you are porting to a new
1092 * architecture, hitting this condition can indicate that you
1093 * got the _exit/_leave calls backward in entry.S.)
1097 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1099 * This also happens with vm86 emulation in a non-nested manner
1100 * (entries without exits), so this case must be caught.
1102 if (context->in_syscall) {
1103 struct audit_context *newctx;
1107 "audit(:%d) pid=%d in syscall=%d;"
1108 " entering syscall=%d\n",
1109 context->serial, tsk->pid, context->major, major);
1111 newctx = audit_alloc_context(context->state);
1113 newctx->previous = context;
1115 tsk->audit_context = newctx;
1117 /* If we can't alloc a new context, the best we
1118 * can do is to leak memory (any pending putname
1119 * will be lost). The only other alternative is
1120 * to abandon auditing. */
1121 audit_zero_context(context, context->state);
1124 BUG_ON(context->in_syscall || context->name_count);
1129 context->arch = arch;
1130 context->major = major;
1131 context->argv[0] = a1;
1132 context->argv[1] = a2;
1133 context->argv[2] = a3;
1134 context->argv[3] = a4;
1136 state = context->state;
1137 context->dummy = !audit_n_rules;
1138 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1139 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1140 if (likely(state == AUDIT_DISABLED))
1143 context->serial = 0;
1144 context->ctime = CURRENT_TIME;
1145 context->in_syscall = 1;
1146 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1151 * audit_syscall_exit - deallocate audit context after a system call
1152 * @tsk: task being audited
1153 * @valid: success/failure flag
1154 * @return_code: syscall return value
1156 * Tear down after system call. If the audit context has been marked as
1157 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1158 * filtering, or because some other part of the kernel write an audit
1159 * message), then write out the syscall information. In call cases,
1160 * free the names stored from getname().
1162 void audit_syscall_exit(int valid, long return_code)
1164 struct task_struct *tsk = current;
1165 struct audit_context *context;
1167 context = audit_get_context(tsk, valid, return_code);
1169 if (likely(!context))
1172 if (context->in_syscall && context->auditable)
1173 audit_log_exit(context, tsk);
1175 context->in_syscall = 0;
1176 context->auditable = 0;
1178 if (context->previous) {
1179 struct audit_context *new_context = context->previous;
1180 context->previous = NULL;
1181 audit_free_context(context);
1182 tsk->audit_context = new_context;
1184 audit_free_names(context);
1185 audit_free_aux(context);
1186 kfree(context->filterkey);
1187 context->filterkey = NULL;
1188 tsk->audit_context = context;
1193 * audit_getname - add a name to the list
1194 * @name: name to add
1196 * Add a name to the list of audit names for this context.
1197 * Called from fs/namei.c:getname().
1199 void __audit_getname(const char *name)
1201 struct audit_context *context = current->audit_context;
1203 if (IS_ERR(name) || !name)
1206 if (!context->in_syscall) {
1207 #if AUDIT_DEBUG == 2
1208 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1209 __FILE__, __LINE__, context->serial, name);
1214 BUG_ON(context->name_count >= AUDIT_NAMES);
1215 context->names[context->name_count].name = name;
1216 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1217 context->names[context->name_count].name_put = 1;
1218 context->names[context->name_count].ino = (unsigned long)-1;
1219 ++context->name_count;
1220 if (!context->pwd) {
1221 read_lock(¤t->fs->lock);
1222 context->pwd = dget(current->fs->pwd);
1223 context->pwdmnt = mntget(current->fs->pwdmnt);
1224 read_unlock(¤t->fs->lock);
1229 /* audit_putname - intercept a putname request
1230 * @name: name to intercept and delay for putname
1232 * If we have stored the name from getname in the audit context,
1233 * then we delay the putname until syscall exit.
1234 * Called from include/linux/fs.h:putname().
1236 void audit_putname(const char *name)
1238 struct audit_context *context = current->audit_context;
1241 if (!context->in_syscall) {
1242 #if AUDIT_DEBUG == 2
1243 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1244 __FILE__, __LINE__, context->serial, name);
1245 if (context->name_count) {
1247 for (i = 0; i < context->name_count; i++)
1248 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1249 context->names[i].name,
1250 context->names[i].name ?: "(null)");
1257 ++context->put_count;
1258 if (context->put_count > context->name_count) {
1259 printk(KERN_ERR "%s:%d(:%d): major=%d"
1260 " in_syscall=%d putname(%p) name_count=%d"
1263 context->serial, context->major,
1264 context->in_syscall, name, context->name_count,
1265 context->put_count);
1272 /* Copy inode data into an audit_names. */
1273 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1275 name->ino = inode->i_ino;
1276 name->dev = inode->i_sb->s_dev;
1277 name->mode = inode->i_mode;
1278 name->uid = inode->i_uid;
1279 name->gid = inode->i_gid;
1280 name->rdev = inode->i_rdev;
1281 selinux_get_inode_sid(inode, &name->osid);
1285 * audit_inode - store the inode and device from a lookup
1286 * @name: name being audited
1287 * @inode: inode being audited
1289 * Called from fs/namei.c:path_lookup().
1291 void __audit_inode(const char *name, const struct inode *inode)
1294 struct audit_context *context = current->audit_context;
1296 if (!context->in_syscall)
1298 if (context->name_count
1299 && context->names[context->name_count-1].name
1300 && context->names[context->name_count-1].name == name)
1301 idx = context->name_count - 1;
1302 else if (context->name_count > 1
1303 && context->names[context->name_count-2].name
1304 && context->names[context->name_count-2].name == name)
1305 idx = context->name_count - 2;
1307 /* FIXME: how much do we care about inodes that have no
1308 * associated name? */
1309 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1311 idx = context->name_count++;
1312 context->names[idx].name = NULL;
1314 ++context->ino_count;
1317 audit_copy_inode(&context->names[idx], inode);
1321 * audit_inode_child - collect inode info for created/removed objects
1322 * @dname: inode's dentry name
1323 * @inode: inode being audited
1324 * @parent: inode of dentry parent
1326 * For syscalls that create or remove filesystem objects, audit_inode
1327 * can only collect information for the filesystem object's parent.
1328 * This call updates the audit context with the child's information.
1329 * Syscalls that create a new filesystem object must be hooked after
1330 * the object is created. Syscalls that remove a filesystem object
1331 * must be hooked prior, in order to capture the target inode during
1332 * unsuccessful attempts.
1334 void __audit_inode_child(const char *dname, const struct inode *inode,
1335 const struct inode *parent)
1338 struct audit_context *context = current->audit_context;
1339 const char *found_name = NULL;
1342 if (!context->in_syscall)
1345 /* determine matching parent */
1347 goto update_context;
1348 for (idx = 0; idx < context->name_count; idx++)
1349 if (context->names[idx].ino == parent->i_ino) {
1350 const char *name = context->names[idx].name;
1355 if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
1356 context->names[idx].name_len = dirlen;
1363 idx = context->name_count;
1364 if (context->name_count == AUDIT_NAMES) {
1365 printk(KERN_DEBUG "name_count maxed and losing %s\n",
1366 found_name ?: "(null)");
1369 context->name_count++;
1371 context->ino_count++;
1373 /* Re-use the name belonging to the slot for a matching parent directory.
1374 * All names for this context are relinquished in audit_free_names() */
1375 context->names[idx].name = found_name;
1376 context->names[idx].name_len = AUDIT_NAME_FULL;
1377 context->names[idx].name_put = 0; /* don't call __putname() */
1380 context->names[idx].ino = (unsigned long)-1;
1382 audit_copy_inode(&context->names[idx], inode);
1384 /* A parent was not found in audit_names, so copy the inode data for the
1385 * provided parent. */
1387 idx = context->name_count;
1388 if (context->name_count == AUDIT_NAMES) {
1390 "name_count maxed and losing parent inode data: dev=%02x:%02x, inode=%lu",
1391 MAJOR(parent->i_sb->s_dev),
1392 MINOR(parent->i_sb->s_dev),
1396 context->name_count++;
1398 context->ino_count++;
1400 audit_copy_inode(&context->names[idx], parent);
1405 * audit_inode_update - update inode info for last collected name
1406 * @inode: inode being audited
1408 * When open() is called on an existing object with the O_CREAT flag, the inode
1409 * data audit initially collects is incorrect. This additional hook ensures
1410 * audit has the inode data for the actual object to be opened.
1412 void __audit_inode_update(const struct inode *inode)
1414 struct audit_context *context = current->audit_context;
1417 if (!context->in_syscall || !inode)
1420 if (context->name_count == 0) {
1421 context->name_count++;
1423 context->ino_count++;
1426 idx = context->name_count - 1;
1428 audit_copy_inode(&context->names[idx], inode);
1432 * auditsc_get_stamp - get local copies of audit_context values
1433 * @ctx: audit_context for the task
1434 * @t: timespec to store time recorded in the audit_context
1435 * @serial: serial value that is recorded in the audit_context
1437 * Also sets the context as auditable.
1439 void auditsc_get_stamp(struct audit_context *ctx,
1440 struct timespec *t, unsigned int *serial)
1443 ctx->serial = audit_serial();
1444 t->tv_sec = ctx->ctime.tv_sec;
1445 t->tv_nsec = ctx->ctime.tv_nsec;
1446 *serial = ctx->serial;
1451 * audit_set_loginuid - set a task's audit_context loginuid
1452 * @task: task whose audit context is being modified
1453 * @loginuid: loginuid value
1457 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1459 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1461 struct audit_context *context = task->audit_context;
1464 /* Only log if audit is enabled */
1465 if (context->in_syscall) {
1466 struct audit_buffer *ab;
1468 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1470 audit_log_format(ab, "login pid=%d uid=%u "
1471 "old auid=%u new auid=%u",
1472 task->pid, task->uid,
1473 context->loginuid, loginuid);
1477 context->loginuid = loginuid;
1483 * audit_get_loginuid - get the loginuid for an audit_context
1484 * @ctx: the audit_context
1486 * Returns the context's loginuid or -1 if @ctx is NULL.
1488 uid_t audit_get_loginuid(struct audit_context *ctx)
1490 return ctx ? ctx->loginuid : -1;
1493 EXPORT_SYMBOL(audit_get_loginuid);
1496 * __audit_mq_open - record audit data for a POSIX MQ open
1499 * @u_attr: queue attributes
1501 * Returns 0 for success or NULL context or < 0 on error.
1503 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1505 struct audit_aux_data_mq_open *ax;
1506 struct audit_context *context = current->audit_context;
1511 if (likely(!context))
1514 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1518 if (u_attr != NULL) {
1519 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1524 memset(&ax->attr, 0, sizeof(ax->attr));
1529 ax->d.type = AUDIT_MQ_OPEN;
1530 ax->d.next = context->aux;
1531 context->aux = (void *)ax;
1536 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1537 * @mqdes: MQ descriptor
1538 * @msg_len: Message length
1539 * @msg_prio: Message priority
1540 * @u_abs_timeout: Message timeout in absolute time
1542 * Returns 0 for success or NULL context or < 0 on error.
1544 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1545 const struct timespec __user *u_abs_timeout)
1547 struct audit_aux_data_mq_sendrecv *ax;
1548 struct audit_context *context = current->audit_context;
1553 if (likely(!context))
1556 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1560 if (u_abs_timeout != NULL) {
1561 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1566 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1569 ax->msg_len = msg_len;
1570 ax->msg_prio = msg_prio;
1572 ax->d.type = AUDIT_MQ_SENDRECV;
1573 ax->d.next = context->aux;
1574 context->aux = (void *)ax;
1579 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1580 * @mqdes: MQ descriptor
1581 * @msg_len: Message length
1582 * @u_msg_prio: Message priority
1583 * @u_abs_timeout: Message timeout in absolute time
1585 * Returns 0 for success or NULL context or < 0 on error.
1587 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1588 unsigned int __user *u_msg_prio,
1589 const struct timespec __user *u_abs_timeout)
1591 struct audit_aux_data_mq_sendrecv *ax;
1592 struct audit_context *context = current->audit_context;
1597 if (likely(!context))
1600 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1604 if (u_msg_prio != NULL) {
1605 if (get_user(ax->msg_prio, u_msg_prio)) {
1612 if (u_abs_timeout != NULL) {
1613 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1618 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1621 ax->msg_len = msg_len;
1623 ax->d.type = AUDIT_MQ_SENDRECV;
1624 ax->d.next = context->aux;
1625 context->aux = (void *)ax;
1630 * __audit_mq_notify - record audit data for a POSIX MQ notify
1631 * @mqdes: MQ descriptor
1632 * @u_notification: Notification event
1634 * Returns 0 for success or NULL context or < 0 on error.
1637 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1639 struct audit_aux_data_mq_notify *ax;
1640 struct audit_context *context = current->audit_context;
1645 if (likely(!context))
1648 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1652 if (u_notification != NULL) {
1653 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1658 memset(&ax->notification, 0, sizeof(ax->notification));
1662 ax->d.type = AUDIT_MQ_NOTIFY;
1663 ax->d.next = context->aux;
1664 context->aux = (void *)ax;
1669 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1670 * @mqdes: MQ descriptor
1673 * Returns 0 for success or NULL context or < 0 on error.
1675 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1677 struct audit_aux_data_mq_getsetattr *ax;
1678 struct audit_context *context = current->audit_context;
1683 if (likely(!context))
1686 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1691 ax->mqstat = *mqstat;
1693 ax->d.type = AUDIT_MQ_GETSETATTR;
1694 ax->d.next = context->aux;
1695 context->aux = (void *)ax;
1700 * audit_ipc_obj - record audit data for ipc object
1701 * @ipcp: ipc permissions
1703 * Returns 0 for success or NULL context or < 0 on error.
1705 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
1707 struct audit_aux_data_ipcctl *ax;
1708 struct audit_context *context = current->audit_context;
1710 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1714 ax->uid = ipcp->uid;
1715 ax->gid = ipcp->gid;
1716 ax->mode = ipcp->mode;
1717 selinux_get_ipc_sid(ipcp, &ax->osid);
1719 ax->d.type = AUDIT_IPC;
1720 ax->d.next = context->aux;
1721 context->aux = (void *)ax;
1726 * audit_ipc_set_perm - record audit data for new ipc permissions
1727 * @qbytes: msgq bytes
1728 * @uid: msgq user id
1729 * @gid: msgq group id
1730 * @mode: msgq mode (permissions)
1732 * Returns 0 for success or NULL context or < 0 on error.
1734 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1736 struct audit_aux_data_ipcctl *ax;
1737 struct audit_context *context = current->audit_context;
1739 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1743 ax->qbytes = qbytes;
1748 ax->d.type = AUDIT_IPC_SET_PERM;
1749 ax->d.next = context->aux;
1750 context->aux = (void *)ax;
1754 int audit_bprm(struct linux_binprm *bprm)
1756 struct audit_aux_data_execve *ax;
1757 struct audit_context *context = current->audit_context;
1758 unsigned long p, next;
1761 if (likely(!audit_enabled || !context || context->dummy))
1764 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1769 ax->argc = bprm->argc;
1770 ax->envc = bprm->envc;
1771 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1772 struct page *page = bprm->page[p / PAGE_SIZE];
1773 void *kaddr = kmap(page);
1774 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1775 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1780 ax->d.type = AUDIT_EXECVE;
1781 ax->d.next = context->aux;
1782 context->aux = (void *)ax;
1788 * audit_socketcall - record audit data for sys_socketcall
1789 * @nargs: number of args
1792 * Returns 0 for success or NULL context or < 0 on error.
1794 int audit_socketcall(int nargs, unsigned long *args)
1796 struct audit_aux_data_socketcall *ax;
1797 struct audit_context *context = current->audit_context;
1799 if (likely(!context || context->dummy))
1802 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1807 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1809 ax->d.type = AUDIT_SOCKETCALL;
1810 ax->d.next = context->aux;
1811 context->aux = (void *)ax;
1816 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1817 * @len: data length in user space
1818 * @a: data address in kernel space
1820 * Returns 0 for success or NULL context or < 0 on error.
1822 int audit_sockaddr(int len, void *a)
1824 struct audit_aux_data_sockaddr *ax;
1825 struct audit_context *context = current->audit_context;
1827 if (likely(!context || context->dummy))
1830 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1835 memcpy(ax->a, a, len);
1837 ax->d.type = AUDIT_SOCKADDR;
1838 ax->d.next = context->aux;
1839 context->aux = (void *)ax;
1844 * audit_avc_path - record the granting or denial of permissions
1845 * @dentry: dentry to record
1846 * @mnt: mnt to record
1848 * Returns 0 for success or NULL context or < 0 on error.
1850 * Called from security/selinux/avc.c::avc_audit()
1852 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1854 struct audit_aux_data_path *ax;
1855 struct audit_context *context = current->audit_context;
1857 if (likely(!context))
1860 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1864 ax->dentry = dget(dentry);
1865 ax->mnt = mntget(mnt);
1867 ax->d.type = AUDIT_AVC_PATH;
1868 ax->d.next = context->aux;
1869 context->aux = (void *)ax;
1874 * audit_signal_info - record signal info for shutting down audit subsystem
1875 * @sig: signal value
1876 * @t: task being signaled
1878 * If the audit subsystem is being terminated, record the task (pid)
1879 * and uid that is doing that.
1881 void __audit_signal_info(int sig, struct task_struct *t)
1883 extern pid_t audit_sig_pid;
1884 extern uid_t audit_sig_uid;
1885 extern u32 audit_sig_sid;
1887 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1888 struct task_struct *tsk = current;
1889 struct audit_context *ctx = tsk->audit_context;
1890 audit_sig_pid = tsk->pid;
1892 audit_sig_uid = ctx->loginuid;
1894 audit_sig_uid = tsk->uid;
1895 selinux_get_task_sid(tsk, &audit_sig_sid);