[PATCH] audit: path-based rules
[h-e-n] / 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, 2006 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  * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
33  * 2006.
34  *
35  * The support of additional filter rules compares (>, <, >=, <=) was
36  * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
37  *
38  * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39  * filesystem information.
40  *
41  * Subject and object context labeling support added by <danjones@us.ibm.com>
42  * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
43  */
44
45 #include <linux/init.h>
46 #include <asm/types.h>
47 #include <asm/atomic.h>
48 #include <asm/types.h>
49 #include <linux/fs.h>
50 #include <linux/namei.h>
51 #include <linux/mm.h>
52 #include <linux/module.h>
53 #include <linux/mount.h>
54 #include <linux/socket.h>
55 #include <linux/mqueue.h>
56 #include <linux/audit.h>
57 #include <linux/personality.h>
58 #include <linux/time.h>
59 #include <linux/netlink.h>
60 #include <linux/compiler.h>
61 #include <asm/unistd.h>
62 #include <linux/security.h>
63 #include <linux/list.h>
64 #include <linux/tty.h>
65 #include <linux/selinux.h>
66 #include <linux/binfmts.h>
67 #include <linux/syscalls.h>
68
69 #include "audit.h"
70
71 extern struct list_head audit_filter_list[];
72
73 /* No syscall auditing will take place unless audit_enabled != 0. */
74 extern int audit_enabled;
75
76 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
77  * for saving names from getname(). */
78 #define AUDIT_NAMES    20
79
80 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
81  * audit_context from being used for nameless inodes from
82  * path_lookup. */
83 #define AUDIT_NAMES_RESERVED 7
84
85 /* When fs/namei.c:getname() is called, we store the pointer in name and
86  * we don't let putname() free it (instead we free all of the saved
87  * pointers at syscall exit time).
88  *
89  * Further, in fs/namei.c:path_lookup() we store the inode and device. */
90 struct audit_names {
91         const char      *name;
92         unsigned long   ino;
93         unsigned long   pino;
94         dev_t           dev;
95         umode_t         mode;
96         uid_t           uid;
97         gid_t           gid;
98         dev_t           rdev;
99         u32             osid;
100 };
101
102 struct audit_aux_data {
103         struct audit_aux_data   *next;
104         int                     type;
105 };
106
107 #define AUDIT_AUX_IPCPERM       0
108
109 struct audit_aux_data_mq_open {
110         struct audit_aux_data   d;
111         int                     oflag;
112         mode_t                  mode;
113         struct mq_attr          attr;
114 };
115
116 struct audit_aux_data_mq_sendrecv {
117         struct audit_aux_data   d;
118         mqd_t                   mqdes;
119         size_t                  msg_len;
120         unsigned int            msg_prio;
121         struct timespec         abs_timeout;
122 };
123
124 struct audit_aux_data_mq_notify {
125         struct audit_aux_data   d;
126         mqd_t                   mqdes;
127         struct sigevent         notification;
128 };
129
130 struct audit_aux_data_mq_getsetattr {
131         struct audit_aux_data   d;
132         mqd_t                   mqdes;
133         struct mq_attr          mqstat;
134 };
135
136 struct audit_aux_data_ipcctl {
137         struct audit_aux_data   d;
138         struct ipc_perm         p;
139         unsigned long           qbytes;
140         uid_t                   uid;
141         gid_t                   gid;
142         mode_t                  mode;
143         u32                     osid;
144 };
145
146 struct audit_aux_data_execve {
147         struct audit_aux_data   d;
148         int argc;
149         int envc;
150         char mem[0];
151 };
152
153 struct audit_aux_data_socketcall {
154         struct audit_aux_data   d;
155         int                     nargs;
156         unsigned long           args[0];
157 };
158
159 struct audit_aux_data_sockaddr {
160         struct audit_aux_data   d;
161         int                     len;
162         char                    a[0];
163 };
164
165 struct audit_aux_data_path {
166         struct audit_aux_data   d;
167         struct dentry           *dentry;
168         struct vfsmount         *mnt;
169 };
170
171 /* The per-task audit context. */
172 struct audit_context {
173         int                 in_syscall; /* 1 if task is in a syscall */
174         enum audit_state    state;
175         unsigned int        serial;     /* serial number for record */
176         struct timespec     ctime;      /* time of syscall entry */
177         uid_t               loginuid;   /* login uid (identity) */
178         int                 major;      /* syscall number */
179         unsigned long       argv[4];    /* syscall arguments */
180         int                 return_valid; /* return code is valid */
181         long                return_code;/* syscall return code */
182         int                 auditable;  /* 1 if record should be written */
183         int                 name_count;
184         struct audit_names  names[AUDIT_NAMES];
185         struct dentry *     pwd;
186         struct vfsmount *   pwdmnt;
187         struct audit_context *previous; /* For nested syscalls */
188         struct audit_aux_data *aux;
189
190                                 /* Save things to print about task_struct */
191         pid_t               pid, ppid;
192         uid_t               uid, euid, suid, fsuid;
193         gid_t               gid, egid, sgid, fsgid;
194         unsigned long       personality;
195         int                 arch;
196
197 #if AUDIT_DEBUG
198         int                 put_count;
199         int                 ino_count;
200 #endif
201 };
202
203 /* Determine if any context name data matches a rule's watch data */
204 /* Compare a task_struct with an audit_rule.  Return 1 on match, 0
205  * otherwise. */
206 static int audit_filter_rules(struct task_struct *tsk,
207                               struct audit_krule *rule,
208                               struct audit_context *ctx,
209                               struct audit_names *name,
210                               enum audit_state *state)
211 {
212         int i, j, need_sid = 1;
213         u32 sid;
214
215         for (i = 0; i < rule->field_count; i++) {
216                 struct audit_field *f = &rule->fields[i];
217                 int result = 0;
218
219                 switch (f->type) {
220                 case AUDIT_PID:
221                         result = audit_comparator(tsk->pid, f->op, f->val);
222                         break;
223                 case AUDIT_PPID:
224                         if (ctx)
225                                 result = audit_comparator(ctx->ppid, f->op, f->val);
226                         break;
227                 case AUDIT_UID:
228                         result = audit_comparator(tsk->uid, f->op, f->val);
229                         break;
230                 case AUDIT_EUID:
231                         result = audit_comparator(tsk->euid, f->op, f->val);
232                         break;
233                 case AUDIT_SUID:
234                         result = audit_comparator(tsk->suid, f->op, f->val);
235                         break;
236                 case AUDIT_FSUID:
237                         result = audit_comparator(tsk->fsuid, f->op, f->val);
238                         break;
239                 case AUDIT_GID:
240                         result = audit_comparator(tsk->gid, f->op, f->val);
241                         break;
242                 case AUDIT_EGID:
243                         result = audit_comparator(tsk->egid, f->op, f->val);
244                         break;
245                 case AUDIT_SGID:
246                         result = audit_comparator(tsk->sgid, f->op, f->val);
247                         break;
248                 case AUDIT_FSGID:
249                         result = audit_comparator(tsk->fsgid, f->op, f->val);
250                         break;
251                 case AUDIT_PERS:
252                         result = audit_comparator(tsk->personality, f->op, f->val);
253                         break;
254                 case AUDIT_ARCH:
255                         if (ctx)
256                                 result = audit_comparator(ctx->arch, f->op, f->val);
257                         break;
258
259                 case AUDIT_EXIT:
260                         if (ctx && ctx->return_valid)
261                                 result = audit_comparator(ctx->return_code, f->op, f->val);
262                         break;
263                 case AUDIT_SUCCESS:
264                         if (ctx && ctx->return_valid) {
265                                 if (f->val)
266                                         result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
267                                 else
268                                         result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
269                         }
270                         break;
271                 case AUDIT_DEVMAJOR:
272                         if (name)
273                                 result = audit_comparator(MAJOR(name->dev),
274                                                           f->op, f->val);
275                         else if (ctx) {
276                                 for (j = 0; j < ctx->name_count; j++) {
277                                         if (audit_comparator(MAJOR(ctx->names[j].dev),  f->op, f->val)) {
278                                                 ++result;
279                                                 break;
280                                         }
281                                 }
282                         }
283                         break;
284                 case AUDIT_DEVMINOR:
285                         if (name)
286                                 result = audit_comparator(MINOR(name->dev),
287                                                           f->op, f->val);
288                         else if (ctx) {
289                                 for (j = 0; j < ctx->name_count; j++) {
290                                         if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
291                                                 ++result;
292                                                 break;
293                                         }
294                                 }
295                         }
296                         break;
297                 case AUDIT_INODE:
298                         if (name)
299                                 result = (name->ino == f->val ||
300                                           name->pino == f->val);
301                         else if (ctx) {
302                                 for (j = 0; j < ctx->name_count; j++) {
303                                         if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
304                                             audit_comparator(ctx->names[j].pino, f->op, f->val)) {
305                                                 ++result;
306                                                 break;
307                                         }
308                                 }
309                         }
310                         break;
311                 case AUDIT_WATCH:
312                         if (name && rule->watch->ino != (unsigned long)-1)
313                                 result = (name->dev == rule->watch->dev &&
314                                           (name->ino == rule->watch->ino ||
315                                            name->pino == rule->watch->ino));
316                         break;
317                 case AUDIT_LOGINUID:
318                         result = 0;
319                         if (ctx)
320                                 result = audit_comparator(ctx->loginuid, f->op, f->val);
321                         break;
322                 case AUDIT_SE_USER:
323                 case AUDIT_SE_ROLE:
324                 case AUDIT_SE_TYPE:
325                 case AUDIT_SE_SEN:
326                 case AUDIT_SE_CLR:
327                         /* NOTE: this may return negative values indicating
328                            a temporary error.  We simply treat this as a
329                            match for now to avoid losing information that
330                            may be wanted.   An error message will also be
331                            logged upon error */
332                         if (f->se_rule) {
333                                 if (need_sid) {
334                                         selinux_task_ctxid(tsk, &sid);
335                                         need_sid = 0;
336                                 }
337                                 result = selinux_audit_rule_match(sid, f->type,
338                                                                   f->op,
339                                                                   f->se_rule,
340                                                                   ctx);
341                         }
342                         break;
343                 case AUDIT_ARG0:
344                 case AUDIT_ARG1:
345                 case AUDIT_ARG2:
346                 case AUDIT_ARG3:
347                         if (ctx)
348                                 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
349                         break;
350                 }
351
352                 if (!result)
353                         return 0;
354         }
355         switch (rule->action) {
356         case AUDIT_NEVER:    *state = AUDIT_DISABLED;       break;
357         case AUDIT_ALWAYS:   *state = AUDIT_RECORD_CONTEXT; break;
358         }
359         return 1;
360 }
361
362 /* At process creation time, we can determine if system-call auditing is
363  * completely disabled for this task.  Since we only have the task
364  * structure at this point, we can only check uid and gid.
365  */
366 static enum audit_state audit_filter_task(struct task_struct *tsk)
367 {
368         struct audit_entry *e;
369         enum audit_state   state;
370
371         rcu_read_lock();
372         list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
373                 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
374                         rcu_read_unlock();
375                         return state;
376                 }
377         }
378         rcu_read_unlock();
379         return AUDIT_BUILD_CONTEXT;
380 }
381
382 /* At syscall entry and exit time, this filter is called if the
383  * audit_state is not low enough that auditing cannot take place, but is
384  * also not high enough that we already know we have to write an audit
385  * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
386  */
387 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
388                                              struct audit_context *ctx,
389                                              struct list_head *list)
390 {
391         struct audit_entry *e;
392         enum audit_state state;
393
394         if (audit_pid && tsk->tgid == audit_pid)
395                 return AUDIT_DISABLED;
396
397         rcu_read_lock();
398         if (!list_empty(list)) {
399                 int word = AUDIT_WORD(ctx->major);
400                 int bit  = AUDIT_BIT(ctx->major);
401
402                 list_for_each_entry_rcu(e, list, list) {
403                         if ((e->rule.mask[word] & bit) == bit &&
404                             audit_filter_rules(tsk, &e->rule, ctx, NULL,
405                                                &state)) {
406                                 rcu_read_unlock();
407                                 return state;
408                         }
409                 }
410         }
411         rcu_read_unlock();
412         return AUDIT_BUILD_CONTEXT;
413 }
414
415 /* At syscall exit time, this filter is called if any audit_names[] have been
416  * collected during syscall processing.  We only check rules in sublists at hash
417  * buckets applicable to the inode numbers in audit_names[].
418  * Regarding audit_state, same rules apply as for audit_filter_syscall().
419  */
420 enum audit_state audit_filter_inodes(struct task_struct *tsk,
421                                      struct audit_context *ctx)
422 {
423         int i;
424         struct audit_entry *e;
425         enum audit_state state;
426
427         if (audit_pid && tsk->tgid == audit_pid)
428                 return AUDIT_DISABLED;
429
430         rcu_read_lock();
431         for (i = 0; i < ctx->name_count; i++) {
432                 int word = AUDIT_WORD(ctx->major);
433                 int bit  = AUDIT_BIT(ctx->major);
434                 struct audit_names *n = &ctx->names[i];
435                 int h = audit_hash_ino((u32)n->ino);
436                 struct list_head *list = &audit_inode_hash[h];
437
438                 if (list_empty(list))
439                         continue;
440
441                 list_for_each_entry_rcu(e, list, list) {
442                         if ((e->rule.mask[word] & bit) == bit &&
443                             audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
444                                 rcu_read_unlock();
445                                 return state;
446                         }
447                 }
448         }
449         rcu_read_unlock();
450         return AUDIT_BUILD_CONTEXT;
451 }
452
453 void audit_set_auditable(struct audit_context *ctx)
454 {
455         ctx->auditable = 1;
456 }
457
458 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
459                                                       int return_valid,
460                                                       int return_code)
461 {
462         struct audit_context *context = tsk->audit_context;
463
464         if (likely(!context))
465                 return NULL;
466         context->return_valid = return_valid;
467         context->return_code  = return_code;
468
469         if (context->in_syscall && !context->auditable) {
470                 enum audit_state state;
471
472                 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
473                 if (state == AUDIT_RECORD_CONTEXT) {
474                         context->auditable = 1;
475                         goto get_context;
476                 }
477
478                 state = audit_filter_inodes(tsk, context);
479                 if (state == AUDIT_RECORD_CONTEXT)
480                         context->auditable = 1;
481
482         }
483
484 get_context:
485         context->pid = tsk->pid;
486         context->ppid = sys_getppid();  /* sic.  tsk == current in all cases */
487         context->uid = tsk->uid;
488         context->gid = tsk->gid;
489         context->euid = tsk->euid;
490         context->suid = tsk->suid;
491         context->fsuid = tsk->fsuid;
492         context->egid = tsk->egid;
493         context->sgid = tsk->sgid;
494         context->fsgid = tsk->fsgid;
495         context->personality = tsk->personality;
496         tsk->audit_context = NULL;
497         return context;
498 }
499
500 static inline void audit_free_names(struct audit_context *context)
501 {
502         int i;
503
504 #if AUDIT_DEBUG == 2
505         if (context->auditable
506             ||context->put_count + context->ino_count != context->name_count) {
507                 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
508                        " name_count=%d put_count=%d"
509                        " ino_count=%d [NOT freeing]\n",
510                        __FILE__, __LINE__,
511                        context->serial, context->major, context->in_syscall,
512                        context->name_count, context->put_count,
513                        context->ino_count);
514                 for (i = 0; i < context->name_count; i++) {
515                         printk(KERN_ERR "names[%d] = %p = %s\n", i,
516                                context->names[i].name,
517                                context->names[i].name ?: "(null)");
518                 }
519                 dump_stack();
520                 return;
521         }
522 #endif
523 #if AUDIT_DEBUG
524         context->put_count  = 0;
525         context->ino_count  = 0;
526 #endif
527
528         for (i = 0; i < context->name_count; i++) {
529                 if (context->names[i].name)
530                         __putname(context->names[i].name);
531         }
532         context->name_count = 0;
533         if (context->pwd)
534                 dput(context->pwd);
535         if (context->pwdmnt)
536                 mntput(context->pwdmnt);
537         context->pwd = NULL;
538         context->pwdmnt = NULL;
539 }
540
541 static inline void audit_free_aux(struct audit_context *context)
542 {
543         struct audit_aux_data *aux;
544
545         while ((aux = context->aux)) {
546                 if (aux->type == AUDIT_AVC_PATH) {
547                         struct audit_aux_data_path *axi = (void *)aux;
548                         dput(axi->dentry);
549                         mntput(axi->mnt);
550                 }
551
552                 context->aux = aux->next;
553                 kfree(aux);
554         }
555 }
556
557 static inline void audit_zero_context(struct audit_context *context,
558                                       enum audit_state state)
559 {
560         uid_t loginuid = context->loginuid;
561
562         memset(context, 0, sizeof(*context));
563         context->state      = state;
564         context->loginuid   = loginuid;
565 }
566
567 static inline struct audit_context *audit_alloc_context(enum audit_state state)
568 {
569         struct audit_context *context;
570
571         if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
572                 return NULL;
573         audit_zero_context(context, state);
574         return context;
575 }
576
577 /**
578  * audit_alloc - allocate an audit context block for a task
579  * @tsk: task
580  *
581  * Filter on the task information and allocate a per-task audit context
582  * if necessary.  Doing so turns on system call auditing for the
583  * specified task.  This is called from copy_process, so no lock is
584  * needed.
585  */
586 int audit_alloc(struct task_struct *tsk)
587 {
588         struct audit_context *context;
589         enum audit_state     state;
590
591         if (likely(!audit_enabled))
592                 return 0; /* Return if not auditing. */
593
594         state = audit_filter_task(tsk);
595         if (likely(state == AUDIT_DISABLED))
596                 return 0;
597
598         if (!(context = audit_alloc_context(state))) {
599                 audit_log_lost("out of memory in audit_alloc");
600                 return -ENOMEM;
601         }
602
603                                 /* Preserve login uid */
604         context->loginuid = -1;
605         if (current->audit_context)
606                 context->loginuid = current->audit_context->loginuid;
607
608         tsk->audit_context  = context;
609         set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
610         return 0;
611 }
612
613 static inline void audit_free_context(struct audit_context *context)
614 {
615         struct audit_context *previous;
616         int                  count = 0;
617
618         do {
619                 previous = context->previous;
620                 if (previous || (count &&  count < 10)) {
621                         ++count;
622                         printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
623                                " freeing multiple contexts (%d)\n",
624                                context->serial, context->major,
625                                context->name_count, count);
626                 }
627                 audit_free_names(context);
628                 audit_free_aux(context);
629                 kfree(context);
630                 context  = previous;
631         } while (context);
632         if (count >= 10)
633                 printk(KERN_ERR "audit: freed %d contexts\n", count);
634 }
635
636 static void audit_log_task_context(struct audit_buffer *ab)
637 {
638         char *ctx = NULL;
639         ssize_t len = 0;
640
641         len = security_getprocattr(current, "current", NULL, 0);
642         if (len < 0) {
643                 if (len != -EINVAL)
644                         goto error_path;
645                 return;
646         }
647
648         ctx = kmalloc(len, GFP_KERNEL);
649         if (!ctx)
650                 goto error_path;
651
652         len = security_getprocattr(current, "current", ctx, len);
653         if (len < 0 )
654                 goto error_path;
655
656         audit_log_format(ab, " subj=%s", ctx);
657         return;
658
659 error_path:
660         if (ctx)
661                 kfree(ctx);
662         audit_panic("error in audit_log_task_context");
663         return;
664 }
665
666 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
667 {
668         char name[sizeof(tsk->comm)];
669         struct mm_struct *mm = tsk->mm;
670         struct vm_area_struct *vma;
671
672         /* tsk == current */
673
674         get_task_comm(name, tsk);
675         audit_log_format(ab, " comm=");
676         audit_log_untrustedstring(ab, name);
677
678         if (mm) {
679                 down_read(&mm->mmap_sem);
680                 vma = mm->mmap;
681                 while (vma) {
682                         if ((vma->vm_flags & VM_EXECUTABLE) &&
683                             vma->vm_file) {
684                                 audit_log_d_path(ab, "exe=",
685                                                  vma->vm_file->f_dentry,
686                                                  vma->vm_file->f_vfsmnt);
687                                 break;
688                         }
689                         vma = vma->vm_next;
690                 }
691                 up_read(&mm->mmap_sem);
692         }
693         audit_log_task_context(ab);
694 }
695
696 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
697 {
698         int i, call_panic = 0;
699         struct audit_buffer *ab;
700         struct audit_aux_data *aux;
701         const char *tty;
702
703         /* tsk == current */
704
705         ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
706         if (!ab)
707                 return;         /* audit_panic has been called */
708         audit_log_format(ab, "arch=%x syscall=%d",
709                          context->arch, context->major);
710         if (context->personality != PER_LINUX)
711                 audit_log_format(ab, " per=%lx", context->personality);
712         if (context->return_valid)
713                 audit_log_format(ab, " success=%s exit=%ld", 
714                                  (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
715                                  context->return_code);
716         if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
717                 tty = tsk->signal->tty->name;
718         else
719                 tty = "(none)";
720         audit_log_format(ab,
721                   " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
722                   " ppid=%d pid=%d auid=%u uid=%u gid=%u"
723                   " euid=%u suid=%u fsuid=%u"
724                   " egid=%u sgid=%u fsgid=%u tty=%s",
725                   context->argv[0],
726                   context->argv[1],
727                   context->argv[2],
728                   context->argv[3],
729                   context->name_count,
730                   context->ppid,
731                   context->pid,
732                   context->loginuid,
733                   context->uid,
734                   context->gid,
735                   context->euid, context->suid, context->fsuid,
736                   context->egid, context->sgid, context->fsgid, tty);
737         audit_log_task_info(ab, tsk);
738         audit_log_end(ab);
739
740         for (aux = context->aux; aux; aux = aux->next) {
741
742                 ab = audit_log_start(context, GFP_KERNEL, aux->type);
743                 if (!ab)
744                         continue; /* audit_panic has been called */
745
746                 switch (aux->type) {
747                 case AUDIT_MQ_OPEN: {
748                         struct audit_aux_data_mq_open *axi = (void *)aux;
749                         audit_log_format(ab,
750                                 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
751                                 "mq_msgsize=%ld mq_curmsgs=%ld",
752                                 axi->oflag, axi->mode, axi->attr.mq_flags,
753                                 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
754                                 axi->attr.mq_curmsgs);
755                         break; }
756
757                 case AUDIT_MQ_SENDRECV: {
758                         struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
759                         audit_log_format(ab,
760                                 "mqdes=%d msg_len=%zd msg_prio=%u "
761                                 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
762                                 axi->mqdes, axi->msg_len, axi->msg_prio,
763                                 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
764                         break; }
765
766                 case AUDIT_MQ_NOTIFY: {
767                         struct audit_aux_data_mq_notify *axi = (void *)aux;
768                         audit_log_format(ab,
769                                 "mqdes=%d sigev_signo=%d",
770                                 axi->mqdes,
771                                 axi->notification.sigev_signo);
772                         break; }
773
774                 case AUDIT_MQ_GETSETATTR: {
775                         struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
776                         audit_log_format(ab,
777                                 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
778                                 "mq_curmsgs=%ld ",
779                                 axi->mqdes,
780                                 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
781                                 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
782                         break; }
783
784                 case AUDIT_IPC: {
785                         struct audit_aux_data_ipcctl *axi = (void *)aux;
786                         audit_log_format(ab, 
787                                  "ouid=%u ogid=%u mode=%x",
788                                  axi->uid, axi->gid, axi->mode);
789                         if (axi->osid != 0) {
790                                 char *ctx = NULL;
791                                 u32 len;
792                                 if (selinux_ctxid_to_string(
793                                                 axi->osid, &ctx, &len)) {
794                                         audit_log_format(ab, " osid=%u",
795                                                         axi->osid);
796                                         call_panic = 1;
797                                 } else
798                                         audit_log_format(ab, " obj=%s", ctx);
799                                 kfree(ctx);
800                         }
801                         break; }
802
803                 case AUDIT_IPC_SET_PERM: {
804                         struct audit_aux_data_ipcctl *axi = (void *)aux;
805                         audit_log_format(ab,
806                                 "qbytes=%lx ouid=%u ogid=%u mode=%x",
807                                 axi->qbytes, axi->uid, axi->gid, axi->mode);
808                         break; }
809
810                 case AUDIT_EXECVE: {
811                         struct audit_aux_data_execve *axi = (void *)aux;
812                         int i;
813                         const char *p;
814                         for (i = 0, p = axi->mem; i < axi->argc; i++) {
815                                 audit_log_format(ab, "a%d=", i);
816                                 p = audit_log_untrustedstring(ab, p);
817                                 audit_log_format(ab, "\n");
818                         }
819                         break; }
820
821                 case AUDIT_SOCKETCALL: {
822                         int i;
823                         struct audit_aux_data_socketcall *axs = (void *)aux;
824                         audit_log_format(ab, "nargs=%d", axs->nargs);
825                         for (i=0; i<axs->nargs; i++)
826                                 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
827                         break; }
828
829                 case AUDIT_SOCKADDR: {
830                         struct audit_aux_data_sockaddr *axs = (void *)aux;
831
832                         audit_log_format(ab, "saddr=");
833                         audit_log_hex(ab, axs->a, axs->len);
834                         break; }
835
836                 case AUDIT_AVC_PATH: {
837                         struct audit_aux_data_path *axi = (void *)aux;
838                         audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
839                         break; }
840
841                 }
842                 audit_log_end(ab);
843         }
844
845         if (context->pwd && context->pwdmnt) {
846                 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
847                 if (ab) {
848                         audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
849                         audit_log_end(ab);
850                 }
851         }
852         for (i = 0; i < context->name_count; i++) {
853                 unsigned long ino  = context->names[i].ino;
854                 unsigned long pino = context->names[i].pino;
855
856                 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
857                 if (!ab)
858                         continue; /* audit_panic has been called */
859
860                 audit_log_format(ab, "item=%d", i);
861
862                 audit_log_format(ab, " name=");
863                 if (context->names[i].name)
864                         audit_log_untrustedstring(ab, context->names[i].name);
865                 else
866                         audit_log_format(ab, "(null)");
867
868                 if (pino != (unsigned long)-1)
869                         audit_log_format(ab, " parent=%lu",  pino);
870                 if (ino != (unsigned long)-1)
871                         audit_log_format(ab, " inode=%lu",  ino);
872                 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
873                         audit_log_format(ab, " dev=%02x:%02x mode=%#o" 
874                                          " ouid=%u ogid=%u rdev=%02x:%02x", 
875                                          MAJOR(context->names[i].dev), 
876                                          MINOR(context->names[i].dev), 
877                                          context->names[i].mode, 
878                                          context->names[i].uid, 
879                                          context->names[i].gid, 
880                                          MAJOR(context->names[i].rdev), 
881                                          MINOR(context->names[i].rdev));
882                 if (context->names[i].osid != 0) {
883                         char *ctx = NULL;
884                         u32 len;
885                         if (selinux_ctxid_to_string(
886                                 context->names[i].osid, &ctx, &len)) {
887                                 audit_log_format(ab, " osid=%u",
888                                                 context->names[i].osid);
889                                 call_panic = 2;
890                         } else
891                                 audit_log_format(ab, " obj=%s", ctx);
892                         kfree(ctx);
893                 }
894
895                 audit_log_end(ab);
896         }
897         if (call_panic)
898                 audit_panic("error converting sid to string");
899 }
900
901 /**
902  * audit_free - free a per-task audit context
903  * @tsk: task whose audit context block to free
904  *
905  * Called from copy_process and do_exit
906  */
907 void audit_free(struct task_struct *tsk)
908 {
909         struct audit_context *context;
910
911         context = audit_get_context(tsk, 0, 0);
912         if (likely(!context))
913                 return;
914
915         /* Check for system calls that do not go through the exit
916          * function (e.g., exit_group), then free context block. 
917          * We use GFP_ATOMIC here because we might be doing this 
918          * in the context of the idle thread */
919         /* that can happen only if we are called from do_exit() */
920         if (context->in_syscall && context->auditable)
921                 audit_log_exit(context, tsk);
922
923         audit_free_context(context);
924 }
925
926 /**
927  * audit_syscall_entry - fill in an audit record at syscall entry
928  * @tsk: task being audited
929  * @arch: architecture type
930  * @major: major syscall type (function)
931  * @a1: additional syscall register 1
932  * @a2: additional syscall register 2
933  * @a3: additional syscall register 3
934  * @a4: additional syscall register 4
935  *
936  * Fill in audit context at syscall entry.  This only happens if the
937  * audit context was created when the task was created and the state or
938  * filters demand the audit context be built.  If the state from the
939  * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
940  * then the record will be written at syscall exit time (otherwise, it
941  * will only be written if another part of the kernel requests that it
942  * be written).
943  */
944 void audit_syscall_entry(int arch, int major,
945                          unsigned long a1, unsigned long a2,
946                          unsigned long a3, unsigned long a4)
947 {
948         struct task_struct *tsk = current;
949         struct audit_context *context = tsk->audit_context;
950         enum audit_state     state;
951
952         BUG_ON(!context);
953
954         /*
955          * This happens only on certain architectures that make system
956          * calls in kernel_thread via the entry.S interface, instead of
957          * with direct calls.  (If you are porting to a new
958          * architecture, hitting this condition can indicate that you
959          * got the _exit/_leave calls backward in entry.S.)
960          *
961          * i386     no
962          * x86_64   no
963          * ppc64    yes (see arch/powerpc/platforms/iseries/misc.S)
964          *
965          * This also happens with vm86 emulation in a non-nested manner
966          * (entries without exits), so this case must be caught.
967          */
968         if (context->in_syscall) {
969                 struct audit_context *newctx;
970
971 #if AUDIT_DEBUG
972                 printk(KERN_ERR
973                        "audit(:%d) pid=%d in syscall=%d;"
974                        " entering syscall=%d\n",
975                        context->serial, tsk->pid, context->major, major);
976 #endif
977                 newctx = audit_alloc_context(context->state);
978                 if (newctx) {
979                         newctx->previous   = context;
980                         context            = newctx;
981                         tsk->audit_context = newctx;
982                 } else  {
983                         /* If we can't alloc a new context, the best we
984                          * can do is to leak memory (any pending putname
985                          * will be lost).  The only other alternative is
986                          * to abandon auditing. */
987                         audit_zero_context(context, context->state);
988                 }
989         }
990         BUG_ON(context->in_syscall || context->name_count);
991
992         if (!audit_enabled)
993                 return;
994
995         context->arch       = arch;
996         context->major      = major;
997         context->argv[0]    = a1;
998         context->argv[1]    = a2;
999         context->argv[2]    = a3;
1000         context->argv[3]    = a4;
1001
1002         state = context->state;
1003         if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
1004                 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1005         if (likely(state == AUDIT_DISABLED))
1006                 return;
1007
1008         context->serial     = 0;
1009         context->ctime      = CURRENT_TIME;
1010         context->in_syscall = 1;
1011         context->auditable  = !!(state == AUDIT_RECORD_CONTEXT);
1012 }
1013
1014 /**
1015  * audit_syscall_exit - deallocate audit context after a system call
1016  * @tsk: task being audited
1017  * @valid: success/failure flag
1018  * @return_code: syscall return value
1019  *
1020  * Tear down after system call.  If the audit context has been marked as
1021  * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1022  * filtering, or because some other part of the kernel write an audit
1023  * message), then write out the syscall information.  In call cases,
1024  * free the names stored from getname().
1025  */
1026 void audit_syscall_exit(int valid, long return_code)
1027 {
1028         struct task_struct *tsk = current;
1029         struct audit_context *context;
1030
1031         context = audit_get_context(tsk, valid, return_code);
1032
1033         if (likely(!context))
1034                 return;
1035
1036         if (context->in_syscall && context->auditable)
1037                 audit_log_exit(context, tsk);
1038
1039         context->in_syscall = 0;
1040         context->auditable  = 0;
1041
1042         if (context->previous) {
1043                 struct audit_context *new_context = context->previous;
1044                 context->previous  = NULL;
1045                 audit_free_context(context);
1046                 tsk->audit_context = new_context;
1047         } else {
1048                 audit_free_names(context);
1049                 audit_free_aux(context);
1050                 tsk->audit_context = context;
1051         }
1052 }
1053
1054 /**
1055  * audit_getname - add a name to the list
1056  * @name: name to add
1057  *
1058  * Add a name to the list of audit names for this context.
1059  * Called from fs/namei.c:getname().
1060  */
1061 void __audit_getname(const char *name)
1062 {
1063         struct audit_context *context = current->audit_context;
1064
1065         if (IS_ERR(name) || !name)
1066                 return;
1067
1068         if (!context->in_syscall) {
1069 #if AUDIT_DEBUG == 2
1070                 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1071                        __FILE__, __LINE__, context->serial, name);
1072                 dump_stack();
1073 #endif
1074                 return;
1075         }
1076         BUG_ON(context->name_count >= AUDIT_NAMES);
1077         context->names[context->name_count].name = name;
1078         context->names[context->name_count].ino  = (unsigned long)-1;
1079         ++context->name_count;
1080         if (!context->pwd) {
1081                 read_lock(&current->fs->lock);
1082                 context->pwd = dget(current->fs->pwd);
1083                 context->pwdmnt = mntget(current->fs->pwdmnt);
1084                 read_unlock(&current->fs->lock);
1085         }
1086                 
1087 }
1088
1089 /* audit_putname - intercept a putname request
1090  * @name: name to intercept and delay for putname
1091  *
1092  * If we have stored the name from getname in the audit context,
1093  * then we delay the putname until syscall exit.
1094  * Called from include/linux/fs.h:putname().
1095  */
1096 void audit_putname(const char *name)
1097 {
1098         struct audit_context *context = current->audit_context;
1099
1100         BUG_ON(!context);
1101         if (!context->in_syscall) {
1102 #if AUDIT_DEBUG == 2
1103                 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1104                        __FILE__, __LINE__, context->serial, name);
1105                 if (context->name_count) {
1106                         int i;
1107                         for (i = 0; i < context->name_count; i++)
1108                                 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1109                                        context->names[i].name,
1110                                        context->names[i].name ?: "(null)");
1111                 }
1112 #endif
1113                 __putname(name);
1114         }
1115 #if AUDIT_DEBUG
1116         else {
1117                 ++context->put_count;
1118                 if (context->put_count > context->name_count) {
1119                         printk(KERN_ERR "%s:%d(:%d): major=%d"
1120                                " in_syscall=%d putname(%p) name_count=%d"
1121                                " put_count=%d\n",
1122                                __FILE__, __LINE__,
1123                                context->serial, context->major,
1124                                context->in_syscall, name, context->name_count,
1125                                context->put_count);
1126                         dump_stack();
1127                 }
1128         }
1129 #endif
1130 }
1131
1132 static void audit_inode_context(int idx, const struct inode *inode)
1133 {
1134         struct audit_context *context = current->audit_context;
1135
1136         selinux_get_inode_sid(inode, &context->names[idx].osid);
1137 }
1138
1139
1140 /**
1141  * audit_inode - store the inode and device from a lookup
1142  * @name: name being audited
1143  * @inode: inode being audited
1144  * @flags: lookup flags (as used in path_lookup())
1145  *
1146  * Called from fs/namei.c:path_lookup().
1147  */
1148 void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1149 {
1150         int idx;
1151         struct audit_context *context = current->audit_context;
1152
1153         if (!context->in_syscall)
1154                 return;
1155         if (context->name_count
1156             && context->names[context->name_count-1].name
1157             && context->names[context->name_count-1].name == name)
1158                 idx = context->name_count - 1;
1159         else if (context->name_count > 1
1160                  && context->names[context->name_count-2].name
1161                  && context->names[context->name_count-2].name == name)
1162                 idx = context->name_count - 2;
1163         else {
1164                 /* FIXME: how much do we care about inodes that have no
1165                  * associated name? */
1166                 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1167                         return;
1168                 idx = context->name_count++;
1169                 context->names[idx].name = NULL;
1170 #if AUDIT_DEBUG
1171                 ++context->ino_count;
1172 #endif
1173         }
1174         context->names[idx].dev   = inode->i_sb->s_dev;
1175         context->names[idx].mode  = inode->i_mode;
1176         context->names[idx].uid   = inode->i_uid;
1177         context->names[idx].gid   = inode->i_gid;
1178         context->names[idx].rdev  = inode->i_rdev;
1179         audit_inode_context(idx, inode);
1180         if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) && 
1181             (strcmp(name, ".") != 0)) {
1182                 context->names[idx].ino   = (unsigned long)-1;
1183                 context->names[idx].pino  = inode->i_ino;
1184         } else {
1185                 context->names[idx].ino   = inode->i_ino;
1186                 context->names[idx].pino  = (unsigned long)-1;
1187         }
1188 }
1189
1190 /**
1191  * audit_inode_child - collect inode info for created/removed objects
1192  * @dname: inode's dentry name
1193  * @inode: inode being audited
1194  * @pino: inode number of dentry parent
1195  *
1196  * For syscalls that create or remove filesystem objects, audit_inode
1197  * can only collect information for the filesystem object's parent.
1198  * This call updates the audit context with the child's information.
1199  * Syscalls that create a new filesystem object must be hooked after
1200  * the object is created.  Syscalls that remove a filesystem object
1201  * must be hooked prior, in order to capture the target inode during
1202  * unsuccessful attempts.
1203  */
1204 void __audit_inode_child(const char *dname, const struct inode *inode,
1205                          unsigned long pino)
1206 {
1207         int idx;
1208         struct audit_context *context = current->audit_context;
1209
1210         if (!context->in_syscall)
1211                 return;
1212
1213         /* determine matching parent */
1214         if (!dname)
1215                 goto no_match;
1216         for (idx = 0; idx < context->name_count; idx++)
1217                 if (context->names[idx].pino == pino) {
1218                         const char *name = context->names[idx].name;
1219
1220                         if (!name)
1221                                 continue;
1222
1223                         if (audit_compare_dname_path(dname, name) == 0)
1224                                 goto update_context;
1225                 }
1226
1227 no_match:
1228         /* catch-all in case match not found */
1229         idx = context->name_count++;
1230         context->names[idx].name  = NULL;
1231         context->names[idx].pino  = pino;
1232 #if AUDIT_DEBUG
1233         context->ino_count++;
1234 #endif
1235
1236 update_context:
1237         if (inode) {
1238                 context->names[idx].ino   = inode->i_ino;
1239                 context->names[idx].dev   = inode->i_sb->s_dev;
1240                 context->names[idx].mode  = inode->i_mode;
1241                 context->names[idx].uid   = inode->i_uid;
1242                 context->names[idx].gid   = inode->i_gid;
1243                 context->names[idx].rdev  = inode->i_rdev;
1244                 audit_inode_context(idx, inode);
1245         }
1246 }
1247
1248 /**
1249  * auditsc_get_stamp - get local copies of audit_context values
1250  * @ctx: audit_context for the task
1251  * @t: timespec to store time recorded in the audit_context
1252  * @serial: serial value that is recorded in the audit_context
1253  *
1254  * Also sets the context as auditable.
1255  */
1256 void auditsc_get_stamp(struct audit_context *ctx,
1257                        struct timespec *t, unsigned int *serial)
1258 {
1259         if (!ctx->serial)
1260                 ctx->serial = audit_serial();
1261         t->tv_sec  = ctx->ctime.tv_sec;
1262         t->tv_nsec = ctx->ctime.tv_nsec;
1263         *serial    = ctx->serial;
1264         ctx->auditable = 1;
1265 }
1266
1267 /**
1268  * audit_set_loginuid - set a task's audit_context loginuid
1269  * @task: task whose audit context is being modified
1270  * @loginuid: loginuid value
1271  *
1272  * Returns 0.
1273  *
1274  * Called (set) from fs/proc/base.c::proc_loginuid_write().
1275  */
1276 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1277 {
1278         if (task->audit_context) {
1279                 struct audit_buffer *ab;
1280
1281                 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1282                 if (ab) {
1283                         audit_log_format(ab, "login pid=%d uid=%u "
1284                                 "old auid=%u new auid=%u",
1285                                 task->pid, task->uid, 
1286                                 task->audit_context->loginuid, loginuid);
1287                         audit_log_end(ab);
1288                 }
1289                 task->audit_context->loginuid = loginuid;
1290         }
1291         return 0;
1292 }
1293
1294 /**
1295  * audit_get_loginuid - get the loginuid for an audit_context
1296  * @ctx: the audit_context
1297  *
1298  * Returns the context's loginuid or -1 if @ctx is NULL.
1299  */
1300 uid_t audit_get_loginuid(struct audit_context *ctx)
1301 {
1302         return ctx ? ctx->loginuid : -1;
1303 }
1304
1305 /**
1306  * __audit_mq_open - record audit data for a POSIX MQ open
1307  * @oflag: open flag
1308  * @mode: mode bits
1309  * @u_attr: queue attributes
1310  *
1311  * Returns 0 for success or NULL context or < 0 on error.
1312  */
1313 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1314 {
1315         struct audit_aux_data_mq_open *ax;
1316         struct audit_context *context = current->audit_context;
1317
1318         if (!audit_enabled)
1319                 return 0;
1320
1321         if (likely(!context))
1322                 return 0;
1323
1324         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1325         if (!ax)
1326                 return -ENOMEM;
1327
1328         if (u_attr != NULL) {
1329                 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1330                         kfree(ax);
1331                         return -EFAULT;
1332                 }
1333         } else
1334                 memset(&ax->attr, 0, sizeof(ax->attr));
1335
1336         ax->oflag = oflag;
1337         ax->mode = mode;
1338
1339         ax->d.type = AUDIT_MQ_OPEN;
1340         ax->d.next = context->aux;
1341         context->aux = (void *)ax;
1342         return 0;
1343 }
1344
1345 /**
1346  * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1347  * @mqdes: MQ descriptor
1348  * @msg_len: Message length
1349  * @msg_prio: Message priority
1350  * @abs_timeout: Message timeout in absolute time
1351  *
1352  * Returns 0 for success or NULL context or < 0 on error.
1353  */
1354 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1355                         const struct timespec __user *u_abs_timeout)
1356 {
1357         struct audit_aux_data_mq_sendrecv *ax;
1358         struct audit_context *context = current->audit_context;
1359
1360         if (!audit_enabled)
1361                 return 0;
1362
1363         if (likely(!context))
1364                 return 0;
1365
1366         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1367         if (!ax)
1368                 return -ENOMEM;
1369
1370         if (u_abs_timeout != NULL) {
1371                 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1372                         kfree(ax);
1373                         return -EFAULT;
1374                 }
1375         } else
1376                 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1377
1378         ax->mqdes = mqdes;
1379         ax->msg_len = msg_len;
1380         ax->msg_prio = msg_prio;
1381
1382         ax->d.type = AUDIT_MQ_SENDRECV;
1383         ax->d.next = context->aux;
1384         context->aux = (void *)ax;
1385         return 0;
1386 }
1387
1388 /**
1389  * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1390  * @mqdes: MQ descriptor
1391  * @msg_len: Message length
1392  * @msg_prio: Message priority
1393  * @abs_timeout: Message timeout in absolute time
1394  *
1395  * Returns 0 for success or NULL context or < 0 on error.
1396  */
1397 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1398                                 unsigned int __user *u_msg_prio,
1399                                 const struct timespec __user *u_abs_timeout)
1400 {
1401         struct audit_aux_data_mq_sendrecv *ax;
1402         struct audit_context *context = current->audit_context;
1403
1404         if (!audit_enabled)
1405                 return 0;
1406
1407         if (likely(!context))
1408                 return 0;
1409
1410         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1411         if (!ax)
1412                 return -ENOMEM;
1413
1414         if (u_msg_prio != NULL) {
1415                 if (get_user(ax->msg_prio, u_msg_prio)) {
1416                         kfree(ax);
1417                         return -EFAULT;
1418                 }
1419         } else
1420                 ax->msg_prio = 0;
1421
1422         if (u_abs_timeout != NULL) {
1423                 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1424                         kfree(ax);
1425                         return -EFAULT;
1426                 }
1427         } else
1428                 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1429
1430         ax->mqdes = mqdes;
1431         ax->msg_len = msg_len;
1432
1433         ax->d.type = AUDIT_MQ_SENDRECV;
1434         ax->d.next = context->aux;
1435         context->aux = (void *)ax;
1436         return 0;
1437 }
1438
1439 /**
1440  * __audit_mq_notify - record audit data for a POSIX MQ notify
1441  * @mqdes: MQ descriptor
1442  * @u_notification: Notification event
1443  *
1444  * Returns 0 for success or NULL context or < 0 on error.
1445  */
1446
1447 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1448 {
1449         struct audit_aux_data_mq_notify *ax;
1450         struct audit_context *context = current->audit_context;
1451
1452         if (!audit_enabled)
1453                 return 0;
1454
1455         if (likely(!context))
1456                 return 0;
1457
1458         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1459         if (!ax)
1460                 return -ENOMEM;
1461
1462         if (u_notification != NULL) {
1463                 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1464                         kfree(ax);
1465                         return -EFAULT;
1466                 }
1467         } else
1468                 memset(&ax->notification, 0, sizeof(ax->notification));
1469
1470         ax->mqdes = mqdes;
1471
1472         ax->d.type = AUDIT_MQ_NOTIFY;
1473         ax->d.next = context->aux;
1474         context->aux = (void *)ax;
1475         return 0;
1476 }
1477
1478 /**
1479  * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1480  * @mqdes: MQ descriptor
1481  * @mqstat: MQ flags
1482  *
1483  * Returns 0 for success or NULL context or < 0 on error.
1484  */
1485 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1486 {
1487         struct audit_aux_data_mq_getsetattr *ax;
1488         struct audit_context *context = current->audit_context;
1489
1490         if (!audit_enabled)
1491                 return 0;
1492
1493         if (likely(!context))
1494                 return 0;
1495
1496         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1497         if (!ax)
1498                 return -ENOMEM;
1499
1500         ax->mqdes = mqdes;
1501         ax->mqstat = *mqstat;
1502
1503         ax->d.type = AUDIT_MQ_GETSETATTR;
1504         ax->d.next = context->aux;
1505         context->aux = (void *)ax;
1506         return 0;
1507 }
1508
1509 /**
1510  * audit_ipc_obj - record audit data for ipc object
1511  * @ipcp: ipc permissions
1512  *
1513  * Returns 0 for success or NULL context or < 0 on error.
1514  */
1515 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
1516 {
1517         struct audit_aux_data_ipcctl *ax;
1518         struct audit_context *context = current->audit_context;
1519
1520         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1521         if (!ax)
1522                 return -ENOMEM;
1523
1524         ax->uid = ipcp->uid;
1525         ax->gid = ipcp->gid;
1526         ax->mode = ipcp->mode;
1527         selinux_get_ipc_sid(ipcp, &ax->osid);
1528
1529         ax->d.type = AUDIT_IPC;
1530         ax->d.next = context->aux;
1531         context->aux = (void *)ax;
1532         return 0;
1533 }
1534
1535 /**
1536  * audit_ipc_set_perm - record audit data for new ipc permissions
1537  * @qbytes: msgq bytes
1538  * @uid: msgq user id
1539  * @gid: msgq group id
1540  * @mode: msgq mode (permissions)
1541  *
1542  * Returns 0 for success or NULL context or < 0 on error.
1543  */
1544 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1545 {
1546         struct audit_aux_data_ipcctl *ax;
1547         struct audit_context *context = current->audit_context;
1548
1549         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1550         if (!ax)
1551                 return -ENOMEM;
1552
1553         ax->qbytes = qbytes;
1554         ax->uid = uid;
1555         ax->gid = gid;
1556         ax->mode = mode;
1557
1558         ax->d.type = AUDIT_IPC_SET_PERM;
1559         ax->d.next = context->aux;
1560         context->aux = (void *)ax;
1561         return 0;
1562 }
1563
1564 int audit_bprm(struct linux_binprm *bprm)
1565 {
1566         struct audit_aux_data_execve *ax;
1567         struct audit_context *context = current->audit_context;
1568         unsigned long p, next;
1569         void *to;
1570
1571         if (likely(!audit_enabled || !context))
1572                 return 0;
1573
1574         ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1575                                 GFP_KERNEL);
1576         if (!ax)
1577                 return -ENOMEM;
1578
1579         ax->argc = bprm->argc;
1580         ax->envc = bprm->envc;
1581         for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1582                 struct page *page = bprm->page[p / PAGE_SIZE];
1583                 void *kaddr = kmap(page);
1584                 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1585                 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1586                 to += next - p;
1587                 kunmap(page);
1588         }
1589
1590         ax->d.type = AUDIT_EXECVE;
1591         ax->d.next = context->aux;
1592         context->aux = (void *)ax;
1593         return 0;
1594 }
1595
1596
1597 /**
1598  * audit_socketcall - record audit data for sys_socketcall
1599  * @nargs: number of args
1600  * @args: args array
1601  *
1602  * Returns 0 for success or NULL context or < 0 on error.
1603  */
1604 int audit_socketcall(int nargs, unsigned long *args)
1605 {
1606         struct audit_aux_data_socketcall *ax;
1607         struct audit_context *context = current->audit_context;
1608
1609         if (likely(!context))
1610                 return 0;
1611
1612         ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1613         if (!ax)
1614                 return -ENOMEM;
1615
1616         ax->nargs = nargs;
1617         memcpy(ax->args, args, nargs * sizeof(unsigned long));
1618
1619         ax->d.type = AUDIT_SOCKETCALL;
1620         ax->d.next = context->aux;
1621         context->aux = (void *)ax;
1622         return 0;
1623 }
1624
1625 /**
1626  * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1627  * @len: data length in user space
1628  * @a: data address in kernel space
1629  *
1630  * Returns 0 for success or NULL context or < 0 on error.
1631  */
1632 int audit_sockaddr(int len, void *a)
1633 {
1634         struct audit_aux_data_sockaddr *ax;
1635         struct audit_context *context = current->audit_context;
1636
1637         if (likely(!context))
1638                 return 0;
1639
1640         ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1641         if (!ax)
1642                 return -ENOMEM;
1643
1644         ax->len = len;
1645         memcpy(ax->a, a, len);
1646
1647         ax->d.type = AUDIT_SOCKADDR;
1648         ax->d.next = context->aux;
1649         context->aux = (void *)ax;
1650         return 0;
1651 }
1652
1653 /**
1654  * audit_avc_path - record the granting or denial of permissions
1655  * @dentry: dentry to record
1656  * @mnt: mnt to record
1657  *
1658  * Returns 0 for success or NULL context or < 0 on error.
1659  *
1660  * Called from security/selinux/avc.c::avc_audit()
1661  */
1662 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1663 {
1664         struct audit_aux_data_path *ax;
1665         struct audit_context *context = current->audit_context;
1666
1667         if (likely(!context))
1668                 return 0;
1669
1670         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1671         if (!ax)
1672                 return -ENOMEM;
1673
1674         ax->dentry = dget(dentry);
1675         ax->mnt = mntget(mnt);
1676
1677         ax->d.type = AUDIT_AVC_PATH;
1678         ax->d.next = context->aux;
1679         context->aux = (void *)ax;
1680         return 0;
1681 }
1682
1683 /**
1684  * audit_signal_info - record signal info for shutting down audit subsystem
1685  * @sig: signal value
1686  * @t: task being signaled
1687  *
1688  * If the audit subsystem is being terminated, record the task (pid)
1689  * and uid that is doing that.
1690  */
1691 void __audit_signal_info(int sig, struct task_struct *t)
1692 {
1693         extern pid_t audit_sig_pid;
1694         extern uid_t audit_sig_uid;
1695         extern u32 audit_sig_sid;
1696
1697         if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1698                 struct task_struct *tsk = current;
1699                 struct audit_context *ctx = tsk->audit_context;
1700                 audit_sig_pid = tsk->pid;
1701                 if (ctx)
1702                         audit_sig_uid = ctx->loginuid;
1703                 else
1704                         audit_sig_uid = tsk->uid;
1705                 selinux_get_task_sid(tsk, &audit_sig_sid);
1706         }
1707 }