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, 2006 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 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
35 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
38 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
45 #include <linux/init.h>
46 #include <asm/types.h>
47 #include <asm/atomic.h>
49 #include <linux/namei.h>
51 #include <linux/module.h>
52 #include <linux/mount.h>
53 #include <linux/socket.h>
54 #include <linux/mqueue.h>
55 #include <linux/audit.h>
56 #include <linux/personality.h>
57 #include <linux/time.h>
58 #include <linux/netlink.h>
59 #include <linux/compiler.h>
60 #include <asm/unistd.h>
61 #include <linux/security.h>
62 #include <linux/list.h>
63 #include <linux/tty.h>
64 #include <linux/selinux.h>
65 #include <linux/binfmts.h>
66 #include <linux/highmem.h>
67 #include <linux/syscalls.h>
68 #include <linux/inotify.h>
72 extern struct list_head audit_filter_list[];
74 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
75 * for saving names from getname(). */
76 #define AUDIT_NAMES 20
78 /* Indicates that audit should log the full pathname. */
79 #define AUDIT_NAME_FULL -1
81 /* number of audit rules */
84 /* determines whether we collect data for signals sent */
87 /* When fs/namei.c:getname() is called, we store the pointer in name and
88 * we don't let putname() free it (instead we free all of the saved
89 * pointers at syscall exit time).
91 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
94 int name_len; /* number of name's characters to log */
95 unsigned name_put; /* call __putname() for this name */
105 struct audit_aux_data {
106 struct audit_aux_data *next;
110 #define AUDIT_AUX_IPCPERM 0
112 /* Number of target pids per aux struct. */
113 #define AUDIT_AUX_PIDS 16
115 struct audit_aux_data_mq_open {
116 struct audit_aux_data d;
122 struct audit_aux_data_mq_sendrecv {
123 struct audit_aux_data d;
126 unsigned int msg_prio;
127 struct timespec abs_timeout;
130 struct audit_aux_data_mq_notify {
131 struct audit_aux_data d;
133 struct sigevent notification;
136 struct audit_aux_data_mq_getsetattr {
137 struct audit_aux_data d;
139 struct mq_attr mqstat;
142 struct audit_aux_data_ipcctl {
143 struct audit_aux_data d;
145 unsigned long qbytes;
152 struct audit_aux_data_execve {
153 struct audit_aux_data d;
156 struct mm_struct *mm;
159 struct audit_aux_data_socketcall {
160 struct audit_aux_data d;
162 unsigned long args[0];
165 struct audit_aux_data_sockaddr {
166 struct audit_aux_data d;
171 struct audit_aux_data_fd_pair {
172 struct audit_aux_data d;
176 struct audit_aux_data_pids {
177 struct audit_aux_data d;
178 pid_t target_pid[AUDIT_AUX_PIDS];
179 u32 target_sid[AUDIT_AUX_PIDS];
183 struct audit_tree_refs {
184 struct audit_tree_refs *next;
185 struct audit_chunk *c[31];
188 /* The per-task audit context. */
189 struct audit_context {
190 int dummy; /* must be the first element */
191 int in_syscall; /* 1 if task is in a syscall */
192 enum audit_state state;
193 unsigned int serial; /* serial number for record */
194 struct timespec ctime; /* time of syscall entry */
195 int major; /* syscall number */
196 unsigned long argv[4]; /* syscall arguments */
197 int return_valid; /* return code is valid */
198 long return_code;/* syscall return code */
199 int auditable; /* 1 if record should be written */
201 struct audit_names names[AUDIT_NAMES];
202 char * filterkey; /* key for rule that triggered record */
204 struct vfsmount * pwdmnt;
205 struct audit_context *previous; /* For nested syscalls */
206 struct audit_aux_data *aux;
207 struct audit_aux_data *aux_pids;
209 /* Save things to print about task_struct */
211 uid_t uid, euid, suid, fsuid;
212 gid_t gid, egid, sgid, fsgid;
213 unsigned long personality;
219 struct audit_tree_refs *trees, *first_trees;
228 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
229 static inline int open_arg(int flags, int mask)
231 int n = ACC_MODE(flags);
232 if (flags & (O_TRUNC | O_CREAT))
233 n |= AUDIT_PERM_WRITE;
237 static int audit_match_perm(struct audit_context *ctx, int mask)
239 unsigned n = ctx->major;
240 switch (audit_classify_syscall(ctx->arch, n)) {
242 if ((mask & AUDIT_PERM_WRITE) &&
243 audit_match_class(AUDIT_CLASS_WRITE, n))
245 if ((mask & AUDIT_PERM_READ) &&
246 audit_match_class(AUDIT_CLASS_READ, n))
248 if ((mask & AUDIT_PERM_ATTR) &&
249 audit_match_class(AUDIT_CLASS_CHATTR, n))
252 case 1: /* 32bit on biarch */
253 if ((mask & AUDIT_PERM_WRITE) &&
254 audit_match_class(AUDIT_CLASS_WRITE_32, n))
256 if ((mask & AUDIT_PERM_READ) &&
257 audit_match_class(AUDIT_CLASS_READ_32, n))
259 if ((mask & AUDIT_PERM_ATTR) &&
260 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
264 return mask & ACC_MODE(ctx->argv[1]);
266 return mask & ACC_MODE(ctx->argv[2]);
267 case 4: /* socketcall */
268 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
270 return mask & AUDIT_PERM_EXEC;
277 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
278 * ->first_trees points to its beginning, ->trees - to the current end of data.
279 * ->tree_count is the number of free entries in array pointed to by ->trees.
280 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
281 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
282 * it's going to remain 1-element for almost any setup) until we free context itself.
283 * References in it _are_ dropped - at the same time we free/drop aux stuff.
286 #ifdef CONFIG_AUDIT_TREE
287 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
289 struct audit_tree_refs *p = ctx->trees;
290 int left = ctx->tree_count;
292 p->c[--left] = chunk;
293 ctx->tree_count = left;
302 ctx->tree_count = 30;
308 static int grow_tree_refs(struct audit_context *ctx)
310 struct audit_tree_refs *p = ctx->trees;
311 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
317 p->next = ctx->trees;
319 ctx->first_trees = ctx->trees;
320 ctx->tree_count = 31;
325 static void unroll_tree_refs(struct audit_context *ctx,
326 struct audit_tree_refs *p, int count)
328 #ifdef CONFIG_AUDIT_TREE
329 struct audit_tree_refs *q;
332 /* we started with empty chain */
333 p = ctx->first_trees;
335 /* if the very first allocation has failed, nothing to do */
340 for (q = p; q != ctx->trees; q = q->next, n = 31) {
342 audit_put_chunk(q->c[n]);
346 while (n-- > ctx->tree_count) {
347 audit_put_chunk(q->c[n]);
351 ctx->tree_count = count;
355 static void free_tree_refs(struct audit_context *ctx)
357 struct audit_tree_refs *p, *q;
358 for (p = ctx->first_trees; p; p = q) {
364 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
366 #ifdef CONFIG_AUDIT_TREE
367 struct audit_tree_refs *p;
372 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
373 for (n = 0; n < 31; n++)
374 if (audit_tree_match(p->c[n], tree))
379 for (n = ctx->tree_count; n < 31; n++)
380 if (audit_tree_match(p->c[n], tree))
387 /* Determine if any context name data matches a rule's watch data */
388 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
390 static int audit_filter_rules(struct task_struct *tsk,
391 struct audit_krule *rule,
392 struct audit_context *ctx,
393 struct audit_names *name,
394 enum audit_state *state)
396 int i, j, need_sid = 1;
399 for (i = 0; i < rule->field_count; i++) {
400 struct audit_field *f = &rule->fields[i];
405 result = audit_comparator(tsk->pid, f->op, f->val);
410 ctx->ppid = sys_getppid();
411 result = audit_comparator(ctx->ppid, f->op, f->val);
415 result = audit_comparator(tsk->uid, f->op, f->val);
418 result = audit_comparator(tsk->euid, f->op, f->val);
421 result = audit_comparator(tsk->suid, f->op, f->val);
424 result = audit_comparator(tsk->fsuid, f->op, f->val);
427 result = audit_comparator(tsk->gid, f->op, f->val);
430 result = audit_comparator(tsk->egid, f->op, f->val);
433 result = audit_comparator(tsk->sgid, f->op, f->val);
436 result = audit_comparator(tsk->fsgid, f->op, f->val);
439 result = audit_comparator(tsk->personality, f->op, f->val);
443 result = audit_comparator(ctx->arch, f->op, f->val);
447 if (ctx && ctx->return_valid)
448 result = audit_comparator(ctx->return_code, f->op, f->val);
451 if (ctx && ctx->return_valid) {
453 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
455 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
460 result = audit_comparator(MAJOR(name->dev),
463 for (j = 0; j < ctx->name_count; j++) {
464 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
473 result = audit_comparator(MINOR(name->dev),
476 for (j = 0; j < ctx->name_count; j++) {
477 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
486 result = (name->ino == f->val);
488 for (j = 0; j < ctx->name_count; j++) {
489 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
497 if (name && rule->watch->ino != (unsigned long)-1)
498 result = (name->dev == rule->watch->dev &&
499 name->ino == rule->watch->ino);
503 result = match_tree_refs(ctx, rule->tree);
508 result = audit_comparator(tsk->loginuid, f->op, f->val);
510 case AUDIT_SUBJ_USER:
511 case AUDIT_SUBJ_ROLE:
512 case AUDIT_SUBJ_TYPE:
515 /* NOTE: this may return negative values indicating
516 a temporary error. We simply treat this as a
517 match for now to avoid losing information that
518 may be wanted. An error message will also be
522 selinux_get_task_sid(tsk, &sid);
525 result = selinux_audit_rule_match(sid, f->type,
534 case AUDIT_OBJ_LEV_LOW:
535 case AUDIT_OBJ_LEV_HIGH:
536 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
539 /* Find files that match */
541 result = selinux_audit_rule_match(
542 name->osid, f->type, f->op,
545 for (j = 0; j < ctx->name_count; j++) {
546 if (selinux_audit_rule_match(
555 /* Find ipc objects that match */
557 struct audit_aux_data *aux;
558 for (aux = ctx->aux; aux;
560 if (aux->type == AUDIT_IPC) {
561 struct audit_aux_data_ipcctl *axi = (void *)aux;
562 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
576 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
578 case AUDIT_FILTERKEY:
579 /* ignore this field for filtering */
583 result = audit_match_perm(ctx, f->val);
591 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
592 switch (rule->action) {
593 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
594 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
599 /* At process creation time, we can determine if system-call auditing is
600 * completely disabled for this task. Since we only have the task
601 * structure at this point, we can only check uid and gid.
603 static enum audit_state audit_filter_task(struct task_struct *tsk)
605 struct audit_entry *e;
606 enum audit_state state;
609 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
610 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
616 return AUDIT_BUILD_CONTEXT;
619 /* At syscall entry and exit time, this filter is called if the
620 * audit_state is not low enough that auditing cannot take place, but is
621 * also not high enough that we already know we have to write an audit
622 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
624 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
625 struct audit_context *ctx,
626 struct list_head *list)
628 struct audit_entry *e;
629 enum audit_state state;
631 if (audit_pid && tsk->tgid == audit_pid)
632 return AUDIT_DISABLED;
635 if (!list_empty(list)) {
636 int word = AUDIT_WORD(ctx->major);
637 int bit = AUDIT_BIT(ctx->major);
639 list_for_each_entry_rcu(e, list, list) {
640 if ((e->rule.mask[word] & bit) == bit &&
641 audit_filter_rules(tsk, &e->rule, ctx, NULL,
649 return AUDIT_BUILD_CONTEXT;
652 /* At syscall exit time, this filter is called if any audit_names[] have been
653 * collected during syscall processing. We only check rules in sublists at hash
654 * buckets applicable to the inode numbers in audit_names[].
655 * Regarding audit_state, same rules apply as for audit_filter_syscall().
657 enum audit_state audit_filter_inodes(struct task_struct *tsk,
658 struct audit_context *ctx)
661 struct audit_entry *e;
662 enum audit_state state;
664 if (audit_pid && tsk->tgid == audit_pid)
665 return AUDIT_DISABLED;
668 for (i = 0; i < ctx->name_count; i++) {
669 int word = AUDIT_WORD(ctx->major);
670 int bit = AUDIT_BIT(ctx->major);
671 struct audit_names *n = &ctx->names[i];
672 int h = audit_hash_ino((u32)n->ino);
673 struct list_head *list = &audit_inode_hash[h];
675 if (list_empty(list))
678 list_for_each_entry_rcu(e, list, list) {
679 if ((e->rule.mask[word] & bit) == bit &&
680 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
687 return AUDIT_BUILD_CONTEXT;
690 void audit_set_auditable(struct audit_context *ctx)
695 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
699 struct audit_context *context = tsk->audit_context;
701 if (likely(!context))
703 context->return_valid = return_valid;
704 context->return_code = return_code;
706 if (context->in_syscall && !context->dummy && !context->auditable) {
707 enum audit_state state;
709 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
710 if (state == AUDIT_RECORD_CONTEXT) {
711 context->auditable = 1;
715 state = audit_filter_inodes(tsk, context);
716 if (state == AUDIT_RECORD_CONTEXT)
717 context->auditable = 1;
723 tsk->audit_context = NULL;
727 static inline void audit_free_names(struct audit_context *context)
732 if (context->auditable
733 ||context->put_count + context->ino_count != context->name_count) {
734 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
735 " name_count=%d put_count=%d"
736 " ino_count=%d [NOT freeing]\n",
738 context->serial, context->major, context->in_syscall,
739 context->name_count, context->put_count,
741 for (i = 0; i < context->name_count; i++) {
742 printk(KERN_ERR "names[%d] = %p = %s\n", i,
743 context->names[i].name,
744 context->names[i].name ?: "(null)");
751 context->put_count = 0;
752 context->ino_count = 0;
755 for (i = 0; i < context->name_count; i++) {
756 if (context->names[i].name && context->names[i].name_put)
757 __putname(context->names[i].name);
759 context->name_count = 0;
763 mntput(context->pwdmnt);
765 context->pwdmnt = NULL;
768 static inline void audit_free_aux(struct audit_context *context)
770 struct audit_aux_data *aux;
772 while ((aux = context->aux)) {
773 context->aux = aux->next;
776 while ((aux = context->aux_pids)) {
777 context->aux_pids = aux->next;
782 static inline void audit_zero_context(struct audit_context *context,
783 enum audit_state state)
785 memset(context, 0, sizeof(*context));
786 context->state = state;
789 static inline struct audit_context *audit_alloc_context(enum audit_state state)
791 struct audit_context *context;
793 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
795 audit_zero_context(context, state);
800 * audit_alloc - allocate an audit context block for a task
803 * Filter on the task information and allocate a per-task audit context
804 * if necessary. Doing so turns on system call auditing for the
805 * specified task. This is called from copy_process, so no lock is
808 int audit_alloc(struct task_struct *tsk)
810 struct audit_context *context;
811 enum audit_state state;
813 if (likely(!audit_enabled))
814 return 0; /* Return if not auditing. */
816 state = audit_filter_task(tsk);
817 if (likely(state == AUDIT_DISABLED))
820 if (!(context = audit_alloc_context(state))) {
821 audit_log_lost("out of memory in audit_alloc");
825 tsk->audit_context = context;
826 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
830 static inline void audit_free_context(struct audit_context *context)
832 struct audit_context *previous;
836 previous = context->previous;
837 if (previous || (count && count < 10)) {
839 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
840 " freeing multiple contexts (%d)\n",
841 context->serial, context->major,
842 context->name_count, count);
844 audit_free_names(context);
845 unroll_tree_refs(context, NULL, 0);
846 free_tree_refs(context);
847 audit_free_aux(context);
848 kfree(context->filterkey);
853 printk(KERN_ERR "audit: freed %d contexts\n", count);
856 void audit_log_task_context(struct audit_buffer *ab)
863 selinux_get_task_sid(current, &sid);
867 error = selinux_sid_to_string(sid, &ctx, &len);
869 if (error != -EINVAL)
874 audit_log_format(ab, " subj=%s", ctx);
879 audit_panic("error in audit_log_task_context");
883 EXPORT_SYMBOL(audit_log_task_context);
885 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
887 char name[sizeof(tsk->comm)];
888 struct mm_struct *mm = tsk->mm;
889 struct vm_area_struct *vma;
893 get_task_comm(name, tsk);
894 audit_log_format(ab, " comm=");
895 audit_log_untrustedstring(ab, name);
898 down_read(&mm->mmap_sem);
901 if ((vma->vm_flags & VM_EXECUTABLE) &&
903 audit_log_d_path(ab, "exe=",
904 vma->vm_file->f_path.dentry,
905 vma->vm_file->f_path.mnt);
910 up_read(&mm->mmap_sem);
912 audit_log_task_context(ab);
915 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
918 struct audit_buffer *ab;
923 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
927 if (selinux_sid_to_string(sid, &s, &len)) {
928 audit_log_format(ab, "opid=%d obj=(none)", pid);
931 audit_log_format(ab, "opid=%d obj=%s", pid, s);
938 static void audit_log_execve_info(struct audit_buffer *ab,
939 struct audit_aux_data_execve *axi)
943 const char __user *p;
946 if (axi->mm != current->mm)
947 return; /* execve failed, no additional info */
949 p = (const char __user *)axi->mm->arg_start;
951 for (i = 0; i < axi->argc; i++, p += len) {
952 len = strnlen_user(p, MAX_ARG_STRLEN);
954 * We just created this mm, if we can't find the strings
955 * we just copied into it something is _very_ wrong. Similar
956 * for strings that are too long, we should not have created
959 if (!len || len > MAX_ARG_STRLEN) {
961 send_sig(SIGKILL, current, 0);
964 buf = kmalloc(len, GFP_KERNEL);
966 audit_panic("out of memory for argv string\n");
970 ret = copy_from_user(buf, p, len);
972 * There is no reason for this copy to be short. We just
973 * copied them here, and the mm hasn't been exposed to user-
978 send_sig(SIGKILL, current, 0);
981 audit_log_format(ab, "a%d=", i);
982 audit_log_untrustedstring(ab, buf);
983 audit_log_format(ab, "\n");
989 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
991 int i, call_panic = 0;
992 struct audit_buffer *ab;
993 struct audit_aux_data *aux;
997 context->pid = tsk->pid;
999 context->ppid = sys_getppid();
1000 context->uid = tsk->uid;
1001 context->gid = tsk->gid;
1002 context->euid = tsk->euid;
1003 context->suid = tsk->suid;
1004 context->fsuid = tsk->fsuid;
1005 context->egid = tsk->egid;
1006 context->sgid = tsk->sgid;
1007 context->fsgid = tsk->fsgid;
1008 context->personality = tsk->personality;
1010 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1012 return; /* audit_panic has been called */
1013 audit_log_format(ab, "arch=%x syscall=%d",
1014 context->arch, context->major);
1015 if (context->personality != PER_LINUX)
1016 audit_log_format(ab, " per=%lx", context->personality);
1017 if (context->return_valid)
1018 audit_log_format(ab, " success=%s exit=%ld",
1019 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1020 context->return_code);
1022 mutex_lock(&tty_mutex);
1023 read_lock(&tasklist_lock);
1024 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1025 tty = tsk->signal->tty->name;
1028 read_unlock(&tasklist_lock);
1029 audit_log_format(ab,
1030 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1031 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1032 " euid=%u suid=%u fsuid=%u"
1033 " egid=%u sgid=%u fsgid=%u tty=%s",
1038 context->name_count,
1044 context->euid, context->suid, context->fsuid,
1045 context->egid, context->sgid, context->fsgid, tty);
1047 mutex_unlock(&tty_mutex);
1049 audit_log_task_info(ab, tsk);
1050 if (context->filterkey) {
1051 audit_log_format(ab, " key=");
1052 audit_log_untrustedstring(ab, context->filterkey);
1054 audit_log_format(ab, " key=(null)");
1057 for (aux = context->aux; aux; aux = aux->next) {
1059 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1061 continue; /* audit_panic has been called */
1063 switch (aux->type) {
1064 case AUDIT_MQ_OPEN: {
1065 struct audit_aux_data_mq_open *axi = (void *)aux;
1066 audit_log_format(ab,
1067 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1068 "mq_msgsize=%ld mq_curmsgs=%ld",
1069 axi->oflag, axi->mode, axi->attr.mq_flags,
1070 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1071 axi->attr.mq_curmsgs);
1074 case AUDIT_MQ_SENDRECV: {
1075 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1076 audit_log_format(ab,
1077 "mqdes=%d msg_len=%zd msg_prio=%u "
1078 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1079 axi->mqdes, axi->msg_len, axi->msg_prio,
1080 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1083 case AUDIT_MQ_NOTIFY: {
1084 struct audit_aux_data_mq_notify *axi = (void *)aux;
1085 audit_log_format(ab,
1086 "mqdes=%d sigev_signo=%d",
1088 axi->notification.sigev_signo);
1091 case AUDIT_MQ_GETSETATTR: {
1092 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1093 audit_log_format(ab,
1094 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1097 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1098 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1102 struct audit_aux_data_ipcctl *axi = (void *)aux;
1103 audit_log_format(ab,
1104 "ouid=%u ogid=%u mode=%#o",
1105 axi->uid, axi->gid, axi->mode);
1106 if (axi->osid != 0) {
1109 if (selinux_sid_to_string(
1110 axi->osid, &ctx, &len)) {
1111 audit_log_format(ab, " osid=%u",
1115 audit_log_format(ab, " obj=%s", ctx);
1120 case AUDIT_IPC_SET_PERM: {
1121 struct audit_aux_data_ipcctl *axi = (void *)aux;
1122 audit_log_format(ab,
1123 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1124 axi->qbytes, axi->uid, axi->gid, axi->mode);
1127 case AUDIT_EXECVE: {
1128 struct audit_aux_data_execve *axi = (void *)aux;
1129 audit_log_execve_info(ab, axi);
1132 case AUDIT_SOCKETCALL: {
1134 struct audit_aux_data_socketcall *axs = (void *)aux;
1135 audit_log_format(ab, "nargs=%d", axs->nargs);
1136 for (i=0; i<axs->nargs; i++)
1137 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1140 case AUDIT_SOCKADDR: {
1141 struct audit_aux_data_sockaddr *axs = (void *)aux;
1143 audit_log_format(ab, "saddr=");
1144 audit_log_hex(ab, axs->a, axs->len);
1147 case AUDIT_FD_PAIR: {
1148 struct audit_aux_data_fd_pair *axs = (void *)aux;
1149 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1156 for (aux = context->aux_pids; aux; aux = aux->next) {
1157 struct audit_aux_data_pids *axs = (void *)aux;
1160 for (i = 0; i < axs->pid_count; i++)
1161 if (audit_log_pid_context(context, axs->target_pid[i],
1162 axs->target_sid[i]))
1166 if (context->target_pid &&
1167 audit_log_pid_context(context, context->target_pid,
1168 context->target_sid))
1171 if (context->pwd && context->pwdmnt) {
1172 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1174 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1178 for (i = 0; i < context->name_count; i++) {
1179 struct audit_names *n = &context->names[i];
1181 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1183 continue; /* audit_panic has been called */
1185 audit_log_format(ab, "item=%d", i);
1188 switch(n->name_len) {
1189 case AUDIT_NAME_FULL:
1190 /* log the full path */
1191 audit_log_format(ab, " name=");
1192 audit_log_untrustedstring(ab, n->name);
1195 /* name was specified as a relative path and the
1196 * directory component is the cwd */
1197 audit_log_d_path(ab, " name=", context->pwd,
1201 /* log the name's directory component */
1202 audit_log_format(ab, " name=");
1203 audit_log_n_untrustedstring(ab, n->name_len,
1207 audit_log_format(ab, " name=(null)");
1209 if (n->ino != (unsigned long)-1) {
1210 audit_log_format(ab, " inode=%lu"
1211 " dev=%02x:%02x mode=%#o"
1212 " ouid=%u ogid=%u rdev=%02x:%02x",
1225 if (selinux_sid_to_string(
1226 n->osid, &ctx, &len)) {
1227 audit_log_format(ab, " osid=%u", n->osid);
1230 audit_log_format(ab, " obj=%s", ctx);
1237 audit_panic("error converting sid to string");
1241 * audit_free - free a per-task audit context
1242 * @tsk: task whose audit context block to free
1244 * Called from copy_process and do_exit
1246 void audit_free(struct task_struct *tsk)
1248 struct audit_context *context;
1250 context = audit_get_context(tsk, 0, 0);
1251 if (likely(!context))
1254 /* Check for system calls that do not go through the exit
1255 * function (e.g., exit_group), then free context block.
1256 * We use GFP_ATOMIC here because we might be doing this
1257 * in the context of the idle thread */
1258 /* that can happen only if we are called from do_exit() */
1259 if (context->in_syscall && context->auditable)
1260 audit_log_exit(context, tsk);
1262 audit_free_context(context);
1266 * audit_syscall_entry - fill in an audit record at syscall entry
1267 * @tsk: task being audited
1268 * @arch: architecture type
1269 * @major: major syscall type (function)
1270 * @a1: additional syscall register 1
1271 * @a2: additional syscall register 2
1272 * @a3: additional syscall register 3
1273 * @a4: additional syscall register 4
1275 * Fill in audit context at syscall entry. This only happens if the
1276 * audit context was created when the task was created and the state or
1277 * filters demand the audit context be built. If the state from the
1278 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1279 * then the record will be written at syscall exit time (otherwise, it
1280 * will only be written if another part of the kernel requests that it
1283 void audit_syscall_entry(int arch, int major,
1284 unsigned long a1, unsigned long a2,
1285 unsigned long a3, unsigned long a4)
1287 struct task_struct *tsk = current;
1288 struct audit_context *context = tsk->audit_context;
1289 enum audit_state state;
1294 * This happens only on certain architectures that make system
1295 * calls in kernel_thread via the entry.S interface, instead of
1296 * with direct calls. (If you are porting to a new
1297 * architecture, hitting this condition can indicate that you
1298 * got the _exit/_leave calls backward in entry.S.)
1302 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1304 * This also happens with vm86 emulation in a non-nested manner
1305 * (entries without exits), so this case must be caught.
1307 if (context->in_syscall) {
1308 struct audit_context *newctx;
1312 "audit(:%d) pid=%d in syscall=%d;"
1313 " entering syscall=%d\n",
1314 context->serial, tsk->pid, context->major, major);
1316 newctx = audit_alloc_context(context->state);
1318 newctx->previous = context;
1320 tsk->audit_context = newctx;
1322 /* If we can't alloc a new context, the best we
1323 * can do is to leak memory (any pending putname
1324 * will be lost). The only other alternative is
1325 * to abandon auditing. */
1326 audit_zero_context(context, context->state);
1329 BUG_ON(context->in_syscall || context->name_count);
1334 context->arch = arch;
1335 context->major = major;
1336 context->argv[0] = a1;
1337 context->argv[1] = a2;
1338 context->argv[2] = a3;
1339 context->argv[3] = a4;
1341 state = context->state;
1342 context->dummy = !audit_n_rules;
1343 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1344 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1345 if (likely(state == AUDIT_DISABLED))
1348 context->serial = 0;
1349 context->ctime = CURRENT_TIME;
1350 context->in_syscall = 1;
1351 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1356 * audit_syscall_exit - deallocate audit context after a system call
1357 * @tsk: task being audited
1358 * @valid: success/failure flag
1359 * @return_code: syscall return value
1361 * Tear down after system call. If the audit context has been marked as
1362 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1363 * filtering, or because some other part of the kernel write an audit
1364 * message), then write out the syscall information. In call cases,
1365 * free the names stored from getname().
1367 void audit_syscall_exit(int valid, long return_code)
1369 struct task_struct *tsk = current;
1370 struct audit_context *context;
1372 context = audit_get_context(tsk, valid, return_code);
1374 if (likely(!context))
1377 if (context->in_syscall && context->auditable)
1378 audit_log_exit(context, tsk);
1380 context->in_syscall = 0;
1381 context->auditable = 0;
1383 if (context->previous) {
1384 struct audit_context *new_context = context->previous;
1385 context->previous = NULL;
1386 audit_free_context(context);
1387 tsk->audit_context = new_context;
1389 audit_free_names(context);
1390 unroll_tree_refs(context, NULL, 0);
1391 audit_free_aux(context);
1392 context->aux = NULL;
1393 context->aux_pids = NULL;
1394 context->target_pid = 0;
1395 context->target_sid = 0;
1396 kfree(context->filterkey);
1397 context->filterkey = NULL;
1398 tsk->audit_context = context;
1402 static inline void handle_one(const struct inode *inode)
1404 #ifdef CONFIG_AUDIT_TREE
1405 struct audit_context *context;
1406 struct audit_tree_refs *p;
1407 struct audit_chunk *chunk;
1409 if (likely(list_empty(&inode->inotify_watches)))
1411 context = current->audit_context;
1413 count = context->tree_count;
1415 chunk = audit_tree_lookup(inode);
1419 if (likely(put_tree_ref(context, chunk)))
1421 if (unlikely(!grow_tree_refs(context))) {
1422 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1423 audit_set_auditable(context);
1424 audit_put_chunk(chunk);
1425 unroll_tree_refs(context, p, count);
1428 put_tree_ref(context, chunk);
1432 static void handle_path(const struct dentry *dentry)
1434 #ifdef CONFIG_AUDIT_TREE
1435 struct audit_context *context;
1436 struct audit_tree_refs *p;
1437 const struct dentry *d, *parent;
1438 struct audit_chunk *drop;
1442 context = current->audit_context;
1444 count = context->tree_count;
1449 seq = read_seqbegin(&rename_lock);
1451 struct inode *inode = d->d_inode;
1452 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1453 struct audit_chunk *chunk;
1454 chunk = audit_tree_lookup(inode);
1456 if (unlikely(!put_tree_ref(context, chunk))) {
1462 parent = d->d_parent;
1467 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1470 /* just a race with rename */
1471 unroll_tree_refs(context, p, count);
1474 audit_put_chunk(drop);
1475 if (grow_tree_refs(context)) {
1476 /* OK, got more space */
1477 unroll_tree_refs(context, p, count);
1482 "out of memory, audit has lost a tree reference");
1483 unroll_tree_refs(context, p, count);
1484 audit_set_auditable(context);
1492 * audit_getname - add a name to the list
1493 * @name: name to add
1495 * Add a name to the list of audit names for this context.
1496 * Called from fs/namei.c:getname().
1498 void __audit_getname(const char *name)
1500 struct audit_context *context = current->audit_context;
1502 if (IS_ERR(name) || !name)
1505 if (!context->in_syscall) {
1506 #if AUDIT_DEBUG == 2
1507 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1508 __FILE__, __LINE__, context->serial, name);
1513 BUG_ON(context->name_count >= AUDIT_NAMES);
1514 context->names[context->name_count].name = name;
1515 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1516 context->names[context->name_count].name_put = 1;
1517 context->names[context->name_count].ino = (unsigned long)-1;
1518 context->names[context->name_count].osid = 0;
1519 ++context->name_count;
1520 if (!context->pwd) {
1521 read_lock(¤t->fs->lock);
1522 context->pwd = dget(current->fs->pwd);
1523 context->pwdmnt = mntget(current->fs->pwdmnt);
1524 read_unlock(¤t->fs->lock);
1529 /* audit_putname - intercept a putname request
1530 * @name: name to intercept and delay for putname
1532 * If we have stored the name from getname in the audit context,
1533 * then we delay the putname until syscall exit.
1534 * Called from include/linux/fs.h:putname().
1536 void audit_putname(const char *name)
1538 struct audit_context *context = current->audit_context;
1541 if (!context->in_syscall) {
1542 #if AUDIT_DEBUG == 2
1543 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1544 __FILE__, __LINE__, context->serial, name);
1545 if (context->name_count) {
1547 for (i = 0; i < context->name_count; i++)
1548 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1549 context->names[i].name,
1550 context->names[i].name ?: "(null)");
1557 ++context->put_count;
1558 if (context->put_count > context->name_count) {
1559 printk(KERN_ERR "%s:%d(:%d): major=%d"
1560 " in_syscall=%d putname(%p) name_count=%d"
1563 context->serial, context->major,
1564 context->in_syscall, name, context->name_count,
1565 context->put_count);
1572 static int audit_inc_name_count(struct audit_context *context,
1573 const struct inode *inode)
1575 if (context->name_count >= AUDIT_NAMES) {
1577 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1578 "dev=%02x:%02x, inode=%lu",
1579 MAJOR(inode->i_sb->s_dev),
1580 MINOR(inode->i_sb->s_dev),
1584 printk(KERN_DEBUG "name_count maxed, losing inode data");
1587 context->name_count++;
1589 context->ino_count++;
1594 /* Copy inode data into an audit_names. */
1595 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1597 name->ino = inode->i_ino;
1598 name->dev = inode->i_sb->s_dev;
1599 name->mode = inode->i_mode;
1600 name->uid = inode->i_uid;
1601 name->gid = inode->i_gid;
1602 name->rdev = inode->i_rdev;
1603 selinux_get_inode_sid(inode, &name->osid);
1607 * audit_inode - store the inode and device from a lookup
1608 * @name: name being audited
1609 * @dentry: dentry being audited
1611 * Called from fs/namei.c:path_lookup().
1613 void __audit_inode(const char *name, const struct dentry *dentry)
1616 struct audit_context *context = current->audit_context;
1617 const struct inode *inode = dentry->d_inode;
1619 if (!context->in_syscall)
1621 if (context->name_count
1622 && context->names[context->name_count-1].name
1623 && context->names[context->name_count-1].name == name)
1624 idx = context->name_count - 1;
1625 else if (context->name_count > 1
1626 && context->names[context->name_count-2].name
1627 && context->names[context->name_count-2].name == name)
1628 idx = context->name_count - 2;
1630 /* FIXME: how much do we care about inodes that have no
1631 * associated name? */
1632 if (audit_inc_name_count(context, inode))
1634 idx = context->name_count - 1;
1635 context->names[idx].name = NULL;
1637 handle_path(dentry);
1638 audit_copy_inode(&context->names[idx], inode);
1642 * audit_inode_child - collect inode info for created/removed objects
1643 * @dname: inode's dentry name
1644 * @dentry: dentry being audited
1645 * @parent: inode of dentry parent
1647 * For syscalls that create or remove filesystem objects, audit_inode
1648 * can only collect information for the filesystem object's parent.
1649 * This call updates the audit context with the child's information.
1650 * Syscalls that create a new filesystem object must be hooked after
1651 * the object is created. Syscalls that remove a filesystem object
1652 * must be hooked prior, in order to capture the target inode during
1653 * unsuccessful attempts.
1655 void __audit_inode_child(const char *dname, const struct dentry *dentry,
1656 const struct inode *parent)
1659 struct audit_context *context = current->audit_context;
1660 const char *found_parent = NULL, *found_child = NULL;
1661 const struct inode *inode = dentry->d_inode;
1664 if (!context->in_syscall)
1669 /* determine matching parent */
1673 /* parent is more likely, look for it first */
1674 for (idx = 0; idx < context->name_count; idx++) {
1675 struct audit_names *n = &context->names[idx];
1680 if (n->ino == parent->i_ino &&
1681 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1682 n->name_len = dirlen; /* update parent data in place */
1683 found_parent = n->name;
1688 /* no matching parent, look for matching child */
1689 for (idx = 0; idx < context->name_count; idx++) {
1690 struct audit_names *n = &context->names[idx];
1695 /* strcmp() is the more likely scenario */
1696 if (!strcmp(dname, n->name) ||
1697 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1699 audit_copy_inode(n, inode);
1701 n->ino = (unsigned long)-1;
1702 found_child = n->name;
1708 if (!found_parent) {
1709 if (audit_inc_name_count(context, parent))
1711 idx = context->name_count - 1;
1712 context->names[idx].name = NULL;
1713 audit_copy_inode(&context->names[idx], parent);
1717 if (audit_inc_name_count(context, inode))
1719 idx = context->name_count - 1;
1721 /* Re-use the name belonging to the slot for a matching parent
1722 * directory. All names for this context are relinquished in
1723 * audit_free_names() */
1725 context->names[idx].name = found_parent;
1726 context->names[idx].name_len = AUDIT_NAME_FULL;
1727 /* don't call __putname() */
1728 context->names[idx].name_put = 0;
1730 context->names[idx].name = NULL;
1734 audit_copy_inode(&context->names[idx], inode);
1736 context->names[idx].ino = (unsigned long)-1;
1739 EXPORT_SYMBOL_GPL(__audit_inode_child);
1742 * auditsc_get_stamp - get local copies of audit_context values
1743 * @ctx: audit_context for the task
1744 * @t: timespec to store time recorded in the audit_context
1745 * @serial: serial value that is recorded in the audit_context
1747 * Also sets the context as auditable.
1749 void auditsc_get_stamp(struct audit_context *ctx,
1750 struct timespec *t, unsigned int *serial)
1753 ctx->serial = audit_serial();
1754 t->tv_sec = ctx->ctime.tv_sec;
1755 t->tv_nsec = ctx->ctime.tv_nsec;
1756 *serial = ctx->serial;
1761 * audit_set_loginuid - set a task's audit_context loginuid
1762 * @task: task whose audit context is being modified
1763 * @loginuid: loginuid value
1767 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1769 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1771 struct audit_context *context = task->audit_context;
1773 if (context && context->in_syscall) {
1774 struct audit_buffer *ab;
1776 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1778 audit_log_format(ab, "login pid=%d uid=%u "
1779 "old auid=%u new auid=%u",
1780 task->pid, task->uid,
1781 task->loginuid, loginuid);
1785 task->loginuid = loginuid;
1790 * __audit_mq_open - record audit data for a POSIX MQ open
1793 * @u_attr: queue attributes
1795 * Returns 0 for success or NULL context or < 0 on error.
1797 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1799 struct audit_aux_data_mq_open *ax;
1800 struct audit_context *context = current->audit_context;
1805 if (likely(!context))
1808 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1812 if (u_attr != NULL) {
1813 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1818 memset(&ax->attr, 0, sizeof(ax->attr));
1823 ax->d.type = AUDIT_MQ_OPEN;
1824 ax->d.next = context->aux;
1825 context->aux = (void *)ax;
1830 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1831 * @mqdes: MQ descriptor
1832 * @msg_len: Message length
1833 * @msg_prio: Message priority
1834 * @u_abs_timeout: Message timeout in absolute time
1836 * Returns 0 for success or NULL context or < 0 on error.
1838 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1839 const struct timespec __user *u_abs_timeout)
1841 struct audit_aux_data_mq_sendrecv *ax;
1842 struct audit_context *context = current->audit_context;
1847 if (likely(!context))
1850 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1854 if (u_abs_timeout != NULL) {
1855 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1860 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1863 ax->msg_len = msg_len;
1864 ax->msg_prio = msg_prio;
1866 ax->d.type = AUDIT_MQ_SENDRECV;
1867 ax->d.next = context->aux;
1868 context->aux = (void *)ax;
1873 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1874 * @mqdes: MQ descriptor
1875 * @msg_len: Message length
1876 * @u_msg_prio: Message priority
1877 * @u_abs_timeout: Message timeout in absolute time
1879 * Returns 0 for success or NULL context or < 0 on error.
1881 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1882 unsigned int __user *u_msg_prio,
1883 const struct timespec __user *u_abs_timeout)
1885 struct audit_aux_data_mq_sendrecv *ax;
1886 struct audit_context *context = current->audit_context;
1891 if (likely(!context))
1894 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1898 if (u_msg_prio != NULL) {
1899 if (get_user(ax->msg_prio, u_msg_prio)) {
1906 if (u_abs_timeout != NULL) {
1907 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1912 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1915 ax->msg_len = msg_len;
1917 ax->d.type = AUDIT_MQ_SENDRECV;
1918 ax->d.next = context->aux;
1919 context->aux = (void *)ax;
1924 * __audit_mq_notify - record audit data for a POSIX MQ notify
1925 * @mqdes: MQ descriptor
1926 * @u_notification: Notification event
1928 * Returns 0 for success or NULL context or < 0 on error.
1931 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1933 struct audit_aux_data_mq_notify *ax;
1934 struct audit_context *context = current->audit_context;
1939 if (likely(!context))
1942 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1946 if (u_notification != NULL) {
1947 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1952 memset(&ax->notification, 0, sizeof(ax->notification));
1956 ax->d.type = AUDIT_MQ_NOTIFY;
1957 ax->d.next = context->aux;
1958 context->aux = (void *)ax;
1963 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1964 * @mqdes: MQ descriptor
1967 * Returns 0 for success or NULL context or < 0 on error.
1969 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1971 struct audit_aux_data_mq_getsetattr *ax;
1972 struct audit_context *context = current->audit_context;
1977 if (likely(!context))
1980 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1985 ax->mqstat = *mqstat;
1987 ax->d.type = AUDIT_MQ_GETSETATTR;
1988 ax->d.next = context->aux;
1989 context->aux = (void *)ax;
1994 * audit_ipc_obj - record audit data for ipc object
1995 * @ipcp: ipc permissions
1997 * Returns 0 for success or NULL context or < 0 on error.
1999 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2001 struct audit_aux_data_ipcctl *ax;
2002 struct audit_context *context = current->audit_context;
2004 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2008 ax->uid = ipcp->uid;
2009 ax->gid = ipcp->gid;
2010 ax->mode = ipcp->mode;
2011 selinux_get_ipc_sid(ipcp, &ax->osid);
2013 ax->d.type = AUDIT_IPC;
2014 ax->d.next = context->aux;
2015 context->aux = (void *)ax;
2020 * audit_ipc_set_perm - record audit data for new ipc permissions
2021 * @qbytes: msgq bytes
2022 * @uid: msgq user id
2023 * @gid: msgq group id
2024 * @mode: msgq mode (permissions)
2026 * Returns 0 for success or NULL context or < 0 on error.
2028 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2030 struct audit_aux_data_ipcctl *ax;
2031 struct audit_context *context = current->audit_context;
2033 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2037 ax->qbytes = qbytes;
2042 ax->d.type = AUDIT_IPC_SET_PERM;
2043 ax->d.next = context->aux;
2044 context->aux = (void *)ax;
2048 int audit_argv_kb = 32;
2050 int audit_bprm(struct linux_binprm *bprm)
2052 struct audit_aux_data_execve *ax;
2053 struct audit_context *context = current->audit_context;
2055 if (likely(!audit_enabled || !context || context->dummy))
2059 * Even though the stack code doesn't limit the arg+env size any more,
2060 * the audit code requires that _all_ arguments be logged in a single
2061 * netlink skb. Hence cap it :-(
2063 if (bprm->argv_len > (audit_argv_kb << 10))
2066 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2070 ax->argc = bprm->argc;
2071 ax->envc = bprm->envc;
2073 ax->d.type = AUDIT_EXECVE;
2074 ax->d.next = context->aux;
2075 context->aux = (void *)ax;
2081 * audit_socketcall - record audit data for sys_socketcall
2082 * @nargs: number of args
2085 * Returns 0 for success or NULL context or < 0 on error.
2087 int audit_socketcall(int nargs, unsigned long *args)
2089 struct audit_aux_data_socketcall *ax;
2090 struct audit_context *context = current->audit_context;
2092 if (likely(!context || context->dummy))
2095 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2100 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2102 ax->d.type = AUDIT_SOCKETCALL;
2103 ax->d.next = context->aux;
2104 context->aux = (void *)ax;
2109 * __audit_fd_pair - record audit data for pipe and socketpair
2110 * @fd1: the first file descriptor
2111 * @fd2: the second file descriptor
2113 * Returns 0 for success or NULL context or < 0 on error.
2115 int __audit_fd_pair(int fd1, int fd2)
2117 struct audit_context *context = current->audit_context;
2118 struct audit_aux_data_fd_pair *ax;
2120 if (likely(!context)) {
2124 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2132 ax->d.type = AUDIT_FD_PAIR;
2133 ax->d.next = context->aux;
2134 context->aux = (void *)ax;
2139 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2140 * @len: data length in user space
2141 * @a: data address in kernel space
2143 * Returns 0 for success or NULL context or < 0 on error.
2145 int audit_sockaddr(int len, void *a)
2147 struct audit_aux_data_sockaddr *ax;
2148 struct audit_context *context = current->audit_context;
2150 if (likely(!context || context->dummy))
2153 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2158 memcpy(ax->a, a, len);
2160 ax->d.type = AUDIT_SOCKADDR;
2161 ax->d.next = context->aux;
2162 context->aux = (void *)ax;
2166 void __audit_ptrace(struct task_struct *t)
2168 struct audit_context *context = current->audit_context;
2170 context->target_pid = t->pid;
2171 selinux_get_task_sid(t, &context->target_sid);
2175 * audit_signal_info - record signal info for shutting down audit subsystem
2176 * @sig: signal value
2177 * @t: task being signaled
2179 * If the audit subsystem is being terminated, record the task (pid)
2180 * and uid that is doing that.
2182 int __audit_signal_info(int sig, struct task_struct *t)
2184 struct audit_aux_data_pids *axp;
2185 struct task_struct *tsk = current;
2186 struct audit_context *ctx = tsk->audit_context;
2187 extern pid_t audit_sig_pid;
2188 extern uid_t audit_sig_uid;
2189 extern u32 audit_sig_sid;
2191 if (audit_pid && t->tgid == audit_pid) {
2192 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2193 audit_sig_pid = tsk->pid;
2194 if (tsk->loginuid != -1)
2195 audit_sig_uid = tsk->loginuid;
2197 audit_sig_uid = tsk->uid;
2198 selinux_get_task_sid(tsk, &audit_sig_sid);
2200 if (!audit_signals || audit_dummy_context())
2204 /* optimize the common case by putting first signal recipient directly
2205 * in audit_context */
2206 if (!ctx->target_pid) {
2207 ctx->target_pid = t->tgid;
2208 selinux_get_task_sid(t, &ctx->target_sid);
2212 axp = (void *)ctx->aux_pids;
2213 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2214 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2218 axp->d.type = AUDIT_OBJ_PID;
2219 axp->d.next = ctx->aux_pids;
2220 ctx->aux_pids = (void *)axp;
2222 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2224 axp->target_pid[axp->pid_count] = t->tgid;
2225 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2232 * audit_core_dumps - record information about processes that end abnormally
2233 * @signr: signal value
2235 * If a process ends with a core dump, something fishy is going on and we
2236 * should record the event for investigation.
2238 void audit_core_dumps(long signr)
2240 struct audit_buffer *ab;
2246 if (signr == SIGQUIT) /* don't care for those */
2249 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2250 audit_log_format(ab, "auid=%u uid=%u gid=%u",
2251 audit_get_loginuid(current),
2252 current->uid, current->gid);
2253 selinux_get_task_sid(current, &sid);
2258 if (selinux_sid_to_string(sid, &ctx, &len))
2259 audit_log_format(ab, " ssid=%u", sid);
2261 audit_log_format(ab, " subj=%s", ctx);
2264 audit_log_format(ab, " pid=%d comm=", current->pid);
2265 audit_log_untrustedstring(ab, current->comm);
2266 audit_log_format(ab, " sig=%ld", signr);