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/syscalls.h>
71 extern struct list_head audit_filter_list[];
73 /* No syscall auditing will take place unless audit_enabled != 0. */
74 extern int audit_enabled;
76 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
77 * for saving names from getname(). */
78 #define AUDIT_NAMES 20
80 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
81 * audit_context from being used for nameless inodes from
83 #define AUDIT_NAMES_RESERVED 7
85 /* When fs/namei.c:getname() is called, we store the pointer in name and
86 * we don't let putname() free it (instead we free all of the saved
87 * pointers at syscall exit time).
89 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
102 struct audit_aux_data {
103 struct audit_aux_data *next;
107 #define AUDIT_AUX_IPCPERM 0
109 struct audit_aux_data_mq_open {
110 struct audit_aux_data d;
116 struct audit_aux_data_mq_sendrecv {
117 struct audit_aux_data d;
120 unsigned int msg_prio;
121 struct timespec abs_timeout;
124 struct audit_aux_data_mq_notify {
125 struct audit_aux_data d;
127 struct sigevent notification;
130 struct audit_aux_data_mq_getsetattr {
131 struct audit_aux_data d;
133 struct mq_attr mqstat;
136 struct audit_aux_data_ipcctl {
137 struct audit_aux_data d;
139 unsigned long qbytes;
146 struct audit_aux_data_execve {
147 struct audit_aux_data d;
153 struct audit_aux_data_socketcall {
154 struct audit_aux_data d;
156 unsigned long args[0];
159 struct audit_aux_data_sockaddr {
160 struct audit_aux_data d;
165 struct audit_aux_data_path {
166 struct audit_aux_data d;
167 struct dentry *dentry;
168 struct vfsmount *mnt;
171 /* The per-task audit context. */
172 struct audit_context {
173 int in_syscall; /* 1 if task is in a syscall */
174 enum audit_state state;
175 unsigned int serial; /* serial number for record */
176 struct timespec ctime; /* time of syscall entry */
177 uid_t loginuid; /* login uid (identity) */
178 int major; /* syscall number */
179 unsigned long argv[4]; /* syscall arguments */
180 int return_valid; /* return code is valid */
181 long return_code;/* syscall return code */
182 int auditable; /* 1 if record should be written */
184 struct audit_names names[AUDIT_NAMES];
186 struct vfsmount * pwdmnt;
187 struct audit_context *previous; /* For nested syscalls */
188 struct audit_aux_data *aux;
190 /* Save things to print about task_struct */
192 uid_t uid, euid, suid, fsuid;
193 gid_t gid, egid, sgid, fsgid;
194 unsigned long personality;
203 /* Determine if any context name data matches a rule's watch data */
204 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
206 static int audit_filter_rules(struct task_struct *tsk,
207 struct audit_krule *rule,
208 struct audit_context *ctx,
209 struct audit_names *name,
210 enum audit_state *state)
212 int i, j, need_sid = 1;
215 for (i = 0; i < rule->field_count; i++) {
216 struct audit_field *f = &rule->fields[i];
221 result = audit_comparator(tsk->pid, f->op, f->val);
225 result = audit_comparator(ctx->ppid, f->op, f->val);
228 result = audit_comparator(tsk->uid, f->op, f->val);
231 result = audit_comparator(tsk->euid, f->op, f->val);
234 result = audit_comparator(tsk->suid, f->op, f->val);
237 result = audit_comparator(tsk->fsuid, f->op, f->val);
240 result = audit_comparator(tsk->gid, f->op, f->val);
243 result = audit_comparator(tsk->egid, f->op, f->val);
246 result = audit_comparator(tsk->sgid, f->op, f->val);
249 result = audit_comparator(tsk->fsgid, f->op, f->val);
252 result = audit_comparator(tsk->personality, f->op, f->val);
256 result = audit_comparator(ctx->arch, f->op, f->val);
260 if (ctx && ctx->return_valid)
261 result = audit_comparator(ctx->return_code, f->op, f->val);
264 if (ctx && ctx->return_valid) {
266 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
268 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
273 result = audit_comparator(MAJOR(name->dev),
276 for (j = 0; j < ctx->name_count; j++) {
277 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
286 result = audit_comparator(MINOR(name->dev),
289 for (j = 0; j < ctx->name_count; j++) {
290 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
299 result = (name->ino == f->val ||
300 name->pino == f->val);
302 for (j = 0; j < ctx->name_count; j++) {
303 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
304 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
312 if (name && rule->watch->ino != (unsigned long)-1)
313 result = (name->dev == rule->watch->dev &&
314 (name->ino == rule->watch->ino ||
315 name->pino == rule->watch->ino));
320 result = audit_comparator(ctx->loginuid, f->op, f->val);
327 /* NOTE: this may return negative values indicating
328 a temporary error. We simply treat this as a
329 match for now to avoid losing information that
330 may be wanted. An error message will also be
334 selinux_task_ctxid(tsk, &sid);
337 result = selinux_audit_rule_match(sid, f->type,
348 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
355 switch (rule->action) {
356 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
357 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
362 /* At process creation time, we can determine if system-call auditing is
363 * completely disabled for this task. Since we only have the task
364 * structure at this point, we can only check uid and gid.
366 static enum audit_state audit_filter_task(struct task_struct *tsk)
368 struct audit_entry *e;
369 enum audit_state state;
372 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
373 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
379 return AUDIT_BUILD_CONTEXT;
382 /* At syscall entry and exit time, this filter is called if the
383 * audit_state is not low enough that auditing cannot take place, but is
384 * also not high enough that we already know we have to write an audit
385 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
387 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
388 struct audit_context *ctx,
389 struct list_head *list)
391 struct audit_entry *e;
392 enum audit_state state;
394 if (audit_pid && tsk->tgid == audit_pid)
395 return AUDIT_DISABLED;
398 if (!list_empty(list)) {
399 int word = AUDIT_WORD(ctx->major);
400 int bit = AUDIT_BIT(ctx->major);
402 list_for_each_entry_rcu(e, list, list) {
403 if ((e->rule.mask[word] & bit) == bit &&
404 audit_filter_rules(tsk, &e->rule, ctx, NULL,
412 return AUDIT_BUILD_CONTEXT;
415 /* At syscall exit time, this filter is called if any audit_names[] have been
416 * collected during syscall processing. We only check rules in sublists at hash
417 * buckets applicable to the inode numbers in audit_names[].
418 * Regarding audit_state, same rules apply as for audit_filter_syscall().
420 enum audit_state audit_filter_inodes(struct task_struct *tsk,
421 struct audit_context *ctx)
424 struct audit_entry *e;
425 enum audit_state state;
427 if (audit_pid && tsk->tgid == audit_pid)
428 return AUDIT_DISABLED;
431 for (i = 0; i < ctx->name_count; i++) {
432 int word = AUDIT_WORD(ctx->major);
433 int bit = AUDIT_BIT(ctx->major);
434 struct audit_names *n = &ctx->names[i];
435 int h = audit_hash_ino((u32)n->ino);
436 struct list_head *list = &audit_inode_hash[h];
438 if (list_empty(list))
441 list_for_each_entry_rcu(e, list, list) {
442 if ((e->rule.mask[word] & bit) == bit &&
443 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
450 return AUDIT_BUILD_CONTEXT;
453 void audit_set_auditable(struct audit_context *ctx)
458 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
462 struct audit_context *context = tsk->audit_context;
464 if (likely(!context))
466 context->return_valid = return_valid;
467 context->return_code = return_code;
469 if (context->in_syscall && !context->auditable) {
470 enum audit_state state;
472 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
473 if (state == AUDIT_RECORD_CONTEXT) {
474 context->auditable = 1;
478 state = audit_filter_inodes(tsk, context);
479 if (state == AUDIT_RECORD_CONTEXT)
480 context->auditable = 1;
485 context->pid = tsk->pid;
486 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
487 context->uid = tsk->uid;
488 context->gid = tsk->gid;
489 context->euid = tsk->euid;
490 context->suid = tsk->suid;
491 context->fsuid = tsk->fsuid;
492 context->egid = tsk->egid;
493 context->sgid = tsk->sgid;
494 context->fsgid = tsk->fsgid;
495 context->personality = tsk->personality;
496 tsk->audit_context = NULL;
500 static inline void audit_free_names(struct audit_context *context)
505 if (context->auditable
506 ||context->put_count + context->ino_count != context->name_count) {
507 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
508 " name_count=%d put_count=%d"
509 " ino_count=%d [NOT freeing]\n",
511 context->serial, context->major, context->in_syscall,
512 context->name_count, context->put_count,
514 for (i = 0; i < context->name_count; i++) {
515 printk(KERN_ERR "names[%d] = %p = %s\n", i,
516 context->names[i].name,
517 context->names[i].name ?: "(null)");
524 context->put_count = 0;
525 context->ino_count = 0;
528 for (i = 0; i < context->name_count; i++) {
529 if (context->names[i].name)
530 __putname(context->names[i].name);
532 context->name_count = 0;
536 mntput(context->pwdmnt);
538 context->pwdmnt = NULL;
541 static inline void audit_free_aux(struct audit_context *context)
543 struct audit_aux_data *aux;
545 while ((aux = context->aux)) {
546 if (aux->type == AUDIT_AVC_PATH) {
547 struct audit_aux_data_path *axi = (void *)aux;
552 context->aux = aux->next;
557 static inline void audit_zero_context(struct audit_context *context,
558 enum audit_state state)
560 uid_t loginuid = context->loginuid;
562 memset(context, 0, sizeof(*context));
563 context->state = state;
564 context->loginuid = loginuid;
567 static inline struct audit_context *audit_alloc_context(enum audit_state state)
569 struct audit_context *context;
571 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
573 audit_zero_context(context, state);
578 * audit_alloc - allocate an audit context block for a task
581 * Filter on the task information and allocate a per-task audit context
582 * if necessary. Doing so turns on system call auditing for the
583 * specified task. This is called from copy_process, so no lock is
586 int audit_alloc(struct task_struct *tsk)
588 struct audit_context *context;
589 enum audit_state state;
591 if (likely(!audit_enabled))
592 return 0; /* Return if not auditing. */
594 state = audit_filter_task(tsk);
595 if (likely(state == AUDIT_DISABLED))
598 if (!(context = audit_alloc_context(state))) {
599 audit_log_lost("out of memory in audit_alloc");
603 /* Preserve login uid */
604 context->loginuid = -1;
605 if (current->audit_context)
606 context->loginuid = current->audit_context->loginuid;
608 tsk->audit_context = context;
609 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
613 static inline void audit_free_context(struct audit_context *context)
615 struct audit_context *previous;
619 previous = context->previous;
620 if (previous || (count && count < 10)) {
622 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
623 " freeing multiple contexts (%d)\n",
624 context->serial, context->major,
625 context->name_count, count);
627 audit_free_names(context);
628 audit_free_aux(context);
633 printk(KERN_ERR "audit: freed %d contexts\n", count);
636 static void audit_log_task_context(struct audit_buffer *ab)
641 len = security_getprocattr(current, "current", NULL, 0);
648 ctx = kmalloc(len, GFP_KERNEL);
652 len = security_getprocattr(current, "current", ctx, len);
656 audit_log_format(ab, " subj=%s", ctx);
662 audit_panic("error in audit_log_task_context");
666 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
668 char name[sizeof(tsk->comm)];
669 struct mm_struct *mm = tsk->mm;
670 struct vm_area_struct *vma;
674 get_task_comm(name, tsk);
675 audit_log_format(ab, " comm=");
676 audit_log_untrustedstring(ab, name);
679 down_read(&mm->mmap_sem);
682 if ((vma->vm_flags & VM_EXECUTABLE) &&
684 audit_log_d_path(ab, "exe=",
685 vma->vm_file->f_dentry,
686 vma->vm_file->f_vfsmnt);
691 up_read(&mm->mmap_sem);
693 audit_log_task_context(ab);
696 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
698 int i, call_panic = 0;
699 struct audit_buffer *ab;
700 struct audit_aux_data *aux;
705 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
707 return; /* audit_panic has been called */
708 audit_log_format(ab, "arch=%x syscall=%d",
709 context->arch, context->major);
710 if (context->personality != PER_LINUX)
711 audit_log_format(ab, " per=%lx", context->personality);
712 if (context->return_valid)
713 audit_log_format(ab, " success=%s exit=%ld",
714 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
715 context->return_code);
716 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
717 tty = tsk->signal->tty->name;
721 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
722 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
723 " euid=%u suid=%u fsuid=%u"
724 " egid=%u sgid=%u fsgid=%u tty=%s",
735 context->euid, context->suid, context->fsuid,
736 context->egid, context->sgid, context->fsgid, tty);
737 audit_log_task_info(ab, tsk);
740 for (aux = context->aux; aux; aux = aux->next) {
742 ab = audit_log_start(context, GFP_KERNEL, aux->type);
744 continue; /* audit_panic has been called */
747 case AUDIT_MQ_OPEN: {
748 struct audit_aux_data_mq_open *axi = (void *)aux;
750 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
751 "mq_msgsize=%ld mq_curmsgs=%ld",
752 axi->oflag, axi->mode, axi->attr.mq_flags,
753 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
754 axi->attr.mq_curmsgs);
757 case AUDIT_MQ_SENDRECV: {
758 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
760 "mqdes=%d msg_len=%zd msg_prio=%u "
761 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
762 axi->mqdes, axi->msg_len, axi->msg_prio,
763 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
766 case AUDIT_MQ_NOTIFY: {
767 struct audit_aux_data_mq_notify *axi = (void *)aux;
769 "mqdes=%d sigev_signo=%d",
771 axi->notification.sigev_signo);
774 case AUDIT_MQ_GETSETATTR: {
775 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
777 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
780 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
781 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
785 struct audit_aux_data_ipcctl *axi = (void *)aux;
787 "ouid=%u ogid=%u mode=%x",
788 axi->uid, axi->gid, axi->mode);
789 if (axi->osid != 0) {
792 if (selinux_ctxid_to_string(
793 axi->osid, &ctx, &len)) {
794 audit_log_format(ab, " osid=%u",
798 audit_log_format(ab, " obj=%s", ctx);
803 case AUDIT_IPC_SET_PERM: {
804 struct audit_aux_data_ipcctl *axi = (void *)aux;
806 "qbytes=%lx ouid=%u ogid=%u mode=%x",
807 axi->qbytes, axi->uid, axi->gid, axi->mode);
811 struct audit_aux_data_execve *axi = (void *)aux;
814 for (i = 0, p = axi->mem; i < axi->argc; i++) {
815 audit_log_format(ab, "a%d=", i);
816 p = audit_log_untrustedstring(ab, p);
817 audit_log_format(ab, "\n");
821 case AUDIT_SOCKETCALL: {
823 struct audit_aux_data_socketcall *axs = (void *)aux;
824 audit_log_format(ab, "nargs=%d", axs->nargs);
825 for (i=0; i<axs->nargs; i++)
826 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
829 case AUDIT_SOCKADDR: {
830 struct audit_aux_data_sockaddr *axs = (void *)aux;
832 audit_log_format(ab, "saddr=");
833 audit_log_hex(ab, axs->a, axs->len);
836 case AUDIT_AVC_PATH: {
837 struct audit_aux_data_path *axi = (void *)aux;
838 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
845 if (context->pwd && context->pwdmnt) {
846 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
848 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
852 for (i = 0; i < context->name_count; i++) {
853 unsigned long ino = context->names[i].ino;
854 unsigned long pino = context->names[i].pino;
856 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
858 continue; /* audit_panic has been called */
860 audit_log_format(ab, "item=%d", i);
862 audit_log_format(ab, " name=");
863 if (context->names[i].name)
864 audit_log_untrustedstring(ab, context->names[i].name);
866 audit_log_format(ab, "(null)");
868 if (pino != (unsigned long)-1)
869 audit_log_format(ab, " parent=%lu", pino);
870 if (ino != (unsigned long)-1)
871 audit_log_format(ab, " inode=%lu", ino);
872 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
873 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
874 " ouid=%u ogid=%u rdev=%02x:%02x",
875 MAJOR(context->names[i].dev),
876 MINOR(context->names[i].dev),
877 context->names[i].mode,
878 context->names[i].uid,
879 context->names[i].gid,
880 MAJOR(context->names[i].rdev),
881 MINOR(context->names[i].rdev));
882 if (context->names[i].osid != 0) {
885 if (selinux_ctxid_to_string(
886 context->names[i].osid, &ctx, &len)) {
887 audit_log_format(ab, " osid=%u",
888 context->names[i].osid);
891 audit_log_format(ab, " obj=%s", ctx);
898 audit_panic("error converting sid to string");
902 * audit_free - free a per-task audit context
903 * @tsk: task whose audit context block to free
905 * Called from copy_process and do_exit
907 void audit_free(struct task_struct *tsk)
909 struct audit_context *context;
911 context = audit_get_context(tsk, 0, 0);
912 if (likely(!context))
915 /* Check for system calls that do not go through the exit
916 * function (e.g., exit_group), then free context block.
917 * We use GFP_ATOMIC here because we might be doing this
918 * in the context of the idle thread */
919 /* that can happen only if we are called from do_exit() */
920 if (context->in_syscall && context->auditable)
921 audit_log_exit(context, tsk);
923 audit_free_context(context);
927 * audit_syscall_entry - fill in an audit record at syscall entry
928 * @tsk: task being audited
929 * @arch: architecture type
930 * @major: major syscall type (function)
931 * @a1: additional syscall register 1
932 * @a2: additional syscall register 2
933 * @a3: additional syscall register 3
934 * @a4: additional syscall register 4
936 * Fill in audit context at syscall entry. This only happens if the
937 * audit context was created when the task was created and the state or
938 * filters demand the audit context be built. If the state from the
939 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
940 * then the record will be written at syscall exit time (otherwise, it
941 * will only be written if another part of the kernel requests that it
944 void audit_syscall_entry(int arch, int major,
945 unsigned long a1, unsigned long a2,
946 unsigned long a3, unsigned long a4)
948 struct task_struct *tsk = current;
949 struct audit_context *context = tsk->audit_context;
950 enum audit_state state;
955 * This happens only on certain architectures that make system
956 * calls in kernel_thread via the entry.S interface, instead of
957 * with direct calls. (If you are porting to a new
958 * architecture, hitting this condition can indicate that you
959 * got the _exit/_leave calls backward in entry.S.)
963 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
965 * This also happens with vm86 emulation in a non-nested manner
966 * (entries without exits), so this case must be caught.
968 if (context->in_syscall) {
969 struct audit_context *newctx;
973 "audit(:%d) pid=%d in syscall=%d;"
974 " entering syscall=%d\n",
975 context->serial, tsk->pid, context->major, major);
977 newctx = audit_alloc_context(context->state);
979 newctx->previous = context;
981 tsk->audit_context = newctx;
983 /* If we can't alloc a new context, the best we
984 * can do is to leak memory (any pending putname
985 * will be lost). The only other alternative is
986 * to abandon auditing. */
987 audit_zero_context(context, context->state);
990 BUG_ON(context->in_syscall || context->name_count);
995 context->arch = arch;
996 context->major = major;
997 context->argv[0] = a1;
998 context->argv[1] = a2;
999 context->argv[2] = a3;
1000 context->argv[3] = a4;
1002 state = context->state;
1003 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
1004 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1005 if (likely(state == AUDIT_DISABLED))
1008 context->serial = 0;
1009 context->ctime = CURRENT_TIME;
1010 context->in_syscall = 1;
1011 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1015 * audit_syscall_exit - deallocate audit context after a system call
1016 * @tsk: task being audited
1017 * @valid: success/failure flag
1018 * @return_code: syscall return value
1020 * Tear down after system call. If the audit context has been marked as
1021 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1022 * filtering, or because some other part of the kernel write an audit
1023 * message), then write out the syscall information. In call cases,
1024 * free the names stored from getname().
1026 void audit_syscall_exit(int valid, long return_code)
1028 struct task_struct *tsk = current;
1029 struct audit_context *context;
1031 context = audit_get_context(tsk, valid, return_code);
1033 if (likely(!context))
1036 if (context->in_syscall && context->auditable)
1037 audit_log_exit(context, tsk);
1039 context->in_syscall = 0;
1040 context->auditable = 0;
1042 if (context->previous) {
1043 struct audit_context *new_context = context->previous;
1044 context->previous = NULL;
1045 audit_free_context(context);
1046 tsk->audit_context = new_context;
1048 audit_free_names(context);
1049 audit_free_aux(context);
1050 tsk->audit_context = context;
1055 * audit_getname - add a name to the list
1056 * @name: name to add
1058 * Add a name to the list of audit names for this context.
1059 * Called from fs/namei.c:getname().
1061 void __audit_getname(const char *name)
1063 struct audit_context *context = current->audit_context;
1065 if (IS_ERR(name) || !name)
1068 if (!context->in_syscall) {
1069 #if AUDIT_DEBUG == 2
1070 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1071 __FILE__, __LINE__, context->serial, name);
1076 BUG_ON(context->name_count >= AUDIT_NAMES);
1077 context->names[context->name_count].name = name;
1078 context->names[context->name_count].ino = (unsigned long)-1;
1079 ++context->name_count;
1080 if (!context->pwd) {
1081 read_lock(¤t->fs->lock);
1082 context->pwd = dget(current->fs->pwd);
1083 context->pwdmnt = mntget(current->fs->pwdmnt);
1084 read_unlock(¤t->fs->lock);
1089 /* audit_putname - intercept a putname request
1090 * @name: name to intercept and delay for putname
1092 * If we have stored the name from getname in the audit context,
1093 * then we delay the putname until syscall exit.
1094 * Called from include/linux/fs.h:putname().
1096 void audit_putname(const char *name)
1098 struct audit_context *context = current->audit_context;
1101 if (!context->in_syscall) {
1102 #if AUDIT_DEBUG == 2
1103 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1104 __FILE__, __LINE__, context->serial, name);
1105 if (context->name_count) {
1107 for (i = 0; i < context->name_count; i++)
1108 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1109 context->names[i].name,
1110 context->names[i].name ?: "(null)");
1117 ++context->put_count;
1118 if (context->put_count > context->name_count) {
1119 printk(KERN_ERR "%s:%d(:%d): major=%d"
1120 " in_syscall=%d putname(%p) name_count=%d"
1123 context->serial, context->major,
1124 context->in_syscall, name, context->name_count,
1125 context->put_count);
1132 static void audit_inode_context(int idx, const struct inode *inode)
1134 struct audit_context *context = current->audit_context;
1136 selinux_get_inode_sid(inode, &context->names[idx].osid);
1141 * audit_inode - store the inode and device from a lookup
1142 * @name: name being audited
1143 * @inode: inode being audited
1144 * @flags: lookup flags (as used in path_lookup())
1146 * Called from fs/namei.c:path_lookup().
1148 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1151 struct audit_context *context = current->audit_context;
1153 if (!context->in_syscall)
1155 if (context->name_count
1156 && context->names[context->name_count-1].name
1157 && context->names[context->name_count-1].name == name)
1158 idx = context->name_count - 1;
1159 else if (context->name_count > 1
1160 && context->names[context->name_count-2].name
1161 && context->names[context->name_count-2].name == name)
1162 idx = context->name_count - 2;
1164 /* FIXME: how much do we care about inodes that have no
1165 * associated name? */
1166 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1168 idx = context->name_count++;
1169 context->names[idx].name = NULL;
1171 ++context->ino_count;
1174 context->names[idx].dev = inode->i_sb->s_dev;
1175 context->names[idx].mode = inode->i_mode;
1176 context->names[idx].uid = inode->i_uid;
1177 context->names[idx].gid = inode->i_gid;
1178 context->names[idx].rdev = inode->i_rdev;
1179 audit_inode_context(idx, inode);
1180 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1181 (strcmp(name, ".") != 0)) {
1182 context->names[idx].ino = (unsigned long)-1;
1183 context->names[idx].pino = inode->i_ino;
1185 context->names[idx].ino = inode->i_ino;
1186 context->names[idx].pino = (unsigned long)-1;
1191 * audit_inode_child - collect inode info for created/removed objects
1192 * @dname: inode's dentry name
1193 * @inode: inode being audited
1194 * @pino: inode number of dentry parent
1196 * For syscalls that create or remove filesystem objects, audit_inode
1197 * can only collect information for the filesystem object's parent.
1198 * This call updates the audit context with the child's information.
1199 * Syscalls that create a new filesystem object must be hooked after
1200 * the object is created. Syscalls that remove a filesystem object
1201 * must be hooked prior, in order to capture the target inode during
1202 * unsuccessful attempts.
1204 void __audit_inode_child(const char *dname, const struct inode *inode,
1208 struct audit_context *context = current->audit_context;
1210 if (!context->in_syscall)
1213 /* determine matching parent */
1216 for (idx = 0; idx < context->name_count; idx++)
1217 if (context->names[idx].pino == pino) {
1218 const char *name = context->names[idx].name;
1223 if (audit_compare_dname_path(dname, name) == 0)
1224 goto update_context;
1228 /* catch-all in case match not found */
1229 idx = context->name_count++;
1230 context->names[idx].name = NULL;
1231 context->names[idx].pino = pino;
1233 context->ino_count++;
1238 context->names[idx].ino = inode->i_ino;
1239 context->names[idx].dev = inode->i_sb->s_dev;
1240 context->names[idx].mode = inode->i_mode;
1241 context->names[idx].uid = inode->i_uid;
1242 context->names[idx].gid = inode->i_gid;
1243 context->names[idx].rdev = inode->i_rdev;
1244 audit_inode_context(idx, inode);
1249 * auditsc_get_stamp - get local copies of audit_context values
1250 * @ctx: audit_context for the task
1251 * @t: timespec to store time recorded in the audit_context
1252 * @serial: serial value that is recorded in the audit_context
1254 * Also sets the context as auditable.
1256 void auditsc_get_stamp(struct audit_context *ctx,
1257 struct timespec *t, unsigned int *serial)
1260 ctx->serial = audit_serial();
1261 t->tv_sec = ctx->ctime.tv_sec;
1262 t->tv_nsec = ctx->ctime.tv_nsec;
1263 *serial = ctx->serial;
1268 * audit_set_loginuid - set a task's audit_context loginuid
1269 * @task: task whose audit context is being modified
1270 * @loginuid: loginuid value
1274 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1276 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1278 if (task->audit_context) {
1279 struct audit_buffer *ab;
1281 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1283 audit_log_format(ab, "login pid=%d uid=%u "
1284 "old auid=%u new auid=%u",
1285 task->pid, task->uid,
1286 task->audit_context->loginuid, loginuid);
1289 task->audit_context->loginuid = loginuid;
1295 * audit_get_loginuid - get the loginuid for an audit_context
1296 * @ctx: the audit_context
1298 * Returns the context's loginuid or -1 if @ctx is NULL.
1300 uid_t audit_get_loginuid(struct audit_context *ctx)
1302 return ctx ? ctx->loginuid : -1;
1306 * __audit_mq_open - record audit data for a POSIX MQ open
1309 * @u_attr: queue attributes
1311 * Returns 0 for success or NULL context or < 0 on error.
1313 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1315 struct audit_aux_data_mq_open *ax;
1316 struct audit_context *context = current->audit_context;
1321 if (likely(!context))
1324 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1328 if (u_attr != NULL) {
1329 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1334 memset(&ax->attr, 0, sizeof(ax->attr));
1339 ax->d.type = AUDIT_MQ_OPEN;
1340 ax->d.next = context->aux;
1341 context->aux = (void *)ax;
1346 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1347 * @mqdes: MQ descriptor
1348 * @msg_len: Message length
1349 * @msg_prio: Message priority
1350 * @abs_timeout: Message timeout in absolute time
1352 * Returns 0 for success or NULL context or < 0 on error.
1354 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1355 const struct timespec __user *u_abs_timeout)
1357 struct audit_aux_data_mq_sendrecv *ax;
1358 struct audit_context *context = current->audit_context;
1363 if (likely(!context))
1366 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1370 if (u_abs_timeout != NULL) {
1371 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1376 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1379 ax->msg_len = msg_len;
1380 ax->msg_prio = msg_prio;
1382 ax->d.type = AUDIT_MQ_SENDRECV;
1383 ax->d.next = context->aux;
1384 context->aux = (void *)ax;
1389 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1390 * @mqdes: MQ descriptor
1391 * @msg_len: Message length
1392 * @msg_prio: Message priority
1393 * @abs_timeout: Message timeout in absolute time
1395 * Returns 0 for success or NULL context or < 0 on error.
1397 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1398 unsigned int __user *u_msg_prio,
1399 const struct timespec __user *u_abs_timeout)
1401 struct audit_aux_data_mq_sendrecv *ax;
1402 struct audit_context *context = current->audit_context;
1407 if (likely(!context))
1410 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1414 if (u_msg_prio != NULL) {
1415 if (get_user(ax->msg_prio, u_msg_prio)) {
1422 if (u_abs_timeout != NULL) {
1423 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1428 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1431 ax->msg_len = msg_len;
1433 ax->d.type = AUDIT_MQ_SENDRECV;
1434 ax->d.next = context->aux;
1435 context->aux = (void *)ax;
1440 * __audit_mq_notify - record audit data for a POSIX MQ notify
1441 * @mqdes: MQ descriptor
1442 * @u_notification: Notification event
1444 * Returns 0 for success or NULL context or < 0 on error.
1447 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1449 struct audit_aux_data_mq_notify *ax;
1450 struct audit_context *context = current->audit_context;
1455 if (likely(!context))
1458 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1462 if (u_notification != NULL) {
1463 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1468 memset(&ax->notification, 0, sizeof(ax->notification));
1472 ax->d.type = AUDIT_MQ_NOTIFY;
1473 ax->d.next = context->aux;
1474 context->aux = (void *)ax;
1479 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1480 * @mqdes: MQ descriptor
1483 * Returns 0 for success or NULL context or < 0 on error.
1485 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1487 struct audit_aux_data_mq_getsetattr *ax;
1488 struct audit_context *context = current->audit_context;
1493 if (likely(!context))
1496 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1501 ax->mqstat = *mqstat;
1503 ax->d.type = AUDIT_MQ_GETSETATTR;
1504 ax->d.next = context->aux;
1505 context->aux = (void *)ax;
1510 * audit_ipc_obj - record audit data for ipc object
1511 * @ipcp: ipc permissions
1513 * Returns 0 for success or NULL context or < 0 on error.
1515 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
1517 struct audit_aux_data_ipcctl *ax;
1518 struct audit_context *context = current->audit_context;
1520 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1524 ax->uid = ipcp->uid;
1525 ax->gid = ipcp->gid;
1526 ax->mode = ipcp->mode;
1527 selinux_get_ipc_sid(ipcp, &ax->osid);
1529 ax->d.type = AUDIT_IPC;
1530 ax->d.next = context->aux;
1531 context->aux = (void *)ax;
1536 * audit_ipc_set_perm - record audit data for new ipc permissions
1537 * @qbytes: msgq bytes
1538 * @uid: msgq user id
1539 * @gid: msgq group id
1540 * @mode: msgq mode (permissions)
1542 * Returns 0 for success or NULL context or < 0 on error.
1544 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1546 struct audit_aux_data_ipcctl *ax;
1547 struct audit_context *context = current->audit_context;
1549 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1553 ax->qbytes = qbytes;
1558 ax->d.type = AUDIT_IPC_SET_PERM;
1559 ax->d.next = context->aux;
1560 context->aux = (void *)ax;
1564 int audit_bprm(struct linux_binprm *bprm)
1566 struct audit_aux_data_execve *ax;
1567 struct audit_context *context = current->audit_context;
1568 unsigned long p, next;
1571 if (likely(!audit_enabled || !context))
1574 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1579 ax->argc = bprm->argc;
1580 ax->envc = bprm->envc;
1581 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1582 struct page *page = bprm->page[p / PAGE_SIZE];
1583 void *kaddr = kmap(page);
1584 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1585 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1590 ax->d.type = AUDIT_EXECVE;
1591 ax->d.next = context->aux;
1592 context->aux = (void *)ax;
1598 * audit_socketcall - record audit data for sys_socketcall
1599 * @nargs: number of args
1602 * Returns 0 for success or NULL context or < 0 on error.
1604 int audit_socketcall(int nargs, unsigned long *args)
1606 struct audit_aux_data_socketcall *ax;
1607 struct audit_context *context = current->audit_context;
1609 if (likely(!context))
1612 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1617 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1619 ax->d.type = AUDIT_SOCKETCALL;
1620 ax->d.next = context->aux;
1621 context->aux = (void *)ax;
1626 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1627 * @len: data length in user space
1628 * @a: data address in kernel space
1630 * Returns 0 for success or NULL context or < 0 on error.
1632 int audit_sockaddr(int len, void *a)
1634 struct audit_aux_data_sockaddr *ax;
1635 struct audit_context *context = current->audit_context;
1637 if (likely(!context))
1640 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1645 memcpy(ax->a, a, len);
1647 ax->d.type = AUDIT_SOCKADDR;
1648 ax->d.next = context->aux;
1649 context->aux = (void *)ax;
1654 * audit_avc_path - record the granting or denial of permissions
1655 * @dentry: dentry to record
1656 * @mnt: mnt to record
1658 * Returns 0 for success or NULL context or < 0 on error.
1660 * Called from security/selinux/avc.c::avc_audit()
1662 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1664 struct audit_aux_data_path *ax;
1665 struct audit_context *context = current->audit_context;
1667 if (likely(!context))
1670 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1674 ax->dentry = dget(dentry);
1675 ax->mnt = mntget(mnt);
1677 ax->d.type = AUDIT_AVC_PATH;
1678 ax->d.next = context->aux;
1679 context->aux = (void *)ax;
1684 * audit_signal_info - record signal info for shutting down audit subsystem
1685 * @sig: signal value
1686 * @t: task being signaled
1688 * If the audit subsystem is being terminated, record the task (pid)
1689 * and uid that is doing that.
1691 void __audit_signal_info(int sig, struct task_struct *t)
1693 extern pid_t audit_sig_pid;
1694 extern uid_t audit_sig_uid;
1695 extern u32 audit_sig_sid;
1697 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1698 struct task_struct *tsk = current;
1699 struct audit_context *ctx = tsk->audit_context;
1700 audit_sig_pid = tsk->pid;
1702 audit_sig_uid = ctx->loginuid;
1704 audit_sig_uid = tsk->uid;
1705 selinux_get_task_sid(tsk, &audit_sig_sid);