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/binfmts.h>
65 #include <linux/highmem.h>
66 #include <linux/syscalls.h>
67 #include <linux/inotify.h>
68 #include <linux/capability.h>
72 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
73 * for saving names from getname(). */
74 #define AUDIT_NAMES 20
76 /* Indicates that audit should log the full pathname. */
77 #define AUDIT_NAME_FULL -1
79 /* no execve audit message should be longer than this (userspace limits) */
80 #define MAX_EXECVE_AUDIT_LEN 7500
82 /* number of audit rules */
85 /* determines whether we collect data for signals sent */
88 struct audit_cap_data {
89 kernel_cap_t permitted;
90 kernel_cap_t inheritable;
92 unsigned int fE; /* effective bit of a file capability */
93 kernel_cap_t effective; /* effective set of a process */
97 /* When fs/namei.c:getname() is called, we store the pointer in name and
98 * we don't let putname() free it (instead we free all of the saved
99 * pointers at syscall exit time).
101 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
104 int name_len; /* number of name's characters to log */
105 unsigned name_put; /* call __putname() for this name */
113 struct audit_cap_data fcap;
114 unsigned int fcap_ver;
117 struct audit_aux_data {
118 struct audit_aux_data *next;
122 #define AUDIT_AUX_IPCPERM 0
124 /* Number of target pids per aux struct. */
125 #define AUDIT_AUX_PIDS 16
127 struct audit_aux_data_mq_open {
128 struct audit_aux_data d;
134 struct audit_aux_data_mq_sendrecv {
135 struct audit_aux_data d;
138 unsigned int msg_prio;
139 struct timespec abs_timeout;
142 struct audit_aux_data_mq_notify {
143 struct audit_aux_data d;
145 struct sigevent notification;
148 struct audit_aux_data_mq_getsetattr {
149 struct audit_aux_data d;
151 struct mq_attr mqstat;
154 struct audit_aux_data_ipcctl {
155 struct audit_aux_data d;
157 unsigned long qbytes;
164 struct audit_aux_data_execve {
165 struct audit_aux_data d;
168 struct mm_struct *mm;
171 struct audit_aux_data_socketcall {
172 struct audit_aux_data d;
174 unsigned long args[0];
177 struct audit_aux_data_sockaddr {
178 struct audit_aux_data d;
183 struct audit_aux_data_fd_pair {
184 struct audit_aux_data d;
188 struct audit_aux_data_pids {
189 struct audit_aux_data d;
190 pid_t target_pid[AUDIT_AUX_PIDS];
191 uid_t target_auid[AUDIT_AUX_PIDS];
192 uid_t target_uid[AUDIT_AUX_PIDS];
193 unsigned int target_sessionid[AUDIT_AUX_PIDS];
194 u32 target_sid[AUDIT_AUX_PIDS];
195 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
199 struct audit_aux_data_bprm_fcaps {
200 struct audit_aux_data d;
201 struct audit_cap_data fcap;
202 unsigned int fcap_ver;
203 struct audit_cap_data old_pcap;
204 struct audit_cap_data new_pcap;
207 struct audit_tree_refs {
208 struct audit_tree_refs *next;
209 struct audit_chunk *c[31];
212 /* The per-task audit context. */
213 struct audit_context {
214 int dummy; /* must be the first element */
215 int in_syscall; /* 1 if task is in a syscall */
216 enum audit_state state;
217 unsigned int serial; /* serial number for record */
218 struct timespec ctime; /* time of syscall entry */
219 int major; /* syscall number */
220 unsigned long argv[4]; /* syscall arguments */
221 int return_valid; /* return code is valid */
222 long return_code;/* syscall return code */
223 int auditable; /* 1 if record should be written */
225 struct audit_names names[AUDIT_NAMES];
226 char * filterkey; /* key for rule that triggered record */
228 struct audit_context *previous; /* For nested syscalls */
229 struct audit_aux_data *aux;
230 struct audit_aux_data *aux_pids;
232 /* Save things to print about task_struct */
234 uid_t uid, euid, suid, fsuid;
235 gid_t gid, egid, sgid, fsgid;
236 unsigned long personality;
242 unsigned int target_sessionid;
244 char target_comm[TASK_COMM_LEN];
246 struct audit_tree_refs *trees, *first_trees;
255 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
256 static inline int open_arg(int flags, int mask)
258 int n = ACC_MODE(flags);
259 if (flags & (O_TRUNC | O_CREAT))
260 n |= AUDIT_PERM_WRITE;
264 static int audit_match_perm(struct audit_context *ctx, int mask)
271 switch (audit_classify_syscall(ctx->arch, n)) {
273 if ((mask & AUDIT_PERM_WRITE) &&
274 audit_match_class(AUDIT_CLASS_WRITE, n))
276 if ((mask & AUDIT_PERM_READ) &&
277 audit_match_class(AUDIT_CLASS_READ, n))
279 if ((mask & AUDIT_PERM_ATTR) &&
280 audit_match_class(AUDIT_CLASS_CHATTR, n))
283 case 1: /* 32bit on biarch */
284 if ((mask & AUDIT_PERM_WRITE) &&
285 audit_match_class(AUDIT_CLASS_WRITE_32, n))
287 if ((mask & AUDIT_PERM_READ) &&
288 audit_match_class(AUDIT_CLASS_READ_32, n))
290 if ((mask & AUDIT_PERM_ATTR) &&
291 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
295 return mask & ACC_MODE(ctx->argv[1]);
297 return mask & ACC_MODE(ctx->argv[2]);
298 case 4: /* socketcall */
299 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
301 return mask & AUDIT_PERM_EXEC;
307 static int audit_match_filetype(struct audit_context *ctx, int which)
309 unsigned index = which & ~S_IFMT;
310 mode_t mode = which & S_IFMT;
315 if (index >= ctx->name_count)
317 if (ctx->names[index].ino == -1)
319 if ((ctx->names[index].mode ^ mode) & S_IFMT)
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 int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
337 struct audit_tree_refs *p = ctx->trees;
338 int left = ctx->tree_count;
340 p->c[--left] = chunk;
341 ctx->tree_count = left;
350 ctx->tree_count = 30;
356 static int grow_tree_refs(struct audit_context *ctx)
358 struct audit_tree_refs *p = ctx->trees;
359 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
365 p->next = ctx->trees;
367 ctx->first_trees = ctx->trees;
368 ctx->tree_count = 31;
373 static void unroll_tree_refs(struct audit_context *ctx,
374 struct audit_tree_refs *p, int count)
376 #ifdef CONFIG_AUDIT_TREE
377 struct audit_tree_refs *q;
380 /* we started with empty chain */
381 p = ctx->first_trees;
383 /* if the very first allocation has failed, nothing to do */
388 for (q = p; q != ctx->trees; q = q->next, n = 31) {
390 audit_put_chunk(q->c[n]);
394 while (n-- > ctx->tree_count) {
395 audit_put_chunk(q->c[n]);
399 ctx->tree_count = count;
403 static void free_tree_refs(struct audit_context *ctx)
405 struct audit_tree_refs *p, *q;
406 for (p = ctx->first_trees; p; p = q) {
412 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
414 #ifdef CONFIG_AUDIT_TREE
415 struct audit_tree_refs *p;
420 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
421 for (n = 0; n < 31; n++)
422 if (audit_tree_match(p->c[n], tree))
427 for (n = ctx->tree_count; n < 31; n++)
428 if (audit_tree_match(p->c[n], tree))
435 /* Determine if any context name data matches a rule's watch data */
436 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
438 static int audit_filter_rules(struct task_struct *tsk,
439 struct audit_krule *rule,
440 struct audit_context *ctx,
441 struct audit_names *name,
442 enum audit_state *state)
444 int i, j, need_sid = 1;
447 for (i = 0; i < rule->field_count; i++) {
448 struct audit_field *f = &rule->fields[i];
453 result = audit_comparator(tsk->pid, f->op, f->val);
458 ctx->ppid = sys_getppid();
459 result = audit_comparator(ctx->ppid, f->op, f->val);
463 result = audit_comparator(tsk->uid, f->op, f->val);
466 result = audit_comparator(tsk->euid, f->op, f->val);
469 result = audit_comparator(tsk->suid, f->op, f->val);
472 result = audit_comparator(tsk->fsuid, f->op, f->val);
475 result = audit_comparator(tsk->gid, f->op, f->val);
478 result = audit_comparator(tsk->egid, f->op, f->val);
481 result = audit_comparator(tsk->sgid, f->op, f->val);
484 result = audit_comparator(tsk->fsgid, f->op, f->val);
487 result = audit_comparator(tsk->personality, f->op, f->val);
491 result = audit_comparator(ctx->arch, f->op, f->val);
495 if (ctx && ctx->return_valid)
496 result = audit_comparator(ctx->return_code, f->op, f->val);
499 if (ctx && ctx->return_valid) {
501 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
503 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
508 result = audit_comparator(MAJOR(name->dev),
511 for (j = 0; j < ctx->name_count; j++) {
512 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
521 result = audit_comparator(MINOR(name->dev),
524 for (j = 0; j < ctx->name_count; j++) {
525 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
534 result = (name->ino == f->val);
536 for (j = 0; j < ctx->name_count; j++) {
537 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
545 if (name && rule->watch->ino != (unsigned long)-1)
546 result = (name->dev == rule->watch->dev &&
547 name->ino == rule->watch->ino);
551 result = match_tree_refs(ctx, rule->tree);
556 result = audit_comparator(tsk->loginuid, f->op, f->val);
558 case AUDIT_SUBJ_USER:
559 case AUDIT_SUBJ_ROLE:
560 case AUDIT_SUBJ_TYPE:
563 /* NOTE: this may return negative values indicating
564 a temporary error. We simply treat this as a
565 match for now to avoid losing information that
566 may be wanted. An error message will also be
570 security_task_getsecid(tsk, &sid);
573 result = security_audit_rule_match(sid, f->type,
582 case AUDIT_OBJ_LEV_LOW:
583 case AUDIT_OBJ_LEV_HIGH:
584 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
587 /* Find files that match */
589 result = security_audit_rule_match(
590 name->osid, f->type, f->op,
593 for (j = 0; j < ctx->name_count; j++) {
594 if (security_audit_rule_match(
603 /* Find ipc objects that match */
605 struct audit_aux_data *aux;
606 for (aux = ctx->aux; aux;
608 if (aux->type == AUDIT_IPC) {
609 struct audit_aux_data_ipcctl *axi = (void *)aux;
610 if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) {
624 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
626 case AUDIT_FILTERKEY:
627 /* ignore this field for filtering */
631 result = audit_match_perm(ctx, f->val);
634 result = audit_match_filetype(ctx, f->val);
641 if (rule->filterkey && ctx)
642 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
643 switch (rule->action) {
644 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
645 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
650 /* At process creation time, we can determine if system-call auditing is
651 * completely disabled for this task. Since we only have the task
652 * structure at this point, we can only check uid and gid.
654 static enum audit_state audit_filter_task(struct task_struct *tsk)
656 struct audit_entry *e;
657 enum audit_state state;
660 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
661 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
667 return AUDIT_BUILD_CONTEXT;
670 /* At syscall entry and exit time, this filter is called if the
671 * audit_state is not low enough that auditing cannot take place, but is
672 * also not high enough that we already know we have to write an audit
673 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
675 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
676 struct audit_context *ctx,
677 struct list_head *list)
679 struct audit_entry *e;
680 enum audit_state state;
682 if (audit_pid && tsk->tgid == audit_pid)
683 return AUDIT_DISABLED;
686 if (!list_empty(list)) {
687 int word = AUDIT_WORD(ctx->major);
688 int bit = AUDIT_BIT(ctx->major);
690 list_for_each_entry_rcu(e, list, list) {
691 if ((e->rule.mask[word] & bit) == bit &&
692 audit_filter_rules(tsk, &e->rule, ctx, NULL,
700 return AUDIT_BUILD_CONTEXT;
703 /* At syscall exit time, this filter is called if any audit_names[] have been
704 * collected during syscall processing. We only check rules in sublists at hash
705 * buckets applicable to the inode numbers in audit_names[].
706 * Regarding audit_state, same rules apply as for audit_filter_syscall().
708 enum audit_state audit_filter_inodes(struct task_struct *tsk,
709 struct audit_context *ctx)
712 struct audit_entry *e;
713 enum audit_state state;
715 if (audit_pid && tsk->tgid == audit_pid)
716 return AUDIT_DISABLED;
719 for (i = 0; i < ctx->name_count; i++) {
720 int word = AUDIT_WORD(ctx->major);
721 int bit = AUDIT_BIT(ctx->major);
722 struct audit_names *n = &ctx->names[i];
723 int h = audit_hash_ino((u32)n->ino);
724 struct list_head *list = &audit_inode_hash[h];
726 if (list_empty(list))
729 list_for_each_entry_rcu(e, list, list) {
730 if ((e->rule.mask[word] & bit) == bit &&
731 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
738 return AUDIT_BUILD_CONTEXT;
741 void audit_set_auditable(struct audit_context *ctx)
746 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
750 struct audit_context *context = tsk->audit_context;
752 if (likely(!context))
754 context->return_valid = return_valid;
757 * we need to fix up the return code in the audit logs if the actual
758 * return codes are later going to be fixed up by the arch specific
761 * This is actually a test for:
762 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
763 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
765 * but is faster than a bunch of ||
767 if (unlikely(return_code <= -ERESTARTSYS) &&
768 (return_code >= -ERESTART_RESTARTBLOCK) &&
769 (return_code != -ENOIOCTLCMD))
770 context->return_code = -EINTR;
772 context->return_code = return_code;
774 if (context->in_syscall && !context->dummy && !context->auditable) {
775 enum audit_state state;
777 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
778 if (state == AUDIT_RECORD_CONTEXT) {
779 context->auditable = 1;
783 state = audit_filter_inodes(tsk, context);
784 if (state == AUDIT_RECORD_CONTEXT)
785 context->auditable = 1;
791 tsk->audit_context = NULL;
795 static inline void audit_free_names(struct audit_context *context)
800 if (context->auditable
801 ||context->put_count + context->ino_count != context->name_count) {
802 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
803 " name_count=%d put_count=%d"
804 " ino_count=%d [NOT freeing]\n",
806 context->serial, context->major, context->in_syscall,
807 context->name_count, context->put_count,
809 for (i = 0; i < context->name_count; i++) {
810 printk(KERN_ERR "names[%d] = %p = %s\n", i,
811 context->names[i].name,
812 context->names[i].name ?: "(null)");
819 context->put_count = 0;
820 context->ino_count = 0;
823 for (i = 0; i < context->name_count; i++) {
824 if (context->names[i].name && context->names[i].name_put)
825 __putname(context->names[i].name);
827 context->name_count = 0;
828 path_put(&context->pwd);
829 context->pwd.dentry = NULL;
830 context->pwd.mnt = NULL;
833 static inline void audit_free_aux(struct audit_context *context)
835 struct audit_aux_data *aux;
837 while ((aux = context->aux)) {
838 context->aux = aux->next;
841 while ((aux = context->aux_pids)) {
842 context->aux_pids = aux->next;
847 static inline void audit_zero_context(struct audit_context *context,
848 enum audit_state state)
850 memset(context, 0, sizeof(*context));
851 context->state = state;
854 static inline struct audit_context *audit_alloc_context(enum audit_state state)
856 struct audit_context *context;
858 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
860 audit_zero_context(context, state);
865 * audit_alloc - allocate an audit context block for a task
868 * Filter on the task information and allocate a per-task audit context
869 * if necessary. Doing so turns on system call auditing for the
870 * specified task. This is called from copy_process, so no lock is
873 int audit_alloc(struct task_struct *tsk)
875 struct audit_context *context;
876 enum audit_state state;
878 if (likely(!audit_ever_enabled))
879 return 0; /* Return if not auditing. */
881 state = audit_filter_task(tsk);
882 if (likely(state == AUDIT_DISABLED))
885 if (!(context = audit_alloc_context(state))) {
886 audit_log_lost("out of memory in audit_alloc");
890 tsk->audit_context = context;
891 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
895 static inline void audit_free_context(struct audit_context *context)
897 struct audit_context *previous;
901 previous = context->previous;
902 if (previous || (count && count < 10)) {
904 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
905 " freeing multiple contexts (%d)\n",
906 context->serial, context->major,
907 context->name_count, count);
909 audit_free_names(context);
910 unroll_tree_refs(context, NULL, 0);
911 free_tree_refs(context);
912 audit_free_aux(context);
913 kfree(context->filterkey);
918 printk(KERN_ERR "audit: freed %d contexts\n", count);
921 void audit_log_task_context(struct audit_buffer *ab)
928 security_task_getsecid(current, &sid);
932 error = security_secid_to_secctx(sid, &ctx, &len);
934 if (error != -EINVAL)
939 audit_log_format(ab, " subj=%s", ctx);
940 security_release_secctx(ctx, len);
944 audit_panic("error in audit_log_task_context");
948 EXPORT_SYMBOL(audit_log_task_context);
950 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
952 char name[sizeof(tsk->comm)];
953 struct mm_struct *mm = tsk->mm;
954 struct vm_area_struct *vma;
958 get_task_comm(name, tsk);
959 audit_log_format(ab, " comm=");
960 audit_log_untrustedstring(ab, name);
963 down_read(&mm->mmap_sem);
966 if ((vma->vm_flags & VM_EXECUTABLE) &&
968 audit_log_d_path(ab, "exe=",
969 &vma->vm_file->f_path);
974 up_read(&mm->mmap_sem);
976 audit_log_task_context(ab);
979 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
980 uid_t auid, uid_t uid, unsigned int sessionid,
983 struct audit_buffer *ab;
988 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
992 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
994 if (security_secid_to_secctx(sid, &ctx, &len)) {
995 audit_log_format(ab, " obj=(none)");
998 audit_log_format(ab, " obj=%s", ctx);
999 security_release_secctx(ctx, len);
1001 audit_log_format(ab, " ocomm=");
1002 audit_log_untrustedstring(ab, comm);
1009 * to_send and len_sent accounting are very loose estimates. We aren't
1010 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1011 * within about 500 bytes (next page boundry)
1013 * why snprintf? an int is up to 12 digits long. if we just assumed when
1014 * logging that a[%d]= was going to be 16 characters long we would be wasting
1015 * space in every audit message. In one 7500 byte message we can log up to
1016 * about 1000 min size arguments. That comes down to about 50% waste of space
1017 * if we didn't do the snprintf to find out how long arg_num_len was.
1019 static int audit_log_single_execve_arg(struct audit_context *context,
1020 struct audit_buffer **ab,
1023 const char __user *p,
1026 char arg_num_len_buf[12];
1027 const char __user *tmp_p = p;
1028 /* how many digits are in arg_num? 3 is the length of a=\n */
1029 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
1030 size_t len, len_left, to_send;
1031 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1032 unsigned int i, has_cntl = 0, too_long = 0;
1035 /* strnlen_user includes the null we don't want to send */
1036 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1039 * We just created this mm, if we can't find the strings
1040 * we just copied into it something is _very_ wrong. Similar
1041 * for strings that are too long, we should not have created
1044 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
1046 send_sig(SIGKILL, current, 0);
1050 /* walk the whole argument looking for non-ascii chars */
1052 if (len_left > MAX_EXECVE_AUDIT_LEN)
1053 to_send = MAX_EXECVE_AUDIT_LEN;
1056 ret = copy_from_user(buf, tmp_p, to_send);
1058 * There is no reason for this copy to be short. We just
1059 * copied them here, and the mm hasn't been exposed to user-
1064 send_sig(SIGKILL, current, 0);
1067 buf[to_send] = '\0';
1068 has_cntl = audit_string_contains_control(buf, to_send);
1071 * hex messages get logged as 2 bytes, so we can only
1072 * send half as much in each message
1074 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1077 len_left -= to_send;
1079 } while (len_left > 0);
1083 if (len > max_execve_audit_len)
1086 /* rewalk the argument actually logging the message */
1087 for (i = 0; len_left > 0; i++) {
1090 if (len_left > max_execve_audit_len)
1091 to_send = max_execve_audit_len;
1095 /* do we have space left to send this argument in this ab? */
1096 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1098 room_left -= (to_send * 2);
1100 room_left -= to_send;
1101 if (room_left < 0) {
1104 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1110 * first record needs to say how long the original string was
1111 * so we can be sure nothing was lost.
1113 if ((i == 0) && (too_long))
1114 audit_log_format(*ab, "a%d_len=%zu ", arg_num,
1115 has_cntl ? 2*len : len);
1118 * normally arguments are small enough to fit and we already
1119 * filled buf above when we checked for control characters
1120 * so don't bother with another copy_from_user
1122 if (len >= max_execve_audit_len)
1123 ret = copy_from_user(buf, p, to_send);
1128 send_sig(SIGKILL, current, 0);
1131 buf[to_send] = '\0';
1133 /* actually log it */
1134 audit_log_format(*ab, "a%d", arg_num);
1136 audit_log_format(*ab, "[%d]", i);
1137 audit_log_format(*ab, "=");
1139 audit_log_n_hex(*ab, buf, to_send);
1141 audit_log_format(*ab, "\"%s\"", buf);
1142 audit_log_format(*ab, "\n");
1145 len_left -= to_send;
1146 *len_sent += arg_num_len;
1148 *len_sent += to_send * 2;
1150 *len_sent += to_send;
1152 /* include the null we didn't log */
1156 static void audit_log_execve_info(struct audit_context *context,
1157 struct audit_buffer **ab,
1158 struct audit_aux_data_execve *axi)
1161 size_t len, len_sent = 0;
1162 const char __user *p;
1165 if (axi->mm != current->mm)
1166 return; /* execve failed, no additional info */
1168 p = (const char __user *)axi->mm->arg_start;
1170 audit_log_format(*ab, "argc=%d ", axi->argc);
1173 * we need some kernel buffer to hold the userspace args. Just
1174 * allocate one big one rather than allocating one of the right size
1175 * for every single argument inside audit_log_single_execve_arg()
1176 * should be <8k allocation so should be pretty safe.
1178 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1180 audit_panic("out of memory for argv string\n");
1184 for (i = 0; i < axi->argc; i++) {
1185 len = audit_log_single_execve_arg(context, ab, i,
1194 static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1198 audit_log_format(ab, " %s=", prefix);
1199 CAP_FOR_EACH_U32(i) {
1200 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1204 static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1206 kernel_cap_t *perm = &name->fcap.permitted;
1207 kernel_cap_t *inh = &name->fcap.inheritable;
1210 if (!cap_isclear(*perm)) {
1211 audit_log_cap(ab, "cap_fp", perm);
1214 if (!cap_isclear(*inh)) {
1215 audit_log_cap(ab, "cap_fi", inh);
1220 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1223 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1225 int i, call_panic = 0;
1226 struct audit_buffer *ab;
1227 struct audit_aux_data *aux;
1230 /* tsk == current */
1231 context->pid = tsk->pid;
1233 context->ppid = sys_getppid();
1234 context->uid = tsk->uid;
1235 context->gid = tsk->gid;
1236 context->euid = tsk->euid;
1237 context->suid = tsk->suid;
1238 context->fsuid = tsk->fsuid;
1239 context->egid = tsk->egid;
1240 context->sgid = tsk->sgid;
1241 context->fsgid = tsk->fsgid;
1242 context->personality = tsk->personality;
1244 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1246 return; /* audit_panic has been called */
1247 audit_log_format(ab, "arch=%x syscall=%d",
1248 context->arch, context->major);
1249 if (context->personality != PER_LINUX)
1250 audit_log_format(ab, " per=%lx", context->personality);
1251 if (context->return_valid)
1252 audit_log_format(ab, " success=%s exit=%ld",
1253 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1254 context->return_code);
1256 spin_lock_irq(&tsk->sighand->siglock);
1257 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1258 tty = tsk->signal->tty->name;
1261 spin_unlock_irq(&tsk->sighand->siglock);
1263 audit_log_format(ab,
1264 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1265 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1266 " euid=%u suid=%u fsuid=%u"
1267 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1272 context->name_count,
1278 context->euid, context->suid, context->fsuid,
1279 context->egid, context->sgid, context->fsgid, tty,
1283 audit_log_task_info(ab, tsk);
1284 if (context->filterkey) {
1285 audit_log_format(ab, " key=");
1286 audit_log_untrustedstring(ab, context->filterkey);
1288 audit_log_format(ab, " key=(null)");
1291 for (aux = context->aux; aux; aux = aux->next) {
1293 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1295 continue; /* audit_panic has been called */
1297 switch (aux->type) {
1298 case AUDIT_MQ_OPEN: {
1299 struct audit_aux_data_mq_open *axi = (void *)aux;
1300 audit_log_format(ab,
1301 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1302 "mq_msgsize=%ld mq_curmsgs=%ld",
1303 axi->oflag, axi->mode, axi->attr.mq_flags,
1304 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1305 axi->attr.mq_curmsgs);
1308 case AUDIT_MQ_SENDRECV: {
1309 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1310 audit_log_format(ab,
1311 "mqdes=%d msg_len=%zd msg_prio=%u "
1312 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1313 axi->mqdes, axi->msg_len, axi->msg_prio,
1314 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1317 case AUDIT_MQ_NOTIFY: {
1318 struct audit_aux_data_mq_notify *axi = (void *)aux;
1319 audit_log_format(ab,
1320 "mqdes=%d sigev_signo=%d",
1322 axi->notification.sigev_signo);
1325 case AUDIT_MQ_GETSETATTR: {
1326 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1327 audit_log_format(ab,
1328 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1331 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1332 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1336 struct audit_aux_data_ipcctl *axi = (void *)aux;
1337 audit_log_format(ab,
1338 "ouid=%u ogid=%u mode=%#o",
1339 axi->uid, axi->gid, axi->mode);
1340 if (axi->osid != 0) {
1343 if (security_secid_to_secctx(
1344 axi->osid, &ctx, &len)) {
1345 audit_log_format(ab, " osid=%u",
1349 audit_log_format(ab, " obj=%s", ctx);
1350 security_release_secctx(ctx, len);
1355 case AUDIT_IPC_SET_PERM: {
1356 struct audit_aux_data_ipcctl *axi = (void *)aux;
1357 audit_log_format(ab,
1358 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1359 axi->qbytes, axi->uid, axi->gid, axi->mode);
1362 case AUDIT_EXECVE: {
1363 struct audit_aux_data_execve *axi = (void *)aux;
1364 audit_log_execve_info(context, &ab, axi);
1367 case AUDIT_SOCKETCALL: {
1368 struct audit_aux_data_socketcall *axs = (void *)aux;
1369 audit_log_format(ab, "nargs=%d", axs->nargs);
1370 for (i=0; i<axs->nargs; i++)
1371 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1374 case AUDIT_SOCKADDR: {
1375 struct audit_aux_data_sockaddr *axs = (void *)aux;
1377 audit_log_format(ab, "saddr=");
1378 audit_log_n_hex(ab, axs->a, axs->len);
1381 case AUDIT_FD_PAIR: {
1382 struct audit_aux_data_fd_pair *axs = (void *)aux;
1383 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1386 case AUDIT_BPRM_FCAPS: {
1387 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1388 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1389 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1390 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1391 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1392 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1393 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1394 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1395 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1396 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1397 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1404 for (aux = context->aux_pids; aux; aux = aux->next) {
1405 struct audit_aux_data_pids *axs = (void *)aux;
1407 for (i = 0; i < axs->pid_count; i++)
1408 if (audit_log_pid_context(context, axs->target_pid[i],
1409 axs->target_auid[i],
1411 axs->target_sessionid[i],
1413 axs->target_comm[i]))
1417 if (context->target_pid &&
1418 audit_log_pid_context(context, context->target_pid,
1419 context->target_auid, context->target_uid,
1420 context->target_sessionid,
1421 context->target_sid, context->target_comm))
1424 if (context->pwd.dentry && context->pwd.mnt) {
1425 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1427 audit_log_d_path(ab, "cwd=", &context->pwd);
1431 for (i = 0; i < context->name_count; i++) {
1432 struct audit_names *n = &context->names[i];
1434 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1436 continue; /* audit_panic has been called */
1438 audit_log_format(ab, "item=%d", i);
1441 switch(n->name_len) {
1442 case AUDIT_NAME_FULL:
1443 /* log the full path */
1444 audit_log_format(ab, " name=");
1445 audit_log_untrustedstring(ab, n->name);
1448 /* name was specified as a relative path and the
1449 * directory component is the cwd */
1450 audit_log_d_path(ab, " name=", &context->pwd);
1453 /* log the name's directory component */
1454 audit_log_format(ab, " name=");
1455 audit_log_n_untrustedstring(ab, n->name,
1459 audit_log_format(ab, " name=(null)");
1461 if (n->ino != (unsigned long)-1) {
1462 audit_log_format(ab, " inode=%lu"
1463 " dev=%02x:%02x mode=%#o"
1464 " ouid=%u ogid=%u rdev=%02x:%02x",
1477 if (security_secid_to_secctx(
1478 n->osid, &ctx, &len)) {
1479 audit_log_format(ab, " osid=%u", n->osid);
1482 audit_log_format(ab, " obj=%s", ctx);
1483 security_release_secctx(ctx, len);
1487 audit_log_fcaps(ab, n);
1492 /* Send end of event record to help user space know we are finished */
1493 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1497 audit_panic("error converting sid to string");
1501 * audit_free - free a per-task audit context
1502 * @tsk: task whose audit context block to free
1504 * Called from copy_process and do_exit
1506 void audit_free(struct task_struct *tsk)
1508 struct audit_context *context;
1510 context = audit_get_context(tsk, 0, 0);
1511 if (likely(!context))
1514 /* Check for system calls that do not go through the exit
1515 * function (e.g., exit_group), then free context block.
1516 * We use GFP_ATOMIC here because we might be doing this
1517 * in the context of the idle thread */
1518 /* that can happen only if we are called from do_exit() */
1519 if (context->in_syscall && context->auditable)
1520 audit_log_exit(context, tsk);
1522 audit_free_context(context);
1526 * audit_syscall_entry - fill in an audit record at syscall entry
1527 * @tsk: task being audited
1528 * @arch: architecture type
1529 * @major: major syscall type (function)
1530 * @a1: additional syscall register 1
1531 * @a2: additional syscall register 2
1532 * @a3: additional syscall register 3
1533 * @a4: additional syscall register 4
1535 * Fill in audit context at syscall entry. This only happens if the
1536 * audit context was created when the task was created and the state or
1537 * filters demand the audit context be built. If the state from the
1538 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1539 * then the record will be written at syscall exit time (otherwise, it
1540 * will only be written if another part of the kernel requests that it
1543 void audit_syscall_entry(int arch, int major,
1544 unsigned long a1, unsigned long a2,
1545 unsigned long a3, unsigned long a4)
1547 struct task_struct *tsk = current;
1548 struct audit_context *context = tsk->audit_context;
1549 enum audit_state state;
1551 if (unlikely(!context))
1555 * This happens only on certain architectures that make system
1556 * calls in kernel_thread via the entry.S interface, instead of
1557 * with direct calls. (If you are porting to a new
1558 * architecture, hitting this condition can indicate that you
1559 * got the _exit/_leave calls backward in entry.S.)
1563 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1565 * This also happens with vm86 emulation in a non-nested manner
1566 * (entries without exits), so this case must be caught.
1568 if (context->in_syscall) {
1569 struct audit_context *newctx;
1573 "audit(:%d) pid=%d in syscall=%d;"
1574 " entering syscall=%d\n",
1575 context->serial, tsk->pid, context->major, major);
1577 newctx = audit_alloc_context(context->state);
1579 newctx->previous = context;
1581 tsk->audit_context = newctx;
1583 /* If we can't alloc a new context, the best we
1584 * can do is to leak memory (any pending putname
1585 * will be lost). The only other alternative is
1586 * to abandon auditing. */
1587 audit_zero_context(context, context->state);
1590 BUG_ON(context->in_syscall || context->name_count);
1595 context->arch = arch;
1596 context->major = major;
1597 context->argv[0] = a1;
1598 context->argv[1] = a2;
1599 context->argv[2] = a3;
1600 context->argv[3] = a4;
1602 state = context->state;
1603 context->dummy = !audit_n_rules;
1604 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1605 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1606 if (likely(state == AUDIT_DISABLED))
1609 context->serial = 0;
1610 context->ctime = CURRENT_TIME;
1611 context->in_syscall = 1;
1612 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1617 * audit_syscall_exit - deallocate audit context after a system call
1618 * @tsk: task being audited
1619 * @valid: success/failure flag
1620 * @return_code: syscall return value
1622 * Tear down after system call. If the audit context has been marked as
1623 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1624 * filtering, or because some other part of the kernel write an audit
1625 * message), then write out the syscall information. In call cases,
1626 * free the names stored from getname().
1628 void audit_syscall_exit(int valid, long return_code)
1630 struct task_struct *tsk = current;
1631 struct audit_context *context;
1633 context = audit_get_context(tsk, valid, return_code);
1635 if (likely(!context))
1638 if (context->in_syscall && context->auditable)
1639 audit_log_exit(context, tsk);
1641 context->in_syscall = 0;
1642 context->auditable = 0;
1644 if (context->previous) {
1645 struct audit_context *new_context = context->previous;
1646 context->previous = NULL;
1647 audit_free_context(context);
1648 tsk->audit_context = new_context;
1650 audit_free_names(context);
1651 unroll_tree_refs(context, NULL, 0);
1652 audit_free_aux(context);
1653 context->aux = NULL;
1654 context->aux_pids = NULL;
1655 context->target_pid = 0;
1656 context->target_sid = 0;
1657 kfree(context->filterkey);
1658 context->filterkey = NULL;
1659 tsk->audit_context = context;
1663 static inline void handle_one(const struct inode *inode)
1665 #ifdef CONFIG_AUDIT_TREE
1666 struct audit_context *context;
1667 struct audit_tree_refs *p;
1668 struct audit_chunk *chunk;
1670 if (likely(list_empty(&inode->inotify_watches)))
1672 context = current->audit_context;
1674 count = context->tree_count;
1676 chunk = audit_tree_lookup(inode);
1680 if (likely(put_tree_ref(context, chunk)))
1682 if (unlikely(!grow_tree_refs(context))) {
1683 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
1684 audit_set_auditable(context);
1685 audit_put_chunk(chunk);
1686 unroll_tree_refs(context, p, count);
1689 put_tree_ref(context, chunk);
1693 static void handle_path(const struct dentry *dentry)
1695 #ifdef CONFIG_AUDIT_TREE
1696 struct audit_context *context;
1697 struct audit_tree_refs *p;
1698 const struct dentry *d, *parent;
1699 struct audit_chunk *drop;
1703 context = current->audit_context;
1705 count = context->tree_count;
1710 seq = read_seqbegin(&rename_lock);
1712 struct inode *inode = d->d_inode;
1713 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1714 struct audit_chunk *chunk;
1715 chunk = audit_tree_lookup(inode);
1717 if (unlikely(!put_tree_ref(context, chunk))) {
1723 parent = d->d_parent;
1728 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1731 /* just a race with rename */
1732 unroll_tree_refs(context, p, count);
1735 audit_put_chunk(drop);
1736 if (grow_tree_refs(context)) {
1737 /* OK, got more space */
1738 unroll_tree_refs(context, p, count);
1743 "out of memory, audit has lost a tree reference\n");
1744 unroll_tree_refs(context, p, count);
1745 audit_set_auditable(context);
1753 * audit_getname - add a name to the list
1754 * @name: name to add
1756 * Add a name to the list of audit names for this context.
1757 * Called from fs/namei.c:getname().
1759 void __audit_getname(const char *name)
1761 struct audit_context *context = current->audit_context;
1763 if (IS_ERR(name) || !name)
1766 if (!context->in_syscall) {
1767 #if AUDIT_DEBUG == 2
1768 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1769 __FILE__, __LINE__, context->serial, name);
1774 BUG_ON(context->name_count >= AUDIT_NAMES);
1775 context->names[context->name_count].name = name;
1776 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1777 context->names[context->name_count].name_put = 1;
1778 context->names[context->name_count].ino = (unsigned long)-1;
1779 context->names[context->name_count].osid = 0;
1780 ++context->name_count;
1781 if (!context->pwd.dentry) {
1782 read_lock(¤t->fs->lock);
1783 context->pwd = current->fs->pwd;
1784 path_get(¤t->fs->pwd);
1785 read_unlock(¤t->fs->lock);
1790 /* audit_putname - intercept a putname request
1791 * @name: name to intercept and delay for putname
1793 * If we have stored the name from getname in the audit context,
1794 * then we delay the putname until syscall exit.
1795 * Called from include/linux/fs.h:putname().
1797 void audit_putname(const char *name)
1799 struct audit_context *context = current->audit_context;
1802 if (!context->in_syscall) {
1803 #if AUDIT_DEBUG == 2
1804 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1805 __FILE__, __LINE__, context->serial, name);
1806 if (context->name_count) {
1808 for (i = 0; i < context->name_count; i++)
1809 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1810 context->names[i].name,
1811 context->names[i].name ?: "(null)");
1818 ++context->put_count;
1819 if (context->put_count > context->name_count) {
1820 printk(KERN_ERR "%s:%d(:%d): major=%d"
1821 " in_syscall=%d putname(%p) name_count=%d"
1824 context->serial, context->major,
1825 context->in_syscall, name, context->name_count,
1826 context->put_count);
1833 static int audit_inc_name_count(struct audit_context *context,
1834 const struct inode *inode)
1836 if (context->name_count >= AUDIT_NAMES) {
1838 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1839 "dev=%02x:%02x, inode=%lu\n",
1840 MAJOR(inode->i_sb->s_dev),
1841 MINOR(inode->i_sb->s_dev),
1845 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
1848 context->name_count++;
1850 context->ino_count++;
1856 static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1858 struct cpu_vfs_cap_data caps;
1861 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1862 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1869 rc = get_vfs_caps_from_disk(dentry, &caps);
1873 name->fcap.permitted = caps.permitted;
1874 name->fcap.inheritable = caps.inheritable;
1875 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1876 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1882 /* Copy inode data into an audit_names. */
1883 static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1884 const struct inode *inode)
1886 name->ino = inode->i_ino;
1887 name->dev = inode->i_sb->s_dev;
1888 name->mode = inode->i_mode;
1889 name->uid = inode->i_uid;
1890 name->gid = inode->i_gid;
1891 name->rdev = inode->i_rdev;
1892 security_inode_getsecid(inode, &name->osid);
1893 audit_copy_fcaps(name, dentry);
1897 * audit_inode - store the inode and device from a lookup
1898 * @name: name being audited
1899 * @dentry: dentry being audited
1901 * Called from fs/namei.c:path_lookup().
1903 void __audit_inode(const char *name, const struct dentry *dentry)
1906 struct audit_context *context = current->audit_context;
1907 const struct inode *inode = dentry->d_inode;
1909 if (!context->in_syscall)
1911 if (context->name_count
1912 && context->names[context->name_count-1].name
1913 && context->names[context->name_count-1].name == name)
1914 idx = context->name_count - 1;
1915 else if (context->name_count > 1
1916 && context->names[context->name_count-2].name
1917 && context->names[context->name_count-2].name == name)
1918 idx = context->name_count - 2;
1920 /* FIXME: how much do we care about inodes that have no
1921 * associated name? */
1922 if (audit_inc_name_count(context, inode))
1924 idx = context->name_count - 1;
1925 context->names[idx].name = NULL;
1927 handle_path(dentry);
1928 audit_copy_inode(&context->names[idx], dentry, inode);
1932 * audit_inode_child - collect inode info for created/removed objects
1933 * @dname: inode's dentry name
1934 * @dentry: dentry being audited
1935 * @parent: inode of dentry parent
1937 * For syscalls that create or remove filesystem objects, audit_inode
1938 * can only collect information for the filesystem object's parent.
1939 * This call updates the audit context with the child's information.
1940 * Syscalls that create a new filesystem object must be hooked after
1941 * the object is created. Syscalls that remove a filesystem object
1942 * must be hooked prior, in order to capture the target inode during
1943 * unsuccessful attempts.
1945 void __audit_inode_child(const char *dname, const struct dentry *dentry,
1946 const struct inode *parent)
1949 struct audit_context *context = current->audit_context;
1950 const char *found_parent = NULL, *found_child = NULL;
1951 const struct inode *inode = dentry->d_inode;
1954 if (!context->in_syscall)
1959 /* determine matching parent */
1963 /* parent is more likely, look for it first */
1964 for (idx = 0; idx < context->name_count; idx++) {
1965 struct audit_names *n = &context->names[idx];
1970 if (n->ino == parent->i_ino &&
1971 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1972 n->name_len = dirlen; /* update parent data in place */
1973 found_parent = n->name;
1978 /* no matching parent, look for matching child */
1979 for (idx = 0; idx < context->name_count; idx++) {
1980 struct audit_names *n = &context->names[idx];
1985 /* strcmp() is the more likely scenario */
1986 if (!strcmp(dname, n->name) ||
1987 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1989 audit_copy_inode(n, NULL, inode);
1991 n->ino = (unsigned long)-1;
1992 found_child = n->name;
1998 if (!found_parent) {
1999 if (audit_inc_name_count(context, parent))
2001 idx = context->name_count - 1;
2002 context->names[idx].name = NULL;
2003 audit_copy_inode(&context->names[idx], NULL, parent);
2007 if (audit_inc_name_count(context, inode))
2009 idx = context->name_count - 1;
2011 /* Re-use the name belonging to the slot for a matching parent
2012 * directory. All names for this context are relinquished in
2013 * audit_free_names() */
2015 context->names[idx].name = found_parent;
2016 context->names[idx].name_len = AUDIT_NAME_FULL;
2017 /* don't call __putname() */
2018 context->names[idx].name_put = 0;
2020 context->names[idx].name = NULL;
2024 audit_copy_inode(&context->names[idx], NULL, inode);
2026 context->names[idx].ino = (unsigned long)-1;
2029 EXPORT_SYMBOL_GPL(__audit_inode_child);
2032 * auditsc_get_stamp - get local copies of audit_context values
2033 * @ctx: audit_context for the task
2034 * @t: timespec to store time recorded in the audit_context
2035 * @serial: serial value that is recorded in the audit_context
2037 * Also sets the context as auditable.
2039 void auditsc_get_stamp(struct audit_context *ctx,
2040 struct timespec *t, unsigned int *serial)
2043 ctx->serial = audit_serial();
2044 t->tv_sec = ctx->ctime.tv_sec;
2045 t->tv_nsec = ctx->ctime.tv_nsec;
2046 *serial = ctx->serial;
2050 /* global counter which is incremented every time something logs in */
2051 static atomic_t session_id = ATOMIC_INIT(0);
2054 * audit_set_loginuid - set a task's audit_context loginuid
2055 * @task: task whose audit context is being modified
2056 * @loginuid: loginuid value
2060 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2062 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
2064 unsigned int sessionid = atomic_inc_return(&session_id);
2065 struct audit_context *context = task->audit_context;
2067 if (context && context->in_syscall) {
2068 struct audit_buffer *ab;
2070 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2072 audit_log_format(ab, "login pid=%d uid=%u "
2073 "old auid=%u new auid=%u"
2074 " old ses=%u new ses=%u",
2075 task->pid, task->uid,
2076 task->loginuid, loginuid,
2077 task->sessionid, sessionid);
2081 task->sessionid = sessionid;
2082 task->loginuid = loginuid;
2087 * __audit_mq_open - record audit data for a POSIX MQ open
2090 * @u_attr: queue attributes
2092 * Returns 0 for success or NULL context or < 0 on error.
2094 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
2096 struct audit_aux_data_mq_open *ax;
2097 struct audit_context *context = current->audit_context;
2102 if (likely(!context))
2105 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2109 if (u_attr != NULL) {
2110 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
2115 memset(&ax->attr, 0, sizeof(ax->attr));
2120 ax->d.type = AUDIT_MQ_OPEN;
2121 ax->d.next = context->aux;
2122 context->aux = (void *)ax;
2127 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
2128 * @mqdes: MQ descriptor
2129 * @msg_len: Message length
2130 * @msg_prio: Message priority
2131 * @u_abs_timeout: Message timeout in absolute time
2133 * Returns 0 for success or NULL context or < 0 on error.
2135 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2136 const struct timespec __user *u_abs_timeout)
2138 struct audit_aux_data_mq_sendrecv *ax;
2139 struct audit_context *context = current->audit_context;
2144 if (likely(!context))
2147 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2151 if (u_abs_timeout != NULL) {
2152 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2157 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2160 ax->msg_len = msg_len;
2161 ax->msg_prio = msg_prio;
2163 ax->d.type = AUDIT_MQ_SENDRECV;
2164 ax->d.next = context->aux;
2165 context->aux = (void *)ax;
2170 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
2171 * @mqdes: MQ descriptor
2172 * @msg_len: Message length
2173 * @u_msg_prio: Message priority
2174 * @u_abs_timeout: Message timeout in absolute time
2176 * Returns 0 for success or NULL context or < 0 on error.
2178 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
2179 unsigned int __user *u_msg_prio,
2180 const struct timespec __user *u_abs_timeout)
2182 struct audit_aux_data_mq_sendrecv *ax;
2183 struct audit_context *context = current->audit_context;
2188 if (likely(!context))
2191 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2195 if (u_msg_prio != NULL) {
2196 if (get_user(ax->msg_prio, u_msg_prio)) {
2203 if (u_abs_timeout != NULL) {
2204 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2209 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2212 ax->msg_len = msg_len;
2214 ax->d.type = AUDIT_MQ_SENDRECV;
2215 ax->d.next = context->aux;
2216 context->aux = (void *)ax;
2221 * __audit_mq_notify - record audit data for a POSIX MQ notify
2222 * @mqdes: MQ descriptor
2223 * @u_notification: Notification event
2225 * Returns 0 for success or NULL context or < 0 on error.
2228 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
2230 struct audit_aux_data_mq_notify *ax;
2231 struct audit_context *context = current->audit_context;
2236 if (likely(!context))
2239 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2243 if (u_notification != NULL) {
2244 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
2249 memset(&ax->notification, 0, sizeof(ax->notification));
2253 ax->d.type = AUDIT_MQ_NOTIFY;
2254 ax->d.next = context->aux;
2255 context->aux = (void *)ax;
2260 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2261 * @mqdes: MQ descriptor
2264 * Returns 0 for success or NULL context or < 0 on error.
2266 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2268 struct audit_aux_data_mq_getsetattr *ax;
2269 struct audit_context *context = current->audit_context;
2274 if (likely(!context))
2277 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2282 ax->mqstat = *mqstat;
2284 ax->d.type = AUDIT_MQ_GETSETATTR;
2285 ax->d.next = context->aux;
2286 context->aux = (void *)ax;
2291 * audit_ipc_obj - record audit data for ipc object
2292 * @ipcp: ipc permissions
2294 * Returns 0 for success or NULL context or < 0 on error.
2296 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2298 struct audit_aux_data_ipcctl *ax;
2299 struct audit_context *context = current->audit_context;
2301 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2305 ax->uid = ipcp->uid;
2306 ax->gid = ipcp->gid;
2307 ax->mode = ipcp->mode;
2308 security_ipc_getsecid(ipcp, &ax->osid);
2309 ax->d.type = AUDIT_IPC;
2310 ax->d.next = context->aux;
2311 context->aux = (void *)ax;
2316 * audit_ipc_set_perm - record audit data for new ipc permissions
2317 * @qbytes: msgq bytes
2318 * @uid: msgq user id
2319 * @gid: msgq group id
2320 * @mode: msgq mode (permissions)
2322 * Returns 0 for success or NULL context or < 0 on error.
2324 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2326 struct audit_aux_data_ipcctl *ax;
2327 struct audit_context *context = current->audit_context;
2329 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2333 ax->qbytes = qbytes;
2338 ax->d.type = AUDIT_IPC_SET_PERM;
2339 ax->d.next = context->aux;
2340 context->aux = (void *)ax;
2344 int audit_bprm(struct linux_binprm *bprm)
2346 struct audit_aux_data_execve *ax;
2347 struct audit_context *context = current->audit_context;
2349 if (likely(!audit_enabled || !context || context->dummy))
2352 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2356 ax->argc = bprm->argc;
2357 ax->envc = bprm->envc;
2359 ax->d.type = AUDIT_EXECVE;
2360 ax->d.next = context->aux;
2361 context->aux = (void *)ax;
2367 * audit_socketcall - record audit data for sys_socketcall
2368 * @nargs: number of args
2371 * Returns 0 for success or NULL context or < 0 on error.
2373 int audit_socketcall(int nargs, unsigned long *args)
2375 struct audit_aux_data_socketcall *ax;
2376 struct audit_context *context = current->audit_context;
2378 if (likely(!context || context->dummy))
2381 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2386 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2388 ax->d.type = AUDIT_SOCKETCALL;
2389 ax->d.next = context->aux;
2390 context->aux = (void *)ax;
2395 * __audit_fd_pair - record audit data for pipe and socketpair
2396 * @fd1: the first file descriptor
2397 * @fd2: the second file descriptor
2399 * Returns 0 for success or NULL context or < 0 on error.
2401 int __audit_fd_pair(int fd1, int fd2)
2403 struct audit_context *context = current->audit_context;
2404 struct audit_aux_data_fd_pair *ax;
2406 if (likely(!context)) {
2410 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2418 ax->d.type = AUDIT_FD_PAIR;
2419 ax->d.next = context->aux;
2420 context->aux = (void *)ax;
2425 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2426 * @len: data length in user space
2427 * @a: data address in kernel space
2429 * Returns 0 for success or NULL context or < 0 on error.
2431 int audit_sockaddr(int len, void *a)
2433 struct audit_aux_data_sockaddr *ax;
2434 struct audit_context *context = current->audit_context;
2436 if (likely(!context || context->dummy))
2439 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2444 memcpy(ax->a, a, len);
2446 ax->d.type = AUDIT_SOCKADDR;
2447 ax->d.next = context->aux;
2448 context->aux = (void *)ax;
2452 void __audit_ptrace(struct task_struct *t)
2454 struct audit_context *context = current->audit_context;
2456 context->target_pid = t->pid;
2457 context->target_auid = audit_get_loginuid(t);
2458 context->target_uid = t->uid;
2459 context->target_sessionid = audit_get_sessionid(t);
2460 security_task_getsecid(t, &context->target_sid);
2461 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2465 * audit_signal_info - record signal info for shutting down audit subsystem
2466 * @sig: signal value
2467 * @t: task being signaled
2469 * If the audit subsystem is being terminated, record the task (pid)
2470 * and uid that is doing that.
2472 int __audit_signal_info(int sig, struct task_struct *t)
2474 struct audit_aux_data_pids *axp;
2475 struct task_struct *tsk = current;
2476 struct audit_context *ctx = tsk->audit_context;
2478 if (audit_pid && t->tgid == audit_pid) {
2479 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2480 audit_sig_pid = tsk->pid;
2481 if (tsk->loginuid != -1)
2482 audit_sig_uid = tsk->loginuid;
2484 audit_sig_uid = tsk->uid;
2485 security_task_getsecid(tsk, &audit_sig_sid);
2487 if (!audit_signals || audit_dummy_context())
2491 /* optimize the common case by putting first signal recipient directly
2492 * in audit_context */
2493 if (!ctx->target_pid) {
2494 ctx->target_pid = t->tgid;
2495 ctx->target_auid = audit_get_loginuid(t);
2496 ctx->target_uid = t->uid;
2497 ctx->target_sessionid = audit_get_sessionid(t);
2498 security_task_getsecid(t, &ctx->target_sid);
2499 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2503 axp = (void *)ctx->aux_pids;
2504 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2505 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2509 axp->d.type = AUDIT_OBJ_PID;
2510 axp->d.next = ctx->aux_pids;
2511 ctx->aux_pids = (void *)axp;
2513 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2515 axp->target_pid[axp->pid_count] = t->tgid;
2516 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2517 axp->target_uid[axp->pid_count] = t->uid;
2518 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2519 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2520 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2527 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2528 * @bprm pointer to the bprm being processed
2529 * @caps the caps read from the disk
2531 * Simply check if the proc already has the caps given by the file and if not
2532 * store the priv escalation info for later auditing at the end of the syscall
2534 * this can fail and we don't care. See the note in audit.h for
2535 * audit_log_bprm_fcaps() for my explaination....
2539 void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_cap_t *pE)
2541 struct audit_aux_data_bprm_fcaps *ax;
2542 struct audit_context *context = current->audit_context;
2543 struct cpu_vfs_cap_data vcaps;
2544 struct dentry *dentry;
2546 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2550 ax->d.type = AUDIT_BPRM_FCAPS;
2551 ax->d.next = context->aux;
2552 context->aux = (void *)ax;
2554 dentry = dget(bprm->file->f_dentry);
2555 get_vfs_caps_from_disk(dentry, &vcaps);
2558 ax->fcap.permitted = vcaps.permitted;
2559 ax->fcap.inheritable = vcaps.inheritable;
2560 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2561 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2563 ax->old_pcap.permitted = *pP;
2564 ax->old_pcap.inheritable = current->cap_inheritable;
2565 ax->old_pcap.effective = *pE;
2567 ax->new_pcap.permitted = current->cap_permitted;
2568 ax->new_pcap.inheritable = current->cap_inheritable;
2569 ax->new_pcap.effective = current->cap_effective;
2573 * audit_core_dumps - record information about processes that end abnormally
2574 * @signr: signal value
2576 * If a process ends with a core dump, something fishy is going on and we
2577 * should record the event for investigation.
2579 void audit_core_dumps(long signr)
2581 struct audit_buffer *ab;
2583 uid_t auid = audit_get_loginuid(current);
2584 unsigned int sessionid = audit_get_sessionid(current);
2589 if (signr == SIGQUIT) /* don't care for those */
2592 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2593 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2594 auid, current->uid, current->gid, sessionid);
2595 security_task_getsecid(current, &sid);
2600 if (security_secid_to_secctx(sid, &ctx, &len))
2601 audit_log_format(ab, " ssid=%u", sid);
2603 audit_log_format(ab, " subj=%s", ctx);
2604 security_release_secctx(ctx, len);
2607 audit_log_format(ab, " pid=%d comm=", current->pid);
2608 audit_log_untrustedstring(ab, current->comm);
2609 audit_log_format(ab, " sig=%ld", signr);