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