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 uid_t target_auid[AUDIT_AUX_PIDS];
180 uid_t target_uid[AUDIT_AUX_PIDS];
181 u32 target_sid[AUDIT_AUX_PIDS];
182 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
186 struct audit_tree_refs {
187 struct audit_tree_refs *next;
188 struct audit_chunk *c[31];
191 /* The per-task audit context. */
192 struct audit_context {
193 int dummy; /* must be the first element */
194 int in_syscall; /* 1 if task is in a syscall */
195 enum audit_state state;
196 unsigned int serial; /* serial number for record */
197 struct timespec ctime; /* time of syscall entry */
198 int major; /* syscall number */
199 unsigned long argv[4]; /* syscall arguments */
200 int return_valid; /* return code is valid */
201 long return_code;/* syscall return code */
202 int auditable; /* 1 if record should be written */
204 struct audit_names names[AUDIT_NAMES];
205 char * filterkey; /* key for rule that triggered record */
207 struct vfsmount * pwdmnt;
208 struct audit_context *previous; /* For nested syscalls */
209 struct audit_aux_data *aux;
210 struct audit_aux_data *aux_pids;
212 /* Save things to print about task_struct */
214 uid_t uid, euid, suid, fsuid;
215 gid_t gid, egid, sgid, fsgid;
216 unsigned long personality;
223 char target_comm[TASK_COMM_LEN];
225 struct audit_tree_refs *trees, *first_trees;
234 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
235 static inline int open_arg(int flags, int mask)
237 int n = ACC_MODE(flags);
238 if (flags & (O_TRUNC | O_CREAT))
239 n |= AUDIT_PERM_WRITE;
243 static int audit_match_perm(struct audit_context *ctx, int mask)
245 unsigned n = ctx->major;
246 switch (audit_classify_syscall(ctx->arch, n)) {
248 if ((mask & AUDIT_PERM_WRITE) &&
249 audit_match_class(AUDIT_CLASS_WRITE, n))
251 if ((mask & AUDIT_PERM_READ) &&
252 audit_match_class(AUDIT_CLASS_READ, n))
254 if ((mask & AUDIT_PERM_ATTR) &&
255 audit_match_class(AUDIT_CLASS_CHATTR, n))
258 case 1: /* 32bit on biarch */
259 if ((mask & AUDIT_PERM_WRITE) &&
260 audit_match_class(AUDIT_CLASS_WRITE_32, n))
262 if ((mask & AUDIT_PERM_READ) &&
263 audit_match_class(AUDIT_CLASS_READ_32, n))
265 if ((mask & AUDIT_PERM_ATTR) &&
266 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
270 return mask & ACC_MODE(ctx->argv[1]);
272 return mask & ACC_MODE(ctx->argv[2]);
273 case 4: /* socketcall */
274 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
276 return mask & AUDIT_PERM_EXEC;
283 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
284 * ->first_trees points to its beginning, ->trees - to the current end of data.
285 * ->tree_count is the number of free entries in array pointed to by ->trees.
286 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
287 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
288 * it's going to remain 1-element for almost any setup) until we free context itself.
289 * References in it _are_ dropped - at the same time we free/drop aux stuff.
292 #ifdef CONFIG_AUDIT_TREE
293 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
295 struct audit_tree_refs *p = ctx->trees;
296 int left = ctx->tree_count;
298 p->c[--left] = chunk;
299 ctx->tree_count = left;
308 ctx->tree_count = 30;
314 static int grow_tree_refs(struct audit_context *ctx)
316 struct audit_tree_refs *p = ctx->trees;
317 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
323 p->next = ctx->trees;
325 ctx->first_trees = ctx->trees;
326 ctx->tree_count = 31;
331 static void unroll_tree_refs(struct audit_context *ctx,
332 struct audit_tree_refs *p, int count)
334 #ifdef CONFIG_AUDIT_TREE
335 struct audit_tree_refs *q;
338 /* we started with empty chain */
339 p = ctx->first_trees;
341 /* if the very first allocation has failed, nothing to do */
346 for (q = p; q != ctx->trees; q = q->next, n = 31) {
348 audit_put_chunk(q->c[n]);
352 while (n-- > ctx->tree_count) {
353 audit_put_chunk(q->c[n]);
357 ctx->tree_count = count;
361 static void free_tree_refs(struct audit_context *ctx)
363 struct audit_tree_refs *p, *q;
364 for (p = ctx->first_trees; p; p = q) {
370 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
372 #ifdef CONFIG_AUDIT_TREE
373 struct audit_tree_refs *p;
378 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
379 for (n = 0; n < 31; n++)
380 if (audit_tree_match(p->c[n], tree))
385 for (n = ctx->tree_count; n < 31; n++)
386 if (audit_tree_match(p->c[n], tree))
393 /* Determine if any context name data matches a rule's watch data */
394 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
396 static int audit_filter_rules(struct task_struct *tsk,
397 struct audit_krule *rule,
398 struct audit_context *ctx,
399 struct audit_names *name,
400 enum audit_state *state)
402 int i, j, need_sid = 1;
405 for (i = 0; i < rule->field_count; i++) {
406 struct audit_field *f = &rule->fields[i];
411 result = audit_comparator(tsk->pid, f->op, f->val);
416 ctx->ppid = sys_getppid();
417 result = audit_comparator(ctx->ppid, f->op, f->val);
421 result = audit_comparator(tsk->uid, f->op, f->val);
424 result = audit_comparator(tsk->euid, f->op, f->val);
427 result = audit_comparator(tsk->suid, f->op, f->val);
430 result = audit_comparator(tsk->fsuid, f->op, f->val);
433 result = audit_comparator(tsk->gid, f->op, f->val);
436 result = audit_comparator(tsk->egid, f->op, f->val);
439 result = audit_comparator(tsk->sgid, f->op, f->val);
442 result = audit_comparator(tsk->fsgid, f->op, f->val);
445 result = audit_comparator(tsk->personality, f->op, f->val);
449 result = audit_comparator(ctx->arch, f->op, f->val);
453 if (ctx && ctx->return_valid)
454 result = audit_comparator(ctx->return_code, f->op, f->val);
457 if (ctx && ctx->return_valid) {
459 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
461 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
466 result = audit_comparator(MAJOR(name->dev),
469 for (j = 0; j < ctx->name_count; j++) {
470 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
479 result = audit_comparator(MINOR(name->dev),
482 for (j = 0; j < ctx->name_count; j++) {
483 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
492 result = (name->ino == f->val);
494 for (j = 0; j < ctx->name_count; j++) {
495 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
503 if (name && rule->watch->ino != (unsigned long)-1)
504 result = (name->dev == rule->watch->dev &&
505 name->ino == rule->watch->ino);
509 result = match_tree_refs(ctx, rule->tree);
514 result = audit_comparator(tsk->loginuid, f->op, f->val);
516 case AUDIT_SUBJ_USER:
517 case AUDIT_SUBJ_ROLE:
518 case AUDIT_SUBJ_TYPE:
521 /* NOTE: this may return negative values indicating
522 a temporary error. We simply treat this as a
523 match for now to avoid losing information that
524 may be wanted. An error message will also be
528 selinux_get_task_sid(tsk, &sid);
531 result = selinux_audit_rule_match(sid, f->type,
540 case AUDIT_OBJ_LEV_LOW:
541 case AUDIT_OBJ_LEV_HIGH:
542 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
545 /* Find files that match */
547 result = selinux_audit_rule_match(
548 name->osid, f->type, f->op,
551 for (j = 0; j < ctx->name_count; j++) {
552 if (selinux_audit_rule_match(
561 /* Find ipc objects that match */
563 struct audit_aux_data *aux;
564 for (aux = ctx->aux; aux;
566 if (aux->type == AUDIT_IPC) {
567 struct audit_aux_data_ipcctl *axi = (void *)aux;
568 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
582 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
584 case AUDIT_FILTERKEY:
585 /* ignore this field for filtering */
589 result = audit_match_perm(ctx, f->val);
597 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
598 switch (rule->action) {
599 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
600 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
605 /* At process creation time, we can determine if system-call auditing is
606 * completely disabled for this task. Since we only have the task
607 * structure at this point, we can only check uid and gid.
609 static enum audit_state audit_filter_task(struct task_struct *tsk)
611 struct audit_entry *e;
612 enum audit_state state;
615 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
616 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
622 return AUDIT_BUILD_CONTEXT;
625 /* At syscall entry and exit time, this filter is called if the
626 * audit_state is not low enough that auditing cannot take place, but is
627 * also not high enough that we already know we have to write an audit
628 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
630 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
631 struct audit_context *ctx,
632 struct list_head *list)
634 struct audit_entry *e;
635 enum audit_state state;
637 if (audit_pid && tsk->tgid == audit_pid)
638 return AUDIT_DISABLED;
641 if (!list_empty(list)) {
642 int word = AUDIT_WORD(ctx->major);
643 int bit = AUDIT_BIT(ctx->major);
645 list_for_each_entry_rcu(e, list, list) {
646 if ((e->rule.mask[word] & bit) == bit &&
647 audit_filter_rules(tsk, &e->rule, ctx, NULL,
655 return AUDIT_BUILD_CONTEXT;
658 /* At syscall exit time, this filter is called if any audit_names[] have been
659 * collected during syscall processing. We only check rules in sublists at hash
660 * buckets applicable to the inode numbers in audit_names[].
661 * Regarding audit_state, same rules apply as for audit_filter_syscall().
663 enum audit_state audit_filter_inodes(struct task_struct *tsk,
664 struct audit_context *ctx)
667 struct audit_entry *e;
668 enum audit_state state;
670 if (audit_pid && tsk->tgid == audit_pid)
671 return AUDIT_DISABLED;
674 for (i = 0; i < ctx->name_count; i++) {
675 int word = AUDIT_WORD(ctx->major);
676 int bit = AUDIT_BIT(ctx->major);
677 struct audit_names *n = &ctx->names[i];
678 int h = audit_hash_ino((u32)n->ino);
679 struct list_head *list = &audit_inode_hash[h];
681 if (list_empty(list))
684 list_for_each_entry_rcu(e, list, list) {
685 if ((e->rule.mask[word] & bit) == bit &&
686 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
693 return AUDIT_BUILD_CONTEXT;
696 void audit_set_auditable(struct audit_context *ctx)
701 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
705 struct audit_context *context = tsk->audit_context;
707 if (likely(!context))
709 context->return_valid = return_valid;
712 * we need to fix up the return code in the audit logs if the actual
713 * return codes are later going to be fixed up by the arch specific
716 * This is actually a test for:
717 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
718 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
720 * but is faster than a bunch of ||
722 if (unlikely(return_code <= -ERESTARTSYS) &&
723 (return_code >= -ERESTART_RESTARTBLOCK) &&
724 (return_code != -ENOIOCTLCMD))
725 context->return_code = -EINTR;
727 context->return_code = return_code;
729 if (context->in_syscall && !context->dummy && !context->auditable) {
730 enum audit_state state;
732 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
733 if (state == AUDIT_RECORD_CONTEXT) {
734 context->auditable = 1;
738 state = audit_filter_inodes(tsk, context);
739 if (state == AUDIT_RECORD_CONTEXT)
740 context->auditable = 1;
746 tsk->audit_context = NULL;
750 static inline void audit_free_names(struct audit_context *context)
755 if (context->auditable
756 ||context->put_count + context->ino_count != context->name_count) {
757 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
758 " name_count=%d put_count=%d"
759 " ino_count=%d [NOT freeing]\n",
761 context->serial, context->major, context->in_syscall,
762 context->name_count, context->put_count,
764 for (i = 0; i < context->name_count; i++) {
765 printk(KERN_ERR "names[%d] = %p = %s\n", i,
766 context->names[i].name,
767 context->names[i].name ?: "(null)");
774 context->put_count = 0;
775 context->ino_count = 0;
778 for (i = 0; i < context->name_count; i++) {
779 if (context->names[i].name && context->names[i].name_put)
780 __putname(context->names[i].name);
782 context->name_count = 0;
786 mntput(context->pwdmnt);
788 context->pwdmnt = NULL;
791 static inline void audit_free_aux(struct audit_context *context)
793 struct audit_aux_data *aux;
795 while ((aux = context->aux)) {
796 context->aux = aux->next;
799 while ((aux = context->aux_pids)) {
800 context->aux_pids = aux->next;
805 static inline void audit_zero_context(struct audit_context *context,
806 enum audit_state state)
808 memset(context, 0, sizeof(*context));
809 context->state = state;
812 static inline struct audit_context *audit_alloc_context(enum audit_state state)
814 struct audit_context *context;
816 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
818 audit_zero_context(context, state);
823 * audit_alloc - allocate an audit context block for a task
826 * Filter on the task information and allocate a per-task audit context
827 * if necessary. Doing so turns on system call auditing for the
828 * specified task. This is called from copy_process, so no lock is
831 int audit_alloc(struct task_struct *tsk)
833 struct audit_context *context;
834 enum audit_state state;
836 if (likely(!audit_enabled))
837 return 0; /* Return if not auditing. */
839 state = audit_filter_task(tsk);
840 if (likely(state == AUDIT_DISABLED))
843 if (!(context = audit_alloc_context(state))) {
844 audit_log_lost("out of memory in audit_alloc");
848 tsk->audit_context = context;
849 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
853 static inline void audit_free_context(struct audit_context *context)
855 struct audit_context *previous;
859 previous = context->previous;
860 if (previous || (count && count < 10)) {
862 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
863 " freeing multiple contexts (%d)\n",
864 context->serial, context->major,
865 context->name_count, count);
867 audit_free_names(context);
868 unroll_tree_refs(context, NULL, 0);
869 free_tree_refs(context);
870 audit_free_aux(context);
871 kfree(context->filterkey);
876 printk(KERN_ERR "audit: freed %d contexts\n", count);
879 void audit_log_task_context(struct audit_buffer *ab)
886 selinux_get_task_sid(current, &sid);
890 error = selinux_sid_to_string(sid, &ctx, &len);
892 if (error != -EINVAL)
897 audit_log_format(ab, " subj=%s", ctx);
902 audit_panic("error in audit_log_task_context");
906 EXPORT_SYMBOL(audit_log_task_context);
908 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
910 char name[sizeof(tsk->comm)];
911 struct mm_struct *mm = tsk->mm;
912 struct vm_area_struct *vma;
916 get_task_comm(name, tsk);
917 audit_log_format(ab, " comm=");
918 audit_log_untrustedstring(ab, name);
921 down_read(&mm->mmap_sem);
924 if ((vma->vm_flags & VM_EXECUTABLE) &&
926 audit_log_d_path(ab, "exe=",
927 vma->vm_file->f_path.dentry,
928 vma->vm_file->f_path.mnt);
933 up_read(&mm->mmap_sem);
935 audit_log_task_context(ab);
938 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
939 uid_t auid, uid_t uid, u32 sid, char *comm)
941 struct audit_buffer *ab;
946 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
950 audit_log_format(ab, "opid=%d oauid=%d ouid=%d", pid, auid, uid);
951 if (selinux_sid_to_string(sid, &s, &len)) {
952 audit_log_format(ab, " obj=(none)");
955 audit_log_format(ab, " obj=%s", s);
956 audit_log_format(ab, " ocomm=");
957 audit_log_untrustedstring(ab, comm);
964 static void audit_log_execve_info(struct audit_buffer *ab,
965 struct audit_aux_data_execve *axi)
969 const char __user *p;
972 if (axi->mm != current->mm)
973 return; /* execve failed, no additional info */
975 p = (const char __user *)axi->mm->arg_start;
977 for (i = 0; i < axi->argc; i++, p += len) {
978 len = strnlen_user(p, MAX_ARG_STRLEN);
980 * We just created this mm, if we can't find the strings
981 * we just copied into it something is _very_ wrong. Similar
982 * for strings that are too long, we should not have created
985 if (!len || len > MAX_ARG_STRLEN) {
987 send_sig(SIGKILL, current, 0);
990 buf = kmalloc(len, GFP_KERNEL);
992 audit_panic("out of memory for argv string\n");
996 ret = copy_from_user(buf, p, len);
998 * There is no reason for this copy to be short. We just
999 * copied them here, and the mm hasn't been exposed to user-
1004 send_sig(SIGKILL, current, 0);
1007 audit_log_format(ab, "a%d=", i);
1008 audit_log_untrustedstring(ab, buf);
1009 audit_log_format(ab, "\n");
1015 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1017 int i, call_panic = 0;
1018 struct audit_buffer *ab;
1019 struct audit_aux_data *aux;
1022 /* tsk == current */
1023 context->pid = tsk->pid;
1025 context->ppid = sys_getppid();
1026 context->uid = tsk->uid;
1027 context->gid = tsk->gid;
1028 context->euid = tsk->euid;
1029 context->suid = tsk->suid;
1030 context->fsuid = tsk->fsuid;
1031 context->egid = tsk->egid;
1032 context->sgid = tsk->sgid;
1033 context->fsgid = tsk->fsgid;
1034 context->personality = tsk->personality;
1036 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1038 return; /* audit_panic has been called */
1039 audit_log_format(ab, "arch=%x syscall=%d",
1040 context->arch, context->major);
1041 if (context->personality != PER_LINUX)
1042 audit_log_format(ab, " per=%lx", context->personality);
1043 if (context->return_valid)
1044 audit_log_format(ab, " success=%s exit=%ld",
1045 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1046 context->return_code);
1048 mutex_lock(&tty_mutex);
1049 read_lock(&tasklist_lock);
1050 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1051 tty = tsk->signal->tty->name;
1054 read_unlock(&tasklist_lock);
1055 audit_log_format(ab,
1056 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1057 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1058 " euid=%u suid=%u fsuid=%u"
1059 " egid=%u sgid=%u fsgid=%u tty=%s",
1064 context->name_count,
1070 context->euid, context->suid, context->fsuid,
1071 context->egid, context->sgid, context->fsgid, tty);
1073 mutex_unlock(&tty_mutex);
1075 audit_log_task_info(ab, tsk);
1076 if (context->filterkey) {
1077 audit_log_format(ab, " key=");
1078 audit_log_untrustedstring(ab, context->filterkey);
1080 audit_log_format(ab, " key=(null)");
1083 for (aux = context->aux; aux; aux = aux->next) {
1085 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1087 continue; /* audit_panic has been called */
1089 switch (aux->type) {
1090 case AUDIT_MQ_OPEN: {
1091 struct audit_aux_data_mq_open *axi = (void *)aux;
1092 audit_log_format(ab,
1093 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1094 "mq_msgsize=%ld mq_curmsgs=%ld",
1095 axi->oflag, axi->mode, axi->attr.mq_flags,
1096 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1097 axi->attr.mq_curmsgs);
1100 case AUDIT_MQ_SENDRECV: {
1101 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1102 audit_log_format(ab,
1103 "mqdes=%d msg_len=%zd msg_prio=%u "
1104 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1105 axi->mqdes, axi->msg_len, axi->msg_prio,
1106 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1109 case AUDIT_MQ_NOTIFY: {
1110 struct audit_aux_data_mq_notify *axi = (void *)aux;
1111 audit_log_format(ab,
1112 "mqdes=%d sigev_signo=%d",
1114 axi->notification.sigev_signo);
1117 case AUDIT_MQ_GETSETATTR: {
1118 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1119 audit_log_format(ab,
1120 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1123 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1124 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1128 struct audit_aux_data_ipcctl *axi = (void *)aux;
1129 audit_log_format(ab,
1130 "ouid=%u ogid=%u mode=%#o",
1131 axi->uid, axi->gid, axi->mode);
1132 if (axi->osid != 0) {
1135 if (selinux_sid_to_string(
1136 axi->osid, &ctx, &len)) {
1137 audit_log_format(ab, " osid=%u",
1141 audit_log_format(ab, " obj=%s", ctx);
1146 case AUDIT_IPC_SET_PERM: {
1147 struct audit_aux_data_ipcctl *axi = (void *)aux;
1148 audit_log_format(ab,
1149 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1150 axi->qbytes, axi->uid, axi->gid, axi->mode);
1153 case AUDIT_EXECVE: {
1154 struct audit_aux_data_execve *axi = (void *)aux;
1155 audit_log_execve_info(ab, axi);
1158 case AUDIT_SOCKETCALL: {
1160 struct audit_aux_data_socketcall *axs = (void *)aux;
1161 audit_log_format(ab, "nargs=%d", axs->nargs);
1162 for (i=0; i<axs->nargs; i++)
1163 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1166 case AUDIT_SOCKADDR: {
1167 struct audit_aux_data_sockaddr *axs = (void *)aux;
1169 audit_log_format(ab, "saddr=");
1170 audit_log_hex(ab, axs->a, axs->len);
1173 case AUDIT_FD_PAIR: {
1174 struct audit_aux_data_fd_pair *axs = (void *)aux;
1175 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1182 for (aux = context->aux_pids; aux; aux = aux->next) {
1183 struct audit_aux_data_pids *axs = (void *)aux;
1186 for (i = 0; i < axs->pid_count; i++)
1187 if (audit_log_pid_context(context, axs->target_pid[i],
1188 axs->target_auid[i],
1191 axs->target_comm[i]))
1195 if (context->target_pid &&
1196 audit_log_pid_context(context, context->target_pid,
1197 context->target_auid, context->target_uid,
1198 context->target_sid, context->target_comm))
1201 if (context->pwd && context->pwdmnt) {
1202 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1204 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1208 for (i = 0; i < context->name_count; i++) {
1209 struct audit_names *n = &context->names[i];
1211 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1213 continue; /* audit_panic has been called */
1215 audit_log_format(ab, "item=%d", i);
1218 switch(n->name_len) {
1219 case AUDIT_NAME_FULL:
1220 /* log the full path */
1221 audit_log_format(ab, " name=");
1222 audit_log_untrustedstring(ab, n->name);
1225 /* name was specified as a relative path and the
1226 * directory component is the cwd */
1227 audit_log_d_path(ab, " name=", context->pwd,
1231 /* log the name's directory component */
1232 audit_log_format(ab, " name=");
1233 audit_log_n_untrustedstring(ab, n->name_len,
1237 audit_log_format(ab, " name=(null)");
1239 if (n->ino != (unsigned long)-1) {
1240 audit_log_format(ab, " inode=%lu"
1241 " dev=%02x:%02x mode=%#o"
1242 " ouid=%u ogid=%u rdev=%02x:%02x",
1255 if (selinux_sid_to_string(
1256 n->osid, &ctx, &len)) {
1257 audit_log_format(ab, " osid=%u", n->osid);
1260 audit_log_format(ab, " obj=%s", ctx);
1267 audit_panic("error converting sid to string");
1271 * audit_free - free a per-task audit context
1272 * @tsk: task whose audit context block to free
1274 * Called from copy_process and do_exit
1276 void audit_free(struct task_struct *tsk)
1278 struct audit_context *context;
1280 context = audit_get_context(tsk, 0, 0);
1281 if (likely(!context))
1284 /* Check for system calls that do not go through the exit
1285 * function (e.g., exit_group), then free context block.
1286 * We use GFP_ATOMIC here because we might be doing this
1287 * in the context of the idle thread */
1288 /* that can happen only if we are called from do_exit() */
1289 if (context->in_syscall && context->auditable)
1290 audit_log_exit(context, tsk);
1292 audit_free_context(context);
1296 * audit_syscall_entry - fill in an audit record at syscall entry
1297 * @tsk: task being audited
1298 * @arch: architecture type
1299 * @major: major syscall type (function)
1300 * @a1: additional syscall register 1
1301 * @a2: additional syscall register 2
1302 * @a3: additional syscall register 3
1303 * @a4: additional syscall register 4
1305 * Fill in audit context at syscall entry. This only happens if the
1306 * audit context was created when the task was created and the state or
1307 * filters demand the audit context be built. If the state from the
1308 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1309 * then the record will be written at syscall exit time (otherwise, it
1310 * will only be written if another part of the kernel requests that it
1313 void audit_syscall_entry(int arch, int major,
1314 unsigned long a1, unsigned long a2,
1315 unsigned long a3, unsigned long a4)
1317 struct task_struct *tsk = current;
1318 struct audit_context *context = tsk->audit_context;
1319 enum audit_state state;
1324 * This happens only on certain architectures that make system
1325 * calls in kernel_thread via the entry.S interface, instead of
1326 * with direct calls. (If you are porting to a new
1327 * architecture, hitting this condition can indicate that you
1328 * got the _exit/_leave calls backward in entry.S.)
1332 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1334 * This also happens with vm86 emulation in a non-nested manner
1335 * (entries without exits), so this case must be caught.
1337 if (context->in_syscall) {
1338 struct audit_context *newctx;
1342 "audit(:%d) pid=%d in syscall=%d;"
1343 " entering syscall=%d\n",
1344 context->serial, tsk->pid, context->major, major);
1346 newctx = audit_alloc_context(context->state);
1348 newctx->previous = context;
1350 tsk->audit_context = newctx;
1352 /* If we can't alloc a new context, the best we
1353 * can do is to leak memory (any pending putname
1354 * will be lost). The only other alternative is
1355 * to abandon auditing. */
1356 audit_zero_context(context, context->state);
1359 BUG_ON(context->in_syscall || context->name_count);
1364 context->arch = arch;
1365 context->major = major;
1366 context->argv[0] = a1;
1367 context->argv[1] = a2;
1368 context->argv[2] = a3;
1369 context->argv[3] = a4;
1371 state = context->state;
1372 context->dummy = !audit_n_rules;
1373 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1374 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1375 if (likely(state == AUDIT_DISABLED))
1378 context->serial = 0;
1379 context->ctime = CURRENT_TIME;
1380 context->in_syscall = 1;
1381 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1386 * audit_syscall_exit - deallocate audit context after a system call
1387 * @tsk: task being audited
1388 * @valid: success/failure flag
1389 * @return_code: syscall return value
1391 * Tear down after system call. If the audit context has been marked as
1392 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1393 * filtering, or because some other part of the kernel write an audit
1394 * message), then write out the syscall information. In call cases,
1395 * free the names stored from getname().
1397 void audit_syscall_exit(int valid, long return_code)
1399 struct task_struct *tsk = current;
1400 struct audit_context *context;
1402 context = audit_get_context(tsk, valid, return_code);
1404 if (likely(!context))
1407 if (context->in_syscall && context->auditable)
1408 audit_log_exit(context, tsk);
1410 context->in_syscall = 0;
1411 context->auditable = 0;
1413 if (context->previous) {
1414 struct audit_context *new_context = context->previous;
1415 context->previous = NULL;
1416 audit_free_context(context);
1417 tsk->audit_context = new_context;
1419 audit_free_names(context);
1420 unroll_tree_refs(context, NULL, 0);
1421 audit_free_aux(context);
1422 context->aux = NULL;
1423 context->aux_pids = NULL;
1424 context->target_pid = 0;
1425 context->target_sid = 0;
1426 kfree(context->filterkey);
1427 context->filterkey = NULL;
1428 tsk->audit_context = context;
1432 static inline void handle_one(const struct inode *inode)
1434 #ifdef CONFIG_AUDIT_TREE
1435 struct audit_context *context;
1436 struct audit_tree_refs *p;
1437 struct audit_chunk *chunk;
1439 if (likely(list_empty(&inode->inotify_watches)))
1441 context = current->audit_context;
1443 count = context->tree_count;
1445 chunk = audit_tree_lookup(inode);
1449 if (likely(put_tree_ref(context, chunk)))
1451 if (unlikely(!grow_tree_refs(context))) {
1452 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1453 audit_set_auditable(context);
1454 audit_put_chunk(chunk);
1455 unroll_tree_refs(context, p, count);
1458 put_tree_ref(context, chunk);
1462 static void handle_path(const struct dentry *dentry)
1464 #ifdef CONFIG_AUDIT_TREE
1465 struct audit_context *context;
1466 struct audit_tree_refs *p;
1467 const struct dentry *d, *parent;
1468 struct audit_chunk *drop;
1472 context = current->audit_context;
1474 count = context->tree_count;
1479 seq = read_seqbegin(&rename_lock);
1481 struct inode *inode = d->d_inode;
1482 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1483 struct audit_chunk *chunk;
1484 chunk = audit_tree_lookup(inode);
1486 if (unlikely(!put_tree_ref(context, chunk))) {
1492 parent = d->d_parent;
1497 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1500 /* just a race with rename */
1501 unroll_tree_refs(context, p, count);
1504 audit_put_chunk(drop);
1505 if (grow_tree_refs(context)) {
1506 /* OK, got more space */
1507 unroll_tree_refs(context, p, count);
1512 "out of memory, audit has lost a tree reference");
1513 unroll_tree_refs(context, p, count);
1514 audit_set_auditable(context);
1522 * audit_getname - add a name to the list
1523 * @name: name to add
1525 * Add a name to the list of audit names for this context.
1526 * Called from fs/namei.c:getname().
1528 void __audit_getname(const char *name)
1530 struct audit_context *context = current->audit_context;
1532 if (IS_ERR(name) || !name)
1535 if (!context->in_syscall) {
1536 #if AUDIT_DEBUG == 2
1537 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1538 __FILE__, __LINE__, context->serial, name);
1543 BUG_ON(context->name_count >= AUDIT_NAMES);
1544 context->names[context->name_count].name = name;
1545 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1546 context->names[context->name_count].name_put = 1;
1547 context->names[context->name_count].ino = (unsigned long)-1;
1548 context->names[context->name_count].osid = 0;
1549 ++context->name_count;
1550 if (!context->pwd) {
1551 read_lock(¤t->fs->lock);
1552 context->pwd = dget(current->fs->pwd);
1553 context->pwdmnt = mntget(current->fs->pwdmnt);
1554 read_unlock(¤t->fs->lock);
1559 /* audit_putname - intercept a putname request
1560 * @name: name to intercept and delay for putname
1562 * If we have stored the name from getname in the audit context,
1563 * then we delay the putname until syscall exit.
1564 * Called from include/linux/fs.h:putname().
1566 void audit_putname(const char *name)
1568 struct audit_context *context = current->audit_context;
1571 if (!context->in_syscall) {
1572 #if AUDIT_DEBUG == 2
1573 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1574 __FILE__, __LINE__, context->serial, name);
1575 if (context->name_count) {
1577 for (i = 0; i < context->name_count; i++)
1578 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1579 context->names[i].name,
1580 context->names[i].name ?: "(null)");
1587 ++context->put_count;
1588 if (context->put_count > context->name_count) {
1589 printk(KERN_ERR "%s:%d(:%d): major=%d"
1590 " in_syscall=%d putname(%p) name_count=%d"
1593 context->serial, context->major,
1594 context->in_syscall, name, context->name_count,
1595 context->put_count);
1602 static int audit_inc_name_count(struct audit_context *context,
1603 const struct inode *inode)
1605 if (context->name_count >= AUDIT_NAMES) {
1607 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1608 "dev=%02x:%02x, inode=%lu",
1609 MAJOR(inode->i_sb->s_dev),
1610 MINOR(inode->i_sb->s_dev),
1614 printk(KERN_DEBUG "name_count maxed, losing inode data");
1617 context->name_count++;
1619 context->ino_count++;
1624 /* Copy inode data into an audit_names. */
1625 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1627 name->ino = inode->i_ino;
1628 name->dev = inode->i_sb->s_dev;
1629 name->mode = inode->i_mode;
1630 name->uid = inode->i_uid;
1631 name->gid = inode->i_gid;
1632 name->rdev = inode->i_rdev;
1633 selinux_get_inode_sid(inode, &name->osid);
1637 * audit_inode - store the inode and device from a lookup
1638 * @name: name being audited
1639 * @dentry: dentry being audited
1641 * Called from fs/namei.c:path_lookup().
1643 void __audit_inode(const char *name, const struct dentry *dentry)
1646 struct audit_context *context = current->audit_context;
1647 const struct inode *inode = dentry->d_inode;
1649 if (!context->in_syscall)
1651 if (context->name_count
1652 && context->names[context->name_count-1].name
1653 && context->names[context->name_count-1].name == name)
1654 idx = context->name_count - 1;
1655 else if (context->name_count > 1
1656 && context->names[context->name_count-2].name
1657 && context->names[context->name_count-2].name == name)
1658 idx = context->name_count - 2;
1660 /* FIXME: how much do we care about inodes that have no
1661 * associated name? */
1662 if (audit_inc_name_count(context, inode))
1664 idx = context->name_count - 1;
1665 context->names[idx].name = NULL;
1667 handle_path(dentry);
1668 audit_copy_inode(&context->names[idx], inode);
1672 * audit_inode_child - collect inode info for created/removed objects
1673 * @dname: inode's dentry name
1674 * @dentry: dentry being audited
1675 * @parent: inode of dentry parent
1677 * For syscalls that create or remove filesystem objects, audit_inode
1678 * can only collect information for the filesystem object's parent.
1679 * This call updates the audit context with the child's information.
1680 * Syscalls that create a new filesystem object must be hooked after
1681 * the object is created. Syscalls that remove a filesystem object
1682 * must be hooked prior, in order to capture the target inode during
1683 * unsuccessful attempts.
1685 void __audit_inode_child(const char *dname, const struct dentry *dentry,
1686 const struct inode *parent)
1689 struct audit_context *context = current->audit_context;
1690 const char *found_parent = NULL, *found_child = NULL;
1691 const struct inode *inode = dentry->d_inode;
1694 if (!context->in_syscall)
1699 /* determine matching parent */
1703 /* parent is more likely, look for it first */
1704 for (idx = 0; idx < context->name_count; idx++) {
1705 struct audit_names *n = &context->names[idx];
1710 if (n->ino == parent->i_ino &&
1711 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1712 n->name_len = dirlen; /* update parent data in place */
1713 found_parent = n->name;
1718 /* no matching parent, look for matching child */
1719 for (idx = 0; idx < context->name_count; idx++) {
1720 struct audit_names *n = &context->names[idx];
1725 /* strcmp() is the more likely scenario */
1726 if (!strcmp(dname, n->name) ||
1727 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1729 audit_copy_inode(n, inode);
1731 n->ino = (unsigned long)-1;
1732 found_child = n->name;
1738 if (!found_parent) {
1739 if (audit_inc_name_count(context, parent))
1741 idx = context->name_count - 1;
1742 context->names[idx].name = NULL;
1743 audit_copy_inode(&context->names[idx], parent);
1747 if (audit_inc_name_count(context, inode))
1749 idx = context->name_count - 1;
1751 /* Re-use the name belonging to the slot for a matching parent
1752 * directory. All names for this context are relinquished in
1753 * audit_free_names() */
1755 context->names[idx].name = found_parent;
1756 context->names[idx].name_len = AUDIT_NAME_FULL;
1757 /* don't call __putname() */
1758 context->names[idx].name_put = 0;
1760 context->names[idx].name = NULL;
1764 audit_copy_inode(&context->names[idx], inode);
1766 context->names[idx].ino = (unsigned long)-1;
1769 EXPORT_SYMBOL_GPL(__audit_inode_child);
1772 * auditsc_get_stamp - get local copies of audit_context values
1773 * @ctx: audit_context for the task
1774 * @t: timespec to store time recorded in the audit_context
1775 * @serial: serial value that is recorded in the audit_context
1777 * Also sets the context as auditable.
1779 void auditsc_get_stamp(struct audit_context *ctx,
1780 struct timespec *t, unsigned int *serial)
1783 ctx->serial = audit_serial();
1784 t->tv_sec = ctx->ctime.tv_sec;
1785 t->tv_nsec = ctx->ctime.tv_nsec;
1786 *serial = ctx->serial;
1791 * audit_set_loginuid - set a task's audit_context loginuid
1792 * @task: task whose audit context is being modified
1793 * @loginuid: loginuid value
1797 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1799 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1801 struct audit_context *context = task->audit_context;
1803 if (context && context->in_syscall) {
1804 struct audit_buffer *ab;
1806 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1808 audit_log_format(ab, "login pid=%d uid=%u "
1809 "old auid=%u new auid=%u",
1810 task->pid, task->uid,
1811 task->loginuid, loginuid);
1815 task->loginuid = loginuid;
1820 * __audit_mq_open - record audit data for a POSIX MQ open
1823 * @u_attr: queue attributes
1825 * Returns 0 for success or NULL context or < 0 on error.
1827 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1829 struct audit_aux_data_mq_open *ax;
1830 struct audit_context *context = current->audit_context;
1835 if (likely(!context))
1838 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1842 if (u_attr != NULL) {
1843 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1848 memset(&ax->attr, 0, sizeof(ax->attr));
1853 ax->d.type = AUDIT_MQ_OPEN;
1854 ax->d.next = context->aux;
1855 context->aux = (void *)ax;
1860 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1861 * @mqdes: MQ descriptor
1862 * @msg_len: Message length
1863 * @msg_prio: Message priority
1864 * @u_abs_timeout: Message timeout in absolute time
1866 * Returns 0 for success or NULL context or < 0 on error.
1868 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1869 const struct timespec __user *u_abs_timeout)
1871 struct audit_aux_data_mq_sendrecv *ax;
1872 struct audit_context *context = current->audit_context;
1877 if (likely(!context))
1880 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1884 if (u_abs_timeout != NULL) {
1885 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1890 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1893 ax->msg_len = msg_len;
1894 ax->msg_prio = msg_prio;
1896 ax->d.type = AUDIT_MQ_SENDRECV;
1897 ax->d.next = context->aux;
1898 context->aux = (void *)ax;
1903 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1904 * @mqdes: MQ descriptor
1905 * @msg_len: Message length
1906 * @u_msg_prio: Message priority
1907 * @u_abs_timeout: Message timeout in absolute time
1909 * Returns 0 for success or NULL context or < 0 on error.
1911 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1912 unsigned int __user *u_msg_prio,
1913 const struct timespec __user *u_abs_timeout)
1915 struct audit_aux_data_mq_sendrecv *ax;
1916 struct audit_context *context = current->audit_context;
1921 if (likely(!context))
1924 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1928 if (u_msg_prio != NULL) {
1929 if (get_user(ax->msg_prio, u_msg_prio)) {
1936 if (u_abs_timeout != NULL) {
1937 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1942 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1945 ax->msg_len = msg_len;
1947 ax->d.type = AUDIT_MQ_SENDRECV;
1948 ax->d.next = context->aux;
1949 context->aux = (void *)ax;
1954 * __audit_mq_notify - record audit data for a POSIX MQ notify
1955 * @mqdes: MQ descriptor
1956 * @u_notification: Notification event
1958 * Returns 0 for success or NULL context or < 0 on error.
1961 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1963 struct audit_aux_data_mq_notify *ax;
1964 struct audit_context *context = current->audit_context;
1969 if (likely(!context))
1972 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1976 if (u_notification != NULL) {
1977 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1982 memset(&ax->notification, 0, sizeof(ax->notification));
1986 ax->d.type = AUDIT_MQ_NOTIFY;
1987 ax->d.next = context->aux;
1988 context->aux = (void *)ax;
1993 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1994 * @mqdes: MQ descriptor
1997 * Returns 0 for success or NULL context or < 0 on error.
1999 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2001 struct audit_aux_data_mq_getsetattr *ax;
2002 struct audit_context *context = current->audit_context;
2007 if (likely(!context))
2010 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2015 ax->mqstat = *mqstat;
2017 ax->d.type = AUDIT_MQ_GETSETATTR;
2018 ax->d.next = context->aux;
2019 context->aux = (void *)ax;
2024 * audit_ipc_obj - record audit data for ipc object
2025 * @ipcp: ipc permissions
2027 * Returns 0 for success or NULL context or < 0 on error.
2029 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2031 struct audit_aux_data_ipcctl *ax;
2032 struct audit_context *context = current->audit_context;
2034 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2038 ax->uid = ipcp->uid;
2039 ax->gid = ipcp->gid;
2040 ax->mode = ipcp->mode;
2041 selinux_get_ipc_sid(ipcp, &ax->osid);
2043 ax->d.type = AUDIT_IPC;
2044 ax->d.next = context->aux;
2045 context->aux = (void *)ax;
2050 * audit_ipc_set_perm - record audit data for new ipc permissions
2051 * @qbytes: msgq bytes
2052 * @uid: msgq user id
2053 * @gid: msgq group id
2054 * @mode: msgq mode (permissions)
2056 * Returns 0 for success or NULL context or < 0 on error.
2058 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2060 struct audit_aux_data_ipcctl *ax;
2061 struct audit_context *context = current->audit_context;
2063 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2067 ax->qbytes = qbytes;
2072 ax->d.type = AUDIT_IPC_SET_PERM;
2073 ax->d.next = context->aux;
2074 context->aux = (void *)ax;
2078 int audit_argv_kb = 32;
2080 int audit_bprm(struct linux_binprm *bprm)
2082 struct audit_aux_data_execve *ax;
2083 struct audit_context *context = current->audit_context;
2085 if (likely(!audit_enabled || !context || context->dummy))
2089 * Even though the stack code doesn't limit the arg+env size any more,
2090 * the audit code requires that _all_ arguments be logged in a single
2091 * netlink skb. Hence cap it :-(
2093 if (bprm->argv_len > (audit_argv_kb << 10))
2096 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2100 ax->argc = bprm->argc;
2101 ax->envc = bprm->envc;
2103 ax->d.type = AUDIT_EXECVE;
2104 ax->d.next = context->aux;
2105 context->aux = (void *)ax;
2111 * audit_socketcall - record audit data for sys_socketcall
2112 * @nargs: number of args
2115 * Returns 0 for success or NULL context or < 0 on error.
2117 int audit_socketcall(int nargs, unsigned long *args)
2119 struct audit_aux_data_socketcall *ax;
2120 struct audit_context *context = current->audit_context;
2122 if (likely(!context || context->dummy))
2125 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2130 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2132 ax->d.type = AUDIT_SOCKETCALL;
2133 ax->d.next = context->aux;
2134 context->aux = (void *)ax;
2139 * __audit_fd_pair - record audit data for pipe and socketpair
2140 * @fd1: the first file descriptor
2141 * @fd2: the second file descriptor
2143 * Returns 0 for success or NULL context or < 0 on error.
2145 int __audit_fd_pair(int fd1, int fd2)
2147 struct audit_context *context = current->audit_context;
2148 struct audit_aux_data_fd_pair *ax;
2150 if (likely(!context)) {
2154 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2162 ax->d.type = AUDIT_FD_PAIR;
2163 ax->d.next = context->aux;
2164 context->aux = (void *)ax;
2169 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2170 * @len: data length in user space
2171 * @a: data address in kernel space
2173 * Returns 0 for success or NULL context or < 0 on error.
2175 int audit_sockaddr(int len, void *a)
2177 struct audit_aux_data_sockaddr *ax;
2178 struct audit_context *context = current->audit_context;
2180 if (likely(!context || context->dummy))
2183 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2188 memcpy(ax->a, a, len);
2190 ax->d.type = AUDIT_SOCKADDR;
2191 ax->d.next = context->aux;
2192 context->aux = (void *)ax;
2196 void __audit_ptrace(struct task_struct *t)
2198 struct audit_context *context = current->audit_context;
2200 context->target_pid = t->pid;
2201 context->target_auid = audit_get_loginuid(t);
2202 context->target_uid = t->uid;
2203 selinux_get_task_sid(t, &context->target_sid);
2204 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2208 * audit_signal_info - record signal info for shutting down audit subsystem
2209 * @sig: signal value
2210 * @t: task being signaled
2212 * If the audit subsystem is being terminated, record the task (pid)
2213 * and uid that is doing that.
2215 int __audit_signal_info(int sig, struct task_struct *t)
2217 struct audit_aux_data_pids *axp;
2218 struct task_struct *tsk = current;
2219 struct audit_context *ctx = tsk->audit_context;
2220 extern pid_t audit_sig_pid;
2221 extern uid_t audit_sig_uid;
2222 extern u32 audit_sig_sid;
2224 if (audit_pid && t->tgid == audit_pid) {
2225 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2226 audit_sig_pid = tsk->pid;
2227 if (tsk->loginuid != -1)
2228 audit_sig_uid = tsk->loginuid;
2230 audit_sig_uid = tsk->uid;
2231 selinux_get_task_sid(tsk, &audit_sig_sid);
2233 if (!audit_signals || audit_dummy_context())
2237 /* optimize the common case by putting first signal recipient directly
2238 * in audit_context */
2239 if (!ctx->target_pid) {
2240 ctx->target_pid = t->tgid;
2241 ctx->target_auid = audit_get_loginuid(t);
2242 ctx->target_uid = t->uid;
2243 selinux_get_task_sid(t, &ctx->target_sid);
2244 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2248 axp = (void *)ctx->aux_pids;
2249 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2250 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2254 axp->d.type = AUDIT_OBJ_PID;
2255 axp->d.next = ctx->aux_pids;
2256 ctx->aux_pids = (void *)axp;
2258 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2260 axp->target_pid[axp->pid_count] = t->tgid;
2261 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2262 axp->target_uid[axp->pid_count] = t->uid;
2263 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2264 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2271 * audit_core_dumps - record information about processes that end abnormally
2272 * @signr: signal value
2274 * If a process ends with a core dump, something fishy is going on and we
2275 * should record the event for investigation.
2277 void audit_core_dumps(long signr)
2279 struct audit_buffer *ab;
2285 if (signr == SIGQUIT) /* don't care for those */
2288 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2289 audit_log_format(ab, "auid=%u uid=%u gid=%u",
2290 audit_get_loginuid(current),
2291 current->uid, current->gid);
2292 selinux_get_task_sid(current, &sid);
2297 if (selinux_sid_to_string(sid, &ctx, &len))
2298 audit_log_format(ab, " ssid=%u", sid);
2300 audit_log_format(ab, " subj=%s", ctx);
2303 audit_log_format(ab, " pid=%d comm=", current->pid);
2304 audit_log_untrustedstring(ab, current->comm);
2305 audit_log_format(ab, " sig=%ld", signr);