a09c503170597e9624a0491e85abb8b15e6798f8
[deliverable/linux.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 #include <linux/init.h>
46 #include <asm/types.h>
47 #include <linux/atomic.h>
48 #include <linux/fs.h>
49 #include <linux/namei.h>
50 #include <linux/mm.h>
51 #include <linux/export.h>
52 #include <linux/slab.h>
53 #include <linux/mount.h>
54 #include <linux/socket.h>
55 #include <linux/mqueue.h>
56 #include <linux/audit.h>
57 #include <linux/personality.h>
58 #include <linux/time.h>
59 #include <linux/netlink.h>
60 #include <linux/compiler.h>
61 #include <asm/unistd.h>
62 #include <linux/security.h>
63 #include <linux/list.h>
64 #include <linux/tty.h>
65 #include <linux/binfmts.h>
66 #include <linux/highmem.h>
67 #include <linux/syscalls.h>
68 #include <linux/capability.h>
69 #include <linux/fs_struct.h>
70
71 #include "audit.h"
72
73 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
74 * for saving names from getname(). */
75 #define AUDIT_NAMES 20
76
77 /* Indicates that audit should log the full pathname. */
78 #define AUDIT_NAME_FULL -1
79
80 /* no execve audit message should be longer than this (userspace limits) */
81 #define MAX_EXECVE_AUDIT_LEN 7500
82
83 /* number of audit rules */
84 int audit_n_rules;
85
86 /* determines whether we collect data for signals sent */
87 int audit_signals;
88
89 struct audit_cap_data {
90 kernel_cap_t permitted;
91 kernel_cap_t inheritable;
92 union {
93 unsigned int fE; /* effective bit of a file capability */
94 kernel_cap_t effective; /* effective set of a process */
95 };
96 };
97
98 /* When fs/namei.c:getname() is called, we store the pointer in name and
99 * we don't let putname() free it (instead we free all of the saved
100 * pointers at syscall exit time).
101 *
102 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
103 struct audit_names {
104 const char *name;
105 int name_len; /* number of name's characters to log */
106 unsigned name_put; /* call __putname() for this name */
107 unsigned long ino;
108 dev_t dev;
109 umode_t mode;
110 uid_t uid;
111 gid_t gid;
112 dev_t rdev;
113 u32 osid;
114 struct audit_cap_data fcap;
115 unsigned int fcap_ver;
116 };
117
118 struct audit_aux_data {
119 struct audit_aux_data *next;
120 int type;
121 };
122
123 #define AUDIT_AUX_IPCPERM 0
124
125 /* Number of target pids per aux struct. */
126 #define AUDIT_AUX_PIDS 16
127
128 struct audit_aux_data_execve {
129 struct audit_aux_data d;
130 int argc;
131 int envc;
132 struct mm_struct *mm;
133 };
134
135 struct audit_aux_data_pids {
136 struct audit_aux_data d;
137 pid_t target_pid[AUDIT_AUX_PIDS];
138 uid_t target_auid[AUDIT_AUX_PIDS];
139 uid_t target_uid[AUDIT_AUX_PIDS];
140 unsigned int target_sessionid[AUDIT_AUX_PIDS];
141 u32 target_sid[AUDIT_AUX_PIDS];
142 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
143 int pid_count;
144 };
145
146 struct audit_aux_data_bprm_fcaps {
147 struct audit_aux_data d;
148 struct audit_cap_data fcap;
149 unsigned int fcap_ver;
150 struct audit_cap_data old_pcap;
151 struct audit_cap_data new_pcap;
152 };
153
154 struct audit_aux_data_capset {
155 struct audit_aux_data d;
156 pid_t pid;
157 struct audit_cap_data cap;
158 };
159
160 struct audit_tree_refs {
161 struct audit_tree_refs *next;
162 struct audit_chunk *c[31];
163 };
164
165 /* The per-task audit context. */
166 struct audit_context {
167 int dummy; /* must be the first element */
168 int in_syscall; /* 1 if task is in a syscall */
169 enum audit_state state, current_state;
170 unsigned int serial; /* serial number for record */
171 int major; /* syscall number */
172 struct timespec ctime; /* time of syscall entry */
173 unsigned long argv[4]; /* syscall arguments */
174 long return_code;/* syscall return code */
175 u64 prio;
176 int return_valid; /* return code is valid */
177 int name_count;
178 struct audit_names names[AUDIT_NAMES];
179 char * filterkey; /* key for rule that triggered record */
180 struct path pwd;
181 struct audit_context *previous; /* For nested syscalls */
182 struct audit_aux_data *aux;
183 struct audit_aux_data *aux_pids;
184 struct sockaddr_storage *sockaddr;
185 size_t sockaddr_len;
186 /* Save things to print about task_struct */
187 pid_t pid, ppid;
188 uid_t uid, euid, suid, fsuid;
189 gid_t gid, egid, sgid, fsgid;
190 unsigned long personality;
191 int arch;
192
193 pid_t target_pid;
194 uid_t target_auid;
195 uid_t target_uid;
196 unsigned int target_sessionid;
197 u32 target_sid;
198 char target_comm[TASK_COMM_LEN];
199
200 struct audit_tree_refs *trees, *first_trees;
201 struct list_head killed_trees;
202 int tree_count;
203
204 int type;
205 union {
206 struct {
207 int nargs;
208 long args[6];
209 } socketcall;
210 struct {
211 uid_t uid;
212 gid_t gid;
213 umode_t mode;
214 u32 osid;
215 int has_perm;
216 uid_t perm_uid;
217 gid_t perm_gid;
218 umode_t perm_mode;
219 unsigned long qbytes;
220 } ipc;
221 struct {
222 mqd_t mqdes;
223 struct mq_attr mqstat;
224 } mq_getsetattr;
225 struct {
226 mqd_t mqdes;
227 int sigev_signo;
228 } mq_notify;
229 struct {
230 mqd_t mqdes;
231 size_t msg_len;
232 unsigned int msg_prio;
233 struct timespec abs_timeout;
234 } mq_sendrecv;
235 struct {
236 int oflag;
237 umode_t mode;
238 struct mq_attr attr;
239 } mq_open;
240 struct {
241 pid_t pid;
242 struct audit_cap_data cap;
243 } capset;
244 struct {
245 int fd;
246 int flags;
247 } mmap;
248 };
249 int fds[2];
250
251 #if AUDIT_DEBUG
252 int put_count;
253 int ino_count;
254 #endif
255 };
256
257 static inline int open_arg(int flags, int mask)
258 {
259 int n = ACC_MODE(flags);
260 if (flags & (O_TRUNC | O_CREAT))
261 n |= AUDIT_PERM_WRITE;
262 return n & mask;
263 }
264
265 static int audit_match_perm(struct audit_context *ctx, int mask)
266 {
267 unsigned n;
268 if (unlikely(!ctx))
269 return 0;
270 n = ctx->major;
271
272 switch (audit_classify_syscall(ctx->arch, n)) {
273 case 0: /* native */
274 if ((mask & AUDIT_PERM_WRITE) &&
275 audit_match_class(AUDIT_CLASS_WRITE, n))
276 return 1;
277 if ((mask & AUDIT_PERM_READ) &&
278 audit_match_class(AUDIT_CLASS_READ, n))
279 return 1;
280 if ((mask & AUDIT_PERM_ATTR) &&
281 audit_match_class(AUDIT_CLASS_CHATTR, n))
282 return 1;
283 return 0;
284 case 1: /* 32bit on biarch */
285 if ((mask & AUDIT_PERM_WRITE) &&
286 audit_match_class(AUDIT_CLASS_WRITE_32, n))
287 return 1;
288 if ((mask & AUDIT_PERM_READ) &&
289 audit_match_class(AUDIT_CLASS_READ_32, n))
290 return 1;
291 if ((mask & AUDIT_PERM_ATTR) &&
292 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
293 return 1;
294 return 0;
295 case 2: /* open */
296 return mask & ACC_MODE(ctx->argv[1]);
297 case 3: /* openat */
298 return mask & ACC_MODE(ctx->argv[2]);
299 case 4: /* socketcall */
300 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
301 case 5: /* execve */
302 return mask & AUDIT_PERM_EXEC;
303 default:
304 return 0;
305 }
306 }
307
308 static int audit_match_filetype(struct audit_context *ctx, int val)
309 {
310 int index;
311 umode_t mode = (umode_t)val;
312
313 if (unlikely(!ctx))
314 return 0;
315
316 for (index = 0; index < ctx->name_count; index++) {
317 if ((ctx->names[index].ino != -1) &&
318 ((ctx->names[index].mode & S_IFMT) == mode))
319 return 1;
320 }
321 return 0;
322 }
323
324 /*
325 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
326 * ->first_trees points to its beginning, ->trees - to the current end of data.
327 * ->tree_count is the number of free entries in array pointed to by ->trees.
328 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
329 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
330 * it's going to remain 1-element for almost any setup) until we free context itself.
331 * References in it _are_ dropped - at the same time we free/drop aux stuff.
332 */
333
334 #ifdef CONFIG_AUDIT_TREE
335 static void audit_set_auditable(struct audit_context *ctx)
336 {
337 if (!ctx->prio) {
338 ctx->prio = 1;
339 ctx->current_state = AUDIT_RECORD_CONTEXT;
340 }
341 }
342
343 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
344 {
345 struct audit_tree_refs *p = ctx->trees;
346 int left = ctx->tree_count;
347 if (likely(left)) {
348 p->c[--left] = chunk;
349 ctx->tree_count = left;
350 return 1;
351 }
352 if (!p)
353 return 0;
354 p = p->next;
355 if (p) {
356 p->c[30] = chunk;
357 ctx->trees = p;
358 ctx->tree_count = 30;
359 return 1;
360 }
361 return 0;
362 }
363
364 static int grow_tree_refs(struct audit_context *ctx)
365 {
366 struct audit_tree_refs *p = ctx->trees;
367 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
368 if (!ctx->trees) {
369 ctx->trees = p;
370 return 0;
371 }
372 if (p)
373 p->next = ctx->trees;
374 else
375 ctx->first_trees = ctx->trees;
376 ctx->tree_count = 31;
377 return 1;
378 }
379 #endif
380
381 static void unroll_tree_refs(struct audit_context *ctx,
382 struct audit_tree_refs *p, int count)
383 {
384 #ifdef CONFIG_AUDIT_TREE
385 struct audit_tree_refs *q;
386 int n;
387 if (!p) {
388 /* we started with empty chain */
389 p = ctx->first_trees;
390 count = 31;
391 /* if the very first allocation has failed, nothing to do */
392 if (!p)
393 return;
394 }
395 n = count;
396 for (q = p; q != ctx->trees; q = q->next, n = 31) {
397 while (n--) {
398 audit_put_chunk(q->c[n]);
399 q->c[n] = NULL;
400 }
401 }
402 while (n-- > ctx->tree_count) {
403 audit_put_chunk(q->c[n]);
404 q->c[n] = NULL;
405 }
406 ctx->trees = p;
407 ctx->tree_count = count;
408 #endif
409 }
410
411 static void free_tree_refs(struct audit_context *ctx)
412 {
413 struct audit_tree_refs *p, *q;
414 for (p = ctx->first_trees; p; p = q) {
415 q = p->next;
416 kfree(p);
417 }
418 }
419
420 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
421 {
422 #ifdef CONFIG_AUDIT_TREE
423 struct audit_tree_refs *p;
424 int n;
425 if (!tree)
426 return 0;
427 /* full ones */
428 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
429 for (n = 0; n < 31; n++)
430 if (audit_tree_match(p->c[n], tree))
431 return 1;
432 }
433 /* partial */
434 if (p) {
435 for (n = ctx->tree_count; n < 31; n++)
436 if (audit_tree_match(p->c[n], tree))
437 return 1;
438 }
439 #endif
440 return 0;
441 }
442
443 /* Determine if any context name data matches a rule's watch data */
444 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
445 * otherwise.
446 *
447 * If task_creation is true, this is an explicit indication that we are
448 * filtering a task rule at task creation time. This and tsk == current are
449 * the only situations where tsk->cred may be accessed without an rcu read lock.
450 */
451 static int audit_filter_rules(struct task_struct *tsk,
452 struct audit_krule *rule,
453 struct audit_context *ctx,
454 struct audit_names *name,
455 enum audit_state *state,
456 bool task_creation)
457 {
458 const struct cred *cred;
459 int i, j, need_sid = 1;
460 u32 sid;
461
462 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
463
464 for (i = 0; i < rule->field_count; i++) {
465 struct audit_field *f = &rule->fields[i];
466 int result = 0;
467
468 switch (f->type) {
469 case AUDIT_PID:
470 result = audit_comparator(tsk->pid, f->op, f->val);
471 break;
472 case AUDIT_PPID:
473 if (ctx) {
474 if (!ctx->ppid)
475 ctx->ppid = sys_getppid();
476 result = audit_comparator(ctx->ppid, f->op, f->val);
477 }
478 break;
479 case AUDIT_UID:
480 result = audit_comparator(cred->uid, f->op, f->val);
481 break;
482 case AUDIT_EUID:
483 result = audit_comparator(cred->euid, f->op, f->val);
484 break;
485 case AUDIT_SUID:
486 result = audit_comparator(cred->suid, f->op, f->val);
487 break;
488 case AUDIT_FSUID:
489 result = audit_comparator(cred->fsuid, f->op, f->val);
490 break;
491 case AUDIT_GID:
492 result = audit_comparator(cred->gid, f->op, f->val);
493 break;
494 case AUDIT_EGID:
495 result = audit_comparator(cred->egid, f->op, f->val);
496 break;
497 case AUDIT_SGID:
498 result = audit_comparator(cred->sgid, f->op, f->val);
499 break;
500 case AUDIT_FSGID:
501 result = audit_comparator(cred->fsgid, f->op, f->val);
502 break;
503 case AUDIT_PERS:
504 result = audit_comparator(tsk->personality, f->op, f->val);
505 break;
506 case AUDIT_ARCH:
507 if (ctx)
508 result = audit_comparator(ctx->arch, f->op, f->val);
509 break;
510
511 case AUDIT_EXIT:
512 if (ctx && ctx->return_valid)
513 result = audit_comparator(ctx->return_code, f->op, f->val);
514 break;
515 case AUDIT_SUCCESS:
516 if (ctx && ctx->return_valid) {
517 if (f->val)
518 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
519 else
520 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
521 }
522 break;
523 case AUDIT_DEVMAJOR:
524 if (name)
525 result = audit_comparator(MAJOR(name->dev),
526 f->op, f->val);
527 else if (ctx) {
528 for (j = 0; j < ctx->name_count; j++) {
529 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
530 ++result;
531 break;
532 }
533 }
534 }
535 break;
536 case AUDIT_DEVMINOR:
537 if (name)
538 result = audit_comparator(MINOR(name->dev),
539 f->op, f->val);
540 else if (ctx) {
541 for (j = 0; j < ctx->name_count; j++) {
542 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
543 ++result;
544 break;
545 }
546 }
547 }
548 break;
549 case AUDIT_INODE:
550 if (name)
551 result = (name->ino == f->val);
552 else if (ctx) {
553 for (j = 0; j < ctx->name_count; j++) {
554 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
555 ++result;
556 break;
557 }
558 }
559 }
560 break;
561 case AUDIT_WATCH:
562 if (name)
563 result = audit_watch_compare(rule->watch, name->ino, name->dev);
564 break;
565 case AUDIT_DIR:
566 if (ctx)
567 result = match_tree_refs(ctx, rule->tree);
568 break;
569 case AUDIT_LOGINUID:
570 result = 0;
571 if (ctx)
572 result = audit_comparator(tsk->loginuid, f->op, f->val);
573 break;
574 case AUDIT_SUBJ_USER:
575 case AUDIT_SUBJ_ROLE:
576 case AUDIT_SUBJ_TYPE:
577 case AUDIT_SUBJ_SEN:
578 case AUDIT_SUBJ_CLR:
579 /* NOTE: this may return negative values indicating
580 a temporary error. We simply treat this as a
581 match for now to avoid losing information that
582 may be wanted. An error message will also be
583 logged upon error */
584 if (f->lsm_rule) {
585 if (need_sid) {
586 security_task_getsecid(tsk, &sid);
587 need_sid = 0;
588 }
589 result = security_audit_rule_match(sid, f->type,
590 f->op,
591 f->lsm_rule,
592 ctx);
593 }
594 break;
595 case AUDIT_OBJ_USER:
596 case AUDIT_OBJ_ROLE:
597 case AUDIT_OBJ_TYPE:
598 case AUDIT_OBJ_LEV_LOW:
599 case AUDIT_OBJ_LEV_HIGH:
600 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
601 also applies here */
602 if (f->lsm_rule) {
603 /* Find files that match */
604 if (name) {
605 result = security_audit_rule_match(
606 name->osid, f->type, f->op,
607 f->lsm_rule, ctx);
608 } else if (ctx) {
609 for (j = 0; j < ctx->name_count; j++) {
610 if (security_audit_rule_match(
611 ctx->names[j].osid,
612 f->type, f->op,
613 f->lsm_rule, ctx)) {
614 ++result;
615 break;
616 }
617 }
618 }
619 /* Find ipc objects that match */
620 if (!ctx || ctx->type != AUDIT_IPC)
621 break;
622 if (security_audit_rule_match(ctx->ipc.osid,
623 f->type, f->op,
624 f->lsm_rule, ctx))
625 ++result;
626 }
627 break;
628 case AUDIT_ARG0:
629 case AUDIT_ARG1:
630 case AUDIT_ARG2:
631 case AUDIT_ARG3:
632 if (ctx)
633 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
634 break;
635 case AUDIT_FILTERKEY:
636 /* ignore this field for filtering */
637 result = 1;
638 break;
639 case AUDIT_PERM:
640 result = audit_match_perm(ctx, f->val);
641 break;
642 case AUDIT_FILETYPE:
643 result = audit_match_filetype(ctx, f->val);
644 break;
645 }
646
647 if (!result)
648 return 0;
649 }
650
651 if (ctx) {
652 if (rule->prio <= ctx->prio)
653 return 0;
654 if (rule->filterkey) {
655 kfree(ctx->filterkey);
656 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
657 }
658 ctx->prio = rule->prio;
659 }
660 switch (rule->action) {
661 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
662 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
663 }
664 return 1;
665 }
666
667 /* At process creation time, we can determine if system-call auditing is
668 * completely disabled for this task. Since we only have the task
669 * structure at this point, we can only check uid and gid.
670 */
671 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
672 {
673 struct audit_entry *e;
674 enum audit_state state;
675
676 rcu_read_lock();
677 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
678 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
679 &state, true)) {
680 if (state == AUDIT_RECORD_CONTEXT)
681 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
682 rcu_read_unlock();
683 return state;
684 }
685 }
686 rcu_read_unlock();
687 return AUDIT_BUILD_CONTEXT;
688 }
689
690 /* At syscall entry and exit time, this filter is called if the
691 * audit_state is not low enough that auditing cannot take place, but is
692 * also not high enough that we already know we have to write an audit
693 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
694 */
695 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
696 struct audit_context *ctx,
697 struct list_head *list)
698 {
699 struct audit_entry *e;
700 enum audit_state state;
701
702 if (audit_pid && tsk->tgid == audit_pid)
703 return AUDIT_DISABLED;
704
705 rcu_read_lock();
706 if (!list_empty(list)) {
707 int word = AUDIT_WORD(ctx->major);
708 int bit = AUDIT_BIT(ctx->major);
709
710 list_for_each_entry_rcu(e, list, list) {
711 if ((e->rule.mask[word] & bit) == bit &&
712 audit_filter_rules(tsk, &e->rule, ctx, NULL,
713 &state, false)) {
714 rcu_read_unlock();
715 ctx->current_state = state;
716 return state;
717 }
718 }
719 }
720 rcu_read_unlock();
721 return AUDIT_BUILD_CONTEXT;
722 }
723
724 /* At syscall exit time, this filter is called if any audit_names[] have been
725 * collected during syscall processing. We only check rules in sublists at hash
726 * buckets applicable to the inode numbers in audit_names[].
727 * Regarding audit_state, same rules apply as for audit_filter_syscall().
728 */
729 void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
730 {
731 int i;
732 struct audit_entry *e;
733 enum audit_state state;
734
735 if (audit_pid && tsk->tgid == audit_pid)
736 return;
737
738 rcu_read_lock();
739 for (i = 0; i < ctx->name_count; i++) {
740 int word = AUDIT_WORD(ctx->major);
741 int bit = AUDIT_BIT(ctx->major);
742 struct audit_names *n = &ctx->names[i];
743 int h = audit_hash_ino((u32)n->ino);
744 struct list_head *list = &audit_inode_hash[h];
745
746 if (list_empty(list))
747 continue;
748
749 list_for_each_entry_rcu(e, list, list) {
750 if ((e->rule.mask[word] & bit) == bit &&
751 audit_filter_rules(tsk, &e->rule, ctx, n,
752 &state, false)) {
753 rcu_read_unlock();
754 ctx->current_state = state;
755 return;
756 }
757 }
758 }
759 rcu_read_unlock();
760 }
761
762 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
763 int return_valid,
764 long return_code)
765 {
766 struct audit_context *context = tsk->audit_context;
767
768 if (likely(!context))
769 return NULL;
770 context->return_valid = return_valid;
771
772 /*
773 * we need to fix up the return code in the audit logs if the actual
774 * return codes are later going to be fixed up by the arch specific
775 * signal handlers
776 *
777 * This is actually a test for:
778 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
779 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
780 *
781 * but is faster than a bunch of ||
782 */
783 if (unlikely(return_code <= -ERESTARTSYS) &&
784 (return_code >= -ERESTART_RESTARTBLOCK) &&
785 (return_code != -ENOIOCTLCMD))
786 context->return_code = -EINTR;
787 else
788 context->return_code = return_code;
789
790 if (context->in_syscall && !context->dummy) {
791 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
792 audit_filter_inodes(tsk, context);
793 }
794
795 tsk->audit_context = NULL;
796 return context;
797 }
798
799 static inline void audit_free_names(struct audit_context *context)
800 {
801 int i;
802
803 #if AUDIT_DEBUG == 2
804 if (context->put_count + context->ino_count != context->name_count) {
805 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
806 " name_count=%d put_count=%d"
807 " ino_count=%d [NOT freeing]\n",
808 __FILE__, __LINE__,
809 context->serial, context->major, context->in_syscall,
810 context->name_count, context->put_count,
811 context->ino_count);
812 for (i = 0; i < context->name_count; i++) {
813 printk(KERN_ERR "names[%d] = %p = %s\n", i,
814 context->names[i].name,
815 context->names[i].name ?: "(null)");
816 }
817 dump_stack();
818 return;
819 }
820 #endif
821 #if AUDIT_DEBUG
822 context->put_count = 0;
823 context->ino_count = 0;
824 #endif
825
826 for (i = 0; i < context->name_count; i++) {
827 if (context->names[i].name && context->names[i].name_put)
828 __putname(context->names[i].name);
829 }
830 context->name_count = 0;
831 path_put(&context->pwd);
832 context->pwd.dentry = NULL;
833 context->pwd.mnt = NULL;
834 }
835
836 static inline void audit_free_aux(struct audit_context *context)
837 {
838 struct audit_aux_data *aux;
839
840 while ((aux = context->aux)) {
841 context->aux = aux->next;
842 kfree(aux);
843 }
844 while ((aux = context->aux_pids)) {
845 context->aux_pids = aux->next;
846 kfree(aux);
847 }
848 }
849
850 static inline void audit_zero_context(struct audit_context *context,
851 enum audit_state state)
852 {
853 memset(context, 0, sizeof(*context));
854 context->state = state;
855 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
856 }
857
858 static inline struct audit_context *audit_alloc_context(enum audit_state state)
859 {
860 struct audit_context *context;
861
862 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
863 return NULL;
864 audit_zero_context(context, state);
865 INIT_LIST_HEAD(&context->killed_trees);
866 return context;
867 }
868
869 /**
870 * audit_alloc - allocate an audit context block for a task
871 * @tsk: task
872 *
873 * Filter on the task information and allocate a per-task audit context
874 * if necessary. Doing so turns on system call auditing for the
875 * specified task. This is called from copy_process, so no lock is
876 * needed.
877 */
878 int audit_alloc(struct task_struct *tsk)
879 {
880 struct audit_context *context;
881 enum audit_state state;
882 char *key = NULL;
883
884 if (likely(!audit_ever_enabled))
885 return 0; /* Return if not auditing. */
886
887 state = audit_filter_task(tsk, &key);
888 if (likely(state == AUDIT_DISABLED))
889 return 0;
890
891 if (!(context = audit_alloc_context(state))) {
892 kfree(key);
893 audit_log_lost("out of memory in audit_alloc");
894 return -ENOMEM;
895 }
896 context->filterkey = key;
897
898 tsk->audit_context = context;
899 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
900 return 0;
901 }
902
903 static inline void audit_free_context(struct audit_context *context)
904 {
905 struct audit_context *previous;
906 int count = 0;
907
908 do {
909 previous = context->previous;
910 if (previous || (count && count < 10)) {
911 ++count;
912 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
913 " freeing multiple contexts (%d)\n",
914 context->serial, context->major,
915 context->name_count, count);
916 }
917 audit_free_names(context);
918 unroll_tree_refs(context, NULL, 0);
919 free_tree_refs(context);
920 audit_free_aux(context);
921 kfree(context->filterkey);
922 kfree(context->sockaddr);
923 kfree(context);
924 context = previous;
925 } while (context);
926 if (count >= 10)
927 printk(KERN_ERR "audit: freed %d contexts\n", count);
928 }
929
930 void audit_log_task_context(struct audit_buffer *ab)
931 {
932 char *ctx = NULL;
933 unsigned len;
934 int error;
935 u32 sid;
936
937 security_task_getsecid(current, &sid);
938 if (!sid)
939 return;
940
941 error = security_secid_to_secctx(sid, &ctx, &len);
942 if (error) {
943 if (error != -EINVAL)
944 goto error_path;
945 return;
946 }
947
948 audit_log_format(ab, " subj=%s", ctx);
949 security_release_secctx(ctx, len);
950 return;
951
952 error_path:
953 audit_panic("error in audit_log_task_context");
954 return;
955 }
956
957 EXPORT_SYMBOL(audit_log_task_context);
958
959 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
960 {
961 char name[sizeof(tsk->comm)];
962 struct mm_struct *mm = tsk->mm;
963 struct vm_area_struct *vma;
964
965 /* tsk == current */
966
967 get_task_comm(name, tsk);
968 audit_log_format(ab, " comm=");
969 audit_log_untrustedstring(ab, name);
970
971 if (mm) {
972 down_read(&mm->mmap_sem);
973 vma = mm->mmap;
974 while (vma) {
975 if ((vma->vm_flags & VM_EXECUTABLE) &&
976 vma->vm_file) {
977 audit_log_d_path(ab, "exe=",
978 &vma->vm_file->f_path);
979 break;
980 }
981 vma = vma->vm_next;
982 }
983 up_read(&mm->mmap_sem);
984 }
985 audit_log_task_context(ab);
986 }
987
988 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
989 uid_t auid, uid_t uid, unsigned int sessionid,
990 u32 sid, char *comm)
991 {
992 struct audit_buffer *ab;
993 char *ctx = NULL;
994 u32 len;
995 int rc = 0;
996
997 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
998 if (!ab)
999 return rc;
1000
1001 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
1002 uid, sessionid);
1003 if (security_secid_to_secctx(sid, &ctx, &len)) {
1004 audit_log_format(ab, " obj=(none)");
1005 rc = 1;
1006 } else {
1007 audit_log_format(ab, " obj=%s", ctx);
1008 security_release_secctx(ctx, len);
1009 }
1010 audit_log_format(ab, " ocomm=");
1011 audit_log_untrustedstring(ab, comm);
1012 audit_log_end(ab);
1013
1014 return rc;
1015 }
1016
1017 /*
1018 * to_send and len_sent accounting are very loose estimates. We aren't
1019 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1020 * within about 500 bytes (next page boundary)
1021 *
1022 * why snprintf? an int is up to 12 digits long. if we just assumed when
1023 * logging that a[%d]= was going to be 16 characters long we would be wasting
1024 * space in every audit message. In one 7500 byte message we can log up to
1025 * about 1000 min size arguments. That comes down to about 50% waste of space
1026 * if we didn't do the snprintf to find out how long arg_num_len was.
1027 */
1028 static int audit_log_single_execve_arg(struct audit_context *context,
1029 struct audit_buffer **ab,
1030 int arg_num,
1031 size_t *len_sent,
1032 const char __user *p,
1033 char *buf)
1034 {
1035 char arg_num_len_buf[12];
1036 const char __user *tmp_p = p;
1037 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1038 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
1039 size_t len, len_left, to_send;
1040 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1041 unsigned int i, has_cntl = 0, too_long = 0;
1042 int ret;
1043
1044 /* strnlen_user includes the null we don't want to send */
1045 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1046
1047 /*
1048 * We just created this mm, if we can't find the strings
1049 * we just copied into it something is _very_ wrong. Similar
1050 * for strings that are too long, we should not have created
1051 * any.
1052 */
1053 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
1054 WARN_ON(1);
1055 send_sig(SIGKILL, current, 0);
1056 return -1;
1057 }
1058
1059 /* walk the whole argument looking for non-ascii chars */
1060 do {
1061 if (len_left > MAX_EXECVE_AUDIT_LEN)
1062 to_send = MAX_EXECVE_AUDIT_LEN;
1063 else
1064 to_send = len_left;
1065 ret = copy_from_user(buf, tmp_p, to_send);
1066 /*
1067 * There is no reason for this copy to be short. We just
1068 * copied them here, and the mm hasn't been exposed to user-
1069 * space yet.
1070 */
1071 if (ret) {
1072 WARN_ON(1);
1073 send_sig(SIGKILL, current, 0);
1074 return -1;
1075 }
1076 buf[to_send] = '\0';
1077 has_cntl = audit_string_contains_control(buf, to_send);
1078 if (has_cntl) {
1079 /*
1080 * hex messages get logged as 2 bytes, so we can only
1081 * send half as much in each message
1082 */
1083 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1084 break;
1085 }
1086 len_left -= to_send;
1087 tmp_p += to_send;
1088 } while (len_left > 0);
1089
1090 len_left = len;
1091
1092 if (len > max_execve_audit_len)
1093 too_long = 1;
1094
1095 /* rewalk the argument actually logging the message */
1096 for (i = 0; len_left > 0; i++) {
1097 int room_left;
1098
1099 if (len_left > max_execve_audit_len)
1100 to_send = max_execve_audit_len;
1101 else
1102 to_send = len_left;
1103
1104 /* do we have space left to send this argument in this ab? */
1105 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1106 if (has_cntl)
1107 room_left -= (to_send * 2);
1108 else
1109 room_left -= to_send;
1110 if (room_left < 0) {
1111 *len_sent = 0;
1112 audit_log_end(*ab);
1113 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1114 if (!*ab)
1115 return 0;
1116 }
1117
1118 /*
1119 * first record needs to say how long the original string was
1120 * so we can be sure nothing was lost.
1121 */
1122 if ((i == 0) && (too_long))
1123 audit_log_format(*ab, " a%d_len=%zu", arg_num,
1124 has_cntl ? 2*len : len);
1125
1126 /*
1127 * normally arguments are small enough to fit and we already
1128 * filled buf above when we checked for control characters
1129 * so don't bother with another copy_from_user
1130 */
1131 if (len >= max_execve_audit_len)
1132 ret = copy_from_user(buf, p, to_send);
1133 else
1134 ret = 0;
1135 if (ret) {
1136 WARN_ON(1);
1137 send_sig(SIGKILL, current, 0);
1138 return -1;
1139 }
1140 buf[to_send] = '\0';
1141
1142 /* actually log it */
1143 audit_log_format(*ab, " a%d", arg_num);
1144 if (too_long)
1145 audit_log_format(*ab, "[%d]", i);
1146 audit_log_format(*ab, "=");
1147 if (has_cntl)
1148 audit_log_n_hex(*ab, buf, to_send);
1149 else
1150 audit_log_string(*ab, buf);
1151
1152 p += to_send;
1153 len_left -= to_send;
1154 *len_sent += arg_num_len;
1155 if (has_cntl)
1156 *len_sent += to_send * 2;
1157 else
1158 *len_sent += to_send;
1159 }
1160 /* include the null we didn't log */
1161 return len + 1;
1162 }
1163
1164 static void audit_log_execve_info(struct audit_context *context,
1165 struct audit_buffer **ab,
1166 struct audit_aux_data_execve *axi)
1167 {
1168 int i;
1169 size_t len, len_sent = 0;
1170 const char __user *p;
1171 char *buf;
1172
1173 if (axi->mm != current->mm)
1174 return; /* execve failed, no additional info */
1175
1176 p = (const char __user *)axi->mm->arg_start;
1177
1178 audit_log_format(*ab, "argc=%d", axi->argc);
1179
1180 /*
1181 * we need some kernel buffer to hold the userspace args. Just
1182 * allocate one big one rather than allocating one of the right size
1183 * for every single argument inside audit_log_single_execve_arg()
1184 * should be <8k allocation so should be pretty safe.
1185 */
1186 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1187 if (!buf) {
1188 audit_panic("out of memory for argv string\n");
1189 return;
1190 }
1191
1192 for (i = 0; i < axi->argc; i++) {
1193 len = audit_log_single_execve_arg(context, ab, i,
1194 &len_sent, p, buf);
1195 if (len <= 0)
1196 break;
1197 p += len;
1198 }
1199 kfree(buf);
1200 }
1201
1202 static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1203 {
1204 int i;
1205
1206 audit_log_format(ab, " %s=", prefix);
1207 CAP_FOR_EACH_U32(i) {
1208 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1209 }
1210 }
1211
1212 static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1213 {
1214 kernel_cap_t *perm = &name->fcap.permitted;
1215 kernel_cap_t *inh = &name->fcap.inheritable;
1216 int log = 0;
1217
1218 if (!cap_isclear(*perm)) {
1219 audit_log_cap(ab, "cap_fp", perm);
1220 log = 1;
1221 }
1222 if (!cap_isclear(*inh)) {
1223 audit_log_cap(ab, "cap_fi", inh);
1224 log = 1;
1225 }
1226
1227 if (log)
1228 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1229 }
1230
1231 static void show_special(struct audit_context *context, int *call_panic)
1232 {
1233 struct audit_buffer *ab;
1234 int i;
1235
1236 ab = audit_log_start(context, GFP_KERNEL, context->type);
1237 if (!ab)
1238 return;
1239
1240 switch (context->type) {
1241 case AUDIT_SOCKETCALL: {
1242 int nargs = context->socketcall.nargs;
1243 audit_log_format(ab, "nargs=%d", nargs);
1244 for (i = 0; i < nargs; i++)
1245 audit_log_format(ab, " a%d=%lx", i,
1246 context->socketcall.args[i]);
1247 break; }
1248 case AUDIT_IPC: {
1249 u32 osid = context->ipc.osid;
1250
1251 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
1252 context->ipc.uid, context->ipc.gid, context->ipc.mode);
1253 if (osid) {
1254 char *ctx = NULL;
1255 u32 len;
1256 if (security_secid_to_secctx(osid, &ctx, &len)) {
1257 audit_log_format(ab, " osid=%u", osid);
1258 *call_panic = 1;
1259 } else {
1260 audit_log_format(ab, " obj=%s", ctx);
1261 security_release_secctx(ctx, len);
1262 }
1263 }
1264 if (context->ipc.has_perm) {
1265 audit_log_end(ab);
1266 ab = audit_log_start(context, GFP_KERNEL,
1267 AUDIT_IPC_SET_PERM);
1268 audit_log_format(ab,
1269 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1270 context->ipc.qbytes,
1271 context->ipc.perm_uid,
1272 context->ipc.perm_gid,
1273 context->ipc.perm_mode);
1274 if (!ab)
1275 return;
1276 }
1277 break; }
1278 case AUDIT_MQ_OPEN: {
1279 audit_log_format(ab,
1280 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1281 "mq_msgsize=%ld mq_curmsgs=%ld",
1282 context->mq_open.oflag, context->mq_open.mode,
1283 context->mq_open.attr.mq_flags,
1284 context->mq_open.attr.mq_maxmsg,
1285 context->mq_open.attr.mq_msgsize,
1286 context->mq_open.attr.mq_curmsgs);
1287 break; }
1288 case AUDIT_MQ_SENDRECV: {
1289 audit_log_format(ab,
1290 "mqdes=%d msg_len=%zd msg_prio=%u "
1291 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1292 context->mq_sendrecv.mqdes,
1293 context->mq_sendrecv.msg_len,
1294 context->mq_sendrecv.msg_prio,
1295 context->mq_sendrecv.abs_timeout.tv_sec,
1296 context->mq_sendrecv.abs_timeout.tv_nsec);
1297 break; }
1298 case AUDIT_MQ_NOTIFY: {
1299 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1300 context->mq_notify.mqdes,
1301 context->mq_notify.sigev_signo);
1302 break; }
1303 case AUDIT_MQ_GETSETATTR: {
1304 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1305 audit_log_format(ab,
1306 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1307 "mq_curmsgs=%ld ",
1308 context->mq_getsetattr.mqdes,
1309 attr->mq_flags, attr->mq_maxmsg,
1310 attr->mq_msgsize, attr->mq_curmsgs);
1311 break; }
1312 case AUDIT_CAPSET: {
1313 audit_log_format(ab, "pid=%d", context->capset.pid);
1314 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1315 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1316 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1317 break; }
1318 case AUDIT_MMAP: {
1319 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1320 context->mmap.flags);
1321 break; }
1322 }
1323 audit_log_end(ab);
1324 }
1325
1326 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1327 {
1328 const struct cred *cred;
1329 int i, call_panic = 0;
1330 struct audit_buffer *ab;
1331 struct audit_aux_data *aux;
1332 const char *tty;
1333
1334 /* tsk == current */
1335 context->pid = tsk->pid;
1336 if (!context->ppid)
1337 context->ppid = sys_getppid();
1338 cred = current_cred();
1339 context->uid = cred->uid;
1340 context->gid = cred->gid;
1341 context->euid = cred->euid;
1342 context->suid = cred->suid;
1343 context->fsuid = cred->fsuid;
1344 context->egid = cred->egid;
1345 context->sgid = cred->sgid;
1346 context->fsgid = cred->fsgid;
1347 context->personality = tsk->personality;
1348
1349 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1350 if (!ab)
1351 return; /* audit_panic has been called */
1352 audit_log_format(ab, "arch=%x syscall=%d",
1353 context->arch, context->major);
1354 if (context->personality != PER_LINUX)
1355 audit_log_format(ab, " per=%lx", context->personality);
1356 if (context->return_valid)
1357 audit_log_format(ab, " success=%s exit=%ld",
1358 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1359 context->return_code);
1360
1361 spin_lock_irq(&tsk->sighand->siglock);
1362 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1363 tty = tsk->signal->tty->name;
1364 else
1365 tty = "(none)";
1366 spin_unlock_irq(&tsk->sighand->siglock);
1367
1368 audit_log_format(ab,
1369 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1370 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1371 " euid=%u suid=%u fsuid=%u"
1372 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1373 context->argv[0],
1374 context->argv[1],
1375 context->argv[2],
1376 context->argv[3],
1377 context->name_count,
1378 context->ppid,
1379 context->pid,
1380 tsk->loginuid,
1381 context->uid,
1382 context->gid,
1383 context->euid, context->suid, context->fsuid,
1384 context->egid, context->sgid, context->fsgid, tty,
1385 tsk->sessionid);
1386
1387
1388 audit_log_task_info(ab, tsk);
1389 audit_log_key(ab, context->filterkey);
1390 audit_log_end(ab);
1391
1392 for (aux = context->aux; aux; aux = aux->next) {
1393
1394 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1395 if (!ab)
1396 continue; /* audit_panic has been called */
1397
1398 switch (aux->type) {
1399
1400 case AUDIT_EXECVE: {
1401 struct audit_aux_data_execve *axi = (void *)aux;
1402 audit_log_execve_info(context, &ab, axi);
1403 break; }
1404
1405 case AUDIT_BPRM_FCAPS: {
1406 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1407 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1408 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1409 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1410 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1411 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1412 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1413 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1414 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1415 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1416 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1417 break; }
1418
1419 }
1420 audit_log_end(ab);
1421 }
1422
1423 if (context->type)
1424 show_special(context, &call_panic);
1425
1426 if (context->fds[0] >= 0) {
1427 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1428 if (ab) {
1429 audit_log_format(ab, "fd0=%d fd1=%d",
1430 context->fds[0], context->fds[1]);
1431 audit_log_end(ab);
1432 }
1433 }
1434
1435 if (context->sockaddr_len) {
1436 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1437 if (ab) {
1438 audit_log_format(ab, "saddr=");
1439 audit_log_n_hex(ab, (void *)context->sockaddr,
1440 context->sockaddr_len);
1441 audit_log_end(ab);
1442 }
1443 }
1444
1445 for (aux = context->aux_pids; aux; aux = aux->next) {
1446 struct audit_aux_data_pids *axs = (void *)aux;
1447
1448 for (i = 0; i < axs->pid_count; i++)
1449 if (audit_log_pid_context(context, axs->target_pid[i],
1450 axs->target_auid[i],
1451 axs->target_uid[i],
1452 axs->target_sessionid[i],
1453 axs->target_sid[i],
1454 axs->target_comm[i]))
1455 call_panic = 1;
1456 }
1457
1458 if (context->target_pid &&
1459 audit_log_pid_context(context, context->target_pid,
1460 context->target_auid, context->target_uid,
1461 context->target_sessionid,
1462 context->target_sid, context->target_comm))
1463 call_panic = 1;
1464
1465 if (context->pwd.dentry && context->pwd.mnt) {
1466 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1467 if (ab) {
1468 audit_log_d_path(ab, "cwd=", &context->pwd);
1469 audit_log_end(ab);
1470 }
1471 }
1472 for (i = 0; i < context->name_count; i++) {
1473 struct audit_names *n = &context->names[i];
1474
1475 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1476 if (!ab)
1477 continue; /* audit_panic has been called */
1478
1479 audit_log_format(ab, "item=%d", i);
1480
1481 if (n->name) {
1482 switch(n->name_len) {
1483 case AUDIT_NAME_FULL:
1484 /* log the full path */
1485 audit_log_format(ab, " name=");
1486 audit_log_untrustedstring(ab, n->name);
1487 break;
1488 case 0:
1489 /* name was specified as a relative path and the
1490 * directory component is the cwd */
1491 audit_log_d_path(ab, "name=", &context->pwd);
1492 break;
1493 default:
1494 /* log the name's directory component */
1495 audit_log_format(ab, " name=");
1496 audit_log_n_untrustedstring(ab, n->name,
1497 n->name_len);
1498 }
1499 } else
1500 audit_log_format(ab, " name=(null)");
1501
1502 if (n->ino != (unsigned long)-1) {
1503 audit_log_format(ab, " inode=%lu"
1504 " dev=%02x:%02x mode=%#ho"
1505 " ouid=%u ogid=%u rdev=%02x:%02x",
1506 n->ino,
1507 MAJOR(n->dev),
1508 MINOR(n->dev),
1509 n->mode,
1510 n->uid,
1511 n->gid,
1512 MAJOR(n->rdev),
1513 MINOR(n->rdev));
1514 }
1515 if (n->osid != 0) {
1516 char *ctx = NULL;
1517 u32 len;
1518 if (security_secid_to_secctx(
1519 n->osid, &ctx, &len)) {
1520 audit_log_format(ab, " osid=%u", n->osid);
1521 call_panic = 2;
1522 } else {
1523 audit_log_format(ab, " obj=%s", ctx);
1524 security_release_secctx(ctx, len);
1525 }
1526 }
1527
1528 audit_log_fcaps(ab, n);
1529
1530 audit_log_end(ab);
1531 }
1532
1533 /* Send end of event record to help user space know we are finished */
1534 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1535 if (ab)
1536 audit_log_end(ab);
1537 if (call_panic)
1538 audit_panic("error converting sid to string");
1539 }
1540
1541 /**
1542 * audit_free - free a per-task audit context
1543 * @tsk: task whose audit context block to free
1544 *
1545 * Called from copy_process and do_exit
1546 */
1547 void audit_free(struct task_struct *tsk)
1548 {
1549 struct audit_context *context;
1550
1551 context = audit_get_context(tsk, 0, 0);
1552 if (likely(!context))
1553 return;
1554
1555 /* Check for system calls that do not go through the exit
1556 * function (e.g., exit_group), then free context block.
1557 * We use GFP_ATOMIC here because we might be doing this
1558 * in the context of the idle thread */
1559 /* that can happen only if we are called from do_exit() */
1560 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1561 audit_log_exit(context, tsk);
1562 if (!list_empty(&context->killed_trees))
1563 audit_kill_trees(&context->killed_trees);
1564
1565 audit_free_context(context);
1566 }
1567
1568 /**
1569 * audit_syscall_entry - fill in an audit record at syscall entry
1570 * @arch: architecture type
1571 * @major: major syscall type (function)
1572 * @a1: additional syscall register 1
1573 * @a2: additional syscall register 2
1574 * @a3: additional syscall register 3
1575 * @a4: additional syscall register 4
1576 *
1577 * Fill in audit context at syscall entry. This only happens if the
1578 * audit context was created when the task was created and the state or
1579 * filters demand the audit context be built. If the state from the
1580 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1581 * then the record will be written at syscall exit time (otherwise, it
1582 * will only be written if another part of the kernel requests that it
1583 * be written).
1584 */
1585 void audit_syscall_entry(int arch, int major,
1586 unsigned long a1, unsigned long a2,
1587 unsigned long a3, unsigned long a4)
1588 {
1589 struct task_struct *tsk = current;
1590 struct audit_context *context = tsk->audit_context;
1591 enum audit_state state;
1592
1593 if (unlikely(!context))
1594 return;
1595
1596 /*
1597 * This happens only on certain architectures that make system
1598 * calls in kernel_thread via the entry.S interface, instead of
1599 * with direct calls. (If you are porting to a new
1600 * architecture, hitting this condition can indicate that you
1601 * got the _exit/_leave calls backward in entry.S.)
1602 *
1603 * i386 no
1604 * x86_64 no
1605 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1606 *
1607 * This also happens with vm86 emulation in a non-nested manner
1608 * (entries without exits), so this case must be caught.
1609 */
1610 if (context->in_syscall) {
1611 struct audit_context *newctx;
1612
1613 #if AUDIT_DEBUG
1614 printk(KERN_ERR
1615 "audit(:%d) pid=%d in syscall=%d;"
1616 " entering syscall=%d\n",
1617 context->serial, tsk->pid, context->major, major);
1618 #endif
1619 newctx = audit_alloc_context(context->state);
1620 if (newctx) {
1621 newctx->previous = context;
1622 context = newctx;
1623 tsk->audit_context = newctx;
1624 } else {
1625 /* If we can't alloc a new context, the best we
1626 * can do is to leak memory (any pending putname
1627 * will be lost). The only other alternative is
1628 * to abandon auditing. */
1629 audit_zero_context(context, context->state);
1630 }
1631 }
1632 BUG_ON(context->in_syscall || context->name_count);
1633
1634 if (!audit_enabled)
1635 return;
1636
1637 context->arch = arch;
1638 context->major = major;
1639 context->argv[0] = a1;
1640 context->argv[1] = a2;
1641 context->argv[2] = a3;
1642 context->argv[3] = a4;
1643
1644 state = context->state;
1645 context->dummy = !audit_n_rules;
1646 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1647 context->prio = 0;
1648 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1649 }
1650 if (likely(state == AUDIT_DISABLED))
1651 return;
1652
1653 context->serial = 0;
1654 context->ctime = CURRENT_TIME;
1655 context->in_syscall = 1;
1656 context->current_state = state;
1657 context->ppid = 0;
1658 }
1659
1660 void audit_finish_fork(struct task_struct *child)
1661 {
1662 struct audit_context *ctx = current->audit_context;
1663 struct audit_context *p = child->audit_context;
1664 if (!p || !ctx)
1665 return;
1666 if (!ctx->in_syscall || ctx->current_state != AUDIT_RECORD_CONTEXT)
1667 return;
1668 p->arch = ctx->arch;
1669 p->major = ctx->major;
1670 memcpy(p->argv, ctx->argv, sizeof(ctx->argv));
1671 p->ctime = ctx->ctime;
1672 p->dummy = ctx->dummy;
1673 p->in_syscall = ctx->in_syscall;
1674 p->filterkey = kstrdup(ctx->filterkey, GFP_KERNEL);
1675 p->ppid = current->pid;
1676 p->prio = ctx->prio;
1677 p->current_state = ctx->current_state;
1678 }
1679
1680 /**
1681 * audit_syscall_exit - deallocate audit context after a system call
1682 * @valid: success/failure flag
1683 * @return_code: syscall return value
1684 *
1685 * Tear down after system call. If the audit context has been marked as
1686 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1687 * filtering, or because some other part of the kernel write an audit
1688 * message), then write out the syscall information. In call cases,
1689 * free the names stored from getname().
1690 */
1691 void audit_syscall_exit(int valid, long return_code)
1692 {
1693 struct task_struct *tsk = current;
1694 struct audit_context *context;
1695
1696 context = audit_get_context(tsk, valid, return_code);
1697
1698 if (likely(!context))
1699 return;
1700
1701 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1702 audit_log_exit(context, tsk);
1703
1704 context->in_syscall = 0;
1705 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
1706
1707 if (!list_empty(&context->killed_trees))
1708 audit_kill_trees(&context->killed_trees);
1709
1710 if (context->previous) {
1711 struct audit_context *new_context = context->previous;
1712 context->previous = NULL;
1713 audit_free_context(context);
1714 tsk->audit_context = new_context;
1715 } else {
1716 audit_free_names(context);
1717 unroll_tree_refs(context, NULL, 0);
1718 audit_free_aux(context);
1719 context->aux = NULL;
1720 context->aux_pids = NULL;
1721 context->target_pid = 0;
1722 context->target_sid = 0;
1723 context->sockaddr_len = 0;
1724 context->type = 0;
1725 context->fds[0] = -1;
1726 if (context->state != AUDIT_RECORD_CONTEXT) {
1727 kfree(context->filterkey);
1728 context->filterkey = NULL;
1729 }
1730 tsk->audit_context = context;
1731 }
1732 }
1733
1734 static inline void handle_one(const struct inode *inode)
1735 {
1736 #ifdef CONFIG_AUDIT_TREE
1737 struct audit_context *context;
1738 struct audit_tree_refs *p;
1739 struct audit_chunk *chunk;
1740 int count;
1741 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
1742 return;
1743 context = current->audit_context;
1744 p = context->trees;
1745 count = context->tree_count;
1746 rcu_read_lock();
1747 chunk = audit_tree_lookup(inode);
1748 rcu_read_unlock();
1749 if (!chunk)
1750 return;
1751 if (likely(put_tree_ref(context, chunk)))
1752 return;
1753 if (unlikely(!grow_tree_refs(context))) {
1754 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
1755 audit_set_auditable(context);
1756 audit_put_chunk(chunk);
1757 unroll_tree_refs(context, p, count);
1758 return;
1759 }
1760 put_tree_ref(context, chunk);
1761 #endif
1762 }
1763
1764 static void handle_path(const struct dentry *dentry)
1765 {
1766 #ifdef CONFIG_AUDIT_TREE
1767 struct audit_context *context;
1768 struct audit_tree_refs *p;
1769 const struct dentry *d, *parent;
1770 struct audit_chunk *drop;
1771 unsigned long seq;
1772 int count;
1773
1774 context = current->audit_context;
1775 p = context->trees;
1776 count = context->tree_count;
1777 retry:
1778 drop = NULL;
1779 d = dentry;
1780 rcu_read_lock();
1781 seq = read_seqbegin(&rename_lock);
1782 for(;;) {
1783 struct inode *inode = d->d_inode;
1784 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
1785 struct audit_chunk *chunk;
1786 chunk = audit_tree_lookup(inode);
1787 if (chunk) {
1788 if (unlikely(!put_tree_ref(context, chunk))) {
1789 drop = chunk;
1790 break;
1791 }
1792 }
1793 }
1794 parent = d->d_parent;
1795 if (parent == d)
1796 break;
1797 d = parent;
1798 }
1799 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1800 rcu_read_unlock();
1801 if (!drop) {
1802 /* just a race with rename */
1803 unroll_tree_refs(context, p, count);
1804 goto retry;
1805 }
1806 audit_put_chunk(drop);
1807 if (grow_tree_refs(context)) {
1808 /* OK, got more space */
1809 unroll_tree_refs(context, p, count);
1810 goto retry;
1811 }
1812 /* too bad */
1813 printk(KERN_WARNING
1814 "out of memory, audit has lost a tree reference\n");
1815 unroll_tree_refs(context, p, count);
1816 audit_set_auditable(context);
1817 return;
1818 }
1819 rcu_read_unlock();
1820 #endif
1821 }
1822
1823 /**
1824 * audit_getname - add a name to the list
1825 * @name: name to add
1826 *
1827 * Add a name to the list of audit names for this context.
1828 * Called from fs/namei.c:getname().
1829 */
1830 void __audit_getname(const char *name)
1831 {
1832 struct audit_context *context = current->audit_context;
1833
1834 if (IS_ERR(name) || !name)
1835 return;
1836
1837 if (!context->in_syscall) {
1838 #if AUDIT_DEBUG == 2
1839 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1840 __FILE__, __LINE__, context->serial, name);
1841 dump_stack();
1842 #endif
1843 return;
1844 }
1845 BUG_ON(context->name_count >= AUDIT_NAMES);
1846 context->names[context->name_count].name = name;
1847 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1848 context->names[context->name_count].name_put = 1;
1849 context->names[context->name_count].ino = (unsigned long)-1;
1850 context->names[context->name_count].osid = 0;
1851 ++context->name_count;
1852 if (!context->pwd.dentry)
1853 get_fs_pwd(current->fs, &context->pwd);
1854 }
1855
1856 /* audit_putname - intercept a putname request
1857 * @name: name to intercept and delay for putname
1858 *
1859 * If we have stored the name from getname in the audit context,
1860 * then we delay the putname until syscall exit.
1861 * Called from include/linux/fs.h:putname().
1862 */
1863 void audit_putname(const char *name)
1864 {
1865 struct audit_context *context = current->audit_context;
1866
1867 BUG_ON(!context);
1868 if (!context->in_syscall) {
1869 #if AUDIT_DEBUG == 2
1870 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1871 __FILE__, __LINE__, context->serial, name);
1872 if (context->name_count) {
1873 int i;
1874 for (i = 0; i < context->name_count; i++)
1875 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1876 context->names[i].name,
1877 context->names[i].name ?: "(null)");
1878 }
1879 #endif
1880 __putname(name);
1881 }
1882 #if AUDIT_DEBUG
1883 else {
1884 ++context->put_count;
1885 if (context->put_count > context->name_count) {
1886 printk(KERN_ERR "%s:%d(:%d): major=%d"
1887 " in_syscall=%d putname(%p) name_count=%d"
1888 " put_count=%d\n",
1889 __FILE__, __LINE__,
1890 context->serial, context->major,
1891 context->in_syscall, name, context->name_count,
1892 context->put_count);
1893 dump_stack();
1894 }
1895 }
1896 #endif
1897 }
1898
1899 static int audit_inc_name_count(struct audit_context *context,
1900 const struct inode *inode)
1901 {
1902 if (context->name_count >= AUDIT_NAMES) {
1903 if (inode)
1904 printk(KERN_DEBUG "audit: name_count maxed, losing inode data: "
1905 "dev=%02x:%02x, inode=%lu\n",
1906 MAJOR(inode->i_sb->s_dev),
1907 MINOR(inode->i_sb->s_dev),
1908 inode->i_ino);
1909
1910 else
1911 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
1912 return 1;
1913 }
1914 context->name_count++;
1915 #if AUDIT_DEBUG
1916 context->ino_count++;
1917 #endif
1918 return 0;
1919 }
1920
1921
1922 static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1923 {
1924 struct cpu_vfs_cap_data caps;
1925 int rc;
1926
1927 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1928 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1929 name->fcap.fE = 0;
1930 name->fcap_ver = 0;
1931
1932 if (!dentry)
1933 return 0;
1934
1935 rc = get_vfs_caps_from_disk(dentry, &caps);
1936 if (rc)
1937 return rc;
1938
1939 name->fcap.permitted = caps.permitted;
1940 name->fcap.inheritable = caps.inheritable;
1941 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1942 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1943
1944 return 0;
1945 }
1946
1947
1948 /* Copy inode data into an audit_names. */
1949 static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1950 const struct inode *inode)
1951 {
1952 name->ino = inode->i_ino;
1953 name->dev = inode->i_sb->s_dev;
1954 name->mode = inode->i_mode;
1955 name->uid = inode->i_uid;
1956 name->gid = inode->i_gid;
1957 name->rdev = inode->i_rdev;
1958 security_inode_getsecid(inode, &name->osid);
1959 audit_copy_fcaps(name, dentry);
1960 }
1961
1962 /**
1963 * audit_inode - store the inode and device from a lookup
1964 * @name: name being audited
1965 * @dentry: dentry being audited
1966 *
1967 * Called from fs/namei.c:path_lookup().
1968 */
1969 void __audit_inode(const char *name, const struct dentry *dentry)
1970 {
1971 int idx;
1972 struct audit_context *context = current->audit_context;
1973 const struct inode *inode = dentry->d_inode;
1974
1975 if (!context->in_syscall)
1976 return;
1977 if (context->name_count
1978 && context->names[context->name_count-1].name
1979 && context->names[context->name_count-1].name == name)
1980 idx = context->name_count - 1;
1981 else if (context->name_count > 1
1982 && context->names[context->name_count-2].name
1983 && context->names[context->name_count-2].name == name)
1984 idx = context->name_count - 2;
1985 else {
1986 /* FIXME: how much do we care about inodes that have no
1987 * associated name? */
1988 if (audit_inc_name_count(context, inode))
1989 return;
1990 idx = context->name_count - 1;
1991 context->names[idx].name = NULL;
1992 }
1993 handle_path(dentry);
1994 audit_copy_inode(&context->names[idx], dentry, inode);
1995 }
1996
1997 /**
1998 * audit_inode_child - collect inode info for created/removed objects
1999 * @dentry: dentry being audited
2000 * @parent: inode of dentry parent
2001 *
2002 * For syscalls that create or remove filesystem objects, audit_inode
2003 * can only collect information for the filesystem object's parent.
2004 * This call updates the audit context with the child's information.
2005 * Syscalls that create a new filesystem object must be hooked after
2006 * the object is created. Syscalls that remove a filesystem object
2007 * must be hooked prior, in order to capture the target inode during
2008 * unsuccessful attempts.
2009 */
2010 void __audit_inode_child(const struct dentry *dentry,
2011 const struct inode *parent)
2012 {
2013 int idx;
2014 struct audit_context *context = current->audit_context;
2015 const char *found_parent = NULL, *found_child = NULL;
2016 const struct inode *inode = dentry->d_inode;
2017 const char *dname = dentry->d_name.name;
2018 int dirlen = 0;
2019
2020 if (!context->in_syscall)
2021 return;
2022
2023 if (inode)
2024 handle_one(inode);
2025
2026 /* parent is more likely, look for it first */
2027 for (idx = 0; idx < context->name_count; idx++) {
2028 struct audit_names *n = &context->names[idx];
2029
2030 if (!n->name)
2031 continue;
2032
2033 if (n->ino == parent->i_ino &&
2034 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2035 n->name_len = dirlen; /* update parent data in place */
2036 found_parent = n->name;
2037 goto add_names;
2038 }
2039 }
2040
2041 /* no matching parent, look for matching child */
2042 for (idx = 0; idx < context->name_count; idx++) {
2043 struct audit_names *n = &context->names[idx];
2044
2045 if (!n->name)
2046 continue;
2047
2048 /* strcmp() is the more likely scenario */
2049 if (!strcmp(dname, n->name) ||
2050 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2051 if (inode)
2052 audit_copy_inode(n, NULL, inode);
2053 else
2054 n->ino = (unsigned long)-1;
2055 found_child = n->name;
2056 goto add_names;
2057 }
2058 }
2059
2060 add_names:
2061 if (!found_parent) {
2062 if (audit_inc_name_count(context, parent))
2063 return;
2064 idx = context->name_count - 1;
2065 context->names[idx].name = NULL;
2066 audit_copy_inode(&context->names[idx], NULL, parent);
2067 }
2068
2069 if (!found_child) {
2070 if (audit_inc_name_count(context, inode))
2071 return;
2072 idx = context->name_count - 1;
2073
2074 /* Re-use the name belonging to the slot for a matching parent
2075 * directory. All names for this context are relinquished in
2076 * audit_free_names() */
2077 if (found_parent) {
2078 context->names[idx].name = found_parent;
2079 context->names[idx].name_len = AUDIT_NAME_FULL;
2080 /* don't call __putname() */
2081 context->names[idx].name_put = 0;
2082 } else {
2083 context->names[idx].name = NULL;
2084 }
2085
2086 if (inode)
2087 audit_copy_inode(&context->names[idx], NULL, inode);
2088 else
2089 context->names[idx].ino = (unsigned long)-1;
2090 }
2091 }
2092 EXPORT_SYMBOL_GPL(__audit_inode_child);
2093
2094 /**
2095 * auditsc_get_stamp - get local copies of audit_context values
2096 * @ctx: audit_context for the task
2097 * @t: timespec to store time recorded in the audit_context
2098 * @serial: serial value that is recorded in the audit_context
2099 *
2100 * Also sets the context as auditable.
2101 */
2102 int auditsc_get_stamp(struct audit_context *ctx,
2103 struct timespec *t, unsigned int *serial)
2104 {
2105 if (!ctx->in_syscall)
2106 return 0;
2107 if (!ctx->serial)
2108 ctx->serial = audit_serial();
2109 t->tv_sec = ctx->ctime.tv_sec;
2110 t->tv_nsec = ctx->ctime.tv_nsec;
2111 *serial = ctx->serial;
2112 if (!ctx->prio) {
2113 ctx->prio = 1;
2114 ctx->current_state = AUDIT_RECORD_CONTEXT;
2115 }
2116 return 1;
2117 }
2118
2119 /* global counter which is incremented every time something logs in */
2120 static atomic_t session_id = ATOMIC_INIT(0);
2121
2122 /**
2123 * audit_set_loginuid - set a task's audit_context loginuid
2124 * @task: task whose audit context is being modified
2125 * @loginuid: loginuid value
2126 *
2127 * Returns 0.
2128 *
2129 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2130 */
2131 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
2132 {
2133 unsigned int sessionid = atomic_inc_return(&session_id);
2134 struct audit_context *context = task->audit_context;
2135
2136 if (context && context->in_syscall) {
2137 struct audit_buffer *ab;
2138
2139 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2140 if (ab) {
2141 audit_log_format(ab, "login pid=%d uid=%u "
2142 "old auid=%u new auid=%u"
2143 " old ses=%u new ses=%u",
2144 task->pid, task_uid(task),
2145 task->loginuid, loginuid,
2146 task->sessionid, sessionid);
2147 audit_log_end(ab);
2148 }
2149 }
2150 task->sessionid = sessionid;
2151 task->loginuid = loginuid;
2152 return 0;
2153 }
2154
2155 /**
2156 * __audit_mq_open - record audit data for a POSIX MQ open
2157 * @oflag: open flag
2158 * @mode: mode bits
2159 * @attr: queue attributes
2160 *
2161 */
2162 void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
2163 {
2164 struct audit_context *context = current->audit_context;
2165
2166 if (attr)
2167 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2168 else
2169 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
2170
2171 context->mq_open.oflag = oflag;
2172 context->mq_open.mode = mode;
2173
2174 context->type = AUDIT_MQ_OPEN;
2175 }
2176
2177 /**
2178 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
2179 * @mqdes: MQ descriptor
2180 * @msg_len: Message length
2181 * @msg_prio: Message priority
2182 * @abs_timeout: Message timeout in absolute time
2183 *
2184 */
2185 void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2186 const struct timespec *abs_timeout)
2187 {
2188 struct audit_context *context = current->audit_context;
2189 struct timespec *p = &context->mq_sendrecv.abs_timeout;
2190
2191 if (abs_timeout)
2192 memcpy(p, abs_timeout, sizeof(struct timespec));
2193 else
2194 memset(p, 0, sizeof(struct timespec));
2195
2196 context->mq_sendrecv.mqdes = mqdes;
2197 context->mq_sendrecv.msg_len = msg_len;
2198 context->mq_sendrecv.msg_prio = msg_prio;
2199
2200 context->type = AUDIT_MQ_SENDRECV;
2201 }
2202
2203 /**
2204 * __audit_mq_notify - record audit data for a POSIX MQ notify
2205 * @mqdes: MQ descriptor
2206 * @notification: Notification event
2207 *
2208 */
2209
2210 void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
2211 {
2212 struct audit_context *context = current->audit_context;
2213
2214 if (notification)
2215 context->mq_notify.sigev_signo = notification->sigev_signo;
2216 else
2217 context->mq_notify.sigev_signo = 0;
2218
2219 context->mq_notify.mqdes = mqdes;
2220 context->type = AUDIT_MQ_NOTIFY;
2221 }
2222
2223 /**
2224 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2225 * @mqdes: MQ descriptor
2226 * @mqstat: MQ flags
2227 *
2228 */
2229 void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2230 {
2231 struct audit_context *context = current->audit_context;
2232 context->mq_getsetattr.mqdes = mqdes;
2233 context->mq_getsetattr.mqstat = *mqstat;
2234 context->type = AUDIT_MQ_GETSETATTR;
2235 }
2236
2237 /**
2238 * audit_ipc_obj - record audit data for ipc object
2239 * @ipcp: ipc permissions
2240 *
2241 */
2242 void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2243 {
2244 struct audit_context *context = current->audit_context;
2245 context->ipc.uid = ipcp->uid;
2246 context->ipc.gid = ipcp->gid;
2247 context->ipc.mode = ipcp->mode;
2248 context->ipc.has_perm = 0;
2249 security_ipc_getsecid(ipcp, &context->ipc.osid);
2250 context->type = AUDIT_IPC;
2251 }
2252
2253 /**
2254 * audit_ipc_set_perm - record audit data for new ipc permissions
2255 * @qbytes: msgq bytes
2256 * @uid: msgq user id
2257 * @gid: msgq group id
2258 * @mode: msgq mode (permissions)
2259 *
2260 * Called only after audit_ipc_obj().
2261 */
2262 void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
2263 {
2264 struct audit_context *context = current->audit_context;
2265
2266 context->ipc.qbytes = qbytes;
2267 context->ipc.perm_uid = uid;
2268 context->ipc.perm_gid = gid;
2269 context->ipc.perm_mode = mode;
2270 context->ipc.has_perm = 1;
2271 }
2272
2273 int audit_bprm(struct linux_binprm *bprm)
2274 {
2275 struct audit_aux_data_execve *ax;
2276 struct audit_context *context = current->audit_context;
2277
2278 if (likely(!audit_enabled || !context || context->dummy))
2279 return 0;
2280
2281 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2282 if (!ax)
2283 return -ENOMEM;
2284
2285 ax->argc = bprm->argc;
2286 ax->envc = bprm->envc;
2287 ax->mm = bprm->mm;
2288 ax->d.type = AUDIT_EXECVE;
2289 ax->d.next = context->aux;
2290 context->aux = (void *)ax;
2291 return 0;
2292 }
2293
2294
2295 /**
2296 * audit_socketcall - record audit data for sys_socketcall
2297 * @nargs: number of args
2298 * @args: args array
2299 *
2300 */
2301 void audit_socketcall(int nargs, unsigned long *args)
2302 {
2303 struct audit_context *context = current->audit_context;
2304
2305 if (likely(!context || context->dummy))
2306 return;
2307
2308 context->type = AUDIT_SOCKETCALL;
2309 context->socketcall.nargs = nargs;
2310 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
2311 }
2312
2313 /**
2314 * __audit_fd_pair - record audit data for pipe and socketpair
2315 * @fd1: the first file descriptor
2316 * @fd2: the second file descriptor
2317 *
2318 */
2319 void __audit_fd_pair(int fd1, int fd2)
2320 {
2321 struct audit_context *context = current->audit_context;
2322 context->fds[0] = fd1;
2323 context->fds[1] = fd2;
2324 }
2325
2326 /**
2327 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2328 * @len: data length in user space
2329 * @a: data address in kernel space
2330 *
2331 * Returns 0 for success or NULL context or < 0 on error.
2332 */
2333 int audit_sockaddr(int len, void *a)
2334 {
2335 struct audit_context *context = current->audit_context;
2336
2337 if (likely(!context || context->dummy))
2338 return 0;
2339
2340 if (!context->sockaddr) {
2341 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2342 if (!p)
2343 return -ENOMEM;
2344 context->sockaddr = p;
2345 }
2346
2347 context->sockaddr_len = len;
2348 memcpy(context->sockaddr, a, len);
2349 return 0;
2350 }
2351
2352 void __audit_ptrace(struct task_struct *t)
2353 {
2354 struct audit_context *context = current->audit_context;
2355
2356 context->target_pid = t->pid;
2357 context->target_auid = audit_get_loginuid(t);
2358 context->target_uid = task_uid(t);
2359 context->target_sessionid = audit_get_sessionid(t);
2360 security_task_getsecid(t, &context->target_sid);
2361 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
2362 }
2363
2364 /**
2365 * audit_signal_info - record signal info for shutting down audit subsystem
2366 * @sig: signal value
2367 * @t: task being signaled
2368 *
2369 * If the audit subsystem is being terminated, record the task (pid)
2370 * and uid that is doing that.
2371 */
2372 int __audit_signal_info(int sig, struct task_struct *t)
2373 {
2374 struct audit_aux_data_pids *axp;
2375 struct task_struct *tsk = current;
2376 struct audit_context *ctx = tsk->audit_context;
2377 uid_t uid = current_uid(), t_uid = task_uid(t);
2378
2379 if (audit_pid && t->tgid == audit_pid) {
2380 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
2381 audit_sig_pid = tsk->pid;
2382 if (tsk->loginuid != -1)
2383 audit_sig_uid = tsk->loginuid;
2384 else
2385 audit_sig_uid = uid;
2386 security_task_getsecid(tsk, &audit_sig_sid);
2387 }
2388 if (!audit_signals || audit_dummy_context())
2389 return 0;
2390 }
2391
2392 /* optimize the common case by putting first signal recipient directly
2393 * in audit_context */
2394 if (!ctx->target_pid) {
2395 ctx->target_pid = t->tgid;
2396 ctx->target_auid = audit_get_loginuid(t);
2397 ctx->target_uid = t_uid;
2398 ctx->target_sessionid = audit_get_sessionid(t);
2399 security_task_getsecid(t, &ctx->target_sid);
2400 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
2401 return 0;
2402 }
2403
2404 axp = (void *)ctx->aux_pids;
2405 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2406 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2407 if (!axp)
2408 return -ENOMEM;
2409
2410 axp->d.type = AUDIT_OBJ_PID;
2411 axp->d.next = ctx->aux_pids;
2412 ctx->aux_pids = (void *)axp;
2413 }
2414 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2415
2416 axp->target_pid[axp->pid_count] = t->tgid;
2417 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2418 axp->target_uid[axp->pid_count] = t_uid;
2419 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
2420 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
2421 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
2422 axp->pid_count++;
2423
2424 return 0;
2425 }
2426
2427 /**
2428 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2429 * @bprm: pointer to the bprm being processed
2430 * @new: the proposed new credentials
2431 * @old: the old credentials
2432 *
2433 * Simply check if the proc already has the caps given by the file and if not
2434 * store the priv escalation info for later auditing at the end of the syscall
2435 *
2436 * -Eric
2437 */
2438 int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2439 const struct cred *new, const struct cred *old)
2440 {
2441 struct audit_aux_data_bprm_fcaps *ax;
2442 struct audit_context *context = current->audit_context;
2443 struct cpu_vfs_cap_data vcaps;
2444 struct dentry *dentry;
2445
2446 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2447 if (!ax)
2448 return -ENOMEM;
2449
2450 ax->d.type = AUDIT_BPRM_FCAPS;
2451 ax->d.next = context->aux;
2452 context->aux = (void *)ax;
2453
2454 dentry = dget(bprm->file->f_dentry);
2455 get_vfs_caps_from_disk(dentry, &vcaps);
2456 dput(dentry);
2457
2458 ax->fcap.permitted = vcaps.permitted;
2459 ax->fcap.inheritable = vcaps.inheritable;
2460 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2461 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2462
2463 ax->old_pcap.permitted = old->cap_permitted;
2464 ax->old_pcap.inheritable = old->cap_inheritable;
2465 ax->old_pcap.effective = old->cap_effective;
2466
2467 ax->new_pcap.permitted = new->cap_permitted;
2468 ax->new_pcap.inheritable = new->cap_inheritable;
2469 ax->new_pcap.effective = new->cap_effective;
2470 return 0;
2471 }
2472
2473 /**
2474 * __audit_log_capset - store information about the arguments to the capset syscall
2475 * @pid: target pid of the capset call
2476 * @new: the new credentials
2477 * @old: the old (current) credentials
2478 *
2479 * Record the aguments userspace sent to sys_capset for later printing by the
2480 * audit system if applicable
2481 */
2482 void __audit_log_capset(pid_t pid,
2483 const struct cred *new, const struct cred *old)
2484 {
2485 struct audit_context *context = current->audit_context;
2486 context->capset.pid = pid;
2487 context->capset.cap.effective = new->cap_effective;
2488 context->capset.cap.inheritable = new->cap_effective;
2489 context->capset.cap.permitted = new->cap_permitted;
2490 context->type = AUDIT_CAPSET;
2491 }
2492
2493 void __audit_mmap_fd(int fd, int flags)
2494 {
2495 struct audit_context *context = current->audit_context;
2496 context->mmap.fd = fd;
2497 context->mmap.flags = flags;
2498 context->type = AUDIT_MMAP;
2499 }
2500
2501 /**
2502 * audit_core_dumps - record information about processes that end abnormally
2503 * @signr: signal value
2504 *
2505 * If a process ends with a core dump, something fishy is going on and we
2506 * should record the event for investigation.
2507 */
2508 void audit_core_dumps(long signr)
2509 {
2510 struct audit_buffer *ab;
2511 u32 sid;
2512 uid_t auid = audit_get_loginuid(current), uid;
2513 gid_t gid;
2514 unsigned int sessionid = audit_get_sessionid(current);
2515
2516 if (!audit_enabled)
2517 return;
2518
2519 if (signr == SIGQUIT) /* don't care for those */
2520 return;
2521
2522 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2523 current_uid_gid(&uid, &gid);
2524 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2525 auid, uid, gid, sessionid);
2526 security_task_getsecid(current, &sid);
2527 if (sid) {
2528 char *ctx = NULL;
2529 u32 len;
2530
2531 if (security_secid_to_secctx(sid, &ctx, &len))
2532 audit_log_format(ab, " ssid=%u", sid);
2533 else {
2534 audit_log_format(ab, " subj=%s", ctx);
2535 security_release_secctx(ctx, len);
2536 }
2537 }
2538 audit_log_format(ab, " pid=%d comm=", current->pid);
2539 audit_log_untrustedstring(ab, current->comm);
2540 audit_log_format(ab, " sig=%ld", signr);
2541 audit_log_end(ab);
2542 }
2543
2544 struct list_head *audit_killed_trees(void)
2545 {
2546 struct audit_context *ctx = current->audit_context;
2547 if (likely(!ctx || !ctx->in_syscall))
2548 return NULL;
2549 return &ctx->killed_trees;
2550 }
This page took 0.08312 seconds and 4 git commands to generate.