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 <linux/atomic.h>
49 #include <linux/namei.h>
51 #include <linux/export.h>
52 #include <linux/slab.h>
53 #include <linux/mount.h>
54 #include <linux/socket.h>
55 #include <linux/mqueue.h>
56 #include <linux/audit.h>
57 #include <linux/personality.h>
58 #include <linux/time.h>
59 #include <linux/netlink.h>
60 #include <linux/compiler.h>
61 #include <asm/unistd.h>
62 #include <linux/security.h>
63 #include <linux/list.h>
64 #include <linux/tty.h>
65 #include <linux/binfmts.h>
66 #include <linux/highmem.h>
67 #include <linux/syscalls.h>
68 #include <linux/capability.h>
69 #include <linux/fs_struct.h>
73 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
74 * for saving names from getname(). */
75 #define AUDIT_NAMES 20
77 /* Indicates that audit should log the full pathname. */
78 #define AUDIT_NAME_FULL -1
80 /* no execve audit message should be longer than this (userspace limits) */
81 #define MAX_EXECVE_AUDIT_LEN 7500
83 /* number of audit rules */
86 /* determines whether we collect data for signals sent */
89 struct audit_cap_data {
90 kernel_cap_t permitted;
91 kernel_cap_t inheritable;
93 unsigned int fE; /* effective bit of a file capability */
94 kernel_cap_t effective; /* effective set of a process */
98 /* When fs/namei.c:getname() is called, we store the pointer in name and
99 * we don't let putname() free it (instead we free all of the saved
100 * pointers at syscall exit time).
102 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
105 int name_len; /* number of name's characters to log */
106 unsigned name_put; /* call __putname() for this name */
114 struct audit_cap_data fcap;
115 unsigned int fcap_ver;
118 struct audit_aux_data {
119 struct audit_aux_data *next;
123 #define AUDIT_AUX_IPCPERM 0
125 /* Number of target pids per aux struct. */
126 #define AUDIT_AUX_PIDS 16
128 struct audit_aux_data_execve {
129 struct audit_aux_data d;
132 struct mm_struct *mm;
135 struct audit_aux_data_pids {
136 struct audit_aux_data d;
137 pid_t target_pid[AUDIT_AUX_PIDS];
138 uid_t target_auid[AUDIT_AUX_PIDS];
139 uid_t target_uid[AUDIT_AUX_PIDS];
140 unsigned int target_sessionid[AUDIT_AUX_PIDS];
141 u32 target_sid[AUDIT_AUX_PIDS];
142 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
146 struct audit_aux_data_bprm_fcaps {
147 struct audit_aux_data d;
148 struct audit_cap_data fcap;
149 unsigned int fcap_ver;
150 struct audit_cap_data old_pcap;
151 struct audit_cap_data new_pcap;
154 struct audit_aux_data_capset {
155 struct audit_aux_data d;
157 struct audit_cap_data cap;
160 struct audit_tree_refs {
161 struct audit_tree_refs *next;
162 struct audit_chunk *c[31];
165 /* The per-task audit context. */
166 struct audit_context {
167 int dummy; /* must be the first element */
168 int in_syscall; /* 1 if task is in a syscall */
169 enum audit_state state, current_state;
170 unsigned int serial; /* serial number for record */
171 int major; /* syscall number */
172 struct timespec ctime; /* time of syscall entry */
173 unsigned long argv[4]; /* syscall arguments */
174 long return_code;/* syscall return code */
176 int return_valid; /* return code is valid */
178 struct audit_names names[AUDIT_NAMES];
179 char * filterkey; /* key for rule that triggered record */
181 struct audit_context *previous; /* For nested syscalls */
182 struct audit_aux_data *aux;
183 struct audit_aux_data *aux_pids;
184 struct sockaddr_storage *sockaddr;
186 /* Save things to print about task_struct */
188 uid_t uid, euid, suid, fsuid;
189 gid_t gid, egid, sgid, fsgid;
190 unsigned long personality;
196 unsigned int target_sessionid;
198 char target_comm[TASK_COMM_LEN];
200 struct audit_tree_refs *trees, *first_trees;
201 struct list_head killed_trees;
219 unsigned long qbytes;
223 struct mq_attr mqstat;
232 unsigned int msg_prio;
233 struct timespec abs_timeout;
242 struct audit_cap_data cap;
257 static inline int open_arg(int flags, int mask)
259 int n = ACC_MODE(flags);
260 if (flags & (O_TRUNC | O_CREAT))
261 n |= AUDIT_PERM_WRITE;
265 static int audit_match_perm(struct audit_context *ctx, int mask)
272 switch (audit_classify_syscall(ctx->arch, n)) {
274 if ((mask & AUDIT_PERM_WRITE) &&
275 audit_match_class(AUDIT_CLASS_WRITE, n))
277 if ((mask & AUDIT_PERM_READ) &&
278 audit_match_class(AUDIT_CLASS_READ, n))
280 if ((mask & AUDIT_PERM_ATTR) &&
281 audit_match_class(AUDIT_CLASS_CHATTR, n))
284 case 1: /* 32bit on biarch */
285 if ((mask & AUDIT_PERM_WRITE) &&
286 audit_match_class(AUDIT_CLASS_WRITE_32, n))
288 if ((mask & AUDIT_PERM_READ) &&
289 audit_match_class(AUDIT_CLASS_READ_32, n))
291 if ((mask & AUDIT_PERM_ATTR) &&
292 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
296 return mask & ACC_MODE(ctx->argv[1]);
298 return mask & ACC_MODE(ctx->argv[2]);
299 case 4: /* socketcall */
300 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
302 return mask & AUDIT_PERM_EXEC;
308 static int audit_match_filetype(struct audit_context *ctx, int val)
311 umode_t mode = (umode_t)val;
316 for (index = 0; index < ctx->name_count; index++) {
317 if ((ctx->names[index].ino != -1) &&
318 ((ctx->names[index].mode & S_IFMT) == mode))
325 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
326 * ->first_trees points to its beginning, ->trees - to the current end of data.
327 * ->tree_count is the number of free entries in array pointed to by ->trees.
328 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
329 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
330 * it's going to remain 1-element for almost any setup) until we free context itself.
331 * References in it _are_ dropped - at the same time we free/drop aux stuff.
334 #ifdef CONFIG_AUDIT_TREE
335 static void audit_set_auditable(struct audit_context *ctx)
339 ctx->current_state = AUDIT_RECORD_CONTEXT;
343 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
345 struct audit_tree_refs *p = ctx->trees;
346 int left = ctx->tree_count;
348 p->c[--left] = chunk;
349 ctx->tree_count = left;
358 ctx->tree_count = 30;
364 static int grow_tree_refs(struct audit_context *ctx)
366 struct audit_tree_refs *p = ctx->trees;
367 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
373 p->next = ctx->trees;
375 ctx->first_trees = ctx->trees;
376 ctx->tree_count = 31;
381 static void unroll_tree_refs(struct audit_context *ctx,
382 struct audit_tree_refs *p, int count)
384 #ifdef CONFIG_AUDIT_TREE
385 struct audit_tree_refs *q;
388 /* we started with empty chain */
389 p = ctx->first_trees;
391 /* if the very first allocation has failed, nothing to do */
396 for (q = p; q != ctx->trees; q = q->next, n = 31) {
398 audit_put_chunk(q->c[n]);
402 while (n-- > ctx->tree_count) {
403 audit_put_chunk(q->c[n]);
407 ctx->tree_count = count;
411 static void free_tree_refs(struct audit_context *ctx)
413 struct audit_tree_refs *p, *q;
414 for (p = ctx->first_trees; p; p = q) {
420 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
422 #ifdef CONFIG_AUDIT_TREE
423 struct audit_tree_refs *p;
428 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
429 for (n = 0; n < 31; n++)
430 if (audit_tree_match(p->c[n], tree))
435 for (n = ctx->tree_count; n < 31; n++)
436 if (audit_tree_match(p->c[n], tree))
443 /* Determine if any context name data matches a rule's watch data */
444 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
447 * If task_creation is true, this is an explicit indication that we are
448 * filtering a task rule at task creation time. This and tsk == current are
449 * the only situations where tsk->cred may be accessed without an rcu read lock.
451 static int audit_filter_rules(struct task_struct *tsk,
452 struct audit_krule *rule,
453 struct audit_context *ctx,
454 struct audit_names *name,
455 enum audit_state *state,
458 const struct cred *cred;
459 int i, j, need_sid = 1;
462 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
464 for (i = 0; i < rule->field_count; i++) {
465 struct audit_field *f = &rule->fields[i];
470 result = audit_comparator(tsk->pid, f->op, f->val);
475 ctx->ppid = sys_getppid();
476 result = audit_comparator(ctx->ppid, f->op, f->val);
480 result = audit_comparator(cred->uid, f->op, f->val);
483 result = audit_comparator(cred->euid, f->op, f->val);
486 result = audit_comparator(cred->suid, f->op, f->val);
489 result = audit_comparator(cred->fsuid, f->op, f->val);
492 result = audit_comparator(cred->gid, f->op, f->val);
495 result = audit_comparator(cred->egid, f->op, f->val);
498 result = audit_comparator(cred->sgid, f->op, f->val);
501 result = audit_comparator(cred->fsgid, f->op, f->val);
504 result = audit_comparator(tsk->personality, f->op, f->val);
508 result = audit_comparator(ctx->arch, f->op, f->val);
512 if (ctx && ctx->return_valid)
513 result = audit_comparator(ctx->return_code, f->op, f->val);
516 if (ctx && ctx->return_valid) {
518 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
520 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
525 result = audit_comparator(MAJOR(name->dev),
528 for (j = 0; j < ctx->name_count; j++) {
529 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
538 result = audit_comparator(MINOR(name->dev),
541 for (j = 0; j < ctx->name_count; j++) {
542 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
551 result = (name->ino == f->val);
553 for (j = 0; j < ctx->name_count; j++) {
554 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
563 result = audit_watch_compare(rule->watch, name->ino, name->dev);
567 result = match_tree_refs(ctx, rule->tree);
572 result = audit_comparator(tsk->loginuid, f->op, f->val);
574 case AUDIT_SUBJ_USER:
575 case AUDIT_SUBJ_ROLE:
576 case AUDIT_SUBJ_TYPE:
579 /* NOTE: this may return negative values indicating
580 a temporary error. We simply treat this as a
581 match for now to avoid losing information that
582 may be wanted. An error message will also be
586 security_task_getsecid(tsk, &sid);
589 result = security_audit_rule_match(sid, f->type,
598 case AUDIT_OBJ_LEV_LOW:
599 case AUDIT_OBJ_LEV_HIGH:
600 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
603 /* Find files that match */
605 result = security_audit_rule_match(
606 name->osid, f->type, f->op,
609 for (j = 0; j < ctx->name_count; j++) {
610 if (security_audit_rule_match(
619 /* Find ipc objects that match */
620 if (!ctx || ctx->type != AUDIT_IPC)
622 if (security_audit_rule_match(ctx->ipc.osid,
633 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
635 case AUDIT_FILTERKEY:
636 /* ignore this field for filtering */
640 result = audit_match_perm(ctx, f->val);
643 result = audit_match_filetype(ctx, f->val);
652 if (rule->prio <= ctx->prio)
654 if (rule->filterkey) {
655 kfree(ctx->filterkey);
656 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
658 ctx->prio = rule->prio;
660 switch (rule->action) {
661 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
662 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
667 /* At process creation time, we can determine if system-call auditing is
668 * completely disabled for this task. Since we only have the task
669 * structure at this point, we can only check uid and gid.
671 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
673 struct audit_entry *e;
674 enum audit_state state;
677 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
678 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
680 if (state == AUDIT_RECORD_CONTEXT)
681 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
687 return AUDIT_BUILD_CONTEXT;
690 /* At syscall entry and exit time, this filter is called if the
691 * audit_state is not low enough that auditing cannot take place, but is
692 * also not high enough that we already know we have to write an audit
693 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
695 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
696 struct audit_context *ctx,
697 struct list_head *list)
699 struct audit_entry *e;
700 enum audit_state state;
702 if (audit_pid && tsk->tgid == audit_pid)
703 return AUDIT_DISABLED;
706 if (!list_empty(list)) {
707 int word = AUDIT_WORD(ctx->major);
708 int bit = AUDIT_BIT(ctx->major);
710 list_for_each_entry_rcu(e, list, list) {
711 if ((e->rule.mask[word] & bit) == bit &&
712 audit_filter_rules(tsk, &e->rule, ctx, NULL,
715 ctx->current_state = state;
721 return AUDIT_BUILD_CONTEXT;
724 /* At syscall exit time, this filter is called if any audit_names[] have been
725 * collected during syscall processing. We only check rules in sublists at hash
726 * buckets applicable to the inode numbers in audit_names[].
727 * Regarding audit_state, same rules apply as for audit_filter_syscall().
729 void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
732 struct audit_entry *e;
733 enum audit_state state;
735 if (audit_pid && tsk->tgid == audit_pid)
739 for (i = 0; i < ctx->name_count; i++) {
740 int word = AUDIT_WORD(ctx->major);
741 int bit = AUDIT_BIT(ctx->major);
742 struct audit_names *n = &ctx->names[i];
743 int h = audit_hash_ino((u32)n->ino);
744 struct list_head *list = &audit_inode_hash[h];
746 if (list_empty(list))
749 list_for_each_entry_rcu(e, list, list) {
750 if ((e->rule.mask[word] & bit) == bit &&
751 audit_filter_rules(tsk, &e->rule, ctx, n,
754 ctx->current_state = state;
762 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
766 struct audit_context *context = tsk->audit_context;
768 if (likely(!context))
770 context->return_valid = return_valid;
773 * we need to fix up the return code in the audit logs if the actual
774 * return codes are later going to be fixed up by the arch specific
777 * This is actually a test for:
778 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
779 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
781 * but is faster than a bunch of ||
783 if (unlikely(return_code <= -ERESTARTSYS) &&
784 (return_code >= -ERESTART_RESTARTBLOCK) &&
785 (return_code != -ENOIOCTLCMD))
786 context->return_code = -EINTR;
788 context->return_code = return_code;
790 if (context->in_syscall && !context->dummy) {
791 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
792 audit_filter_inodes(tsk, context);
795 tsk->audit_context = NULL;
799 static inline void audit_free_names(struct audit_context *context)
804 if (context->put_count + context->ino_count != context->name_count) {
805 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
806 " name_count=%d put_count=%d"
807 " ino_count=%d [NOT freeing]\n",
809 context->serial, context->major, context->in_syscall,
810 context->name_count, context->put_count,
812 for (i = 0; i < context->name_count; i++) {
813 printk(KERN_ERR "names[%d] = %p = %s\n", i,
814 context->names[i].name,
815 context->names[i].name ?: "(null)");
822 context->put_count = 0;
823 context->ino_count = 0;
826 for (i = 0; i < context->name_count; i++) {
827 if (context->names[i].name && context->names[i].name_put)
828 __putname(context->names[i].name);
830 context->name_count = 0;
831 path_put(&context->pwd);
832 context->pwd.dentry = NULL;
833 context->pwd.mnt = NULL;
836 static inline void audit_free_aux(struct audit_context *context)
838 struct audit_aux_data *aux;
840 while ((aux = context->aux)) {
841 context->aux = aux->next;
844 while ((aux = context->aux_pids)) {
845 context->aux_pids = aux->next;
850 static inline void audit_zero_context(struct audit_context *context,
851 enum audit_state state)
853 memset(context, 0, sizeof(*context));
854 context->state = state;
855 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
858 static inline struct audit_context *audit_alloc_context(enum audit_state state)
860 struct audit_context *context;
862 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
864 audit_zero_context(context, state);
865 INIT_LIST_HEAD(&context->killed_trees);
870 * audit_alloc - allocate an audit context block for a task
873 * Filter on the task information and allocate a per-task audit context
874 * if necessary. Doing so turns on system call auditing for the
875 * specified task. This is called from copy_process, so no lock is
878 int audit_alloc(struct task_struct *tsk)
880 struct audit_context *context;
881 enum audit_state state;
884 if (likely(!audit_ever_enabled))
885 return 0; /* Return if not auditing. */
887 state = audit_filter_task(tsk, &key);
888 if (likely(state == AUDIT_DISABLED))
891 if (!(context = audit_alloc_context(state))) {
893 audit_log_lost("out of memory in audit_alloc");
896 context->filterkey = key;
898 tsk->audit_context = context;
899 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
903 static inline void audit_free_context(struct audit_context *context)
905 struct audit_context *previous;
909 previous = context->previous;
910 if (previous || (count && count < 10)) {
912 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
913 " freeing multiple contexts (%d)\n",
914 context->serial, context->major,
915 context->name_count, count);
917 audit_free_names(context);
918 unroll_tree_refs(context, NULL, 0);
919 free_tree_refs(context);
920 audit_free_aux(context);
921 kfree(context->filterkey);
922 kfree(context->sockaddr);
927 printk(KERN_ERR "audit: freed %d contexts\n", count);
930 void audit_log_task_context(struct audit_buffer *ab)
937 security_task_getsecid(current, &sid);
941 error = security_secid_to_secctx(sid, &ctx, &len);
943 if (error != -EINVAL)
948 audit_log_format(ab, " subj=%s", ctx);
949 security_release_secctx(ctx, len);
953 audit_panic("error in audit_log_task_context");
957 EXPORT_SYMBOL(audit_log_task_context);
959 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
961 char name[sizeof(tsk->comm)];
962 struct mm_struct *mm = tsk->mm;
963 struct vm_area_struct *vma;
967 get_task_comm(name, tsk);
968 audit_log_format(ab, " comm=");
969 audit_log_untrustedstring(ab, name);
972 down_read(&mm->mmap_sem);
975 if ((vma->vm_flags & VM_EXECUTABLE) &&
977 audit_log_d_path(ab, "exe=",
978 &vma->vm_file->f_path);
983 up_read(&mm->mmap_sem);
985 audit_log_task_context(ab);
988 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
989 uid_t auid, uid_t uid, unsigned int sessionid,
992 struct audit_buffer *ab;
997 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
1001 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
1003 if (security_secid_to_secctx(sid, &ctx, &len)) {
1004 audit_log_format(ab, " obj=(none)");
1007 audit_log_format(ab, " obj=%s", ctx);
1008 security_release_secctx(ctx, len);
1010 audit_log_format(ab, " ocomm=");
1011 audit_log_untrustedstring(ab, comm);
1018 * to_send and len_sent accounting are very loose estimates. We aren't
1019 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1020 * within about 500 bytes (next page boundary)
1022 * why snprintf? an int is up to 12 digits long. if we just assumed when
1023 * logging that a[%d]= was going to be 16 characters long we would be wasting
1024 * space in every audit message. In one 7500 byte message we can log up to
1025 * about 1000 min size arguments. That comes down to about 50% waste of space
1026 * if we didn't do the snprintf to find out how long arg_num_len was.
1028 static int audit_log_single_execve_arg(struct audit_context *context,
1029 struct audit_buffer **ab,
1032 const char __user *p,
1035 char arg_num_len_buf[12];
1036 const char __user *tmp_p = p;
1037 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1038 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
1039 size_t len, len_left, to_send;
1040 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1041 unsigned int i, has_cntl = 0, too_long = 0;
1044 /* strnlen_user includes the null we don't want to send */
1045 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1048 * We just created this mm, if we can't find the strings
1049 * we just copied into it something is _very_ wrong. Similar
1050 * for strings that are too long, we should not have created
1053 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
1055 send_sig(SIGKILL, current, 0);
1059 /* walk the whole argument looking for non-ascii chars */
1061 if (len_left > MAX_EXECVE_AUDIT_LEN)
1062 to_send = MAX_EXECVE_AUDIT_LEN;
1065 ret = copy_from_user(buf, tmp_p, to_send);
1067 * There is no reason for this copy to be short. We just
1068 * copied them here, and the mm hasn't been exposed to user-
1073 send_sig(SIGKILL, current, 0);
1076 buf[to_send] = '\0';
1077 has_cntl = audit_string_contains_control(buf, to_send);
1080 * hex messages get logged as 2 bytes, so we can only
1081 * send half as much in each message
1083 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1086 len_left -= to_send;
1088 } while (len_left > 0);
1092 if (len > max_execve_audit_len)
1095 /* rewalk the argument actually logging the message */
1096 for (i = 0; len_left > 0; i++) {
1099 if (len_left > max_execve_audit_len)
1100 to_send = max_execve_audit_len;
1104 /* do we have space left to send this argument in this ab? */
1105 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1107 room_left -= (to_send * 2);
1109 room_left -= to_send;
1110 if (room_left < 0) {
1113 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1119 * first record needs to say how long the original string was
1120 * so we can be sure nothing was lost.
1122 if ((i == 0) && (too_long))
1123 audit_log_format(*ab, " a%d_len=%zu", arg_num,
1124 has_cntl ? 2*len : len);
1127 * normally arguments are small enough to fit and we already
1128 * filled buf above when we checked for control characters
1129 * so don't bother with another copy_from_user
1131 if (len >= max_execve_audit_len)
1132 ret = copy_from_user(buf, p, to_send);
1137 send_sig(SIGKILL, current, 0);
1140 buf[to_send] = '\0';
1142 /* actually log it */
1143 audit_log_format(*ab, " a%d", arg_num);
1145 audit_log_format(*ab, "[%d]", i);
1146 audit_log_format(*ab, "=");
1148 audit_log_n_hex(*ab, buf, to_send);
1150 audit_log_string(*ab, buf);
1153 len_left -= to_send;
1154 *len_sent += arg_num_len;
1156 *len_sent += to_send * 2;
1158 *len_sent += to_send;
1160 /* include the null we didn't log */
1164 static void audit_log_execve_info(struct audit_context *context,
1165 struct audit_buffer **ab,
1166 struct audit_aux_data_execve *axi)
1169 size_t len, len_sent = 0;
1170 const char __user *p;
1173 if (axi->mm != current->mm)
1174 return; /* execve failed, no additional info */
1176 p = (const char __user *)axi->mm->arg_start;
1178 audit_log_format(*ab, "argc=%d", axi->argc);
1181 * we need some kernel buffer to hold the userspace args. Just
1182 * allocate one big one rather than allocating one of the right size
1183 * for every single argument inside audit_log_single_execve_arg()
1184 * should be <8k allocation so should be pretty safe.
1186 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1188 audit_panic("out of memory for argv string\n");
1192 for (i = 0; i < axi->argc; i++) {
1193 len = audit_log_single_execve_arg(context, ab, i,
1202 static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1206 audit_log_format(ab, " %s=", prefix);
1207 CAP_FOR_EACH_U32(i) {
1208 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1212 static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1214 kernel_cap_t *perm = &name->fcap.permitted;
1215 kernel_cap_t *inh = &name->fcap.inheritable;
1218 if (!cap_isclear(*perm)) {
1219 audit_log_cap(ab, "cap_fp", perm);
1222 if (!cap_isclear(*inh)) {
1223 audit_log_cap(ab, "cap_fi", inh);
1228 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1231 static void show_special(struct audit_context *context, int *call_panic)
1233 struct audit_buffer *ab;
1236 ab = audit_log_start(context, GFP_KERNEL, context->type);
1240 switch (context->type) {
1241 case AUDIT_SOCKETCALL: {
1242 int nargs = context->socketcall.nargs;
1243 audit_log_format(ab, "nargs=%d", nargs);
1244 for (i = 0; i < nargs; i++)
1245 audit_log_format(ab, " a%d=%lx", i,
1246 context->socketcall.args[i]);
1249 u32 osid = context->ipc.osid;
1251 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1252 context->ipc.uid, context->ipc.gid, context->ipc.mode);
1256 if (security_secid_to_secctx(osid, &ctx, &len)) {
1257 audit_log_format(ab, " osid=%u", osid);
1260 audit_log_format(ab, " obj=%s", ctx);
1261 security_release_secctx(ctx, len);
1264 if (context->ipc.has_perm) {
1266 ab = audit_log_start(context, GFP_KERNEL,
1267 AUDIT_IPC_SET_PERM);
1268 audit_log_format(ab,
1269 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1270 context->ipc.qbytes,
1271 context->ipc.perm_uid,
1272 context->ipc.perm_gid,
1273 context->ipc.perm_mode);
1278 case AUDIT_MQ_OPEN: {
1279 audit_log_format(ab,
1280 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1281 "mq_msgsize=%ld mq_curmsgs=%ld",
1282 context->mq_open.oflag, context->mq_open.mode,
1283 context->mq_open.attr.mq_flags,
1284 context->mq_open.attr.mq_maxmsg,
1285 context->mq_open.attr.mq_msgsize,
1286 context->mq_open.attr.mq_curmsgs);
1288 case AUDIT_MQ_SENDRECV: {
1289 audit_log_format(ab,
1290 "mqdes=%d msg_len=%zd msg_prio=%u "
1291 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1292 context->mq_sendrecv.mqdes,
1293 context->mq_sendrecv.msg_len,
1294 context->mq_sendrecv.msg_prio,
1295 context->mq_sendrecv.abs_timeout.tv_sec,
1296 context->mq_sendrecv.abs_timeout.tv_nsec);
1298 case AUDIT_MQ_NOTIFY: {
1299 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1300 context->mq_notify.mqdes,
1301 context->mq_notify.sigev_signo);
1303 case AUDIT_MQ_GETSETATTR: {
1304 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1305 audit_log_format(ab,
1306 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1308 context->mq_getsetattr.mqdes,
1309 attr->mq_flags, attr->mq_maxmsg,
1310 attr->mq_msgsize, attr->mq_curmsgs);
1312 case AUDIT_CAPSET: {
1313 audit_log_format(ab, "pid=%d", context->capset.pid);
1314 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1315 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1316 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1319 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1320 context->mmap.flags);
1326 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1328 const struct cred *cred;
1329 int i, call_panic = 0;
1330 struct audit_buffer *ab;
1331 struct audit_aux_data *aux;
1334 /* tsk == current */
1335 context->pid = tsk->pid;
1337 context->ppid = sys_getppid();
1338 cred = current_cred();
1339 context->uid = cred->uid;
1340 context->gid = cred->gid;
1341 context->euid = cred->euid;
1342 context->suid = cred->suid;
1343 context->fsuid = cred->fsuid;
1344 context->egid = cred->egid;
1345 context->sgid = cred->sgid;
1346 context->fsgid = cred->fsgid;
1347 context->personality = tsk->personality;
1349 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1351 return; /* audit_panic has been called */
1352 audit_log_format(ab, "arch=%x syscall=%d",
1353 context->arch, context->major);
1354 if (context->personality != PER_LINUX)
1355 audit_log_format(ab, " per=%lx", context->personality);
1356 if (context->return_valid)
1357 audit_log_format(ab, " success=%s exit=%ld",
1358 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1359 context->return_code);
1361 spin_lock_irq(&tsk->sighand->siglock);
1362 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1363 tty = tsk->signal->tty->name;
1366 spin_unlock_irq(&tsk->sighand->siglock);
1368 audit_log_format(ab,
1369 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1370 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1371 " euid=%u suid=%u fsuid=%u"
1372 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1377 context->name_count,
1383 context->euid, context->suid, context->fsuid,
1384 context->egid, context->sgid, context->fsgid, tty,
1388 audit_log_task_info(ab, tsk);
1389 audit_log_key(ab, context->filterkey);
1392 for (aux = context->aux; aux; aux = aux->next) {
1394 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1396 continue; /* audit_panic has been called */
1398 switch (aux->type) {
1400 case AUDIT_EXECVE: {
1401 struct audit_aux_data_execve *axi = (void *)aux;
1402 audit_log_execve_info(context, &ab, axi);
1405 case AUDIT_BPRM_FCAPS: {
1406 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1407 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1408 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1409 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1410 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1411 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1412 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1413 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1414 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1415 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1416 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1424 show_special(context, &call_panic);
1426 if (context->fds[0] >= 0) {
1427 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1429 audit_log_format(ab, "fd0=%d fd1=%d",
1430 context->fds[0], context->fds[1]);
1435 if (context->sockaddr_len) {
1436 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1438 audit_log_format(ab, "saddr=");
1439 audit_log_n_hex(ab, (void *)context->sockaddr,
1440 context->sockaddr_len);
1445 for (aux = context->aux_pids; aux; aux = aux->next) {
1446 struct audit_aux_data_pids *axs = (void *)aux;
1448 for (i = 0; i < axs->pid_count; i++)
1449 if (audit_log_pid_context(context, axs->target_pid[i],
1450 axs->target_auid[i],
1452 axs->target_sessionid[i],
1454 axs->target_comm[i]))
1458 if (context->target_pid &&
1459 audit_log_pid_context(context, context->target_pid,
1460 context->target_auid, context->target_uid,
1461 context->target_sessionid,
1462 context->target_sid, context->target_comm))
1465 if (context->pwd.dentry && context->pwd.mnt) {
1466 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1468 audit_log_d_path(ab, "cwd=", &context->pwd);
1472 for (i = 0; i < context->name_count; i++) {
1473 struct audit_names *n = &context->names[i];
1475 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1477 continue; /* audit_panic has been called */
1479 audit_log_format(ab, "item=%d", i);
1482 switch(n->name_len) {
1483 case AUDIT_NAME_FULL:
1484 /* log the full path */
1485 audit_log_format(ab, " name=");
1486 audit_log_untrustedstring(ab, n->name);
1489 /* name was specified as a relative path and the
1490 * directory component is the cwd */
1491 audit_log_d_path(ab, "name=", &context->pwd);
1494 /* log the name's directory component */
1495 audit_log_format(ab, " name=");
1496 audit_log_n_untrustedstring(ab, n->name,
1500 audit_log_format(ab, " name=(null)");
1502 if (n->ino != (unsigned long)-1) {
1503 audit_log_format(ab, " inode=%lu"
1504 " dev=%02x:%02x mode=%#ho"
1505 " ouid=%u ogid=%u rdev=%02x:%02x",
1518 if (security_secid_to_secctx(
1519 n->osid, &ctx, &len)) {
1520 audit_log_format(ab, " osid=%u", n->osid);
1523 audit_log_format(ab, " obj=%s", ctx);
1524 security_release_secctx(ctx, len);
1528 audit_log_fcaps(ab, n);
1533 /* Send end of event record to help user space know we are finished */
1534 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1538 audit_panic("error converting sid to string");
1542 * audit_free - free a per-task audit context
1543 * @tsk: task whose audit context block to free
1545 * Called from copy_process and do_exit
1547 void audit_free(struct task_struct *tsk)
1549 struct audit_context *context;
1551 context = audit_get_context(tsk, 0, 0);
1552 if (likely(!context))
1555 /* Check for system calls that do not go through the exit
1556 * function (e.g., exit_group), then free context block.
1557 * We use GFP_ATOMIC here because we might be doing this
1558 * in the context of the idle thread */
1559 /* that can happen only if we are called from do_exit() */
1560 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1561 audit_log_exit(context, tsk);
1562 if (!list_empty(&context->killed_trees))
1563 audit_kill_trees(&context->killed_trees);
1565 audit_free_context(context);
1569 * audit_syscall_entry - fill in an audit record at syscall entry
1570 * @arch: architecture type
1571 * @major: major syscall type (function)
1572 * @a1: additional syscall register 1
1573 * @a2: additional syscall register 2
1574 * @a3: additional syscall register 3
1575 * @a4: additional syscall register 4
1577 * Fill in audit context at syscall entry. This only happens if the
1578 * audit context was created when the task was created and the state or
1579 * filters demand the audit context be built. If the state from the
1580 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1581 * then the record will be written at syscall exit time (otherwise, it
1582 * will only be written if another part of the kernel requests that it
1585 void audit_syscall_entry(int arch, int major,
1586 unsigned long a1, unsigned long a2,
1587 unsigned long a3, unsigned long a4)
1589 struct task_struct *tsk = current;
1590 struct audit_context *context = tsk->audit_context;
1591 enum audit_state state;
1593 if (unlikely(!context))
1597 * This happens only on certain architectures that make system
1598 * calls in kernel_thread via the entry.S interface, instead of
1599 * with direct calls. (If you are porting to a new
1600 * architecture, hitting this condition can indicate that you
1601 * got the _exit/_leave calls backward in entry.S.)
1605 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1607 * This also happens with vm86 emulation in a non-nested manner
1608 * (entries without exits), so this case must be caught.
1610 if (context->in_syscall) {
1611 struct audit_context *newctx;
1615 "audit(:%d) pid=%d in syscall=%d;"
1616 " entering syscall=%d\n",
1617 context->serial, tsk->pid, context->major, major);
1619 newctx = audit_alloc_context(context->state);
1621 newctx->previous = context;
1623 tsk->audit_context = newctx;
1625 /* If we can't alloc a new context, the best we
1626 * can do is to leak memory (any pending putname
1627 * will be lost). The only other alternative is
1628 * to abandon auditing. */
1629 audit_zero_context(context, context->state);
1632 BUG_ON(context->in_syscall || context->name_count);
1637 context->arch = arch;
1638 context->major = major;
1639 context->argv[0] = a1;
1640 context->argv[1] = a2;
1641 context->argv[2] = a3;
1642 context->argv[3] = a4;
1644 state = context->state;
1645 context->dummy = !audit_n_rules;
1646 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1648 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1650 if (likely(state == AUDIT_DISABLED))
1653 context->serial = 0;
1654 context->ctime = CURRENT_TIME;
1655 context->in_syscall = 1;
1656 context->current_state = state;
1660 void audit_finish_fork(struct task_struct *child)
1662 struct audit_context *ctx = current->audit_context;
1663 struct audit_context *p = child->audit_context;
1666 if (!ctx->in_syscall || ctx->current_state != AUDIT_RECORD_CONTEXT)
1668 p->arch = ctx->arch;
1669 p->major = ctx->major;
1670 memcpy(p->argv, ctx->argv, sizeof(ctx->argv));
1671 p->ctime = ctx->ctime;
1672 p->dummy = ctx->dummy;
1673 p->in_syscall = ctx->in_syscall;
1674 p->filterkey = kstrdup(ctx->filterkey, GFP_KERNEL);
1675 p->ppid = current->pid;
1676 p->prio = ctx->prio;
1677 p->current_state = ctx->current_state;
1681 * audit_syscall_exit - deallocate audit context after a system call
1682 * @valid: success/failure flag
1683 * @return_code: syscall return value
1685 * Tear down after system call. If the audit context has been marked as
1686 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1687 * filtering, or because some other part of the kernel write an audit
1688 * message), then write out the syscall information. In call cases,
1689 * free the names stored from getname().
1691 void audit_syscall_exit(int valid, long return_code)
1693 struct task_struct *tsk = current;
1694 struct audit_context *context;
1696 context = audit_get_context(tsk, valid, return_code);
1698 if (likely(!context))
1701 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1702 audit_log_exit(context, tsk);
1704 context->in_syscall = 0;
1705 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
1707 if (!list_empty(&context->killed_trees))
1708 audit_kill_trees(&context->killed_trees);
1710 if (context->previous) {
1711 struct audit_context *new_context = context->previous;
1712 context->previous = NULL;
1713 audit_free_context(context);
1714 tsk->audit_context = new_context;
1716 audit_free_names(context);
1717 unroll_tree_refs(context, NULL, 0);
1718 audit_free_aux(context);
1719 context->aux = NULL;
1720 context->aux_pids = NULL;
1721 context->target_pid = 0;
1722 context->target_sid = 0;
1723 context->sockaddr_len = 0;
1725 context->fds[0] = -1;
1726 if (context->state != AUDIT_RECORD_CONTEXT) {
1727 kfree(context->filterkey);
1728 context->filterkey = NULL;
1730 tsk->audit_context = context;
1734 static inline void handle_one(const struct inode *inode)
1736 #ifdef CONFIG_AUDIT_TREE
1737 struct audit_context *context;
1738 struct audit_tree_refs *p;
1739 struct audit_chunk *chunk;
1741 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
1743 context = current->audit_context;
1745 count = context->tree_count;
1747 chunk = audit_tree_lookup(inode);
1751 if (likely(put_tree_ref(context, chunk)))
1753 if (unlikely(!grow_tree_refs(context))) {
1754 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
1755 audit_set_auditable(context);
1756 audit_put_chunk(chunk);
1757 unroll_tree_refs(context, p, count);
1760 put_tree_ref(context, chunk);
1764 static void handle_path(const struct dentry *dentry)
1766 #ifdef CONFIG_AUDIT_TREE
1767 struct audit_context *context;
1768 struct audit_tree_refs *p;
1769 const struct dentry *d, *parent;
1770 struct audit_chunk *drop;
1774 context = current->audit_context;
1776 count = context->tree_count;
1781 seq = read_seqbegin(&rename_lock);
1783 struct inode *inode = d->d_inode;
1784 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
1785 struct audit_chunk *chunk;
1786 chunk = audit_tree_lookup(inode);
1788 if (unlikely(!put_tree_ref(context, chunk))) {
1794 parent = d->d_parent;
1799 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1802 /* just a race with rename */
1803 unroll_tree_refs(context, p, count);
1806 audit_put_chunk(drop);
1807 if (grow_tree_refs(context)) {
1808 /* OK, got more space */
1809 unroll_tree_refs(context, p, count);
1814 "out of memory, audit has lost a tree reference\n");
1815 unroll_tree_refs(context, p, count);
1816 audit_set_auditable(context);
1824 * audit_getname - add a name to the list
1825 * @name: name to add
1827 * Add a name to the list of audit names for this context.
1828 * Called from fs/namei.c:getname().
1830 void __audit_getname(const char *name)
1832 struct audit_context *context = current->audit_context;
1834 if (IS_ERR(name) || !name)
1837 if (!context->in_syscall) {
1838 #if AUDIT_DEBUG == 2
1839 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1840 __FILE__, __LINE__, context->serial, name);
1845 BUG_ON(context->name_count >= AUDIT_NAMES);
1846 context->names[context->name_count].name = name;
1847 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1848 context->names[context->name_count].name_put = 1;
1849 context->names[context->name_count].ino = (unsigned long)-1;
1850 context->names[context->name_count].osid = 0;
1851 ++context->name_count;
1852 if (!context->pwd.dentry)
1853 get_fs_pwd(current->fs, &context->pwd);
1856 /* audit_putname - intercept a putname request
1857 * @name: name to intercept and delay for putname
1859 * If we have stored the name from getname in the audit context,
1860 * then we delay the putname until syscall exit.
1861 * Called from include/linux/fs.h:putname().
1863 void audit_putname(const char *name)
1865 struct audit_context *context = current->audit_context;
1868 if (!context->in_syscall) {
1869 #if AUDIT_DEBUG == 2
1870 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1871 __FILE__, __LINE__, context->serial, name);
1872 if (context->name_count) {
1874 for (i = 0; i < context->name_count; i++)
1875 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1876 context->names[i].name,
1877 context->names[i].name ?: "(null)");
1884 ++context->put_count;
1885 if (context->put_count > context->name_count) {
1886 printk(KERN_ERR "%s:%d(:%d): major=%d"
1887 " in_syscall=%d putname(%p) name_count=%d"
1890 context->serial, context->major,
1891 context->in_syscall, name, context->name_count,
1892 context->put_count);
1899 static int audit_inc_name_count(struct audit_context *context,
1900 const struct inode *inode)
1902 if (context->name_count >= AUDIT_NAMES) {
1904 printk(KERN_DEBUG "audit: name_count maxed, losing inode data: "
1905 "dev=%02x:%02x, inode=%lu\n",
1906 MAJOR(inode->i_sb->s_dev),
1907 MINOR(inode->i_sb->s_dev),
1911 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
1914 context->name_count++;
1916 context->ino_count++;
1922 static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1924 struct cpu_vfs_cap_data caps;
1927 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1928 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1935 rc = get_vfs_caps_from_disk(dentry, &caps);
1939 name->fcap.permitted = caps.permitted;
1940 name->fcap.inheritable = caps.inheritable;
1941 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1942 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1948 /* Copy inode data into an audit_names. */
1949 static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1950 const struct inode *inode)
1952 name->ino = inode->i_ino;
1953 name->dev = inode->i_sb->s_dev;
1954 name->mode = inode->i_mode;
1955 name->uid = inode->i_uid;
1956 name->gid = inode->i_gid;
1957 name->rdev = inode->i_rdev;
1958 security_inode_getsecid(inode, &name->osid);
1959 audit_copy_fcaps(name, dentry);
1963 * audit_inode - store the inode and device from a lookup
1964 * @name: name being audited
1965 * @dentry: dentry being audited
1967 * Called from fs/namei.c:path_lookup().
1969 void __audit_inode(const char *name, const struct dentry *dentry)
1972 struct audit_context *context = current->audit_context;
1973 const struct inode *inode = dentry->d_inode;
1975 if (!context->in_syscall)
1977 if (context->name_count
1978 && context->names[context->name_count-1].name
1979 && context->names[context->name_count-1].name == name)
1980 idx = context->name_count - 1;
1981 else if (context->name_count > 1
1982 && context->names[context->name_count-2].name
1983 && context->names[context->name_count-2].name == name)
1984 idx = context->name_count - 2;
1986 /* FIXME: how much do we care about inodes that have no
1987 * associated name? */
1988 if (audit_inc_name_count(context, inode))
1990 idx = context->name_count - 1;
1991 context->names[idx].name = NULL;
1993 handle_path(dentry);
1994 audit_copy_inode(&context->names[idx], dentry, inode);
1998 * audit_inode_child - collect inode info for created/removed objects
1999 * @dentry: dentry being audited
2000 * @parent: inode of dentry parent
2002 * For syscalls that create or remove filesystem objects, audit_inode
2003 * can only collect information for the filesystem object's parent.
2004 * This call updates the audit context with the child's information.
2005 * Syscalls that create a new filesystem object must be hooked after
2006 * the object is created. Syscalls that remove a filesystem object
2007 * must be hooked prior, in order to capture the target inode during
2008 * unsuccessful attempts.
2010 void __audit_inode_child(const struct dentry *dentry,
2011 const struct inode *parent)
2014 struct audit_context *context = current->audit_context;
2015 const char *found_parent = NULL, *found_child = NULL;
2016 const struct inode *inode = dentry->d_inode;
2017 const char *dname = dentry->d_name.name;
2020 if (!context->in_syscall)
2026 /* parent is more likely, look for it first */
2027 for (idx = 0; idx < context->name_count; idx++) {
2028 struct audit_names *n = &context->names[idx];
2033 if (n->ino == parent->i_ino &&
2034 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2035 n->name_len = dirlen; /* update parent data in place */
2036 found_parent = n->name;
2041 /* no matching parent, look for matching child */
2042 for (idx = 0; idx < context->name_count; idx++) {
2043 struct audit_names *n = &context->names[idx];
2048 /* strcmp() is the more likely scenario */
2049 if (!strcmp(dname, n->name) ||
2050 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2052 audit_copy_inode(n, NULL, inode);
2054 n->ino = (unsigned long)-1;
2055 found_child = n->name;
2061 if (!found_parent) {
2062 if (audit_inc_name_count(context, parent))
2064 idx = context->name_count - 1;
2065 context->names[idx].name = NULL;
2066 audit_copy_inode(&context->names[idx], NULL, parent);
2070 if (audit_inc_name_count(context, inode))
2072 idx = context->name_count - 1;
2074 /* Re-use the name belonging to the slot for a matching parent
2075 * directory. All names for this context are relinquished in
2076 * audit_free_names() */
2078 context->names[idx].name = found_parent;
2079 context->names[idx].name_len = AUDIT_NAME_FULL;
2080 /* don't call __putname() */
2081 context->names[idx].name_put = 0;
2083 context->names[idx].name = NULL;
2087 audit_copy_inode(&context->names[idx], NULL, inode);
2089 context->names[idx].ino = (unsigned long)-1;
2092 EXPORT_SYMBOL_GPL(__audit_inode_child);
2095 * auditsc_get_stamp - get local copies of audit_context values
2096 * @ctx: audit_context for the task
2097 * @t: timespec to store time recorded in the audit_context
2098 * @serial: serial value that is recorded in the audit_context
2100 * Also sets the context as auditable.
2102 int auditsc_get_stamp(struct audit_context *ctx,
2103 struct timespec *t, unsigned int *serial)
2105 if (!ctx->in_syscall)
2108 ctx->serial = audit_serial();
2109 t->tv_sec = ctx->ctime.tv_sec;
2110 t->tv_nsec = ctx->ctime.tv_nsec;
2111 *serial = ctx->serial;
2114 ctx->current_state = AUDIT_RECORD_CONTEXT;
2119 /* global counter which is incremented every time something logs in */
2120 static atomic_t session_id = ATOMIC_INIT(0);
2123 * audit_set_loginuid - set a task's audit_context loginuid
2124 * @task: task whose audit context is being modified
2125 * @loginuid: loginuid value
2129 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2131 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
2133 unsigned int sessionid = atomic_inc_return(&session_id);
2134 struct audit_context *context = task->audit_context;
2136 if (context && context->in_syscall) {
2137 struct audit_buffer *ab;
2139 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2141 audit_log_format(ab, "login pid=%d uid=%u "
2142 "old auid=%u new auid=%u"
2143 " old ses=%u new ses=%u",
2144 task->pid, task_uid(task),
2145 task->loginuid, loginuid,
2146 task->sessionid, sessionid);
2150 task->sessionid = sessionid;
2151 task->loginuid = loginuid;
2156 * __audit_mq_open - record audit data for a POSIX MQ open
2159 * @attr: queue attributes
2162 void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2164 struct audit_context *context = current->audit_context;
2167 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2169 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2171 context->mq_open.oflag = oflag;
2172 context->mq_open.mode = mode;
2174 context->type = AUDIT_MQ_OPEN;
2178 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2179 * @mqdes: MQ descriptor
2180 * @msg_len: Message length
2181 * @msg_prio: Message priority
2182 * @abs_timeout: Message timeout in absolute time
2185 void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2186 const struct timespec *abs_timeout)
2188 struct audit_context *context = current->audit_context;
2189 struct timespec *p = &context->mq_sendrecv.abs_timeout;
2192 memcpy(p, abs_timeout, sizeof(struct timespec));
2194 memset(p, 0, sizeof(struct timespec));
2196 context->mq_sendrecv.mqdes = mqdes;
2197 context->mq_sendrecv.msg_len = msg_len;
2198 context->mq_sendrecv.msg_prio = msg_prio;
2200 context->type = AUDIT_MQ_SENDRECV;
2204 * __audit_mq_notify - record audit data for a POSIX MQ notify
2205 * @mqdes: MQ descriptor
2206 * @notification: Notification event
2210 void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2212 struct audit_context *context = current->audit_context;
2215 context->mq_notify.sigev_signo = notification->sigev_signo;
2217 context->mq_notify.sigev_signo = 0;
2219 context->mq_notify.mqdes = mqdes;
2220 context->type = AUDIT_MQ_NOTIFY;
2224 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2225 * @mqdes: MQ descriptor
2229 void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2231 struct audit_context *context = current->audit_context;
2232 context->mq_getsetattr.mqdes = mqdes;
2233 context->mq_getsetattr.mqstat = *mqstat;
2234 context->type = AUDIT_MQ_GETSETATTR;
2238 * audit_ipc_obj - record audit data for ipc object
2239 * @ipcp: ipc permissions
2242 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2244 struct audit_context *context = current->audit_context;
2245 context->ipc.uid = ipcp->uid;
2246 context->ipc.gid = ipcp->gid;
2247 context->ipc.mode = ipcp->mode;
2248 context->ipc.has_perm = 0;
2249 security_ipc_getsecid(ipcp, &context->ipc.osid);
2250 context->type = AUDIT_IPC;
2254 * audit_ipc_set_perm - record audit data for new ipc permissions
2255 * @qbytes: msgq bytes
2256 * @uid: msgq user id
2257 * @gid: msgq group id
2258 * @mode: msgq mode (permissions)
2260 * Called only after audit_ipc_obj().
2262 void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2264 struct audit_context *context = current->audit_context;
2266 context->ipc.qbytes = qbytes;
2267 context->ipc.perm_uid = uid;
2268 context->ipc.perm_gid = gid;
2269 context->ipc.perm_mode = mode;
2270 context->ipc.has_perm = 1;
2273 int audit_bprm(struct linux_binprm *bprm)
2275 struct audit_aux_data_execve *ax;
2276 struct audit_context *context = current->audit_context;
2278 if (likely(!audit_enabled || !context || context->dummy))
2281 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2285 ax->argc = bprm->argc;
2286 ax->envc = bprm->envc;
2288 ax->d.type = AUDIT_EXECVE;
2289 ax->d.next = context->aux;
2290 context->aux = (void *)ax;
2296 * audit_socketcall - record audit data for sys_socketcall
2297 * @nargs: number of args
2301 void audit_socketcall(int nargs, unsigned long *args)
2303 struct audit_context *context = current->audit_context;
2305 if (likely(!context || context->dummy))
2308 context->type = AUDIT_SOCKETCALL;
2309 context->socketcall.nargs = nargs;
2310 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2314 * __audit_fd_pair - record audit data for pipe and socketpair
2315 * @fd1: the first file descriptor
2316 * @fd2: the second file descriptor
2319 void __audit_fd_pair(int fd1, int fd2)
2321 struct audit_context *context = current->audit_context;
2322 context->fds[0] = fd1;
2323 context->fds[1] = fd2;
2327 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2328 * @len: data length in user space
2329 * @a: data address in kernel space
2331 * Returns 0 for success or NULL context or < 0 on error.
2333 int audit_sockaddr(int len, void *a)
2335 struct audit_context *context = current->audit_context;
2337 if (likely(!context || context->dummy))
2340 if (!context->sockaddr) {
2341 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2344 context->sockaddr = p;
2347 context->sockaddr_len = len;
2348 memcpy(context->sockaddr, a, len);
2352 void __audit_ptrace(struct task_struct *t)
2354 struct audit_context *context = current->audit_context;
2356 context->target_pid = t->pid;
2357 context->target_auid = audit_get_loginuid(t);
2358 context->target_uid = task_uid(t);
2359 context->target_sessionid = audit_get_sessionid(t);
2360 security_task_getsecid(t, &context->target_sid);
2361 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2365 * audit_signal_info - record signal info for shutting down audit subsystem
2366 * @sig: signal value
2367 * @t: task being signaled
2369 * If the audit subsystem is being terminated, record the task (pid)
2370 * and uid that is doing that.
2372 int __audit_signal_info(int sig, struct task_struct *t)
2374 struct audit_aux_data_pids *axp;
2375 struct task_struct *tsk = current;
2376 struct audit_context *ctx = tsk->audit_context;
2377 uid_t uid = current_uid(), t_uid = task_uid(t);
2379 if (audit_pid && t->tgid == audit_pid) {
2380 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2381 audit_sig_pid = tsk->pid;
2382 if (tsk->loginuid != -1)
2383 audit_sig_uid = tsk->loginuid;
2385 audit_sig_uid = uid;
2386 security_task_getsecid(tsk, &audit_sig_sid);
2388 if (!audit_signals || audit_dummy_context())
2392 /* optimize the common case by putting first signal recipient directly
2393 * in audit_context */
2394 if (!ctx->target_pid) {
2395 ctx->target_pid = t->tgid;
2396 ctx->target_auid = audit_get_loginuid(t);
2397 ctx->target_uid = t_uid;
2398 ctx->target_sessionid = audit_get_sessionid(t);
2399 security_task_getsecid(t, &ctx->target_sid);
2400 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2404 axp = (void *)ctx->aux_pids;
2405 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2406 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2410 axp->d.type = AUDIT_OBJ_PID;
2411 axp->d.next = ctx->aux_pids;
2412 ctx->aux_pids = (void *)axp;
2414 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2416 axp->target_pid[axp->pid_count] = t->tgid;
2417 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2418 axp->target_uid[axp->pid_count] = t_uid;
2419 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2420 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2421 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2428 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2429 * @bprm: pointer to the bprm being processed
2430 * @new: the proposed new credentials
2431 * @old: the old credentials
2433 * Simply check if the proc already has the caps given by the file and if not
2434 * store the priv escalation info for later auditing at the end of the syscall
2438 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2439 const struct cred *new, const struct cred *old)
2441 struct audit_aux_data_bprm_fcaps *ax;
2442 struct audit_context *context = current->audit_context;
2443 struct cpu_vfs_cap_data vcaps;
2444 struct dentry *dentry;
2446 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2450 ax->d.type = AUDIT_BPRM_FCAPS;
2451 ax->d.next = context->aux;
2452 context->aux = (void *)ax;
2454 dentry = dget(bprm->file->f_dentry);
2455 get_vfs_caps_from_disk(dentry, &vcaps);
2458 ax->fcap.permitted = vcaps.permitted;
2459 ax->fcap.inheritable = vcaps.inheritable;
2460 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2461 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2463 ax->old_pcap.permitted = old->cap_permitted;
2464 ax->old_pcap.inheritable = old->cap_inheritable;
2465 ax->old_pcap.effective = old->cap_effective;
2467 ax->new_pcap.permitted = new->cap_permitted;
2468 ax->new_pcap.inheritable = new->cap_inheritable;
2469 ax->new_pcap.effective = new->cap_effective;
2474 * __audit_log_capset - store information about the arguments to the capset syscall
2475 * @pid: target pid of the capset call
2476 * @new: the new credentials
2477 * @old: the old (current) credentials
2479 * Record the aguments userspace sent to sys_capset for later printing by the
2480 * audit system if applicable
2482 void __audit_log_capset(pid_t pid,
2483 const struct cred *new, const struct cred *old)
2485 struct audit_context *context = current->audit_context;
2486 context->capset.pid = pid;
2487 context->capset.cap.effective = new->cap_effective;
2488 context->capset.cap.inheritable = new->cap_effective;
2489 context->capset.cap.permitted = new->cap_permitted;
2490 context->type = AUDIT_CAPSET;
2493 void __audit_mmap_fd(int fd, int flags)
2495 struct audit_context *context = current->audit_context;
2496 context->mmap.fd = fd;
2497 context->mmap.flags = flags;
2498 context->type = AUDIT_MMAP;
2502 * audit_core_dumps - record information about processes that end abnormally
2503 * @signr: signal value
2505 * If a process ends with a core dump, something fishy is going on and we
2506 * should record the event for investigation.
2508 void audit_core_dumps(long signr)
2510 struct audit_buffer *ab;
2512 uid_t auid = audit_get_loginuid(current), uid;
2514 unsigned int sessionid = audit_get_sessionid(current);
2519 if (signr == SIGQUIT) /* don't care for those */
2522 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2523 current_uid_gid(&uid, &gid);
2524 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2525 auid, uid, gid, sessionid);
2526 security_task_getsecid(current, &sid);
2531 if (security_secid_to_secctx(sid, &ctx, &len))
2532 audit_log_format(ab, " ssid=%u", sid);
2534 audit_log_format(ab, " subj=%s", ctx);
2535 security_release_secctx(ctx, len);
2538 audit_log_format(ab, " pid=%d comm=", current->pid);
2539 audit_log_untrustedstring(ab, current->comm);
2540 audit_log_format(ab, " sig=%ld", signr);
2544 struct list_head *audit_killed_trees(void)
2546 struct audit_context *ctx = current->audit_context;
2547 if (likely(!ctx || !ctx->in_syscall))
2549 return &ctx->killed_trees;