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>
63 extern struct list_head audit_filter_list[];
65 /* No syscall auditing will take place unless audit_enabled != 0. */
66 extern int audit_enabled;
68 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
69 * for saving names from getname(). */
70 #define AUDIT_NAMES 20
72 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
73 * audit_context from being used for nameless inodes from
75 #define AUDIT_NAMES_RESERVED 7
77 /* When fs/namei.c:getname() is called, we store the pointer in name and
78 * we don't let putname() free it (instead we free all of the saved
79 * pointers at syscall exit time).
81 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
94 struct audit_aux_data {
95 struct audit_aux_data *next;
99 #define AUDIT_AUX_IPCPERM 0
101 struct audit_aux_data_ipcctl {
102 struct audit_aux_data d;
104 unsigned long qbytes;
111 struct audit_aux_data_socketcall {
112 struct audit_aux_data d;
114 unsigned long args[0];
117 struct audit_aux_data_sockaddr {
118 struct audit_aux_data d;
123 struct audit_aux_data_path {
124 struct audit_aux_data d;
125 struct dentry *dentry;
126 struct vfsmount *mnt;
129 /* The per-task audit context. */
130 struct audit_context {
131 int in_syscall; /* 1 if task is in a syscall */
132 enum audit_state state;
133 unsigned int serial; /* serial number for record */
134 struct timespec ctime; /* time of syscall entry */
135 uid_t loginuid; /* login uid (identity) */
136 int major; /* syscall number */
137 unsigned long argv[4]; /* syscall arguments */
138 int return_valid; /* return code is valid */
139 long return_code;/* syscall return code */
140 int auditable; /* 1 if record should be written */
142 struct audit_names names[AUDIT_NAMES];
144 struct vfsmount * pwdmnt;
145 struct audit_context *previous; /* For nested syscalls */
146 struct audit_aux_data *aux;
148 /* Save things to print about task_struct */
150 uid_t uid, euid, suid, fsuid;
151 gid_t gid, egid, sgid, fsgid;
152 unsigned long personality;
162 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
164 static int audit_filter_rules(struct task_struct *tsk,
165 struct audit_krule *rule,
166 struct audit_context *ctx,
167 enum audit_state *state)
171 for (i = 0; i < rule->field_count; i++) {
172 struct audit_field *f = &rule->fields[i];
177 result = audit_comparator(tsk->pid, f->op, f->val);
180 result = audit_comparator(tsk->uid, f->op, f->val);
183 result = audit_comparator(tsk->euid, f->op, f->val);
186 result = audit_comparator(tsk->suid, f->op, f->val);
189 result = audit_comparator(tsk->fsuid, f->op, f->val);
192 result = audit_comparator(tsk->gid, f->op, f->val);
195 result = audit_comparator(tsk->egid, f->op, f->val);
198 result = audit_comparator(tsk->sgid, f->op, f->val);
201 result = audit_comparator(tsk->fsgid, f->op, f->val);
204 result = audit_comparator(tsk->personality, f->op, f->val);
208 result = audit_comparator(ctx->arch, f->op, f->val);
212 if (ctx && ctx->return_valid)
213 result = audit_comparator(ctx->return_code, f->op, f->val);
216 if (ctx && ctx->return_valid) {
218 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
220 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
225 for (j = 0; j < ctx->name_count; j++) {
226 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
235 for (j = 0; j < ctx->name_count; j++) {
236 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
245 for (j = 0; j < ctx->name_count; j++) {
246 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
247 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
257 result = audit_comparator(ctx->loginuid, f->op, f->val);
264 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
271 switch (rule->action) {
272 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
273 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
274 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
279 /* At process creation time, we can determine if system-call auditing is
280 * completely disabled for this task. Since we only have the task
281 * structure at this point, we can only check uid and gid.
283 static enum audit_state audit_filter_task(struct task_struct *tsk)
285 struct audit_entry *e;
286 enum audit_state state;
289 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
290 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
296 return AUDIT_BUILD_CONTEXT;
299 /* At syscall entry and exit time, this filter is called if the
300 * audit_state is not low enough that auditing cannot take place, but is
301 * also not high enough that we already know we have to write an audit
302 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
304 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
305 struct audit_context *ctx,
306 struct list_head *list)
308 struct audit_entry *e;
309 enum audit_state state;
311 if (audit_pid && tsk->tgid == audit_pid)
312 return AUDIT_DISABLED;
315 if (!list_empty(list)) {
316 int word = AUDIT_WORD(ctx->major);
317 int bit = AUDIT_BIT(ctx->major);
319 list_for_each_entry_rcu(e, list, list) {
320 if ((e->rule.mask[word] & bit) == bit
321 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
328 return AUDIT_BUILD_CONTEXT;
331 /* This should be called with task_lock() held. */
332 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
336 struct audit_context *context = tsk->audit_context;
338 if (likely(!context))
340 context->return_valid = return_valid;
341 context->return_code = return_code;
343 if (context->in_syscall && !context->auditable) {
344 enum audit_state state;
345 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
346 if (state == AUDIT_RECORD_CONTEXT)
347 context->auditable = 1;
350 context->pid = tsk->pid;
351 context->uid = tsk->uid;
352 context->gid = tsk->gid;
353 context->euid = tsk->euid;
354 context->suid = tsk->suid;
355 context->fsuid = tsk->fsuid;
356 context->egid = tsk->egid;
357 context->sgid = tsk->sgid;
358 context->fsgid = tsk->fsgid;
359 context->personality = tsk->personality;
360 tsk->audit_context = NULL;
364 static inline void audit_free_names(struct audit_context *context)
369 if (context->auditable
370 ||context->put_count + context->ino_count != context->name_count) {
371 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
372 " name_count=%d put_count=%d"
373 " ino_count=%d [NOT freeing]\n",
375 context->serial, context->major, context->in_syscall,
376 context->name_count, context->put_count,
378 for (i = 0; i < context->name_count; i++) {
379 printk(KERN_ERR "names[%d] = %p = %s\n", i,
380 context->names[i].name,
381 context->names[i].name ?: "(null)");
388 context->put_count = 0;
389 context->ino_count = 0;
392 for (i = 0; i < context->name_count; i++) {
393 char *p = context->names[i].ctx;
394 context->names[i].ctx = NULL;
396 if (context->names[i].name)
397 __putname(context->names[i].name);
399 context->name_count = 0;
403 mntput(context->pwdmnt);
405 context->pwdmnt = NULL;
408 static inline void audit_free_aux(struct audit_context *context)
410 struct audit_aux_data *aux;
412 while ((aux = context->aux)) {
413 if (aux->type == AUDIT_AVC_PATH) {
414 struct audit_aux_data_path *axi = (void *)aux;
418 if ( aux->type == AUDIT_IPC ) {
419 struct audit_aux_data_ipcctl *axi = (void *)aux;
424 context->aux = aux->next;
429 static inline void audit_zero_context(struct audit_context *context,
430 enum audit_state state)
432 uid_t loginuid = context->loginuid;
434 memset(context, 0, sizeof(*context));
435 context->state = state;
436 context->loginuid = loginuid;
439 static inline struct audit_context *audit_alloc_context(enum audit_state state)
441 struct audit_context *context;
443 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
445 audit_zero_context(context, state);
450 * audit_alloc - allocate an audit context block for a task
453 * Filter on the task information and allocate a per-task audit context
454 * if necessary. Doing so turns on system call auditing for the
455 * specified task. This is called from copy_process, so no lock is
458 int audit_alloc(struct task_struct *tsk)
460 struct audit_context *context;
461 enum audit_state state;
463 if (likely(!audit_enabled))
464 return 0; /* Return if not auditing. */
466 state = audit_filter_task(tsk);
467 if (likely(state == AUDIT_DISABLED))
470 if (!(context = audit_alloc_context(state))) {
471 audit_log_lost("out of memory in audit_alloc");
475 /* Preserve login uid */
476 context->loginuid = -1;
477 if (current->audit_context)
478 context->loginuid = current->audit_context->loginuid;
480 tsk->audit_context = context;
481 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
485 static inline void audit_free_context(struct audit_context *context)
487 struct audit_context *previous;
491 previous = context->previous;
492 if (previous || (count && count < 10)) {
494 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
495 " freeing multiple contexts (%d)\n",
496 context->serial, context->major,
497 context->name_count, count);
499 audit_free_names(context);
500 audit_free_aux(context);
505 printk(KERN_ERR "audit: freed %d contexts\n", count);
508 static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask)
513 len = security_getprocattr(current, "current", NULL, 0);
520 ctx = kmalloc(len, gfp_mask);
524 len = security_getprocattr(current, "current", ctx, len);
528 audit_log_format(ab, " subj=%s", ctx);
534 audit_panic("error in audit_log_task_context");
538 static void audit_log_task_info(struct audit_buffer *ab, gfp_t gfp_mask)
540 char name[sizeof(current->comm)];
541 struct mm_struct *mm = current->mm;
542 struct vm_area_struct *vma;
544 get_task_comm(name, current);
545 audit_log_format(ab, " comm=");
546 audit_log_untrustedstring(ab, name);
552 * this is brittle; all callers that pass GFP_ATOMIC will have
553 * NULL current->mm and we won't get here.
555 down_read(&mm->mmap_sem);
558 if ((vma->vm_flags & VM_EXECUTABLE) &&
560 audit_log_d_path(ab, "exe=",
561 vma->vm_file->f_dentry,
562 vma->vm_file->f_vfsmnt);
567 up_read(&mm->mmap_sem);
568 audit_log_task_context(ab, gfp_mask);
571 static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
574 struct audit_buffer *ab;
575 struct audit_aux_data *aux;
577 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
579 return; /* audit_panic has been called */
580 audit_log_format(ab, "arch=%x syscall=%d",
581 context->arch, context->major);
582 if (context->personality != PER_LINUX)
583 audit_log_format(ab, " per=%lx", context->personality);
584 if (context->return_valid)
585 audit_log_format(ab, " success=%s exit=%ld",
586 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
587 context->return_code);
589 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
590 " pid=%d auid=%u uid=%u gid=%u"
591 " euid=%u suid=%u fsuid=%u"
592 " egid=%u sgid=%u fsgid=%u",
602 context->euid, context->suid, context->fsuid,
603 context->egid, context->sgid, context->fsgid);
604 audit_log_task_info(ab, gfp_mask);
607 for (aux = context->aux; aux; aux = aux->next) {
609 ab = audit_log_start(context, gfp_mask, aux->type);
611 continue; /* audit_panic has been called */
615 struct audit_aux_data_ipcctl *axi = (void *)aux;
617 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
618 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
621 case AUDIT_SOCKETCALL: {
623 struct audit_aux_data_socketcall *axs = (void *)aux;
624 audit_log_format(ab, "nargs=%d", axs->nargs);
625 for (i=0; i<axs->nargs; i++)
626 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
629 case AUDIT_SOCKADDR: {
630 struct audit_aux_data_sockaddr *axs = (void *)aux;
632 audit_log_format(ab, "saddr=");
633 audit_log_hex(ab, axs->a, axs->len);
636 case AUDIT_AVC_PATH: {
637 struct audit_aux_data_path *axi = (void *)aux;
638 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
645 if (context->pwd && context->pwdmnt) {
646 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
648 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
652 for (i = 0; i < context->name_count; i++) {
653 unsigned long ino = context->names[i].ino;
654 unsigned long pino = context->names[i].pino;
656 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
658 continue; /* audit_panic has been called */
660 audit_log_format(ab, "item=%d", i);
662 audit_log_format(ab, " name=");
663 if (context->names[i].name)
664 audit_log_untrustedstring(ab, context->names[i].name);
666 audit_log_format(ab, "(null)");
668 if (pino != (unsigned long)-1)
669 audit_log_format(ab, " parent=%lu", pino);
670 if (ino != (unsigned long)-1)
671 audit_log_format(ab, " inode=%lu", ino);
672 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
673 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
674 " ouid=%u ogid=%u rdev=%02x:%02x",
675 MAJOR(context->names[i].dev),
676 MINOR(context->names[i].dev),
677 context->names[i].mode,
678 context->names[i].uid,
679 context->names[i].gid,
680 MAJOR(context->names[i].rdev),
681 MINOR(context->names[i].rdev));
682 if (context->names[i].ctx) {
683 audit_log_format(ab, " obj=%s",
684 context->names[i].ctx);
692 * audit_free - free a per-task audit context
693 * @tsk: task whose audit context block to free
695 * Called from copy_process and __put_task_struct.
697 void audit_free(struct task_struct *tsk)
699 struct audit_context *context;
702 context = audit_get_context(tsk, 0, 0);
705 if (likely(!context))
708 /* Check for system calls that do not go through the exit
709 * function (e.g., exit_group), then free context block.
710 * We use GFP_ATOMIC here because we might be doing this
711 * in the context of the idle thread */
712 if (context->in_syscall && context->auditable)
713 audit_log_exit(context, GFP_ATOMIC);
715 audit_free_context(context);
719 * audit_syscall_entry - fill in an audit record at syscall entry
720 * @tsk: task being audited
721 * @arch: architecture type
722 * @major: major syscall type (function)
723 * @a1: additional syscall register 1
724 * @a2: additional syscall register 2
725 * @a3: additional syscall register 3
726 * @a4: additional syscall register 4
728 * Fill in audit context at syscall entry. This only happens if the
729 * audit context was created when the task was created and the state or
730 * filters demand the audit context be built. If the state from the
731 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
732 * then the record will be written at syscall exit time (otherwise, it
733 * will only be written if another part of the kernel requests that it
736 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
737 unsigned long a1, unsigned long a2,
738 unsigned long a3, unsigned long a4)
740 struct audit_context *context = tsk->audit_context;
741 enum audit_state state;
746 * This happens only on certain architectures that make system
747 * calls in kernel_thread via the entry.S interface, instead of
748 * with direct calls. (If you are porting to a new
749 * architecture, hitting this condition can indicate that you
750 * got the _exit/_leave calls backward in entry.S.)
754 * ppc64 yes (see arch/ppc64/kernel/misc.S)
756 * This also happens with vm86 emulation in a non-nested manner
757 * (entries without exits), so this case must be caught.
759 if (context->in_syscall) {
760 struct audit_context *newctx;
764 "audit(:%d) pid=%d in syscall=%d;"
765 " entering syscall=%d\n",
766 context->serial, tsk->pid, context->major, major);
768 newctx = audit_alloc_context(context->state);
770 newctx->previous = context;
772 tsk->audit_context = newctx;
774 /* If we can't alloc a new context, the best we
775 * can do is to leak memory (any pending putname
776 * will be lost). The only other alternative is
777 * to abandon auditing. */
778 audit_zero_context(context, context->state);
781 BUG_ON(context->in_syscall || context->name_count);
786 context->arch = arch;
787 context->major = major;
788 context->argv[0] = a1;
789 context->argv[1] = a2;
790 context->argv[2] = a3;
791 context->argv[3] = a4;
793 state = context->state;
794 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
795 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
796 if (likely(state == AUDIT_DISABLED))
800 context->ctime = CURRENT_TIME;
801 context->in_syscall = 1;
802 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
806 * audit_syscall_exit - deallocate audit context after a system call
807 * @tsk: task being audited
808 * @valid: success/failure flag
809 * @return_code: syscall return value
811 * Tear down after system call. If the audit context has been marked as
812 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
813 * filtering, or because some other part of the kernel write an audit
814 * message), then write out the syscall information. In call cases,
815 * free the names stored from getname().
817 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
819 struct audit_context *context;
821 get_task_struct(tsk);
823 context = audit_get_context(tsk, valid, return_code);
826 /* Not having a context here is ok, since the parent may have
827 * called __put_task_struct. */
828 if (likely(!context))
831 if (context->in_syscall && context->auditable)
832 audit_log_exit(context, GFP_KERNEL);
834 context->in_syscall = 0;
835 context->auditable = 0;
837 if (context->previous) {
838 struct audit_context *new_context = context->previous;
839 context->previous = NULL;
840 audit_free_context(context);
841 tsk->audit_context = new_context;
843 audit_free_names(context);
844 audit_free_aux(context);
845 tsk->audit_context = context;
848 put_task_struct(tsk);
852 * audit_getname - add a name to the list
855 * Add a name to the list of audit names for this context.
856 * Called from fs/namei.c:getname().
858 void audit_getname(const char *name)
860 struct audit_context *context = current->audit_context;
862 if (!context || IS_ERR(name) || !name)
865 if (!context->in_syscall) {
867 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
868 __FILE__, __LINE__, context->serial, name);
873 BUG_ON(context->name_count >= AUDIT_NAMES);
874 context->names[context->name_count].name = name;
875 context->names[context->name_count].ino = (unsigned long)-1;
876 ++context->name_count;
878 read_lock(¤t->fs->lock);
879 context->pwd = dget(current->fs->pwd);
880 context->pwdmnt = mntget(current->fs->pwdmnt);
881 read_unlock(¤t->fs->lock);
886 /* audit_putname - intercept a putname request
887 * @name: name to intercept and delay for putname
889 * If we have stored the name from getname in the audit context,
890 * then we delay the putname until syscall exit.
891 * Called from include/linux/fs.h:putname().
893 void audit_putname(const char *name)
895 struct audit_context *context = current->audit_context;
898 if (!context->in_syscall) {
900 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
901 __FILE__, __LINE__, context->serial, name);
902 if (context->name_count) {
904 for (i = 0; i < context->name_count; i++)
905 printk(KERN_ERR "name[%d] = %p = %s\n", i,
906 context->names[i].name,
907 context->names[i].name ?: "(null)");
914 ++context->put_count;
915 if (context->put_count > context->name_count) {
916 printk(KERN_ERR "%s:%d(:%d): major=%d"
917 " in_syscall=%d putname(%p) name_count=%d"
920 context->serial, context->major,
921 context->in_syscall, name, context->name_count,
929 void audit_inode_context(int idx, const struct inode *inode)
931 struct audit_context *context = current->audit_context;
932 const char *suffix = security_inode_xattr_getsuffix();
939 len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
940 if (len == -EOPNOTSUPP)
945 ctx = kmalloc(len, GFP_KERNEL);
949 len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
953 kfree(context->names[idx].ctx);
954 context->names[idx].ctx = ctx;
960 audit_panic("error in audit_inode_context");
967 * audit_inode - store the inode and device from a lookup
968 * @name: name being audited
969 * @inode: inode being audited
970 * @flags: lookup flags (as used in path_lookup())
972 * Called from fs/namei.c:path_lookup().
974 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
977 struct audit_context *context = current->audit_context;
979 if (!context->in_syscall)
981 if (context->name_count
982 && context->names[context->name_count-1].name
983 && context->names[context->name_count-1].name == name)
984 idx = context->name_count - 1;
985 else if (context->name_count > 1
986 && context->names[context->name_count-2].name
987 && context->names[context->name_count-2].name == name)
988 idx = context->name_count - 2;
990 /* FIXME: how much do we care about inodes that have no
991 * associated name? */
992 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
994 idx = context->name_count++;
995 context->names[idx].name = NULL;
997 ++context->ino_count;
1000 context->names[idx].dev = inode->i_sb->s_dev;
1001 context->names[idx].mode = inode->i_mode;
1002 context->names[idx].uid = inode->i_uid;
1003 context->names[idx].gid = inode->i_gid;
1004 context->names[idx].rdev = inode->i_rdev;
1005 audit_inode_context(idx, inode);
1006 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1007 (strcmp(name, ".") != 0)) {
1008 context->names[idx].ino = (unsigned long)-1;
1009 context->names[idx].pino = inode->i_ino;
1011 context->names[idx].ino = inode->i_ino;
1012 context->names[idx].pino = (unsigned long)-1;
1017 * audit_inode_child - collect inode info for created/removed objects
1018 * @dname: inode's dentry name
1019 * @inode: inode being audited
1020 * @pino: inode number of dentry parent
1022 * For syscalls that create or remove filesystem objects, audit_inode
1023 * can only collect information for the filesystem object's parent.
1024 * This call updates the audit context with the child's information.
1025 * Syscalls that create a new filesystem object must be hooked after
1026 * the object is created. Syscalls that remove a filesystem object
1027 * must be hooked prior, in order to capture the target inode during
1028 * unsuccessful attempts.
1030 void __audit_inode_child(const char *dname, const struct inode *inode,
1034 struct audit_context *context = current->audit_context;
1036 if (!context->in_syscall)
1039 /* determine matching parent */
1041 for (idx = 0; idx < context->name_count; idx++)
1042 if (context->names[idx].pino == pino) {
1044 const char *name = context->names[idx].name;
1045 int dlen = strlen(dname);
1046 int nlen = name ? strlen(name) : 0;
1051 /* disregard trailing slashes */
1052 n = name + nlen - 1;
1053 while ((*n == '/') && (n > name))
1056 /* find last path component */
1060 else if (n > name) {
1067 if (strncmp(n, dname, dlen) == 0)
1068 goto update_context;
1071 /* catch-all in case match not found */
1072 idx = context->name_count++;
1073 context->names[idx].name = NULL;
1074 context->names[idx].pino = pino;
1076 context->ino_count++;
1081 context->names[idx].ino = inode->i_ino;
1082 context->names[idx].dev = inode->i_sb->s_dev;
1083 context->names[idx].mode = inode->i_mode;
1084 context->names[idx].uid = inode->i_uid;
1085 context->names[idx].gid = inode->i_gid;
1086 context->names[idx].rdev = inode->i_rdev;
1087 audit_inode_context(idx, inode);
1092 * auditsc_get_stamp - get local copies of audit_context values
1093 * @ctx: audit_context for the task
1094 * @t: timespec to store time recorded in the audit_context
1095 * @serial: serial value that is recorded in the audit_context
1097 * Also sets the context as auditable.
1099 void auditsc_get_stamp(struct audit_context *ctx,
1100 struct timespec *t, unsigned int *serial)
1103 ctx->serial = audit_serial();
1104 t->tv_sec = ctx->ctime.tv_sec;
1105 t->tv_nsec = ctx->ctime.tv_nsec;
1106 *serial = ctx->serial;
1111 * audit_set_loginuid - set a task's audit_context loginuid
1112 * @task: task whose audit context is being modified
1113 * @loginuid: loginuid value
1117 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1119 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1121 if (task->audit_context) {
1122 struct audit_buffer *ab;
1124 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1126 audit_log_format(ab, "login pid=%d uid=%u "
1127 "old auid=%u new auid=%u",
1128 task->pid, task->uid,
1129 task->audit_context->loginuid, loginuid);
1132 task->audit_context->loginuid = loginuid;
1138 * audit_get_loginuid - get the loginuid for an audit_context
1139 * @ctx: the audit_context
1141 * Returns the context's loginuid or -1 if @ctx is NULL.
1143 uid_t audit_get_loginuid(struct audit_context *ctx)
1145 return ctx ? ctx->loginuid : -1;
1148 static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1150 struct audit_context *context = current->audit_context;
1154 if (likely(!context))
1157 len = security_ipc_getsecurity(ipcp, NULL, 0);
1158 if (len == -EOPNOTSUPP)
1163 ctx = kmalloc(len, GFP_ATOMIC);
1167 len = security_ipc_getsecurity(ipcp, ctx, len);
1175 audit_panic("error in audit_ipc_context");
1181 * audit_ipc_perms - record audit data for ipc
1182 * @qbytes: msgq bytes
1183 * @uid: msgq user id
1184 * @gid: msgq group id
1185 * @mode: msgq mode (permissions)
1187 * Returns 0 for success or NULL context or < 0 on error.
1189 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
1191 struct audit_aux_data_ipcctl *ax;
1192 struct audit_context *context = current->audit_context;
1194 if (likely(!context))
1197 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1201 ax->qbytes = qbytes;
1205 ax->ctx = audit_ipc_context(ipcp);
1207 ax->d.type = AUDIT_IPC;
1208 ax->d.next = context->aux;
1209 context->aux = (void *)ax;
1214 * audit_socketcall - record audit data for sys_socketcall
1215 * @nargs: number of args
1218 * Returns 0 for success or NULL context or < 0 on error.
1220 int audit_socketcall(int nargs, unsigned long *args)
1222 struct audit_aux_data_socketcall *ax;
1223 struct audit_context *context = current->audit_context;
1225 if (likely(!context))
1228 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1233 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1235 ax->d.type = AUDIT_SOCKETCALL;
1236 ax->d.next = context->aux;
1237 context->aux = (void *)ax;
1242 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1243 * @len: data length in user space
1244 * @a: data address in kernel space
1246 * Returns 0 for success or NULL context or < 0 on error.
1248 int audit_sockaddr(int len, void *a)
1250 struct audit_aux_data_sockaddr *ax;
1251 struct audit_context *context = current->audit_context;
1253 if (likely(!context))
1256 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1261 memcpy(ax->a, a, len);
1263 ax->d.type = AUDIT_SOCKADDR;
1264 ax->d.next = context->aux;
1265 context->aux = (void *)ax;
1270 * audit_avc_path - record the granting or denial of permissions
1271 * @dentry: dentry to record
1272 * @mnt: mnt to record
1274 * Returns 0 for success or NULL context or < 0 on error.
1276 * Called from security/selinux/avc.c::avc_audit()
1278 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1280 struct audit_aux_data_path *ax;
1281 struct audit_context *context = current->audit_context;
1283 if (likely(!context))
1286 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1290 ax->dentry = dget(dentry);
1291 ax->mnt = mntget(mnt);
1293 ax->d.type = AUDIT_AVC_PATH;
1294 ax->d.next = context->aux;
1295 context->aux = (void *)ax;
1300 * audit_signal_info - record signal info for shutting down audit subsystem
1301 * @sig: signal value
1302 * @t: task being signaled
1304 * If the audit subsystem is being terminated, record the task (pid)
1305 * and uid that is doing that.
1307 void audit_signal_info(int sig, struct task_struct *t)
1309 extern pid_t audit_sig_pid;
1310 extern uid_t audit_sig_uid;
1312 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1313 if (sig == SIGTERM || sig == SIGHUP) {
1314 struct audit_context *ctx = current->audit_context;
1315 audit_sig_pid = current->pid;
1317 audit_sig_uid = ctx->loginuid;
1319 audit_sig_uid = current->uid;