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