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