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);
193 result = audit_comparator(ctx->ppid, f->op, f->val);
196 result = audit_comparator(tsk->uid, f->op, f->val);
199 result = audit_comparator(tsk->euid, f->op, f->val);
202 result = audit_comparator(tsk->suid, f->op, f->val);
205 result = audit_comparator(tsk->fsuid, f->op, f->val);
208 result = audit_comparator(tsk->gid, f->op, f->val);
211 result = audit_comparator(tsk->egid, f->op, f->val);
214 result = audit_comparator(tsk->sgid, f->op, f->val);
217 result = audit_comparator(tsk->fsgid, f->op, f->val);
220 result = audit_comparator(tsk->personality, f->op, f->val);
224 result = audit_comparator(ctx->arch, f->op, f->val);
228 if (ctx && ctx->return_valid)
229 result = audit_comparator(ctx->return_code, f->op, f->val);
232 if (ctx && ctx->return_valid) {
234 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
236 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
241 for (j = 0; j < ctx->name_count; j++) {
242 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
251 for (j = 0; j < ctx->name_count; j++) {
252 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
261 for (j = 0; j < ctx->name_count; j++) {
262 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
263 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
273 result = audit_comparator(ctx->loginuid, f->op, f->val);
280 /* NOTE: this may return negative values indicating
281 a temporary error. We simply treat this as a
282 match for now to avoid losing information that
283 may be wanted. An error message will also be
287 selinux_task_ctxid(tsk, &sid);
290 result = selinux_audit_rule_match(sid, f->type,
301 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
308 switch (rule->action) {
309 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
310 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
311 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
316 /* At process creation time, we can determine if system-call auditing is
317 * completely disabled for this task. Since we only have the task
318 * structure at this point, we can only check uid and gid.
320 static enum audit_state audit_filter_task(struct task_struct *tsk)
322 struct audit_entry *e;
323 enum audit_state state;
326 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
327 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
333 return AUDIT_BUILD_CONTEXT;
336 /* At syscall entry and exit time, this filter is called if the
337 * audit_state is not low enough that auditing cannot take place, but is
338 * also not high enough that we already know we have to write an audit
339 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
341 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
342 struct audit_context *ctx,
343 struct list_head *list)
345 struct audit_entry *e;
346 enum audit_state state;
348 if (audit_pid && tsk->tgid == audit_pid)
349 return AUDIT_DISABLED;
352 if (!list_empty(list)) {
353 int word = AUDIT_WORD(ctx->major);
354 int bit = AUDIT_BIT(ctx->major);
356 list_for_each_entry_rcu(e, list, list) {
357 if ((e->rule.mask[word] & bit) == bit
358 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
365 return AUDIT_BUILD_CONTEXT;
368 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
372 struct audit_context *context = tsk->audit_context;
374 if (likely(!context))
376 context->return_valid = return_valid;
377 context->return_code = return_code;
379 if (context->in_syscall && !context->auditable) {
380 enum audit_state state;
381 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
382 if (state == AUDIT_RECORD_CONTEXT)
383 context->auditable = 1;
386 context->pid = tsk->pid;
387 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
388 context->uid = tsk->uid;
389 context->gid = tsk->gid;
390 context->euid = tsk->euid;
391 context->suid = tsk->suid;
392 context->fsuid = tsk->fsuid;
393 context->egid = tsk->egid;
394 context->sgid = tsk->sgid;
395 context->fsgid = tsk->fsgid;
396 context->personality = tsk->personality;
397 tsk->audit_context = NULL;
401 static inline void audit_free_names(struct audit_context *context)
406 if (context->auditable
407 ||context->put_count + context->ino_count != context->name_count) {
408 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
409 " name_count=%d put_count=%d"
410 " ino_count=%d [NOT freeing]\n",
412 context->serial, context->major, context->in_syscall,
413 context->name_count, context->put_count,
415 for (i = 0; i < context->name_count; i++) {
416 printk(KERN_ERR "names[%d] = %p = %s\n", i,
417 context->names[i].name,
418 context->names[i].name ?: "(null)");
425 context->put_count = 0;
426 context->ino_count = 0;
429 for (i = 0; i < context->name_count; i++) {
430 if (context->names[i].name)
431 __putname(context->names[i].name);
433 context->name_count = 0;
437 mntput(context->pwdmnt);
439 context->pwdmnt = NULL;
442 static inline void audit_free_aux(struct audit_context *context)
444 struct audit_aux_data *aux;
446 while ((aux = context->aux)) {
447 if (aux->type == AUDIT_AVC_PATH) {
448 struct audit_aux_data_path *axi = (void *)aux;
453 context->aux = aux->next;
458 static inline void audit_zero_context(struct audit_context *context,
459 enum audit_state state)
461 uid_t loginuid = context->loginuid;
463 memset(context, 0, sizeof(*context));
464 context->state = state;
465 context->loginuid = loginuid;
468 static inline struct audit_context *audit_alloc_context(enum audit_state state)
470 struct audit_context *context;
472 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
474 audit_zero_context(context, state);
479 * audit_alloc - allocate an audit context block for a task
482 * Filter on the task information and allocate a per-task audit context
483 * if necessary. Doing so turns on system call auditing for the
484 * specified task. This is called from copy_process, so no lock is
487 int audit_alloc(struct task_struct *tsk)
489 struct audit_context *context;
490 enum audit_state state;
492 if (likely(!audit_enabled))
493 return 0; /* Return if not auditing. */
495 state = audit_filter_task(tsk);
496 if (likely(state == AUDIT_DISABLED))
499 if (!(context = audit_alloc_context(state))) {
500 audit_log_lost("out of memory in audit_alloc");
504 /* Preserve login uid */
505 context->loginuid = -1;
506 if (current->audit_context)
507 context->loginuid = current->audit_context->loginuid;
509 tsk->audit_context = context;
510 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
514 static inline void audit_free_context(struct audit_context *context)
516 struct audit_context *previous;
520 previous = context->previous;
521 if (previous || (count && count < 10)) {
523 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
524 " freeing multiple contexts (%d)\n",
525 context->serial, context->major,
526 context->name_count, count);
528 audit_free_names(context);
529 audit_free_aux(context);
534 printk(KERN_ERR "audit: freed %d contexts\n", count);
537 static void audit_log_task_context(struct audit_buffer *ab)
542 len = security_getprocattr(current, "current", NULL, 0);
549 ctx = kmalloc(len, GFP_KERNEL);
553 len = security_getprocattr(current, "current", ctx, len);
557 audit_log_format(ab, " subj=%s", ctx);
563 audit_panic("error in audit_log_task_context");
567 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
569 char name[sizeof(tsk->comm)];
570 struct mm_struct *mm = tsk->mm;
571 struct vm_area_struct *vma;
575 get_task_comm(name, tsk);
576 audit_log_format(ab, " comm=");
577 audit_log_untrustedstring(ab, name);
580 down_read(&mm->mmap_sem);
583 if ((vma->vm_flags & VM_EXECUTABLE) &&
585 audit_log_d_path(ab, "exe=",
586 vma->vm_file->f_dentry,
587 vma->vm_file->f_vfsmnt);
592 up_read(&mm->mmap_sem);
594 audit_log_task_context(ab);
597 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
599 int i, call_panic = 0;
600 struct audit_buffer *ab;
601 struct audit_aux_data *aux;
606 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
608 return; /* audit_panic has been called */
609 audit_log_format(ab, "arch=%x syscall=%d",
610 context->arch, context->major);
611 if (context->personality != PER_LINUX)
612 audit_log_format(ab, " per=%lx", context->personality);
613 if (context->return_valid)
614 audit_log_format(ab, " success=%s exit=%ld",
615 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
616 context->return_code);
617 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
618 tty = tsk->signal->tty->name;
622 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
623 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
624 " euid=%u suid=%u fsuid=%u"
625 " egid=%u sgid=%u fsgid=%u tty=%s",
636 context->euid, context->suid, context->fsuid,
637 context->egid, context->sgid, context->fsgid, tty);
638 audit_log_task_info(ab, tsk);
641 for (aux = context->aux; aux; aux = aux->next) {
643 ab = audit_log_start(context, GFP_KERNEL, aux->type);
645 continue; /* audit_panic has been called */
649 struct audit_aux_data_ipcctl *axi = (void *)aux;
651 "ouid=%u ogid=%u mode=%x",
652 axi->uid, axi->gid, axi->mode);
653 if (axi->osid != 0) {
656 if (selinux_ctxid_to_string(
657 axi->osid, &ctx, &len)) {
658 audit_log_format(ab, " osid=%u",
662 audit_log_format(ab, " obj=%s", ctx);
667 case AUDIT_IPC_SET_PERM: {
668 struct audit_aux_data_ipcctl *axi = (void *)aux;
670 "qbytes=%lx ouid=%u ogid=%u mode=%x",
671 axi->qbytes, axi->uid, axi->gid, axi->mode);
675 struct audit_aux_data_execve *axi = (void *)aux;
678 for (i = 0, p = axi->mem; i < axi->argc; i++) {
679 audit_log_format(ab, "a%d=", i);
680 p = audit_log_untrustedstring(ab, p);
681 audit_log_format(ab, "\n");
685 case AUDIT_SOCKETCALL: {
687 struct audit_aux_data_socketcall *axs = (void *)aux;
688 audit_log_format(ab, "nargs=%d", axs->nargs);
689 for (i=0; i<axs->nargs; i++)
690 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
693 case AUDIT_SOCKADDR: {
694 struct audit_aux_data_sockaddr *axs = (void *)aux;
696 audit_log_format(ab, "saddr=");
697 audit_log_hex(ab, axs->a, axs->len);
700 case AUDIT_AVC_PATH: {
701 struct audit_aux_data_path *axi = (void *)aux;
702 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
709 if (context->pwd && context->pwdmnt) {
710 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
712 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
716 for (i = 0; i < context->name_count; i++) {
717 unsigned long ino = context->names[i].ino;
718 unsigned long pino = context->names[i].pino;
720 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
722 continue; /* audit_panic has been called */
724 audit_log_format(ab, "item=%d", i);
726 audit_log_format(ab, " name=");
727 if (context->names[i].name)
728 audit_log_untrustedstring(ab, context->names[i].name);
730 audit_log_format(ab, "(null)");
732 if (pino != (unsigned long)-1)
733 audit_log_format(ab, " parent=%lu", pino);
734 if (ino != (unsigned long)-1)
735 audit_log_format(ab, " inode=%lu", ino);
736 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
737 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
738 " ouid=%u ogid=%u rdev=%02x:%02x",
739 MAJOR(context->names[i].dev),
740 MINOR(context->names[i].dev),
741 context->names[i].mode,
742 context->names[i].uid,
743 context->names[i].gid,
744 MAJOR(context->names[i].rdev),
745 MINOR(context->names[i].rdev));
746 if (context->names[i].osid != 0) {
749 if (selinux_ctxid_to_string(
750 context->names[i].osid, &ctx, &len)) {
751 audit_log_format(ab, " osid=%u",
752 context->names[i].osid);
755 audit_log_format(ab, " obj=%s", ctx);
762 audit_panic("error converting sid to string");
766 * audit_free - free a per-task audit context
767 * @tsk: task whose audit context block to free
769 * Called from copy_process and do_exit
771 void audit_free(struct task_struct *tsk)
773 struct audit_context *context;
775 context = audit_get_context(tsk, 0, 0);
776 if (likely(!context))
779 /* Check for system calls that do not go through the exit
780 * function (e.g., exit_group), then free context block.
781 * We use GFP_ATOMIC here because we might be doing this
782 * in the context of the idle thread */
783 /* that can happen only if we are called from do_exit() */
784 if (context->in_syscall && context->auditable)
785 audit_log_exit(context, tsk);
787 audit_free_context(context);
791 * audit_syscall_entry - fill in an audit record at syscall entry
792 * @tsk: task being audited
793 * @arch: architecture type
794 * @major: major syscall type (function)
795 * @a1: additional syscall register 1
796 * @a2: additional syscall register 2
797 * @a3: additional syscall register 3
798 * @a4: additional syscall register 4
800 * Fill in audit context at syscall entry. This only happens if the
801 * audit context was created when the task was created and the state or
802 * filters demand the audit context be built. If the state from the
803 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
804 * then the record will be written at syscall exit time (otherwise, it
805 * will only be written if another part of the kernel requests that it
808 void audit_syscall_entry(int arch, int major,
809 unsigned long a1, unsigned long a2,
810 unsigned long a3, unsigned long a4)
812 struct task_struct *tsk = current;
813 struct audit_context *context = tsk->audit_context;
814 enum audit_state state;
819 * This happens only on certain architectures that make system
820 * calls in kernel_thread via the entry.S interface, instead of
821 * with direct calls. (If you are porting to a new
822 * architecture, hitting this condition can indicate that you
823 * got the _exit/_leave calls backward in entry.S.)
827 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
829 * This also happens with vm86 emulation in a non-nested manner
830 * (entries without exits), so this case must be caught.
832 if (context->in_syscall) {
833 struct audit_context *newctx;
837 "audit(:%d) pid=%d in syscall=%d;"
838 " entering syscall=%d\n",
839 context->serial, tsk->pid, context->major, major);
841 newctx = audit_alloc_context(context->state);
843 newctx->previous = context;
845 tsk->audit_context = newctx;
847 /* If we can't alloc a new context, the best we
848 * can do is to leak memory (any pending putname
849 * will be lost). The only other alternative is
850 * to abandon auditing. */
851 audit_zero_context(context, context->state);
854 BUG_ON(context->in_syscall || context->name_count);
859 context->arch = arch;
860 context->major = major;
861 context->argv[0] = a1;
862 context->argv[1] = a2;
863 context->argv[2] = a3;
864 context->argv[3] = a4;
866 state = context->state;
867 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
868 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
869 if (likely(state == AUDIT_DISABLED))
873 context->ctime = CURRENT_TIME;
874 context->in_syscall = 1;
875 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
879 * audit_syscall_exit - deallocate audit context after a system call
880 * @tsk: task being audited
881 * @valid: success/failure flag
882 * @return_code: syscall return value
884 * Tear down after system call. If the audit context has been marked as
885 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
886 * filtering, or because some other part of the kernel write an audit
887 * message), then write out the syscall information. In call cases,
888 * free the names stored from getname().
890 void audit_syscall_exit(int valid, long return_code)
892 struct task_struct *tsk = current;
893 struct audit_context *context;
895 context = audit_get_context(tsk, valid, return_code);
897 if (likely(!context))
900 if (context->in_syscall && context->auditable)
901 audit_log_exit(context, tsk);
903 context->in_syscall = 0;
904 context->auditable = 0;
906 if (context->previous) {
907 struct audit_context *new_context = context->previous;
908 context->previous = NULL;
909 audit_free_context(context);
910 tsk->audit_context = new_context;
912 audit_free_names(context);
913 audit_free_aux(context);
914 tsk->audit_context = context;
919 * audit_getname - add a name to the list
922 * Add a name to the list of audit names for this context.
923 * Called from fs/namei.c:getname().
925 void audit_getname(const char *name)
927 struct audit_context *context = current->audit_context;
929 if (!context || IS_ERR(name) || !name)
932 if (!context->in_syscall) {
934 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
935 __FILE__, __LINE__, context->serial, name);
940 BUG_ON(context->name_count >= AUDIT_NAMES);
941 context->names[context->name_count].name = name;
942 context->names[context->name_count].ino = (unsigned long)-1;
943 ++context->name_count;
945 read_lock(¤t->fs->lock);
946 context->pwd = dget(current->fs->pwd);
947 context->pwdmnt = mntget(current->fs->pwdmnt);
948 read_unlock(¤t->fs->lock);
953 /* audit_putname - intercept a putname request
954 * @name: name to intercept and delay for putname
956 * If we have stored the name from getname in the audit context,
957 * then we delay the putname until syscall exit.
958 * Called from include/linux/fs.h:putname().
960 void audit_putname(const char *name)
962 struct audit_context *context = current->audit_context;
965 if (!context->in_syscall) {
967 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
968 __FILE__, __LINE__, context->serial, name);
969 if (context->name_count) {
971 for (i = 0; i < context->name_count; i++)
972 printk(KERN_ERR "name[%d] = %p = %s\n", i,
973 context->names[i].name,
974 context->names[i].name ?: "(null)");
981 ++context->put_count;
982 if (context->put_count > context->name_count) {
983 printk(KERN_ERR "%s:%d(:%d): major=%d"
984 " in_syscall=%d putname(%p) name_count=%d"
987 context->serial, context->major,
988 context->in_syscall, name, context->name_count,
996 static void audit_inode_context(int idx, const struct inode *inode)
998 struct audit_context *context = current->audit_context;
1000 selinux_get_inode_sid(inode, &context->names[idx].osid);
1005 * audit_inode - store the inode and device from a lookup
1006 * @name: name being audited
1007 * @inode: inode being audited
1008 * @flags: lookup flags (as used in path_lookup())
1010 * Called from fs/namei.c:path_lookup().
1012 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1015 struct audit_context *context = current->audit_context;
1017 if (!context->in_syscall)
1019 if (context->name_count
1020 && context->names[context->name_count-1].name
1021 && context->names[context->name_count-1].name == name)
1022 idx = context->name_count - 1;
1023 else if (context->name_count > 1
1024 && context->names[context->name_count-2].name
1025 && context->names[context->name_count-2].name == name)
1026 idx = context->name_count - 2;
1028 /* FIXME: how much do we care about inodes that have no
1029 * associated name? */
1030 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1032 idx = context->name_count++;
1033 context->names[idx].name = NULL;
1035 ++context->ino_count;
1038 context->names[idx].dev = inode->i_sb->s_dev;
1039 context->names[idx].mode = inode->i_mode;
1040 context->names[idx].uid = inode->i_uid;
1041 context->names[idx].gid = inode->i_gid;
1042 context->names[idx].rdev = inode->i_rdev;
1043 audit_inode_context(idx, inode);
1044 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1045 (strcmp(name, ".") != 0)) {
1046 context->names[idx].ino = (unsigned long)-1;
1047 context->names[idx].pino = inode->i_ino;
1049 context->names[idx].ino = inode->i_ino;
1050 context->names[idx].pino = (unsigned long)-1;
1055 * audit_inode_child - collect inode info for created/removed objects
1056 * @dname: inode's dentry name
1057 * @inode: inode being audited
1058 * @pino: inode number of dentry parent
1060 * For syscalls that create or remove filesystem objects, audit_inode
1061 * can only collect information for the filesystem object's parent.
1062 * This call updates the audit context with the child's information.
1063 * Syscalls that create a new filesystem object must be hooked after
1064 * the object is created. Syscalls that remove a filesystem object
1065 * must be hooked prior, in order to capture the target inode during
1066 * unsuccessful attempts.
1068 void __audit_inode_child(const char *dname, const struct inode *inode,
1072 struct audit_context *context = current->audit_context;
1074 if (!context->in_syscall)
1077 /* determine matching parent */
1079 for (idx = 0; idx < context->name_count; idx++)
1080 if (context->names[idx].pino == pino) {
1082 const char *name = context->names[idx].name;
1083 int dlen = strlen(dname);
1084 int nlen = name ? strlen(name) : 0;
1089 /* disregard trailing slashes */
1090 n = name + nlen - 1;
1091 while ((*n == '/') && (n > name))
1094 /* find last path component */
1098 else if (n > name) {
1105 if (strncmp(n, dname, dlen) == 0)
1106 goto update_context;
1109 /* catch-all in case match not found */
1110 idx = context->name_count++;
1111 context->names[idx].name = NULL;
1112 context->names[idx].pino = pino;
1114 context->ino_count++;
1119 context->names[idx].ino = inode->i_ino;
1120 context->names[idx].dev = inode->i_sb->s_dev;
1121 context->names[idx].mode = inode->i_mode;
1122 context->names[idx].uid = inode->i_uid;
1123 context->names[idx].gid = inode->i_gid;
1124 context->names[idx].rdev = inode->i_rdev;
1125 audit_inode_context(idx, inode);
1130 * auditsc_get_stamp - get local copies of audit_context values
1131 * @ctx: audit_context for the task
1132 * @t: timespec to store time recorded in the audit_context
1133 * @serial: serial value that is recorded in the audit_context
1135 * Also sets the context as auditable.
1137 void auditsc_get_stamp(struct audit_context *ctx,
1138 struct timespec *t, unsigned int *serial)
1141 ctx->serial = audit_serial();
1142 t->tv_sec = ctx->ctime.tv_sec;
1143 t->tv_nsec = ctx->ctime.tv_nsec;
1144 *serial = ctx->serial;
1149 * audit_set_loginuid - set a task's audit_context loginuid
1150 * @task: task whose audit context is being modified
1151 * @loginuid: loginuid value
1155 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1157 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1159 if (task->audit_context) {
1160 struct audit_buffer *ab;
1162 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1164 audit_log_format(ab, "login pid=%d uid=%u "
1165 "old auid=%u new auid=%u",
1166 task->pid, task->uid,
1167 task->audit_context->loginuid, loginuid);
1170 task->audit_context->loginuid = loginuid;
1176 * audit_get_loginuid - get the loginuid for an audit_context
1177 * @ctx: the audit_context
1179 * Returns the context's loginuid or -1 if @ctx is NULL.
1181 uid_t audit_get_loginuid(struct audit_context *ctx)
1183 return ctx ? ctx->loginuid : -1;
1187 * audit_ipc_obj - record audit data for ipc object
1188 * @ipcp: ipc permissions
1190 * Returns 0 for success or NULL context or < 0 on error.
1192 int audit_ipc_obj(struct kern_ipc_perm *ipcp)
1194 struct audit_aux_data_ipcctl *ax;
1195 struct audit_context *context = current->audit_context;
1197 if (likely(!context))
1200 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1204 ax->uid = ipcp->uid;
1205 ax->gid = ipcp->gid;
1206 ax->mode = ipcp->mode;
1207 selinux_get_ipc_sid(ipcp, &ax->osid);
1209 ax->d.type = AUDIT_IPC;
1210 ax->d.next = context->aux;
1211 context->aux = (void *)ax;
1216 * audit_ipc_set_perm - record audit data for new ipc permissions
1217 * @qbytes: msgq bytes
1218 * @uid: msgq user id
1219 * @gid: msgq group id
1220 * @mode: msgq mode (permissions)
1222 * Returns 0 for success or NULL context or < 0 on error.
1224 int audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1226 struct audit_aux_data_ipcctl *ax;
1227 struct audit_context *context = current->audit_context;
1229 if (likely(!context))
1232 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1236 ax->qbytes = qbytes;
1241 ax->d.type = AUDIT_IPC_SET_PERM;
1242 ax->d.next = context->aux;
1243 context->aux = (void *)ax;
1247 int audit_bprm(struct linux_binprm *bprm)
1249 struct audit_aux_data_execve *ax;
1250 struct audit_context *context = current->audit_context;
1251 unsigned long p, next;
1254 if (likely(!audit_enabled || !context))
1257 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1262 ax->argc = bprm->argc;
1263 ax->envc = bprm->envc;
1264 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1265 struct page *page = bprm->page[p / PAGE_SIZE];
1266 void *kaddr = kmap(page);
1267 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1268 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1273 ax->d.type = AUDIT_EXECVE;
1274 ax->d.next = context->aux;
1275 context->aux = (void *)ax;
1281 * audit_socketcall - record audit data for sys_socketcall
1282 * @nargs: number of args
1285 * Returns 0 for success or NULL context or < 0 on error.
1287 int audit_socketcall(int nargs, unsigned long *args)
1289 struct audit_aux_data_socketcall *ax;
1290 struct audit_context *context = current->audit_context;
1292 if (likely(!context))
1295 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1300 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1302 ax->d.type = AUDIT_SOCKETCALL;
1303 ax->d.next = context->aux;
1304 context->aux = (void *)ax;
1309 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1310 * @len: data length in user space
1311 * @a: data address in kernel space
1313 * Returns 0 for success or NULL context or < 0 on error.
1315 int audit_sockaddr(int len, void *a)
1317 struct audit_aux_data_sockaddr *ax;
1318 struct audit_context *context = current->audit_context;
1320 if (likely(!context))
1323 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1328 memcpy(ax->a, a, len);
1330 ax->d.type = AUDIT_SOCKADDR;
1331 ax->d.next = context->aux;
1332 context->aux = (void *)ax;
1337 * audit_avc_path - record the granting or denial of permissions
1338 * @dentry: dentry to record
1339 * @mnt: mnt to record
1341 * Returns 0 for success or NULL context or < 0 on error.
1343 * Called from security/selinux/avc.c::avc_audit()
1345 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1347 struct audit_aux_data_path *ax;
1348 struct audit_context *context = current->audit_context;
1350 if (likely(!context))
1353 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1357 ax->dentry = dget(dentry);
1358 ax->mnt = mntget(mnt);
1360 ax->d.type = AUDIT_AVC_PATH;
1361 ax->d.next = context->aux;
1362 context->aux = (void *)ax;
1367 * audit_signal_info - record signal info for shutting down audit subsystem
1368 * @sig: signal value
1369 * @t: task being signaled
1371 * If the audit subsystem is being terminated, record the task (pid)
1372 * and uid that is doing that.
1374 void __audit_signal_info(int sig, struct task_struct *t)
1376 extern pid_t audit_sig_pid;
1377 extern uid_t audit_sig_uid;
1378 extern u32 audit_sig_sid;
1380 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1381 struct task_struct *tsk = current;
1382 struct audit_context *ctx = tsk->audit_context;
1383 audit_sig_pid = tsk->pid;
1385 audit_sig_uid = ctx->loginuid;
1387 audit_sig_uid = tsk->uid;
1388 selinux_get_task_sid(tsk, &audit_sig_sid);