1 /* auditsc.c -- System-call auditing support
2 * Handles all system-call specific auditing features.
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5 * Copyright 2005 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2005 IBM Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
32 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
35 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
38 * Subject and object context labeling support added by <danjones@us.ibm.com>
39 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
42 #include <linux/init.h>
43 #include <asm/types.h>
44 #include <asm/atomic.h>
45 #include <asm/types.h>
47 #include <linux/namei.h>
49 #include <linux/module.h>
50 #include <linux/mount.h>
51 #include <linux/socket.h>
52 #include <linux/audit.h>
53 #include <linux/personality.h>
54 #include <linux/time.h>
55 #include <linux/netlink.h>
56 #include <linux/compiler.h>
57 #include <asm/unistd.h>
58 #include <linux/security.h>
59 #include <linux/list.h>
60 #include <linux/tty.h>
61 #include <linux/selinux.h>
62 #include <linux/binfmts.h>
63 #include <linux/syscalls.h>
67 extern struct list_head audit_filter_list[];
69 /* No syscall auditing will take place unless audit_enabled != 0. */
70 extern int audit_enabled;
72 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
73 * for saving names from getname(). */
74 #define AUDIT_NAMES 20
76 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
77 * audit_context from being used for nameless inodes from
79 #define AUDIT_NAMES_RESERVED 7
81 /* When fs/namei.c:getname() is called, we store the pointer in name and
82 * we don't let putname() free it (instead we free all of the saved
83 * pointers at syscall exit time).
85 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
98 struct audit_aux_data {
99 struct audit_aux_data *next;
103 #define AUDIT_AUX_IPCPERM 0
105 struct audit_aux_data_ipcctl {
106 struct audit_aux_data d;
108 unsigned long qbytes;
115 struct audit_aux_data_execve {
116 struct audit_aux_data d;
122 struct audit_aux_data_socketcall {
123 struct audit_aux_data d;
125 unsigned long args[0];
128 struct audit_aux_data_sockaddr {
129 struct audit_aux_data d;
134 struct audit_aux_data_path {
135 struct audit_aux_data d;
136 struct dentry *dentry;
137 struct vfsmount *mnt;
140 /* The per-task audit context. */
141 struct audit_context {
142 int in_syscall; /* 1 if task is in a syscall */
143 enum audit_state state;
144 unsigned int serial; /* serial number for record */
145 struct timespec ctime; /* time of syscall entry */
146 uid_t loginuid; /* login uid (identity) */
147 int major; /* syscall number */
148 unsigned long argv[4]; /* syscall arguments */
149 int return_valid; /* return code is valid */
150 long return_code;/* syscall return code */
151 int auditable; /* 1 if record should be written */
153 struct audit_names names[AUDIT_NAMES];
155 struct vfsmount * pwdmnt;
156 struct audit_context *previous; /* For nested syscalls */
157 struct audit_aux_data *aux;
159 /* Save things to print about task_struct */
161 uid_t uid, euid, suid, fsuid;
162 gid_t gid, egid, sgid, fsgid;
163 unsigned long personality;
173 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
175 static int audit_filter_rules(struct task_struct *tsk,
176 struct audit_krule *rule,
177 struct audit_context *ctx,
178 enum audit_state *state)
180 int i, j, need_sid = 1;
183 for (i = 0; i < rule->field_count; i++) {
184 struct audit_field *f = &rule->fields[i];
189 result = audit_comparator(tsk->pid, f->op, f->val);
192 result = audit_comparator(tsk->uid, f->op, f->val);
195 result = audit_comparator(tsk->euid, f->op, f->val);
198 result = audit_comparator(tsk->suid, f->op, f->val);
201 result = audit_comparator(tsk->fsuid, f->op, f->val);
204 result = audit_comparator(tsk->gid, f->op, f->val);
207 result = audit_comparator(tsk->egid, f->op, f->val);
210 result = audit_comparator(tsk->sgid, f->op, f->val);
213 result = audit_comparator(tsk->fsgid, f->op, f->val);
216 result = audit_comparator(tsk->personality, f->op, f->val);
220 result = audit_comparator(ctx->arch, f->op, f->val);
224 if (ctx && ctx->return_valid)
225 result = audit_comparator(ctx->return_code, f->op, f->val);
228 if (ctx && ctx->return_valid) {
230 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
232 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
237 for (j = 0; j < ctx->name_count; j++) {
238 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
247 for (j = 0; j < ctx->name_count; j++) {
248 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
257 for (j = 0; j < ctx->name_count; j++) {
258 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
259 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
269 result = audit_comparator(ctx->loginuid, f->op, f->val);
276 /* NOTE: this may return negative values indicating
277 a temporary error. We simply treat this as a
278 match for now to avoid losing information that
279 may be wanted. An error message will also be
283 selinux_task_ctxid(tsk, &sid);
286 result = selinux_audit_rule_match(sid, f->type,
297 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
304 switch (rule->action) {
305 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
306 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
307 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
312 /* At process creation time, we can determine if system-call auditing is
313 * completely disabled for this task. Since we only have the task
314 * structure at this point, we can only check uid and gid.
316 static enum audit_state audit_filter_task(struct task_struct *tsk)
318 struct audit_entry *e;
319 enum audit_state state;
322 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
323 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
329 return AUDIT_BUILD_CONTEXT;
332 /* At syscall entry and exit time, this filter is called if the
333 * audit_state is not low enough that auditing cannot take place, but is
334 * also not high enough that we already know we have to write an audit
335 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
337 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
338 struct audit_context *ctx,
339 struct list_head *list)
341 struct audit_entry *e;
342 enum audit_state state;
344 if (audit_pid && tsk->tgid == audit_pid)
345 return AUDIT_DISABLED;
348 if (!list_empty(list)) {
349 int word = AUDIT_WORD(ctx->major);
350 int bit = AUDIT_BIT(ctx->major);
352 list_for_each_entry_rcu(e, list, list) {
353 if ((e->rule.mask[word] & bit) == bit
354 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
361 return AUDIT_BUILD_CONTEXT;
364 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
368 struct audit_context *context = tsk->audit_context;
370 if (likely(!context))
372 context->return_valid = return_valid;
373 context->return_code = return_code;
375 if (context->in_syscall && !context->auditable) {
376 enum audit_state state;
377 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
378 if (state == AUDIT_RECORD_CONTEXT)
379 context->auditable = 1;
382 context->pid = tsk->pid;
383 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
384 context->uid = tsk->uid;
385 context->gid = tsk->gid;
386 context->euid = tsk->euid;
387 context->suid = tsk->suid;
388 context->fsuid = tsk->fsuid;
389 context->egid = tsk->egid;
390 context->sgid = tsk->sgid;
391 context->fsgid = tsk->fsgid;
392 context->personality = tsk->personality;
393 tsk->audit_context = NULL;
397 static inline void audit_free_names(struct audit_context *context)
402 if (context->auditable
403 ||context->put_count + context->ino_count != context->name_count) {
404 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
405 " name_count=%d put_count=%d"
406 " ino_count=%d [NOT freeing]\n",
408 context->serial, context->major, context->in_syscall,
409 context->name_count, context->put_count,
411 for (i = 0; i < context->name_count; i++) {
412 printk(KERN_ERR "names[%d] = %p = %s\n", i,
413 context->names[i].name,
414 context->names[i].name ?: "(null)");
421 context->put_count = 0;
422 context->ino_count = 0;
425 for (i = 0; i < context->name_count; i++) {
426 if (context->names[i].name)
427 __putname(context->names[i].name);
429 context->name_count = 0;
433 mntput(context->pwdmnt);
435 context->pwdmnt = NULL;
438 static inline void audit_free_aux(struct audit_context *context)
440 struct audit_aux_data *aux;
442 while ((aux = context->aux)) {
443 if (aux->type == AUDIT_AVC_PATH) {
444 struct audit_aux_data_path *axi = (void *)aux;
449 context->aux = aux->next;
454 static inline void audit_zero_context(struct audit_context *context,
455 enum audit_state state)
457 uid_t loginuid = context->loginuid;
459 memset(context, 0, sizeof(*context));
460 context->state = state;
461 context->loginuid = loginuid;
464 static inline struct audit_context *audit_alloc_context(enum audit_state state)
466 struct audit_context *context;
468 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
470 audit_zero_context(context, state);
475 * audit_alloc - allocate an audit context block for a task
478 * Filter on the task information and allocate a per-task audit context
479 * if necessary. Doing so turns on system call auditing for the
480 * specified task. This is called from copy_process, so no lock is
483 int audit_alloc(struct task_struct *tsk)
485 struct audit_context *context;
486 enum audit_state state;
488 if (likely(!audit_enabled))
489 return 0; /* Return if not auditing. */
491 state = audit_filter_task(tsk);
492 if (likely(state == AUDIT_DISABLED))
495 if (!(context = audit_alloc_context(state))) {
496 audit_log_lost("out of memory in audit_alloc");
500 /* Preserve login uid */
501 context->loginuid = -1;
502 if (current->audit_context)
503 context->loginuid = current->audit_context->loginuid;
505 tsk->audit_context = context;
506 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
510 static inline void audit_free_context(struct audit_context *context)
512 struct audit_context *previous;
516 previous = context->previous;
517 if (previous || (count && count < 10)) {
519 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
520 " freeing multiple contexts (%d)\n",
521 context->serial, context->major,
522 context->name_count, count);
524 audit_free_names(context);
525 audit_free_aux(context);
530 printk(KERN_ERR "audit: freed %d contexts\n", count);
533 static void audit_log_task_context(struct audit_buffer *ab)
538 len = security_getprocattr(current, "current", NULL, 0);
545 ctx = kmalloc(len, GFP_KERNEL);
549 len = security_getprocattr(current, "current", ctx, len);
553 audit_log_format(ab, " subj=%s", ctx);
559 audit_panic("error in audit_log_task_context");
563 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
565 char name[sizeof(tsk->comm)];
566 struct mm_struct *mm = tsk->mm;
567 struct vm_area_struct *vma;
571 get_task_comm(name, tsk);
572 audit_log_format(ab, " comm=");
573 audit_log_untrustedstring(ab, name);
576 down_read(&mm->mmap_sem);
579 if ((vma->vm_flags & VM_EXECUTABLE) &&
581 audit_log_d_path(ab, "exe=",
582 vma->vm_file->f_dentry,
583 vma->vm_file->f_vfsmnt);
588 up_read(&mm->mmap_sem);
590 audit_log_task_context(ab);
593 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
595 int i, call_panic = 0;
596 struct audit_buffer *ab;
597 struct audit_aux_data *aux;
602 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
604 return; /* audit_panic has been called */
605 audit_log_format(ab, "arch=%x syscall=%d",
606 context->arch, context->major);
607 if (context->personality != PER_LINUX)
608 audit_log_format(ab, " per=%lx", context->personality);
609 if (context->return_valid)
610 audit_log_format(ab, " success=%s exit=%ld",
611 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
612 context->return_code);
613 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
614 tty = tsk->signal->tty->name;
618 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
619 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
620 " euid=%u suid=%u fsuid=%u"
621 " egid=%u sgid=%u fsgid=%u tty=%s",
632 context->euid, context->suid, context->fsuid,
633 context->egid, context->sgid, context->fsgid, tty);
634 audit_log_task_info(ab, tsk);
637 for (aux = context->aux; aux; aux = aux->next) {
639 ab = audit_log_start(context, GFP_KERNEL, aux->type);
641 continue; /* audit_panic has been called */
645 struct audit_aux_data_ipcctl *axi = (void *)aux;
647 " qbytes=%lx iuid=%u igid=%u mode=%x",
648 axi->qbytes, axi->uid, axi->gid, axi->mode);
649 if (axi->osid != 0) {
652 if (selinux_ctxid_to_string(
653 axi->osid, &ctx, &len)) {
654 audit_log_format(ab, " osid=%u",
658 audit_log_format(ab, " obj=%s", ctx);
663 case AUDIT_IPC_SET_PERM: {
664 struct audit_aux_data_ipcctl *axi = (void *)aux;
666 " new qbytes=%lx new iuid=%u new igid=%u new mode=%x",
667 axi->qbytes, axi->uid, axi->gid, axi->mode);
668 if (axi->osid != 0) {
671 if (selinux_ctxid_to_string(
672 axi->osid, &ctx, &len)) {
673 audit_log_format(ab, " osid=%u",
677 audit_log_format(ab, " obj=%s", ctx);
682 struct audit_aux_data_execve *axi = (void *)aux;
685 for (i = 0, p = axi->mem; i < axi->argc; i++) {
686 audit_log_format(ab, "a%d=", i);
687 p = audit_log_untrustedstring(ab, p);
688 audit_log_format(ab, "\n");
692 case AUDIT_SOCKETCALL: {
694 struct audit_aux_data_socketcall *axs = (void *)aux;
695 audit_log_format(ab, "nargs=%d", axs->nargs);
696 for (i=0; i<axs->nargs; i++)
697 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
700 case AUDIT_SOCKADDR: {
701 struct audit_aux_data_sockaddr *axs = (void *)aux;
703 audit_log_format(ab, "saddr=");
704 audit_log_hex(ab, axs->a, axs->len);
707 case AUDIT_AVC_PATH: {
708 struct audit_aux_data_path *axi = (void *)aux;
709 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
716 if (context->pwd && context->pwdmnt) {
717 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
719 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
723 for (i = 0; i < context->name_count; i++) {
724 unsigned long ino = context->names[i].ino;
725 unsigned long pino = context->names[i].pino;
727 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
729 continue; /* audit_panic has been called */
731 audit_log_format(ab, "item=%d", i);
733 audit_log_format(ab, " name=");
734 if (context->names[i].name)
735 audit_log_untrustedstring(ab, context->names[i].name);
737 audit_log_format(ab, "(null)");
739 if (pino != (unsigned long)-1)
740 audit_log_format(ab, " parent=%lu", pino);
741 if (ino != (unsigned long)-1)
742 audit_log_format(ab, " inode=%lu", ino);
743 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
744 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
745 " ouid=%u ogid=%u rdev=%02x:%02x",
746 MAJOR(context->names[i].dev),
747 MINOR(context->names[i].dev),
748 context->names[i].mode,
749 context->names[i].uid,
750 context->names[i].gid,
751 MAJOR(context->names[i].rdev),
752 MINOR(context->names[i].rdev));
753 if (context->names[i].osid != 0) {
756 if (selinux_ctxid_to_string(
757 context->names[i].osid, &ctx, &len)) {
758 audit_log_format(ab, " osid=%u",
759 context->names[i].osid);
762 audit_log_format(ab, " obj=%s", ctx);
769 audit_panic("error converting sid to string");
773 * audit_free - free a per-task audit context
774 * @tsk: task whose audit context block to free
776 * Called from copy_process and do_exit
778 void audit_free(struct task_struct *tsk)
780 struct audit_context *context;
782 context = audit_get_context(tsk, 0, 0);
783 if (likely(!context))
786 /* Check for system calls that do not go through the exit
787 * function (e.g., exit_group), then free context block.
788 * We use GFP_ATOMIC here because we might be doing this
789 * in the context of the idle thread */
790 /* that can happen only if we are called from do_exit() */
791 if (context->in_syscall && context->auditable)
792 audit_log_exit(context, tsk);
794 audit_free_context(context);
798 * audit_syscall_entry - fill in an audit record at syscall entry
799 * @tsk: task being audited
800 * @arch: architecture type
801 * @major: major syscall type (function)
802 * @a1: additional syscall register 1
803 * @a2: additional syscall register 2
804 * @a3: additional syscall register 3
805 * @a4: additional syscall register 4
807 * Fill in audit context at syscall entry. This only happens if the
808 * audit context was created when the task was created and the state or
809 * filters demand the audit context be built. If the state from the
810 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
811 * then the record will be written at syscall exit time (otherwise, it
812 * will only be written if another part of the kernel requests that it
815 void audit_syscall_entry(int arch, int major,
816 unsigned long a1, unsigned long a2,
817 unsigned long a3, unsigned long a4)
819 struct task_struct *tsk = current;
820 struct audit_context *context = tsk->audit_context;
821 enum audit_state state;
826 * This happens only on certain architectures that make system
827 * calls in kernel_thread via the entry.S interface, instead of
828 * with direct calls. (If you are porting to a new
829 * architecture, hitting this condition can indicate that you
830 * got the _exit/_leave calls backward in entry.S.)
834 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
836 * This also happens with vm86 emulation in a non-nested manner
837 * (entries without exits), so this case must be caught.
839 if (context->in_syscall) {
840 struct audit_context *newctx;
844 "audit(:%d) pid=%d in syscall=%d;"
845 " entering syscall=%d\n",
846 context->serial, tsk->pid, context->major, major);
848 newctx = audit_alloc_context(context->state);
850 newctx->previous = context;
852 tsk->audit_context = newctx;
854 /* If we can't alloc a new context, the best we
855 * can do is to leak memory (any pending putname
856 * will be lost). The only other alternative is
857 * to abandon auditing. */
858 audit_zero_context(context, context->state);
861 BUG_ON(context->in_syscall || context->name_count);
866 context->arch = arch;
867 context->major = major;
868 context->argv[0] = a1;
869 context->argv[1] = a2;
870 context->argv[2] = a3;
871 context->argv[3] = a4;
873 state = context->state;
874 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
875 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
876 if (likely(state == AUDIT_DISABLED))
880 context->ctime = CURRENT_TIME;
881 context->in_syscall = 1;
882 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
886 * audit_syscall_exit - deallocate audit context after a system call
887 * @tsk: task being audited
888 * @valid: success/failure flag
889 * @return_code: syscall return value
891 * Tear down after system call. If the audit context has been marked as
892 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
893 * filtering, or because some other part of the kernel write an audit
894 * message), then write out the syscall information. In call cases,
895 * free the names stored from getname().
897 void audit_syscall_exit(int valid, long return_code)
899 struct task_struct *tsk = current;
900 struct audit_context *context;
902 context = audit_get_context(tsk, valid, return_code);
904 if (likely(!context))
907 if (context->in_syscall && context->auditable)
908 audit_log_exit(context, tsk);
910 context->in_syscall = 0;
911 context->auditable = 0;
913 if (context->previous) {
914 struct audit_context *new_context = context->previous;
915 context->previous = NULL;
916 audit_free_context(context);
917 tsk->audit_context = new_context;
919 audit_free_names(context);
920 audit_free_aux(context);
921 tsk->audit_context = context;
926 * audit_getname - add a name to the list
929 * Add a name to the list of audit names for this context.
930 * Called from fs/namei.c:getname().
932 void audit_getname(const char *name)
934 struct audit_context *context = current->audit_context;
936 if (!context || IS_ERR(name) || !name)
939 if (!context->in_syscall) {
941 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
942 __FILE__, __LINE__, context->serial, name);
947 BUG_ON(context->name_count >= AUDIT_NAMES);
948 context->names[context->name_count].name = name;
949 context->names[context->name_count].ino = (unsigned long)-1;
950 ++context->name_count;
952 read_lock(¤t->fs->lock);
953 context->pwd = dget(current->fs->pwd);
954 context->pwdmnt = mntget(current->fs->pwdmnt);
955 read_unlock(¤t->fs->lock);
960 /* audit_putname - intercept a putname request
961 * @name: name to intercept and delay for putname
963 * If we have stored the name from getname in the audit context,
964 * then we delay the putname until syscall exit.
965 * Called from include/linux/fs.h:putname().
967 void audit_putname(const char *name)
969 struct audit_context *context = current->audit_context;
972 if (!context->in_syscall) {
974 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
975 __FILE__, __LINE__, context->serial, name);
976 if (context->name_count) {
978 for (i = 0; i < context->name_count; i++)
979 printk(KERN_ERR "name[%d] = %p = %s\n", i,
980 context->names[i].name,
981 context->names[i].name ?: "(null)");
988 ++context->put_count;
989 if (context->put_count > context->name_count) {
990 printk(KERN_ERR "%s:%d(:%d): major=%d"
991 " in_syscall=%d putname(%p) name_count=%d"
994 context->serial, context->major,
995 context->in_syscall, name, context->name_count,
1003 static void audit_inode_context(int idx, const struct inode *inode)
1005 struct audit_context *context = current->audit_context;
1007 selinux_get_inode_sid(inode, &context->names[idx].osid);
1012 * audit_inode - store the inode and device from a lookup
1013 * @name: name being audited
1014 * @inode: inode being audited
1015 * @flags: lookup flags (as used in path_lookup())
1017 * Called from fs/namei.c:path_lookup().
1019 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1022 struct audit_context *context = current->audit_context;
1024 if (!context->in_syscall)
1026 if (context->name_count
1027 && context->names[context->name_count-1].name
1028 && context->names[context->name_count-1].name == name)
1029 idx = context->name_count - 1;
1030 else if (context->name_count > 1
1031 && context->names[context->name_count-2].name
1032 && context->names[context->name_count-2].name == name)
1033 idx = context->name_count - 2;
1035 /* FIXME: how much do we care about inodes that have no
1036 * associated name? */
1037 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1039 idx = context->name_count++;
1040 context->names[idx].name = NULL;
1042 ++context->ino_count;
1045 context->names[idx].dev = inode->i_sb->s_dev;
1046 context->names[idx].mode = inode->i_mode;
1047 context->names[idx].uid = inode->i_uid;
1048 context->names[idx].gid = inode->i_gid;
1049 context->names[idx].rdev = inode->i_rdev;
1050 audit_inode_context(idx, inode);
1051 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1052 (strcmp(name, ".") != 0)) {
1053 context->names[idx].ino = (unsigned long)-1;
1054 context->names[idx].pino = inode->i_ino;
1056 context->names[idx].ino = inode->i_ino;
1057 context->names[idx].pino = (unsigned long)-1;
1062 * audit_inode_child - collect inode info for created/removed objects
1063 * @dname: inode's dentry name
1064 * @inode: inode being audited
1065 * @pino: inode number of dentry parent
1067 * For syscalls that create or remove filesystem objects, audit_inode
1068 * can only collect information for the filesystem object's parent.
1069 * This call updates the audit context with the child's information.
1070 * Syscalls that create a new filesystem object must be hooked after
1071 * the object is created. Syscalls that remove a filesystem object
1072 * must be hooked prior, in order to capture the target inode during
1073 * unsuccessful attempts.
1075 void __audit_inode_child(const char *dname, const struct inode *inode,
1079 struct audit_context *context = current->audit_context;
1081 if (!context->in_syscall)
1084 /* determine matching parent */
1086 for (idx = 0; idx < context->name_count; idx++)
1087 if (context->names[idx].pino == pino) {
1089 const char *name = context->names[idx].name;
1090 int dlen = strlen(dname);
1091 int nlen = name ? strlen(name) : 0;
1096 /* disregard trailing slashes */
1097 n = name + nlen - 1;
1098 while ((*n == '/') && (n > name))
1101 /* find last path component */
1105 else if (n > name) {
1112 if (strncmp(n, dname, dlen) == 0)
1113 goto update_context;
1116 /* catch-all in case match not found */
1117 idx = context->name_count++;
1118 context->names[idx].name = NULL;
1119 context->names[idx].pino = pino;
1121 context->ino_count++;
1126 context->names[idx].ino = inode->i_ino;
1127 context->names[idx].dev = inode->i_sb->s_dev;
1128 context->names[idx].mode = inode->i_mode;
1129 context->names[idx].uid = inode->i_uid;
1130 context->names[idx].gid = inode->i_gid;
1131 context->names[idx].rdev = inode->i_rdev;
1132 audit_inode_context(idx, inode);
1137 * auditsc_get_stamp - get local copies of audit_context values
1138 * @ctx: audit_context for the task
1139 * @t: timespec to store time recorded in the audit_context
1140 * @serial: serial value that is recorded in the audit_context
1142 * Also sets the context as auditable.
1144 void auditsc_get_stamp(struct audit_context *ctx,
1145 struct timespec *t, unsigned int *serial)
1148 ctx->serial = audit_serial();
1149 t->tv_sec = ctx->ctime.tv_sec;
1150 t->tv_nsec = ctx->ctime.tv_nsec;
1151 *serial = ctx->serial;
1156 * audit_set_loginuid - set a task's audit_context loginuid
1157 * @task: task whose audit context is being modified
1158 * @loginuid: loginuid value
1162 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1164 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1166 if (task->audit_context) {
1167 struct audit_buffer *ab;
1169 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1171 audit_log_format(ab, "login pid=%d uid=%u "
1172 "old auid=%u new auid=%u",
1173 task->pid, task->uid,
1174 task->audit_context->loginuid, loginuid);
1177 task->audit_context->loginuid = loginuid;
1183 * audit_get_loginuid - get the loginuid for an audit_context
1184 * @ctx: the audit_context
1186 * Returns the context's loginuid or -1 if @ctx is NULL.
1188 uid_t audit_get_loginuid(struct audit_context *ctx)
1190 return ctx ? ctx->loginuid : -1;
1194 * audit_ipc_obj - record audit data for ipc object
1195 * @ipcp: ipc permissions
1197 * Returns 0 for success or NULL context or < 0 on error.
1199 int audit_ipc_obj(struct kern_ipc_perm *ipcp)
1201 struct audit_aux_data_ipcctl *ax;
1202 struct audit_context *context = current->audit_context;
1204 if (likely(!context))
1207 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1211 ax->uid = ipcp->uid;
1212 ax->gid = ipcp->gid;
1213 ax->mode = ipcp->mode;
1214 selinux_get_ipc_sid(ipcp, &ax->osid);
1216 ax->d.type = AUDIT_IPC;
1217 ax->d.next = context->aux;
1218 context->aux = (void *)ax;
1223 * audit_ipc_set_perm - record audit data for new ipc permissions
1224 * @qbytes: msgq bytes
1225 * @uid: msgq user id
1226 * @gid: msgq group id
1227 * @mode: msgq mode (permissions)
1229 * Returns 0 for success or NULL context or < 0 on error.
1231 int audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
1233 struct audit_aux_data_ipcctl *ax;
1234 struct audit_context *context = current->audit_context;
1236 if (likely(!context))
1239 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1243 ax->qbytes = qbytes;
1247 selinux_get_ipc_sid(ipcp, &ax->osid);
1249 ax->d.type = AUDIT_IPC_SET_PERM;
1250 ax->d.next = context->aux;
1251 context->aux = (void *)ax;
1255 int audit_bprm(struct linux_binprm *bprm)
1257 struct audit_aux_data_execve *ax;
1258 struct audit_context *context = current->audit_context;
1259 unsigned long p, next;
1262 if (likely(!audit_enabled || !context))
1265 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1270 ax->argc = bprm->argc;
1271 ax->envc = bprm->envc;
1272 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1273 struct page *page = bprm->page[p / PAGE_SIZE];
1274 void *kaddr = kmap(page);
1275 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1276 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1281 ax->d.type = AUDIT_EXECVE;
1282 ax->d.next = context->aux;
1283 context->aux = (void *)ax;
1289 * audit_socketcall - record audit data for sys_socketcall
1290 * @nargs: number of args
1293 * Returns 0 for success or NULL context or < 0 on error.
1295 int audit_socketcall(int nargs, unsigned long *args)
1297 struct audit_aux_data_socketcall *ax;
1298 struct audit_context *context = current->audit_context;
1300 if (likely(!context))
1303 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1308 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1310 ax->d.type = AUDIT_SOCKETCALL;
1311 ax->d.next = context->aux;
1312 context->aux = (void *)ax;
1317 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1318 * @len: data length in user space
1319 * @a: data address in kernel space
1321 * Returns 0 for success or NULL context or < 0 on error.
1323 int audit_sockaddr(int len, void *a)
1325 struct audit_aux_data_sockaddr *ax;
1326 struct audit_context *context = current->audit_context;
1328 if (likely(!context))
1331 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1336 memcpy(ax->a, a, len);
1338 ax->d.type = AUDIT_SOCKADDR;
1339 ax->d.next = context->aux;
1340 context->aux = (void *)ax;
1345 * audit_avc_path - record the granting or denial of permissions
1346 * @dentry: dentry to record
1347 * @mnt: mnt to record
1349 * Returns 0 for success or NULL context or < 0 on error.
1351 * Called from security/selinux/avc.c::avc_audit()
1353 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1355 struct audit_aux_data_path *ax;
1356 struct audit_context *context = current->audit_context;
1358 if (likely(!context))
1361 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1365 ax->dentry = dget(dentry);
1366 ax->mnt = mntget(mnt);
1368 ax->d.type = AUDIT_AVC_PATH;
1369 ax->d.next = context->aux;
1370 context->aux = (void *)ax;
1375 * audit_signal_info - record signal info for shutting down audit subsystem
1376 * @sig: signal value
1377 * @t: task being signaled
1379 * If the audit subsystem is being terminated, record the task (pid)
1380 * and uid that is doing that.
1382 void __audit_signal_info(int sig, struct task_struct *t)
1384 extern pid_t audit_sig_pid;
1385 extern uid_t audit_sig_uid;
1386 extern u32 audit_sig_sid;
1388 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1389 struct task_struct *tsk = current;
1390 struct audit_context *ctx = tsk->audit_context;
1391 audit_sig_pid = tsk->pid;
1393 audit_sig_uid = ctx->loginuid;
1395 audit_sig_uid = tsk->uid;
1396 selinux_get_task_sid(tsk, &audit_sig_sid);