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;
706 * we need to fix up the return code in the audit logs if the actual
707 * return codes are later going to be fixed up by the arch specific
710 * This is actually a test for:
711 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
712 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
714 * but is faster than a bunch of ||
716 if (unlikely(return_code <= -ERESTARTSYS) &&
717 (return_code >= -ERESTART_RESTARTBLOCK) &&
718 (return_code != -ENOIOCTLCMD))
719 context->return_code = -EINTR;
721 context->return_code = return_code;
723 if (context->in_syscall && !context->dummy && !context->auditable) {
724 enum audit_state state;
726 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
727 if (state == AUDIT_RECORD_CONTEXT) {
728 context->auditable = 1;
732 state = audit_filter_inodes(tsk, context);
733 if (state == AUDIT_RECORD_CONTEXT)
734 context->auditable = 1;
740 tsk->audit_context = NULL;
744 static inline void audit_free_names(struct audit_context *context)
749 if (context->auditable
750 ||context->put_count + context->ino_count != context->name_count) {
751 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
752 " name_count=%d put_count=%d"
753 " ino_count=%d [NOT freeing]\n",
755 context->serial, context->major, context->in_syscall,
756 context->name_count, context->put_count,
758 for (i = 0; i < context->name_count; i++) {
759 printk(KERN_ERR "names[%d] = %p = %s\n", i,
760 context->names[i].name,
761 context->names[i].name ?: "(null)");
768 context->put_count = 0;
769 context->ino_count = 0;
772 for (i = 0; i < context->name_count; i++) {
773 if (context->names[i].name && context->names[i].name_put)
774 __putname(context->names[i].name);
776 context->name_count = 0;
780 mntput(context->pwdmnt);
782 context->pwdmnt = NULL;
785 static inline void audit_free_aux(struct audit_context *context)
787 struct audit_aux_data *aux;
789 while ((aux = context->aux)) {
790 context->aux = aux->next;
793 while ((aux = context->aux_pids)) {
794 context->aux_pids = aux->next;
799 static inline void audit_zero_context(struct audit_context *context,
800 enum audit_state state)
802 memset(context, 0, sizeof(*context));
803 context->state = state;
806 static inline struct audit_context *audit_alloc_context(enum audit_state state)
808 struct audit_context *context;
810 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
812 audit_zero_context(context, state);
817 * audit_alloc - allocate an audit context block for a task
820 * Filter on the task information and allocate a per-task audit context
821 * if necessary. Doing so turns on system call auditing for the
822 * specified task. This is called from copy_process, so no lock is
825 int audit_alloc(struct task_struct *tsk)
827 struct audit_context *context;
828 enum audit_state state;
830 if (likely(!audit_enabled))
831 return 0; /* Return if not auditing. */
833 state = audit_filter_task(tsk);
834 if (likely(state == AUDIT_DISABLED))
837 if (!(context = audit_alloc_context(state))) {
838 audit_log_lost("out of memory in audit_alloc");
842 tsk->audit_context = context;
843 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
847 static inline void audit_free_context(struct audit_context *context)
849 struct audit_context *previous;
853 previous = context->previous;
854 if (previous || (count && count < 10)) {
856 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
857 " freeing multiple contexts (%d)\n",
858 context->serial, context->major,
859 context->name_count, count);
861 audit_free_names(context);
862 unroll_tree_refs(context, NULL, 0);
863 free_tree_refs(context);
864 audit_free_aux(context);
865 kfree(context->filterkey);
870 printk(KERN_ERR "audit: freed %d contexts\n", count);
873 void audit_log_task_context(struct audit_buffer *ab)
880 selinux_get_task_sid(current, &sid);
884 error = selinux_sid_to_string(sid, &ctx, &len);
886 if (error != -EINVAL)
891 audit_log_format(ab, " subj=%s", ctx);
896 audit_panic("error in audit_log_task_context");
900 EXPORT_SYMBOL(audit_log_task_context);
902 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
904 char name[sizeof(tsk->comm)];
905 struct mm_struct *mm = tsk->mm;
906 struct vm_area_struct *vma;
910 get_task_comm(name, tsk);
911 audit_log_format(ab, " comm=");
912 audit_log_untrustedstring(ab, name);
915 down_read(&mm->mmap_sem);
918 if ((vma->vm_flags & VM_EXECUTABLE) &&
920 audit_log_d_path(ab, "exe=",
921 vma->vm_file->f_path.dentry,
922 vma->vm_file->f_path.mnt);
927 up_read(&mm->mmap_sem);
929 audit_log_task_context(ab);
932 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
935 struct audit_buffer *ab;
940 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
944 if (selinux_sid_to_string(sid, &s, &len)) {
945 audit_log_format(ab, "opid=%d obj=(none)", pid);
948 audit_log_format(ab, "opid=%d obj=%s", pid, s);
955 static void audit_log_execve_info(struct audit_buffer *ab,
956 struct audit_aux_data_execve *axi)
960 const char __user *p;
963 if (axi->mm != current->mm)
964 return; /* execve failed, no additional info */
966 p = (const char __user *)axi->mm->arg_start;
968 for (i = 0; i < axi->argc; i++, p += len) {
969 len = strnlen_user(p, MAX_ARG_STRLEN);
971 * We just created this mm, if we can't find the strings
972 * we just copied into it something is _very_ wrong. Similar
973 * for strings that are too long, we should not have created
976 if (!len || len > MAX_ARG_STRLEN) {
978 send_sig(SIGKILL, current, 0);
981 buf = kmalloc(len, GFP_KERNEL);
983 audit_panic("out of memory for argv string\n");
987 ret = copy_from_user(buf, p, len);
989 * There is no reason for this copy to be short. We just
990 * copied them here, and the mm hasn't been exposed to user-
995 send_sig(SIGKILL, current, 0);
998 audit_log_format(ab, "a%d=", i);
999 audit_log_untrustedstring(ab, buf);
1000 audit_log_format(ab, "\n");
1006 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1008 int i, call_panic = 0;
1009 struct audit_buffer *ab;
1010 struct audit_aux_data *aux;
1013 /* tsk == current */
1014 context->pid = tsk->pid;
1016 context->ppid = sys_getppid();
1017 context->uid = tsk->uid;
1018 context->gid = tsk->gid;
1019 context->euid = tsk->euid;
1020 context->suid = tsk->suid;
1021 context->fsuid = tsk->fsuid;
1022 context->egid = tsk->egid;
1023 context->sgid = tsk->sgid;
1024 context->fsgid = tsk->fsgid;
1025 context->personality = tsk->personality;
1027 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1029 return; /* audit_panic has been called */
1030 audit_log_format(ab, "arch=%x syscall=%d",
1031 context->arch, context->major);
1032 if (context->personality != PER_LINUX)
1033 audit_log_format(ab, " per=%lx", context->personality);
1034 if (context->return_valid)
1035 audit_log_format(ab, " success=%s exit=%ld",
1036 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1037 context->return_code);
1039 mutex_lock(&tty_mutex);
1040 read_lock(&tasklist_lock);
1041 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1042 tty = tsk->signal->tty->name;
1045 read_unlock(&tasklist_lock);
1046 audit_log_format(ab,
1047 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1048 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1049 " euid=%u suid=%u fsuid=%u"
1050 " egid=%u sgid=%u fsgid=%u tty=%s",
1055 context->name_count,
1061 context->euid, context->suid, context->fsuid,
1062 context->egid, context->sgid, context->fsgid, tty);
1064 mutex_unlock(&tty_mutex);
1066 audit_log_task_info(ab, tsk);
1067 if (context->filterkey) {
1068 audit_log_format(ab, " key=");
1069 audit_log_untrustedstring(ab, context->filterkey);
1071 audit_log_format(ab, " key=(null)");
1074 for (aux = context->aux; aux; aux = aux->next) {
1076 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1078 continue; /* audit_panic has been called */
1080 switch (aux->type) {
1081 case AUDIT_MQ_OPEN: {
1082 struct audit_aux_data_mq_open *axi = (void *)aux;
1083 audit_log_format(ab,
1084 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1085 "mq_msgsize=%ld mq_curmsgs=%ld",
1086 axi->oflag, axi->mode, axi->attr.mq_flags,
1087 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1088 axi->attr.mq_curmsgs);
1091 case AUDIT_MQ_SENDRECV: {
1092 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1093 audit_log_format(ab,
1094 "mqdes=%d msg_len=%zd msg_prio=%u "
1095 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1096 axi->mqdes, axi->msg_len, axi->msg_prio,
1097 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1100 case AUDIT_MQ_NOTIFY: {
1101 struct audit_aux_data_mq_notify *axi = (void *)aux;
1102 audit_log_format(ab,
1103 "mqdes=%d sigev_signo=%d",
1105 axi->notification.sigev_signo);
1108 case AUDIT_MQ_GETSETATTR: {
1109 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1110 audit_log_format(ab,
1111 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1114 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1115 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1119 struct audit_aux_data_ipcctl *axi = (void *)aux;
1120 audit_log_format(ab,
1121 "ouid=%u ogid=%u mode=%#o",
1122 axi->uid, axi->gid, axi->mode);
1123 if (axi->osid != 0) {
1126 if (selinux_sid_to_string(
1127 axi->osid, &ctx, &len)) {
1128 audit_log_format(ab, " osid=%u",
1132 audit_log_format(ab, " obj=%s", ctx);
1137 case AUDIT_IPC_SET_PERM: {
1138 struct audit_aux_data_ipcctl *axi = (void *)aux;
1139 audit_log_format(ab,
1140 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1141 axi->qbytes, axi->uid, axi->gid, axi->mode);
1144 case AUDIT_EXECVE: {
1145 struct audit_aux_data_execve *axi = (void *)aux;
1146 audit_log_execve_info(ab, axi);
1149 case AUDIT_SOCKETCALL: {
1151 struct audit_aux_data_socketcall *axs = (void *)aux;
1152 audit_log_format(ab, "nargs=%d", axs->nargs);
1153 for (i=0; i<axs->nargs; i++)
1154 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1157 case AUDIT_SOCKADDR: {
1158 struct audit_aux_data_sockaddr *axs = (void *)aux;
1160 audit_log_format(ab, "saddr=");
1161 audit_log_hex(ab, axs->a, axs->len);
1164 case AUDIT_FD_PAIR: {
1165 struct audit_aux_data_fd_pair *axs = (void *)aux;
1166 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1173 for (aux = context->aux_pids; aux; aux = aux->next) {
1174 struct audit_aux_data_pids *axs = (void *)aux;
1177 for (i = 0; i < axs->pid_count; i++)
1178 if (audit_log_pid_context(context, axs->target_pid[i],
1179 axs->target_sid[i]))
1183 if (context->target_pid &&
1184 audit_log_pid_context(context, context->target_pid,
1185 context->target_sid))
1188 if (context->pwd && context->pwdmnt) {
1189 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1191 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1195 for (i = 0; i < context->name_count; i++) {
1196 struct audit_names *n = &context->names[i];
1198 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1200 continue; /* audit_panic has been called */
1202 audit_log_format(ab, "item=%d", i);
1205 switch(n->name_len) {
1206 case AUDIT_NAME_FULL:
1207 /* log the full path */
1208 audit_log_format(ab, " name=");
1209 audit_log_untrustedstring(ab, n->name);
1212 /* name was specified as a relative path and the
1213 * directory component is the cwd */
1214 audit_log_d_path(ab, " name=", context->pwd,
1218 /* log the name's directory component */
1219 audit_log_format(ab, " name=");
1220 audit_log_n_untrustedstring(ab, n->name_len,
1224 audit_log_format(ab, " name=(null)");
1226 if (n->ino != (unsigned long)-1) {
1227 audit_log_format(ab, " inode=%lu"
1228 " dev=%02x:%02x mode=%#o"
1229 " ouid=%u ogid=%u rdev=%02x:%02x",
1242 if (selinux_sid_to_string(
1243 n->osid, &ctx, &len)) {
1244 audit_log_format(ab, " osid=%u", n->osid);
1247 audit_log_format(ab, " obj=%s", ctx);
1254 audit_panic("error converting sid to string");
1258 * audit_free - free a per-task audit context
1259 * @tsk: task whose audit context block to free
1261 * Called from copy_process and do_exit
1263 void audit_free(struct task_struct *tsk)
1265 struct audit_context *context;
1267 context = audit_get_context(tsk, 0, 0);
1268 if (likely(!context))
1271 /* Check for system calls that do not go through the exit
1272 * function (e.g., exit_group), then free context block.
1273 * We use GFP_ATOMIC here because we might be doing this
1274 * in the context of the idle thread */
1275 /* that can happen only if we are called from do_exit() */
1276 if (context->in_syscall && context->auditable)
1277 audit_log_exit(context, tsk);
1279 audit_free_context(context);
1283 * audit_syscall_entry - fill in an audit record at syscall entry
1284 * @tsk: task being audited
1285 * @arch: architecture type
1286 * @major: major syscall type (function)
1287 * @a1: additional syscall register 1
1288 * @a2: additional syscall register 2
1289 * @a3: additional syscall register 3
1290 * @a4: additional syscall register 4
1292 * Fill in audit context at syscall entry. This only happens if the
1293 * audit context was created when the task was created and the state or
1294 * filters demand the audit context be built. If the state from the
1295 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1296 * then the record will be written at syscall exit time (otherwise, it
1297 * will only be written if another part of the kernel requests that it
1300 void audit_syscall_entry(int arch, int major,
1301 unsigned long a1, unsigned long a2,
1302 unsigned long a3, unsigned long a4)
1304 struct task_struct *tsk = current;
1305 struct audit_context *context = tsk->audit_context;
1306 enum audit_state state;
1311 * This happens only on certain architectures that make system
1312 * calls in kernel_thread via the entry.S interface, instead of
1313 * with direct calls. (If you are porting to a new
1314 * architecture, hitting this condition can indicate that you
1315 * got the _exit/_leave calls backward in entry.S.)
1319 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1321 * This also happens with vm86 emulation in a non-nested manner
1322 * (entries without exits), so this case must be caught.
1324 if (context->in_syscall) {
1325 struct audit_context *newctx;
1329 "audit(:%d) pid=%d in syscall=%d;"
1330 " entering syscall=%d\n",
1331 context->serial, tsk->pid, context->major, major);
1333 newctx = audit_alloc_context(context->state);
1335 newctx->previous = context;
1337 tsk->audit_context = newctx;
1339 /* If we can't alloc a new context, the best we
1340 * can do is to leak memory (any pending putname
1341 * will be lost). The only other alternative is
1342 * to abandon auditing. */
1343 audit_zero_context(context, context->state);
1346 BUG_ON(context->in_syscall || context->name_count);
1351 context->arch = arch;
1352 context->major = major;
1353 context->argv[0] = a1;
1354 context->argv[1] = a2;
1355 context->argv[2] = a3;
1356 context->argv[3] = a4;
1358 state = context->state;
1359 context->dummy = !audit_n_rules;
1360 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1361 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1362 if (likely(state == AUDIT_DISABLED))
1365 context->serial = 0;
1366 context->ctime = CURRENT_TIME;
1367 context->in_syscall = 1;
1368 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1373 * audit_syscall_exit - deallocate audit context after a system call
1374 * @tsk: task being audited
1375 * @valid: success/failure flag
1376 * @return_code: syscall return value
1378 * Tear down after system call. If the audit context has been marked as
1379 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1380 * filtering, or because some other part of the kernel write an audit
1381 * message), then write out the syscall information. In call cases,
1382 * free the names stored from getname().
1384 void audit_syscall_exit(int valid, long return_code)
1386 struct task_struct *tsk = current;
1387 struct audit_context *context;
1389 context = audit_get_context(tsk, valid, return_code);
1391 if (likely(!context))
1394 if (context->in_syscall && context->auditable)
1395 audit_log_exit(context, tsk);
1397 context->in_syscall = 0;
1398 context->auditable = 0;
1400 if (context->previous) {
1401 struct audit_context *new_context = context->previous;
1402 context->previous = NULL;
1403 audit_free_context(context);
1404 tsk->audit_context = new_context;
1406 audit_free_names(context);
1407 unroll_tree_refs(context, NULL, 0);
1408 audit_free_aux(context);
1409 context->aux = NULL;
1410 context->aux_pids = NULL;
1411 context->target_pid = 0;
1412 context->target_sid = 0;
1413 kfree(context->filterkey);
1414 context->filterkey = NULL;
1415 tsk->audit_context = context;
1419 static inline void handle_one(const struct inode *inode)
1421 #ifdef CONFIG_AUDIT_TREE
1422 struct audit_context *context;
1423 struct audit_tree_refs *p;
1424 struct audit_chunk *chunk;
1426 if (likely(list_empty(&inode->inotify_watches)))
1428 context = current->audit_context;
1430 count = context->tree_count;
1432 chunk = audit_tree_lookup(inode);
1436 if (likely(put_tree_ref(context, chunk)))
1438 if (unlikely(!grow_tree_refs(context))) {
1439 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1440 audit_set_auditable(context);
1441 audit_put_chunk(chunk);
1442 unroll_tree_refs(context, p, count);
1445 put_tree_ref(context, chunk);
1449 static void handle_path(const struct dentry *dentry)
1451 #ifdef CONFIG_AUDIT_TREE
1452 struct audit_context *context;
1453 struct audit_tree_refs *p;
1454 const struct dentry *d, *parent;
1455 struct audit_chunk *drop;
1459 context = current->audit_context;
1461 count = context->tree_count;
1466 seq = read_seqbegin(&rename_lock);
1468 struct inode *inode = d->d_inode;
1469 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1470 struct audit_chunk *chunk;
1471 chunk = audit_tree_lookup(inode);
1473 if (unlikely(!put_tree_ref(context, chunk))) {
1479 parent = d->d_parent;
1484 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1487 /* just a race with rename */
1488 unroll_tree_refs(context, p, count);
1491 audit_put_chunk(drop);
1492 if (grow_tree_refs(context)) {
1493 /* OK, got more space */
1494 unroll_tree_refs(context, p, count);
1499 "out of memory, audit has lost a tree reference");
1500 unroll_tree_refs(context, p, count);
1501 audit_set_auditable(context);
1509 * audit_getname - add a name to the list
1510 * @name: name to add
1512 * Add a name to the list of audit names for this context.
1513 * Called from fs/namei.c:getname().
1515 void __audit_getname(const char *name)
1517 struct audit_context *context = current->audit_context;
1519 if (IS_ERR(name) || !name)
1522 if (!context->in_syscall) {
1523 #if AUDIT_DEBUG == 2
1524 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1525 __FILE__, __LINE__, context->serial, name);
1530 BUG_ON(context->name_count >= AUDIT_NAMES);
1531 context->names[context->name_count].name = name;
1532 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1533 context->names[context->name_count].name_put = 1;
1534 context->names[context->name_count].ino = (unsigned long)-1;
1535 context->names[context->name_count].osid = 0;
1536 ++context->name_count;
1537 if (!context->pwd) {
1538 read_lock(¤t->fs->lock);
1539 context->pwd = dget(current->fs->pwd);
1540 context->pwdmnt = mntget(current->fs->pwdmnt);
1541 read_unlock(¤t->fs->lock);
1546 /* audit_putname - intercept a putname request
1547 * @name: name to intercept and delay for putname
1549 * If we have stored the name from getname in the audit context,
1550 * then we delay the putname until syscall exit.
1551 * Called from include/linux/fs.h:putname().
1553 void audit_putname(const char *name)
1555 struct audit_context *context = current->audit_context;
1558 if (!context->in_syscall) {
1559 #if AUDIT_DEBUG == 2
1560 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1561 __FILE__, __LINE__, context->serial, name);
1562 if (context->name_count) {
1564 for (i = 0; i < context->name_count; i++)
1565 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1566 context->names[i].name,
1567 context->names[i].name ?: "(null)");
1574 ++context->put_count;
1575 if (context->put_count > context->name_count) {
1576 printk(KERN_ERR "%s:%d(:%d): major=%d"
1577 " in_syscall=%d putname(%p) name_count=%d"
1580 context->serial, context->major,
1581 context->in_syscall, name, context->name_count,
1582 context->put_count);
1589 static int audit_inc_name_count(struct audit_context *context,
1590 const struct inode *inode)
1592 if (context->name_count >= AUDIT_NAMES) {
1594 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1595 "dev=%02x:%02x, inode=%lu",
1596 MAJOR(inode->i_sb->s_dev),
1597 MINOR(inode->i_sb->s_dev),
1601 printk(KERN_DEBUG "name_count maxed, losing inode data");
1604 context->name_count++;
1606 context->ino_count++;
1611 /* Copy inode data into an audit_names. */
1612 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1614 name->ino = inode->i_ino;
1615 name->dev = inode->i_sb->s_dev;
1616 name->mode = inode->i_mode;
1617 name->uid = inode->i_uid;
1618 name->gid = inode->i_gid;
1619 name->rdev = inode->i_rdev;
1620 selinux_get_inode_sid(inode, &name->osid);
1624 * audit_inode - store the inode and device from a lookup
1625 * @name: name being audited
1626 * @dentry: dentry being audited
1628 * Called from fs/namei.c:path_lookup().
1630 void __audit_inode(const char *name, const struct dentry *dentry)
1633 struct audit_context *context = current->audit_context;
1634 const struct inode *inode = dentry->d_inode;
1636 if (!context->in_syscall)
1638 if (context->name_count
1639 && context->names[context->name_count-1].name
1640 && context->names[context->name_count-1].name == name)
1641 idx = context->name_count - 1;
1642 else if (context->name_count > 1
1643 && context->names[context->name_count-2].name
1644 && context->names[context->name_count-2].name == name)
1645 idx = context->name_count - 2;
1647 /* FIXME: how much do we care about inodes that have no
1648 * associated name? */
1649 if (audit_inc_name_count(context, inode))
1651 idx = context->name_count - 1;
1652 context->names[idx].name = NULL;
1654 handle_path(dentry);
1655 audit_copy_inode(&context->names[idx], inode);
1659 * audit_inode_child - collect inode info for created/removed objects
1660 * @dname: inode's dentry name
1661 * @dentry: dentry being audited
1662 * @parent: inode of dentry parent
1664 * For syscalls that create or remove filesystem objects, audit_inode
1665 * can only collect information for the filesystem object's parent.
1666 * This call updates the audit context with the child's information.
1667 * Syscalls that create a new filesystem object must be hooked after
1668 * the object is created. Syscalls that remove a filesystem object
1669 * must be hooked prior, in order to capture the target inode during
1670 * unsuccessful attempts.
1672 void __audit_inode_child(const char *dname, const struct dentry *dentry,
1673 const struct inode *parent)
1676 struct audit_context *context = current->audit_context;
1677 const char *found_parent = NULL, *found_child = NULL;
1678 const struct inode *inode = dentry->d_inode;
1681 if (!context->in_syscall)
1686 /* determine matching parent */
1690 /* parent is more likely, look for it first */
1691 for (idx = 0; idx < context->name_count; idx++) {
1692 struct audit_names *n = &context->names[idx];
1697 if (n->ino == parent->i_ino &&
1698 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1699 n->name_len = dirlen; /* update parent data in place */
1700 found_parent = n->name;
1705 /* no matching parent, look for matching child */
1706 for (idx = 0; idx < context->name_count; idx++) {
1707 struct audit_names *n = &context->names[idx];
1712 /* strcmp() is the more likely scenario */
1713 if (!strcmp(dname, n->name) ||
1714 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1716 audit_copy_inode(n, inode);
1718 n->ino = (unsigned long)-1;
1719 found_child = n->name;
1725 if (!found_parent) {
1726 if (audit_inc_name_count(context, parent))
1728 idx = context->name_count - 1;
1729 context->names[idx].name = NULL;
1730 audit_copy_inode(&context->names[idx], parent);
1734 if (audit_inc_name_count(context, inode))
1736 idx = context->name_count - 1;
1738 /* Re-use the name belonging to the slot for a matching parent
1739 * directory. All names for this context are relinquished in
1740 * audit_free_names() */
1742 context->names[idx].name = found_parent;
1743 context->names[idx].name_len = AUDIT_NAME_FULL;
1744 /* don't call __putname() */
1745 context->names[idx].name_put = 0;
1747 context->names[idx].name = NULL;
1751 audit_copy_inode(&context->names[idx], inode);
1753 context->names[idx].ino = (unsigned long)-1;
1756 EXPORT_SYMBOL_GPL(__audit_inode_child);
1759 * auditsc_get_stamp - get local copies of audit_context values
1760 * @ctx: audit_context for the task
1761 * @t: timespec to store time recorded in the audit_context
1762 * @serial: serial value that is recorded in the audit_context
1764 * Also sets the context as auditable.
1766 void auditsc_get_stamp(struct audit_context *ctx,
1767 struct timespec *t, unsigned int *serial)
1770 ctx->serial = audit_serial();
1771 t->tv_sec = ctx->ctime.tv_sec;
1772 t->tv_nsec = ctx->ctime.tv_nsec;
1773 *serial = ctx->serial;
1778 * audit_set_loginuid - set a task's audit_context loginuid
1779 * @task: task whose audit context is being modified
1780 * @loginuid: loginuid value
1784 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1786 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1788 struct audit_context *context = task->audit_context;
1790 if (context && context->in_syscall) {
1791 struct audit_buffer *ab;
1793 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1795 audit_log_format(ab, "login pid=%d uid=%u "
1796 "old auid=%u new auid=%u",
1797 task->pid, task->uid,
1798 task->loginuid, loginuid);
1802 task->loginuid = loginuid;
1807 * __audit_mq_open - record audit data for a POSIX MQ open
1810 * @u_attr: queue attributes
1812 * Returns 0 for success or NULL context or < 0 on error.
1814 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1816 struct audit_aux_data_mq_open *ax;
1817 struct audit_context *context = current->audit_context;
1822 if (likely(!context))
1825 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1829 if (u_attr != NULL) {
1830 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1835 memset(&ax->attr, 0, sizeof(ax->attr));
1840 ax->d.type = AUDIT_MQ_OPEN;
1841 ax->d.next = context->aux;
1842 context->aux = (void *)ax;
1847 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1848 * @mqdes: MQ descriptor
1849 * @msg_len: Message length
1850 * @msg_prio: Message priority
1851 * @u_abs_timeout: Message timeout in absolute time
1853 * Returns 0 for success or NULL context or < 0 on error.
1855 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1856 const struct timespec __user *u_abs_timeout)
1858 struct audit_aux_data_mq_sendrecv *ax;
1859 struct audit_context *context = current->audit_context;
1864 if (likely(!context))
1867 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1871 if (u_abs_timeout != NULL) {
1872 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1877 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1880 ax->msg_len = msg_len;
1881 ax->msg_prio = msg_prio;
1883 ax->d.type = AUDIT_MQ_SENDRECV;
1884 ax->d.next = context->aux;
1885 context->aux = (void *)ax;
1890 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1891 * @mqdes: MQ descriptor
1892 * @msg_len: Message length
1893 * @u_msg_prio: Message priority
1894 * @u_abs_timeout: Message timeout in absolute time
1896 * Returns 0 for success or NULL context or < 0 on error.
1898 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1899 unsigned int __user *u_msg_prio,
1900 const struct timespec __user *u_abs_timeout)
1902 struct audit_aux_data_mq_sendrecv *ax;
1903 struct audit_context *context = current->audit_context;
1908 if (likely(!context))
1911 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1915 if (u_msg_prio != NULL) {
1916 if (get_user(ax->msg_prio, u_msg_prio)) {
1923 if (u_abs_timeout != NULL) {
1924 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1929 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1932 ax->msg_len = msg_len;
1934 ax->d.type = AUDIT_MQ_SENDRECV;
1935 ax->d.next = context->aux;
1936 context->aux = (void *)ax;
1941 * __audit_mq_notify - record audit data for a POSIX MQ notify
1942 * @mqdes: MQ descriptor
1943 * @u_notification: Notification event
1945 * Returns 0 for success or NULL context or < 0 on error.
1948 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1950 struct audit_aux_data_mq_notify *ax;
1951 struct audit_context *context = current->audit_context;
1956 if (likely(!context))
1959 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1963 if (u_notification != NULL) {
1964 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1969 memset(&ax->notification, 0, sizeof(ax->notification));
1973 ax->d.type = AUDIT_MQ_NOTIFY;
1974 ax->d.next = context->aux;
1975 context->aux = (void *)ax;
1980 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1981 * @mqdes: MQ descriptor
1984 * Returns 0 for success or NULL context or < 0 on error.
1986 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1988 struct audit_aux_data_mq_getsetattr *ax;
1989 struct audit_context *context = current->audit_context;
1994 if (likely(!context))
1997 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2002 ax->mqstat = *mqstat;
2004 ax->d.type = AUDIT_MQ_GETSETATTR;
2005 ax->d.next = context->aux;
2006 context->aux = (void *)ax;
2011 * audit_ipc_obj - record audit data for ipc object
2012 * @ipcp: ipc permissions
2014 * Returns 0 for success or NULL context or < 0 on error.
2016 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2018 struct audit_aux_data_ipcctl *ax;
2019 struct audit_context *context = current->audit_context;
2021 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2025 ax->uid = ipcp->uid;
2026 ax->gid = ipcp->gid;
2027 ax->mode = ipcp->mode;
2028 selinux_get_ipc_sid(ipcp, &ax->osid);
2030 ax->d.type = AUDIT_IPC;
2031 ax->d.next = context->aux;
2032 context->aux = (void *)ax;
2037 * audit_ipc_set_perm - record audit data for new ipc permissions
2038 * @qbytes: msgq bytes
2039 * @uid: msgq user id
2040 * @gid: msgq group id
2041 * @mode: msgq mode (permissions)
2043 * Returns 0 for success or NULL context or < 0 on error.
2045 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2047 struct audit_aux_data_ipcctl *ax;
2048 struct audit_context *context = current->audit_context;
2050 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2054 ax->qbytes = qbytes;
2059 ax->d.type = AUDIT_IPC_SET_PERM;
2060 ax->d.next = context->aux;
2061 context->aux = (void *)ax;
2065 int audit_argv_kb = 32;
2067 int audit_bprm(struct linux_binprm *bprm)
2069 struct audit_aux_data_execve *ax;
2070 struct audit_context *context = current->audit_context;
2072 if (likely(!audit_enabled || !context || context->dummy))
2076 * Even though the stack code doesn't limit the arg+env size any more,
2077 * the audit code requires that _all_ arguments be logged in a single
2078 * netlink skb. Hence cap it :-(
2080 if (bprm->argv_len > (audit_argv_kb << 10))
2083 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2087 ax->argc = bprm->argc;
2088 ax->envc = bprm->envc;
2090 ax->d.type = AUDIT_EXECVE;
2091 ax->d.next = context->aux;
2092 context->aux = (void *)ax;
2098 * audit_socketcall - record audit data for sys_socketcall
2099 * @nargs: number of args
2102 * Returns 0 for success or NULL context or < 0 on error.
2104 int audit_socketcall(int nargs, unsigned long *args)
2106 struct audit_aux_data_socketcall *ax;
2107 struct audit_context *context = current->audit_context;
2109 if (likely(!context || context->dummy))
2112 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2117 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2119 ax->d.type = AUDIT_SOCKETCALL;
2120 ax->d.next = context->aux;
2121 context->aux = (void *)ax;
2126 * __audit_fd_pair - record audit data for pipe and socketpair
2127 * @fd1: the first file descriptor
2128 * @fd2: the second file descriptor
2130 * Returns 0 for success or NULL context or < 0 on error.
2132 int __audit_fd_pair(int fd1, int fd2)
2134 struct audit_context *context = current->audit_context;
2135 struct audit_aux_data_fd_pair *ax;
2137 if (likely(!context)) {
2141 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2149 ax->d.type = AUDIT_FD_PAIR;
2150 ax->d.next = context->aux;
2151 context->aux = (void *)ax;
2156 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2157 * @len: data length in user space
2158 * @a: data address in kernel space
2160 * Returns 0 for success or NULL context or < 0 on error.
2162 int audit_sockaddr(int len, void *a)
2164 struct audit_aux_data_sockaddr *ax;
2165 struct audit_context *context = current->audit_context;
2167 if (likely(!context || context->dummy))
2170 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2175 memcpy(ax->a, a, len);
2177 ax->d.type = AUDIT_SOCKADDR;
2178 ax->d.next = context->aux;
2179 context->aux = (void *)ax;
2183 void __audit_ptrace(struct task_struct *t)
2185 struct audit_context *context = current->audit_context;
2187 context->target_pid = t->pid;
2188 selinux_get_task_sid(t, &context->target_sid);
2192 * audit_signal_info - record signal info for shutting down audit subsystem
2193 * @sig: signal value
2194 * @t: task being signaled
2196 * If the audit subsystem is being terminated, record the task (pid)
2197 * and uid that is doing that.
2199 int __audit_signal_info(int sig, struct task_struct *t)
2201 struct audit_aux_data_pids *axp;
2202 struct task_struct *tsk = current;
2203 struct audit_context *ctx = tsk->audit_context;
2204 extern pid_t audit_sig_pid;
2205 extern uid_t audit_sig_uid;
2206 extern u32 audit_sig_sid;
2208 if (audit_pid && t->tgid == audit_pid) {
2209 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2210 audit_sig_pid = tsk->pid;
2211 if (tsk->loginuid != -1)
2212 audit_sig_uid = tsk->loginuid;
2214 audit_sig_uid = tsk->uid;
2215 selinux_get_task_sid(tsk, &audit_sig_sid);
2217 if (!audit_signals || audit_dummy_context())
2221 /* optimize the common case by putting first signal recipient directly
2222 * in audit_context */
2223 if (!ctx->target_pid) {
2224 ctx->target_pid = t->tgid;
2225 selinux_get_task_sid(t, &ctx->target_sid);
2229 axp = (void *)ctx->aux_pids;
2230 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2231 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2235 axp->d.type = AUDIT_OBJ_PID;
2236 axp->d.next = ctx->aux_pids;
2237 ctx->aux_pids = (void *)axp;
2239 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2241 axp->target_pid[axp->pid_count] = t->tgid;
2242 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2249 * audit_core_dumps - record information about processes that end abnormally
2250 * @signr: signal value
2252 * If a process ends with a core dump, something fishy is going on and we
2253 * should record the event for investigation.
2255 void audit_core_dumps(long signr)
2257 struct audit_buffer *ab;
2263 if (signr == SIGQUIT) /* don't care for those */
2266 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2267 audit_log_format(ab, "auid=%u uid=%u gid=%u",
2268 audit_get_loginuid(current),
2269 current->uid, current->gid);
2270 selinux_get_task_sid(current, &sid);
2275 if (selinux_sid_to_string(sid, &ctx, &len))
2276 audit_log_format(ab, " ssid=%u", sid);
2278 audit_log_format(ab, " subj=%s", ctx);
2281 audit_log_format(ab, " pid=%d comm=", current->pid);
2282 audit_log_untrustedstring(ab, current->comm);
2283 audit_log_format(ab, " sig=%ld", signr);