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