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