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_fd_pair {
172 struct audit_aux_data d;
176 struct audit_aux_data_pids {
177 struct audit_aux_data d;
178 pid_t target_pid[AUDIT_AUX_PIDS];
179 uid_t target_auid[AUDIT_AUX_PIDS];
180 uid_t target_uid[AUDIT_AUX_PIDS];
181 unsigned int target_sessionid[AUDIT_AUX_PIDS];
182 u32 target_sid[AUDIT_AUX_PIDS];
183 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
187 struct audit_aux_data_bprm_fcaps {
188 struct audit_aux_data d;
189 struct audit_cap_data fcap;
190 unsigned int fcap_ver;
191 struct audit_cap_data old_pcap;
192 struct audit_cap_data new_pcap;
195 struct audit_aux_data_capset {
196 struct audit_aux_data d;
198 struct audit_cap_data cap;
201 struct audit_tree_refs {
202 struct audit_tree_refs *next;
203 struct audit_chunk *c[31];
206 /* The per-task audit context. */
207 struct audit_context {
208 int dummy; /* must be the first element */
209 int in_syscall; /* 1 if task is in a syscall */
210 enum audit_state state;
211 unsigned int serial; /* serial number for record */
212 struct timespec ctime; /* time of syscall entry */
213 int major; /* syscall number */
214 unsigned long argv[4]; /* syscall arguments */
215 int return_valid; /* return code is valid */
216 long return_code;/* syscall return code */
217 int auditable; /* 1 if record should be written */
219 struct audit_names names[AUDIT_NAMES];
220 char * filterkey; /* key for rule that triggered record */
222 struct audit_context *previous; /* For nested syscalls */
223 struct audit_aux_data *aux;
224 struct audit_aux_data *aux_pids;
225 struct sockaddr_storage *sockaddr;
227 /* Save things to print about task_struct */
229 uid_t uid, euid, suid, fsuid;
230 gid_t gid, egid, sgid, fsgid;
231 unsigned long personality;
237 unsigned int target_sessionid;
239 char target_comm[TASK_COMM_LEN];
241 struct audit_tree_refs *trees, *first_trees;
264 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
265 static inline int open_arg(int flags, int mask)
267 int n = ACC_MODE(flags);
268 if (flags & (O_TRUNC | O_CREAT))
269 n |= AUDIT_PERM_WRITE;
273 static int audit_match_perm(struct audit_context *ctx, int mask)
280 switch (audit_classify_syscall(ctx->arch, n)) {
282 if ((mask & AUDIT_PERM_WRITE) &&
283 audit_match_class(AUDIT_CLASS_WRITE, n))
285 if ((mask & AUDIT_PERM_READ) &&
286 audit_match_class(AUDIT_CLASS_READ, n))
288 if ((mask & AUDIT_PERM_ATTR) &&
289 audit_match_class(AUDIT_CLASS_CHATTR, n))
292 case 1: /* 32bit on biarch */
293 if ((mask & AUDIT_PERM_WRITE) &&
294 audit_match_class(AUDIT_CLASS_WRITE_32, n))
296 if ((mask & AUDIT_PERM_READ) &&
297 audit_match_class(AUDIT_CLASS_READ_32, n))
299 if ((mask & AUDIT_PERM_ATTR) &&
300 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
304 return mask & ACC_MODE(ctx->argv[1]);
306 return mask & ACC_MODE(ctx->argv[2]);
307 case 4: /* socketcall */
308 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
310 return mask & AUDIT_PERM_EXEC;
316 static int audit_match_filetype(struct audit_context *ctx, int which)
318 unsigned index = which & ~S_IFMT;
319 mode_t mode = which & S_IFMT;
324 if (index >= ctx->name_count)
326 if (ctx->names[index].ino == -1)
328 if ((ctx->names[index].mode ^ mode) & S_IFMT)
334 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
335 * ->first_trees points to its beginning, ->trees - to the current end of data.
336 * ->tree_count is the number of free entries in array pointed to by ->trees.
337 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
338 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
339 * it's going to remain 1-element for almost any setup) until we free context itself.
340 * References in it _are_ dropped - at the same time we free/drop aux stuff.
343 #ifdef CONFIG_AUDIT_TREE
344 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
346 struct audit_tree_refs *p = ctx->trees;
347 int left = ctx->tree_count;
349 p->c[--left] = chunk;
350 ctx->tree_count = left;
359 ctx->tree_count = 30;
365 static int grow_tree_refs(struct audit_context *ctx)
367 struct audit_tree_refs *p = ctx->trees;
368 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
374 p->next = ctx->trees;
376 ctx->first_trees = ctx->trees;
377 ctx->tree_count = 31;
382 static void unroll_tree_refs(struct audit_context *ctx,
383 struct audit_tree_refs *p, int count)
385 #ifdef CONFIG_AUDIT_TREE
386 struct audit_tree_refs *q;
389 /* we started with empty chain */
390 p = ctx->first_trees;
392 /* if the very first allocation has failed, nothing to do */
397 for (q = p; q != ctx->trees; q = q->next, n = 31) {
399 audit_put_chunk(q->c[n]);
403 while (n-- > ctx->tree_count) {
404 audit_put_chunk(q->c[n]);
408 ctx->tree_count = count;
412 static void free_tree_refs(struct audit_context *ctx)
414 struct audit_tree_refs *p, *q;
415 for (p = ctx->first_trees; p; p = q) {
421 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
423 #ifdef CONFIG_AUDIT_TREE
424 struct audit_tree_refs *p;
429 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
430 for (n = 0; n < 31; n++)
431 if (audit_tree_match(p->c[n], tree))
436 for (n = ctx->tree_count; n < 31; n++)
437 if (audit_tree_match(p->c[n], tree))
444 /* Determine if any context name data matches a rule's watch data */
445 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
447 static int audit_filter_rules(struct task_struct *tsk,
448 struct audit_krule *rule,
449 struct audit_context *ctx,
450 struct audit_names *name,
451 enum audit_state *state)
453 const struct cred *cred = get_task_cred(tsk);
454 int i, j, need_sid = 1;
457 for (i = 0; i < rule->field_count; i++) {
458 struct audit_field *f = &rule->fields[i];
463 result = audit_comparator(tsk->pid, f->op, f->val);
468 ctx->ppid = sys_getppid();
469 result = audit_comparator(ctx->ppid, f->op, f->val);
473 result = audit_comparator(cred->uid, f->op, f->val);
476 result = audit_comparator(cred->euid, f->op, f->val);
479 result = audit_comparator(cred->suid, f->op, f->val);
482 result = audit_comparator(cred->fsuid, f->op, f->val);
485 result = audit_comparator(cred->gid, f->op, f->val);
488 result = audit_comparator(cred->egid, f->op, f->val);
491 result = audit_comparator(cred->sgid, f->op, f->val);
494 result = audit_comparator(cred->fsgid, f->op, f->val);
497 result = audit_comparator(tsk->personality, f->op, f->val);
501 result = audit_comparator(ctx->arch, f->op, f->val);
505 if (ctx && ctx->return_valid)
506 result = audit_comparator(ctx->return_code, f->op, f->val);
509 if (ctx && ctx->return_valid) {
511 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
513 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
518 result = audit_comparator(MAJOR(name->dev),
521 for (j = 0; j < ctx->name_count; j++) {
522 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
531 result = audit_comparator(MINOR(name->dev),
534 for (j = 0; j < ctx->name_count; j++) {
535 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
544 result = (name->ino == f->val);
546 for (j = 0; j < ctx->name_count; j++) {
547 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
555 if (name && rule->watch->ino != (unsigned long)-1)
556 result = (name->dev == rule->watch->dev &&
557 name->ino == rule->watch->ino);
561 result = match_tree_refs(ctx, rule->tree);
566 result = audit_comparator(tsk->loginuid, f->op, f->val);
568 case AUDIT_SUBJ_USER:
569 case AUDIT_SUBJ_ROLE:
570 case AUDIT_SUBJ_TYPE:
573 /* NOTE: this may return negative values indicating
574 a temporary error. We simply treat this as a
575 match for now to avoid losing information that
576 may be wanted. An error message will also be
580 security_task_getsecid(tsk, &sid);
583 result = security_audit_rule_match(sid, f->type,
592 case AUDIT_OBJ_LEV_LOW:
593 case AUDIT_OBJ_LEV_HIGH:
594 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
597 /* Find files that match */
599 result = security_audit_rule_match(
600 name->osid, f->type, f->op,
603 for (j = 0; j < ctx->name_count; j++) {
604 if (security_audit_rule_match(
613 /* Find ipc objects that match */
614 if (!ctx || ctx->type != AUDIT_IPC)
616 if (security_audit_rule_match(ctx->ipc.osid,
627 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
629 case AUDIT_FILTERKEY:
630 /* ignore this field for filtering */
634 result = audit_match_perm(ctx, f->val);
637 result = audit_match_filetype(ctx, f->val);
646 if (rule->filterkey && ctx)
647 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
648 switch (rule->action) {
649 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
650 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
656 /* At process creation time, we can determine if system-call auditing is
657 * completely disabled for this task. Since we only have the task
658 * structure at this point, we can only check uid and gid.
660 static enum audit_state audit_filter_task(struct task_struct *tsk)
662 struct audit_entry *e;
663 enum audit_state state;
666 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
667 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
673 return AUDIT_BUILD_CONTEXT;
676 /* At syscall entry and exit time, this filter is called if the
677 * audit_state is not low enough that auditing cannot take place, but is
678 * also not high enough that we already know we have to write an audit
679 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
681 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
682 struct audit_context *ctx,
683 struct list_head *list)
685 struct audit_entry *e;
686 enum audit_state state;
688 if (audit_pid && tsk->tgid == audit_pid)
689 return AUDIT_DISABLED;
692 if (!list_empty(list)) {
693 int word = AUDIT_WORD(ctx->major);
694 int bit = AUDIT_BIT(ctx->major);
696 list_for_each_entry_rcu(e, list, list) {
697 if ((e->rule.mask[word] & bit) == bit &&
698 audit_filter_rules(tsk, &e->rule, ctx, NULL,
706 return AUDIT_BUILD_CONTEXT;
709 /* At syscall exit time, this filter is called if any audit_names[] have been
710 * collected during syscall processing. We only check rules in sublists at hash
711 * buckets applicable to the inode numbers in audit_names[].
712 * Regarding audit_state, same rules apply as for audit_filter_syscall().
714 enum audit_state audit_filter_inodes(struct task_struct *tsk,
715 struct audit_context *ctx)
718 struct audit_entry *e;
719 enum audit_state state;
721 if (audit_pid && tsk->tgid == audit_pid)
722 return AUDIT_DISABLED;
725 for (i = 0; i < ctx->name_count; i++) {
726 int word = AUDIT_WORD(ctx->major);
727 int bit = AUDIT_BIT(ctx->major);
728 struct audit_names *n = &ctx->names[i];
729 int h = audit_hash_ino((u32)n->ino);
730 struct list_head *list = &audit_inode_hash[h];
732 if (list_empty(list))
735 list_for_each_entry_rcu(e, list, list) {
736 if ((e->rule.mask[word] & bit) == bit &&
737 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
744 return AUDIT_BUILD_CONTEXT;
747 void audit_set_auditable(struct audit_context *ctx)
752 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
756 struct audit_context *context = tsk->audit_context;
758 if (likely(!context))
760 context->return_valid = return_valid;
763 * we need to fix up the return code in the audit logs if the actual
764 * return codes are later going to be fixed up by the arch specific
767 * This is actually a test for:
768 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
769 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
771 * but is faster than a bunch of ||
773 if (unlikely(return_code <= -ERESTARTSYS) &&
774 (return_code >= -ERESTART_RESTARTBLOCK) &&
775 (return_code != -ENOIOCTLCMD))
776 context->return_code = -EINTR;
778 context->return_code = return_code;
780 if (context->in_syscall && !context->dummy && !context->auditable) {
781 enum audit_state state;
783 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
784 if (state == AUDIT_RECORD_CONTEXT) {
785 context->auditable = 1;
789 state = audit_filter_inodes(tsk, context);
790 if (state == AUDIT_RECORD_CONTEXT)
791 context->auditable = 1;
797 tsk->audit_context = NULL;
801 static inline void audit_free_names(struct audit_context *context)
806 if (context->auditable
807 ||context->put_count + context->ino_count != context->name_count) {
808 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
809 " name_count=%d put_count=%d"
810 " ino_count=%d [NOT freeing]\n",
812 context->serial, context->major, context->in_syscall,
813 context->name_count, context->put_count,
815 for (i = 0; i < context->name_count; i++) {
816 printk(KERN_ERR "names[%d] = %p = %s\n", i,
817 context->names[i].name,
818 context->names[i].name ?: "(null)");
825 context->put_count = 0;
826 context->ino_count = 0;
829 for (i = 0; i < context->name_count; i++) {
830 if (context->names[i].name && context->names[i].name_put)
831 __putname(context->names[i].name);
833 context->name_count = 0;
834 path_put(&context->pwd);
835 context->pwd.dentry = NULL;
836 context->pwd.mnt = NULL;
839 static inline void audit_free_aux(struct audit_context *context)
841 struct audit_aux_data *aux;
843 while ((aux = context->aux)) {
844 context->aux = aux->next;
847 while ((aux = context->aux_pids)) {
848 context->aux_pids = aux->next;
853 static inline void audit_zero_context(struct audit_context *context,
854 enum audit_state state)
856 memset(context, 0, sizeof(*context));
857 context->state = state;
860 static inline struct audit_context *audit_alloc_context(enum audit_state state)
862 struct audit_context *context;
864 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
866 audit_zero_context(context, state);
871 * audit_alloc - allocate an audit context block for a task
874 * Filter on the task information and allocate a per-task audit context
875 * if necessary. Doing so turns on system call auditing for the
876 * specified task. This is called from copy_process, so no lock is
879 int audit_alloc(struct task_struct *tsk)
881 struct audit_context *context;
882 enum audit_state state;
884 if (likely(!audit_ever_enabled))
885 return 0; /* Return if not auditing. */
887 state = audit_filter_task(tsk);
888 if (likely(state == AUDIT_DISABLED))
891 if (!(context = audit_alloc_context(state))) {
892 audit_log_lost("out of memory in audit_alloc");
896 tsk->audit_context = context;
897 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
901 static inline void audit_free_context(struct audit_context *context)
903 struct audit_context *previous;
907 previous = context->previous;
908 if (previous || (count && count < 10)) {
910 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
911 " freeing multiple contexts (%d)\n",
912 context->serial, context->major,
913 context->name_count, count);
915 audit_free_names(context);
916 unroll_tree_refs(context, NULL, 0);
917 free_tree_refs(context);
918 audit_free_aux(context);
919 kfree(context->filterkey);
920 kfree(context->sockaddr);
925 printk(KERN_ERR "audit: freed %d contexts\n", count);
928 void audit_log_task_context(struct audit_buffer *ab)
935 security_task_getsecid(current, &sid);
939 error = security_secid_to_secctx(sid, &ctx, &len);
941 if (error != -EINVAL)
946 audit_log_format(ab, " subj=%s", ctx);
947 security_release_secctx(ctx, len);
951 audit_panic("error in audit_log_task_context");
955 EXPORT_SYMBOL(audit_log_task_context);
957 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
959 char name[sizeof(tsk->comm)];
960 struct mm_struct *mm = tsk->mm;
961 struct vm_area_struct *vma;
965 get_task_comm(name, tsk);
966 audit_log_format(ab, " comm=");
967 audit_log_untrustedstring(ab, name);
970 down_read(&mm->mmap_sem);
973 if ((vma->vm_flags & VM_EXECUTABLE) &&
975 audit_log_d_path(ab, "exe=",
976 &vma->vm_file->f_path);
981 up_read(&mm->mmap_sem);
983 audit_log_task_context(ab);
986 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
987 uid_t auid, uid_t uid, unsigned int sessionid,
990 struct audit_buffer *ab;
995 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
999 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
1001 if (security_secid_to_secctx(sid, &ctx, &len)) {
1002 audit_log_format(ab, " obj=(none)");
1005 audit_log_format(ab, " obj=%s", ctx);
1006 security_release_secctx(ctx, len);
1008 audit_log_format(ab, " ocomm=");
1009 audit_log_untrustedstring(ab, comm);
1016 * to_send and len_sent accounting are very loose estimates. We aren't
1017 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1018 * within about 500 bytes (next page boundry)
1020 * why snprintf? an int is up to 12 digits long. if we just assumed when
1021 * logging that a[%d]= was going to be 16 characters long we would be wasting
1022 * space in every audit message. In one 7500 byte message we can log up to
1023 * about 1000 min size arguments. That comes down to about 50% waste of space
1024 * if we didn't do the snprintf to find out how long arg_num_len was.
1026 static int audit_log_single_execve_arg(struct audit_context *context,
1027 struct audit_buffer **ab,
1030 const char __user *p,
1033 char arg_num_len_buf[12];
1034 const char __user *tmp_p = p;
1035 /* how many digits are in arg_num? 3 is the length of a=\n */
1036 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
1037 size_t len, len_left, to_send;
1038 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1039 unsigned int i, has_cntl = 0, too_long = 0;
1042 /* strnlen_user includes the null we don't want to send */
1043 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1046 * We just created this mm, if we can't find the strings
1047 * we just copied into it something is _very_ wrong. Similar
1048 * for strings that are too long, we should not have created
1051 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
1053 send_sig(SIGKILL, current, 0);
1057 /* walk the whole argument looking for non-ascii chars */
1059 if (len_left > MAX_EXECVE_AUDIT_LEN)
1060 to_send = MAX_EXECVE_AUDIT_LEN;
1063 ret = copy_from_user(buf, tmp_p, to_send);
1065 * There is no reason for this copy to be short. We just
1066 * copied them here, and the mm hasn't been exposed to user-
1071 send_sig(SIGKILL, current, 0);
1074 buf[to_send] = '\0';
1075 has_cntl = audit_string_contains_control(buf, to_send);
1078 * hex messages get logged as 2 bytes, so we can only
1079 * send half as much in each message
1081 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1084 len_left -= to_send;
1086 } while (len_left > 0);
1090 if (len > max_execve_audit_len)
1093 /* rewalk the argument actually logging the message */
1094 for (i = 0; len_left > 0; i++) {
1097 if (len_left > max_execve_audit_len)
1098 to_send = max_execve_audit_len;
1102 /* do we have space left to send this argument in this ab? */
1103 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1105 room_left -= (to_send * 2);
1107 room_left -= to_send;
1108 if (room_left < 0) {
1111 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1117 * first record needs to say how long the original string was
1118 * so we can be sure nothing was lost.
1120 if ((i == 0) && (too_long))
1121 audit_log_format(*ab, "a%d_len=%zu ", arg_num,
1122 has_cntl ? 2*len : len);
1125 * normally arguments are small enough to fit and we already
1126 * filled buf above when we checked for control characters
1127 * so don't bother with another copy_from_user
1129 if (len >= max_execve_audit_len)
1130 ret = copy_from_user(buf, p, to_send);
1135 send_sig(SIGKILL, current, 0);
1138 buf[to_send] = '\0';
1140 /* actually log it */
1141 audit_log_format(*ab, "a%d", arg_num);
1143 audit_log_format(*ab, "[%d]", i);
1144 audit_log_format(*ab, "=");
1146 audit_log_n_hex(*ab, buf, to_send);
1148 audit_log_format(*ab, "\"%s\"", buf);
1149 audit_log_format(*ab, "\n");
1152 len_left -= to_send;
1153 *len_sent += arg_num_len;
1155 *len_sent += to_send * 2;
1157 *len_sent += to_send;
1159 /* include the null we didn't log */
1163 static void audit_log_execve_info(struct audit_context *context,
1164 struct audit_buffer **ab,
1165 struct audit_aux_data_execve *axi)
1168 size_t len, len_sent = 0;
1169 const char __user *p;
1172 if (axi->mm != current->mm)
1173 return; /* execve failed, no additional info */
1175 p = (const char __user *)axi->mm->arg_start;
1177 audit_log_format(*ab, "argc=%d ", axi->argc);
1180 * we need some kernel buffer to hold the userspace args. Just
1181 * allocate one big one rather than allocating one of the right size
1182 * for every single argument inside audit_log_single_execve_arg()
1183 * should be <8k allocation so should be pretty safe.
1185 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1187 audit_panic("out of memory for argv string\n");
1191 for (i = 0; i < axi->argc; i++) {
1192 len = audit_log_single_execve_arg(context, ab, i,
1201 static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1205 audit_log_format(ab, " %s=", prefix);
1206 CAP_FOR_EACH_U32(i) {
1207 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1211 static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1213 kernel_cap_t *perm = &name->fcap.permitted;
1214 kernel_cap_t *inh = &name->fcap.inheritable;
1217 if (!cap_isclear(*perm)) {
1218 audit_log_cap(ab, "cap_fp", perm);
1221 if (!cap_isclear(*inh)) {
1222 audit_log_cap(ab, "cap_fi", inh);
1227 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1230 static void show_special(struct audit_context *context, int *call_panic)
1232 struct audit_buffer *ab;
1235 ab = audit_log_start(context, GFP_KERNEL, context->type);
1239 switch (context->type) {
1240 case AUDIT_SOCKETCALL: {
1241 int nargs = context->socketcall.nargs;
1242 audit_log_format(ab, "nargs=%d", nargs);
1243 for (i = 0; i < nargs; i++)
1244 audit_log_format(ab, " a%d=%lx", i,
1245 context->socketcall.args[i]);
1248 u32 osid = context->ipc.osid;
1250 audit_log_format(ab, "ouid=%u ogid=%u mode=%#o",
1251 context->ipc.uid, context->ipc.gid, context->ipc.mode);
1255 if (security_secid_to_secctx(osid, &ctx, &len)) {
1256 audit_log_format(ab, " osid=%u", osid);
1259 audit_log_format(ab, " obj=%s", ctx);
1260 security_release_secctx(ctx, len);
1268 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1270 const struct cred *cred;
1271 int i, call_panic = 0;
1272 struct audit_buffer *ab;
1273 struct audit_aux_data *aux;
1276 /* tsk == current */
1277 context->pid = tsk->pid;
1279 context->ppid = sys_getppid();
1280 cred = current_cred();
1281 context->uid = cred->uid;
1282 context->gid = cred->gid;
1283 context->euid = cred->euid;
1284 context->suid = cred->suid;
1285 context->fsuid = cred->fsuid;
1286 context->egid = cred->egid;
1287 context->sgid = cred->sgid;
1288 context->fsgid = cred->fsgid;
1289 context->personality = tsk->personality;
1291 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1293 return; /* audit_panic has been called */
1294 audit_log_format(ab, "arch=%x syscall=%d",
1295 context->arch, context->major);
1296 if (context->personality != PER_LINUX)
1297 audit_log_format(ab, " per=%lx", context->personality);
1298 if (context->return_valid)
1299 audit_log_format(ab, " success=%s exit=%ld",
1300 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1301 context->return_code);
1303 spin_lock_irq(&tsk->sighand->siglock);
1304 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1305 tty = tsk->signal->tty->name;
1308 spin_unlock_irq(&tsk->sighand->siglock);
1310 audit_log_format(ab,
1311 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1312 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1313 " euid=%u suid=%u fsuid=%u"
1314 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1319 context->name_count,
1325 context->euid, context->suid, context->fsuid,
1326 context->egid, context->sgid, context->fsgid, tty,
1330 audit_log_task_info(ab, tsk);
1331 if (context->filterkey) {
1332 audit_log_format(ab, " key=");
1333 audit_log_untrustedstring(ab, context->filterkey);
1335 audit_log_format(ab, " key=(null)");
1338 for (aux = context->aux; aux; aux = aux->next) {
1340 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1342 continue; /* audit_panic has been called */
1344 switch (aux->type) {
1345 case AUDIT_MQ_OPEN: {
1346 struct audit_aux_data_mq_open *axi = (void *)aux;
1347 audit_log_format(ab,
1348 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1349 "mq_msgsize=%ld mq_curmsgs=%ld",
1350 axi->oflag, axi->mode, axi->attr.mq_flags,
1351 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1352 axi->attr.mq_curmsgs);
1355 case AUDIT_MQ_SENDRECV: {
1356 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1357 audit_log_format(ab,
1358 "mqdes=%d msg_len=%zd msg_prio=%u "
1359 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1360 axi->mqdes, axi->msg_len, axi->msg_prio,
1361 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1364 case AUDIT_MQ_NOTIFY: {
1365 struct audit_aux_data_mq_notify *axi = (void *)aux;
1366 audit_log_format(ab,
1367 "mqdes=%d sigev_signo=%d",
1369 axi->notification.sigev_signo);
1372 case AUDIT_MQ_GETSETATTR: {
1373 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1374 audit_log_format(ab,
1375 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1378 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1379 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1382 case AUDIT_IPC_SET_PERM: {
1383 struct audit_aux_data_ipcctl *axi = (void *)aux;
1384 audit_log_format(ab,
1385 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1386 axi->qbytes, axi->uid, axi->gid, axi->mode);
1389 case AUDIT_EXECVE: {
1390 struct audit_aux_data_execve *axi = (void *)aux;
1391 audit_log_execve_info(context, &ab, axi);
1394 case AUDIT_FD_PAIR: {
1395 struct audit_aux_data_fd_pair *axs = (void *)aux;
1396 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1399 case AUDIT_BPRM_FCAPS: {
1400 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1401 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1402 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1403 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1404 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1405 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1406 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1407 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1408 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1409 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1410 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1413 case AUDIT_CAPSET: {
1414 struct audit_aux_data_capset *axs = (void *)aux;
1415 audit_log_format(ab, "pid=%d", axs->pid);
1416 audit_log_cap(ab, "cap_pi", &axs->cap.inheritable);
1417 audit_log_cap(ab, "cap_pp", &axs->cap.permitted);
1418 audit_log_cap(ab, "cap_pe", &axs->cap.effective);
1426 show_special(context, &call_panic);
1428 if (context->sockaddr_len) {
1429 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1431 audit_log_format(ab, "saddr=");
1432 audit_log_n_hex(ab, (void *)context->sockaddr,
1433 context->sockaddr_len);
1438 for (aux = context->aux_pids; aux; aux = aux->next) {
1439 struct audit_aux_data_pids *axs = (void *)aux;
1441 for (i = 0; i < axs->pid_count; i++)
1442 if (audit_log_pid_context(context, axs->target_pid[i],
1443 axs->target_auid[i],
1445 axs->target_sessionid[i],
1447 axs->target_comm[i]))
1451 if (context->target_pid &&
1452 audit_log_pid_context(context, context->target_pid,
1453 context->target_auid, context->target_uid,
1454 context->target_sessionid,
1455 context->target_sid, context->target_comm))
1458 if (context->pwd.dentry && context->pwd.mnt) {
1459 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1461 audit_log_d_path(ab, "cwd=", &context->pwd);
1465 for (i = 0; i < context->name_count; i++) {
1466 struct audit_names *n = &context->names[i];
1468 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1470 continue; /* audit_panic has been called */
1472 audit_log_format(ab, "item=%d", i);
1475 switch(n->name_len) {
1476 case AUDIT_NAME_FULL:
1477 /* log the full path */
1478 audit_log_format(ab, " name=");
1479 audit_log_untrustedstring(ab, n->name);
1482 /* name was specified as a relative path and the
1483 * directory component is the cwd */
1484 audit_log_d_path(ab, " name=", &context->pwd);
1487 /* log the name's directory component */
1488 audit_log_format(ab, " name=");
1489 audit_log_n_untrustedstring(ab, n->name,
1493 audit_log_format(ab, " name=(null)");
1495 if (n->ino != (unsigned long)-1) {
1496 audit_log_format(ab, " inode=%lu"
1497 " dev=%02x:%02x mode=%#o"
1498 " ouid=%u ogid=%u rdev=%02x:%02x",
1511 if (security_secid_to_secctx(
1512 n->osid, &ctx, &len)) {
1513 audit_log_format(ab, " osid=%u", n->osid);
1516 audit_log_format(ab, " obj=%s", ctx);
1517 security_release_secctx(ctx, len);
1521 audit_log_fcaps(ab, n);
1526 /* Send end of event record to help user space know we are finished */
1527 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1531 audit_panic("error converting sid to string");
1535 * audit_free - free a per-task audit context
1536 * @tsk: task whose audit context block to free
1538 * Called from copy_process and do_exit
1540 void audit_free(struct task_struct *tsk)
1542 struct audit_context *context;
1544 context = audit_get_context(tsk, 0, 0);
1545 if (likely(!context))
1548 /* Check for system calls that do not go through the exit
1549 * function (e.g., exit_group), then free context block.
1550 * We use GFP_ATOMIC here because we might be doing this
1551 * in the context of the idle thread */
1552 /* that can happen only if we are called from do_exit() */
1553 if (context->in_syscall && context->auditable)
1554 audit_log_exit(context, tsk);
1556 audit_free_context(context);
1560 * audit_syscall_entry - fill in an audit record at syscall entry
1561 * @arch: architecture type
1562 * @major: major syscall type (function)
1563 * @a1: additional syscall register 1
1564 * @a2: additional syscall register 2
1565 * @a3: additional syscall register 3
1566 * @a4: additional syscall register 4
1568 * Fill in audit context at syscall entry. This only happens if the
1569 * audit context was created when the task was created and the state or
1570 * filters demand the audit context be built. If the state from the
1571 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1572 * then the record will be written at syscall exit time (otherwise, it
1573 * will only be written if another part of the kernel requests that it
1576 void audit_syscall_entry(int arch, int major,
1577 unsigned long a1, unsigned long a2,
1578 unsigned long a3, unsigned long a4)
1580 struct task_struct *tsk = current;
1581 struct audit_context *context = tsk->audit_context;
1582 enum audit_state state;
1584 if (unlikely(!context))
1588 * This happens only on certain architectures that make system
1589 * calls in kernel_thread via the entry.S interface, instead of
1590 * with direct calls. (If you are porting to a new
1591 * architecture, hitting this condition can indicate that you
1592 * got the _exit/_leave calls backward in entry.S.)
1596 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1598 * This also happens with vm86 emulation in a non-nested manner
1599 * (entries without exits), so this case must be caught.
1601 if (context->in_syscall) {
1602 struct audit_context *newctx;
1606 "audit(:%d) pid=%d in syscall=%d;"
1607 " entering syscall=%d\n",
1608 context->serial, tsk->pid, context->major, major);
1610 newctx = audit_alloc_context(context->state);
1612 newctx->previous = context;
1614 tsk->audit_context = newctx;
1616 /* If we can't alloc a new context, the best we
1617 * can do is to leak memory (any pending putname
1618 * will be lost). The only other alternative is
1619 * to abandon auditing. */
1620 audit_zero_context(context, context->state);
1623 BUG_ON(context->in_syscall || context->name_count);
1628 context->arch = arch;
1629 context->major = major;
1630 context->argv[0] = a1;
1631 context->argv[1] = a2;
1632 context->argv[2] = a3;
1633 context->argv[3] = a4;
1635 state = context->state;
1636 context->dummy = !audit_n_rules;
1637 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1638 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1639 if (likely(state == AUDIT_DISABLED))
1642 context->serial = 0;
1643 context->ctime = CURRENT_TIME;
1644 context->in_syscall = 1;
1645 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1649 void audit_finish_fork(struct task_struct *child)
1651 struct audit_context *ctx = current->audit_context;
1652 struct audit_context *p = child->audit_context;
1653 if (!p || !ctx || !ctx->auditable)
1655 p->arch = ctx->arch;
1656 p->major = ctx->major;
1657 memcpy(p->argv, ctx->argv, sizeof(ctx->argv));
1658 p->ctime = ctx->ctime;
1659 p->dummy = ctx->dummy;
1660 p->auditable = ctx->auditable;
1661 p->in_syscall = ctx->in_syscall;
1662 p->filterkey = kstrdup(ctx->filterkey, GFP_KERNEL);
1663 p->ppid = current->pid;
1667 * audit_syscall_exit - deallocate audit context after a system call
1668 * @valid: success/failure flag
1669 * @return_code: syscall return value
1671 * Tear down after system call. If the audit context has been marked as
1672 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1673 * filtering, or because some other part of the kernel write an audit
1674 * message), then write out the syscall information. In call cases,
1675 * free the names stored from getname().
1677 void audit_syscall_exit(int valid, long return_code)
1679 struct task_struct *tsk = current;
1680 struct audit_context *context;
1682 context = audit_get_context(tsk, valid, return_code);
1684 if (likely(!context))
1687 if (context->in_syscall && context->auditable)
1688 audit_log_exit(context, tsk);
1690 context->in_syscall = 0;
1691 context->auditable = 0;
1693 if (context->previous) {
1694 struct audit_context *new_context = context->previous;
1695 context->previous = NULL;
1696 audit_free_context(context);
1697 tsk->audit_context = new_context;
1699 audit_free_names(context);
1700 unroll_tree_refs(context, NULL, 0);
1701 audit_free_aux(context);
1702 context->aux = NULL;
1703 context->aux_pids = NULL;
1704 context->target_pid = 0;
1705 context->target_sid = 0;
1706 context->sockaddr_len = 0;
1708 kfree(context->filterkey);
1709 context->filterkey = NULL;
1710 tsk->audit_context = context;
1714 static inline void handle_one(const struct inode *inode)
1716 #ifdef CONFIG_AUDIT_TREE
1717 struct audit_context *context;
1718 struct audit_tree_refs *p;
1719 struct audit_chunk *chunk;
1721 if (likely(list_empty(&inode->inotify_watches)))
1723 context = current->audit_context;
1725 count = context->tree_count;
1727 chunk = audit_tree_lookup(inode);
1731 if (likely(put_tree_ref(context, chunk)))
1733 if (unlikely(!grow_tree_refs(context))) {
1734 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
1735 audit_set_auditable(context);
1736 audit_put_chunk(chunk);
1737 unroll_tree_refs(context, p, count);
1740 put_tree_ref(context, chunk);
1744 static void handle_path(const struct dentry *dentry)
1746 #ifdef CONFIG_AUDIT_TREE
1747 struct audit_context *context;
1748 struct audit_tree_refs *p;
1749 const struct dentry *d, *parent;
1750 struct audit_chunk *drop;
1754 context = current->audit_context;
1756 count = context->tree_count;
1761 seq = read_seqbegin(&rename_lock);
1763 struct inode *inode = d->d_inode;
1764 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1765 struct audit_chunk *chunk;
1766 chunk = audit_tree_lookup(inode);
1768 if (unlikely(!put_tree_ref(context, chunk))) {
1774 parent = d->d_parent;
1779 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1782 /* just a race with rename */
1783 unroll_tree_refs(context, p, count);
1786 audit_put_chunk(drop);
1787 if (grow_tree_refs(context)) {
1788 /* OK, got more space */
1789 unroll_tree_refs(context, p, count);
1794 "out of memory, audit has lost a tree reference\n");
1795 unroll_tree_refs(context, p, count);
1796 audit_set_auditable(context);
1804 * audit_getname - add a name to the list
1805 * @name: name to add
1807 * Add a name to the list of audit names for this context.
1808 * Called from fs/namei.c:getname().
1810 void __audit_getname(const char *name)
1812 struct audit_context *context = current->audit_context;
1814 if (IS_ERR(name) || !name)
1817 if (!context->in_syscall) {
1818 #if AUDIT_DEBUG == 2
1819 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1820 __FILE__, __LINE__, context->serial, name);
1825 BUG_ON(context->name_count >= AUDIT_NAMES);
1826 context->names[context->name_count].name = name;
1827 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1828 context->names[context->name_count].name_put = 1;
1829 context->names[context->name_count].ino = (unsigned long)-1;
1830 context->names[context->name_count].osid = 0;
1831 ++context->name_count;
1832 if (!context->pwd.dentry) {
1833 read_lock(¤t->fs->lock);
1834 context->pwd = current->fs->pwd;
1835 path_get(¤t->fs->pwd);
1836 read_unlock(¤t->fs->lock);
1841 /* audit_putname - intercept a putname request
1842 * @name: name to intercept and delay for putname
1844 * If we have stored the name from getname in the audit context,
1845 * then we delay the putname until syscall exit.
1846 * Called from include/linux/fs.h:putname().
1848 void audit_putname(const char *name)
1850 struct audit_context *context = current->audit_context;
1853 if (!context->in_syscall) {
1854 #if AUDIT_DEBUG == 2
1855 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1856 __FILE__, __LINE__, context->serial, name);
1857 if (context->name_count) {
1859 for (i = 0; i < context->name_count; i++)
1860 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1861 context->names[i].name,
1862 context->names[i].name ?: "(null)");
1869 ++context->put_count;
1870 if (context->put_count > context->name_count) {
1871 printk(KERN_ERR "%s:%d(:%d): major=%d"
1872 " in_syscall=%d putname(%p) name_count=%d"
1875 context->serial, context->major,
1876 context->in_syscall, name, context->name_count,
1877 context->put_count);
1884 static int audit_inc_name_count(struct audit_context *context,
1885 const struct inode *inode)
1887 if (context->name_count >= AUDIT_NAMES) {
1889 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1890 "dev=%02x:%02x, inode=%lu\n",
1891 MAJOR(inode->i_sb->s_dev),
1892 MINOR(inode->i_sb->s_dev),
1896 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
1899 context->name_count++;
1901 context->ino_count++;
1907 static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1909 struct cpu_vfs_cap_data caps;
1912 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1913 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1920 rc = get_vfs_caps_from_disk(dentry, &caps);
1924 name->fcap.permitted = caps.permitted;
1925 name->fcap.inheritable = caps.inheritable;
1926 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1927 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1933 /* Copy inode data into an audit_names. */
1934 static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1935 const struct inode *inode)
1937 name->ino = inode->i_ino;
1938 name->dev = inode->i_sb->s_dev;
1939 name->mode = inode->i_mode;
1940 name->uid = inode->i_uid;
1941 name->gid = inode->i_gid;
1942 name->rdev = inode->i_rdev;
1943 security_inode_getsecid(inode, &name->osid);
1944 audit_copy_fcaps(name, dentry);
1948 * audit_inode - store the inode and device from a lookup
1949 * @name: name being audited
1950 * @dentry: dentry being audited
1952 * Called from fs/namei.c:path_lookup().
1954 void __audit_inode(const char *name, const struct dentry *dentry)
1957 struct audit_context *context = current->audit_context;
1958 const struct inode *inode = dentry->d_inode;
1960 if (!context->in_syscall)
1962 if (context->name_count
1963 && context->names[context->name_count-1].name
1964 && context->names[context->name_count-1].name == name)
1965 idx = context->name_count - 1;
1966 else if (context->name_count > 1
1967 && context->names[context->name_count-2].name
1968 && context->names[context->name_count-2].name == name)
1969 idx = context->name_count - 2;
1971 /* FIXME: how much do we care about inodes that have no
1972 * associated name? */
1973 if (audit_inc_name_count(context, inode))
1975 idx = context->name_count - 1;
1976 context->names[idx].name = NULL;
1978 handle_path(dentry);
1979 audit_copy_inode(&context->names[idx], dentry, inode);
1983 * audit_inode_child - collect inode info for created/removed objects
1984 * @dname: inode's dentry name
1985 * @dentry: dentry being audited
1986 * @parent: inode of dentry parent
1988 * For syscalls that create or remove filesystem objects, audit_inode
1989 * can only collect information for the filesystem object's parent.
1990 * This call updates the audit context with the child's information.
1991 * Syscalls that create a new filesystem object must be hooked after
1992 * the object is created. Syscalls that remove a filesystem object
1993 * must be hooked prior, in order to capture the target inode during
1994 * unsuccessful attempts.
1996 void __audit_inode_child(const char *dname, const struct dentry *dentry,
1997 const struct inode *parent)
2000 struct audit_context *context = current->audit_context;
2001 const char *found_parent = NULL, *found_child = NULL;
2002 const struct inode *inode = dentry->d_inode;
2005 if (!context->in_syscall)
2010 /* determine matching parent */
2014 /* parent is more likely, look for it first */
2015 for (idx = 0; idx < context->name_count; idx++) {
2016 struct audit_names *n = &context->names[idx];
2021 if (n->ino == parent->i_ino &&
2022 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2023 n->name_len = dirlen; /* update parent data in place */
2024 found_parent = n->name;
2029 /* no matching parent, look for matching child */
2030 for (idx = 0; idx < context->name_count; idx++) {
2031 struct audit_names *n = &context->names[idx];
2036 /* strcmp() is the more likely scenario */
2037 if (!strcmp(dname, n->name) ||
2038 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2040 audit_copy_inode(n, NULL, inode);
2042 n->ino = (unsigned long)-1;
2043 found_child = n->name;
2049 if (!found_parent) {
2050 if (audit_inc_name_count(context, parent))
2052 idx = context->name_count - 1;
2053 context->names[idx].name = NULL;
2054 audit_copy_inode(&context->names[idx], NULL, parent);
2058 if (audit_inc_name_count(context, inode))
2060 idx = context->name_count - 1;
2062 /* Re-use the name belonging to the slot for a matching parent
2063 * directory. All names for this context are relinquished in
2064 * audit_free_names() */
2066 context->names[idx].name = found_parent;
2067 context->names[idx].name_len = AUDIT_NAME_FULL;
2068 /* don't call __putname() */
2069 context->names[idx].name_put = 0;
2071 context->names[idx].name = NULL;
2075 audit_copy_inode(&context->names[idx], NULL, inode);
2077 context->names[idx].ino = (unsigned long)-1;
2080 EXPORT_SYMBOL_GPL(__audit_inode_child);
2083 * auditsc_get_stamp - get local copies of audit_context values
2084 * @ctx: audit_context for the task
2085 * @t: timespec to store time recorded in the audit_context
2086 * @serial: serial value that is recorded in the audit_context
2088 * Also sets the context as auditable.
2090 int auditsc_get_stamp(struct audit_context *ctx,
2091 struct timespec *t, unsigned int *serial)
2093 if (!ctx->in_syscall)
2096 ctx->serial = audit_serial();
2097 t->tv_sec = ctx->ctime.tv_sec;
2098 t->tv_nsec = ctx->ctime.tv_nsec;
2099 *serial = ctx->serial;
2104 /* global counter which is incremented every time something logs in */
2105 static atomic_t session_id = ATOMIC_INIT(0);
2108 * audit_set_loginuid - set a task's audit_context loginuid
2109 * @task: task whose audit context is being modified
2110 * @loginuid: loginuid value
2114 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2116 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
2118 unsigned int sessionid = atomic_inc_return(&session_id);
2119 struct audit_context *context = task->audit_context;
2121 if (context && context->in_syscall) {
2122 struct audit_buffer *ab;
2124 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2126 audit_log_format(ab, "login pid=%d uid=%u "
2127 "old auid=%u new auid=%u"
2128 " old ses=%u new ses=%u",
2129 task->pid, task_uid(task),
2130 task->loginuid, loginuid,
2131 task->sessionid, sessionid);
2135 task->sessionid = sessionid;
2136 task->loginuid = loginuid;
2141 * __audit_mq_open - record audit data for a POSIX MQ open
2144 * @u_attr: queue attributes
2146 * Returns 0 for success or NULL context or < 0 on error.
2148 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
2150 struct audit_aux_data_mq_open *ax;
2151 struct audit_context *context = current->audit_context;
2156 if (likely(!context))
2159 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2163 if (u_attr != NULL) {
2164 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
2169 memset(&ax->attr, 0, sizeof(ax->attr));
2174 ax->d.type = AUDIT_MQ_OPEN;
2175 ax->d.next = context->aux;
2176 context->aux = (void *)ax;
2181 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
2182 * @mqdes: MQ descriptor
2183 * @msg_len: Message length
2184 * @msg_prio: Message priority
2185 * @u_abs_timeout: Message timeout in absolute time
2187 * Returns 0 for success or NULL context or < 0 on error.
2189 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2190 const struct timespec __user *u_abs_timeout)
2192 struct audit_aux_data_mq_sendrecv *ax;
2193 struct audit_context *context = current->audit_context;
2198 if (likely(!context))
2201 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2205 if (u_abs_timeout != NULL) {
2206 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2211 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2214 ax->msg_len = msg_len;
2215 ax->msg_prio = msg_prio;
2217 ax->d.type = AUDIT_MQ_SENDRECV;
2218 ax->d.next = context->aux;
2219 context->aux = (void *)ax;
2224 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
2225 * @mqdes: MQ descriptor
2226 * @msg_len: Message length
2227 * @u_msg_prio: Message priority
2228 * @u_abs_timeout: Message timeout in absolute time
2230 * Returns 0 for success or NULL context or < 0 on error.
2232 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
2233 unsigned int __user *u_msg_prio,
2234 const struct timespec __user *u_abs_timeout)
2236 struct audit_aux_data_mq_sendrecv *ax;
2237 struct audit_context *context = current->audit_context;
2242 if (likely(!context))
2245 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2249 if (u_msg_prio != NULL) {
2250 if (get_user(ax->msg_prio, u_msg_prio)) {
2257 if (u_abs_timeout != NULL) {
2258 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2263 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2266 ax->msg_len = msg_len;
2268 ax->d.type = AUDIT_MQ_SENDRECV;
2269 ax->d.next = context->aux;
2270 context->aux = (void *)ax;
2275 * __audit_mq_notify - record audit data for a POSIX MQ notify
2276 * @mqdes: MQ descriptor
2277 * @u_notification: Notification event
2279 * Returns 0 for success or NULL context or < 0 on error.
2282 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
2284 struct audit_aux_data_mq_notify *ax;
2285 struct audit_context *context = current->audit_context;
2290 if (likely(!context))
2293 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2297 if (u_notification != NULL) {
2298 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
2303 memset(&ax->notification, 0, sizeof(ax->notification));
2307 ax->d.type = AUDIT_MQ_NOTIFY;
2308 ax->d.next = context->aux;
2309 context->aux = (void *)ax;
2314 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2315 * @mqdes: MQ descriptor
2318 * Returns 0 for success or NULL context or < 0 on error.
2320 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2322 struct audit_aux_data_mq_getsetattr *ax;
2323 struct audit_context *context = current->audit_context;
2328 if (likely(!context))
2331 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2336 ax->mqstat = *mqstat;
2338 ax->d.type = AUDIT_MQ_GETSETATTR;
2339 ax->d.next = context->aux;
2340 context->aux = (void *)ax;
2345 * audit_ipc_obj - record audit data for ipc object
2346 * @ipcp: ipc permissions
2349 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2351 struct audit_context *context = current->audit_context;
2352 context->ipc.uid = ipcp->uid;
2353 context->ipc.gid = ipcp->gid;
2354 context->ipc.mode = ipcp->mode;
2355 security_ipc_getsecid(ipcp, &context->ipc.osid);
2356 context->type = AUDIT_IPC;
2360 * audit_ipc_set_perm - record audit data for new ipc permissions
2361 * @qbytes: msgq bytes
2362 * @uid: msgq user id
2363 * @gid: msgq group id
2364 * @mode: msgq mode (permissions)
2366 * Returns 0 for success or NULL context or < 0 on error.
2368 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2370 struct audit_aux_data_ipcctl *ax;
2371 struct audit_context *context = current->audit_context;
2373 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2377 ax->qbytes = qbytes;
2382 ax->d.type = AUDIT_IPC_SET_PERM;
2383 ax->d.next = context->aux;
2384 context->aux = (void *)ax;
2388 int audit_bprm(struct linux_binprm *bprm)
2390 struct audit_aux_data_execve *ax;
2391 struct audit_context *context = current->audit_context;
2393 if (likely(!audit_enabled || !context || context->dummy))
2396 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2400 ax->argc = bprm->argc;
2401 ax->envc = bprm->envc;
2403 ax->d.type = AUDIT_EXECVE;
2404 ax->d.next = context->aux;
2405 context->aux = (void *)ax;
2411 * audit_socketcall - record audit data for sys_socketcall
2412 * @nargs: number of args
2416 void audit_socketcall(int nargs, unsigned long *args)
2418 struct audit_context *context = current->audit_context;
2420 if (likely(!context || context->dummy))
2423 context->type = AUDIT_SOCKETCALL;
2424 context->socketcall.nargs = nargs;
2425 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2429 * __audit_fd_pair - record audit data for pipe and socketpair
2430 * @fd1: the first file descriptor
2431 * @fd2: the second file descriptor
2433 * Returns 0 for success or NULL context or < 0 on error.
2435 int __audit_fd_pair(int fd1, int fd2)
2437 struct audit_context *context = current->audit_context;
2438 struct audit_aux_data_fd_pair *ax;
2440 if (likely(!context)) {
2444 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2452 ax->d.type = AUDIT_FD_PAIR;
2453 ax->d.next = context->aux;
2454 context->aux = (void *)ax;
2459 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2460 * @len: data length in user space
2461 * @a: data address in kernel space
2463 * Returns 0 for success or NULL context or < 0 on error.
2465 int audit_sockaddr(int len, void *a)
2467 struct audit_context *context = current->audit_context;
2469 if (likely(!context || context->dummy))
2472 if (!context->sockaddr) {
2473 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2476 context->sockaddr = p;
2479 context->sockaddr_len = len;
2480 memcpy(context->sockaddr, a, len);
2484 void __audit_ptrace(struct task_struct *t)
2486 struct audit_context *context = current->audit_context;
2488 context->target_pid = t->pid;
2489 context->target_auid = audit_get_loginuid(t);
2490 context->target_uid = task_uid(t);
2491 context->target_sessionid = audit_get_sessionid(t);
2492 security_task_getsecid(t, &context->target_sid);
2493 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2497 * audit_signal_info - record signal info for shutting down audit subsystem
2498 * @sig: signal value
2499 * @t: task being signaled
2501 * If the audit subsystem is being terminated, record the task (pid)
2502 * and uid that is doing that.
2504 int __audit_signal_info(int sig, struct task_struct *t)
2506 struct audit_aux_data_pids *axp;
2507 struct task_struct *tsk = current;
2508 struct audit_context *ctx = tsk->audit_context;
2509 uid_t uid = current_uid(), t_uid = task_uid(t);
2511 if (audit_pid && t->tgid == audit_pid) {
2512 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2513 audit_sig_pid = tsk->pid;
2514 if (tsk->loginuid != -1)
2515 audit_sig_uid = tsk->loginuid;
2517 audit_sig_uid = uid;
2518 security_task_getsecid(tsk, &audit_sig_sid);
2520 if (!audit_signals || audit_dummy_context())
2524 /* optimize the common case by putting first signal recipient directly
2525 * in audit_context */
2526 if (!ctx->target_pid) {
2527 ctx->target_pid = t->tgid;
2528 ctx->target_auid = audit_get_loginuid(t);
2529 ctx->target_uid = t_uid;
2530 ctx->target_sessionid = audit_get_sessionid(t);
2531 security_task_getsecid(t, &ctx->target_sid);
2532 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2536 axp = (void *)ctx->aux_pids;
2537 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2538 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2542 axp->d.type = AUDIT_OBJ_PID;
2543 axp->d.next = ctx->aux_pids;
2544 ctx->aux_pids = (void *)axp;
2546 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2548 axp->target_pid[axp->pid_count] = t->tgid;
2549 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2550 axp->target_uid[axp->pid_count] = t_uid;
2551 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2552 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2553 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2560 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2561 * @bprm: pointer to the bprm being processed
2562 * @new: the proposed new credentials
2563 * @old: the old credentials
2565 * Simply check if the proc already has the caps given by the file and if not
2566 * store the priv escalation info for later auditing at the end of the syscall
2570 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2571 const struct cred *new, const struct cred *old)
2573 struct audit_aux_data_bprm_fcaps *ax;
2574 struct audit_context *context = current->audit_context;
2575 struct cpu_vfs_cap_data vcaps;
2576 struct dentry *dentry;
2578 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2582 ax->d.type = AUDIT_BPRM_FCAPS;
2583 ax->d.next = context->aux;
2584 context->aux = (void *)ax;
2586 dentry = dget(bprm->file->f_dentry);
2587 get_vfs_caps_from_disk(dentry, &vcaps);
2590 ax->fcap.permitted = vcaps.permitted;
2591 ax->fcap.inheritable = vcaps.inheritable;
2592 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2593 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2595 ax->old_pcap.permitted = old->cap_permitted;
2596 ax->old_pcap.inheritable = old->cap_inheritable;
2597 ax->old_pcap.effective = old->cap_effective;
2599 ax->new_pcap.permitted = new->cap_permitted;
2600 ax->new_pcap.inheritable = new->cap_inheritable;
2601 ax->new_pcap.effective = new->cap_effective;
2606 * __audit_log_capset - store information about the arguments to the capset syscall
2607 * @pid: target pid of the capset call
2608 * @new: the new credentials
2609 * @old: the old (current) credentials
2611 * Record the aguments userspace sent to sys_capset for later printing by the
2612 * audit system if applicable
2614 int __audit_log_capset(pid_t pid,
2615 const struct cred *new, const struct cred *old)
2617 struct audit_aux_data_capset *ax;
2618 struct audit_context *context = current->audit_context;
2620 if (likely(!audit_enabled || !context || context->dummy))
2623 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2627 ax->d.type = AUDIT_CAPSET;
2628 ax->d.next = context->aux;
2629 context->aux = (void *)ax;
2632 ax->cap.effective = new->cap_effective;
2633 ax->cap.inheritable = new->cap_effective;
2634 ax->cap.permitted = new->cap_permitted;
2640 * audit_core_dumps - record information about processes that end abnormally
2641 * @signr: signal value
2643 * If a process ends with a core dump, something fishy is going on and we
2644 * should record the event for investigation.
2646 void audit_core_dumps(long signr)
2648 struct audit_buffer *ab;
2650 uid_t auid = audit_get_loginuid(current), uid;
2652 unsigned int sessionid = audit_get_sessionid(current);
2657 if (signr == SIGQUIT) /* don't care for those */
2660 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2661 current_uid_gid(&uid, &gid);
2662 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2663 auid, uid, gid, sessionid);
2664 security_task_getsecid(current, &sid);
2669 if (security_secid_to_secctx(sid, &ctx, &len))
2670 audit_log_format(ab, " ssid=%u", sid);
2672 audit_log_format(ab, " subj=%s", ctx);
2673 security_release_secctx(ctx, len);
2676 audit_log_format(ab, " pid=%d comm=", current->pid);
2677 audit_log_untrustedstring(ab, current->comm);
2678 audit_log_format(ab, " sig=%ld", signr);