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