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