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>
65 extern struct list_head audit_filter_list[];
67 /* No syscall auditing will take place unless audit_enabled != 0. */
68 extern int audit_enabled;
70 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
71 * for saving names from getname(). */
72 #define AUDIT_NAMES 20
74 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
75 * audit_context from being used for nameless inodes from
77 #define AUDIT_NAMES_RESERVED 7
79 /* When fs/namei.c:getname() is called, we store the pointer in name and
80 * we don't let putname() free it (instead we free all of the saved
81 * pointers at syscall exit time).
83 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
96 struct audit_aux_data {
97 struct audit_aux_data *next;
101 #define AUDIT_AUX_IPCPERM 0
103 struct audit_aux_data_ipcctl {
104 struct audit_aux_data d;
106 unsigned long qbytes;
113 struct audit_aux_data_socketcall {
114 struct audit_aux_data d;
116 unsigned long args[0];
119 struct audit_aux_data_sockaddr {
120 struct audit_aux_data d;
125 struct audit_aux_data_path {
126 struct audit_aux_data d;
127 struct dentry *dentry;
128 struct vfsmount *mnt;
131 /* The per-task audit context. */
132 struct audit_context {
133 int in_syscall; /* 1 if task is in a syscall */
134 enum audit_state state;
135 unsigned int serial; /* serial number for record */
136 struct timespec ctime; /* time of syscall entry */
137 uid_t loginuid; /* login uid (identity) */
138 int major; /* syscall number */
139 unsigned long argv[4]; /* syscall arguments */
140 int return_valid; /* return code is valid */
141 long return_code;/* syscall return code */
142 int auditable; /* 1 if record should be written */
144 struct audit_names names[AUDIT_NAMES];
146 struct vfsmount * pwdmnt;
147 struct audit_context *previous; /* For nested syscalls */
148 struct audit_aux_data *aux;
150 /* Save things to print about task_struct */
152 uid_t uid, euid, suid, fsuid;
153 gid_t gid, egid, sgid, fsgid;
154 unsigned long personality;
164 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
166 static int audit_filter_rules(struct task_struct *tsk,
167 struct audit_krule *rule,
168 struct audit_context *ctx,
169 enum audit_state *state)
174 selinux_task_ctxid(tsk, &sid);
176 for (i = 0; i < rule->field_count; i++) {
177 struct audit_field *f = &rule->fields[i];
182 result = audit_comparator(tsk->pid, f->op, f->val);
185 result = audit_comparator(tsk->uid, f->op, f->val);
188 result = audit_comparator(tsk->euid, f->op, f->val);
191 result = audit_comparator(tsk->suid, f->op, f->val);
194 result = audit_comparator(tsk->fsuid, f->op, f->val);
197 result = audit_comparator(tsk->gid, f->op, f->val);
200 result = audit_comparator(tsk->egid, f->op, f->val);
203 result = audit_comparator(tsk->sgid, f->op, f->val);
206 result = audit_comparator(tsk->fsgid, f->op, f->val);
209 result = audit_comparator(tsk->personality, f->op, f->val);
213 result = audit_comparator(ctx->arch, f->op, f->val);
217 if (ctx && ctx->return_valid)
218 result = audit_comparator(ctx->return_code, f->op, f->val);
221 if (ctx && ctx->return_valid) {
223 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
225 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
230 for (j = 0; j < ctx->name_count; j++) {
231 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
240 for (j = 0; j < ctx->name_count; j++) {
241 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
250 for (j = 0; j < ctx->name_count; j++) {
251 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
252 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
262 result = audit_comparator(ctx->loginuid, f->op, f->val);
269 /* NOTE: this may return negative values indicating
270 a temporary error. We simply treat this as a
271 match for now to avoid losing information that
272 may be wanted. An error message will also be
275 result = selinux_audit_rule_match(sid, f->type,
285 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
292 switch (rule->action) {
293 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
294 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
295 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
300 /* At process creation time, we can determine if system-call auditing is
301 * completely disabled for this task. Since we only have the task
302 * structure at this point, we can only check uid and gid.
304 static enum audit_state audit_filter_task(struct task_struct *tsk)
306 struct audit_entry *e;
307 enum audit_state state;
310 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
311 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
317 return AUDIT_BUILD_CONTEXT;
320 /* At syscall entry and exit time, this filter is called if the
321 * audit_state is not low enough that auditing cannot take place, but is
322 * also not high enough that we already know we have to write an audit
323 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
325 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
326 struct audit_context *ctx,
327 struct list_head *list)
329 struct audit_entry *e;
330 enum audit_state state;
332 if (audit_pid && tsk->tgid == audit_pid)
333 return AUDIT_DISABLED;
336 if (!list_empty(list)) {
337 int word = AUDIT_WORD(ctx->major);
338 int bit = AUDIT_BIT(ctx->major);
340 list_for_each_entry_rcu(e, list, list) {
341 if ((e->rule.mask[word] & bit) == bit
342 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
349 return AUDIT_BUILD_CONTEXT;
352 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
356 struct audit_context *context = tsk->audit_context;
358 if (likely(!context))
360 context->return_valid = return_valid;
361 context->return_code = return_code;
363 if (context->in_syscall && !context->auditable) {
364 enum audit_state state;
365 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
366 if (state == AUDIT_RECORD_CONTEXT)
367 context->auditable = 1;
370 context->pid = tsk->pid;
371 context->uid = tsk->uid;
372 context->gid = tsk->gid;
373 context->euid = tsk->euid;
374 context->suid = tsk->suid;
375 context->fsuid = tsk->fsuid;
376 context->egid = tsk->egid;
377 context->sgid = tsk->sgid;
378 context->fsgid = tsk->fsgid;
379 context->personality = tsk->personality;
380 tsk->audit_context = NULL;
384 static inline void audit_free_names(struct audit_context *context)
389 if (context->auditable
390 ||context->put_count + context->ino_count != context->name_count) {
391 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
392 " name_count=%d put_count=%d"
393 " ino_count=%d [NOT freeing]\n",
395 context->serial, context->major, context->in_syscall,
396 context->name_count, context->put_count,
398 for (i = 0; i < context->name_count; i++) {
399 printk(KERN_ERR "names[%d] = %p = %s\n", i,
400 context->names[i].name,
401 context->names[i].name ?: "(null)");
408 context->put_count = 0;
409 context->ino_count = 0;
412 for (i = 0; i < context->name_count; i++) {
413 if (context->names[i].name)
414 __putname(context->names[i].name);
416 context->name_count = 0;
420 mntput(context->pwdmnt);
422 context->pwdmnt = NULL;
425 static inline void audit_free_aux(struct audit_context *context)
427 struct audit_aux_data *aux;
429 while ((aux = context->aux)) {
430 if (aux->type == AUDIT_AVC_PATH) {
431 struct audit_aux_data_path *axi = (void *)aux;
435 if ( aux->type == AUDIT_IPC ) {
436 struct audit_aux_data_ipcctl *axi = (void *)aux;
441 context->aux = aux->next;
446 static inline void audit_zero_context(struct audit_context *context,
447 enum audit_state state)
449 uid_t loginuid = context->loginuid;
451 memset(context, 0, sizeof(*context));
452 context->state = state;
453 context->loginuid = loginuid;
456 static inline struct audit_context *audit_alloc_context(enum audit_state state)
458 struct audit_context *context;
460 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
462 audit_zero_context(context, state);
467 * audit_alloc - allocate an audit context block for a task
470 * Filter on the task information and allocate a per-task audit context
471 * if necessary. Doing so turns on system call auditing for the
472 * specified task. This is called from copy_process, so no lock is
475 int audit_alloc(struct task_struct *tsk)
477 struct audit_context *context;
478 enum audit_state state;
480 if (likely(!audit_enabled))
481 return 0; /* Return if not auditing. */
483 state = audit_filter_task(tsk);
484 if (likely(state == AUDIT_DISABLED))
487 if (!(context = audit_alloc_context(state))) {
488 audit_log_lost("out of memory in audit_alloc");
492 /* Preserve login uid */
493 context->loginuid = -1;
494 if (current->audit_context)
495 context->loginuid = current->audit_context->loginuid;
497 tsk->audit_context = context;
498 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
502 static inline void audit_free_context(struct audit_context *context)
504 struct audit_context *previous;
508 previous = context->previous;
509 if (previous || (count && count < 10)) {
511 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
512 " freeing multiple contexts (%d)\n",
513 context->serial, context->major,
514 context->name_count, count);
516 audit_free_names(context);
517 audit_free_aux(context);
522 printk(KERN_ERR "audit: freed %d contexts\n", count);
525 static void audit_log_task_context(struct audit_buffer *ab)
530 len = security_getprocattr(current, "current", NULL, 0);
537 ctx = kmalloc(len, GFP_KERNEL);
541 len = security_getprocattr(current, "current", ctx, len);
545 audit_log_format(ab, " subj=%s", ctx);
551 audit_panic("error in audit_log_task_context");
555 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
557 char name[sizeof(tsk->comm)];
558 struct mm_struct *mm = tsk->mm;
559 struct vm_area_struct *vma;
563 get_task_comm(name, tsk);
564 audit_log_format(ab, " comm=");
565 audit_log_untrustedstring(ab, name);
568 down_read(&mm->mmap_sem);
571 if ((vma->vm_flags & VM_EXECUTABLE) &&
573 audit_log_d_path(ab, "exe=",
574 vma->vm_file->f_dentry,
575 vma->vm_file->f_vfsmnt);
580 up_read(&mm->mmap_sem);
582 audit_log_task_context(ab);
585 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
588 struct audit_buffer *ab;
589 struct audit_aux_data *aux;
594 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
596 return; /* audit_panic has been called */
597 audit_log_format(ab, "arch=%x syscall=%d",
598 context->arch, context->major);
599 if (context->personality != PER_LINUX)
600 audit_log_format(ab, " per=%lx", context->personality);
601 if (context->return_valid)
602 audit_log_format(ab, " success=%s exit=%ld",
603 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
604 context->return_code);
605 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
606 tty = tsk->signal->tty->name;
610 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
611 " pid=%d auid=%u uid=%u gid=%u"
612 " euid=%u suid=%u fsuid=%u"
613 " egid=%u sgid=%u fsgid=%u tty=%s",
623 context->euid, context->suid, context->fsuid,
624 context->egid, context->sgid, context->fsgid, tty);
625 audit_log_task_info(ab, tsk);
628 for (aux = context->aux; aux; aux = aux->next) {
630 ab = audit_log_start(context, GFP_KERNEL, aux->type);
632 continue; /* audit_panic has been called */
636 struct audit_aux_data_ipcctl *axi = (void *)aux;
638 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
639 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
642 case AUDIT_SOCKETCALL: {
644 struct audit_aux_data_socketcall *axs = (void *)aux;
645 audit_log_format(ab, "nargs=%d", axs->nargs);
646 for (i=0; i<axs->nargs; i++)
647 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
650 case AUDIT_SOCKADDR: {
651 struct audit_aux_data_sockaddr *axs = (void *)aux;
653 audit_log_format(ab, "saddr=");
654 audit_log_hex(ab, axs->a, axs->len);
657 case AUDIT_AVC_PATH: {
658 struct audit_aux_data_path *axi = (void *)aux;
659 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
666 if (context->pwd && context->pwdmnt) {
667 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
669 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
673 for (i = 0; i < context->name_count; i++) {
675 unsigned long ino = context->names[i].ino;
676 unsigned long pino = context->names[i].pino;
678 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
680 continue; /* audit_panic has been called */
682 audit_log_format(ab, "item=%d", i);
684 audit_log_format(ab, " name=");
685 if (context->names[i].name)
686 audit_log_untrustedstring(ab, context->names[i].name);
688 audit_log_format(ab, "(null)");
690 if (pino != (unsigned long)-1)
691 audit_log_format(ab, " parent=%lu", pino);
692 if (ino != (unsigned long)-1)
693 audit_log_format(ab, " inode=%lu", ino);
694 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
695 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
696 " ouid=%u ogid=%u rdev=%02x:%02x",
697 MAJOR(context->names[i].dev),
698 MINOR(context->names[i].dev),
699 context->names[i].mode,
700 context->names[i].uid,
701 context->names[i].gid,
702 MAJOR(context->names[i].rdev),
703 MINOR(context->names[i].rdev));
704 if (context->names[i].osid != 0) {
707 if (selinux_ctxid_to_string(
708 context->names[i].osid, &ctx, &len)) {
709 audit_log_format(ab, " obj=%u",
710 context->names[i].osid);
713 audit_log_format(ab, " obj=%s", ctx);
719 audit_panic("error converting sid to string");
724 * audit_free - free a per-task audit context
725 * @tsk: task whose audit context block to free
727 * Called from copy_process and do_exit
729 void audit_free(struct task_struct *tsk)
731 struct audit_context *context;
733 context = audit_get_context(tsk, 0, 0);
734 if (likely(!context))
737 /* Check for system calls that do not go through the exit
738 * function (e.g., exit_group), then free context block.
739 * We use GFP_ATOMIC here because we might be doing this
740 * in the context of the idle thread */
741 /* that can happen only if we are called from do_exit() */
742 if (context->in_syscall && context->auditable)
743 audit_log_exit(context, tsk);
745 audit_free_context(context);
749 * audit_syscall_entry - fill in an audit record at syscall entry
750 * @tsk: task being audited
751 * @arch: architecture type
752 * @major: major syscall type (function)
753 * @a1: additional syscall register 1
754 * @a2: additional syscall register 2
755 * @a3: additional syscall register 3
756 * @a4: additional syscall register 4
758 * Fill in audit context at syscall entry. This only happens if the
759 * audit context was created when the task was created and the state or
760 * filters demand the audit context be built. If the state from the
761 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
762 * then the record will be written at syscall exit time (otherwise, it
763 * will only be written if another part of the kernel requests that it
766 void audit_syscall_entry(int arch, int major,
767 unsigned long a1, unsigned long a2,
768 unsigned long a3, unsigned long a4)
770 struct task_struct *tsk = current;
771 struct audit_context *context = tsk->audit_context;
772 enum audit_state state;
777 * This happens only on certain architectures that make system
778 * calls in kernel_thread via the entry.S interface, instead of
779 * with direct calls. (If you are porting to a new
780 * architecture, hitting this condition can indicate that you
781 * got the _exit/_leave calls backward in entry.S.)
785 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
787 * This also happens with vm86 emulation in a non-nested manner
788 * (entries without exits), so this case must be caught.
790 if (context->in_syscall) {
791 struct audit_context *newctx;
795 "audit(:%d) pid=%d in syscall=%d;"
796 " entering syscall=%d\n",
797 context->serial, tsk->pid, context->major, major);
799 newctx = audit_alloc_context(context->state);
801 newctx->previous = context;
803 tsk->audit_context = newctx;
805 /* If we can't alloc a new context, the best we
806 * can do is to leak memory (any pending putname
807 * will be lost). The only other alternative is
808 * to abandon auditing. */
809 audit_zero_context(context, context->state);
812 BUG_ON(context->in_syscall || context->name_count);
817 context->arch = arch;
818 context->major = major;
819 context->argv[0] = a1;
820 context->argv[1] = a2;
821 context->argv[2] = a3;
822 context->argv[3] = a4;
824 state = context->state;
825 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
826 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
827 if (likely(state == AUDIT_DISABLED))
831 context->ctime = CURRENT_TIME;
832 context->in_syscall = 1;
833 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
837 * audit_syscall_exit - deallocate audit context after a system call
838 * @tsk: task being audited
839 * @valid: success/failure flag
840 * @return_code: syscall return value
842 * Tear down after system call. If the audit context has been marked as
843 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
844 * filtering, or because some other part of the kernel write an audit
845 * message), then write out the syscall information. In call cases,
846 * free the names stored from getname().
848 void audit_syscall_exit(int valid, long return_code)
850 struct task_struct *tsk = current;
851 struct audit_context *context;
853 context = audit_get_context(tsk, valid, return_code);
855 if (likely(!context))
858 if (context->in_syscall && context->auditable)
859 audit_log_exit(context, tsk);
861 context->in_syscall = 0;
862 context->auditable = 0;
864 if (context->previous) {
865 struct audit_context *new_context = context->previous;
866 context->previous = NULL;
867 audit_free_context(context);
868 tsk->audit_context = new_context;
870 audit_free_names(context);
871 audit_free_aux(context);
872 tsk->audit_context = context;
877 * audit_getname - add a name to the list
880 * Add a name to the list of audit names for this context.
881 * Called from fs/namei.c:getname().
883 void audit_getname(const char *name)
885 struct audit_context *context = current->audit_context;
887 if (!context || IS_ERR(name) || !name)
890 if (!context->in_syscall) {
892 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
893 __FILE__, __LINE__, context->serial, name);
898 BUG_ON(context->name_count >= AUDIT_NAMES);
899 context->names[context->name_count].name = name;
900 context->names[context->name_count].ino = (unsigned long)-1;
901 ++context->name_count;
903 read_lock(¤t->fs->lock);
904 context->pwd = dget(current->fs->pwd);
905 context->pwdmnt = mntget(current->fs->pwdmnt);
906 read_unlock(¤t->fs->lock);
911 /* audit_putname - intercept a putname request
912 * @name: name to intercept and delay for putname
914 * If we have stored the name from getname in the audit context,
915 * then we delay the putname until syscall exit.
916 * Called from include/linux/fs.h:putname().
918 void audit_putname(const char *name)
920 struct audit_context *context = current->audit_context;
923 if (!context->in_syscall) {
925 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
926 __FILE__, __LINE__, context->serial, name);
927 if (context->name_count) {
929 for (i = 0; i < context->name_count; i++)
930 printk(KERN_ERR "name[%d] = %p = %s\n", i,
931 context->names[i].name,
932 context->names[i].name ?: "(null)");
939 ++context->put_count;
940 if (context->put_count > context->name_count) {
941 printk(KERN_ERR "%s:%d(:%d): major=%d"
942 " in_syscall=%d putname(%p) name_count=%d"
945 context->serial, context->major,
946 context->in_syscall, name, context->name_count,
954 void audit_inode_context(int idx, const struct inode *inode)
956 struct audit_context *context = current->audit_context;
958 selinux_get_inode_sid(inode, &context->names[idx].osid);
963 * audit_inode - store the inode and device from a lookup
964 * @name: name being audited
965 * @inode: inode being audited
966 * @flags: lookup flags (as used in path_lookup())
968 * Called from fs/namei.c:path_lookup().
970 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
973 struct audit_context *context = current->audit_context;
975 if (!context->in_syscall)
977 if (context->name_count
978 && context->names[context->name_count-1].name
979 && context->names[context->name_count-1].name == name)
980 idx = context->name_count - 1;
981 else if (context->name_count > 1
982 && context->names[context->name_count-2].name
983 && context->names[context->name_count-2].name == name)
984 idx = context->name_count - 2;
986 /* FIXME: how much do we care about inodes that have no
987 * associated name? */
988 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
990 idx = context->name_count++;
991 context->names[idx].name = NULL;
993 ++context->ino_count;
996 context->names[idx].dev = inode->i_sb->s_dev;
997 context->names[idx].mode = inode->i_mode;
998 context->names[idx].uid = inode->i_uid;
999 context->names[idx].gid = inode->i_gid;
1000 context->names[idx].rdev = inode->i_rdev;
1001 audit_inode_context(idx, inode);
1002 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1003 (strcmp(name, ".") != 0)) {
1004 context->names[idx].ino = (unsigned long)-1;
1005 context->names[idx].pino = inode->i_ino;
1007 context->names[idx].ino = inode->i_ino;
1008 context->names[idx].pino = (unsigned long)-1;
1013 * audit_inode_child - collect inode info for created/removed objects
1014 * @dname: inode's dentry name
1015 * @inode: inode being audited
1016 * @pino: inode number of dentry parent
1018 * For syscalls that create or remove filesystem objects, audit_inode
1019 * can only collect information for the filesystem object's parent.
1020 * This call updates the audit context with the child's information.
1021 * Syscalls that create a new filesystem object must be hooked after
1022 * the object is created. Syscalls that remove a filesystem object
1023 * must be hooked prior, in order to capture the target inode during
1024 * unsuccessful attempts.
1026 void __audit_inode_child(const char *dname, const struct inode *inode,
1030 struct audit_context *context = current->audit_context;
1032 if (!context->in_syscall)
1035 /* determine matching parent */
1037 for (idx = 0; idx < context->name_count; idx++)
1038 if (context->names[idx].pino == pino) {
1040 const char *name = context->names[idx].name;
1041 int dlen = strlen(dname);
1042 int nlen = name ? strlen(name) : 0;
1047 /* disregard trailing slashes */
1048 n = name + nlen - 1;
1049 while ((*n == '/') && (n > name))
1052 /* find last path component */
1056 else if (n > name) {
1063 if (strncmp(n, dname, dlen) == 0)
1064 goto update_context;
1067 /* catch-all in case match not found */
1068 idx = context->name_count++;
1069 context->names[idx].name = NULL;
1070 context->names[idx].pino = pino;
1072 context->ino_count++;
1077 context->names[idx].ino = inode->i_ino;
1078 context->names[idx].dev = inode->i_sb->s_dev;
1079 context->names[idx].mode = inode->i_mode;
1080 context->names[idx].uid = inode->i_uid;
1081 context->names[idx].gid = inode->i_gid;
1082 context->names[idx].rdev = inode->i_rdev;
1083 audit_inode_context(idx, inode);
1088 * auditsc_get_stamp - get local copies of audit_context values
1089 * @ctx: audit_context for the task
1090 * @t: timespec to store time recorded in the audit_context
1091 * @serial: serial value that is recorded in the audit_context
1093 * Also sets the context as auditable.
1095 void auditsc_get_stamp(struct audit_context *ctx,
1096 struct timespec *t, unsigned int *serial)
1099 ctx->serial = audit_serial();
1100 t->tv_sec = ctx->ctime.tv_sec;
1101 t->tv_nsec = ctx->ctime.tv_nsec;
1102 *serial = ctx->serial;
1107 * audit_set_loginuid - set a task's audit_context loginuid
1108 * @task: task whose audit context is being modified
1109 * @loginuid: loginuid value
1113 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1115 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1117 if (task->audit_context) {
1118 struct audit_buffer *ab;
1120 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1122 audit_log_format(ab, "login pid=%d uid=%u "
1123 "old auid=%u new auid=%u",
1124 task->pid, task->uid,
1125 task->audit_context->loginuid, loginuid);
1128 task->audit_context->loginuid = loginuid;
1134 * audit_get_loginuid - get the loginuid for an audit_context
1135 * @ctx: the audit_context
1137 * Returns the context's loginuid or -1 if @ctx is NULL.
1139 uid_t audit_get_loginuid(struct audit_context *ctx)
1141 return ctx ? ctx->loginuid : -1;
1144 static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1146 struct audit_context *context = current->audit_context;
1150 if (likely(!context))
1153 len = security_ipc_getsecurity(ipcp, NULL, 0);
1154 if (len == -EOPNOTSUPP)
1159 ctx = kmalloc(len, GFP_ATOMIC);
1163 len = security_ipc_getsecurity(ipcp, ctx, len);
1171 audit_panic("error in audit_ipc_context");
1177 * audit_ipc_perms - record audit data for ipc
1178 * @qbytes: msgq bytes
1179 * @uid: msgq user id
1180 * @gid: msgq group id
1181 * @mode: msgq mode (permissions)
1183 * Returns 0 for success or NULL context or < 0 on error.
1185 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
1187 struct audit_aux_data_ipcctl *ax;
1188 struct audit_context *context = current->audit_context;
1190 if (likely(!context))
1193 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1197 ax->qbytes = qbytes;
1201 ax->ctx = audit_ipc_context(ipcp);
1203 ax->d.type = AUDIT_IPC;
1204 ax->d.next = context->aux;
1205 context->aux = (void *)ax;
1210 * audit_socketcall - record audit data for sys_socketcall
1211 * @nargs: number of args
1214 * Returns 0 for success or NULL context or < 0 on error.
1216 int audit_socketcall(int nargs, unsigned long *args)
1218 struct audit_aux_data_socketcall *ax;
1219 struct audit_context *context = current->audit_context;
1221 if (likely(!context))
1224 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1229 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1231 ax->d.type = AUDIT_SOCKETCALL;
1232 ax->d.next = context->aux;
1233 context->aux = (void *)ax;
1238 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1239 * @len: data length in user space
1240 * @a: data address in kernel space
1242 * Returns 0 for success or NULL context or < 0 on error.
1244 int audit_sockaddr(int len, void *a)
1246 struct audit_aux_data_sockaddr *ax;
1247 struct audit_context *context = current->audit_context;
1249 if (likely(!context))
1252 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1257 memcpy(ax->a, a, len);
1259 ax->d.type = AUDIT_SOCKADDR;
1260 ax->d.next = context->aux;
1261 context->aux = (void *)ax;
1266 * audit_avc_path - record the granting or denial of permissions
1267 * @dentry: dentry to record
1268 * @mnt: mnt to record
1270 * Returns 0 for success or NULL context or < 0 on error.
1272 * Called from security/selinux/avc.c::avc_audit()
1274 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1276 struct audit_aux_data_path *ax;
1277 struct audit_context *context = current->audit_context;
1279 if (likely(!context))
1282 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1286 ax->dentry = dget(dentry);
1287 ax->mnt = mntget(mnt);
1289 ax->d.type = AUDIT_AVC_PATH;
1290 ax->d.next = context->aux;
1291 context->aux = (void *)ax;
1296 * audit_signal_info - record signal info for shutting down audit subsystem
1297 * @sig: signal value
1298 * @t: task being signaled
1300 * If the audit subsystem is being terminated, record the task (pid)
1301 * and uid that is doing that.
1303 void audit_signal_info(int sig, struct task_struct *t)
1305 extern pid_t audit_sig_pid;
1306 extern uid_t audit_sig_uid;
1308 if (unlikely(audit_pid && t->tgid == audit_pid)) {
1309 if (sig == SIGTERM || sig == SIGHUP) {
1310 struct audit_context *ctx = current->audit_context;
1311 audit_sig_pid = current->pid;
1313 audit_sig_uid = ctx->loginuid;
1315 audit_sig_uid = current->uid;