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