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