[PATCH] get rid of loginuid races
[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 <linux/fs.h>
49 #include <linux/namei.h>
50 #include <linux/mm.h>
51 #include <linux/module.h>
52 #include <linux/mount.h>
53 #include <linux/socket.h>
54 #include <linux/mqueue.h>
55 #include <linux/audit.h>
56 #include <linux/personality.h>
57 #include <linux/time.h>
58 #include <linux/netlink.h>
59 #include <linux/compiler.h>
60 #include <asm/unistd.h>
61 #include <linux/security.h>
62 #include <linux/list.h>
63 #include <linux/tty.h>
64 #include <linux/selinux.h>
65 #include <linux/binfmts.h>
66 #include <linux/highmem.h>
67 #include <linux/syscalls.h>
68 #include <linux/inotify.h>
69
70 #include "audit.h"
71
72 extern struct list_head audit_filter_list[];
73
74 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
75  * for saving names from getname(). */
76 #define AUDIT_NAMES    20
77
78 /* Indicates that audit should log the full pathname. */
79 #define AUDIT_NAME_FULL -1
80
81 /* number of audit rules */
82 int audit_n_rules;
83
84 /* determines whether we collect data for signals sent */
85 int audit_signals;
86
87 /* When fs/namei.c:getname() is called, we store the pointer in name and
88  * we don't let putname() free it (instead we free all of the saved
89  * pointers at syscall exit time).
90  *
91  * Further, in fs/namei.c:path_lookup() we store the inode and device. */
92 struct audit_names {
93         const char      *name;
94         int             name_len;       /* number of name's characters to log */
95         unsigned        name_put;       /* call __putname() for this name */
96         unsigned long   ino;
97         dev_t           dev;
98         umode_t         mode;
99         uid_t           uid;
100         gid_t           gid;
101         dev_t           rdev;
102         u32             osid;
103 };
104
105 struct audit_aux_data {
106         struct audit_aux_data   *next;
107         int                     type;
108 };
109
110 #define AUDIT_AUX_IPCPERM       0
111
112 /* Number of target pids per aux struct. */
113 #define AUDIT_AUX_PIDS  16
114
115 struct audit_aux_data_mq_open {
116         struct audit_aux_data   d;
117         int                     oflag;
118         mode_t                  mode;
119         struct mq_attr          attr;
120 };
121
122 struct audit_aux_data_mq_sendrecv {
123         struct audit_aux_data   d;
124         mqd_t                   mqdes;
125         size_t                  msg_len;
126         unsigned int            msg_prio;
127         struct timespec         abs_timeout;
128 };
129
130 struct audit_aux_data_mq_notify {
131         struct audit_aux_data   d;
132         mqd_t                   mqdes;
133         struct sigevent         notification;
134 };
135
136 struct audit_aux_data_mq_getsetattr {
137         struct audit_aux_data   d;
138         mqd_t                   mqdes;
139         struct mq_attr          mqstat;
140 };
141
142 struct audit_aux_data_ipcctl {
143         struct audit_aux_data   d;
144         struct ipc_perm         p;
145         unsigned long           qbytes;
146         uid_t                   uid;
147         gid_t                   gid;
148         mode_t                  mode;
149         u32                     osid;
150 };
151
152 struct audit_aux_data_execve {
153         struct audit_aux_data   d;
154         int argc;
155         int envc;
156         struct mm_struct *mm;
157 };
158
159 struct audit_aux_data_socketcall {
160         struct audit_aux_data   d;
161         int                     nargs;
162         unsigned long           args[0];
163 };
164
165 struct audit_aux_data_sockaddr {
166         struct audit_aux_data   d;
167         int                     len;
168         char                    a[0];
169 };
170
171 struct audit_aux_data_fd_pair {
172         struct  audit_aux_data d;
173         int     fd[2];
174 };
175
176 struct audit_aux_data_pids {
177         struct audit_aux_data   d;
178         pid_t                   target_pid[AUDIT_AUX_PIDS];
179         u32                     target_sid[AUDIT_AUX_PIDS];
180         int                     pid_count;
181 };
182
183 struct audit_tree_refs {
184         struct audit_tree_refs *next;
185         struct audit_chunk *c[31];
186 };
187
188 /* The per-task audit context. */
189 struct audit_context {
190         int                 dummy;      /* must be the first element */
191         int                 in_syscall; /* 1 if task is in a syscall */
192         enum audit_state    state;
193         unsigned int        serial;     /* serial number for record */
194         struct timespec     ctime;      /* time of syscall entry */
195         int                 major;      /* syscall number */
196         unsigned long       argv[4];    /* syscall arguments */
197         int                 return_valid; /* return code is valid */
198         long                return_code;/* syscall return code */
199         int                 auditable;  /* 1 if record should be written */
200         int                 name_count;
201         struct audit_names  names[AUDIT_NAMES];
202         char *              filterkey;  /* key for rule that triggered record */
203         struct dentry *     pwd;
204         struct vfsmount *   pwdmnt;
205         struct audit_context *previous; /* For nested syscalls */
206         struct audit_aux_data *aux;
207         struct audit_aux_data *aux_pids;
208
209                                 /* Save things to print about task_struct */
210         pid_t               pid, ppid;
211         uid_t               uid, euid, suid, fsuid;
212         gid_t               gid, egid, sgid, fsgid;
213         unsigned long       personality;
214         int                 arch;
215
216         pid_t               target_pid;
217         u32                 target_sid;
218
219         struct audit_tree_refs *trees, *first_trees;
220         int tree_count;
221
222 #if AUDIT_DEBUG
223         int                 put_count;
224         int                 ino_count;
225 #endif
226 };
227
228 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
229 static inline int open_arg(int flags, int mask)
230 {
231         int n = ACC_MODE(flags);
232         if (flags & (O_TRUNC | O_CREAT))
233                 n |= AUDIT_PERM_WRITE;
234         return n & mask;
235 }
236
237 static int audit_match_perm(struct audit_context *ctx, int mask)
238 {
239         unsigned n = ctx->major;
240         switch (audit_classify_syscall(ctx->arch, n)) {
241         case 0: /* native */
242                 if ((mask & AUDIT_PERM_WRITE) &&
243                      audit_match_class(AUDIT_CLASS_WRITE, n))
244                         return 1;
245                 if ((mask & AUDIT_PERM_READ) &&
246                      audit_match_class(AUDIT_CLASS_READ, n))
247                         return 1;
248                 if ((mask & AUDIT_PERM_ATTR) &&
249                      audit_match_class(AUDIT_CLASS_CHATTR, n))
250                         return 1;
251                 return 0;
252         case 1: /* 32bit on biarch */
253                 if ((mask & AUDIT_PERM_WRITE) &&
254                      audit_match_class(AUDIT_CLASS_WRITE_32, n))
255                         return 1;
256                 if ((mask & AUDIT_PERM_READ) &&
257                      audit_match_class(AUDIT_CLASS_READ_32, n))
258                         return 1;
259                 if ((mask & AUDIT_PERM_ATTR) &&
260                      audit_match_class(AUDIT_CLASS_CHATTR_32, n))
261                         return 1;
262                 return 0;
263         case 2: /* open */
264                 return mask & ACC_MODE(ctx->argv[1]);
265         case 3: /* openat */
266                 return mask & ACC_MODE(ctx->argv[2]);
267         case 4: /* socketcall */
268                 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
269         case 5: /* execve */
270                 return mask & AUDIT_PERM_EXEC;
271         default:
272                 return 0;
273         }
274 }
275
276 /*
277  * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
278  * ->first_trees points to its beginning, ->trees - to the current end of data.
279  * ->tree_count is the number of free entries in array pointed to by ->trees.
280  * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
281  * "empty" becomes (p, p, 31) afterwards.  We don't shrink the list (and seriously,
282  * it's going to remain 1-element for almost any setup) until we free context itself.
283  * References in it _are_ dropped - at the same time we free/drop aux stuff.
284  */
285
286 #ifdef CONFIG_AUDIT_TREE
287 static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
288 {
289         struct audit_tree_refs *p = ctx->trees;
290         int left = ctx->tree_count;
291         if (likely(left)) {
292                 p->c[--left] = chunk;
293                 ctx->tree_count = left;
294                 return 1;
295         }
296         if (!p)
297                 return 0;
298         p = p->next;
299         if (p) {
300                 p->c[30] = chunk;
301                 ctx->trees = p;
302                 ctx->tree_count = 30;
303                 return 1;
304         }
305         return 0;
306 }
307
308 static int grow_tree_refs(struct audit_context *ctx)
309 {
310         struct audit_tree_refs *p = ctx->trees;
311         ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
312         if (!ctx->trees) {
313                 ctx->trees = p;
314                 return 0;
315         }
316         if (p)
317                 p->next = ctx->trees;
318         else
319                 ctx->first_trees = ctx->trees;
320         ctx->tree_count = 31;
321         return 1;
322 }
323 #endif
324
325 static void unroll_tree_refs(struct audit_context *ctx,
326                       struct audit_tree_refs *p, int count)
327 {
328 #ifdef CONFIG_AUDIT_TREE
329         struct audit_tree_refs *q;
330         int n;
331         if (!p) {
332                 /* we started with empty chain */
333                 p = ctx->first_trees;
334                 count = 31;
335                 /* if the very first allocation has failed, nothing to do */
336                 if (!p)
337                         return;
338         }
339         n = count;
340         for (q = p; q != ctx->trees; q = q->next, n = 31) {
341                 while (n--) {
342                         audit_put_chunk(q->c[n]);
343                         q->c[n] = NULL;
344                 }
345         }
346         while (n-- > ctx->tree_count) {
347                 audit_put_chunk(q->c[n]);
348                 q->c[n] = NULL;
349         }
350         ctx->trees = p;
351         ctx->tree_count = count;
352 #endif
353 }
354
355 static void free_tree_refs(struct audit_context *ctx)
356 {
357         struct audit_tree_refs *p, *q;
358         for (p = ctx->first_trees; p; p = q) {
359                 q = p->next;
360                 kfree(p);
361         }
362 }
363
364 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
365 {
366 #ifdef CONFIG_AUDIT_TREE
367         struct audit_tree_refs *p;
368         int n;
369         if (!tree)
370                 return 0;
371         /* full ones */
372         for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
373                 for (n = 0; n < 31; n++)
374                         if (audit_tree_match(p->c[n], tree))
375                                 return 1;
376         }
377         /* partial */
378         if (p) {
379                 for (n = ctx->tree_count; n < 31; n++)
380                         if (audit_tree_match(p->c[n], tree))
381                                 return 1;
382         }
383 #endif
384         return 0;
385 }
386
387 /* Determine if any context name data matches a rule's watch data */
388 /* Compare a task_struct with an audit_rule.  Return 1 on match, 0
389  * otherwise. */
390 static int audit_filter_rules(struct task_struct *tsk,
391                               struct audit_krule *rule,
392                               struct audit_context *ctx,
393                               struct audit_names *name,
394                               enum audit_state *state)
395 {
396         int i, j, need_sid = 1;
397         u32 sid;
398
399         for (i = 0; i < rule->field_count; i++) {
400                 struct audit_field *f = &rule->fields[i];
401                 int result = 0;
402
403                 switch (f->type) {
404                 case AUDIT_PID:
405                         result = audit_comparator(tsk->pid, f->op, f->val);
406                         break;
407                 case AUDIT_PPID:
408                         if (ctx) {
409                                 if (!ctx->ppid)
410                                         ctx->ppid = sys_getppid();
411                                 result = audit_comparator(ctx->ppid, f->op, f->val);
412                         }
413                         break;
414                 case AUDIT_UID:
415                         result = audit_comparator(tsk->uid, f->op, f->val);
416                         break;
417                 case AUDIT_EUID:
418                         result = audit_comparator(tsk->euid, f->op, f->val);
419                         break;
420                 case AUDIT_SUID:
421                         result = audit_comparator(tsk->suid, f->op, f->val);
422                         break;
423                 case AUDIT_FSUID:
424                         result = audit_comparator(tsk->fsuid, f->op, f->val);
425                         break;
426                 case AUDIT_GID:
427                         result = audit_comparator(tsk->gid, f->op, f->val);
428                         break;
429                 case AUDIT_EGID:
430                         result = audit_comparator(tsk->egid, f->op, f->val);
431                         break;
432                 case AUDIT_SGID:
433                         result = audit_comparator(tsk->sgid, f->op, f->val);
434                         break;
435                 case AUDIT_FSGID:
436                         result = audit_comparator(tsk->fsgid, f->op, f->val);
437                         break;
438                 case AUDIT_PERS:
439                         result = audit_comparator(tsk->personality, f->op, f->val);
440                         break;
441                 case AUDIT_ARCH:
442                         if (ctx)
443                                 result = audit_comparator(ctx->arch, f->op, f->val);
444                         break;
445
446                 case AUDIT_EXIT:
447                         if (ctx && ctx->return_valid)
448                                 result = audit_comparator(ctx->return_code, f->op, f->val);
449                         break;
450                 case AUDIT_SUCCESS:
451                         if (ctx && ctx->return_valid) {
452                                 if (f->val)
453                                         result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
454                                 else
455                                         result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
456                         }
457                         break;
458                 case AUDIT_DEVMAJOR:
459                         if (name)
460                                 result = audit_comparator(MAJOR(name->dev),
461                                                           f->op, f->val);
462                         else if (ctx) {
463                                 for (j = 0; j < ctx->name_count; j++) {
464                                         if (audit_comparator(MAJOR(ctx->names[j].dev),  f->op, f->val)) {
465                                                 ++result;
466                                                 break;
467                                         }
468                                 }
469                         }
470                         break;
471                 case AUDIT_DEVMINOR:
472                         if (name)
473                                 result = audit_comparator(MINOR(name->dev),
474                                                           f->op, f->val);
475                         else if (ctx) {
476                                 for (j = 0; j < ctx->name_count; j++) {
477                                         if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
478                                                 ++result;
479                                                 break;
480                                         }
481                                 }
482                         }
483                         break;
484                 case AUDIT_INODE:
485                         if (name)
486                                 result = (name->ino == f->val);
487                         else if (ctx) {
488                                 for (j = 0; j < ctx->name_count; j++) {
489                                         if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
490                                                 ++result;
491                                                 break;
492                                         }
493                                 }
494                         }
495                         break;
496                 case AUDIT_WATCH:
497                         if (name && rule->watch->ino != (unsigned long)-1)
498                                 result = (name->dev == rule->watch->dev &&
499                                           name->ino == rule->watch->ino);
500                         break;
501                 case AUDIT_DIR:
502                         if (ctx)
503                                 result = match_tree_refs(ctx, rule->tree);
504                         break;
505                 case AUDIT_LOGINUID:
506                         result = 0;
507                         if (ctx)
508                                 result = audit_comparator(tsk->loginuid, f->op, f->val);
509                         break;
510                 case AUDIT_SUBJ_USER:
511                 case AUDIT_SUBJ_ROLE:
512                 case AUDIT_SUBJ_TYPE:
513                 case AUDIT_SUBJ_SEN:
514                 case AUDIT_SUBJ_CLR:
515                         /* NOTE: this may return negative values indicating
516                            a temporary error.  We simply treat this as a
517                            match for now to avoid losing information that
518                            may be wanted.   An error message will also be
519                            logged upon error */
520                         if (f->se_rule) {
521                                 if (need_sid) {
522                                         selinux_get_task_sid(tsk, &sid);
523                                         need_sid = 0;
524                                 }
525                                 result = selinux_audit_rule_match(sid, f->type,
526                                                                   f->op,
527                                                                   f->se_rule,
528                                                                   ctx);
529                         }
530                         break;
531                 case AUDIT_OBJ_USER:
532                 case AUDIT_OBJ_ROLE:
533                 case AUDIT_OBJ_TYPE:
534                 case AUDIT_OBJ_LEV_LOW:
535                 case AUDIT_OBJ_LEV_HIGH:
536                         /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
537                            also applies here */
538                         if (f->se_rule) {
539                                 /* Find files that match */
540                                 if (name) {
541                                         result = selinux_audit_rule_match(
542                                                    name->osid, f->type, f->op,
543                                                    f->se_rule, ctx);
544                                 } else if (ctx) {
545                                         for (j = 0; j < ctx->name_count; j++) {
546                                                 if (selinux_audit_rule_match(
547                                                       ctx->names[j].osid,
548                                                       f->type, f->op,
549                                                       f->se_rule, ctx)) {
550                                                         ++result;
551                                                         break;
552                                                 }
553                                         }
554                                 }
555                                 /* Find ipc objects that match */
556                                 if (ctx) {
557                                         struct audit_aux_data *aux;
558                                         for (aux = ctx->aux; aux;
559                                              aux = aux->next) {
560                                                 if (aux->type == AUDIT_IPC) {
561                                                         struct audit_aux_data_ipcctl *axi = (void *)aux;
562                                                         if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
563                                                                 ++result;
564                                                                 break;
565                                                         }
566                                                 }
567                                         }
568                                 }
569                         }
570                         break;
571                 case AUDIT_ARG0:
572                 case AUDIT_ARG1:
573                 case AUDIT_ARG2:
574                 case AUDIT_ARG3:
575                         if (ctx)
576                                 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
577                         break;
578                 case AUDIT_FILTERKEY:
579                         /* ignore this field for filtering */
580                         result = 1;
581                         break;
582                 case AUDIT_PERM:
583                         result = audit_match_perm(ctx, f->val);
584                         break;
585                 }
586
587                 if (!result)
588                         return 0;
589         }
590         if (rule->filterkey)
591                 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
592         switch (rule->action) {
593         case AUDIT_NEVER:    *state = AUDIT_DISABLED;       break;
594         case AUDIT_ALWAYS:   *state = AUDIT_RECORD_CONTEXT; break;
595         }
596         return 1;
597 }
598
599 /* At process creation time, we can determine if system-call auditing is
600  * completely disabled for this task.  Since we only have the task
601  * structure at this point, we can only check uid and gid.
602  */
603 static enum audit_state audit_filter_task(struct task_struct *tsk)
604 {
605         struct audit_entry *e;
606         enum audit_state   state;
607
608         rcu_read_lock();
609         list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
610                 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
611                         rcu_read_unlock();
612                         return state;
613                 }
614         }
615         rcu_read_unlock();
616         return AUDIT_BUILD_CONTEXT;
617 }
618
619 /* At syscall entry and exit time, this filter is called if the
620  * audit_state is not low enough that auditing cannot take place, but is
621  * also not high enough that we already know we have to write an audit
622  * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
623  */
624 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
625                                              struct audit_context *ctx,
626                                              struct list_head *list)
627 {
628         struct audit_entry *e;
629         enum audit_state state;
630
631         if (audit_pid && tsk->tgid == audit_pid)
632                 return AUDIT_DISABLED;
633
634         rcu_read_lock();
635         if (!list_empty(list)) {
636                 int word = AUDIT_WORD(ctx->major);
637                 int bit  = AUDIT_BIT(ctx->major);
638
639                 list_for_each_entry_rcu(e, list, list) {
640                         if ((e->rule.mask[word] & bit) == bit &&
641                             audit_filter_rules(tsk, &e->rule, ctx, NULL,
642                                                &state)) {
643                                 rcu_read_unlock();
644                                 return state;
645                         }
646                 }
647         }
648         rcu_read_unlock();
649         return AUDIT_BUILD_CONTEXT;
650 }
651
652 /* At syscall exit time, this filter is called if any audit_names[] have been
653  * collected during syscall processing.  We only check rules in sublists at hash
654  * buckets applicable to the inode numbers in audit_names[].
655  * Regarding audit_state, same rules apply as for audit_filter_syscall().
656  */
657 enum audit_state audit_filter_inodes(struct task_struct *tsk,
658                                      struct audit_context *ctx)
659 {
660         int i;
661         struct audit_entry *e;
662         enum audit_state state;
663
664         if (audit_pid && tsk->tgid == audit_pid)
665                 return AUDIT_DISABLED;
666
667         rcu_read_lock();
668         for (i = 0; i < ctx->name_count; i++) {
669                 int word = AUDIT_WORD(ctx->major);
670                 int bit  = AUDIT_BIT(ctx->major);
671                 struct audit_names *n = &ctx->names[i];
672                 int h = audit_hash_ino((u32)n->ino);
673                 struct list_head *list = &audit_inode_hash[h];
674
675                 if (list_empty(list))
676                         continue;
677
678                 list_for_each_entry_rcu(e, list, list) {
679                         if ((e->rule.mask[word] & bit) == bit &&
680                             audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
681                                 rcu_read_unlock();
682                                 return state;
683                         }
684                 }
685         }
686         rcu_read_unlock();
687         return AUDIT_BUILD_CONTEXT;
688 }
689
690 void audit_set_auditable(struct audit_context *ctx)
691 {
692         ctx->auditable = 1;
693 }
694
695 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
696                                                       int return_valid,
697                                                       int return_code)
698 {
699         struct audit_context *context = tsk->audit_context;
700
701         if (likely(!context))
702                 return NULL;
703         context->return_valid = return_valid;
704         context->return_code  = return_code;
705
706         if (context->in_syscall && !context->dummy && !context->auditable) {
707                 enum audit_state state;
708
709                 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
710                 if (state == AUDIT_RECORD_CONTEXT) {
711                         context->auditable = 1;
712                         goto get_context;
713                 }
714
715                 state = audit_filter_inodes(tsk, context);
716                 if (state == AUDIT_RECORD_CONTEXT)
717                         context->auditable = 1;
718
719         }
720
721 get_context:
722
723         tsk->audit_context = NULL;
724         return context;
725 }
726
727 static inline void audit_free_names(struct audit_context *context)
728 {
729         int i;
730
731 #if AUDIT_DEBUG == 2
732         if (context->auditable
733             ||context->put_count + context->ino_count != context->name_count) {
734                 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
735                        " name_count=%d put_count=%d"
736                        " ino_count=%d [NOT freeing]\n",
737                        __FILE__, __LINE__,
738                        context->serial, context->major, context->in_syscall,
739                        context->name_count, context->put_count,
740                        context->ino_count);
741                 for (i = 0; i < context->name_count; i++) {
742                         printk(KERN_ERR "names[%d] = %p = %s\n", i,
743                                context->names[i].name,
744                                context->names[i].name ?: "(null)");
745                 }
746                 dump_stack();
747                 return;
748         }
749 #endif
750 #if AUDIT_DEBUG
751         context->put_count  = 0;
752         context->ino_count  = 0;
753 #endif
754
755         for (i = 0; i < context->name_count; i++) {
756                 if (context->names[i].name && context->names[i].name_put)
757                         __putname(context->names[i].name);
758         }
759         context->name_count = 0;
760         if (context->pwd)
761                 dput(context->pwd);
762         if (context->pwdmnt)
763                 mntput(context->pwdmnt);
764         context->pwd = NULL;
765         context->pwdmnt = NULL;
766 }
767
768 static inline void audit_free_aux(struct audit_context *context)
769 {
770         struct audit_aux_data *aux;
771
772         while ((aux = context->aux)) {
773                 context->aux = aux->next;
774                 kfree(aux);
775         }
776         while ((aux = context->aux_pids)) {
777                 context->aux_pids = aux->next;
778                 kfree(aux);
779         }
780 }
781
782 static inline void audit_zero_context(struct audit_context *context,
783                                       enum audit_state state)
784 {
785         memset(context, 0, sizeof(*context));
786         context->state      = state;
787 }
788
789 static inline struct audit_context *audit_alloc_context(enum audit_state state)
790 {
791         struct audit_context *context;
792
793         if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
794                 return NULL;
795         audit_zero_context(context, state);
796         return context;
797 }
798
799 /**
800  * audit_alloc - allocate an audit context block for a task
801  * @tsk: task
802  *
803  * Filter on the task information and allocate a per-task audit context
804  * if necessary.  Doing so turns on system call auditing for the
805  * specified task.  This is called from copy_process, so no lock is
806  * needed.
807  */
808 int audit_alloc(struct task_struct *tsk)
809 {
810         struct audit_context *context;
811         enum audit_state     state;
812
813         if (likely(!audit_enabled))
814                 return 0; /* Return if not auditing. */
815
816         state = audit_filter_task(tsk);
817         if (likely(state == AUDIT_DISABLED))
818                 return 0;
819
820         if (!(context = audit_alloc_context(state))) {
821                 audit_log_lost("out of memory in audit_alloc");
822                 return -ENOMEM;
823         }
824
825         tsk->audit_context  = context;
826         set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
827         return 0;
828 }
829
830 static inline void audit_free_context(struct audit_context *context)
831 {
832         struct audit_context *previous;
833         int                  count = 0;
834
835         do {
836                 previous = context->previous;
837                 if (previous || (count &&  count < 10)) {
838                         ++count;
839                         printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
840                                " freeing multiple contexts (%d)\n",
841                                context->serial, context->major,
842                                context->name_count, count);
843                 }
844                 audit_free_names(context);
845                 unroll_tree_refs(context, NULL, 0);
846                 free_tree_refs(context);
847                 audit_free_aux(context);
848                 kfree(context->filterkey);
849                 kfree(context);
850                 context  = previous;
851         } while (context);
852         if (count >= 10)
853                 printk(KERN_ERR "audit: freed %d contexts\n", count);
854 }
855
856 void audit_log_task_context(struct audit_buffer *ab)
857 {
858         char *ctx = NULL;
859         unsigned len;
860         int error;
861         u32 sid;
862
863         selinux_get_task_sid(current, &sid);
864         if (!sid)
865                 return;
866
867         error = selinux_sid_to_string(sid, &ctx, &len);
868         if (error) {
869                 if (error != -EINVAL)
870                         goto error_path;
871                 return;
872         }
873
874         audit_log_format(ab, " subj=%s", ctx);
875         kfree(ctx);
876         return;
877
878 error_path:
879         audit_panic("error in audit_log_task_context");
880         return;
881 }
882
883 EXPORT_SYMBOL(audit_log_task_context);
884
885 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
886 {
887         char name[sizeof(tsk->comm)];
888         struct mm_struct *mm = tsk->mm;
889         struct vm_area_struct *vma;
890
891         /* tsk == current */
892
893         get_task_comm(name, tsk);
894         audit_log_format(ab, " comm=");
895         audit_log_untrustedstring(ab, name);
896
897         if (mm) {
898                 down_read(&mm->mmap_sem);
899                 vma = mm->mmap;
900                 while (vma) {
901                         if ((vma->vm_flags & VM_EXECUTABLE) &&
902                             vma->vm_file) {
903                                 audit_log_d_path(ab, "exe=",
904                                                  vma->vm_file->f_path.dentry,
905                                                  vma->vm_file->f_path.mnt);
906                                 break;
907                         }
908                         vma = vma->vm_next;
909                 }
910                 up_read(&mm->mmap_sem);
911         }
912         audit_log_task_context(ab);
913 }
914
915 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
916                                  u32 sid)
917 {
918         struct audit_buffer *ab;
919         char *s = NULL;
920         u32 len;
921         int rc = 0;
922
923         ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
924         if (!ab)
925                 return 1;
926
927         if (selinux_sid_to_string(sid, &s, &len)) {
928                 audit_log_format(ab, "opid=%d obj=(none)", pid);
929                 rc = 1;
930         } else
931                 audit_log_format(ab, "opid=%d  obj=%s", pid, s);
932         audit_log_end(ab);
933         kfree(s);
934
935         return rc;
936 }
937
938 static void audit_log_execve_info(struct audit_buffer *ab,
939                 struct audit_aux_data_execve *axi)
940 {
941         int i;
942         long len, ret;
943         const char __user *p;
944         char *buf;
945
946         if (axi->mm != current->mm)
947                 return; /* execve failed, no additional info */
948
949         p = (const char __user *)axi->mm->arg_start;
950
951         for (i = 0; i < axi->argc; i++, p += len) {
952                 len = strnlen_user(p, MAX_ARG_STRLEN);
953                 /*
954                  * We just created this mm, if we can't find the strings
955                  * we just copied into it something is _very_ wrong. Similar
956                  * for strings that are too long, we should not have created
957                  * any.
958                  */
959                 if (!len || len > MAX_ARG_STRLEN) {
960                         WARN_ON(1);
961                         send_sig(SIGKILL, current, 0);
962                 }
963
964                 buf = kmalloc(len, GFP_KERNEL);
965                 if (!buf) {
966                         audit_panic("out of memory for argv string\n");
967                         break;
968                 }
969
970                 ret = copy_from_user(buf, p, len);
971                 /*
972                  * There is no reason for this copy to be short. We just
973                  * copied them here, and the mm hasn't been exposed to user-
974                  * space yet.
975                  */
976                 if (ret) {
977                         WARN_ON(1);
978                         send_sig(SIGKILL, current, 0);
979                 }
980
981                 audit_log_format(ab, "a%d=", i);
982                 audit_log_untrustedstring(ab, buf);
983                 audit_log_format(ab, "\n");
984
985                 kfree(buf);
986         }
987 }
988
989 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
990 {
991         int i, call_panic = 0;
992         struct audit_buffer *ab;
993         struct audit_aux_data *aux;
994         const char *tty;
995
996         /* tsk == current */
997         context->pid = tsk->pid;
998         if (!context->ppid)
999                 context->ppid = sys_getppid();
1000         context->uid = tsk->uid;
1001         context->gid = tsk->gid;
1002         context->euid = tsk->euid;
1003         context->suid = tsk->suid;
1004         context->fsuid = tsk->fsuid;
1005         context->egid = tsk->egid;
1006         context->sgid = tsk->sgid;
1007         context->fsgid = tsk->fsgid;
1008         context->personality = tsk->personality;
1009
1010         ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
1011         if (!ab)
1012                 return;         /* audit_panic has been called */
1013         audit_log_format(ab, "arch=%x syscall=%d",
1014                          context->arch, context->major);
1015         if (context->personality != PER_LINUX)
1016                 audit_log_format(ab, " per=%lx", context->personality);
1017         if (context->return_valid)
1018                 audit_log_format(ab, " success=%s exit=%ld",
1019                                  (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1020                                  context->return_code);
1021
1022         mutex_lock(&tty_mutex);
1023         read_lock(&tasklist_lock);
1024         if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1025                 tty = tsk->signal->tty->name;
1026         else
1027                 tty = "(none)";
1028         read_unlock(&tasklist_lock);
1029         audit_log_format(ab,
1030                   " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1031                   " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1032                   " euid=%u suid=%u fsuid=%u"
1033                   " egid=%u sgid=%u fsgid=%u tty=%s",
1034                   context->argv[0],
1035                   context->argv[1],
1036                   context->argv[2],
1037                   context->argv[3],
1038                   context->name_count,
1039                   context->ppid,
1040                   context->pid,
1041                   tsk->loginuid,
1042                   context->uid,
1043                   context->gid,
1044                   context->euid, context->suid, context->fsuid,
1045                   context->egid, context->sgid, context->fsgid, tty);
1046
1047         mutex_unlock(&tty_mutex);
1048
1049         audit_log_task_info(ab, tsk);
1050         if (context->filterkey) {
1051                 audit_log_format(ab, " key=");
1052                 audit_log_untrustedstring(ab, context->filterkey);
1053         } else
1054                 audit_log_format(ab, " key=(null)");
1055         audit_log_end(ab);
1056
1057         for (aux = context->aux; aux; aux = aux->next) {
1058
1059                 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1060                 if (!ab)
1061                         continue; /* audit_panic has been called */
1062
1063                 switch (aux->type) {
1064                 case AUDIT_MQ_OPEN: {
1065                         struct audit_aux_data_mq_open *axi = (void *)aux;
1066                         audit_log_format(ab,
1067                                 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1068                                 "mq_msgsize=%ld mq_curmsgs=%ld",
1069                                 axi->oflag, axi->mode, axi->attr.mq_flags,
1070                                 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1071                                 axi->attr.mq_curmsgs);
1072                         break; }
1073
1074                 case AUDIT_MQ_SENDRECV: {
1075                         struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1076                         audit_log_format(ab,
1077                                 "mqdes=%d msg_len=%zd msg_prio=%u "
1078                                 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1079                                 axi->mqdes, axi->msg_len, axi->msg_prio,
1080                                 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1081                         break; }
1082
1083                 case AUDIT_MQ_NOTIFY: {
1084                         struct audit_aux_data_mq_notify *axi = (void *)aux;
1085                         audit_log_format(ab,
1086                                 "mqdes=%d sigev_signo=%d",
1087                                 axi->mqdes,
1088                                 axi->notification.sigev_signo);
1089                         break; }
1090
1091                 case AUDIT_MQ_GETSETATTR: {
1092                         struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1093                         audit_log_format(ab,
1094                                 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1095                                 "mq_curmsgs=%ld ",
1096                                 axi->mqdes,
1097                                 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1098                                 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1099                         break; }
1100
1101                 case AUDIT_IPC: {
1102                         struct audit_aux_data_ipcctl *axi = (void *)aux;
1103                         audit_log_format(ab, 
1104                                  "ouid=%u ogid=%u mode=%#o",
1105                                  axi->uid, axi->gid, axi->mode);
1106                         if (axi->osid != 0) {
1107                                 char *ctx = NULL;
1108                                 u32 len;
1109                                 if (selinux_sid_to_string(
1110                                                 axi->osid, &ctx, &len)) {
1111                                         audit_log_format(ab, " osid=%u",
1112                                                         axi->osid);
1113                                         call_panic = 1;
1114                                 } else
1115                                         audit_log_format(ab, " obj=%s", ctx);
1116                                 kfree(ctx);
1117                         }
1118                         break; }
1119
1120                 case AUDIT_IPC_SET_PERM: {
1121                         struct audit_aux_data_ipcctl *axi = (void *)aux;
1122                         audit_log_format(ab,
1123                                 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1124                                 axi->qbytes, axi->uid, axi->gid, axi->mode);
1125                         break; }
1126
1127                 case AUDIT_EXECVE: {
1128                         struct audit_aux_data_execve *axi = (void *)aux;
1129                         audit_log_execve_info(ab, axi);
1130                         break; }
1131
1132                 case AUDIT_SOCKETCALL: {
1133                         int i;
1134                         struct audit_aux_data_socketcall *axs = (void *)aux;
1135                         audit_log_format(ab, "nargs=%d", axs->nargs);
1136                         for (i=0; i<axs->nargs; i++)
1137                                 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1138                         break; }
1139
1140                 case AUDIT_SOCKADDR: {
1141                         struct audit_aux_data_sockaddr *axs = (void *)aux;
1142
1143                         audit_log_format(ab, "saddr=");
1144                         audit_log_hex(ab, axs->a, axs->len);
1145                         break; }
1146
1147                 case AUDIT_FD_PAIR: {
1148                         struct audit_aux_data_fd_pair *axs = (void *)aux;
1149                         audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1150                         break; }
1151
1152                 }
1153                 audit_log_end(ab);
1154         }
1155
1156         for (aux = context->aux_pids; aux; aux = aux->next) {
1157                 struct audit_aux_data_pids *axs = (void *)aux;
1158                 int i;
1159
1160                 for (i = 0; i < axs->pid_count; i++)
1161                         if (audit_log_pid_context(context, axs->target_pid[i],
1162                                                   axs->target_sid[i]))
1163                                 call_panic = 1;
1164         }
1165
1166         if (context->target_pid &&
1167             audit_log_pid_context(context, context->target_pid,
1168                                   context->target_sid))
1169                         call_panic = 1;
1170
1171         if (context->pwd && context->pwdmnt) {
1172                 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1173                 if (ab) {
1174                         audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1175                         audit_log_end(ab);
1176                 }
1177         }
1178         for (i = 0; i < context->name_count; i++) {
1179                 struct audit_names *n = &context->names[i];
1180
1181                 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1182                 if (!ab)
1183                         continue; /* audit_panic has been called */
1184
1185                 audit_log_format(ab, "item=%d", i);
1186
1187                 if (n->name) {
1188                         switch(n->name_len) {
1189                         case AUDIT_NAME_FULL:
1190                                 /* log the full path */
1191                                 audit_log_format(ab, " name=");
1192                                 audit_log_untrustedstring(ab, n->name);
1193                                 break;
1194                         case 0:
1195                                 /* name was specified as a relative path and the
1196                                  * directory component is the cwd */
1197                                 audit_log_d_path(ab, " name=", context->pwd,
1198                                                  context->pwdmnt);
1199                                 break;
1200                         default:
1201                                 /* log the name's directory component */
1202                                 audit_log_format(ab, " name=");
1203                                 audit_log_n_untrustedstring(ab, n->name_len,
1204                                                             n->name);
1205                         }
1206                 } else
1207                         audit_log_format(ab, " name=(null)");
1208
1209                 if (n->ino != (unsigned long)-1) {
1210                         audit_log_format(ab, " inode=%lu"
1211                                          " dev=%02x:%02x mode=%#o"
1212                                          " ouid=%u ogid=%u rdev=%02x:%02x",
1213                                          n->ino,
1214                                          MAJOR(n->dev),
1215                                          MINOR(n->dev),
1216                                          n->mode,
1217                                          n->uid,
1218                                          n->gid,
1219                                          MAJOR(n->rdev),
1220                                          MINOR(n->rdev));
1221                 }
1222                 if (n->osid != 0) {
1223                         char *ctx = NULL;
1224                         u32 len;
1225                         if (selinux_sid_to_string(
1226                                 n->osid, &ctx, &len)) {
1227                                 audit_log_format(ab, " osid=%u", n->osid);
1228                                 call_panic = 2;
1229                         } else
1230                                 audit_log_format(ab, " obj=%s", ctx);
1231                         kfree(ctx);
1232                 }
1233
1234                 audit_log_end(ab);
1235         }
1236         if (call_panic)
1237                 audit_panic("error converting sid to string");
1238 }
1239
1240 /**
1241  * audit_free - free a per-task audit context
1242  * @tsk: task whose audit context block to free
1243  *
1244  * Called from copy_process and do_exit
1245  */
1246 void audit_free(struct task_struct *tsk)
1247 {
1248         struct audit_context *context;
1249
1250         context = audit_get_context(tsk, 0, 0);
1251         if (likely(!context))
1252                 return;
1253
1254         /* Check for system calls that do not go through the exit
1255          * function (e.g., exit_group), then free context block.
1256          * We use GFP_ATOMIC here because we might be doing this
1257          * in the context of the idle thread */
1258         /* that can happen only if we are called from do_exit() */
1259         if (context->in_syscall && context->auditable)
1260                 audit_log_exit(context, tsk);
1261
1262         audit_free_context(context);
1263 }
1264
1265 /**
1266  * audit_syscall_entry - fill in an audit record at syscall entry
1267  * @tsk: task being audited
1268  * @arch: architecture type
1269  * @major: major syscall type (function)
1270  * @a1: additional syscall register 1
1271  * @a2: additional syscall register 2
1272  * @a3: additional syscall register 3
1273  * @a4: additional syscall register 4
1274  *
1275  * Fill in audit context at syscall entry.  This only happens if the
1276  * audit context was created when the task was created and the state or
1277  * filters demand the audit context be built.  If the state from the
1278  * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1279  * then the record will be written at syscall exit time (otherwise, it
1280  * will only be written if another part of the kernel requests that it
1281  * be written).
1282  */
1283 void audit_syscall_entry(int arch, int major,
1284                          unsigned long a1, unsigned long a2,
1285                          unsigned long a3, unsigned long a4)
1286 {
1287         struct task_struct *tsk = current;
1288         struct audit_context *context = tsk->audit_context;
1289         enum audit_state     state;
1290
1291         BUG_ON(!context);
1292
1293         /*
1294          * This happens only on certain architectures that make system
1295          * calls in kernel_thread via the entry.S interface, instead of
1296          * with direct calls.  (If you are porting to a new
1297          * architecture, hitting this condition can indicate that you
1298          * got the _exit/_leave calls backward in entry.S.)
1299          *
1300          * i386     no
1301          * x86_64   no
1302          * ppc64    yes (see arch/powerpc/platforms/iseries/misc.S)
1303          *
1304          * This also happens with vm86 emulation in a non-nested manner
1305          * (entries without exits), so this case must be caught.
1306          */
1307         if (context->in_syscall) {
1308                 struct audit_context *newctx;
1309
1310 #if AUDIT_DEBUG
1311                 printk(KERN_ERR
1312                        "audit(:%d) pid=%d in syscall=%d;"
1313                        " entering syscall=%d\n",
1314                        context->serial, tsk->pid, context->major, major);
1315 #endif
1316                 newctx = audit_alloc_context(context->state);
1317                 if (newctx) {
1318                         newctx->previous   = context;
1319                         context            = newctx;
1320                         tsk->audit_context = newctx;
1321                 } else  {
1322                         /* If we can't alloc a new context, the best we
1323                          * can do is to leak memory (any pending putname
1324                          * will be lost).  The only other alternative is
1325                          * to abandon auditing. */
1326                         audit_zero_context(context, context->state);
1327                 }
1328         }
1329         BUG_ON(context->in_syscall || context->name_count);
1330
1331         if (!audit_enabled)
1332                 return;
1333
1334         context->arch       = arch;
1335         context->major      = major;
1336         context->argv[0]    = a1;
1337         context->argv[1]    = a2;
1338         context->argv[2]    = a3;
1339         context->argv[3]    = a4;
1340
1341         state = context->state;
1342         context->dummy = !audit_n_rules;
1343         if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1344                 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1345         if (likely(state == AUDIT_DISABLED))
1346                 return;
1347
1348         context->serial     = 0;
1349         context->ctime      = CURRENT_TIME;
1350         context->in_syscall = 1;
1351         context->auditable  = !!(state == AUDIT_RECORD_CONTEXT);
1352         context->ppid       = 0;
1353 }
1354
1355 /**
1356  * audit_syscall_exit - deallocate audit context after a system call
1357  * @tsk: task being audited
1358  * @valid: success/failure flag
1359  * @return_code: syscall return value
1360  *
1361  * Tear down after system call.  If the audit context has been marked as
1362  * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1363  * filtering, or because some other part of the kernel write an audit
1364  * message), then write out the syscall information.  In call cases,
1365  * free the names stored from getname().
1366  */
1367 void audit_syscall_exit(int valid, long return_code)
1368 {
1369         struct task_struct *tsk = current;
1370         struct audit_context *context;
1371
1372         context = audit_get_context(tsk, valid, return_code);
1373
1374         if (likely(!context))
1375                 return;
1376
1377         if (context->in_syscall && context->auditable)
1378                 audit_log_exit(context, tsk);
1379
1380         context->in_syscall = 0;
1381         context->auditable  = 0;
1382
1383         if (context->previous) {
1384                 struct audit_context *new_context = context->previous;
1385                 context->previous  = NULL;
1386                 audit_free_context(context);
1387                 tsk->audit_context = new_context;
1388         } else {
1389                 audit_free_names(context);
1390                 unroll_tree_refs(context, NULL, 0);
1391                 audit_free_aux(context);
1392                 context->aux = NULL;
1393                 context->aux_pids = NULL;
1394                 context->target_pid = 0;
1395                 context->target_sid = 0;
1396                 kfree(context->filterkey);
1397                 context->filterkey = NULL;
1398                 tsk->audit_context = context;
1399         }
1400 }
1401
1402 static inline void handle_one(const struct inode *inode)
1403 {
1404 #ifdef CONFIG_AUDIT_TREE
1405         struct audit_context *context;
1406         struct audit_tree_refs *p;
1407         struct audit_chunk *chunk;
1408         int count;
1409         if (likely(list_empty(&inode->inotify_watches)))
1410                 return;
1411         context = current->audit_context;
1412         p = context->trees;
1413         count = context->tree_count;
1414         rcu_read_lock();
1415         chunk = audit_tree_lookup(inode);
1416         rcu_read_unlock();
1417         if (!chunk)
1418                 return;
1419         if (likely(put_tree_ref(context, chunk)))
1420                 return;
1421         if (unlikely(!grow_tree_refs(context))) {
1422                 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1423                 audit_set_auditable(context);
1424                 audit_put_chunk(chunk);
1425                 unroll_tree_refs(context, p, count);
1426                 return;
1427         }
1428         put_tree_ref(context, chunk);
1429 #endif
1430 }
1431
1432 static void handle_path(const struct dentry *dentry)
1433 {
1434 #ifdef CONFIG_AUDIT_TREE
1435         struct audit_context *context;
1436         struct audit_tree_refs *p;
1437         const struct dentry *d, *parent;
1438         struct audit_chunk *drop;
1439         unsigned long seq;
1440         int count;
1441
1442         context = current->audit_context;
1443         p = context->trees;
1444         count = context->tree_count;
1445 retry:
1446         drop = NULL;
1447         d = dentry;
1448         rcu_read_lock();
1449         seq = read_seqbegin(&rename_lock);
1450         for(;;) {
1451                 struct inode *inode = d->d_inode;
1452                 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1453                         struct audit_chunk *chunk;
1454                         chunk = audit_tree_lookup(inode);
1455                         if (chunk) {
1456                                 if (unlikely(!put_tree_ref(context, chunk))) {
1457                                         drop = chunk;
1458                                         break;
1459                                 }
1460                         }
1461                 }
1462                 parent = d->d_parent;
1463                 if (parent == d)
1464                         break;
1465                 d = parent;
1466         }
1467         if (unlikely(read_seqretry(&rename_lock, seq) || drop)) {  /* in this order */
1468                 rcu_read_unlock();
1469                 if (!drop) {
1470                         /* just a race with rename */
1471                         unroll_tree_refs(context, p, count);
1472                         goto retry;
1473                 }
1474                 audit_put_chunk(drop);
1475                 if (grow_tree_refs(context)) {
1476                         /* OK, got more space */
1477                         unroll_tree_refs(context, p, count);
1478                         goto retry;
1479                 }
1480                 /* too bad */
1481                 printk(KERN_WARNING
1482                         "out of memory, audit has lost a tree reference");
1483                 unroll_tree_refs(context, p, count);
1484                 audit_set_auditable(context);
1485                 return;
1486         }
1487         rcu_read_unlock();
1488 #endif
1489 }
1490
1491 /**
1492  * audit_getname - add a name to the list
1493  * @name: name to add
1494  *
1495  * Add a name to the list of audit names for this context.
1496  * Called from fs/namei.c:getname().
1497  */
1498 void __audit_getname(const char *name)
1499 {
1500         struct audit_context *context = current->audit_context;
1501
1502         if (IS_ERR(name) || !name)
1503                 return;
1504
1505         if (!context->in_syscall) {
1506 #if AUDIT_DEBUG == 2
1507                 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1508                        __FILE__, __LINE__, context->serial, name);
1509                 dump_stack();
1510 #endif
1511                 return;
1512         }
1513         BUG_ON(context->name_count >= AUDIT_NAMES);
1514         context->names[context->name_count].name = name;
1515         context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1516         context->names[context->name_count].name_put = 1;
1517         context->names[context->name_count].ino  = (unsigned long)-1;
1518         context->names[context->name_count].osid = 0;
1519         ++context->name_count;
1520         if (!context->pwd) {
1521                 read_lock(&current->fs->lock);
1522                 context->pwd = dget(current->fs->pwd);
1523                 context->pwdmnt = mntget(current->fs->pwdmnt);
1524                 read_unlock(&current->fs->lock);
1525         }
1526
1527 }
1528
1529 /* audit_putname - intercept a putname request
1530  * @name: name to intercept and delay for putname
1531  *
1532  * If we have stored the name from getname in the audit context,
1533  * then we delay the putname until syscall exit.
1534  * Called from include/linux/fs.h:putname().
1535  */
1536 void audit_putname(const char *name)
1537 {
1538         struct audit_context *context = current->audit_context;
1539
1540         BUG_ON(!context);
1541         if (!context->in_syscall) {
1542 #if AUDIT_DEBUG == 2
1543                 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1544                        __FILE__, __LINE__, context->serial, name);
1545                 if (context->name_count) {
1546                         int i;
1547                         for (i = 0; i < context->name_count; i++)
1548                                 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1549                                        context->names[i].name,
1550                                        context->names[i].name ?: "(null)");
1551                 }
1552 #endif
1553                 __putname(name);
1554         }
1555 #if AUDIT_DEBUG
1556         else {
1557                 ++context->put_count;
1558                 if (context->put_count > context->name_count) {
1559                         printk(KERN_ERR "%s:%d(:%d): major=%d"
1560                                " in_syscall=%d putname(%p) name_count=%d"
1561                                " put_count=%d\n",
1562                                __FILE__, __LINE__,
1563                                context->serial, context->major,
1564                                context->in_syscall, name, context->name_count,
1565                                context->put_count);
1566                         dump_stack();
1567                 }
1568         }
1569 #endif
1570 }
1571
1572 static int audit_inc_name_count(struct audit_context *context,
1573                                 const struct inode *inode)
1574 {
1575         if (context->name_count >= AUDIT_NAMES) {
1576                 if (inode)
1577                         printk(KERN_DEBUG "name_count maxed, losing inode data: "
1578                                "dev=%02x:%02x, inode=%lu",
1579                                MAJOR(inode->i_sb->s_dev),
1580                                MINOR(inode->i_sb->s_dev),
1581                                inode->i_ino);
1582
1583                 else
1584                         printk(KERN_DEBUG "name_count maxed, losing inode data");
1585                 return 1;
1586         }
1587         context->name_count++;
1588 #if AUDIT_DEBUG
1589         context->ino_count++;
1590 #endif
1591         return 0;
1592 }
1593
1594 /* Copy inode data into an audit_names. */
1595 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1596 {
1597         name->ino   = inode->i_ino;
1598         name->dev   = inode->i_sb->s_dev;
1599         name->mode  = inode->i_mode;
1600         name->uid   = inode->i_uid;
1601         name->gid   = inode->i_gid;
1602         name->rdev  = inode->i_rdev;
1603         selinux_get_inode_sid(inode, &name->osid);
1604 }
1605
1606 /**
1607  * audit_inode - store the inode and device from a lookup
1608  * @name: name being audited
1609  * @dentry: dentry being audited
1610  *
1611  * Called from fs/namei.c:path_lookup().
1612  */
1613 void __audit_inode(const char *name, const struct dentry *dentry)
1614 {
1615         int idx;
1616         struct audit_context *context = current->audit_context;
1617         const struct inode *inode = dentry->d_inode;
1618
1619         if (!context->in_syscall)
1620                 return;
1621         if (context->name_count
1622             && context->names[context->name_count-1].name
1623             && context->names[context->name_count-1].name == name)
1624                 idx = context->name_count - 1;
1625         else if (context->name_count > 1
1626                  && context->names[context->name_count-2].name
1627                  && context->names[context->name_count-2].name == name)
1628                 idx = context->name_count - 2;
1629         else {
1630                 /* FIXME: how much do we care about inodes that have no
1631                  * associated name? */
1632                 if (audit_inc_name_count(context, inode))
1633                         return;
1634                 idx = context->name_count - 1;
1635                 context->names[idx].name = NULL;
1636         }
1637         handle_path(dentry);
1638         audit_copy_inode(&context->names[idx], inode);
1639 }
1640
1641 /**
1642  * audit_inode_child - collect inode info for created/removed objects
1643  * @dname: inode's dentry name
1644  * @dentry: dentry being audited
1645  * @parent: inode of dentry parent
1646  *
1647  * For syscalls that create or remove filesystem objects, audit_inode
1648  * can only collect information for the filesystem object's parent.
1649  * This call updates the audit context with the child's information.
1650  * Syscalls that create a new filesystem object must be hooked after
1651  * the object is created.  Syscalls that remove a filesystem object
1652  * must be hooked prior, in order to capture the target inode during
1653  * unsuccessful attempts.
1654  */
1655 void __audit_inode_child(const char *dname, const struct dentry *dentry,
1656                          const struct inode *parent)
1657 {
1658         int idx;
1659         struct audit_context *context = current->audit_context;
1660         const char *found_parent = NULL, *found_child = NULL;
1661         const struct inode *inode = dentry->d_inode;
1662         int dirlen = 0;
1663
1664         if (!context->in_syscall)
1665                 return;
1666
1667         if (inode)
1668                 handle_one(inode);
1669         /* determine matching parent */
1670         if (!dname)
1671                 goto add_names;
1672
1673         /* parent is more likely, look for it first */
1674         for (idx = 0; idx < context->name_count; idx++) {
1675                 struct audit_names *n = &context->names[idx];
1676
1677                 if (!n->name)
1678                         continue;
1679
1680                 if (n->ino == parent->i_ino &&
1681                     !audit_compare_dname_path(dname, n->name, &dirlen)) {
1682                         n->name_len = dirlen; /* update parent data in place */
1683                         found_parent = n->name;
1684                         goto add_names;
1685                 }
1686         }
1687
1688         /* no matching parent, look for matching child */
1689         for (idx = 0; idx < context->name_count; idx++) {
1690                 struct audit_names *n = &context->names[idx];
1691
1692                 if (!n->name)
1693                         continue;
1694
1695                 /* strcmp() is the more likely scenario */
1696                 if (!strcmp(dname, n->name) ||
1697                      !audit_compare_dname_path(dname, n->name, &dirlen)) {
1698                         if (inode)
1699                                 audit_copy_inode(n, inode);
1700                         else
1701                                 n->ino = (unsigned long)-1;
1702                         found_child = n->name;
1703                         goto add_names;
1704                 }
1705         }
1706
1707 add_names:
1708         if (!found_parent) {
1709                 if (audit_inc_name_count(context, parent))
1710                         return;
1711                 idx = context->name_count - 1;
1712                 context->names[idx].name = NULL;
1713                 audit_copy_inode(&context->names[idx], parent);
1714         }
1715
1716         if (!found_child) {
1717                 if (audit_inc_name_count(context, inode))
1718                         return;
1719                 idx = context->name_count - 1;
1720
1721                 /* Re-use the name belonging to the slot for a matching parent
1722                  * directory. All names for this context are relinquished in
1723                  * audit_free_names() */
1724                 if (found_parent) {
1725                         context->names[idx].name = found_parent;
1726                         context->names[idx].name_len = AUDIT_NAME_FULL;
1727                         /* don't call __putname() */
1728                         context->names[idx].name_put = 0;
1729                 } else {
1730                         context->names[idx].name = NULL;
1731                 }
1732
1733                 if (inode)
1734                         audit_copy_inode(&context->names[idx], inode);
1735                 else
1736                         context->names[idx].ino = (unsigned long)-1;
1737         }
1738 }
1739 EXPORT_SYMBOL_GPL(__audit_inode_child);
1740
1741 /**
1742  * auditsc_get_stamp - get local copies of audit_context values
1743  * @ctx: audit_context for the task
1744  * @t: timespec to store time recorded in the audit_context
1745  * @serial: serial value that is recorded in the audit_context
1746  *
1747  * Also sets the context as auditable.
1748  */
1749 void auditsc_get_stamp(struct audit_context *ctx,
1750                        struct timespec *t, unsigned int *serial)
1751 {
1752         if (!ctx->serial)
1753                 ctx->serial = audit_serial();
1754         t->tv_sec  = ctx->ctime.tv_sec;
1755         t->tv_nsec = ctx->ctime.tv_nsec;
1756         *serial    = ctx->serial;
1757         ctx->auditable = 1;
1758 }
1759
1760 /**
1761  * audit_set_loginuid - set a task's audit_context loginuid
1762  * @task: task whose audit context is being modified
1763  * @loginuid: loginuid value
1764  *
1765  * Returns 0.
1766  *
1767  * Called (set) from fs/proc/base.c::proc_loginuid_write().
1768  */
1769 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1770 {
1771         struct audit_context *context = task->audit_context;
1772
1773         if (context && context->in_syscall) {
1774                 struct audit_buffer *ab;
1775
1776                 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1777                 if (ab) {
1778                         audit_log_format(ab, "login pid=%d uid=%u "
1779                                 "old auid=%u new auid=%u",
1780                                 task->pid, task->uid,
1781                                 task->loginuid, loginuid);
1782                         audit_log_end(ab);
1783                 }
1784         }
1785         task->loginuid = loginuid;
1786         return 0;
1787 }
1788
1789 /**
1790  * __audit_mq_open - record audit data for a POSIX MQ open
1791  * @oflag: open flag
1792  * @mode: mode bits
1793  * @u_attr: queue attributes
1794  *
1795  * Returns 0 for success or NULL context or < 0 on error.
1796  */
1797 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1798 {
1799         struct audit_aux_data_mq_open *ax;
1800         struct audit_context *context = current->audit_context;
1801
1802         if (!audit_enabled)
1803                 return 0;
1804
1805         if (likely(!context))
1806                 return 0;
1807
1808         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1809         if (!ax)
1810                 return -ENOMEM;
1811
1812         if (u_attr != NULL) {
1813                 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1814                         kfree(ax);
1815                         return -EFAULT;
1816                 }
1817         } else
1818                 memset(&ax->attr, 0, sizeof(ax->attr));
1819
1820         ax->oflag = oflag;
1821         ax->mode = mode;
1822
1823         ax->d.type = AUDIT_MQ_OPEN;
1824         ax->d.next = context->aux;
1825         context->aux = (void *)ax;
1826         return 0;
1827 }
1828
1829 /**
1830  * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1831  * @mqdes: MQ descriptor
1832  * @msg_len: Message length
1833  * @msg_prio: Message priority
1834  * @u_abs_timeout: Message timeout in absolute time
1835  *
1836  * Returns 0 for success or NULL context or < 0 on error.
1837  */
1838 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1839                         const struct timespec __user *u_abs_timeout)
1840 {
1841         struct audit_aux_data_mq_sendrecv *ax;
1842         struct audit_context *context = current->audit_context;
1843
1844         if (!audit_enabled)
1845                 return 0;
1846
1847         if (likely(!context))
1848                 return 0;
1849
1850         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1851         if (!ax)
1852                 return -ENOMEM;
1853
1854         if (u_abs_timeout != NULL) {
1855                 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1856                         kfree(ax);
1857                         return -EFAULT;
1858                 }
1859         } else
1860                 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1861
1862         ax->mqdes = mqdes;
1863         ax->msg_len = msg_len;
1864         ax->msg_prio = msg_prio;
1865
1866         ax->d.type = AUDIT_MQ_SENDRECV;
1867         ax->d.next = context->aux;
1868         context->aux = (void *)ax;
1869         return 0;
1870 }
1871
1872 /**
1873  * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1874  * @mqdes: MQ descriptor
1875  * @msg_len: Message length
1876  * @u_msg_prio: Message priority
1877  * @u_abs_timeout: Message timeout in absolute time
1878  *
1879  * Returns 0 for success or NULL context or < 0 on error.
1880  */
1881 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1882                                 unsigned int __user *u_msg_prio,
1883                                 const struct timespec __user *u_abs_timeout)
1884 {
1885         struct audit_aux_data_mq_sendrecv *ax;
1886         struct audit_context *context = current->audit_context;
1887
1888         if (!audit_enabled)
1889                 return 0;
1890
1891         if (likely(!context))
1892                 return 0;
1893
1894         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1895         if (!ax)
1896                 return -ENOMEM;
1897
1898         if (u_msg_prio != NULL) {
1899                 if (get_user(ax->msg_prio, u_msg_prio)) {
1900                         kfree(ax);
1901                         return -EFAULT;
1902                 }
1903         } else
1904                 ax->msg_prio = 0;
1905
1906         if (u_abs_timeout != NULL) {
1907                 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1908                         kfree(ax);
1909                         return -EFAULT;
1910                 }
1911         } else
1912                 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1913
1914         ax->mqdes = mqdes;
1915         ax->msg_len = msg_len;
1916
1917         ax->d.type = AUDIT_MQ_SENDRECV;
1918         ax->d.next = context->aux;
1919         context->aux = (void *)ax;
1920         return 0;
1921 }
1922
1923 /**
1924  * __audit_mq_notify - record audit data for a POSIX MQ notify
1925  * @mqdes: MQ descriptor
1926  * @u_notification: Notification event
1927  *
1928  * Returns 0 for success or NULL context or < 0 on error.
1929  */
1930
1931 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1932 {
1933         struct audit_aux_data_mq_notify *ax;
1934         struct audit_context *context = current->audit_context;
1935
1936         if (!audit_enabled)
1937                 return 0;
1938
1939         if (likely(!context))
1940                 return 0;
1941
1942         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1943         if (!ax)
1944                 return -ENOMEM;
1945
1946         if (u_notification != NULL) {
1947                 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1948                         kfree(ax);
1949                         return -EFAULT;
1950                 }
1951         } else
1952                 memset(&ax->notification, 0, sizeof(ax->notification));
1953
1954         ax->mqdes = mqdes;
1955
1956         ax->d.type = AUDIT_MQ_NOTIFY;
1957         ax->d.next = context->aux;
1958         context->aux = (void *)ax;
1959         return 0;
1960 }
1961
1962 /**
1963  * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1964  * @mqdes: MQ descriptor
1965  * @mqstat: MQ flags
1966  *
1967  * Returns 0 for success or NULL context or < 0 on error.
1968  */
1969 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1970 {
1971         struct audit_aux_data_mq_getsetattr *ax;
1972         struct audit_context *context = current->audit_context;
1973
1974         if (!audit_enabled)
1975                 return 0;
1976
1977         if (likely(!context))
1978                 return 0;
1979
1980         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1981         if (!ax)
1982                 return -ENOMEM;
1983
1984         ax->mqdes = mqdes;
1985         ax->mqstat = *mqstat;
1986
1987         ax->d.type = AUDIT_MQ_GETSETATTR;
1988         ax->d.next = context->aux;
1989         context->aux = (void *)ax;
1990         return 0;
1991 }
1992
1993 /**
1994  * audit_ipc_obj - record audit data for ipc object
1995  * @ipcp: ipc permissions
1996  *
1997  * Returns 0 for success or NULL context or < 0 on error.
1998  */
1999 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
2000 {
2001         struct audit_aux_data_ipcctl *ax;
2002         struct audit_context *context = current->audit_context;
2003
2004         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2005         if (!ax)
2006                 return -ENOMEM;
2007
2008         ax->uid = ipcp->uid;
2009         ax->gid = ipcp->gid;
2010         ax->mode = ipcp->mode;
2011         selinux_get_ipc_sid(ipcp, &ax->osid);
2012
2013         ax->d.type = AUDIT_IPC;
2014         ax->d.next = context->aux;
2015         context->aux = (void *)ax;
2016         return 0;
2017 }
2018
2019 /**
2020  * audit_ipc_set_perm - record audit data for new ipc permissions
2021  * @qbytes: msgq bytes
2022  * @uid: msgq user id
2023  * @gid: msgq group id
2024  * @mode: msgq mode (permissions)
2025  *
2026  * Returns 0 for success or NULL context or < 0 on error.
2027  */
2028 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
2029 {
2030         struct audit_aux_data_ipcctl *ax;
2031         struct audit_context *context = current->audit_context;
2032
2033         ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2034         if (!ax)
2035                 return -ENOMEM;
2036
2037         ax->qbytes = qbytes;
2038         ax->uid = uid;
2039         ax->gid = gid;
2040         ax->mode = mode;
2041
2042         ax->d.type = AUDIT_IPC_SET_PERM;
2043         ax->d.next = context->aux;
2044         context->aux = (void *)ax;
2045         return 0;
2046 }
2047
2048 int audit_argv_kb = 32;
2049
2050 int audit_bprm(struct linux_binprm *bprm)
2051 {
2052         struct audit_aux_data_execve *ax;
2053         struct audit_context *context = current->audit_context;
2054
2055         if (likely(!audit_enabled || !context || context->dummy))
2056                 return 0;
2057
2058         /*
2059          * Even though the stack code doesn't limit the arg+env size any more,
2060          * the audit code requires that _all_ arguments be logged in a single
2061          * netlink skb. Hence cap it :-(
2062          */
2063         if (bprm->argv_len > (audit_argv_kb << 10))
2064                 return -E2BIG;
2065
2066         ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2067         if (!ax)
2068                 return -ENOMEM;
2069
2070         ax->argc = bprm->argc;
2071         ax->envc = bprm->envc;
2072         ax->mm = bprm->mm;
2073         ax->d.type = AUDIT_EXECVE;
2074         ax->d.next = context->aux;
2075         context->aux = (void *)ax;
2076         return 0;
2077 }
2078
2079
2080 /**
2081  * audit_socketcall - record audit data for sys_socketcall
2082  * @nargs: number of args
2083  * @args: args array
2084  *
2085  * Returns 0 for success or NULL context or < 0 on error.
2086  */
2087 int audit_socketcall(int nargs, unsigned long *args)
2088 {
2089         struct audit_aux_data_socketcall *ax;
2090         struct audit_context *context = current->audit_context;
2091
2092         if (likely(!context || context->dummy))
2093                 return 0;
2094
2095         ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2096         if (!ax)
2097                 return -ENOMEM;
2098
2099         ax->nargs = nargs;
2100         memcpy(ax->args, args, nargs * sizeof(unsigned long));
2101
2102         ax->d.type = AUDIT_SOCKETCALL;
2103         ax->d.next = context->aux;
2104         context->aux = (void *)ax;
2105         return 0;
2106 }
2107
2108 /**
2109  * __audit_fd_pair - record audit data for pipe and socketpair
2110  * @fd1: the first file descriptor
2111  * @fd2: the second file descriptor
2112  *
2113  * Returns 0 for success or NULL context or < 0 on error.
2114  */
2115 int __audit_fd_pair(int fd1, int fd2)
2116 {
2117         struct audit_context *context = current->audit_context;
2118         struct audit_aux_data_fd_pair *ax;
2119
2120         if (likely(!context)) {
2121                 return 0;
2122         }
2123
2124         ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2125         if (!ax) {
2126                 return -ENOMEM;
2127         }
2128
2129         ax->fd[0] = fd1;
2130         ax->fd[1] = fd2;
2131
2132         ax->d.type = AUDIT_FD_PAIR;
2133         ax->d.next = context->aux;
2134         context->aux = (void *)ax;
2135         return 0;
2136 }
2137
2138 /**
2139  * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2140  * @len: data length in user space
2141  * @a: data address in kernel space
2142  *
2143  * Returns 0 for success or NULL context or < 0 on error.
2144  */
2145 int audit_sockaddr(int len, void *a)
2146 {
2147         struct audit_aux_data_sockaddr *ax;
2148         struct audit_context *context = current->audit_context;
2149
2150         if (likely(!context || context->dummy))
2151                 return 0;
2152
2153         ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2154         if (!ax)
2155                 return -ENOMEM;
2156
2157         ax->len = len;
2158         memcpy(ax->a, a, len);
2159
2160         ax->d.type = AUDIT_SOCKADDR;
2161         ax->d.next = context->aux;
2162         context->aux = (void *)ax;
2163         return 0;
2164 }
2165
2166 void __audit_ptrace(struct task_struct *t)
2167 {
2168         struct audit_context *context = current->audit_context;
2169
2170         context->target_pid = t->pid;
2171         selinux_get_task_sid(t, &context->target_sid);
2172 }
2173
2174 /**
2175  * audit_signal_info - record signal info for shutting down audit subsystem
2176  * @sig: signal value
2177  * @t: task being signaled
2178  *
2179  * If the audit subsystem is being terminated, record the task (pid)
2180  * and uid that is doing that.
2181  */
2182 int __audit_signal_info(int sig, struct task_struct *t)
2183 {
2184         struct audit_aux_data_pids *axp;
2185         struct task_struct *tsk = current;
2186         struct audit_context *ctx = tsk->audit_context;
2187         extern pid_t audit_sig_pid;
2188         extern uid_t audit_sig_uid;
2189         extern u32 audit_sig_sid;
2190
2191         if (audit_pid && t->tgid == audit_pid) {
2192                 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2193                         audit_sig_pid = tsk->pid;
2194                         if (tsk->loginuid != -1)
2195                                 audit_sig_uid = tsk->loginuid;
2196                         else
2197                                 audit_sig_uid = tsk->uid;
2198                         selinux_get_task_sid(tsk, &audit_sig_sid);
2199                 }
2200                 if (!audit_signals || audit_dummy_context())
2201                         return 0;
2202         }
2203
2204         /* optimize the common case by putting first signal recipient directly
2205          * in audit_context */
2206         if (!ctx->target_pid) {
2207                 ctx->target_pid = t->tgid;
2208                 selinux_get_task_sid(t, &ctx->target_sid);
2209                 return 0;
2210         }
2211
2212         axp = (void *)ctx->aux_pids;
2213         if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2214                 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2215                 if (!axp)
2216                         return -ENOMEM;
2217
2218                 axp->d.type = AUDIT_OBJ_PID;
2219                 axp->d.next = ctx->aux_pids;
2220                 ctx->aux_pids = (void *)axp;
2221         }
2222         BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
2223
2224         axp->target_pid[axp->pid_count] = t->tgid;
2225         selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2226         axp->pid_count++;
2227
2228         return 0;
2229 }
2230
2231 /**
2232  * audit_core_dumps - record information about processes that end abnormally
2233  * @signr: signal value
2234  *
2235  * If a process ends with a core dump, something fishy is going on and we
2236  * should record the event for investigation.
2237  */
2238 void audit_core_dumps(long signr)
2239 {
2240         struct audit_buffer *ab;
2241         u32 sid;
2242
2243         if (!audit_enabled)
2244                 return;
2245
2246         if (signr == SIGQUIT)   /* don't care for those */
2247                 return;
2248
2249         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2250         audit_log_format(ab, "auid=%u uid=%u gid=%u",
2251                         audit_get_loginuid(current),
2252                         current->uid, current->gid);
2253         selinux_get_task_sid(current, &sid);
2254         if (sid) {
2255                 char *ctx = NULL;
2256                 u32 len;
2257
2258                 if (selinux_sid_to_string(sid, &ctx, &len))
2259                         audit_log_format(ab, " ssid=%u", sid);
2260                 else
2261                         audit_log_format(ab, " subj=%s", ctx);
2262                 kfree(ctx);
2263         }
2264         audit_log_format(ab, " pid=%d comm=", current->pid);
2265         audit_log_untrustedstring(ab, current->comm);
2266         audit_log_format(ab, " sig=%ld", signr);
2267         audit_log_end(ab);
2268 }