[PATCH] log ppid
[safe/jmp/linux-2.6] / 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  * Copyright 2005 Hewlett-Packard Development Company, L.P.
6  * Copyright (C) 2005 IBM Corporation
7  * All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24  *
25  * Many of the ideas implemented here are from Stephen C. Tweedie,
26  * especially the idea of avoiding a copy by using getname.
27  *
28  * The method for actual interception of syscall entry and exit (not in
29  * this file -- see entry.S) is based on a GPL'd patch written by
30  * okir@suse.de and Copyright 2003 SuSE Linux AG.
31  *
32  * The support of additional filter rules compares (>, <, >=, <=) was
33  * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
34  *
35  * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36  * filesystem information.
37  *
38  * Subject and object context labeling support added by <danjones@us.ibm.com>
39  * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
40  */
41
42 #include <linux/init.h>
43 #include <asm/types.h>
44 #include <asm/atomic.h>
45 #include <asm/types.h>
46 #include <linux/fs.h>
47 #include <linux/namei.h>
48 #include <linux/mm.h>
49 #include <linux/module.h>
50 #include <linux/mount.h>
51 #include <linux/socket.h>
52 #include <linux/audit.h>
53 #include <linux/personality.h>
54 #include <linux/time.h>
55 #include <linux/netlink.h>
56 #include <linux/compiler.h>
57 #include <asm/unistd.h>
58 #include <linux/security.h>
59 #include <linux/list.h>
60 #include <linux/tty.h>
61 #include <linux/selinux.h>
62 #include <linux/binfmts.h>
63 #include <linux/syscalls.h>
64
65 #include "audit.h"
66
67 extern struct list_head audit_filter_list[];
68
69 /* No syscall auditing will take place unless audit_enabled != 0. */
70 extern int audit_enabled;
71
72 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
73  * for saving names from getname(). */
74 #define AUDIT_NAMES    20
75
76 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
77  * audit_context from being used for nameless inodes from
78  * path_lookup. */
79 #define AUDIT_NAMES_RESERVED 7
80
81 /* When fs/namei.c:getname() is called, we store the pointer in name and
82  * we don't let putname() free it (instead we free all of the saved
83  * pointers at syscall exit time).
84  *
85  * Further, in fs/namei.c:path_lookup() we store the inode and device. */
86 struct audit_names {
87         const char      *name;
88         unsigned long   ino;
89         unsigned long   pino;
90         dev_t           dev;
91         umode_t         mode;
92         uid_t           uid;
93         gid_t           gid;
94         dev_t           rdev;
95         u32             osid;
96 };
97
98 struct audit_aux_data {
99         struct audit_aux_data   *next;
100         int                     type;
101 };
102
103 #define AUDIT_AUX_IPCPERM       0
104
105 struct audit_aux_data_ipcctl {
106         struct audit_aux_data   d;
107         struct ipc_perm         p;
108         unsigned long           qbytes;
109         uid_t                   uid;
110         gid_t                   gid;
111         mode_t                  mode;
112         u32                     osid;
113 };
114
115 struct audit_aux_data_execve {
116         struct audit_aux_data   d;
117         int argc;
118         int envc;
119         char mem[0];
120 };
121
122 struct audit_aux_data_socketcall {
123         struct audit_aux_data   d;
124         int                     nargs;
125         unsigned long           args[0];
126 };
127
128 struct audit_aux_data_sockaddr {
129         struct audit_aux_data   d;
130         int                     len;
131         char                    a[0];
132 };
133
134 struct audit_aux_data_path {
135         struct audit_aux_data   d;
136         struct dentry           *dentry;
137         struct vfsmount         *mnt;
138 };
139
140 /* The per-task audit context. */
141 struct audit_context {
142         int                 in_syscall; /* 1 if task is in a syscall */
143         enum audit_state    state;
144         unsigned int        serial;     /* serial number for record */
145         struct timespec     ctime;      /* time of syscall entry */
146         uid_t               loginuid;   /* login uid (identity) */
147         int                 major;      /* syscall number */
148         unsigned long       argv[4];    /* syscall arguments */
149         int                 return_valid; /* return code is valid */
150         long                return_code;/* syscall return code */
151         int                 auditable;  /* 1 if record should be written */
152         int                 name_count;
153         struct audit_names  names[AUDIT_NAMES];
154         struct dentry *     pwd;
155         struct vfsmount *   pwdmnt;
156         struct audit_context *previous; /* For nested syscalls */
157         struct audit_aux_data *aux;
158
159                                 /* Save things to print about task_struct */
160         pid_t               pid, ppid;
161         uid_t               uid, euid, suid, fsuid;
162         gid_t               gid, egid, sgid, fsgid;
163         unsigned long       personality;
164         int                 arch;
165
166 #if AUDIT_DEBUG
167         int                 put_count;
168         int                 ino_count;
169 #endif
170 };
171
172
173 /* Compare a task_struct with an audit_rule.  Return 1 on match, 0
174  * otherwise. */
175 static int audit_filter_rules(struct task_struct *tsk,
176                               struct audit_krule *rule,
177                               struct audit_context *ctx,
178                               enum audit_state *state)
179 {
180         int i, j, need_sid = 1;
181         u32 sid;
182
183         for (i = 0; i < rule->field_count; i++) {
184                 struct audit_field *f = &rule->fields[i];
185                 int result = 0;
186
187                 switch (f->type) {
188                 case AUDIT_PID:
189                         result = audit_comparator(tsk->pid, f->op, f->val);
190                         break;
191                 case AUDIT_UID:
192                         result = audit_comparator(tsk->uid, f->op, f->val);
193                         break;
194                 case AUDIT_EUID:
195                         result = audit_comparator(tsk->euid, f->op, f->val);
196                         break;
197                 case AUDIT_SUID:
198                         result = audit_comparator(tsk->suid, f->op, f->val);
199                         break;
200                 case AUDIT_FSUID:
201                         result = audit_comparator(tsk->fsuid, f->op, f->val);
202                         break;
203                 case AUDIT_GID:
204                         result = audit_comparator(tsk->gid, f->op, f->val);
205                         break;
206                 case AUDIT_EGID:
207                         result = audit_comparator(tsk->egid, f->op, f->val);
208                         break;
209                 case AUDIT_SGID:
210                         result = audit_comparator(tsk->sgid, f->op, f->val);
211                         break;
212                 case AUDIT_FSGID:
213                         result = audit_comparator(tsk->fsgid, f->op, f->val);
214                         break;
215                 case AUDIT_PERS:
216                         result = audit_comparator(tsk->personality, f->op, f->val);
217                         break;
218                 case AUDIT_ARCH:
219                         if (ctx)
220                                 result = audit_comparator(ctx->arch, f->op, f->val);
221                         break;
222
223                 case AUDIT_EXIT:
224                         if (ctx && ctx->return_valid)
225                                 result = audit_comparator(ctx->return_code, f->op, f->val);
226                         break;
227                 case AUDIT_SUCCESS:
228                         if (ctx && ctx->return_valid) {
229                                 if (f->val)
230                                         result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
231                                 else
232                                         result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
233                         }
234                         break;
235                 case AUDIT_DEVMAJOR:
236                         if (ctx) {
237                                 for (j = 0; j < ctx->name_count; j++) {
238                                         if (audit_comparator(MAJOR(ctx->names[j].dev),  f->op, f->val)) {
239                                                 ++result;
240                                                 break;
241                                         }
242                                 }
243                         }
244                         break;
245                 case AUDIT_DEVMINOR:
246                         if (ctx) {
247                                 for (j = 0; j < ctx->name_count; j++) {
248                                         if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
249                                                 ++result;
250                                                 break;
251                                         }
252                                 }
253                         }
254                         break;
255                 case AUDIT_INODE:
256                         if (ctx) {
257                                 for (j = 0; j < ctx->name_count; j++) {
258                                         if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
259                                             audit_comparator(ctx->names[j].pino, f->op, f->val)) {
260                                                 ++result;
261                                                 break;
262                                         }
263                                 }
264                         }
265                         break;
266                 case AUDIT_LOGINUID:
267                         result = 0;
268                         if (ctx)
269                                 result = audit_comparator(ctx->loginuid, f->op, f->val);
270                         break;
271                 case AUDIT_SE_USER:
272                 case AUDIT_SE_ROLE:
273                 case AUDIT_SE_TYPE:
274                 case AUDIT_SE_SEN:
275                 case AUDIT_SE_CLR:
276                         /* NOTE: this may return negative values indicating
277                            a temporary error.  We simply treat this as a
278                            match for now to avoid losing information that
279                            may be wanted.   An error message will also be
280                            logged upon error */
281                         if (f->se_rule) {
282                                 if (need_sid) {
283                                         selinux_task_ctxid(tsk, &sid);
284                                         need_sid = 0;
285                                 }
286                                 result = selinux_audit_rule_match(sid, f->type,
287                                                                   f->op,
288                                                                   f->se_rule,
289                                                                   ctx);
290                         }
291                         break;
292                 case AUDIT_ARG0:
293                 case AUDIT_ARG1:
294                 case AUDIT_ARG2:
295                 case AUDIT_ARG3:
296                         if (ctx)
297                                 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
298                         break;
299                 }
300
301                 if (!result)
302                         return 0;
303         }
304         switch (rule->action) {
305         case AUDIT_NEVER:    *state = AUDIT_DISABLED;       break;
306         case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT;  break;
307         case AUDIT_ALWAYS:   *state = AUDIT_RECORD_CONTEXT; break;
308         }
309         return 1;
310 }
311
312 /* At process creation time, we can determine if system-call auditing is
313  * completely disabled for this task.  Since we only have the task
314  * structure at this point, we can only check uid and gid.
315  */
316 static enum audit_state audit_filter_task(struct task_struct *tsk)
317 {
318         struct audit_entry *e;
319         enum audit_state   state;
320
321         rcu_read_lock();
322         list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
323                 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
324                         rcu_read_unlock();
325                         return state;
326                 }
327         }
328         rcu_read_unlock();
329         return AUDIT_BUILD_CONTEXT;
330 }
331
332 /* At syscall entry and exit time, this filter is called if the
333  * audit_state is not low enough that auditing cannot take place, but is
334  * also not high enough that we already know we have to write an audit
335  * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
336  */
337 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
338                                              struct audit_context *ctx,
339                                              struct list_head *list)
340 {
341         struct audit_entry *e;
342         enum audit_state state;
343
344         if (audit_pid && tsk->tgid == audit_pid)
345                 return AUDIT_DISABLED;
346
347         rcu_read_lock();
348         if (!list_empty(list)) {
349                 int word = AUDIT_WORD(ctx->major);
350                 int bit  = AUDIT_BIT(ctx->major);
351
352                 list_for_each_entry_rcu(e, list, list) {
353                         if ((e->rule.mask[word] & bit) == bit
354                                         && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
355                                 rcu_read_unlock();
356                                 return state;
357                         }
358                 }
359         }
360         rcu_read_unlock();
361         return AUDIT_BUILD_CONTEXT;
362 }
363
364 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
365                                                       int return_valid,
366                                                       int return_code)
367 {
368         struct audit_context *context = tsk->audit_context;
369
370         if (likely(!context))
371                 return NULL;
372         context->return_valid = return_valid;
373         context->return_code  = return_code;
374
375         if (context->in_syscall && !context->auditable) {
376                 enum audit_state state;
377                 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
378                 if (state == AUDIT_RECORD_CONTEXT)
379                         context->auditable = 1;
380         }
381
382         context->pid = tsk->pid;
383         context->ppid = sys_getppid();  /* sic.  tsk == current in all cases */
384         context->uid = tsk->uid;
385         context->gid = tsk->gid;
386         context->euid = tsk->euid;
387         context->suid = tsk->suid;
388         context->fsuid = tsk->fsuid;
389         context->egid = tsk->egid;
390         context->sgid = tsk->sgid;
391         context->fsgid = tsk->fsgid;
392         context->personality = tsk->personality;
393         tsk->audit_context = NULL;
394         return context;
395 }
396
397 static inline void audit_free_names(struct audit_context *context)
398 {
399         int i;
400
401 #if AUDIT_DEBUG == 2
402         if (context->auditable
403             ||context->put_count + context->ino_count != context->name_count) {
404                 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
405                        " name_count=%d put_count=%d"
406                        " ino_count=%d [NOT freeing]\n",
407                        __FILE__, __LINE__,
408                        context->serial, context->major, context->in_syscall,
409                        context->name_count, context->put_count,
410                        context->ino_count);
411                 for (i = 0; i < context->name_count; i++) {
412                         printk(KERN_ERR "names[%d] = %p = %s\n", i,
413                                context->names[i].name,
414                                context->names[i].name ?: "(null)");
415                 }
416                 dump_stack();
417                 return;
418         }
419 #endif
420 #if AUDIT_DEBUG
421         context->put_count  = 0;
422         context->ino_count  = 0;
423 #endif
424
425         for (i = 0; i < context->name_count; i++) {
426                 if (context->names[i].name)
427                         __putname(context->names[i].name);
428         }
429         context->name_count = 0;
430         if (context->pwd)
431                 dput(context->pwd);
432         if (context->pwdmnt)
433                 mntput(context->pwdmnt);
434         context->pwd = NULL;
435         context->pwdmnt = NULL;
436 }
437
438 static inline void audit_free_aux(struct audit_context *context)
439 {
440         struct audit_aux_data *aux;
441
442         while ((aux = context->aux)) {
443                 if (aux->type == AUDIT_AVC_PATH) {
444                         struct audit_aux_data_path *axi = (void *)aux;
445                         dput(axi->dentry);
446                         mntput(axi->mnt);
447                 }
448
449                 context->aux = aux->next;
450                 kfree(aux);
451         }
452 }
453
454 static inline void audit_zero_context(struct audit_context *context,
455                                       enum audit_state state)
456 {
457         uid_t loginuid = context->loginuid;
458
459         memset(context, 0, sizeof(*context));
460         context->state      = state;
461         context->loginuid   = loginuid;
462 }
463
464 static inline struct audit_context *audit_alloc_context(enum audit_state state)
465 {
466         struct audit_context *context;
467
468         if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
469                 return NULL;
470         audit_zero_context(context, state);
471         return context;
472 }
473
474 /**
475  * audit_alloc - allocate an audit context block for a task
476  * @tsk: task
477  *
478  * Filter on the task information and allocate a per-task audit context
479  * if necessary.  Doing so turns on system call auditing for the
480  * specified task.  This is called from copy_process, so no lock is
481  * needed.
482  */
483 int audit_alloc(struct task_struct *tsk)
484 {
485         struct audit_context *context;
486         enum audit_state     state;
487
488         if (likely(!audit_enabled))
489                 return 0; /* Return if not auditing. */
490
491         state = audit_filter_task(tsk);
492         if (likely(state == AUDIT_DISABLED))
493                 return 0;
494
495         if (!(context = audit_alloc_context(state))) {
496                 audit_log_lost("out of memory in audit_alloc");
497                 return -ENOMEM;
498         }
499
500                                 /* Preserve login uid */
501         context->loginuid = -1;
502         if (current->audit_context)
503                 context->loginuid = current->audit_context->loginuid;
504
505         tsk->audit_context  = context;
506         set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
507         return 0;
508 }
509
510 static inline void audit_free_context(struct audit_context *context)
511 {
512         struct audit_context *previous;
513         int                  count = 0;
514
515         do {
516                 previous = context->previous;
517                 if (previous || (count &&  count < 10)) {
518                         ++count;
519                         printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
520                                " freeing multiple contexts (%d)\n",
521                                context->serial, context->major,
522                                context->name_count, count);
523                 }
524                 audit_free_names(context);
525                 audit_free_aux(context);
526                 kfree(context);
527                 context  = previous;
528         } while (context);
529         if (count >= 10)
530                 printk(KERN_ERR "audit: freed %d contexts\n", count);
531 }
532
533 static void audit_log_task_context(struct audit_buffer *ab)
534 {
535         char *ctx = NULL;
536         ssize_t len = 0;
537
538         len = security_getprocattr(current, "current", NULL, 0);
539         if (len < 0) {
540                 if (len != -EINVAL)
541                         goto error_path;
542                 return;
543         }
544
545         ctx = kmalloc(len, GFP_KERNEL);
546         if (!ctx)
547                 goto error_path;
548
549         len = security_getprocattr(current, "current", ctx, len);
550         if (len < 0 )
551                 goto error_path;
552
553         audit_log_format(ab, " subj=%s", ctx);
554         return;
555
556 error_path:
557         if (ctx)
558                 kfree(ctx);
559         audit_panic("error in audit_log_task_context");
560         return;
561 }
562
563 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
564 {
565         char name[sizeof(tsk->comm)];
566         struct mm_struct *mm = tsk->mm;
567         struct vm_area_struct *vma;
568
569         /* tsk == current */
570
571         get_task_comm(name, tsk);
572         audit_log_format(ab, " comm=");
573         audit_log_untrustedstring(ab, name);
574
575         if (mm) {
576                 down_read(&mm->mmap_sem);
577                 vma = mm->mmap;
578                 while (vma) {
579                         if ((vma->vm_flags & VM_EXECUTABLE) &&
580                             vma->vm_file) {
581                                 audit_log_d_path(ab, "exe=",
582                                                  vma->vm_file->f_dentry,
583                                                  vma->vm_file->f_vfsmnt);
584                                 break;
585                         }
586                         vma = vma->vm_next;
587                 }
588                 up_read(&mm->mmap_sem);
589         }
590         audit_log_task_context(ab);
591 }
592
593 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
594 {
595         int i, call_panic = 0;
596         struct audit_buffer *ab;
597         struct audit_aux_data *aux;
598         const char *tty;
599
600         /* tsk == current */
601
602         ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
603         if (!ab)
604                 return;         /* audit_panic has been called */
605         audit_log_format(ab, "arch=%x syscall=%d",
606                          context->arch, context->major);
607         if (context->personality != PER_LINUX)
608                 audit_log_format(ab, " per=%lx", context->personality);
609         if (context->return_valid)
610                 audit_log_format(ab, " success=%s exit=%ld", 
611                                  (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
612                                  context->return_code);
613         if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
614                 tty = tsk->signal->tty->name;
615         else
616                 tty = "(none)";
617         audit_log_format(ab,
618                   " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
619                   " ppid=%d pid=%d auid=%u uid=%u gid=%u"
620                   " euid=%u suid=%u fsuid=%u"
621                   " egid=%u sgid=%u fsgid=%u tty=%s",
622                   context->argv[0],
623                   context->argv[1],
624                   context->argv[2],
625                   context->argv[3],
626                   context->name_count,
627                   context->ppid,
628                   context->pid,
629                   context->loginuid,
630                   context->uid,
631                   context->gid,
632                   context->euid, context->suid, context->fsuid,
633                   context->egid, context->sgid, context->fsgid, tty);
634         audit_log_task_info(ab, tsk);
635         audit_log_end(ab);
636
637         for (aux = context->aux; aux; aux = aux->next) {
638
639                 ab = audit_log_start(context, GFP_KERNEL, aux->type);
640                 if (!ab)
641                         continue; /* audit_panic has been called */
642
643                 switch (aux->type) {
644                 case AUDIT_IPC: {
645                         struct audit_aux_data_ipcctl *axi = (void *)aux;
646                         audit_log_format(ab, 
647                                  " qbytes=%lx iuid=%u igid=%u mode=%x",
648                                  axi->qbytes, axi->uid, axi->gid, axi->mode);
649                         if (axi->osid != 0) {
650                                 char *ctx = NULL;
651                                 u32 len;
652                                 if (selinux_ctxid_to_string(
653                                                 axi->osid, &ctx, &len)) {
654                                         audit_log_format(ab, " osid=%u",
655                                                         axi->osid);
656                                         call_panic = 1;
657                                 } else
658                                         audit_log_format(ab, " obj=%s", ctx);
659                                 kfree(ctx);
660                         }
661                         break; }
662
663                 case AUDIT_IPC_SET_PERM: {
664                         struct audit_aux_data_ipcctl *axi = (void *)aux;
665                         audit_log_format(ab,
666                                 " new qbytes=%lx new iuid=%u new igid=%u new mode=%x",
667                                 axi->qbytes, axi->uid, axi->gid, axi->mode);
668                         if (axi->osid != 0) {
669                                 char *ctx = NULL;
670                                 u32 len;
671                                 if (selinux_ctxid_to_string(
672                                                 axi->osid, &ctx, &len)) {
673                                         audit_log_format(ab, " osid=%u",
674                                                         axi->osid);
675                                         call_panic = 1;
676                                 } else
677                                         audit_log_format(ab, " obj=%s", ctx);
678                                 kfree(ctx);
679                         }
680                         break; }
681                 case AUDIT_EXECVE: {
682                         struct audit_aux_data_execve *axi = (void *)aux;
683                         int i;
684                         const char *p;
685                         for (i = 0, p = axi->mem; i < axi->argc; i++) {
686                                 audit_log_format(ab, "a%d=", i);
687                                 p = audit_log_untrustedstring(ab, p);
688                                 audit_log_format(ab, "\n");
689                         }
690                         break; }
691
692                 case AUDIT_SOCKETCALL: {
693                         int i;
694                         struct audit_aux_data_socketcall *axs = (void *)aux;
695                         audit_log_format(ab, "nargs=%d", axs->nargs);
696                         for (i=0; i<axs->nargs; i++)
697                                 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
698                         break; }
699
700                 case AUDIT_SOCKADDR: {
701                         struct audit_aux_data_sockaddr *axs = (void *)aux;
702
703                         audit_log_format(ab, "saddr=");
704                         audit_log_hex(ab, axs->a, axs->len);
705                         break; }
706
707                 case AUDIT_AVC_PATH: {
708                         struct audit_aux_data_path *axi = (void *)aux;
709                         audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
710                         break; }
711
712                 }
713                 audit_log_end(ab);
714         }
715
716         if (context->pwd && context->pwdmnt) {
717                 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
718                 if (ab) {
719                         audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
720                         audit_log_end(ab);
721                 }
722         }
723         for (i = 0; i < context->name_count; i++) {
724                 unsigned long ino  = context->names[i].ino;
725                 unsigned long pino = context->names[i].pino;
726
727                 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
728                 if (!ab)
729                         continue; /* audit_panic has been called */
730
731                 audit_log_format(ab, "item=%d", i);
732
733                 audit_log_format(ab, " name=");
734                 if (context->names[i].name)
735                         audit_log_untrustedstring(ab, context->names[i].name);
736                 else
737                         audit_log_format(ab, "(null)");
738
739                 if (pino != (unsigned long)-1)
740                         audit_log_format(ab, " parent=%lu",  pino);
741                 if (ino != (unsigned long)-1)
742                         audit_log_format(ab, " inode=%lu",  ino);
743                 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
744                         audit_log_format(ab, " dev=%02x:%02x mode=%#o" 
745                                          " ouid=%u ogid=%u rdev=%02x:%02x", 
746                                          MAJOR(context->names[i].dev), 
747                                          MINOR(context->names[i].dev), 
748                                          context->names[i].mode, 
749                                          context->names[i].uid, 
750                                          context->names[i].gid, 
751                                          MAJOR(context->names[i].rdev), 
752                                          MINOR(context->names[i].rdev));
753                 if (context->names[i].osid != 0) {
754                         char *ctx = NULL;
755                         u32 len;
756                         if (selinux_ctxid_to_string(
757                                 context->names[i].osid, &ctx, &len)) {
758                                 audit_log_format(ab, " osid=%u",
759                                                 context->names[i].osid);
760                                 call_panic = 2;
761                         } else
762                                 audit_log_format(ab, " obj=%s", ctx);
763                         kfree(ctx);
764                 }
765
766                 audit_log_end(ab);
767         }
768         if (call_panic)
769                 audit_panic("error converting sid to string");
770 }
771
772 /**
773  * audit_free - free a per-task audit context
774  * @tsk: task whose audit context block to free
775  *
776  * Called from copy_process and do_exit
777  */
778 void audit_free(struct task_struct *tsk)
779 {
780         struct audit_context *context;
781
782         context = audit_get_context(tsk, 0, 0);
783         if (likely(!context))
784                 return;
785
786         /* Check for system calls that do not go through the exit
787          * function (e.g., exit_group), then free context block. 
788          * We use GFP_ATOMIC here because we might be doing this 
789          * in the context of the idle thread */
790         /* that can happen only if we are called from do_exit() */
791         if (context->in_syscall && context->auditable)
792                 audit_log_exit(context, tsk);
793
794         audit_free_context(context);
795 }
796
797 /**
798  * audit_syscall_entry - fill in an audit record at syscall entry
799  * @tsk: task being audited
800  * @arch: architecture type
801  * @major: major syscall type (function)
802  * @a1: additional syscall register 1
803  * @a2: additional syscall register 2
804  * @a3: additional syscall register 3
805  * @a4: additional syscall register 4
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  */
815 void audit_syscall_entry(int arch, int major,
816                          unsigned long a1, unsigned long a2,
817                          unsigned long a3, unsigned long a4)
818 {
819         struct task_struct *tsk = current;
820         struct audit_context *context = tsk->audit_context;
821         enum audit_state     state;
822
823         BUG_ON(!context);
824
825         /*
826          * This happens only on certain architectures that make system
827          * calls in kernel_thread via the entry.S interface, instead of
828          * with direct calls.  (If you are porting to a new
829          * architecture, hitting this condition can indicate that you
830          * got the _exit/_leave calls backward in entry.S.)
831          *
832          * i386     no
833          * x86_64   no
834          * ppc64    yes (see arch/powerpc/platforms/iseries/misc.S)
835          *
836          * This also happens with vm86 emulation in a non-nested manner
837          * (entries without exits), so this case must be caught.
838          */
839         if (context->in_syscall) {
840                 struct audit_context *newctx;
841
842 #if AUDIT_DEBUG
843                 printk(KERN_ERR
844                        "audit(:%d) pid=%d in syscall=%d;"
845                        " entering syscall=%d\n",
846                        context->serial, tsk->pid, context->major, major);
847 #endif
848                 newctx = audit_alloc_context(context->state);
849                 if (newctx) {
850                         newctx->previous   = context;
851                         context            = newctx;
852                         tsk->audit_context = newctx;
853                 } else  {
854                         /* If we can't alloc a new context, the best we
855                          * can do is to leak memory (any pending putname
856                          * will be lost).  The only other alternative is
857                          * to abandon auditing. */
858                         audit_zero_context(context, context->state);
859                 }
860         }
861         BUG_ON(context->in_syscall || context->name_count);
862
863         if (!audit_enabled)
864                 return;
865
866         context->arch       = arch;
867         context->major      = major;
868         context->argv[0]    = a1;
869         context->argv[1]    = a2;
870         context->argv[2]    = a3;
871         context->argv[3]    = a4;
872
873         state = context->state;
874         if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
875                 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
876         if (likely(state == AUDIT_DISABLED))
877                 return;
878
879         context->serial     = 0;
880         context->ctime      = CURRENT_TIME;
881         context->in_syscall = 1;
882         context->auditable  = !!(state == AUDIT_RECORD_CONTEXT);
883 }
884
885 /**
886  * audit_syscall_exit - deallocate audit context after a system call
887  * @tsk: task being audited
888  * @valid: success/failure flag
889  * @return_code: syscall return value
890  *
891  * Tear down after system call.  If the audit context has been marked as
892  * auditable (either because of the AUDIT_RECORD_CONTEXT state from
893  * filtering, or because some other part of the kernel write an audit
894  * message), then write out the syscall information.  In call cases,
895  * free the names stored from getname().
896  */
897 void audit_syscall_exit(int valid, long return_code)
898 {
899         struct task_struct *tsk = current;
900         struct audit_context *context;
901
902         context = audit_get_context(tsk, valid, return_code);
903
904         if (likely(!context))
905                 return;
906
907         if (context->in_syscall && context->auditable)
908                 audit_log_exit(context, tsk);
909
910         context->in_syscall = 0;
911         context->auditable  = 0;
912
913         if (context->previous) {
914                 struct audit_context *new_context = context->previous;
915                 context->previous  = NULL;
916                 audit_free_context(context);
917                 tsk->audit_context = new_context;
918         } else {
919                 audit_free_names(context);
920                 audit_free_aux(context);
921                 tsk->audit_context = context;
922         }
923 }
924
925 /**
926  * audit_getname - add a name to the list
927  * @name: name to add
928  *
929  * Add a name to the list of audit names for this context.
930  * Called from fs/namei.c:getname().
931  */
932 void audit_getname(const char *name)
933 {
934         struct audit_context *context = current->audit_context;
935
936         if (!context || IS_ERR(name) || !name)
937                 return;
938
939         if (!context->in_syscall) {
940 #if AUDIT_DEBUG == 2
941                 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
942                        __FILE__, __LINE__, context->serial, name);
943                 dump_stack();
944 #endif
945                 return;
946         }
947         BUG_ON(context->name_count >= AUDIT_NAMES);
948         context->names[context->name_count].name = name;
949         context->names[context->name_count].ino  = (unsigned long)-1;
950         ++context->name_count;
951         if (!context->pwd) {
952                 read_lock(&current->fs->lock);
953                 context->pwd = dget(current->fs->pwd);
954                 context->pwdmnt = mntget(current->fs->pwdmnt);
955                 read_unlock(&current->fs->lock);
956         }
957                 
958 }
959
960 /* audit_putname - intercept a putname request
961  * @name: name to intercept and delay for putname
962  *
963  * If we have stored the name from getname in the audit context,
964  * then we delay the putname until syscall exit.
965  * Called from include/linux/fs.h:putname().
966  */
967 void audit_putname(const char *name)
968 {
969         struct audit_context *context = current->audit_context;
970
971         BUG_ON(!context);
972         if (!context->in_syscall) {
973 #if AUDIT_DEBUG == 2
974                 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
975                        __FILE__, __LINE__, context->serial, name);
976                 if (context->name_count) {
977                         int i;
978                         for (i = 0; i < context->name_count; i++)
979                                 printk(KERN_ERR "name[%d] = %p = %s\n", i,
980                                        context->names[i].name,
981                                        context->names[i].name ?: "(null)");
982                 }
983 #endif
984                 __putname(name);
985         }
986 #if AUDIT_DEBUG
987         else {
988                 ++context->put_count;
989                 if (context->put_count > context->name_count) {
990                         printk(KERN_ERR "%s:%d(:%d): major=%d"
991                                " in_syscall=%d putname(%p) name_count=%d"
992                                " put_count=%d\n",
993                                __FILE__, __LINE__,
994                                context->serial, context->major,
995                                context->in_syscall, name, context->name_count,
996                                context->put_count);
997                         dump_stack();
998                 }
999         }
1000 #endif
1001 }
1002
1003 static void audit_inode_context(int idx, const struct inode *inode)
1004 {
1005         struct audit_context *context = current->audit_context;
1006
1007         selinux_get_inode_sid(inode, &context->names[idx].osid);
1008 }
1009
1010
1011 /**
1012  * audit_inode - store the inode and device from a lookup
1013  * @name: name being audited
1014  * @inode: inode being audited
1015  * @flags: lookup flags (as used in path_lookup())
1016  *
1017  * Called from fs/namei.c:path_lookup().
1018  */
1019 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1020 {
1021         int idx;
1022         struct audit_context *context = current->audit_context;
1023
1024         if (!context->in_syscall)
1025                 return;
1026         if (context->name_count
1027             && context->names[context->name_count-1].name
1028             && context->names[context->name_count-1].name == name)
1029                 idx = context->name_count - 1;
1030         else if (context->name_count > 1
1031                  && context->names[context->name_count-2].name
1032                  && context->names[context->name_count-2].name == name)
1033                 idx = context->name_count - 2;
1034         else {
1035                 /* FIXME: how much do we care about inodes that have no
1036                  * associated name? */
1037                 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1038                         return;
1039                 idx = context->name_count++;
1040                 context->names[idx].name = NULL;
1041 #if AUDIT_DEBUG
1042                 ++context->ino_count;
1043 #endif
1044         }
1045         context->names[idx].dev   = inode->i_sb->s_dev;
1046         context->names[idx].mode  = inode->i_mode;
1047         context->names[idx].uid   = inode->i_uid;
1048         context->names[idx].gid   = inode->i_gid;
1049         context->names[idx].rdev  = inode->i_rdev;
1050         audit_inode_context(idx, inode);
1051         if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) && 
1052             (strcmp(name, ".") != 0)) {
1053                 context->names[idx].ino   = (unsigned long)-1;
1054                 context->names[idx].pino  = inode->i_ino;
1055         } else {
1056                 context->names[idx].ino   = inode->i_ino;
1057                 context->names[idx].pino  = (unsigned long)-1;
1058         }
1059 }
1060
1061 /**
1062  * audit_inode_child - collect inode info for created/removed objects
1063  * @dname: inode's dentry name
1064  * @inode: inode being audited
1065  * @pino: inode number of dentry parent
1066  *
1067  * For syscalls that create or remove filesystem objects, audit_inode
1068  * can only collect information for the filesystem object's parent.
1069  * This call updates the audit context with the child's information.
1070  * Syscalls that create a new filesystem object must be hooked after
1071  * the object is created.  Syscalls that remove a filesystem object
1072  * must be hooked prior, in order to capture the target inode during
1073  * unsuccessful attempts.
1074  */
1075 void __audit_inode_child(const char *dname, const struct inode *inode,
1076                          unsigned long pino)
1077 {
1078         int idx;
1079         struct audit_context *context = current->audit_context;
1080
1081         if (!context->in_syscall)
1082                 return;
1083
1084         /* determine matching parent */
1085         if (dname)
1086                 for (idx = 0; idx < context->name_count; idx++)
1087                         if (context->names[idx].pino == pino) {
1088                                 const char *n;
1089                                 const char *name = context->names[idx].name;
1090                                 int dlen = strlen(dname);
1091                                 int nlen = name ? strlen(name) : 0;
1092
1093                                 if (nlen < dlen)
1094                                         continue;
1095                                 
1096                                 /* disregard trailing slashes */
1097                                 n = name + nlen - 1;
1098                                 while ((*n == '/') && (n > name))
1099                                         n--;
1100
1101                                 /* find last path component */
1102                                 n = n - dlen + 1;
1103                                 if (n < name)
1104                                         continue;
1105                                 else if (n > name) {
1106                                         if (*--n != '/')
1107                                                 continue;
1108                                         else
1109                                                 n++;
1110                                 }
1111
1112                                 if (strncmp(n, dname, dlen) == 0)
1113                                         goto update_context;
1114                         }
1115
1116         /* catch-all in case match not found */
1117         idx = context->name_count++;
1118         context->names[idx].name  = NULL;
1119         context->names[idx].pino  = pino;
1120 #if AUDIT_DEBUG
1121         context->ino_count++;
1122 #endif
1123
1124 update_context:
1125         if (inode) {
1126                 context->names[idx].ino   = inode->i_ino;
1127                 context->names[idx].dev   = inode->i_sb->s_dev;
1128                 context->names[idx].mode  = inode->i_mode;
1129                 context->names[idx].uid   = inode->i_uid;
1130                 context->names[idx].gid   = inode->i_gid;
1131                 context->names[idx].rdev  = inode->i_rdev;
1132                 audit_inode_context(idx, inode);
1133         }
1134 }
1135
1136 /**
1137  * auditsc_get_stamp - get local copies of audit_context values
1138  * @ctx: audit_context for the task
1139  * @t: timespec to store time recorded in the audit_context
1140  * @serial: serial value that is recorded in the audit_context
1141  *
1142  * Also sets the context as auditable.
1143  */
1144 void auditsc_get_stamp(struct audit_context *ctx,
1145                        struct timespec *t, unsigned int *serial)
1146 {
1147         if (!ctx->serial)
1148                 ctx->serial = audit_serial();
1149         t->tv_sec  = ctx->ctime.tv_sec;
1150         t->tv_nsec = ctx->ctime.tv_nsec;
1151         *serial    = ctx->serial;
1152         ctx->auditable = 1;
1153 }
1154
1155 /**
1156  * audit_set_loginuid - set a task's audit_context loginuid
1157  * @task: task whose audit context is being modified
1158  * @loginuid: loginuid value
1159  *
1160  * Returns 0.
1161  *
1162  * Called (set) from fs/proc/base.c::proc_loginuid_write().
1163  */
1164 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1165 {
1166         if (task->audit_context) {
1167                 struct audit_buffer *ab;
1168
1169                 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1170                 if (ab) {
1171                         audit_log_format(ab, "login pid=%d uid=%u "
1172                                 "old auid=%u new auid=%u",
1173                                 task->pid, task->uid, 
1174                                 task->audit_context->loginuid, loginuid);
1175                         audit_log_end(ab);
1176                 }
1177                 task->audit_context->loginuid = loginuid;
1178         }
1179         return 0;
1180 }
1181
1182 /**
1183  * audit_get_loginuid - get the loginuid for an audit_context
1184  * @ctx: the audit_context
1185  *
1186  * Returns the context's loginuid or -1 if @ctx is NULL.
1187  */
1188 uid_t audit_get_loginuid(struct audit_context *ctx)
1189 {
1190         return ctx ? ctx->loginuid : -1;
1191 }
1192
1193 /**
1194  * audit_ipc_obj - record audit data for ipc object
1195  * @ipcp: ipc permissions
1196  *
1197  * Returns 0 for success or NULL context or < 0 on error.
1198  */
1199 int audit_ipc_obj(struct kern_ipc_perm *ipcp)
1200 {
1201         struct audit_aux_data_ipcctl *ax;
1202         struct audit_context *context = current->audit_context;
1203
1204         if (likely(!context))
1205                 return 0;
1206
1207         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1208         if (!ax)
1209                 return -ENOMEM;
1210
1211         ax->uid = ipcp->uid;
1212         ax->gid = ipcp->gid;
1213         ax->mode = ipcp->mode;
1214         selinux_get_ipc_sid(ipcp, &ax->osid);
1215
1216         ax->d.type = AUDIT_IPC;
1217         ax->d.next = context->aux;
1218         context->aux = (void *)ax;
1219         return 0;
1220 }
1221
1222 /**
1223  * audit_ipc_set_perm - record audit data for new ipc permissions
1224  * @qbytes: msgq bytes
1225  * @uid: msgq user id
1226  * @gid: msgq group id
1227  * @mode: msgq mode (permissions)
1228  *
1229  * Returns 0 for success or NULL context or < 0 on error.
1230  */
1231 int audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
1232 {
1233         struct audit_aux_data_ipcctl *ax;
1234         struct audit_context *context = current->audit_context;
1235
1236         if (likely(!context))
1237                 return 0;
1238
1239         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1240         if (!ax)
1241                 return -ENOMEM;
1242
1243         ax->qbytes = qbytes;
1244         ax->uid = uid;
1245         ax->gid = gid;
1246         ax->mode = mode;
1247         selinux_get_ipc_sid(ipcp, &ax->osid);
1248
1249         ax->d.type = AUDIT_IPC_SET_PERM;
1250         ax->d.next = context->aux;
1251         context->aux = (void *)ax;
1252         return 0;
1253 }
1254
1255 int audit_bprm(struct linux_binprm *bprm)
1256 {
1257         struct audit_aux_data_execve *ax;
1258         struct audit_context *context = current->audit_context;
1259         unsigned long p, next;
1260         void *to;
1261
1262         if (likely(!audit_enabled || !context))
1263                 return 0;
1264
1265         ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1266                                 GFP_KERNEL);
1267         if (!ax)
1268                 return -ENOMEM;
1269
1270         ax->argc = bprm->argc;
1271         ax->envc = bprm->envc;
1272         for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1273                 struct page *page = bprm->page[p / PAGE_SIZE];
1274                 void *kaddr = kmap(page);
1275                 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1276                 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1277                 to += next - p;
1278                 kunmap(page);
1279         }
1280
1281         ax->d.type = AUDIT_EXECVE;
1282         ax->d.next = context->aux;
1283         context->aux = (void *)ax;
1284         return 0;
1285 }
1286
1287
1288 /**
1289  * audit_socketcall - record audit data for sys_socketcall
1290  * @nargs: number of args
1291  * @args: args array
1292  *
1293  * Returns 0 for success or NULL context or < 0 on error.
1294  */
1295 int audit_socketcall(int nargs, unsigned long *args)
1296 {
1297         struct audit_aux_data_socketcall *ax;
1298         struct audit_context *context = current->audit_context;
1299
1300         if (likely(!context))
1301                 return 0;
1302
1303         ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1304         if (!ax)
1305                 return -ENOMEM;
1306
1307         ax->nargs = nargs;
1308         memcpy(ax->args, args, nargs * sizeof(unsigned long));
1309
1310         ax->d.type = AUDIT_SOCKETCALL;
1311         ax->d.next = context->aux;
1312         context->aux = (void *)ax;
1313         return 0;
1314 }
1315
1316 /**
1317  * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1318  * @len: data length in user space
1319  * @a: data address in kernel space
1320  *
1321  * Returns 0 for success or NULL context or < 0 on error.
1322  */
1323 int audit_sockaddr(int len, void *a)
1324 {
1325         struct audit_aux_data_sockaddr *ax;
1326         struct audit_context *context = current->audit_context;
1327
1328         if (likely(!context))
1329                 return 0;
1330
1331         ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1332         if (!ax)
1333                 return -ENOMEM;
1334
1335         ax->len = len;
1336         memcpy(ax->a, a, len);
1337
1338         ax->d.type = AUDIT_SOCKADDR;
1339         ax->d.next = context->aux;
1340         context->aux = (void *)ax;
1341         return 0;
1342 }
1343
1344 /**
1345  * audit_avc_path - record the granting or denial of permissions
1346  * @dentry: dentry to record
1347  * @mnt: mnt to record
1348  *
1349  * Returns 0 for success or NULL context or < 0 on error.
1350  *
1351  * Called from security/selinux/avc.c::avc_audit()
1352  */
1353 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1354 {
1355         struct audit_aux_data_path *ax;
1356         struct audit_context *context = current->audit_context;
1357
1358         if (likely(!context))
1359                 return 0;
1360
1361         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1362         if (!ax)
1363                 return -ENOMEM;
1364
1365         ax->dentry = dget(dentry);
1366         ax->mnt = mntget(mnt);
1367
1368         ax->d.type = AUDIT_AVC_PATH;
1369         ax->d.next = context->aux;
1370         context->aux = (void *)ax;
1371         return 0;
1372 }
1373
1374 /**
1375  * audit_signal_info - record signal info for shutting down audit subsystem
1376  * @sig: signal value
1377  * @t: task being signaled
1378  *
1379  * If the audit subsystem is being terminated, record the task (pid)
1380  * and uid that is doing that.
1381  */
1382 void __audit_signal_info(int sig, struct task_struct *t)
1383 {
1384         extern pid_t audit_sig_pid;
1385         extern uid_t audit_sig_uid;
1386         extern u32 audit_sig_sid;
1387
1388         if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1389                 struct task_struct *tsk = current;
1390                 struct audit_context *ctx = tsk->audit_context;
1391                 audit_sig_pid = tsk->pid;
1392                 if (ctx)
1393                         audit_sig_uid = ctx->loginuid;
1394                 else
1395                         audit_sig_uid = tsk->uid;
1396                 selinux_get_task_sid(tsk, &audit_sig_sid);
1397         }
1398 }