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