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