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 unsigned int target_sessionid[AUDIT_AUX_PIDS];
182 u32 target_sid[AUDIT_AUX_PIDS];
183 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
187 struct audit_tree_refs {
188 struct audit_tree_refs *next;
189 struct audit_chunk *c[31];
192 /* The per-task audit context. */
193 struct audit_context {
194 int dummy; /* must be the first element */
195 int in_syscall; /* 1 if task is in a syscall */
196 enum audit_state state;
197 unsigned int serial; /* serial number for record */
198 struct timespec ctime; /* time of syscall entry */
199 int major; /* syscall number */
200 unsigned long argv[4]; /* syscall arguments */
201 int return_valid; /* return code is valid */
202 long return_code;/* syscall return code */
203 int auditable; /* 1 if record should be written */
205 struct audit_names names[AUDIT_NAMES];
206 char * filterkey; /* key for rule that triggered record */
208 struct vfsmount * pwdmnt;
209 struct audit_context *previous; /* For nested syscalls */
210 struct audit_aux_data *aux;
211 struct audit_aux_data *aux_pids;
213 /* Save things to print about task_struct */
215 uid_t uid, euid, suid, fsuid;
216 gid_t gid, egid, sgid, fsgid;
217 unsigned long personality;
223 unsigned int target_sessionid;
225 char target_comm[TASK_COMM_LEN];
227 struct audit_tree_refs *trees, *first_trees;
236 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
237 static inline int open_arg(int flags, int mask)
239 int n = ACC_MODE(flags);
240 if (flags & (O_TRUNC | O_CREAT))
241 n |= AUDIT_PERM_WRITE;
245 static int audit_match_perm(struct audit_context *ctx, int mask)
247 unsigned n = ctx->major;
248 switch (audit_classify_syscall(ctx->arch, n)) {
250 if ((mask & AUDIT_PERM_WRITE) &&
251 audit_match_class(AUDIT_CLASS_WRITE, n))
253 if ((mask & AUDIT_PERM_READ) &&
254 audit_match_class(AUDIT_CLASS_READ, n))
256 if ((mask & AUDIT_PERM_ATTR) &&
257 audit_match_class(AUDIT_CLASS_CHATTR, n))
260 case 1: /* 32bit on biarch */
261 if ((mask & AUDIT_PERM_WRITE) &&
262 audit_match_class(AUDIT_CLASS_WRITE_32, n))
264 if ((mask & AUDIT_PERM_READ) &&
265 audit_match_class(AUDIT_CLASS_READ_32, n))
267 if ((mask & AUDIT_PERM_ATTR) &&
268 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
272 return mask & ACC_MODE(ctx->argv[1]);
274 return mask & ACC_MODE(ctx->argv[2]);
275 case 4: /* socketcall */
276 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
278 return mask & AUDIT_PERM_EXEC;
285 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
286 * ->first_trees points to its beginning, ->trees - to the current end of data.
287 * ->tree_count is the number of free entries in array pointed to by ->trees.
288 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
289 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
290 * it's going to remain 1-element for almost any setup) until we free context itself.
291 * References in it _are_ dropped - at the same time we free/drop aux stuff.
294 #ifdef CONFIG_AUDIT_TREE
295 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
297 struct audit_tree_refs *p = ctx->trees;
298 int left = ctx->tree_count;
300 p->c[--left] = chunk;
301 ctx->tree_count = left;
310 ctx->tree_count = 30;
316 static int grow_tree_refs(struct audit_context *ctx)
318 struct audit_tree_refs *p = ctx->trees;
319 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
325 p->next = ctx->trees;
327 ctx->first_trees = ctx->trees;
328 ctx->tree_count = 31;
333 static void unroll_tree_refs(struct audit_context *ctx,
334 struct audit_tree_refs *p, int count)
336 #ifdef CONFIG_AUDIT_TREE
337 struct audit_tree_refs *q;
340 /* we started with empty chain */
341 p = ctx->first_trees;
343 /* if the very first allocation has failed, nothing to do */
348 for (q = p; q != ctx->trees; q = q->next, n = 31) {
350 audit_put_chunk(q->c[n]);
354 while (n-- > ctx->tree_count) {
355 audit_put_chunk(q->c[n]);
359 ctx->tree_count = count;
363 static void free_tree_refs(struct audit_context *ctx)
365 struct audit_tree_refs *p, *q;
366 for (p = ctx->first_trees; p; p = q) {
372 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
374 #ifdef CONFIG_AUDIT_TREE
375 struct audit_tree_refs *p;
380 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
381 for (n = 0; n < 31; n++)
382 if (audit_tree_match(p->c[n], tree))
387 for (n = ctx->tree_count; n < 31; n++)
388 if (audit_tree_match(p->c[n], tree))
395 /* Determine if any context name data matches a rule's watch data */
396 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
398 static int audit_filter_rules(struct task_struct *tsk,
399 struct audit_krule *rule,
400 struct audit_context *ctx,
401 struct audit_names *name,
402 enum audit_state *state)
404 int i, j, need_sid = 1;
407 for (i = 0; i < rule->field_count; i++) {
408 struct audit_field *f = &rule->fields[i];
413 result = audit_comparator(tsk->pid, f->op, f->val);
418 ctx->ppid = sys_getppid();
419 result = audit_comparator(ctx->ppid, f->op, f->val);
423 result = audit_comparator(tsk->uid, f->op, f->val);
426 result = audit_comparator(tsk->euid, f->op, f->val);
429 result = audit_comparator(tsk->suid, f->op, f->val);
432 result = audit_comparator(tsk->fsuid, f->op, f->val);
435 result = audit_comparator(tsk->gid, f->op, f->val);
438 result = audit_comparator(tsk->egid, f->op, f->val);
441 result = audit_comparator(tsk->sgid, f->op, f->val);
444 result = audit_comparator(tsk->fsgid, f->op, f->val);
447 result = audit_comparator(tsk->personality, f->op, f->val);
451 result = audit_comparator(ctx->arch, f->op, f->val);
455 if (ctx && ctx->return_valid)
456 result = audit_comparator(ctx->return_code, f->op, f->val);
459 if (ctx && ctx->return_valid) {
461 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
463 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
468 result = audit_comparator(MAJOR(name->dev),
471 for (j = 0; j < ctx->name_count; j++) {
472 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
481 result = audit_comparator(MINOR(name->dev),
484 for (j = 0; j < ctx->name_count; j++) {
485 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
494 result = (name->ino == f->val);
496 for (j = 0; j < ctx->name_count; j++) {
497 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
505 if (name && rule->watch->ino != (unsigned long)-1)
506 result = (name->dev == rule->watch->dev &&
507 name->ino == rule->watch->ino);
511 result = match_tree_refs(ctx, rule->tree);
516 result = audit_comparator(tsk->loginuid, f->op, f->val);
518 case AUDIT_SUBJ_USER:
519 case AUDIT_SUBJ_ROLE:
520 case AUDIT_SUBJ_TYPE:
523 /* NOTE: this may return negative values indicating
524 a temporary error. We simply treat this as a
525 match for now to avoid losing information that
526 may be wanted. An error message will also be
530 selinux_get_task_sid(tsk, &sid);
533 result = selinux_audit_rule_match(sid, f->type,
542 case AUDIT_OBJ_LEV_LOW:
543 case AUDIT_OBJ_LEV_HIGH:
544 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
547 /* Find files that match */
549 result = selinux_audit_rule_match(
550 name->osid, f->type, f->op,
553 for (j = 0; j < ctx->name_count; j++) {
554 if (selinux_audit_rule_match(
563 /* Find ipc objects that match */
565 struct audit_aux_data *aux;
566 for (aux = ctx->aux; aux;
568 if (aux->type == AUDIT_IPC) {
569 struct audit_aux_data_ipcctl *axi = (void *)aux;
570 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
584 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
586 case AUDIT_FILTERKEY:
587 /* ignore this field for filtering */
591 result = audit_match_perm(ctx, f->val);
599 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
600 switch (rule->action) {
601 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
602 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
607 /* At process creation time, we can determine if system-call auditing is
608 * completely disabled for this task. Since we only have the task
609 * structure at this point, we can only check uid and gid.
611 static enum audit_state audit_filter_task(struct task_struct *tsk)
613 struct audit_entry *e;
614 enum audit_state state;
617 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
618 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
624 return AUDIT_BUILD_CONTEXT;
627 /* At syscall entry and exit time, this filter is called if the
628 * audit_state is not low enough that auditing cannot take place, but is
629 * also not high enough that we already know we have to write an audit
630 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
632 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
633 struct audit_context *ctx,
634 struct list_head *list)
636 struct audit_entry *e;
637 enum audit_state state;
639 if (audit_pid && tsk->tgid == audit_pid)
640 return AUDIT_DISABLED;
643 if (!list_empty(list)) {
644 int word = AUDIT_WORD(ctx->major);
645 int bit = AUDIT_BIT(ctx->major);
647 list_for_each_entry_rcu(e, list, list) {
648 if ((e->rule.mask[word] & bit) == bit &&
649 audit_filter_rules(tsk, &e->rule, ctx, NULL,
657 return AUDIT_BUILD_CONTEXT;
660 /* At syscall exit time, this filter is called if any audit_names[] have been
661 * collected during syscall processing. We only check rules in sublists at hash
662 * buckets applicable to the inode numbers in audit_names[].
663 * Regarding audit_state, same rules apply as for audit_filter_syscall().
665 enum audit_state audit_filter_inodes(struct task_struct *tsk,
666 struct audit_context *ctx)
669 struct audit_entry *e;
670 enum audit_state state;
672 if (audit_pid && tsk->tgid == audit_pid)
673 return AUDIT_DISABLED;
676 for (i = 0; i < ctx->name_count; i++) {
677 int word = AUDIT_WORD(ctx->major);
678 int bit = AUDIT_BIT(ctx->major);
679 struct audit_names *n = &ctx->names[i];
680 int h = audit_hash_ino((u32)n->ino);
681 struct list_head *list = &audit_inode_hash[h];
683 if (list_empty(list))
686 list_for_each_entry_rcu(e, list, list) {
687 if ((e->rule.mask[word] & bit) == bit &&
688 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
695 return AUDIT_BUILD_CONTEXT;
698 void audit_set_auditable(struct audit_context *ctx)
703 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
707 struct audit_context *context = tsk->audit_context;
709 if (likely(!context))
711 context->return_valid = return_valid;
714 * we need to fix up the return code in the audit logs if the actual
715 * return codes are later going to be fixed up by the arch specific
718 * This is actually a test for:
719 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
720 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
722 * but is faster than a bunch of ||
724 if (unlikely(return_code <= -ERESTARTSYS) &&
725 (return_code >= -ERESTART_RESTARTBLOCK) &&
726 (return_code != -ENOIOCTLCMD))
727 context->return_code = -EINTR;
729 context->return_code = return_code;
731 if (context->in_syscall && !context->dummy && !context->auditable) {
732 enum audit_state state;
734 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
735 if (state == AUDIT_RECORD_CONTEXT) {
736 context->auditable = 1;
740 state = audit_filter_inodes(tsk, context);
741 if (state == AUDIT_RECORD_CONTEXT)
742 context->auditable = 1;
748 tsk->audit_context = NULL;
752 static inline void audit_free_names(struct audit_context *context)
757 if (context->auditable
758 ||context->put_count + context->ino_count != context->name_count) {
759 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
760 " name_count=%d put_count=%d"
761 " ino_count=%d [NOT freeing]\n",
763 context->serial, context->major, context->in_syscall,
764 context->name_count, context->put_count,
766 for (i = 0; i < context->name_count; i++) {
767 printk(KERN_ERR "names[%d] = %p = %s\n", i,
768 context->names[i].name,
769 context->names[i].name ?: "(null)");
776 context->put_count = 0;
777 context->ino_count = 0;
780 for (i = 0; i < context->name_count; i++) {
781 if (context->names[i].name && context->names[i].name_put)
782 __putname(context->names[i].name);
784 context->name_count = 0;
788 mntput(context->pwdmnt);
790 context->pwdmnt = NULL;
793 static inline void audit_free_aux(struct audit_context *context)
795 struct audit_aux_data *aux;
797 while ((aux = context->aux)) {
798 context->aux = aux->next;
801 while ((aux = context->aux_pids)) {
802 context->aux_pids = aux->next;
807 static inline void audit_zero_context(struct audit_context *context,
808 enum audit_state state)
810 memset(context, 0, sizeof(*context));
811 context->state = state;
814 static inline struct audit_context *audit_alloc_context(enum audit_state state)
816 struct audit_context *context;
818 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
820 audit_zero_context(context, state);
825 * audit_alloc - allocate an audit context block for a task
828 * Filter on the task information and allocate a per-task audit context
829 * if necessary. Doing so turns on system call auditing for the
830 * specified task. This is called from copy_process, so no lock is
833 int audit_alloc(struct task_struct *tsk)
835 struct audit_context *context;
836 enum audit_state state;
838 if (likely(!audit_enabled))
839 return 0; /* Return if not auditing. */
841 state = audit_filter_task(tsk);
842 if (likely(state == AUDIT_DISABLED))
845 if (!(context = audit_alloc_context(state))) {
846 audit_log_lost("out of memory in audit_alloc");
850 tsk->audit_context = context;
851 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
855 static inline void audit_free_context(struct audit_context *context)
857 struct audit_context *previous;
861 previous = context->previous;
862 if (previous || (count && count < 10)) {
864 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
865 " freeing multiple contexts (%d)\n",
866 context->serial, context->major,
867 context->name_count, count);
869 audit_free_names(context);
870 unroll_tree_refs(context, NULL, 0);
871 free_tree_refs(context);
872 audit_free_aux(context);
873 kfree(context->filterkey);
878 printk(KERN_ERR "audit: freed %d contexts\n", count);
881 void audit_log_task_context(struct audit_buffer *ab)
888 selinux_get_task_sid(current, &sid);
892 error = selinux_sid_to_string(sid, &ctx, &len);
894 if (error != -EINVAL)
899 audit_log_format(ab, " subj=%s", ctx);
904 audit_panic("error in audit_log_task_context");
908 EXPORT_SYMBOL(audit_log_task_context);
910 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
912 char name[sizeof(tsk->comm)];
913 struct mm_struct *mm = tsk->mm;
914 struct vm_area_struct *vma;
918 get_task_comm(name, tsk);
919 audit_log_format(ab, " comm=");
920 audit_log_untrustedstring(ab, name);
923 down_read(&mm->mmap_sem);
926 if ((vma->vm_flags & VM_EXECUTABLE) &&
928 audit_log_d_path(ab, "exe=",
929 vma->vm_file->f_path.dentry,
930 vma->vm_file->f_path.mnt);
935 up_read(&mm->mmap_sem);
937 audit_log_task_context(ab);
940 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
941 uid_t auid, uid_t uid, unsigned int sessionid,
944 struct audit_buffer *ab;
949 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
953 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
955 if (selinux_sid_to_string(sid, &s, &len)) {
956 audit_log_format(ab, " obj=(none)");
959 audit_log_format(ab, " obj=%s", s);
960 audit_log_format(ab, " ocomm=");
961 audit_log_untrustedstring(ab, comm);
968 static void audit_log_execve_info(struct audit_buffer *ab,
969 struct audit_aux_data_execve *axi)
973 const char __user *p;
976 if (axi->mm != current->mm)
977 return; /* execve failed, no additional info */
979 p = (const char __user *)axi->mm->arg_start;
981 for (i = 0; i < axi->argc; i++, p += len) {
982 len = strnlen_user(p, MAX_ARG_STRLEN);
984 * We just created this mm, if we can't find the strings
985 * we just copied into it something is _very_ wrong. Similar
986 * for strings that are too long, we should not have created
989 if (!len || len > MAX_ARG_STRLEN) {
991 send_sig(SIGKILL, current, 0);
994 buf = kmalloc(len, GFP_KERNEL);
996 audit_panic("out of memory for argv string\n");
1000 ret = copy_from_user(buf, p, len);
1002 * There is no reason for this copy to be short. We just
1003 * copied them here, and the mm hasn't been exposed to user-
1008 send_sig(SIGKILL, current, 0);
1011 audit_log_format(ab, "a%d=", i);
1012 audit_log_untrustedstring(ab, buf);
1013 audit_log_format(ab, "\n");
1019 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1021 int i, call_panic = 0;
1022 struct audit_buffer *ab;
1023 struct audit_aux_data *aux;
1026 /* tsk == current */
1027 context->pid = tsk->pid;
1029 context->ppid = sys_getppid();
1030 context->uid = tsk->uid;
1031 context->gid = tsk->gid;
1032 context->euid = tsk->euid;
1033 context->suid = tsk->suid;
1034 context->fsuid = tsk->fsuid;
1035 context->egid = tsk->egid;
1036 context->sgid = tsk->sgid;
1037 context->fsgid = tsk->fsgid;
1038 context->personality = tsk->personality;
1040 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1042 return; /* audit_panic has been called */
1043 audit_log_format(ab, "arch=%x syscall=%d",
1044 context->arch, context->major);
1045 if (context->personality != PER_LINUX)
1046 audit_log_format(ab, " per=%lx", context->personality);
1047 if (context->return_valid)
1048 audit_log_format(ab, " success=%s exit=%ld",
1049 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1050 context->return_code);
1052 mutex_lock(&tty_mutex);
1053 read_lock(&tasklist_lock);
1054 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1055 tty = tsk->signal->tty->name;
1058 read_unlock(&tasklist_lock);
1059 audit_log_format(ab,
1060 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1061 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1062 " euid=%u suid=%u fsuid=%u"
1063 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1068 context->name_count,
1074 context->euid, context->suid, context->fsuid,
1075 context->egid, context->sgid, context->fsgid, tty,
1078 mutex_unlock(&tty_mutex);
1080 audit_log_task_info(ab, tsk);
1081 if (context->filterkey) {
1082 audit_log_format(ab, " key=");
1083 audit_log_untrustedstring(ab, context->filterkey);
1085 audit_log_format(ab, " key=(null)");
1088 for (aux = context->aux; aux; aux = aux->next) {
1090 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1092 continue; /* audit_panic has been called */
1094 switch (aux->type) {
1095 case AUDIT_MQ_OPEN: {
1096 struct audit_aux_data_mq_open *axi = (void *)aux;
1097 audit_log_format(ab,
1098 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1099 "mq_msgsize=%ld mq_curmsgs=%ld",
1100 axi->oflag, axi->mode, axi->attr.mq_flags,
1101 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1102 axi->attr.mq_curmsgs);
1105 case AUDIT_MQ_SENDRECV: {
1106 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1107 audit_log_format(ab,
1108 "mqdes=%d msg_len=%zd msg_prio=%u "
1109 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1110 axi->mqdes, axi->msg_len, axi->msg_prio,
1111 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1114 case AUDIT_MQ_NOTIFY: {
1115 struct audit_aux_data_mq_notify *axi = (void *)aux;
1116 audit_log_format(ab,
1117 "mqdes=%d sigev_signo=%d",
1119 axi->notification.sigev_signo);
1122 case AUDIT_MQ_GETSETATTR: {
1123 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1124 audit_log_format(ab,
1125 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1128 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1129 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1133 struct audit_aux_data_ipcctl *axi = (void *)aux;
1134 audit_log_format(ab,
1135 "ouid=%u ogid=%u mode=%#o",
1136 axi->uid, axi->gid, axi->mode);
1137 if (axi->osid != 0) {
1140 if (selinux_sid_to_string(
1141 axi->osid, &ctx, &len)) {
1142 audit_log_format(ab, " osid=%u",
1146 audit_log_format(ab, " obj=%s", ctx);
1151 case AUDIT_IPC_SET_PERM: {
1152 struct audit_aux_data_ipcctl *axi = (void *)aux;
1153 audit_log_format(ab,
1154 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1155 axi->qbytes, axi->uid, axi->gid, axi->mode);
1158 case AUDIT_EXECVE: {
1159 struct audit_aux_data_execve *axi = (void *)aux;
1160 audit_log_execve_info(ab, axi);
1163 case AUDIT_SOCKETCALL: {
1165 struct audit_aux_data_socketcall *axs = (void *)aux;
1166 audit_log_format(ab, "nargs=%d", axs->nargs);
1167 for (i=0; i<axs->nargs; i++)
1168 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1171 case AUDIT_SOCKADDR: {
1172 struct audit_aux_data_sockaddr *axs = (void *)aux;
1174 audit_log_format(ab, "saddr=");
1175 audit_log_hex(ab, axs->a, axs->len);
1178 case AUDIT_FD_PAIR: {
1179 struct audit_aux_data_fd_pair *axs = (void *)aux;
1180 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1187 for (aux = context->aux_pids; aux; aux = aux->next) {
1188 struct audit_aux_data_pids *axs = (void *)aux;
1191 for (i = 0; i < axs->pid_count; i++)
1192 if (audit_log_pid_context(context, axs->target_pid[i],
1193 axs->target_auid[i],
1195 axs->target_sessionid[i],
1197 axs->target_comm[i]))
1201 if (context->target_pid &&
1202 audit_log_pid_context(context, context->target_pid,
1203 context->target_auid, context->target_uid,
1204 context->target_sessionid,
1205 context->target_sid, context->target_comm))
1208 if (context->pwd && context->pwdmnt) {
1209 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1211 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1215 for (i = 0; i < context->name_count; i++) {
1216 struct audit_names *n = &context->names[i];
1218 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1220 continue; /* audit_panic has been called */
1222 audit_log_format(ab, "item=%d", i);
1225 switch(n->name_len) {
1226 case AUDIT_NAME_FULL:
1227 /* log the full path */
1228 audit_log_format(ab, " name=");
1229 audit_log_untrustedstring(ab, n->name);
1232 /* name was specified as a relative path and the
1233 * directory component is the cwd */
1234 audit_log_d_path(ab, " name=", context->pwd,
1238 /* log the name's directory component */
1239 audit_log_format(ab, " name=");
1240 audit_log_n_untrustedstring(ab, n->name_len,
1244 audit_log_format(ab, " name=(null)");
1246 if (n->ino != (unsigned long)-1) {
1247 audit_log_format(ab, " inode=%lu"
1248 " dev=%02x:%02x mode=%#o"
1249 " ouid=%u ogid=%u rdev=%02x:%02x",
1262 if (selinux_sid_to_string(
1263 n->osid, &ctx, &len)) {
1264 audit_log_format(ab, " osid=%u", n->osid);
1267 audit_log_format(ab, " obj=%s", ctx);
1274 /* Send end of event record to help user space know we are finished */
1275 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1279 audit_panic("error converting sid to string");
1283 * audit_free - free a per-task audit context
1284 * @tsk: task whose audit context block to free
1286 * Called from copy_process and do_exit
1288 void audit_free(struct task_struct *tsk)
1290 struct audit_context *context;
1292 context = audit_get_context(tsk, 0, 0);
1293 if (likely(!context))
1296 /* Check for system calls that do not go through the exit
1297 * function (e.g., exit_group), then free context block.
1298 * We use GFP_ATOMIC here because we might be doing this
1299 * in the context of the idle thread */
1300 /* that can happen only if we are called from do_exit() */
1301 if (context->in_syscall && context->auditable)
1302 audit_log_exit(context, tsk);
1304 audit_free_context(context);
1308 * audit_syscall_entry - fill in an audit record at syscall entry
1309 * @tsk: task being audited
1310 * @arch: architecture type
1311 * @major: major syscall type (function)
1312 * @a1: additional syscall register 1
1313 * @a2: additional syscall register 2
1314 * @a3: additional syscall register 3
1315 * @a4: additional syscall register 4
1317 * Fill in audit context at syscall entry. This only happens if the
1318 * audit context was created when the task was created and the state or
1319 * filters demand the audit context be built. If the state from the
1320 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1321 * then the record will be written at syscall exit time (otherwise, it
1322 * will only be written if another part of the kernel requests that it
1325 void audit_syscall_entry(int arch, int major,
1326 unsigned long a1, unsigned long a2,
1327 unsigned long a3, unsigned long a4)
1329 struct task_struct *tsk = current;
1330 struct audit_context *context = tsk->audit_context;
1331 enum audit_state state;
1336 * This happens only on certain architectures that make system
1337 * calls in kernel_thread via the entry.S interface, instead of
1338 * with direct calls. (If you are porting to a new
1339 * architecture, hitting this condition can indicate that you
1340 * got the _exit/_leave calls backward in entry.S.)
1344 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1346 * This also happens with vm86 emulation in a non-nested manner
1347 * (entries without exits), so this case must be caught.
1349 if (context->in_syscall) {
1350 struct audit_context *newctx;
1354 "audit(:%d) pid=%d in syscall=%d;"
1355 " entering syscall=%d\n",
1356 context->serial, tsk->pid, context->major, major);
1358 newctx = audit_alloc_context(context->state);
1360 newctx->previous = context;
1362 tsk->audit_context = newctx;
1364 /* If we can't alloc a new context, the best we
1365 * can do is to leak memory (any pending putname
1366 * will be lost). The only other alternative is
1367 * to abandon auditing. */
1368 audit_zero_context(context, context->state);
1371 BUG_ON(context->in_syscall || context->name_count);
1376 context->arch = arch;
1377 context->major = major;
1378 context->argv[0] = a1;
1379 context->argv[1] = a2;
1380 context->argv[2] = a3;
1381 context->argv[3] = a4;
1383 state = context->state;
1384 context->dummy = !audit_n_rules;
1385 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1386 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1387 if (likely(state == AUDIT_DISABLED))
1390 context->serial = 0;
1391 context->ctime = CURRENT_TIME;
1392 context->in_syscall = 1;
1393 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1398 * audit_syscall_exit - deallocate audit context after a system call
1399 * @tsk: task being audited
1400 * @valid: success/failure flag
1401 * @return_code: syscall return value
1403 * Tear down after system call. If the audit context has been marked as
1404 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1405 * filtering, or because some other part of the kernel write an audit
1406 * message), then write out the syscall information. In call cases,
1407 * free the names stored from getname().
1409 void audit_syscall_exit(int valid, long return_code)
1411 struct task_struct *tsk = current;
1412 struct audit_context *context;
1414 context = audit_get_context(tsk, valid, return_code);
1416 if (likely(!context))
1419 if (context->in_syscall && context->auditable)
1420 audit_log_exit(context, tsk);
1422 context->in_syscall = 0;
1423 context->auditable = 0;
1425 if (context->previous) {
1426 struct audit_context *new_context = context->previous;
1427 context->previous = NULL;
1428 audit_free_context(context);
1429 tsk->audit_context = new_context;
1431 audit_free_names(context);
1432 unroll_tree_refs(context, NULL, 0);
1433 audit_free_aux(context);
1434 context->aux = NULL;
1435 context->aux_pids = NULL;
1436 context->target_pid = 0;
1437 context->target_sid = 0;
1438 kfree(context->filterkey);
1439 context->filterkey = NULL;
1440 tsk->audit_context = context;
1444 static inline void handle_one(const struct inode *inode)
1446 #ifdef CONFIG_AUDIT_TREE
1447 struct audit_context *context;
1448 struct audit_tree_refs *p;
1449 struct audit_chunk *chunk;
1451 if (likely(list_empty(&inode->inotify_watches)))
1453 context = current->audit_context;
1455 count = context->tree_count;
1457 chunk = audit_tree_lookup(inode);
1461 if (likely(put_tree_ref(context, chunk)))
1463 if (unlikely(!grow_tree_refs(context))) {
1464 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1465 audit_set_auditable(context);
1466 audit_put_chunk(chunk);
1467 unroll_tree_refs(context, p, count);
1470 put_tree_ref(context, chunk);
1474 static void handle_path(const struct dentry *dentry)
1476 #ifdef CONFIG_AUDIT_TREE
1477 struct audit_context *context;
1478 struct audit_tree_refs *p;
1479 const struct dentry *d, *parent;
1480 struct audit_chunk *drop;
1484 context = current->audit_context;
1486 count = context->tree_count;
1491 seq = read_seqbegin(&rename_lock);
1493 struct inode *inode = d->d_inode;
1494 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1495 struct audit_chunk *chunk;
1496 chunk = audit_tree_lookup(inode);
1498 if (unlikely(!put_tree_ref(context, chunk))) {
1504 parent = d->d_parent;
1509 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1512 /* just a race with rename */
1513 unroll_tree_refs(context, p, count);
1516 audit_put_chunk(drop);
1517 if (grow_tree_refs(context)) {
1518 /* OK, got more space */
1519 unroll_tree_refs(context, p, count);
1524 "out of memory, audit has lost a tree reference");
1525 unroll_tree_refs(context, p, count);
1526 audit_set_auditable(context);
1534 * audit_getname - add a name to the list
1535 * @name: name to add
1537 * Add a name to the list of audit names for this context.
1538 * Called from fs/namei.c:getname().
1540 void __audit_getname(const char *name)
1542 struct audit_context *context = current->audit_context;
1544 if (IS_ERR(name) || !name)
1547 if (!context->in_syscall) {
1548 #if AUDIT_DEBUG == 2
1549 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1550 __FILE__, __LINE__, context->serial, name);
1555 BUG_ON(context->name_count >= AUDIT_NAMES);
1556 context->names[context->name_count].name = name;
1557 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1558 context->names[context->name_count].name_put = 1;
1559 context->names[context->name_count].ino = (unsigned long)-1;
1560 context->names[context->name_count].osid = 0;
1561 ++context->name_count;
1562 if (!context->pwd) {
1563 read_lock(¤t->fs->lock);
1564 context->pwd = dget(current->fs->pwd);
1565 context->pwdmnt = mntget(current->fs->pwdmnt);
1566 read_unlock(¤t->fs->lock);
1571 /* audit_putname - intercept a putname request
1572 * @name: name to intercept and delay for putname
1574 * If we have stored the name from getname in the audit context,
1575 * then we delay the putname until syscall exit.
1576 * Called from include/linux/fs.h:putname().
1578 void audit_putname(const char *name)
1580 struct audit_context *context = current->audit_context;
1583 if (!context->in_syscall) {
1584 #if AUDIT_DEBUG == 2
1585 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1586 __FILE__, __LINE__, context->serial, name);
1587 if (context->name_count) {
1589 for (i = 0; i < context->name_count; i++)
1590 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1591 context->names[i].name,
1592 context->names[i].name ?: "(null)");
1599 ++context->put_count;
1600 if (context->put_count > context->name_count) {
1601 printk(KERN_ERR "%s:%d(:%d): major=%d"
1602 " in_syscall=%d putname(%p) name_count=%d"
1605 context->serial, context->major,
1606 context->in_syscall, name, context->name_count,
1607 context->put_count);
1614 static int audit_inc_name_count(struct audit_context *context,
1615 const struct inode *inode)
1617 if (context->name_count >= AUDIT_NAMES) {
1619 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1620 "dev=%02x:%02x, inode=%lu",
1621 MAJOR(inode->i_sb->s_dev),
1622 MINOR(inode->i_sb->s_dev),
1626 printk(KERN_DEBUG "name_count maxed, losing inode data");
1629 context->name_count++;
1631 context->ino_count++;
1636 /* Copy inode data into an audit_names. */
1637 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1639 name->ino = inode->i_ino;
1640 name->dev = inode->i_sb->s_dev;
1641 name->mode = inode->i_mode;
1642 name->uid = inode->i_uid;
1643 name->gid = inode->i_gid;
1644 name->rdev = inode->i_rdev;
1645 selinux_get_inode_sid(inode, &name->osid);
1649 * audit_inode - store the inode and device from a lookup
1650 * @name: name being audited
1651 * @dentry: dentry being audited
1653 * Called from fs/namei.c:path_lookup().
1655 void __audit_inode(const char *name, const struct dentry *dentry)
1658 struct audit_context *context = current->audit_context;
1659 const struct inode *inode = dentry->d_inode;
1661 if (!context->in_syscall)
1663 if (context->name_count
1664 && context->names[context->name_count-1].name
1665 && context->names[context->name_count-1].name == name)
1666 idx = context->name_count - 1;
1667 else if (context->name_count > 1
1668 && context->names[context->name_count-2].name
1669 && context->names[context->name_count-2].name == name)
1670 idx = context->name_count - 2;
1672 /* FIXME: how much do we care about inodes that have no
1673 * associated name? */
1674 if (audit_inc_name_count(context, inode))
1676 idx = context->name_count - 1;
1677 context->names[idx].name = NULL;
1679 handle_path(dentry);
1680 audit_copy_inode(&context->names[idx], inode);
1684 * audit_inode_child - collect inode info for created/removed objects
1685 * @dname: inode's dentry name
1686 * @dentry: dentry being audited
1687 * @parent: inode of dentry parent
1689 * For syscalls that create or remove filesystem objects, audit_inode
1690 * can only collect information for the filesystem object's parent.
1691 * This call updates the audit context with the child's information.
1692 * Syscalls that create a new filesystem object must be hooked after
1693 * the object is created. Syscalls that remove a filesystem object
1694 * must be hooked prior, in order to capture the target inode during
1695 * unsuccessful attempts.
1697 void __audit_inode_child(const char *dname, const struct dentry *dentry,
1698 const struct inode *parent)
1701 struct audit_context *context = current->audit_context;
1702 const char *found_parent = NULL, *found_child = NULL;
1703 const struct inode *inode = dentry->d_inode;
1706 if (!context->in_syscall)
1711 /* determine matching parent */
1715 /* parent is more likely, look for it first */
1716 for (idx = 0; idx < context->name_count; idx++) {
1717 struct audit_names *n = &context->names[idx];
1722 if (n->ino == parent->i_ino &&
1723 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1724 n->name_len = dirlen; /* update parent data in place */
1725 found_parent = n->name;
1730 /* no matching parent, look for matching child */
1731 for (idx = 0; idx < context->name_count; idx++) {
1732 struct audit_names *n = &context->names[idx];
1737 /* strcmp() is the more likely scenario */
1738 if (!strcmp(dname, n->name) ||
1739 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1741 audit_copy_inode(n, inode);
1743 n->ino = (unsigned long)-1;
1744 found_child = n->name;
1750 if (!found_parent) {
1751 if (audit_inc_name_count(context, parent))
1753 idx = context->name_count - 1;
1754 context->names[idx].name = NULL;
1755 audit_copy_inode(&context->names[idx], parent);
1759 if (audit_inc_name_count(context, inode))
1761 idx = context->name_count - 1;
1763 /* Re-use the name belonging to the slot for a matching parent
1764 * directory. All names for this context are relinquished in
1765 * audit_free_names() */
1767 context->names[idx].name = found_parent;
1768 context->names[idx].name_len = AUDIT_NAME_FULL;
1769 /* don't call __putname() */
1770 context->names[idx].name_put = 0;
1772 context->names[idx].name = NULL;
1776 audit_copy_inode(&context->names[idx], inode);
1778 context->names[idx].ino = (unsigned long)-1;
1781 EXPORT_SYMBOL_GPL(__audit_inode_child);
1784 * auditsc_get_stamp - get local copies of audit_context values
1785 * @ctx: audit_context for the task
1786 * @t: timespec to store time recorded in the audit_context
1787 * @serial: serial value that is recorded in the audit_context
1789 * Also sets the context as auditable.
1791 void auditsc_get_stamp(struct audit_context *ctx,
1792 struct timespec *t, unsigned int *serial)
1795 ctx->serial = audit_serial();
1796 t->tv_sec = ctx->ctime.tv_sec;
1797 t->tv_nsec = ctx->ctime.tv_nsec;
1798 *serial = ctx->serial;
1802 /* global counter which is incremented every time something logs in */
1803 static atomic_t session_id = ATOMIC_INIT(0);
1806 * audit_set_loginuid - set a task's audit_context loginuid
1807 * @task: task whose audit context is being modified
1808 * @loginuid: loginuid value
1812 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1814 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1816 unsigned int sessionid = atomic_inc_return(&session_id);
1817 struct audit_context *context = task->audit_context;
1819 if (context && context->in_syscall) {
1820 struct audit_buffer *ab;
1822 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1824 audit_log_format(ab, "login pid=%d uid=%u "
1825 "old auid=%u new auid=%u"
1826 " old ses=%u new ses=%u",
1827 task->pid, task->uid,
1828 task->loginuid, loginuid,
1829 task->sessionid, sessionid);
1833 task->sessionid = sessionid;
1834 task->loginuid = loginuid;
1839 * __audit_mq_open - record audit data for a POSIX MQ open
1842 * @u_attr: queue attributes
1844 * Returns 0 for success or NULL context or < 0 on error.
1846 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1848 struct audit_aux_data_mq_open *ax;
1849 struct audit_context *context = current->audit_context;
1854 if (likely(!context))
1857 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1861 if (u_attr != NULL) {
1862 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1867 memset(&ax->attr, 0, sizeof(ax->attr));
1872 ax->d.type = AUDIT_MQ_OPEN;
1873 ax->d.next = context->aux;
1874 context->aux = (void *)ax;
1879 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1880 * @mqdes: MQ descriptor
1881 * @msg_len: Message length
1882 * @msg_prio: Message priority
1883 * @u_abs_timeout: Message timeout in absolute time
1885 * Returns 0 for success or NULL context or < 0 on error.
1887 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1888 const struct timespec __user *u_abs_timeout)
1890 struct audit_aux_data_mq_sendrecv *ax;
1891 struct audit_context *context = current->audit_context;
1896 if (likely(!context))
1899 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1903 if (u_abs_timeout != NULL) {
1904 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1909 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1912 ax->msg_len = msg_len;
1913 ax->msg_prio = msg_prio;
1915 ax->d.type = AUDIT_MQ_SENDRECV;
1916 ax->d.next = context->aux;
1917 context->aux = (void *)ax;
1922 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1923 * @mqdes: MQ descriptor
1924 * @msg_len: Message length
1925 * @u_msg_prio: Message priority
1926 * @u_abs_timeout: Message timeout in absolute time
1928 * Returns 0 for success or NULL context or < 0 on error.
1930 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1931 unsigned int __user *u_msg_prio,
1932 const struct timespec __user *u_abs_timeout)
1934 struct audit_aux_data_mq_sendrecv *ax;
1935 struct audit_context *context = current->audit_context;
1940 if (likely(!context))
1943 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1947 if (u_msg_prio != NULL) {
1948 if (get_user(ax->msg_prio, u_msg_prio)) {
1955 if (u_abs_timeout != NULL) {
1956 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1961 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1964 ax->msg_len = msg_len;
1966 ax->d.type = AUDIT_MQ_SENDRECV;
1967 ax->d.next = context->aux;
1968 context->aux = (void *)ax;
1973 * __audit_mq_notify - record audit data for a POSIX MQ notify
1974 * @mqdes: MQ descriptor
1975 * @u_notification: Notification event
1977 * Returns 0 for success or NULL context or < 0 on error.
1980 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1982 struct audit_aux_data_mq_notify *ax;
1983 struct audit_context *context = current->audit_context;
1988 if (likely(!context))
1991 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1995 if (u_notification != NULL) {
1996 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
2001 memset(&ax->notification, 0, sizeof(ax->notification));
2005 ax->d.type = AUDIT_MQ_NOTIFY;
2006 ax->d.next = context->aux;
2007 context->aux = (void *)ax;
2012 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2013 * @mqdes: MQ descriptor
2016 * Returns 0 for success or NULL context or < 0 on error.
2018 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2020 struct audit_aux_data_mq_getsetattr *ax;
2021 struct audit_context *context = current->audit_context;
2026 if (likely(!context))
2029 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2034 ax->mqstat = *mqstat;
2036 ax->d.type = AUDIT_MQ_GETSETATTR;
2037 ax->d.next = context->aux;
2038 context->aux = (void *)ax;
2043 * audit_ipc_obj - record audit data for ipc object
2044 * @ipcp: ipc permissions
2046 * Returns 0 for success or NULL context or < 0 on error.
2048 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2050 struct audit_aux_data_ipcctl *ax;
2051 struct audit_context *context = current->audit_context;
2053 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2057 ax->uid = ipcp->uid;
2058 ax->gid = ipcp->gid;
2059 ax->mode = ipcp->mode;
2060 selinux_get_ipc_sid(ipcp, &ax->osid);
2062 ax->d.type = AUDIT_IPC;
2063 ax->d.next = context->aux;
2064 context->aux = (void *)ax;
2069 * audit_ipc_set_perm - record audit data for new ipc permissions
2070 * @qbytes: msgq bytes
2071 * @uid: msgq user id
2072 * @gid: msgq group id
2073 * @mode: msgq mode (permissions)
2075 * Returns 0 for success or NULL context or < 0 on error.
2077 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2079 struct audit_aux_data_ipcctl *ax;
2080 struct audit_context *context = current->audit_context;
2082 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2086 ax->qbytes = qbytes;
2091 ax->d.type = AUDIT_IPC_SET_PERM;
2092 ax->d.next = context->aux;
2093 context->aux = (void *)ax;
2097 int audit_argv_kb = 32;
2099 int audit_bprm(struct linux_binprm *bprm)
2101 struct audit_aux_data_execve *ax;
2102 struct audit_context *context = current->audit_context;
2104 if (likely(!audit_enabled || !context || context->dummy))
2108 * Even though the stack code doesn't limit the arg+env size any more,
2109 * the audit code requires that _all_ arguments be logged in a single
2110 * netlink skb. Hence cap it :-(
2112 if (bprm->argv_len > (audit_argv_kb << 10))
2115 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2119 ax->argc = bprm->argc;
2120 ax->envc = bprm->envc;
2122 ax->d.type = AUDIT_EXECVE;
2123 ax->d.next = context->aux;
2124 context->aux = (void *)ax;
2130 * audit_socketcall - record audit data for sys_socketcall
2131 * @nargs: number of args
2134 * Returns 0 for success or NULL context or < 0 on error.
2136 int audit_socketcall(int nargs, unsigned long *args)
2138 struct audit_aux_data_socketcall *ax;
2139 struct audit_context *context = current->audit_context;
2141 if (likely(!context || context->dummy))
2144 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2149 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2151 ax->d.type = AUDIT_SOCKETCALL;
2152 ax->d.next = context->aux;
2153 context->aux = (void *)ax;
2158 * __audit_fd_pair - record audit data for pipe and socketpair
2159 * @fd1: the first file descriptor
2160 * @fd2: the second file descriptor
2162 * Returns 0 for success or NULL context or < 0 on error.
2164 int __audit_fd_pair(int fd1, int fd2)
2166 struct audit_context *context = current->audit_context;
2167 struct audit_aux_data_fd_pair *ax;
2169 if (likely(!context)) {
2173 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2181 ax->d.type = AUDIT_FD_PAIR;
2182 ax->d.next = context->aux;
2183 context->aux = (void *)ax;
2188 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2189 * @len: data length in user space
2190 * @a: data address in kernel space
2192 * Returns 0 for success or NULL context or < 0 on error.
2194 int audit_sockaddr(int len, void *a)
2196 struct audit_aux_data_sockaddr *ax;
2197 struct audit_context *context = current->audit_context;
2199 if (likely(!context || context->dummy))
2202 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2207 memcpy(ax->a, a, len);
2209 ax->d.type = AUDIT_SOCKADDR;
2210 ax->d.next = context->aux;
2211 context->aux = (void *)ax;
2215 void __audit_ptrace(struct task_struct *t)
2217 struct audit_context *context = current->audit_context;
2219 context->target_pid = t->pid;
2220 context->target_auid = audit_get_loginuid(t);
2221 context->target_uid = t->uid;
2222 context->target_sessionid = audit_get_sessionid(t);
2223 selinux_get_task_sid(t, &context->target_sid);
2224 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2228 * audit_signal_info - record signal info for shutting down audit subsystem
2229 * @sig: signal value
2230 * @t: task being signaled
2232 * If the audit subsystem is being terminated, record the task (pid)
2233 * and uid that is doing that.
2235 int __audit_signal_info(int sig, struct task_struct *t)
2237 struct audit_aux_data_pids *axp;
2238 struct task_struct *tsk = current;
2239 struct audit_context *ctx = tsk->audit_context;
2240 extern pid_t audit_sig_pid;
2241 extern uid_t audit_sig_uid;
2242 extern u32 audit_sig_sid;
2244 if (audit_pid && t->tgid == audit_pid) {
2245 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2246 audit_sig_pid = tsk->pid;
2247 if (tsk->loginuid != -1)
2248 audit_sig_uid = tsk->loginuid;
2250 audit_sig_uid = tsk->uid;
2251 selinux_get_task_sid(tsk, &audit_sig_sid);
2253 if (!audit_signals || audit_dummy_context())
2257 /* optimize the common case by putting first signal recipient directly
2258 * in audit_context */
2259 if (!ctx->target_pid) {
2260 ctx->target_pid = t->tgid;
2261 ctx->target_auid = audit_get_loginuid(t);
2262 ctx->target_uid = t->uid;
2263 ctx->target_sessionid = audit_get_sessionid(t);
2264 selinux_get_task_sid(t, &ctx->target_sid);
2265 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2269 axp = (void *)ctx->aux_pids;
2270 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2271 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2275 axp->d.type = AUDIT_OBJ_PID;
2276 axp->d.next = ctx->aux_pids;
2277 ctx->aux_pids = (void *)axp;
2279 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2281 axp->target_pid[axp->pid_count] = t->tgid;
2282 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2283 axp->target_uid[axp->pid_count] = t->uid;
2284 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2285 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2286 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2293 * audit_core_dumps - record information about processes that end abnormally
2294 * @signr: signal value
2296 * If a process ends with a core dump, something fishy is going on and we
2297 * should record the event for investigation.
2299 void audit_core_dumps(long signr)
2301 struct audit_buffer *ab;
2303 uid_t auid = audit_get_loginuid(current);
2304 unsigned int sessionid = audit_get_sessionid(current);
2309 if (signr == SIGQUIT) /* don't care for those */
2312 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2313 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2314 auid, current->uid, current->gid, sessionid);
2315 selinux_get_task_sid(current, &sid);
2320 if (selinux_sid_to_string(sid, &ctx, &len))
2321 audit_log_format(ab, " ssid=%u", sid);
2323 audit_log_format(ab, " subj=%s", ctx);
2326 audit_log_format(ab, " pid=%d comm=", current->pid);
2327 audit_log_untrustedstring(ab, current->comm);
2328 audit_log_format(ab, " sig=%ld", signr);