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