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