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_pids {
99 struct audit_aux_data d;
100 pid_t target_pid[AUDIT_AUX_PIDS];
101 kuid_t target_auid[AUDIT_AUX_PIDS];
102 kuid_t target_uid[AUDIT_AUX_PIDS];
103 unsigned int target_sessionid[AUDIT_AUX_PIDS];
104 u32 target_sid[AUDIT_AUX_PIDS];
105 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
109 struct audit_aux_data_bprm_fcaps {
110 struct audit_aux_data d;
111 struct audit_cap_data fcap;
112 unsigned int fcap_ver;
113 struct audit_cap_data old_pcap;
114 struct audit_cap_data new_pcap;
117 struct audit_tree_refs {
118 struct audit_tree_refs *next;
119 struct audit_chunk *c[31];
122 static inline int open_arg(int flags, int mask)
124 int n = ACC_MODE(flags);
125 if (flags & (O_TRUNC | O_CREAT))
126 n |= AUDIT_PERM_WRITE;
130 static int audit_match_perm(struct audit_context *ctx, int mask)
137 switch (audit_classify_syscall(ctx->arch, n)) {
139 if ((mask & AUDIT_PERM_WRITE) &&
140 audit_match_class(AUDIT_CLASS_WRITE, n))
142 if ((mask & AUDIT_PERM_READ) &&
143 audit_match_class(AUDIT_CLASS_READ, n))
145 if ((mask & AUDIT_PERM_ATTR) &&
146 audit_match_class(AUDIT_CLASS_CHATTR, n))
149 case 1: /* 32bit on biarch */
150 if ((mask & AUDIT_PERM_WRITE) &&
151 audit_match_class(AUDIT_CLASS_WRITE_32, n))
153 if ((mask & AUDIT_PERM_READ) &&
154 audit_match_class(AUDIT_CLASS_READ_32, n))
156 if ((mask & AUDIT_PERM_ATTR) &&
157 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
161 return mask & ACC_MODE(ctx->argv[1]);
163 return mask & ACC_MODE(ctx->argv[2]);
164 case 4: /* socketcall */
165 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
167 return mask & AUDIT_PERM_EXEC;
173 static int audit_match_filetype(struct audit_context *ctx, int val)
175 struct audit_names *n;
176 umode_t mode = (umode_t)val;
181 list_for_each_entry(n, &ctx->names_list, list) {
182 if ((n->ino != -1) &&
183 ((n->mode & S_IFMT) == mode))
191 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
192 * ->first_trees points to its beginning, ->trees - to the current end of data.
193 * ->tree_count is the number of free entries in array pointed to by ->trees.
194 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
195 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
196 * it's going to remain 1-element for almost any setup) until we free context itself.
197 * References in it _are_ dropped - at the same time we free/drop aux stuff.
200 #ifdef CONFIG_AUDIT_TREE
201 static void audit_set_auditable(struct audit_context *ctx)
205 ctx->current_state = AUDIT_RECORD_CONTEXT;
209 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
211 struct audit_tree_refs *p = ctx->trees;
212 int left = ctx->tree_count;
214 p->c[--left] = chunk;
215 ctx->tree_count = left;
224 ctx->tree_count = 30;
230 static int grow_tree_refs(struct audit_context *ctx)
232 struct audit_tree_refs *p = ctx->trees;
233 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
239 p->next = ctx->trees;
241 ctx->first_trees = ctx->trees;
242 ctx->tree_count = 31;
247 static void unroll_tree_refs(struct audit_context *ctx,
248 struct audit_tree_refs *p, int count)
250 #ifdef CONFIG_AUDIT_TREE
251 struct audit_tree_refs *q;
254 /* we started with empty chain */
255 p = ctx->first_trees;
257 /* if the very first allocation has failed, nothing to do */
262 for (q = p; q != ctx->trees; q = q->next, n = 31) {
264 audit_put_chunk(q->c[n]);
268 while (n-- > ctx->tree_count) {
269 audit_put_chunk(q->c[n]);
273 ctx->tree_count = count;
277 static void free_tree_refs(struct audit_context *ctx)
279 struct audit_tree_refs *p, *q;
280 for (p = ctx->first_trees; p; p = q) {
286 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
288 #ifdef CONFIG_AUDIT_TREE
289 struct audit_tree_refs *p;
294 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
295 for (n = 0; n < 31; n++)
296 if (audit_tree_match(p->c[n], tree))
301 for (n = ctx->tree_count; n < 31; n++)
302 if (audit_tree_match(p->c[n], tree))
309 static int audit_compare_uid(kuid_t uid,
310 struct audit_names *name,
311 struct audit_field *f,
312 struct audit_context *ctx)
314 struct audit_names *n;
318 rc = audit_uid_comparator(uid, f->op, name->uid);
324 list_for_each_entry(n, &ctx->names_list, list) {
325 rc = audit_uid_comparator(uid, f->op, n->uid);
333 static int audit_compare_gid(kgid_t gid,
334 struct audit_names *name,
335 struct audit_field *f,
336 struct audit_context *ctx)
338 struct audit_names *n;
342 rc = audit_gid_comparator(gid, f->op, name->gid);
348 list_for_each_entry(n, &ctx->names_list, list) {
349 rc = audit_gid_comparator(gid, f->op, n->gid);
357 static int audit_field_compare(struct task_struct *tsk,
358 const struct cred *cred,
359 struct audit_field *f,
360 struct audit_context *ctx,
361 struct audit_names *name)
364 /* process to file object comparisons */
365 case AUDIT_COMPARE_UID_TO_OBJ_UID:
366 return audit_compare_uid(cred->uid, name, f, ctx);
367 case AUDIT_COMPARE_GID_TO_OBJ_GID:
368 return audit_compare_gid(cred->gid, name, f, ctx);
369 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
370 return audit_compare_uid(cred->euid, name, f, ctx);
371 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
372 return audit_compare_gid(cred->egid, name, f, ctx);
373 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
374 return audit_compare_uid(tsk->loginuid, name, f, ctx);
375 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
376 return audit_compare_uid(cred->suid, name, f, ctx);
377 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
378 return audit_compare_gid(cred->sgid, name, f, ctx);
379 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
380 return audit_compare_uid(cred->fsuid, name, f, ctx);
381 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
382 return audit_compare_gid(cred->fsgid, name, f, ctx);
383 /* uid comparisons */
384 case AUDIT_COMPARE_UID_TO_AUID:
385 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
386 case AUDIT_COMPARE_UID_TO_EUID:
387 return audit_uid_comparator(cred->uid, f->op, cred->euid);
388 case AUDIT_COMPARE_UID_TO_SUID:
389 return audit_uid_comparator(cred->uid, f->op, cred->suid);
390 case AUDIT_COMPARE_UID_TO_FSUID:
391 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
392 /* auid comparisons */
393 case AUDIT_COMPARE_AUID_TO_EUID:
394 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
395 case AUDIT_COMPARE_AUID_TO_SUID:
396 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
397 case AUDIT_COMPARE_AUID_TO_FSUID:
398 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
399 /* euid comparisons */
400 case AUDIT_COMPARE_EUID_TO_SUID:
401 return audit_uid_comparator(cred->euid, f->op, cred->suid);
402 case AUDIT_COMPARE_EUID_TO_FSUID:
403 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
404 /* suid comparisons */
405 case AUDIT_COMPARE_SUID_TO_FSUID:
406 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
407 /* gid comparisons */
408 case AUDIT_COMPARE_GID_TO_EGID:
409 return audit_gid_comparator(cred->gid, f->op, cred->egid);
410 case AUDIT_COMPARE_GID_TO_SGID:
411 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
412 case AUDIT_COMPARE_GID_TO_FSGID:
413 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
414 /* egid comparisons */
415 case AUDIT_COMPARE_EGID_TO_SGID:
416 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
417 case AUDIT_COMPARE_EGID_TO_FSGID:
418 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
419 /* sgid comparison */
420 case AUDIT_COMPARE_SGID_TO_FSGID:
421 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
423 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
429 /* Determine if any context name data matches a rule's watch data */
430 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
433 * If task_creation is true, this is an explicit indication that we are
434 * filtering a task rule at task creation time. This and tsk == current are
435 * the only situations where tsk->cred may be accessed without an rcu read lock.
437 static int audit_filter_rules(struct task_struct *tsk,
438 struct audit_krule *rule,
439 struct audit_context *ctx,
440 struct audit_names *name,
441 enum audit_state *state,
444 const struct cred *cred;
448 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
450 for (i = 0; i < rule->field_count; i++) {
451 struct audit_field *f = &rule->fields[i];
452 struct audit_names *n;
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_uid_comparator(cred->uid, f->op, f->uid);
470 result = audit_uid_comparator(cred->euid, f->op, f->uid);
473 result = audit_uid_comparator(cred->suid, f->op, f->uid);
476 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
479 result = audit_gid_comparator(cred->gid, f->op, f->gid);
480 if (f->op == Audit_equal) {
482 result = in_group_p(f->gid);
483 } else if (f->op == Audit_not_equal) {
485 result = !in_group_p(f->gid);
489 result = audit_gid_comparator(cred->egid, f->op, f->gid);
490 if (f->op == Audit_equal) {
492 result = in_egroup_p(f->gid);
493 } else if (f->op == Audit_not_equal) {
495 result = !in_egroup_p(f->gid);
499 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
502 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
505 result = audit_comparator(tsk->personality, f->op, f->val);
509 result = audit_comparator(ctx->arch, f->op, f->val);
513 if (ctx && ctx->return_valid)
514 result = audit_comparator(ctx->return_code, f->op, f->val);
517 if (ctx && ctx->return_valid) {
519 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
521 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
526 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
527 audit_comparator(MAJOR(name->rdev), f->op, f->val))
530 list_for_each_entry(n, &ctx->names_list, list) {
531 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
532 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
541 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
542 audit_comparator(MINOR(name->rdev), f->op, f->val))
545 list_for_each_entry(n, &ctx->names_list, list) {
546 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
547 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
556 result = audit_comparator(name->ino, f->op, f->val);
558 list_for_each_entry(n, &ctx->names_list, list) {
559 if (audit_comparator(n->ino, f->op, f->val)) {
568 result = audit_uid_comparator(name->uid, f->op, f->uid);
570 list_for_each_entry(n, &ctx->names_list, list) {
571 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
580 result = audit_gid_comparator(name->gid, f->op, f->gid);
582 list_for_each_entry(n, &ctx->names_list, list) {
583 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
592 result = audit_watch_compare(rule->watch, name->ino, name->dev);
596 result = match_tree_refs(ctx, rule->tree);
601 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
603 case AUDIT_LOGINUID_SET:
604 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
606 case AUDIT_SUBJ_USER:
607 case AUDIT_SUBJ_ROLE:
608 case AUDIT_SUBJ_TYPE:
611 /* NOTE: this may return negative values indicating
612 a temporary error. We simply treat this as a
613 match for now to avoid losing information that
614 may be wanted. An error message will also be
618 security_task_getsecid(tsk, &sid);
621 result = security_audit_rule_match(sid, f->type,
630 case AUDIT_OBJ_LEV_LOW:
631 case AUDIT_OBJ_LEV_HIGH:
632 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
635 /* Find files that match */
637 result = security_audit_rule_match(
638 name->osid, f->type, f->op,
641 list_for_each_entry(n, &ctx->names_list, list) {
642 if (security_audit_rule_match(n->osid, f->type,
650 /* Find ipc objects that match */
651 if (!ctx || ctx->type != AUDIT_IPC)
653 if (security_audit_rule_match(ctx->ipc.osid,
664 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
666 case AUDIT_FILTERKEY:
667 /* ignore this field for filtering */
671 result = audit_match_perm(ctx, f->val);
674 result = audit_match_filetype(ctx, f->val);
676 case AUDIT_FIELD_COMPARE:
677 result = audit_field_compare(tsk, cred, f, ctx, name);
685 if (rule->prio <= ctx->prio)
687 if (rule->filterkey) {
688 kfree(ctx->filterkey);
689 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
691 ctx->prio = rule->prio;
693 switch (rule->action) {
694 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
695 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
700 /* At process creation time, we can determine if system-call auditing is
701 * completely disabled for this task. Since we only have the task
702 * structure at this point, we can only check uid and gid.
704 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
706 struct audit_entry *e;
707 enum audit_state state;
710 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
711 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
713 if (state == AUDIT_RECORD_CONTEXT)
714 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
720 return AUDIT_BUILD_CONTEXT;
723 /* At syscall entry and exit time, this filter is called if the
724 * audit_state is not low enough that auditing cannot take place, but is
725 * also not high enough that we already know we have to write an audit
726 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
728 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
729 struct audit_context *ctx,
730 struct list_head *list)
732 struct audit_entry *e;
733 enum audit_state state;
735 if (audit_pid && tsk->tgid == audit_pid)
736 return AUDIT_DISABLED;
739 if (!list_empty(list)) {
740 int word = AUDIT_WORD(ctx->major);
741 int bit = AUDIT_BIT(ctx->major);
743 list_for_each_entry_rcu(e, list, list) {
744 if ((e->rule.mask[word] & bit) == bit &&
745 audit_filter_rules(tsk, &e->rule, ctx, NULL,
748 ctx->current_state = state;
754 return AUDIT_BUILD_CONTEXT;
758 * Given an audit_name check the inode hash table to see if they match.
759 * Called holding the rcu read lock to protect the use of audit_inode_hash
761 static int audit_filter_inode_name(struct task_struct *tsk,
762 struct audit_names *n,
763 struct audit_context *ctx) {
765 int h = audit_hash_ino((u32)n->ino);
766 struct list_head *list = &audit_inode_hash[h];
767 struct audit_entry *e;
768 enum audit_state state;
770 word = AUDIT_WORD(ctx->major);
771 bit = AUDIT_BIT(ctx->major);
773 if (list_empty(list))
776 list_for_each_entry_rcu(e, list, list) {
777 if ((e->rule.mask[word] & bit) == bit &&
778 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
779 ctx->current_state = state;
787 /* At syscall exit time, this filter is called if any audit_names have been
788 * collected during syscall processing. We only check rules in sublists at hash
789 * buckets applicable to the inode numbers in audit_names.
790 * Regarding audit_state, same rules apply as for audit_filter_syscall().
792 void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
794 struct audit_names *n;
796 if (audit_pid && tsk->tgid == audit_pid)
801 list_for_each_entry(n, &ctx->names_list, list) {
802 if (audit_filter_inode_name(tsk, n, ctx))
808 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
812 struct audit_context *context = tsk->audit_context;
816 context->return_valid = return_valid;
819 * we need to fix up the return code in the audit logs if the actual
820 * return codes are later going to be fixed up by the arch specific
823 * This is actually a test for:
824 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
825 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
827 * but is faster than a bunch of ||
829 if (unlikely(return_code <= -ERESTARTSYS) &&
830 (return_code >= -ERESTART_RESTARTBLOCK) &&
831 (return_code != -ENOIOCTLCMD))
832 context->return_code = -EINTR;
834 context->return_code = return_code;
836 if (context->in_syscall && !context->dummy) {
837 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
838 audit_filter_inodes(tsk, context);
841 tsk->audit_context = NULL;
845 static inline void audit_free_names(struct audit_context *context)
847 struct audit_names *n, *next;
850 if (context->put_count + context->ino_count != context->name_count) {
853 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
854 " name_count=%d put_count=%d"
855 " ino_count=%d [NOT freeing]\n",
857 context->serial, context->major, context->in_syscall,
858 context->name_count, context->put_count,
860 list_for_each_entry(n, &context->names_list, list) {
861 printk(KERN_ERR "names[%d] = %p = %s\n", i++,
862 n->name, n->name->name ?: "(null)");
869 context->put_count = 0;
870 context->ino_count = 0;
873 list_for_each_entry_safe(n, next, &context->names_list, list) {
875 if (n->name && n->name_put)
876 final_putname(n->name);
880 context->name_count = 0;
881 path_put(&context->pwd);
882 context->pwd.dentry = NULL;
883 context->pwd.mnt = NULL;
886 static inline void audit_free_aux(struct audit_context *context)
888 struct audit_aux_data *aux;
890 while ((aux = context->aux)) {
891 context->aux = aux->next;
894 while ((aux = context->aux_pids)) {
895 context->aux_pids = aux->next;
900 static inline struct audit_context *audit_alloc_context(enum audit_state state)
902 struct audit_context *context;
904 context = kzalloc(sizeof(*context), GFP_KERNEL);
907 context->state = state;
908 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
909 INIT_LIST_HEAD(&context->killed_trees);
910 INIT_LIST_HEAD(&context->names_list);
915 * audit_alloc - allocate an audit context block for a task
918 * Filter on the task information and allocate a per-task audit context
919 * if necessary. Doing so turns on system call auditing for the
920 * specified task. This is called from copy_process, so no lock is
923 int audit_alloc(struct task_struct *tsk)
925 struct audit_context *context;
926 enum audit_state state;
929 if (likely(!audit_ever_enabled))
930 return 0; /* Return if not auditing. */
932 state = audit_filter_task(tsk, &key);
933 if (state == AUDIT_DISABLED) {
934 clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
938 if (!(context = audit_alloc_context(state))) {
940 audit_log_lost("out of memory in audit_alloc");
943 context->filterkey = key;
945 tsk->audit_context = context;
946 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
950 static inline void audit_free_context(struct audit_context *context)
952 audit_free_names(context);
953 unroll_tree_refs(context, NULL, 0);
954 free_tree_refs(context);
955 audit_free_aux(context);
956 kfree(context->filterkey);
957 kfree(context->sockaddr);
961 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
962 kuid_t auid, kuid_t uid, unsigned int sessionid,
965 struct audit_buffer *ab;
970 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
974 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
975 from_kuid(&init_user_ns, auid),
976 from_kuid(&init_user_ns, uid), sessionid);
978 if (security_secid_to_secctx(sid, &ctx, &len)) {
979 audit_log_format(ab, " obj=(none)");
982 audit_log_format(ab, " obj=%s", ctx);
983 security_release_secctx(ctx, len);
986 audit_log_format(ab, " ocomm=");
987 audit_log_untrustedstring(ab, comm);
994 * to_send and len_sent accounting are very loose estimates. We aren't
995 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
996 * within about 500 bytes (next page boundary)
998 * why snprintf? an int is up to 12 digits long. if we just assumed when
999 * logging that a[%d]= was going to be 16 characters long we would be wasting
1000 * space in every audit message. In one 7500 byte message we can log up to
1001 * about 1000 min size arguments. That comes down to about 50% waste of space
1002 * if we didn't do the snprintf to find out how long arg_num_len was.
1004 static int audit_log_single_execve_arg(struct audit_context *context,
1005 struct audit_buffer **ab,
1008 const char __user *p,
1011 char arg_num_len_buf[12];
1012 const char __user *tmp_p = p;
1013 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1014 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
1015 size_t len, len_left, to_send;
1016 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1017 unsigned int i, has_cntl = 0, too_long = 0;
1020 /* strnlen_user includes the null we don't want to send */
1021 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1024 * We just created this mm, if we can't find the strings
1025 * we just copied into it something is _very_ wrong. Similar
1026 * for strings that are too long, we should not have created
1029 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
1031 send_sig(SIGKILL, current, 0);
1035 /* walk the whole argument looking for non-ascii chars */
1037 if (len_left > MAX_EXECVE_AUDIT_LEN)
1038 to_send = MAX_EXECVE_AUDIT_LEN;
1041 ret = copy_from_user(buf, tmp_p, to_send);
1043 * There is no reason for this copy to be short. We just
1044 * copied them here, and the mm hasn't been exposed to user-
1049 send_sig(SIGKILL, current, 0);
1052 buf[to_send] = '\0';
1053 has_cntl = audit_string_contains_control(buf, to_send);
1056 * hex messages get logged as 2 bytes, so we can only
1057 * send half as much in each message
1059 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1062 len_left -= to_send;
1064 } while (len_left > 0);
1068 if (len > max_execve_audit_len)
1071 /* rewalk the argument actually logging the message */
1072 for (i = 0; len_left > 0; i++) {
1075 if (len_left > max_execve_audit_len)
1076 to_send = max_execve_audit_len;
1080 /* do we have space left to send this argument in this ab? */
1081 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1083 room_left -= (to_send * 2);
1085 room_left -= to_send;
1086 if (room_left < 0) {
1089 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1095 * first record needs to say how long the original string was
1096 * so we can be sure nothing was lost.
1098 if ((i == 0) && (too_long))
1099 audit_log_format(*ab, " a%d_len=%zu", arg_num,
1100 has_cntl ? 2*len : len);
1103 * normally arguments are small enough to fit and we already
1104 * filled buf above when we checked for control characters
1105 * so don't bother with another copy_from_user
1107 if (len >= max_execve_audit_len)
1108 ret = copy_from_user(buf, p, to_send);
1113 send_sig(SIGKILL, current, 0);
1116 buf[to_send] = '\0';
1118 /* actually log it */
1119 audit_log_format(*ab, " a%d", arg_num);
1121 audit_log_format(*ab, "[%d]", i);
1122 audit_log_format(*ab, "=");
1124 audit_log_n_hex(*ab, buf, to_send);
1126 audit_log_string(*ab, buf);
1129 len_left -= to_send;
1130 *len_sent += arg_num_len;
1132 *len_sent += to_send * 2;
1134 *len_sent += to_send;
1136 /* include the null we didn't log */
1140 static void audit_log_execve_info(struct audit_context *context,
1141 struct audit_buffer **ab)
1144 size_t len_sent = 0;
1145 const char __user *p;
1148 if (context->execve.mm != current->mm)
1149 return; /* execve failed, no additional info */
1151 p = (const char __user *)current->mm->arg_start;
1153 audit_log_format(*ab, "argc=%d", context->execve.argc);
1156 * we need some kernel buffer to hold the userspace args. Just
1157 * allocate one big one rather than allocating one of the right size
1158 * for every single argument inside audit_log_single_execve_arg()
1159 * should be <8k allocation so should be pretty safe.
1161 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1163 audit_panic("out of memory for argv string\n");
1167 for (i = 0; i < context->execve.argc; i++) {
1168 len = audit_log_single_execve_arg(context, ab, i,
1177 static void show_special(struct audit_context *context, int *call_panic)
1179 struct audit_buffer *ab;
1182 ab = audit_log_start(context, GFP_KERNEL, context->type);
1186 switch (context->type) {
1187 case AUDIT_SOCKETCALL: {
1188 int nargs = context->socketcall.nargs;
1189 audit_log_format(ab, "nargs=%d", nargs);
1190 for (i = 0; i < nargs; i++)
1191 audit_log_format(ab, " a%d=%lx", i,
1192 context->socketcall.args[i]);
1195 u32 osid = context->ipc.osid;
1197 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1198 from_kuid(&init_user_ns, context->ipc.uid),
1199 from_kgid(&init_user_ns, context->ipc.gid),
1204 if (security_secid_to_secctx(osid, &ctx, &len)) {
1205 audit_log_format(ab, " osid=%u", osid);
1208 audit_log_format(ab, " obj=%s", ctx);
1209 security_release_secctx(ctx, len);
1212 if (context->ipc.has_perm) {
1214 ab = audit_log_start(context, GFP_KERNEL,
1215 AUDIT_IPC_SET_PERM);
1218 audit_log_format(ab,
1219 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1220 context->ipc.qbytes,
1221 context->ipc.perm_uid,
1222 context->ipc.perm_gid,
1223 context->ipc.perm_mode);
1226 case AUDIT_MQ_OPEN: {
1227 audit_log_format(ab,
1228 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1229 "mq_msgsize=%ld mq_curmsgs=%ld",
1230 context->mq_open.oflag, context->mq_open.mode,
1231 context->mq_open.attr.mq_flags,
1232 context->mq_open.attr.mq_maxmsg,
1233 context->mq_open.attr.mq_msgsize,
1234 context->mq_open.attr.mq_curmsgs);
1236 case AUDIT_MQ_SENDRECV: {
1237 audit_log_format(ab,
1238 "mqdes=%d msg_len=%zd msg_prio=%u "
1239 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1240 context->mq_sendrecv.mqdes,
1241 context->mq_sendrecv.msg_len,
1242 context->mq_sendrecv.msg_prio,
1243 context->mq_sendrecv.abs_timeout.tv_sec,
1244 context->mq_sendrecv.abs_timeout.tv_nsec);
1246 case AUDIT_MQ_NOTIFY: {
1247 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1248 context->mq_notify.mqdes,
1249 context->mq_notify.sigev_signo);
1251 case AUDIT_MQ_GETSETATTR: {
1252 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1253 audit_log_format(ab,
1254 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1256 context->mq_getsetattr.mqdes,
1257 attr->mq_flags, attr->mq_maxmsg,
1258 attr->mq_msgsize, attr->mq_curmsgs);
1260 case AUDIT_CAPSET: {
1261 audit_log_format(ab, "pid=%d", context->capset.pid);
1262 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1263 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1264 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1267 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1268 context->mmap.flags);
1270 case AUDIT_EXECVE: {
1271 audit_log_execve_info(context, &ab);
1277 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1279 int i, call_panic = 0;
1280 struct audit_buffer *ab;
1281 struct audit_aux_data *aux;
1282 struct audit_names *n;
1284 /* tsk == current */
1285 context->personality = tsk->personality;
1287 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1289 return; /* audit_panic has been called */
1290 audit_log_format(ab, "arch=%x syscall=%d",
1291 context->arch, context->major);
1292 if (context->personality != PER_LINUX)
1293 audit_log_format(ab, " per=%lx", context->personality);
1294 if (context->return_valid)
1295 audit_log_format(ab, " success=%s exit=%ld",
1296 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1297 context->return_code);
1299 audit_log_format(ab,
1300 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1305 context->name_count);
1307 audit_log_task_info(ab, tsk);
1308 audit_log_key(ab, context->filterkey);
1311 for (aux = context->aux; aux; aux = aux->next) {
1313 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1315 continue; /* audit_panic has been called */
1317 switch (aux->type) {
1319 case AUDIT_BPRM_FCAPS: {
1320 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1321 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1322 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1323 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1324 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1325 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1326 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1327 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1328 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1329 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1330 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1338 show_special(context, &call_panic);
1340 if (context->fds[0] >= 0) {
1341 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1343 audit_log_format(ab, "fd0=%d fd1=%d",
1344 context->fds[0], context->fds[1]);
1349 if (context->sockaddr_len) {
1350 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1352 audit_log_format(ab, "saddr=");
1353 audit_log_n_hex(ab, (void *)context->sockaddr,
1354 context->sockaddr_len);
1359 for (aux = context->aux_pids; aux; aux = aux->next) {
1360 struct audit_aux_data_pids *axs = (void *)aux;
1362 for (i = 0; i < axs->pid_count; i++)
1363 if (audit_log_pid_context(context, axs->target_pid[i],
1364 axs->target_auid[i],
1366 axs->target_sessionid[i],
1368 axs->target_comm[i]))
1372 if (context->target_pid &&
1373 audit_log_pid_context(context, context->target_pid,
1374 context->target_auid, context->target_uid,
1375 context->target_sessionid,
1376 context->target_sid, context->target_comm))
1379 if (context->pwd.dentry && context->pwd.mnt) {
1380 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1382 audit_log_d_path(ab, " cwd=", &context->pwd);
1388 list_for_each_entry(n, &context->names_list, list) {
1391 audit_log_name(context, n, NULL, i++, &call_panic);
1394 /* Send end of event record to help user space know we are finished */
1395 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1399 audit_panic("error converting sid to string");
1403 * audit_free - free a per-task audit context
1404 * @tsk: task whose audit context block to free
1406 * Called from copy_process and do_exit
1408 void __audit_free(struct task_struct *tsk)
1410 struct audit_context *context;
1412 context = audit_get_context(tsk, 0, 0);
1416 /* Check for system calls that do not go through the exit
1417 * function (e.g., exit_group), then free context block.
1418 * We use GFP_ATOMIC here because we might be doing this
1419 * in the context of the idle thread */
1420 /* that can happen only if we are called from do_exit() */
1421 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1422 audit_log_exit(context, tsk);
1423 if (!list_empty(&context->killed_trees))
1424 audit_kill_trees(&context->killed_trees);
1426 audit_free_context(context);
1430 * audit_syscall_entry - fill in an audit record at syscall entry
1431 * @arch: architecture type
1432 * @major: major syscall type (function)
1433 * @a1: additional syscall register 1
1434 * @a2: additional syscall register 2
1435 * @a3: additional syscall register 3
1436 * @a4: additional syscall register 4
1438 * Fill in audit context at syscall entry. This only happens if the
1439 * audit context was created when the task was created and the state or
1440 * filters demand the audit context be built. If the state from the
1441 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1442 * then the record will be written at syscall exit time (otherwise, it
1443 * will only be written if another part of the kernel requests that it
1446 void __audit_syscall_entry(int arch, int major,
1447 unsigned long a1, unsigned long a2,
1448 unsigned long a3, unsigned long a4)
1450 struct task_struct *tsk = current;
1451 struct audit_context *context = tsk->audit_context;
1452 enum audit_state state;
1457 BUG_ON(context->in_syscall || context->name_count);
1462 context->arch = arch;
1463 context->major = major;
1464 context->argv[0] = a1;
1465 context->argv[1] = a2;
1466 context->argv[2] = a3;
1467 context->argv[3] = a4;
1469 state = context->state;
1470 context->dummy = !audit_n_rules;
1471 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1473 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1475 if (state == AUDIT_DISABLED)
1478 context->serial = 0;
1479 context->ctime = CURRENT_TIME;
1480 context->in_syscall = 1;
1481 context->current_state = state;
1486 * audit_syscall_exit - deallocate audit context after a system call
1487 * @success: success value of the syscall
1488 * @return_code: return value of the syscall
1490 * Tear down after system call. If the audit context has been marked as
1491 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1492 * filtering, or because some other part of the kernel wrote an audit
1493 * message), then write out the syscall information. In call cases,
1494 * free the names stored from getname().
1496 void __audit_syscall_exit(int success, long return_code)
1498 struct task_struct *tsk = current;
1499 struct audit_context *context;
1502 success = AUDITSC_SUCCESS;
1504 success = AUDITSC_FAILURE;
1506 context = audit_get_context(tsk, success, return_code);
1510 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1511 audit_log_exit(context, tsk);
1513 context->in_syscall = 0;
1514 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
1516 if (!list_empty(&context->killed_trees))
1517 audit_kill_trees(&context->killed_trees);
1519 audit_free_names(context);
1520 unroll_tree_refs(context, NULL, 0);
1521 audit_free_aux(context);
1522 context->aux = NULL;
1523 context->aux_pids = NULL;
1524 context->target_pid = 0;
1525 context->target_sid = 0;
1526 context->sockaddr_len = 0;
1528 context->fds[0] = -1;
1529 if (context->state != AUDIT_RECORD_CONTEXT) {
1530 kfree(context->filterkey);
1531 context->filterkey = NULL;
1533 tsk->audit_context = context;
1536 static inline void handle_one(const struct inode *inode)
1538 #ifdef CONFIG_AUDIT_TREE
1539 struct audit_context *context;
1540 struct audit_tree_refs *p;
1541 struct audit_chunk *chunk;
1543 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
1545 context = current->audit_context;
1547 count = context->tree_count;
1549 chunk = audit_tree_lookup(inode);
1553 if (likely(put_tree_ref(context, chunk)))
1555 if (unlikely(!grow_tree_refs(context))) {
1556 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
1557 audit_set_auditable(context);
1558 audit_put_chunk(chunk);
1559 unroll_tree_refs(context, p, count);
1562 put_tree_ref(context, chunk);
1566 static void handle_path(const struct dentry *dentry)
1568 #ifdef CONFIG_AUDIT_TREE
1569 struct audit_context *context;
1570 struct audit_tree_refs *p;
1571 const struct dentry *d, *parent;
1572 struct audit_chunk *drop;
1576 context = current->audit_context;
1578 count = context->tree_count;
1583 seq = read_seqbegin(&rename_lock);
1585 struct inode *inode = d->d_inode;
1586 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
1587 struct audit_chunk *chunk;
1588 chunk = audit_tree_lookup(inode);
1590 if (unlikely(!put_tree_ref(context, chunk))) {
1596 parent = d->d_parent;
1601 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1604 /* just a race with rename */
1605 unroll_tree_refs(context, p, count);
1608 audit_put_chunk(drop);
1609 if (grow_tree_refs(context)) {
1610 /* OK, got more space */
1611 unroll_tree_refs(context, p, count);
1616 "out of memory, audit has lost a tree reference\n");
1617 unroll_tree_refs(context, p, count);
1618 audit_set_auditable(context);
1625 static struct audit_names *audit_alloc_name(struct audit_context *context,
1628 struct audit_names *aname;
1630 if (context->name_count < AUDIT_NAMES) {
1631 aname = &context->preallocated_names[context->name_count];
1632 memset(aname, 0, sizeof(*aname));
1634 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1637 aname->should_free = true;
1640 aname->ino = (unsigned long)-1;
1642 list_add_tail(&aname->list, &context->names_list);
1644 context->name_count++;
1646 context->ino_count++;
1652 * audit_reusename - fill out filename with info from existing entry
1653 * @uptr: userland ptr to pathname
1655 * Search the audit_names list for the current audit context. If there is an
1656 * existing entry with a matching "uptr" then return the filename
1657 * associated with that audit_name. If not, return NULL.
1660 __audit_reusename(const __user char *uptr)
1662 struct audit_context *context = current->audit_context;
1663 struct audit_names *n;
1665 list_for_each_entry(n, &context->names_list, list) {
1668 if (n->name->uptr == uptr)
1675 * audit_getname - add a name to the list
1676 * @name: name to add
1678 * Add a name to the list of audit names for this context.
1679 * Called from fs/namei.c:getname().
1681 void __audit_getname(struct filename *name)
1683 struct audit_context *context = current->audit_context;
1684 struct audit_names *n;
1686 if (!context->in_syscall) {
1687 #if AUDIT_DEBUG == 2
1688 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1689 __FILE__, __LINE__, context->serial, name);
1696 /* The filename _must_ have a populated ->name */
1697 BUG_ON(!name->name);
1700 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
1705 n->name_len = AUDIT_NAME_FULL;
1709 if (!context->pwd.dentry)
1710 get_fs_pwd(current->fs, &context->pwd);
1713 /* audit_putname - intercept a putname request
1714 * @name: name to intercept and delay for putname
1716 * If we have stored the name from getname in the audit context,
1717 * then we delay the putname until syscall exit.
1718 * Called from include/linux/fs.h:putname().
1720 void audit_putname(struct filename *name)
1722 struct audit_context *context = current->audit_context;
1725 if (!context->in_syscall) {
1726 #if AUDIT_DEBUG == 2
1727 printk(KERN_ERR "%s:%d(:%d): final_putname(%p)\n",
1728 __FILE__, __LINE__, context->serial, name);
1729 if (context->name_count) {
1730 struct audit_names *n;
1733 list_for_each_entry(n, &context->names_list, list)
1734 printk(KERN_ERR "name[%d] = %p = %s\n", i++,
1735 n->name, n->name->name ?: "(null)");
1738 final_putname(name);
1742 ++context->put_count;
1743 if (context->put_count > context->name_count) {
1744 printk(KERN_ERR "%s:%d(:%d): major=%d"
1745 " in_syscall=%d putname(%p) name_count=%d"
1748 context->serial, context->major,
1749 context->in_syscall, name->name,
1750 context->name_count, context->put_count);
1758 * __audit_inode - store the inode and device from a lookup
1759 * @name: name being audited
1760 * @dentry: dentry being audited
1761 * @flags: attributes for this particular entry
1763 void __audit_inode(struct filename *name, const struct dentry *dentry,
1766 struct audit_context *context = current->audit_context;
1767 const struct inode *inode = dentry->d_inode;
1768 struct audit_names *n;
1769 bool parent = flags & AUDIT_INODE_PARENT;
1771 if (!context->in_syscall)
1778 /* The struct filename _must_ have a populated ->name */
1779 BUG_ON(!name->name);
1782 * If we have a pointer to an audit_names entry already, then we can
1783 * just use it directly if the type is correct.
1788 if (n->type == AUDIT_TYPE_PARENT ||
1789 n->type == AUDIT_TYPE_UNKNOWN)
1792 if (n->type != AUDIT_TYPE_PARENT)
1797 list_for_each_entry_reverse(n, &context->names_list, list) {
1798 /* does the name pointer match? */
1799 if (!n->name || n->name->name != name->name)
1802 /* match the correct record type */
1804 if (n->type == AUDIT_TYPE_PARENT ||
1805 n->type == AUDIT_TYPE_UNKNOWN)
1808 if (n->type != AUDIT_TYPE_PARENT)
1814 /* unable to find the name from a previous getname(). Allocate a new
1817 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
1822 n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
1823 n->type = AUDIT_TYPE_PARENT;
1824 if (flags & AUDIT_INODE_HIDDEN)
1827 n->name_len = AUDIT_NAME_FULL;
1828 n->type = AUDIT_TYPE_NORMAL;
1830 handle_path(dentry);
1831 audit_copy_inode(n, dentry, inode);
1835 * __audit_inode_child - collect inode info for created/removed objects
1836 * @parent: inode of dentry parent
1837 * @dentry: dentry being audited
1838 * @type: AUDIT_TYPE_* value that we're looking for
1840 * For syscalls that create or remove filesystem objects, audit_inode
1841 * can only collect information for the filesystem object's parent.
1842 * This call updates the audit context with the child's information.
1843 * Syscalls that create a new filesystem object must be hooked after
1844 * the object is created. Syscalls that remove a filesystem object
1845 * must be hooked prior, in order to capture the target inode during
1846 * unsuccessful attempts.
1848 void __audit_inode_child(const struct inode *parent,
1849 const struct dentry *dentry,
1850 const unsigned char type)
1852 struct audit_context *context = current->audit_context;
1853 const struct inode *inode = dentry->d_inode;
1854 const char *dname = dentry->d_name.name;
1855 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
1857 if (!context->in_syscall)
1863 /* look for a parent entry first */
1864 list_for_each_entry(n, &context->names_list, list) {
1865 if (!n->name || n->type != AUDIT_TYPE_PARENT)
1868 if (n->ino == parent->i_ino &&
1869 !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
1875 /* is there a matching child entry? */
1876 list_for_each_entry(n, &context->names_list, list) {
1877 /* can only match entries that have a name */
1878 if (!n->name || n->type != type)
1881 /* if we found a parent, make sure this one is a child of it */
1882 if (found_parent && (n->name != found_parent->name))
1885 if (!strcmp(dname, n->name->name) ||
1886 !audit_compare_dname_path(dname, n->name->name,
1888 found_parent->name_len :
1895 if (!found_parent) {
1896 /* create a new, "anonymous" parent record */
1897 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
1900 audit_copy_inode(n, NULL, parent);
1904 found_child = audit_alloc_name(context, type);
1908 /* Re-use the name belonging to the slot for a matching parent
1909 * directory. All names for this context are relinquished in
1910 * audit_free_names() */
1912 found_child->name = found_parent->name;
1913 found_child->name_len = AUDIT_NAME_FULL;
1914 /* don't call __putname() */
1915 found_child->name_put = false;
1919 audit_copy_inode(found_child, dentry, inode);
1921 found_child->ino = (unsigned long)-1;
1923 EXPORT_SYMBOL_GPL(__audit_inode_child);
1926 * auditsc_get_stamp - get local copies of audit_context values
1927 * @ctx: audit_context for the task
1928 * @t: timespec to store time recorded in the audit_context
1929 * @serial: serial value that is recorded in the audit_context
1931 * Also sets the context as auditable.
1933 int auditsc_get_stamp(struct audit_context *ctx,
1934 struct timespec *t, unsigned int *serial)
1936 if (!ctx->in_syscall)
1939 ctx->serial = audit_serial();
1940 t->tv_sec = ctx->ctime.tv_sec;
1941 t->tv_nsec = ctx->ctime.tv_nsec;
1942 *serial = ctx->serial;
1945 ctx->current_state = AUDIT_RECORD_CONTEXT;
1950 /* global counter which is incremented every time something logs in */
1951 static atomic_t session_id = ATOMIC_INIT(0);
1953 static int audit_set_loginuid_perm(kuid_t loginuid)
1955 /* if we are unset, we don't need privs */
1956 if (!audit_loginuid_set(current))
1958 /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
1959 if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
1961 /* it is set, you need permission */
1962 if (!capable(CAP_AUDIT_CONTROL))
1964 /* reject if this is not an unset and we don't allow that */
1965 if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
1970 static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
1971 unsigned int oldsessionid, unsigned int sessionid,
1974 struct audit_buffer *ab;
1975 uid_t uid, ologinuid, nloginuid;
1977 uid = from_kuid(&init_user_ns, task_uid(current));
1978 ologinuid = from_kuid(&init_user_ns, koldloginuid);
1979 nloginuid = from_kuid(&init_user_ns, kloginuid),
1981 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1984 audit_log_format(ab, "pid=%d uid=%u old auid=%u new auid=%u old "
1985 "ses=%u new ses=%u res=%d", current->pid, uid, ologinuid,
1986 nloginuid, oldsessionid, sessionid, !rc);
1991 * audit_set_loginuid - set current task's audit_context loginuid
1992 * @loginuid: loginuid value
1996 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1998 int audit_set_loginuid(kuid_t loginuid)
2000 struct task_struct *task = current;
2001 unsigned int sessionid = -1;
2002 kuid_t oldloginuid, oldsessionid;
2005 oldloginuid = audit_get_loginuid(current);
2006 oldsessionid = audit_get_sessionid(current);
2008 rc = audit_set_loginuid_perm(loginuid);
2012 /* are we setting or clearing? */
2013 if (uid_valid(loginuid))
2014 sessionid = atomic_inc_return(&session_id);
2016 task->sessionid = sessionid;
2017 task->loginuid = loginuid;
2019 audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
2024 * __audit_mq_open - record audit data for a POSIX MQ open
2027 * @attr: queue attributes
2030 void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2032 struct audit_context *context = current->audit_context;
2035 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2037 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2039 context->mq_open.oflag = oflag;
2040 context->mq_open.mode = mode;
2042 context->type = AUDIT_MQ_OPEN;
2046 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2047 * @mqdes: MQ descriptor
2048 * @msg_len: Message length
2049 * @msg_prio: Message priority
2050 * @abs_timeout: Message timeout in absolute time
2053 void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2054 const struct timespec *abs_timeout)
2056 struct audit_context *context = current->audit_context;
2057 struct timespec *p = &context->mq_sendrecv.abs_timeout;
2060 memcpy(p, abs_timeout, sizeof(struct timespec));
2062 memset(p, 0, sizeof(struct timespec));
2064 context->mq_sendrecv.mqdes = mqdes;
2065 context->mq_sendrecv.msg_len = msg_len;
2066 context->mq_sendrecv.msg_prio = msg_prio;
2068 context->type = AUDIT_MQ_SENDRECV;
2072 * __audit_mq_notify - record audit data for a POSIX MQ notify
2073 * @mqdes: MQ descriptor
2074 * @notification: Notification event
2078 void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2080 struct audit_context *context = current->audit_context;
2083 context->mq_notify.sigev_signo = notification->sigev_signo;
2085 context->mq_notify.sigev_signo = 0;
2087 context->mq_notify.mqdes = mqdes;
2088 context->type = AUDIT_MQ_NOTIFY;
2092 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2093 * @mqdes: MQ descriptor
2097 void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2099 struct audit_context *context = current->audit_context;
2100 context->mq_getsetattr.mqdes = mqdes;
2101 context->mq_getsetattr.mqstat = *mqstat;
2102 context->type = AUDIT_MQ_GETSETATTR;
2106 * audit_ipc_obj - record audit data for ipc object
2107 * @ipcp: ipc permissions
2110 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2112 struct audit_context *context = current->audit_context;
2113 context->ipc.uid = ipcp->uid;
2114 context->ipc.gid = ipcp->gid;
2115 context->ipc.mode = ipcp->mode;
2116 context->ipc.has_perm = 0;
2117 security_ipc_getsecid(ipcp, &context->ipc.osid);
2118 context->type = AUDIT_IPC;
2122 * audit_ipc_set_perm - record audit data for new ipc permissions
2123 * @qbytes: msgq bytes
2124 * @uid: msgq user id
2125 * @gid: msgq group id
2126 * @mode: msgq mode (permissions)
2128 * Called only after audit_ipc_obj().
2130 void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2132 struct audit_context *context = current->audit_context;
2134 context->ipc.qbytes = qbytes;
2135 context->ipc.perm_uid = uid;
2136 context->ipc.perm_gid = gid;
2137 context->ipc.perm_mode = mode;
2138 context->ipc.has_perm = 1;
2141 void __audit_bprm(struct linux_binprm *bprm)
2143 struct audit_context *context = current->audit_context;
2145 context->type = AUDIT_EXECVE;
2146 context->execve.argc = bprm->argc;
2147 context->execve.mm = bprm->mm;
2152 * audit_socketcall - record audit data for sys_socketcall
2153 * @nargs: number of args, which should not be more than AUDITSC_ARGS.
2157 int __audit_socketcall(int nargs, unsigned long *args)
2159 struct audit_context *context = current->audit_context;
2161 if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2163 context->type = AUDIT_SOCKETCALL;
2164 context->socketcall.nargs = nargs;
2165 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2170 * __audit_fd_pair - record audit data for pipe and socketpair
2171 * @fd1: the first file descriptor
2172 * @fd2: the second file descriptor
2175 void __audit_fd_pair(int fd1, int fd2)
2177 struct audit_context *context = current->audit_context;
2178 context->fds[0] = fd1;
2179 context->fds[1] = fd2;
2183 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2184 * @len: data length in user space
2185 * @a: data address in kernel space
2187 * Returns 0 for success or NULL context or < 0 on error.
2189 int __audit_sockaddr(int len, void *a)
2191 struct audit_context *context = current->audit_context;
2193 if (!context->sockaddr) {
2194 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2197 context->sockaddr = p;
2200 context->sockaddr_len = len;
2201 memcpy(context->sockaddr, a, len);
2205 void __audit_ptrace(struct task_struct *t)
2207 struct audit_context *context = current->audit_context;
2209 context->target_pid = t->pid;
2210 context->target_auid = audit_get_loginuid(t);
2211 context->target_uid = task_uid(t);
2212 context->target_sessionid = audit_get_sessionid(t);
2213 security_task_getsecid(t, &context->target_sid);
2214 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2218 * audit_signal_info - record signal info for shutting down audit subsystem
2219 * @sig: signal value
2220 * @t: task being signaled
2222 * If the audit subsystem is being terminated, record the task (pid)
2223 * and uid that is doing that.
2225 int __audit_signal_info(int sig, struct task_struct *t)
2227 struct audit_aux_data_pids *axp;
2228 struct task_struct *tsk = current;
2229 struct audit_context *ctx = tsk->audit_context;
2230 kuid_t uid = current_uid(), t_uid = task_uid(t);
2232 if (audit_pid && t->tgid == audit_pid) {
2233 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2234 audit_sig_pid = tsk->pid;
2235 if (uid_valid(tsk->loginuid))
2236 audit_sig_uid = tsk->loginuid;
2238 audit_sig_uid = uid;
2239 security_task_getsecid(tsk, &audit_sig_sid);
2241 if (!audit_signals || audit_dummy_context())
2245 /* optimize the common case by putting first signal recipient directly
2246 * in audit_context */
2247 if (!ctx->target_pid) {
2248 ctx->target_pid = t->tgid;
2249 ctx->target_auid = audit_get_loginuid(t);
2250 ctx->target_uid = t_uid;
2251 ctx->target_sessionid = audit_get_sessionid(t);
2252 security_task_getsecid(t, &ctx->target_sid);
2253 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2257 axp = (void *)ctx->aux_pids;
2258 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2259 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2263 axp->d.type = AUDIT_OBJ_PID;
2264 axp->d.next = ctx->aux_pids;
2265 ctx->aux_pids = (void *)axp;
2267 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2269 axp->target_pid[axp->pid_count] = t->tgid;
2270 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2271 axp->target_uid[axp->pid_count] = t_uid;
2272 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2273 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2274 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2281 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2282 * @bprm: pointer to the bprm being processed
2283 * @new: the proposed new credentials
2284 * @old: the old credentials
2286 * Simply check if the proc already has the caps given by the file and if not
2287 * store the priv escalation info for later auditing at the end of the syscall
2291 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2292 const struct cred *new, const struct cred *old)
2294 struct audit_aux_data_bprm_fcaps *ax;
2295 struct audit_context *context = current->audit_context;
2296 struct cpu_vfs_cap_data vcaps;
2297 struct dentry *dentry;
2299 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2303 ax->d.type = AUDIT_BPRM_FCAPS;
2304 ax->d.next = context->aux;
2305 context->aux = (void *)ax;
2307 dentry = dget(bprm->file->f_dentry);
2308 get_vfs_caps_from_disk(dentry, &vcaps);
2311 ax->fcap.permitted = vcaps.permitted;
2312 ax->fcap.inheritable = vcaps.inheritable;
2313 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2314 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2316 ax->old_pcap.permitted = old->cap_permitted;
2317 ax->old_pcap.inheritable = old->cap_inheritable;
2318 ax->old_pcap.effective = old->cap_effective;
2320 ax->new_pcap.permitted = new->cap_permitted;
2321 ax->new_pcap.inheritable = new->cap_inheritable;
2322 ax->new_pcap.effective = new->cap_effective;
2327 * __audit_log_capset - store information about the arguments to the capset syscall
2328 * @pid: target pid of the capset call
2329 * @new: the new credentials
2330 * @old: the old (current) credentials
2332 * Record the aguments userspace sent to sys_capset for later printing by the
2333 * audit system if applicable
2335 void __audit_log_capset(pid_t pid,
2336 const struct cred *new, const struct cred *old)
2338 struct audit_context *context = current->audit_context;
2339 context->capset.pid = pid;
2340 context->capset.cap.effective = new->cap_effective;
2341 context->capset.cap.inheritable = new->cap_effective;
2342 context->capset.cap.permitted = new->cap_permitted;
2343 context->type = AUDIT_CAPSET;
2346 void __audit_mmap_fd(int fd, int flags)
2348 struct audit_context *context = current->audit_context;
2349 context->mmap.fd = fd;
2350 context->mmap.flags = flags;
2351 context->type = AUDIT_MMAP;
2354 static void audit_log_task(struct audit_buffer *ab)
2358 unsigned int sessionid;
2360 auid = audit_get_loginuid(current);
2361 sessionid = audit_get_sessionid(current);
2362 current_uid_gid(&uid, &gid);
2364 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2365 from_kuid(&init_user_ns, auid),
2366 from_kuid(&init_user_ns, uid),
2367 from_kgid(&init_user_ns, gid),
2369 audit_log_task_context(ab);
2370 audit_log_format(ab, " pid=%d comm=", current->pid);
2371 audit_log_untrustedstring(ab, current->comm);
2374 static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
2377 audit_log_format(ab, " reason=");
2378 audit_log_string(ab, reason);
2379 audit_log_format(ab, " sig=%ld", signr);
2382 * audit_core_dumps - record information about processes that end abnormally
2383 * @signr: signal value
2385 * If a process ends with a core dump, something fishy is going on and we
2386 * should record the event for investigation.
2388 void audit_core_dumps(long signr)
2390 struct audit_buffer *ab;
2395 if (signr == SIGQUIT) /* don't care for those */
2398 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2401 audit_log_abend(ab, "memory violation", signr);
2405 void __audit_seccomp(unsigned long syscall, long signr, int code)
2407 struct audit_buffer *ab;
2409 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
2413 audit_log_format(ab, " sig=%ld", signr);
2414 audit_log_format(ab, " syscall=%ld", syscall);
2415 audit_log_format(ab, " compat=%d", is_compat_task());
2416 audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
2417 audit_log_format(ab, " code=0x%x", code);
2421 struct list_head *audit_killed_trees(void)
2423 struct audit_context *ctx = current->audit_context;
2424 if (likely(!ctx || !ctx->in_syscall))
2426 return &ctx->killed_trees;