xref: /linux/kernel/auditsc.c (revision 870b7fdc660b38c4e1bd8bf48e62aa352ddf8f42)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* auditsc.c -- System-call auditing support
3  * Handles all system-call specific auditing features.
4  *
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
8  * All Rights Reserved.
9  *
10  * Written by Rickard E. (Rik) Faith <faith@redhat.com>
11  *
12  * Many of the ideas implemented here are from Stephen C. Tweedie,
13  * especially the idea of avoiding a copy by using getname.
14  *
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.
18  *
19  * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
20  * 2006.
21  *
22  * The support of additional filter rules compares (>, <, >=, <=) was
23  * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
24  *
25  * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
26  * filesystem information.
27  *
28  * Subject and object context labeling support added by <danjones@us.ibm.com>
29  * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
30  */
31 
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 
34 #include <linux/init.h>
35 #include <asm/types.h>
36 #include <linux/atomic.h>
37 #include <linux/fs.h>
38 #include <linux/namei.h>
39 #include <linux/mm.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
67 #include <uapi/linux/fanotify.h>
68 
69 #include "audit.h"
70 
71 /* flags stating the success for a syscall */
72 #define AUDITSC_INVALID 0
73 #define AUDITSC_SUCCESS 1
74 #define AUDITSC_FAILURE 2
75 
76 /* no execve audit message should be longer than this (userspace limits),
77  * see the note near the top of audit_log_execve_info() about this value */
78 #define MAX_EXECVE_AUDIT_LEN 7500
79 
80 /* max length to print of cmdline/proctitle value during audit */
81 #define MAX_PROCTITLE_AUDIT_LEN 128
82 
83 /* number of audit rules */
84 int audit_n_rules;
85 
86 /* determines whether we collect data for signals sent */
87 int audit_signals;
88 
89 struct audit_aux_data {
90 	struct audit_aux_data	*next;
91 	int			type;
92 };
93 
94 /* Number of target pids per aux struct. */
95 #define AUDIT_AUX_PIDS	16
96 
97 struct audit_aux_data_pids {
98 	struct audit_aux_data	d;
99 	pid_t			target_pid[AUDIT_AUX_PIDS];
100 	kuid_t			target_auid[AUDIT_AUX_PIDS];
101 	kuid_t			target_uid[AUDIT_AUX_PIDS];
102 	unsigned int		target_sessionid[AUDIT_AUX_PIDS];
103 	u32			target_sid[AUDIT_AUX_PIDS];
104 	char 			target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
105 	int			pid_count;
106 };
107 
108 struct audit_aux_data_bprm_fcaps {
109 	struct audit_aux_data	d;
110 	struct audit_cap_data	fcap;
111 	unsigned int		fcap_ver;
112 	struct audit_cap_data	old_pcap;
113 	struct audit_cap_data	new_pcap;
114 };
115 
116 struct audit_tree_refs {
117 	struct audit_tree_refs *next;
118 	struct audit_chunk *c[31];
119 };
120 
121 struct audit_nfcfgop_tab {
122 	enum audit_nfcfgop	op;
123 	const char		*s;
124 };
125 
126 static const struct audit_nfcfgop_tab audit_nfcfgs[] = {
127 	{ AUDIT_XT_OP_REGISTER,			"xt_register"		   },
128 	{ AUDIT_XT_OP_REPLACE,			"xt_replace"		   },
129 	{ AUDIT_XT_OP_UNREGISTER,		"xt_unregister"		   },
130 	{ AUDIT_NFT_OP_TABLE_REGISTER,		"nft_register_table"	   },
131 	{ AUDIT_NFT_OP_TABLE_UNREGISTER,	"nft_unregister_table"	   },
132 	{ AUDIT_NFT_OP_CHAIN_REGISTER,		"nft_register_chain"	   },
133 	{ AUDIT_NFT_OP_CHAIN_UNREGISTER,	"nft_unregister_chain"	   },
134 	{ AUDIT_NFT_OP_RULE_REGISTER,		"nft_register_rule"	   },
135 	{ AUDIT_NFT_OP_RULE_UNREGISTER,		"nft_unregister_rule"	   },
136 	{ AUDIT_NFT_OP_SET_REGISTER,		"nft_register_set"	   },
137 	{ AUDIT_NFT_OP_SET_UNREGISTER,		"nft_unregister_set"	   },
138 	{ AUDIT_NFT_OP_SETELEM_REGISTER,	"nft_register_setelem"	   },
139 	{ AUDIT_NFT_OP_SETELEM_UNREGISTER,	"nft_unregister_setelem"   },
140 	{ AUDIT_NFT_OP_GEN_REGISTER,		"nft_register_gen"	   },
141 	{ AUDIT_NFT_OP_OBJ_REGISTER,		"nft_register_obj"	   },
142 	{ AUDIT_NFT_OP_OBJ_UNREGISTER,		"nft_unregister_obj"	   },
143 	{ AUDIT_NFT_OP_OBJ_RESET,		"nft_reset_obj"		   },
144 	{ AUDIT_NFT_OP_FLOWTABLE_REGISTER,	"nft_register_flowtable"   },
145 	{ AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,	"nft_unregister_flowtable" },
146 	{ AUDIT_NFT_OP_SETELEM_RESET,		"nft_reset_setelem"        },
147 	{ AUDIT_NFT_OP_RULE_RESET,		"nft_reset_rule"           },
148 	{ AUDIT_NFT_OP_INVALID,			"nft_invalid"		   },
149 };
150 
151 static int audit_match_perm(struct audit_context *ctx, int mask)
152 {
153 	unsigned n;
154 
155 	if (unlikely(!ctx))
156 		return 0;
157 	n = ctx->major;
158 
159 	switch (audit_classify_syscall(ctx->arch, n)) {
160 	case AUDITSC_NATIVE:
161 		if ((mask & AUDIT_PERM_WRITE) &&
162 		     audit_match_class(AUDIT_CLASS_WRITE, n))
163 			return 1;
164 		if ((mask & AUDIT_PERM_READ) &&
165 		     audit_match_class(AUDIT_CLASS_READ, n))
166 			return 1;
167 		if ((mask & AUDIT_PERM_ATTR) &&
168 		     audit_match_class(AUDIT_CLASS_CHATTR, n))
169 			return 1;
170 		return 0;
171 	case AUDITSC_COMPAT: /* 32bit on biarch */
172 		if ((mask & AUDIT_PERM_WRITE) &&
173 		     audit_match_class(AUDIT_CLASS_WRITE_32, n))
174 			return 1;
175 		if ((mask & AUDIT_PERM_READ) &&
176 		     audit_match_class(AUDIT_CLASS_READ_32, n))
177 			return 1;
178 		if ((mask & AUDIT_PERM_ATTR) &&
179 		     audit_match_class(AUDIT_CLASS_CHATTR_32, n))
180 			return 1;
181 		return 0;
182 	case AUDITSC_OPEN:
183 		return mask & ACC_MODE(ctx->argv[1]);
184 	case AUDITSC_OPENAT:
185 		return mask & ACC_MODE(ctx->argv[2]);
186 	case AUDITSC_SOCKETCALL:
187 		return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
188 	case AUDITSC_EXECVE:
189 		return mask & AUDIT_PERM_EXEC;
190 	case AUDITSC_OPENAT2:
191 		return mask & ACC_MODE((u32)ctx->openat2.flags);
192 	default:
193 		return 0;
194 	}
195 }
196 
197 static int audit_match_filetype(struct audit_context *ctx, int val)
198 {
199 	struct audit_names *n;
200 	umode_t mode = (umode_t)val;
201 
202 	if (unlikely(!ctx))
203 		return 0;
204 
205 	list_for_each_entry(n, &ctx->names_list, list) {
206 		if ((n->ino != AUDIT_INO_UNSET) &&
207 		    ((n->mode & S_IFMT) == mode))
208 			return 1;
209 	}
210 
211 	return 0;
212 }
213 
214 /*
215  * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
216  * ->first_trees points to its beginning, ->trees - to the current end of data.
217  * ->tree_count is the number of free entries in array pointed to by ->trees.
218  * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
219  * "empty" becomes (p, p, 31) afterwards.  We don't shrink the list (and seriously,
220  * it's going to remain 1-element for almost any setup) until we free context itself.
221  * References in it _are_ dropped - at the same time we free/drop aux stuff.
222  */
223 
224 static void audit_set_auditable(struct audit_context *ctx)
225 {
226 	if (!ctx->prio) {
227 		ctx->prio = 1;
228 		ctx->current_state = AUDIT_STATE_RECORD;
229 	}
230 }
231 
232 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
233 {
234 	struct audit_tree_refs *p = ctx->trees;
235 	int left = ctx->tree_count;
236 
237 	if (likely(left)) {
238 		p->c[--left] = chunk;
239 		ctx->tree_count = left;
240 		return 1;
241 	}
242 	if (!p)
243 		return 0;
244 	p = p->next;
245 	if (p) {
246 		p->c[30] = chunk;
247 		ctx->trees = p;
248 		ctx->tree_count = 30;
249 		return 1;
250 	}
251 	return 0;
252 }
253 
254 static int grow_tree_refs(struct audit_context *ctx)
255 {
256 	struct audit_tree_refs *p = ctx->trees;
257 
258 	ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
259 	if (!ctx->trees) {
260 		ctx->trees = p;
261 		return 0;
262 	}
263 	if (p)
264 		p->next = ctx->trees;
265 	else
266 		ctx->first_trees = ctx->trees;
267 	ctx->tree_count = 31;
268 	return 1;
269 }
270 
271 static void unroll_tree_refs(struct audit_context *ctx,
272 		      struct audit_tree_refs *p, int count)
273 {
274 	struct audit_tree_refs *q;
275 	int n;
276 
277 	if (!p) {
278 		/* we started with empty chain */
279 		p = ctx->first_trees;
280 		count = 31;
281 		/* if the very first allocation has failed, nothing to do */
282 		if (!p)
283 			return;
284 	}
285 	n = count;
286 	for (q = p; q != ctx->trees; q = q->next, n = 31) {
287 		while (n--) {
288 			audit_put_chunk(q->c[n]);
289 			q->c[n] = NULL;
290 		}
291 	}
292 	while (n-- > ctx->tree_count) {
293 		audit_put_chunk(q->c[n]);
294 		q->c[n] = NULL;
295 	}
296 	ctx->trees = p;
297 	ctx->tree_count = count;
298 }
299 
300 static void free_tree_refs(struct audit_context *ctx)
301 {
302 	struct audit_tree_refs *p, *q;
303 
304 	for (p = ctx->first_trees; p; p = q) {
305 		q = p->next;
306 		kfree(p);
307 	}
308 }
309 
310 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
311 {
312 	struct audit_tree_refs *p;
313 	int n;
314 
315 	if (!tree)
316 		return 0;
317 	/* full ones */
318 	for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
319 		for (n = 0; n < 31; n++)
320 			if (audit_tree_match(p->c[n], tree))
321 				return 1;
322 	}
323 	/* partial */
324 	if (p) {
325 		for (n = ctx->tree_count; n < 31; n++)
326 			if (audit_tree_match(p->c[n], tree))
327 				return 1;
328 	}
329 	return 0;
330 }
331 
332 static int audit_compare_uid(kuid_t uid,
333 			     struct audit_names *name,
334 			     struct audit_field *f,
335 			     struct audit_context *ctx)
336 {
337 	struct audit_names *n;
338 	int rc;
339 
340 	if (name) {
341 		rc = audit_uid_comparator(uid, f->op, name->uid);
342 		if (rc)
343 			return rc;
344 	}
345 
346 	if (ctx) {
347 		list_for_each_entry(n, &ctx->names_list, list) {
348 			rc = audit_uid_comparator(uid, f->op, n->uid);
349 			if (rc)
350 				return rc;
351 		}
352 	}
353 	return 0;
354 }
355 
356 static int audit_compare_gid(kgid_t gid,
357 			     struct audit_names *name,
358 			     struct audit_field *f,
359 			     struct audit_context *ctx)
360 {
361 	struct audit_names *n;
362 	int rc;
363 
364 	if (name) {
365 		rc = audit_gid_comparator(gid, f->op, name->gid);
366 		if (rc)
367 			return rc;
368 	}
369 
370 	if (ctx) {
371 		list_for_each_entry(n, &ctx->names_list, list) {
372 			rc = audit_gid_comparator(gid, f->op, n->gid);
373 			if (rc)
374 				return rc;
375 		}
376 	}
377 	return 0;
378 }
379 
380 static int audit_field_compare(struct task_struct *tsk,
381 			       const struct cred *cred,
382 			       struct audit_field *f,
383 			       struct audit_context *ctx,
384 			       struct audit_names *name)
385 {
386 	switch (f->val) {
387 	/* process to file object comparisons */
388 	case AUDIT_COMPARE_UID_TO_OBJ_UID:
389 		return audit_compare_uid(cred->uid, name, f, ctx);
390 	case AUDIT_COMPARE_GID_TO_OBJ_GID:
391 		return audit_compare_gid(cred->gid, name, f, ctx);
392 	case AUDIT_COMPARE_EUID_TO_OBJ_UID:
393 		return audit_compare_uid(cred->euid, name, f, ctx);
394 	case AUDIT_COMPARE_EGID_TO_OBJ_GID:
395 		return audit_compare_gid(cred->egid, name, f, ctx);
396 	case AUDIT_COMPARE_AUID_TO_OBJ_UID:
397 		return audit_compare_uid(audit_get_loginuid(tsk), name, f, ctx);
398 	case AUDIT_COMPARE_SUID_TO_OBJ_UID:
399 		return audit_compare_uid(cred->suid, name, f, ctx);
400 	case AUDIT_COMPARE_SGID_TO_OBJ_GID:
401 		return audit_compare_gid(cred->sgid, name, f, ctx);
402 	case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
403 		return audit_compare_uid(cred->fsuid, name, f, ctx);
404 	case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
405 		return audit_compare_gid(cred->fsgid, name, f, ctx);
406 	/* uid comparisons */
407 	case AUDIT_COMPARE_UID_TO_AUID:
408 		return audit_uid_comparator(cred->uid, f->op,
409 					    audit_get_loginuid(tsk));
410 	case AUDIT_COMPARE_UID_TO_EUID:
411 		return audit_uid_comparator(cred->uid, f->op, cred->euid);
412 	case AUDIT_COMPARE_UID_TO_SUID:
413 		return audit_uid_comparator(cred->uid, f->op, cred->suid);
414 	case AUDIT_COMPARE_UID_TO_FSUID:
415 		return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
416 	/* auid comparisons */
417 	case AUDIT_COMPARE_AUID_TO_EUID:
418 		return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
419 					    cred->euid);
420 	case AUDIT_COMPARE_AUID_TO_SUID:
421 		return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
422 					    cred->suid);
423 	case AUDIT_COMPARE_AUID_TO_FSUID:
424 		return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
425 					    cred->fsuid);
426 	/* euid comparisons */
427 	case AUDIT_COMPARE_EUID_TO_SUID:
428 		return audit_uid_comparator(cred->euid, f->op, cred->suid);
429 	case AUDIT_COMPARE_EUID_TO_FSUID:
430 		return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
431 	/* suid comparisons */
432 	case AUDIT_COMPARE_SUID_TO_FSUID:
433 		return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
434 	/* gid comparisons */
435 	case AUDIT_COMPARE_GID_TO_EGID:
436 		return audit_gid_comparator(cred->gid, f->op, cred->egid);
437 	case AUDIT_COMPARE_GID_TO_SGID:
438 		return audit_gid_comparator(cred->gid, f->op, cred->sgid);
439 	case AUDIT_COMPARE_GID_TO_FSGID:
440 		return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
441 	/* egid comparisons */
442 	case AUDIT_COMPARE_EGID_TO_SGID:
443 		return audit_gid_comparator(cred->egid, f->op, cred->sgid);
444 	case AUDIT_COMPARE_EGID_TO_FSGID:
445 		return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
446 	/* sgid comparison */
447 	case AUDIT_COMPARE_SGID_TO_FSGID:
448 		return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
449 	default:
450 		WARN(1, "Missing AUDIT_COMPARE define.  Report as a bug\n");
451 		return 0;
452 	}
453 	return 0;
454 }
455 
456 /* Determine if any context name data matches a rule's watch data */
457 /* Compare a task_struct with an audit_rule.  Return 1 on match, 0
458  * otherwise.
459  *
460  * If task_creation is true, this is an explicit indication that we are
461  * filtering a task rule at task creation time.  This and tsk == current are
462  * the only situations where tsk->cred may be accessed without an rcu read lock.
463  */
464 static int audit_filter_rules(struct task_struct *tsk,
465 			      struct audit_krule *rule,
466 			      struct audit_context *ctx,
467 			      struct audit_names *name,
468 			      enum audit_state *state,
469 			      bool task_creation)
470 {
471 	const struct cred *cred;
472 	int i, need_sid = 1;
473 	u32 sid;
474 	struct lsm_prop prop = { };
475 	unsigned int sessionid;
476 
477 	if (ctx && rule->prio <= ctx->prio)
478 		return 0;
479 
480 	cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
481 
482 	for (i = 0; i < rule->field_count; i++) {
483 		struct audit_field *f = &rule->fields[i];
484 		struct audit_names *n;
485 		int result = 0;
486 		pid_t pid;
487 
488 		switch (f->type) {
489 		case AUDIT_PID:
490 			pid = task_tgid_nr(tsk);
491 			result = audit_comparator(pid, f->op, f->val);
492 			break;
493 		case AUDIT_PPID:
494 			if (ctx) {
495 				if (!ctx->ppid)
496 					ctx->ppid = task_ppid_nr(tsk);
497 				result = audit_comparator(ctx->ppid, f->op, f->val);
498 			}
499 			break;
500 		case AUDIT_EXE:
501 			result = audit_exe_compare(tsk, rule->exe);
502 			if (f->op == Audit_not_equal)
503 				result = !result;
504 			break;
505 		case AUDIT_UID:
506 			result = audit_uid_comparator(cred->uid, f->op, f->uid);
507 			break;
508 		case AUDIT_EUID:
509 			result = audit_uid_comparator(cred->euid, f->op, f->uid);
510 			break;
511 		case AUDIT_SUID:
512 			result = audit_uid_comparator(cred->suid, f->op, f->uid);
513 			break;
514 		case AUDIT_FSUID:
515 			result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
516 			break;
517 		case AUDIT_GID:
518 			result = audit_gid_comparator(cred->gid, f->op, f->gid);
519 			if (f->op == Audit_equal) {
520 				if (!result)
521 					result = groups_search(cred->group_info, f->gid);
522 			} else if (f->op == Audit_not_equal) {
523 				if (result)
524 					result = !groups_search(cred->group_info, f->gid);
525 			}
526 			break;
527 		case AUDIT_EGID:
528 			result = audit_gid_comparator(cred->egid, f->op, f->gid);
529 			if (f->op == Audit_equal) {
530 				if (!result)
531 					result = groups_search(cred->group_info, f->gid);
532 			} else if (f->op == Audit_not_equal) {
533 				if (result)
534 					result = !groups_search(cred->group_info, f->gid);
535 			}
536 			break;
537 		case AUDIT_SGID:
538 			result = audit_gid_comparator(cred->sgid, f->op, f->gid);
539 			break;
540 		case AUDIT_FSGID:
541 			result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
542 			break;
543 		case AUDIT_SESSIONID:
544 			sessionid = audit_get_sessionid(tsk);
545 			result = audit_comparator(sessionid, f->op, f->val);
546 			break;
547 		case AUDIT_PERS:
548 			result = audit_comparator(tsk->personality, f->op, f->val);
549 			break;
550 		case AUDIT_ARCH:
551 			if (ctx)
552 				result = audit_comparator(ctx->arch, f->op, f->val);
553 			break;
554 
555 		case AUDIT_EXIT:
556 			if (ctx && ctx->return_valid != AUDITSC_INVALID)
557 				result = audit_comparator(ctx->return_code, f->op, f->val);
558 			break;
559 		case AUDIT_SUCCESS:
560 			if (ctx && ctx->return_valid != AUDITSC_INVALID) {
561 				if (f->val)
562 					result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
563 				else
564 					result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
565 			}
566 			break;
567 		case AUDIT_DEVMAJOR:
568 			if (name) {
569 				if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
570 				    audit_comparator(MAJOR(name->rdev), f->op, f->val))
571 					++result;
572 			} else if (ctx) {
573 				list_for_each_entry(n, &ctx->names_list, list) {
574 					if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
575 					    audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
576 						++result;
577 						break;
578 					}
579 				}
580 			}
581 			break;
582 		case AUDIT_DEVMINOR:
583 			if (name) {
584 				if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
585 				    audit_comparator(MINOR(name->rdev), f->op, f->val))
586 					++result;
587 			} else if (ctx) {
588 				list_for_each_entry(n, &ctx->names_list, list) {
589 					if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
590 					    audit_comparator(MINOR(n->rdev), f->op, f->val)) {
591 						++result;
592 						break;
593 					}
594 				}
595 			}
596 			break;
597 		case AUDIT_INODE:
598 			if (name)
599 				result = audit_comparator(name->ino, f->op, f->val);
600 			else if (ctx) {
601 				list_for_each_entry(n, &ctx->names_list, list) {
602 					if (audit_comparator(n->ino, f->op, f->val)) {
603 						++result;
604 						break;
605 					}
606 				}
607 			}
608 			break;
609 		case AUDIT_OBJ_UID:
610 			if (name) {
611 				result = audit_uid_comparator(name->uid, f->op, f->uid);
612 			} else if (ctx) {
613 				list_for_each_entry(n, &ctx->names_list, list) {
614 					if (audit_uid_comparator(n->uid, f->op, f->uid)) {
615 						++result;
616 						break;
617 					}
618 				}
619 			}
620 			break;
621 		case AUDIT_OBJ_GID:
622 			if (name) {
623 				result = audit_gid_comparator(name->gid, f->op, f->gid);
624 			} else if (ctx) {
625 				list_for_each_entry(n, &ctx->names_list, list) {
626 					if (audit_gid_comparator(n->gid, f->op, f->gid)) {
627 						++result;
628 						break;
629 					}
630 				}
631 			}
632 			break;
633 		case AUDIT_WATCH:
634 			if (name) {
635 				result = audit_watch_compare(rule->watch,
636 							     name->ino,
637 							     name->dev);
638 				if (f->op == Audit_not_equal)
639 					result = !result;
640 			}
641 			break;
642 		case AUDIT_DIR:
643 			if (ctx) {
644 				result = match_tree_refs(ctx, rule->tree);
645 				if (f->op == Audit_not_equal)
646 					result = !result;
647 			}
648 			break;
649 		case AUDIT_LOGINUID:
650 			result = audit_uid_comparator(audit_get_loginuid(tsk),
651 						      f->op, f->uid);
652 			break;
653 		case AUDIT_LOGINUID_SET:
654 			result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
655 			break;
656 		case AUDIT_SADDR_FAM:
657 			if (ctx && ctx->sockaddr)
658 				result = audit_comparator(ctx->sockaddr->ss_family,
659 							  f->op, f->val);
660 			break;
661 		case AUDIT_SUBJ_USER:
662 		case AUDIT_SUBJ_ROLE:
663 		case AUDIT_SUBJ_TYPE:
664 		case AUDIT_SUBJ_SEN:
665 		case AUDIT_SUBJ_CLR:
666 			/* NOTE: this may return negative values indicating
667 			   a temporary error.  We simply treat this as a
668 			   match for now to avoid losing information that
669 			   may be wanted.   An error message will also be
670 			   logged upon error */
671 			if (f->lsm_rule) {
672 				if (need_sid) {
673 					/* @tsk should always be equal to
674 					 * @current with the exception of
675 					 * fork()/copy_process() in which case
676 					 * the new @tsk creds are still a dup
677 					 * of @current's creds so we can still
678 					 * use security_current_getsecid_subj()
679 					 * here even though it always refs
680 					 * @current's creds
681 					 */
682 					security_current_getsecid_subj(&sid);
683 					need_sid = 0;
684 				}
685 				/* scaffolding */
686 				prop.scaffold.secid = sid;
687 				result = security_audit_rule_match(&prop,
688 								   f->type,
689 								   f->op,
690 								   f->lsm_rule);
691 			}
692 			break;
693 		case AUDIT_OBJ_USER:
694 		case AUDIT_OBJ_ROLE:
695 		case AUDIT_OBJ_TYPE:
696 		case AUDIT_OBJ_LEV_LOW:
697 		case AUDIT_OBJ_LEV_HIGH:
698 			/* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
699 			   also applies here */
700 			if (f->lsm_rule) {
701 				/* Find files that match */
702 				if (name) {
703 					/* scaffolding */
704 					prop.scaffold.secid = name->osid;
705 					result = security_audit_rule_match(
706 								&prop,
707 								f->type,
708 								f->op,
709 								f->lsm_rule);
710 				} else if (ctx) {
711 					list_for_each_entry(n, &ctx->names_list, list) {
712 						/* scaffolding */
713 						prop.scaffold.secid = n->osid;
714 						if (security_audit_rule_match(
715 								&prop,
716 								f->type,
717 								f->op,
718 								f->lsm_rule)) {
719 							++result;
720 							break;
721 						}
722 					}
723 				}
724 				/* Find ipc objects that match */
725 				if (!ctx || ctx->type != AUDIT_IPC)
726 					break;
727 				/* scaffolding */
728 				prop.scaffold.secid = ctx->ipc.osid;
729 				if (security_audit_rule_match(&prop,
730 							      f->type, f->op,
731 							      f->lsm_rule))
732 					++result;
733 			}
734 			break;
735 		case AUDIT_ARG0:
736 		case AUDIT_ARG1:
737 		case AUDIT_ARG2:
738 		case AUDIT_ARG3:
739 			if (ctx)
740 				result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
741 			break;
742 		case AUDIT_FILTERKEY:
743 			/* ignore this field for filtering */
744 			result = 1;
745 			break;
746 		case AUDIT_PERM:
747 			result = audit_match_perm(ctx, f->val);
748 			if (f->op == Audit_not_equal)
749 				result = !result;
750 			break;
751 		case AUDIT_FILETYPE:
752 			result = audit_match_filetype(ctx, f->val);
753 			if (f->op == Audit_not_equal)
754 				result = !result;
755 			break;
756 		case AUDIT_FIELD_COMPARE:
757 			result = audit_field_compare(tsk, cred, f, ctx, name);
758 			break;
759 		}
760 		if (!result)
761 			return 0;
762 	}
763 
764 	if (ctx) {
765 		if (rule->filterkey) {
766 			kfree(ctx->filterkey);
767 			ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
768 		}
769 		ctx->prio = rule->prio;
770 	}
771 	switch (rule->action) {
772 	case AUDIT_NEVER:
773 		*state = AUDIT_STATE_DISABLED;
774 		break;
775 	case AUDIT_ALWAYS:
776 		*state = AUDIT_STATE_RECORD;
777 		break;
778 	}
779 	return 1;
780 }
781 
782 /* At process creation time, we can determine if system-call auditing is
783  * completely disabled for this task.  Since we only have the task
784  * structure at this point, we can only check uid and gid.
785  */
786 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
787 {
788 	struct audit_entry *e;
789 	enum audit_state   state;
790 
791 	rcu_read_lock();
792 	list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
793 		if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
794 				       &state, true)) {
795 			if (state == AUDIT_STATE_RECORD)
796 				*key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
797 			rcu_read_unlock();
798 			return state;
799 		}
800 	}
801 	rcu_read_unlock();
802 	return AUDIT_STATE_BUILD;
803 }
804 
805 static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
806 {
807 	int word, bit;
808 
809 	if (val > 0xffffffff)
810 		return false;
811 
812 	word = AUDIT_WORD(val);
813 	if (word >= AUDIT_BITMASK_SIZE)
814 		return false;
815 
816 	bit = AUDIT_BIT(val);
817 
818 	return rule->mask[word] & bit;
819 }
820 
821 /**
822  * __audit_filter_op - common filter helper for operations (syscall/uring/etc)
823  * @tsk: associated task
824  * @ctx: audit context
825  * @list: audit filter list
826  * @name: audit_name (can be NULL)
827  * @op: current syscall/uring_op
828  *
829  * Run the udit filters specified in @list against @tsk using @ctx,
830  * @name, and @op, as necessary; the caller is responsible for ensuring
831  * that the call is made while the RCU read lock is held. The @name
832  * parameter can be NULL, but all others must be specified.
833  * Returns 1/true if the filter finds a match, 0/false if none are found.
834  */
835 static int __audit_filter_op(struct task_struct *tsk,
836 			   struct audit_context *ctx,
837 			   struct list_head *list,
838 			   struct audit_names *name,
839 			   unsigned long op)
840 {
841 	struct audit_entry *e;
842 	enum audit_state state;
843 
844 	list_for_each_entry_rcu(e, list, list) {
845 		if (audit_in_mask(&e->rule, op) &&
846 		    audit_filter_rules(tsk, &e->rule, ctx, name,
847 				       &state, false)) {
848 			ctx->current_state = state;
849 			return 1;
850 		}
851 	}
852 	return 0;
853 }
854 
855 /**
856  * audit_filter_uring - apply filters to an io_uring operation
857  * @tsk: associated task
858  * @ctx: audit context
859  */
860 static void audit_filter_uring(struct task_struct *tsk,
861 			       struct audit_context *ctx)
862 {
863 	if (auditd_test_task(tsk))
864 		return;
865 
866 	rcu_read_lock();
867 	__audit_filter_op(tsk, ctx, &audit_filter_list[AUDIT_FILTER_URING_EXIT],
868 			NULL, ctx->uring_op);
869 	rcu_read_unlock();
870 }
871 
872 /* At syscall exit time, this filter is called if the audit_state is
873  * not low enough that auditing cannot take place, but is also not
874  * high enough that we already know we have to write an audit record
875  * (i.e., the state is AUDIT_STATE_BUILD).
876  */
877 static void audit_filter_syscall(struct task_struct *tsk,
878 				 struct audit_context *ctx)
879 {
880 	if (auditd_test_task(tsk))
881 		return;
882 
883 	rcu_read_lock();
884 	__audit_filter_op(tsk, ctx, &audit_filter_list[AUDIT_FILTER_EXIT],
885 			NULL, ctx->major);
886 	rcu_read_unlock();
887 }
888 
889 /*
890  * Given an audit_name check the inode hash table to see if they match.
891  * Called holding the rcu read lock to protect the use of audit_inode_hash
892  */
893 static int audit_filter_inode_name(struct task_struct *tsk,
894 				   struct audit_names *n,
895 				   struct audit_context *ctx)
896 {
897 	int h = audit_hash_ino((u32)n->ino);
898 	struct list_head *list = &audit_inode_hash[h];
899 
900 	return __audit_filter_op(tsk, ctx, list, n, ctx->major);
901 }
902 
903 /* At syscall exit time, this filter is called if any audit_names have been
904  * collected during syscall processing.  We only check rules in sublists at hash
905  * buckets applicable to the inode numbers in audit_names.
906  * Regarding audit_state, same rules apply as for audit_filter_syscall().
907  */
908 void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
909 {
910 	struct audit_names *n;
911 
912 	if (auditd_test_task(tsk))
913 		return;
914 
915 	rcu_read_lock();
916 
917 	list_for_each_entry(n, &ctx->names_list, list) {
918 		if (audit_filter_inode_name(tsk, n, ctx))
919 			break;
920 	}
921 	rcu_read_unlock();
922 }
923 
924 static inline void audit_proctitle_free(struct audit_context *context)
925 {
926 	kfree(context->proctitle.value);
927 	context->proctitle.value = NULL;
928 	context->proctitle.len = 0;
929 }
930 
931 static inline void audit_free_module(struct audit_context *context)
932 {
933 	if (context->type == AUDIT_KERN_MODULE) {
934 		kfree(context->module.name);
935 		context->module.name = NULL;
936 	}
937 }
938 static inline void audit_free_names(struct audit_context *context)
939 {
940 	struct audit_names *n, *next;
941 
942 	list_for_each_entry_safe(n, next, &context->names_list, list) {
943 		list_del(&n->list);
944 		if (n->name)
945 			putname(n->name);
946 		if (n->should_free)
947 			kfree(n);
948 	}
949 	context->name_count = 0;
950 	path_put(&context->pwd);
951 	context->pwd.dentry = NULL;
952 	context->pwd.mnt = NULL;
953 }
954 
955 static inline void audit_free_aux(struct audit_context *context)
956 {
957 	struct audit_aux_data *aux;
958 
959 	while ((aux = context->aux)) {
960 		context->aux = aux->next;
961 		kfree(aux);
962 	}
963 	context->aux = NULL;
964 	while ((aux = context->aux_pids)) {
965 		context->aux_pids = aux->next;
966 		kfree(aux);
967 	}
968 	context->aux_pids = NULL;
969 }
970 
971 /**
972  * audit_reset_context - reset a audit_context structure
973  * @ctx: the audit_context to reset
974  *
975  * All fields in the audit_context will be reset to an initial state, all
976  * references held by fields will be dropped, and private memory will be
977  * released.  When this function returns the audit_context will be suitable
978  * for reuse, so long as the passed context is not NULL or a dummy context.
979  */
980 static void audit_reset_context(struct audit_context *ctx)
981 {
982 	if (!ctx)
983 		return;
984 
985 	/* if ctx is non-null, reset the "ctx->context" regardless */
986 	ctx->context = AUDIT_CTX_UNUSED;
987 	if (ctx->dummy)
988 		return;
989 
990 	/*
991 	 * NOTE: It shouldn't matter in what order we release the fields, so
992 	 *       release them in the order in which they appear in the struct;
993 	 *       this gives us some hope of quickly making sure we are
994 	 *       resetting the audit_context properly.
995 	 *
996 	 *       Other things worth mentioning:
997 	 *       - we don't reset "dummy"
998 	 *       - we don't reset "state", we do reset "current_state"
999 	 *       - we preserve "filterkey" if "state" is AUDIT_STATE_RECORD
1000 	 *       - much of this is likely overkill, but play it safe for now
1001 	 *       - we really need to work on improving the audit_context struct
1002 	 */
1003 
1004 	ctx->current_state = ctx->state;
1005 	ctx->serial = 0;
1006 	ctx->major = 0;
1007 	ctx->uring_op = 0;
1008 	ctx->ctime = (struct timespec64){ .tv_sec = 0, .tv_nsec = 0 };
1009 	memset(ctx->argv, 0, sizeof(ctx->argv));
1010 	ctx->return_code = 0;
1011 	ctx->prio = (ctx->state == AUDIT_STATE_RECORD ? ~0ULL : 0);
1012 	ctx->return_valid = AUDITSC_INVALID;
1013 	audit_free_names(ctx);
1014 	if (ctx->state != AUDIT_STATE_RECORD) {
1015 		kfree(ctx->filterkey);
1016 		ctx->filterkey = NULL;
1017 	}
1018 	audit_free_aux(ctx);
1019 	kfree(ctx->sockaddr);
1020 	ctx->sockaddr = NULL;
1021 	ctx->sockaddr_len = 0;
1022 	ctx->ppid = 0;
1023 	ctx->uid = ctx->euid = ctx->suid = ctx->fsuid = KUIDT_INIT(0);
1024 	ctx->gid = ctx->egid = ctx->sgid = ctx->fsgid = KGIDT_INIT(0);
1025 	ctx->personality = 0;
1026 	ctx->arch = 0;
1027 	ctx->target_pid = 0;
1028 	ctx->target_auid = ctx->target_uid = KUIDT_INIT(0);
1029 	ctx->target_sessionid = 0;
1030 	ctx->target_sid = 0;
1031 	ctx->target_comm[0] = '\0';
1032 	unroll_tree_refs(ctx, NULL, 0);
1033 	WARN_ON(!list_empty(&ctx->killed_trees));
1034 	audit_free_module(ctx);
1035 	ctx->fds[0] = -1;
1036 	ctx->type = 0; /* reset last for audit_free_*() */
1037 }
1038 
1039 static inline struct audit_context *audit_alloc_context(enum audit_state state)
1040 {
1041 	struct audit_context *context;
1042 
1043 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1044 	if (!context)
1045 		return NULL;
1046 	context->context = AUDIT_CTX_UNUSED;
1047 	context->state = state;
1048 	context->prio = state == AUDIT_STATE_RECORD ? ~0ULL : 0;
1049 	INIT_LIST_HEAD(&context->killed_trees);
1050 	INIT_LIST_HEAD(&context->names_list);
1051 	context->fds[0] = -1;
1052 	context->return_valid = AUDITSC_INVALID;
1053 	return context;
1054 }
1055 
1056 /**
1057  * audit_alloc - allocate an audit context block for a task
1058  * @tsk: task
1059  *
1060  * Filter on the task information and allocate a per-task audit context
1061  * if necessary.  Doing so turns on system call auditing for the
1062  * specified task.  This is called from copy_process, so no lock is
1063  * needed.
1064  */
1065 int audit_alloc(struct task_struct *tsk)
1066 {
1067 	struct audit_context *context;
1068 	enum audit_state     state;
1069 	char *key = NULL;
1070 
1071 	if (likely(!audit_ever_enabled))
1072 		return 0;
1073 
1074 	state = audit_filter_task(tsk, &key);
1075 	if (state == AUDIT_STATE_DISABLED) {
1076 		clear_task_syscall_work(tsk, SYSCALL_AUDIT);
1077 		return 0;
1078 	}
1079 
1080 	context = audit_alloc_context(state);
1081 	if (!context) {
1082 		kfree(key);
1083 		audit_log_lost("out of memory in audit_alloc");
1084 		return -ENOMEM;
1085 	}
1086 	context->filterkey = key;
1087 
1088 	audit_set_context(tsk, context);
1089 	set_task_syscall_work(tsk, SYSCALL_AUDIT);
1090 	return 0;
1091 }
1092 
1093 static inline void audit_free_context(struct audit_context *context)
1094 {
1095 	/* resetting is extra work, but it is likely just noise */
1096 	audit_reset_context(context);
1097 	audit_proctitle_free(context);
1098 	free_tree_refs(context);
1099 	kfree(context->filterkey);
1100 	kfree(context);
1101 }
1102 
1103 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
1104 				 kuid_t auid, kuid_t uid, unsigned int sessionid,
1105 				 u32 sid, char *comm)
1106 {
1107 	struct audit_buffer *ab;
1108 	char *ctx = NULL;
1109 	u32 len;
1110 	int rc = 0;
1111 
1112 	ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
1113 	if (!ab)
1114 		return rc;
1115 
1116 	audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
1117 			 from_kuid(&init_user_ns, auid),
1118 			 from_kuid(&init_user_ns, uid), sessionid);
1119 	if (sid) {
1120 		if (security_secid_to_secctx(sid, &ctx, &len)) {
1121 			audit_log_format(ab, " obj=(none)");
1122 			rc = 1;
1123 		} else {
1124 			audit_log_format(ab, " obj=%s", ctx);
1125 			security_release_secctx(ctx, len);
1126 		}
1127 	}
1128 	audit_log_format(ab, " ocomm=");
1129 	audit_log_untrustedstring(ab, comm);
1130 	audit_log_end(ab);
1131 
1132 	return rc;
1133 }
1134 
1135 static void audit_log_execve_info(struct audit_context *context,
1136 				  struct audit_buffer **ab)
1137 {
1138 	long len_max;
1139 	long len_rem;
1140 	long len_full;
1141 	long len_buf;
1142 	long len_abuf = 0;
1143 	long len_tmp;
1144 	bool require_data;
1145 	bool encode;
1146 	unsigned int iter;
1147 	unsigned int arg;
1148 	char *buf_head;
1149 	char *buf;
1150 	const char __user *p = (const char __user *)current->mm->arg_start;
1151 
1152 	/* NOTE: this buffer needs to be large enough to hold all the non-arg
1153 	 *       data we put in the audit record for this argument (see the
1154 	 *       code below) ... at this point in time 96 is plenty */
1155 	char abuf[96];
1156 
1157 	/* NOTE: we set MAX_EXECVE_AUDIT_LEN to a rather arbitrary limit, the
1158 	 *       current value of 7500 is not as important as the fact that it
1159 	 *       is less than 8k, a setting of 7500 gives us plenty of wiggle
1160 	 *       room if we go over a little bit in the logging below */
1161 	WARN_ON_ONCE(MAX_EXECVE_AUDIT_LEN > 7500);
1162 	len_max = MAX_EXECVE_AUDIT_LEN;
1163 
1164 	/* scratch buffer to hold the userspace args */
1165 	buf_head = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1166 	if (!buf_head) {
1167 		audit_panic("out of memory for argv string");
1168 		return;
1169 	}
1170 	buf = buf_head;
1171 
1172 	audit_log_format(*ab, "argc=%d", context->execve.argc);
1173 
1174 	len_rem = len_max;
1175 	len_buf = 0;
1176 	len_full = 0;
1177 	require_data = true;
1178 	encode = false;
1179 	iter = 0;
1180 	arg = 0;
1181 	do {
1182 		/* NOTE: we don't ever want to trust this value for anything
1183 		 *       serious, but the audit record format insists we
1184 		 *       provide an argument length for really long arguments,
1185 		 *       e.g. > MAX_EXECVE_AUDIT_LEN, so we have no choice but
1186 		 *       to use strncpy_from_user() to obtain this value for
1187 		 *       recording in the log, although we don't use it
1188 		 *       anywhere here to avoid a double-fetch problem */
1189 		if (len_full == 0)
1190 			len_full = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1191 
1192 		/* read more data from userspace */
1193 		if (require_data) {
1194 			/* can we make more room in the buffer? */
1195 			if (buf != buf_head) {
1196 				memmove(buf_head, buf, len_buf);
1197 				buf = buf_head;
1198 			}
1199 
1200 			/* fetch as much as we can of the argument */
1201 			len_tmp = strncpy_from_user(&buf_head[len_buf], p,
1202 						    len_max - len_buf);
1203 			if (len_tmp == -EFAULT) {
1204 				/* unable to copy from userspace */
1205 				send_sig(SIGKILL, current, 0);
1206 				goto out;
1207 			} else if (len_tmp == (len_max - len_buf)) {
1208 				/* buffer is not large enough */
1209 				require_data = true;
1210 				/* NOTE: if we are going to span multiple
1211 				 *       buffers force the encoding so we stand
1212 				 *       a chance at a sane len_full value and
1213 				 *       consistent record encoding */
1214 				encode = true;
1215 				len_full = len_full * 2;
1216 				p += len_tmp;
1217 			} else {
1218 				require_data = false;
1219 				if (!encode)
1220 					encode = audit_string_contains_control(
1221 								buf, len_tmp);
1222 				/* try to use a trusted value for len_full */
1223 				if (len_full < len_max)
1224 					len_full = (encode ?
1225 						    len_tmp * 2 : len_tmp);
1226 				p += len_tmp + 1;
1227 			}
1228 			len_buf += len_tmp;
1229 			buf_head[len_buf] = '\0';
1230 
1231 			/* length of the buffer in the audit record? */
1232 			len_abuf = (encode ? len_buf * 2 : len_buf + 2);
1233 		}
1234 
1235 		/* write as much as we can to the audit log */
1236 		if (len_buf >= 0) {
1237 			/* NOTE: some magic numbers here - basically if we
1238 			 *       can't fit a reasonable amount of data into the
1239 			 *       existing audit buffer, flush it and start with
1240 			 *       a new buffer */
1241 			if ((sizeof(abuf) + 8) > len_rem) {
1242 				len_rem = len_max;
1243 				audit_log_end(*ab);
1244 				*ab = audit_log_start(context,
1245 						      GFP_KERNEL, AUDIT_EXECVE);
1246 				if (!*ab)
1247 					goto out;
1248 			}
1249 
1250 			/* create the non-arg portion of the arg record */
1251 			len_tmp = 0;
1252 			if (require_data || (iter > 0) ||
1253 			    ((len_abuf + sizeof(abuf)) > len_rem)) {
1254 				if (iter == 0) {
1255 					len_tmp += snprintf(&abuf[len_tmp],
1256 							sizeof(abuf) - len_tmp,
1257 							" a%d_len=%lu",
1258 							arg, len_full);
1259 				}
1260 				len_tmp += snprintf(&abuf[len_tmp],
1261 						    sizeof(abuf) - len_tmp,
1262 						    " a%d[%d]=", arg, iter++);
1263 			} else
1264 				len_tmp += snprintf(&abuf[len_tmp],
1265 						    sizeof(abuf) - len_tmp,
1266 						    " a%d=", arg);
1267 			WARN_ON(len_tmp >= sizeof(abuf));
1268 			abuf[sizeof(abuf) - 1] = '\0';
1269 
1270 			/* log the arg in the audit record */
1271 			audit_log_format(*ab, "%s", abuf);
1272 			len_rem -= len_tmp;
1273 			len_tmp = len_buf;
1274 			if (encode) {
1275 				if (len_abuf > len_rem)
1276 					len_tmp = len_rem / 2; /* encoding */
1277 				audit_log_n_hex(*ab, buf, len_tmp);
1278 				len_rem -= len_tmp * 2;
1279 				len_abuf -= len_tmp * 2;
1280 			} else {
1281 				if (len_abuf > len_rem)
1282 					len_tmp = len_rem - 2; /* quotes */
1283 				audit_log_n_string(*ab, buf, len_tmp);
1284 				len_rem -= len_tmp + 2;
1285 				/* don't subtract the "2" because we still need
1286 				 * to add quotes to the remaining string */
1287 				len_abuf -= len_tmp;
1288 			}
1289 			len_buf -= len_tmp;
1290 			buf += len_tmp;
1291 		}
1292 
1293 		/* ready to move to the next argument? */
1294 		if ((len_buf == 0) && !require_data) {
1295 			arg++;
1296 			iter = 0;
1297 			len_full = 0;
1298 			require_data = true;
1299 			encode = false;
1300 		}
1301 	} while (arg < context->execve.argc);
1302 
1303 	/* NOTE: the caller handles the final audit_log_end() call */
1304 
1305 out:
1306 	kfree(buf_head);
1307 }
1308 
1309 static void audit_log_cap(struct audit_buffer *ab, char *prefix,
1310 			  kernel_cap_t *cap)
1311 {
1312 	if (cap_isclear(*cap)) {
1313 		audit_log_format(ab, " %s=0", prefix);
1314 		return;
1315 	}
1316 	audit_log_format(ab, " %s=%016llx", prefix, cap->val);
1317 }
1318 
1319 static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1320 {
1321 	if (name->fcap_ver == -1) {
1322 		audit_log_format(ab, " cap_fe=? cap_fver=? cap_fp=? cap_fi=?");
1323 		return;
1324 	}
1325 	audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
1326 	audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
1327 	audit_log_format(ab, " cap_fe=%d cap_fver=%x cap_frootid=%d",
1328 			 name->fcap.fE, name->fcap_ver,
1329 			 from_kuid(&init_user_ns, name->fcap.rootid));
1330 }
1331 
1332 static void audit_log_time(struct audit_context *context, struct audit_buffer **ab)
1333 {
1334 	const struct audit_ntp_data *ntp = &context->time.ntp_data;
1335 	const struct timespec64 *tk = &context->time.tk_injoffset;
1336 	static const char * const ntp_name[] = {
1337 		"offset",
1338 		"freq",
1339 		"status",
1340 		"tai",
1341 		"tick",
1342 		"adjust",
1343 	};
1344 	int type;
1345 
1346 	if (context->type == AUDIT_TIME_ADJNTPVAL) {
1347 		for (type = 0; type < AUDIT_NTP_NVALS; type++) {
1348 			if (ntp->vals[type].newval != ntp->vals[type].oldval) {
1349 				if (!*ab) {
1350 					*ab = audit_log_start(context,
1351 							GFP_KERNEL,
1352 							AUDIT_TIME_ADJNTPVAL);
1353 					if (!*ab)
1354 						return;
1355 				}
1356 				audit_log_format(*ab, "op=%s old=%lli new=%lli",
1357 						 ntp_name[type],
1358 						 ntp->vals[type].oldval,
1359 						 ntp->vals[type].newval);
1360 				audit_log_end(*ab);
1361 				*ab = NULL;
1362 			}
1363 		}
1364 	}
1365 	if (tk->tv_sec != 0 || tk->tv_nsec != 0) {
1366 		if (!*ab) {
1367 			*ab = audit_log_start(context, GFP_KERNEL,
1368 					      AUDIT_TIME_INJOFFSET);
1369 			if (!*ab)
1370 				return;
1371 		}
1372 		audit_log_format(*ab, "sec=%lli nsec=%li",
1373 				 (long long)tk->tv_sec, tk->tv_nsec);
1374 		audit_log_end(*ab);
1375 		*ab = NULL;
1376 	}
1377 }
1378 
1379 static void show_special(struct audit_context *context, int *call_panic)
1380 {
1381 	struct audit_buffer *ab;
1382 	int i;
1383 
1384 	ab = audit_log_start(context, GFP_KERNEL, context->type);
1385 	if (!ab)
1386 		return;
1387 
1388 	switch (context->type) {
1389 	case AUDIT_SOCKETCALL: {
1390 		int nargs = context->socketcall.nargs;
1391 
1392 		audit_log_format(ab, "nargs=%d", nargs);
1393 		for (i = 0; i < nargs; i++)
1394 			audit_log_format(ab, " a%d=%lx", i,
1395 				context->socketcall.args[i]);
1396 		break; }
1397 	case AUDIT_IPC: {
1398 		u32 osid = context->ipc.osid;
1399 
1400 		audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1401 				 from_kuid(&init_user_ns, context->ipc.uid),
1402 				 from_kgid(&init_user_ns, context->ipc.gid),
1403 				 context->ipc.mode);
1404 		if (osid) {
1405 			char *ctx = NULL;
1406 			u32 len;
1407 
1408 			if (security_secid_to_secctx(osid, &ctx, &len)) {
1409 				audit_log_format(ab, " osid=%u", osid);
1410 				*call_panic = 1;
1411 			} else {
1412 				audit_log_format(ab, " obj=%s", ctx);
1413 				security_release_secctx(ctx, len);
1414 			}
1415 		}
1416 		if (context->ipc.has_perm) {
1417 			audit_log_end(ab);
1418 			ab = audit_log_start(context, GFP_KERNEL,
1419 					     AUDIT_IPC_SET_PERM);
1420 			if (unlikely(!ab))
1421 				return;
1422 			audit_log_format(ab,
1423 				"qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1424 				context->ipc.qbytes,
1425 				context->ipc.perm_uid,
1426 				context->ipc.perm_gid,
1427 				context->ipc.perm_mode);
1428 		}
1429 		break; }
1430 	case AUDIT_MQ_OPEN:
1431 		audit_log_format(ab,
1432 			"oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1433 			"mq_msgsize=%ld mq_curmsgs=%ld",
1434 			context->mq_open.oflag, context->mq_open.mode,
1435 			context->mq_open.attr.mq_flags,
1436 			context->mq_open.attr.mq_maxmsg,
1437 			context->mq_open.attr.mq_msgsize,
1438 			context->mq_open.attr.mq_curmsgs);
1439 		break;
1440 	case AUDIT_MQ_SENDRECV:
1441 		audit_log_format(ab,
1442 			"mqdes=%d msg_len=%zd msg_prio=%u "
1443 			"abs_timeout_sec=%lld abs_timeout_nsec=%ld",
1444 			context->mq_sendrecv.mqdes,
1445 			context->mq_sendrecv.msg_len,
1446 			context->mq_sendrecv.msg_prio,
1447 			(long long) context->mq_sendrecv.abs_timeout.tv_sec,
1448 			context->mq_sendrecv.abs_timeout.tv_nsec);
1449 		break;
1450 	case AUDIT_MQ_NOTIFY:
1451 		audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1452 				context->mq_notify.mqdes,
1453 				context->mq_notify.sigev_signo);
1454 		break;
1455 	case AUDIT_MQ_GETSETATTR: {
1456 		struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1457 
1458 		audit_log_format(ab,
1459 			"mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1460 			"mq_curmsgs=%ld ",
1461 			context->mq_getsetattr.mqdes,
1462 			attr->mq_flags, attr->mq_maxmsg,
1463 			attr->mq_msgsize, attr->mq_curmsgs);
1464 		break; }
1465 	case AUDIT_CAPSET:
1466 		audit_log_format(ab, "pid=%d", context->capset.pid);
1467 		audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1468 		audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1469 		audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1470 		audit_log_cap(ab, "cap_pa", &context->capset.cap.ambient);
1471 		break;
1472 	case AUDIT_MMAP:
1473 		audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1474 				 context->mmap.flags);
1475 		break;
1476 	case AUDIT_OPENAT2:
1477 		audit_log_format(ab, "oflag=0%llo mode=0%llo resolve=0x%llx",
1478 				 context->openat2.flags,
1479 				 context->openat2.mode,
1480 				 context->openat2.resolve);
1481 		break;
1482 	case AUDIT_EXECVE:
1483 		audit_log_execve_info(context, &ab);
1484 		break;
1485 	case AUDIT_KERN_MODULE:
1486 		audit_log_format(ab, "name=");
1487 		if (context->module.name) {
1488 			audit_log_untrustedstring(ab, context->module.name);
1489 		} else
1490 			audit_log_format(ab, "(null)");
1491 
1492 		break;
1493 	case AUDIT_TIME_ADJNTPVAL:
1494 	case AUDIT_TIME_INJOFFSET:
1495 		/* this call deviates from the rest, eating the buffer */
1496 		audit_log_time(context, &ab);
1497 		break;
1498 	}
1499 	audit_log_end(ab);
1500 }
1501 
1502 static inline int audit_proctitle_rtrim(char *proctitle, int len)
1503 {
1504 	char *end = proctitle + len - 1;
1505 
1506 	while (end > proctitle && !isprint(*end))
1507 		end--;
1508 
1509 	/* catch the case where proctitle is only 1 non-print character */
1510 	len = end - proctitle + 1;
1511 	len -= isprint(proctitle[len-1]) == 0;
1512 	return len;
1513 }
1514 
1515 /*
1516  * audit_log_name - produce AUDIT_PATH record from struct audit_names
1517  * @context: audit_context for the task
1518  * @n: audit_names structure with reportable details
1519  * @path: optional path to report instead of audit_names->name
1520  * @record_num: record number to report when handling a list of names
1521  * @call_panic: optional pointer to int that will be updated if secid fails
1522  */
1523 static void audit_log_name(struct audit_context *context, struct audit_names *n,
1524 		    const struct path *path, int record_num, int *call_panic)
1525 {
1526 	struct audit_buffer *ab;
1527 
1528 	ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1529 	if (!ab)
1530 		return;
1531 
1532 	audit_log_format(ab, "item=%d", record_num);
1533 
1534 	if (path)
1535 		audit_log_d_path(ab, " name=", path);
1536 	else if (n->name) {
1537 		switch (n->name_len) {
1538 		case AUDIT_NAME_FULL:
1539 			/* log the full path */
1540 			audit_log_format(ab, " name=");
1541 			audit_log_untrustedstring(ab, n->name->name);
1542 			break;
1543 		case 0:
1544 			/* name was specified as a relative path and the
1545 			 * directory component is the cwd
1546 			 */
1547 			if (context->pwd.dentry && context->pwd.mnt)
1548 				audit_log_d_path(ab, " name=", &context->pwd);
1549 			else
1550 				audit_log_format(ab, " name=(null)");
1551 			break;
1552 		default:
1553 			/* log the name's directory component */
1554 			audit_log_format(ab, " name=");
1555 			audit_log_n_untrustedstring(ab, n->name->name,
1556 						    n->name_len);
1557 		}
1558 	} else
1559 		audit_log_format(ab, " name=(null)");
1560 
1561 	if (n->ino != AUDIT_INO_UNSET)
1562 		audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#ho ouid=%u ogid=%u rdev=%02x:%02x",
1563 				 n->ino,
1564 				 MAJOR(n->dev),
1565 				 MINOR(n->dev),
1566 				 n->mode,
1567 				 from_kuid(&init_user_ns, n->uid),
1568 				 from_kgid(&init_user_ns, n->gid),
1569 				 MAJOR(n->rdev),
1570 				 MINOR(n->rdev));
1571 	if (n->osid != 0) {
1572 		char *ctx = NULL;
1573 		u32 len;
1574 
1575 		if (security_secid_to_secctx(
1576 			n->osid, &ctx, &len)) {
1577 			audit_log_format(ab, " osid=%u", n->osid);
1578 			if (call_panic)
1579 				*call_panic = 2;
1580 		} else {
1581 			audit_log_format(ab, " obj=%s", ctx);
1582 			security_release_secctx(ctx, len);
1583 		}
1584 	}
1585 
1586 	/* log the audit_names record type */
1587 	switch (n->type) {
1588 	case AUDIT_TYPE_NORMAL:
1589 		audit_log_format(ab, " nametype=NORMAL");
1590 		break;
1591 	case AUDIT_TYPE_PARENT:
1592 		audit_log_format(ab, " nametype=PARENT");
1593 		break;
1594 	case AUDIT_TYPE_CHILD_DELETE:
1595 		audit_log_format(ab, " nametype=DELETE");
1596 		break;
1597 	case AUDIT_TYPE_CHILD_CREATE:
1598 		audit_log_format(ab, " nametype=CREATE");
1599 		break;
1600 	default:
1601 		audit_log_format(ab, " nametype=UNKNOWN");
1602 		break;
1603 	}
1604 
1605 	audit_log_fcaps(ab, n);
1606 	audit_log_end(ab);
1607 }
1608 
1609 static void audit_log_proctitle(void)
1610 {
1611 	int res;
1612 	char *buf;
1613 	char *msg = "(null)";
1614 	int len = strlen(msg);
1615 	struct audit_context *context = audit_context();
1616 	struct audit_buffer *ab;
1617 
1618 	ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
1619 	if (!ab)
1620 		return;	/* audit_panic or being filtered */
1621 
1622 	audit_log_format(ab, "proctitle=");
1623 
1624 	/* Not  cached */
1625 	if (!context->proctitle.value) {
1626 		buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
1627 		if (!buf)
1628 			goto out;
1629 		/* Historically called this from procfs naming */
1630 		res = get_cmdline(current, buf, MAX_PROCTITLE_AUDIT_LEN);
1631 		if (res == 0) {
1632 			kfree(buf);
1633 			goto out;
1634 		}
1635 		res = audit_proctitle_rtrim(buf, res);
1636 		if (res == 0) {
1637 			kfree(buf);
1638 			goto out;
1639 		}
1640 		context->proctitle.value = buf;
1641 		context->proctitle.len = res;
1642 	}
1643 	msg = context->proctitle.value;
1644 	len = context->proctitle.len;
1645 out:
1646 	audit_log_n_untrustedstring(ab, msg, len);
1647 	audit_log_end(ab);
1648 }
1649 
1650 /**
1651  * audit_log_uring - generate a AUDIT_URINGOP record
1652  * @ctx: the audit context
1653  */
1654 static void audit_log_uring(struct audit_context *ctx)
1655 {
1656 	struct audit_buffer *ab;
1657 	const struct cred *cred;
1658 
1659 	ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_URINGOP);
1660 	if (!ab)
1661 		return;
1662 	cred = current_cred();
1663 	audit_log_format(ab, "uring_op=%d", ctx->uring_op);
1664 	if (ctx->return_valid != AUDITSC_INVALID)
1665 		audit_log_format(ab, " success=%s exit=%ld",
1666 				 (ctx->return_valid == AUDITSC_SUCCESS ?
1667 				  "yes" : "no"),
1668 				 ctx->return_code);
1669 	audit_log_format(ab,
1670 			 " items=%d"
1671 			 " ppid=%d pid=%d uid=%u gid=%u euid=%u suid=%u"
1672 			 " fsuid=%u egid=%u sgid=%u fsgid=%u",
1673 			 ctx->name_count,
1674 			 task_ppid_nr(current), task_tgid_nr(current),
1675 			 from_kuid(&init_user_ns, cred->uid),
1676 			 from_kgid(&init_user_ns, cred->gid),
1677 			 from_kuid(&init_user_ns, cred->euid),
1678 			 from_kuid(&init_user_ns, cred->suid),
1679 			 from_kuid(&init_user_ns, cred->fsuid),
1680 			 from_kgid(&init_user_ns, cred->egid),
1681 			 from_kgid(&init_user_ns, cred->sgid),
1682 			 from_kgid(&init_user_ns, cred->fsgid));
1683 	audit_log_task_context(ab);
1684 	audit_log_key(ab, ctx->filterkey);
1685 	audit_log_end(ab);
1686 }
1687 
1688 static void audit_log_exit(void)
1689 {
1690 	int i, call_panic = 0;
1691 	struct audit_context *context = audit_context();
1692 	struct audit_buffer *ab;
1693 	struct audit_aux_data *aux;
1694 	struct audit_names *n;
1695 
1696 	context->personality = current->personality;
1697 
1698 	switch (context->context) {
1699 	case AUDIT_CTX_SYSCALL:
1700 		ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1701 		if (!ab)
1702 			return;
1703 		audit_log_format(ab, "arch=%x syscall=%d",
1704 				 context->arch, context->major);
1705 		if (context->personality != PER_LINUX)
1706 			audit_log_format(ab, " per=%lx", context->personality);
1707 		if (context->return_valid != AUDITSC_INVALID)
1708 			audit_log_format(ab, " success=%s exit=%ld",
1709 					 (context->return_valid == AUDITSC_SUCCESS ?
1710 					  "yes" : "no"),
1711 					 context->return_code);
1712 		audit_log_format(ab,
1713 				 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1714 				 context->argv[0],
1715 				 context->argv[1],
1716 				 context->argv[2],
1717 				 context->argv[3],
1718 				 context->name_count);
1719 		audit_log_task_info(ab);
1720 		audit_log_key(ab, context->filterkey);
1721 		audit_log_end(ab);
1722 		break;
1723 	case AUDIT_CTX_URING:
1724 		audit_log_uring(context);
1725 		break;
1726 	default:
1727 		BUG();
1728 		break;
1729 	}
1730 
1731 	for (aux = context->aux; aux; aux = aux->next) {
1732 
1733 		ab = audit_log_start(context, GFP_KERNEL, aux->type);
1734 		if (!ab)
1735 			continue; /* audit_panic has been called */
1736 
1737 		switch (aux->type) {
1738 
1739 		case AUDIT_BPRM_FCAPS: {
1740 			struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1741 
1742 			audit_log_format(ab, "fver=%x", axs->fcap_ver);
1743 			audit_log_cap(ab, "fp", &axs->fcap.permitted);
1744 			audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1745 			audit_log_format(ab, " fe=%d", axs->fcap.fE);
1746 			audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1747 			audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1748 			audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1749 			audit_log_cap(ab, "old_pa", &axs->old_pcap.ambient);
1750 			audit_log_cap(ab, "pp", &axs->new_pcap.permitted);
1751 			audit_log_cap(ab, "pi", &axs->new_pcap.inheritable);
1752 			audit_log_cap(ab, "pe", &axs->new_pcap.effective);
1753 			audit_log_cap(ab, "pa", &axs->new_pcap.ambient);
1754 			audit_log_format(ab, " frootid=%d",
1755 					 from_kuid(&init_user_ns,
1756 						   axs->fcap.rootid));
1757 			break; }
1758 
1759 		}
1760 		audit_log_end(ab);
1761 	}
1762 
1763 	if (context->type)
1764 		show_special(context, &call_panic);
1765 
1766 	if (context->fds[0] >= 0) {
1767 		ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1768 		if (ab) {
1769 			audit_log_format(ab, "fd0=%d fd1=%d",
1770 					context->fds[0], context->fds[1]);
1771 			audit_log_end(ab);
1772 		}
1773 	}
1774 
1775 	if (context->sockaddr_len) {
1776 		ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1777 		if (ab) {
1778 			audit_log_format(ab, "saddr=");
1779 			audit_log_n_hex(ab, (void *)context->sockaddr,
1780 					context->sockaddr_len);
1781 			audit_log_end(ab);
1782 		}
1783 	}
1784 
1785 	for (aux = context->aux_pids; aux; aux = aux->next) {
1786 		struct audit_aux_data_pids *axs = (void *)aux;
1787 
1788 		for (i = 0; i < axs->pid_count; i++)
1789 			if (audit_log_pid_context(context, axs->target_pid[i],
1790 						  axs->target_auid[i],
1791 						  axs->target_uid[i],
1792 						  axs->target_sessionid[i],
1793 						  axs->target_sid[i],
1794 						  axs->target_comm[i]))
1795 				call_panic = 1;
1796 	}
1797 
1798 	if (context->target_pid &&
1799 	    audit_log_pid_context(context, context->target_pid,
1800 				  context->target_auid, context->target_uid,
1801 				  context->target_sessionid,
1802 				  context->target_sid, context->target_comm))
1803 			call_panic = 1;
1804 
1805 	if (context->pwd.dentry && context->pwd.mnt) {
1806 		ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1807 		if (ab) {
1808 			audit_log_d_path(ab, "cwd=", &context->pwd);
1809 			audit_log_end(ab);
1810 		}
1811 	}
1812 
1813 	i = 0;
1814 	list_for_each_entry(n, &context->names_list, list) {
1815 		if (n->hidden)
1816 			continue;
1817 		audit_log_name(context, n, NULL, i++, &call_panic);
1818 	}
1819 
1820 	if (context->context == AUDIT_CTX_SYSCALL)
1821 		audit_log_proctitle();
1822 
1823 	/* Send end of event record to help user space know we are finished */
1824 	ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1825 	if (ab)
1826 		audit_log_end(ab);
1827 	if (call_panic)
1828 		audit_panic("error in audit_log_exit()");
1829 }
1830 
1831 /**
1832  * __audit_free - free a per-task audit context
1833  * @tsk: task whose audit context block to free
1834  *
1835  * Called from copy_process, do_exit, and the io_uring code
1836  */
1837 void __audit_free(struct task_struct *tsk)
1838 {
1839 	struct audit_context *context = tsk->audit_context;
1840 
1841 	if (!context)
1842 		return;
1843 
1844 	/* this may generate CONFIG_CHANGE records */
1845 	if (!list_empty(&context->killed_trees))
1846 		audit_kill_trees(context);
1847 
1848 	/* We are called either by do_exit() or the fork() error handling code;
1849 	 * in the former case tsk == current and in the latter tsk is a
1850 	 * random task_struct that doesn't have any meaningful data we
1851 	 * need to log via audit_log_exit().
1852 	 */
1853 	if (tsk == current && !context->dummy) {
1854 		context->return_valid = AUDITSC_INVALID;
1855 		context->return_code = 0;
1856 		if (context->context == AUDIT_CTX_SYSCALL) {
1857 			audit_filter_syscall(tsk, context);
1858 			audit_filter_inodes(tsk, context);
1859 			if (context->current_state == AUDIT_STATE_RECORD)
1860 				audit_log_exit();
1861 		} else if (context->context == AUDIT_CTX_URING) {
1862 			/* TODO: verify this case is real and valid */
1863 			audit_filter_uring(tsk, context);
1864 			audit_filter_inodes(tsk, context);
1865 			if (context->current_state == AUDIT_STATE_RECORD)
1866 				audit_log_uring(context);
1867 		}
1868 	}
1869 
1870 	audit_set_context(tsk, NULL);
1871 	audit_free_context(context);
1872 }
1873 
1874 /**
1875  * audit_return_fixup - fixup the return codes in the audit_context
1876  * @ctx: the audit_context
1877  * @success: true/false value to indicate if the operation succeeded or not
1878  * @code: operation return code
1879  *
1880  * We need to fixup the return code in the audit logs if the actual return
1881  * codes are later going to be fixed by the arch specific signal handlers.
1882  */
1883 static void audit_return_fixup(struct audit_context *ctx,
1884 			       int success, long code)
1885 {
1886 	/*
1887 	 * This is actually a test for:
1888 	 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
1889 	 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
1890 	 *
1891 	 * but is faster than a bunch of ||
1892 	 */
1893 	if (unlikely(code <= -ERESTARTSYS) &&
1894 	    (code >= -ERESTART_RESTARTBLOCK) &&
1895 	    (code != -ENOIOCTLCMD))
1896 		ctx->return_code = -EINTR;
1897 	else
1898 		ctx->return_code  = code;
1899 	ctx->return_valid = (success ? AUDITSC_SUCCESS : AUDITSC_FAILURE);
1900 }
1901 
1902 /**
1903  * __audit_uring_entry - prepare the kernel task's audit context for io_uring
1904  * @op: the io_uring opcode
1905  *
1906  * This is similar to audit_syscall_entry() but is intended for use by io_uring
1907  * operations.  This function should only ever be called from
1908  * audit_uring_entry() as we rely on the audit context checking present in that
1909  * function.
1910  */
1911 void __audit_uring_entry(u8 op)
1912 {
1913 	struct audit_context *ctx = audit_context();
1914 
1915 	if (ctx->state == AUDIT_STATE_DISABLED)
1916 		return;
1917 
1918 	/*
1919 	 * NOTE: It's possible that we can be called from the process' context
1920 	 *       before it returns to userspace, and before audit_syscall_exit()
1921 	 *       is called.  In this case there is not much to do, just record
1922 	 *       the io_uring details and return.
1923 	 */
1924 	ctx->uring_op = op;
1925 	if (ctx->context == AUDIT_CTX_SYSCALL)
1926 		return;
1927 
1928 	ctx->dummy = !audit_n_rules;
1929 	if (!ctx->dummy && ctx->state == AUDIT_STATE_BUILD)
1930 		ctx->prio = 0;
1931 
1932 	ctx->context = AUDIT_CTX_URING;
1933 	ctx->current_state = ctx->state;
1934 	ktime_get_coarse_real_ts64(&ctx->ctime);
1935 }
1936 
1937 /**
1938  * __audit_uring_exit - wrap up the kernel task's audit context after io_uring
1939  * @success: true/false value to indicate if the operation succeeded or not
1940  * @code: operation return code
1941  *
1942  * This is similar to audit_syscall_exit() but is intended for use by io_uring
1943  * operations.  This function should only ever be called from
1944  * audit_uring_exit() as we rely on the audit context checking present in that
1945  * function.
1946  */
1947 void __audit_uring_exit(int success, long code)
1948 {
1949 	struct audit_context *ctx = audit_context();
1950 
1951 	if (ctx->dummy) {
1952 		if (ctx->context != AUDIT_CTX_URING)
1953 			return;
1954 		goto out;
1955 	}
1956 
1957 	audit_return_fixup(ctx, success, code);
1958 	if (ctx->context == AUDIT_CTX_SYSCALL) {
1959 		/*
1960 		 * NOTE: See the note in __audit_uring_entry() about the case
1961 		 *       where we may be called from process context before we
1962 		 *       return to userspace via audit_syscall_exit().  In this
1963 		 *       case we simply emit a URINGOP record and bail, the
1964 		 *       normal syscall exit handling will take care of
1965 		 *       everything else.
1966 		 *       It is also worth mentioning that when we are called,
1967 		 *       the current process creds may differ from the creds
1968 		 *       used during the normal syscall processing; keep that
1969 		 *       in mind if/when we move the record generation code.
1970 		 */
1971 
1972 		/*
1973 		 * We need to filter on the syscall info here to decide if we
1974 		 * should emit a URINGOP record.  I know it seems odd but this
1975 		 * solves the problem where users have a filter to block *all*
1976 		 * syscall records in the "exit" filter; we want to preserve
1977 		 * the behavior here.
1978 		 */
1979 		audit_filter_syscall(current, ctx);
1980 		if (ctx->current_state != AUDIT_STATE_RECORD)
1981 			audit_filter_uring(current, ctx);
1982 		audit_filter_inodes(current, ctx);
1983 		if (ctx->current_state != AUDIT_STATE_RECORD)
1984 			return;
1985 
1986 		audit_log_uring(ctx);
1987 		return;
1988 	}
1989 
1990 	/* this may generate CONFIG_CHANGE records */
1991 	if (!list_empty(&ctx->killed_trees))
1992 		audit_kill_trees(ctx);
1993 
1994 	/* run through both filters to ensure we set the filterkey properly */
1995 	audit_filter_uring(current, ctx);
1996 	audit_filter_inodes(current, ctx);
1997 	if (ctx->current_state != AUDIT_STATE_RECORD)
1998 		goto out;
1999 	audit_log_exit();
2000 
2001 out:
2002 	audit_reset_context(ctx);
2003 }
2004 
2005 /**
2006  * __audit_syscall_entry - fill in an audit record at syscall entry
2007  * @major: major syscall type (function)
2008  * @a1: additional syscall register 1
2009  * @a2: additional syscall register 2
2010  * @a3: additional syscall register 3
2011  * @a4: additional syscall register 4
2012  *
2013  * Fill in audit context at syscall entry.  This only happens if the
2014  * audit context was created when the task was created and the state or
2015  * filters demand the audit context be built.  If the state from the
2016  * per-task filter or from the per-syscall filter is AUDIT_STATE_RECORD,
2017  * then the record will be written at syscall exit time (otherwise, it
2018  * will only be written if another part of the kernel requests that it
2019  * be written).
2020  */
2021 void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
2022 			   unsigned long a3, unsigned long a4)
2023 {
2024 	struct audit_context *context = audit_context();
2025 	enum audit_state     state;
2026 
2027 	if (!audit_enabled || !context)
2028 		return;
2029 
2030 	WARN_ON(context->context != AUDIT_CTX_UNUSED);
2031 	WARN_ON(context->name_count);
2032 	if (context->context != AUDIT_CTX_UNUSED || context->name_count) {
2033 		audit_panic("unrecoverable error in audit_syscall_entry()");
2034 		return;
2035 	}
2036 
2037 	state = context->state;
2038 	if (state == AUDIT_STATE_DISABLED)
2039 		return;
2040 
2041 	context->dummy = !audit_n_rules;
2042 	if (!context->dummy && state == AUDIT_STATE_BUILD) {
2043 		context->prio = 0;
2044 		if (auditd_test_task(current))
2045 			return;
2046 	}
2047 
2048 	context->arch	    = syscall_get_arch(current);
2049 	context->major      = major;
2050 	context->argv[0]    = a1;
2051 	context->argv[1]    = a2;
2052 	context->argv[2]    = a3;
2053 	context->argv[3]    = a4;
2054 	context->context = AUDIT_CTX_SYSCALL;
2055 	context->current_state  = state;
2056 	ktime_get_coarse_real_ts64(&context->ctime);
2057 }
2058 
2059 /**
2060  * __audit_syscall_exit - deallocate audit context after a system call
2061  * @success: success value of the syscall
2062  * @return_code: return value of the syscall
2063  *
2064  * Tear down after system call.  If the audit context has been marked as
2065  * auditable (either because of the AUDIT_STATE_RECORD state from
2066  * filtering, or because some other part of the kernel wrote an audit
2067  * message), then write out the syscall information.  In call cases,
2068  * free the names stored from getname().
2069  */
2070 void __audit_syscall_exit(int success, long return_code)
2071 {
2072 	struct audit_context *context = audit_context();
2073 
2074 	if (!context || context->dummy ||
2075 	    context->context != AUDIT_CTX_SYSCALL)
2076 		goto out;
2077 
2078 	/* this may generate CONFIG_CHANGE records */
2079 	if (!list_empty(&context->killed_trees))
2080 		audit_kill_trees(context);
2081 
2082 	audit_return_fixup(context, success, return_code);
2083 	/* run through both filters to ensure we set the filterkey properly */
2084 	audit_filter_syscall(current, context);
2085 	audit_filter_inodes(current, context);
2086 	if (context->current_state != AUDIT_STATE_RECORD)
2087 		goto out;
2088 
2089 	audit_log_exit();
2090 
2091 out:
2092 	audit_reset_context(context);
2093 }
2094 
2095 static inline void handle_one(const struct inode *inode)
2096 {
2097 	struct audit_context *context;
2098 	struct audit_tree_refs *p;
2099 	struct audit_chunk *chunk;
2100 	int count;
2101 
2102 	if (likely(!inode->i_fsnotify_marks))
2103 		return;
2104 	context = audit_context();
2105 	p = context->trees;
2106 	count = context->tree_count;
2107 	rcu_read_lock();
2108 	chunk = audit_tree_lookup(inode);
2109 	rcu_read_unlock();
2110 	if (!chunk)
2111 		return;
2112 	if (likely(put_tree_ref(context, chunk)))
2113 		return;
2114 	if (unlikely(!grow_tree_refs(context))) {
2115 		pr_warn("out of memory, audit has lost a tree reference\n");
2116 		audit_set_auditable(context);
2117 		audit_put_chunk(chunk);
2118 		unroll_tree_refs(context, p, count);
2119 		return;
2120 	}
2121 	put_tree_ref(context, chunk);
2122 }
2123 
2124 static void handle_path(const struct dentry *dentry)
2125 {
2126 	struct audit_context *context;
2127 	struct audit_tree_refs *p;
2128 	const struct dentry *d, *parent;
2129 	struct audit_chunk *drop;
2130 	unsigned long seq;
2131 	int count;
2132 
2133 	context = audit_context();
2134 	p = context->trees;
2135 	count = context->tree_count;
2136 retry:
2137 	drop = NULL;
2138 	d = dentry;
2139 	rcu_read_lock();
2140 	seq = read_seqbegin(&rename_lock);
2141 	for (;;) {
2142 		struct inode *inode = d_backing_inode(d);
2143 
2144 		if (inode && unlikely(inode->i_fsnotify_marks)) {
2145 			struct audit_chunk *chunk;
2146 
2147 			chunk = audit_tree_lookup(inode);
2148 			if (chunk) {
2149 				if (unlikely(!put_tree_ref(context, chunk))) {
2150 					drop = chunk;
2151 					break;
2152 				}
2153 			}
2154 		}
2155 		parent = d->d_parent;
2156 		if (parent == d)
2157 			break;
2158 		d = parent;
2159 	}
2160 	if (unlikely(read_seqretry(&rename_lock, seq) || drop)) {  /* in this order */
2161 		rcu_read_unlock();
2162 		if (!drop) {
2163 			/* just a race with rename */
2164 			unroll_tree_refs(context, p, count);
2165 			goto retry;
2166 		}
2167 		audit_put_chunk(drop);
2168 		if (grow_tree_refs(context)) {
2169 			/* OK, got more space */
2170 			unroll_tree_refs(context, p, count);
2171 			goto retry;
2172 		}
2173 		/* too bad */
2174 		pr_warn("out of memory, audit has lost a tree reference\n");
2175 		unroll_tree_refs(context, p, count);
2176 		audit_set_auditable(context);
2177 		return;
2178 	}
2179 	rcu_read_unlock();
2180 }
2181 
2182 static struct audit_names *audit_alloc_name(struct audit_context *context,
2183 						unsigned char type)
2184 {
2185 	struct audit_names *aname;
2186 
2187 	if (context->name_count < AUDIT_NAMES) {
2188 		aname = &context->preallocated_names[context->name_count];
2189 		memset(aname, 0, sizeof(*aname));
2190 	} else {
2191 		aname = kzalloc(sizeof(*aname), GFP_NOFS);
2192 		if (!aname)
2193 			return NULL;
2194 		aname->should_free = true;
2195 	}
2196 
2197 	aname->ino = AUDIT_INO_UNSET;
2198 	aname->type = type;
2199 	list_add_tail(&aname->list, &context->names_list);
2200 
2201 	context->name_count++;
2202 	if (!context->pwd.dentry)
2203 		get_fs_pwd(current->fs, &context->pwd);
2204 	return aname;
2205 }
2206 
2207 /**
2208  * __audit_reusename - fill out filename with info from existing entry
2209  * @uptr: userland ptr to pathname
2210  *
2211  * Search the audit_names list for the current audit context. If there is an
2212  * existing entry with a matching "uptr" then return the filename
2213  * associated with that audit_name. If not, return NULL.
2214  */
2215 struct filename *
2216 __audit_reusename(const __user char *uptr)
2217 {
2218 	struct audit_context *context = audit_context();
2219 	struct audit_names *n;
2220 
2221 	list_for_each_entry(n, &context->names_list, list) {
2222 		if (!n->name)
2223 			continue;
2224 		if (n->name->uptr == uptr) {
2225 			atomic_inc(&n->name->refcnt);
2226 			return n->name;
2227 		}
2228 	}
2229 	return NULL;
2230 }
2231 
2232 /**
2233  * __audit_getname - add a name to the list
2234  * @name: name to add
2235  *
2236  * Add a name to the list of audit names for this context.
2237  * Called from fs/namei.c:getname().
2238  */
2239 void __audit_getname(struct filename *name)
2240 {
2241 	struct audit_context *context = audit_context();
2242 	struct audit_names *n;
2243 
2244 	if (context->context == AUDIT_CTX_UNUSED)
2245 		return;
2246 
2247 	n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
2248 	if (!n)
2249 		return;
2250 
2251 	n->name = name;
2252 	n->name_len = AUDIT_NAME_FULL;
2253 	name->aname = n;
2254 	atomic_inc(&name->refcnt);
2255 }
2256 
2257 static inline int audit_copy_fcaps(struct audit_names *name,
2258 				   const struct dentry *dentry)
2259 {
2260 	struct cpu_vfs_cap_data caps;
2261 	int rc;
2262 
2263 	if (!dentry)
2264 		return 0;
2265 
2266 	rc = get_vfs_caps_from_disk(&nop_mnt_idmap, dentry, &caps);
2267 	if (rc)
2268 		return rc;
2269 
2270 	name->fcap.permitted = caps.permitted;
2271 	name->fcap.inheritable = caps.inheritable;
2272 	name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2273 	name->fcap.rootid = caps.rootid;
2274 	name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
2275 				VFS_CAP_REVISION_SHIFT;
2276 
2277 	return 0;
2278 }
2279 
2280 /* Copy inode data into an audit_names. */
2281 static void audit_copy_inode(struct audit_names *name,
2282 			     const struct dentry *dentry,
2283 			     struct inode *inode, unsigned int flags)
2284 {
2285 	name->ino   = inode->i_ino;
2286 	name->dev   = inode->i_sb->s_dev;
2287 	name->mode  = inode->i_mode;
2288 	name->uid   = inode->i_uid;
2289 	name->gid   = inode->i_gid;
2290 	name->rdev  = inode->i_rdev;
2291 	security_inode_getsecid(inode, &name->osid);
2292 	if (flags & AUDIT_INODE_NOEVAL) {
2293 		name->fcap_ver = -1;
2294 		return;
2295 	}
2296 	audit_copy_fcaps(name, dentry);
2297 }
2298 
2299 /**
2300  * __audit_inode - store the inode and device from a lookup
2301  * @name: name being audited
2302  * @dentry: dentry being audited
2303  * @flags: attributes for this particular entry
2304  */
2305 void __audit_inode(struct filename *name, const struct dentry *dentry,
2306 		   unsigned int flags)
2307 {
2308 	struct audit_context *context = audit_context();
2309 	struct inode *inode = d_backing_inode(dentry);
2310 	struct audit_names *n;
2311 	bool parent = flags & AUDIT_INODE_PARENT;
2312 	struct audit_entry *e;
2313 	struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
2314 	int i;
2315 
2316 	if (context->context == AUDIT_CTX_UNUSED)
2317 		return;
2318 
2319 	rcu_read_lock();
2320 	list_for_each_entry_rcu(e, list, list) {
2321 		for (i = 0; i < e->rule.field_count; i++) {
2322 			struct audit_field *f = &e->rule.fields[i];
2323 
2324 			if (f->type == AUDIT_FSTYPE
2325 			    && audit_comparator(inode->i_sb->s_magic,
2326 						f->op, f->val)
2327 			    && e->rule.action == AUDIT_NEVER) {
2328 				rcu_read_unlock();
2329 				return;
2330 			}
2331 		}
2332 	}
2333 	rcu_read_unlock();
2334 
2335 	if (!name)
2336 		goto out_alloc;
2337 
2338 	/*
2339 	 * If we have a pointer to an audit_names entry already, then we can
2340 	 * just use it directly if the type is correct.
2341 	 */
2342 	n = name->aname;
2343 	if (n) {
2344 		if (parent) {
2345 			if (n->type == AUDIT_TYPE_PARENT ||
2346 			    n->type == AUDIT_TYPE_UNKNOWN)
2347 				goto out;
2348 		} else {
2349 			if (n->type != AUDIT_TYPE_PARENT)
2350 				goto out;
2351 		}
2352 	}
2353 
2354 	list_for_each_entry_reverse(n, &context->names_list, list) {
2355 		if (n->ino) {
2356 			/* valid inode number, use that for the comparison */
2357 			if (n->ino != inode->i_ino ||
2358 			    n->dev != inode->i_sb->s_dev)
2359 				continue;
2360 		} else if (n->name) {
2361 			/* inode number has not been set, check the name */
2362 			if (strcmp(n->name->name, name->name))
2363 				continue;
2364 		} else
2365 			/* no inode and no name (?!) ... this is odd ... */
2366 			continue;
2367 
2368 		/* match the correct record type */
2369 		if (parent) {
2370 			if (n->type == AUDIT_TYPE_PARENT ||
2371 			    n->type == AUDIT_TYPE_UNKNOWN)
2372 				goto out;
2373 		} else {
2374 			if (n->type != AUDIT_TYPE_PARENT)
2375 				goto out;
2376 		}
2377 	}
2378 
2379 out_alloc:
2380 	/* unable to find an entry with both a matching name and type */
2381 	n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
2382 	if (!n)
2383 		return;
2384 	if (name) {
2385 		n->name = name;
2386 		atomic_inc(&name->refcnt);
2387 	}
2388 
2389 out:
2390 	if (parent) {
2391 		n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
2392 		n->type = AUDIT_TYPE_PARENT;
2393 		if (flags & AUDIT_INODE_HIDDEN)
2394 			n->hidden = true;
2395 	} else {
2396 		n->name_len = AUDIT_NAME_FULL;
2397 		n->type = AUDIT_TYPE_NORMAL;
2398 	}
2399 	handle_path(dentry);
2400 	audit_copy_inode(n, dentry, inode, flags & AUDIT_INODE_NOEVAL);
2401 }
2402 
2403 void __audit_file(const struct file *file)
2404 {
2405 	__audit_inode(NULL, file->f_path.dentry, 0);
2406 }
2407 
2408 /**
2409  * __audit_inode_child - collect inode info for created/removed objects
2410  * @parent: inode of dentry parent
2411  * @dentry: dentry being audited
2412  * @type:   AUDIT_TYPE_* value that we're looking for
2413  *
2414  * For syscalls that create or remove filesystem objects, audit_inode
2415  * can only collect information for the filesystem object's parent.
2416  * This call updates the audit context with the child's information.
2417  * Syscalls that create a new filesystem object must be hooked after
2418  * the object is created.  Syscalls that remove a filesystem object
2419  * must be hooked prior, in order to capture the target inode during
2420  * unsuccessful attempts.
2421  */
2422 void __audit_inode_child(struct inode *parent,
2423 			 const struct dentry *dentry,
2424 			 const unsigned char type)
2425 {
2426 	struct audit_context *context = audit_context();
2427 	struct inode *inode = d_backing_inode(dentry);
2428 	const struct qstr *dname = &dentry->d_name;
2429 	struct audit_names *n, *found_parent = NULL, *found_child = NULL;
2430 	struct audit_entry *e;
2431 	struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
2432 	int i;
2433 
2434 	if (context->context == AUDIT_CTX_UNUSED)
2435 		return;
2436 
2437 	rcu_read_lock();
2438 	list_for_each_entry_rcu(e, list, list) {
2439 		for (i = 0; i < e->rule.field_count; i++) {
2440 			struct audit_field *f = &e->rule.fields[i];
2441 
2442 			if (f->type == AUDIT_FSTYPE
2443 			    && audit_comparator(parent->i_sb->s_magic,
2444 						f->op, f->val)
2445 			    && e->rule.action == AUDIT_NEVER) {
2446 				rcu_read_unlock();
2447 				return;
2448 			}
2449 		}
2450 	}
2451 	rcu_read_unlock();
2452 
2453 	if (inode)
2454 		handle_one(inode);
2455 
2456 	/* look for a parent entry first */
2457 	list_for_each_entry(n, &context->names_list, list) {
2458 		if (!n->name ||
2459 		    (n->type != AUDIT_TYPE_PARENT &&
2460 		     n->type != AUDIT_TYPE_UNKNOWN))
2461 			continue;
2462 
2463 		if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
2464 		    !audit_compare_dname_path(dname,
2465 					      n->name->name, n->name_len)) {
2466 			if (n->type == AUDIT_TYPE_UNKNOWN)
2467 				n->type = AUDIT_TYPE_PARENT;
2468 			found_parent = n;
2469 			break;
2470 		}
2471 	}
2472 
2473 	cond_resched();
2474 
2475 	/* is there a matching child entry? */
2476 	list_for_each_entry(n, &context->names_list, list) {
2477 		/* can only match entries that have a name */
2478 		if (!n->name ||
2479 		    (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
2480 			continue;
2481 
2482 		if (!strcmp(dname->name, n->name->name) ||
2483 		    !audit_compare_dname_path(dname, n->name->name,
2484 						found_parent ?
2485 						found_parent->name_len :
2486 						AUDIT_NAME_FULL)) {
2487 			if (n->type == AUDIT_TYPE_UNKNOWN)
2488 				n->type = type;
2489 			found_child = n;
2490 			break;
2491 		}
2492 	}
2493 
2494 	if (!found_parent) {
2495 		/* create a new, "anonymous" parent record */
2496 		n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
2497 		if (!n)
2498 			return;
2499 		audit_copy_inode(n, NULL, parent, 0);
2500 	}
2501 
2502 	if (!found_child) {
2503 		found_child = audit_alloc_name(context, type);
2504 		if (!found_child)
2505 			return;
2506 
2507 		/* Re-use the name belonging to the slot for a matching parent
2508 		 * directory. All names for this context are relinquished in
2509 		 * audit_free_names() */
2510 		if (found_parent) {
2511 			found_child->name = found_parent->name;
2512 			found_child->name_len = AUDIT_NAME_FULL;
2513 			atomic_inc(&found_child->name->refcnt);
2514 		}
2515 	}
2516 
2517 	if (inode)
2518 		audit_copy_inode(found_child, dentry, inode, 0);
2519 	else
2520 		found_child->ino = AUDIT_INO_UNSET;
2521 }
2522 EXPORT_SYMBOL_GPL(__audit_inode_child);
2523 
2524 /**
2525  * auditsc_get_stamp - get local copies of audit_context values
2526  * @ctx: audit_context for the task
2527  * @t: timespec64 to store time recorded in the audit_context
2528  * @serial: serial value that is recorded in the audit_context
2529  *
2530  * Also sets the context as auditable.
2531  */
2532 int auditsc_get_stamp(struct audit_context *ctx,
2533 		       struct timespec64 *t, unsigned int *serial)
2534 {
2535 	if (ctx->context == AUDIT_CTX_UNUSED)
2536 		return 0;
2537 	if (!ctx->serial)
2538 		ctx->serial = audit_serial();
2539 	t->tv_sec  = ctx->ctime.tv_sec;
2540 	t->tv_nsec = ctx->ctime.tv_nsec;
2541 	*serial    = ctx->serial;
2542 	if (!ctx->prio) {
2543 		ctx->prio = 1;
2544 		ctx->current_state = AUDIT_STATE_RECORD;
2545 	}
2546 	return 1;
2547 }
2548 
2549 /**
2550  * __audit_mq_open - record audit data for a POSIX MQ open
2551  * @oflag: open flag
2552  * @mode: mode bits
2553  * @attr: queue attributes
2554  *
2555  */
2556 void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2557 {
2558 	struct audit_context *context = audit_context();
2559 
2560 	if (attr)
2561 		memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2562 	else
2563 		memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2564 
2565 	context->mq_open.oflag = oflag;
2566 	context->mq_open.mode = mode;
2567 
2568 	context->type = AUDIT_MQ_OPEN;
2569 }
2570 
2571 /**
2572  * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2573  * @mqdes: MQ descriptor
2574  * @msg_len: Message length
2575  * @msg_prio: Message priority
2576  * @abs_timeout: Message timeout in absolute time
2577  *
2578  */
2579 void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2580 			const struct timespec64 *abs_timeout)
2581 {
2582 	struct audit_context *context = audit_context();
2583 	struct timespec64 *p = &context->mq_sendrecv.abs_timeout;
2584 
2585 	if (abs_timeout)
2586 		memcpy(p, abs_timeout, sizeof(*p));
2587 	else
2588 		memset(p, 0, sizeof(*p));
2589 
2590 	context->mq_sendrecv.mqdes = mqdes;
2591 	context->mq_sendrecv.msg_len = msg_len;
2592 	context->mq_sendrecv.msg_prio = msg_prio;
2593 
2594 	context->type = AUDIT_MQ_SENDRECV;
2595 }
2596 
2597 /**
2598  * __audit_mq_notify - record audit data for a POSIX MQ notify
2599  * @mqdes: MQ descriptor
2600  * @notification: Notification event
2601  *
2602  */
2603 
2604 void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2605 {
2606 	struct audit_context *context = audit_context();
2607 
2608 	if (notification)
2609 		context->mq_notify.sigev_signo = notification->sigev_signo;
2610 	else
2611 		context->mq_notify.sigev_signo = 0;
2612 
2613 	context->mq_notify.mqdes = mqdes;
2614 	context->type = AUDIT_MQ_NOTIFY;
2615 }
2616 
2617 /**
2618  * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2619  * @mqdes: MQ descriptor
2620  * @mqstat: MQ flags
2621  *
2622  */
2623 void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2624 {
2625 	struct audit_context *context = audit_context();
2626 
2627 	context->mq_getsetattr.mqdes = mqdes;
2628 	context->mq_getsetattr.mqstat = *mqstat;
2629 	context->type = AUDIT_MQ_GETSETATTR;
2630 }
2631 
2632 /**
2633  * __audit_ipc_obj - record audit data for ipc object
2634  * @ipcp: ipc permissions
2635  *
2636  */
2637 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2638 {
2639 	struct audit_context *context = audit_context();
2640 
2641 	context->ipc.uid = ipcp->uid;
2642 	context->ipc.gid = ipcp->gid;
2643 	context->ipc.mode = ipcp->mode;
2644 	context->ipc.has_perm = 0;
2645 	security_ipc_getsecid(ipcp, &context->ipc.osid);
2646 	context->type = AUDIT_IPC;
2647 }
2648 
2649 /**
2650  * __audit_ipc_set_perm - record audit data for new ipc permissions
2651  * @qbytes: msgq bytes
2652  * @uid: msgq user id
2653  * @gid: msgq group id
2654  * @mode: msgq mode (permissions)
2655  *
2656  * Called only after audit_ipc_obj().
2657  */
2658 void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2659 {
2660 	struct audit_context *context = audit_context();
2661 
2662 	context->ipc.qbytes = qbytes;
2663 	context->ipc.perm_uid = uid;
2664 	context->ipc.perm_gid = gid;
2665 	context->ipc.perm_mode = mode;
2666 	context->ipc.has_perm = 1;
2667 }
2668 
2669 void __audit_bprm(struct linux_binprm *bprm)
2670 {
2671 	struct audit_context *context = audit_context();
2672 
2673 	context->type = AUDIT_EXECVE;
2674 	context->execve.argc = bprm->argc;
2675 }
2676 
2677 
2678 /**
2679  * __audit_socketcall - record audit data for sys_socketcall
2680  * @nargs: number of args, which should not be more than AUDITSC_ARGS.
2681  * @args: args array
2682  *
2683  */
2684 int __audit_socketcall(int nargs, unsigned long *args)
2685 {
2686 	struct audit_context *context = audit_context();
2687 
2688 	if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2689 		return -EINVAL;
2690 	context->type = AUDIT_SOCKETCALL;
2691 	context->socketcall.nargs = nargs;
2692 	memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2693 	return 0;
2694 }
2695 
2696 /**
2697  * __audit_fd_pair - record audit data for pipe and socketpair
2698  * @fd1: the first file descriptor
2699  * @fd2: the second file descriptor
2700  *
2701  */
2702 void __audit_fd_pair(int fd1, int fd2)
2703 {
2704 	struct audit_context *context = audit_context();
2705 
2706 	context->fds[0] = fd1;
2707 	context->fds[1] = fd2;
2708 }
2709 
2710 /**
2711  * __audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2712  * @len: data length in user space
2713  * @a: data address in kernel space
2714  *
2715  * Returns 0 for success or NULL context or < 0 on error.
2716  */
2717 int __audit_sockaddr(int len, void *a)
2718 {
2719 	struct audit_context *context = audit_context();
2720 
2721 	if (!context->sockaddr) {
2722 		void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2723 
2724 		if (!p)
2725 			return -ENOMEM;
2726 		context->sockaddr = p;
2727 	}
2728 
2729 	context->sockaddr_len = len;
2730 	memcpy(context->sockaddr, a, len);
2731 	return 0;
2732 }
2733 
2734 void __audit_ptrace(struct task_struct *t)
2735 {
2736 	struct audit_context *context = audit_context();
2737 
2738 	context->target_pid = task_tgid_nr(t);
2739 	context->target_auid = audit_get_loginuid(t);
2740 	context->target_uid = task_uid(t);
2741 	context->target_sessionid = audit_get_sessionid(t);
2742 	security_task_getsecid_obj(t, &context->target_sid);
2743 	memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2744 }
2745 
2746 /**
2747  * audit_signal_info_syscall - record signal info for syscalls
2748  * @t: task being signaled
2749  *
2750  * If the audit subsystem is being terminated, record the task (pid)
2751  * and uid that is doing that.
2752  */
2753 int audit_signal_info_syscall(struct task_struct *t)
2754 {
2755 	struct audit_aux_data_pids *axp;
2756 	struct audit_context *ctx = audit_context();
2757 	kuid_t t_uid = task_uid(t);
2758 
2759 	if (!audit_signals || audit_dummy_context())
2760 		return 0;
2761 
2762 	/* optimize the common case by putting first signal recipient directly
2763 	 * in audit_context */
2764 	if (!ctx->target_pid) {
2765 		ctx->target_pid = task_tgid_nr(t);
2766 		ctx->target_auid = audit_get_loginuid(t);
2767 		ctx->target_uid = t_uid;
2768 		ctx->target_sessionid = audit_get_sessionid(t);
2769 		security_task_getsecid_obj(t, &ctx->target_sid);
2770 		memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2771 		return 0;
2772 	}
2773 
2774 	axp = (void *)ctx->aux_pids;
2775 	if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2776 		axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2777 		if (!axp)
2778 			return -ENOMEM;
2779 
2780 		axp->d.type = AUDIT_OBJ_PID;
2781 		axp->d.next = ctx->aux_pids;
2782 		ctx->aux_pids = (void *)axp;
2783 	}
2784 	BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2785 
2786 	axp->target_pid[axp->pid_count] = task_tgid_nr(t);
2787 	axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2788 	axp->target_uid[axp->pid_count] = t_uid;
2789 	axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2790 	security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]);
2791 	memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2792 	axp->pid_count++;
2793 
2794 	return 0;
2795 }
2796 
2797 /**
2798  * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2799  * @bprm: pointer to the bprm being processed
2800  * @new: the proposed new credentials
2801  * @old: the old credentials
2802  *
2803  * Simply check if the proc already has the caps given by the file and if not
2804  * store the priv escalation info for later auditing at the end of the syscall
2805  *
2806  * -Eric
2807  */
2808 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2809 			   const struct cred *new, const struct cred *old)
2810 {
2811 	struct audit_aux_data_bprm_fcaps *ax;
2812 	struct audit_context *context = audit_context();
2813 	struct cpu_vfs_cap_data vcaps;
2814 
2815 	ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2816 	if (!ax)
2817 		return -ENOMEM;
2818 
2819 	ax->d.type = AUDIT_BPRM_FCAPS;
2820 	ax->d.next = context->aux;
2821 	context->aux = (void *)ax;
2822 
2823 	get_vfs_caps_from_disk(&nop_mnt_idmap,
2824 			       bprm->file->f_path.dentry, &vcaps);
2825 
2826 	ax->fcap.permitted = vcaps.permitted;
2827 	ax->fcap.inheritable = vcaps.inheritable;
2828 	ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2829 	ax->fcap.rootid = vcaps.rootid;
2830 	ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2831 
2832 	ax->old_pcap.permitted   = old->cap_permitted;
2833 	ax->old_pcap.inheritable = old->cap_inheritable;
2834 	ax->old_pcap.effective   = old->cap_effective;
2835 	ax->old_pcap.ambient     = old->cap_ambient;
2836 
2837 	ax->new_pcap.permitted   = new->cap_permitted;
2838 	ax->new_pcap.inheritable = new->cap_inheritable;
2839 	ax->new_pcap.effective   = new->cap_effective;
2840 	ax->new_pcap.ambient     = new->cap_ambient;
2841 	return 0;
2842 }
2843 
2844 /**
2845  * __audit_log_capset - store information about the arguments to the capset syscall
2846  * @new: the new credentials
2847  * @old: the old (current) credentials
2848  *
2849  * Record the arguments userspace sent to sys_capset for later printing by the
2850  * audit system if applicable
2851  */
2852 void __audit_log_capset(const struct cred *new, const struct cred *old)
2853 {
2854 	struct audit_context *context = audit_context();
2855 
2856 	context->capset.pid = task_tgid_nr(current);
2857 	context->capset.cap.effective   = new->cap_effective;
2858 	context->capset.cap.inheritable = new->cap_effective;
2859 	context->capset.cap.permitted   = new->cap_permitted;
2860 	context->capset.cap.ambient     = new->cap_ambient;
2861 	context->type = AUDIT_CAPSET;
2862 }
2863 
2864 void __audit_mmap_fd(int fd, int flags)
2865 {
2866 	struct audit_context *context = audit_context();
2867 
2868 	context->mmap.fd = fd;
2869 	context->mmap.flags = flags;
2870 	context->type = AUDIT_MMAP;
2871 }
2872 
2873 void __audit_openat2_how(struct open_how *how)
2874 {
2875 	struct audit_context *context = audit_context();
2876 
2877 	context->openat2.flags = how->flags;
2878 	context->openat2.mode = how->mode;
2879 	context->openat2.resolve = how->resolve;
2880 	context->type = AUDIT_OPENAT2;
2881 }
2882 
2883 void __audit_log_kern_module(char *name)
2884 {
2885 	struct audit_context *context = audit_context();
2886 
2887 	context->module.name = kstrdup(name, GFP_KERNEL);
2888 	if (!context->module.name)
2889 		audit_log_lost("out of memory in __audit_log_kern_module");
2890 	context->type = AUDIT_KERN_MODULE;
2891 }
2892 
2893 void __audit_fanotify(u32 response, struct fanotify_response_info_audit_rule *friar)
2894 {
2895 	/* {subj,obj}_trust values are {0,1,2}: no,yes,unknown */
2896 	switch (friar->hdr.type) {
2897 	case FAN_RESPONSE_INFO_NONE:
2898 		audit_log(audit_context(), GFP_KERNEL, AUDIT_FANOTIFY,
2899 			  "resp=%u fan_type=%u fan_info=0 subj_trust=2 obj_trust=2",
2900 			  response, FAN_RESPONSE_INFO_NONE);
2901 		break;
2902 	case FAN_RESPONSE_INFO_AUDIT_RULE:
2903 		audit_log(audit_context(), GFP_KERNEL, AUDIT_FANOTIFY,
2904 			  "resp=%u fan_type=%u fan_info=%X subj_trust=%u obj_trust=%u",
2905 			  response, friar->hdr.type, friar->rule_number,
2906 			  friar->subj_trust, friar->obj_trust);
2907 	}
2908 }
2909 
2910 void __audit_tk_injoffset(struct timespec64 offset)
2911 {
2912 	struct audit_context *context = audit_context();
2913 
2914 	/* only set type if not already set by NTP */
2915 	if (!context->type)
2916 		context->type = AUDIT_TIME_INJOFFSET;
2917 	memcpy(&context->time.tk_injoffset, &offset, sizeof(offset));
2918 }
2919 
2920 void __audit_ntp_log(const struct audit_ntp_data *ad)
2921 {
2922 	struct audit_context *context = audit_context();
2923 	int type;
2924 
2925 	for (type = 0; type < AUDIT_NTP_NVALS; type++)
2926 		if (ad->vals[type].newval != ad->vals[type].oldval) {
2927 			/* unconditionally set type, overwriting TK */
2928 			context->type = AUDIT_TIME_ADJNTPVAL;
2929 			memcpy(&context->time.ntp_data, ad, sizeof(*ad));
2930 			break;
2931 		}
2932 }
2933 
2934 void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries,
2935 		       enum audit_nfcfgop op, gfp_t gfp)
2936 {
2937 	struct audit_buffer *ab;
2938 	char comm[sizeof(current->comm)];
2939 
2940 	ab = audit_log_start(audit_context(), gfp, AUDIT_NETFILTER_CFG);
2941 	if (!ab)
2942 		return;
2943 	audit_log_format(ab, "table=%s family=%u entries=%u op=%s",
2944 			 name, af, nentries, audit_nfcfgs[op].s);
2945 
2946 	audit_log_format(ab, " pid=%u", task_tgid_nr(current));
2947 	audit_log_task_context(ab); /* subj= */
2948 	audit_log_format(ab, " comm=");
2949 	audit_log_untrustedstring(ab, get_task_comm(comm, current));
2950 	audit_log_end(ab);
2951 }
2952 EXPORT_SYMBOL_GPL(__audit_log_nfcfg);
2953 
2954 static void audit_log_task(struct audit_buffer *ab)
2955 {
2956 	kuid_t auid, uid;
2957 	kgid_t gid;
2958 	unsigned int sessionid;
2959 	char comm[sizeof(current->comm)];
2960 
2961 	auid = audit_get_loginuid(current);
2962 	sessionid = audit_get_sessionid(current);
2963 	current_uid_gid(&uid, &gid);
2964 
2965 	audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2966 			 from_kuid(&init_user_ns, auid),
2967 			 from_kuid(&init_user_ns, uid),
2968 			 from_kgid(&init_user_ns, gid),
2969 			 sessionid);
2970 	audit_log_task_context(ab);
2971 	audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
2972 	audit_log_untrustedstring(ab, get_task_comm(comm, current));
2973 	audit_log_d_path_exe(ab, current->mm);
2974 }
2975 
2976 /**
2977  * audit_core_dumps - record information about processes that end abnormally
2978  * @signr: signal value
2979  *
2980  * If a process ends with a core dump, something fishy is going on and we
2981  * should record the event for investigation.
2982  */
2983 void audit_core_dumps(long signr)
2984 {
2985 	struct audit_buffer *ab;
2986 
2987 	if (!audit_enabled)
2988 		return;
2989 
2990 	if (signr == SIGQUIT)	/* don't care for those */
2991 		return;
2992 
2993 	ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_ANOM_ABEND);
2994 	if (unlikely(!ab))
2995 		return;
2996 	audit_log_task(ab);
2997 	audit_log_format(ab, " sig=%ld res=1", signr);
2998 	audit_log_end(ab);
2999 }
3000 
3001 /**
3002  * audit_seccomp - record information about a seccomp action
3003  * @syscall: syscall number
3004  * @signr: signal value
3005  * @code: the seccomp action
3006  *
3007  * Record the information associated with a seccomp action. Event filtering for
3008  * seccomp actions that are not to be logged is done in seccomp_log().
3009  * Therefore, this function forces auditing independent of the audit_enabled
3010  * and dummy context state because seccomp actions should be logged even when
3011  * audit is not in use.
3012  */
3013 void audit_seccomp(unsigned long syscall, long signr, int code)
3014 {
3015 	struct audit_buffer *ab;
3016 
3017 	ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_SECCOMP);
3018 	if (unlikely(!ab))
3019 		return;
3020 	audit_log_task(ab);
3021 	audit_log_format(ab, " sig=%ld arch=%x syscall=%ld compat=%d ip=0x%lx code=0x%x",
3022 			 signr, syscall_get_arch(current), syscall,
3023 			 in_compat_syscall(), KSTK_EIP(current), code);
3024 	audit_log_end(ab);
3025 }
3026 
3027 void audit_seccomp_actions_logged(const char *names, const char *old_names,
3028 				  int res)
3029 {
3030 	struct audit_buffer *ab;
3031 
3032 	if (!audit_enabled)
3033 		return;
3034 
3035 	ab = audit_log_start(audit_context(), GFP_KERNEL,
3036 			     AUDIT_CONFIG_CHANGE);
3037 	if (unlikely(!ab))
3038 		return;
3039 
3040 	audit_log_format(ab,
3041 			 "op=seccomp-logging actions=%s old-actions=%s res=%d",
3042 			 names, old_names, res);
3043 	audit_log_end(ab);
3044 }
3045 
3046 struct list_head *audit_killed_trees(void)
3047 {
3048 	struct audit_context *ctx = audit_context();
3049 	if (likely(!ctx || ctx->context == AUDIT_CTX_UNUSED))
3050 		return NULL;
3051 	return &ctx->killed_trees;
3052 }
3053