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>
66 extern struct list_head audit_filter_list[];
68 /* No syscall auditing will take place unless audit_enabled != 0. */
69 extern int audit_enabled;
71 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
72 * for saving names from getname(). */
73 #define AUDIT_NAMES 20
75 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
76 * audit_context from being used for nameless inodes from
78 #define AUDIT_NAMES_RESERVED 7
80 /* When fs/namei.c:getname() is called, we store the pointer in name and
81 * we don't let putname() free it (instead we free all of the saved
82 * pointers at syscall exit time).
84 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
97 struct audit_aux_data {
98 struct audit_aux_data *next;
102 #define AUDIT_AUX_IPCPERM 0
104 struct audit_aux_data_ipcctl {
105 struct audit_aux_data d;
107 unsigned long qbytes;
114 struct audit_aux_data_execve {
115 struct audit_aux_data d;
121 struct audit_aux_data_socketcall {
122 struct audit_aux_data d;
124 unsigned long args[0];
127 struct audit_aux_data_sockaddr {
128 struct audit_aux_data d;
133 struct audit_aux_data_path {
134 struct audit_aux_data d;
135 struct dentry *dentry;
136 struct vfsmount *mnt;
139 /* The per-task audit context. */
140 struct audit_context {
141 int in_syscall; /* 1 if task is in a syscall */
142 enum audit_state state;
143 unsigned int serial; /* serial number for record */
144 struct timespec ctime; /* time of syscall entry */
145 uid_t loginuid; /* login uid (identity) */
146 int major; /* syscall number */
147 unsigned long argv[4]; /* syscall arguments */
148 int return_valid; /* return code is valid */
149 long return_code;/* syscall return code */
150 int auditable; /* 1 if record should be written */
152 struct audit_names names[AUDIT_NAMES];
154 struct vfsmount * pwdmnt;
155 struct audit_context *previous; /* For nested syscalls */
156 struct audit_aux_data *aux;
158 /* Save things to print about task_struct */
160 uid_t uid, euid, suid, fsuid;
161 gid_t gid, egid, sgid, fsgid;
162 unsigned long personality;
172 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
174 static int audit_filter_rules(struct task_struct *tsk,
175 struct audit_krule *rule,
176 struct audit_context *ctx,
177 enum audit_state *state)
179 int i, j, need_sid = 1;
182 for (i = 0; i < rule->field_count; i++) {
183 struct audit_field *f = &rule->fields[i];
188 result = audit_comparator(tsk->pid, f->op, f->val);
191 result = audit_comparator(tsk->uid, f->op, f->val);
194 result = audit_comparator(tsk->euid, f->op, f->val);
197 result = audit_comparator(tsk->suid, f->op, f->val);
200 result = audit_comparator(tsk->fsuid, f->op, f->val);
203 result = audit_comparator(tsk->gid, f->op, f->val);
206 result = audit_comparator(tsk->egid, f->op, f->val);
209 result = audit_comparator(tsk->sgid, f->op, f->val);
212 result = audit_comparator(tsk->fsgid, f->op, f->val);
215 result = audit_comparator(tsk->personality, f->op, f->val);
219 result = audit_comparator(ctx->arch, f->op, f->val);
223 if (ctx && ctx->return_valid)
224 result = audit_comparator(ctx->return_code, f->op, f->val);
227 if (ctx && ctx->return_valid) {
229 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
231 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
236 for (j = 0; j < ctx->name_count; j++) {
237 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
246 for (j = 0; j < ctx->name_count; j++) {
247 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
256 for (j = 0; j < ctx->name_count; j++) {
257 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
258 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
268 result = audit_comparator(ctx->loginuid, f->op, f->val);
275 /* NOTE: this may return negative values indicating
276 a temporary error. We simply treat this as a
277 match for now to avoid losing information that
278 may be wanted. An error message will also be
282 selinux_task_ctxid(tsk, &sid);
285 result = selinux_audit_rule_match(sid, f->type,
296 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
303 switch (rule->action) {
304 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
305 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
306 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
311 /* At process creation time, we can determine if system-call auditing is
312 * completely disabled for this task. Since we only have the task
313 * structure at this point, we can only check uid and gid.
315 static enum audit_state audit_filter_task(struct task_struct *tsk)
317 struct audit_entry *e;
318 enum audit_state state;
321 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
322 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
328 return AUDIT_BUILD_CONTEXT;
331 /* At syscall entry and exit time, this filter is called if the
332 * audit_state is not low enough that auditing cannot take place, but is
333 * also not high enough that we already know we have to write an audit
334 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
336 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
337 struct audit_context *ctx,
338 struct list_head *list)
340 struct audit_entry *e;
341 enum audit_state state;
343 if (audit_pid && tsk->tgid == audit_pid)
344 return AUDIT_DISABLED;
347 if (!list_empty(list)) {
348 int word = AUDIT_WORD(ctx->major);
349 int bit = AUDIT_BIT(ctx->major);
351 list_for_each_entry_rcu(e, list, list) {
352 if ((e->rule.mask[word] & bit) == bit
353 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
360 return AUDIT_BUILD_CONTEXT;
363 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
367 struct audit_context *context = tsk->audit_context;
369 if (likely(!context))
371 context->return_valid = return_valid;
372 context->return_code = return_code;
374 if (context->in_syscall && !context->auditable) {
375 enum audit_state state;
376 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
377 if (state == AUDIT_RECORD_CONTEXT)
378 context->auditable = 1;
381 context->pid = tsk->pid;
382 context->uid = tsk->uid;
383 context->gid = tsk->gid;
384 context->euid = tsk->euid;
385 context->suid = tsk->suid;
386 context->fsuid = tsk->fsuid;
387 context->egid = tsk->egid;
388 context->sgid = tsk->sgid;
389 context->fsgid = tsk->fsgid;
390 context->personality = tsk->personality;
391 tsk->audit_context = NULL;
395 static inline void audit_free_names(struct audit_context *context)
400 if (context->auditable
401 ||context->put_count + context->ino_count != context->name_count) {
402 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
403 " name_count=%d put_count=%d"
404 " ino_count=%d [NOT freeing]\n",
406 context->serial, context->major, context->in_syscall,
407 context->name_count, context->put_count,
409 for (i = 0; i < context->name_count; i++) {
410 printk(KERN_ERR "names[%d] = %p = %s\n", i,
411 context->names[i].name,
412 context->names[i].name ?: "(null)");
419 context->put_count = 0;
420 context->ino_count = 0;
423 for (i = 0; i < context->name_count; i++) {
424 if (context->names[i].name)
425 __putname(context->names[i].name);
427 context->name_count = 0;
431 mntput(context->pwdmnt);
433 context->pwdmnt = NULL;
436 static inline void audit_free_aux(struct audit_context *context)
438 struct audit_aux_data *aux;
440 while ((aux = context->aux)) {
441 if (aux->type == AUDIT_AVC_PATH) {
442 struct audit_aux_data_path *axi = (void *)aux;
447 context->aux = aux->next;
452 static inline void audit_zero_context(struct audit_context *context,
453 enum audit_state state)
455 uid_t loginuid = context->loginuid;
457 memset(context, 0, sizeof(*context));
458 context->state = state;
459 context->loginuid = loginuid;
462 static inline struct audit_context *audit_alloc_context(enum audit_state state)
464 struct audit_context *context;
466 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
468 audit_zero_context(context, state);
473 * audit_alloc - allocate an audit context block for a task
476 * Filter on the task information and allocate a per-task audit context
477 * if necessary. Doing so turns on system call auditing for the
478 * specified task. This is called from copy_process, so no lock is
481 int audit_alloc(struct task_struct *tsk)
483 struct audit_context *context;
484 enum audit_state state;
486 if (likely(!audit_enabled))
487 return 0; /* Return if not auditing. */
489 state = audit_filter_task(tsk);
490 if (likely(state == AUDIT_DISABLED))
493 if (!(context = audit_alloc_context(state))) {
494 audit_log_lost("out of memory in audit_alloc");
498 /* Preserve login uid */
499 context->loginuid = -1;
500 if (current->audit_context)
501 context->loginuid = current->audit_context->loginuid;
503 tsk->audit_context = context;
504 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
508 static inline void audit_free_context(struct audit_context *context)
510 struct audit_context *previous;
514 previous = context->previous;
515 if (previous || (count && count < 10)) {
517 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
518 " freeing multiple contexts (%d)\n",
519 context->serial, context->major,
520 context->name_count, count);
522 audit_free_names(context);
523 audit_free_aux(context);
528 printk(KERN_ERR "audit: freed %d contexts\n", count);
531 static void audit_log_task_context(struct audit_buffer *ab)
536 len = security_getprocattr(current, "current", NULL, 0);
543 ctx = kmalloc(len, GFP_KERNEL);
547 len = security_getprocattr(current, "current", ctx, len);
551 audit_log_format(ab, " subj=%s", ctx);
557 audit_panic("error in audit_log_task_context");
561 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
563 char name[sizeof(tsk->comm)];
564 struct mm_struct *mm = tsk->mm;
565 struct vm_area_struct *vma;
569 get_task_comm(name, tsk);
570 audit_log_format(ab, " comm=");
571 audit_log_untrustedstring(ab, name);
574 down_read(&mm->mmap_sem);
577 if ((vma->vm_flags & VM_EXECUTABLE) &&
579 audit_log_d_path(ab, "exe=",
580 vma->vm_file->f_dentry,
581 vma->vm_file->f_vfsmnt);
586 up_read(&mm->mmap_sem);
588 audit_log_task_context(ab);
591 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
593 int i, call_panic = 0;
594 struct audit_buffer *ab;
595 struct audit_aux_data *aux;
600 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
602 return; /* audit_panic has been called */
603 audit_log_format(ab, "arch=%x syscall=%d",
604 context->arch, context->major);
605 if (context->personality != PER_LINUX)
606 audit_log_format(ab, " per=%lx", context->personality);
607 if (context->return_valid)
608 audit_log_format(ab, " success=%s exit=%ld",
609 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
610 context->return_code);
611 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
612 tty = tsk->signal->tty->name;
616 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
617 " pid=%d auid=%u uid=%u gid=%u"
618 " euid=%u suid=%u fsuid=%u"
619 " egid=%u sgid=%u fsgid=%u tty=%s",
629 context->euid, context->suid, context->fsuid,
630 context->egid, context->sgid, context->fsgid, tty);
631 audit_log_task_info(ab, tsk);
634 for (aux = context->aux; aux; aux = aux->next) {
636 ab = audit_log_start(context, GFP_KERNEL, aux->type);
638 continue; /* audit_panic has been called */
642 struct audit_aux_data_ipcctl *axi = (void *)aux;
644 " qbytes=%lx iuid=%u igid=%u mode=%x",
645 axi->qbytes, axi->uid, axi->gid, axi->mode);
646 if (axi->osid != 0) {
649 if (selinux_ctxid_to_string(
650 axi->osid, &ctx, &len)) {
651 audit_log_format(ab, " osid=%u",
655 audit_log_format(ab, " obj=%s", ctx);
660 case AUDIT_IPC_SET_PERM: {
661 struct audit_aux_data_ipcctl *axi = (void *)aux;
663 " new qbytes=%lx new iuid=%u new igid=%u new mode=%x",
664 axi->qbytes, axi->uid, axi->gid, axi->mode);
665 if (axi->osid != 0) {
668 if (selinux_ctxid_to_string(
669 axi->osid, &ctx, &len)) {
670 audit_log_format(ab, " osid=%u",
674 audit_log_format(ab, " obj=%s", ctx);
679 struct audit_aux_data_execve *axi = (void *)aux;
682 for (i = 0, p = axi->mem; i < axi->argc; i++) {
683 audit_log_format(ab, "a%d=", i);
684 p = audit_log_untrustedstring(ab, p);
685 audit_log_format(ab, "\n");
689 case AUDIT_SOCKETCALL: {
691 struct audit_aux_data_socketcall *axs = (void *)aux;
692 audit_log_format(ab, "nargs=%d", axs->nargs);
693 for (i=0; i<axs->nargs; i++)
694 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
697 case AUDIT_SOCKADDR: {
698 struct audit_aux_data_sockaddr *axs = (void *)aux;
700 audit_log_format(ab, "saddr=");
701 audit_log_hex(ab, axs->a, axs->len);
704 case AUDIT_AVC_PATH: {
705 struct audit_aux_data_path *axi = (void *)aux;
706 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
713 if (context->pwd && context->pwdmnt) {
714 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
716 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
720 for (i = 0; i < context->name_count; i++) {
721 unsigned long ino = context->names[i].ino;
722 unsigned long pino = context->names[i].pino;
724 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
726 continue; /* audit_panic has been called */
728 audit_log_format(ab, "item=%d", i);
730 audit_log_format(ab, " name=");
731 if (context->names[i].name)
732 audit_log_untrustedstring(ab, context->names[i].name);
734 audit_log_format(ab, "(null)");
736 if (pino != (unsigned long)-1)
737 audit_log_format(ab, " parent=%lu", pino);
738 if (ino != (unsigned long)-1)
739 audit_log_format(ab, " inode=%lu", ino);
740 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
741 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
742 " ouid=%u ogid=%u rdev=%02x:%02x",
743 MAJOR(context->names[i].dev),
744 MINOR(context->names[i].dev),
745 context->names[i].mode,
746 context->names[i].uid,
747 context->names[i].gid,
748 MAJOR(context->names[i].rdev),
749 MINOR(context->names[i].rdev));
750 if (context->names[i].osid != 0) {
753 if (selinux_ctxid_to_string(
754 context->names[i].osid, &ctx, &len)) {
755 audit_log_format(ab, " osid=%u",
756 context->names[i].osid);
759 audit_log_format(ab, " obj=%s", ctx);
766 audit_panic("error converting sid to string");
770 * audit_free - free a per-task audit context
771 * @tsk: task whose audit context block to free
773 * Called from copy_process and do_exit
775 void audit_free(struct task_struct *tsk)
777 struct audit_context *context;
779 context = audit_get_context(tsk, 0, 0);
780 if (likely(!context))
783 /* Check for system calls that do not go through the exit
784 * function (e.g., exit_group), then free context block.
785 * We use GFP_ATOMIC here because we might be doing this
786 * in the context of the idle thread */
787 /* that can happen only if we are called from do_exit() */
788 if (context->in_syscall && context->auditable)
789 audit_log_exit(context, tsk);
791 audit_free_context(context);
795 * audit_syscall_entry - fill in an audit record at syscall entry
796 * @tsk: task being audited
797 * @arch: architecture type
798 * @major: major syscall type (function)
799 * @a1: additional syscall register 1
800 * @a2: additional syscall register 2
801 * @a3: additional syscall register 3
802 * @a4: additional syscall register 4
804 * Fill in audit context at syscall entry. This only happens if the
805 * audit context was created when the task was created and the state or
806 * filters demand the audit context be built. If the state from the
807 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
808 * then the record will be written at syscall exit time (otherwise, it
809 * will only be written if another part of the kernel requests that it
812 void audit_syscall_entry(int arch, int major,
813 unsigned long a1, unsigned long a2,
814 unsigned long a3, unsigned long a4)
816 struct task_struct *tsk = current;
817 struct audit_context *context = tsk->audit_context;
818 enum audit_state state;
823 * This happens only on certain architectures that make system
824 * calls in kernel_thread via the entry.S interface, instead of
825 * with direct calls. (If you are porting to a new
826 * architecture, hitting this condition can indicate that you
827 * got the _exit/_leave calls backward in entry.S.)
831 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
833 * This also happens with vm86 emulation in a non-nested manner
834 * (entries without exits), so this case must be caught.
836 if (context->in_syscall) {
837 struct audit_context *newctx;
841 "audit(:%d) pid=%d in syscall=%d;"
842 " entering syscall=%d\n",
843 context->serial, tsk->pid, context->major, major);
845 newctx = audit_alloc_context(context->state);
847 newctx->previous = context;
849 tsk->audit_context = newctx;
851 /* If we can't alloc a new context, the best we
852 * can do is to leak memory (any pending putname
853 * will be lost). The only other alternative is
854 * to abandon auditing. */
855 audit_zero_context(context, context->state);
858 BUG_ON(context->in_syscall || context->name_count);
863 context->arch = arch;
864 context->major = major;
865 context->argv[0] = a1;
866 context->argv[1] = a2;
867 context->argv[2] = a3;
868 context->argv[3] = a4;
870 state = context->state;
871 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
872 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
873 if (likely(state == AUDIT_DISABLED))
877 context->ctime = CURRENT_TIME;
878 context->in_syscall = 1;
879 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
883 * audit_syscall_exit - deallocate audit context after a system call
884 * @tsk: task being audited
885 * @valid: success/failure flag
886 * @return_code: syscall return value
888 * Tear down after system call. If the audit context has been marked as
889 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
890 * filtering, or because some other part of the kernel write an audit
891 * message), then write out the syscall information. In call cases,
892 * free the names stored from getname().
894 void audit_syscall_exit(int valid, long return_code)
896 struct task_struct *tsk = current;
897 struct audit_context *context;
899 context = audit_get_context(tsk, valid, return_code);
901 if (likely(!context))
904 if (context->in_syscall && context->auditable)
905 audit_log_exit(context, tsk);
907 context->in_syscall = 0;
908 context->auditable = 0;
910 if (context->previous) {
911 struct audit_context *new_context = context->previous;
912 context->previous = NULL;
913 audit_free_context(context);
914 tsk->audit_context = new_context;
916 audit_free_names(context);
917 audit_free_aux(context);
918 tsk->audit_context = context;
923 * audit_getname - add a name to the list
926 * Add a name to the list of audit names for this context.
927 * Called from fs/namei.c:getname().
929 void audit_getname(const char *name)
931 struct audit_context *context = current->audit_context;
933 if (!context || IS_ERR(name) || !name)
936 if (!context->in_syscall) {
938 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
939 __FILE__, __LINE__, context->serial, name);
944 BUG_ON(context->name_count >= AUDIT_NAMES);
945 context->names[context->name_count].name = name;
946 context->names[context->name_count].ino = (unsigned long)-1;
947 ++context->name_count;
949 read_lock(¤t->fs->lock);
950 context->pwd = dget(current->fs->pwd);
951 context->pwdmnt = mntget(current->fs->pwdmnt);
952 read_unlock(¤t->fs->lock);
957 /* audit_putname - intercept a putname request
958 * @name: name to intercept and delay for putname
960 * If we have stored the name from getname in the audit context,
961 * then we delay the putname until syscall exit.
962 * Called from include/linux/fs.h:putname().
964 void audit_putname(const char *name)
966 struct audit_context *context = current->audit_context;
969 if (!context->in_syscall) {
971 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
972 __FILE__, __LINE__, context->serial, name);
973 if (context->name_count) {
975 for (i = 0; i < context->name_count; i++)
976 printk(KERN_ERR "name[%d] = %p = %s\n", i,
977 context->names[i].name,
978 context->names[i].name ?: "(null)");
985 ++context->put_count;
986 if (context->put_count > context->name_count) {
987 printk(KERN_ERR "%s:%d(:%d): major=%d"
988 " in_syscall=%d putname(%p) name_count=%d"
991 context->serial, context->major,
992 context->in_syscall, name, context->name_count,
1000 static void audit_inode_context(int idx, const struct inode *inode)
1002 struct audit_context *context = current->audit_context;
1004 selinux_get_inode_sid(inode, &context->names[idx].osid);
1009 * audit_inode - store the inode and device from a lookup
1010 * @name: name being audited
1011 * @inode: inode being audited
1012 * @flags: lookup flags (as used in path_lookup())
1014 * Called from fs/namei.c:path_lookup().
1016 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1019 struct audit_context *context = current->audit_context;
1021 if (!context->in_syscall)
1023 if (context->name_count
1024 && context->names[context->name_count-1].name
1025 && context->names[context->name_count-1].name == name)
1026 idx = context->name_count - 1;
1027 else if (context->name_count > 1
1028 && context->names[context->name_count-2].name
1029 && context->names[context->name_count-2].name == name)
1030 idx = context->name_count - 2;
1032 /* FIXME: how much do we care about inodes that have no
1033 * associated name? */
1034 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1036 idx = context->name_count++;
1037 context->names[idx].name = NULL;
1039 ++context->ino_count;
1042 context->names[idx].dev = inode->i_sb->s_dev;
1043 context->names[idx].mode = inode->i_mode;
1044 context->names[idx].uid = inode->i_uid;
1045 context->names[idx].gid = inode->i_gid;
1046 context->names[idx].rdev = inode->i_rdev;
1047 audit_inode_context(idx, inode);
1048 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1049 (strcmp(name, ".") != 0)) {
1050 context->names[idx].ino = (unsigned long)-1;
1051 context->names[idx].pino = inode->i_ino;
1053 context->names[idx].ino = inode->i_ino;
1054 context->names[idx].pino = (unsigned long)-1;
1059 * audit_inode_child - collect inode info for created/removed objects
1060 * @dname: inode's dentry name
1061 * @inode: inode being audited
1062 * @pino: inode number of dentry parent
1064 * For syscalls that create or remove filesystem objects, audit_inode
1065 * can only collect information for the filesystem object's parent.
1066 * This call updates the audit context with the child's information.
1067 * Syscalls that create a new filesystem object must be hooked after
1068 * the object is created. Syscalls that remove a filesystem object
1069 * must be hooked prior, in order to capture the target inode during
1070 * unsuccessful attempts.
1072 void __audit_inode_child(const char *dname, const struct inode *inode,
1076 struct audit_context *context = current->audit_context;
1078 if (!context->in_syscall)
1081 /* determine matching parent */
1083 for (idx = 0; idx < context->name_count; idx++)
1084 if (context->names[idx].pino == pino) {
1086 const char *name = context->names[idx].name;
1087 int dlen = strlen(dname);
1088 int nlen = name ? strlen(name) : 0;
1093 /* disregard trailing slashes */
1094 n = name + nlen - 1;
1095 while ((*n == '/') && (n > name))
1098 /* find last path component */
1102 else if (n > name) {
1109 if (strncmp(n, dname, dlen) == 0)
1110 goto update_context;
1113 /* catch-all in case match not found */
1114 idx = context->name_count++;
1115 context->names[idx].name = NULL;
1116 context->names[idx].pino = pino;
1118 context->ino_count++;
1123 context->names[idx].ino = inode->i_ino;
1124 context->names[idx].dev = inode->i_sb->s_dev;
1125 context->names[idx].mode = inode->i_mode;
1126 context->names[idx].uid = inode->i_uid;
1127 context->names[idx].gid = inode->i_gid;
1128 context->names[idx].rdev = inode->i_rdev;
1129 audit_inode_context(idx, inode);
1134 * auditsc_get_stamp - get local copies of audit_context values
1135 * @ctx: audit_context for the task
1136 * @t: timespec to store time recorded in the audit_context
1137 * @serial: serial value that is recorded in the audit_context
1139 * Also sets the context as auditable.
1141 void auditsc_get_stamp(struct audit_context *ctx,
1142 struct timespec *t, unsigned int *serial)
1145 ctx->serial = audit_serial();
1146 t->tv_sec = ctx->ctime.tv_sec;
1147 t->tv_nsec = ctx->ctime.tv_nsec;
1148 *serial = ctx->serial;
1153 * audit_set_loginuid - set a task's audit_context loginuid
1154 * @task: task whose audit context is being modified
1155 * @loginuid: loginuid value
1159 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1161 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1163 if (task->audit_context) {
1164 struct audit_buffer *ab;
1166 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1168 audit_log_format(ab, "login pid=%d uid=%u "
1169 "old auid=%u new auid=%u",
1170 task->pid, task->uid,
1171 task->audit_context->loginuid, loginuid);
1174 task->audit_context->loginuid = loginuid;
1180 * audit_get_loginuid - get the loginuid for an audit_context
1181 * @ctx: the audit_context
1183 * Returns the context's loginuid or -1 if @ctx is NULL.
1185 uid_t audit_get_loginuid(struct audit_context *ctx)
1187 return ctx ? ctx->loginuid : -1;
1191 * audit_ipc_obj - record audit data for ipc object
1192 * @ipcp: ipc permissions
1194 * Returns 0 for success or NULL context or < 0 on error.
1196 int audit_ipc_obj(struct kern_ipc_perm *ipcp)
1198 struct audit_aux_data_ipcctl *ax;
1199 struct audit_context *context = current->audit_context;
1201 if (likely(!context))
1204 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1208 ax->uid = ipcp->uid;
1209 ax->gid = ipcp->gid;
1210 ax->mode = ipcp->mode;
1211 selinux_get_ipc_sid(ipcp, &ax->osid);
1213 ax->d.type = AUDIT_IPC;
1214 ax->d.next = context->aux;
1215 context->aux = (void *)ax;
1220 * audit_ipc_set_perm - record audit data for new ipc permissions
1221 * @qbytes: msgq bytes
1222 * @uid: msgq user id
1223 * @gid: msgq group id
1224 * @mode: msgq mode (permissions)
1226 * Returns 0 for success or NULL context or < 0 on error.
1228 int audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
1230 struct audit_aux_data_ipcctl *ax;
1231 struct audit_context *context = current->audit_context;
1233 if (likely(!context))
1236 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1240 ax->qbytes = qbytes;
1244 selinux_get_ipc_sid(ipcp, &ax->osid);
1246 ax->d.type = AUDIT_IPC_SET_PERM;
1247 ax->d.next = context->aux;
1248 context->aux = (void *)ax;
1252 int audit_bprm(struct linux_binprm *bprm)
1254 struct audit_aux_data_execve *ax;
1255 struct audit_context *context = current->audit_context;
1256 unsigned long p, next;
1259 if (likely(!audit_enabled || !context))
1262 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1267 ax->argc = bprm->argc;
1268 ax->envc = bprm->envc;
1269 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1270 struct page *page = bprm->page[p / PAGE_SIZE];
1271 void *kaddr = kmap(page);
1272 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1273 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1278 ax->d.type = AUDIT_EXECVE;
1279 ax->d.next = context->aux;
1280 context->aux = (void *)ax;
1286 * audit_socketcall - record audit data for sys_socketcall
1287 * @nargs: number of args
1290 * Returns 0 for success or NULL context or < 0 on error.
1292 int audit_socketcall(int nargs, unsigned long *args)
1294 struct audit_aux_data_socketcall *ax;
1295 struct audit_context *context = current->audit_context;
1297 if (likely(!context))
1300 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1305 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1307 ax->d.type = AUDIT_SOCKETCALL;
1308 ax->d.next = context->aux;
1309 context->aux = (void *)ax;
1314 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1315 * @len: data length in user space
1316 * @a: data address in kernel space
1318 * Returns 0 for success or NULL context or < 0 on error.
1320 int audit_sockaddr(int len, void *a)
1322 struct audit_aux_data_sockaddr *ax;
1323 struct audit_context *context = current->audit_context;
1325 if (likely(!context))
1328 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1333 memcpy(ax->a, a, len);
1335 ax->d.type = AUDIT_SOCKADDR;
1336 ax->d.next = context->aux;
1337 context->aux = (void *)ax;
1342 * audit_avc_path - record the granting or denial of permissions
1343 * @dentry: dentry to record
1344 * @mnt: mnt to record
1346 * Returns 0 for success or NULL context or < 0 on error.
1348 * Called from security/selinux/avc.c::avc_audit()
1350 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1352 struct audit_aux_data_path *ax;
1353 struct audit_context *context = current->audit_context;
1355 if (likely(!context))
1358 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1362 ax->dentry = dget(dentry);
1363 ax->mnt = mntget(mnt);
1365 ax->d.type = AUDIT_AVC_PATH;
1366 ax->d.next = context->aux;
1367 context->aux = (void *)ax;
1372 * audit_signal_info - record signal info for shutting down audit subsystem
1373 * @sig: signal value
1374 * @t: task being signaled
1376 * If the audit subsystem is being terminated, record the task (pid)
1377 * and uid that is doing that.
1379 void audit_signal_info(int sig, struct task_struct *t)
1381 extern pid_t audit_sig_pid;
1382 extern uid_t audit_sig_uid;
1384 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1385 if (sig == SIGTERM || sig == SIGHUP) {
1386 struct audit_context *ctx = current->audit_context;
1387 audit_sig_pid = current->pid;
1389 audit_sig_uid = ctx->loginuid;
1391 audit_sig_uid = current->uid;