]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - security/smack/smackfs.c
856c8a2875231f2a6d64075bb114f99e0c356628
[linux-2.6-omap-h63xx.git] / security / smack / smackfs.c
1 /*
2  * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
3  *
4  *      This program is free software; you can redistribute it and/or modify
5  *      it under the terms of the GNU General Public License as published by
6  *      the Free Software Foundation, version 2.
7  *
8  * Authors:
9  *      Casey Schaufler <casey@schaufler-ca.com>
10  *      Ahmed S. Darwish <darwish.07@gmail.com>
11  *
12  * Special thanks to the authors of selinuxfs.
13  *
14  *      Karl MacMillan <kmacmillan@tresys.com>
15  *      James Morris <jmorris@redhat.com>
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/vmalloc.h>
21 #include <linux/security.h>
22 #include <linux/mutex.h>
23 #include <net/net_namespace.h>
24 #include <net/netlabel.h>
25 #include <net/cipso_ipv4.h>
26 #include <linux/seq_file.h>
27 #include <linux/ctype.h>
28 #include <linux/audit.h>
29 #include "smack.h"
30
31 /*
32  * smackfs pseudo filesystem.
33  */
34
35 enum smk_inos {
36         SMK_ROOT_INO    = 2,
37         SMK_LOAD        = 3,    /* load policy */
38         SMK_CIPSO       = 4,    /* load label -> CIPSO mapping */
39         SMK_DOI         = 5,    /* CIPSO DOI */
40         SMK_DIRECT      = 6,    /* CIPSO level indicating direct label */
41         SMK_AMBIENT     = 7,    /* internet ambient label */
42         SMK_NETLBLADDR  = 8,    /* single label hosts */
43         SMK_ONLYCAP     = 9,    /* the only "capable" label */
44 };
45
46 /*
47  * List locks
48  */
49 static DEFINE_MUTEX(smack_list_lock);
50 static DEFINE_MUTEX(smack_cipso_lock);
51 static DEFINE_MUTEX(smack_ambient_lock);
52 static DEFINE_MUTEX(smk_netlbladdr_lock);
53
54 /*
55  * This is the "ambient" label for network traffic.
56  * If it isn't somehow marked, use this.
57  * It can be reset via smackfs/ambient
58  */
59 char *smack_net_ambient = smack_known_floor.smk_known;
60
61 /*
62  * This is the level in a CIPSO header that indicates a
63  * smack label is contained directly in the category set.
64  * It can be reset via smackfs/direct
65  */
66 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
67
68 /*
69  * Unless a process is running with this label even
70  * having CAP_MAC_OVERRIDE isn't enough to grant
71  * privilege to violate MAC policy. If no label is
72  * designated (the NULL case) capabilities apply to
73  * everyone. It is expected that the hat (^) label
74  * will be used if any label is used.
75  */
76 char *smack_onlycap;
77
78 /*
79  * Certain IP addresses may be designated as single label hosts.
80  * Packets are sent there unlabeled, but only from tasks that
81  * can write to the specified label.
82  */
83
84 LIST_HEAD(smk_netlbladdr_list);
85 LIST_HEAD(smack_rule_list);
86
87 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
88
89 #define SEQ_READ_FINISHED       1
90
91 /*
92  * Values for parsing cipso rules
93  * SMK_DIGITLEN: Length of a digit field in a rule.
94  * SMK_CIPSOMIN: Minimum possible cipso rule length.
95  * SMK_CIPSOMAX: Maximum possible cipso rule length.
96  */
97 #define SMK_DIGITLEN 4
98 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
99 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
100
101 /*
102  * Values for parsing MAC rules
103  * SMK_ACCESS: Maximum possible combination of access permissions
104  * SMK_ACCESSLEN: Maximum length for a rule access field
105  * SMK_LOADLEN: Smack rule length
106  */
107 #define SMK_ACCESS    "rwxa"
108 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
109 #define SMK_LOADLEN   (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
110
111 /**
112  * smk_netlabel_audit_set - fill a netlbl_audit struct
113  * @nap: structure to fill
114  */
115 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
116 {
117         nap->loginuid = audit_get_loginuid(current);
118         nap->sessionid = audit_get_sessionid(current);
119         nap->secid = smack_to_secid(current_security());
120 }
121
122 /*
123  * Values for parsing single label host rules
124  * "1.2.3.4 X"
125  * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
126  */
127 #define SMK_NETLBLADDRMIN       9
128 #define SMK_NETLBLADDRMAX       42
129
130 /*
131  * Seq_file read operations for /smack/load
132  */
133
134 static void *load_seq_start(struct seq_file *s, loff_t *pos)
135 {
136         if (*pos == SEQ_READ_FINISHED)
137                 return NULL;
138         if (list_empty(&smack_rule_list))
139                 return NULL;
140         return smack_rule_list.next;
141 }
142
143 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
144 {
145         struct list_head *list = v;
146
147         if (list_is_last(list, &smack_rule_list)) {
148                 *pos = SEQ_READ_FINISHED;
149                 return NULL;
150         }
151         return list->next;
152 }
153
154 static int load_seq_show(struct seq_file *s, void *v)
155 {
156         struct list_head *list = v;
157         struct smack_rule *srp =
158                  list_entry(list, struct smack_rule, list);
159
160         seq_printf(s, "%s %s", (char *)srp->smk_subject,
161                    (char *)srp->smk_object);
162
163         seq_putc(s, ' ');
164
165         if (srp->smk_access & MAY_READ)
166                 seq_putc(s, 'r');
167         if (srp->smk_access & MAY_WRITE)
168                 seq_putc(s, 'w');
169         if (srp->smk_access & MAY_EXEC)
170                 seq_putc(s, 'x');
171         if (srp->smk_access & MAY_APPEND)
172                 seq_putc(s, 'a');
173         if (srp->smk_access == 0)
174                 seq_putc(s, '-');
175
176         seq_putc(s, '\n');
177
178         return 0;
179 }
180
181 static void load_seq_stop(struct seq_file *s, void *v)
182 {
183         /* No-op */
184 }
185
186 static struct seq_operations load_seq_ops = {
187         .start = load_seq_start,
188         .next  = load_seq_next,
189         .show  = load_seq_show,
190         .stop  = load_seq_stop,
191 };
192
193 /**
194  * smk_open_load - open() for /smack/load
195  * @inode: inode structure representing file
196  * @file: "load" file pointer
197  *
198  * For reading, use load_seq_* seq_file reading operations.
199  */
200 static int smk_open_load(struct inode *inode, struct file *file)
201 {
202         return seq_open(file, &load_seq_ops);
203 }
204
205 /**
206  * smk_set_access - add a rule to the rule list
207  * @srp: the new rule to add
208  *
209  * Looks through the current subject/object/access list for
210  * the subject/object pair and replaces the access that was
211  * there. If the pair isn't found add it with the specified
212  * access.
213  *
214  * Returns 0 if nothing goes wrong or -ENOMEM if it fails
215  * during the allocation of the new pair to add.
216  */
217 static int smk_set_access(struct smack_rule *srp)
218 {
219         struct smack_rule *sp;
220         int ret = 0;
221         int found;
222         mutex_lock(&smack_list_lock);
223
224         found = 0;
225         list_for_each_entry_rcu(sp, &smack_rule_list, list) {
226                 if (sp->smk_subject == srp->smk_subject &&
227                     sp->smk_object == srp->smk_object) {
228                         found = 1;
229                         sp->smk_access = srp->smk_access;
230                         break;
231                 }
232         }
233         if (found == 0)
234                 list_add_rcu(&srp->list, &smack_rule_list);
235
236         mutex_unlock(&smack_list_lock);
237
238         return ret;
239 }
240
241 /**
242  * smk_write_load - write() for /smack/load
243  * @file: file pointer, not actually used
244  * @buf: where to get the data from
245  * @count: bytes sent
246  * @ppos: where to start - must be 0
247  *
248  * Get one smack access rule from above.
249  * The format is exactly:
250  *     char subject[SMK_LABELLEN]
251  *     char object[SMK_LABELLEN]
252  *     char access[SMK_ACCESSLEN]
253  *
254  * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
255  */
256 static ssize_t smk_write_load(struct file *file, const char __user *buf,
257                               size_t count, loff_t *ppos)
258 {
259         struct smack_rule *rule;
260         char *data;
261         int rc = -EINVAL;
262
263         /*
264          * Must have privilege.
265          * No partial writes.
266          * Enough data must be present.
267          */
268         if (!capable(CAP_MAC_ADMIN))
269                 return -EPERM;
270
271         if (*ppos != 0 || count != SMK_LOADLEN)
272                 return -EINVAL;
273
274         data = kzalloc(count, GFP_KERNEL);
275         if (data == NULL)
276                 return -ENOMEM;
277
278         if (copy_from_user(data, buf, count) != 0) {
279                 rc = -EFAULT;
280                 goto out;
281         }
282
283         rule = kzalloc(sizeof(*rule), GFP_KERNEL);
284         if (rule == NULL) {
285                 rc = -ENOMEM;
286                 goto out;
287         }
288
289         rule->smk_subject = smk_import(data, 0);
290         if (rule->smk_subject == NULL)
291                 goto out_free_rule;
292
293         rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
294         if (rule->smk_object == NULL)
295                 goto out_free_rule;
296
297         rule->smk_access = 0;
298
299         switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
300         case '-':
301                 break;
302         case 'r':
303         case 'R':
304                 rule->smk_access |= MAY_READ;
305                 break;
306         default:
307                 goto out_free_rule;
308         }
309
310         switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
311         case '-':
312                 break;
313         case 'w':
314         case 'W':
315                 rule->smk_access |= MAY_WRITE;
316                 break;
317         default:
318                 goto out_free_rule;
319         }
320
321         switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
322         case '-':
323                 break;
324         case 'x':
325         case 'X':
326                 rule->smk_access |= MAY_EXEC;
327                 break;
328         default:
329                 goto out_free_rule;
330         }
331
332         switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
333         case '-':
334                 break;
335         case 'a':
336         case 'A':
337                 rule->smk_access |= MAY_APPEND;
338                 break;
339         default:
340                 goto out_free_rule;
341         }
342
343         rc = smk_set_access(rule);
344
345         if (!rc)
346                 rc = count;
347         goto out;
348
349 out_free_rule:
350         kfree(rule);
351 out:
352         kfree(data);
353         return rc;
354 }
355
356 static const struct file_operations smk_load_ops = {
357         .open           = smk_open_load,
358         .read           = seq_read,
359         .llseek         = seq_lseek,
360         .write          = smk_write_load,
361         .release        = seq_release,
362 };
363
364 /**
365  * smk_cipso_doi - initialize the CIPSO domain
366  */
367 static void smk_cipso_doi(void)
368 {
369         int rc;
370         struct cipso_v4_doi *doip;
371         struct netlbl_audit nai;
372
373         smk_netlabel_audit_set(&nai);
374
375         rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
376         if (rc != 0)
377                 printk(KERN_WARNING "%s:%d remove rc = %d\n",
378                        __func__, __LINE__, rc);
379
380         doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
381         if (doip == NULL)
382                 panic("smack:  Failed to initialize cipso DOI.\n");
383         doip->map.std = NULL;
384         doip->doi = smk_cipso_doi_value;
385         doip->type = CIPSO_V4_MAP_PASS;
386         doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
387         for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
388                 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
389
390         rc = netlbl_cfg_cipsov4_add(doip, &nai);
391         if (rc != 0) {
392                 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
393                        __func__, __LINE__, rc);
394                 kfree(doip);
395                 return;
396         }
397         rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
398         if (rc != 0) {
399                 printk(KERN_WARNING "%s:%d map add rc = %d\n",
400                        __func__, __LINE__, rc);
401                 kfree(doip);
402                 return;
403         }
404 }
405
406 /**
407  * smk_unlbl_ambient - initialize the unlabeled domain
408  * @oldambient: previous domain string
409  */
410 static void smk_unlbl_ambient(char *oldambient)
411 {
412         int rc;
413         struct netlbl_audit nai;
414
415         smk_netlabel_audit_set(&nai);
416
417         if (oldambient != NULL) {
418                 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
419                 if (rc != 0)
420                         printk(KERN_WARNING "%s:%d remove rc = %d\n",
421                                __func__, __LINE__, rc);
422         }
423
424         rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
425                                       NULL, NULL, &nai);
426         if (rc != 0)
427                 printk(KERN_WARNING "%s:%d add rc = %d\n",
428                        __func__, __LINE__, rc);
429 }
430
431 /*
432  * Seq_file read operations for /smack/cipso
433  */
434
435 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
436 {
437         if (*pos == SEQ_READ_FINISHED)
438                 return NULL;
439         if (list_empty(&smack_known_list))
440                 return NULL;
441
442         return smack_known_list.next;
443 }
444
445 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
446 {
447         struct list_head  *list = v;
448
449         /*
450          * labels with no associated cipso value wont be printed
451          * in cipso_seq_show
452          */
453         if (list_is_last(list, &smack_known_list)) {
454                 *pos = SEQ_READ_FINISHED;
455                 return NULL;
456         }
457
458         return list->next;
459 }
460
461 /*
462  * Print cipso labels in format:
463  * label level[/cat[,cat]]
464  */
465 static int cipso_seq_show(struct seq_file *s, void *v)
466 {
467         struct list_head  *list = v;
468         struct smack_known *skp =
469                  list_entry(list, struct smack_known, list);
470         struct smack_cipso *scp = skp->smk_cipso;
471         char *cbp;
472         char sep = '/';
473         int cat = 1;
474         int i;
475         unsigned char m;
476
477         if (scp == NULL)
478                 return 0;
479
480         seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
481
482         cbp = scp->smk_catset;
483         for (i = 0; i < SMK_LABELLEN; i++)
484                 for (m = 0x80; m != 0; m >>= 1) {
485                         if (m & cbp[i]) {
486                                 seq_printf(s, "%c%d", sep, cat);
487                                 sep = ',';
488                         }
489                         cat++;
490                 }
491
492         seq_putc(s, '\n');
493
494         return 0;
495 }
496
497 static void cipso_seq_stop(struct seq_file *s, void *v)
498 {
499         /* No-op */
500 }
501
502 static struct seq_operations cipso_seq_ops = {
503         .start = cipso_seq_start,
504         .stop  = cipso_seq_stop,
505         .next  = cipso_seq_next,
506         .show  = cipso_seq_show,
507 };
508
509 /**
510  * smk_open_cipso - open() for /smack/cipso
511  * @inode: inode structure representing file
512  * @file: "cipso" file pointer
513  *
514  * Connect our cipso_seq_* operations with /smack/cipso
515  * file_operations
516  */
517 static int smk_open_cipso(struct inode *inode, struct file *file)
518 {
519         return seq_open(file, &cipso_seq_ops);
520 }
521
522 /**
523  * smk_write_cipso - write() for /smack/cipso
524  * @file: file pointer, not actually used
525  * @buf: where to get the data from
526  * @count: bytes sent
527  * @ppos: where to start
528  *
529  * Accepts only one cipso rule per write call.
530  * Returns number of bytes written or error code, as appropriate
531  */
532 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
533                                size_t count, loff_t *ppos)
534 {
535         struct smack_known *skp;
536         struct smack_cipso *scp = NULL;
537         char mapcatset[SMK_LABELLEN];
538         int maplevel;
539         int cat;
540         int catlen;
541         ssize_t rc = -EINVAL;
542         char *data = NULL;
543         char *rule;
544         int ret;
545         int i;
546
547         /*
548          * Must have privilege.
549          * No partial writes.
550          * Enough data must be present.
551          */
552         if (!capable(CAP_MAC_ADMIN))
553                 return -EPERM;
554         if (*ppos != 0)
555                 return -EINVAL;
556         if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
557                 return -EINVAL;
558
559         data = kzalloc(count + 1, GFP_KERNEL);
560         if (data == NULL)
561                 return -ENOMEM;
562
563         if (copy_from_user(data, buf, count) != 0) {
564                 rc = -EFAULT;
565                 goto unlockedout;
566         }
567
568         data[count] = '\0';
569         rule = data;
570         /*
571          * Only allow one writer at a time. Writes should be
572          * quite rare and small in any case.
573          */
574         mutex_lock(&smack_cipso_lock);
575
576         skp = smk_import_entry(rule, 0);
577         if (skp == NULL)
578                 goto out;
579
580         rule += SMK_LABELLEN;
581         ret = sscanf(rule, "%d", &maplevel);
582         if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
583                 goto out;
584
585         rule += SMK_DIGITLEN;
586         ret = sscanf(rule, "%d", &catlen);
587         if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
588                 goto out;
589
590         if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
591                 goto out;
592
593         memset(mapcatset, 0, sizeof(mapcatset));
594
595         for (i = 0; i < catlen; i++) {
596                 rule += SMK_DIGITLEN;
597                 ret = sscanf(rule, "%d", &cat);
598                 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
599                         goto out;
600
601                 smack_catset_bit(cat, mapcatset);
602         }
603
604         if (skp->smk_cipso == NULL) {
605                 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
606                 if (scp == NULL) {
607                         rc = -ENOMEM;
608                         goto out;
609                 }
610         }
611
612         spin_lock_bh(&skp->smk_cipsolock);
613
614         if (scp == NULL)
615                 scp = skp->smk_cipso;
616         else
617                 skp->smk_cipso = scp;
618
619         scp->smk_level = maplevel;
620         memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
621
622         spin_unlock_bh(&skp->smk_cipsolock);
623
624         rc = count;
625 out:
626         mutex_unlock(&smack_cipso_lock);
627 unlockedout:
628         kfree(data);
629         return rc;
630 }
631
632 static const struct file_operations smk_cipso_ops = {
633         .open           = smk_open_cipso,
634         .read           = seq_read,
635         .llseek         = seq_lseek,
636         .write          = smk_write_cipso,
637         .release        = seq_release,
638 };
639
640 /*
641  * Seq_file read operations for /smack/netlabel
642  */
643
644 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
645 {
646         if (*pos == SEQ_READ_FINISHED)
647                 return NULL;
648         if (list_empty(&smk_netlbladdr_list))
649                 return NULL;
650         return smk_netlbladdr_list.next;
651 }
652
653 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
654 {
655         struct list_head *list = v;
656
657         if (list_is_last(list, &smk_netlbladdr_list)) {
658                 *pos = SEQ_READ_FINISHED;
659                 return NULL;
660         }
661
662         return list->next;
663 }
664 #define BEBITS  (sizeof(__be32) * 8)
665
666 /*
667  * Print host/label pairs
668  */
669 static int netlbladdr_seq_show(struct seq_file *s, void *v)
670 {
671         struct list_head *list = v;
672         struct smk_netlbladdr *skp =
673                          list_entry(list, struct smk_netlbladdr, list);
674         unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
675         int maskn;
676         u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
677
678         for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
679
680         seq_printf(s, "%u.%u.%u.%u/%d %s\n",
681                 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
682
683         return 0;
684 }
685
686 static void netlbladdr_seq_stop(struct seq_file *s, void *v)
687 {
688         /* No-op */
689 }
690
691 static struct seq_operations netlbladdr_seq_ops = {
692         .start = netlbladdr_seq_start,
693         .stop  = netlbladdr_seq_stop,
694         .next  = netlbladdr_seq_next,
695         .show  = netlbladdr_seq_show,
696 };
697
698 /**
699  * smk_open_netlbladdr - open() for /smack/netlabel
700  * @inode: inode structure representing file
701  * @file: "netlabel" file pointer
702  *
703  * Connect our netlbladdr_seq_* operations with /smack/netlabel
704  * file_operations
705  */
706 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
707 {
708         return seq_open(file, &netlbladdr_seq_ops);
709 }
710
711 /**
712  * smk_netlbladdr_insert
713  * @new : netlabel to insert
714  *
715  * This helper insert netlabel in the smack_netlbladdrs list
716  * sorted by netmask length (longest to smallest)
717  * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
718  *
719  */
720 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
721 {
722         struct smk_netlbladdr *m, *m_next;
723
724         if (list_empty(&smk_netlbladdr_list)) {
725                 list_add_rcu(&new->list, &smk_netlbladdr_list);
726                 return;
727         }
728
729         m = list_entry(rcu_dereference(smk_netlbladdr_list.next),
730                          struct smk_netlbladdr, list);
731
732         /* the comparison '>' is a bit hacky, but works */
733         if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
734                 list_add_rcu(&new->list, &smk_netlbladdr_list);
735                 return;
736         }
737
738         list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
739                 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
740                         list_add_rcu(&new->list, &m->list);
741                         return;
742                 }
743                 m_next = list_entry(rcu_dereference(m->list.next),
744                                  struct smk_netlbladdr, list);
745                 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
746                         list_add_rcu(&new->list, &m->list);
747                         return;
748                 }
749         }
750 }
751
752
753 /**
754  * smk_write_netlbladdr - write() for /smack/netlabel
755  * @file: file pointer, not actually used
756  * @buf: where to get the data from
757  * @count: bytes sent
758  * @ppos: where to start
759  *
760  * Accepts only one netlbladdr per write call.
761  * Returns number of bytes written or error code, as appropriate
762  */
763 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
764                                 size_t count, loff_t *ppos)
765 {
766         struct smk_netlbladdr *skp;
767         struct sockaddr_in newname;
768         char smack[SMK_LABELLEN];
769         char *sp;
770         char data[SMK_NETLBLADDRMAX];
771         char *host = (char *)&newname.sin_addr.s_addr;
772         int rc;
773         struct netlbl_audit audit_info;
774         struct in_addr mask;
775         unsigned int m;
776         int found;
777         u32 mask_bits = (1<<31);
778         __be32 nsa;
779         u32 temp_mask;
780
781         /*
782          * Must have privilege.
783          * No partial writes.
784          * Enough data must be present.
785          * "<addr/mask, as a.b.c.d/e><space><label>"
786          * "<addr, as a.b.c.d><space><label>"
787          */
788         if (!capable(CAP_MAC_ADMIN))
789                 return -EPERM;
790         if (*ppos != 0)
791                 return -EINVAL;
792         if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
793                 return -EINVAL;
794         if (copy_from_user(data, buf, count) != 0)
795                 return -EFAULT;
796
797         data[count] = '\0';
798
799         rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
800                 &host[0], &host[1], &host[2], &host[3], &m, smack);
801         if (rc != 6) {
802                 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
803                         &host[0], &host[1], &host[2], &host[3], smack);
804                 if (rc != 5)
805                         return -EINVAL;
806                 m = BEBITS;
807         }
808         if (m > BEBITS)
809                 return -EINVAL;
810
811         sp = smk_import(smack, 0);
812         if (sp == NULL)
813                 return -EINVAL;
814
815         for (temp_mask = 0; m > 0; m--) {
816                 temp_mask |= mask_bits;
817                 mask_bits >>= 1;
818         }
819         mask.s_addr = cpu_to_be32(temp_mask);
820
821         newname.sin_addr.s_addr &= mask.s_addr;
822         /*
823          * Only allow one writer at a time. Writes should be
824          * quite rare and small in any case.
825          */
826         mutex_lock(&smk_netlbladdr_lock);
827
828         nsa = newname.sin_addr.s_addr;
829         /* try to find if the prefix is already in the list */
830         found = 0;
831         list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
832                 if (skp->smk_host.sin_addr.s_addr == nsa &&
833                     skp->smk_mask.s_addr == mask.s_addr) {
834                         found = 1;
835                         break;
836                 }
837         }
838         smk_netlabel_audit_set(&audit_info);
839
840         if (found == 0) {
841                 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
842                 if (skp == NULL)
843                         rc = -ENOMEM;
844                 else {
845                         rc = 0;
846                         skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
847                         skp->smk_mask.s_addr = mask.s_addr;
848                         skp->smk_label = sp;
849                         smk_netlbladdr_insert(skp);
850                 }
851         } else {
852                 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
853                         &skp->smk_host.sin_addr, &skp->smk_mask,
854                         PF_INET, &audit_info);
855                 skp->smk_label = sp;
856         }
857
858         /*
859          * Now tell netlabel about the single label nature of
860          * this host so that incoming packets get labeled.
861          */
862
863         if (rc == 0)
864                 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
865                         &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
866                         smack_to_secid(skp->smk_label), &audit_info);
867
868         if (rc == 0)
869                 rc = count;
870
871         mutex_unlock(&smk_netlbladdr_lock);
872
873         return rc;
874 }
875
876 static const struct file_operations smk_netlbladdr_ops = {
877         .open           = smk_open_netlbladdr,
878         .read           = seq_read,
879         .llseek         = seq_lseek,
880         .write          = smk_write_netlbladdr,
881         .release        = seq_release,
882 };
883
884 /**
885  * smk_read_doi - read() for /smack/doi
886  * @filp: file pointer, not actually used
887  * @buf: where to put the result
888  * @count: maximum to send along
889  * @ppos: where to start
890  *
891  * Returns number of bytes read or error code, as appropriate
892  */
893 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
894                             size_t count, loff_t *ppos)
895 {
896         char temp[80];
897         ssize_t rc;
898
899         if (*ppos != 0)
900                 return 0;
901
902         sprintf(temp, "%d", smk_cipso_doi_value);
903         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
904
905         return rc;
906 }
907
908 /**
909  * smk_write_doi - write() for /smack/doi
910  * @file: file pointer, not actually used
911  * @buf: where to get the data from
912  * @count: bytes sent
913  * @ppos: where to start
914  *
915  * Returns number of bytes written or error code, as appropriate
916  */
917 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
918                              size_t count, loff_t *ppos)
919 {
920         char temp[80];
921         int i;
922
923         if (!capable(CAP_MAC_ADMIN))
924                 return -EPERM;
925
926         if (count >= sizeof(temp) || count == 0)
927                 return -EINVAL;
928
929         if (copy_from_user(temp, buf, count) != 0)
930                 return -EFAULT;
931
932         temp[count] = '\0';
933
934         if (sscanf(temp, "%d", &i) != 1)
935                 return -EINVAL;
936
937         smk_cipso_doi_value = i;
938
939         smk_cipso_doi();
940
941         return count;
942 }
943
944 static const struct file_operations smk_doi_ops = {
945         .read           = smk_read_doi,
946         .write          = smk_write_doi,
947 };
948
949 /**
950  * smk_read_direct - read() for /smack/direct
951  * @filp: file pointer, not actually used
952  * @buf: where to put the result
953  * @count: maximum to send along
954  * @ppos: where to start
955  *
956  * Returns number of bytes read or error code, as appropriate
957  */
958 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
959                                size_t count, loff_t *ppos)
960 {
961         char temp[80];
962         ssize_t rc;
963
964         if (*ppos != 0)
965                 return 0;
966
967         sprintf(temp, "%d", smack_cipso_direct);
968         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
969
970         return rc;
971 }
972
973 /**
974  * smk_write_direct - write() for /smack/direct
975  * @file: file pointer, not actually used
976  * @buf: where to get the data from
977  * @count: bytes sent
978  * @ppos: where to start
979  *
980  * Returns number of bytes written or error code, as appropriate
981  */
982 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
983                                 size_t count, loff_t *ppos)
984 {
985         char temp[80];
986         int i;
987
988         if (!capable(CAP_MAC_ADMIN))
989                 return -EPERM;
990
991         if (count >= sizeof(temp) || count == 0)
992                 return -EINVAL;
993
994         if (copy_from_user(temp, buf, count) != 0)
995                 return -EFAULT;
996
997         temp[count] = '\0';
998
999         if (sscanf(temp, "%d", &i) != 1)
1000                 return -EINVAL;
1001
1002         smack_cipso_direct = i;
1003
1004         return count;
1005 }
1006
1007 static const struct file_operations smk_direct_ops = {
1008         .read           = smk_read_direct,
1009         .write          = smk_write_direct,
1010 };
1011
1012 /**
1013  * smk_read_ambient - read() for /smack/ambient
1014  * @filp: file pointer, not actually used
1015  * @buf: where to put the result
1016  * @cn: maximum to send along
1017  * @ppos: where to start
1018  *
1019  * Returns number of bytes read or error code, as appropriate
1020  */
1021 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1022                                 size_t cn, loff_t *ppos)
1023 {
1024         ssize_t rc;
1025         int asize;
1026
1027         if (*ppos != 0)
1028                 return 0;
1029         /*
1030          * Being careful to avoid a problem in the case where
1031          * smack_net_ambient gets changed in midstream.
1032          */
1033         mutex_lock(&smack_ambient_lock);
1034
1035         asize = strlen(smack_net_ambient) + 1;
1036
1037         if (cn >= asize)
1038                 rc = simple_read_from_buffer(buf, cn, ppos,
1039                                              smack_net_ambient, asize);
1040         else
1041                 rc = -EINVAL;
1042
1043         mutex_unlock(&smack_ambient_lock);
1044
1045         return rc;
1046 }
1047
1048 /**
1049  * smk_write_ambient - write() for /smack/ambient
1050  * @file: file pointer, not actually used
1051  * @buf: where to get the data from
1052  * @count: bytes sent
1053  * @ppos: where to start
1054  *
1055  * Returns number of bytes written or error code, as appropriate
1056  */
1057 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1058                                  size_t count, loff_t *ppos)
1059 {
1060         char in[SMK_LABELLEN];
1061         char *oldambient;
1062         char *smack;
1063
1064         if (!capable(CAP_MAC_ADMIN))
1065                 return -EPERM;
1066
1067         if (count >= SMK_LABELLEN)
1068                 return -EINVAL;
1069
1070         if (copy_from_user(in, buf, count) != 0)
1071                 return -EFAULT;
1072
1073         smack = smk_import(in, count);
1074         if (smack == NULL)
1075                 return -EINVAL;
1076
1077         mutex_lock(&smack_ambient_lock);
1078
1079         oldambient = smack_net_ambient;
1080         smack_net_ambient = smack;
1081         smk_unlbl_ambient(oldambient);
1082
1083         mutex_unlock(&smack_ambient_lock);
1084
1085         return count;
1086 }
1087
1088 static const struct file_operations smk_ambient_ops = {
1089         .read           = smk_read_ambient,
1090         .write          = smk_write_ambient,
1091 };
1092
1093 /**
1094  * smk_read_onlycap - read() for /smack/onlycap
1095  * @filp: file pointer, not actually used
1096  * @buf: where to put the result
1097  * @cn: maximum to send along
1098  * @ppos: where to start
1099  *
1100  * Returns number of bytes read or error code, as appropriate
1101  */
1102 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1103                                 size_t cn, loff_t *ppos)
1104 {
1105         char *smack = "";
1106         ssize_t rc = -EINVAL;
1107         int asize;
1108
1109         if (*ppos != 0)
1110                 return 0;
1111
1112         if (smack_onlycap != NULL)
1113                 smack = smack_onlycap;
1114
1115         asize = strlen(smack) + 1;
1116
1117         if (cn >= asize)
1118                 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1119
1120         return rc;
1121 }
1122
1123 /**
1124  * smk_write_onlycap - write() for /smack/onlycap
1125  * @file: file pointer, not actually used
1126  * @buf: where to get the data from
1127  * @count: bytes sent
1128  * @ppos: where to start
1129  *
1130  * Returns number of bytes written or error code, as appropriate
1131  */
1132 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1133                                  size_t count, loff_t *ppos)
1134 {
1135         char in[SMK_LABELLEN];
1136         char *sp = current->cred->security;
1137
1138         if (!capable(CAP_MAC_ADMIN))
1139                 return -EPERM;
1140
1141         /*
1142          * This can be done using smk_access() but is done
1143          * explicitly for clarity. The smk_access() implementation
1144          * would use smk_access(smack_onlycap, MAY_WRITE)
1145          */
1146         if (smack_onlycap != NULL && smack_onlycap != sp)
1147                 return -EPERM;
1148
1149         if (count >= SMK_LABELLEN)
1150                 return -EINVAL;
1151
1152         if (copy_from_user(in, buf, count) != 0)
1153                 return -EFAULT;
1154
1155         /*
1156          * Should the null string be passed in unset the onlycap value.
1157          * This seems like something to be careful with as usually
1158          * smk_import only expects to return NULL for errors. It
1159          * is usually the case that a nullstring or "\n" would be
1160          * bad to pass to smk_import but in fact this is useful here.
1161          */
1162         smack_onlycap = smk_import(in, count);
1163
1164         return count;
1165 }
1166
1167 static const struct file_operations smk_onlycap_ops = {
1168         .read           = smk_read_onlycap,
1169         .write          = smk_write_onlycap,
1170 };
1171
1172 /**
1173  * smk_fill_super - fill the /smackfs superblock
1174  * @sb: the empty superblock
1175  * @data: unused
1176  * @silent: unused
1177  *
1178  * Fill in the well known entries for /smack
1179  *
1180  * Returns 0 on success, an error code on failure
1181  */
1182 static int smk_fill_super(struct super_block *sb, void *data, int silent)
1183 {
1184         int rc;
1185         struct inode *root_inode;
1186
1187         static struct tree_descr smack_files[] = {
1188                 [SMK_LOAD]      =
1189                         {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
1190                 [SMK_CIPSO]     =
1191                         {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1192                 [SMK_DOI]       =
1193                         {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1194                 [SMK_DIRECT]    =
1195                         {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1196                 [SMK_AMBIENT]   =
1197                         {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
1198                 [SMK_NETLBLADDR] =
1199                         {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
1200                 [SMK_ONLYCAP]   =
1201                         {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
1202                 /* last one */ {""}
1203         };
1204
1205         rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1206         if (rc != 0) {
1207                 printk(KERN_ERR "%s failed %d while creating inodes\n",
1208                         __func__, rc);
1209                 return rc;
1210         }
1211
1212         root_inode = sb->s_root->d_inode;
1213         root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1214
1215         return 0;
1216 }
1217
1218 /**
1219  * smk_get_sb - get the smackfs superblock
1220  * @fs_type: passed along without comment
1221  * @flags: passed along without comment
1222  * @dev_name: passed along without comment
1223  * @data: passed along without comment
1224  * @mnt: passed along without comment
1225  *
1226  * Just passes everything along.
1227  *
1228  * Returns what the lower level code does.
1229  */
1230 static int smk_get_sb(struct file_system_type *fs_type,
1231                       int flags, const char *dev_name, void *data,
1232                       struct vfsmount *mnt)
1233 {
1234         return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
1235 }
1236
1237 static struct file_system_type smk_fs_type = {
1238         .name           = "smackfs",
1239         .get_sb         = smk_get_sb,
1240         .kill_sb        = kill_litter_super,
1241 };
1242
1243 static struct vfsmount *smackfs_mount;
1244
1245 /**
1246  * init_smk_fs - get the smackfs superblock
1247  *
1248  * register the smackfs
1249  *
1250  * Do not register smackfs if Smack wasn't enabled
1251  * on boot. We can not put this method normally under the
1252  * smack_init() code path since the security subsystem get
1253  * initialized before the vfs caches.
1254  *
1255  * Returns true if we were not chosen on boot or if
1256  * we were chosen and filesystem registration succeeded.
1257  */
1258 static int __init init_smk_fs(void)
1259 {
1260         int err;
1261
1262         if (!security_module_enable(&smack_ops))
1263                 return 0;
1264
1265         err = register_filesystem(&smk_fs_type);
1266         if (!err) {
1267                 smackfs_mount = kern_mount(&smk_fs_type);
1268                 if (IS_ERR(smackfs_mount)) {
1269                         printk(KERN_ERR "smackfs:  could not mount!\n");
1270                         err = PTR_ERR(smackfs_mount);
1271                         smackfs_mount = NULL;
1272                 }
1273         }
1274
1275         smk_cipso_doi();
1276         smk_unlbl_ambient(NULL);
1277
1278         return err;
1279 }
1280
1281 __initcall(init_smk_fs);