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