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