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;
258 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
259 static inline int open_arg(int flags, int mask)
261 int n = ACC_MODE(flags);
262 if (flags & (O_TRUNC | O_CREAT))
263 n |= AUDIT_PERM_WRITE;
267 static int audit_match_perm(struct audit_context *ctx, int mask)
274 switch (audit_classify_syscall(ctx->arch, n)) {
276 if ((mask & AUDIT_PERM_WRITE) &&
277 audit_match_class(AUDIT_CLASS_WRITE, n))
279 if ((mask & AUDIT_PERM_READ) &&
280 audit_match_class(AUDIT_CLASS_READ, n))
282 if ((mask & AUDIT_PERM_ATTR) &&
283 audit_match_class(AUDIT_CLASS_CHATTR, n))
286 case 1: /* 32bit on biarch */
287 if ((mask & AUDIT_PERM_WRITE) &&
288 audit_match_class(AUDIT_CLASS_WRITE_32, n))
290 if ((mask & AUDIT_PERM_READ) &&
291 audit_match_class(AUDIT_CLASS_READ_32, n))
293 if ((mask & AUDIT_PERM_ATTR) &&
294 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
298 return mask & ACC_MODE(ctx->argv[1]);
300 return mask & ACC_MODE(ctx->argv[2]);
301 case 4: /* socketcall */
302 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
304 return mask & AUDIT_PERM_EXEC;
310 static int audit_match_filetype(struct audit_context *ctx, int which)
312 unsigned index = which & ~S_IFMT;
313 mode_t mode = which & S_IFMT;
318 if (index >= ctx->name_count)
320 if (ctx->names[index].ino == -1)
322 if ((ctx->names[index].mode ^ mode) & S_IFMT)
328 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
329 * ->first_trees points to its beginning, ->trees - to the current end of data.
330 * ->tree_count is the number of free entries in array pointed to by ->trees.
331 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
332 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
333 * it's going to remain 1-element for almost any setup) until we free context itself.
334 * References in it _are_ dropped - at the same time we free/drop aux stuff.
337 #ifdef CONFIG_AUDIT_TREE
338 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
340 struct audit_tree_refs *p = ctx->trees;
341 int left = ctx->tree_count;
343 p->c[--left] = chunk;
344 ctx->tree_count = left;
353 ctx->tree_count = 30;
359 static int grow_tree_refs(struct audit_context *ctx)
361 struct audit_tree_refs *p = ctx->trees;
362 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
368 p->next = ctx->trees;
370 ctx->first_trees = ctx->trees;
371 ctx->tree_count = 31;
376 static void unroll_tree_refs(struct audit_context *ctx,
377 struct audit_tree_refs *p, int count)
379 #ifdef CONFIG_AUDIT_TREE
380 struct audit_tree_refs *q;
383 /* we started with empty chain */
384 p = ctx->first_trees;
386 /* if the very first allocation has failed, nothing to do */
391 for (q = p; q != ctx->trees; q = q->next, n = 31) {
393 audit_put_chunk(q->c[n]);
397 while (n-- > ctx->tree_count) {
398 audit_put_chunk(q->c[n]);
402 ctx->tree_count = count;
406 static void free_tree_refs(struct audit_context *ctx)
408 struct audit_tree_refs *p, *q;
409 for (p = ctx->first_trees; p; p = q) {
415 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
417 #ifdef CONFIG_AUDIT_TREE
418 struct audit_tree_refs *p;
423 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
424 for (n = 0; n < 31; n++)
425 if (audit_tree_match(p->c[n], tree))
430 for (n = ctx->tree_count; n < 31; n++)
431 if (audit_tree_match(p->c[n], tree))
438 /* Determine if any context name data matches a rule's watch data */
439 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
441 static int audit_filter_rules(struct task_struct *tsk,
442 struct audit_krule *rule,
443 struct audit_context *ctx,
444 struct audit_names *name,
445 enum audit_state *state)
447 const struct cred *cred = get_task_cred(tsk);
448 int i, j, need_sid = 1;
451 for (i = 0; i < rule->field_count; i++) {
452 struct audit_field *f = &rule->fields[i];
457 result = audit_comparator(tsk->pid, f->op, f->val);
462 ctx->ppid = sys_getppid();
463 result = audit_comparator(ctx->ppid, f->op, f->val);
467 result = audit_comparator(cred->uid, f->op, f->val);
470 result = audit_comparator(cred->euid, f->op, f->val);
473 result = audit_comparator(cred->suid, f->op, f->val);
476 result = audit_comparator(cred->fsuid, f->op, f->val);
479 result = audit_comparator(cred->gid, f->op, f->val);
482 result = audit_comparator(cred->egid, f->op, f->val);
485 result = audit_comparator(cred->sgid, f->op, f->val);
488 result = audit_comparator(cred->fsgid, f->op, f->val);
491 result = audit_comparator(tsk->personality, f->op, f->val);
495 result = audit_comparator(ctx->arch, f->op, f->val);
499 if (ctx && ctx->return_valid)
500 result = audit_comparator(ctx->return_code, f->op, f->val);
503 if (ctx && ctx->return_valid) {
505 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
507 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
512 result = audit_comparator(MAJOR(name->dev),
515 for (j = 0; j < ctx->name_count; j++) {
516 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
525 result = audit_comparator(MINOR(name->dev),
528 for (j = 0; j < ctx->name_count; j++) {
529 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
538 result = (name->ino == f->val);
540 for (j = 0; j < ctx->name_count; j++) {
541 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
549 if (name && rule->watch->ino != (unsigned long)-1)
550 result = (name->dev == rule->watch->dev &&
551 name->ino == rule->watch->ino);
555 result = match_tree_refs(ctx, rule->tree);
560 result = audit_comparator(tsk->loginuid, f->op, f->val);
562 case AUDIT_SUBJ_USER:
563 case AUDIT_SUBJ_ROLE:
564 case AUDIT_SUBJ_TYPE:
567 /* NOTE: this may return negative values indicating
568 a temporary error. We simply treat this as a
569 match for now to avoid losing information that
570 may be wanted. An error message will also be
574 security_task_getsecid(tsk, &sid);
577 result = security_audit_rule_match(sid, f->type,
586 case AUDIT_OBJ_LEV_LOW:
587 case AUDIT_OBJ_LEV_HIGH:
588 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
591 /* Find files that match */
593 result = security_audit_rule_match(
594 name->osid, f->type, f->op,
597 for (j = 0; j < ctx->name_count; j++) {
598 if (security_audit_rule_match(
607 /* Find ipc objects that match */
609 struct audit_aux_data *aux;
610 for (aux = ctx->aux; aux;
612 if (aux->type == AUDIT_IPC) {
613 struct audit_aux_data_ipcctl *axi = (void *)aux;
614 if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) {
628 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
630 case AUDIT_FILTERKEY:
631 /* ignore this field for filtering */
635 result = audit_match_perm(ctx, f->val);
638 result = audit_match_filetype(ctx, f->val);
647 if (rule->filterkey && ctx)
648 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
649 switch (rule->action) {
650 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
651 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
657 /* At process creation time, we can determine if system-call auditing is
658 * completely disabled for this task. Since we only have the task
659 * structure at this point, we can only check uid and gid.
661 static enum audit_state audit_filter_task(struct task_struct *tsk)
663 struct audit_entry *e;
664 enum audit_state state;
667 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
668 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
674 return AUDIT_BUILD_CONTEXT;
677 /* At syscall entry and exit time, this filter is called if the
678 * audit_state is not low enough that auditing cannot take place, but is
679 * also not high enough that we already know we have to write an audit
680 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
682 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
683 struct audit_context *ctx,
684 struct list_head *list)
686 struct audit_entry *e;
687 enum audit_state state;
689 if (audit_pid && tsk->tgid == audit_pid)
690 return AUDIT_DISABLED;
693 if (!list_empty(list)) {
694 int word = AUDIT_WORD(ctx->major);
695 int bit = AUDIT_BIT(ctx->major);
697 list_for_each_entry_rcu(e, list, list) {
698 if ((e->rule.mask[word] & bit) == bit &&
699 audit_filter_rules(tsk, &e->rule, ctx, NULL,
707 return AUDIT_BUILD_CONTEXT;
710 /* At syscall exit time, this filter is called if any audit_names[] have been
711 * collected during syscall processing. We only check rules in sublists at hash
712 * buckets applicable to the inode numbers in audit_names[].
713 * Regarding audit_state, same rules apply as for audit_filter_syscall().
715 enum audit_state audit_filter_inodes(struct task_struct *tsk,
716 struct audit_context *ctx)
719 struct audit_entry *e;
720 enum audit_state state;
722 if (audit_pid && tsk->tgid == audit_pid)
723 return AUDIT_DISABLED;
726 for (i = 0; i < ctx->name_count; i++) {
727 int word = AUDIT_WORD(ctx->major);
728 int bit = AUDIT_BIT(ctx->major);
729 struct audit_names *n = &ctx->names[i];
730 int h = audit_hash_ino((u32)n->ino);
731 struct list_head *list = &audit_inode_hash[h];
733 if (list_empty(list))
736 list_for_each_entry_rcu(e, list, list) {
737 if ((e->rule.mask[word] & bit) == bit &&
738 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
745 return AUDIT_BUILD_CONTEXT;
748 void audit_set_auditable(struct audit_context *ctx)
753 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
757 struct audit_context *context = tsk->audit_context;
759 if (likely(!context))
761 context->return_valid = return_valid;
764 * we need to fix up the return code in the audit logs if the actual
765 * return codes are later going to be fixed up by the arch specific
768 * This is actually a test for:
769 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
770 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
772 * but is faster than a bunch of ||
774 if (unlikely(return_code <= -ERESTARTSYS) &&
775 (return_code >= -ERESTART_RESTARTBLOCK) &&
776 (return_code != -ENOIOCTLCMD))
777 context->return_code = -EINTR;
779 context->return_code = return_code;
781 if (context->in_syscall && !context->dummy && !context->auditable) {
782 enum audit_state state;
784 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
785 if (state == AUDIT_RECORD_CONTEXT) {
786 context->auditable = 1;
790 state = audit_filter_inodes(tsk, context);
791 if (state == AUDIT_RECORD_CONTEXT)
792 context->auditable = 1;
798 tsk->audit_context = NULL;
802 static inline void audit_free_names(struct audit_context *context)
807 if (context->auditable
808 ||context->put_count + context->ino_count != context->name_count) {
809 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
810 " name_count=%d put_count=%d"
811 " ino_count=%d [NOT freeing]\n",
813 context->serial, context->major, context->in_syscall,
814 context->name_count, context->put_count,
816 for (i = 0; i < context->name_count; i++) {
817 printk(KERN_ERR "names[%d] = %p = %s\n", i,
818 context->names[i].name,
819 context->names[i].name ?: "(null)");
826 context->put_count = 0;
827 context->ino_count = 0;
830 for (i = 0; i < context->name_count; i++) {
831 if (context->names[i].name && context->names[i].name_put)
832 __putname(context->names[i].name);
834 context->name_count = 0;
835 path_put(&context->pwd);
836 context->pwd.dentry = NULL;
837 context->pwd.mnt = NULL;
840 static inline void audit_free_aux(struct audit_context *context)
842 struct audit_aux_data *aux;
844 while ((aux = context->aux)) {
845 context->aux = aux->next;
848 while ((aux = context->aux_pids)) {
849 context->aux_pids = aux->next;
854 static inline void audit_zero_context(struct audit_context *context,
855 enum audit_state state)
857 memset(context, 0, sizeof(*context));
858 context->state = state;
861 static inline struct audit_context *audit_alloc_context(enum audit_state state)
863 struct audit_context *context;
865 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
867 audit_zero_context(context, state);
872 * audit_alloc - allocate an audit context block for a task
875 * Filter on the task information and allocate a per-task audit context
876 * if necessary. Doing so turns on system call auditing for the
877 * specified task. This is called from copy_process, so no lock is
880 int audit_alloc(struct task_struct *tsk)
882 struct audit_context *context;
883 enum audit_state state;
885 if (likely(!audit_ever_enabled))
886 return 0; /* Return if not auditing. */
888 state = audit_filter_task(tsk);
889 if (likely(state == AUDIT_DISABLED))
892 if (!(context = audit_alloc_context(state))) {
893 audit_log_lost("out of memory in audit_alloc");
897 tsk->audit_context = context;
898 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
902 static inline void audit_free_context(struct audit_context *context)
904 struct audit_context *previous;
908 previous = context->previous;
909 if (previous || (count && count < 10)) {
911 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
912 " freeing multiple contexts (%d)\n",
913 context->serial, context->major,
914 context->name_count, count);
916 audit_free_names(context);
917 unroll_tree_refs(context, NULL, 0);
918 free_tree_refs(context);
919 audit_free_aux(context);
920 kfree(context->filterkey);
921 kfree(context->sockaddr);
926 printk(KERN_ERR "audit: freed %d contexts\n", count);
929 void audit_log_task_context(struct audit_buffer *ab)
936 security_task_getsecid(current, &sid);
940 error = security_secid_to_secctx(sid, &ctx, &len);
942 if (error != -EINVAL)
947 audit_log_format(ab, " subj=%s", ctx);
948 security_release_secctx(ctx, len);
952 audit_panic("error in audit_log_task_context");
956 EXPORT_SYMBOL(audit_log_task_context);
958 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
960 char name[sizeof(tsk->comm)];
961 struct mm_struct *mm = tsk->mm;
962 struct vm_area_struct *vma;
966 get_task_comm(name, tsk);
967 audit_log_format(ab, " comm=");
968 audit_log_untrustedstring(ab, name);
971 down_read(&mm->mmap_sem);
974 if ((vma->vm_flags & VM_EXECUTABLE) &&
976 audit_log_d_path(ab, "exe=",
977 &vma->vm_file->f_path);
982 up_read(&mm->mmap_sem);
984 audit_log_task_context(ab);
987 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
988 uid_t auid, uid_t uid, unsigned int sessionid,
991 struct audit_buffer *ab;
996 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
1000 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
1002 if (security_secid_to_secctx(sid, &ctx, &len)) {
1003 audit_log_format(ab, " obj=(none)");
1006 audit_log_format(ab, " obj=%s", ctx);
1007 security_release_secctx(ctx, len);
1009 audit_log_format(ab, " ocomm=");
1010 audit_log_untrustedstring(ab, comm);
1017 * to_send and len_sent accounting are very loose estimates. We aren't
1018 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1019 * within about 500 bytes (next page boundry)
1021 * why snprintf? an int is up to 12 digits long. if we just assumed when
1022 * logging that a[%d]= was going to be 16 characters long we would be wasting
1023 * space in every audit message. In one 7500 byte message we can log up to
1024 * about 1000 min size arguments. That comes down to about 50% waste of space
1025 * if we didn't do the snprintf to find out how long arg_num_len was.
1027 static int audit_log_single_execve_arg(struct audit_context *context,
1028 struct audit_buffer **ab,
1031 const char __user *p,
1034 char arg_num_len_buf[12];
1035 const char __user *tmp_p = p;
1036 /* how many digits are in arg_num? 3 is the length of a=\n */
1037 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
1038 size_t len, len_left, to_send;
1039 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1040 unsigned int i, has_cntl = 0, too_long = 0;
1043 /* strnlen_user includes the null we don't want to send */
1044 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1047 * We just created this mm, if we can't find the strings
1048 * we just copied into it something is _very_ wrong. Similar
1049 * for strings that are too long, we should not have created
1052 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
1054 send_sig(SIGKILL, current, 0);
1058 /* walk the whole argument looking for non-ascii chars */
1060 if (len_left > MAX_EXECVE_AUDIT_LEN)
1061 to_send = MAX_EXECVE_AUDIT_LEN;
1064 ret = copy_from_user(buf, tmp_p, to_send);
1066 * There is no reason for this copy to be short. We just
1067 * copied them here, and the mm hasn't been exposed to user-
1072 send_sig(SIGKILL, current, 0);
1075 buf[to_send] = '\0';
1076 has_cntl = audit_string_contains_control(buf, to_send);
1079 * hex messages get logged as 2 bytes, so we can only
1080 * send half as much in each message
1082 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1085 len_left -= to_send;
1087 } while (len_left > 0);
1091 if (len > max_execve_audit_len)
1094 /* rewalk the argument actually logging the message */
1095 for (i = 0; len_left > 0; i++) {
1098 if (len_left > max_execve_audit_len)
1099 to_send = max_execve_audit_len;
1103 /* do we have space left to send this argument in this ab? */
1104 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1106 room_left -= (to_send * 2);
1108 room_left -= to_send;
1109 if (room_left < 0) {
1112 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1118 * first record needs to say how long the original string was
1119 * so we can be sure nothing was lost.
1121 if ((i == 0) && (too_long))
1122 audit_log_format(*ab, "a%d_len=%zu ", arg_num,
1123 has_cntl ? 2*len : len);
1126 * normally arguments are small enough to fit and we already
1127 * filled buf above when we checked for control characters
1128 * so don't bother with another copy_from_user
1130 if (len >= max_execve_audit_len)
1131 ret = copy_from_user(buf, p, to_send);
1136 send_sig(SIGKILL, current, 0);
1139 buf[to_send] = '\0';
1141 /* actually log it */
1142 audit_log_format(*ab, "a%d", arg_num);
1144 audit_log_format(*ab, "[%d]", i);
1145 audit_log_format(*ab, "=");
1147 audit_log_n_hex(*ab, buf, to_send);
1149 audit_log_format(*ab, "\"%s\"", buf);
1150 audit_log_format(*ab, "\n");
1153 len_left -= to_send;
1154 *len_sent += arg_num_len;
1156 *len_sent += to_send * 2;
1158 *len_sent += to_send;
1160 /* include the null we didn't log */
1164 static void audit_log_execve_info(struct audit_context *context,
1165 struct audit_buffer **ab,
1166 struct audit_aux_data_execve *axi)
1169 size_t len, len_sent = 0;
1170 const char __user *p;
1173 if (axi->mm != current->mm)
1174 return; /* execve failed, no additional info */
1176 p = (const char __user *)axi->mm->arg_start;
1178 audit_log_format(*ab, "argc=%d ", axi->argc);
1181 * we need some kernel buffer to hold the userspace args. Just
1182 * allocate one big one rather than allocating one of the right size
1183 * for every single argument inside audit_log_single_execve_arg()
1184 * should be <8k allocation so should be pretty safe.
1186 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1188 audit_panic("out of memory for argv string\n");
1192 for (i = 0; i < axi->argc; i++) {
1193 len = audit_log_single_execve_arg(context, ab, i,
1202 static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1206 audit_log_format(ab, " %s=", prefix);
1207 CAP_FOR_EACH_U32(i) {
1208 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1212 static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1214 kernel_cap_t *perm = &name->fcap.permitted;
1215 kernel_cap_t *inh = &name->fcap.inheritable;
1218 if (!cap_isclear(*perm)) {
1219 audit_log_cap(ab, "cap_fp", perm);
1222 if (!cap_isclear(*inh)) {
1223 audit_log_cap(ab, "cap_fi", inh);
1228 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1231 static void show_special(struct audit_context *context)
1233 struct audit_buffer *ab;
1236 ab = audit_log_start(context, GFP_KERNEL, context->type);
1240 switch (context->type) {
1241 case AUDIT_SOCKETCALL: {
1242 int nargs = context->socketcall.nargs;
1243 audit_log_format(ab, "nargs=%d", nargs);
1244 for (i = 0; i < nargs; i++)
1245 audit_log_format(ab, " a%d=%lx", i,
1246 context->socketcall.args[i]);
1252 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1254 const struct cred *cred;
1255 int i, call_panic = 0;
1256 struct audit_buffer *ab;
1257 struct audit_aux_data *aux;
1260 /* tsk == current */
1261 context->pid = tsk->pid;
1263 context->ppid = sys_getppid();
1264 cred = current_cred();
1265 context->uid = cred->uid;
1266 context->gid = cred->gid;
1267 context->euid = cred->euid;
1268 context->suid = cred->suid;
1269 context->fsuid = cred->fsuid;
1270 context->egid = cred->egid;
1271 context->sgid = cred->sgid;
1272 context->fsgid = cred->fsgid;
1273 context->personality = tsk->personality;
1275 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1277 return; /* audit_panic has been called */
1278 audit_log_format(ab, "arch=%x syscall=%d",
1279 context->arch, context->major);
1280 if (context->personality != PER_LINUX)
1281 audit_log_format(ab, " per=%lx", context->personality);
1282 if (context->return_valid)
1283 audit_log_format(ab, " success=%s exit=%ld",
1284 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1285 context->return_code);
1287 spin_lock_irq(&tsk->sighand->siglock);
1288 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1289 tty = tsk->signal->tty->name;
1292 spin_unlock_irq(&tsk->sighand->siglock);
1294 audit_log_format(ab,
1295 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1296 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1297 " euid=%u suid=%u fsuid=%u"
1298 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1303 context->name_count,
1309 context->euid, context->suid, context->fsuid,
1310 context->egid, context->sgid, context->fsgid, tty,
1314 audit_log_task_info(ab, tsk);
1315 if (context->filterkey) {
1316 audit_log_format(ab, " key=");
1317 audit_log_untrustedstring(ab, context->filterkey);
1319 audit_log_format(ab, " key=(null)");
1322 for (aux = context->aux; aux; aux = aux->next) {
1324 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1326 continue; /* audit_panic has been called */
1328 switch (aux->type) {
1329 case AUDIT_MQ_OPEN: {
1330 struct audit_aux_data_mq_open *axi = (void *)aux;
1331 audit_log_format(ab,
1332 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1333 "mq_msgsize=%ld mq_curmsgs=%ld",
1334 axi->oflag, axi->mode, axi->attr.mq_flags,
1335 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1336 axi->attr.mq_curmsgs);
1339 case AUDIT_MQ_SENDRECV: {
1340 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1341 audit_log_format(ab,
1342 "mqdes=%d msg_len=%zd msg_prio=%u "
1343 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1344 axi->mqdes, axi->msg_len, axi->msg_prio,
1345 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1348 case AUDIT_MQ_NOTIFY: {
1349 struct audit_aux_data_mq_notify *axi = (void *)aux;
1350 audit_log_format(ab,
1351 "mqdes=%d sigev_signo=%d",
1353 axi->notification.sigev_signo);
1356 case AUDIT_MQ_GETSETATTR: {
1357 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1358 audit_log_format(ab,
1359 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1362 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1363 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1367 struct audit_aux_data_ipcctl *axi = (void *)aux;
1368 audit_log_format(ab,
1369 "ouid=%u ogid=%u mode=%#o",
1370 axi->uid, axi->gid, axi->mode);
1371 if (axi->osid != 0) {
1374 if (security_secid_to_secctx(
1375 axi->osid, &ctx, &len)) {
1376 audit_log_format(ab, " osid=%u",
1380 audit_log_format(ab, " obj=%s", ctx);
1381 security_release_secctx(ctx, len);
1386 case AUDIT_IPC_SET_PERM: {
1387 struct audit_aux_data_ipcctl *axi = (void *)aux;
1388 audit_log_format(ab,
1389 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1390 axi->qbytes, axi->uid, axi->gid, axi->mode);
1393 case AUDIT_EXECVE: {
1394 struct audit_aux_data_execve *axi = (void *)aux;
1395 audit_log_execve_info(context, &ab, axi);
1398 case AUDIT_FD_PAIR: {
1399 struct audit_aux_data_fd_pair *axs = (void *)aux;
1400 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1403 case AUDIT_BPRM_FCAPS: {
1404 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1405 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1406 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1407 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1408 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1409 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1410 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1411 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1412 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1413 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1414 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1417 case AUDIT_CAPSET: {
1418 struct audit_aux_data_capset *axs = (void *)aux;
1419 audit_log_format(ab, "pid=%d", axs->pid);
1420 audit_log_cap(ab, "cap_pi", &axs->cap.inheritable);
1421 audit_log_cap(ab, "cap_pp", &axs->cap.permitted);
1422 audit_log_cap(ab, "cap_pe", &axs->cap.effective);
1430 show_special(context);
1432 if (context->sockaddr_len) {
1433 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1435 audit_log_format(ab, "saddr=");
1436 audit_log_n_hex(ab, (void *)context->sockaddr,
1437 context->sockaddr_len);
1442 for (aux = context->aux_pids; aux; aux = aux->next) {
1443 struct audit_aux_data_pids *axs = (void *)aux;
1445 for (i = 0; i < axs->pid_count; i++)
1446 if (audit_log_pid_context(context, axs->target_pid[i],
1447 axs->target_auid[i],
1449 axs->target_sessionid[i],
1451 axs->target_comm[i]))
1455 if (context->target_pid &&
1456 audit_log_pid_context(context, context->target_pid,
1457 context->target_auid, context->target_uid,
1458 context->target_sessionid,
1459 context->target_sid, context->target_comm))
1462 if (context->pwd.dentry && context->pwd.mnt) {
1463 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1465 audit_log_d_path(ab, "cwd=", &context->pwd);
1469 for (i = 0; i < context->name_count; i++) {
1470 struct audit_names *n = &context->names[i];
1472 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1474 continue; /* audit_panic has been called */
1476 audit_log_format(ab, "item=%d", i);
1479 switch(n->name_len) {
1480 case AUDIT_NAME_FULL:
1481 /* log the full path */
1482 audit_log_format(ab, " name=");
1483 audit_log_untrustedstring(ab, n->name);
1486 /* name was specified as a relative path and the
1487 * directory component is the cwd */
1488 audit_log_d_path(ab, " name=", &context->pwd);
1491 /* log the name's directory component */
1492 audit_log_format(ab, " name=");
1493 audit_log_n_untrustedstring(ab, n->name,
1497 audit_log_format(ab, " name=(null)");
1499 if (n->ino != (unsigned long)-1) {
1500 audit_log_format(ab, " inode=%lu"
1501 " dev=%02x:%02x mode=%#o"
1502 " ouid=%u ogid=%u rdev=%02x:%02x",
1515 if (security_secid_to_secctx(
1516 n->osid, &ctx, &len)) {
1517 audit_log_format(ab, " osid=%u", n->osid);
1520 audit_log_format(ab, " obj=%s", ctx);
1521 security_release_secctx(ctx, len);
1525 audit_log_fcaps(ab, n);
1530 /* Send end of event record to help user space know we are finished */
1531 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1535 audit_panic("error converting sid to string");
1539 * audit_free - free a per-task audit context
1540 * @tsk: task whose audit context block to free
1542 * Called from copy_process and do_exit
1544 void audit_free(struct task_struct *tsk)
1546 struct audit_context *context;
1548 context = audit_get_context(tsk, 0, 0);
1549 if (likely(!context))
1552 /* Check for system calls that do not go through the exit
1553 * function (e.g., exit_group), then free context block.
1554 * We use GFP_ATOMIC here because we might be doing this
1555 * in the context of the idle thread */
1556 /* that can happen only if we are called from do_exit() */
1557 if (context->in_syscall && context->auditable)
1558 audit_log_exit(context, tsk);
1560 audit_free_context(context);
1564 * audit_syscall_entry - fill in an audit record at syscall entry
1565 * @arch: architecture type
1566 * @major: major syscall type (function)
1567 * @a1: additional syscall register 1
1568 * @a2: additional syscall register 2
1569 * @a3: additional syscall register 3
1570 * @a4: additional syscall register 4
1572 * Fill in audit context at syscall entry. This only happens if the
1573 * audit context was created when the task was created and the state or
1574 * filters demand the audit context be built. If the state from the
1575 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1576 * then the record will be written at syscall exit time (otherwise, it
1577 * will only be written if another part of the kernel requests that it
1580 void audit_syscall_entry(int arch, int major,
1581 unsigned long a1, unsigned long a2,
1582 unsigned long a3, unsigned long a4)
1584 struct task_struct *tsk = current;
1585 struct audit_context *context = tsk->audit_context;
1586 enum audit_state state;
1588 if (unlikely(!context))
1592 * This happens only on certain architectures that make system
1593 * calls in kernel_thread via the entry.S interface, instead of
1594 * with direct calls. (If you are porting to a new
1595 * architecture, hitting this condition can indicate that you
1596 * got the _exit/_leave calls backward in entry.S.)
1600 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1602 * This also happens with vm86 emulation in a non-nested manner
1603 * (entries without exits), so this case must be caught.
1605 if (context->in_syscall) {
1606 struct audit_context *newctx;
1610 "audit(:%d) pid=%d in syscall=%d;"
1611 " entering syscall=%d\n",
1612 context->serial, tsk->pid, context->major, major);
1614 newctx = audit_alloc_context(context->state);
1616 newctx->previous = context;
1618 tsk->audit_context = newctx;
1620 /* If we can't alloc a new context, the best we
1621 * can do is to leak memory (any pending putname
1622 * will be lost). The only other alternative is
1623 * to abandon auditing. */
1624 audit_zero_context(context, context->state);
1627 BUG_ON(context->in_syscall || context->name_count);
1632 context->arch = arch;
1633 context->major = major;
1634 context->argv[0] = a1;
1635 context->argv[1] = a2;
1636 context->argv[2] = a3;
1637 context->argv[3] = a4;
1639 state = context->state;
1640 context->dummy = !audit_n_rules;
1641 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1642 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1643 if (likely(state == AUDIT_DISABLED))
1646 context->serial = 0;
1647 context->ctime = CURRENT_TIME;
1648 context->in_syscall = 1;
1649 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1653 void audit_finish_fork(struct task_struct *child)
1655 struct audit_context *ctx = current->audit_context;
1656 struct audit_context *p = child->audit_context;
1657 if (!p || !ctx || !ctx->auditable)
1659 p->arch = ctx->arch;
1660 p->major = ctx->major;
1661 memcpy(p->argv, ctx->argv, sizeof(ctx->argv));
1662 p->ctime = ctx->ctime;
1663 p->dummy = ctx->dummy;
1664 p->auditable = ctx->auditable;
1665 p->in_syscall = ctx->in_syscall;
1666 p->filterkey = kstrdup(ctx->filterkey, GFP_KERNEL);
1667 p->ppid = current->pid;
1671 * audit_syscall_exit - deallocate audit context after a system call
1672 * @valid: success/failure flag
1673 * @return_code: syscall return value
1675 * Tear down after system call. If the audit context has been marked as
1676 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1677 * filtering, or because some other part of the kernel write an audit
1678 * message), then write out the syscall information. In call cases,
1679 * free the names stored from getname().
1681 void audit_syscall_exit(int valid, long return_code)
1683 struct task_struct *tsk = current;
1684 struct audit_context *context;
1686 context = audit_get_context(tsk, valid, return_code);
1688 if (likely(!context))
1691 if (context->in_syscall && context->auditable)
1692 audit_log_exit(context, tsk);
1694 context->in_syscall = 0;
1695 context->auditable = 0;
1697 if (context->previous) {
1698 struct audit_context *new_context = context->previous;
1699 context->previous = NULL;
1700 audit_free_context(context);
1701 tsk->audit_context = new_context;
1703 audit_free_names(context);
1704 unroll_tree_refs(context, NULL, 0);
1705 audit_free_aux(context);
1706 context->aux = NULL;
1707 context->aux_pids = NULL;
1708 context->target_pid = 0;
1709 context->target_sid = 0;
1710 context->sockaddr_len = 0;
1712 kfree(context->filterkey);
1713 context->filterkey = NULL;
1714 tsk->audit_context = context;
1718 static inline void handle_one(const struct inode *inode)
1720 #ifdef CONFIG_AUDIT_TREE
1721 struct audit_context *context;
1722 struct audit_tree_refs *p;
1723 struct audit_chunk *chunk;
1725 if (likely(list_empty(&inode->inotify_watches)))
1727 context = current->audit_context;
1729 count = context->tree_count;
1731 chunk = audit_tree_lookup(inode);
1735 if (likely(put_tree_ref(context, chunk)))
1737 if (unlikely(!grow_tree_refs(context))) {
1738 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
1739 audit_set_auditable(context);
1740 audit_put_chunk(chunk);
1741 unroll_tree_refs(context, p, count);
1744 put_tree_ref(context, chunk);
1748 static void handle_path(const struct dentry *dentry)
1750 #ifdef CONFIG_AUDIT_TREE
1751 struct audit_context *context;
1752 struct audit_tree_refs *p;
1753 const struct dentry *d, *parent;
1754 struct audit_chunk *drop;
1758 context = current->audit_context;
1760 count = context->tree_count;
1765 seq = read_seqbegin(&rename_lock);
1767 struct inode *inode = d->d_inode;
1768 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1769 struct audit_chunk *chunk;
1770 chunk = audit_tree_lookup(inode);
1772 if (unlikely(!put_tree_ref(context, chunk))) {
1778 parent = d->d_parent;
1783 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1786 /* just a race with rename */
1787 unroll_tree_refs(context, p, count);
1790 audit_put_chunk(drop);
1791 if (grow_tree_refs(context)) {
1792 /* OK, got more space */
1793 unroll_tree_refs(context, p, count);
1798 "out of memory, audit has lost a tree reference\n");
1799 unroll_tree_refs(context, p, count);
1800 audit_set_auditable(context);
1808 * audit_getname - add a name to the list
1809 * @name: name to add
1811 * Add a name to the list of audit names for this context.
1812 * Called from fs/namei.c:getname().
1814 void __audit_getname(const char *name)
1816 struct audit_context *context = current->audit_context;
1818 if (IS_ERR(name) || !name)
1821 if (!context->in_syscall) {
1822 #if AUDIT_DEBUG == 2
1823 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1824 __FILE__, __LINE__, context->serial, name);
1829 BUG_ON(context->name_count >= AUDIT_NAMES);
1830 context->names[context->name_count].name = name;
1831 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1832 context->names[context->name_count].name_put = 1;
1833 context->names[context->name_count].ino = (unsigned long)-1;
1834 context->names[context->name_count].osid = 0;
1835 ++context->name_count;
1836 if (!context->pwd.dentry) {
1837 read_lock(¤t->fs->lock);
1838 context->pwd = current->fs->pwd;
1839 path_get(¤t->fs->pwd);
1840 read_unlock(¤t->fs->lock);
1845 /* audit_putname - intercept a putname request
1846 * @name: name to intercept and delay for putname
1848 * If we have stored the name from getname in the audit context,
1849 * then we delay the putname until syscall exit.
1850 * Called from include/linux/fs.h:putname().
1852 void audit_putname(const char *name)
1854 struct audit_context *context = current->audit_context;
1857 if (!context->in_syscall) {
1858 #if AUDIT_DEBUG == 2
1859 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1860 __FILE__, __LINE__, context->serial, name);
1861 if (context->name_count) {
1863 for (i = 0; i < context->name_count; i++)
1864 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1865 context->names[i].name,
1866 context->names[i].name ?: "(null)");
1873 ++context->put_count;
1874 if (context->put_count > context->name_count) {
1875 printk(KERN_ERR "%s:%d(:%d): major=%d"
1876 " in_syscall=%d putname(%p) name_count=%d"
1879 context->serial, context->major,
1880 context->in_syscall, name, context->name_count,
1881 context->put_count);
1888 static int audit_inc_name_count(struct audit_context *context,
1889 const struct inode *inode)
1891 if (context->name_count >= AUDIT_NAMES) {
1893 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1894 "dev=%02x:%02x, inode=%lu\n",
1895 MAJOR(inode->i_sb->s_dev),
1896 MINOR(inode->i_sb->s_dev),
1900 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
1903 context->name_count++;
1905 context->ino_count++;
1911 static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1913 struct cpu_vfs_cap_data caps;
1916 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1917 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1924 rc = get_vfs_caps_from_disk(dentry, &caps);
1928 name->fcap.permitted = caps.permitted;
1929 name->fcap.inheritable = caps.inheritable;
1930 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1931 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1937 /* Copy inode data into an audit_names. */
1938 static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1939 const struct inode *inode)
1941 name->ino = inode->i_ino;
1942 name->dev = inode->i_sb->s_dev;
1943 name->mode = inode->i_mode;
1944 name->uid = inode->i_uid;
1945 name->gid = inode->i_gid;
1946 name->rdev = inode->i_rdev;
1947 security_inode_getsecid(inode, &name->osid);
1948 audit_copy_fcaps(name, dentry);
1952 * audit_inode - store the inode and device from a lookup
1953 * @name: name being audited
1954 * @dentry: dentry being audited
1956 * Called from fs/namei.c:path_lookup().
1958 void __audit_inode(const char *name, const struct dentry *dentry)
1961 struct audit_context *context = current->audit_context;
1962 const struct inode *inode = dentry->d_inode;
1964 if (!context->in_syscall)
1966 if (context->name_count
1967 && context->names[context->name_count-1].name
1968 && context->names[context->name_count-1].name == name)
1969 idx = context->name_count - 1;
1970 else if (context->name_count > 1
1971 && context->names[context->name_count-2].name
1972 && context->names[context->name_count-2].name == name)
1973 idx = context->name_count - 2;
1975 /* FIXME: how much do we care about inodes that have no
1976 * associated name? */
1977 if (audit_inc_name_count(context, inode))
1979 idx = context->name_count - 1;
1980 context->names[idx].name = NULL;
1982 handle_path(dentry);
1983 audit_copy_inode(&context->names[idx], dentry, inode);
1987 * audit_inode_child - collect inode info for created/removed objects
1988 * @dname: inode's dentry name
1989 * @dentry: dentry being audited
1990 * @parent: inode of dentry parent
1992 * For syscalls that create or remove filesystem objects, audit_inode
1993 * can only collect information for the filesystem object's parent.
1994 * This call updates the audit context with the child's information.
1995 * Syscalls that create a new filesystem object must be hooked after
1996 * the object is created. Syscalls that remove a filesystem object
1997 * must be hooked prior, in order to capture the target inode during
1998 * unsuccessful attempts.
2000 void __audit_inode_child(const char *dname, const struct dentry *dentry,
2001 const struct inode *parent)
2004 struct audit_context *context = current->audit_context;
2005 const char *found_parent = NULL, *found_child = NULL;
2006 const struct inode *inode = dentry->d_inode;
2009 if (!context->in_syscall)
2014 /* determine matching parent */
2018 /* parent is more likely, look for it first */
2019 for (idx = 0; idx < context->name_count; idx++) {
2020 struct audit_names *n = &context->names[idx];
2025 if (n->ino == parent->i_ino &&
2026 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2027 n->name_len = dirlen; /* update parent data in place */
2028 found_parent = n->name;
2033 /* no matching parent, look for matching child */
2034 for (idx = 0; idx < context->name_count; idx++) {
2035 struct audit_names *n = &context->names[idx];
2040 /* strcmp() is the more likely scenario */
2041 if (!strcmp(dname, n->name) ||
2042 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2044 audit_copy_inode(n, NULL, inode);
2046 n->ino = (unsigned long)-1;
2047 found_child = n->name;
2053 if (!found_parent) {
2054 if (audit_inc_name_count(context, parent))
2056 idx = context->name_count - 1;
2057 context->names[idx].name = NULL;
2058 audit_copy_inode(&context->names[idx], NULL, parent);
2062 if (audit_inc_name_count(context, inode))
2064 idx = context->name_count - 1;
2066 /* Re-use the name belonging to the slot for a matching parent
2067 * directory. All names for this context are relinquished in
2068 * audit_free_names() */
2070 context->names[idx].name = found_parent;
2071 context->names[idx].name_len = AUDIT_NAME_FULL;
2072 /* don't call __putname() */
2073 context->names[idx].name_put = 0;
2075 context->names[idx].name = NULL;
2079 audit_copy_inode(&context->names[idx], NULL, inode);
2081 context->names[idx].ino = (unsigned long)-1;
2084 EXPORT_SYMBOL_GPL(__audit_inode_child);
2087 * auditsc_get_stamp - get local copies of audit_context values
2088 * @ctx: audit_context for the task
2089 * @t: timespec to store time recorded in the audit_context
2090 * @serial: serial value that is recorded in the audit_context
2092 * Also sets the context as auditable.
2094 int auditsc_get_stamp(struct audit_context *ctx,
2095 struct timespec *t, unsigned int *serial)
2097 if (!ctx->in_syscall)
2100 ctx->serial = audit_serial();
2101 t->tv_sec = ctx->ctime.tv_sec;
2102 t->tv_nsec = ctx->ctime.tv_nsec;
2103 *serial = ctx->serial;
2108 /* global counter which is incremented every time something logs in */
2109 static atomic_t session_id = ATOMIC_INIT(0);
2112 * audit_set_loginuid - set a task's audit_context loginuid
2113 * @task: task whose audit context is being modified
2114 * @loginuid: loginuid value
2118 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2120 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
2122 unsigned int sessionid = atomic_inc_return(&session_id);
2123 struct audit_context *context = task->audit_context;
2125 if (context && context->in_syscall) {
2126 struct audit_buffer *ab;
2128 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2130 audit_log_format(ab, "login pid=%d uid=%u "
2131 "old auid=%u new auid=%u"
2132 " old ses=%u new ses=%u",
2133 task->pid, task_uid(task),
2134 task->loginuid, loginuid,
2135 task->sessionid, sessionid);
2139 task->sessionid = sessionid;
2140 task->loginuid = loginuid;
2145 * __audit_mq_open - record audit data for a POSIX MQ open
2148 * @u_attr: queue attributes
2150 * Returns 0 for success or NULL context or < 0 on error.
2152 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
2154 struct audit_aux_data_mq_open *ax;
2155 struct audit_context *context = current->audit_context;
2160 if (likely(!context))
2163 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2167 if (u_attr != NULL) {
2168 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
2173 memset(&ax->attr, 0, sizeof(ax->attr));
2178 ax->d.type = AUDIT_MQ_OPEN;
2179 ax->d.next = context->aux;
2180 context->aux = (void *)ax;
2185 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
2186 * @mqdes: MQ descriptor
2187 * @msg_len: Message length
2188 * @msg_prio: Message priority
2189 * @u_abs_timeout: Message timeout in absolute time
2191 * Returns 0 for success or NULL context or < 0 on error.
2193 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2194 const struct timespec __user *u_abs_timeout)
2196 struct audit_aux_data_mq_sendrecv *ax;
2197 struct audit_context *context = current->audit_context;
2202 if (likely(!context))
2205 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2209 if (u_abs_timeout != NULL) {
2210 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2215 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2218 ax->msg_len = msg_len;
2219 ax->msg_prio = msg_prio;
2221 ax->d.type = AUDIT_MQ_SENDRECV;
2222 ax->d.next = context->aux;
2223 context->aux = (void *)ax;
2228 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
2229 * @mqdes: MQ descriptor
2230 * @msg_len: Message length
2231 * @u_msg_prio: Message priority
2232 * @u_abs_timeout: Message timeout in absolute time
2234 * Returns 0 for success or NULL context or < 0 on error.
2236 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
2237 unsigned int __user *u_msg_prio,
2238 const struct timespec __user *u_abs_timeout)
2240 struct audit_aux_data_mq_sendrecv *ax;
2241 struct audit_context *context = current->audit_context;
2246 if (likely(!context))
2249 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2253 if (u_msg_prio != NULL) {
2254 if (get_user(ax->msg_prio, u_msg_prio)) {
2261 if (u_abs_timeout != NULL) {
2262 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2267 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2270 ax->msg_len = msg_len;
2272 ax->d.type = AUDIT_MQ_SENDRECV;
2273 ax->d.next = context->aux;
2274 context->aux = (void *)ax;
2279 * __audit_mq_notify - record audit data for a POSIX MQ notify
2280 * @mqdes: MQ descriptor
2281 * @u_notification: Notification event
2283 * Returns 0 for success or NULL context or < 0 on error.
2286 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
2288 struct audit_aux_data_mq_notify *ax;
2289 struct audit_context *context = current->audit_context;
2294 if (likely(!context))
2297 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2301 if (u_notification != NULL) {
2302 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
2307 memset(&ax->notification, 0, sizeof(ax->notification));
2311 ax->d.type = AUDIT_MQ_NOTIFY;
2312 ax->d.next = context->aux;
2313 context->aux = (void *)ax;
2318 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2319 * @mqdes: MQ descriptor
2322 * Returns 0 for success or NULL context or < 0 on error.
2324 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2326 struct audit_aux_data_mq_getsetattr *ax;
2327 struct audit_context *context = current->audit_context;
2332 if (likely(!context))
2335 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2340 ax->mqstat = *mqstat;
2342 ax->d.type = AUDIT_MQ_GETSETATTR;
2343 ax->d.next = context->aux;
2344 context->aux = (void *)ax;
2349 * audit_ipc_obj - record audit data for ipc object
2350 * @ipcp: ipc permissions
2352 * Returns 0 for success or NULL context or < 0 on error.
2354 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2356 struct audit_aux_data_ipcctl *ax;
2357 struct audit_context *context = current->audit_context;
2359 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2363 ax->uid = ipcp->uid;
2364 ax->gid = ipcp->gid;
2365 ax->mode = ipcp->mode;
2366 security_ipc_getsecid(ipcp, &ax->osid);
2367 ax->d.type = AUDIT_IPC;
2368 ax->d.next = context->aux;
2369 context->aux = (void *)ax;
2374 * audit_ipc_set_perm - record audit data for new ipc permissions
2375 * @qbytes: msgq bytes
2376 * @uid: msgq user id
2377 * @gid: msgq group id
2378 * @mode: msgq mode (permissions)
2380 * Returns 0 for success or NULL context or < 0 on error.
2382 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2384 struct audit_aux_data_ipcctl *ax;
2385 struct audit_context *context = current->audit_context;
2387 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2391 ax->qbytes = qbytes;
2396 ax->d.type = AUDIT_IPC_SET_PERM;
2397 ax->d.next = context->aux;
2398 context->aux = (void *)ax;
2402 int audit_bprm(struct linux_binprm *bprm)
2404 struct audit_aux_data_execve *ax;
2405 struct audit_context *context = current->audit_context;
2407 if (likely(!audit_enabled || !context || context->dummy))
2410 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2414 ax->argc = bprm->argc;
2415 ax->envc = bprm->envc;
2417 ax->d.type = AUDIT_EXECVE;
2418 ax->d.next = context->aux;
2419 context->aux = (void *)ax;
2425 * audit_socketcall - record audit data for sys_socketcall
2426 * @nargs: number of args
2430 void audit_socketcall(int nargs, unsigned long *args)
2432 struct audit_context *context = current->audit_context;
2434 if (likely(!context || context->dummy))
2437 context->type = AUDIT_SOCKETCALL;
2438 context->socketcall.nargs = nargs;
2439 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2443 * __audit_fd_pair - record audit data for pipe and socketpair
2444 * @fd1: the first file descriptor
2445 * @fd2: the second file descriptor
2447 * Returns 0 for success or NULL context or < 0 on error.
2449 int __audit_fd_pair(int fd1, int fd2)
2451 struct audit_context *context = current->audit_context;
2452 struct audit_aux_data_fd_pair *ax;
2454 if (likely(!context)) {
2458 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2466 ax->d.type = AUDIT_FD_PAIR;
2467 ax->d.next = context->aux;
2468 context->aux = (void *)ax;
2473 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2474 * @len: data length in user space
2475 * @a: data address in kernel space
2477 * Returns 0 for success or NULL context or < 0 on error.
2479 int audit_sockaddr(int len, void *a)
2481 struct audit_context *context = current->audit_context;
2483 if (likely(!context || context->dummy))
2486 if (!context->sockaddr) {
2487 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2490 context->sockaddr = p;
2493 context->sockaddr_len = len;
2494 memcpy(context->sockaddr, a, len);
2498 void __audit_ptrace(struct task_struct *t)
2500 struct audit_context *context = current->audit_context;
2502 context->target_pid = t->pid;
2503 context->target_auid = audit_get_loginuid(t);
2504 context->target_uid = task_uid(t);
2505 context->target_sessionid = audit_get_sessionid(t);
2506 security_task_getsecid(t, &context->target_sid);
2507 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2511 * audit_signal_info - record signal info for shutting down audit subsystem
2512 * @sig: signal value
2513 * @t: task being signaled
2515 * If the audit subsystem is being terminated, record the task (pid)
2516 * and uid that is doing that.
2518 int __audit_signal_info(int sig, struct task_struct *t)
2520 struct audit_aux_data_pids *axp;
2521 struct task_struct *tsk = current;
2522 struct audit_context *ctx = tsk->audit_context;
2523 uid_t uid = current_uid(), t_uid = task_uid(t);
2525 if (audit_pid && t->tgid == audit_pid) {
2526 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2527 audit_sig_pid = tsk->pid;
2528 if (tsk->loginuid != -1)
2529 audit_sig_uid = tsk->loginuid;
2531 audit_sig_uid = uid;
2532 security_task_getsecid(tsk, &audit_sig_sid);
2534 if (!audit_signals || audit_dummy_context())
2538 /* optimize the common case by putting first signal recipient directly
2539 * in audit_context */
2540 if (!ctx->target_pid) {
2541 ctx->target_pid = t->tgid;
2542 ctx->target_auid = audit_get_loginuid(t);
2543 ctx->target_uid = t_uid;
2544 ctx->target_sessionid = audit_get_sessionid(t);
2545 security_task_getsecid(t, &ctx->target_sid);
2546 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2550 axp = (void *)ctx->aux_pids;
2551 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2552 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2556 axp->d.type = AUDIT_OBJ_PID;
2557 axp->d.next = ctx->aux_pids;
2558 ctx->aux_pids = (void *)axp;
2560 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2562 axp->target_pid[axp->pid_count] = t->tgid;
2563 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2564 axp->target_uid[axp->pid_count] = t_uid;
2565 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2566 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2567 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2574 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2575 * @bprm: pointer to the bprm being processed
2576 * @new: the proposed new credentials
2577 * @old: the old credentials
2579 * Simply check if the proc already has the caps given by the file and if not
2580 * store the priv escalation info for later auditing at the end of the syscall
2584 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2585 const struct cred *new, const struct cred *old)
2587 struct audit_aux_data_bprm_fcaps *ax;
2588 struct audit_context *context = current->audit_context;
2589 struct cpu_vfs_cap_data vcaps;
2590 struct dentry *dentry;
2592 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2596 ax->d.type = AUDIT_BPRM_FCAPS;
2597 ax->d.next = context->aux;
2598 context->aux = (void *)ax;
2600 dentry = dget(bprm->file->f_dentry);
2601 get_vfs_caps_from_disk(dentry, &vcaps);
2604 ax->fcap.permitted = vcaps.permitted;
2605 ax->fcap.inheritable = vcaps.inheritable;
2606 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2607 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2609 ax->old_pcap.permitted = old->cap_permitted;
2610 ax->old_pcap.inheritable = old->cap_inheritable;
2611 ax->old_pcap.effective = old->cap_effective;
2613 ax->new_pcap.permitted = new->cap_permitted;
2614 ax->new_pcap.inheritable = new->cap_inheritable;
2615 ax->new_pcap.effective = new->cap_effective;
2620 * __audit_log_capset - store information about the arguments to the capset syscall
2621 * @pid: target pid of the capset call
2622 * @new: the new credentials
2623 * @old: the old (current) credentials
2625 * Record the aguments userspace sent to sys_capset for later printing by the
2626 * audit system if applicable
2628 int __audit_log_capset(pid_t pid,
2629 const struct cred *new, const struct cred *old)
2631 struct audit_aux_data_capset *ax;
2632 struct audit_context *context = current->audit_context;
2634 if (likely(!audit_enabled || !context || context->dummy))
2637 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2641 ax->d.type = AUDIT_CAPSET;
2642 ax->d.next = context->aux;
2643 context->aux = (void *)ax;
2646 ax->cap.effective = new->cap_effective;
2647 ax->cap.inheritable = new->cap_effective;
2648 ax->cap.permitted = new->cap_permitted;
2654 * audit_core_dumps - record information about processes that end abnormally
2655 * @signr: signal value
2657 * If a process ends with a core dump, something fishy is going on and we
2658 * should record the event for investigation.
2660 void audit_core_dumps(long signr)
2662 struct audit_buffer *ab;
2664 uid_t auid = audit_get_loginuid(current), uid;
2666 unsigned int sessionid = audit_get_sessionid(current);
2671 if (signr == SIGQUIT) /* don't care for those */
2674 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2675 current_uid_gid(&uid, &gid);
2676 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2677 auid, uid, gid, sessionid);
2678 security_task_getsecid(current, &sid);
2683 if (security_secid_to_secctx(sid, &ctx, &len))
2684 audit_log_format(ab, " ssid=%u", sid);
2686 audit_log_format(ab, " subj=%s", ctx);
2687 security_release_secctx(ctx, len);
2690 audit_log_format(ab, " pid=%d comm=", current->pid);
2691 audit_log_untrustedstring(ab, current->comm);
2692 audit_log_format(ab, " sig=%ld", signr);