audit: Use struct net not pid_t to remember the network namespce to reply in
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / auditfilter.c
1 /* auditfilter.c -- filtering of audit events
2  *
3  * Copyright 2003-2004 Red Hat, Inc.
4  * Copyright 2005 Hewlett-Packard Development Company, L.P.
5  * Copyright 2005 IBM Corporation
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
22 #include <linux/kernel.h>
23 #include <linux/audit.h>
24 #include <linux/kthread.h>
25 #include <linux/mutex.h>
26 #include <linux/fs.h>
27 #include <linux/namei.h>
28 #include <linux/netlink.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/security.h>
32 #include <net/net_namespace.h>
33 #include "audit.h"
34
35 /*
36  * Locking model:
37  *
38  * audit_filter_mutex:
39  *              Synchronizes writes and blocking reads of audit's filterlist
40  *              data.  Rcu is used to traverse the filterlist and access
41  *              contents of structs audit_entry, audit_watch and opaque
42  *              LSM rules during filtering.  If modified, these structures
43  *              must be copied and replace their counterparts in the filterlist.
44  *              An audit_parent struct is not accessed during filtering, so may
45  *              be written directly provided audit_filter_mutex is held.
46  */
47
48 /* Audit filter lists, defined in <linux/audit.h> */
49 struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
50         LIST_HEAD_INIT(audit_filter_list[0]),
51         LIST_HEAD_INIT(audit_filter_list[1]),
52         LIST_HEAD_INIT(audit_filter_list[2]),
53         LIST_HEAD_INIT(audit_filter_list[3]),
54         LIST_HEAD_INIT(audit_filter_list[4]),
55         LIST_HEAD_INIT(audit_filter_list[5]),
56 #if AUDIT_NR_FILTERS != 6
57 #error Fix audit_filter_list initialiser
58 #endif
59 };
60 static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
61         LIST_HEAD_INIT(audit_rules_list[0]),
62         LIST_HEAD_INIT(audit_rules_list[1]),
63         LIST_HEAD_INIT(audit_rules_list[2]),
64         LIST_HEAD_INIT(audit_rules_list[3]),
65         LIST_HEAD_INIT(audit_rules_list[4]),
66         LIST_HEAD_INIT(audit_rules_list[5]),
67 };
68
69 DEFINE_MUTEX(audit_filter_mutex);
70
71 static inline void audit_free_rule(struct audit_entry *e)
72 {
73         int i;
74         struct audit_krule *erule = &e->rule;
75
76         /* some rules don't have associated watches */
77         if (erule->watch)
78                 audit_put_watch(erule->watch);
79         if (erule->fields)
80                 for (i = 0; i < erule->field_count; i++) {
81                         struct audit_field *f = &erule->fields[i];
82                         kfree(f->lsm_str);
83                         security_audit_rule_free(f->lsm_rule);
84                 }
85         kfree(erule->fields);
86         kfree(erule->filterkey);
87         kfree(e);
88 }
89
90 void audit_free_rule_rcu(struct rcu_head *head)
91 {
92         struct audit_entry *e = container_of(head, struct audit_entry, rcu);
93         audit_free_rule(e);
94 }
95
96 /* Initialize an audit filterlist entry. */
97 static inline struct audit_entry *audit_init_entry(u32 field_count)
98 {
99         struct audit_entry *entry;
100         struct audit_field *fields;
101
102         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
103         if (unlikely(!entry))
104                 return NULL;
105
106         fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
107         if (unlikely(!fields)) {
108                 kfree(entry);
109                 return NULL;
110         }
111         entry->rule.fields = fields;
112
113         return entry;
114 }
115
116 /* Unpack a filter field's string representation from user-space
117  * buffer. */
118 char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
119 {
120         char *str;
121
122         if (!*bufp || (len == 0) || (len > *remain))
123                 return ERR_PTR(-EINVAL);
124
125         /* Of the currently implemented string fields, PATH_MAX
126          * defines the longest valid length.
127          */
128         if (len > PATH_MAX)
129                 return ERR_PTR(-ENAMETOOLONG);
130
131         str = kmalloc(len + 1, GFP_KERNEL);
132         if (unlikely(!str))
133                 return ERR_PTR(-ENOMEM);
134
135         memcpy(str, *bufp, len);
136         str[len] = 0;
137         *bufp += len;
138         *remain -= len;
139
140         return str;
141 }
142
143 /* Translate an inode field to kernel respresentation. */
144 static inline int audit_to_inode(struct audit_krule *krule,
145                                  struct audit_field *f)
146 {
147         if (krule->listnr != AUDIT_FILTER_EXIT ||
148             krule->watch || krule->inode_f || krule->tree ||
149             (f->op != Audit_equal && f->op != Audit_not_equal))
150                 return -EINVAL;
151
152         krule->inode_f = f;
153         return 0;
154 }
155
156 static __u32 *classes[AUDIT_SYSCALL_CLASSES];
157
158 int __init audit_register_class(int class, unsigned *list)
159 {
160         __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
161         if (!p)
162                 return -ENOMEM;
163         while (*list != ~0U) {
164                 unsigned n = *list++;
165                 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
166                         kfree(p);
167                         return -EINVAL;
168                 }
169                 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
170         }
171         if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
172                 kfree(p);
173                 return -EINVAL;
174         }
175         classes[class] = p;
176         return 0;
177 }
178
179 int audit_match_class(int class, unsigned syscall)
180 {
181         if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
182                 return 0;
183         if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
184                 return 0;
185         return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
186 }
187
188 #ifdef CONFIG_AUDITSYSCALL
189 static inline int audit_match_class_bits(int class, u32 *mask)
190 {
191         int i;
192
193         if (classes[class]) {
194                 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
195                         if (mask[i] & classes[class][i])
196                                 return 0;
197         }
198         return 1;
199 }
200
201 static int audit_match_signal(struct audit_entry *entry)
202 {
203         struct audit_field *arch = entry->rule.arch_f;
204
205         if (!arch) {
206                 /* When arch is unspecified, we must check both masks on biarch
207                  * as syscall number alone is ambiguous. */
208                 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
209                                                entry->rule.mask) &&
210                         audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
211                                                entry->rule.mask));
212         }
213
214         switch(audit_classify_arch(arch->val)) {
215         case 0: /* native */
216                 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
217                                                entry->rule.mask));
218         case 1: /* 32bit on biarch */
219                 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
220                                                entry->rule.mask));
221         default:
222                 return 1;
223         }
224 }
225 #endif
226
227 /* Common user-space to kernel rule translation. */
228 static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
229 {
230         unsigned listnr;
231         struct audit_entry *entry;
232         int i, err;
233
234         err = -EINVAL;
235         listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
236         switch(listnr) {
237         default:
238                 goto exit_err;
239 #ifdef CONFIG_AUDITSYSCALL
240         case AUDIT_FILTER_ENTRY:
241                 if (rule->action == AUDIT_ALWAYS)
242                         goto exit_err;
243         case AUDIT_FILTER_EXIT:
244         case AUDIT_FILTER_TASK:
245 #endif
246         case AUDIT_FILTER_USER:
247         case AUDIT_FILTER_TYPE:
248                 ;
249         }
250         if (unlikely(rule->action == AUDIT_POSSIBLE)) {
251                 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
252                 goto exit_err;
253         }
254         if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
255                 goto exit_err;
256         if (rule->field_count > AUDIT_MAX_FIELDS)
257                 goto exit_err;
258
259         err = -ENOMEM;
260         entry = audit_init_entry(rule->field_count);
261         if (!entry)
262                 goto exit_err;
263
264         entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
265         entry->rule.listnr = listnr;
266         entry->rule.action = rule->action;
267         entry->rule.field_count = rule->field_count;
268
269         for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
270                 entry->rule.mask[i] = rule->mask[i];
271
272         for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
273                 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
274                 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
275                 __u32 *class;
276
277                 if (!(*p & AUDIT_BIT(bit)))
278                         continue;
279                 *p &= ~AUDIT_BIT(bit);
280                 class = classes[i];
281                 if (class) {
282                         int j;
283                         for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
284                                 entry->rule.mask[j] |= class[j];
285                 }
286         }
287
288         return entry;
289
290 exit_err:
291         return ERR_PTR(err);
292 }
293
294 static u32 audit_ops[] =
295 {
296         [Audit_equal] = AUDIT_EQUAL,
297         [Audit_not_equal] = AUDIT_NOT_EQUAL,
298         [Audit_bitmask] = AUDIT_BIT_MASK,
299         [Audit_bittest] = AUDIT_BIT_TEST,
300         [Audit_lt] = AUDIT_LESS_THAN,
301         [Audit_gt] = AUDIT_GREATER_THAN,
302         [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL,
303         [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL,
304 };
305
306 static u32 audit_to_op(u32 op)
307 {
308         u32 n;
309         for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++)
310                 ;
311         return n;
312 }
313
314 /* check if an audit field is valid */
315 static int audit_field_valid(struct audit_entry *entry, struct audit_field *f)
316 {
317         switch(f->type) {
318         case AUDIT_MSGTYPE:
319                 if (entry->rule.listnr != AUDIT_FILTER_TYPE &&
320                     entry->rule.listnr != AUDIT_FILTER_USER)
321                         return -EINVAL;
322                 break;
323         };
324
325         switch(f->type) {
326         default:
327                 return -EINVAL;
328         case AUDIT_UID:
329         case AUDIT_EUID:
330         case AUDIT_SUID:
331         case AUDIT_FSUID:
332         case AUDIT_LOGINUID:
333         case AUDIT_OBJ_UID:
334         case AUDIT_GID:
335         case AUDIT_EGID:
336         case AUDIT_SGID:
337         case AUDIT_FSGID:
338         case AUDIT_OBJ_GID:
339         case AUDIT_PID:
340         case AUDIT_PERS:
341         case AUDIT_MSGTYPE:
342         case AUDIT_PPID:
343         case AUDIT_DEVMAJOR:
344         case AUDIT_DEVMINOR:
345         case AUDIT_EXIT:
346         case AUDIT_SUCCESS:
347         case AUDIT_INODE:
348                 /* bit ops are only useful on syscall args */
349                 if (f->op == Audit_bitmask || f->op == Audit_bittest)
350                         return -EINVAL;
351                 break;
352         case AUDIT_ARG0:
353         case AUDIT_ARG1:
354         case AUDIT_ARG2:
355         case AUDIT_ARG3:
356         case AUDIT_SUBJ_USER:
357         case AUDIT_SUBJ_ROLE:
358         case AUDIT_SUBJ_TYPE:
359         case AUDIT_SUBJ_SEN:
360         case AUDIT_SUBJ_CLR:
361         case AUDIT_OBJ_USER:
362         case AUDIT_OBJ_ROLE:
363         case AUDIT_OBJ_TYPE:
364         case AUDIT_OBJ_LEV_LOW:
365         case AUDIT_OBJ_LEV_HIGH:
366         case AUDIT_WATCH:
367         case AUDIT_DIR:
368         case AUDIT_FILTERKEY:
369                 break;
370         case AUDIT_LOGINUID_SET:
371                 if ((f->val != 0) && (f->val != 1))
372                         return -EINVAL;
373         /* FALL THROUGH */
374         case AUDIT_ARCH:
375                 if (f->op != Audit_not_equal && f->op != Audit_equal)
376                         return -EINVAL;
377                 break;
378         case AUDIT_PERM:
379                 if (f->val & ~15)
380                         return -EINVAL;
381                 break;
382         case AUDIT_FILETYPE:
383                 if (f->val & ~S_IFMT)
384                         return -EINVAL;
385                 break;
386         case AUDIT_FIELD_COMPARE:
387                 if (f->val > AUDIT_MAX_FIELD_COMPARE)
388                         return -EINVAL;
389                 break;
390         };
391         return 0;
392 }
393
394 /* Translate struct audit_rule_data to kernel's rule respresentation. */
395 static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
396                                                size_t datasz)
397 {
398         int err = 0;
399         struct audit_entry *entry;
400         void *bufp;
401         size_t remain = datasz - sizeof(struct audit_rule_data);
402         int i;
403         char *str;
404
405         entry = audit_to_entry_common((struct audit_rule *)data);
406         if (IS_ERR(entry))
407                 goto exit_nofree;
408
409         bufp = data->buf;
410         entry->rule.vers_ops = 2;
411         for (i = 0; i < data->field_count; i++) {
412                 struct audit_field *f = &entry->rule.fields[i];
413
414                 err = -EINVAL;
415
416                 f->op = audit_to_op(data->fieldflags[i]);
417                 if (f->op == Audit_bad)
418                         goto exit_free;
419
420                 f->type = data->fields[i];
421                 f->val = data->values[i];
422                 f->uid = INVALID_UID;
423                 f->gid = INVALID_GID;
424                 f->lsm_str = NULL;
425                 f->lsm_rule = NULL;
426
427                 /* Support legacy tests for a valid loginuid */
428                 if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) {
429                         f->type = AUDIT_LOGINUID_SET;
430                         f->val = 0;
431                 }
432
433                 err = audit_field_valid(entry, f);
434                 if (err)
435                         goto exit_free;
436
437                 err = -EINVAL;
438                 switch (f->type) {
439                 case AUDIT_LOGINUID:
440                 case AUDIT_UID:
441                 case AUDIT_EUID:
442                 case AUDIT_SUID:
443                 case AUDIT_FSUID:
444                 case AUDIT_OBJ_UID:
445                         f->uid = make_kuid(current_user_ns(), f->val);
446                         if (!uid_valid(f->uid))
447                                 goto exit_free;
448                         break;
449                 case AUDIT_GID:
450                 case AUDIT_EGID:
451                 case AUDIT_SGID:
452                 case AUDIT_FSGID:
453                 case AUDIT_OBJ_GID:
454                         f->gid = make_kgid(current_user_ns(), f->val);
455                         if (!gid_valid(f->gid))
456                                 goto exit_free;
457                         break;
458                 case AUDIT_ARCH:
459                         entry->rule.arch_f = f;
460                         break;
461                 case AUDIT_SUBJ_USER:
462                 case AUDIT_SUBJ_ROLE:
463                 case AUDIT_SUBJ_TYPE:
464                 case AUDIT_SUBJ_SEN:
465                 case AUDIT_SUBJ_CLR:
466                 case AUDIT_OBJ_USER:
467                 case AUDIT_OBJ_ROLE:
468                 case AUDIT_OBJ_TYPE:
469                 case AUDIT_OBJ_LEV_LOW:
470                 case AUDIT_OBJ_LEV_HIGH:
471                         str = audit_unpack_string(&bufp, &remain, f->val);
472                         if (IS_ERR(str))
473                                 goto exit_free;
474                         entry->rule.buflen += f->val;
475
476                         err = security_audit_rule_init(f->type, f->op, str,
477                                                        (void **)&f->lsm_rule);
478                         /* Keep currently invalid fields around in case they
479                          * become valid after a policy reload. */
480                         if (err == -EINVAL) {
481                                 printk(KERN_WARNING "audit rule for LSM "
482                                        "\'%s\' is invalid\n",  str);
483                                 err = 0;
484                         }
485                         if (err) {
486                                 kfree(str);
487                                 goto exit_free;
488                         } else
489                                 f->lsm_str = str;
490                         break;
491                 case AUDIT_WATCH:
492                         str = audit_unpack_string(&bufp, &remain, f->val);
493                         if (IS_ERR(str))
494                                 goto exit_free;
495                         entry->rule.buflen += f->val;
496
497                         err = audit_to_watch(&entry->rule, str, f->val, f->op);
498                         if (err) {
499                                 kfree(str);
500                                 goto exit_free;
501                         }
502                         break;
503                 case AUDIT_DIR:
504                         str = audit_unpack_string(&bufp, &remain, f->val);
505                         if (IS_ERR(str))
506                                 goto exit_free;
507                         entry->rule.buflen += f->val;
508
509                         err = audit_make_tree(&entry->rule, str, f->op);
510                         kfree(str);
511                         if (err)
512                                 goto exit_free;
513                         break;
514                 case AUDIT_INODE:
515                         err = audit_to_inode(&entry->rule, f);
516                         if (err)
517                                 goto exit_free;
518                         break;
519                 case AUDIT_FILTERKEY:
520                         if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
521                                 goto exit_free;
522                         str = audit_unpack_string(&bufp, &remain, f->val);
523                         if (IS_ERR(str))
524                                 goto exit_free;
525                         entry->rule.buflen += f->val;
526                         entry->rule.filterkey = str;
527                         break;
528                 }
529         }
530
531         if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
532                 entry->rule.inode_f = NULL;
533
534 exit_nofree:
535         return entry;
536
537 exit_free:
538         if (entry->rule.watch)
539                 audit_put_watch(entry->rule.watch); /* matches initial get */
540         if (entry->rule.tree)
541                 audit_put_tree(entry->rule.tree); /* that's the temporary one */
542         audit_free_rule(entry);
543         return ERR_PTR(err);
544 }
545
546 /* Pack a filter field's string representation into data block. */
547 static inline size_t audit_pack_string(void **bufp, const char *str)
548 {
549         size_t len = strlen(str);
550
551         memcpy(*bufp, str, len);
552         *bufp += len;
553
554         return len;
555 }
556
557 /* Translate kernel rule respresentation to struct audit_rule_data. */
558 static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
559 {
560         struct audit_rule_data *data;
561         void *bufp;
562         int i;
563
564         data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
565         if (unlikely(!data))
566                 return NULL;
567         memset(data, 0, sizeof(*data));
568
569         data->flags = krule->flags | krule->listnr;
570         data->action = krule->action;
571         data->field_count = krule->field_count;
572         bufp = data->buf;
573         for (i = 0; i < data->field_count; i++) {
574                 struct audit_field *f = &krule->fields[i];
575
576                 data->fields[i] = f->type;
577                 data->fieldflags[i] = audit_ops[f->op];
578                 switch(f->type) {
579                 case AUDIT_SUBJ_USER:
580                 case AUDIT_SUBJ_ROLE:
581                 case AUDIT_SUBJ_TYPE:
582                 case AUDIT_SUBJ_SEN:
583                 case AUDIT_SUBJ_CLR:
584                 case AUDIT_OBJ_USER:
585                 case AUDIT_OBJ_ROLE:
586                 case AUDIT_OBJ_TYPE:
587                 case AUDIT_OBJ_LEV_LOW:
588                 case AUDIT_OBJ_LEV_HIGH:
589                         data->buflen += data->values[i] =
590                                 audit_pack_string(&bufp, f->lsm_str);
591                         break;
592                 case AUDIT_WATCH:
593                         data->buflen += data->values[i] =
594                                 audit_pack_string(&bufp,
595                                                   audit_watch_path(krule->watch));
596                         break;
597                 case AUDIT_DIR:
598                         data->buflen += data->values[i] =
599                                 audit_pack_string(&bufp,
600                                                   audit_tree_path(krule->tree));
601                         break;
602                 case AUDIT_FILTERKEY:
603                         data->buflen += data->values[i] =
604                                 audit_pack_string(&bufp, krule->filterkey);
605                         break;
606                 default:
607                         data->values[i] = f->val;
608                 }
609         }
610         for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
611
612         return data;
613 }
614
615 /* Compare two rules in kernel format.  Considered success if rules
616  * don't match. */
617 static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
618 {
619         int i;
620
621         if (a->flags != b->flags ||
622             a->listnr != b->listnr ||
623             a->action != b->action ||
624             a->field_count != b->field_count)
625                 return 1;
626
627         for (i = 0; i < a->field_count; i++) {
628                 if (a->fields[i].type != b->fields[i].type ||
629                     a->fields[i].op != b->fields[i].op)
630                         return 1;
631
632                 switch(a->fields[i].type) {
633                 case AUDIT_SUBJ_USER:
634                 case AUDIT_SUBJ_ROLE:
635                 case AUDIT_SUBJ_TYPE:
636                 case AUDIT_SUBJ_SEN:
637                 case AUDIT_SUBJ_CLR:
638                 case AUDIT_OBJ_USER:
639                 case AUDIT_OBJ_ROLE:
640                 case AUDIT_OBJ_TYPE:
641                 case AUDIT_OBJ_LEV_LOW:
642                 case AUDIT_OBJ_LEV_HIGH:
643                         if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
644                                 return 1;
645                         break;
646                 case AUDIT_WATCH:
647                         if (strcmp(audit_watch_path(a->watch),
648                                    audit_watch_path(b->watch)))
649                                 return 1;
650                         break;
651                 case AUDIT_DIR:
652                         if (strcmp(audit_tree_path(a->tree),
653                                    audit_tree_path(b->tree)))
654                                 return 1;
655                         break;
656                 case AUDIT_FILTERKEY:
657                         /* both filterkeys exist based on above type compare */
658                         if (strcmp(a->filterkey, b->filterkey))
659                                 return 1;
660                         break;
661                 case AUDIT_UID:
662                 case AUDIT_EUID:
663                 case AUDIT_SUID:
664                 case AUDIT_FSUID:
665                 case AUDIT_LOGINUID:
666                 case AUDIT_OBJ_UID:
667                         if (!uid_eq(a->fields[i].uid, b->fields[i].uid))
668                                 return 1;
669                         break;
670                 case AUDIT_GID:
671                 case AUDIT_EGID:
672                 case AUDIT_SGID:
673                 case AUDIT_FSGID:
674                 case AUDIT_OBJ_GID:
675                         if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
676                                 return 1;
677                         break;
678                 default:
679                         if (a->fields[i].val != b->fields[i].val)
680                                 return 1;
681                 }
682         }
683
684         for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
685                 if (a->mask[i] != b->mask[i])
686                         return 1;
687
688         return 0;
689 }
690
691 /* Duplicate LSM field information.  The lsm_rule is opaque, so must be
692  * re-initialized. */
693 static inline int audit_dupe_lsm_field(struct audit_field *df,
694                                            struct audit_field *sf)
695 {
696         int ret = 0;
697         char *lsm_str;
698
699         /* our own copy of lsm_str */
700         lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
701         if (unlikely(!lsm_str))
702                 return -ENOMEM;
703         df->lsm_str = lsm_str;
704
705         /* our own (refreshed) copy of lsm_rule */
706         ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
707                                        (void **)&df->lsm_rule);
708         /* Keep currently invalid fields around in case they
709          * become valid after a policy reload. */
710         if (ret == -EINVAL) {
711                 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
712                        "invalid\n", df->lsm_str);
713                 ret = 0;
714         }
715
716         return ret;
717 }
718
719 /* Duplicate an audit rule.  This will be a deep copy with the exception
720  * of the watch - that pointer is carried over.  The LSM specific fields
721  * will be updated in the copy.  The point is to be able to replace the old
722  * rule with the new rule in the filterlist, then free the old rule.
723  * The rlist element is undefined; list manipulations are handled apart from
724  * the initial copy. */
725 struct audit_entry *audit_dupe_rule(struct audit_krule *old)
726 {
727         u32 fcount = old->field_count;
728         struct audit_entry *entry;
729         struct audit_krule *new;
730         char *fk;
731         int i, err = 0;
732
733         entry = audit_init_entry(fcount);
734         if (unlikely(!entry))
735                 return ERR_PTR(-ENOMEM);
736
737         new = &entry->rule;
738         new->vers_ops = old->vers_ops;
739         new->flags = old->flags;
740         new->listnr = old->listnr;
741         new->action = old->action;
742         for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
743                 new->mask[i] = old->mask[i];
744         new->prio = old->prio;
745         new->buflen = old->buflen;
746         new->inode_f = old->inode_f;
747         new->field_count = old->field_count;
748
749         /*
750          * note that we are OK with not refcounting here; audit_match_tree()
751          * never dereferences tree and we can't get false positives there
752          * since we'd have to have rule gone from the list *and* removed
753          * before the chunks found by lookup had been allocated, i.e. before
754          * the beginning of list scan.
755          */
756         new->tree = old->tree;
757         memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
758
759         /* deep copy this information, updating the lsm_rule fields, because
760          * the originals will all be freed when the old rule is freed. */
761         for (i = 0; i < fcount; i++) {
762                 switch (new->fields[i].type) {
763                 case AUDIT_SUBJ_USER:
764                 case AUDIT_SUBJ_ROLE:
765                 case AUDIT_SUBJ_TYPE:
766                 case AUDIT_SUBJ_SEN:
767                 case AUDIT_SUBJ_CLR:
768                 case AUDIT_OBJ_USER:
769                 case AUDIT_OBJ_ROLE:
770                 case AUDIT_OBJ_TYPE:
771                 case AUDIT_OBJ_LEV_LOW:
772                 case AUDIT_OBJ_LEV_HIGH:
773                         err = audit_dupe_lsm_field(&new->fields[i],
774                                                        &old->fields[i]);
775                         break;
776                 case AUDIT_FILTERKEY:
777                         fk = kstrdup(old->filterkey, GFP_KERNEL);
778                         if (unlikely(!fk))
779                                 err = -ENOMEM;
780                         else
781                                 new->filterkey = fk;
782                 }
783                 if (err) {
784                         audit_free_rule(entry);
785                         return ERR_PTR(err);
786                 }
787         }
788
789         if (old->watch) {
790                 audit_get_watch(old->watch);
791                 new->watch = old->watch;
792         }
793
794         return entry;
795 }
796
797 /* Find an existing audit rule.
798  * Caller must hold audit_filter_mutex to prevent stale rule data. */
799 static struct audit_entry *audit_find_rule(struct audit_entry *entry,
800                                            struct list_head **p)
801 {
802         struct audit_entry *e, *found = NULL;
803         struct list_head *list;
804         int h;
805
806         if (entry->rule.inode_f) {
807                 h = audit_hash_ino(entry->rule.inode_f->val);
808                 *p = list = &audit_inode_hash[h];
809         } else if (entry->rule.watch) {
810                 /* we don't know the inode number, so must walk entire hash */
811                 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
812                         list = &audit_inode_hash[h];
813                         list_for_each_entry(e, list, list)
814                                 if (!audit_compare_rule(&entry->rule, &e->rule)) {
815                                         found = e;
816                                         goto out;
817                                 }
818                 }
819                 goto out;
820         } else {
821                 *p = list = &audit_filter_list[entry->rule.listnr];
822         }
823
824         list_for_each_entry(e, list, list)
825                 if (!audit_compare_rule(&entry->rule, &e->rule)) {
826                         found = e;
827                         goto out;
828                 }
829
830 out:
831         return found;
832 }
833
834 static u64 prio_low = ~0ULL/2;
835 static u64 prio_high = ~0ULL/2 - 1;
836
837 /* Add rule to given filterlist if not a duplicate. */
838 static inline int audit_add_rule(struct audit_entry *entry)
839 {
840         struct audit_entry *e;
841         struct audit_watch *watch = entry->rule.watch;
842         struct audit_tree *tree = entry->rule.tree;
843         struct list_head *list;
844         int err;
845 #ifdef CONFIG_AUDITSYSCALL
846         int dont_count = 0;
847
848         /* If either of these, don't count towards total */
849         if (entry->rule.listnr == AUDIT_FILTER_USER ||
850                 entry->rule.listnr == AUDIT_FILTER_TYPE)
851                 dont_count = 1;
852 #endif
853
854         mutex_lock(&audit_filter_mutex);
855         e = audit_find_rule(entry, &list);
856         if (e) {
857                 mutex_unlock(&audit_filter_mutex);
858                 err = -EEXIST;
859                 /* normally audit_add_tree_rule() will free it on failure */
860                 if (tree)
861                         audit_put_tree(tree);
862                 goto error;
863         }
864
865         if (watch) {
866                 /* audit_filter_mutex is dropped and re-taken during this call */
867                 err = audit_add_watch(&entry->rule, &list);
868                 if (err) {
869                         mutex_unlock(&audit_filter_mutex);
870                         /*
871                          * normally audit_add_tree_rule() will free it
872                          * on failure
873                          */
874                         if (tree)
875                                 audit_put_tree(tree);
876                         goto error;
877                 }
878         }
879         if (tree) {
880                 err = audit_add_tree_rule(&entry->rule);
881                 if (err) {
882                         mutex_unlock(&audit_filter_mutex);
883                         goto error;
884                 }
885         }
886
887         entry->rule.prio = ~0ULL;
888         if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
889                 if (entry->rule.flags & AUDIT_FILTER_PREPEND)
890                         entry->rule.prio = ++prio_high;
891                 else
892                         entry->rule.prio = --prio_low;
893         }
894
895         if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
896                 list_add(&entry->rule.list,
897                          &audit_rules_list[entry->rule.listnr]);
898                 list_add_rcu(&entry->list, list);
899                 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
900         } else {
901                 list_add_tail(&entry->rule.list,
902                               &audit_rules_list[entry->rule.listnr]);
903                 list_add_tail_rcu(&entry->list, list);
904         }
905 #ifdef CONFIG_AUDITSYSCALL
906         if (!dont_count)
907                 audit_n_rules++;
908
909         if (!audit_match_signal(entry))
910                 audit_signals++;
911 #endif
912         mutex_unlock(&audit_filter_mutex);
913
914         return 0;
915
916 error:
917         if (watch)
918                 audit_put_watch(watch); /* tmp watch, matches initial get */
919         return err;
920 }
921
922 /* Remove an existing rule from filterlist. */
923 static inline int audit_del_rule(struct audit_entry *entry)
924 {
925         struct audit_entry  *e;
926         struct audit_watch *watch = entry->rule.watch;
927         struct audit_tree *tree = entry->rule.tree;
928         struct list_head *list;
929         int ret = 0;
930 #ifdef CONFIG_AUDITSYSCALL
931         int dont_count = 0;
932
933         /* If either of these, don't count towards total */
934         if (entry->rule.listnr == AUDIT_FILTER_USER ||
935                 entry->rule.listnr == AUDIT_FILTER_TYPE)
936                 dont_count = 1;
937 #endif
938
939         mutex_lock(&audit_filter_mutex);
940         e = audit_find_rule(entry, &list);
941         if (!e) {
942                 mutex_unlock(&audit_filter_mutex);
943                 ret = -ENOENT;
944                 goto out;
945         }
946
947         if (e->rule.watch)
948                 audit_remove_watch_rule(&e->rule);
949
950         if (e->rule.tree)
951                 audit_remove_tree_rule(&e->rule);
952
953         list_del_rcu(&e->list);
954         list_del(&e->rule.list);
955         call_rcu(&e->rcu, audit_free_rule_rcu);
956
957 #ifdef CONFIG_AUDITSYSCALL
958         if (!dont_count)
959                 audit_n_rules--;
960
961         if (!audit_match_signal(entry))
962                 audit_signals--;
963 #endif
964         mutex_unlock(&audit_filter_mutex);
965
966 out:
967         if (watch)
968                 audit_put_watch(watch); /* match initial get */
969         if (tree)
970                 audit_put_tree(tree);   /* that's the temporary one */
971
972         return ret;
973 }
974
975 /* List rules using struct audit_rule_data. */
976 static void audit_list_rules(__u32 portid, int seq, struct sk_buff_head *q)
977 {
978         struct sk_buff *skb;
979         struct audit_krule *r;
980         int i;
981
982         /* This is a blocking read, so use audit_filter_mutex instead of rcu
983          * iterator to sync with list writers. */
984         for (i=0; i<AUDIT_NR_FILTERS; i++) {
985                 list_for_each_entry(r, &audit_rules_list[i], list) {
986                         struct audit_rule_data *data;
987
988                         data = audit_krule_to_data(r);
989                         if (unlikely(!data))
990                                 break;
991                         skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES,
992                                                0, 1, data,
993                                                sizeof(*data) + data->buflen);
994                         if (skb)
995                                 skb_queue_tail(q, skb);
996                         kfree(data);
997                 }
998         }
999         skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
1000         if (skb)
1001                 skb_queue_tail(q, skb);
1002 }
1003
1004 /* Log rule additions and removals */
1005 static void audit_log_rule_change(char *action, struct audit_krule *rule, int res)
1006 {
1007         struct audit_buffer *ab;
1008         uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1009         unsigned int sessionid = audit_get_sessionid(current);
1010
1011         if (!audit_enabled)
1012                 return;
1013
1014         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1015         if (!ab)
1016                 return;
1017         audit_log_format(ab, "auid=%u ses=%u" ,loginuid, sessionid);
1018         audit_log_task_context(ab);
1019         audit_log_format(ab, " op=");
1020         audit_log_string(ab, action);
1021         audit_log_key(ab, rule->filterkey);
1022         audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1023         audit_log_end(ab);
1024 }
1025
1026 /**
1027  * audit_rule_change - apply all rules to the specified message type
1028  * @type: audit message type
1029  * @portid: target port id for netlink audit messages
1030  * @seq: netlink audit message sequence (serial) number
1031  * @data: payload data
1032  * @datasz: size of payload data
1033  */
1034 int audit_rule_change(int type, __u32 portid, int seq, void *data,
1035                         size_t datasz)
1036 {
1037         int err = 0;
1038         struct audit_entry *entry;
1039
1040         switch (type) {
1041         case AUDIT_ADD_RULE:
1042                 entry = audit_data_to_entry(data, datasz);
1043                 if (IS_ERR(entry))
1044                         return PTR_ERR(entry);
1045
1046                 err = audit_add_rule(entry);
1047                 audit_log_rule_change("add rule", &entry->rule, !err);
1048                 if (err)
1049                         audit_free_rule(entry);
1050                 break;
1051         case AUDIT_DEL_RULE:
1052                 entry = audit_data_to_entry(data, datasz);
1053                 if (IS_ERR(entry))
1054                         return PTR_ERR(entry);
1055
1056                 err = audit_del_rule(entry);
1057                 audit_log_rule_change("remove rule", &entry->rule, !err);
1058                 audit_free_rule(entry);
1059                 break;
1060         default:
1061                 return -EINVAL;
1062         }
1063
1064         return err;
1065 }
1066
1067 /**
1068  * audit_list_rules_send - list the audit rules
1069  * @portid: target portid for netlink audit messages
1070  * @seq: netlink audit message sequence (serial) number
1071  */
1072 int audit_list_rules_send(__u32 portid, int seq)
1073 {
1074         struct task_struct *tsk;
1075         struct audit_netlink_list *dest;
1076         int err = 0;
1077
1078         /* We can't just spew out the rules here because we might fill
1079          * the available socket buffer space and deadlock waiting for
1080          * auditctl to read from it... which isn't ever going to
1081          * happen if we're actually running in the context of auditctl
1082          * trying to _send_ the stuff */
1083
1084         dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
1085         if (!dest)
1086                 return -ENOMEM;
1087         dest->net = get_net(current->nsproxy->net_ns);
1088         dest->portid = portid;
1089         skb_queue_head_init(&dest->q);
1090
1091         mutex_lock(&audit_filter_mutex);
1092         audit_list_rules(portid, seq, &dest->q);
1093         mutex_unlock(&audit_filter_mutex);
1094
1095         tsk = kthread_run(audit_send_list, dest, "audit_send_list");
1096         if (IS_ERR(tsk)) {
1097                 skb_queue_purge(&dest->q);
1098                 kfree(dest);
1099                 err = PTR_ERR(tsk);
1100         }
1101
1102         return err;
1103 }
1104
1105 int audit_comparator(u32 left, u32 op, u32 right)
1106 {
1107         switch (op) {
1108         case Audit_equal:
1109                 return (left == right);
1110         case Audit_not_equal:
1111                 return (left != right);
1112         case Audit_lt:
1113                 return (left < right);
1114         case Audit_le:
1115                 return (left <= right);
1116         case Audit_gt:
1117                 return (left > right);
1118         case Audit_ge:
1119                 return (left >= right);
1120         case Audit_bitmask:
1121                 return (left & right);
1122         case Audit_bittest:
1123                 return ((left & right) == right);
1124         default:
1125                 BUG();
1126                 return 0;
1127         }
1128 }
1129
1130 int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
1131 {
1132         switch (op) {
1133         case Audit_equal:
1134                 return uid_eq(left, right);
1135         case Audit_not_equal:
1136                 return !uid_eq(left, right);
1137         case Audit_lt:
1138                 return uid_lt(left, right);
1139         case Audit_le:
1140                 return uid_lte(left, right);
1141         case Audit_gt:
1142                 return uid_gt(left, right);
1143         case Audit_ge:
1144                 return uid_gte(left, right);
1145         case Audit_bitmask:
1146         case Audit_bittest:
1147         default:
1148                 BUG();
1149                 return 0;
1150         }
1151 }
1152
1153 int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
1154 {
1155         switch (op) {
1156         case Audit_equal:
1157                 return gid_eq(left, right);
1158         case Audit_not_equal:
1159                 return !gid_eq(left, right);
1160         case Audit_lt:
1161                 return gid_lt(left, right);
1162         case Audit_le:
1163                 return gid_lte(left, right);
1164         case Audit_gt:
1165                 return gid_gt(left, right);
1166         case Audit_ge:
1167                 return gid_gte(left, right);
1168         case Audit_bitmask:
1169         case Audit_bittest:
1170         default:
1171                 BUG();
1172                 return 0;
1173         }
1174 }
1175
1176 /**
1177  * parent_len - find the length of the parent portion of a pathname
1178  * @path: pathname of which to determine length
1179  */
1180 int parent_len(const char *path)
1181 {
1182         int plen;
1183         const char *p;
1184
1185         plen = strlen(path);
1186
1187         if (plen == 0)
1188                 return plen;
1189
1190         /* disregard trailing slashes */
1191         p = path + plen - 1;
1192         while ((*p == '/') && (p > path))
1193                 p--;
1194
1195         /* walk backward until we find the next slash or hit beginning */
1196         while ((*p != '/') && (p > path))
1197                 p--;
1198
1199         /* did we find a slash? Then increment to include it in path */
1200         if (*p == '/')
1201                 p++;
1202
1203         return p - path;
1204 }
1205
1206 /**
1207  * audit_compare_dname_path - compare given dentry name with last component in
1208  *                            given path. Return of 0 indicates a match.
1209  * @dname:      dentry name that we're comparing
1210  * @path:       full pathname that we're comparing
1211  * @parentlen:  length of the parent if known. Passing in AUDIT_NAME_FULL
1212  *              here indicates that we must compute this value.
1213  */
1214 int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
1215 {
1216         int dlen, pathlen;
1217         const char *p;
1218
1219         dlen = strlen(dname);
1220         pathlen = strlen(path);
1221         if (pathlen < dlen)
1222                 return 1;
1223
1224         parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
1225         if (pathlen - parentlen != dlen)
1226                 return 1;
1227
1228         p = path + parentlen;
1229
1230         return strncmp(p, dname, dlen);
1231 }
1232
1233 static int audit_filter_user_rules(struct audit_krule *rule, int type,
1234                                    enum audit_state *state)
1235 {
1236         int i;
1237
1238         for (i = 0; i < rule->field_count; i++) {
1239                 struct audit_field *f = &rule->fields[i];
1240                 int result = 0;
1241                 u32 sid;
1242
1243                 switch (f->type) {
1244                 case AUDIT_PID:
1245                         result = audit_comparator(task_pid_vnr(current), f->op, f->val);
1246                         break;
1247                 case AUDIT_UID:
1248                         result = audit_uid_comparator(current_uid(), f->op, f->uid);
1249                         break;
1250                 case AUDIT_GID:
1251                         result = audit_gid_comparator(current_gid(), f->op, f->gid);
1252                         break;
1253                 case AUDIT_LOGINUID:
1254                         result = audit_uid_comparator(audit_get_loginuid(current),
1255                                                   f->op, f->uid);
1256                         break;
1257                 case AUDIT_LOGINUID_SET:
1258                         result = audit_comparator(audit_loginuid_set(current),
1259                                                   f->op, f->val);
1260                         break;
1261                 case AUDIT_MSGTYPE:
1262                         result = audit_comparator(type, f->op, f->val);
1263                         break;
1264                 case AUDIT_SUBJ_USER:
1265                 case AUDIT_SUBJ_ROLE:
1266                 case AUDIT_SUBJ_TYPE:
1267                 case AUDIT_SUBJ_SEN:
1268                 case AUDIT_SUBJ_CLR:
1269                         if (f->lsm_rule) {
1270                                 security_task_getsecid(current, &sid);
1271                                 result = security_audit_rule_match(sid,
1272                                                                    f->type,
1273                                                                    f->op,
1274                                                                    f->lsm_rule,
1275                                                                    NULL);
1276                         }
1277                         break;
1278                 }
1279
1280                 if (!result)
1281                         return 0;
1282         }
1283         switch (rule->action) {
1284         case AUDIT_NEVER:    *state = AUDIT_DISABLED;       break;
1285         case AUDIT_ALWAYS:   *state = AUDIT_RECORD_CONTEXT; break;
1286         }
1287         return 1;
1288 }
1289
1290 int audit_filter_user(int type)
1291 {
1292         enum audit_state state = AUDIT_DISABLED;
1293         struct audit_entry *e;
1294         int rc, ret;
1295
1296         ret = 1; /* Audit by default */
1297
1298         rcu_read_lock();
1299         list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
1300                 rc = audit_filter_user_rules(&e->rule, type, &state);
1301                 if (rc) {
1302                         if (rc > 0 && state == AUDIT_DISABLED)
1303                                 ret = 0;
1304                         break;
1305                 }
1306         }
1307         rcu_read_unlock();
1308
1309         return ret;
1310 }
1311
1312 int audit_filter_type(int type)
1313 {
1314         struct audit_entry *e;
1315         int result = 0;
1316
1317         rcu_read_lock();
1318         if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1319                 goto unlock_and_return;
1320
1321         list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1322                                 list) {
1323                 int i;
1324                 for (i = 0; i < e->rule.field_count; i++) {
1325                         struct audit_field *f = &e->rule.fields[i];
1326                         if (f->type == AUDIT_MSGTYPE) {
1327                                 result = audit_comparator(type, f->op, f->val);
1328                                 if (!result)
1329                                         break;
1330                         }
1331                 }
1332                 if (result)
1333                         goto unlock_and_return;
1334         }
1335 unlock_and_return:
1336         rcu_read_unlock();
1337         return result;
1338 }
1339
1340 static int update_lsm_rule(struct audit_krule *r)
1341 {
1342         struct audit_entry *entry = container_of(r, struct audit_entry, rule);
1343         struct audit_entry *nentry;
1344         int err = 0;
1345
1346         if (!security_audit_rule_known(r))
1347                 return 0;
1348
1349         nentry = audit_dupe_rule(r);
1350         if (IS_ERR(nentry)) {
1351                 /* save the first error encountered for the
1352                  * return value */
1353                 err = PTR_ERR(nentry);
1354                 audit_panic("error updating LSM filters");
1355                 if (r->watch)
1356                         list_del(&r->rlist);
1357                 list_del_rcu(&entry->list);
1358                 list_del(&r->list);
1359         } else {
1360                 if (r->watch || r->tree)
1361                         list_replace_init(&r->rlist, &nentry->rule.rlist);
1362                 list_replace_rcu(&entry->list, &nentry->list);
1363                 list_replace(&r->list, &nentry->rule.list);
1364         }
1365         call_rcu(&entry->rcu, audit_free_rule_rcu);
1366
1367         return err;
1368 }
1369
1370 /* This function will re-initialize the lsm_rule field of all applicable rules.
1371  * It will traverse the filter lists serarching for rules that contain LSM
1372  * specific filter fields.  When such a rule is found, it is copied, the
1373  * LSM field is re-initialized, and the old rule is replaced with the
1374  * updated rule. */
1375 int audit_update_lsm_rules(void)
1376 {
1377         struct audit_krule *r, *n;
1378         int i, err = 0;
1379
1380         /* audit_filter_mutex synchronizes the writers */
1381         mutex_lock(&audit_filter_mutex);
1382
1383         for (i = 0; i < AUDIT_NR_FILTERS; i++) {
1384                 list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1385                         int res = update_lsm_rule(r);
1386                         if (!err)
1387                                 err = res;
1388                 }
1389         }
1390         mutex_unlock(&audit_filter_mutex);
1391
1392         return err;
1393 }