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_rule *rule,
166 struct audit_context *ctx,
167 enum audit_state *state)
171 for (i = 0; i < rule->field_count; i++) {
172 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
173 u32 op = rule->fields[i] & AUDIT_OPERATORS;
174 u32 value = rule->values[i];
179 result = audit_comparator(tsk->pid, op, value);
182 result = audit_comparator(tsk->uid, op, value);
185 result = audit_comparator(tsk->euid, op, value);
188 result = audit_comparator(tsk->suid, op, value);
191 result = audit_comparator(tsk->fsuid, op, value);
194 result = audit_comparator(tsk->gid, op, value);
197 result = audit_comparator(tsk->egid, op, value);
200 result = audit_comparator(tsk->sgid, op, value);
203 result = audit_comparator(tsk->fsgid, op, value);
206 result = audit_comparator(tsk->personality, op, value);
210 result = audit_comparator(ctx->arch, op, value);
214 if (ctx && ctx->return_valid)
215 result = audit_comparator(ctx->return_code, op, value);
218 if (ctx && ctx->return_valid) {
220 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
222 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
227 for (j = 0; j < ctx->name_count; j++) {
228 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
237 for (j = 0; j < ctx->name_count; j++) {
238 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
247 for (j = 0; j < ctx->name_count; j++) {
248 if (audit_comparator(ctx->names[j].ino, op, value) ||
249 audit_comparator(ctx->names[j].pino, op, value)) {
259 result = audit_comparator(ctx->loginuid, op, value);
266 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
273 switch (rule->action) {
274 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
275 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
276 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
281 /* At process creation time, we can determine if system-call auditing is
282 * completely disabled for this task. Since we only have the task
283 * structure at this point, we can only check uid and gid.
285 static enum audit_state audit_filter_task(struct task_struct *tsk)
287 struct audit_entry *e;
288 enum audit_state state;
291 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
292 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
298 return AUDIT_BUILD_CONTEXT;
301 /* At syscall entry and exit time, this filter is called if the
302 * audit_state is not low enough that auditing cannot take place, but is
303 * also not high enough that we already know we have to write an audit
304 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
306 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
307 struct audit_context *ctx,
308 struct list_head *list)
310 struct audit_entry *e;
311 enum audit_state state;
313 if (audit_pid && tsk->tgid == audit_pid)
314 return AUDIT_DISABLED;
317 if (!list_empty(list)) {
318 int word = AUDIT_WORD(ctx->major);
319 int bit = AUDIT_BIT(ctx->major);
321 list_for_each_entry_rcu(e, list, list) {
322 if ((e->rule.mask[word] & bit) == bit
323 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
330 return AUDIT_BUILD_CONTEXT;
333 /* This should be called with task_lock() held. */
334 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
338 struct audit_context *context = tsk->audit_context;
340 if (likely(!context))
342 context->return_valid = return_valid;
343 context->return_code = return_code;
345 if (context->in_syscall && !context->auditable) {
346 enum audit_state state;
347 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
348 if (state == AUDIT_RECORD_CONTEXT)
349 context->auditable = 1;
352 context->pid = tsk->pid;
353 context->uid = tsk->uid;
354 context->gid = tsk->gid;
355 context->euid = tsk->euid;
356 context->suid = tsk->suid;
357 context->fsuid = tsk->fsuid;
358 context->egid = tsk->egid;
359 context->sgid = tsk->sgid;
360 context->fsgid = tsk->fsgid;
361 context->personality = tsk->personality;
362 tsk->audit_context = NULL;
366 static inline void audit_free_names(struct audit_context *context)
371 if (context->auditable
372 ||context->put_count + context->ino_count != context->name_count) {
373 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
374 " name_count=%d put_count=%d"
375 " ino_count=%d [NOT freeing]\n",
377 context->serial, context->major, context->in_syscall,
378 context->name_count, context->put_count,
380 for (i = 0; i < context->name_count; i++) {
381 printk(KERN_ERR "names[%d] = %p = %s\n", i,
382 context->names[i].name,
383 context->names[i].name ?: "(null)");
390 context->put_count = 0;
391 context->ino_count = 0;
394 for (i = 0; i < context->name_count; i++) {
395 char *p = context->names[i].ctx;
396 context->names[i].ctx = NULL;
398 if (context->names[i].name)
399 __putname(context->names[i].name);
401 context->name_count = 0;
405 mntput(context->pwdmnt);
407 context->pwdmnt = NULL;
410 static inline void audit_free_aux(struct audit_context *context)
412 struct audit_aux_data *aux;
414 while ((aux = context->aux)) {
415 if (aux->type == AUDIT_AVC_PATH) {
416 struct audit_aux_data_path *axi = (void *)aux;
420 if ( aux->type == AUDIT_IPC ) {
421 struct audit_aux_data_ipcctl *axi = (void *)aux;
426 context->aux = aux->next;
431 static inline void audit_zero_context(struct audit_context *context,
432 enum audit_state state)
434 uid_t loginuid = context->loginuid;
436 memset(context, 0, sizeof(*context));
437 context->state = state;
438 context->loginuid = loginuid;
441 static inline struct audit_context *audit_alloc_context(enum audit_state state)
443 struct audit_context *context;
445 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
447 audit_zero_context(context, state);
452 * audit_alloc - allocate an audit context block for a task
455 * Filter on the task information and allocate a per-task audit context
456 * if necessary. Doing so turns on system call auditing for the
457 * specified task. This is called from copy_process, so no lock is
460 int audit_alloc(struct task_struct *tsk)
462 struct audit_context *context;
463 enum audit_state state;
465 if (likely(!audit_enabled))
466 return 0; /* Return if not auditing. */
468 state = audit_filter_task(tsk);
469 if (likely(state == AUDIT_DISABLED))
472 if (!(context = audit_alloc_context(state))) {
473 audit_log_lost("out of memory in audit_alloc");
477 /* Preserve login uid */
478 context->loginuid = -1;
479 if (current->audit_context)
480 context->loginuid = current->audit_context->loginuid;
482 tsk->audit_context = context;
483 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
487 static inline void audit_free_context(struct audit_context *context)
489 struct audit_context *previous;
493 previous = context->previous;
494 if (previous || (count && count < 10)) {
496 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
497 " freeing multiple contexts (%d)\n",
498 context->serial, context->major,
499 context->name_count, count);
501 audit_free_names(context);
502 audit_free_aux(context);
507 printk(KERN_ERR "audit: freed %d contexts\n", count);
510 static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask)
515 len = security_getprocattr(current, "current", NULL, 0);
522 ctx = kmalloc(len, gfp_mask);
526 len = security_getprocattr(current, "current", ctx, len);
530 audit_log_format(ab, " subj=%s", ctx);
536 audit_panic("error in audit_log_task_context");
540 static void audit_log_task_info(struct audit_buffer *ab, gfp_t gfp_mask)
542 char name[sizeof(current->comm)];
543 struct mm_struct *mm = current->mm;
544 struct vm_area_struct *vma;
546 get_task_comm(name, current);
547 audit_log_format(ab, " comm=");
548 audit_log_untrustedstring(ab, name);
554 * this is brittle; all callers that pass GFP_ATOMIC will have
555 * NULL current->mm and we won't get here.
557 down_read(&mm->mmap_sem);
560 if ((vma->vm_flags & VM_EXECUTABLE) &&
562 audit_log_d_path(ab, "exe=",
563 vma->vm_file->f_dentry,
564 vma->vm_file->f_vfsmnt);
569 up_read(&mm->mmap_sem);
570 audit_log_task_context(ab, gfp_mask);
573 static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
576 struct audit_buffer *ab;
577 struct audit_aux_data *aux;
579 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
581 return; /* audit_panic has been called */
582 audit_log_format(ab, "arch=%x syscall=%d",
583 context->arch, context->major);
584 if (context->personality != PER_LINUX)
585 audit_log_format(ab, " per=%lx", context->personality);
586 if (context->return_valid)
587 audit_log_format(ab, " success=%s exit=%ld",
588 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
589 context->return_code);
591 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
592 " pid=%d auid=%u uid=%u gid=%u"
593 " euid=%u suid=%u fsuid=%u"
594 " egid=%u sgid=%u fsgid=%u",
604 context->euid, context->suid, context->fsuid,
605 context->egid, context->sgid, context->fsgid);
606 audit_log_task_info(ab, gfp_mask);
609 for (aux = context->aux; aux; aux = aux->next) {
611 ab = audit_log_start(context, gfp_mask, aux->type);
613 continue; /* audit_panic has been called */
617 struct audit_aux_data_ipcctl *axi = (void *)aux;
619 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
620 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
623 case AUDIT_SOCKETCALL: {
625 struct audit_aux_data_socketcall *axs = (void *)aux;
626 audit_log_format(ab, "nargs=%d", axs->nargs);
627 for (i=0; i<axs->nargs; i++)
628 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
631 case AUDIT_SOCKADDR: {
632 struct audit_aux_data_sockaddr *axs = (void *)aux;
634 audit_log_format(ab, "saddr=");
635 audit_log_hex(ab, axs->a, axs->len);
638 case AUDIT_AVC_PATH: {
639 struct audit_aux_data_path *axi = (void *)aux;
640 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
647 if (context->pwd && context->pwdmnt) {
648 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
650 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
654 for (i = 0; i < context->name_count; i++) {
655 unsigned long ino = context->names[i].ino;
656 unsigned long pino = context->names[i].pino;
658 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
660 continue; /* audit_panic has been called */
662 audit_log_format(ab, "item=%d", i);
664 audit_log_format(ab, " name=");
665 if (context->names[i].name)
666 audit_log_untrustedstring(ab, context->names[i].name);
668 audit_log_format(ab, "(null)");
670 if (pino != (unsigned long)-1)
671 audit_log_format(ab, " parent=%lu", pino);
672 if (ino != (unsigned long)-1)
673 audit_log_format(ab, " inode=%lu", ino);
674 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
675 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
676 " ouid=%u ogid=%u rdev=%02x:%02x",
677 MAJOR(context->names[i].dev),
678 MINOR(context->names[i].dev),
679 context->names[i].mode,
680 context->names[i].uid,
681 context->names[i].gid,
682 MAJOR(context->names[i].rdev),
683 MINOR(context->names[i].rdev));
684 if (context->names[i].ctx) {
685 audit_log_format(ab, " obj=%s",
686 context->names[i].ctx);
694 * audit_free - free a per-task audit context
695 * @tsk: task whose audit context block to free
697 * Called from copy_process and __put_task_struct.
699 void audit_free(struct task_struct *tsk)
701 struct audit_context *context;
704 context = audit_get_context(tsk, 0, 0);
707 if (likely(!context))
710 /* Check for system calls that do not go through the exit
711 * function (e.g., exit_group), then free context block.
712 * We use GFP_ATOMIC here because we might be doing this
713 * in the context of the idle thread */
714 if (context->in_syscall && context->auditable)
715 audit_log_exit(context, GFP_ATOMIC);
717 audit_free_context(context);
721 * audit_syscall_entry - fill in an audit record at syscall entry
722 * @tsk: task being audited
723 * @arch: architecture type
724 * @major: major syscall type (function)
725 * @a1: additional syscall register 1
726 * @a2: additional syscall register 2
727 * @a3: additional syscall register 3
728 * @a4: additional syscall register 4
730 * Fill in audit context at syscall entry. This only happens if the
731 * audit context was created when the task was created and the state or
732 * filters demand the audit context be built. If the state from the
733 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
734 * then the record will be written at syscall exit time (otherwise, it
735 * will only be written if another part of the kernel requests that it
738 void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
739 unsigned long a1, unsigned long a2,
740 unsigned long a3, unsigned long a4)
742 struct audit_context *context = tsk->audit_context;
743 enum audit_state state;
748 * This happens only on certain architectures that make system
749 * calls in kernel_thread via the entry.S interface, instead of
750 * with direct calls. (If you are porting to a new
751 * architecture, hitting this condition can indicate that you
752 * got the _exit/_leave calls backward in entry.S.)
756 * ppc64 yes (see arch/ppc64/kernel/misc.S)
758 * This also happens with vm86 emulation in a non-nested manner
759 * (entries without exits), so this case must be caught.
761 if (context->in_syscall) {
762 struct audit_context *newctx;
766 "audit(:%d) pid=%d in syscall=%d;"
767 " entering syscall=%d\n",
768 context->serial, tsk->pid, context->major, major);
770 newctx = audit_alloc_context(context->state);
772 newctx->previous = context;
774 tsk->audit_context = newctx;
776 /* If we can't alloc a new context, the best we
777 * can do is to leak memory (any pending putname
778 * will be lost). The only other alternative is
779 * to abandon auditing. */
780 audit_zero_context(context, context->state);
783 BUG_ON(context->in_syscall || context->name_count);
788 context->arch = arch;
789 context->major = major;
790 context->argv[0] = a1;
791 context->argv[1] = a2;
792 context->argv[2] = a3;
793 context->argv[3] = a4;
795 state = context->state;
796 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
797 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
798 if (likely(state == AUDIT_DISABLED))
802 context->ctime = CURRENT_TIME;
803 context->in_syscall = 1;
804 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
808 * audit_syscall_exit - deallocate audit context after a system call
809 * @tsk: task being audited
810 * @valid: success/failure flag
811 * @return_code: syscall return value
813 * Tear down after system call. If the audit context has been marked as
814 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
815 * filtering, or because some other part of the kernel write an audit
816 * message), then write out the syscall information. In call cases,
817 * free the names stored from getname().
819 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
821 struct audit_context *context;
823 get_task_struct(tsk);
825 context = audit_get_context(tsk, valid, return_code);
828 /* Not having a context here is ok, since the parent may have
829 * called __put_task_struct. */
830 if (likely(!context))
833 if (context->in_syscall && context->auditable)
834 audit_log_exit(context, GFP_KERNEL);
836 context->in_syscall = 0;
837 context->auditable = 0;
839 if (context->previous) {
840 struct audit_context *new_context = context->previous;
841 context->previous = NULL;
842 audit_free_context(context);
843 tsk->audit_context = new_context;
845 audit_free_names(context);
846 audit_free_aux(context);
847 tsk->audit_context = context;
850 put_task_struct(tsk);
854 * audit_getname - add a name to the list
857 * Add a name to the list of audit names for this context.
858 * Called from fs/namei.c:getname().
860 void audit_getname(const char *name)
862 struct audit_context *context = current->audit_context;
864 if (!context || IS_ERR(name) || !name)
867 if (!context->in_syscall) {
869 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
870 __FILE__, __LINE__, context->serial, name);
875 BUG_ON(context->name_count >= AUDIT_NAMES);
876 context->names[context->name_count].name = name;
877 context->names[context->name_count].ino = (unsigned long)-1;
878 ++context->name_count;
880 read_lock(¤t->fs->lock);
881 context->pwd = dget(current->fs->pwd);
882 context->pwdmnt = mntget(current->fs->pwdmnt);
883 read_unlock(¤t->fs->lock);
888 /* audit_putname - intercept a putname request
889 * @name: name to intercept and delay for putname
891 * If we have stored the name from getname in the audit context,
892 * then we delay the putname until syscall exit.
893 * Called from include/linux/fs.h:putname().
895 void audit_putname(const char *name)
897 struct audit_context *context = current->audit_context;
900 if (!context->in_syscall) {
902 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
903 __FILE__, __LINE__, context->serial, name);
904 if (context->name_count) {
906 for (i = 0; i < context->name_count; i++)
907 printk(KERN_ERR "name[%d] = %p = %s\n", i,
908 context->names[i].name,
909 context->names[i].name ?: "(null)");
916 ++context->put_count;
917 if (context->put_count > context->name_count) {
918 printk(KERN_ERR "%s:%d(:%d): major=%d"
919 " in_syscall=%d putname(%p) name_count=%d"
922 context->serial, context->major,
923 context->in_syscall, name, context->name_count,
931 void audit_inode_context(int idx, const struct inode *inode)
933 struct audit_context *context = current->audit_context;
934 const char *suffix = security_inode_xattr_getsuffix();
941 len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
942 if (len == -EOPNOTSUPP)
947 ctx = kmalloc(len, GFP_KERNEL);
951 len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
955 kfree(context->names[idx].ctx);
956 context->names[idx].ctx = ctx;
962 audit_panic("error in audit_inode_context");
969 * audit_inode - store the inode and device from a lookup
970 * @name: name being audited
971 * @inode: inode being audited
972 * @flags: lookup flags (as used in path_lookup())
974 * Called from fs/namei.c:path_lookup().
976 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
979 struct audit_context *context = current->audit_context;
981 if (!context->in_syscall)
983 if (context->name_count
984 && context->names[context->name_count-1].name
985 && context->names[context->name_count-1].name == name)
986 idx = context->name_count - 1;
987 else if (context->name_count > 1
988 && context->names[context->name_count-2].name
989 && context->names[context->name_count-2].name == name)
990 idx = context->name_count - 2;
992 /* FIXME: how much do we care about inodes that have no
993 * associated name? */
994 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
996 idx = context->name_count++;
997 context->names[idx].name = NULL;
999 ++context->ino_count;
1002 context->names[idx].dev = inode->i_sb->s_dev;
1003 context->names[idx].mode = inode->i_mode;
1004 context->names[idx].uid = inode->i_uid;
1005 context->names[idx].gid = inode->i_gid;
1006 context->names[idx].rdev = inode->i_rdev;
1007 audit_inode_context(idx, inode);
1008 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1009 (strcmp(name, ".") != 0)) {
1010 context->names[idx].ino = (unsigned long)-1;
1011 context->names[idx].pino = inode->i_ino;
1013 context->names[idx].ino = inode->i_ino;
1014 context->names[idx].pino = (unsigned long)-1;
1019 * audit_inode_child - collect inode info for created/removed objects
1020 * @dname: inode's dentry name
1021 * @inode: inode being audited
1022 * @pino: inode number of dentry parent
1024 * For syscalls that create or remove filesystem objects, audit_inode
1025 * can only collect information for the filesystem object's parent.
1026 * This call updates the audit context with the child's information.
1027 * Syscalls that create a new filesystem object must be hooked after
1028 * the object is created. Syscalls that remove a filesystem object
1029 * must be hooked prior, in order to capture the target inode during
1030 * unsuccessful attempts.
1032 void __audit_inode_child(const char *dname, const struct inode *inode,
1036 struct audit_context *context = current->audit_context;
1038 if (!context->in_syscall)
1041 /* determine matching parent */
1043 for (idx = 0; idx < context->name_count; idx++)
1044 if (context->names[idx].pino == pino) {
1046 const char *name = context->names[idx].name;
1047 int dlen = strlen(dname);
1048 int nlen = name ? strlen(name) : 0;
1053 /* disregard trailing slashes */
1054 n = name + nlen - 1;
1055 while ((*n == '/') && (n > name))
1058 /* find last path component */
1062 else if (n > name) {
1069 if (strncmp(n, dname, dlen) == 0)
1070 goto update_context;
1073 /* catch-all in case match not found */
1074 idx = context->name_count++;
1075 context->names[idx].name = NULL;
1076 context->names[idx].pino = pino;
1078 context->ino_count++;
1083 context->names[idx].ino = inode->i_ino;
1084 context->names[idx].dev = inode->i_sb->s_dev;
1085 context->names[idx].mode = inode->i_mode;
1086 context->names[idx].uid = inode->i_uid;
1087 context->names[idx].gid = inode->i_gid;
1088 context->names[idx].rdev = inode->i_rdev;
1089 audit_inode_context(idx, inode);
1094 * auditsc_get_stamp - get local copies of audit_context values
1095 * @ctx: audit_context for the task
1096 * @t: timespec to store time recorded in the audit_context
1097 * @serial: serial value that is recorded in the audit_context
1099 * Also sets the context as auditable.
1101 void auditsc_get_stamp(struct audit_context *ctx,
1102 struct timespec *t, unsigned int *serial)
1105 ctx->serial = audit_serial();
1106 t->tv_sec = ctx->ctime.tv_sec;
1107 t->tv_nsec = ctx->ctime.tv_nsec;
1108 *serial = ctx->serial;
1113 * audit_set_loginuid - set a task's audit_context loginuid
1114 * @task: task whose audit context is being modified
1115 * @loginuid: loginuid value
1119 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1121 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1123 if (task->audit_context) {
1124 struct audit_buffer *ab;
1126 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1128 audit_log_format(ab, "login pid=%d uid=%u "
1129 "old auid=%u new auid=%u",
1130 task->pid, task->uid,
1131 task->audit_context->loginuid, loginuid);
1134 task->audit_context->loginuid = loginuid;
1140 * audit_get_loginuid - get the loginuid for an audit_context
1141 * @ctx: the audit_context
1143 * Returns the context's loginuid or -1 if @ctx is NULL.
1145 uid_t audit_get_loginuid(struct audit_context *ctx)
1147 return ctx ? ctx->loginuid : -1;
1150 static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1152 struct audit_context *context = current->audit_context;
1156 if (likely(!context))
1159 len = security_ipc_getsecurity(ipcp, NULL, 0);
1160 if (len == -EOPNOTSUPP)
1165 ctx = kmalloc(len, GFP_ATOMIC);
1169 len = security_ipc_getsecurity(ipcp, ctx, len);
1177 audit_panic("error in audit_ipc_context");
1183 * audit_ipc_perms - record audit data for ipc
1184 * @qbytes: msgq bytes
1185 * @uid: msgq user id
1186 * @gid: msgq group id
1187 * @mode: msgq mode (permissions)
1189 * Returns 0 for success or NULL context or < 0 on error.
1191 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
1193 struct audit_aux_data_ipcctl *ax;
1194 struct audit_context *context = current->audit_context;
1196 if (likely(!context))
1199 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1203 ax->qbytes = qbytes;
1207 ax->ctx = audit_ipc_context(ipcp);
1209 ax->d.type = AUDIT_IPC;
1210 ax->d.next = context->aux;
1211 context->aux = (void *)ax;
1216 * audit_socketcall - record audit data for sys_socketcall
1217 * @nargs: number of args
1220 * Returns 0 for success or NULL context or < 0 on error.
1222 int audit_socketcall(int nargs, unsigned long *args)
1224 struct audit_aux_data_socketcall *ax;
1225 struct audit_context *context = current->audit_context;
1227 if (likely(!context))
1230 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1235 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1237 ax->d.type = AUDIT_SOCKETCALL;
1238 ax->d.next = context->aux;
1239 context->aux = (void *)ax;
1244 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1245 * @len: data length in user space
1246 * @a: data address in kernel space
1248 * Returns 0 for success or NULL context or < 0 on error.
1250 int audit_sockaddr(int len, void *a)
1252 struct audit_aux_data_sockaddr *ax;
1253 struct audit_context *context = current->audit_context;
1255 if (likely(!context))
1258 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1263 memcpy(ax->a, a, len);
1265 ax->d.type = AUDIT_SOCKADDR;
1266 ax->d.next = context->aux;
1267 context->aux = (void *)ax;
1272 * audit_avc_path - record the granting or denial of permissions
1273 * @dentry: dentry to record
1274 * @mnt: mnt to record
1276 * Returns 0 for success or NULL context or < 0 on error.
1278 * Called from security/selinux/avc.c::avc_audit()
1280 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1282 struct audit_aux_data_path *ax;
1283 struct audit_context *context = current->audit_context;
1285 if (likely(!context))
1288 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1292 ax->dentry = dget(dentry);
1293 ax->mnt = mntget(mnt);
1295 ax->d.type = AUDIT_AVC_PATH;
1296 ax->d.next = context->aux;
1297 context->aux = (void *)ax;
1302 * audit_signal_info - record signal info for shutting down audit subsystem
1303 * @sig: signal value
1304 * @t: task being signaled
1306 * If the audit subsystem is being terminated, record the task (pid)
1307 * and uid that is doing that.
1309 void audit_signal_info(int sig, struct task_struct *t)
1311 extern pid_t audit_sig_pid;
1312 extern uid_t audit_sig_uid;
1314 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1315 if (sig == SIGTERM || sig == SIGHUP) {
1316 struct audit_context *ctx = current->audit_context;
1317 audit_sig_pid = current->pid;
1319 audit_sig_uid = ctx->loginuid;
1321 audit_sig_uid = current->uid;