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