1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* auditsc.c -- System-call auditing support
3 * Handles all system-call specific auditing features.
5 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
6 * Copyright 2005 Hewlett-Packard Development Company, L.P.
7 * Copyright (C) 2005, 2006 IBM Corporation
10 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
12 * Many of the ideas implemented here are from Stephen C. Tweedie,
13 * especially the idea of avoiding a copy by using getname.
15 * The method for actual interception of syscall entry and exit (not in
16 * this file -- see entry.S) is based on a GPL'd patch written by
17 * okir@suse.de and Copyright 2003 SuSE Linux AG.
19 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
22 * The support of additional filter rules compares (>, <, >=, <=) was
23 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
25 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
26 * filesystem information.
28 * Subject and object context labeling support added by <danjones@us.ibm.com>
29 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/init.h>
35 #include <asm/types.h>
36 #include <linux/atomic.h>
38 #include <linux/namei.h>
40 #include <linux/export.h>
41 #include <linux/slab.h>
42 #include <linux/mount.h>
43 #include <linux/socket.h>
44 #include <linux/mqueue.h>
45 #include <linux/audit.h>
46 #include <linux/personality.h>
47 #include <linux/time.h>
48 #include <linux/netlink.h>
49 #include <linux/compiler.h>
50 #include <asm/unistd.h>
51 #include <linux/security.h>
52 #include <linux/list.h>
53 #include <linux/binfmts.h>
54 #include <linux/highmem.h>
55 #include <linux/syscalls.h>
56 #include <asm/syscall.h>
57 #include <linux/capability.h>
58 #include <linux/fs_struct.h>
59 #include <linux/compat.h>
60 #include <linux/ctype.h>
61 #include <linux/string.h>
62 #include <linux/uaccess.h>
63 #include <linux/fsnotify_backend.h>
64 #include <uapi/linux/limits.h>
65 #include <uapi/linux/netfilter/nf_tables.h>
66 #include <uapi/linux/openat2.h> // struct open_how
70 /* flags stating the success for a syscall */
71 #define AUDITSC_INVALID 0
72 #define AUDITSC_SUCCESS 1
73 #define AUDITSC_FAILURE 2
75 /* no execve audit message should be longer than this (userspace limits),
76 * see the note near the top of audit_log_execve_info() about this value */
77 #define MAX_EXECVE_AUDIT_LEN 7500
79 /* max length to print of cmdline/proctitle value during audit */
80 #define MAX_PROCTITLE_AUDIT_LEN 128
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 /* Number of target pids per aux struct. */
94 #define AUDIT_AUX_PIDS 16
96 struct audit_aux_data_pids {
97 struct audit_aux_data d;
98 pid_t target_pid[AUDIT_AUX_PIDS];
99 kuid_t target_auid[AUDIT_AUX_PIDS];
100 kuid_t target_uid[AUDIT_AUX_PIDS];
101 unsigned int target_sessionid[AUDIT_AUX_PIDS];
102 u32 target_sid[AUDIT_AUX_PIDS];
103 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
107 struct audit_aux_data_bprm_fcaps {
108 struct audit_aux_data d;
109 struct audit_cap_data fcap;
110 unsigned int fcap_ver;
111 struct audit_cap_data old_pcap;
112 struct audit_cap_data new_pcap;
115 struct audit_tree_refs {
116 struct audit_tree_refs *next;
117 struct audit_chunk *c[31];
120 struct audit_nfcfgop_tab {
121 enum audit_nfcfgop op;
125 static const struct audit_nfcfgop_tab audit_nfcfgs[] = {
126 { AUDIT_XT_OP_REGISTER, "xt_register" },
127 { AUDIT_XT_OP_REPLACE, "xt_replace" },
128 { AUDIT_XT_OP_UNREGISTER, "xt_unregister" },
129 { AUDIT_NFT_OP_TABLE_REGISTER, "nft_register_table" },
130 { AUDIT_NFT_OP_TABLE_UNREGISTER, "nft_unregister_table" },
131 { AUDIT_NFT_OP_CHAIN_REGISTER, "nft_register_chain" },
132 { AUDIT_NFT_OP_CHAIN_UNREGISTER, "nft_unregister_chain" },
133 { AUDIT_NFT_OP_RULE_REGISTER, "nft_register_rule" },
134 { AUDIT_NFT_OP_RULE_UNREGISTER, "nft_unregister_rule" },
135 { AUDIT_NFT_OP_SET_REGISTER, "nft_register_set" },
136 { AUDIT_NFT_OP_SET_UNREGISTER, "nft_unregister_set" },
137 { AUDIT_NFT_OP_SETELEM_REGISTER, "nft_register_setelem" },
138 { AUDIT_NFT_OP_SETELEM_UNREGISTER, "nft_unregister_setelem" },
139 { AUDIT_NFT_OP_GEN_REGISTER, "nft_register_gen" },
140 { AUDIT_NFT_OP_OBJ_REGISTER, "nft_register_obj" },
141 { AUDIT_NFT_OP_OBJ_UNREGISTER, "nft_unregister_obj" },
142 { AUDIT_NFT_OP_OBJ_RESET, "nft_reset_obj" },
143 { AUDIT_NFT_OP_FLOWTABLE_REGISTER, "nft_register_flowtable" },
144 { AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, "nft_unregister_flowtable" },
145 { AUDIT_NFT_OP_INVALID, "nft_invalid" },
148 static int audit_match_perm(struct audit_context *ctx, int mask)
156 switch (audit_classify_syscall(ctx->arch, n)) {
158 if ((mask & AUDIT_PERM_WRITE) &&
159 audit_match_class(AUDIT_CLASS_WRITE, n))
161 if ((mask & AUDIT_PERM_READ) &&
162 audit_match_class(AUDIT_CLASS_READ, n))
164 if ((mask & AUDIT_PERM_ATTR) &&
165 audit_match_class(AUDIT_CLASS_CHATTR, n))
168 case AUDITSC_COMPAT: /* 32bit on biarch */
169 if ((mask & AUDIT_PERM_WRITE) &&
170 audit_match_class(AUDIT_CLASS_WRITE_32, n))
172 if ((mask & AUDIT_PERM_READ) &&
173 audit_match_class(AUDIT_CLASS_READ_32, n))
175 if ((mask & AUDIT_PERM_ATTR) &&
176 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
180 return mask & ACC_MODE(ctx->argv[1]);
182 return mask & ACC_MODE(ctx->argv[2]);
183 case AUDITSC_SOCKETCALL:
184 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
186 return mask & AUDIT_PERM_EXEC;
187 case AUDITSC_OPENAT2:
188 return mask & ACC_MODE((u32)((struct open_how *)ctx->argv[2])->flags);
194 static int audit_match_filetype(struct audit_context *ctx, int val)
196 struct audit_names *n;
197 umode_t mode = (umode_t)val;
202 list_for_each_entry(n, &ctx->names_list, list) {
203 if ((n->ino != AUDIT_INO_UNSET) &&
204 ((n->mode & S_IFMT) == mode))
212 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
213 * ->first_trees points to its beginning, ->trees - to the current end of data.
214 * ->tree_count is the number of free entries in array pointed to by ->trees.
215 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
216 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
217 * it's going to remain 1-element for almost any setup) until we free context itself.
218 * References in it _are_ dropped - at the same time we free/drop aux stuff.
221 static void audit_set_auditable(struct audit_context *ctx)
225 ctx->current_state = AUDIT_STATE_RECORD;
229 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
231 struct audit_tree_refs *p = ctx->trees;
232 int left = ctx->tree_count;
235 p->c[--left] = chunk;
236 ctx->tree_count = left;
245 ctx->tree_count = 30;
251 static int grow_tree_refs(struct audit_context *ctx)
253 struct audit_tree_refs *p = ctx->trees;
255 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
261 p->next = ctx->trees;
263 ctx->first_trees = ctx->trees;
264 ctx->tree_count = 31;
268 static void unroll_tree_refs(struct audit_context *ctx,
269 struct audit_tree_refs *p, int count)
271 struct audit_tree_refs *q;
275 /* we started with empty chain */
276 p = ctx->first_trees;
278 /* if the very first allocation has failed, nothing to do */
283 for (q = p; q != ctx->trees; q = q->next, n = 31) {
285 audit_put_chunk(q->c[n]);
289 while (n-- > ctx->tree_count) {
290 audit_put_chunk(q->c[n]);
294 ctx->tree_count = count;
297 static void free_tree_refs(struct audit_context *ctx)
299 struct audit_tree_refs *p, *q;
301 for (p = ctx->first_trees; p; p = q) {
307 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
309 struct audit_tree_refs *p;
315 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
316 for (n = 0; n < 31; n++)
317 if (audit_tree_match(p->c[n], tree))
322 for (n = ctx->tree_count; n < 31; n++)
323 if (audit_tree_match(p->c[n], tree))
329 static int audit_compare_uid(kuid_t uid,
330 struct audit_names *name,
331 struct audit_field *f,
332 struct audit_context *ctx)
334 struct audit_names *n;
338 rc = audit_uid_comparator(uid, f->op, name->uid);
344 list_for_each_entry(n, &ctx->names_list, list) {
345 rc = audit_uid_comparator(uid, f->op, n->uid);
353 static int audit_compare_gid(kgid_t gid,
354 struct audit_names *name,
355 struct audit_field *f,
356 struct audit_context *ctx)
358 struct audit_names *n;
362 rc = audit_gid_comparator(gid, f->op, name->gid);
368 list_for_each_entry(n, &ctx->names_list, list) {
369 rc = audit_gid_comparator(gid, f->op, n->gid);
377 static int audit_field_compare(struct task_struct *tsk,
378 const struct cred *cred,
379 struct audit_field *f,
380 struct audit_context *ctx,
381 struct audit_names *name)
384 /* process to file object comparisons */
385 case AUDIT_COMPARE_UID_TO_OBJ_UID:
386 return audit_compare_uid(cred->uid, name, f, ctx);
387 case AUDIT_COMPARE_GID_TO_OBJ_GID:
388 return audit_compare_gid(cred->gid, name, f, ctx);
389 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
390 return audit_compare_uid(cred->euid, name, f, ctx);
391 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
392 return audit_compare_gid(cred->egid, name, f, ctx);
393 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
394 return audit_compare_uid(audit_get_loginuid(tsk), name, f, ctx);
395 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
396 return audit_compare_uid(cred->suid, name, f, ctx);
397 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
398 return audit_compare_gid(cred->sgid, name, f, ctx);
399 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
400 return audit_compare_uid(cred->fsuid, name, f, ctx);
401 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
402 return audit_compare_gid(cred->fsgid, name, f, ctx);
403 /* uid comparisons */
404 case AUDIT_COMPARE_UID_TO_AUID:
405 return audit_uid_comparator(cred->uid, f->op,
406 audit_get_loginuid(tsk));
407 case AUDIT_COMPARE_UID_TO_EUID:
408 return audit_uid_comparator(cred->uid, f->op, cred->euid);
409 case AUDIT_COMPARE_UID_TO_SUID:
410 return audit_uid_comparator(cred->uid, f->op, cred->suid);
411 case AUDIT_COMPARE_UID_TO_FSUID:
412 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
413 /* auid comparisons */
414 case AUDIT_COMPARE_AUID_TO_EUID:
415 return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
417 case AUDIT_COMPARE_AUID_TO_SUID:
418 return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
420 case AUDIT_COMPARE_AUID_TO_FSUID:
421 return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
423 /* euid comparisons */
424 case AUDIT_COMPARE_EUID_TO_SUID:
425 return audit_uid_comparator(cred->euid, f->op, cred->suid);
426 case AUDIT_COMPARE_EUID_TO_FSUID:
427 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
428 /* suid comparisons */
429 case AUDIT_COMPARE_SUID_TO_FSUID:
430 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
431 /* gid comparisons */
432 case AUDIT_COMPARE_GID_TO_EGID:
433 return audit_gid_comparator(cred->gid, f->op, cred->egid);
434 case AUDIT_COMPARE_GID_TO_SGID:
435 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
436 case AUDIT_COMPARE_GID_TO_FSGID:
437 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
438 /* egid comparisons */
439 case AUDIT_COMPARE_EGID_TO_SGID:
440 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
441 case AUDIT_COMPARE_EGID_TO_FSGID:
442 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
443 /* sgid comparison */
444 case AUDIT_COMPARE_SGID_TO_FSGID:
445 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
447 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
453 /* Determine if any context name data matches a rule's watch data */
454 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
457 * If task_creation is true, this is an explicit indication that we are
458 * filtering a task rule at task creation time. This and tsk == current are
459 * the only situations where tsk->cred may be accessed without an rcu read lock.
461 static int audit_filter_rules(struct task_struct *tsk,
462 struct audit_krule *rule,
463 struct audit_context *ctx,
464 struct audit_names *name,
465 enum audit_state *state,
468 const struct cred *cred;
471 unsigned int sessionid;
473 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
475 for (i = 0; i < rule->field_count; i++) {
476 struct audit_field *f = &rule->fields[i];
477 struct audit_names *n;
483 pid = task_tgid_nr(tsk);
484 result = audit_comparator(pid, f->op, f->val);
489 ctx->ppid = task_ppid_nr(tsk);
490 result = audit_comparator(ctx->ppid, f->op, f->val);
494 result = audit_exe_compare(tsk, rule->exe);
495 if (f->op == Audit_not_equal)
499 result = audit_uid_comparator(cred->uid, f->op, f->uid);
502 result = audit_uid_comparator(cred->euid, f->op, f->uid);
505 result = audit_uid_comparator(cred->suid, f->op, f->uid);
508 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
511 result = audit_gid_comparator(cred->gid, f->op, f->gid);
512 if (f->op == Audit_equal) {
514 result = groups_search(cred->group_info, f->gid);
515 } else if (f->op == Audit_not_equal) {
517 result = !groups_search(cred->group_info, f->gid);
521 result = audit_gid_comparator(cred->egid, f->op, f->gid);
522 if (f->op == Audit_equal) {
524 result = groups_search(cred->group_info, f->gid);
525 } else if (f->op == Audit_not_equal) {
527 result = !groups_search(cred->group_info, f->gid);
531 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
534 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
536 case AUDIT_SESSIONID:
537 sessionid = audit_get_sessionid(tsk);
538 result = audit_comparator(sessionid, f->op, f->val);
541 result = audit_comparator(tsk->personality, f->op, f->val);
545 result = audit_comparator(ctx->arch, f->op, f->val);
549 if (ctx && ctx->return_valid != AUDITSC_INVALID)
550 result = audit_comparator(ctx->return_code, f->op, f->val);
553 if (ctx && ctx->return_valid != AUDITSC_INVALID) {
555 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
557 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
562 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
563 audit_comparator(MAJOR(name->rdev), f->op, f->val))
566 list_for_each_entry(n, &ctx->names_list, list) {
567 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
568 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
577 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
578 audit_comparator(MINOR(name->rdev), f->op, f->val))
581 list_for_each_entry(n, &ctx->names_list, list) {
582 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
583 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
592 result = audit_comparator(name->ino, f->op, f->val);
594 list_for_each_entry(n, &ctx->names_list, list) {
595 if (audit_comparator(n->ino, f->op, f->val)) {
604 result = audit_uid_comparator(name->uid, f->op, f->uid);
606 list_for_each_entry(n, &ctx->names_list, list) {
607 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
616 result = audit_gid_comparator(name->gid, f->op, f->gid);
618 list_for_each_entry(n, &ctx->names_list, list) {
619 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
628 result = audit_watch_compare(rule->watch,
631 if (f->op == Audit_not_equal)
637 result = match_tree_refs(ctx, rule->tree);
638 if (f->op == Audit_not_equal)
643 result = audit_uid_comparator(audit_get_loginuid(tsk),
646 case AUDIT_LOGINUID_SET:
647 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
649 case AUDIT_SADDR_FAM:
651 result = audit_comparator(ctx->sockaddr->ss_family,
654 case AUDIT_SUBJ_USER:
655 case AUDIT_SUBJ_ROLE:
656 case AUDIT_SUBJ_TYPE:
659 /* NOTE: this may return negative values indicating
660 a temporary error. We simply treat this as a
661 match for now to avoid losing information that
662 may be wanted. An error message will also be
666 security_task_getsecid_subj(tsk, &sid);
669 result = security_audit_rule_match(sid, f->type,
677 case AUDIT_OBJ_LEV_LOW:
678 case AUDIT_OBJ_LEV_HIGH:
679 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
682 /* Find files that match */
684 result = security_audit_rule_match(
690 list_for_each_entry(n, &ctx->names_list, list) {
691 if (security_audit_rule_match(
701 /* Find ipc objects that match */
702 if (!ctx || ctx->type != AUDIT_IPC)
704 if (security_audit_rule_match(ctx->ipc.osid,
715 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
717 case AUDIT_FILTERKEY:
718 /* ignore this field for filtering */
722 result = audit_match_perm(ctx, f->val);
723 if (f->op == Audit_not_equal)
727 result = audit_match_filetype(ctx, f->val);
728 if (f->op == Audit_not_equal)
731 case AUDIT_FIELD_COMPARE:
732 result = audit_field_compare(tsk, cred, f, ctx, name);
740 if (rule->prio <= ctx->prio)
742 if (rule->filterkey) {
743 kfree(ctx->filterkey);
744 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
746 ctx->prio = rule->prio;
748 switch (rule->action) {
750 *state = AUDIT_STATE_DISABLED;
753 *state = AUDIT_STATE_RECORD;
759 /* At process creation time, we can determine if system-call auditing is
760 * completely disabled for this task. Since we only have the task
761 * structure at this point, we can only check uid and gid.
763 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
765 struct audit_entry *e;
766 enum audit_state state;
769 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
770 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
772 if (state == AUDIT_STATE_RECORD)
773 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
779 return AUDIT_STATE_BUILD;
782 static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
786 if (val > 0xffffffff)
789 word = AUDIT_WORD(val);
790 if (word >= AUDIT_BITMASK_SIZE)
793 bit = AUDIT_BIT(val);
795 return rule->mask[word] & bit;
798 /* At syscall exit time, this filter is called if the audit_state is
799 * not low enough that auditing cannot take place, but is also not
800 * high enough that we already know we have to write an audit record
801 * (i.e., the state is AUDIT_STATE_BUILD).
803 static void audit_filter_syscall(struct task_struct *tsk,
804 struct audit_context *ctx)
806 struct audit_entry *e;
807 enum audit_state state;
809 if (auditd_test_task(tsk))
813 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_EXIT], list) {
814 if (audit_in_mask(&e->rule, ctx->major) &&
815 audit_filter_rules(tsk, &e->rule, ctx, NULL,
818 ctx->current_state = state;
827 * Given an audit_name check the inode hash table to see if they match.
828 * Called holding the rcu read lock to protect the use of audit_inode_hash
830 static int audit_filter_inode_name(struct task_struct *tsk,
831 struct audit_names *n,
832 struct audit_context *ctx) {
833 int h = audit_hash_ino((u32)n->ino);
834 struct list_head *list = &audit_inode_hash[h];
835 struct audit_entry *e;
836 enum audit_state state;
838 list_for_each_entry_rcu(e, list, list) {
839 if (audit_in_mask(&e->rule, ctx->major) &&
840 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
841 ctx->current_state = state;
848 /* At syscall exit time, this filter is called if any audit_names have been
849 * collected during syscall processing. We only check rules in sublists at hash
850 * buckets applicable to the inode numbers in audit_names.
851 * Regarding audit_state, same rules apply as for audit_filter_syscall().
853 void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
855 struct audit_names *n;
857 if (auditd_test_task(tsk))
862 list_for_each_entry(n, &ctx->names_list, list) {
863 if (audit_filter_inode_name(tsk, n, ctx))
869 static inline void audit_proctitle_free(struct audit_context *context)
871 kfree(context->proctitle.value);
872 context->proctitle.value = NULL;
873 context->proctitle.len = 0;
876 static inline void audit_free_module(struct audit_context *context)
878 if (context->type == AUDIT_KERN_MODULE) {
879 kfree(context->module.name);
880 context->module.name = NULL;
883 static inline void audit_free_names(struct audit_context *context)
885 struct audit_names *n, *next;
887 list_for_each_entry_safe(n, next, &context->names_list, list) {
894 context->name_count = 0;
895 path_put(&context->pwd);
896 context->pwd.dentry = NULL;
897 context->pwd.mnt = NULL;
900 static inline void audit_free_aux(struct audit_context *context)
902 struct audit_aux_data *aux;
904 while ((aux = context->aux)) {
905 context->aux = aux->next;
908 while ((aux = context->aux_pids)) {
909 context->aux_pids = aux->next;
914 static inline struct audit_context *audit_alloc_context(enum audit_state state)
916 struct audit_context *context;
918 context = kzalloc(sizeof(*context), GFP_KERNEL);
921 context->state = state;
922 context->prio = state == AUDIT_STATE_RECORD ? ~0ULL : 0;
923 INIT_LIST_HEAD(&context->killed_trees);
924 INIT_LIST_HEAD(&context->names_list);
925 context->fds[0] = -1;
926 context->return_valid = AUDITSC_INVALID;
931 * audit_alloc - allocate an audit context block for a task
934 * Filter on the task information and allocate a per-task audit context
935 * if necessary. Doing so turns on system call auditing for the
936 * specified task. This is called from copy_process, so no lock is
939 int audit_alloc(struct task_struct *tsk)
941 struct audit_context *context;
942 enum audit_state state;
945 if (likely(!audit_ever_enabled))
946 return 0; /* Return if not auditing. */
948 state = audit_filter_task(tsk, &key);
949 if (state == AUDIT_STATE_DISABLED) {
950 clear_task_syscall_work(tsk, SYSCALL_AUDIT);
954 if (!(context = audit_alloc_context(state))) {
956 audit_log_lost("out of memory in audit_alloc");
959 context->filterkey = key;
961 audit_set_context(tsk, context);
962 set_task_syscall_work(tsk, SYSCALL_AUDIT);
966 static inline void audit_free_context(struct audit_context *context)
968 audit_free_module(context);
969 audit_free_names(context);
970 unroll_tree_refs(context, NULL, 0);
971 free_tree_refs(context);
972 audit_free_aux(context);
973 kfree(context->filterkey);
974 kfree(context->sockaddr);
975 audit_proctitle_free(context);
979 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
980 kuid_t auid, kuid_t uid, unsigned int sessionid,
983 struct audit_buffer *ab;
988 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
992 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
993 from_kuid(&init_user_ns, auid),
994 from_kuid(&init_user_ns, uid), sessionid);
996 if (security_secid_to_secctx(sid, &ctx, &len)) {
997 audit_log_format(ab, " obj=(none)");
1000 audit_log_format(ab, " obj=%s", ctx);
1001 security_release_secctx(ctx, len);
1004 audit_log_format(ab, " ocomm=");
1005 audit_log_untrustedstring(ab, comm);
1011 static void audit_log_execve_info(struct audit_context *context,
1012 struct audit_buffer **ab)
1026 const char __user *p = (const char __user *)current->mm->arg_start;
1028 /* NOTE: this buffer needs to be large enough to hold all the non-arg
1029 * data we put in the audit record for this argument (see the
1030 * code below) ... at this point in time 96 is plenty */
1033 /* NOTE: we set MAX_EXECVE_AUDIT_LEN to a rather arbitrary limit, the
1034 * current value of 7500 is not as important as the fact that it
1035 * is less than 8k, a setting of 7500 gives us plenty of wiggle
1036 * room if we go over a little bit in the logging below */
1037 WARN_ON_ONCE(MAX_EXECVE_AUDIT_LEN > 7500);
1038 len_max = MAX_EXECVE_AUDIT_LEN;
1040 /* scratch buffer to hold the userspace args */
1041 buf_head = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1043 audit_panic("out of memory for argv string");
1048 audit_log_format(*ab, "argc=%d", context->execve.argc);
1053 require_data = true;
1058 /* NOTE: we don't ever want to trust this value for anything
1059 * serious, but the audit record format insists we
1060 * provide an argument length for really long arguments,
1061 * e.g. > MAX_EXECVE_AUDIT_LEN, so we have no choice but
1062 * to use strncpy_from_user() to obtain this value for
1063 * recording in the log, although we don't use it
1064 * anywhere here to avoid a double-fetch problem */
1066 len_full = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1068 /* read more data from userspace */
1070 /* can we make more room in the buffer? */
1071 if (buf != buf_head) {
1072 memmove(buf_head, buf, len_buf);
1076 /* fetch as much as we can of the argument */
1077 len_tmp = strncpy_from_user(&buf_head[len_buf], p,
1079 if (len_tmp == -EFAULT) {
1080 /* unable to copy from userspace */
1081 send_sig(SIGKILL, current, 0);
1083 } else if (len_tmp == (len_max - len_buf)) {
1084 /* buffer is not large enough */
1085 require_data = true;
1086 /* NOTE: if we are going to span multiple
1087 * buffers force the encoding so we stand
1088 * a chance at a sane len_full value and
1089 * consistent record encoding */
1091 len_full = len_full * 2;
1094 require_data = false;
1096 encode = audit_string_contains_control(
1098 /* try to use a trusted value for len_full */
1099 if (len_full < len_max)
1100 len_full = (encode ?
1101 len_tmp * 2 : len_tmp);
1105 buf_head[len_buf] = '\0';
1107 /* length of the buffer in the audit record? */
1108 len_abuf = (encode ? len_buf * 2 : len_buf + 2);
1111 /* write as much as we can to the audit log */
1113 /* NOTE: some magic numbers here - basically if we
1114 * can't fit a reasonable amount of data into the
1115 * existing audit buffer, flush it and start with
1117 if ((sizeof(abuf) + 8) > len_rem) {
1120 *ab = audit_log_start(context,
1121 GFP_KERNEL, AUDIT_EXECVE);
1126 /* create the non-arg portion of the arg record */
1128 if (require_data || (iter > 0) ||
1129 ((len_abuf + sizeof(abuf)) > len_rem)) {
1131 len_tmp += snprintf(&abuf[len_tmp],
1132 sizeof(abuf) - len_tmp,
1136 len_tmp += snprintf(&abuf[len_tmp],
1137 sizeof(abuf) - len_tmp,
1138 " a%d[%d]=", arg, iter++);
1140 len_tmp += snprintf(&abuf[len_tmp],
1141 sizeof(abuf) - len_tmp,
1143 WARN_ON(len_tmp >= sizeof(abuf));
1144 abuf[sizeof(abuf) - 1] = '\0';
1146 /* log the arg in the audit record */
1147 audit_log_format(*ab, "%s", abuf);
1151 if (len_abuf > len_rem)
1152 len_tmp = len_rem / 2; /* encoding */
1153 audit_log_n_hex(*ab, buf, len_tmp);
1154 len_rem -= len_tmp * 2;
1155 len_abuf -= len_tmp * 2;
1157 if (len_abuf > len_rem)
1158 len_tmp = len_rem - 2; /* quotes */
1159 audit_log_n_string(*ab, buf, len_tmp);
1160 len_rem -= len_tmp + 2;
1161 /* don't subtract the "2" because we still need
1162 * to add quotes to the remaining string */
1163 len_abuf -= len_tmp;
1169 /* ready to move to the next argument? */
1170 if ((len_buf == 0) && !require_data) {
1174 require_data = true;
1177 } while (arg < context->execve.argc);
1179 /* NOTE: the caller handles the final audit_log_end() call */
1185 static void audit_log_cap(struct audit_buffer *ab, char *prefix,
1190 if (cap_isclear(*cap)) {
1191 audit_log_format(ab, " %s=0", prefix);
1194 audit_log_format(ab, " %s=", prefix);
1196 audit_log_format(ab, "%08x", cap->cap[CAP_LAST_U32 - i]);
1199 static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1201 if (name->fcap_ver == -1) {
1202 audit_log_format(ab, " cap_fe=? cap_fver=? cap_fp=? cap_fi=?");
1205 audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
1206 audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
1207 audit_log_format(ab, " cap_fe=%d cap_fver=%x cap_frootid=%d",
1208 name->fcap.fE, name->fcap_ver,
1209 from_kuid(&init_user_ns, name->fcap.rootid));
1212 static void show_special(struct audit_context *context, int *call_panic)
1214 struct audit_buffer *ab;
1217 ab = audit_log_start(context, GFP_KERNEL, context->type);
1221 switch (context->type) {
1222 case AUDIT_SOCKETCALL: {
1223 int nargs = context->socketcall.nargs;
1225 audit_log_format(ab, "nargs=%d", nargs);
1226 for (i = 0; i < nargs; i++)
1227 audit_log_format(ab, " a%d=%lx", i,
1228 context->socketcall.args[i]);
1231 u32 osid = context->ipc.osid;
1233 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1234 from_kuid(&init_user_ns, context->ipc.uid),
1235 from_kgid(&init_user_ns, context->ipc.gid),
1241 if (security_secid_to_secctx(osid, &ctx, &len)) {
1242 audit_log_format(ab, " osid=%u", osid);
1245 audit_log_format(ab, " obj=%s", ctx);
1246 security_release_secctx(ctx, len);
1249 if (context->ipc.has_perm) {
1251 ab = audit_log_start(context, GFP_KERNEL,
1252 AUDIT_IPC_SET_PERM);
1255 audit_log_format(ab,
1256 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1257 context->ipc.qbytes,
1258 context->ipc.perm_uid,
1259 context->ipc.perm_gid,
1260 context->ipc.perm_mode);
1264 audit_log_format(ab,
1265 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1266 "mq_msgsize=%ld mq_curmsgs=%ld",
1267 context->mq_open.oflag, context->mq_open.mode,
1268 context->mq_open.attr.mq_flags,
1269 context->mq_open.attr.mq_maxmsg,
1270 context->mq_open.attr.mq_msgsize,
1271 context->mq_open.attr.mq_curmsgs);
1273 case AUDIT_MQ_SENDRECV:
1274 audit_log_format(ab,
1275 "mqdes=%d msg_len=%zd msg_prio=%u "
1276 "abs_timeout_sec=%lld abs_timeout_nsec=%ld",
1277 context->mq_sendrecv.mqdes,
1278 context->mq_sendrecv.msg_len,
1279 context->mq_sendrecv.msg_prio,
1280 (long long) context->mq_sendrecv.abs_timeout.tv_sec,
1281 context->mq_sendrecv.abs_timeout.tv_nsec);
1283 case AUDIT_MQ_NOTIFY:
1284 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1285 context->mq_notify.mqdes,
1286 context->mq_notify.sigev_signo);
1288 case AUDIT_MQ_GETSETATTR: {
1289 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1291 audit_log_format(ab,
1292 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1294 context->mq_getsetattr.mqdes,
1295 attr->mq_flags, attr->mq_maxmsg,
1296 attr->mq_msgsize, attr->mq_curmsgs);
1299 audit_log_format(ab, "pid=%d", context->capset.pid);
1300 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1301 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1302 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1303 audit_log_cap(ab, "cap_pa", &context->capset.cap.ambient);
1306 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1307 context->mmap.flags);
1310 audit_log_format(ab, "oflag=0%llo mode=0%llo resolve=0x%llx",
1311 context->openat2.flags,
1312 context->openat2.mode,
1313 context->openat2.resolve);
1316 audit_log_execve_info(context, &ab);
1318 case AUDIT_KERN_MODULE:
1319 audit_log_format(ab, "name=");
1320 if (context->module.name) {
1321 audit_log_untrustedstring(ab, context->module.name);
1323 audit_log_format(ab, "(null)");
1330 static inline int audit_proctitle_rtrim(char *proctitle, int len)
1332 char *end = proctitle + len - 1;
1334 while (end > proctitle && !isprint(*end))
1337 /* catch the case where proctitle is only 1 non-print character */
1338 len = end - proctitle + 1;
1339 len -= isprint(proctitle[len-1]) == 0;
1344 * audit_log_name - produce AUDIT_PATH record from struct audit_names
1345 * @context: audit_context for the task
1346 * @n: audit_names structure with reportable details
1347 * @path: optional path to report instead of audit_names->name
1348 * @record_num: record number to report when handling a list of names
1349 * @call_panic: optional pointer to int that will be updated if secid fails
1351 static void audit_log_name(struct audit_context *context, struct audit_names *n,
1352 const struct path *path, int record_num, int *call_panic)
1354 struct audit_buffer *ab;
1356 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1360 audit_log_format(ab, "item=%d", record_num);
1363 audit_log_d_path(ab, " name=", path);
1365 switch (n->name_len) {
1366 case AUDIT_NAME_FULL:
1367 /* log the full path */
1368 audit_log_format(ab, " name=");
1369 audit_log_untrustedstring(ab, n->name->name);
1372 /* name was specified as a relative path and the
1373 * directory component is the cwd
1375 if (context->pwd.dentry && context->pwd.mnt)
1376 audit_log_d_path(ab, " name=", &context->pwd);
1378 audit_log_format(ab, " name=(null)");
1381 /* log the name's directory component */
1382 audit_log_format(ab, " name=");
1383 audit_log_n_untrustedstring(ab, n->name->name,
1387 audit_log_format(ab, " name=(null)");
1389 if (n->ino != AUDIT_INO_UNSET)
1390 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#ho ouid=%u ogid=%u rdev=%02x:%02x",
1395 from_kuid(&init_user_ns, n->uid),
1396 from_kgid(&init_user_ns, n->gid),
1403 if (security_secid_to_secctx(
1404 n->osid, &ctx, &len)) {
1405 audit_log_format(ab, " osid=%u", n->osid);
1409 audit_log_format(ab, " obj=%s", ctx);
1410 security_release_secctx(ctx, len);
1414 /* log the audit_names record type */
1416 case AUDIT_TYPE_NORMAL:
1417 audit_log_format(ab, " nametype=NORMAL");
1419 case AUDIT_TYPE_PARENT:
1420 audit_log_format(ab, " nametype=PARENT");
1422 case AUDIT_TYPE_CHILD_DELETE:
1423 audit_log_format(ab, " nametype=DELETE");
1425 case AUDIT_TYPE_CHILD_CREATE:
1426 audit_log_format(ab, " nametype=CREATE");
1429 audit_log_format(ab, " nametype=UNKNOWN");
1433 audit_log_fcaps(ab, n);
1437 static void audit_log_proctitle(void)
1441 char *msg = "(null)";
1442 int len = strlen(msg);
1443 struct audit_context *context = audit_context();
1444 struct audit_buffer *ab;
1446 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
1448 return; /* audit_panic or being filtered */
1450 audit_log_format(ab, "proctitle=");
1453 if (!context->proctitle.value) {
1454 buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
1457 /* Historically called this from procfs naming */
1458 res = get_cmdline(current, buf, MAX_PROCTITLE_AUDIT_LEN);
1463 res = audit_proctitle_rtrim(buf, res);
1468 context->proctitle.value = buf;
1469 context->proctitle.len = res;
1471 msg = context->proctitle.value;
1472 len = context->proctitle.len;
1474 audit_log_n_untrustedstring(ab, msg, len);
1478 static void audit_log_exit(void)
1480 int i, call_panic = 0;
1481 struct audit_context *context = audit_context();
1482 struct audit_buffer *ab;
1483 struct audit_aux_data *aux;
1484 struct audit_names *n;
1486 context->personality = current->personality;
1488 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1490 return; /* audit_panic has been called */
1491 audit_log_format(ab, "arch=%x syscall=%d",
1492 context->arch, context->major);
1493 if (context->personality != PER_LINUX)
1494 audit_log_format(ab, " per=%lx", context->personality);
1495 if (context->return_valid != AUDITSC_INVALID)
1496 audit_log_format(ab, " success=%s exit=%ld",
1497 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1498 context->return_code);
1500 audit_log_format(ab,
1501 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1506 context->name_count);
1508 audit_log_task_info(ab);
1509 audit_log_key(ab, context->filterkey);
1512 for (aux = context->aux; aux; aux = aux->next) {
1514 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1516 continue; /* audit_panic has been called */
1518 switch (aux->type) {
1520 case AUDIT_BPRM_FCAPS: {
1521 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1523 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1524 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1525 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1526 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1527 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1528 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1529 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1530 audit_log_cap(ab, "old_pa", &axs->old_pcap.ambient);
1531 audit_log_cap(ab, "pp", &axs->new_pcap.permitted);
1532 audit_log_cap(ab, "pi", &axs->new_pcap.inheritable);
1533 audit_log_cap(ab, "pe", &axs->new_pcap.effective);
1534 audit_log_cap(ab, "pa", &axs->new_pcap.ambient);
1535 audit_log_format(ab, " frootid=%d",
1536 from_kuid(&init_user_ns,
1545 show_special(context, &call_panic);
1547 if (context->fds[0] >= 0) {
1548 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1550 audit_log_format(ab, "fd0=%d fd1=%d",
1551 context->fds[0], context->fds[1]);
1556 if (context->sockaddr_len) {
1557 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1559 audit_log_format(ab, "saddr=");
1560 audit_log_n_hex(ab, (void *)context->sockaddr,
1561 context->sockaddr_len);
1566 for (aux = context->aux_pids; aux; aux = aux->next) {
1567 struct audit_aux_data_pids *axs = (void *)aux;
1569 for (i = 0; i < axs->pid_count; i++)
1570 if (audit_log_pid_context(context, axs->target_pid[i],
1571 axs->target_auid[i],
1573 axs->target_sessionid[i],
1575 axs->target_comm[i]))
1579 if (context->target_pid &&
1580 audit_log_pid_context(context, context->target_pid,
1581 context->target_auid, context->target_uid,
1582 context->target_sessionid,
1583 context->target_sid, context->target_comm))
1586 if (context->pwd.dentry && context->pwd.mnt) {
1587 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1589 audit_log_d_path(ab, "cwd=", &context->pwd);
1595 list_for_each_entry(n, &context->names_list, list) {
1598 audit_log_name(context, n, NULL, i++, &call_panic);
1601 audit_log_proctitle();
1603 /* Send end of event record to help user space know we are finished */
1604 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1608 audit_panic("error converting sid to string");
1612 * __audit_free - free a per-task audit context
1613 * @tsk: task whose audit context block to free
1615 * Called from copy_process and do_exit
1617 void __audit_free(struct task_struct *tsk)
1619 struct audit_context *context = tsk->audit_context;
1624 if (!list_empty(&context->killed_trees))
1625 audit_kill_trees(context);
1627 /* We are called either by do_exit() or the fork() error handling code;
1628 * in the former case tsk == current and in the latter tsk is a
1629 * random task_struct that doesn't doesn't have any meaningful data we
1630 * need to log via audit_log_exit().
1632 if (tsk == current && !context->dummy && context->in_syscall) {
1633 context->return_valid = AUDITSC_INVALID;
1634 context->return_code = 0;
1636 audit_filter_syscall(tsk, context);
1637 audit_filter_inodes(tsk, context);
1638 if (context->current_state == AUDIT_STATE_RECORD)
1642 audit_set_context(tsk, NULL);
1643 audit_free_context(context);
1647 * __audit_syscall_entry - fill in an audit record at syscall entry
1648 * @major: major syscall type (function)
1649 * @a1: additional syscall register 1
1650 * @a2: additional syscall register 2
1651 * @a3: additional syscall register 3
1652 * @a4: additional syscall register 4
1654 * Fill in audit context at syscall entry. This only happens if the
1655 * audit context was created when the task was created and the state or
1656 * filters demand the audit context be built. If the state from the
1657 * per-task filter or from the per-syscall filter is AUDIT_STATE_RECORD,
1658 * then the record will be written at syscall exit time (otherwise, it
1659 * will only be written if another part of the kernel requests that it
1662 void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
1663 unsigned long a3, unsigned long a4)
1665 struct audit_context *context = audit_context();
1666 enum audit_state state;
1668 if (!audit_enabled || !context)
1671 BUG_ON(context->in_syscall || context->name_count);
1673 state = context->state;
1674 if (state == AUDIT_STATE_DISABLED)
1677 context->dummy = !audit_n_rules;
1678 if (!context->dummy && state == AUDIT_STATE_BUILD) {
1680 if (auditd_test_task(current))
1684 context->arch = syscall_get_arch(current);
1685 context->major = major;
1686 context->argv[0] = a1;
1687 context->argv[1] = a2;
1688 context->argv[2] = a3;
1689 context->argv[3] = a4;
1690 context->serial = 0;
1691 context->in_syscall = 1;
1692 context->current_state = state;
1694 ktime_get_coarse_real_ts64(&context->ctime);
1698 * __audit_syscall_exit - deallocate audit context after a system call
1699 * @success: success value of the syscall
1700 * @return_code: return value of the syscall
1702 * Tear down after system call. If the audit context has been marked as
1703 * auditable (either because of the AUDIT_STATE_RECORD state from
1704 * filtering, or because some other part of the kernel wrote an audit
1705 * message), then write out the syscall information. In call cases,
1706 * free the names stored from getname().
1708 void __audit_syscall_exit(int success, long return_code)
1710 struct audit_context *context;
1712 context = audit_context();
1716 if (!list_empty(&context->killed_trees))
1717 audit_kill_trees(context);
1719 if (!context->dummy && context->in_syscall) {
1721 context->return_valid = AUDITSC_SUCCESS;
1723 context->return_valid = AUDITSC_FAILURE;
1726 * we need to fix up the return code in the audit logs if the
1727 * actual return codes are later going to be fixed up by the
1728 * arch specific signal handlers
1730 * This is actually a test for:
1731 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
1732 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
1734 * but is faster than a bunch of ||
1736 if (unlikely(return_code <= -ERESTARTSYS) &&
1737 (return_code >= -ERESTART_RESTARTBLOCK) &&
1738 (return_code != -ENOIOCTLCMD))
1739 context->return_code = -EINTR;
1741 context->return_code = return_code;
1743 audit_filter_syscall(current, context);
1744 audit_filter_inodes(current, context);
1745 if (context->current_state == AUDIT_STATE_RECORD)
1749 context->in_syscall = 0;
1750 context->prio = context->state == AUDIT_STATE_RECORD ? ~0ULL : 0;
1752 audit_free_module(context);
1753 audit_free_names(context);
1754 unroll_tree_refs(context, NULL, 0);
1755 audit_free_aux(context);
1756 context->aux = NULL;
1757 context->aux_pids = NULL;
1758 context->target_pid = 0;
1759 context->target_sid = 0;
1760 context->sockaddr_len = 0;
1762 context->fds[0] = -1;
1763 if (context->state != AUDIT_STATE_RECORD) {
1764 kfree(context->filterkey);
1765 context->filterkey = NULL;
1769 static inline void handle_one(const struct inode *inode)
1771 struct audit_context *context;
1772 struct audit_tree_refs *p;
1773 struct audit_chunk *chunk;
1776 if (likely(!inode->i_fsnotify_marks))
1778 context = audit_context();
1780 count = context->tree_count;
1782 chunk = audit_tree_lookup(inode);
1786 if (likely(put_tree_ref(context, chunk)))
1788 if (unlikely(!grow_tree_refs(context))) {
1789 pr_warn("out of memory, audit has lost a tree reference\n");
1790 audit_set_auditable(context);
1791 audit_put_chunk(chunk);
1792 unroll_tree_refs(context, p, count);
1795 put_tree_ref(context, chunk);
1798 static void handle_path(const struct dentry *dentry)
1800 struct audit_context *context;
1801 struct audit_tree_refs *p;
1802 const struct dentry *d, *parent;
1803 struct audit_chunk *drop;
1807 context = audit_context();
1809 count = context->tree_count;
1814 seq = read_seqbegin(&rename_lock);
1816 struct inode *inode = d_backing_inode(d);
1818 if (inode && unlikely(inode->i_fsnotify_marks)) {
1819 struct audit_chunk *chunk;
1821 chunk = audit_tree_lookup(inode);
1823 if (unlikely(!put_tree_ref(context, chunk))) {
1829 parent = d->d_parent;
1834 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1837 /* just a race with rename */
1838 unroll_tree_refs(context, p, count);
1841 audit_put_chunk(drop);
1842 if (grow_tree_refs(context)) {
1843 /* OK, got more space */
1844 unroll_tree_refs(context, p, count);
1848 pr_warn("out of memory, audit has lost a tree reference\n");
1849 unroll_tree_refs(context, p, count);
1850 audit_set_auditable(context);
1856 static struct audit_names *audit_alloc_name(struct audit_context *context,
1859 struct audit_names *aname;
1861 if (context->name_count < AUDIT_NAMES) {
1862 aname = &context->preallocated_names[context->name_count];
1863 memset(aname, 0, sizeof(*aname));
1865 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1868 aname->should_free = true;
1871 aname->ino = AUDIT_INO_UNSET;
1873 list_add_tail(&aname->list, &context->names_list);
1875 context->name_count++;
1876 if (!context->pwd.dentry)
1877 get_fs_pwd(current->fs, &context->pwd);
1882 * __audit_reusename - fill out filename with info from existing entry
1883 * @uptr: userland ptr to pathname
1885 * Search the audit_names list for the current audit context. If there is an
1886 * existing entry with a matching "uptr" then return the filename
1887 * associated with that audit_name. If not, return NULL.
1890 __audit_reusename(const __user char *uptr)
1892 struct audit_context *context = audit_context();
1893 struct audit_names *n;
1895 list_for_each_entry(n, &context->names_list, list) {
1898 if (n->name->uptr == uptr) {
1907 * __audit_getname - add a name to the list
1908 * @name: name to add
1910 * Add a name to the list of audit names for this context.
1911 * Called from fs/namei.c:getname().
1913 void __audit_getname(struct filename *name)
1915 struct audit_context *context = audit_context();
1916 struct audit_names *n;
1918 if (!context->in_syscall)
1921 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
1926 n->name_len = AUDIT_NAME_FULL;
1931 static inline int audit_copy_fcaps(struct audit_names *name,
1932 const struct dentry *dentry)
1934 struct cpu_vfs_cap_data caps;
1940 rc = get_vfs_caps_from_disk(&init_user_ns, dentry, &caps);
1944 name->fcap.permitted = caps.permitted;
1945 name->fcap.inheritable = caps.inheritable;
1946 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1947 name->fcap.rootid = caps.rootid;
1948 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1949 VFS_CAP_REVISION_SHIFT;
1954 /* Copy inode data into an audit_names. */
1955 static void audit_copy_inode(struct audit_names *name,
1956 const struct dentry *dentry,
1957 struct inode *inode, unsigned int flags)
1959 name->ino = inode->i_ino;
1960 name->dev = inode->i_sb->s_dev;
1961 name->mode = inode->i_mode;
1962 name->uid = inode->i_uid;
1963 name->gid = inode->i_gid;
1964 name->rdev = inode->i_rdev;
1965 security_inode_getsecid(inode, &name->osid);
1966 if (flags & AUDIT_INODE_NOEVAL) {
1967 name->fcap_ver = -1;
1970 audit_copy_fcaps(name, dentry);
1974 * __audit_inode - store the inode and device from a lookup
1975 * @name: name being audited
1976 * @dentry: dentry being audited
1977 * @flags: attributes for this particular entry
1979 void __audit_inode(struct filename *name, const struct dentry *dentry,
1982 struct audit_context *context = audit_context();
1983 struct inode *inode = d_backing_inode(dentry);
1984 struct audit_names *n;
1985 bool parent = flags & AUDIT_INODE_PARENT;
1986 struct audit_entry *e;
1987 struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
1990 if (!context->in_syscall)
1994 list_for_each_entry_rcu(e, list, list) {
1995 for (i = 0; i < e->rule.field_count; i++) {
1996 struct audit_field *f = &e->rule.fields[i];
1998 if (f->type == AUDIT_FSTYPE
1999 && audit_comparator(inode->i_sb->s_magic,
2001 && e->rule.action == AUDIT_NEVER) {
2013 * If we have a pointer to an audit_names entry already, then we can
2014 * just use it directly if the type is correct.
2019 if (n->type == AUDIT_TYPE_PARENT ||
2020 n->type == AUDIT_TYPE_UNKNOWN)
2023 if (n->type != AUDIT_TYPE_PARENT)
2028 list_for_each_entry_reverse(n, &context->names_list, list) {
2030 /* valid inode number, use that for the comparison */
2031 if (n->ino != inode->i_ino ||
2032 n->dev != inode->i_sb->s_dev)
2034 } else if (n->name) {
2035 /* inode number has not been set, check the name */
2036 if (strcmp(n->name->name, name->name))
2039 /* no inode and no name (?!) ... this is odd ... */
2042 /* match the correct record type */
2044 if (n->type == AUDIT_TYPE_PARENT ||
2045 n->type == AUDIT_TYPE_UNKNOWN)
2048 if (n->type != AUDIT_TYPE_PARENT)
2054 /* unable to find an entry with both a matching name and type */
2055 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
2065 n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
2066 n->type = AUDIT_TYPE_PARENT;
2067 if (flags & AUDIT_INODE_HIDDEN)
2070 n->name_len = AUDIT_NAME_FULL;
2071 n->type = AUDIT_TYPE_NORMAL;
2073 handle_path(dentry);
2074 audit_copy_inode(n, dentry, inode, flags & AUDIT_INODE_NOEVAL);
2077 void __audit_file(const struct file *file)
2079 __audit_inode(NULL, file->f_path.dentry, 0);
2083 * __audit_inode_child - collect inode info for created/removed objects
2084 * @parent: inode of dentry parent
2085 * @dentry: dentry being audited
2086 * @type: AUDIT_TYPE_* value that we're looking for
2088 * For syscalls that create or remove filesystem objects, audit_inode
2089 * can only collect information for the filesystem object's parent.
2090 * This call updates the audit context with the child's information.
2091 * Syscalls that create a new filesystem object must be hooked after
2092 * the object is created. Syscalls that remove a filesystem object
2093 * must be hooked prior, in order to capture the target inode during
2094 * unsuccessful attempts.
2096 void __audit_inode_child(struct inode *parent,
2097 const struct dentry *dentry,
2098 const unsigned char type)
2100 struct audit_context *context = audit_context();
2101 struct inode *inode = d_backing_inode(dentry);
2102 const struct qstr *dname = &dentry->d_name;
2103 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
2104 struct audit_entry *e;
2105 struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
2108 if (!context->in_syscall)
2112 list_for_each_entry_rcu(e, list, list) {
2113 for (i = 0; i < e->rule.field_count; i++) {
2114 struct audit_field *f = &e->rule.fields[i];
2116 if (f->type == AUDIT_FSTYPE
2117 && audit_comparator(parent->i_sb->s_magic,
2119 && e->rule.action == AUDIT_NEVER) {
2130 /* look for a parent entry first */
2131 list_for_each_entry(n, &context->names_list, list) {
2133 (n->type != AUDIT_TYPE_PARENT &&
2134 n->type != AUDIT_TYPE_UNKNOWN))
2137 if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
2138 !audit_compare_dname_path(dname,
2139 n->name->name, n->name_len)) {
2140 if (n->type == AUDIT_TYPE_UNKNOWN)
2141 n->type = AUDIT_TYPE_PARENT;
2147 /* is there a matching child entry? */
2148 list_for_each_entry(n, &context->names_list, list) {
2149 /* can only match entries that have a name */
2151 (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
2154 if (!strcmp(dname->name, n->name->name) ||
2155 !audit_compare_dname_path(dname, n->name->name,
2157 found_parent->name_len :
2159 if (n->type == AUDIT_TYPE_UNKNOWN)
2166 if (!found_parent) {
2167 /* create a new, "anonymous" parent record */
2168 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
2171 audit_copy_inode(n, NULL, parent, 0);
2175 found_child = audit_alloc_name(context, type);
2179 /* Re-use the name belonging to the slot for a matching parent
2180 * directory. All names for this context are relinquished in
2181 * audit_free_names() */
2183 found_child->name = found_parent->name;
2184 found_child->name_len = AUDIT_NAME_FULL;
2185 found_child->name->refcnt++;
2190 audit_copy_inode(found_child, dentry, inode, 0);
2192 found_child->ino = AUDIT_INO_UNSET;
2194 EXPORT_SYMBOL_GPL(__audit_inode_child);
2197 * auditsc_get_stamp - get local copies of audit_context values
2198 * @ctx: audit_context for the task
2199 * @t: timespec64 to store time recorded in the audit_context
2200 * @serial: serial value that is recorded in the audit_context
2202 * Also sets the context as auditable.
2204 int auditsc_get_stamp(struct audit_context *ctx,
2205 struct timespec64 *t, unsigned int *serial)
2207 if (!ctx->in_syscall)
2210 ctx->serial = audit_serial();
2211 t->tv_sec = ctx->ctime.tv_sec;
2212 t->tv_nsec = ctx->ctime.tv_nsec;
2213 *serial = ctx->serial;
2216 ctx->current_state = AUDIT_STATE_RECORD;
2222 * __audit_mq_open - record audit data for a POSIX MQ open
2225 * @attr: queue attributes
2228 void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2230 struct audit_context *context = audit_context();
2233 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2235 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2237 context->mq_open.oflag = oflag;
2238 context->mq_open.mode = mode;
2240 context->type = AUDIT_MQ_OPEN;
2244 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2245 * @mqdes: MQ descriptor
2246 * @msg_len: Message length
2247 * @msg_prio: Message priority
2248 * @abs_timeout: Message timeout in absolute time
2251 void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2252 const struct timespec64 *abs_timeout)
2254 struct audit_context *context = audit_context();
2255 struct timespec64 *p = &context->mq_sendrecv.abs_timeout;
2258 memcpy(p, abs_timeout, sizeof(*p));
2260 memset(p, 0, sizeof(*p));
2262 context->mq_sendrecv.mqdes = mqdes;
2263 context->mq_sendrecv.msg_len = msg_len;
2264 context->mq_sendrecv.msg_prio = msg_prio;
2266 context->type = AUDIT_MQ_SENDRECV;
2270 * __audit_mq_notify - record audit data for a POSIX MQ notify
2271 * @mqdes: MQ descriptor
2272 * @notification: Notification event
2276 void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2278 struct audit_context *context = audit_context();
2281 context->mq_notify.sigev_signo = notification->sigev_signo;
2283 context->mq_notify.sigev_signo = 0;
2285 context->mq_notify.mqdes = mqdes;
2286 context->type = AUDIT_MQ_NOTIFY;
2290 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2291 * @mqdes: MQ descriptor
2295 void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2297 struct audit_context *context = audit_context();
2299 context->mq_getsetattr.mqdes = mqdes;
2300 context->mq_getsetattr.mqstat = *mqstat;
2301 context->type = AUDIT_MQ_GETSETATTR;
2305 * __audit_ipc_obj - record audit data for ipc object
2306 * @ipcp: ipc permissions
2309 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2311 struct audit_context *context = audit_context();
2313 context->ipc.uid = ipcp->uid;
2314 context->ipc.gid = ipcp->gid;
2315 context->ipc.mode = ipcp->mode;
2316 context->ipc.has_perm = 0;
2317 security_ipc_getsecid(ipcp, &context->ipc.osid);
2318 context->type = AUDIT_IPC;
2322 * __audit_ipc_set_perm - record audit data for new ipc permissions
2323 * @qbytes: msgq bytes
2324 * @uid: msgq user id
2325 * @gid: msgq group id
2326 * @mode: msgq mode (permissions)
2328 * Called only after audit_ipc_obj().
2330 void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2332 struct audit_context *context = audit_context();
2334 context->ipc.qbytes = qbytes;
2335 context->ipc.perm_uid = uid;
2336 context->ipc.perm_gid = gid;
2337 context->ipc.perm_mode = mode;
2338 context->ipc.has_perm = 1;
2341 void __audit_bprm(struct linux_binprm *bprm)
2343 struct audit_context *context = audit_context();
2345 context->type = AUDIT_EXECVE;
2346 context->execve.argc = bprm->argc;
2351 * __audit_socketcall - record audit data for sys_socketcall
2352 * @nargs: number of args, which should not be more than AUDITSC_ARGS.
2356 int __audit_socketcall(int nargs, unsigned long *args)
2358 struct audit_context *context = audit_context();
2360 if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2362 context->type = AUDIT_SOCKETCALL;
2363 context->socketcall.nargs = nargs;
2364 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2369 * __audit_fd_pair - record audit data for pipe and socketpair
2370 * @fd1: the first file descriptor
2371 * @fd2: the second file descriptor
2374 void __audit_fd_pair(int fd1, int fd2)
2376 struct audit_context *context = audit_context();
2378 context->fds[0] = fd1;
2379 context->fds[1] = fd2;
2383 * __audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2384 * @len: data length in user space
2385 * @a: data address in kernel space
2387 * Returns 0 for success or NULL context or < 0 on error.
2389 int __audit_sockaddr(int len, void *a)
2391 struct audit_context *context = audit_context();
2393 if (!context->sockaddr) {
2394 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2398 context->sockaddr = p;
2401 context->sockaddr_len = len;
2402 memcpy(context->sockaddr, a, len);
2406 void __audit_ptrace(struct task_struct *t)
2408 struct audit_context *context = audit_context();
2410 context->target_pid = task_tgid_nr(t);
2411 context->target_auid = audit_get_loginuid(t);
2412 context->target_uid = task_uid(t);
2413 context->target_sessionid = audit_get_sessionid(t);
2414 security_task_getsecid_obj(t, &context->target_sid);
2415 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2419 * audit_signal_info_syscall - record signal info for syscalls
2420 * @t: task being signaled
2422 * If the audit subsystem is being terminated, record the task (pid)
2423 * and uid that is doing that.
2425 int audit_signal_info_syscall(struct task_struct *t)
2427 struct audit_aux_data_pids *axp;
2428 struct audit_context *ctx = audit_context();
2429 kuid_t t_uid = task_uid(t);
2431 if (!audit_signals || audit_dummy_context())
2434 /* optimize the common case by putting first signal recipient directly
2435 * in audit_context */
2436 if (!ctx->target_pid) {
2437 ctx->target_pid = task_tgid_nr(t);
2438 ctx->target_auid = audit_get_loginuid(t);
2439 ctx->target_uid = t_uid;
2440 ctx->target_sessionid = audit_get_sessionid(t);
2441 security_task_getsecid_obj(t, &ctx->target_sid);
2442 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2446 axp = (void *)ctx->aux_pids;
2447 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2448 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2452 axp->d.type = AUDIT_OBJ_PID;
2453 axp->d.next = ctx->aux_pids;
2454 ctx->aux_pids = (void *)axp;
2456 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2458 axp->target_pid[axp->pid_count] = task_tgid_nr(t);
2459 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2460 axp->target_uid[axp->pid_count] = t_uid;
2461 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2462 security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]);
2463 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2470 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2471 * @bprm: pointer to the bprm being processed
2472 * @new: the proposed new credentials
2473 * @old: the old credentials
2475 * Simply check if the proc already has the caps given by the file and if not
2476 * store the priv escalation info for later auditing at the end of the syscall
2480 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2481 const struct cred *new, const struct cred *old)
2483 struct audit_aux_data_bprm_fcaps *ax;
2484 struct audit_context *context = audit_context();
2485 struct cpu_vfs_cap_data vcaps;
2487 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2491 ax->d.type = AUDIT_BPRM_FCAPS;
2492 ax->d.next = context->aux;
2493 context->aux = (void *)ax;
2495 get_vfs_caps_from_disk(&init_user_ns,
2496 bprm->file->f_path.dentry, &vcaps);
2498 ax->fcap.permitted = vcaps.permitted;
2499 ax->fcap.inheritable = vcaps.inheritable;
2500 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2501 ax->fcap.rootid = vcaps.rootid;
2502 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2504 ax->old_pcap.permitted = old->cap_permitted;
2505 ax->old_pcap.inheritable = old->cap_inheritable;
2506 ax->old_pcap.effective = old->cap_effective;
2507 ax->old_pcap.ambient = old->cap_ambient;
2509 ax->new_pcap.permitted = new->cap_permitted;
2510 ax->new_pcap.inheritable = new->cap_inheritable;
2511 ax->new_pcap.effective = new->cap_effective;
2512 ax->new_pcap.ambient = new->cap_ambient;
2517 * __audit_log_capset - store information about the arguments to the capset syscall
2518 * @new: the new credentials
2519 * @old: the old (current) credentials
2521 * Record the arguments userspace sent to sys_capset for later printing by the
2522 * audit system if applicable
2524 void __audit_log_capset(const struct cred *new, const struct cred *old)
2526 struct audit_context *context = audit_context();
2528 context->capset.pid = task_tgid_nr(current);
2529 context->capset.cap.effective = new->cap_effective;
2530 context->capset.cap.inheritable = new->cap_effective;
2531 context->capset.cap.permitted = new->cap_permitted;
2532 context->capset.cap.ambient = new->cap_ambient;
2533 context->type = AUDIT_CAPSET;
2536 void __audit_mmap_fd(int fd, int flags)
2538 struct audit_context *context = audit_context();
2540 context->mmap.fd = fd;
2541 context->mmap.flags = flags;
2542 context->type = AUDIT_MMAP;
2545 void __audit_openat2_how(struct open_how *how)
2547 struct audit_context *context = audit_context();
2549 context->openat2.flags = how->flags;
2550 context->openat2.mode = how->mode;
2551 context->openat2.resolve = how->resolve;
2552 context->type = AUDIT_OPENAT2;
2555 void __audit_log_kern_module(char *name)
2557 struct audit_context *context = audit_context();
2559 context->module.name = kstrdup(name, GFP_KERNEL);
2560 if (!context->module.name)
2561 audit_log_lost("out of memory in __audit_log_kern_module");
2562 context->type = AUDIT_KERN_MODULE;
2565 void __audit_fanotify(unsigned int response)
2567 audit_log(audit_context(), GFP_KERNEL,
2568 AUDIT_FANOTIFY, "resp=%u", response);
2571 void __audit_tk_injoffset(struct timespec64 offset)
2573 audit_log(audit_context(), GFP_KERNEL, AUDIT_TIME_INJOFFSET,
2574 "sec=%lli nsec=%li",
2575 (long long)offset.tv_sec, offset.tv_nsec);
2578 static void audit_log_ntp_val(const struct audit_ntp_data *ad,
2579 const char *op, enum audit_ntp_type type)
2581 const struct audit_ntp_val *val = &ad->vals[type];
2583 if (val->newval == val->oldval)
2586 audit_log(audit_context(), GFP_KERNEL, AUDIT_TIME_ADJNTPVAL,
2587 "op=%s old=%lli new=%lli", op, val->oldval, val->newval);
2590 void __audit_ntp_log(const struct audit_ntp_data *ad)
2592 audit_log_ntp_val(ad, "offset", AUDIT_NTP_OFFSET);
2593 audit_log_ntp_val(ad, "freq", AUDIT_NTP_FREQ);
2594 audit_log_ntp_val(ad, "status", AUDIT_NTP_STATUS);
2595 audit_log_ntp_val(ad, "tai", AUDIT_NTP_TAI);
2596 audit_log_ntp_val(ad, "tick", AUDIT_NTP_TICK);
2597 audit_log_ntp_val(ad, "adjust", AUDIT_NTP_ADJUST);
2600 void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries,
2601 enum audit_nfcfgop op, gfp_t gfp)
2603 struct audit_buffer *ab;
2604 char comm[sizeof(current->comm)];
2606 ab = audit_log_start(audit_context(), gfp, AUDIT_NETFILTER_CFG);
2609 audit_log_format(ab, "table=%s family=%u entries=%u op=%s",
2610 name, af, nentries, audit_nfcfgs[op].s);
2612 audit_log_format(ab, " pid=%u", task_pid_nr(current));
2613 audit_log_task_context(ab); /* subj= */
2614 audit_log_format(ab, " comm=");
2615 audit_log_untrustedstring(ab, get_task_comm(comm, current));
2618 EXPORT_SYMBOL_GPL(__audit_log_nfcfg);
2620 static void audit_log_task(struct audit_buffer *ab)
2624 unsigned int sessionid;
2625 char comm[sizeof(current->comm)];
2627 auid = audit_get_loginuid(current);
2628 sessionid = audit_get_sessionid(current);
2629 current_uid_gid(&uid, &gid);
2631 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2632 from_kuid(&init_user_ns, auid),
2633 from_kuid(&init_user_ns, uid),
2634 from_kgid(&init_user_ns, gid),
2636 audit_log_task_context(ab);
2637 audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
2638 audit_log_untrustedstring(ab, get_task_comm(comm, current));
2639 audit_log_d_path_exe(ab, current->mm);
2643 * audit_core_dumps - record information about processes that end abnormally
2644 * @signr: signal value
2646 * If a process ends with a core dump, something fishy is going on and we
2647 * should record the event for investigation.
2649 void audit_core_dumps(long signr)
2651 struct audit_buffer *ab;
2656 if (signr == SIGQUIT) /* don't care for those */
2659 ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_ANOM_ABEND);
2663 audit_log_format(ab, " sig=%ld res=1", signr);
2668 * audit_seccomp - record information about a seccomp action
2669 * @syscall: syscall number
2670 * @signr: signal value
2671 * @code: the seccomp action
2673 * Record the information associated with a seccomp action. Event filtering for
2674 * seccomp actions that are not to be logged is done in seccomp_log().
2675 * Therefore, this function forces auditing independent of the audit_enabled
2676 * and dummy context state because seccomp actions should be logged even when
2677 * audit is not in use.
2679 void audit_seccomp(unsigned long syscall, long signr, int code)
2681 struct audit_buffer *ab;
2683 ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_SECCOMP);
2687 audit_log_format(ab, " sig=%ld arch=%x syscall=%ld compat=%d ip=0x%lx code=0x%x",
2688 signr, syscall_get_arch(current), syscall,
2689 in_compat_syscall(), KSTK_EIP(current), code);
2693 void audit_seccomp_actions_logged(const char *names, const char *old_names,
2696 struct audit_buffer *ab;
2701 ab = audit_log_start(audit_context(), GFP_KERNEL,
2702 AUDIT_CONFIG_CHANGE);
2706 audit_log_format(ab,
2707 "op=seccomp-logging actions=%s old-actions=%s res=%d",
2708 names, old_names, res);
2712 struct list_head *audit_killed_trees(void)
2714 struct audit_context *ctx = audit_context();
2716 if (likely(!ctx || !ctx->in_syscall))
2718 return &ctx->killed_trees;