vserver 1.9.5.x5
[linux-2.6.git] / kernel / auditsc.c
1 /* auditsc.c -- System-call auditing support -*- linux-c -*-
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>
37
38 #include <linux/audit.h>
39 #include <linux/personality.h>
40 #include <linux/time.h>
41 #include <asm/unistd.h>
42
43 /* 0 = no checking
44    1 = put_count checking
45    2 = verbose put_count checking
46 */
47 #define AUDIT_DEBUG 0
48
49 /* No syscall auditing will take place unless audit_enabled != 0. */
50 extern int audit_enabled;
51
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
55
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
60
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.  */
82 };
83
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).
87  *
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           rdev;
93 };
94
95 /* The per-task audit context. */
96 struct audit_context {
97         int                 in_syscall; /* 1 if task is in a syscall */
98         enum audit_state    state;
99         unsigned int        serial;     /* serial number for record */
100         struct timespec     ctime;      /* time of syscall entry */
101         uid_t               loginuid;   /* login uid (identity) */
102         int                 major;      /* syscall number */
103         unsigned long       argv[4];    /* syscall arguments */
104         int                 return_valid; /* return code is valid */
105         int                 return_code;/* syscall return code */
106         int                 auditable;  /* 1 if record should be written */
107         int                 name_count;
108         struct audit_names  names[AUDIT_NAMES];
109         struct audit_context *previous; /* For nested syscalls */
110
111                                 /* Save things to print about task_struct */
112         pid_t               pid;
113         uid_t               uid, euid, suid, fsuid;
114         gid_t               gid, egid, sgid, fsgid;
115         unsigned long       personality;
116
117 #if AUDIT_DEBUG
118         int                 put_count;
119         int                 ino_count;
120 #endif
121 };
122
123                                 /* Public API */
124 /* There are three lists of rules -- one to search at task creation
125  * time, one to search at syscall entry time, and another to search at
126  * syscall exit time. */
127 static LIST_HEAD(audit_tsklist);
128 static LIST_HEAD(audit_entlist);
129 static LIST_HEAD(audit_extlist);
130
131 struct audit_entry {
132         struct list_head  list;
133         struct rcu_head   rcu;
134         struct audit_rule rule;
135 };
136
137 /* Check to see if two rules are identical.  It is called from
138  * audit_del_rule during AUDIT_DEL. */
139 static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
140 {
141         int i;
142
143         if (a->flags != b->flags)
144                 return 1;
145
146         if (a->action != b->action)
147                 return 1;
148
149         if (a->field_count != b->field_count)
150                 return 1;
151
152         for (i = 0; i < a->field_count; i++) {
153                 if (a->fields[i] != b->fields[i]
154                     || a->values[i] != b->values[i])
155                         return 1;
156         }
157
158         for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
159                 if (a->mask[i] != b->mask[i])
160                         return 1;
161
162         return 0;
163 }
164
165 /* Note that audit_add_rule and audit_del_rule are called via
166  * audit_receive() in audit.c, and are protected by
167  * audit_netlink_sem. */
168 static inline int audit_add_rule(struct audit_entry *entry,
169                                  struct list_head *list)
170 {
171         if (entry->rule.flags & AUDIT_PREPEND) {
172                 entry->rule.flags &= ~AUDIT_PREPEND;
173                 list_add_rcu(&entry->list, list);
174         } else {
175                 list_add_tail_rcu(&entry->list, list);
176         }
177         return 0;
178 }
179
180 static void audit_free_rule(struct rcu_head *head)
181 {
182         struct audit_entry *e = container_of(head, struct audit_entry, rcu);
183         kfree(e);
184 }
185
186 /* Note that audit_add_rule and audit_del_rule are called via
187  * audit_receive() in audit.c, and are protected by
188  * audit_netlink_sem. */
189 static inline int audit_del_rule(struct audit_rule *rule,
190                                  struct list_head *list)
191 {
192         struct audit_entry  *e;
193
194         /* Do not use the _rcu iterator here, since this is the only
195          * deletion routine. */
196         list_for_each_entry(e, list, list) {
197                 if (!audit_compare_rule(rule, &e->rule)) {
198                         list_del_rcu(&e->list);
199                         call_rcu(&e->rcu, audit_free_rule);
200                         return 0;
201                 }
202         }
203         return -EFAULT;         /* No matching rule */
204 }
205
206 #ifdef CONFIG_NET
207 /* Copy rule from user-space to kernel-space.  Called during
208  * AUDIT_ADD. */
209 static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
210 {
211         int i;
212
213         if (s->action != AUDIT_NEVER
214             && s->action != AUDIT_POSSIBLE
215             && s->action != AUDIT_ALWAYS)
216                 return -1;
217         if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
218                 return -1;
219
220         d->flags        = s->flags;
221         d->action       = s->action;
222         d->field_count  = s->field_count;
223         for (i = 0; i < d->field_count; i++) {
224                 d->fields[i] = s->fields[i];
225                 d->values[i] = s->values[i];
226         }
227         for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
228         return 0;
229 }
230
231 int audit_receive_filter(int type, int pid, int uid, int seq, void *data)
232 {
233         u32                flags;
234         struct audit_entry *entry;
235         int                err = 0;
236
237         switch (type) {
238         case AUDIT_LIST:
239                 /* The *_rcu iterators not needed here because we are
240                    always called with audit_netlink_sem held. */
241                 list_for_each_entry(entry, &audit_tsklist, list)
242                         audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
243                                          &entry->rule, sizeof(entry->rule));
244                 list_for_each_entry(entry, &audit_entlist, list)
245                         audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
246                                          &entry->rule, sizeof(entry->rule));
247                 list_for_each_entry(entry, &audit_extlist, list)
248                         audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
249                                          &entry->rule, sizeof(entry->rule));
250                 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
251                 break;
252         case AUDIT_ADD:
253                 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
254                         return -ENOMEM;
255                 if (audit_copy_rule(&entry->rule, data)) {
256                         kfree(entry);
257                         return -EINVAL;
258                 }
259                 flags = entry->rule.flags;
260                 if (!err && (flags & AUDIT_PER_TASK))
261                         err = audit_add_rule(entry, &audit_tsklist);
262                 if (!err && (flags & AUDIT_AT_ENTRY))
263                         err = audit_add_rule(entry, &audit_entlist);
264                 if (!err && (flags & AUDIT_AT_EXIT))
265                         err = audit_add_rule(entry, &audit_extlist);
266                 break;
267         case AUDIT_DEL:
268                 flags =((struct audit_rule *)data)->flags;
269                 if (!err && (flags & AUDIT_PER_TASK))
270                         err = audit_del_rule(data, &audit_tsklist);
271                 if (!err && (flags & AUDIT_AT_ENTRY))
272                         err = audit_del_rule(data, &audit_entlist);
273                 if (!err && (flags & AUDIT_AT_EXIT))
274                         err = audit_del_rule(data, &audit_extlist);
275                 break;
276         default:
277                 return -EINVAL;
278         }
279
280         return err;
281 }
282 #endif
283
284 /* Compare a task_struct with an audit_rule.  Return 1 on match, 0
285  * otherwise. */
286 static int audit_filter_rules(struct task_struct *tsk,
287                               struct audit_rule *rule,
288                               struct audit_context *ctx,
289                               enum audit_state *state)
290 {
291         int i, j;
292
293         for (i = 0; i < rule->field_count; i++) {
294                 u32 field  = rule->fields[i] & ~AUDIT_NEGATE;
295                 u32 value  = rule->values[i];
296                 int result = 0;
297
298                 switch (field) {
299                 case AUDIT_PID:
300                         result = (tsk->pid == value);
301                         break;
302                 case AUDIT_UID:
303                         result = (tsk->uid == value);
304                         break;
305                 case AUDIT_EUID:
306                         result = (tsk->euid == value);
307                         break;
308                 case AUDIT_SUID:
309                         result = (tsk->suid == value);
310                         break;
311                 case AUDIT_FSUID:
312                         result = (tsk->fsuid == value);
313                         break;
314                 case AUDIT_GID:
315                         result = (tsk->gid == value);
316                         break;
317                 case AUDIT_EGID:
318                         result = (tsk->egid == value);
319                         break;
320                 case AUDIT_SGID:
321                         result = (tsk->sgid == value);
322                         break;
323                 case AUDIT_FSGID:
324                         result = (tsk->fsgid == value);
325                         break;
326                 case AUDIT_PERS:
327                         result = (tsk->personality == value);
328                         break;
329
330                 case AUDIT_EXIT:
331                         if (ctx && ctx->return_valid)
332                                 result = (ctx->return_code == value);
333                         break;
334                 case AUDIT_SUCCESS:
335                         if (ctx && ctx->return_valid)
336                                 result = (ctx->return_code >= 0);
337                         break;
338                 case AUDIT_DEVMAJOR:
339                         if (ctx) {
340                                 for (j = 0; j < ctx->name_count; j++) {
341                                         if (MAJOR(ctx->names[j].rdev)==value) {
342                                                 ++result;
343                                                 break;
344                                         }
345                                 }
346                         }
347                         break;
348                 case AUDIT_DEVMINOR:
349                         if (ctx) {
350                                 for (j = 0; j < ctx->name_count; j++) {
351                                         if (MINOR(ctx->names[j].rdev)==value) {
352                                                 ++result;
353                                                 break;
354                                         }
355                                 }
356                         }
357                         break;
358                 case AUDIT_INODE:
359                         if (ctx) {
360                                 for (j = 0; j < ctx->name_count; j++) {
361                                         if (ctx->names[j].ino == value) {
362                                                 ++result;
363                                                 break;
364                                         }
365                                 }
366                         }
367                         break;
368                 case AUDIT_LOGINUID:
369                         result = 0;
370                         if (ctx)
371                                 result = (ctx->loginuid == value);
372                         break;
373                 case AUDIT_ARG0:
374                 case AUDIT_ARG1:
375                 case AUDIT_ARG2:
376                 case AUDIT_ARG3:
377                         if (ctx)
378                                 result = (ctx->argv[field-AUDIT_ARG0]==value);
379                         break;
380                 }
381
382                 if (rule->fields[i] & AUDIT_NEGATE)
383                         result = !result;
384                 if (!result)
385                         return 0;
386         }
387         switch (rule->action) {
388         case AUDIT_NEVER:    *state = AUDIT_DISABLED;       break;
389         case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT;  break;
390         case AUDIT_ALWAYS:   *state = AUDIT_RECORD_CONTEXT; break;
391         }
392         return 1;
393 }
394
395 /* At process creation time, we can determine if system-call auditing is
396  * completely disabled for this task.  Since we only have the task
397  * structure at this point, we can only check uid and gid.
398  */
399 static enum audit_state audit_filter_task(struct task_struct *tsk)
400 {
401         struct audit_entry *e;
402         enum audit_state   state;
403
404         rcu_read_lock();
405         list_for_each_entry_rcu(e, &audit_tsklist, list) {
406                 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
407                         rcu_read_unlock();
408                         return state;
409                 }
410         }
411         rcu_read_unlock();
412         return AUDIT_BUILD_CONTEXT;
413 }
414
415 /* At syscall entry and exit time, this filter is called if the
416  * audit_state is not low enough that auditing cannot take place, but is
417  * also not high enough that we already know we have to write and audit
418  * record (i.e., the state is AUDIT_SETUP_CONTEXT or  AUDIT_BUILD_CONTEXT).
419  */
420 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
421                                              struct audit_context *ctx,
422                                              struct list_head *list)
423 {
424         struct audit_entry *e;
425         enum audit_state   state;
426         int                word = AUDIT_WORD(ctx->major);
427         int                bit  = AUDIT_BIT(ctx->major);
428
429         rcu_read_lock();
430         list_for_each_entry_rcu(e, list, list) {
431                 if ((e->rule.mask[word] & bit) == bit
432                     && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
433                         rcu_read_unlock();
434                         return state;
435                 }
436         }
437         rcu_read_unlock();
438         return AUDIT_BUILD_CONTEXT;
439 }
440
441 /* This should be called with task_lock() held. */
442 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
443                                                       int return_valid,
444                                                       int return_code)
445 {
446         struct audit_context *context = tsk->audit_context;
447
448         if (likely(!context))
449                 return NULL;
450         context->return_valid = return_valid;
451         context->return_code  = return_code;
452
453         if (context->in_syscall && !context->auditable) {
454                 enum audit_state state;
455                 state = audit_filter_syscall(tsk, context, &audit_extlist);
456                 if (state == AUDIT_RECORD_CONTEXT)
457                         context->auditable = 1;
458         }
459
460         context->pid = tsk->pid;
461         context->uid = tsk->uid;
462         context->gid = tsk->gid;
463         context->euid = tsk->euid;
464         context->suid = tsk->suid;
465         context->fsuid = tsk->fsuid;
466         context->egid = tsk->egid;
467         context->sgid = tsk->sgid;
468         context->fsgid = tsk->fsgid;
469         context->personality = tsk->personality;
470         tsk->audit_context = NULL;
471         return context;
472 }
473
474 static inline void audit_free_names(struct audit_context *context)
475 {
476         int i;
477
478 #if AUDIT_DEBUG == 2
479         if (context->auditable
480             ||context->put_count + context->ino_count != context->name_count) {
481                 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
482                        " name_count=%d put_count=%d"
483                        " ino_count=%d [NOT freeing]\n",
484                        __LINE__,
485                        context->serial, context->major, context->in_syscall,
486                        context->name_count, context->put_count,
487                        context->ino_count);
488                 for (i = 0; i < context->name_count; i++)
489                         printk(KERN_ERR "names[%d] = %p = %s\n", i,
490                                context->names[i].name,
491                                context->names[i].name);
492                 dump_stack();
493                 return;
494         }
495 #endif
496 #if AUDIT_DEBUG
497         context->put_count  = 0;
498         context->ino_count  = 0;
499 #endif
500
501         for (i = 0; i < context->name_count; i++)
502                 if (context->names[i].name)
503                         __putname(context->names[i].name);
504         context->name_count = 0;
505 }
506
507 static inline void audit_zero_context(struct audit_context *context,
508                                       enum audit_state state)
509 {
510         uid_t loginuid = context->loginuid;
511
512         memset(context, 0, sizeof(*context));
513         context->state      = state;
514         context->loginuid   = loginuid;
515 }
516
517 static inline struct audit_context *audit_alloc_context(enum audit_state state)
518 {
519         struct audit_context *context;
520
521         if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
522                 return NULL;
523         audit_zero_context(context, state);
524         return context;
525 }
526
527 /* Filter on the task information and allocate a per-task audit context
528  * if necessary.  Doing so turns on system call auditing for the
529  * specified task.  This is called from copy_process, so no lock is
530  * needed. */
531 int audit_alloc(struct task_struct *tsk)
532 {
533         struct audit_context *context;
534         enum audit_state     state;
535
536         if (likely(!audit_enabled))
537                 return 0; /* Return if not auditing. */
538
539         state = audit_filter_task(tsk);
540         if (likely(state == AUDIT_DISABLED))
541                 return 0;
542
543         if (!(context = audit_alloc_context(state))) {
544                 audit_log_lost("out of memory in audit_alloc");
545                 return -ENOMEM;
546         }
547
548                                 /* Preserve login uid */
549         context->loginuid = -1;
550         if (current->audit_context)
551                 context->loginuid = current->audit_context->loginuid;
552
553         tsk->audit_context  = context;
554         set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
555         return 0;
556 }
557
558 static inline void audit_free_context(struct audit_context *context)
559 {
560         struct audit_context *previous;
561         int                  count = 0;
562
563         do {
564                 previous = context->previous;
565                 if (previous || (count &&  count < 10)) {
566                         ++count;
567                         printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
568                                " freeing multiple contexts (%d)\n",
569                                context->serial, context->major,
570                                context->name_count, count);
571                 }
572                 audit_free_names(context);
573                 kfree(context);
574                 context  = previous;
575         } while (context);
576         if (count >= 10)
577                 printk(KERN_ERR "audit: freed %d contexts\n", count);
578 }
579
580 static void audit_log_exit(struct audit_context *context)
581 {
582         int i;
583         struct audit_buffer *ab;
584
585         ab = audit_log_start(context);
586         if (!ab)
587                 return;         /* audit_panic has been called */
588         audit_log_format(ab, "syscall=%d", context->major);
589         if (context->personality != PER_LINUX)
590                 audit_log_format(ab, " per=%lx", context->personality);
591         if (context->return_valid)
592                 audit_log_format(ab, " exit=%d", context->return_code);
593         audit_log_format(ab,
594                   " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
595                   " pid=%d loginuid=%d uid=%d gid=%d"
596                   " euid=%d suid=%d fsuid=%d"
597                   " egid=%d sgid=%d fsgid=%d",
598                   context->argv[0],
599                   context->argv[1],
600                   context->argv[2],
601                   context->argv[3],
602                   context->name_count,
603                   context->pid,
604                   context->loginuid,
605                   context->uid,
606                   context->gid,
607                   context->euid, context->suid, context->fsuid,
608                   context->egid, context->sgid, context->fsgid);
609         audit_log_end(ab);
610         for (i = 0; i < context->name_count; i++) {
611                 ab = audit_log_start(context);
612                 if (!ab)
613                         continue; /* audit_panic has been called */
614                 audit_log_format(ab, "item=%d", i);
615                 if (context->names[i].name)
616                         audit_log_format(ab, " name=%s",
617                                          context->names[i].name);
618                 if (context->names[i].ino != (unsigned long)-1)
619                         audit_log_format(ab, " inode=%lu",
620                                          context->names[i].ino);
621                 /* FIXME: should use format_dev_t, but ab structure is
622                  * opaque. */
623                 if (context->names[i].rdev != -1)
624                         audit_log_format(ab, " dev=%02x:%02x",
625                                          MAJOR(context->names[i].rdev),
626                                          MINOR(context->names[i].rdev));
627                 audit_log_end(ab);
628         }
629 }
630
631 /* Free a per-task audit context.  Called from copy_process and
632  * __put_task_struct. */
633 void audit_free(struct task_struct *tsk)
634 {
635         struct audit_context *context;
636
637         task_lock(tsk);
638         context = audit_get_context(tsk, 0, 0);
639         task_unlock(tsk);
640
641         if (likely(!context))
642                 return;
643
644         /* Check for system calls that do not go through the exit
645          * function (e.g., exit_group), then free context block. */
646         if (context->in_syscall && context->auditable)
647                 audit_log_exit(context);
648
649         audit_free_context(context);
650 }
651
652 /* Compute a serial number for the audit record.  Audit records are
653  * written to user-space as soon as they are generated, so a complete
654  * audit record may be written in several pieces.  The timestamp of the
655  * record and this serial number are used by the user-space daemon to
656  * determine which pieces belong to the same audit record.  The
657  * (timestamp,serial) tuple is unique for each syscall and is live from
658  * syscall entry to syscall exit.
659  *
660  * Atomic values are only guaranteed to be 24-bit, so we count down.
661  *
662  * NOTE: Another possibility is to store the formatted records off the
663  * audit context (for those records that have a context), and emit them
664  * all at syscall exit.  However, this could delay the reporting of
665  * significant errors until syscall exit (or never, if the system
666  * halts). */
667 static inline unsigned int audit_serial(void)
668 {
669         static atomic_t serial = ATOMIC_INIT(0xffffff);
670         unsigned int a, b;
671
672         do {
673                 a = atomic_read(&serial);
674                 if (atomic_dec_and_test(&serial))
675                         atomic_set(&serial, 0xffffff);
676                 b = atomic_read(&serial);
677         } while (b != a - 1);
678
679         return 0xffffff - b;
680 }
681
682 /* Fill in audit context at syscall entry.  This only happens if the
683  * audit context was created when the task was created and the state or
684  * filters demand the audit context be built.  If the state from the
685  * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
686  * then the record will be written at syscall exit time (otherwise, it
687  * will only be written if another part of the kernel requests that it
688  * be written). */
689 void audit_syscall_entry(struct task_struct *tsk, int major,
690                          unsigned long a1, unsigned long a2,
691                          unsigned long a3, unsigned long a4)
692 {
693         struct audit_context *context = tsk->audit_context;
694         enum audit_state     state;
695
696         BUG_ON(!context);
697
698         /* This happens only on certain architectures that make system
699          * calls in kernel_thread via the entry.S interface, instead of
700          * with direct calls.  (If you are porting to a new
701          * architecture, hitting this condition can indicate that you
702          * got the _exit/_leave calls backward in entry.S.)
703          *
704          * i386     no
705          * x86_64   no
706          * ppc64    yes (see arch/ppc64/kernel/misc.S)
707          *
708          * This also happens with vm86 emulation in a non-nested manner
709          * (entries without exits), so this case must be caught.
710          */
711         if (context->in_syscall) {
712                 struct audit_context *newctx;
713
714 #if defined(__NR_vm86) && defined(__NR_vm86old)
715                 /* vm86 mode should only be entered once */
716                 if (major == __NR_vm86 || major == __NR_vm86old)
717                         return;
718 #endif
719 #if AUDIT_DEBUG
720                 printk(KERN_ERR
721                        "audit(:%d) pid=%d in syscall=%d;"
722                        " entering syscall=%d\n",
723                        context->serial, tsk->pid, context->major, major);
724 #endif
725                 newctx = audit_alloc_context(context->state);
726                 if (newctx) {
727                         newctx->previous   = context;
728                         context            = newctx;
729                         tsk->audit_context = newctx;
730                 } else  {
731                         /* If we can't alloc a new context, the best we
732                          * can do is to leak memory (any pending putname
733                          * will be lost).  The only other alternative is
734                          * to abandon auditing. */
735                         audit_zero_context(context, context->state);
736                 }
737         }
738         BUG_ON(context->in_syscall || context->name_count);
739
740         if (!audit_enabled)
741                 return;
742
743         context->major      = major;
744         context->argv[0]    = a1;
745         context->argv[1]    = a2;
746         context->argv[2]    = a3;
747         context->argv[3]    = a4;
748
749         state = context->state;
750         if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
751                 state = audit_filter_syscall(tsk, context, &audit_entlist);
752         if (likely(state == AUDIT_DISABLED))
753                 return;
754
755         context->serial     = audit_serial();
756         context->ctime      = CURRENT_TIME;
757         context->in_syscall = 1;
758         context->auditable  = !!(state == AUDIT_RECORD_CONTEXT);
759 }
760
761 /* Tear down after system call.  If the audit context has been marked as
762  * auditable (either because of the AUDIT_RECORD_CONTEXT state from
763  * filtering, or because some other part of the kernel write an audit
764  * message), then write out the syscall information.  In call cases,
765  * free the names stored from getname(). */
766 void audit_syscall_exit(struct task_struct *tsk, int return_code)
767 {
768         struct audit_context *context;
769
770         get_task_struct(tsk);
771         task_lock(tsk);
772         context = audit_get_context(tsk, 1, return_code);
773         task_unlock(tsk);
774
775         /* Not having a context here is ok, since the parent may have
776          * called __put_task_struct. */
777         if (likely(!context))
778                 return;
779
780         if (context->in_syscall && context->auditable)
781                 audit_log_exit(context);
782
783         context->in_syscall = 0;
784         context->auditable  = 0;
785         if (context->previous) {
786                 struct audit_context *new_context = context->previous;
787                 context->previous  = NULL;
788                 audit_free_context(context);
789                 tsk->audit_context = new_context;
790         } else {
791                 audit_free_names(context);
792                 audit_zero_context(context, context->state);
793                 tsk->audit_context = context;
794         }
795         put_task_struct(tsk);
796 }
797
798 /* Add a name to the list.  Called from fs/namei.c:getname(). */
799 void audit_getname(const char *name)
800 {
801         struct audit_context *context = current->audit_context;
802
803         BUG_ON(!context);
804         if (!context->in_syscall) {
805 #if AUDIT_DEBUG == 2
806                 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
807                        __FILE__, __LINE__, context->serial, name);
808                 dump_stack();
809 #endif
810                 return;
811         }
812         BUG_ON(context->name_count >= AUDIT_NAMES);
813         context->names[context->name_count].name = name;
814         context->names[context->name_count].ino  = (unsigned long)-1;
815         context->names[context->name_count].rdev = -1;
816         ++context->name_count;
817 }
818
819 /* Intercept a putname request.  Called from
820  * include/linux/fs.h:putname().  If we have stored the name from
821  * getname in the audit context, then we delay the putname until syscall
822  * exit. */
823 void audit_putname(const char *name)
824 {
825         struct audit_context *context = current->audit_context;
826
827         BUG_ON(!context);
828         if (!context->in_syscall) {
829 #if AUDIT_DEBUG == 2
830                 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
831                        __FILE__, __LINE__, context->serial, name);
832                 if (context->name_count) {
833                         int i;
834                         for (i = 0; i < context->name_count; i++)
835                                 printk(KERN_ERR "name[%d] = %p = %s\n", i,
836                                        context->names[i].name,
837                                        context->names[i].name);
838                 }
839 #endif
840                 __putname(name);
841         }
842 #if AUDIT_DEBUG
843         else {
844                 ++context->put_count;
845                 if (context->put_count > context->name_count) {
846                         printk(KERN_ERR "%s:%d(:%d): major=%d"
847                                " in_syscall=%d putname(%p) name_count=%d"
848                                " put_count=%d\n",
849                                __FILE__, __LINE__,
850                                context->serial, context->major,
851                                context->in_syscall, name, context->name_count,
852                                context->put_count);
853                         dump_stack();
854                 }
855         }
856 #endif
857 }
858 EXPORT_SYMBOL(audit_putname);
859
860 /* Store the inode and device from a lookup.  Called from
861  * fs/namei.c:path_lookup(). */
862 void audit_inode(const char *name, unsigned long ino, dev_t rdev)
863 {
864         int idx;
865         struct audit_context *context = current->audit_context;
866
867         if (!context->in_syscall)
868                 return;
869         if (context->name_count
870             && context->names[context->name_count-1].name
871             && context->names[context->name_count-1].name == name)
872                 idx = context->name_count - 1;
873         else if (context->name_count > 1
874                  && context->names[context->name_count-2].name
875                  && context->names[context->name_count-2].name == name)
876                 idx = context->name_count - 2;
877         else {
878                 /* FIXME: how much do we care about inodes that have no
879                  * associated name? */
880                 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
881                         return;
882                 idx = context->name_count++;
883                 context->names[idx].name = NULL;
884 #if AUDIT_DEBUG
885                 ++context->ino_count;
886 #endif
887         }
888         context->names[idx].ino  = ino;
889         context->names[idx].rdev = rdev;
890 }
891
892 void audit_get_stamp(struct audit_context *ctx,
893                      struct timespec *t, int *serial)
894 {
895         if (ctx) {
896                 t->tv_sec  = ctx->ctime.tv_sec;
897                 t->tv_nsec = ctx->ctime.tv_nsec;
898                 *serial    = ctx->serial;
899                 ctx->auditable = 1;
900         } else {
901                 *t      = CURRENT_TIME;
902                 *serial = 0;
903         }
904 }
905
906 extern int audit_set_type(struct audit_buffer *ab, int type);
907
908 int audit_set_loginuid(struct audit_context *ctx, uid_t loginuid)
909 {
910         if (ctx) {
911                 struct audit_buffer *ab;
912
913                 ab = audit_log_start(NULL);
914                 if (ab) {
915                         audit_log_format(ab, "login pid=%d uid=%u "
916                                 "old loginuid=%u new loginuid=%u",
917                                 ctx->pid, ctx->uid, ctx->loginuid, loginuid);
918                         audit_set_type(ab, AUDIT_LOGIN);
919                         audit_log_end(ab);
920                 }
921                 ctx->loginuid = loginuid;
922         }
923         return 0;
924 }
925
926 uid_t audit_get_loginuid(struct audit_context *ctx)
927 {
928         return ctx ? ctx->loginuid : -1;
929 }