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