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