1 /* auditsc.c -- System-call auditing support
2 * Handles all system-call specific auditing features.
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5 * Copyright 2005 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2005, 2006 IBM Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
32 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
35 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
38 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
45 #include <linux/init.h>
46 #include <asm/types.h>
47 #include <linux/atomic.h>
49 #include <linux/namei.h>
51 #include <linux/export.h>
52 #include <linux/slab.h>
53 #include <linux/mount.h>
54 #include <linux/socket.h>
55 #include <linux/mqueue.h>
56 #include <linux/audit.h>
57 #include <linux/personality.h>
58 #include <linux/time.h>
59 #include <linux/netlink.h>
60 #include <linux/compiler.h>
61 #include <asm/unistd.h>
62 #include <linux/security.h>
63 #include <linux/list.h>
64 #include <linux/tty.h>
65 #include <linux/binfmts.h>
66 #include <linux/highmem.h>
67 #include <linux/syscalls.h>
68 #include <linux/capability.h>
69 #include <linux/fs_struct.h>
70 #include <linux/compat.h>
74 /* flags stating the success for a syscall */
75 #define AUDITSC_INVALID 0
76 #define AUDITSC_SUCCESS 1
77 #define AUDITSC_FAILURE 2
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_aux_data {
89 struct audit_aux_data *next;
93 #define AUDIT_AUX_IPCPERM 0
95 /* Number of target pids per aux struct. */
96 #define AUDIT_AUX_PIDS 16
98 struct audit_aux_data_execve {
99 struct audit_aux_data d;
102 struct mm_struct *mm;
105 struct audit_aux_data_pids {
106 struct audit_aux_data d;
107 pid_t target_pid[AUDIT_AUX_PIDS];
108 kuid_t target_auid[AUDIT_AUX_PIDS];
109 kuid_t target_uid[AUDIT_AUX_PIDS];
110 unsigned int target_sessionid[AUDIT_AUX_PIDS];
111 u32 target_sid[AUDIT_AUX_PIDS];
112 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
116 struct audit_aux_data_bprm_fcaps {
117 struct audit_aux_data d;
118 struct audit_cap_data fcap;
119 unsigned int fcap_ver;
120 struct audit_cap_data old_pcap;
121 struct audit_cap_data new_pcap;
124 struct audit_tree_refs {
125 struct audit_tree_refs *next;
126 struct audit_chunk *c[31];
129 static inline int open_arg(int flags, int mask)
131 int n = ACC_MODE(flags);
132 if (flags & (O_TRUNC | O_CREAT))
133 n |= AUDIT_PERM_WRITE;
137 static int audit_match_perm(struct audit_context *ctx, int mask)
144 switch (audit_classify_syscall(ctx->arch, n)) {
146 if ((mask & AUDIT_PERM_WRITE) &&
147 audit_match_class(AUDIT_CLASS_WRITE, n))
149 if ((mask & AUDIT_PERM_READ) &&
150 audit_match_class(AUDIT_CLASS_READ, n))
152 if ((mask & AUDIT_PERM_ATTR) &&
153 audit_match_class(AUDIT_CLASS_CHATTR, n))
156 case 1: /* 32bit on biarch */
157 if ((mask & AUDIT_PERM_WRITE) &&
158 audit_match_class(AUDIT_CLASS_WRITE_32, n))
160 if ((mask & AUDIT_PERM_READ) &&
161 audit_match_class(AUDIT_CLASS_READ_32, n))
163 if ((mask & AUDIT_PERM_ATTR) &&
164 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
168 return mask & ACC_MODE(ctx->argv[1]);
170 return mask & ACC_MODE(ctx->argv[2]);
171 case 4: /* socketcall */
172 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
174 return mask & AUDIT_PERM_EXEC;
180 static int audit_match_filetype(struct audit_context *ctx, int val)
182 struct audit_names *n;
183 umode_t mode = (umode_t)val;
188 list_for_each_entry(n, &ctx->names_list, list) {
189 if ((n->ino != -1) &&
190 ((n->mode & S_IFMT) == mode))
198 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
199 * ->first_trees points to its beginning, ->trees - to the current end of data.
200 * ->tree_count is the number of free entries in array pointed to by ->trees.
201 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
202 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
203 * it's going to remain 1-element for almost any setup) until we free context itself.
204 * References in it _are_ dropped - at the same time we free/drop aux stuff.
207 #ifdef CONFIG_AUDIT_TREE
208 static void audit_set_auditable(struct audit_context *ctx)
212 ctx->current_state = AUDIT_RECORD_CONTEXT;
216 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
218 struct audit_tree_refs *p = ctx->trees;
219 int left = ctx->tree_count;
221 p->c[--left] = chunk;
222 ctx->tree_count = left;
231 ctx->tree_count = 30;
237 static int grow_tree_refs(struct audit_context *ctx)
239 struct audit_tree_refs *p = ctx->trees;
240 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
246 p->next = ctx->trees;
248 ctx->first_trees = ctx->trees;
249 ctx->tree_count = 31;
254 static void unroll_tree_refs(struct audit_context *ctx,
255 struct audit_tree_refs *p, int count)
257 #ifdef CONFIG_AUDIT_TREE
258 struct audit_tree_refs *q;
261 /* we started with empty chain */
262 p = ctx->first_trees;
264 /* if the very first allocation has failed, nothing to do */
269 for (q = p; q != ctx->trees; q = q->next, n = 31) {
271 audit_put_chunk(q->c[n]);
275 while (n-- > ctx->tree_count) {
276 audit_put_chunk(q->c[n]);
280 ctx->tree_count = count;
284 static void free_tree_refs(struct audit_context *ctx)
286 struct audit_tree_refs *p, *q;
287 for (p = ctx->first_trees; p; p = q) {
293 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
295 #ifdef CONFIG_AUDIT_TREE
296 struct audit_tree_refs *p;
301 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
302 for (n = 0; n < 31; n++)
303 if (audit_tree_match(p->c[n], tree))
308 for (n = ctx->tree_count; n < 31; n++)
309 if (audit_tree_match(p->c[n], tree))
316 static int audit_compare_uid(kuid_t uid,
317 struct audit_names *name,
318 struct audit_field *f,
319 struct audit_context *ctx)
321 struct audit_names *n;
325 rc = audit_uid_comparator(uid, f->op, name->uid);
331 list_for_each_entry(n, &ctx->names_list, list) {
332 rc = audit_uid_comparator(uid, f->op, n->uid);
340 static int audit_compare_gid(kgid_t gid,
341 struct audit_names *name,
342 struct audit_field *f,
343 struct audit_context *ctx)
345 struct audit_names *n;
349 rc = audit_gid_comparator(gid, f->op, name->gid);
355 list_for_each_entry(n, &ctx->names_list, list) {
356 rc = audit_gid_comparator(gid, f->op, n->gid);
364 static int audit_field_compare(struct task_struct *tsk,
365 const struct cred *cred,
366 struct audit_field *f,
367 struct audit_context *ctx,
368 struct audit_names *name)
371 /* process to file object comparisons */
372 case AUDIT_COMPARE_UID_TO_OBJ_UID:
373 return audit_compare_uid(cred->uid, name, f, ctx);
374 case AUDIT_COMPARE_GID_TO_OBJ_GID:
375 return audit_compare_gid(cred->gid, name, f, ctx);
376 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
377 return audit_compare_uid(cred->euid, name, f, ctx);
378 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
379 return audit_compare_gid(cred->egid, name, f, ctx);
380 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
381 return audit_compare_uid(tsk->loginuid, name, f, ctx);
382 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
383 return audit_compare_uid(cred->suid, name, f, ctx);
384 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
385 return audit_compare_gid(cred->sgid, name, f, ctx);
386 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
387 return audit_compare_uid(cred->fsuid, name, f, ctx);
388 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
389 return audit_compare_gid(cred->fsgid, name, f, ctx);
390 /* uid comparisons */
391 case AUDIT_COMPARE_UID_TO_AUID:
392 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
393 case AUDIT_COMPARE_UID_TO_EUID:
394 return audit_uid_comparator(cred->uid, f->op, cred->euid);
395 case AUDIT_COMPARE_UID_TO_SUID:
396 return audit_uid_comparator(cred->uid, f->op, cred->suid);
397 case AUDIT_COMPARE_UID_TO_FSUID:
398 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
399 /* auid comparisons */
400 case AUDIT_COMPARE_AUID_TO_EUID:
401 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
402 case AUDIT_COMPARE_AUID_TO_SUID:
403 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
404 case AUDIT_COMPARE_AUID_TO_FSUID:
405 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
406 /* euid comparisons */
407 case AUDIT_COMPARE_EUID_TO_SUID:
408 return audit_uid_comparator(cred->euid, f->op, cred->suid);
409 case AUDIT_COMPARE_EUID_TO_FSUID:
410 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
411 /* suid comparisons */
412 case AUDIT_COMPARE_SUID_TO_FSUID:
413 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
414 /* gid comparisons */
415 case AUDIT_COMPARE_GID_TO_EGID:
416 return audit_gid_comparator(cred->gid, f->op, cred->egid);
417 case AUDIT_COMPARE_GID_TO_SGID:
418 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
419 case AUDIT_COMPARE_GID_TO_FSGID:
420 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
421 /* egid comparisons */
422 case AUDIT_COMPARE_EGID_TO_SGID:
423 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
424 case AUDIT_COMPARE_EGID_TO_FSGID:
425 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
426 /* sgid comparison */
427 case AUDIT_COMPARE_SGID_TO_FSGID:
428 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
430 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
436 /* Determine if any context name data matches a rule's watch data */
437 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
440 * If task_creation is true, this is an explicit indication that we are
441 * filtering a task rule at task creation time. This and tsk == current are
442 * the only situations where tsk->cred may be accessed without an rcu read lock.
444 static int audit_filter_rules(struct task_struct *tsk,
445 struct audit_krule *rule,
446 struct audit_context *ctx,
447 struct audit_names *name,
448 enum audit_state *state,
451 const struct cred *cred;
455 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
457 for (i = 0; i < rule->field_count; i++) {
458 struct audit_field *f = &rule->fields[i];
459 struct audit_names *n;
464 result = audit_comparator(tsk->pid, f->op, f->val);
469 ctx->ppid = sys_getppid();
470 result = audit_comparator(ctx->ppid, f->op, f->val);
474 result = audit_uid_comparator(cred->uid, f->op, f->uid);
477 result = audit_uid_comparator(cred->euid, f->op, f->uid);
480 result = audit_uid_comparator(cred->suid, f->op, f->uid);
483 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
486 result = audit_gid_comparator(cred->gid, f->op, f->gid);
487 if (f->op == Audit_equal) {
489 result = in_group_p(f->gid);
490 } else if (f->op == Audit_not_equal) {
492 result = !in_group_p(f->gid);
496 result = audit_gid_comparator(cred->egid, f->op, f->gid);
497 if (f->op == Audit_equal) {
499 result = in_egroup_p(f->gid);
500 } else if (f->op == Audit_not_equal) {
502 result = !in_egroup_p(f->gid);
506 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
509 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
512 result = audit_comparator(tsk->personality, f->op, f->val);
516 result = audit_comparator(ctx->arch, f->op, f->val);
520 if (ctx && ctx->return_valid)
521 result = audit_comparator(ctx->return_code, f->op, f->val);
524 if (ctx && ctx->return_valid) {
526 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
528 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
533 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
534 audit_comparator(MAJOR(name->rdev), f->op, f->val))
537 list_for_each_entry(n, &ctx->names_list, list) {
538 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
539 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
548 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
549 audit_comparator(MINOR(name->rdev), f->op, f->val))
552 list_for_each_entry(n, &ctx->names_list, list) {
553 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
554 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
563 result = audit_comparator(name->ino, f->op, f->val);
565 list_for_each_entry(n, &ctx->names_list, list) {
566 if (audit_comparator(n->ino, f->op, f->val)) {
575 result = audit_uid_comparator(name->uid, f->op, f->uid);
577 list_for_each_entry(n, &ctx->names_list, list) {
578 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
587 result = audit_gid_comparator(name->gid, f->op, f->gid);
589 list_for_each_entry(n, &ctx->names_list, list) {
590 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
599 result = audit_watch_compare(rule->watch, name->ino, name->dev);
603 result = match_tree_refs(ctx, rule->tree);
608 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
610 case AUDIT_LOGINUID_SET:
611 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
613 case AUDIT_SUBJ_USER:
614 case AUDIT_SUBJ_ROLE:
615 case AUDIT_SUBJ_TYPE:
618 /* NOTE: this may return negative values indicating
619 a temporary error. We simply treat this as a
620 match for now to avoid losing information that
621 may be wanted. An error message will also be
625 security_task_getsecid(tsk, &sid);
628 result = security_audit_rule_match(sid, f->type,
637 case AUDIT_OBJ_LEV_LOW:
638 case AUDIT_OBJ_LEV_HIGH:
639 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
642 /* Find files that match */
644 result = security_audit_rule_match(
645 name->osid, f->type, f->op,
648 list_for_each_entry(n, &ctx->names_list, list) {
649 if (security_audit_rule_match(n->osid, f->type,
657 /* Find ipc objects that match */
658 if (!ctx || ctx->type != AUDIT_IPC)
660 if (security_audit_rule_match(ctx->ipc.osid,
671 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
673 case AUDIT_FILTERKEY:
674 /* ignore this field for filtering */
678 result = audit_match_perm(ctx, f->val);
681 result = audit_match_filetype(ctx, f->val);
683 case AUDIT_FIELD_COMPARE:
684 result = audit_field_compare(tsk, cred, f, ctx, name);
692 if (rule->prio <= ctx->prio)
694 if (rule->filterkey) {
695 kfree(ctx->filterkey);
696 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
698 ctx->prio = rule->prio;
700 switch (rule->action) {
701 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
702 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
707 /* At process creation time, we can determine if system-call auditing is
708 * completely disabled for this task. Since we only have the task
709 * structure at this point, we can only check uid and gid.
711 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
713 struct audit_entry *e;
714 enum audit_state state;
717 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
718 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
720 if (state == AUDIT_RECORD_CONTEXT)
721 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
727 return AUDIT_BUILD_CONTEXT;
730 /* At syscall entry and exit time, this filter is called if the
731 * audit_state is not low enough that auditing cannot take place, but is
732 * also not high enough that we already know we have to write an audit
733 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
735 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
736 struct audit_context *ctx,
737 struct list_head *list)
739 struct audit_entry *e;
740 enum audit_state state;
742 if (audit_pid && tsk->tgid == audit_pid)
743 return AUDIT_DISABLED;
746 if (!list_empty(list)) {
747 int word = AUDIT_WORD(ctx->major);
748 int bit = AUDIT_BIT(ctx->major);
750 list_for_each_entry_rcu(e, list, list) {
751 if ((e->rule.mask[word] & bit) == bit &&
752 audit_filter_rules(tsk, &e->rule, ctx, NULL,
755 ctx->current_state = state;
761 return AUDIT_BUILD_CONTEXT;
765 * Given an audit_name check the inode hash table to see if they match.
766 * Called holding the rcu read lock to protect the use of audit_inode_hash
768 static int audit_filter_inode_name(struct task_struct *tsk,
769 struct audit_names *n,
770 struct audit_context *ctx) {
772 int h = audit_hash_ino((u32)n->ino);
773 struct list_head *list = &audit_inode_hash[h];
774 struct audit_entry *e;
775 enum audit_state state;
777 word = AUDIT_WORD(ctx->major);
778 bit = AUDIT_BIT(ctx->major);
780 if (list_empty(list))
783 list_for_each_entry_rcu(e, list, list) {
784 if ((e->rule.mask[word] & bit) == bit &&
785 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
786 ctx->current_state = state;
794 /* At syscall exit time, this filter is called if any audit_names have been
795 * collected during syscall processing. We only check rules in sublists at hash
796 * buckets applicable to the inode numbers in audit_names.
797 * Regarding audit_state, same rules apply as for audit_filter_syscall().
799 void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
801 struct audit_names *n;
803 if (audit_pid && tsk->tgid == audit_pid)
808 list_for_each_entry(n, &ctx->names_list, list) {
809 if (audit_filter_inode_name(tsk, n, ctx))
815 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
819 struct audit_context *context = tsk->audit_context;
823 context->return_valid = return_valid;
826 * we need to fix up the return code in the audit logs if the actual
827 * return codes are later going to be fixed up by the arch specific
830 * This is actually a test for:
831 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
832 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
834 * but is faster than a bunch of ||
836 if (unlikely(return_code <= -ERESTARTSYS) &&
837 (return_code >= -ERESTART_RESTARTBLOCK) &&
838 (return_code != -ENOIOCTLCMD))
839 context->return_code = -EINTR;
841 context->return_code = return_code;
843 if (context->in_syscall && !context->dummy) {
844 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
845 audit_filter_inodes(tsk, context);
848 tsk->audit_context = NULL;
852 static inline void audit_free_names(struct audit_context *context)
854 struct audit_names *n, *next;
857 if (context->put_count + context->ino_count != context->name_count) {
860 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
861 " name_count=%d put_count=%d"
862 " ino_count=%d [NOT freeing]\n",
864 context->serial, context->major, context->in_syscall,
865 context->name_count, context->put_count,
867 list_for_each_entry(n, &context->names_list, list) {
868 printk(KERN_ERR "names[%d] = %p = %s\n", i++,
869 n->name, n->name->name ?: "(null)");
876 context->put_count = 0;
877 context->ino_count = 0;
880 list_for_each_entry_safe(n, next, &context->names_list, list) {
882 if (n->name && n->name_put)
883 final_putname(n->name);
887 context->name_count = 0;
888 path_put(&context->pwd);
889 context->pwd.dentry = NULL;
890 context->pwd.mnt = NULL;
893 static inline void audit_free_aux(struct audit_context *context)
895 struct audit_aux_data *aux;
897 while ((aux = context->aux)) {
898 context->aux = aux->next;
901 while ((aux = context->aux_pids)) {
902 context->aux_pids = aux->next;
907 static inline struct audit_context *audit_alloc_context(enum audit_state state)
909 struct audit_context *context;
911 context = kzalloc(sizeof(*context), GFP_KERNEL);
914 context->state = state;
915 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
916 INIT_LIST_HEAD(&context->killed_trees);
917 INIT_LIST_HEAD(&context->names_list);
922 * audit_alloc - allocate an audit context block for a task
925 * Filter on the task information and allocate a per-task audit context
926 * if necessary. Doing so turns on system call auditing for the
927 * specified task. This is called from copy_process, so no lock is
930 int audit_alloc(struct task_struct *tsk)
932 struct audit_context *context;
933 enum audit_state state;
936 if (likely(!audit_ever_enabled))
937 return 0; /* Return if not auditing. */
939 state = audit_filter_task(tsk, &key);
940 if (state == AUDIT_DISABLED) {
941 clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
945 if (!(context = audit_alloc_context(state))) {
947 audit_log_lost("out of memory in audit_alloc");
950 context->filterkey = key;
952 tsk->audit_context = context;
953 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
957 static inline void audit_free_context(struct audit_context *context)
959 audit_free_names(context);
960 unroll_tree_refs(context, NULL, 0);
961 free_tree_refs(context);
962 audit_free_aux(context);
963 kfree(context->filterkey);
964 kfree(context->sockaddr);
968 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
969 kuid_t auid, kuid_t uid, unsigned int sessionid,
972 struct audit_buffer *ab;
977 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
981 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
982 from_kuid(&init_user_ns, auid),
983 from_kuid(&init_user_ns, uid), sessionid);
985 if (security_secid_to_secctx(sid, &ctx, &len)) {
986 audit_log_format(ab, " obj=(none)");
989 audit_log_format(ab, " obj=%s", ctx);
990 security_release_secctx(ctx, len);
993 audit_log_format(ab, " ocomm=");
994 audit_log_untrustedstring(ab, comm);
1001 * to_send and len_sent accounting are very loose estimates. We aren't
1002 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1003 * within about 500 bytes (next page boundary)
1005 * why snprintf? an int is up to 12 digits long. if we just assumed when
1006 * logging that a[%d]= was going to be 16 characters long we would be wasting
1007 * space in every audit message. In one 7500 byte message we can log up to
1008 * about 1000 min size arguments. That comes down to about 50% waste of space
1009 * if we didn't do the snprintf to find out how long arg_num_len was.
1011 static int audit_log_single_execve_arg(struct audit_context *context,
1012 struct audit_buffer **ab,
1015 const char __user *p,
1018 char arg_num_len_buf[12];
1019 const char __user *tmp_p = p;
1020 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1021 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
1022 size_t len, len_left, to_send;
1023 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1024 unsigned int i, has_cntl = 0, too_long = 0;
1027 /* strnlen_user includes the null we don't want to send */
1028 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1031 * We just created this mm, if we can't find the strings
1032 * we just copied into it something is _very_ wrong. Similar
1033 * for strings that are too long, we should not have created
1036 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
1038 send_sig(SIGKILL, current, 0);
1042 /* walk the whole argument looking for non-ascii chars */
1044 if (len_left > MAX_EXECVE_AUDIT_LEN)
1045 to_send = MAX_EXECVE_AUDIT_LEN;
1048 ret = copy_from_user(buf, tmp_p, to_send);
1050 * There is no reason for this copy to be short. We just
1051 * copied them here, and the mm hasn't been exposed to user-
1056 send_sig(SIGKILL, current, 0);
1059 buf[to_send] = '\0';
1060 has_cntl = audit_string_contains_control(buf, to_send);
1063 * hex messages get logged as 2 bytes, so we can only
1064 * send half as much in each message
1066 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1069 len_left -= to_send;
1071 } while (len_left > 0);
1075 if (len > max_execve_audit_len)
1078 /* rewalk the argument actually logging the message */
1079 for (i = 0; len_left > 0; i++) {
1082 if (len_left > max_execve_audit_len)
1083 to_send = max_execve_audit_len;
1087 /* do we have space left to send this argument in this ab? */
1088 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1090 room_left -= (to_send * 2);
1092 room_left -= to_send;
1093 if (room_left < 0) {
1096 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1102 * first record needs to say how long the original string was
1103 * so we can be sure nothing was lost.
1105 if ((i == 0) && (too_long))
1106 audit_log_format(*ab, " a%d_len=%zu", arg_num,
1107 has_cntl ? 2*len : len);
1110 * normally arguments are small enough to fit and we already
1111 * filled buf above when we checked for control characters
1112 * so don't bother with another copy_from_user
1114 if (len >= max_execve_audit_len)
1115 ret = copy_from_user(buf, p, to_send);
1120 send_sig(SIGKILL, current, 0);
1123 buf[to_send] = '\0';
1125 /* actually log it */
1126 audit_log_format(*ab, " a%d", arg_num);
1128 audit_log_format(*ab, "[%d]", i);
1129 audit_log_format(*ab, "=");
1131 audit_log_n_hex(*ab, buf, to_send);
1133 audit_log_string(*ab, buf);
1136 len_left -= to_send;
1137 *len_sent += arg_num_len;
1139 *len_sent += to_send * 2;
1141 *len_sent += to_send;
1143 /* include the null we didn't log */
1147 static void audit_log_execve_info(struct audit_context *context,
1148 struct audit_buffer **ab,
1149 struct audit_aux_data_execve *axi)
1152 size_t len_sent = 0;
1153 const char __user *p;
1156 if (axi->mm != current->mm)
1157 return; /* execve failed, no additional info */
1159 p = (const char __user *)axi->mm->arg_start;
1161 audit_log_format(*ab, "argc=%d", axi->argc);
1164 * we need some kernel buffer to hold the userspace args. Just
1165 * allocate one big one rather than allocating one of the right size
1166 * for every single argument inside audit_log_single_execve_arg()
1167 * should be <8k allocation so should be pretty safe.
1169 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1171 audit_panic("out of memory for argv string\n");
1175 for (i = 0; i < axi->argc; i++) {
1176 len = audit_log_single_execve_arg(context, ab, i,
1185 static void show_special(struct audit_context *context, int *call_panic)
1187 struct audit_buffer *ab;
1190 ab = audit_log_start(context, GFP_KERNEL, context->type);
1194 switch (context->type) {
1195 case AUDIT_SOCKETCALL: {
1196 int nargs = context->socketcall.nargs;
1197 audit_log_format(ab, "nargs=%d", nargs);
1198 for (i = 0; i < nargs; i++)
1199 audit_log_format(ab, " a%d=%lx", i,
1200 context->socketcall.args[i]);
1203 u32 osid = context->ipc.osid;
1205 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1206 from_kuid(&init_user_ns, context->ipc.uid),
1207 from_kgid(&init_user_ns, context->ipc.gid),
1212 if (security_secid_to_secctx(osid, &ctx, &len)) {
1213 audit_log_format(ab, " osid=%u", osid);
1216 audit_log_format(ab, " obj=%s", ctx);
1217 security_release_secctx(ctx, len);
1220 if (context->ipc.has_perm) {
1222 ab = audit_log_start(context, GFP_KERNEL,
1223 AUDIT_IPC_SET_PERM);
1226 audit_log_format(ab,
1227 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1228 context->ipc.qbytes,
1229 context->ipc.perm_uid,
1230 context->ipc.perm_gid,
1231 context->ipc.perm_mode);
1234 case AUDIT_MQ_OPEN: {
1235 audit_log_format(ab,
1236 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1237 "mq_msgsize=%ld mq_curmsgs=%ld",
1238 context->mq_open.oflag, context->mq_open.mode,
1239 context->mq_open.attr.mq_flags,
1240 context->mq_open.attr.mq_maxmsg,
1241 context->mq_open.attr.mq_msgsize,
1242 context->mq_open.attr.mq_curmsgs);
1244 case AUDIT_MQ_SENDRECV: {
1245 audit_log_format(ab,
1246 "mqdes=%d msg_len=%zd msg_prio=%u "
1247 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1248 context->mq_sendrecv.mqdes,
1249 context->mq_sendrecv.msg_len,
1250 context->mq_sendrecv.msg_prio,
1251 context->mq_sendrecv.abs_timeout.tv_sec,
1252 context->mq_sendrecv.abs_timeout.tv_nsec);
1254 case AUDIT_MQ_NOTIFY: {
1255 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1256 context->mq_notify.mqdes,
1257 context->mq_notify.sigev_signo);
1259 case AUDIT_MQ_GETSETATTR: {
1260 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1261 audit_log_format(ab,
1262 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1264 context->mq_getsetattr.mqdes,
1265 attr->mq_flags, attr->mq_maxmsg,
1266 attr->mq_msgsize, attr->mq_curmsgs);
1268 case AUDIT_CAPSET: {
1269 audit_log_format(ab, "pid=%d", context->capset.pid);
1270 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1271 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1272 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1275 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1276 context->mmap.flags);
1282 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1284 int i, call_panic = 0;
1285 struct audit_buffer *ab;
1286 struct audit_aux_data *aux;
1287 struct audit_names *n;
1289 /* tsk == current */
1290 context->personality = tsk->personality;
1292 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1294 return; /* audit_panic has been called */
1295 audit_log_format(ab, "arch=%x syscall=%d",
1296 context->arch, context->major);
1297 if (context->personality != PER_LINUX)
1298 audit_log_format(ab, " per=%lx", context->personality);
1299 if (context->return_valid)
1300 audit_log_format(ab, " success=%s exit=%ld",
1301 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1302 context->return_code);
1304 audit_log_format(ab,
1305 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1310 context->name_count);
1312 audit_log_task_info(ab, tsk);
1313 audit_log_key(ab, context->filterkey);
1316 for (aux = context->aux; aux; aux = aux->next) {
1318 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1320 continue; /* audit_panic has been called */
1322 switch (aux->type) {
1324 case AUDIT_EXECVE: {
1325 struct audit_aux_data_execve *axi = (void *)aux;
1326 audit_log_execve_info(context, &ab, axi);
1329 case AUDIT_BPRM_FCAPS: {
1330 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1331 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1332 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1333 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1334 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1335 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1336 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1337 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1338 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1339 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1340 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1348 show_special(context, &call_panic);
1350 if (context->fds[0] >= 0) {
1351 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1353 audit_log_format(ab, "fd0=%d fd1=%d",
1354 context->fds[0], context->fds[1]);
1359 if (context->sockaddr_len) {
1360 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1362 audit_log_format(ab, "saddr=");
1363 audit_log_n_hex(ab, (void *)context->sockaddr,
1364 context->sockaddr_len);
1369 for (aux = context->aux_pids; aux; aux = aux->next) {
1370 struct audit_aux_data_pids *axs = (void *)aux;
1372 for (i = 0; i < axs->pid_count; i++)
1373 if (audit_log_pid_context(context, axs->target_pid[i],
1374 axs->target_auid[i],
1376 axs->target_sessionid[i],
1378 axs->target_comm[i]))
1382 if (context->target_pid &&
1383 audit_log_pid_context(context, context->target_pid,
1384 context->target_auid, context->target_uid,
1385 context->target_sessionid,
1386 context->target_sid, context->target_comm))
1389 if (context->pwd.dentry && context->pwd.mnt) {
1390 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1392 audit_log_d_path(ab, " cwd=", &context->pwd);
1398 list_for_each_entry(n, &context->names_list, list) {
1401 audit_log_name(context, n, NULL, i++, &call_panic);
1404 /* Send end of event record to help user space know we are finished */
1405 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1409 audit_panic("error converting sid to string");
1413 * audit_free - free a per-task audit context
1414 * @tsk: task whose audit context block to free
1416 * Called from copy_process and do_exit
1418 void __audit_free(struct task_struct *tsk)
1420 struct audit_context *context;
1422 context = audit_get_context(tsk, 0, 0);
1426 /* Check for system calls that do not go through the exit
1427 * function (e.g., exit_group), then free context block.
1428 * We use GFP_ATOMIC here because we might be doing this
1429 * in the context of the idle thread */
1430 /* that can happen only if we are called from do_exit() */
1431 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1432 audit_log_exit(context, tsk);
1433 if (!list_empty(&context->killed_trees))
1434 audit_kill_trees(&context->killed_trees);
1436 audit_free_context(context);
1440 * audit_syscall_entry - fill in an audit record at syscall entry
1441 * @arch: architecture type
1442 * @major: major syscall type (function)
1443 * @a1: additional syscall register 1
1444 * @a2: additional syscall register 2
1445 * @a3: additional syscall register 3
1446 * @a4: additional syscall register 4
1448 * Fill in audit context at syscall entry. This only happens if the
1449 * audit context was created when the task was created and the state or
1450 * filters demand the audit context be built. If the state from the
1451 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1452 * then the record will be written at syscall exit time (otherwise, it
1453 * will only be written if another part of the kernel requests that it
1456 void __audit_syscall_entry(int arch, int major,
1457 unsigned long a1, unsigned long a2,
1458 unsigned long a3, unsigned long a4)
1460 struct task_struct *tsk = current;
1461 struct audit_context *context = tsk->audit_context;
1462 enum audit_state state;
1467 BUG_ON(context->in_syscall || context->name_count);
1472 context->arch = arch;
1473 context->major = major;
1474 context->argv[0] = a1;
1475 context->argv[1] = a2;
1476 context->argv[2] = a3;
1477 context->argv[3] = a4;
1479 state = context->state;
1480 context->dummy = !audit_n_rules;
1481 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1483 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1485 if (state == AUDIT_DISABLED)
1488 context->serial = 0;
1489 context->ctime = CURRENT_TIME;
1490 context->in_syscall = 1;
1491 context->current_state = state;
1496 * audit_syscall_exit - deallocate audit context after a system call
1497 * @success: success value of the syscall
1498 * @return_code: return value of the syscall
1500 * Tear down after system call. If the audit context has been marked as
1501 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1502 * filtering, or because some other part of the kernel wrote an audit
1503 * message), then write out the syscall information. In call cases,
1504 * free the names stored from getname().
1506 void __audit_syscall_exit(int success, long return_code)
1508 struct task_struct *tsk = current;
1509 struct audit_context *context;
1512 success = AUDITSC_SUCCESS;
1514 success = AUDITSC_FAILURE;
1516 context = audit_get_context(tsk, success, return_code);
1520 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1521 audit_log_exit(context, tsk);
1523 context->in_syscall = 0;
1524 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
1526 if (!list_empty(&context->killed_trees))
1527 audit_kill_trees(&context->killed_trees);
1529 audit_free_names(context);
1530 unroll_tree_refs(context, NULL, 0);
1531 audit_free_aux(context);
1532 context->aux = NULL;
1533 context->aux_pids = NULL;
1534 context->target_pid = 0;
1535 context->target_sid = 0;
1536 context->sockaddr_len = 0;
1538 context->fds[0] = -1;
1539 if (context->state != AUDIT_RECORD_CONTEXT) {
1540 kfree(context->filterkey);
1541 context->filterkey = NULL;
1543 tsk->audit_context = context;
1546 static inline void handle_one(const struct inode *inode)
1548 #ifdef CONFIG_AUDIT_TREE
1549 struct audit_context *context;
1550 struct audit_tree_refs *p;
1551 struct audit_chunk *chunk;
1553 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
1555 context = current->audit_context;
1557 count = context->tree_count;
1559 chunk = audit_tree_lookup(inode);
1563 if (likely(put_tree_ref(context, chunk)))
1565 if (unlikely(!grow_tree_refs(context))) {
1566 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
1567 audit_set_auditable(context);
1568 audit_put_chunk(chunk);
1569 unroll_tree_refs(context, p, count);
1572 put_tree_ref(context, chunk);
1576 static void handle_path(const struct dentry *dentry)
1578 #ifdef CONFIG_AUDIT_TREE
1579 struct audit_context *context;
1580 struct audit_tree_refs *p;
1581 const struct dentry *d, *parent;
1582 struct audit_chunk *drop;
1586 context = current->audit_context;
1588 count = context->tree_count;
1593 seq = read_seqbegin(&rename_lock);
1595 struct inode *inode = d->d_inode;
1596 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
1597 struct audit_chunk *chunk;
1598 chunk = audit_tree_lookup(inode);
1600 if (unlikely(!put_tree_ref(context, chunk))) {
1606 parent = d->d_parent;
1611 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1614 /* just a race with rename */
1615 unroll_tree_refs(context, p, count);
1618 audit_put_chunk(drop);
1619 if (grow_tree_refs(context)) {
1620 /* OK, got more space */
1621 unroll_tree_refs(context, p, count);
1626 "out of memory, audit has lost a tree reference\n");
1627 unroll_tree_refs(context, p, count);
1628 audit_set_auditable(context);
1635 static struct audit_names *audit_alloc_name(struct audit_context *context,
1638 struct audit_names *aname;
1640 if (context->name_count < AUDIT_NAMES) {
1641 aname = &context->preallocated_names[context->name_count];
1642 memset(aname, 0, sizeof(*aname));
1644 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1647 aname->should_free = true;
1650 aname->ino = (unsigned long)-1;
1652 list_add_tail(&aname->list, &context->names_list);
1654 context->name_count++;
1656 context->ino_count++;
1662 * audit_reusename - fill out filename with info from existing entry
1663 * @uptr: userland ptr to pathname
1665 * Search the audit_names list for the current audit context. If there is an
1666 * existing entry with a matching "uptr" then return the filename
1667 * associated with that audit_name. If not, return NULL.
1670 __audit_reusename(const __user char *uptr)
1672 struct audit_context *context = current->audit_context;
1673 struct audit_names *n;
1675 list_for_each_entry(n, &context->names_list, list) {
1678 if (n->name->uptr == uptr)
1685 * audit_getname - add a name to the list
1686 * @name: name to add
1688 * Add a name to the list of audit names for this context.
1689 * Called from fs/namei.c:getname().
1691 void __audit_getname(struct filename *name)
1693 struct audit_context *context = current->audit_context;
1694 struct audit_names *n;
1696 if (!context->in_syscall) {
1697 #if AUDIT_DEBUG == 2
1698 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1699 __FILE__, __LINE__, context->serial, name);
1706 /* The filename _must_ have a populated ->name */
1707 BUG_ON(!name->name);
1710 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
1715 n->name_len = AUDIT_NAME_FULL;
1719 if (!context->pwd.dentry)
1720 get_fs_pwd(current->fs, &context->pwd);
1723 /* audit_putname - intercept a putname request
1724 * @name: name to intercept and delay for putname
1726 * If we have stored the name from getname in the audit context,
1727 * then we delay the putname until syscall exit.
1728 * Called from include/linux/fs.h:putname().
1730 void audit_putname(struct filename *name)
1732 struct audit_context *context = current->audit_context;
1735 if (!context->in_syscall) {
1736 #if AUDIT_DEBUG == 2
1737 printk(KERN_ERR "%s:%d(:%d): final_putname(%p)\n",
1738 __FILE__, __LINE__, context->serial, name);
1739 if (context->name_count) {
1740 struct audit_names *n;
1743 list_for_each_entry(n, &context->names_list, list)
1744 printk(KERN_ERR "name[%d] = %p = %s\n", i++,
1745 n->name, n->name->name ?: "(null)");
1748 final_putname(name);
1752 ++context->put_count;
1753 if (context->put_count > context->name_count) {
1754 printk(KERN_ERR "%s:%d(:%d): major=%d"
1755 " in_syscall=%d putname(%p) name_count=%d"
1758 context->serial, context->major,
1759 context->in_syscall, name->name,
1760 context->name_count, context->put_count);
1768 * __audit_inode - store the inode and device from a lookup
1769 * @name: name being audited
1770 * @dentry: dentry being audited
1771 * @flags: attributes for this particular entry
1773 void __audit_inode(struct filename *name, const struct dentry *dentry,
1776 struct audit_context *context = current->audit_context;
1777 const struct inode *inode = dentry->d_inode;
1778 struct audit_names *n;
1779 bool parent = flags & AUDIT_INODE_PARENT;
1781 if (!context->in_syscall)
1788 /* The struct filename _must_ have a populated ->name */
1789 BUG_ON(!name->name);
1792 * If we have a pointer to an audit_names entry already, then we can
1793 * just use it directly if the type is correct.
1798 if (n->type == AUDIT_TYPE_PARENT ||
1799 n->type == AUDIT_TYPE_UNKNOWN)
1802 if (n->type != AUDIT_TYPE_PARENT)
1807 list_for_each_entry_reverse(n, &context->names_list, list) {
1808 /* does the name pointer match? */
1809 if (!n->name || n->name->name != name->name)
1812 /* match the correct record type */
1814 if (n->type == AUDIT_TYPE_PARENT ||
1815 n->type == AUDIT_TYPE_UNKNOWN)
1818 if (n->type != AUDIT_TYPE_PARENT)
1824 /* unable to find the name from a previous getname(). Allocate a new
1827 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
1832 n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
1833 n->type = AUDIT_TYPE_PARENT;
1834 if (flags & AUDIT_INODE_HIDDEN)
1837 n->name_len = AUDIT_NAME_FULL;
1838 n->type = AUDIT_TYPE_NORMAL;
1840 handle_path(dentry);
1841 audit_copy_inode(n, dentry, inode);
1845 * __audit_inode_child - collect inode info for created/removed objects
1846 * @parent: inode of dentry parent
1847 * @dentry: dentry being audited
1848 * @type: AUDIT_TYPE_* value that we're looking for
1850 * For syscalls that create or remove filesystem objects, audit_inode
1851 * can only collect information for the filesystem object's parent.
1852 * This call updates the audit context with the child's information.
1853 * Syscalls that create a new filesystem object must be hooked after
1854 * the object is created. Syscalls that remove a filesystem object
1855 * must be hooked prior, in order to capture the target inode during
1856 * unsuccessful attempts.
1858 void __audit_inode_child(const struct inode *parent,
1859 const struct dentry *dentry,
1860 const unsigned char type)
1862 struct audit_context *context = current->audit_context;
1863 const struct inode *inode = dentry->d_inode;
1864 const char *dname = dentry->d_name.name;
1865 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
1867 if (!context->in_syscall)
1873 /* look for a parent entry first */
1874 list_for_each_entry(n, &context->names_list, list) {
1875 if (!n->name || n->type != AUDIT_TYPE_PARENT)
1878 if (n->ino == parent->i_ino &&
1879 !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
1885 /* is there a matching child entry? */
1886 list_for_each_entry(n, &context->names_list, list) {
1887 /* can only match entries that have a name */
1888 if (!n->name || n->type != type)
1891 /* if we found a parent, make sure this one is a child of it */
1892 if (found_parent && (n->name != found_parent->name))
1895 if (!strcmp(dname, n->name->name) ||
1896 !audit_compare_dname_path(dname, n->name->name,
1898 found_parent->name_len :
1905 if (!found_parent) {
1906 /* create a new, "anonymous" parent record */
1907 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
1910 audit_copy_inode(n, NULL, parent);
1914 found_child = audit_alloc_name(context, type);
1918 /* Re-use the name belonging to the slot for a matching parent
1919 * directory. All names for this context are relinquished in
1920 * audit_free_names() */
1922 found_child->name = found_parent->name;
1923 found_child->name_len = AUDIT_NAME_FULL;
1924 /* don't call __putname() */
1925 found_child->name_put = false;
1929 audit_copy_inode(found_child, dentry, inode);
1931 found_child->ino = (unsigned long)-1;
1933 EXPORT_SYMBOL_GPL(__audit_inode_child);
1936 * auditsc_get_stamp - get local copies of audit_context values
1937 * @ctx: audit_context for the task
1938 * @t: timespec to store time recorded in the audit_context
1939 * @serial: serial value that is recorded in the audit_context
1941 * Also sets the context as auditable.
1943 int auditsc_get_stamp(struct audit_context *ctx,
1944 struct timespec *t, unsigned int *serial)
1946 if (!ctx->in_syscall)
1949 ctx->serial = audit_serial();
1950 t->tv_sec = ctx->ctime.tv_sec;
1951 t->tv_nsec = ctx->ctime.tv_nsec;
1952 *serial = ctx->serial;
1955 ctx->current_state = AUDIT_RECORD_CONTEXT;
1960 /* global counter which is incremented every time something logs in */
1961 static atomic_t session_id = ATOMIC_INIT(0);
1963 static int audit_set_loginuid_perm(kuid_t loginuid)
1965 /* if we are unset, we don't need privs */
1966 if (!audit_loginuid_set(current))
1968 /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
1969 if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
1971 /* it is set, you need permission */
1972 if (!capable(CAP_AUDIT_CONTROL))
1974 /* reject if this is not an unset and we don't allow that */
1975 if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
1980 static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
1981 unsigned int oldsessionid, unsigned int sessionid,
1984 struct audit_buffer *ab;
1985 uid_t uid, ologinuid, nloginuid;
1987 uid = from_kuid(&init_user_ns, task_uid(current));
1988 ologinuid = from_kuid(&init_user_ns, koldloginuid);
1989 nloginuid = from_kuid(&init_user_ns, kloginuid),
1991 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1994 audit_log_format(ab, "pid=%d uid=%u old auid=%u new auid=%u old "
1995 "ses=%u new ses=%u res=%d", current->pid, uid, ologinuid,
1996 nloginuid, oldsessionid, sessionid, !rc);
2001 * audit_set_loginuid - set current task's audit_context loginuid
2002 * @loginuid: loginuid value
2006 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2008 int audit_set_loginuid(kuid_t loginuid)
2010 struct task_struct *task = current;
2011 unsigned int sessionid = -1;
2012 kuid_t oldloginuid, oldsessionid;
2015 oldloginuid = audit_get_loginuid(current);
2016 oldsessionid = audit_get_sessionid(current);
2018 rc = audit_set_loginuid_perm(loginuid);
2022 /* are we setting or clearing? */
2023 if (uid_valid(loginuid))
2024 sessionid = atomic_inc_return(&session_id);
2026 task->sessionid = sessionid;
2027 task->loginuid = loginuid;
2029 audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
2034 * __audit_mq_open - record audit data for a POSIX MQ open
2037 * @attr: queue attributes
2040 void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2042 struct audit_context *context = current->audit_context;
2045 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2047 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2049 context->mq_open.oflag = oflag;
2050 context->mq_open.mode = mode;
2052 context->type = AUDIT_MQ_OPEN;
2056 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2057 * @mqdes: MQ descriptor
2058 * @msg_len: Message length
2059 * @msg_prio: Message priority
2060 * @abs_timeout: Message timeout in absolute time
2063 void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2064 const struct timespec *abs_timeout)
2066 struct audit_context *context = current->audit_context;
2067 struct timespec *p = &context->mq_sendrecv.abs_timeout;
2070 memcpy(p, abs_timeout, sizeof(struct timespec));
2072 memset(p, 0, sizeof(struct timespec));
2074 context->mq_sendrecv.mqdes = mqdes;
2075 context->mq_sendrecv.msg_len = msg_len;
2076 context->mq_sendrecv.msg_prio = msg_prio;
2078 context->type = AUDIT_MQ_SENDRECV;
2082 * __audit_mq_notify - record audit data for a POSIX MQ notify
2083 * @mqdes: MQ descriptor
2084 * @notification: Notification event
2088 void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2090 struct audit_context *context = current->audit_context;
2093 context->mq_notify.sigev_signo = notification->sigev_signo;
2095 context->mq_notify.sigev_signo = 0;
2097 context->mq_notify.mqdes = mqdes;
2098 context->type = AUDIT_MQ_NOTIFY;
2102 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2103 * @mqdes: MQ descriptor
2107 void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2109 struct audit_context *context = current->audit_context;
2110 context->mq_getsetattr.mqdes = mqdes;
2111 context->mq_getsetattr.mqstat = *mqstat;
2112 context->type = AUDIT_MQ_GETSETATTR;
2116 * audit_ipc_obj - record audit data for ipc object
2117 * @ipcp: ipc permissions
2120 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2122 struct audit_context *context = current->audit_context;
2123 context->ipc.uid = ipcp->uid;
2124 context->ipc.gid = ipcp->gid;
2125 context->ipc.mode = ipcp->mode;
2126 context->ipc.has_perm = 0;
2127 security_ipc_getsecid(ipcp, &context->ipc.osid);
2128 context->type = AUDIT_IPC;
2132 * audit_ipc_set_perm - record audit data for new ipc permissions
2133 * @qbytes: msgq bytes
2134 * @uid: msgq user id
2135 * @gid: msgq group id
2136 * @mode: msgq mode (permissions)
2138 * Called only after audit_ipc_obj().
2140 void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2142 struct audit_context *context = current->audit_context;
2144 context->ipc.qbytes = qbytes;
2145 context->ipc.perm_uid = uid;
2146 context->ipc.perm_gid = gid;
2147 context->ipc.perm_mode = mode;
2148 context->ipc.has_perm = 1;
2151 int __audit_bprm(struct linux_binprm *bprm)
2153 struct audit_aux_data_execve *ax;
2154 struct audit_context *context = current->audit_context;
2156 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2160 ax->argc = bprm->argc;
2161 ax->envc = bprm->envc;
2163 ax->d.type = AUDIT_EXECVE;
2164 ax->d.next = context->aux;
2165 context->aux = (void *)ax;
2171 * audit_socketcall - record audit data for sys_socketcall
2172 * @nargs: number of args, which should not be more than AUDITSC_ARGS.
2176 int __audit_socketcall(int nargs, unsigned long *args)
2178 struct audit_context *context = current->audit_context;
2180 if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2182 context->type = AUDIT_SOCKETCALL;
2183 context->socketcall.nargs = nargs;
2184 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2189 * __audit_fd_pair - record audit data for pipe and socketpair
2190 * @fd1: the first file descriptor
2191 * @fd2: the second file descriptor
2194 void __audit_fd_pair(int fd1, int fd2)
2196 struct audit_context *context = current->audit_context;
2197 context->fds[0] = fd1;
2198 context->fds[1] = fd2;
2202 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2203 * @len: data length in user space
2204 * @a: data address in kernel space
2206 * Returns 0 for success or NULL context or < 0 on error.
2208 int __audit_sockaddr(int len, void *a)
2210 struct audit_context *context = current->audit_context;
2212 if (!context->sockaddr) {
2213 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2216 context->sockaddr = p;
2219 context->sockaddr_len = len;
2220 memcpy(context->sockaddr, a, len);
2224 void __audit_ptrace(struct task_struct *t)
2226 struct audit_context *context = current->audit_context;
2228 context->target_pid = t->pid;
2229 context->target_auid = audit_get_loginuid(t);
2230 context->target_uid = task_uid(t);
2231 context->target_sessionid = audit_get_sessionid(t);
2232 security_task_getsecid(t, &context->target_sid);
2233 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2237 * audit_signal_info - record signal info for shutting down audit subsystem
2238 * @sig: signal value
2239 * @t: task being signaled
2241 * If the audit subsystem is being terminated, record the task (pid)
2242 * and uid that is doing that.
2244 int __audit_signal_info(int sig, struct task_struct *t)
2246 struct audit_aux_data_pids *axp;
2247 struct task_struct *tsk = current;
2248 struct audit_context *ctx = tsk->audit_context;
2249 kuid_t uid = current_uid(), t_uid = task_uid(t);
2251 if (audit_pid && t->tgid == audit_pid) {
2252 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2253 audit_sig_pid = tsk->pid;
2254 if (uid_valid(tsk->loginuid))
2255 audit_sig_uid = tsk->loginuid;
2257 audit_sig_uid = uid;
2258 security_task_getsecid(tsk, &audit_sig_sid);
2260 if (!audit_signals || audit_dummy_context())
2264 /* optimize the common case by putting first signal recipient directly
2265 * in audit_context */
2266 if (!ctx->target_pid) {
2267 ctx->target_pid = t->tgid;
2268 ctx->target_auid = audit_get_loginuid(t);
2269 ctx->target_uid = t_uid;
2270 ctx->target_sessionid = audit_get_sessionid(t);
2271 security_task_getsecid(t, &ctx->target_sid);
2272 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2276 axp = (void *)ctx->aux_pids;
2277 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2278 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2282 axp->d.type = AUDIT_OBJ_PID;
2283 axp->d.next = ctx->aux_pids;
2284 ctx->aux_pids = (void *)axp;
2286 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2288 axp->target_pid[axp->pid_count] = t->tgid;
2289 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2290 axp->target_uid[axp->pid_count] = t_uid;
2291 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2292 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2293 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2300 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2301 * @bprm: pointer to the bprm being processed
2302 * @new: the proposed new credentials
2303 * @old: the old credentials
2305 * Simply check if the proc already has the caps given by the file and if not
2306 * store the priv escalation info for later auditing at the end of the syscall
2310 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2311 const struct cred *new, const struct cred *old)
2313 struct audit_aux_data_bprm_fcaps *ax;
2314 struct audit_context *context = current->audit_context;
2315 struct cpu_vfs_cap_data vcaps;
2316 struct dentry *dentry;
2318 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2322 ax->d.type = AUDIT_BPRM_FCAPS;
2323 ax->d.next = context->aux;
2324 context->aux = (void *)ax;
2326 dentry = dget(bprm->file->f_dentry);
2327 get_vfs_caps_from_disk(dentry, &vcaps);
2330 ax->fcap.permitted = vcaps.permitted;
2331 ax->fcap.inheritable = vcaps.inheritable;
2332 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2333 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2335 ax->old_pcap.permitted = old->cap_permitted;
2336 ax->old_pcap.inheritable = old->cap_inheritable;
2337 ax->old_pcap.effective = old->cap_effective;
2339 ax->new_pcap.permitted = new->cap_permitted;
2340 ax->new_pcap.inheritable = new->cap_inheritable;
2341 ax->new_pcap.effective = new->cap_effective;
2346 * __audit_log_capset - store information about the arguments to the capset syscall
2347 * @pid: target pid of the capset call
2348 * @new: the new credentials
2349 * @old: the old (current) credentials
2351 * Record the aguments userspace sent to sys_capset for later printing by the
2352 * audit system if applicable
2354 void __audit_log_capset(pid_t pid,
2355 const struct cred *new, const struct cred *old)
2357 struct audit_context *context = current->audit_context;
2358 context->capset.pid = pid;
2359 context->capset.cap.effective = new->cap_effective;
2360 context->capset.cap.inheritable = new->cap_effective;
2361 context->capset.cap.permitted = new->cap_permitted;
2362 context->type = AUDIT_CAPSET;
2365 void __audit_mmap_fd(int fd, int flags)
2367 struct audit_context *context = current->audit_context;
2368 context->mmap.fd = fd;
2369 context->mmap.flags = flags;
2370 context->type = AUDIT_MMAP;
2373 static void audit_log_task(struct audit_buffer *ab)
2377 unsigned int sessionid;
2379 auid = audit_get_loginuid(current);
2380 sessionid = audit_get_sessionid(current);
2381 current_uid_gid(&uid, &gid);
2383 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2384 from_kuid(&init_user_ns, auid),
2385 from_kuid(&init_user_ns, uid),
2386 from_kgid(&init_user_ns, gid),
2388 audit_log_task_context(ab);
2389 audit_log_format(ab, " pid=%d comm=", current->pid);
2390 audit_log_untrustedstring(ab, current->comm);
2393 static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
2396 audit_log_format(ab, " reason=");
2397 audit_log_string(ab, reason);
2398 audit_log_format(ab, " sig=%ld", signr);
2401 * audit_core_dumps - record information about processes that end abnormally
2402 * @signr: signal value
2404 * If a process ends with a core dump, something fishy is going on and we
2405 * should record the event for investigation.
2407 void audit_core_dumps(long signr)
2409 struct audit_buffer *ab;
2414 if (signr == SIGQUIT) /* don't care for those */
2417 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2420 audit_log_abend(ab, "memory violation", signr);
2424 void __audit_seccomp(unsigned long syscall, long signr, int code)
2426 struct audit_buffer *ab;
2428 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
2432 audit_log_format(ab, " sig=%ld", signr);
2433 audit_log_format(ab, " syscall=%ld", syscall);
2434 audit_log_format(ab, " compat=%d", is_compat_task());
2435 audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
2436 audit_log_format(ab, " code=0x%x", code);
2440 struct list_head *audit_killed_trees(void)
2442 struct audit_context *ctx = current->audit_context;
2443 if (likely(!ctx || !ctx->in_syscall))
2445 return &ctx->killed_trees;