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;
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 enum audit_state *state)
211 int i, j, need_sid = 1;
214 for (i = 0; i < rule->field_count; i++) {
215 struct audit_field *f = &rule->fields[i];
220 result = audit_comparator(tsk->pid, f->op, f->val);
224 result = audit_comparator(ctx->ppid, f->op, f->val);
227 result = audit_comparator(tsk->uid, f->op, f->val);
230 result = audit_comparator(tsk->euid, f->op, f->val);
233 result = audit_comparator(tsk->suid, f->op, f->val);
236 result = audit_comparator(tsk->fsuid, f->op, f->val);
239 result = audit_comparator(tsk->gid, f->op, f->val);
242 result = audit_comparator(tsk->egid, f->op, f->val);
245 result = audit_comparator(tsk->sgid, f->op, f->val);
248 result = audit_comparator(tsk->fsgid, f->op, f->val);
251 result = audit_comparator(tsk->personality, f->op, f->val);
255 result = audit_comparator(ctx->arch, f->op, f->val);
259 if (ctx && ctx->return_valid)
260 result = audit_comparator(ctx->return_code, f->op, f->val);
263 if (ctx && ctx->return_valid) {
265 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
267 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
272 for (j = 0; j < ctx->name_count; j++) {
273 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
282 for (j = 0; j < ctx->name_count; j++) {
283 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
292 for (j = 0; j < ctx->name_count; j++) {
293 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
294 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
304 result = audit_comparator(ctx->loginuid, f->op, f->val);
311 /* NOTE: this may return negative values indicating
312 a temporary error. We simply treat this as a
313 match for now to avoid losing information that
314 may be wanted. An error message will also be
318 selinux_task_ctxid(tsk, &sid);
321 result = selinux_audit_rule_match(sid, f->type,
332 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
339 switch (rule->action) {
340 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
341 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
346 /* At process creation time, we can determine if system-call auditing is
347 * completely disabled for this task. Since we only have the task
348 * structure at this point, we can only check uid and gid.
350 static enum audit_state audit_filter_task(struct task_struct *tsk)
352 struct audit_entry *e;
353 enum audit_state state;
356 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
357 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
363 return AUDIT_BUILD_CONTEXT;
366 /* At syscall entry and exit time, this filter is called if the
367 * audit_state is not low enough that auditing cannot take place, but is
368 * also not high enough that we already know we have to write an audit
369 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
371 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
372 struct audit_context *ctx,
373 struct list_head *list)
375 struct audit_entry *e;
376 enum audit_state state;
378 if (audit_pid && tsk->tgid == audit_pid)
379 return AUDIT_DISABLED;
382 if (!list_empty(list)) {
383 int word = AUDIT_WORD(ctx->major);
384 int bit = AUDIT_BIT(ctx->major);
386 list_for_each_entry_rcu(e, list, list) {
387 if ((e->rule.mask[word] & bit) == bit
388 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
395 return AUDIT_BUILD_CONTEXT;
398 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
402 struct audit_context *context = tsk->audit_context;
404 if (likely(!context))
406 context->return_valid = return_valid;
407 context->return_code = return_code;
409 if (context->in_syscall && !context->auditable) {
410 enum audit_state state;
411 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
412 if (state == AUDIT_RECORD_CONTEXT)
413 context->auditable = 1;
416 context->pid = tsk->pid;
417 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
418 context->uid = tsk->uid;
419 context->gid = tsk->gid;
420 context->euid = tsk->euid;
421 context->suid = tsk->suid;
422 context->fsuid = tsk->fsuid;
423 context->egid = tsk->egid;
424 context->sgid = tsk->sgid;
425 context->fsgid = tsk->fsgid;
426 context->personality = tsk->personality;
427 tsk->audit_context = NULL;
431 static inline void audit_free_names(struct audit_context *context)
436 if (context->auditable
437 ||context->put_count + context->ino_count != context->name_count) {
438 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
439 " name_count=%d put_count=%d"
440 " ino_count=%d [NOT freeing]\n",
442 context->serial, context->major, context->in_syscall,
443 context->name_count, context->put_count,
445 for (i = 0; i < context->name_count; i++) {
446 printk(KERN_ERR "names[%d] = %p = %s\n", i,
447 context->names[i].name,
448 context->names[i].name ?: "(null)");
455 context->put_count = 0;
456 context->ino_count = 0;
459 for (i = 0; i < context->name_count; i++) {
460 if (context->names[i].name)
461 __putname(context->names[i].name);
463 context->name_count = 0;
467 mntput(context->pwdmnt);
469 context->pwdmnt = NULL;
472 static inline void audit_free_aux(struct audit_context *context)
474 struct audit_aux_data *aux;
476 while ((aux = context->aux)) {
477 if (aux->type == AUDIT_AVC_PATH) {
478 struct audit_aux_data_path *axi = (void *)aux;
483 context->aux = aux->next;
488 static inline void audit_zero_context(struct audit_context *context,
489 enum audit_state state)
491 uid_t loginuid = context->loginuid;
493 memset(context, 0, sizeof(*context));
494 context->state = state;
495 context->loginuid = loginuid;
498 static inline struct audit_context *audit_alloc_context(enum audit_state state)
500 struct audit_context *context;
502 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
504 audit_zero_context(context, state);
509 * audit_alloc - allocate an audit context block for a task
512 * Filter on the task information and allocate a per-task audit context
513 * if necessary. Doing so turns on system call auditing for the
514 * specified task. This is called from copy_process, so no lock is
517 int audit_alloc(struct task_struct *tsk)
519 struct audit_context *context;
520 enum audit_state state;
522 if (likely(!audit_enabled))
523 return 0; /* Return if not auditing. */
525 state = audit_filter_task(tsk);
526 if (likely(state == AUDIT_DISABLED))
529 if (!(context = audit_alloc_context(state))) {
530 audit_log_lost("out of memory in audit_alloc");
534 /* Preserve login uid */
535 context->loginuid = -1;
536 if (current->audit_context)
537 context->loginuid = current->audit_context->loginuid;
539 tsk->audit_context = context;
540 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
544 static inline void audit_free_context(struct audit_context *context)
546 struct audit_context *previous;
550 previous = context->previous;
551 if (previous || (count && count < 10)) {
553 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
554 " freeing multiple contexts (%d)\n",
555 context->serial, context->major,
556 context->name_count, count);
558 audit_free_names(context);
559 audit_free_aux(context);
564 printk(KERN_ERR "audit: freed %d contexts\n", count);
567 static void audit_log_task_context(struct audit_buffer *ab)
572 len = security_getprocattr(current, "current", NULL, 0);
579 ctx = kmalloc(len, GFP_KERNEL);
583 len = security_getprocattr(current, "current", ctx, len);
587 audit_log_format(ab, " subj=%s", ctx);
593 audit_panic("error in audit_log_task_context");
597 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
599 char name[sizeof(tsk->comm)];
600 struct mm_struct *mm = tsk->mm;
601 struct vm_area_struct *vma;
605 get_task_comm(name, tsk);
606 audit_log_format(ab, " comm=");
607 audit_log_untrustedstring(ab, name);
610 down_read(&mm->mmap_sem);
613 if ((vma->vm_flags & VM_EXECUTABLE) &&
615 audit_log_d_path(ab, "exe=",
616 vma->vm_file->f_dentry,
617 vma->vm_file->f_vfsmnt);
622 up_read(&mm->mmap_sem);
624 audit_log_task_context(ab);
627 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
629 int i, call_panic = 0;
630 struct audit_buffer *ab;
631 struct audit_aux_data *aux;
636 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
638 return; /* audit_panic has been called */
639 audit_log_format(ab, "arch=%x syscall=%d",
640 context->arch, context->major);
641 if (context->personality != PER_LINUX)
642 audit_log_format(ab, " per=%lx", context->personality);
643 if (context->return_valid)
644 audit_log_format(ab, " success=%s exit=%ld",
645 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
646 context->return_code);
647 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
648 tty = tsk->signal->tty->name;
652 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
653 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
654 " euid=%u suid=%u fsuid=%u"
655 " egid=%u sgid=%u fsgid=%u tty=%s",
666 context->euid, context->suid, context->fsuid,
667 context->egid, context->sgid, context->fsgid, tty);
668 audit_log_task_info(ab, tsk);
671 for (aux = context->aux; aux; aux = aux->next) {
673 ab = audit_log_start(context, GFP_KERNEL, aux->type);
675 continue; /* audit_panic has been called */
678 case AUDIT_MQ_OPEN: {
679 struct audit_aux_data_mq_open *axi = (void *)aux;
681 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
682 "mq_msgsize=%ld mq_curmsgs=%ld",
683 axi->oflag, axi->mode, axi->attr.mq_flags,
684 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
685 axi->attr.mq_curmsgs);
688 case AUDIT_MQ_SENDRECV: {
689 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
691 "mqdes=%d msg_len=%zd msg_prio=%u "
692 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
693 axi->mqdes, axi->msg_len, axi->msg_prio,
694 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
697 case AUDIT_MQ_NOTIFY: {
698 struct audit_aux_data_mq_notify *axi = (void *)aux;
700 "mqdes=%d sigev_signo=%d",
702 axi->notification.sigev_signo);
705 case AUDIT_MQ_GETSETATTR: {
706 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
708 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
711 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
712 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
716 struct audit_aux_data_ipcctl *axi = (void *)aux;
718 "ouid=%u ogid=%u mode=%x",
719 axi->uid, axi->gid, axi->mode);
720 if (axi->osid != 0) {
723 if (selinux_ctxid_to_string(
724 axi->osid, &ctx, &len)) {
725 audit_log_format(ab, " osid=%u",
729 audit_log_format(ab, " obj=%s", ctx);
734 case AUDIT_IPC_SET_PERM: {
735 struct audit_aux_data_ipcctl *axi = (void *)aux;
737 "qbytes=%lx ouid=%u ogid=%u mode=%x",
738 axi->qbytes, axi->uid, axi->gid, axi->mode);
742 struct audit_aux_data_execve *axi = (void *)aux;
745 for (i = 0, p = axi->mem; i < axi->argc; i++) {
746 audit_log_format(ab, "a%d=", i);
747 p = audit_log_untrustedstring(ab, p);
748 audit_log_format(ab, "\n");
752 case AUDIT_SOCKETCALL: {
754 struct audit_aux_data_socketcall *axs = (void *)aux;
755 audit_log_format(ab, "nargs=%d", axs->nargs);
756 for (i=0; i<axs->nargs; i++)
757 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
760 case AUDIT_SOCKADDR: {
761 struct audit_aux_data_sockaddr *axs = (void *)aux;
763 audit_log_format(ab, "saddr=");
764 audit_log_hex(ab, axs->a, axs->len);
767 case AUDIT_AVC_PATH: {
768 struct audit_aux_data_path *axi = (void *)aux;
769 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
776 if (context->pwd && context->pwdmnt) {
777 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
779 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
783 for (i = 0; i < context->name_count; i++) {
784 unsigned long ino = context->names[i].ino;
785 unsigned long pino = context->names[i].pino;
787 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
789 continue; /* audit_panic has been called */
791 audit_log_format(ab, "item=%d", i);
793 audit_log_format(ab, " name=");
794 if (context->names[i].name)
795 audit_log_untrustedstring(ab, context->names[i].name);
797 audit_log_format(ab, "(null)");
799 if (pino != (unsigned long)-1)
800 audit_log_format(ab, " parent=%lu", pino);
801 if (ino != (unsigned long)-1)
802 audit_log_format(ab, " inode=%lu", ino);
803 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
804 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
805 " ouid=%u ogid=%u rdev=%02x:%02x",
806 MAJOR(context->names[i].dev),
807 MINOR(context->names[i].dev),
808 context->names[i].mode,
809 context->names[i].uid,
810 context->names[i].gid,
811 MAJOR(context->names[i].rdev),
812 MINOR(context->names[i].rdev));
813 if (context->names[i].osid != 0) {
816 if (selinux_ctxid_to_string(
817 context->names[i].osid, &ctx, &len)) {
818 audit_log_format(ab, " osid=%u",
819 context->names[i].osid);
822 audit_log_format(ab, " obj=%s", ctx);
829 audit_panic("error converting sid to string");
833 * audit_free - free a per-task audit context
834 * @tsk: task whose audit context block to free
836 * Called from copy_process and do_exit
838 void audit_free(struct task_struct *tsk)
840 struct audit_context *context;
842 context = audit_get_context(tsk, 0, 0);
843 if (likely(!context))
846 /* Check for system calls that do not go through the exit
847 * function (e.g., exit_group), then free context block.
848 * We use GFP_ATOMIC here because we might be doing this
849 * in the context of the idle thread */
850 /* that can happen only if we are called from do_exit() */
851 if (context->in_syscall && context->auditable)
852 audit_log_exit(context, tsk);
854 audit_free_context(context);
858 * audit_syscall_entry - fill in an audit record at syscall entry
859 * @tsk: task being audited
860 * @arch: architecture type
861 * @major: major syscall type (function)
862 * @a1: additional syscall register 1
863 * @a2: additional syscall register 2
864 * @a3: additional syscall register 3
865 * @a4: additional syscall register 4
867 * Fill in audit context at syscall entry. This only happens if the
868 * audit context was created when the task was created and the state or
869 * filters demand the audit context be built. If the state from the
870 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
871 * then the record will be written at syscall exit time (otherwise, it
872 * will only be written if another part of the kernel requests that it
875 void audit_syscall_entry(int arch, int major,
876 unsigned long a1, unsigned long a2,
877 unsigned long a3, unsigned long a4)
879 struct task_struct *tsk = current;
880 struct audit_context *context = tsk->audit_context;
881 enum audit_state state;
886 * This happens only on certain architectures that make system
887 * calls in kernel_thread via the entry.S interface, instead of
888 * with direct calls. (If you are porting to a new
889 * architecture, hitting this condition can indicate that you
890 * got the _exit/_leave calls backward in entry.S.)
894 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
896 * This also happens with vm86 emulation in a non-nested manner
897 * (entries without exits), so this case must be caught.
899 if (context->in_syscall) {
900 struct audit_context *newctx;
904 "audit(:%d) pid=%d in syscall=%d;"
905 " entering syscall=%d\n",
906 context->serial, tsk->pid, context->major, major);
908 newctx = audit_alloc_context(context->state);
910 newctx->previous = context;
912 tsk->audit_context = newctx;
914 /* If we can't alloc a new context, the best we
915 * can do is to leak memory (any pending putname
916 * will be lost). The only other alternative is
917 * to abandon auditing. */
918 audit_zero_context(context, context->state);
921 BUG_ON(context->in_syscall || context->name_count);
926 context->arch = arch;
927 context->major = major;
928 context->argv[0] = a1;
929 context->argv[1] = a2;
930 context->argv[2] = a3;
931 context->argv[3] = a4;
933 state = context->state;
934 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
935 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
936 if (likely(state == AUDIT_DISABLED))
940 context->ctime = CURRENT_TIME;
941 context->in_syscall = 1;
942 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
946 * audit_syscall_exit - deallocate audit context after a system call
947 * @tsk: task being audited
948 * @valid: success/failure flag
949 * @return_code: syscall return value
951 * Tear down after system call. If the audit context has been marked as
952 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
953 * filtering, or because some other part of the kernel write an audit
954 * message), then write out the syscall information. In call cases,
955 * free the names stored from getname().
957 void audit_syscall_exit(int valid, long return_code)
959 struct task_struct *tsk = current;
960 struct audit_context *context;
962 context = audit_get_context(tsk, valid, return_code);
964 if (likely(!context))
967 if (context->in_syscall && context->auditable)
968 audit_log_exit(context, tsk);
970 context->in_syscall = 0;
971 context->auditable = 0;
973 if (context->previous) {
974 struct audit_context *new_context = context->previous;
975 context->previous = NULL;
976 audit_free_context(context);
977 tsk->audit_context = new_context;
979 audit_free_names(context);
980 audit_free_aux(context);
981 tsk->audit_context = context;
986 * audit_getname - add a name to the list
989 * Add a name to the list of audit names for this context.
990 * Called from fs/namei.c:getname().
992 void __audit_getname(const char *name)
994 struct audit_context *context = current->audit_context;
996 if (IS_ERR(name) || !name)
999 if (!context->in_syscall) {
1000 #if AUDIT_DEBUG == 2
1001 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1002 __FILE__, __LINE__, context->serial, name);
1007 BUG_ON(context->name_count >= AUDIT_NAMES);
1008 context->names[context->name_count].name = name;
1009 context->names[context->name_count].ino = (unsigned long)-1;
1010 ++context->name_count;
1011 if (!context->pwd) {
1012 read_lock(¤t->fs->lock);
1013 context->pwd = dget(current->fs->pwd);
1014 context->pwdmnt = mntget(current->fs->pwdmnt);
1015 read_unlock(¤t->fs->lock);
1020 /* audit_putname - intercept a putname request
1021 * @name: name to intercept and delay for putname
1023 * If we have stored the name from getname in the audit context,
1024 * then we delay the putname until syscall exit.
1025 * Called from include/linux/fs.h:putname().
1027 void audit_putname(const char *name)
1029 struct audit_context *context = current->audit_context;
1032 if (!context->in_syscall) {
1033 #if AUDIT_DEBUG == 2
1034 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1035 __FILE__, __LINE__, context->serial, name);
1036 if (context->name_count) {
1038 for (i = 0; i < context->name_count; i++)
1039 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1040 context->names[i].name,
1041 context->names[i].name ?: "(null)");
1048 ++context->put_count;
1049 if (context->put_count > context->name_count) {
1050 printk(KERN_ERR "%s:%d(:%d): major=%d"
1051 " in_syscall=%d putname(%p) name_count=%d"
1054 context->serial, context->major,
1055 context->in_syscall, name, context->name_count,
1056 context->put_count);
1063 static void audit_inode_context(int idx, const struct inode *inode)
1065 struct audit_context *context = current->audit_context;
1067 selinux_get_inode_sid(inode, &context->names[idx].osid);
1072 * audit_inode - store the inode and device from a lookup
1073 * @name: name being audited
1074 * @inode: inode being audited
1075 * @flags: lookup flags (as used in path_lookup())
1077 * Called from fs/namei.c:path_lookup().
1079 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1082 struct audit_context *context = current->audit_context;
1084 if (!context->in_syscall)
1086 if (context->name_count
1087 && context->names[context->name_count-1].name
1088 && context->names[context->name_count-1].name == name)
1089 idx = context->name_count - 1;
1090 else if (context->name_count > 1
1091 && context->names[context->name_count-2].name
1092 && context->names[context->name_count-2].name == name)
1093 idx = context->name_count - 2;
1095 /* FIXME: how much do we care about inodes that have no
1096 * associated name? */
1097 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1099 idx = context->name_count++;
1100 context->names[idx].name = NULL;
1102 ++context->ino_count;
1105 context->names[idx].dev = inode->i_sb->s_dev;
1106 context->names[idx].mode = inode->i_mode;
1107 context->names[idx].uid = inode->i_uid;
1108 context->names[idx].gid = inode->i_gid;
1109 context->names[idx].rdev = inode->i_rdev;
1110 audit_inode_context(idx, inode);
1111 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1112 (strcmp(name, ".") != 0)) {
1113 context->names[idx].ino = (unsigned long)-1;
1114 context->names[idx].pino = inode->i_ino;
1116 context->names[idx].ino = inode->i_ino;
1117 context->names[idx].pino = (unsigned long)-1;
1122 * audit_inode_child - collect inode info for created/removed objects
1123 * @dname: inode's dentry name
1124 * @inode: inode being audited
1125 * @pino: inode number of dentry parent
1127 * For syscalls that create or remove filesystem objects, audit_inode
1128 * can only collect information for the filesystem object's parent.
1129 * This call updates the audit context with the child's information.
1130 * Syscalls that create a new filesystem object must be hooked after
1131 * the object is created. Syscalls that remove a filesystem object
1132 * must be hooked prior, in order to capture the target inode during
1133 * unsuccessful attempts.
1135 void __audit_inode_child(const char *dname, const struct inode *inode,
1139 struct audit_context *context = current->audit_context;
1141 if (!context->in_syscall)
1144 /* determine matching parent */
1146 for (idx = 0; idx < context->name_count; idx++)
1147 if (context->names[idx].pino == pino) {
1149 const char *name = context->names[idx].name;
1150 int dlen = strlen(dname);
1151 int nlen = name ? strlen(name) : 0;
1156 /* disregard trailing slashes */
1157 n = name + nlen - 1;
1158 while ((*n == '/') && (n > name))
1161 /* find last path component */
1165 else if (n > name) {
1172 if (strncmp(n, dname, dlen) == 0)
1173 goto update_context;
1176 /* catch-all in case match not found */
1177 idx = context->name_count++;
1178 context->names[idx].name = NULL;
1179 context->names[idx].pino = pino;
1181 context->ino_count++;
1186 context->names[idx].ino = inode->i_ino;
1187 context->names[idx].dev = inode->i_sb->s_dev;
1188 context->names[idx].mode = inode->i_mode;
1189 context->names[idx].uid = inode->i_uid;
1190 context->names[idx].gid = inode->i_gid;
1191 context->names[idx].rdev = inode->i_rdev;
1192 audit_inode_context(idx, inode);
1197 * auditsc_get_stamp - get local copies of audit_context values
1198 * @ctx: audit_context for the task
1199 * @t: timespec to store time recorded in the audit_context
1200 * @serial: serial value that is recorded in the audit_context
1202 * Also sets the context as auditable.
1204 void auditsc_get_stamp(struct audit_context *ctx,
1205 struct timespec *t, unsigned int *serial)
1208 ctx->serial = audit_serial();
1209 t->tv_sec = ctx->ctime.tv_sec;
1210 t->tv_nsec = ctx->ctime.tv_nsec;
1211 *serial = ctx->serial;
1216 * audit_set_loginuid - set a task's audit_context loginuid
1217 * @task: task whose audit context is being modified
1218 * @loginuid: loginuid value
1222 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1224 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1226 if (task->audit_context) {
1227 struct audit_buffer *ab;
1229 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1231 audit_log_format(ab, "login pid=%d uid=%u "
1232 "old auid=%u new auid=%u",
1233 task->pid, task->uid,
1234 task->audit_context->loginuid, loginuid);
1237 task->audit_context->loginuid = loginuid;
1243 * audit_get_loginuid - get the loginuid for an audit_context
1244 * @ctx: the audit_context
1246 * Returns the context's loginuid or -1 if @ctx is NULL.
1248 uid_t audit_get_loginuid(struct audit_context *ctx)
1250 return ctx ? ctx->loginuid : -1;
1254 * __audit_mq_open - record audit data for a POSIX MQ open
1257 * @u_attr: queue attributes
1259 * Returns 0 for success or NULL context or < 0 on error.
1261 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1263 struct audit_aux_data_mq_open *ax;
1264 struct audit_context *context = current->audit_context;
1269 if (likely(!context))
1272 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1276 if (u_attr != NULL) {
1277 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1282 memset(&ax->attr, 0, sizeof(ax->attr));
1287 ax->d.type = AUDIT_MQ_OPEN;
1288 ax->d.next = context->aux;
1289 context->aux = (void *)ax;
1294 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1295 * @mqdes: MQ descriptor
1296 * @msg_len: Message length
1297 * @msg_prio: Message priority
1298 * @abs_timeout: Message timeout in absolute time
1300 * Returns 0 for success or NULL context or < 0 on error.
1302 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1303 const struct timespec __user *u_abs_timeout)
1305 struct audit_aux_data_mq_sendrecv *ax;
1306 struct audit_context *context = current->audit_context;
1311 if (likely(!context))
1314 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1318 if (u_abs_timeout != NULL) {
1319 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1324 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1327 ax->msg_len = msg_len;
1328 ax->msg_prio = msg_prio;
1330 ax->d.type = AUDIT_MQ_SENDRECV;
1331 ax->d.next = context->aux;
1332 context->aux = (void *)ax;
1337 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1338 * @mqdes: MQ descriptor
1339 * @msg_len: Message length
1340 * @msg_prio: Message priority
1341 * @abs_timeout: Message timeout in absolute time
1343 * Returns 0 for success or NULL context or < 0 on error.
1345 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1346 unsigned int __user *u_msg_prio,
1347 const struct timespec __user *u_abs_timeout)
1349 struct audit_aux_data_mq_sendrecv *ax;
1350 struct audit_context *context = current->audit_context;
1355 if (likely(!context))
1358 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1362 if (u_msg_prio != NULL) {
1363 if (get_user(ax->msg_prio, u_msg_prio)) {
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;
1381 ax->d.type = AUDIT_MQ_SENDRECV;
1382 ax->d.next = context->aux;
1383 context->aux = (void *)ax;
1388 * __audit_mq_notify - record audit data for a POSIX MQ notify
1389 * @mqdes: MQ descriptor
1390 * @u_notification: Notification event
1392 * Returns 0 for success or NULL context or < 0 on error.
1395 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1397 struct audit_aux_data_mq_notify *ax;
1398 struct audit_context *context = current->audit_context;
1403 if (likely(!context))
1406 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1410 if (u_notification != NULL) {
1411 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1416 memset(&ax->notification, 0, sizeof(ax->notification));
1420 ax->d.type = AUDIT_MQ_NOTIFY;
1421 ax->d.next = context->aux;
1422 context->aux = (void *)ax;
1427 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1428 * @mqdes: MQ descriptor
1431 * Returns 0 for success or NULL context or < 0 on error.
1433 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1435 struct audit_aux_data_mq_getsetattr *ax;
1436 struct audit_context *context = current->audit_context;
1441 if (likely(!context))
1444 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1449 ax->mqstat = *mqstat;
1451 ax->d.type = AUDIT_MQ_GETSETATTR;
1452 ax->d.next = context->aux;
1453 context->aux = (void *)ax;
1458 * audit_ipc_obj - record audit data for ipc object
1459 * @ipcp: ipc permissions
1461 * Returns 0 for success or NULL context or < 0 on error.
1463 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
1465 struct audit_aux_data_ipcctl *ax;
1466 struct audit_context *context = current->audit_context;
1468 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1472 ax->uid = ipcp->uid;
1473 ax->gid = ipcp->gid;
1474 ax->mode = ipcp->mode;
1475 selinux_get_ipc_sid(ipcp, &ax->osid);
1477 ax->d.type = AUDIT_IPC;
1478 ax->d.next = context->aux;
1479 context->aux = (void *)ax;
1484 * audit_ipc_set_perm - record audit data for new ipc permissions
1485 * @qbytes: msgq bytes
1486 * @uid: msgq user id
1487 * @gid: msgq group id
1488 * @mode: msgq mode (permissions)
1490 * Returns 0 for success or NULL context or < 0 on error.
1492 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1494 struct audit_aux_data_ipcctl *ax;
1495 struct audit_context *context = current->audit_context;
1497 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1501 ax->qbytes = qbytes;
1506 ax->d.type = AUDIT_IPC_SET_PERM;
1507 ax->d.next = context->aux;
1508 context->aux = (void *)ax;
1512 int audit_bprm(struct linux_binprm *bprm)
1514 struct audit_aux_data_execve *ax;
1515 struct audit_context *context = current->audit_context;
1516 unsigned long p, next;
1519 if (likely(!audit_enabled || !context))
1522 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1527 ax->argc = bprm->argc;
1528 ax->envc = bprm->envc;
1529 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1530 struct page *page = bprm->page[p / PAGE_SIZE];
1531 void *kaddr = kmap(page);
1532 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1533 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1538 ax->d.type = AUDIT_EXECVE;
1539 ax->d.next = context->aux;
1540 context->aux = (void *)ax;
1546 * audit_socketcall - record audit data for sys_socketcall
1547 * @nargs: number of args
1550 * Returns 0 for success or NULL context or < 0 on error.
1552 int audit_socketcall(int nargs, unsigned long *args)
1554 struct audit_aux_data_socketcall *ax;
1555 struct audit_context *context = current->audit_context;
1557 if (likely(!context))
1560 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1565 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1567 ax->d.type = AUDIT_SOCKETCALL;
1568 ax->d.next = context->aux;
1569 context->aux = (void *)ax;
1574 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1575 * @len: data length in user space
1576 * @a: data address in kernel space
1578 * Returns 0 for success or NULL context or < 0 on error.
1580 int audit_sockaddr(int len, void *a)
1582 struct audit_aux_data_sockaddr *ax;
1583 struct audit_context *context = current->audit_context;
1585 if (likely(!context))
1588 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1593 memcpy(ax->a, a, len);
1595 ax->d.type = AUDIT_SOCKADDR;
1596 ax->d.next = context->aux;
1597 context->aux = (void *)ax;
1602 * audit_avc_path - record the granting or denial of permissions
1603 * @dentry: dentry to record
1604 * @mnt: mnt to record
1606 * Returns 0 for success or NULL context or < 0 on error.
1608 * Called from security/selinux/avc.c::avc_audit()
1610 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1612 struct audit_aux_data_path *ax;
1613 struct audit_context *context = current->audit_context;
1615 if (likely(!context))
1618 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1622 ax->dentry = dget(dentry);
1623 ax->mnt = mntget(mnt);
1625 ax->d.type = AUDIT_AVC_PATH;
1626 ax->d.next = context->aux;
1627 context->aux = (void *)ax;
1632 * audit_signal_info - record signal info for shutting down audit subsystem
1633 * @sig: signal value
1634 * @t: task being signaled
1636 * If the audit subsystem is being terminated, record the task (pid)
1637 * and uid that is doing that.
1639 void __audit_signal_info(int sig, struct task_struct *t)
1641 extern pid_t audit_sig_pid;
1642 extern uid_t audit_sig_uid;
1643 extern u32 audit_sig_sid;
1645 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1646 struct task_struct *tsk = current;
1647 struct audit_context *ctx = tsk->audit_context;
1648 audit_sig_pid = tsk->pid;
1650 audit_sig_uid = ctx->loginuid;
1652 audit_sig_uid = tsk->uid;
1653 selinux_get_task_sid(tsk, &audit_sig_sid);