ba60872cbbc84c00acb7b3aa32e46ef0ba193593
[kernel/linux-3.0.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 <linux/slab.h>
24 #include <net/net_namespace.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         SMK_LOGGING     = 10,   /* logging */
45         SMK_LOAD_SELF   = 11,   /* task specific rules */
46         SMK_ACCESSES    = 12,   /* access policy */
47         SMK_MAPPED      = 13,   /* CIPSO level indicating mapped label */
48         SMK_LOAD2       = 14,   /* load policy with long labels */
49         SMK_LOAD_SELF2  = 15,   /* load task specific rules with long labels */
50         SMK_ACCESS2     = 16,   /* make an access check with long labels */
51         SMK_CIPSO2      = 17,   /* load long label -> CIPSO mapping */
52         SMK_REVOKE_SUBJ = 18,   /* set rules with subject label to '-' */
53         SMK_CHANGE_RULE = 19,   /* change or add rules (long labels) */
54 };
55
56 /*
57  * List locks
58  */
59 static DEFINE_MUTEX(smack_cipso_lock);
60 static DEFINE_MUTEX(smack_ambient_lock);
61 static DEFINE_MUTEX(smk_netlbladdr_lock);
62
63 /*
64  * This is the "ambient" label for network traffic.
65  * If it isn't somehow marked, use this.
66  * It can be reset via smackfs/ambient
67  */
68 char *smack_net_ambient;
69
70 /*
71  * This is the level in a CIPSO header that indicates a
72  * smack label is contained directly in the category set.
73  * It can be reset via smackfs/direct
74  */
75 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
76
77 /*
78  * This is the level in a CIPSO header that indicates a
79  * secid is contained directly in the category set.
80  * It can be reset via smackfs/mapped
81  */
82 int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
83
84 /*
85  * Unless a process is running with this label even
86  * having CAP_MAC_OVERRIDE isn't enough to grant
87  * privilege to violate MAC policy. If no label is
88  * designated (the NULL case) capabilities apply to
89  * everyone. It is expected that the hat (^) label
90  * will be used if any label is used.
91  */
92 char *smack_onlycap;
93
94 /*
95  * Certain IP addresses may be designated as single label hosts.
96  * Packets are sent there unlabeled, but only from tasks that
97  * can write to the specified label.
98  */
99
100 LIST_HEAD(smk_netlbladdr_list);
101
102 /*
103  * Rule lists are maintained for each label.
104  * This master list is just for reading /smack/load and /smack/load2.
105  */
106 struct smack_master_list {
107         struct list_head        list;
108         struct smack_rule       *smk_rule;
109 };
110
111 LIST_HEAD(smack_rule_list);
112
113 struct smack_parsed_rule {
114         char                    *smk_subject;
115         char                    *smk_object;
116         int                     smk_access1;
117         int                     smk_access2;
118 };
119
120 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
121
122 const char *smack_cipso_option = SMACK_CIPSO_OPTION;
123
124 /*
125  * Values for parsing cipso rules
126  * SMK_DIGITLEN: Length of a digit field in a rule.
127  * SMK_CIPSOMIN: Minimum possible cipso rule length.
128  * SMK_CIPSOMAX: Maximum possible cipso rule length.
129  */
130 #define SMK_DIGITLEN 4
131 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
132 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
133
134 /*
135  * Values for parsing MAC rules
136  * SMK_ACCESS: Maximum possible combination of access permissions
137  * SMK_ACCESSLEN: Maximum length for a rule access field
138  * SMK_LOADLEN: Smack rule length
139  */
140 #define SMK_OACCESS     "rwxa"
141 #define SMK_ACCESS      "rwxat"
142 #define SMK_OACCESSLEN  (sizeof(SMK_OACCESS) - 1)
143 #define SMK_ACCESSLEN   (sizeof(SMK_ACCESS) - 1)
144 #define SMK_OLOADLEN    (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
145 #define SMK_LOADLEN     (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
146
147 /*
148  * Stricly for CIPSO level manipulation.
149  * Set the category bit number in a smack label sized buffer.
150  */
151 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
152 {
153         if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
154                 return;
155
156         catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
157 }
158
159 /**
160  * smk_netlabel_audit_set - fill a netlbl_audit struct
161  * @nap: structure to fill
162  */
163 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
164 {
165         nap->loginuid = audit_get_loginuid(current);
166         nap->sessionid = audit_get_sessionid(current);
167         nap->secid = smack_to_secid(smk_of_current());
168 }
169
170 /*
171  * Value for parsing single label host rules
172  * "1.2.3.4 X"
173  */
174 #define SMK_NETLBLADDRMIN       9
175
176 /**
177  * smk_set_access - add a rule to the rule list or replace an old rule
178  * @srp: the rule to add or replace
179  * @rule_list: the list of rules
180  * @rule_lock: the rule list lock
181  * @global: if non-zero, indicates a global rule
182  *
183  * Looks through the current subject/object/access list for
184  * the subject/object pair and replaces the access that was
185  * there. If the pair isn't found add it with the specified
186  * access.
187  *
188  * Returns 0 if nothing goes wrong or -ENOMEM if it fails
189  * during the allocation of the new pair to add.
190  */
191 static int smk_set_access(struct smack_parsed_rule *srp,
192                                 struct list_head *rule_list,
193                                 struct mutex *rule_lock, int global)
194 {
195         struct smack_rule *sp;
196         struct smack_master_list *smlp;
197         int found = 0;
198         int rc = 0;
199
200         mutex_lock(rule_lock);
201
202         /*
203          * Because the object label is less likely to match
204          * than the subject label check it first
205          */
206         list_for_each_entry_rcu(sp, rule_list, list) {
207                 if (sp->smk_object == srp->smk_object &&
208                     sp->smk_subject == srp->smk_subject) {
209                         found = 1;
210                         sp->smk_access |= srp->smk_access1;
211                         sp->smk_access &= ~srp->smk_access2;
212                         break;
213                 }
214         }
215
216         if (found == 0) {
217                 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
218                 if (sp == NULL) {
219                         rc = -ENOMEM;
220                         goto out;
221                 }
222
223                 sp->smk_subject = srp->smk_subject;
224                 sp->smk_object = srp->smk_object;
225                 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
226
227                 list_add_rcu(&sp->list, rule_list);
228                 /*
229                  * If this is a global as opposed to self and a new rule
230                  * it needs to get added for reporting.
231                  */
232                 if (global) {
233                         smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
234                         if (smlp != NULL) {
235                                 smlp->smk_rule = sp;
236                                 list_add_rcu(&smlp->list, &smack_rule_list);
237                         } else
238                                 rc = -ENOMEM;
239                 }
240         }
241
242 out:
243         mutex_unlock(rule_lock);
244         return rc;
245 }
246
247 /**
248  * smk_perm_from_str - parse smack accesses from a text string
249  * @string: a text string that contains a Smack accesses code
250  *
251  * Returns an integer with respective bits set for specified accesses.
252  */
253 static int smk_perm_from_str(const char *string)
254 {
255         int perm = 0;
256         const char *cp;
257
258         for (cp = string; ; cp++)
259                 switch (*cp) {
260                 case '-':
261                         break;
262                 case 'r':
263                 case 'R':
264                         perm |= MAY_READ;
265                         break;
266                 case 'w':
267                 case 'W':
268                         perm |= MAY_WRITE;
269                         break;
270                 case 'x':
271                 case 'X':
272                         perm |= MAY_EXEC;
273                         break;
274                 case 'a':
275                 case 'A':
276                         perm |= MAY_APPEND;
277                         break;
278                 case 't':
279                 case 'T':
280                         perm |= MAY_TRANSMUTE;
281                         break;
282                 default:
283                         return perm;
284                 }
285 }
286
287 /**
288  * smk_fill_rule - Fill Smack rule from strings
289  * @subject: subject label string
290  * @object: object label string
291  * @access1: access string
292  * @access2: string with permissions to be removed
293  * @rule: Smack rule
294  * @import: if non-zero, import labels
295  * @len: label length limit
296  *
297  * Returns 0 on success, -1 on failure
298  */
299 static int smk_fill_rule(const char *subject, const char *object,
300                                 const char *access1, const char *access2,
301                                 struct smack_parsed_rule *rule, int import,
302                                 int len)
303 {
304         const char *cp;
305         struct smack_known *skp;
306
307         if (import) {
308                 rule->smk_subject = smk_import(subject, len);
309                 if (rule->smk_subject == NULL)
310                         return -1;
311
312                 rule->smk_object = smk_import(object, len);
313                 if (rule->smk_object == NULL)
314                         return -1;
315         } else {
316                 cp = smk_parse_smack(subject, len);
317                 if (cp == NULL)
318                         return -1;
319                 skp = smk_find_entry(cp);
320                 kfree(cp);
321                 if (skp == NULL)
322                         return -1;
323                 rule->smk_subject = skp->smk_known;
324
325                 cp = smk_parse_smack(object, len);
326                 if (cp == NULL)
327                         return -1;
328                 skp = smk_find_entry(cp);
329                 kfree(cp);
330                 if (skp == NULL)
331                         return -1;
332                 rule->smk_object = skp->smk_known;
333         }
334
335         rule->smk_access1 = smk_perm_from_str(access1);
336         if (access2)
337                 rule->smk_access2 = smk_perm_from_str(access2);
338         else
339                 rule->smk_access2 = ~rule->smk_access1;
340
341         return 0;
342 }
343
344 /**
345  * smk_parse_rule - parse Smack rule from load string
346  * @data: string to be parsed whose size is SMK_LOADLEN
347  * @rule: Smack rule
348  * @import: if non-zero, import labels
349  *
350  * Returns 0 on success, -1 on errors.
351  */
352 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
353                                 int import)
354 {
355         int rc;
356
357         rc = smk_fill_rule(data, data + SMK_LABELLEN,
358                            data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
359                            import, SMK_LABELLEN);
360         return rc;
361 }
362
363 /**
364  * smk_parse_long_rule - parse Smack rule from rule string
365  * @data: string to be parsed, null terminated
366  * @rule: Will be filled with Smack parsed rule
367  * @import: if non-zero, import labels
368  * @change: if non-zero, data is from /smack/change-rule
369  *
370  * Returns 0 on success, -1 on failure
371  */
372 static int smk_parse_long_rule(const char *data, struct smack_parsed_rule *rule,
373                                 int import, int change)
374 {
375         char *subject;
376         char *object;
377         char *access1;
378         char *access2;
379         int datalen;
380         int rc = -1;
381
382         /* This is inefficient */
383         datalen = strlen(data);
384
385         /* Our first element can be 64 + \0 with no spaces */
386         subject = kzalloc(datalen + 1, GFP_KERNEL);
387         if (subject == NULL)
388                 return -1;
389         object = kzalloc(datalen, GFP_KERNEL);
390         if (object == NULL)
391                 goto free_out_s;
392         access1 = kzalloc(datalen, GFP_KERNEL);
393         if (access1 == NULL)
394                 goto free_out_o;
395         access2 = kzalloc(datalen, GFP_KERNEL);
396         if (access2 == NULL)
397                 goto free_out_a;
398
399         if (change) {
400                 if (sscanf(data, "%s %s %s %s",
401                         subject, object, access1, access2) == 4)
402                         rc = smk_fill_rule(subject, object, access1, access2,
403                                 rule, import, 0);
404         } else {
405                 if (sscanf(data, "%s %s %s", subject, object, access1) == 3)
406                         rc = smk_fill_rule(subject, object, access1, NULL,
407                                 rule, import, 0);
408         }
409
410         kfree(access2);
411 free_out_a:
412         kfree(access1);
413 free_out_o:
414         kfree(object);
415 free_out_s:
416         kfree(subject);
417         return rc;
418 }
419
420 #define SMK_FIXED24_FMT 0       /* Fixed 24byte label format */
421 #define SMK_LONG_FMT    1       /* Variable long label format */
422 #define SMK_CHANGE_FMT  2       /* Rule modification format */
423 /**
424  * smk_write_rules_list - write() for any /smack rule file
425  * @file: file pointer, not actually used
426  * @buf: where to get the data from
427  * @count: bytes sent
428  * @ppos: where to start - must be 0
429  * @rule_list: the list of rules to write to
430  * @rule_lock: lock for the rule list
431  * @format: /smack/load or /smack/load2 or /smack/change-rule format.
432  *
433  * Get one smack access rule from above.
434  * The format for SMK_LONG_FMT is:
435  *      "subject<whitespace>object<whitespace>access[<whitespace>...]"
436  * The format for SMK_FIXED24_FMT is exactly:
437  *      "subject                 object                  rwxat"
438  * The format for SMK_CHANGE_FMT is:
439  *      "subject<whitespace>object<whitespace>
440  *       acc_enable<whitespace>acc_disable[<whitespace>...]"
441  */
442 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
443                                         size_t count, loff_t *ppos,
444                                         struct list_head *rule_list,
445                                         struct mutex *rule_lock, int format)
446 {
447         struct smack_known *skp;
448         struct smack_parsed_rule rule;
449         char *data;
450         int datalen;
451         int rc = -EINVAL;
452         int load = 0;
453
454         /*
455          * No partial writes.
456          * Enough data must be present.
457          */
458         if (*ppos != 0)
459                 return -EINVAL;
460
461         if (format == SMK_FIXED24_FMT) {
462                 /*
463                  * Minor hack for backward compatibility
464                  */
465                 if (count != SMK_OLOADLEN && count != SMK_LOADLEN)
466                         return -EINVAL;
467                 datalen = SMK_LOADLEN;
468         } else
469                 datalen = count + 1;
470
471         data = kzalloc(datalen, GFP_KERNEL);
472         if (data == NULL)
473                 return -ENOMEM;
474
475         if (copy_from_user(data, buf, count) != 0) {
476                 rc = -EFAULT;
477                 goto out;
478         }
479
480         if (format == SMK_LONG_FMT) {
481                 /*
482                  * Be sure the data string is terminated.
483                  */
484                 data[count] = '\0';
485                 if (smk_parse_long_rule(data, &rule, 1, 0))
486                         goto out;
487         } else if (format == SMK_CHANGE_FMT) {
488                 data[count] = '\0';
489                 if (smk_parse_long_rule(data, &rule, 1, 1))
490                         goto out;
491         } else {
492                 /*
493                  * More on the minor hack for backward compatibility
494                  */
495                 if (count == (SMK_OLOADLEN))
496                         data[SMK_OLOADLEN] = '-';
497                 if (smk_parse_rule(data, &rule, 1))
498                         goto out;
499         }
500
501
502         if (rule_list == NULL) {
503                 load = 1;
504                 skp = smk_find_entry(rule.smk_subject);
505                 rule_list = &skp->smk_rules;
506                 rule_lock = &skp->smk_rules_lock;
507         }
508
509         rc = smk_set_access(&rule, rule_list, rule_lock, load);
510         if (rc == 0)
511                 rc = count;
512 out:
513         kfree(data);
514         return rc;
515 }
516
517 /*
518  * Core logic for smackfs seq list operations.
519  */
520
521 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
522                                 struct list_head *head)
523 {
524         struct list_head *list;
525
526         /*
527          * This is 0 the first time through.
528          */
529         if (s->index == 0)
530                 s->private = head;
531
532         if (s->private == NULL)
533                 return NULL;
534
535         list = s->private;
536         if (list_empty(list))
537                 return NULL;
538
539         if (s->index == 0)
540                 return list->next;
541         return list;
542 }
543
544 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
545                                 struct list_head *head)
546 {
547         struct list_head *list = v;
548
549         if (list_is_last(list, head)) {
550                 s->private = NULL;
551                 return NULL;
552         }
553         s->private = list->next;
554         return list->next;
555 }
556
557 static void smk_seq_stop(struct seq_file *s, void *v)
558 {
559         /* No-op */
560 }
561
562 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
563 {
564         /*
565          * Don't show any rules with label names too long for
566          * interface file (/smack/load or /smack/load2)
567          * because you should expect to be able to write
568          * anything you read back.
569          */
570         if (strlen(srp->smk_subject) >= max || strlen(srp->smk_object) >= max)
571                 return;
572
573         if (srp->smk_access == 0)
574                 return;
575
576         seq_printf(s, "%s %s", srp->smk_subject, srp->smk_object);
577
578         seq_putc(s, ' ');
579
580         if (srp->smk_access & MAY_READ)
581                 seq_putc(s, 'r');
582         if (srp->smk_access & MAY_WRITE)
583                 seq_putc(s, 'w');
584         if (srp->smk_access & MAY_EXEC)
585                 seq_putc(s, 'x');
586         if (srp->smk_access & MAY_APPEND)
587                 seq_putc(s, 'a');
588         if (srp->smk_access & MAY_TRANSMUTE)
589                 seq_putc(s, 't');
590
591         seq_putc(s, '\n');
592 }
593
594 /*
595  * Seq_file read operations for /smack/load
596  */
597
598 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
599 {
600         return smk_seq_start(s, pos, &smack_rule_list);
601 }
602
603 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
604 {
605         return smk_seq_next(s, v, pos, &smack_rule_list);
606 }
607
608 static int load_seq_show(struct seq_file *s, void *v)
609 {
610         struct list_head *list = v;
611         struct smack_master_list *smlp =
612                  list_entry(list, struct smack_master_list, list);
613
614         smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
615
616         return 0;
617 }
618
619 static const struct seq_operations load_seq_ops = {
620         .start = load2_seq_start,
621         .next  = load2_seq_next,
622         .show  = load_seq_show,
623         .stop  = smk_seq_stop,
624 };
625
626 /**
627  * smk_open_load - open() for /smack/load
628  * @inode: inode structure representing file
629  * @file: "load" file pointer
630  *
631  * For reading, use load_seq_* seq_file reading operations.
632  */
633 static int smk_open_load(struct inode *inode, struct file *file)
634 {
635         return seq_open(file, &load_seq_ops);
636 }
637
638 /**
639  * smk_write_load - write() for /smack/load
640  * @file: file pointer, not actually used
641  * @buf: where to get the data from
642  * @count: bytes sent
643  * @ppos: where to start - must be 0
644  *
645  */
646 static ssize_t smk_write_load(struct file *file, const char __user *buf,
647                               size_t count, loff_t *ppos)
648 {
649         /*
650          * Must have privilege.
651          * No partial writes.
652          * Enough data must be present.
653          */
654         if (!smack_privileged(CAP_MAC_ADMIN))
655                 return -EPERM;
656
657         return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
658                                     SMK_FIXED24_FMT);
659 }
660
661 static const struct file_operations smk_load_ops = {
662         .open           = smk_open_load,
663         .read           = seq_read,
664         .llseek         = seq_lseek,
665         .write          = smk_write_load,
666         .release        = seq_release,
667 };
668
669 /**
670  * smk_cipso_doi - initialize the CIPSO domain
671  */
672 static void smk_cipso_doi(void)
673 {
674         int rc;
675         struct cipso_v4_doi *doip;
676         struct netlbl_audit nai;
677
678         smk_netlabel_audit_set(&nai);
679
680         rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
681         if (rc != 0)
682                 printk(KERN_WARNING "%s:%d remove rc = %d\n",
683                        __func__, __LINE__, rc);
684
685         doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
686         if (doip == NULL)
687                 panic("smack:  Failed to initialize cipso DOI.\n");
688         doip->map.std = NULL;
689         doip->doi = smk_cipso_doi_value;
690         doip->type = CIPSO_V4_MAP_PASS;
691         doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
692         for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
693                 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
694
695         rc = netlbl_cfg_cipsov4_add(doip, &nai);
696         if (rc != 0) {
697                 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
698                        __func__, __LINE__, rc);
699                 kfree(doip);
700                 return;
701         }
702         rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
703         if (rc != 0) {
704                 printk(KERN_WARNING "%s:%d map add rc = %d\n",
705                        __func__, __LINE__, rc);
706                 kfree(doip);
707                 return;
708         }
709 }
710
711 /**
712  * smk_unlbl_ambient - initialize the unlabeled domain
713  * @oldambient: previous domain string
714  */
715 static void smk_unlbl_ambient(char *oldambient)
716 {
717         int rc;
718         struct netlbl_audit nai;
719
720         smk_netlabel_audit_set(&nai);
721
722         if (oldambient != NULL) {
723                 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
724                 if (rc != 0)
725                         printk(KERN_WARNING "%s:%d remove rc = %d\n",
726                                __func__, __LINE__, rc);
727         }
728         if (smack_net_ambient == NULL)
729                 smack_net_ambient = smack_known_floor.smk_known;
730
731         rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
732                                       NULL, NULL, &nai);
733         if (rc != 0)
734                 printk(KERN_WARNING "%s:%d add rc = %d\n",
735                        __func__, __LINE__, rc);
736 }
737
738 /*
739  * Seq_file read operations for /smack/cipso
740  */
741
742 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
743 {
744         return smk_seq_start(s, pos, &smack_known_list);
745 }
746
747 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
748 {
749         return smk_seq_next(s, v, pos, &smack_known_list);
750 }
751
752 /*
753  * Print cipso labels in format:
754  * label level[/cat[,cat]]
755  */
756 static int cipso_seq_show(struct seq_file *s, void *v)
757 {
758         struct list_head  *list = v;
759         struct smack_known *skp =
760                  list_entry(list, struct smack_known, list);
761         struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
762         char sep = '/';
763         int i;
764
765         /*
766          * Don't show a label that could not have been set using
767          * /smack/cipso. This is in support of the notion that
768          * anything read from /smack/cipso ought to be writeable
769          * to /smack/cipso.
770          *
771          * /smack/cipso2 should be used instead.
772          */
773         if (strlen(skp->smk_known) >= SMK_LABELLEN)
774                 return 0;
775
776         seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
777
778         for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
779              i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
780                 seq_printf(s, "%c%d", sep, i);
781                 sep = ',';
782         }
783
784         seq_putc(s, '\n');
785
786         return 0;
787 }
788
789 static const struct seq_operations cipso_seq_ops = {
790         .start = cipso_seq_start,
791         .next  = cipso_seq_next,
792         .show  = cipso_seq_show,
793         .stop  = smk_seq_stop,
794 };
795
796 /**
797  * smk_open_cipso - open() for /smack/cipso
798  * @inode: inode structure representing file
799  * @file: "cipso" file pointer
800  *
801  * Connect our cipso_seq_* operations with /smack/cipso
802  * file_operations
803  */
804 static int smk_open_cipso(struct inode *inode, struct file *file)
805 {
806         return seq_open(file, &cipso_seq_ops);
807 }
808
809 /**
810  * smk_set_cipso - do the work for write() for cipso and cipso2
811  * @file: file pointer, not actually used
812  * @buf: where to get the data from
813  * @count: bytes sent
814  * @ppos: where to start
815  * @format: /smack/cipso or /smack/cipso2
816  *
817  * Accepts only one cipso rule per write call.
818  * Returns number of bytes written or error code, as appropriate
819  */
820 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
821                                 size_t count, loff_t *ppos, int format)
822 {
823         struct smack_known *skp;
824         struct netlbl_lsm_secattr ncats;
825         char mapcatset[SMK_CIPSOLEN];
826         int maplevel;
827         unsigned int cat;
828         int catlen;
829         ssize_t rc = -EINVAL;
830         char *data = NULL;
831         char *rule;
832         int ret;
833         int i;
834
835         /*
836          * Must have privilege.
837          * No partial writes.
838          * Enough data must be present.
839          */
840         if (!smack_privileged(CAP_MAC_ADMIN))
841                 return -EPERM;
842         if (*ppos != 0)
843                 return -EINVAL;
844         if (format == SMK_FIXED24_FMT &&
845             (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
846                 return -EINVAL;
847
848         data = kzalloc(count + 1, GFP_KERNEL);
849         if (data == NULL)
850                 return -ENOMEM;
851
852         if (copy_from_user(data, buf, count) != 0) {
853                 rc = -EFAULT;
854                 goto unlockedout;
855         }
856
857         data[count] = '\0';
858         rule = data;
859         /*
860          * Only allow one writer at a time. Writes should be
861          * quite rare and small in any case.
862          */
863         mutex_lock(&smack_cipso_lock);
864
865         skp = smk_import_entry(rule, 0);
866         if (skp == NULL)
867                 goto out;
868
869         if (format == SMK_FIXED24_FMT)
870                 rule += SMK_LABELLEN;
871         else
872                 rule += strlen(skp->smk_known) + 1;
873
874         ret = sscanf(rule, "%d", &maplevel);
875         if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
876                 goto out;
877
878         rule += SMK_DIGITLEN;
879         ret = sscanf(rule, "%d", &catlen);
880         if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
881                 goto out;
882
883         if (format == SMK_FIXED24_FMT &&
884             count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
885                 goto out;
886
887         memset(mapcatset, 0, sizeof(mapcatset));
888
889         for (i = 0; i < catlen; i++) {
890                 rule += SMK_DIGITLEN;
891                 ret = sscanf(rule, "%u", &cat);
892                 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
893                         goto out;
894
895                 smack_catset_bit(cat, mapcatset);
896         }
897
898         rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
899         if (rc >= 0) {
900                 netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
901                 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
902                 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
903                 rc = count;
904         }
905
906 out:
907         mutex_unlock(&smack_cipso_lock);
908 unlockedout:
909         kfree(data);
910         return rc;
911 }
912
913 /**
914  * smk_write_cipso - write() for /smack/cipso
915  * @file: file pointer, not actually used
916  * @buf: where to get the data from
917  * @count: bytes sent
918  * @ppos: where to start
919  *
920  * Accepts only one cipso rule per write call.
921  * Returns number of bytes written or error code, as appropriate
922  */
923 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
924                                size_t count, loff_t *ppos)
925 {
926         return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
927 }
928
929 static const struct file_operations smk_cipso_ops = {
930         .open           = smk_open_cipso,
931         .read           = seq_read,
932         .llseek         = seq_lseek,
933         .write          = smk_write_cipso,
934         .release        = seq_release,
935 };
936
937 /*
938  * Seq_file read operations for /smack/cipso2
939  */
940
941 /*
942  * Print cipso labels in format:
943  * label level[/cat[,cat]]
944  */
945 static int cipso2_seq_show(struct seq_file *s, void *v)
946 {
947         struct list_head  *list = v;
948         struct smack_known *skp =
949                  list_entry(list, struct smack_known, list);
950         struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
951         char sep = '/';
952         int i;
953
954         seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
955
956         for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
957              i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
958                 seq_printf(s, "%c%d", sep, i);
959                 sep = ',';
960         }
961
962         seq_putc(s, '\n');
963
964         return 0;
965 }
966
967 static const struct seq_operations cipso2_seq_ops = {
968         .start = cipso_seq_start,
969         .next  = cipso_seq_next,
970         .show  = cipso2_seq_show,
971         .stop  = smk_seq_stop,
972 };
973
974 /**
975  * smk_open_cipso2 - open() for /smack/cipso2
976  * @inode: inode structure representing file
977  * @file: "cipso2" file pointer
978  *
979  * Connect our cipso_seq_* operations with /smack/cipso2
980  * file_operations
981  */
982 static int smk_open_cipso2(struct inode *inode, struct file *file)
983 {
984         return seq_open(file, &cipso2_seq_ops);
985 }
986
987 /**
988  * smk_write_cipso2 - write() for /smack/cipso2
989  * @file: file pointer, not actually used
990  * @buf: where to get the data from
991  * @count: bytes sent
992  * @ppos: where to start
993  *
994  * Accepts only one cipso rule per write call.
995  * Returns number of bytes written or error code, as appropriate
996  */
997 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
998                               size_t count, loff_t *ppos)
999 {
1000         return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1001 }
1002
1003 static const struct file_operations smk_cipso2_ops = {
1004         .open           = smk_open_cipso2,
1005         .read           = seq_read,
1006         .llseek         = seq_lseek,
1007         .write          = smk_write_cipso2,
1008         .release        = seq_release,
1009 };
1010
1011 /*
1012  * Seq_file read operations for /smack/netlabel
1013  */
1014
1015 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1016 {
1017         return smk_seq_start(s, pos, &smk_netlbladdr_list);
1018 }
1019
1020 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1021 {
1022         return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
1023 }
1024 #define BEBITS  (sizeof(__be32) * 8)
1025
1026 /*
1027  * Print host/label pairs
1028  */
1029 static int netlbladdr_seq_show(struct seq_file *s, void *v)
1030 {
1031         struct list_head *list = v;
1032         struct smk_netlbladdr *skp =
1033                          list_entry(list, struct smk_netlbladdr, list);
1034         unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
1035         int maskn;
1036         u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
1037
1038         for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
1039
1040         seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1041                 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
1042
1043         return 0;
1044 }
1045
1046 static const struct seq_operations netlbladdr_seq_ops = {
1047         .start = netlbladdr_seq_start,
1048         .next  = netlbladdr_seq_next,
1049         .show  = netlbladdr_seq_show,
1050         .stop  = smk_seq_stop,
1051 };
1052
1053 /**
1054  * smk_open_netlbladdr - open() for /smack/netlabel
1055  * @inode: inode structure representing file
1056  * @file: "netlabel" file pointer
1057  *
1058  * Connect our netlbladdr_seq_* operations with /smack/netlabel
1059  * file_operations
1060  */
1061 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1062 {
1063         return seq_open(file, &netlbladdr_seq_ops);
1064 }
1065
1066 /**
1067  * smk_netlbladdr_insert
1068  * @new : netlabel to insert
1069  *
1070  * This helper insert netlabel in the smack_netlbladdrs list
1071  * sorted by netmask length (longest to smallest)
1072  * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1073  *
1074  */
1075 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1076 {
1077         struct smk_netlbladdr *m, *m_next;
1078
1079         if (list_empty(&smk_netlbladdr_list)) {
1080                 list_add_rcu(&new->list, &smk_netlbladdr_list);
1081                 return;
1082         }
1083
1084         m = list_entry_rcu(smk_netlbladdr_list.next,
1085                            struct smk_netlbladdr, list);
1086
1087         /* the comparison '>' is a bit hacky, but works */
1088         if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1089                 list_add_rcu(&new->list, &smk_netlbladdr_list);
1090                 return;
1091         }
1092
1093         list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1094                 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1095                         list_add_rcu(&new->list, &m->list);
1096                         return;
1097                 }
1098                 m_next = list_entry_rcu(m->list.next,
1099                                         struct smk_netlbladdr, list);
1100                 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1101                         list_add_rcu(&new->list, &m->list);
1102                         return;
1103                 }
1104         }
1105 }
1106
1107
1108 /**
1109  * smk_write_netlbladdr - write() for /smack/netlabel
1110  * @file: file pointer, not actually used
1111  * @buf: where to get the data from
1112  * @count: bytes sent
1113  * @ppos: where to start
1114  *
1115  * Accepts only one netlbladdr per write call.
1116  * Returns number of bytes written or error code, as appropriate
1117  */
1118 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1119                                 size_t count, loff_t *ppos)
1120 {
1121         struct smk_netlbladdr *skp;
1122         struct sockaddr_in newname;
1123         char *smack;
1124         char *sp;
1125         char *data;
1126         char *host = (char *)&newname.sin_addr.s_addr;
1127         int rc;
1128         struct netlbl_audit audit_info;
1129         struct in_addr mask;
1130         unsigned int m;
1131         int found;
1132         u32 mask_bits = (1<<31);
1133         __be32 nsa;
1134         u32 temp_mask;
1135
1136         /*
1137          * Must have privilege.
1138          * No partial writes.
1139          * Enough data must be present.
1140          * "<addr/mask, as a.b.c.d/e><space><label>"
1141          * "<addr, as a.b.c.d><space><label>"
1142          */
1143         if (!smack_privileged(CAP_MAC_ADMIN))
1144                 return -EPERM;
1145         if (*ppos != 0)
1146                 return -EINVAL;
1147         if (count < SMK_NETLBLADDRMIN)
1148                 return -EINVAL;
1149
1150         data = kzalloc(count + 1, GFP_KERNEL);
1151         if (data == NULL)
1152                 return -ENOMEM;
1153
1154         if (copy_from_user(data, buf, count) != 0) {
1155                 rc = -EFAULT;
1156                 goto free_data_out;
1157         }
1158
1159         smack = kzalloc(count + 1, GFP_KERNEL);
1160         if (smack == NULL) {
1161                 rc = -ENOMEM;
1162                 goto free_data_out;
1163         }
1164
1165         data[count] = '\0';
1166
1167         rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
1168                 &host[0], &host[1], &host[2], &host[3], &m, smack);
1169         if (rc != 6) {
1170                 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1171                         &host[0], &host[1], &host[2], &host[3], smack);
1172                 if (rc != 5) {
1173                         rc = -EINVAL;
1174                         goto free_out;
1175                 }
1176                 m = BEBITS;
1177         }
1178         if (m > BEBITS) {
1179                 rc = -EINVAL;
1180                 goto free_out;
1181         }
1182
1183         /*
1184          * If smack begins with '-', it is an option, don't import it
1185          */
1186         if (smack[0] != '-') {
1187                 sp = smk_import(smack, 0);
1188                 if (sp == NULL) {
1189                         rc = -EINVAL;
1190                         goto free_out;
1191                 }
1192         } else {
1193                 /* check known options */
1194                 if (strcmp(smack, smack_cipso_option) == 0)
1195                         sp = (char *)smack_cipso_option;
1196                 else {
1197                         rc = -EINVAL;
1198                         goto free_out;
1199                 }
1200         }
1201
1202         for (temp_mask = 0; m > 0; m--) {
1203                 temp_mask |= mask_bits;
1204                 mask_bits >>= 1;
1205         }
1206         mask.s_addr = cpu_to_be32(temp_mask);
1207
1208         newname.sin_addr.s_addr &= mask.s_addr;
1209         /*
1210          * Only allow one writer at a time. Writes should be
1211          * quite rare and small in any case.
1212          */
1213         mutex_lock(&smk_netlbladdr_lock);
1214
1215         nsa = newname.sin_addr.s_addr;
1216         /* try to find if the prefix is already in the list */
1217         found = 0;
1218         list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
1219                 if (skp->smk_host.sin_addr.s_addr == nsa &&
1220                     skp->smk_mask.s_addr == mask.s_addr) {
1221                         found = 1;
1222                         break;
1223                 }
1224         }
1225         smk_netlabel_audit_set(&audit_info);
1226
1227         if (found == 0) {
1228                 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
1229                 if (skp == NULL)
1230                         rc = -ENOMEM;
1231                 else {
1232                         rc = 0;
1233                         skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1234                         skp->smk_mask.s_addr = mask.s_addr;
1235                         skp->smk_label = sp;
1236                         smk_netlbladdr_insert(skp);
1237                 }
1238         } else {
1239                 /* we delete the unlabeled entry, only if the previous label
1240                  * wasn't the special CIPSO option */
1241                 if (skp->smk_label != smack_cipso_option)
1242                         rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1243                                         &skp->smk_host.sin_addr, &skp->smk_mask,
1244                                         PF_INET, &audit_info);
1245                 else
1246                         rc = 0;
1247                 skp->smk_label = sp;
1248         }
1249
1250         /*
1251          * Now tell netlabel about the single label nature of
1252          * this host so that incoming packets get labeled.
1253          * but only if we didn't get the special CIPSO option
1254          */
1255         if (rc == 0 && sp != smack_cipso_option)
1256                 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1257                         &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1258                         smack_to_secid(skp->smk_label), &audit_info);
1259
1260         if (rc == 0)
1261                 rc = count;
1262
1263         mutex_unlock(&smk_netlbladdr_lock);
1264
1265 free_out:
1266         kfree(smack);
1267 free_data_out:
1268         kfree(data);
1269
1270         return rc;
1271 }
1272
1273 static const struct file_operations smk_netlbladdr_ops = {
1274         .open           = smk_open_netlbladdr,
1275         .read           = seq_read,
1276         .llseek         = seq_lseek,
1277         .write          = smk_write_netlbladdr,
1278         .release        = seq_release,
1279 };
1280
1281 /**
1282  * smk_read_doi - read() for /smack/doi
1283  * @filp: file pointer, not actually used
1284  * @buf: where to put the result
1285  * @count: maximum to send along
1286  * @ppos: where to start
1287  *
1288  * Returns number of bytes read or error code, as appropriate
1289  */
1290 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1291                             size_t count, loff_t *ppos)
1292 {
1293         char temp[80];
1294         ssize_t rc;
1295
1296         if (*ppos != 0)
1297                 return 0;
1298
1299         sprintf(temp, "%d", smk_cipso_doi_value);
1300         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1301
1302         return rc;
1303 }
1304
1305 /**
1306  * smk_write_doi - write() for /smack/doi
1307  * @file: file pointer, not actually used
1308  * @buf: where to get the data from
1309  * @count: bytes sent
1310  * @ppos: where to start
1311  *
1312  * Returns number of bytes written or error code, as appropriate
1313  */
1314 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1315                              size_t count, loff_t *ppos)
1316 {
1317         char temp[80];
1318         int i;
1319
1320         if (!smack_privileged(CAP_MAC_ADMIN))
1321                 return -EPERM;
1322
1323         if (count >= sizeof(temp) || count == 0)
1324                 return -EINVAL;
1325
1326         if (copy_from_user(temp, buf, count) != 0)
1327                 return -EFAULT;
1328
1329         temp[count] = '\0';
1330
1331         if (sscanf(temp, "%d", &i) != 1)
1332                 return -EINVAL;
1333
1334         smk_cipso_doi_value = i;
1335
1336         smk_cipso_doi();
1337
1338         return count;
1339 }
1340
1341 static const struct file_operations smk_doi_ops = {
1342         .read           = smk_read_doi,
1343         .write          = smk_write_doi,
1344         .llseek         = default_llseek,
1345 };
1346
1347 /**
1348  * smk_read_direct - read() for /smack/direct
1349  * @filp: file pointer, not actually used
1350  * @buf: where to put the result
1351  * @count: maximum to send along
1352  * @ppos: where to start
1353  *
1354  * Returns number of bytes read or error code, as appropriate
1355  */
1356 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1357                                size_t count, loff_t *ppos)
1358 {
1359         char temp[80];
1360         ssize_t rc;
1361
1362         if (*ppos != 0)
1363                 return 0;
1364
1365         sprintf(temp, "%d", smack_cipso_direct);
1366         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1367
1368         return rc;
1369 }
1370
1371 /**
1372  * smk_write_direct - write() for /smack/direct
1373  * @file: file pointer, not actually used
1374  * @buf: where to get the data from
1375  * @count: bytes sent
1376  * @ppos: where to start
1377  *
1378  * Returns number of bytes written or error code, as appropriate
1379  */
1380 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1381                                 size_t count, loff_t *ppos)
1382 {
1383         struct smack_known *skp;
1384         char temp[80];
1385         int i;
1386
1387         if (!smack_privileged(CAP_MAC_ADMIN))
1388                 return -EPERM;
1389
1390         if (count >= sizeof(temp) || count == 0)
1391                 return -EINVAL;
1392
1393         if (copy_from_user(temp, buf, count) != 0)
1394                 return -EFAULT;
1395
1396         temp[count] = '\0';
1397
1398         if (sscanf(temp, "%d", &i) != 1)
1399                 return -EINVAL;
1400
1401         /*
1402          * Don't do anything if the value hasn't actually changed.
1403          * If it is changing reset the level on entries that were
1404          * set up to be direct when they were created.
1405          */
1406         if (smack_cipso_direct != i) {
1407                 mutex_lock(&smack_known_lock);
1408                 list_for_each_entry_rcu(skp, &smack_known_list, list)
1409                         if (skp->smk_netlabel.attr.mls.lvl ==
1410                             smack_cipso_direct)
1411                                 skp->smk_netlabel.attr.mls.lvl = i;
1412                 smack_cipso_direct = i;
1413                 mutex_unlock(&smack_known_lock);
1414         }
1415
1416         return count;
1417 }
1418
1419 static const struct file_operations smk_direct_ops = {
1420         .read           = smk_read_direct,
1421         .write          = smk_write_direct,
1422         .llseek         = default_llseek,
1423 };
1424
1425 /**
1426  * smk_read_mapped - read() for /smack/mapped
1427  * @filp: file pointer, not actually used
1428  * @buf: where to put the result
1429  * @count: maximum to send along
1430  * @ppos: where to start
1431  *
1432  * Returns number of bytes read or error code, as appropriate
1433  */
1434 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1435                                size_t count, loff_t *ppos)
1436 {
1437         char temp[80];
1438         ssize_t rc;
1439
1440         if (*ppos != 0)
1441                 return 0;
1442
1443         sprintf(temp, "%d", smack_cipso_mapped);
1444         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1445
1446         return rc;
1447 }
1448
1449 /**
1450  * smk_write_mapped - write() for /smack/mapped
1451  * @file: file pointer, not actually used
1452  * @buf: where to get the data from
1453  * @count: bytes sent
1454  * @ppos: where to start
1455  *
1456  * Returns number of bytes written or error code, as appropriate
1457  */
1458 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1459                                 size_t count, loff_t *ppos)
1460 {
1461         struct smack_known *skp;
1462         char temp[80];
1463         int i;
1464
1465         if (!smack_privileged(CAP_MAC_ADMIN))
1466                 return -EPERM;
1467
1468         if (count >= sizeof(temp) || count == 0)
1469                 return -EINVAL;
1470
1471         if (copy_from_user(temp, buf, count) != 0)
1472                 return -EFAULT;
1473
1474         temp[count] = '\0';
1475
1476         if (sscanf(temp, "%d", &i) != 1)
1477                 return -EINVAL;
1478
1479         /*
1480          * Don't do anything if the value hasn't actually changed.
1481          * If it is changing reset the level on entries that were
1482          * set up to be mapped when they were created.
1483          */
1484         if (smack_cipso_mapped != i) {
1485                 mutex_lock(&smack_known_lock);
1486                 list_for_each_entry_rcu(skp, &smack_known_list, list)
1487                         if (skp->smk_netlabel.attr.mls.lvl ==
1488                             smack_cipso_mapped)
1489                                 skp->smk_netlabel.attr.mls.lvl = i;
1490                 smack_cipso_mapped = i;
1491                 mutex_unlock(&smack_known_lock);
1492         }
1493
1494         return count;
1495 }
1496
1497 static const struct file_operations smk_mapped_ops = {
1498         .read           = smk_read_mapped,
1499         .write          = smk_write_mapped,
1500         .llseek         = default_llseek,
1501 };
1502
1503 /**
1504  * smk_read_ambient - read() for /smack/ambient
1505  * @filp: file pointer, not actually used
1506  * @buf: where to put the result
1507  * @cn: maximum to send along
1508  * @ppos: where to start
1509  *
1510  * Returns number of bytes read or error code, as appropriate
1511  */
1512 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1513                                 size_t cn, loff_t *ppos)
1514 {
1515         ssize_t rc;
1516         int asize;
1517
1518         if (*ppos != 0)
1519                 return 0;
1520         /*
1521          * Being careful to avoid a problem in the case where
1522          * smack_net_ambient gets changed in midstream.
1523          */
1524         mutex_lock(&smack_ambient_lock);
1525
1526         asize = strlen(smack_net_ambient) + 1;
1527
1528         if (cn >= asize)
1529                 rc = simple_read_from_buffer(buf, cn, ppos,
1530                                              smack_net_ambient, asize);
1531         else
1532                 rc = -EINVAL;
1533
1534         mutex_unlock(&smack_ambient_lock);
1535
1536         return rc;
1537 }
1538
1539 /**
1540  * smk_write_ambient - write() for /smack/ambient
1541  * @file: file pointer, not actually used
1542  * @buf: where to get the data from
1543  * @count: bytes sent
1544  * @ppos: where to start
1545  *
1546  * Returns number of bytes written or error code, as appropriate
1547  */
1548 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1549                                  size_t count, loff_t *ppos)
1550 {
1551         char *oldambient;
1552         char *smack = NULL;
1553         char *data;
1554         int rc = count;
1555
1556         if (!smack_privileged(CAP_MAC_ADMIN))
1557                 return -EPERM;
1558
1559         data = kzalloc(count + 1, GFP_KERNEL);
1560         if (data == NULL)
1561                 return -ENOMEM;
1562
1563         if (copy_from_user(data, buf, count) != 0) {
1564                 rc = -EFAULT;
1565                 goto out;
1566         }
1567
1568         smack = smk_import(data, count);
1569         if (smack == NULL) {
1570                 rc = -EINVAL;
1571                 goto out;
1572         }
1573
1574         mutex_lock(&smack_ambient_lock);
1575
1576         oldambient = smack_net_ambient;
1577         smack_net_ambient = smack;
1578         smk_unlbl_ambient(oldambient);
1579
1580         mutex_unlock(&smack_ambient_lock);
1581
1582 out:
1583         kfree(data);
1584         return rc;
1585 }
1586
1587 static const struct file_operations smk_ambient_ops = {
1588         .read           = smk_read_ambient,
1589         .write          = smk_write_ambient,
1590         .llseek         = default_llseek,
1591 };
1592
1593 /**
1594  * smk_read_onlycap - read() for /smack/onlycap
1595  * @filp: file pointer, not actually used
1596  * @buf: where to put the result
1597  * @cn: maximum to send along
1598  * @ppos: where to start
1599  *
1600  * Returns number of bytes read or error code, as appropriate
1601  */
1602 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1603                                 size_t cn, loff_t *ppos)
1604 {
1605         char *smack = "";
1606         ssize_t rc = -EINVAL;
1607         int asize;
1608
1609         if (*ppos != 0)
1610                 return 0;
1611
1612         if (smack_onlycap != NULL)
1613                 smack = smack_onlycap;
1614
1615         asize = strlen(smack) + 1;
1616
1617         if (cn >= asize)
1618                 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1619
1620         return rc;
1621 }
1622
1623 /**
1624  * smk_write_onlycap - write() for /smack/onlycap
1625  * @file: file pointer, not actually used
1626  * @buf: where to get the data from
1627  * @count: bytes sent
1628  * @ppos: where to start
1629  *
1630  * Returns number of bytes written or error code, as appropriate
1631  */
1632 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1633                                  size_t count, loff_t *ppos)
1634 {
1635         char *data;
1636         char *sp = smk_of_task(current->cred->security);
1637         int rc = count;
1638
1639         if (!smack_privileged(CAP_MAC_ADMIN))
1640                 return -EPERM;
1641
1642         /*
1643          * This can be done using smk_access() but is done
1644          * explicitly for clarity. The smk_access() implementation
1645          * would use smk_access(smack_onlycap, MAY_WRITE)
1646          */
1647         if (smack_onlycap != NULL && smack_onlycap != sp)
1648                 return -EPERM;
1649
1650         data = kzalloc(count, GFP_KERNEL);
1651         if (data == NULL)
1652                 return -ENOMEM;
1653
1654         /*
1655          * Should the null string be passed in unset the onlycap value.
1656          * This seems like something to be careful with as usually
1657          * smk_import only expects to return NULL for errors. It
1658          * is usually the case that a nullstring or "\n" would be
1659          * bad to pass to smk_import but in fact this is useful here.
1660          *
1661          * smk_import will also reject a label beginning with '-',
1662          * so "-usecapabilities" will also work.
1663          */
1664         if (copy_from_user(data, buf, count) != 0)
1665                 rc = -EFAULT;
1666         else
1667                 smack_onlycap = smk_import(data, count);
1668
1669         kfree(data);
1670         return rc;
1671 }
1672
1673 static const struct file_operations smk_onlycap_ops = {
1674         .read           = smk_read_onlycap,
1675         .write          = smk_write_onlycap,
1676         .llseek         = default_llseek,
1677 };
1678
1679 /**
1680  * smk_read_logging - read() for /smack/logging
1681  * @filp: file pointer, not actually used
1682  * @buf: where to put the result
1683  * @cn: maximum to send along
1684  * @ppos: where to start
1685  *
1686  * Returns number of bytes read or error code, as appropriate
1687  */
1688 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1689                                 size_t count, loff_t *ppos)
1690 {
1691         char temp[32];
1692         ssize_t rc;
1693
1694         if (*ppos != 0)
1695                 return 0;
1696
1697         sprintf(temp, "%d\n", log_policy);
1698         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1699         return rc;
1700 }
1701
1702 /**
1703  * smk_write_logging - write() for /smack/logging
1704  * @file: file pointer, not actually used
1705  * @buf: where to get the data from
1706  * @count: bytes sent
1707  * @ppos: where to start
1708  *
1709  * Returns number of bytes written or error code, as appropriate
1710  */
1711 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1712                                 size_t count, loff_t *ppos)
1713 {
1714         char temp[32];
1715         int i;
1716
1717         if (!smack_privileged(CAP_MAC_ADMIN))
1718                 return -EPERM;
1719
1720         if (count >= sizeof(temp) || count == 0)
1721                 return -EINVAL;
1722
1723         if (copy_from_user(temp, buf, count) != 0)
1724                 return -EFAULT;
1725
1726         temp[count] = '\0';
1727
1728         if (sscanf(temp, "%d", &i) != 1)
1729                 return -EINVAL;
1730         if (i < 0 || i > 3)
1731                 return -EINVAL;
1732         log_policy = i;
1733         return count;
1734 }
1735
1736
1737
1738 static const struct file_operations smk_logging_ops = {
1739         .read           = smk_read_logging,
1740         .write          = smk_write_logging,
1741         .llseek         = default_llseek,
1742 };
1743
1744 /*
1745  * Seq_file read operations for /smack/load-self
1746  */
1747
1748 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1749 {
1750         struct task_smack *tsp = current_security();
1751
1752         return smk_seq_start(s, pos, &tsp->smk_rules);
1753 }
1754
1755 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1756 {
1757         struct task_smack *tsp = current_security();
1758
1759         return smk_seq_next(s, v, pos, &tsp->smk_rules);
1760 }
1761
1762 static int load_self_seq_show(struct seq_file *s, void *v)
1763 {
1764         struct list_head *list = v;
1765         struct smack_rule *srp =
1766                  list_entry(list, struct smack_rule, list);
1767
1768         smk_rule_show(s, srp, SMK_LABELLEN);
1769
1770         return 0;
1771 }
1772
1773 static const struct seq_operations load_self_seq_ops = {
1774         .start = load_self_seq_start,
1775         .next  = load_self_seq_next,
1776         .show  = load_self_seq_show,
1777         .stop  = smk_seq_stop,
1778 };
1779
1780
1781 /**
1782  * smk_open_load_self - open() for /smack/load-self2
1783  * @inode: inode structure representing file
1784  * @file: "load" file pointer
1785  *
1786  * For reading, use load_seq_* seq_file reading operations.
1787  */
1788 static int smk_open_load_self(struct inode *inode, struct file *file)
1789 {
1790         return seq_open(file, &load_self_seq_ops);
1791 }
1792
1793 /**
1794  * smk_write_load_self - write() for /smack/load-self
1795  * @file: file pointer, not actually used
1796  * @buf: where to get the data from
1797  * @count: bytes sent
1798  * @ppos: where to start - must be 0
1799  *
1800  */
1801 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1802                               size_t count, loff_t *ppos)
1803 {
1804         struct task_smack *tsp = current_security();
1805
1806         return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1807                                     &tsp->smk_rules_lock, SMK_FIXED24_FMT);
1808 }
1809
1810 static const struct file_operations smk_load_self_ops = {
1811         .open           = smk_open_load_self,
1812         .read           = seq_read,
1813         .llseek         = seq_lseek,
1814         .write          = smk_write_load_self,
1815         .release        = seq_release,
1816 };
1817
1818 /**
1819  * smk_user_access - handle access check transaction
1820  * @file: file pointer
1821  * @buf: data from user space
1822  * @count: bytes sent
1823  * @ppos: where to start - must be 0
1824  */
1825 static ssize_t smk_user_access(struct file *file, const char __user *buf,
1826                                 size_t count, loff_t *ppos, int format)
1827 {
1828         struct smack_parsed_rule rule;
1829         char *data;
1830         char *cod;
1831         int res;
1832
1833         data = simple_transaction_get(file, buf, count);
1834         if (IS_ERR(data))
1835                 return PTR_ERR(data);
1836
1837         if (format == SMK_FIXED24_FMT) {
1838                 if (count < SMK_LOADLEN)
1839                         return -EINVAL;
1840                 res = smk_parse_rule(data, &rule, 0);
1841         } else {
1842                 /*
1843                  * Copy the data to make sure the string is terminated.
1844                  */
1845                 cod = kzalloc(count + 1, GFP_KERNEL);
1846                 if (cod == NULL)
1847                         return -ENOMEM;
1848                 memcpy(cod, data, count);
1849                 cod[count] = '\0';
1850                 res = smk_parse_long_rule(cod, &rule, 0, 0);
1851                 kfree(cod);
1852         }
1853
1854         if (res)
1855                 return -EINVAL;
1856
1857         res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access1,
1858                           NULL);
1859         data[0] = res == 0 ? '1' : '0';
1860         data[1] = '\0';
1861
1862         simple_transaction_set(file, 2);
1863
1864         if (format == SMK_FIXED24_FMT)
1865                 return SMK_LOADLEN;
1866         return count;
1867 }
1868
1869 /**
1870  * smk_write_access - handle access check transaction
1871  * @file: file pointer
1872  * @buf: data from user space
1873  * @count: bytes sent
1874  * @ppos: where to start - must be 0
1875  */
1876 static ssize_t smk_write_access(struct file *file, const char __user *buf,
1877                                 size_t count, loff_t *ppos)
1878 {
1879         return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
1880 }
1881
1882 static const struct file_operations smk_access_ops = {
1883         .write          = smk_write_access,
1884         .read           = simple_transaction_read,
1885         .release        = simple_transaction_release,
1886         .llseek         = generic_file_llseek,
1887 };
1888
1889
1890 /*
1891  * Seq_file read operations for /smack/load2
1892  */
1893
1894 static int load2_seq_show(struct seq_file *s, void *v)
1895 {
1896         struct list_head *list = v;
1897         struct smack_master_list *smlp =
1898                  list_entry(list, struct smack_master_list, list);
1899
1900         smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
1901
1902         return 0;
1903 }
1904
1905 static const struct seq_operations load2_seq_ops = {
1906         .start = load2_seq_start,
1907         .next  = load2_seq_next,
1908         .show  = load2_seq_show,
1909         .stop  = smk_seq_stop,
1910 };
1911
1912 /**
1913  * smk_open_load2 - open() for /smack/load2
1914  * @inode: inode structure representing file
1915  * @file: "load2" file pointer
1916  *
1917  * For reading, use load2_seq_* seq_file reading operations.
1918  */
1919 static int smk_open_load2(struct inode *inode, struct file *file)
1920 {
1921         return seq_open(file, &load2_seq_ops);
1922 }
1923
1924 /**
1925  * smk_write_load2 - write() for /smack/load2
1926  * @file: file pointer, not actually used
1927  * @buf: where to get the data from
1928  * @count: bytes sent
1929  * @ppos: where to start - must be 0
1930  *
1931  */
1932 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
1933                                 size_t count, loff_t *ppos)
1934 {
1935         /*
1936          * Must have privilege.
1937          */
1938         if (!smack_privileged(CAP_MAC_ADMIN))
1939                 return -EPERM;
1940
1941         return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
1942                                     SMK_LONG_FMT);
1943 }
1944
1945 static const struct file_operations smk_load2_ops = {
1946         .open           = smk_open_load2,
1947         .read           = seq_read,
1948         .llseek         = seq_lseek,
1949         .write          = smk_write_load2,
1950         .release        = seq_release,
1951 };
1952
1953 /*
1954  * Seq_file read operations for /smack/load-self2
1955  */
1956
1957 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
1958 {
1959         struct task_smack *tsp = current_security();
1960
1961         return smk_seq_start(s, pos, &tsp->smk_rules);
1962 }
1963
1964 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
1965 {
1966         struct task_smack *tsp = current_security();
1967
1968         return smk_seq_next(s, v, pos, &tsp->smk_rules);
1969 }
1970
1971 static int load_self2_seq_show(struct seq_file *s, void *v)
1972 {
1973         struct list_head *list = v;
1974         struct smack_rule *srp =
1975                  list_entry(list, struct smack_rule, list);
1976
1977         smk_rule_show(s, srp, SMK_LONGLABEL);
1978
1979         return 0;
1980 }
1981
1982 static const struct seq_operations load_self2_seq_ops = {
1983         .start = load_self2_seq_start,
1984         .next  = load_self2_seq_next,
1985         .show  = load_self2_seq_show,
1986         .stop  = smk_seq_stop,
1987 };
1988
1989 /**
1990  * smk_open_load_self2 - open() for /smack/load-self2
1991  * @inode: inode structure representing file
1992  * @file: "load" file pointer
1993  *
1994  * For reading, use load_seq_* seq_file reading operations.
1995  */
1996 static int smk_open_load_self2(struct inode *inode, struct file *file)
1997 {
1998         return seq_open(file, &load_self2_seq_ops);
1999 }
2000
2001 /**
2002  * smk_write_load_self2 - write() for /smack/load-self2
2003  * @file: file pointer, not actually used
2004  * @buf: where to get the data from
2005  * @count: bytes sent
2006  * @ppos: where to start - must be 0
2007  *
2008  */
2009 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2010                               size_t count, loff_t *ppos)
2011 {
2012         struct task_smack *tsp = current_security();
2013
2014         return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2015                                     &tsp->smk_rules_lock, SMK_LONG_FMT);
2016 }
2017
2018 static const struct file_operations smk_load_self2_ops = {
2019         .open           = smk_open_load_self2,
2020         .read           = seq_read,
2021         .llseek         = seq_lseek,
2022         .write          = smk_write_load_self2,
2023         .release        = seq_release,
2024 };
2025
2026 /**
2027  * smk_write_access2 - handle access check transaction
2028  * @file: file pointer
2029  * @buf: data from user space
2030  * @count: bytes sent
2031  * @ppos: where to start - must be 0
2032  */
2033 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2034                                         size_t count, loff_t *ppos)
2035 {
2036         return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2037 }
2038
2039 static const struct file_operations smk_access2_ops = {
2040         .write          = smk_write_access2,
2041         .read           = simple_transaction_read,
2042         .release        = simple_transaction_release,
2043         .llseek         = generic_file_llseek,
2044 };
2045
2046 /**
2047  * smk_write_revoke_subj - write() for /smack/revoke-subject
2048  * @file: file pointer
2049  * @buf: data from user space
2050  * @count: bytes sent
2051  * @ppos: where to start - must be 0
2052  */
2053 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2054                                 size_t count, loff_t *ppos)
2055 {
2056         char *data = NULL;
2057         const char *cp = NULL;
2058         struct smack_known *skp;
2059         struct smack_rule *sp;
2060         struct list_head *rule_list;
2061         struct mutex *rule_lock;
2062         int rc = count;
2063
2064         if (*ppos != 0)
2065                 return -EINVAL;
2066
2067         if (!smack_privileged(CAP_MAC_ADMIN))
2068                 return -EPERM;
2069
2070         if (count == 0 || count > SMK_LONGLABEL)
2071                 return -EINVAL;
2072
2073         data = kzalloc(count, GFP_KERNEL);
2074         if (data == NULL)
2075                 return -ENOMEM;
2076
2077         if (copy_from_user(data, buf, count) != 0) {
2078                 rc = -EFAULT;
2079                 goto free_out;
2080         }
2081
2082         cp = smk_parse_smack(data, count);
2083         if (cp == NULL) {
2084                 rc = -EINVAL;
2085                 goto free_out;
2086         }
2087
2088         skp = smk_find_entry(cp);
2089         if (skp == NULL)
2090                 goto free_out;
2091
2092         rule_list = &skp->smk_rules;
2093         rule_lock = &skp->smk_rules_lock;
2094
2095         mutex_lock(rule_lock);
2096
2097         list_for_each_entry_rcu(sp, rule_list, list)
2098                 sp->smk_access = 0;
2099
2100         mutex_unlock(rule_lock);
2101
2102 free_out:
2103         kfree(data);
2104         kfree(cp);
2105         return rc;
2106 }
2107
2108 static const struct file_operations smk_revoke_subj_ops = {
2109         .write          = smk_write_revoke_subj,
2110         .read           = simple_transaction_read,
2111         .release        = simple_transaction_release,
2112         .llseek         = generic_file_llseek,
2113 };
2114
2115 static struct kset *smackfs_kset;
2116 /**
2117  * smk_init_sysfs - initialize /sys/fs/smackfs
2118  *
2119  */
2120 static int smk_init_sysfs(void)
2121 {
2122         smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2123         if (!smackfs_kset)
2124                 return -ENOMEM;
2125         return 0;
2126 }
2127
2128 /**
2129  * smk_write_change_rule - write() for /smack/change-rule
2130  * @file: file pointer
2131  * @buf: data from user space
2132  * @count: bytes sent
2133  * @ppos: where to start - must be 0
2134  */
2135 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2136                                 size_t count, loff_t *ppos)
2137 {
2138         /*
2139          * Must have privilege.
2140          */
2141         if (!capable(CAP_MAC_ADMIN))
2142                 return -EPERM;
2143
2144         return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2145                                     SMK_CHANGE_FMT);
2146 }
2147
2148 static const struct file_operations smk_change_rule_ops = {
2149         .write          = smk_write_change_rule,
2150         .read           = simple_transaction_read,
2151         .release        = simple_transaction_release,
2152         .llseek         = generic_file_llseek,
2153 };
2154
2155 /**
2156  * smk_fill_super - fill the /smackfs superblock
2157  * @sb: the empty superblock
2158  * @data: unused
2159  * @silent: unused
2160  *
2161  * Fill in the well known entries for /smack
2162  *
2163  * Returns 0 on success, an error code on failure
2164  */
2165 static int smk_fill_super(struct super_block *sb, void *data, int silent)
2166 {
2167         int rc;
2168         struct inode *root_inode;
2169
2170         static struct tree_descr smack_files[] = {
2171                 [SMK_LOAD] = {
2172                         "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2173                 [SMK_CIPSO] = {
2174                         "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2175                 [SMK_DOI] = {
2176                         "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2177                 [SMK_DIRECT] = {
2178                         "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2179                 [SMK_AMBIENT] = {
2180                         "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2181                 [SMK_NETLBLADDR] = {
2182                         "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2183                 [SMK_ONLYCAP] = {
2184                         "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2185                 [SMK_LOGGING] = {
2186                         "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2187                 [SMK_LOAD_SELF] = {
2188                         "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2189                 [SMK_ACCESSES] = {
2190                         "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2191                 [SMK_MAPPED] = {
2192                         "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2193                 [SMK_LOAD2] = {
2194                         "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2195                 [SMK_LOAD_SELF2] = {
2196                         "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2197                 [SMK_ACCESS2] = {
2198                         "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2199                 [SMK_CIPSO2] = {
2200                         "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2201                 [SMK_REVOKE_SUBJ] = {
2202                         "revoke-subject", &smk_revoke_subj_ops,
2203                         S_IRUGO|S_IWUSR},
2204                 [SMK_CHANGE_RULE] = {
2205                         "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2206                 /* last one */
2207                         {""}
2208         };
2209
2210         rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2211         if (rc != 0) {
2212                 printk(KERN_ERR "%s failed %d while creating inodes\n",
2213                         __func__, rc);
2214                 return rc;
2215         }
2216
2217         root_inode = sb->s_root->d_inode;
2218
2219         return 0;
2220 }
2221
2222 /**
2223  * smk_mount - get the smackfs superblock
2224  * @fs_type: passed along without comment
2225  * @flags: passed along without comment
2226  * @dev_name: passed along without comment
2227  * @data: passed along without comment
2228  *
2229  * Just passes everything along.
2230  *
2231  * Returns what the lower level code does.
2232  */
2233 static struct dentry *smk_mount(struct file_system_type *fs_type,
2234                       int flags, const char *dev_name, void *data)
2235 {
2236         return mount_single(fs_type, flags, data, smk_fill_super);
2237 }
2238
2239 static struct file_system_type smk_fs_type = {
2240         .name           = "smackfs",
2241         .mount          = smk_mount,
2242         .kill_sb        = kill_litter_super,
2243 };
2244
2245 static struct vfsmount *smackfs_mount;
2246
2247 static int __init smk_preset_netlabel(struct smack_known *skp)
2248 {
2249         skp->smk_netlabel.domain = skp->smk_known;
2250         skp->smk_netlabel.flags =
2251                 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2252         return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2253                                 &skp->smk_netlabel, strlen(skp->smk_known));
2254 }
2255
2256 /**
2257  * init_smk_fs - get the smackfs superblock
2258  *
2259  * register the smackfs
2260  *
2261  * Do not register smackfs if Smack wasn't enabled
2262  * on boot. We can not put this method normally under the
2263  * smack_init() code path since the security subsystem get
2264  * initialized before the vfs caches.
2265  *
2266  * Returns true if we were not chosen on boot or if
2267  * we were chosen and filesystem registration succeeded.
2268  */
2269 static int __init init_smk_fs(void)
2270 {
2271         int err;
2272         int rc;
2273
2274         if (!security_module_enable(&smack_ops))
2275                 return 0;
2276
2277         err = smk_init_sysfs();
2278         if (err)
2279                 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2280
2281         err = register_filesystem(&smk_fs_type);
2282         if (!err) {
2283                 smackfs_mount = kern_mount(&smk_fs_type);
2284                 if (IS_ERR(smackfs_mount)) {
2285                         printk(KERN_ERR "smackfs:  could not mount!\n");
2286                         err = PTR_ERR(smackfs_mount);
2287                         smackfs_mount = NULL;
2288                 }
2289         }
2290
2291         smk_cipso_doi();
2292         smk_unlbl_ambient(NULL);
2293
2294         rc = smk_preset_netlabel(&smack_known_floor);
2295         if (err == 0 && rc < 0)
2296                 err = rc;
2297         rc = smk_preset_netlabel(&smack_known_hat);
2298         if (err == 0 && rc < 0)
2299                 err = rc;
2300         rc = smk_preset_netlabel(&smack_known_huh);
2301         if (err == 0 && rc < 0)
2302                 err = rc;
2303         rc = smk_preset_netlabel(&smack_known_invalid);
2304         if (err == 0 && rc < 0)
2305                 err = rc;
2306         rc = smk_preset_netlabel(&smack_known_star);
2307         if (err == 0 && rc < 0)
2308                 err = rc;
2309         rc = smk_preset_netlabel(&smack_known_web);
2310         if (err == 0 && rc < 0)
2311                 err = rc;
2312
2313         return err;
2314 }
2315
2316 __initcall(init_smk_fs);