[PATCH] kobject/hotplug split - kobject add/remove
[wandboard.git] / kernel / auditsc.c
blob6f1931381bc9eae1ff454c943036c5b077c4a8a6
1 /* auditsc.c -- System-call auditing support -*- linux-c -*-
2 * Handles all system-call specific auditing features.
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5 * All Rights Reserved.
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.
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.
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
21 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23 * Many of the ideas implemented here are from Stephen C. Tweedie,
24 * especially the idea of avoiding a copy by using getname.
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.
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>
38 #include <linux/audit.h>
39 #include <linux/personality.h>
40 #include <linux/time.h>
41 #include <asm/unistd.h>
43 /* 0 = no checking
44 1 = put_count checking
45 2 = verbose put_count checking
47 #define AUDIT_DEBUG 0
49 /* No syscall auditing will take place unless audit_enabled != 0. */
50 extern int audit_enabled;
52 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
53 * for saving names from getname(). */
54 #define AUDIT_NAMES 20
56 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
57 * audit_context from being used for nameless inodes from
58 * path_lookup. */
59 #define AUDIT_NAMES_RESERVED 7
61 /* At task start time, the audit_state is set in the audit_context using
62 a per-task filter. At syscall entry, the audit_state is augmented by
63 the syscall filter. */
64 enum audit_state {
65 AUDIT_DISABLED, /* Do not create per-task audit_context.
66 * No syscall-specific audit records can
67 * be generated. */
68 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
69 * but don't necessarily fill it in at
70 * syscall entry time (i.e., filter
71 * instead). */
72 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
73 * and always fill it in at syscall
74 * entry time. This makes a full
75 * syscall record available if some
76 * other part of the kernel decides it
77 * should be recorded. */
78 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
79 * always fill it in at syscall entry
80 * time, and always write out the audit
81 * record at syscall exit time. */
84 /* When fs/namei.c:getname() is called, we store the pointer in name and
85 * we don't let putname() free it (instead we free all of the saved
86 * pointers at syscall exit time).
88 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
89 struct audit_names {
90 const char *name;
91 unsigned long ino;
92 dev_t dev;
93 umode_t mode;
94 uid_t uid;
95 gid_t gid;
96 dev_t rdev;
99 struct audit_aux_data {
100 struct audit_aux_data *next;
101 int type;
104 #define AUDIT_AUX_IPCPERM 0
106 struct audit_aux_data_ipcctl {
107 struct audit_aux_data d;
108 struct ipc_perm p;
109 unsigned long qbytes;
110 uid_t uid;
111 gid_t gid;
112 mode_t mode;
116 /* The per-task audit context. */
117 struct audit_context {
118 int in_syscall; /* 1 if task is in a syscall */
119 enum audit_state state;
120 unsigned int serial; /* serial number for record */
121 struct timespec ctime; /* time of syscall entry */
122 uid_t loginuid; /* login uid (identity) */
123 int major; /* syscall number */
124 unsigned long argv[4]; /* syscall arguments */
125 int return_valid; /* return code is valid */
126 int return_code;/* syscall return code */
127 int auditable; /* 1 if record should be written */
128 int name_count;
129 struct audit_names names[AUDIT_NAMES];
130 struct audit_context *previous; /* For nested syscalls */
131 struct audit_aux_data *aux;
133 /* Save things to print about task_struct */
134 pid_t pid;
135 uid_t uid, euid, suid, fsuid;
136 gid_t gid, egid, sgid, fsgid;
137 unsigned long personality;
139 #if AUDIT_DEBUG
140 int put_count;
141 int ino_count;
142 #endif
145 /* Public API */
146 /* There are three lists of rules -- one to search at task creation
147 * time, one to search at syscall entry time, and another to search at
148 * syscall exit time. */
149 static LIST_HEAD(audit_tsklist);
150 static LIST_HEAD(audit_entlist);
151 static LIST_HEAD(audit_extlist);
153 struct audit_entry {
154 struct list_head list;
155 struct rcu_head rcu;
156 struct audit_rule rule;
159 /* Check to see if two rules are identical. It is called from
160 * audit_del_rule during AUDIT_DEL. */
161 static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
163 int i;
165 if (a->flags != b->flags)
166 return 1;
168 if (a->action != b->action)
169 return 1;
171 if (a->field_count != b->field_count)
172 return 1;
174 for (i = 0; i < a->field_count; i++) {
175 if (a->fields[i] != b->fields[i]
176 || a->values[i] != b->values[i])
177 return 1;
180 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
181 if (a->mask[i] != b->mask[i])
182 return 1;
184 return 0;
187 /* Note that audit_add_rule and audit_del_rule are called via
188 * audit_receive() in audit.c, and are protected by
189 * audit_netlink_sem. */
190 static inline int audit_add_rule(struct audit_entry *entry,
191 struct list_head *list)
193 if (entry->rule.flags & AUDIT_PREPEND) {
194 entry->rule.flags &= ~AUDIT_PREPEND;
195 list_add_rcu(&entry->list, list);
196 } else {
197 list_add_tail_rcu(&entry->list, list);
199 return 0;
202 static void audit_free_rule(struct rcu_head *head)
204 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
205 kfree(e);
208 /* Note that audit_add_rule and audit_del_rule are called via
209 * audit_receive() in audit.c, and are protected by
210 * audit_netlink_sem. */
211 static inline int audit_del_rule(struct audit_rule *rule,
212 struct list_head *list)
214 struct audit_entry *e;
216 /* Do not use the _rcu iterator here, since this is the only
217 * deletion routine. */
218 list_for_each_entry(e, list, list) {
219 if (!audit_compare_rule(rule, &e->rule)) {
220 list_del_rcu(&e->list);
221 call_rcu(&e->rcu, audit_free_rule);
222 return 0;
225 return -EFAULT; /* No matching rule */
228 #ifdef CONFIG_NET
229 /* Copy rule from user-space to kernel-space. Called during
230 * AUDIT_ADD. */
231 static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
233 int i;
235 if (s->action != AUDIT_NEVER
236 && s->action != AUDIT_POSSIBLE
237 && s->action != AUDIT_ALWAYS)
238 return -1;
239 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
240 return -1;
242 d->flags = s->flags;
243 d->action = s->action;
244 d->field_count = s->field_count;
245 for (i = 0; i < d->field_count; i++) {
246 d->fields[i] = s->fields[i];
247 d->values[i] = s->values[i];
249 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
250 return 0;
253 int audit_receive_filter(int type, int pid, int uid, int seq, void *data)
255 u32 flags;
256 struct audit_entry *entry;
257 int err = 0;
259 switch (type) {
260 case AUDIT_LIST:
261 /* The *_rcu iterators not needed here because we are
262 always called with audit_netlink_sem held. */
263 list_for_each_entry(entry, &audit_tsklist, list)
264 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
265 &entry->rule, sizeof(entry->rule));
266 list_for_each_entry(entry, &audit_entlist, list)
267 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
268 &entry->rule, sizeof(entry->rule));
269 list_for_each_entry(entry, &audit_extlist, list)
270 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
271 &entry->rule, sizeof(entry->rule));
272 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
273 break;
274 case AUDIT_ADD:
275 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
276 return -ENOMEM;
277 if (audit_copy_rule(&entry->rule, data)) {
278 kfree(entry);
279 return -EINVAL;
281 flags = entry->rule.flags;
282 if (!err && (flags & AUDIT_PER_TASK))
283 err = audit_add_rule(entry, &audit_tsklist);
284 if (!err && (flags & AUDIT_AT_ENTRY))
285 err = audit_add_rule(entry, &audit_entlist);
286 if (!err && (flags & AUDIT_AT_EXIT))
287 err = audit_add_rule(entry, &audit_extlist);
288 break;
289 case AUDIT_DEL:
290 flags =((struct audit_rule *)data)->flags;
291 if (!err && (flags & AUDIT_PER_TASK))
292 err = audit_del_rule(data, &audit_tsklist);
293 if (!err && (flags & AUDIT_AT_ENTRY))
294 err = audit_del_rule(data, &audit_entlist);
295 if (!err && (flags & AUDIT_AT_EXIT))
296 err = audit_del_rule(data, &audit_extlist);
297 break;
298 default:
299 return -EINVAL;
302 return err;
304 #endif
306 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
307 * otherwise. */
308 static int audit_filter_rules(struct task_struct *tsk,
309 struct audit_rule *rule,
310 struct audit_context *ctx,
311 enum audit_state *state)
313 int i, j;
315 for (i = 0; i < rule->field_count; i++) {
316 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
317 u32 value = rule->values[i];
318 int result = 0;
320 switch (field) {
321 case AUDIT_PID:
322 result = (tsk->pid == value);
323 break;
324 case AUDIT_UID:
325 result = (tsk->uid == value);
326 break;
327 case AUDIT_EUID:
328 result = (tsk->euid == value);
329 break;
330 case AUDIT_SUID:
331 result = (tsk->suid == value);
332 break;
333 case AUDIT_FSUID:
334 result = (tsk->fsuid == value);
335 break;
336 case AUDIT_GID:
337 result = (tsk->gid == value);
338 break;
339 case AUDIT_EGID:
340 result = (tsk->egid == value);
341 break;
342 case AUDIT_SGID:
343 result = (tsk->sgid == value);
344 break;
345 case AUDIT_FSGID:
346 result = (tsk->fsgid == value);
347 break;
348 case AUDIT_PERS:
349 result = (tsk->personality == value);
350 break;
352 case AUDIT_EXIT:
353 if (ctx && ctx->return_valid)
354 result = (ctx->return_code == value);
355 break;
356 case AUDIT_SUCCESS:
357 if (ctx && ctx->return_valid)
358 result = (ctx->return_code >= 0);
359 break;
360 case AUDIT_DEVMAJOR:
361 if (ctx) {
362 for (j = 0; j < ctx->name_count; j++) {
363 if (MAJOR(ctx->names[j].dev)==value) {
364 ++result;
365 break;
369 break;
370 case AUDIT_DEVMINOR:
371 if (ctx) {
372 for (j = 0; j < ctx->name_count; j++) {
373 if (MINOR(ctx->names[j].dev)==value) {
374 ++result;
375 break;
379 break;
380 case AUDIT_INODE:
381 if (ctx) {
382 for (j = 0; j < ctx->name_count; j++) {
383 if (ctx->names[j].ino == value) {
384 ++result;
385 break;
389 break;
390 case AUDIT_LOGINUID:
391 result = 0;
392 if (ctx)
393 result = (ctx->loginuid == value);
394 break;
395 case AUDIT_ARG0:
396 case AUDIT_ARG1:
397 case AUDIT_ARG2:
398 case AUDIT_ARG3:
399 if (ctx)
400 result = (ctx->argv[field-AUDIT_ARG0]==value);
401 break;
404 if (rule->fields[i] & AUDIT_NEGATE)
405 result = !result;
406 if (!result)
407 return 0;
409 switch (rule->action) {
410 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
411 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
412 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
414 return 1;
417 /* At process creation time, we can determine if system-call auditing is
418 * completely disabled for this task. Since we only have the task
419 * structure at this point, we can only check uid and gid.
421 static enum audit_state audit_filter_task(struct task_struct *tsk)
423 struct audit_entry *e;
424 enum audit_state state;
426 rcu_read_lock();
427 list_for_each_entry_rcu(e, &audit_tsklist, list) {
428 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
429 rcu_read_unlock();
430 return state;
433 rcu_read_unlock();
434 return AUDIT_BUILD_CONTEXT;
437 /* At syscall entry and exit time, this filter is called if the
438 * audit_state is not low enough that auditing cannot take place, but is
439 * also not high enough that we already know we have to write and audit
440 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
442 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
443 struct audit_context *ctx,
444 struct list_head *list)
446 struct audit_entry *e;
447 enum audit_state state;
448 int word = AUDIT_WORD(ctx->major);
449 int bit = AUDIT_BIT(ctx->major);
451 rcu_read_lock();
452 list_for_each_entry_rcu(e, list, list) {
453 if ((e->rule.mask[word] & bit) == bit
454 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
455 rcu_read_unlock();
456 return state;
459 rcu_read_unlock();
460 return AUDIT_BUILD_CONTEXT;
463 /* This should be called with task_lock() held. */
464 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
465 int return_valid,
466 int return_code)
468 struct audit_context *context = tsk->audit_context;
470 if (likely(!context))
471 return NULL;
472 context->return_valid = return_valid;
473 context->return_code = return_code;
475 if (context->in_syscall && !context->auditable) {
476 enum audit_state state;
477 state = audit_filter_syscall(tsk, context, &audit_extlist);
478 if (state == AUDIT_RECORD_CONTEXT)
479 context->auditable = 1;
482 context->pid = tsk->pid;
483 context->uid = tsk->uid;
484 context->gid = tsk->gid;
485 context->euid = tsk->euid;
486 context->suid = tsk->suid;
487 context->fsuid = tsk->fsuid;
488 context->egid = tsk->egid;
489 context->sgid = tsk->sgid;
490 context->fsgid = tsk->fsgid;
491 context->personality = tsk->personality;
492 tsk->audit_context = NULL;
493 return context;
496 static inline void audit_free_names(struct audit_context *context)
498 int i;
500 #if AUDIT_DEBUG == 2
501 if (context->auditable
502 ||context->put_count + context->ino_count != context->name_count) {
503 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
504 " name_count=%d put_count=%d"
505 " ino_count=%d [NOT freeing]\n",
506 __LINE__,
507 context->serial, context->major, context->in_syscall,
508 context->name_count, context->put_count,
509 context->ino_count);
510 for (i = 0; i < context->name_count; i++)
511 printk(KERN_ERR "names[%d] = %p = %s\n", i,
512 context->names[i].name,
513 context->names[i].name);
514 dump_stack();
515 return;
517 #endif
518 #if AUDIT_DEBUG
519 context->put_count = 0;
520 context->ino_count = 0;
521 #endif
523 for (i = 0; i < context->name_count; i++)
524 if (context->names[i].name)
525 __putname(context->names[i].name);
526 context->name_count = 0;
529 static inline void audit_free_aux(struct audit_context *context)
531 struct audit_aux_data *aux;
533 while ((aux = context->aux)) {
534 context->aux = aux->next;
535 kfree(aux);
539 static inline void audit_zero_context(struct audit_context *context,
540 enum audit_state state)
542 uid_t loginuid = context->loginuid;
544 memset(context, 0, sizeof(*context));
545 context->state = state;
546 context->loginuid = loginuid;
549 static inline struct audit_context *audit_alloc_context(enum audit_state state)
551 struct audit_context *context;
553 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
554 return NULL;
555 audit_zero_context(context, state);
556 return context;
559 /* Filter on the task information and allocate a per-task audit context
560 * if necessary. Doing so turns on system call auditing for the
561 * specified task. This is called from copy_process, so no lock is
562 * needed. */
563 int audit_alloc(struct task_struct *tsk)
565 struct audit_context *context;
566 enum audit_state state;
568 if (likely(!audit_enabled))
569 return 0; /* Return if not auditing. */
571 state = audit_filter_task(tsk);
572 if (likely(state == AUDIT_DISABLED))
573 return 0;
575 if (!(context = audit_alloc_context(state))) {
576 audit_log_lost("out of memory in audit_alloc");
577 return -ENOMEM;
580 /* Preserve login uid */
581 context->loginuid = -1;
582 if (current->audit_context)
583 context->loginuid = current->audit_context->loginuid;
585 tsk->audit_context = context;
586 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
587 return 0;
590 static inline void audit_free_context(struct audit_context *context)
592 struct audit_context *previous;
593 int count = 0;
595 do {
596 previous = context->previous;
597 if (previous || (count && count < 10)) {
598 ++count;
599 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
600 " freeing multiple contexts (%d)\n",
601 context->serial, context->major,
602 context->name_count, count);
604 audit_free_names(context);
605 audit_free_aux(context);
606 kfree(context);
607 context = previous;
608 } while (context);
609 if (count >= 10)
610 printk(KERN_ERR "audit: freed %d contexts\n", count);
613 static void audit_log_task_info(struct audit_buffer *ab)
615 char name[sizeof(current->comm)];
616 struct mm_struct *mm = current->mm;
617 struct vm_area_struct *vma;
619 get_task_comm(name, current);
620 audit_log_format(ab, " comm=%s", name);
622 if (!mm)
623 return;
625 down_read(&mm->mmap_sem);
626 vma = mm->mmap;
627 while (vma) {
628 if ((vma->vm_flags & VM_EXECUTABLE) &&
629 vma->vm_file) {
630 audit_log_d_path(ab, "exe=",
631 vma->vm_file->f_dentry,
632 vma->vm_file->f_vfsmnt);
633 break;
635 vma = vma->vm_next;
637 up_read(&mm->mmap_sem);
640 static void audit_log_exit(struct audit_context *context)
642 int i;
643 struct audit_buffer *ab;
645 ab = audit_log_start(context);
646 if (!ab)
647 return; /* audit_panic has been called */
648 audit_log_format(ab, "syscall=%d", context->major);
649 if (context->personality != PER_LINUX)
650 audit_log_format(ab, " per=%lx", context->personality);
651 if (context->return_valid)
652 audit_log_format(ab, " exit=%d", context->return_code);
653 audit_log_format(ab,
654 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
655 " pid=%d loginuid=%d uid=%d gid=%d"
656 " euid=%d suid=%d fsuid=%d"
657 " egid=%d sgid=%d fsgid=%d",
658 context->argv[0],
659 context->argv[1],
660 context->argv[2],
661 context->argv[3],
662 context->name_count,
663 context->pid,
664 context->loginuid,
665 context->uid,
666 context->gid,
667 context->euid, context->suid, context->fsuid,
668 context->egid, context->sgid, context->fsgid);
669 audit_log_task_info(ab);
670 audit_log_end(ab);
671 while (context->aux) {
672 struct audit_aux_data *aux;
674 ab = audit_log_start(context);
675 if (!ab)
676 continue; /* audit_panic has been called */
678 aux = context->aux;
679 context->aux = aux->next;
681 audit_log_format(ab, "auxitem=%d", aux->type);
682 switch (aux->type) {
683 case AUDIT_AUX_IPCPERM: {
684 struct audit_aux_data_ipcctl *axi = (void *)aux;
685 audit_log_format(ab,
686 " qbytes=%lx uid=%d gid=%d mode=%x",
687 axi->qbytes, axi->uid, axi->gid, axi->mode);
690 audit_log_end(ab);
691 kfree(aux);
694 for (i = 0; i < context->name_count; i++) {
695 ab = audit_log_start(context);
696 if (!ab)
697 continue; /* audit_panic has been called */
698 audit_log_format(ab, "item=%d", i);
699 if (context->names[i].name)
700 audit_log_format(ab, " name=%s",
701 context->names[i].name);
702 if (context->names[i].ino != (unsigned long)-1)
703 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
704 " uid=%d gid=%d rdev=%02x:%02x",
705 context->names[i].ino,
706 MAJOR(context->names[i].dev),
707 MINOR(context->names[i].dev),
708 context->names[i].mode,
709 context->names[i].uid,
710 context->names[i].gid,
711 MAJOR(context->names[i].rdev),
712 MINOR(context->names[i].rdev));
713 audit_log_end(ab);
717 /* Free a per-task audit context. Called from copy_process and
718 * __put_task_struct. */
719 void audit_free(struct task_struct *tsk)
721 struct audit_context *context;
723 task_lock(tsk);
724 context = audit_get_context(tsk, 0, 0);
725 task_unlock(tsk);
727 if (likely(!context))
728 return;
730 /* Check for system calls that do not go through the exit
731 * function (e.g., exit_group), then free context block. */
732 if (context->in_syscall && context->auditable)
733 audit_log_exit(context);
735 audit_free_context(context);
738 /* Compute a serial number for the audit record. Audit records are
739 * written to user-space as soon as they are generated, so a complete
740 * audit record may be written in several pieces. The timestamp of the
741 * record and this serial number are used by the user-space daemon to
742 * determine which pieces belong to the same audit record. The
743 * (timestamp,serial) tuple is unique for each syscall and is live from
744 * syscall entry to syscall exit.
746 * Atomic values are only guaranteed to be 24-bit, so we count down.
748 * NOTE: Another possibility is to store the formatted records off the
749 * audit context (for those records that have a context), and emit them
750 * all at syscall exit. However, this could delay the reporting of
751 * significant errors until syscall exit (or never, if the system
752 * halts). */
753 static inline unsigned int audit_serial(void)
755 static atomic_t serial = ATOMIC_INIT(0xffffff);
756 unsigned int a, b;
758 do {
759 a = atomic_read(&serial);
760 if (atomic_dec_and_test(&serial))
761 atomic_set(&serial, 0xffffff);
762 b = atomic_read(&serial);
763 } while (b != a - 1);
765 return 0xffffff - b;
768 /* Fill in audit context at syscall entry. This only happens if the
769 * audit context was created when the task was created and the state or
770 * filters demand the audit context be built. If the state from the
771 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
772 * then the record will be written at syscall exit time (otherwise, it
773 * will only be written if another part of the kernel requests that it
774 * be written). */
775 void audit_syscall_entry(struct task_struct *tsk, int major,
776 unsigned long a1, unsigned long a2,
777 unsigned long a3, unsigned long a4)
779 struct audit_context *context = tsk->audit_context;
780 enum audit_state state;
782 BUG_ON(!context);
784 /* This happens only on certain architectures that make system
785 * calls in kernel_thread via the entry.S interface, instead of
786 * with direct calls. (If you are porting to a new
787 * architecture, hitting this condition can indicate that you
788 * got the _exit/_leave calls backward in entry.S.)
790 * i386 no
791 * x86_64 no
792 * ppc64 yes (see arch/ppc64/kernel/misc.S)
794 * This also happens with vm86 emulation in a non-nested manner
795 * (entries without exits), so this case must be caught.
797 if (context->in_syscall) {
798 struct audit_context *newctx;
800 #if defined(__NR_vm86) && defined(__NR_vm86old)
801 /* vm86 mode should only be entered once */
802 if (major == __NR_vm86 || major == __NR_vm86old)
803 return;
804 #endif
805 #if AUDIT_DEBUG
806 printk(KERN_ERR
807 "audit(:%d) pid=%d in syscall=%d;"
808 " entering syscall=%d\n",
809 context->serial, tsk->pid, context->major, major);
810 #endif
811 newctx = audit_alloc_context(context->state);
812 if (newctx) {
813 newctx->previous = context;
814 context = newctx;
815 tsk->audit_context = newctx;
816 } else {
817 /* If we can't alloc a new context, the best we
818 * can do is to leak memory (any pending putname
819 * will be lost). The only other alternative is
820 * to abandon auditing. */
821 audit_zero_context(context, context->state);
824 BUG_ON(context->in_syscall || context->name_count);
826 if (!audit_enabled)
827 return;
829 context->major = major;
830 context->argv[0] = a1;
831 context->argv[1] = a2;
832 context->argv[2] = a3;
833 context->argv[3] = a4;
835 state = context->state;
836 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
837 state = audit_filter_syscall(tsk, context, &audit_entlist);
838 if (likely(state == AUDIT_DISABLED))
839 return;
841 context->serial = audit_serial();
842 context->ctime = CURRENT_TIME;
843 context->in_syscall = 1;
844 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
847 /* Tear down after system call. If the audit context has been marked as
848 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
849 * filtering, or because some other part of the kernel write an audit
850 * message), then write out the syscall information. In call cases,
851 * free the names stored from getname(). */
852 void audit_syscall_exit(struct task_struct *tsk, int return_code)
854 struct audit_context *context;
856 get_task_struct(tsk);
857 task_lock(tsk);
858 context = audit_get_context(tsk, 1, return_code);
859 task_unlock(tsk);
861 /* Not having a context here is ok, since the parent may have
862 * called __put_task_struct. */
863 if (likely(!context))
864 return;
866 if (context->in_syscall && context->auditable)
867 audit_log_exit(context);
869 context->in_syscall = 0;
870 context->auditable = 0;
871 if (context->previous) {
872 struct audit_context *new_context = context->previous;
873 context->previous = NULL;
874 audit_free_context(context);
875 tsk->audit_context = new_context;
876 } else {
877 audit_free_names(context);
878 audit_free_aux(context);
879 audit_zero_context(context, context->state);
880 tsk->audit_context = context;
882 put_task_struct(tsk);
885 /* Add a name to the list. Called from fs/namei.c:getname(). */
886 void audit_getname(const char *name)
888 struct audit_context *context = current->audit_context;
890 if (!context || IS_ERR(name) || !name)
891 return;
893 if (!context->in_syscall) {
894 #if AUDIT_DEBUG == 2
895 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
896 __FILE__, __LINE__, context->serial, name);
897 dump_stack();
898 #endif
899 return;
901 BUG_ON(context->name_count >= AUDIT_NAMES);
902 context->names[context->name_count].name = name;
903 context->names[context->name_count].ino = (unsigned long)-1;
904 ++context->name_count;
907 /* Intercept a putname request. Called from
908 * include/linux/fs.h:putname(). If we have stored the name from
909 * getname in the audit context, then we delay the putname until syscall
910 * exit. */
911 void audit_putname(const char *name)
913 struct audit_context *context = current->audit_context;
915 BUG_ON(!context);
916 if (!context->in_syscall) {
917 #if AUDIT_DEBUG == 2
918 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
919 __FILE__, __LINE__, context->serial, name);
920 if (context->name_count) {
921 int i;
922 for (i = 0; i < context->name_count; i++)
923 printk(KERN_ERR "name[%d] = %p = %s\n", i,
924 context->names[i].name,
925 context->names[i].name);
927 #endif
928 __putname(name);
930 #if AUDIT_DEBUG
931 else {
932 ++context->put_count;
933 if (context->put_count > context->name_count) {
934 printk(KERN_ERR "%s:%d(:%d): major=%d"
935 " in_syscall=%d putname(%p) name_count=%d"
936 " put_count=%d\n",
937 __FILE__, __LINE__,
938 context->serial, context->major,
939 context->in_syscall, name, context->name_count,
940 context->put_count);
941 dump_stack();
944 #endif
947 /* Store the inode and device from a lookup. Called from
948 * fs/namei.c:path_lookup(). */
949 void audit_inode(const char *name, const struct inode *inode)
951 int idx;
952 struct audit_context *context = current->audit_context;
954 if (!context->in_syscall)
955 return;
956 if (context->name_count
957 && context->names[context->name_count-1].name
958 && context->names[context->name_count-1].name == name)
959 idx = context->name_count - 1;
960 else if (context->name_count > 1
961 && context->names[context->name_count-2].name
962 && context->names[context->name_count-2].name == name)
963 idx = context->name_count - 2;
964 else {
965 /* FIXME: how much do we care about inodes that have no
966 * associated name? */
967 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
968 return;
969 idx = context->name_count++;
970 context->names[idx].name = NULL;
971 #if AUDIT_DEBUG
972 ++context->ino_count;
973 #endif
975 context->names[idx].ino = inode->i_ino;
976 context->names[idx].dev = inode->i_sb->s_dev;
977 context->names[idx].mode = inode->i_mode;
978 context->names[idx].uid = inode->i_uid;
979 context->names[idx].gid = inode->i_gid;
980 context->names[idx].rdev = inode->i_rdev;
983 void audit_get_stamp(struct audit_context *ctx,
984 struct timespec *t, int *serial)
986 if (ctx) {
987 t->tv_sec = ctx->ctime.tv_sec;
988 t->tv_nsec = ctx->ctime.tv_nsec;
989 *serial = ctx->serial;
990 ctx->auditable = 1;
991 } else {
992 *t = CURRENT_TIME;
993 *serial = 0;
997 extern int audit_set_type(struct audit_buffer *ab, int type);
999 int audit_set_loginuid(struct audit_context *ctx, uid_t loginuid)
1001 if (ctx) {
1002 struct audit_buffer *ab;
1004 ab = audit_log_start(NULL);
1005 if (ab) {
1006 audit_log_format(ab, "login pid=%d uid=%u "
1007 "old loginuid=%u new loginuid=%u",
1008 ctx->pid, ctx->uid, ctx->loginuid, loginuid);
1009 audit_set_type(ab, AUDIT_LOGIN);
1010 audit_log_end(ab);
1012 ctx->loginuid = loginuid;
1014 return 0;
1017 uid_t audit_get_loginuid(struct audit_context *ctx)
1019 return ctx ? ctx->loginuid : -1;
1022 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1024 struct audit_aux_data_ipcctl *ax;
1025 struct audit_context *context = current->audit_context;
1027 if (likely(!context))
1028 return 0;
1030 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1031 if (!ax)
1032 return -ENOMEM;
1034 ax->qbytes = qbytes;
1035 ax->uid = uid;
1036 ax->gid = gid;
1037 ax->mode = mode;
1039 ax->d.type = AUDIT_AUX_IPCPERM;
1040 ax->d.next = context->aux;
1041 context->aux = (void *)ax;
1042 return 0;