KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / kernel / auditsc.c
blob4660027476d2721401a007b2ff258883fb689a9c
1 /* auditsc.c -- System-call auditing support
2 * Handles all system-call specific auditing features.
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.
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.
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.
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
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
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.
32 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
33 * 2006.
35 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
38 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/tty.h>
67 #include <linux/binfmts.h>
68 #include <linux/highmem.h>
69 #include <linux/syscalls.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/uaccess.h>
76 #include "audit.h"
78 /* flags stating the success for a syscall */
79 #define AUDITSC_INVALID 0
80 #define AUDITSC_SUCCESS 1
81 #define AUDITSC_FAILURE 2
83 /* no execve audit message should be longer than this (userspace limits),
84 * see the note near the top of audit_log_execve_info() about this value */
85 #define MAX_EXECVE_AUDIT_LEN 7500
87 /* max length to print of cmdline/proctitle value during audit */
88 #define MAX_PROCTITLE_AUDIT_LEN 128
90 /* number of audit rules */
91 int audit_n_rules;
93 /* determines whether we collect data for signals sent */
94 int audit_signals;
96 struct audit_aux_data {
97 struct audit_aux_data *next;
98 int type;
101 #define AUDIT_AUX_IPCPERM 0
103 /* Number of target pids per aux struct. */
104 #define AUDIT_AUX_PIDS 16
106 struct audit_aux_data_pids {
107 struct audit_aux_data d;
108 pid_t target_pid[AUDIT_AUX_PIDS];
109 kuid_t target_auid[AUDIT_AUX_PIDS];
110 kuid_t target_uid[AUDIT_AUX_PIDS];
111 unsigned int target_sessionid[AUDIT_AUX_PIDS];
112 u32 target_sid[AUDIT_AUX_PIDS];
113 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
114 int pid_count;
117 struct audit_aux_data_bprm_fcaps {
118 struct audit_aux_data d;
119 struct audit_cap_data fcap;
120 unsigned int fcap_ver;
121 struct audit_cap_data old_pcap;
122 struct audit_cap_data new_pcap;
125 struct audit_tree_refs {
126 struct audit_tree_refs *next;
127 struct audit_chunk *c[31];
130 static inline int open_arg(int flags, int mask)
132 int n = ACC_MODE(flags);
133 if (flags & (O_TRUNC | O_CREAT))
134 n |= AUDIT_PERM_WRITE;
135 return n & mask;
138 static int audit_match_perm(struct audit_context *ctx, int mask)
140 unsigned n;
141 if (unlikely(!ctx))
142 return 0;
143 n = ctx->major;
145 switch (audit_classify_syscall(ctx->arch, n)) {
146 case 0: /* native */
147 if ((mask & AUDIT_PERM_WRITE) &&
148 audit_match_class(AUDIT_CLASS_WRITE, n))
149 return 1;
150 if ((mask & AUDIT_PERM_READ) &&
151 audit_match_class(AUDIT_CLASS_READ, n))
152 return 1;
153 if ((mask & AUDIT_PERM_ATTR) &&
154 audit_match_class(AUDIT_CLASS_CHATTR, n))
155 return 1;
156 return 0;
157 case 1: /* 32bit on biarch */
158 if ((mask & AUDIT_PERM_WRITE) &&
159 audit_match_class(AUDIT_CLASS_WRITE_32, n))
160 return 1;
161 if ((mask & AUDIT_PERM_READ) &&
162 audit_match_class(AUDIT_CLASS_READ_32, n))
163 return 1;
164 if ((mask & AUDIT_PERM_ATTR) &&
165 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
166 return 1;
167 return 0;
168 case 2: /* open */
169 return mask & ACC_MODE(ctx->argv[1]);
170 case 3: /* openat */
171 return mask & ACC_MODE(ctx->argv[2]);
172 case 4: /* socketcall */
173 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
174 case 5: /* execve */
175 return mask & AUDIT_PERM_EXEC;
176 default:
177 return 0;
181 static int audit_match_filetype(struct audit_context *ctx, int val)
183 struct audit_names *n;
184 umode_t mode = (umode_t)val;
186 if (unlikely(!ctx))
187 return 0;
189 list_for_each_entry(n, &ctx->names_list, list) {
190 if ((n->ino != -1) &&
191 ((n->mode & S_IFMT) == mode))
192 return 1;
195 return 0;
199 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
200 * ->first_trees points to its beginning, ->trees - to the current end of data.
201 * ->tree_count is the number of free entries in array pointed to by ->trees.
202 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
203 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
204 * it's going to remain 1-element for almost any setup) until we free context itself.
205 * References in it _are_ dropped - at the same time we free/drop aux stuff.
208 #ifdef CONFIG_AUDIT_TREE
209 static void audit_set_auditable(struct audit_context *ctx)
211 if (!ctx->prio) {
212 ctx->prio = 1;
213 ctx->current_state = AUDIT_RECORD_CONTEXT;
217 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
219 struct audit_tree_refs *p = ctx->trees;
220 int left = ctx->tree_count;
221 if (likely(left)) {
222 p->c[--left] = chunk;
223 ctx->tree_count = left;
224 return 1;
226 if (!p)
227 return 0;
228 p = p->next;
229 if (p) {
230 p->c[30] = chunk;
231 ctx->trees = p;
232 ctx->tree_count = 30;
233 return 1;
235 return 0;
238 static int grow_tree_refs(struct audit_context *ctx)
240 struct audit_tree_refs *p = ctx->trees;
241 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
242 if (!ctx->trees) {
243 ctx->trees = p;
244 return 0;
246 if (p)
247 p->next = ctx->trees;
248 else
249 ctx->first_trees = ctx->trees;
250 ctx->tree_count = 31;
251 return 1;
253 #endif
255 static void unroll_tree_refs(struct audit_context *ctx,
256 struct audit_tree_refs *p, int count)
258 #ifdef CONFIG_AUDIT_TREE
259 struct audit_tree_refs *q;
260 int n;
261 if (!p) {
262 /* we started with empty chain */
263 p = ctx->first_trees;
264 count = 31;
265 /* if the very first allocation has failed, nothing to do */
266 if (!p)
267 return;
269 n = count;
270 for (q = p; q != ctx->trees; q = q->next, n = 31) {
271 while (n--) {
272 audit_put_chunk(q->c[n]);
273 q->c[n] = NULL;
276 while (n-- > ctx->tree_count) {
277 audit_put_chunk(q->c[n]);
278 q->c[n] = NULL;
280 ctx->trees = p;
281 ctx->tree_count = count;
282 #endif
285 static void free_tree_refs(struct audit_context *ctx)
287 struct audit_tree_refs *p, *q;
288 for (p = ctx->first_trees; p; p = q) {
289 q = p->next;
290 kfree(p);
294 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
296 #ifdef CONFIG_AUDIT_TREE
297 struct audit_tree_refs *p;
298 int n;
299 if (!tree)
300 return 0;
301 /* full ones */
302 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
303 for (n = 0; n < 31; n++)
304 if (audit_tree_match(p->c[n], tree))
305 return 1;
307 /* partial */
308 if (p) {
309 for (n = ctx->tree_count; n < 31; n++)
310 if (audit_tree_match(p->c[n], tree))
311 return 1;
313 #endif
314 return 0;
317 static int audit_compare_uid(kuid_t uid,
318 struct audit_names *name,
319 struct audit_field *f,
320 struct audit_context *ctx)
322 struct audit_names *n;
323 int rc;
325 if (name) {
326 rc = audit_uid_comparator(uid, f->op, name->uid);
327 if (rc)
328 return rc;
331 if (ctx) {
332 list_for_each_entry(n, &ctx->names_list, list) {
333 rc = audit_uid_comparator(uid, f->op, n->uid);
334 if (rc)
335 return rc;
338 return 0;
341 static int audit_compare_gid(kgid_t gid,
342 struct audit_names *name,
343 struct audit_field *f,
344 struct audit_context *ctx)
346 struct audit_names *n;
347 int rc;
349 if (name) {
350 rc = audit_gid_comparator(gid, f->op, name->gid);
351 if (rc)
352 return rc;
355 if (ctx) {
356 list_for_each_entry(n, &ctx->names_list, list) {
357 rc = audit_gid_comparator(gid, f->op, n->gid);
358 if (rc)
359 return rc;
362 return 0;
365 static int audit_field_compare(struct task_struct *tsk,
366 const struct cred *cred,
367 struct audit_field *f,
368 struct audit_context *ctx,
369 struct audit_names *name)
371 switch (f->val) {
372 /* process to file object comparisons */
373 case AUDIT_COMPARE_UID_TO_OBJ_UID:
374 return audit_compare_uid(cred->uid, name, f, ctx);
375 case AUDIT_COMPARE_GID_TO_OBJ_GID:
376 return audit_compare_gid(cred->gid, name, f, ctx);
377 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
378 return audit_compare_uid(cred->euid, name, f, ctx);
379 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
380 return audit_compare_gid(cred->egid, name, f, ctx);
381 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
382 return audit_compare_uid(tsk->loginuid, name, f, ctx);
383 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
384 return audit_compare_uid(cred->suid, name, f, ctx);
385 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
386 return audit_compare_gid(cred->sgid, name, f, ctx);
387 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
388 return audit_compare_uid(cred->fsuid, name, f, ctx);
389 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
390 return audit_compare_gid(cred->fsgid, name, f, ctx);
391 /* uid comparisons */
392 case AUDIT_COMPARE_UID_TO_AUID:
393 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
394 case AUDIT_COMPARE_UID_TO_EUID:
395 return audit_uid_comparator(cred->uid, f->op, cred->euid);
396 case AUDIT_COMPARE_UID_TO_SUID:
397 return audit_uid_comparator(cred->uid, f->op, cred->suid);
398 case AUDIT_COMPARE_UID_TO_FSUID:
399 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
400 /* auid comparisons */
401 case AUDIT_COMPARE_AUID_TO_EUID:
402 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
403 case AUDIT_COMPARE_AUID_TO_SUID:
404 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
405 case AUDIT_COMPARE_AUID_TO_FSUID:
406 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
407 /* euid comparisons */
408 case AUDIT_COMPARE_EUID_TO_SUID:
409 return audit_uid_comparator(cred->euid, f->op, cred->suid);
410 case AUDIT_COMPARE_EUID_TO_FSUID:
411 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
412 /* suid comparisons */
413 case AUDIT_COMPARE_SUID_TO_FSUID:
414 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
415 /* gid comparisons */
416 case AUDIT_COMPARE_GID_TO_EGID:
417 return audit_gid_comparator(cred->gid, f->op, cred->egid);
418 case AUDIT_COMPARE_GID_TO_SGID:
419 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
420 case AUDIT_COMPARE_GID_TO_FSGID:
421 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
422 /* egid comparisons */
423 case AUDIT_COMPARE_EGID_TO_SGID:
424 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
425 case AUDIT_COMPARE_EGID_TO_FSGID:
426 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
427 /* sgid comparison */
428 case AUDIT_COMPARE_SGID_TO_FSGID:
429 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
430 default:
431 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
432 return 0;
434 return 0;
437 /* Determine if any context name data matches a rule's watch data */
438 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
439 * otherwise.
441 * If task_creation is true, this is an explicit indication that we are
442 * filtering a task rule at task creation time. This and tsk == current are
443 * the only situations where tsk->cred may be accessed without an rcu read lock.
445 static int audit_filter_rules(struct task_struct *tsk,
446 struct audit_krule *rule,
447 struct audit_context *ctx,
448 struct audit_names *name,
449 enum audit_state *state,
450 bool task_creation)
452 const struct cred *cred;
453 int i, need_sid = 1;
454 u32 sid;
456 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
458 for (i = 0; i < rule->field_count; i++) {
459 struct audit_field *f = &rule->fields[i];
460 struct audit_names *n;
461 int result = 0;
462 pid_t pid;
464 switch (f->type) {
465 case AUDIT_PID:
466 pid = task_pid_nr(tsk);
467 result = audit_comparator(pid, f->op, f->val);
468 break;
469 case AUDIT_PPID:
470 if (ctx) {
471 if (!ctx->ppid)
472 ctx->ppid = task_ppid_nr(tsk);
473 result = audit_comparator(ctx->ppid, f->op, f->val);
475 break;
476 case AUDIT_UID:
477 result = audit_uid_comparator(cred->uid, f->op, f->uid);
478 break;
479 case AUDIT_EUID:
480 result = audit_uid_comparator(cred->euid, f->op, f->uid);
481 break;
482 case AUDIT_SUID:
483 result = audit_uid_comparator(cred->suid, f->op, f->uid);
484 break;
485 case AUDIT_FSUID:
486 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
487 break;
488 case AUDIT_GID:
489 result = audit_gid_comparator(cred->gid, f->op, f->gid);
490 if (f->op == Audit_equal) {
491 if (!result)
492 result = in_group_p(f->gid);
493 } else if (f->op == Audit_not_equal) {
494 if (result)
495 result = !in_group_p(f->gid);
497 break;
498 case AUDIT_EGID:
499 result = audit_gid_comparator(cred->egid, f->op, f->gid);
500 if (f->op == Audit_equal) {
501 if (!result)
502 result = in_egroup_p(f->gid);
503 } else if (f->op == Audit_not_equal) {
504 if (result)
505 result = !in_egroup_p(f->gid);
507 break;
508 case AUDIT_SGID:
509 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
510 break;
511 case AUDIT_FSGID:
512 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
513 break;
514 case AUDIT_PERS:
515 result = audit_comparator(tsk->personality, f->op, f->val);
516 break;
517 case AUDIT_ARCH:
518 if (ctx)
519 result = audit_comparator(ctx->arch, f->op, f->val);
520 break;
522 case AUDIT_EXIT:
523 if (ctx && ctx->return_valid)
524 result = audit_comparator(ctx->return_code, f->op, f->val);
525 break;
526 case AUDIT_SUCCESS:
527 if (ctx && ctx->return_valid) {
528 if (f->val)
529 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
530 else
531 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
533 break;
534 case AUDIT_DEVMAJOR:
535 if (name) {
536 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
537 audit_comparator(MAJOR(name->rdev), f->op, f->val))
538 ++result;
539 } else if (ctx) {
540 list_for_each_entry(n, &ctx->names_list, list) {
541 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
542 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
543 ++result;
544 break;
548 break;
549 case AUDIT_DEVMINOR:
550 if (name) {
551 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
552 audit_comparator(MINOR(name->rdev), f->op, f->val))
553 ++result;
554 } else if (ctx) {
555 list_for_each_entry(n, &ctx->names_list, list) {
556 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
557 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
558 ++result;
559 break;
563 break;
564 case AUDIT_INODE:
565 if (name)
566 result = audit_comparator(name->ino, f->op, f->val);
567 else if (ctx) {
568 list_for_each_entry(n, &ctx->names_list, list) {
569 if (audit_comparator(n->ino, f->op, f->val)) {
570 ++result;
571 break;
575 break;
576 case AUDIT_OBJ_UID:
577 if (name) {
578 result = audit_uid_comparator(name->uid, f->op, f->uid);
579 } else if (ctx) {
580 list_for_each_entry(n, &ctx->names_list, list) {
581 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
582 ++result;
583 break;
587 break;
588 case AUDIT_OBJ_GID:
589 if (name) {
590 result = audit_gid_comparator(name->gid, f->op, f->gid);
591 } else if (ctx) {
592 list_for_each_entry(n, &ctx->names_list, list) {
593 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
594 ++result;
595 break;
599 break;
600 case AUDIT_WATCH:
601 if (name)
602 result = audit_watch_compare(rule->watch, name->ino, name->dev);
603 break;
604 case AUDIT_DIR:
605 if (ctx)
606 result = match_tree_refs(ctx, rule->tree);
607 break;
608 case AUDIT_LOGINUID:
609 result = 0;
610 if (ctx)
611 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
612 break;
613 case AUDIT_LOGINUID_SET:
614 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
615 break;
616 case AUDIT_SUBJ_USER:
617 case AUDIT_SUBJ_ROLE:
618 case AUDIT_SUBJ_TYPE:
619 case AUDIT_SUBJ_SEN:
620 case AUDIT_SUBJ_CLR:
621 /* NOTE: this may return negative values indicating
622 a temporary error. We simply treat this as a
623 match for now to avoid losing information that
624 may be wanted. An error message will also be
625 logged upon error */
626 if (f->lsm_rule) {
627 if (need_sid) {
628 security_task_getsecid(tsk, &sid);
629 need_sid = 0;
631 result = security_audit_rule_match(sid, f->type,
632 f->op,
633 f->lsm_rule,
634 ctx);
636 break;
637 case AUDIT_OBJ_USER:
638 case AUDIT_OBJ_ROLE:
639 case AUDIT_OBJ_TYPE:
640 case AUDIT_OBJ_LEV_LOW:
641 case AUDIT_OBJ_LEV_HIGH:
642 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
643 also applies here */
644 if (f->lsm_rule) {
645 /* Find files that match */
646 if (name) {
647 result = security_audit_rule_match(
648 name->osid, f->type, f->op,
649 f->lsm_rule, ctx);
650 } else if (ctx) {
651 list_for_each_entry(n, &ctx->names_list, list) {
652 if (security_audit_rule_match(n->osid, f->type,
653 f->op, f->lsm_rule,
654 ctx)) {
655 ++result;
656 break;
660 /* Find ipc objects that match */
661 if (!ctx || ctx->type != AUDIT_IPC)
662 break;
663 if (security_audit_rule_match(ctx->ipc.osid,
664 f->type, f->op,
665 f->lsm_rule, ctx))
666 ++result;
668 break;
669 case AUDIT_ARG0:
670 case AUDIT_ARG1:
671 case AUDIT_ARG2:
672 case AUDIT_ARG3:
673 if (ctx)
674 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
675 break;
676 case AUDIT_FILTERKEY:
677 /* ignore this field for filtering */
678 result = 1;
679 break;
680 case AUDIT_PERM:
681 result = audit_match_perm(ctx, f->val);
682 break;
683 case AUDIT_FILETYPE:
684 result = audit_match_filetype(ctx, f->val);
685 break;
686 case AUDIT_FIELD_COMPARE:
687 result = audit_field_compare(tsk, cred, f, ctx, name);
688 break;
690 if (!result)
691 return 0;
694 if (ctx) {
695 if (rule->prio <= ctx->prio)
696 return 0;
697 if (rule->filterkey) {
698 kfree(ctx->filterkey);
699 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
701 ctx->prio = rule->prio;
703 switch (rule->action) {
704 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
705 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
707 return 1;
710 /* At process creation time, we can determine if system-call auditing is
711 * completely disabled for this task. Since we only have the task
712 * structure at this point, we can only check uid and gid.
714 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
716 struct audit_entry *e;
717 enum audit_state state;
719 rcu_read_lock();
720 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
721 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
722 &state, true)) {
723 if (state == AUDIT_RECORD_CONTEXT)
724 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
725 rcu_read_unlock();
726 return state;
729 rcu_read_unlock();
730 return AUDIT_BUILD_CONTEXT;
733 static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
735 int word, bit;
737 if (val > 0xffffffff)
738 return false;
740 word = AUDIT_WORD(val);
741 if (word >= AUDIT_BITMASK_SIZE)
742 return false;
744 bit = AUDIT_BIT(val);
746 return rule->mask[word] & bit;
749 /* At syscall entry and exit time, this filter is called if the
750 * audit_state is not low enough that auditing cannot take place, but is
751 * also not high enough that we already know we have to write an audit
752 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
754 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
755 struct audit_context *ctx,
756 struct list_head *list)
758 struct audit_entry *e;
759 enum audit_state state;
761 if (audit_pid && tsk->tgid == audit_pid)
762 return AUDIT_DISABLED;
764 rcu_read_lock();
765 if (!list_empty(list)) {
766 list_for_each_entry_rcu(e, list, list) {
767 if (audit_in_mask(&e->rule, ctx->major) &&
768 audit_filter_rules(tsk, &e->rule, ctx, NULL,
769 &state, false)) {
770 rcu_read_unlock();
771 ctx->current_state = state;
772 return state;
776 rcu_read_unlock();
777 return AUDIT_BUILD_CONTEXT;
781 * Given an audit_name check the inode hash table to see if they match.
782 * Called holding the rcu read lock to protect the use of audit_inode_hash
784 static int audit_filter_inode_name(struct task_struct *tsk,
785 struct audit_names *n,
786 struct audit_context *ctx) {
787 int h = audit_hash_ino((u32)n->ino);
788 struct list_head *list = &audit_inode_hash[h];
789 struct audit_entry *e;
790 enum audit_state state;
792 if (list_empty(list))
793 return 0;
795 list_for_each_entry_rcu(e, list, list) {
796 if (audit_in_mask(&e->rule, ctx->major) &&
797 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
798 ctx->current_state = state;
799 return 1;
803 return 0;
806 /* At syscall exit time, this filter is called if any audit_names have been
807 * collected during syscall processing. We only check rules in sublists at hash
808 * buckets applicable to the inode numbers in audit_names.
809 * Regarding audit_state, same rules apply as for audit_filter_syscall().
811 void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
813 struct audit_names *n;
815 if (audit_pid && tsk->tgid == audit_pid)
816 return;
818 rcu_read_lock();
820 list_for_each_entry(n, &ctx->names_list, list) {
821 if (audit_filter_inode_name(tsk, n, ctx))
822 break;
824 rcu_read_unlock();
827 /* Transfer the audit context pointer to the caller, clearing it in the tsk's struct */
828 static inline struct audit_context *audit_take_context(struct task_struct *tsk,
829 int return_valid,
830 long return_code)
832 struct audit_context *context = tsk->audit_context;
834 if (!context)
835 return NULL;
836 context->return_valid = return_valid;
839 * we need to fix up the return code in the audit logs if the actual
840 * return codes are later going to be fixed up by the arch specific
841 * signal handlers
843 * This is actually a test for:
844 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
845 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
847 * but is faster than a bunch of ||
849 if (unlikely(return_code <= -ERESTARTSYS) &&
850 (return_code >= -ERESTART_RESTARTBLOCK) &&
851 (return_code != -ENOIOCTLCMD))
852 context->return_code = -EINTR;
853 else
854 context->return_code = return_code;
856 if (context->in_syscall && !context->dummy) {
857 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
858 audit_filter_inodes(tsk, context);
861 tsk->audit_context = NULL;
862 return context;
865 static inline void audit_proctitle_free(struct audit_context *context)
867 kfree(context->proctitle.value);
868 context->proctitle.value = NULL;
869 context->proctitle.len = 0;
872 static inline void audit_free_names(struct audit_context *context)
874 struct audit_names *n, *next;
876 #if AUDIT_DEBUG == 2
877 if (context->put_count + context->ino_count != context->name_count) {
878 int i = 0;
880 pr_err("%s:%d(:%d): major=%d in_syscall=%d"
881 " name_count=%d put_count=%d ino_count=%d"
882 " [NOT freeing]\n", __FILE__, __LINE__,
883 context->serial, context->major, context->in_syscall,
884 context->name_count, context->put_count,
885 context->ino_count);
886 list_for_each_entry(n, &context->names_list, list) {
887 pr_err("names[%d] = %p = %s\n", i++, n->name,
888 n->name->name ?: "(null)");
890 dump_stack();
891 return;
893 #endif
894 #if AUDIT_DEBUG
895 context->put_count = 0;
896 context->ino_count = 0;
897 #endif
899 list_for_each_entry_safe(n, next, &context->names_list, list) {
900 list_del(&n->list);
901 if (n->name && n->name_put)
902 final_putname(n->name);
903 if (n->should_free)
904 kfree(n);
906 context->name_count = 0;
907 path_put(&context->pwd);
908 context->pwd.dentry = NULL;
909 context->pwd.mnt = NULL;
912 static inline void audit_free_aux(struct audit_context *context)
914 struct audit_aux_data *aux;
916 while ((aux = context->aux)) {
917 context->aux = aux->next;
918 kfree(aux);
920 while ((aux = context->aux_pids)) {
921 context->aux_pids = aux->next;
922 kfree(aux);
926 static inline struct audit_context *audit_alloc_context(enum audit_state state)
928 struct audit_context *context;
930 context = kzalloc(sizeof(*context), GFP_KERNEL);
931 if (!context)
932 return NULL;
933 context->state = state;
934 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
935 INIT_LIST_HEAD(&context->killed_trees);
936 INIT_LIST_HEAD(&context->names_list);
937 return context;
941 * audit_alloc - allocate an audit context block for a task
942 * @tsk: task
944 * Filter on the task information and allocate a per-task audit context
945 * if necessary. Doing so turns on system call auditing for the
946 * specified task. This is called from copy_process, so no lock is
947 * needed.
949 int audit_alloc(struct task_struct *tsk)
951 struct audit_context *context;
952 enum audit_state state;
953 char *key = NULL;
955 if (likely(!audit_ever_enabled))
956 return 0; /* Return if not auditing. */
958 state = audit_filter_task(tsk, &key);
959 if (state == AUDIT_DISABLED) {
960 clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
961 return 0;
964 if (!(context = audit_alloc_context(state))) {
965 kfree(key);
966 audit_log_lost("out of memory in audit_alloc");
967 return -ENOMEM;
969 context->filterkey = key;
971 tsk->audit_context = context;
972 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
973 return 0;
976 static inline void audit_free_context(struct audit_context *context)
978 audit_free_names(context);
979 unroll_tree_refs(context, NULL, 0);
980 free_tree_refs(context);
981 audit_free_aux(context);
982 kfree(context->filterkey);
983 kfree(context->sockaddr);
984 audit_proctitle_free(context);
985 kfree(context);
988 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
989 kuid_t auid, kuid_t uid, unsigned int sessionid,
990 u32 sid, char *comm)
992 struct audit_buffer *ab;
993 char *ctx = NULL;
994 u32 len;
995 int rc = 0;
997 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
998 if (!ab)
999 return rc;
1001 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
1002 from_kuid(&init_user_ns, auid),
1003 from_kuid(&init_user_ns, uid), sessionid);
1004 if (sid) {
1005 if (security_secid_to_secctx(sid, &ctx, &len)) {
1006 audit_log_format(ab, " obj=(none)");
1007 rc = 1;
1008 } else {
1009 audit_log_format(ab, " obj=%s", ctx);
1010 security_release_secctx(ctx, len);
1013 audit_log_format(ab, " ocomm=");
1014 audit_log_untrustedstring(ab, comm);
1015 audit_log_end(ab);
1017 return rc;
1020 static void audit_log_execve_info(struct audit_context *context,
1021 struct audit_buffer **ab)
1023 long len_max;
1024 long len_rem;
1025 long len_full;
1026 long len_buf;
1027 long len_abuf;
1028 long len_tmp;
1029 bool require_data;
1030 bool encode;
1031 unsigned int iter;
1032 unsigned int arg;
1033 char *buf_head;
1034 char *buf;
1035 const char __user *p = (const char __user *)current->mm->arg_start;
1037 /* NOTE: this buffer needs to be large enough to hold all the non-arg
1038 * data we put in the audit record for this argument (see the
1039 * code below) ... at this point in time 96 is plenty */
1040 char abuf[96];
1042 /* NOTE: we set MAX_EXECVE_AUDIT_LEN to a rather arbitrary limit, the
1043 * current value of 7500 is not as important as the fact that it
1044 * is less than 8k, a setting of 7500 gives us plenty of wiggle
1045 * room if we go over a little bit in the logging below */
1046 WARN_ON_ONCE(MAX_EXECVE_AUDIT_LEN > 7500);
1047 len_max = MAX_EXECVE_AUDIT_LEN;
1049 /* scratch buffer to hold the userspace args */
1050 buf_head = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1051 if (!buf_head) {
1052 audit_panic("out of memory for argv string");
1053 return;
1055 buf = buf_head;
1057 audit_log_format(*ab, "argc=%d", context->execve.argc);
1059 len_rem = len_max;
1060 len_buf = 0;
1061 len_full = 0;
1062 require_data = true;
1063 encode = false;
1064 iter = 0;
1065 arg = 0;
1066 do {
1067 /* NOTE: we don't ever want to trust this value for anything
1068 * serious, but the audit record format insists we
1069 * provide an argument length for really long arguments,
1070 * e.g. > MAX_EXECVE_AUDIT_LEN, so we have no choice but
1071 * to use strncpy_from_user() to obtain this value for
1072 * recording in the log, although we don't use it
1073 * anywhere here to avoid a double-fetch problem */
1074 if (len_full == 0)
1075 len_full = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1077 /* read more data from userspace */
1078 if (require_data) {
1079 /* can we make more room in the buffer? */
1080 if (buf != buf_head) {
1081 memmove(buf_head, buf, len_buf);
1082 buf = buf_head;
1085 /* fetch as much as we can of the argument */
1086 len_tmp = strncpy_from_user(&buf_head[len_buf], p,
1087 len_max - len_buf);
1088 if (len_tmp == -EFAULT) {
1089 /* unable to copy from userspace */
1090 send_sig(SIGKILL, current, 0);
1091 goto out;
1092 } else if (len_tmp == (len_max - len_buf)) {
1093 /* buffer is not large enough */
1094 require_data = true;
1095 /* NOTE: if we are going to span multiple
1096 * buffers force the encoding so we stand
1097 * a chance at a sane len_full value and
1098 * consistent record encoding */
1099 encode = true;
1100 len_full = len_full * 2;
1101 p += len_tmp;
1102 } else {
1103 require_data = false;
1104 if (!encode)
1105 encode = audit_string_contains_control(
1106 buf, len_tmp);
1107 /* try to use a trusted value for len_full */
1108 if (len_full < len_max)
1109 len_full = (encode ?
1110 len_tmp * 2 : len_tmp);
1111 p += len_tmp + 1;
1113 len_buf += len_tmp;
1114 buf_head[len_buf] = '\0';
1116 /* length of the buffer in the audit record? */
1117 len_abuf = (encode ? len_buf * 2 : len_buf + 2);
1120 /* write as much as we can to the audit log */
1121 if (len_buf > 0) {
1122 /* NOTE: some magic numbers here - basically if we
1123 * can't fit a reasonable amount of data into the
1124 * existing audit buffer, flush it and start with
1125 * a new buffer */
1126 if ((sizeof(abuf) + 8) > len_rem) {
1127 len_rem = len_max;
1128 audit_log_end(*ab);
1129 *ab = audit_log_start(context,
1130 GFP_KERNEL, AUDIT_EXECVE);
1131 if (!*ab)
1132 goto out;
1135 /* create the non-arg portion of the arg record */
1136 len_tmp = 0;
1137 if (require_data || (iter > 0) ||
1138 ((len_abuf + sizeof(abuf)) > len_rem)) {
1139 if (iter == 0) {
1140 len_tmp += snprintf(&abuf[len_tmp],
1141 sizeof(abuf) - len_tmp,
1142 " a%d_len=%lu",
1143 arg, len_full);
1145 len_tmp += snprintf(&abuf[len_tmp],
1146 sizeof(abuf) - len_tmp,
1147 " a%d[%d]=", arg, iter++);
1148 } else
1149 len_tmp += snprintf(&abuf[len_tmp],
1150 sizeof(abuf) - len_tmp,
1151 " a%d=", arg);
1152 WARN_ON(len_tmp >= sizeof(abuf));
1153 abuf[sizeof(abuf) - 1] = '\0';
1155 /* log the arg in the audit record */
1156 audit_log_format(*ab, "%s", abuf);
1157 len_rem -= len_tmp;
1158 len_tmp = len_buf;
1159 if (encode) {
1160 if (len_abuf > len_rem)
1161 len_tmp = len_rem / 2; /* encoding */
1162 audit_log_n_hex(*ab, buf, len_tmp);
1163 len_rem -= len_tmp * 2;
1164 len_abuf -= len_tmp * 2;
1165 } else {
1166 if (len_abuf > len_rem)
1167 len_tmp = len_rem - 2; /* quotes */
1168 audit_log_n_string(*ab, buf, len_tmp);
1169 len_rem -= len_tmp + 2;
1170 /* don't subtract the "2" because we still need
1171 * to add quotes to the remaining string */
1172 len_abuf -= len_tmp;
1174 len_buf -= len_tmp;
1175 buf += len_tmp;
1178 /* ready to move to the next argument? */
1179 if ((len_buf == 0) && !require_data) {
1180 arg++;
1181 iter = 0;
1182 len_full = 0;
1183 require_data = true;
1184 encode = false;
1186 } while (arg < context->execve.argc);
1188 /* NOTE: the caller handles the final audit_log_end() call */
1190 out:
1191 kfree(buf_head);
1194 static void show_special(struct audit_context *context, int *call_panic)
1196 struct audit_buffer *ab;
1197 int i;
1199 ab = audit_log_start(context, GFP_KERNEL, context->type);
1200 if (!ab)
1201 return;
1203 switch (context->type) {
1204 case AUDIT_SOCKETCALL: {
1205 int nargs = context->socketcall.nargs;
1206 audit_log_format(ab, "nargs=%d", nargs);
1207 for (i = 0; i < nargs; i++)
1208 audit_log_format(ab, " a%d=%lx", i,
1209 context->socketcall.args[i]);
1210 break; }
1211 case AUDIT_IPC: {
1212 u32 osid = context->ipc.osid;
1214 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1215 from_kuid(&init_user_ns, context->ipc.uid),
1216 from_kgid(&init_user_ns, context->ipc.gid),
1217 context->ipc.mode);
1218 if (osid) {
1219 char *ctx = NULL;
1220 u32 len;
1221 if (security_secid_to_secctx(osid, &ctx, &len)) {
1222 audit_log_format(ab, " osid=%u", osid);
1223 *call_panic = 1;
1224 } else {
1225 audit_log_format(ab, " obj=%s", ctx);
1226 security_release_secctx(ctx, len);
1229 if (context->ipc.has_perm) {
1230 audit_log_end(ab);
1231 ab = audit_log_start(context, GFP_KERNEL,
1232 AUDIT_IPC_SET_PERM);
1233 if (unlikely(!ab))
1234 return;
1235 audit_log_format(ab,
1236 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1237 context->ipc.qbytes,
1238 context->ipc.perm_uid,
1239 context->ipc.perm_gid,
1240 context->ipc.perm_mode);
1242 break; }
1243 case AUDIT_MQ_OPEN: {
1244 audit_log_format(ab,
1245 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1246 "mq_msgsize=%ld mq_curmsgs=%ld",
1247 context->mq_open.oflag, context->mq_open.mode,
1248 context->mq_open.attr.mq_flags,
1249 context->mq_open.attr.mq_maxmsg,
1250 context->mq_open.attr.mq_msgsize,
1251 context->mq_open.attr.mq_curmsgs);
1252 break; }
1253 case AUDIT_MQ_SENDRECV: {
1254 audit_log_format(ab,
1255 "mqdes=%d msg_len=%zd msg_prio=%u "
1256 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1257 context->mq_sendrecv.mqdes,
1258 context->mq_sendrecv.msg_len,
1259 context->mq_sendrecv.msg_prio,
1260 context->mq_sendrecv.abs_timeout.tv_sec,
1261 context->mq_sendrecv.abs_timeout.tv_nsec);
1262 break; }
1263 case AUDIT_MQ_NOTIFY: {
1264 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1265 context->mq_notify.mqdes,
1266 context->mq_notify.sigev_signo);
1267 break; }
1268 case AUDIT_MQ_GETSETATTR: {
1269 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1270 audit_log_format(ab,
1271 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1272 "mq_curmsgs=%ld ",
1273 context->mq_getsetattr.mqdes,
1274 attr->mq_flags, attr->mq_maxmsg,
1275 attr->mq_msgsize, attr->mq_curmsgs);
1276 break; }
1277 case AUDIT_CAPSET: {
1278 audit_log_format(ab, "pid=%d", context->capset.pid);
1279 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1280 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1281 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1282 break; }
1283 case AUDIT_MMAP: {
1284 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1285 context->mmap.flags);
1286 break; }
1287 case AUDIT_EXECVE: {
1288 audit_log_execve_info(context, &ab);
1289 break; }
1291 audit_log_end(ab);
1294 static inline int audit_proctitle_rtrim(char *proctitle, int len)
1296 char *end = proctitle + len - 1;
1297 while (end > proctitle && !isprint(*end))
1298 end--;
1300 /* catch the case where proctitle is only 1 non-print character */
1301 len = end - proctitle + 1;
1302 len -= isprint(proctitle[len-1]) == 0;
1303 return len;
1306 static void audit_log_proctitle(struct task_struct *tsk,
1307 struct audit_context *context)
1309 int res;
1310 char *buf;
1311 char *msg = "(null)";
1312 int len = strlen(msg);
1313 struct audit_buffer *ab;
1315 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
1316 if (!ab)
1317 return; /* audit_panic or being filtered */
1319 audit_log_format(ab, "proctitle=");
1321 /* Not cached */
1322 if (!context->proctitle.value) {
1323 buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
1324 if (!buf)
1325 goto out;
1326 /* Historically called this from procfs naming */
1327 res = get_cmdline(tsk, buf, MAX_PROCTITLE_AUDIT_LEN);
1328 if (res == 0) {
1329 kfree(buf);
1330 goto out;
1332 res = audit_proctitle_rtrim(buf, res);
1333 if (res == 0) {
1334 kfree(buf);
1335 goto out;
1337 context->proctitle.value = buf;
1338 context->proctitle.len = res;
1340 msg = context->proctitle.value;
1341 len = context->proctitle.len;
1342 out:
1343 audit_log_n_untrustedstring(ab, msg, len);
1344 audit_log_end(ab);
1347 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1349 int i, call_panic = 0;
1350 struct audit_buffer *ab;
1351 struct audit_aux_data *aux;
1352 struct audit_names *n;
1354 /* tsk == current */
1355 context->personality = tsk->personality;
1357 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1358 if (!ab)
1359 return; /* audit_panic has been called */
1360 audit_log_format(ab, "arch=%x syscall=%d",
1361 context->arch, context->major);
1362 if (context->personality != PER_LINUX)
1363 audit_log_format(ab, " per=%lx", context->personality);
1364 if (context->return_valid)
1365 audit_log_format(ab, " success=%s exit=%ld",
1366 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1367 context->return_code);
1369 audit_log_format(ab,
1370 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1371 context->argv[0],
1372 context->argv[1],
1373 context->argv[2],
1374 context->argv[3],
1375 context->name_count);
1377 audit_log_task_info(ab, tsk);
1378 audit_log_key(ab, context->filterkey);
1379 audit_log_end(ab);
1381 for (aux = context->aux; aux; aux = aux->next) {
1383 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1384 if (!ab)
1385 continue; /* audit_panic has been called */
1387 switch (aux->type) {
1389 case AUDIT_BPRM_FCAPS: {
1390 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1391 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1392 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1393 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1394 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1395 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1396 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1397 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1398 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1399 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1400 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1401 break; }
1404 audit_log_end(ab);
1407 if (context->type)
1408 show_special(context, &call_panic);
1410 if (context->fds[0] >= 0) {
1411 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1412 if (ab) {
1413 audit_log_format(ab, "fd0=%d fd1=%d",
1414 context->fds[0], context->fds[1]);
1415 audit_log_end(ab);
1419 if (context->sockaddr_len) {
1420 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1421 if (ab) {
1422 audit_log_format(ab, "saddr=");
1423 audit_log_n_hex(ab, (void *)context->sockaddr,
1424 context->sockaddr_len);
1425 audit_log_end(ab);
1429 for (aux = context->aux_pids; aux; aux = aux->next) {
1430 struct audit_aux_data_pids *axs = (void *)aux;
1432 for (i = 0; i < axs->pid_count; i++)
1433 if (audit_log_pid_context(context, axs->target_pid[i],
1434 axs->target_auid[i],
1435 axs->target_uid[i],
1436 axs->target_sessionid[i],
1437 axs->target_sid[i],
1438 axs->target_comm[i]))
1439 call_panic = 1;
1442 if (context->target_pid &&
1443 audit_log_pid_context(context, context->target_pid,
1444 context->target_auid, context->target_uid,
1445 context->target_sessionid,
1446 context->target_sid, context->target_comm))
1447 call_panic = 1;
1449 if (context->pwd.dentry && context->pwd.mnt) {
1450 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1451 if (ab) {
1452 audit_log_d_path(ab, " cwd=", &context->pwd);
1453 audit_log_end(ab);
1457 i = 0;
1458 list_for_each_entry(n, &context->names_list, list) {
1459 if (n->hidden)
1460 continue;
1461 audit_log_name(context, n, NULL, i++, &call_panic);
1464 audit_log_proctitle(tsk, context);
1466 /* Send end of event record to help user space know we are finished */
1467 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1468 if (ab)
1469 audit_log_end(ab);
1470 if (call_panic)
1471 audit_panic("error converting sid to string");
1475 * audit_free - free a per-task audit context
1476 * @tsk: task whose audit context block to free
1478 * Called from copy_process and do_exit
1480 void __audit_free(struct task_struct *tsk)
1482 struct audit_context *context;
1484 context = audit_take_context(tsk, 0, 0);
1485 if (!context)
1486 return;
1488 /* Check for system calls that do not go through the exit
1489 * function (e.g., exit_group), then free context block.
1490 * We use GFP_ATOMIC here because we might be doing this
1491 * in the context of the idle thread */
1492 /* that can happen only if we are called from do_exit() */
1493 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1494 audit_log_exit(context, tsk);
1495 if (!list_empty(&context->killed_trees))
1496 audit_kill_trees(&context->killed_trees);
1498 audit_free_context(context);
1502 * audit_syscall_entry - fill in an audit record at syscall entry
1503 * @arch: architecture type
1504 * @major: major syscall type (function)
1505 * @a1: additional syscall register 1
1506 * @a2: additional syscall register 2
1507 * @a3: additional syscall register 3
1508 * @a4: additional syscall register 4
1510 * Fill in audit context at syscall entry. This only happens if the
1511 * audit context was created when the task was created and the state or
1512 * filters demand the audit context be built. If the state from the
1513 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1514 * then the record will be written at syscall exit time (otherwise, it
1515 * will only be written if another part of the kernel requests that it
1516 * be written).
1518 void __audit_syscall_entry(int arch, int major,
1519 unsigned long a1, unsigned long a2,
1520 unsigned long a3, unsigned long a4)
1522 struct task_struct *tsk = current;
1523 struct audit_context *context = tsk->audit_context;
1524 enum audit_state state;
1526 if (!context)
1527 return;
1529 BUG_ON(context->in_syscall || context->name_count);
1531 if (!audit_enabled)
1532 return;
1534 context->arch = arch;
1535 context->major = major;
1536 context->argv[0] = a1;
1537 context->argv[1] = a2;
1538 context->argv[2] = a3;
1539 context->argv[3] = a4;
1541 state = context->state;
1542 context->dummy = !audit_n_rules;
1543 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1544 context->prio = 0;
1545 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1547 if (state == AUDIT_DISABLED)
1548 return;
1550 context->serial = 0;
1551 context->ctime = CURRENT_TIME;
1552 context->in_syscall = 1;
1553 context->current_state = state;
1554 context->ppid = 0;
1558 * audit_syscall_exit - deallocate audit context after a system call
1559 * @success: success value of the syscall
1560 * @return_code: return value of the syscall
1562 * Tear down after system call. If the audit context has been marked as
1563 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1564 * filtering, or because some other part of the kernel wrote an audit
1565 * message), then write out the syscall information. In call cases,
1566 * free the names stored from getname().
1568 void __audit_syscall_exit(int success, long return_code)
1570 struct task_struct *tsk = current;
1571 struct audit_context *context;
1573 if (success)
1574 success = AUDITSC_SUCCESS;
1575 else
1576 success = AUDITSC_FAILURE;
1578 context = audit_take_context(tsk, success, return_code);
1579 if (!context)
1580 return;
1582 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1583 audit_log_exit(context, tsk);
1585 context->in_syscall = 0;
1586 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
1588 if (!list_empty(&context->killed_trees))
1589 audit_kill_trees(&context->killed_trees);
1591 audit_free_names(context);
1592 unroll_tree_refs(context, NULL, 0);
1593 audit_free_aux(context);
1594 context->aux = NULL;
1595 context->aux_pids = NULL;
1596 context->target_pid = 0;
1597 context->target_sid = 0;
1598 context->sockaddr_len = 0;
1599 context->type = 0;
1600 context->fds[0] = -1;
1601 if (context->state != AUDIT_RECORD_CONTEXT) {
1602 kfree(context->filterkey);
1603 context->filterkey = NULL;
1605 tsk->audit_context = context;
1608 static inline void handle_one(const struct inode *inode)
1610 #ifdef CONFIG_AUDIT_TREE
1611 struct audit_context *context;
1612 struct audit_tree_refs *p;
1613 struct audit_chunk *chunk;
1614 int count;
1615 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
1616 return;
1617 context = current->audit_context;
1618 p = context->trees;
1619 count = context->tree_count;
1620 rcu_read_lock();
1621 chunk = audit_tree_lookup(inode);
1622 rcu_read_unlock();
1623 if (!chunk)
1624 return;
1625 if (likely(put_tree_ref(context, chunk)))
1626 return;
1627 if (unlikely(!grow_tree_refs(context))) {
1628 pr_warn("out of memory, audit has lost a tree reference\n");
1629 audit_set_auditable(context);
1630 audit_put_chunk(chunk);
1631 unroll_tree_refs(context, p, count);
1632 return;
1634 put_tree_ref(context, chunk);
1635 #endif
1638 static void handle_path(const struct dentry *dentry)
1640 #ifdef CONFIG_AUDIT_TREE
1641 struct audit_context *context;
1642 struct audit_tree_refs *p;
1643 const struct dentry *d, *parent;
1644 struct audit_chunk *drop;
1645 unsigned long seq;
1646 int count;
1648 context = current->audit_context;
1649 p = context->trees;
1650 count = context->tree_count;
1651 retry:
1652 drop = NULL;
1653 d = dentry;
1654 rcu_read_lock();
1655 seq = read_seqbegin(&rename_lock);
1656 for(;;) {
1657 struct inode *inode = d->d_inode;
1658 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
1659 struct audit_chunk *chunk;
1660 chunk = audit_tree_lookup(inode);
1661 if (chunk) {
1662 if (unlikely(!put_tree_ref(context, chunk))) {
1663 drop = chunk;
1664 break;
1668 parent = d->d_parent;
1669 if (parent == d)
1670 break;
1671 d = parent;
1673 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1674 rcu_read_unlock();
1675 if (!drop) {
1676 /* just a race with rename */
1677 unroll_tree_refs(context, p, count);
1678 goto retry;
1680 audit_put_chunk(drop);
1681 if (grow_tree_refs(context)) {
1682 /* OK, got more space */
1683 unroll_tree_refs(context, p, count);
1684 goto retry;
1686 /* too bad */
1687 pr_warn("out of memory, audit has lost a tree reference\n");
1688 unroll_tree_refs(context, p, count);
1689 audit_set_auditable(context);
1690 return;
1692 rcu_read_unlock();
1693 #endif
1696 static struct audit_names *audit_alloc_name(struct audit_context *context,
1697 unsigned char type)
1699 struct audit_names *aname;
1701 if (context->name_count < AUDIT_NAMES) {
1702 aname = &context->preallocated_names[context->name_count];
1703 memset(aname, 0, sizeof(*aname));
1704 } else {
1705 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1706 if (!aname)
1707 return NULL;
1708 aname->should_free = true;
1711 aname->ino = (unsigned long)-1;
1712 aname->type = type;
1713 list_add_tail(&aname->list, &context->names_list);
1715 context->name_count++;
1716 #if AUDIT_DEBUG
1717 context->ino_count++;
1718 #endif
1719 return aname;
1723 * audit_reusename - fill out filename with info from existing entry
1724 * @uptr: userland ptr to pathname
1726 * Search the audit_names list for the current audit context. If there is an
1727 * existing entry with a matching "uptr" then return the filename
1728 * associated with that audit_name. If not, return NULL.
1730 struct filename *
1731 __audit_reusename(const __user char *uptr)
1733 struct audit_context *context = current->audit_context;
1734 struct audit_names *n;
1736 list_for_each_entry(n, &context->names_list, list) {
1737 if (!n->name)
1738 continue;
1739 if (n->name->uptr == uptr)
1740 return n->name;
1742 return NULL;
1746 * audit_getname - add a name to the list
1747 * @name: name to add
1749 * Add a name to the list of audit names for this context.
1750 * Called from fs/namei.c:getname().
1752 void __audit_getname(struct filename *name)
1754 struct audit_context *context = current->audit_context;
1755 struct audit_names *n;
1757 if (!context->in_syscall) {
1758 #if AUDIT_DEBUG == 2
1759 pr_err("%s:%d(:%d): ignoring getname(%p)\n",
1760 __FILE__, __LINE__, context->serial, name);
1761 dump_stack();
1762 #endif
1763 return;
1766 #if AUDIT_DEBUG
1767 /* The filename _must_ have a populated ->name */
1768 BUG_ON(!name->name);
1769 #endif
1771 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
1772 if (!n)
1773 return;
1775 n->name = name;
1776 n->name_len = AUDIT_NAME_FULL;
1777 n->name_put = true;
1778 name->aname = n;
1780 if (!context->pwd.dentry)
1781 get_fs_pwd(current->fs, &context->pwd);
1784 /* audit_putname - intercept a putname request
1785 * @name: name to intercept and delay for putname
1787 * If we have stored the name from getname in the audit context,
1788 * then we delay the putname until syscall exit.
1789 * Called from include/linux/fs.h:putname().
1791 void audit_putname(struct filename *name)
1793 struct audit_context *context = current->audit_context;
1795 BUG_ON(!context);
1796 if (!name->aname || !context->in_syscall) {
1797 #if AUDIT_DEBUG == 2
1798 pr_err("%s:%d(:%d): final_putname(%p)\n",
1799 __FILE__, __LINE__, context->serial, name);
1800 if (context->name_count) {
1801 struct audit_names *n;
1802 int i = 0;
1804 list_for_each_entry(n, &context->names_list, list)
1805 pr_err("name[%d] = %p = %s\n", i++, n->name,
1806 n->name->name ?: "(null)");
1808 #endif
1809 final_putname(name);
1811 #if AUDIT_DEBUG
1812 else {
1813 ++context->put_count;
1814 if (context->put_count > context->name_count) {
1815 pr_err("%s:%d(:%d): major=%d in_syscall=%d putname(%p)"
1816 " name_count=%d put_count=%d\n",
1817 __FILE__, __LINE__,
1818 context->serial, context->major,
1819 context->in_syscall, name->name,
1820 context->name_count, context->put_count);
1821 dump_stack();
1824 #endif
1828 * __audit_inode - store the inode and device from a lookup
1829 * @name: name being audited
1830 * @dentry: dentry being audited
1831 * @flags: attributes for this particular entry
1833 void __audit_inode(struct filename *name, const struct dentry *dentry,
1834 unsigned int flags)
1836 struct audit_context *context = current->audit_context;
1837 const struct inode *inode = dentry->d_inode;
1838 struct audit_names *n;
1839 bool parent = flags & AUDIT_INODE_PARENT;
1841 if (!context->in_syscall)
1842 return;
1844 if (!name)
1845 goto out_alloc;
1847 #if AUDIT_DEBUG
1848 /* The struct filename _must_ have a populated ->name */
1849 BUG_ON(!name->name);
1850 #endif
1852 * If we have a pointer to an audit_names entry already, then we can
1853 * just use it directly if the type is correct.
1855 n = name->aname;
1856 if (n) {
1857 if (parent) {
1858 if (n->type == AUDIT_TYPE_PARENT ||
1859 n->type == AUDIT_TYPE_UNKNOWN)
1860 goto out;
1861 } else {
1862 if (n->type != AUDIT_TYPE_PARENT)
1863 goto out;
1867 list_for_each_entry_reverse(n, &context->names_list, list) {
1868 /* does the name pointer match? */
1869 if (!n->name || n->name->name != name->name)
1870 continue;
1872 /* match the correct record type */
1873 if (parent) {
1874 if (n->type == AUDIT_TYPE_PARENT ||
1875 n->type == AUDIT_TYPE_UNKNOWN)
1876 goto out;
1877 } else {
1878 if (n->type != AUDIT_TYPE_PARENT)
1879 goto out;
1883 out_alloc:
1884 /* unable to find the name from a previous getname(). Allocate a new
1885 * anonymous entry.
1887 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
1888 if (!n)
1889 return;
1890 out:
1891 if (parent) {
1892 n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
1893 n->type = AUDIT_TYPE_PARENT;
1894 if (flags & AUDIT_INODE_HIDDEN)
1895 n->hidden = true;
1896 } else {
1897 n->name_len = AUDIT_NAME_FULL;
1898 n->type = AUDIT_TYPE_NORMAL;
1900 handle_path(dentry);
1901 audit_copy_inode(n, dentry, inode);
1905 * __audit_inode_child - collect inode info for created/removed objects
1906 * @parent: inode of dentry parent
1907 * @dentry: dentry being audited
1908 * @type: AUDIT_TYPE_* value that we're looking for
1910 * For syscalls that create or remove filesystem objects, audit_inode
1911 * can only collect information for the filesystem object's parent.
1912 * This call updates the audit context with the child's information.
1913 * Syscalls that create a new filesystem object must be hooked after
1914 * the object is created. Syscalls that remove a filesystem object
1915 * must be hooked prior, in order to capture the target inode during
1916 * unsuccessful attempts.
1918 void __audit_inode_child(const struct inode *parent,
1919 const struct dentry *dentry,
1920 const unsigned char type)
1922 struct audit_context *context = current->audit_context;
1923 const struct inode *inode = dentry->d_inode;
1924 const char *dname = dentry->d_name.name;
1925 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
1927 if (!context->in_syscall)
1928 return;
1930 if (inode)
1931 handle_one(inode);
1933 /* look for a parent entry first */
1934 list_for_each_entry(n, &context->names_list, list) {
1935 if (!n->name || n->type != AUDIT_TYPE_PARENT)
1936 continue;
1938 if (n->ino == parent->i_ino &&
1939 !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
1940 found_parent = n;
1941 break;
1945 /* is there a matching child entry? */
1946 list_for_each_entry(n, &context->names_list, list) {
1947 /* can only match entries that have a name */
1948 if (!n->name || n->type != type)
1949 continue;
1951 /* if we found a parent, make sure this one is a child of it */
1952 if (found_parent && (n->name != found_parent->name))
1953 continue;
1955 if (!strcmp(dname, n->name->name) ||
1956 !audit_compare_dname_path(dname, n->name->name,
1957 found_parent ?
1958 found_parent->name_len :
1959 AUDIT_NAME_FULL)) {
1960 found_child = n;
1961 break;
1965 if (!found_parent) {
1966 /* create a new, "anonymous" parent record */
1967 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
1968 if (!n)
1969 return;
1970 audit_copy_inode(n, NULL, parent);
1973 if (!found_child) {
1974 found_child = audit_alloc_name(context, type);
1975 if (!found_child)
1976 return;
1978 /* Re-use the name belonging to the slot for a matching parent
1979 * directory. All names for this context are relinquished in
1980 * audit_free_names() */
1981 if (found_parent) {
1982 found_child->name = found_parent->name;
1983 found_child->name_len = AUDIT_NAME_FULL;
1984 /* don't call __putname() */
1985 found_child->name_put = false;
1988 if (inode)
1989 audit_copy_inode(found_child, dentry, inode);
1990 else
1991 found_child->ino = (unsigned long)-1;
1993 EXPORT_SYMBOL_GPL(__audit_inode_child);
1996 * auditsc_get_stamp - get local copies of audit_context values
1997 * @ctx: audit_context for the task
1998 * @t: timespec to store time recorded in the audit_context
1999 * @serial: serial value that is recorded in the audit_context
2001 * Also sets the context as auditable.
2003 int auditsc_get_stamp(struct audit_context *ctx,
2004 struct timespec *t, unsigned int *serial)
2006 if (!ctx->in_syscall)
2007 return 0;
2008 if (!ctx->serial)
2009 ctx->serial = audit_serial();
2010 t->tv_sec = ctx->ctime.tv_sec;
2011 t->tv_nsec = ctx->ctime.tv_nsec;
2012 *serial = ctx->serial;
2013 if (!ctx->prio) {
2014 ctx->prio = 1;
2015 ctx->current_state = AUDIT_RECORD_CONTEXT;
2017 return 1;
2020 /* global counter which is incremented every time something logs in */
2021 static atomic_t session_id = ATOMIC_INIT(0);
2023 static int audit_set_loginuid_perm(kuid_t loginuid)
2025 /* if we are unset, we don't need privs */
2026 if (!audit_loginuid_set(current))
2027 return 0;
2028 /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
2029 if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
2030 return -EPERM;
2031 /* it is set, you need permission */
2032 if (!capable(CAP_AUDIT_CONTROL))
2033 return -EPERM;
2034 /* reject if this is not an unset and we don't allow that */
2035 if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
2036 return -EPERM;
2037 return 0;
2040 static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
2041 unsigned int oldsessionid, unsigned int sessionid,
2042 int rc)
2044 struct audit_buffer *ab;
2045 uid_t uid, oldloginuid, loginuid;
2047 if (!audit_enabled)
2048 return;
2050 uid = from_kuid(&init_user_ns, task_uid(current));
2051 oldloginuid = from_kuid(&init_user_ns, koldloginuid);
2052 loginuid = from_kuid(&init_user_ns, kloginuid),
2054 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2055 if (!ab)
2056 return;
2057 audit_log_format(ab, "pid=%d uid=%u", task_pid_nr(current), uid);
2058 audit_log_task_context(ab);
2059 audit_log_format(ab, " old-auid=%u auid=%u old-ses=%u ses=%u res=%d",
2060 oldloginuid, loginuid, oldsessionid, sessionid, !rc);
2061 audit_log_end(ab);
2065 * audit_set_loginuid - set current task's audit_context loginuid
2066 * @loginuid: loginuid value
2068 * Returns 0.
2070 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2072 int audit_set_loginuid(kuid_t loginuid)
2074 struct task_struct *task = current;
2075 unsigned int oldsessionid, sessionid = (unsigned int)-1;
2076 kuid_t oldloginuid;
2077 int rc;
2079 oldloginuid = audit_get_loginuid(current);
2080 oldsessionid = audit_get_sessionid(current);
2082 rc = audit_set_loginuid_perm(loginuid);
2083 if (rc)
2084 goto out;
2086 /* are we setting or clearing? */
2087 if (uid_valid(loginuid))
2088 sessionid = (unsigned int)atomic_inc_return(&session_id);
2090 task->sessionid = sessionid;
2091 task->loginuid = loginuid;
2092 out:
2093 audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
2094 return rc;
2098 * __audit_mq_open - record audit data for a POSIX MQ open
2099 * @oflag: open flag
2100 * @mode: mode bits
2101 * @attr: queue attributes
2104 void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2106 struct audit_context *context = current->audit_context;
2108 if (attr)
2109 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2110 else
2111 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2113 context->mq_open.oflag = oflag;
2114 context->mq_open.mode = mode;
2116 context->type = AUDIT_MQ_OPEN;
2120 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2121 * @mqdes: MQ descriptor
2122 * @msg_len: Message length
2123 * @msg_prio: Message priority
2124 * @abs_timeout: Message timeout in absolute time
2127 void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2128 const struct timespec *abs_timeout)
2130 struct audit_context *context = current->audit_context;
2131 struct timespec *p = &context->mq_sendrecv.abs_timeout;
2133 if (abs_timeout)
2134 memcpy(p, abs_timeout, sizeof(struct timespec));
2135 else
2136 memset(p, 0, sizeof(struct timespec));
2138 context->mq_sendrecv.mqdes = mqdes;
2139 context->mq_sendrecv.msg_len = msg_len;
2140 context->mq_sendrecv.msg_prio = msg_prio;
2142 context->type = AUDIT_MQ_SENDRECV;
2146 * __audit_mq_notify - record audit data for a POSIX MQ notify
2147 * @mqdes: MQ descriptor
2148 * @notification: Notification event
2152 void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2154 struct audit_context *context = current->audit_context;
2156 if (notification)
2157 context->mq_notify.sigev_signo = notification->sigev_signo;
2158 else
2159 context->mq_notify.sigev_signo = 0;
2161 context->mq_notify.mqdes = mqdes;
2162 context->type = AUDIT_MQ_NOTIFY;
2166 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2167 * @mqdes: MQ descriptor
2168 * @mqstat: MQ flags
2171 void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2173 struct audit_context *context = current->audit_context;
2174 context->mq_getsetattr.mqdes = mqdes;
2175 context->mq_getsetattr.mqstat = *mqstat;
2176 context->type = AUDIT_MQ_GETSETATTR;
2180 * audit_ipc_obj - record audit data for ipc object
2181 * @ipcp: ipc permissions
2184 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2186 struct audit_context *context = current->audit_context;
2187 context->ipc.uid = ipcp->uid;
2188 context->ipc.gid = ipcp->gid;
2189 context->ipc.mode = ipcp->mode;
2190 context->ipc.has_perm = 0;
2191 security_ipc_getsecid(ipcp, &context->ipc.osid);
2192 context->type = AUDIT_IPC;
2196 * audit_ipc_set_perm - record audit data for new ipc permissions
2197 * @qbytes: msgq bytes
2198 * @uid: msgq user id
2199 * @gid: msgq group id
2200 * @mode: msgq mode (permissions)
2202 * Called only after audit_ipc_obj().
2204 void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2206 struct audit_context *context = current->audit_context;
2208 context->ipc.qbytes = qbytes;
2209 context->ipc.perm_uid = uid;
2210 context->ipc.perm_gid = gid;
2211 context->ipc.perm_mode = mode;
2212 context->ipc.has_perm = 1;
2215 void __audit_bprm(struct linux_binprm *bprm)
2217 struct audit_context *context = current->audit_context;
2219 context->type = AUDIT_EXECVE;
2220 context->execve.argc = bprm->argc;
2225 * audit_socketcall - record audit data for sys_socketcall
2226 * @nargs: number of args, which should not be more than AUDITSC_ARGS.
2227 * @args: args array
2230 int __audit_socketcall(int nargs, unsigned long *args)
2232 struct audit_context *context = current->audit_context;
2234 if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2235 return -EINVAL;
2236 context->type = AUDIT_SOCKETCALL;
2237 context->socketcall.nargs = nargs;
2238 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2239 return 0;
2243 * __audit_fd_pair - record audit data for pipe and socketpair
2244 * @fd1: the first file descriptor
2245 * @fd2: the second file descriptor
2248 void __audit_fd_pair(int fd1, int fd2)
2250 struct audit_context *context = current->audit_context;
2251 context->fds[0] = fd1;
2252 context->fds[1] = fd2;
2256 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2257 * @len: data length in user space
2258 * @a: data address in kernel space
2260 * Returns 0 for success or NULL context or < 0 on error.
2262 int __audit_sockaddr(int len, void *a)
2264 struct audit_context *context = current->audit_context;
2266 if (!context->sockaddr) {
2267 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2268 if (!p)
2269 return -ENOMEM;
2270 context->sockaddr = p;
2273 context->sockaddr_len = len;
2274 memcpy(context->sockaddr, a, len);
2275 return 0;
2278 void __audit_ptrace(struct task_struct *t)
2280 struct audit_context *context = current->audit_context;
2282 context->target_pid = task_pid_nr(t);
2283 context->target_auid = audit_get_loginuid(t);
2284 context->target_uid = task_uid(t);
2285 context->target_sessionid = audit_get_sessionid(t);
2286 security_task_getsecid(t, &context->target_sid);
2287 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2291 * audit_signal_info - record signal info for shutting down audit subsystem
2292 * @sig: signal value
2293 * @t: task being signaled
2295 * If the audit subsystem is being terminated, record the task (pid)
2296 * and uid that is doing that.
2298 int __audit_signal_info(int sig, struct task_struct *t)
2300 struct audit_aux_data_pids *axp;
2301 struct task_struct *tsk = current;
2302 struct audit_context *ctx = tsk->audit_context;
2303 kuid_t uid = current_uid(), t_uid = task_uid(t);
2305 if (audit_pid && t->tgid == audit_pid) {
2306 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2307 audit_sig_pid = task_pid_nr(tsk);
2308 if (uid_valid(tsk->loginuid))
2309 audit_sig_uid = tsk->loginuid;
2310 else
2311 audit_sig_uid = uid;
2312 security_task_getsecid(tsk, &audit_sig_sid);
2314 if (!audit_signals || audit_dummy_context())
2315 return 0;
2318 /* optimize the common case by putting first signal recipient directly
2319 * in audit_context */
2320 if (!ctx->target_pid) {
2321 ctx->target_pid = task_tgid_nr(t);
2322 ctx->target_auid = audit_get_loginuid(t);
2323 ctx->target_uid = t_uid;
2324 ctx->target_sessionid = audit_get_sessionid(t);
2325 security_task_getsecid(t, &ctx->target_sid);
2326 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2327 return 0;
2330 axp = (void *)ctx->aux_pids;
2331 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2332 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2333 if (!axp)
2334 return -ENOMEM;
2336 axp->d.type = AUDIT_OBJ_PID;
2337 axp->d.next = ctx->aux_pids;
2338 ctx->aux_pids = (void *)axp;
2340 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2342 axp->target_pid[axp->pid_count] = task_tgid_nr(t);
2343 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2344 axp->target_uid[axp->pid_count] = t_uid;
2345 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2346 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2347 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2348 axp->pid_count++;
2350 return 0;
2354 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2355 * @bprm: pointer to the bprm being processed
2356 * @new: the proposed new credentials
2357 * @old: the old credentials
2359 * Simply check if the proc already has the caps given by the file and if not
2360 * store the priv escalation info for later auditing at the end of the syscall
2362 * -Eric
2364 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2365 const struct cred *new, const struct cred *old)
2367 struct audit_aux_data_bprm_fcaps *ax;
2368 struct audit_context *context = current->audit_context;
2369 struct cpu_vfs_cap_data vcaps;
2370 struct dentry *dentry;
2372 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2373 if (!ax)
2374 return -ENOMEM;
2376 ax->d.type = AUDIT_BPRM_FCAPS;
2377 ax->d.next = context->aux;
2378 context->aux = (void *)ax;
2380 dentry = dget(bprm->file->f_dentry);
2381 get_vfs_caps_from_disk(dentry, &vcaps);
2382 dput(dentry);
2384 ax->fcap.permitted = vcaps.permitted;
2385 ax->fcap.inheritable = vcaps.inheritable;
2386 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2387 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2389 ax->old_pcap.permitted = old->cap_permitted;
2390 ax->old_pcap.inheritable = old->cap_inheritable;
2391 ax->old_pcap.effective = old->cap_effective;
2393 ax->new_pcap.permitted = new->cap_permitted;
2394 ax->new_pcap.inheritable = new->cap_inheritable;
2395 ax->new_pcap.effective = new->cap_effective;
2396 return 0;
2400 * __audit_log_capset - store information about the arguments to the capset syscall
2401 * @new: the new credentials
2402 * @old: the old (current) credentials
2404 * Record the aguments userspace sent to sys_capset for later printing by the
2405 * audit system if applicable
2407 void __audit_log_capset(const struct cred *new, const struct cred *old)
2409 struct audit_context *context = current->audit_context;
2410 context->capset.pid = task_pid_nr(current);
2411 context->capset.cap.effective = new->cap_effective;
2412 context->capset.cap.inheritable = new->cap_effective;
2413 context->capset.cap.permitted = new->cap_permitted;
2414 context->type = AUDIT_CAPSET;
2417 void __audit_mmap_fd(int fd, int flags)
2419 struct audit_context *context = current->audit_context;
2420 context->mmap.fd = fd;
2421 context->mmap.flags = flags;
2422 context->type = AUDIT_MMAP;
2425 static void audit_log_task(struct audit_buffer *ab)
2427 kuid_t auid, uid;
2428 kgid_t gid;
2429 unsigned int sessionid;
2430 struct mm_struct *mm = current->mm;
2432 auid = audit_get_loginuid(current);
2433 sessionid = audit_get_sessionid(current);
2434 current_uid_gid(&uid, &gid);
2436 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2437 from_kuid(&init_user_ns, auid),
2438 from_kuid(&init_user_ns, uid),
2439 from_kgid(&init_user_ns, gid),
2440 sessionid);
2441 audit_log_task_context(ab);
2442 audit_log_format(ab, " pid=%d comm=", task_pid_nr(current));
2443 audit_log_untrustedstring(ab, current->comm);
2444 if (mm) {
2445 down_read(&mm->mmap_sem);
2446 if (mm->exe_file)
2447 audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
2448 up_read(&mm->mmap_sem);
2449 } else
2450 audit_log_format(ab, " exe=(null)");
2454 * audit_core_dumps - record information about processes that end abnormally
2455 * @signr: signal value
2457 * If a process ends with a core dump, something fishy is going on and we
2458 * should record the event for investigation.
2460 void audit_core_dumps(long signr)
2462 struct audit_buffer *ab;
2464 if (!audit_enabled)
2465 return;
2467 if (signr == SIGQUIT) /* don't care for those */
2468 return;
2470 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2471 if (unlikely(!ab))
2472 return;
2473 audit_log_task(ab);
2474 audit_log_format(ab, " sig=%ld", signr);
2475 audit_log_end(ab);
2478 void __audit_seccomp(unsigned long syscall, long signr, int code)
2480 struct audit_buffer *ab;
2482 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
2483 if (unlikely(!ab))
2484 return;
2485 audit_log_task(ab);
2486 audit_log_format(ab, " sig=%ld", signr);
2487 audit_log_format(ab, " syscall=%ld", syscall);
2488 audit_log_format(ab, " compat=%d", is_compat_task());
2489 audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
2490 audit_log_format(ab, " code=0x%x", code);
2491 audit_log_end(ab);
2494 struct list_head *audit_killed_trees(void)
2496 struct audit_context *ctx = current->audit_context;
2497 if (likely(!ctx || !ctx->in_syscall))
2498 return NULL;
2499 return &ctx->killed_trees;