[PATCH] add/remove rule update
[linux-2.6-block.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.
b63862f4 6 * Copyright (C) 2005 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 *
b63862f4
DK
32 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
34 *
73241ccc
AG
35 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
8c8570fb
DK
37 *
38 * Subject and object context labeling support added by <danjones@us.ibm.com>
39 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
1da177e4
LT
40 */
41
42#include <linux/init.h>
1da177e4 43#include <asm/types.h>
715b49ef 44#include <asm/atomic.h>
73241ccc
AG
45#include <asm/types.h>
46#include <linux/fs.h>
47#include <linux/namei.h>
1da177e4
LT
48#include <linux/mm.h>
49#include <linux/module.h>
01116105 50#include <linux/mount.h>
3ec3b2fb 51#include <linux/socket.h>
1da177e4
LT
52#include <linux/audit.h>
53#include <linux/personality.h>
54#include <linux/time.h>
5bb289b5 55#include <linux/netlink.h>
f5561964 56#include <linux/compiler.h>
1da177e4 57#include <asm/unistd.h>
8c8570fb 58#include <linux/security.h>
fe7752ba 59#include <linux/list.h>
1da177e4 60
fe7752ba
DW
61#include "audit.h"
62
63extern struct list_head audit_filter_list[];
1da177e4
LT
64
65/* No syscall auditing will take place unless audit_enabled != 0. */
66extern int audit_enabled;
67
68/* AUDIT_NAMES is the number of slots we reserve in the audit_context
69 * for saving names from getname(). */
70#define AUDIT_NAMES 20
71
72/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
73 * audit_context from being used for nameless inodes from
74 * path_lookup. */
75#define AUDIT_NAMES_RESERVED 7
76
1da177e4
LT
77/* When fs/namei.c:getname() is called, we store the pointer in name and
78 * we don't let putname() free it (instead we free all of the saved
79 * pointers at syscall exit time).
80 *
81 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
82struct audit_names {
83 const char *name;
84 unsigned long ino;
73241ccc 85 unsigned long pino;
1da177e4
LT
86 dev_t dev;
87 umode_t mode;
88 uid_t uid;
89 gid_t gid;
90 dev_t rdev;
8c8570fb 91 char *ctx;
1da177e4
LT
92};
93
94struct audit_aux_data {
95 struct audit_aux_data *next;
96 int type;
97};
98
99#define AUDIT_AUX_IPCPERM 0
100
101struct audit_aux_data_ipcctl {
102 struct audit_aux_data d;
103 struct ipc_perm p;
104 unsigned long qbytes;
105 uid_t uid;
106 gid_t gid;
107 mode_t mode;
8c8570fb 108 char *ctx;
1da177e4
LT
109};
110
3ec3b2fb
DW
111struct audit_aux_data_socketcall {
112 struct audit_aux_data d;
113 int nargs;
114 unsigned long args[0];
115};
116
117struct audit_aux_data_sockaddr {
118 struct audit_aux_data d;
119 int len;
120 char a[0];
121};
122
01116105
SS
123struct audit_aux_data_path {
124 struct audit_aux_data d;
125 struct dentry *dentry;
126 struct vfsmount *mnt;
127};
1da177e4
LT
128
129/* The per-task audit context. */
130struct audit_context {
131 int in_syscall; /* 1 if task is in a syscall */
132 enum audit_state state;
133 unsigned int serial; /* serial number for record */
134 struct timespec ctime; /* time of syscall entry */
135 uid_t loginuid; /* login uid (identity) */
136 int major; /* syscall number */
137 unsigned long argv[4]; /* syscall arguments */
138 int return_valid; /* return code is valid */
2fd6f58b 139 long return_code;/* syscall return code */
1da177e4
LT
140 int auditable; /* 1 if record should be written */
141 int name_count;
142 struct audit_names names[AUDIT_NAMES];
8f37d47c
DW
143 struct dentry * pwd;
144 struct vfsmount * pwdmnt;
1da177e4
LT
145 struct audit_context *previous; /* For nested syscalls */
146 struct audit_aux_data *aux;
147
148 /* Save things to print about task_struct */
149 pid_t pid;
150 uid_t uid, euid, suid, fsuid;
151 gid_t gid, egid, sgid, fsgid;
152 unsigned long personality;
2fd6f58b 153 int arch;
1da177e4
LT
154
155#if AUDIT_DEBUG
156 int put_count;
157 int ino_count;
158#endif
159};
160
b63862f4 161
1da177e4
LT
162/* Compare a task_struct with an audit_rule. Return 1 on match, 0
163 * otherwise. */
164static int audit_filter_rules(struct task_struct *tsk,
93315ed6 165 struct audit_krule *rule,
1da177e4
LT
166 struct audit_context *ctx,
167 enum audit_state *state)
168{
169 int i, j;
170
171 for (i = 0; i < rule->field_count; i++) {
93315ed6 172 struct audit_field *f = &rule->fields[i];
1da177e4
LT
173 int result = 0;
174
93315ed6 175 switch (f->type) {
1da177e4 176 case AUDIT_PID:
93315ed6 177 result = audit_comparator(tsk->pid, f->op, f->val);
1da177e4
LT
178 break;
179 case AUDIT_UID:
93315ed6 180 result = audit_comparator(tsk->uid, f->op, f->val);
1da177e4
LT
181 break;
182 case AUDIT_EUID:
93315ed6 183 result = audit_comparator(tsk->euid, f->op, f->val);
1da177e4
LT
184 break;
185 case AUDIT_SUID:
93315ed6 186 result = audit_comparator(tsk->suid, f->op, f->val);
1da177e4
LT
187 break;
188 case AUDIT_FSUID:
93315ed6 189 result = audit_comparator(tsk->fsuid, f->op, f->val);
1da177e4
LT
190 break;
191 case AUDIT_GID:
93315ed6 192 result = audit_comparator(tsk->gid, f->op, f->val);
1da177e4
LT
193 break;
194 case AUDIT_EGID:
93315ed6 195 result = audit_comparator(tsk->egid, f->op, f->val);
1da177e4
LT
196 break;
197 case AUDIT_SGID:
93315ed6 198 result = audit_comparator(tsk->sgid, f->op, f->val);
1da177e4
LT
199 break;
200 case AUDIT_FSGID:
93315ed6 201 result = audit_comparator(tsk->fsgid, f->op, f->val);
1da177e4
LT
202 break;
203 case AUDIT_PERS:
93315ed6 204 result = audit_comparator(tsk->personality, f->op, f->val);
1da177e4 205 break;
2fd6f58b 206 case AUDIT_ARCH:
b63862f4 207 if (ctx)
93315ed6 208 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f58b 209 break;
1da177e4
LT
210
211 case AUDIT_EXIT:
212 if (ctx && ctx->return_valid)
93315ed6 213 result = audit_comparator(ctx->return_code, f->op, f->val);
1da177e4
LT
214 break;
215 case AUDIT_SUCCESS:
b01f2cc1 216 if (ctx && ctx->return_valid) {
93315ed6
AG
217 if (f->val)
218 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
b01f2cc1 219 else
93315ed6 220 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
b01f2cc1 221 }
1da177e4
LT
222 break;
223 case AUDIT_DEVMAJOR:
224 if (ctx) {
225 for (j = 0; j < ctx->name_count; j++) {
93315ed6 226 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
1da177e4
LT
227 ++result;
228 break;
229 }
230 }
231 }
232 break;
233 case AUDIT_DEVMINOR:
234 if (ctx) {
235 for (j = 0; j < ctx->name_count; j++) {
93315ed6 236 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
1da177e4
LT
237 ++result;
238 break;
239 }
240 }
241 }
242 break;
243 case AUDIT_INODE:
244 if (ctx) {
245 for (j = 0; j < ctx->name_count; j++) {
93315ed6
AG
246 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
247 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
1da177e4
LT
248 ++result;
249 break;
250 }
251 }
252 }
253 break;
254 case AUDIT_LOGINUID:
255 result = 0;
256 if (ctx)
93315ed6 257 result = audit_comparator(ctx->loginuid, f->op, f->val);
1da177e4
LT
258 break;
259 case AUDIT_ARG0:
260 case AUDIT_ARG1:
261 case AUDIT_ARG2:
262 case AUDIT_ARG3:
263 if (ctx)
93315ed6 264 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
1da177e4
LT
265 break;
266 }
267
1da177e4
LT
268 if (!result)
269 return 0;
270 }
271 switch (rule->action) {
272 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
273 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
274 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
275 }
276 return 1;
277}
278
279/* At process creation time, we can determine if system-call auditing is
280 * completely disabled for this task. Since we only have the task
281 * structure at this point, we can only check uid and gid.
282 */
283static enum audit_state audit_filter_task(struct task_struct *tsk)
284{
285 struct audit_entry *e;
286 enum audit_state state;
287
288 rcu_read_lock();
0f45aa18 289 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
1da177e4
LT
290 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
291 rcu_read_unlock();
292 return state;
293 }
294 }
295 rcu_read_unlock();
296 return AUDIT_BUILD_CONTEXT;
297}
298
299/* At syscall entry and exit time, this filter is called if the
300 * audit_state is not low enough that auditing cannot take place, but is
23f32d18 301 * also not high enough that we already know we have to write an audit
b0dd25a8 302 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
1da177e4
LT
303 */
304static enum audit_state audit_filter_syscall(struct task_struct *tsk,
305 struct audit_context *ctx,
306 struct list_head *list)
307{
308 struct audit_entry *e;
c3896495 309 enum audit_state state;
1da177e4 310
351bb722 311 if (audit_pid && tsk->tgid == audit_pid)
f7056d64
DW
312 return AUDIT_DISABLED;
313
1da177e4 314 rcu_read_lock();
c3896495 315 if (!list_empty(list)) {
b63862f4
DK
316 int word = AUDIT_WORD(ctx->major);
317 int bit = AUDIT_BIT(ctx->major);
318
319 list_for_each_entry_rcu(e, list, list) {
320 if ((e->rule.mask[word] & bit) == bit
321 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
322 rcu_read_unlock();
323 return state;
324 }
325 }
1da177e4
LT
326 }
327 rcu_read_unlock();
328 return AUDIT_BUILD_CONTEXT;
329}
330
331/* This should be called with task_lock() held. */
332static inline struct audit_context *audit_get_context(struct task_struct *tsk,
333 int return_valid,
334 int return_code)
335{
336 struct audit_context *context = tsk->audit_context;
337
338 if (likely(!context))
339 return NULL;
340 context->return_valid = return_valid;
341 context->return_code = return_code;
342
21af6c4f 343 if (context->in_syscall && !context->auditable) {
1da177e4 344 enum audit_state state;
0f45aa18 345 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
1da177e4
LT
346 if (state == AUDIT_RECORD_CONTEXT)
347 context->auditable = 1;
348 }
349
350 context->pid = tsk->pid;
351 context->uid = tsk->uid;
352 context->gid = tsk->gid;
353 context->euid = tsk->euid;
354 context->suid = tsk->suid;
355 context->fsuid = tsk->fsuid;
356 context->egid = tsk->egid;
357 context->sgid = tsk->sgid;
358 context->fsgid = tsk->fsgid;
359 context->personality = tsk->personality;
360 tsk->audit_context = NULL;
361 return context;
362}
363
364static inline void audit_free_names(struct audit_context *context)
365{
366 int i;
367
368#if AUDIT_DEBUG == 2
369 if (context->auditable
370 ||context->put_count + context->ino_count != context->name_count) {
73241ccc 371 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
1da177e4
LT
372 " name_count=%d put_count=%d"
373 " ino_count=%d [NOT freeing]\n",
73241ccc 374 __FILE__, __LINE__,
1da177e4
LT
375 context->serial, context->major, context->in_syscall,
376 context->name_count, context->put_count,
377 context->ino_count);
8c8570fb 378 for (i = 0; i < context->name_count; i++) {
1da177e4
LT
379 printk(KERN_ERR "names[%d] = %p = %s\n", i,
380 context->names[i].name,
73241ccc 381 context->names[i].name ?: "(null)");
8c8570fb 382 }
1da177e4
LT
383 dump_stack();
384 return;
385 }
386#endif
387#if AUDIT_DEBUG
388 context->put_count = 0;
389 context->ino_count = 0;
390#endif
391
8c8570fb
DK
392 for (i = 0; i < context->name_count; i++) {
393 char *p = context->names[i].ctx;
394 context->names[i].ctx = NULL;
395 kfree(p);
1da177e4
LT
396 if (context->names[i].name)
397 __putname(context->names[i].name);
8c8570fb 398 }
1da177e4 399 context->name_count = 0;
8f37d47c
DW
400 if (context->pwd)
401 dput(context->pwd);
402 if (context->pwdmnt)
403 mntput(context->pwdmnt);
404 context->pwd = NULL;
405 context->pwdmnt = NULL;
1da177e4
LT
406}
407
408static inline void audit_free_aux(struct audit_context *context)
409{
410 struct audit_aux_data *aux;
411
412 while ((aux = context->aux)) {
01116105
SS
413 if (aux->type == AUDIT_AVC_PATH) {
414 struct audit_aux_data_path *axi = (void *)aux;
415 dput(axi->dentry);
416 mntput(axi->mnt);
417 }
8c8570fb
DK
418 if ( aux->type == AUDIT_IPC ) {
419 struct audit_aux_data_ipcctl *axi = (void *)aux;
420 if (axi->ctx)
421 kfree(axi->ctx);
422 }
423
1da177e4
LT
424 context->aux = aux->next;
425 kfree(aux);
426 }
427}
428
429static inline void audit_zero_context(struct audit_context *context,
430 enum audit_state state)
431{
432 uid_t loginuid = context->loginuid;
433
434 memset(context, 0, sizeof(*context));
435 context->state = state;
436 context->loginuid = loginuid;
437}
438
439static inline struct audit_context *audit_alloc_context(enum audit_state state)
440{
441 struct audit_context *context;
442
443 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
444 return NULL;
445 audit_zero_context(context, state);
446 return context;
447}
448
b0dd25a8
RD
449/**
450 * audit_alloc - allocate an audit context block for a task
451 * @tsk: task
452 *
453 * Filter on the task information and allocate a per-task audit context
1da177e4
LT
454 * if necessary. Doing so turns on system call auditing for the
455 * specified task. This is called from copy_process, so no lock is
b0dd25a8
RD
456 * needed.
457 */
1da177e4
LT
458int audit_alloc(struct task_struct *tsk)
459{
460 struct audit_context *context;
461 enum audit_state state;
462
463 if (likely(!audit_enabled))
464 return 0; /* Return if not auditing. */
465
466 state = audit_filter_task(tsk);
467 if (likely(state == AUDIT_DISABLED))
468 return 0;
469
470 if (!(context = audit_alloc_context(state))) {
471 audit_log_lost("out of memory in audit_alloc");
472 return -ENOMEM;
473 }
474
475 /* Preserve login uid */
476 context->loginuid = -1;
477 if (current->audit_context)
478 context->loginuid = current->audit_context->loginuid;
479
480 tsk->audit_context = context;
481 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
482 return 0;
483}
484
485static inline void audit_free_context(struct audit_context *context)
486{
487 struct audit_context *previous;
488 int count = 0;
489
490 do {
491 previous = context->previous;
492 if (previous || (count && count < 10)) {
493 ++count;
494 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
495 " freeing multiple contexts (%d)\n",
496 context->serial, context->major,
497 context->name_count, count);
498 }
499 audit_free_names(context);
500 audit_free_aux(context);
501 kfree(context);
502 context = previous;
503 } while (context);
504 if (count >= 10)
505 printk(KERN_ERR "audit: freed %d contexts\n", count);
506}
507
8c8570fb
DK
508static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask)
509{
510 char *ctx = NULL;
511 ssize_t len = 0;
512
513 len = security_getprocattr(current, "current", NULL, 0);
514 if (len < 0) {
515 if (len != -EINVAL)
516 goto error_path;
517 return;
518 }
519
520 ctx = kmalloc(len, gfp_mask);
7306a0b9 521 if (!ctx)
8c8570fb 522 goto error_path;
8c8570fb
DK
523
524 len = security_getprocattr(current, "current", ctx, len);
525 if (len < 0 )
526 goto error_path;
527
528 audit_log_format(ab, " subj=%s", ctx);
7306a0b9 529 return;
8c8570fb
DK
530
531error_path:
532 if (ctx)
533 kfree(ctx);
7306a0b9 534 audit_panic("error in audit_log_task_context");
8c8570fb
DK
535 return;
536}
537
538static void audit_log_task_info(struct audit_buffer *ab, gfp_t gfp_mask)
219f0817
SS
539{
540 char name[sizeof(current->comm)];
541 struct mm_struct *mm = current->mm;
542 struct vm_area_struct *vma;
543
544 get_task_comm(name, current);
99e45eea
DW
545 audit_log_format(ab, " comm=");
546 audit_log_untrustedstring(ab, name);
219f0817
SS
547
548 if (!mm)
549 return;
550
8c8570fb
DK
551 /*
552 * this is brittle; all callers that pass GFP_ATOMIC will have
553 * NULL current->mm and we won't get here.
554 */
219f0817
SS
555 down_read(&mm->mmap_sem);
556 vma = mm->mmap;
557 while (vma) {
558 if ((vma->vm_flags & VM_EXECUTABLE) &&
559 vma->vm_file) {
560 audit_log_d_path(ab, "exe=",
561 vma->vm_file->f_dentry,
562 vma->vm_file->f_vfsmnt);
563 break;
564 }
565 vma = vma->vm_next;
566 }
567 up_read(&mm->mmap_sem);
8c8570fb 568 audit_log_task_context(ab, gfp_mask);
219f0817
SS
569}
570
9796fdd8 571static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
1da177e4
LT
572{
573 int i;
574 struct audit_buffer *ab;
7551ced3 575 struct audit_aux_data *aux;
1da177e4 576
f5561964 577 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
1da177e4
LT
578 if (!ab)
579 return; /* audit_panic has been called */
bccf6ae0
DW
580 audit_log_format(ab, "arch=%x syscall=%d",
581 context->arch, context->major);
1da177e4
LT
582 if (context->personality != PER_LINUX)
583 audit_log_format(ab, " per=%lx", context->personality);
584 if (context->return_valid)
2fd6f58b 585 audit_log_format(ab, " success=%s exit=%ld",
586 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
587 context->return_code);
1da177e4
LT
588 audit_log_format(ab,
589 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
326e9c8b
SG
590 " pid=%d auid=%u uid=%u gid=%u"
591 " euid=%u suid=%u fsuid=%u"
592 " egid=%u sgid=%u fsgid=%u",
1da177e4
LT
593 context->argv[0],
594 context->argv[1],
595 context->argv[2],
596 context->argv[3],
597 context->name_count,
598 context->pid,
599 context->loginuid,
600 context->uid,
601 context->gid,
602 context->euid, context->suid, context->fsuid,
603 context->egid, context->sgid, context->fsgid);
8c8570fb 604 audit_log_task_info(ab, gfp_mask);
1da177e4 605 audit_log_end(ab);
1da177e4 606
7551ced3 607 for (aux = context->aux; aux; aux = aux->next) {
c0404993 608
ef20c8c1 609 ab = audit_log_start(context, gfp_mask, aux->type);
1da177e4
LT
610 if (!ab)
611 continue; /* audit_panic has been called */
612
1da177e4 613 switch (aux->type) {
c0404993 614 case AUDIT_IPC: {
1da177e4
LT
615 struct audit_aux_data_ipcctl *axi = (void *)aux;
616 audit_log_format(ab,
8c8570fb
DK
617 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
618 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
3ec3b2fb
DW
619 break; }
620
621 case AUDIT_SOCKETCALL: {
622 int i;
623 struct audit_aux_data_socketcall *axs = (void *)aux;
624 audit_log_format(ab, "nargs=%d", axs->nargs);
625 for (i=0; i<axs->nargs; i++)
626 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
627 break; }
628
629 case AUDIT_SOCKADDR: {
630 struct audit_aux_data_sockaddr *axs = (void *)aux;
631
632 audit_log_format(ab, "saddr=");
633 audit_log_hex(ab, axs->a, axs->len);
634 break; }
01116105
SS
635
636 case AUDIT_AVC_PATH: {
637 struct audit_aux_data_path *axi = (void *)aux;
638 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
01116105
SS
639 break; }
640
1da177e4
LT
641 }
642 audit_log_end(ab);
1da177e4
LT
643 }
644
8f37d47c 645 if (context->pwd && context->pwdmnt) {
ef20c8c1 646 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
8f37d47c
DW
647 if (ab) {
648 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
649 audit_log_end(ab);
650 }
651 }
1da177e4 652 for (i = 0; i < context->name_count; i++) {
73241ccc
AG
653 unsigned long ino = context->names[i].ino;
654 unsigned long pino = context->names[i].pino;
655
ef20c8c1 656 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
1da177e4
LT
657 if (!ab)
658 continue; /* audit_panic has been called */
8f37d47c 659
1da177e4 660 audit_log_format(ab, "item=%d", i);
73241ccc
AG
661
662 audit_log_format(ab, " name=");
663 if (context->names[i].name)
83c7d091 664 audit_log_untrustedstring(ab, context->names[i].name);
73241ccc
AG
665 else
666 audit_log_format(ab, "(null)");
667
668 if (pino != (unsigned long)-1)
669 audit_log_format(ab, " parent=%lu", pino);
670 if (ino != (unsigned long)-1)
671 audit_log_format(ab, " inode=%lu", ino);
672 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
673 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
674 " ouid=%u ogid=%u rdev=%02x:%02x",
675 MAJOR(context->names[i].dev),
676 MINOR(context->names[i].dev),
677 context->names[i].mode,
678 context->names[i].uid,
679 context->names[i].gid,
680 MAJOR(context->names[i].rdev),
1da177e4 681 MINOR(context->names[i].rdev));
8c8570fb
DK
682 if (context->names[i].ctx) {
683 audit_log_format(ab, " obj=%s",
684 context->names[i].ctx);
685 }
686
1da177e4
LT
687 audit_log_end(ab);
688 }
689}
690
b0dd25a8
RD
691/**
692 * audit_free - free a per-task audit context
693 * @tsk: task whose audit context block to free
694 *
695 * Called from copy_process and __put_task_struct.
696 */
1da177e4
LT
697void audit_free(struct task_struct *tsk)
698{
699 struct audit_context *context;
700
701 task_lock(tsk);
702 context = audit_get_context(tsk, 0, 0);
703 task_unlock(tsk);
704
705 if (likely(!context))
706 return;
707
708 /* Check for system calls that do not go through the exit
f5561964
DW
709 * function (e.g., exit_group), then free context block.
710 * We use GFP_ATOMIC here because we might be doing this
711 * in the context of the idle thread */
f7056d64 712 if (context->in_syscall && context->auditable)
f5561964 713 audit_log_exit(context, GFP_ATOMIC);
1da177e4
LT
714
715 audit_free_context(context);
716}
717
b0dd25a8
RD
718/**
719 * audit_syscall_entry - fill in an audit record at syscall entry
720 * @tsk: task being audited
721 * @arch: architecture type
722 * @major: major syscall type (function)
723 * @a1: additional syscall register 1
724 * @a2: additional syscall register 2
725 * @a3: additional syscall register 3
726 * @a4: additional syscall register 4
727 *
728 * Fill in audit context at syscall entry. This only happens if the
1da177e4
LT
729 * audit context was created when the task was created and the state or
730 * filters demand the audit context be built. If the state from the
731 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
732 * then the record will be written at syscall exit time (otherwise, it
733 * will only be written if another part of the kernel requests that it
b0dd25a8
RD
734 * be written).
735 */
2fd6f58b 736void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
1da177e4
LT
737 unsigned long a1, unsigned long a2,
738 unsigned long a3, unsigned long a4)
739{
740 struct audit_context *context = tsk->audit_context;
741 enum audit_state state;
742
743 BUG_ON(!context);
744
b0dd25a8
RD
745 /*
746 * This happens only on certain architectures that make system
1da177e4
LT
747 * calls in kernel_thread via the entry.S interface, instead of
748 * with direct calls. (If you are porting to a new
749 * architecture, hitting this condition can indicate that you
750 * got the _exit/_leave calls backward in entry.S.)
751 *
752 * i386 no
753 * x86_64 no
754 * ppc64 yes (see arch/ppc64/kernel/misc.S)
755 *
756 * This also happens with vm86 emulation in a non-nested manner
757 * (entries without exits), so this case must be caught.
758 */
759 if (context->in_syscall) {
760 struct audit_context *newctx;
761
1da177e4
LT
762#if AUDIT_DEBUG
763 printk(KERN_ERR
764 "audit(:%d) pid=%d in syscall=%d;"
765 " entering syscall=%d\n",
766 context->serial, tsk->pid, context->major, major);
767#endif
768 newctx = audit_alloc_context(context->state);
769 if (newctx) {
770 newctx->previous = context;
771 context = newctx;
772 tsk->audit_context = newctx;
773 } else {
774 /* If we can't alloc a new context, the best we
775 * can do is to leak memory (any pending putname
776 * will be lost). The only other alternative is
777 * to abandon auditing. */
778 audit_zero_context(context, context->state);
779 }
780 }
781 BUG_ON(context->in_syscall || context->name_count);
782
783 if (!audit_enabled)
784 return;
785
2fd6f58b 786 context->arch = arch;
1da177e4
LT
787 context->major = major;
788 context->argv[0] = a1;
789 context->argv[1] = a2;
790 context->argv[2] = a3;
791 context->argv[3] = a4;
792
793 state = context->state;
794 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
0f45aa18 795 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1da177e4
LT
796 if (likely(state == AUDIT_DISABLED))
797 return;
798
ce625a80 799 context->serial = 0;
1da177e4
LT
800 context->ctime = CURRENT_TIME;
801 context->in_syscall = 1;
802 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
803}
804
b0dd25a8
RD
805/**
806 * audit_syscall_exit - deallocate audit context after a system call
807 * @tsk: task being audited
808 * @valid: success/failure flag
809 * @return_code: syscall return value
810 *
811 * Tear down after system call. If the audit context has been marked as
1da177e4
LT
812 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
813 * filtering, or because some other part of the kernel write an audit
814 * message), then write out the syscall information. In call cases,
b0dd25a8
RD
815 * free the names stored from getname().
816 */
2fd6f58b 817void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1da177e4
LT
818{
819 struct audit_context *context;
820
821 get_task_struct(tsk);
822 task_lock(tsk);
2fd6f58b 823 context = audit_get_context(tsk, valid, return_code);
1da177e4
LT
824 task_unlock(tsk);
825
826 /* Not having a context here is ok, since the parent may have
827 * called __put_task_struct. */
828 if (likely(!context))
413a1c75 829 goto out;
1da177e4 830
f7056d64 831 if (context->in_syscall && context->auditable)
f5561964 832 audit_log_exit(context, GFP_KERNEL);
1da177e4
LT
833
834 context->in_syscall = 0;
835 context->auditable = 0;
2fd6f58b 836
1da177e4
LT
837 if (context->previous) {
838 struct audit_context *new_context = context->previous;
839 context->previous = NULL;
840 audit_free_context(context);
841 tsk->audit_context = new_context;
842 } else {
843 audit_free_names(context);
844 audit_free_aux(context);
1da177e4
LT
845 tsk->audit_context = context;
846 }
413a1c75 847 out:
1da177e4
LT
848 put_task_struct(tsk);
849}
850
b0dd25a8
RD
851/**
852 * audit_getname - add a name to the list
853 * @name: name to add
854 *
855 * Add a name to the list of audit names for this context.
856 * Called from fs/namei.c:getname().
857 */
1da177e4
LT
858void audit_getname(const char *name)
859{
860 struct audit_context *context = current->audit_context;
861
862 if (!context || IS_ERR(name) || !name)
863 return;
864
865 if (!context->in_syscall) {
866#if AUDIT_DEBUG == 2
867 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
868 __FILE__, __LINE__, context->serial, name);
869 dump_stack();
870#endif
871 return;
872 }
873 BUG_ON(context->name_count >= AUDIT_NAMES);
874 context->names[context->name_count].name = name;
875 context->names[context->name_count].ino = (unsigned long)-1;
876 ++context->name_count;
8f37d47c
DW
877 if (!context->pwd) {
878 read_lock(&current->fs->lock);
879 context->pwd = dget(current->fs->pwd);
880 context->pwdmnt = mntget(current->fs->pwdmnt);
881 read_unlock(&current->fs->lock);
882 }
883
1da177e4
LT
884}
885
b0dd25a8
RD
886/* audit_putname - intercept a putname request
887 * @name: name to intercept and delay for putname
888 *
889 * If we have stored the name from getname in the audit context,
890 * then we delay the putname until syscall exit.
891 * Called from include/linux/fs.h:putname().
892 */
1da177e4
LT
893void audit_putname(const char *name)
894{
895 struct audit_context *context = current->audit_context;
896
897 BUG_ON(!context);
898 if (!context->in_syscall) {
899#if AUDIT_DEBUG == 2
900 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
901 __FILE__, __LINE__, context->serial, name);
902 if (context->name_count) {
903 int i;
904 for (i = 0; i < context->name_count; i++)
905 printk(KERN_ERR "name[%d] = %p = %s\n", i,
906 context->names[i].name,
73241ccc 907 context->names[i].name ?: "(null)");
1da177e4
LT
908 }
909#endif
910 __putname(name);
911 }
912#if AUDIT_DEBUG
913 else {
914 ++context->put_count;
915 if (context->put_count > context->name_count) {
916 printk(KERN_ERR "%s:%d(:%d): major=%d"
917 " in_syscall=%d putname(%p) name_count=%d"
918 " put_count=%d\n",
919 __FILE__, __LINE__,
920 context->serial, context->major,
921 context->in_syscall, name, context->name_count,
922 context->put_count);
923 dump_stack();
924 }
925 }
926#endif
927}
928
8c8570fb
DK
929void audit_inode_context(int idx, const struct inode *inode)
930{
931 struct audit_context *context = current->audit_context;
7306a0b9 932 const char *suffix = security_inode_xattr_getsuffix();
8c8570fb
DK
933 char *ctx = NULL;
934 int len = 0;
935
7306a0b9
DK
936 if (!suffix)
937 goto ret;
8c8570fb 938
7306a0b9
DK
939 len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
940 if (len == -EOPNOTSUPP)
941 goto ret;
8c8570fb
DK
942 if (len < 0)
943 goto error_path;
944
945 ctx = kmalloc(len, GFP_KERNEL);
946 if (!ctx)
947 goto error_path;
948
7306a0b9 949 len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
8c8570fb
DK
950 if (len < 0)
951 goto error_path;
952
953 kfree(context->names[idx].ctx);
954 context->names[idx].ctx = ctx;
7306a0b9 955 goto ret;
8c8570fb
DK
956
957error_path:
958 if (ctx)
959 kfree(ctx);
960 audit_panic("error in audit_inode_context");
7306a0b9 961ret:
8c8570fb
DK
962 return;
963}
964
965
b0dd25a8
RD
966/**
967 * audit_inode - store the inode and device from a lookup
968 * @name: name being audited
969 * @inode: inode being audited
970 * @flags: lookup flags (as used in path_lookup())
971 *
972 * Called from fs/namei.c:path_lookup().
973 */
73241ccc 974void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1da177e4
LT
975{
976 int idx;
977 struct audit_context *context = current->audit_context;
978
979 if (!context->in_syscall)
980 return;
981 if (context->name_count
982 && context->names[context->name_count-1].name
983 && context->names[context->name_count-1].name == name)
984 idx = context->name_count - 1;
985 else if (context->name_count > 1
986 && context->names[context->name_count-2].name
987 && context->names[context->name_count-2].name == name)
988 idx = context->name_count - 2;
989 else {
990 /* FIXME: how much do we care about inodes that have no
991 * associated name? */
992 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
993 return;
994 idx = context->name_count++;
995 context->names[idx].name = NULL;
996#if AUDIT_DEBUG
997 ++context->ino_count;
998#endif
999 }
ae7b961b
DW
1000 context->names[idx].dev = inode->i_sb->s_dev;
1001 context->names[idx].mode = inode->i_mode;
1002 context->names[idx].uid = inode->i_uid;
1003 context->names[idx].gid = inode->i_gid;
1004 context->names[idx].rdev = inode->i_rdev;
8c8570fb 1005 audit_inode_context(idx, inode);
73241ccc
AG
1006 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1007 (strcmp(name, ".") != 0)) {
1008 context->names[idx].ino = (unsigned long)-1;
1009 context->names[idx].pino = inode->i_ino;
1010 } else {
1011 context->names[idx].ino = inode->i_ino;
1012 context->names[idx].pino = (unsigned long)-1;
1013 }
1014}
1015
1016/**
1017 * audit_inode_child - collect inode info for created/removed objects
1018 * @dname: inode's dentry name
1019 * @inode: inode being audited
1020 * @pino: inode number of dentry parent
1021 *
1022 * For syscalls that create or remove filesystem objects, audit_inode
1023 * can only collect information for the filesystem object's parent.
1024 * This call updates the audit context with the child's information.
1025 * Syscalls that create a new filesystem object must be hooked after
1026 * the object is created. Syscalls that remove a filesystem object
1027 * must be hooked prior, in order to capture the target inode during
1028 * unsuccessful attempts.
1029 */
1030void __audit_inode_child(const char *dname, const struct inode *inode,
1031 unsigned long pino)
1032{
1033 int idx;
1034 struct audit_context *context = current->audit_context;
1035
1036 if (!context->in_syscall)
1037 return;
1038
1039 /* determine matching parent */
1040 if (dname)
1041 for (idx = 0; idx < context->name_count; idx++)
1042 if (context->names[idx].pino == pino) {
1043 const char *n;
1044 const char *name = context->names[idx].name;
1045 int dlen = strlen(dname);
1046 int nlen = name ? strlen(name) : 0;
1047
1048 if (nlen < dlen)
1049 continue;
1050
1051 /* disregard trailing slashes */
1052 n = name + nlen - 1;
1053 while ((*n == '/') && (n > name))
1054 n--;
1055
1056 /* find last path component */
1057 n = n - dlen + 1;
1058 if (n < name)
1059 continue;
1060 else if (n > name) {
1061 if (*--n != '/')
1062 continue;
1063 else
1064 n++;
1065 }
1066
1067 if (strncmp(n, dname, dlen) == 0)
1068 goto update_context;
1069 }
1070
1071 /* catch-all in case match not found */
1072 idx = context->name_count++;
1073 context->names[idx].name = NULL;
1074 context->names[idx].pino = pino;
1075#if AUDIT_DEBUG
1076 context->ino_count++;
1077#endif
1078
1079update_context:
1080 if (inode) {
1081 context->names[idx].ino = inode->i_ino;
1082 context->names[idx].dev = inode->i_sb->s_dev;
1083 context->names[idx].mode = inode->i_mode;
1084 context->names[idx].uid = inode->i_uid;
1085 context->names[idx].gid = inode->i_gid;
1086 context->names[idx].rdev = inode->i_rdev;
8c8570fb 1087 audit_inode_context(idx, inode);
73241ccc 1088 }
1da177e4
LT
1089}
1090
b0dd25a8
RD
1091/**
1092 * auditsc_get_stamp - get local copies of audit_context values
1093 * @ctx: audit_context for the task
1094 * @t: timespec to store time recorded in the audit_context
1095 * @serial: serial value that is recorded in the audit_context
1096 *
1097 * Also sets the context as auditable.
1098 */
bfb4496e
DW
1099void auditsc_get_stamp(struct audit_context *ctx,
1100 struct timespec *t, unsigned int *serial)
1da177e4 1101{
ce625a80
DW
1102 if (!ctx->serial)
1103 ctx->serial = audit_serial();
bfb4496e
DW
1104 t->tv_sec = ctx->ctime.tv_sec;
1105 t->tv_nsec = ctx->ctime.tv_nsec;
1106 *serial = ctx->serial;
1107 ctx->auditable = 1;
1da177e4
LT
1108}
1109
b0dd25a8
RD
1110/**
1111 * audit_set_loginuid - set a task's audit_context loginuid
1112 * @task: task whose audit context is being modified
1113 * @loginuid: loginuid value
1114 *
1115 * Returns 0.
1116 *
1117 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1118 */
456be6cd 1119int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1da177e4 1120{
456be6cd 1121 if (task->audit_context) {
c0404993
SG
1122 struct audit_buffer *ab;
1123
9ad9ad38 1124 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
c0404993
SG
1125 if (ab) {
1126 audit_log_format(ab, "login pid=%d uid=%u "
326e9c8b 1127 "old auid=%u new auid=%u",
c0404993
SG
1128 task->pid, task->uid,
1129 task->audit_context->loginuid, loginuid);
1130 audit_log_end(ab);
1131 }
456be6cd 1132 task->audit_context->loginuid = loginuid;
1da177e4
LT
1133 }
1134 return 0;
1135}
1136
b0dd25a8
RD
1137/**
1138 * audit_get_loginuid - get the loginuid for an audit_context
1139 * @ctx: the audit_context
1140 *
1141 * Returns the context's loginuid or -1 if @ctx is NULL.
1142 */
1da177e4
LT
1143uid_t audit_get_loginuid(struct audit_context *ctx)
1144{
1145 return ctx ? ctx->loginuid : -1;
1146}
1147
8c8570fb
DK
1148static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1149{
1150 struct audit_context *context = current->audit_context;
1151 char *ctx = NULL;
1152 int len = 0;
1153
1154 if (likely(!context))
1155 return NULL;
1156
1157 len = security_ipc_getsecurity(ipcp, NULL, 0);
1158 if (len == -EOPNOTSUPP)
1159 goto ret;
1160 if (len < 0)
1161 goto error_path;
1162
1163 ctx = kmalloc(len, GFP_ATOMIC);
1164 if (!ctx)
1165 goto error_path;
1166
1167 len = security_ipc_getsecurity(ipcp, ctx, len);
1168 if (len < 0)
1169 goto error_path;
1170
1171 return ctx;
1172
1173error_path:
1174 kfree(ctx);
1175 audit_panic("error in audit_ipc_context");
1176ret:
1177 return NULL;
1178}
1179
b0dd25a8
RD
1180/**
1181 * audit_ipc_perms - record audit data for ipc
1182 * @qbytes: msgq bytes
1183 * @uid: msgq user id
1184 * @gid: msgq group id
1185 * @mode: msgq mode (permissions)
1186 *
1187 * Returns 0 for success or NULL context or < 0 on error.
1188 */
8c8570fb 1189int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
1da177e4
LT
1190{
1191 struct audit_aux_data_ipcctl *ax;
1192 struct audit_context *context = current->audit_context;
1193
1194 if (likely(!context))
1195 return 0;
1196
8c8570fb 1197 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1da177e4
LT
1198 if (!ax)
1199 return -ENOMEM;
1200
1201 ax->qbytes = qbytes;
1202 ax->uid = uid;
1203 ax->gid = gid;
1204 ax->mode = mode;
8c8570fb 1205 ax->ctx = audit_ipc_context(ipcp);
1da177e4 1206
c0404993 1207 ax->d.type = AUDIT_IPC;
1da177e4
LT
1208 ax->d.next = context->aux;
1209 context->aux = (void *)ax;
1210 return 0;
1211}
c2f0c7c3 1212
b0dd25a8
RD
1213/**
1214 * audit_socketcall - record audit data for sys_socketcall
1215 * @nargs: number of args
1216 * @args: args array
1217 *
1218 * Returns 0 for success or NULL context or < 0 on error.
1219 */
3ec3b2fb
DW
1220int audit_socketcall(int nargs, unsigned long *args)
1221{
1222 struct audit_aux_data_socketcall *ax;
1223 struct audit_context *context = current->audit_context;
1224
1225 if (likely(!context))
1226 return 0;
1227
1228 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1229 if (!ax)
1230 return -ENOMEM;
1231
1232 ax->nargs = nargs;
1233 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1234
1235 ax->d.type = AUDIT_SOCKETCALL;
1236 ax->d.next = context->aux;
1237 context->aux = (void *)ax;
1238 return 0;
1239}
1240
b0dd25a8
RD
1241/**
1242 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1243 * @len: data length in user space
1244 * @a: data address in kernel space
1245 *
1246 * Returns 0 for success or NULL context or < 0 on error.
1247 */
3ec3b2fb
DW
1248int audit_sockaddr(int len, void *a)
1249{
1250 struct audit_aux_data_sockaddr *ax;
1251 struct audit_context *context = current->audit_context;
1252
1253 if (likely(!context))
1254 return 0;
1255
1256 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1257 if (!ax)
1258 return -ENOMEM;
1259
1260 ax->len = len;
1261 memcpy(ax->a, a, len);
1262
1263 ax->d.type = AUDIT_SOCKADDR;
1264 ax->d.next = context->aux;
1265 context->aux = (void *)ax;
1266 return 0;
1267}
1268
b0dd25a8
RD
1269/**
1270 * audit_avc_path - record the granting or denial of permissions
1271 * @dentry: dentry to record
1272 * @mnt: mnt to record
1273 *
1274 * Returns 0 for success or NULL context or < 0 on error.
1275 *
1276 * Called from security/selinux/avc.c::avc_audit()
1277 */
01116105
SS
1278int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1279{
1280 struct audit_aux_data_path *ax;
1281 struct audit_context *context = current->audit_context;
1282
1283 if (likely(!context))
1284 return 0;
1285
1286 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1287 if (!ax)
1288 return -ENOMEM;
1289
1290 ax->dentry = dget(dentry);
1291 ax->mnt = mntget(mnt);
1292
1293 ax->d.type = AUDIT_AVC_PATH;
1294 ax->d.next = context->aux;
1295 context->aux = (void *)ax;
1296 return 0;
1297}
1298
b0dd25a8
RD
1299/**
1300 * audit_signal_info - record signal info for shutting down audit subsystem
1301 * @sig: signal value
1302 * @t: task being signaled
1303 *
1304 * If the audit subsystem is being terminated, record the task (pid)
1305 * and uid that is doing that.
1306 */
c2f0c7c3
SG
1307void audit_signal_info(int sig, struct task_struct *t)
1308{
1309 extern pid_t audit_sig_pid;
1310 extern uid_t audit_sig_uid;
c2f0c7c3 1311
582edda5 1312 if (unlikely(audit_pid && t->tgid == audit_pid)) {
c2f0c7c3
SG
1313 if (sig == SIGTERM || sig == SIGHUP) {
1314 struct audit_context *ctx = current->audit_context;
1315 audit_sig_pid = current->pid;
1316 if (ctx)
1317 audit_sig_uid = ctx->loginuid;
1318 else
1319 audit_sig_uid = current->uid;
1320 }
1321 }
1322}