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