f2fb563af43176045eedd9d5b2d49ffc8a9d4977
[linux-block.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 <linux/magic.h>
30 #include <linux/fs_context.h>
31 #include "smack.h"
32
33 #define BEBITS  (sizeof(__be32) * 8)
34 /*
35  * smackfs pseudo filesystem.
36  */
37
38 enum smk_inos {
39         SMK_ROOT_INO    = 2,
40         SMK_LOAD        = 3,    /* load policy */
41         SMK_CIPSO       = 4,    /* load label -> CIPSO mapping */
42         SMK_DOI         = 5,    /* CIPSO DOI */
43         SMK_DIRECT      = 6,    /* CIPSO level indicating direct label */
44         SMK_AMBIENT     = 7,    /* internet ambient label */
45         SMK_NET4ADDR    = 8,    /* single label hosts */
46         SMK_ONLYCAP     = 9,    /* the only "capable" label */
47         SMK_LOGGING     = 10,   /* logging */
48         SMK_LOAD_SELF   = 11,   /* task specific rules */
49         SMK_ACCESSES    = 12,   /* access policy */
50         SMK_MAPPED      = 13,   /* CIPSO level indicating mapped label */
51         SMK_LOAD2       = 14,   /* load policy with long labels */
52         SMK_LOAD_SELF2  = 15,   /* load task specific rules with long labels */
53         SMK_ACCESS2     = 16,   /* make an access check with long labels */
54         SMK_CIPSO2      = 17,   /* load long label -> CIPSO mapping */
55         SMK_REVOKE_SUBJ = 18,   /* set rules with subject label to '-' */
56         SMK_CHANGE_RULE = 19,   /* change or add rules (long labels) */
57         SMK_SYSLOG      = 20,   /* change syslog label) */
58         SMK_PTRACE      = 21,   /* set ptrace rule */
59 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
60         SMK_UNCONFINED  = 22,   /* define an unconfined label */
61 #endif
62 #if IS_ENABLED(CONFIG_IPV6)
63         SMK_NET6ADDR    = 23,   /* single label IPv6 hosts */
64 #endif /* CONFIG_IPV6 */
65         SMK_RELABEL_SELF = 24, /* relabel possible without CAP_MAC_ADMIN */
66 };
67
68 /*
69  * List locks
70  */
71 static DEFINE_MUTEX(smack_cipso_lock);
72 static DEFINE_MUTEX(smack_ambient_lock);
73 static DEFINE_MUTEX(smk_net4addr_lock);
74 #if IS_ENABLED(CONFIG_IPV6)
75 static DEFINE_MUTEX(smk_net6addr_lock);
76 #endif /* CONFIG_IPV6 */
77
78 /*
79  * This is the "ambient" label for network traffic.
80  * If it isn't somehow marked, use this.
81  * It can be reset via smackfs/ambient
82  */
83 struct smack_known *smack_net_ambient;
84
85 /*
86  * This is the level in a CIPSO header that indicates a
87  * smack label is contained directly in the category set.
88  * It can be reset via smackfs/direct
89  */
90 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
91
92 /*
93  * This is the level in a CIPSO header that indicates a
94  * secid is contained directly in the category set.
95  * It can be reset via smackfs/mapped
96  */
97 int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
98
99 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
100 /*
101  * Allow one label to be unconfined. This is for
102  * debugging and application bring-up purposes only.
103  * It is bad and wrong, but everyone seems to expect
104  * to have it.
105  */
106 struct smack_known *smack_unconfined;
107 #endif
108
109 /*
110  * If this value is set restrict syslog use to the label specified.
111  * It can be reset via smackfs/syslog
112  */
113 struct smack_known *smack_syslog_label;
114
115 /*
116  * Ptrace current rule
117  * SMACK_PTRACE_DEFAULT    regular smack ptrace rules (/proc based)
118  * SMACK_PTRACE_EXACT      labels must match, but can be overriden with
119  *                         CAP_SYS_PTRACE
120  * SMACK_PTRACE_DRACONIAN  lables must match, CAP_SYS_PTRACE has no effect
121  */
122 int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
123
124 /*
125  * Certain IP addresses may be designated as single label hosts.
126  * Packets are sent there unlabeled, but only from tasks that
127  * can write to the specified label.
128  */
129
130 LIST_HEAD(smk_net4addr_list);
131 #if IS_ENABLED(CONFIG_IPV6)
132 LIST_HEAD(smk_net6addr_list);
133 #endif /* CONFIG_IPV6 */
134
135 /*
136  * Rule lists are maintained for each label.
137  */
138 struct smack_parsed_rule {
139         struct smack_known      *smk_subject;
140         struct smack_known      *smk_object;
141         int                     smk_access1;
142         int                     smk_access2;
143 };
144
145 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
146
147 /*
148  * Values for parsing cipso rules
149  * SMK_DIGITLEN: Length of a digit field in a rule.
150  * SMK_CIPSOMIN: Minimum possible cipso rule length.
151  * SMK_CIPSOMAX: Maximum possible cipso rule length.
152  */
153 #define SMK_DIGITLEN 4
154 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
155 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
156
157 /*
158  * Values for parsing MAC rules
159  * SMK_ACCESS: Maximum possible combination of access permissions
160  * SMK_ACCESSLEN: Maximum length for a rule access field
161  * SMK_LOADLEN: Smack rule length
162  */
163 #define SMK_OACCESS     "rwxa"
164 #define SMK_ACCESS      "rwxatl"
165 #define SMK_OACCESSLEN  (sizeof(SMK_OACCESS) - 1)
166 #define SMK_ACCESSLEN   (sizeof(SMK_ACCESS) - 1)
167 #define SMK_OLOADLEN    (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
168 #define SMK_LOADLEN     (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
169
170 /*
171  * Stricly for CIPSO level manipulation.
172  * Set the category bit number in a smack label sized buffer.
173  */
174 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
175 {
176         if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
177                 return;
178
179         catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
180 }
181
182 /**
183  * smk_netlabel_audit_set - fill a netlbl_audit struct
184  * @nap: structure to fill
185  */
186 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
187 {
188         struct smack_known *skp = smk_of_current();
189
190         nap->loginuid = audit_get_loginuid(current);
191         nap->sessionid = audit_get_sessionid(current);
192         nap->secid = skp->smk_secid;
193 }
194
195 /*
196  * Value for parsing single label host rules
197  * "1.2.3.4 X"
198  */
199 #define SMK_NETLBLADDRMIN       9
200
201 /**
202  * smk_set_access - add a rule to the rule list or replace an old rule
203  * @srp: the rule to add or replace
204  * @rule_list: the list of rules
205  * @rule_lock: the rule list lock
206  *
207  * Looks through the current subject/object/access list for
208  * the subject/object pair and replaces the access that was
209  * there. If the pair isn't found add it with the specified
210  * access.
211  *
212  * Returns 0 if nothing goes wrong or -ENOMEM if it fails
213  * during the allocation of the new pair to add.
214  */
215 static int smk_set_access(struct smack_parsed_rule *srp,
216                                 struct list_head *rule_list,
217                                 struct mutex *rule_lock)
218 {
219         struct smack_rule *sp;
220         int found = 0;
221         int rc = 0;
222
223         mutex_lock(rule_lock);
224
225         /*
226          * Because the object label is less likely to match
227          * than the subject label check it first
228          */
229         list_for_each_entry_rcu(sp, rule_list, list) {
230                 if (sp->smk_object == srp->smk_object &&
231                     sp->smk_subject == srp->smk_subject) {
232                         found = 1;
233                         sp->smk_access |= srp->smk_access1;
234                         sp->smk_access &= ~srp->smk_access2;
235                         break;
236                 }
237         }
238
239         if (found == 0) {
240                 sp = kmem_cache_zalloc(smack_rule_cache, GFP_KERNEL);
241                 if (sp == NULL) {
242                         rc = -ENOMEM;
243                         goto out;
244                 }
245
246                 sp->smk_subject = srp->smk_subject;
247                 sp->smk_object = srp->smk_object;
248                 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
249
250                 list_add_rcu(&sp->list, rule_list);
251         }
252
253 out:
254         mutex_unlock(rule_lock);
255         return rc;
256 }
257
258 /**
259  * smk_perm_from_str - parse smack accesses from a text string
260  * @string: a text string that contains a Smack accesses code
261  *
262  * Returns an integer with respective bits set for specified accesses.
263  */
264 static int smk_perm_from_str(const char *string)
265 {
266         int perm = 0;
267         const char *cp;
268
269         for (cp = string; ; cp++)
270                 switch (*cp) {
271                 case '-':
272                         break;
273                 case 'r':
274                 case 'R':
275                         perm |= MAY_READ;
276                         break;
277                 case 'w':
278                 case 'W':
279                         perm |= MAY_WRITE;
280                         break;
281                 case 'x':
282                 case 'X':
283                         perm |= MAY_EXEC;
284                         break;
285                 case 'a':
286                 case 'A':
287                         perm |= MAY_APPEND;
288                         break;
289                 case 't':
290                 case 'T':
291                         perm |= MAY_TRANSMUTE;
292                         break;
293                 case 'l':
294                 case 'L':
295                         perm |= MAY_LOCK;
296                         break;
297                 case 'b':
298                 case 'B':
299                         perm |= MAY_BRINGUP;
300                         break;
301                 default:
302                         return perm;
303                 }
304 }
305
306 /**
307  * smk_fill_rule - Fill Smack rule from strings
308  * @subject: subject label string
309  * @object: object label string
310  * @access1: access string
311  * @access2: string with permissions to be removed
312  * @rule: Smack rule
313  * @import: if non-zero, import labels
314  * @len: label length limit
315  *
316  * Returns 0 on success, appropriate error code on failure.
317  */
318 static int smk_fill_rule(const char *subject, const char *object,
319                                 const char *access1, const char *access2,
320                                 struct smack_parsed_rule *rule, int import,
321                                 int len)
322 {
323         const char *cp;
324         struct smack_known *skp;
325
326         if (import) {
327                 rule->smk_subject = smk_import_entry(subject, len);
328                 if (IS_ERR(rule->smk_subject))
329                         return PTR_ERR(rule->smk_subject);
330
331                 rule->smk_object = smk_import_entry(object, len);
332                 if (IS_ERR(rule->smk_object))
333                         return PTR_ERR(rule->smk_object);
334         } else {
335                 cp = smk_parse_smack(subject, len);
336                 if (IS_ERR(cp))
337                         return PTR_ERR(cp);
338                 skp = smk_find_entry(cp);
339                 kfree(cp);
340                 if (skp == NULL)
341                         return -ENOENT;
342                 rule->smk_subject = skp;
343
344                 cp = smk_parse_smack(object, len);
345                 if (IS_ERR(cp))
346                         return PTR_ERR(cp);
347                 skp = smk_find_entry(cp);
348                 kfree(cp);
349                 if (skp == NULL)
350                         return -ENOENT;
351                 rule->smk_object = skp;
352         }
353
354         rule->smk_access1 = smk_perm_from_str(access1);
355         if (access2)
356                 rule->smk_access2 = smk_perm_from_str(access2);
357         else
358                 rule->smk_access2 = ~rule->smk_access1;
359
360         return 0;
361 }
362
363 /**
364  * smk_parse_rule - parse Smack rule from load string
365  * @data: string to be parsed whose size is SMK_LOADLEN
366  * @rule: Smack rule
367  * @import: if non-zero, import labels
368  *
369  * Returns 0 on success, -1 on errors.
370  */
371 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
372                                 int import)
373 {
374         int rc;
375
376         rc = smk_fill_rule(data, data + SMK_LABELLEN,
377                            data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
378                            import, SMK_LABELLEN);
379         return rc;
380 }
381
382 /**
383  * smk_parse_long_rule - parse Smack rule from rule string
384  * @data: string to be parsed, null terminated
385  * @rule: Will be filled with Smack parsed rule
386  * @import: if non-zero, import labels
387  * @tokens: numer of substrings expected in data
388  *
389  * Returns number of processed bytes on success, -ERRNO on failure.
390  */
391 static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
392                                 int import, int tokens)
393 {
394         ssize_t cnt = 0;
395         char *tok[4];
396         int rc;
397         int i;
398
399         /*
400          * Parsing the rule in-place, filling all white-spaces with '\0'
401          */
402         for (i = 0; i < tokens; ++i) {
403                 while (isspace(data[cnt]))
404                         data[cnt++] = '\0';
405
406                 if (data[cnt] == '\0')
407                         /* Unexpected end of data */
408                         return -EINVAL;
409
410                 tok[i] = data + cnt;
411
412                 while (data[cnt] && !isspace(data[cnt]))
413                         ++cnt;
414         }
415         while (isspace(data[cnt]))
416                 data[cnt++] = '\0';
417
418         while (i < 4)
419                 tok[i++] = NULL;
420
421         rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
422         return rc == 0 ? cnt : rc;
423 }
424
425 #define SMK_FIXED24_FMT 0       /* Fixed 24byte label format */
426 #define SMK_LONG_FMT    1       /* Variable long label format */
427 #define SMK_CHANGE_FMT  2       /* Rule modification format */
428 /**
429  * smk_write_rules_list - write() for any /smack rule file
430  * @file: file pointer, not actually used
431  * @buf: where to get the data from
432  * @count: bytes sent
433  * @ppos: where to start - must be 0
434  * @rule_list: the list of rules to write to
435  * @rule_lock: lock for the rule list
436  * @format: /smack/load or /smack/load2 or /smack/change-rule format.
437  *
438  * Get one smack access rule from above.
439  * The format for SMK_LONG_FMT is:
440  *      "subject<whitespace>object<whitespace>access[<whitespace>...]"
441  * The format for SMK_FIXED24_FMT is exactly:
442  *      "subject                 object                  rwxat"
443  * The format for SMK_CHANGE_FMT is:
444  *      "subject<whitespace>object<whitespace>
445  *       acc_enable<whitespace>acc_disable[<whitespace>...]"
446  */
447 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
448                                         size_t count, loff_t *ppos,
449                                         struct list_head *rule_list,
450                                         struct mutex *rule_lock, int format)
451 {
452         struct smack_parsed_rule rule;
453         char *data;
454         int rc;
455         int trunc = 0;
456         int tokens;
457         ssize_t cnt = 0;
458
459         /*
460          * No partial writes.
461          * Enough data must be present.
462          */
463         if (*ppos != 0)
464                 return -EINVAL;
465
466         if (format == SMK_FIXED24_FMT) {
467                 /*
468                  * Minor hack for backward compatibility
469                  */
470                 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
471                         return -EINVAL;
472         } else {
473                 if (count >= PAGE_SIZE) {
474                         count = PAGE_SIZE - 1;
475                         trunc = 1;
476                 }
477         }
478
479         data = memdup_user_nul(buf, count);
480         if (IS_ERR(data))
481                 return PTR_ERR(data);
482
483         /*
484          * In case of parsing only part of user buf,
485          * avoid having partial rule at the data buffer
486          */
487         if (trunc) {
488                 while (count > 0 && (data[count - 1] != '\n'))
489                         --count;
490                 if (count == 0) {
491                         rc = -EINVAL;
492                         goto out;
493                 }
494         }
495
496         data[count] = '\0';
497         tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
498         while (cnt < count) {
499                 if (format == SMK_FIXED24_FMT) {
500                         rc = smk_parse_rule(data, &rule, 1);
501                         if (rc < 0)
502                                 goto out;
503                         cnt = count;
504                 } else {
505                         rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
506                         if (rc < 0)
507                                 goto out;
508                         if (rc == 0) {
509                                 rc = -EINVAL;
510                                 goto out;
511                         }
512                         cnt += rc;
513                 }
514
515                 if (rule_list == NULL)
516                         rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
517                                 &rule.smk_subject->smk_rules_lock);
518                 else
519                         rc = smk_set_access(&rule, rule_list, rule_lock);
520
521                 if (rc)
522                         goto out;
523         }
524
525         rc = cnt;
526 out:
527         kfree(data);
528         return rc;
529 }
530
531 /*
532  * Core logic for smackfs seq list operations.
533  */
534
535 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
536                                 struct list_head *head)
537 {
538         struct list_head *list;
539         int i = *pos;
540
541         rcu_read_lock();
542         for (list = rcu_dereference(list_next_rcu(head));
543                 list != head;
544                 list = rcu_dereference(list_next_rcu(list))) {
545                 if (i-- == 0)
546                         return list;
547         }
548
549         return NULL;
550 }
551
552 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
553                                 struct list_head *head)
554 {
555         struct list_head *list = v;
556
557         ++*pos;
558         list = rcu_dereference(list_next_rcu(list));
559
560         return (list == head) ? NULL : list;
561 }
562
563 static void smk_seq_stop(struct seq_file *s, void *v)
564 {
565         rcu_read_unlock();
566 }
567
568 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
569 {
570         /*
571          * Don't show any rules with label names too long for
572          * interface file (/smack/load or /smack/load2)
573          * because you should expect to be able to write
574          * anything you read back.
575          */
576         if (strlen(srp->smk_subject->smk_known) >= max ||
577             strlen(srp->smk_object->smk_known) >= max)
578                 return;
579
580         if (srp->smk_access == 0)
581                 return;
582
583         seq_printf(s, "%s %s",
584                    srp->smk_subject->smk_known,
585                    srp->smk_object->smk_known);
586
587         seq_putc(s, ' ');
588
589         if (srp->smk_access & MAY_READ)
590                 seq_putc(s, 'r');
591         if (srp->smk_access & MAY_WRITE)
592                 seq_putc(s, 'w');
593         if (srp->smk_access & MAY_EXEC)
594                 seq_putc(s, 'x');
595         if (srp->smk_access & MAY_APPEND)
596                 seq_putc(s, 'a');
597         if (srp->smk_access & MAY_TRANSMUTE)
598                 seq_putc(s, 't');
599         if (srp->smk_access & MAY_LOCK)
600                 seq_putc(s, 'l');
601         if (srp->smk_access & MAY_BRINGUP)
602                 seq_putc(s, 'b');
603
604         seq_putc(s, '\n');
605 }
606
607 /*
608  * Seq_file read operations for /smack/load
609  */
610
611 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
612 {
613         return smk_seq_start(s, pos, &smack_known_list);
614 }
615
616 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
617 {
618         return smk_seq_next(s, v, pos, &smack_known_list);
619 }
620
621 static int load_seq_show(struct seq_file *s, void *v)
622 {
623         struct list_head *list = v;
624         struct smack_rule *srp;
625         struct smack_known *skp =
626                 list_entry_rcu(list, struct smack_known, list);
627
628         list_for_each_entry_rcu(srp, &skp->smk_rules, list)
629                 smk_rule_show(s, srp, SMK_LABELLEN);
630
631         return 0;
632 }
633
634 static const struct seq_operations load_seq_ops = {
635         .start = load2_seq_start,
636         .next  = load2_seq_next,
637         .show  = load_seq_show,
638         .stop  = smk_seq_stop,
639 };
640
641 /**
642  * smk_open_load - open() for /smack/load
643  * @inode: inode structure representing file
644  * @file: "load" file pointer
645  *
646  * For reading, use load_seq_* seq_file reading operations.
647  */
648 static int smk_open_load(struct inode *inode, struct file *file)
649 {
650         return seq_open(file, &load_seq_ops);
651 }
652
653 /**
654  * smk_write_load - write() for /smack/load
655  * @file: file pointer, not actually used
656  * @buf: where to get the data from
657  * @count: bytes sent
658  * @ppos: where to start - must be 0
659  *
660  */
661 static ssize_t smk_write_load(struct file *file, const char __user *buf,
662                               size_t count, loff_t *ppos)
663 {
664         /*
665          * Must have privilege.
666          * No partial writes.
667          * Enough data must be present.
668          */
669         if (!smack_privileged(CAP_MAC_ADMIN))
670                 return -EPERM;
671
672         return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
673                                     SMK_FIXED24_FMT);
674 }
675
676 static const struct file_operations smk_load_ops = {
677         .open           = smk_open_load,
678         .read           = seq_read,
679         .llseek         = seq_lseek,
680         .write          = smk_write_load,
681         .release        = seq_release,
682 };
683
684 /**
685  * smk_cipso_doi - initialize the CIPSO domain
686  */
687 static void smk_cipso_doi(void)
688 {
689         int rc;
690         struct cipso_v4_doi *doip;
691         struct netlbl_audit nai;
692
693         smk_netlabel_audit_set(&nai);
694
695         rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
696         if (rc != 0)
697                 printk(KERN_WARNING "%s:%d remove rc = %d\n",
698                        __func__, __LINE__, rc);
699
700         doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
701         if (doip == NULL)
702                 panic("smack:  Failed to initialize cipso DOI.\n");
703         doip->map.std = NULL;
704         doip->doi = smk_cipso_doi_value;
705         doip->type = CIPSO_V4_MAP_PASS;
706         doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
707         for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
708                 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
709
710         rc = netlbl_cfg_cipsov4_add(doip, &nai);
711         if (rc != 0) {
712                 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
713                        __func__, __LINE__, rc);
714                 kfree(doip);
715                 return;
716         }
717         rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
718         if (rc != 0) {
719                 printk(KERN_WARNING "%s:%d map add rc = %d\n",
720                        __func__, __LINE__, rc);
721                 kfree(doip);
722                 return;
723         }
724 }
725
726 /**
727  * smk_unlbl_ambient - initialize the unlabeled domain
728  * @oldambient: previous domain string
729  */
730 static void smk_unlbl_ambient(char *oldambient)
731 {
732         int rc;
733         struct netlbl_audit nai;
734
735         smk_netlabel_audit_set(&nai);
736
737         if (oldambient != NULL) {
738                 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
739                 if (rc != 0)
740                         printk(KERN_WARNING "%s:%d remove rc = %d\n",
741                                __func__, __LINE__, rc);
742         }
743         if (smack_net_ambient == NULL)
744                 smack_net_ambient = &smack_known_floor;
745
746         rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
747                                       NULL, NULL, &nai);
748         if (rc != 0)
749                 printk(KERN_WARNING "%s:%d add rc = %d\n",
750                        __func__, __LINE__, rc);
751 }
752
753 /*
754  * Seq_file read operations for /smack/cipso
755  */
756
757 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
758 {
759         return smk_seq_start(s, pos, &smack_known_list);
760 }
761
762 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
763 {
764         return smk_seq_next(s, v, pos, &smack_known_list);
765 }
766
767 /*
768  * Print cipso labels in format:
769  * label level[/cat[,cat]]
770  */
771 static int cipso_seq_show(struct seq_file *s, void *v)
772 {
773         struct list_head  *list = v;
774         struct smack_known *skp =
775                 list_entry_rcu(list, struct smack_known, list);
776         struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
777         char sep = '/';
778         int i;
779
780         /*
781          * Don't show a label that could not have been set using
782          * /smack/cipso. This is in support of the notion that
783          * anything read from /smack/cipso ought to be writeable
784          * to /smack/cipso.
785          *
786          * /smack/cipso2 should be used instead.
787          */
788         if (strlen(skp->smk_known) >= SMK_LABELLEN)
789                 return 0;
790
791         seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
792
793         for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
794              i = netlbl_catmap_walk(cmp, i + 1)) {
795                 seq_printf(s, "%c%d", sep, i);
796                 sep = ',';
797         }
798
799         seq_putc(s, '\n');
800
801         return 0;
802 }
803
804 static const struct seq_operations cipso_seq_ops = {
805         .start = cipso_seq_start,
806         .next  = cipso_seq_next,
807         .show  = cipso_seq_show,
808         .stop  = smk_seq_stop,
809 };
810
811 /**
812  * smk_open_cipso - open() for /smack/cipso
813  * @inode: inode structure representing file
814  * @file: "cipso" file pointer
815  *
816  * Connect our cipso_seq_* operations with /smack/cipso
817  * file_operations
818  */
819 static int smk_open_cipso(struct inode *inode, struct file *file)
820 {
821         return seq_open(file, &cipso_seq_ops);
822 }
823
824 /**
825  * smk_set_cipso - do the work for write() for cipso and cipso2
826  * @file: file pointer, not actually used
827  * @buf: where to get the data from
828  * @count: bytes sent
829  * @ppos: where to start
830  * @format: /smack/cipso or /smack/cipso2
831  *
832  * Accepts only one cipso rule per write call.
833  * Returns number of bytes written or error code, as appropriate
834  */
835 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
836                                 size_t count, loff_t *ppos, int format)
837 {
838         struct smack_known *skp;
839         struct netlbl_lsm_secattr ncats;
840         char mapcatset[SMK_CIPSOLEN];
841         int maplevel;
842         unsigned int cat;
843         int catlen;
844         ssize_t rc = -EINVAL;
845         char *data = NULL;
846         char *rule;
847         int ret;
848         int i;
849
850         /*
851          * Must have privilege.
852          * No partial writes.
853          * Enough data must be present.
854          */
855         if (!smack_privileged(CAP_MAC_ADMIN))
856                 return -EPERM;
857         if (*ppos != 0)
858                 return -EINVAL;
859         if (format == SMK_FIXED24_FMT &&
860             (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
861                 return -EINVAL;
862
863         data = memdup_user_nul(buf, count);
864         if (IS_ERR(data))
865                 return PTR_ERR(data);
866
867         rule = data;
868         /*
869          * Only allow one writer at a time. Writes should be
870          * quite rare and small in any case.
871          */
872         mutex_lock(&smack_cipso_lock);
873
874         skp = smk_import_entry(rule, 0);
875         if (IS_ERR(skp)) {
876                 rc = PTR_ERR(skp);
877                 goto out;
878         }
879
880         if (format == SMK_FIXED24_FMT)
881                 rule += SMK_LABELLEN;
882         else
883                 rule += strlen(skp->smk_known) + 1;
884
885         ret = sscanf(rule, "%d", &maplevel);
886         if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
887                 goto out;
888
889         rule += SMK_DIGITLEN;
890         ret = sscanf(rule, "%d", &catlen);
891         if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
892                 goto out;
893
894         if (format == SMK_FIXED24_FMT &&
895             count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
896                 goto out;
897
898         memset(mapcatset, 0, sizeof(mapcatset));
899
900         for (i = 0; i < catlen; i++) {
901                 rule += SMK_DIGITLEN;
902                 ret = sscanf(rule, "%u", &cat);
903                 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
904                         goto out;
905
906                 smack_catset_bit(cat, mapcatset);
907         }
908
909         rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
910         if (rc >= 0) {
911                 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
912                 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
913                 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
914                 rc = count;
915         }
916
917 out:
918         mutex_unlock(&smack_cipso_lock);
919         kfree(data);
920         return rc;
921 }
922
923 /**
924  * smk_write_cipso - write() for /smack/cipso
925  * @file: file pointer, not actually used
926  * @buf: where to get the data from
927  * @count: bytes sent
928  * @ppos: where to start
929  *
930  * Accepts only one cipso rule per write call.
931  * Returns number of bytes written or error code, as appropriate
932  */
933 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
934                                size_t count, loff_t *ppos)
935 {
936         return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
937 }
938
939 static const struct file_operations smk_cipso_ops = {
940         .open           = smk_open_cipso,
941         .read           = seq_read,
942         .llseek         = seq_lseek,
943         .write          = smk_write_cipso,
944         .release        = seq_release,
945 };
946
947 /*
948  * Seq_file read operations for /smack/cipso2
949  */
950
951 /*
952  * Print cipso labels in format:
953  * label level[/cat[,cat]]
954  */
955 static int cipso2_seq_show(struct seq_file *s, void *v)
956 {
957         struct list_head  *list = v;
958         struct smack_known *skp =
959                 list_entry_rcu(list, struct smack_known, list);
960         struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
961         char sep = '/';
962         int i;
963
964         seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
965
966         for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
967              i = netlbl_catmap_walk(cmp, i + 1)) {
968                 seq_printf(s, "%c%d", sep, i);
969                 sep = ',';
970         }
971
972         seq_putc(s, '\n');
973
974         return 0;
975 }
976
977 static const struct seq_operations cipso2_seq_ops = {
978         .start = cipso_seq_start,
979         .next  = cipso_seq_next,
980         .show  = cipso2_seq_show,
981         .stop  = smk_seq_stop,
982 };
983
984 /**
985  * smk_open_cipso2 - open() for /smack/cipso2
986  * @inode: inode structure representing file
987  * @file: "cipso2" file pointer
988  *
989  * Connect our cipso_seq_* operations with /smack/cipso2
990  * file_operations
991  */
992 static int smk_open_cipso2(struct inode *inode, struct file *file)
993 {
994         return seq_open(file, &cipso2_seq_ops);
995 }
996
997 /**
998  * smk_write_cipso2 - write() for /smack/cipso2
999  * @file: file pointer, not actually used
1000  * @buf: where to get the data from
1001  * @count: bytes sent
1002  * @ppos: where to start
1003  *
1004  * Accepts only one cipso rule per write call.
1005  * Returns number of bytes written or error code, as appropriate
1006  */
1007 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1008                               size_t count, loff_t *ppos)
1009 {
1010         return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1011 }
1012
1013 static const struct file_operations smk_cipso2_ops = {
1014         .open           = smk_open_cipso2,
1015         .read           = seq_read,
1016         .llseek         = seq_lseek,
1017         .write          = smk_write_cipso2,
1018         .release        = seq_release,
1019 };
1020
1021 /*
1022  * Seq_file read operations for /smack/netlabel
1023  */
1024
1025 static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
1026 {
1027         return smk_seq_start(s, pos, &smk_net4addr_list);
1028 }
1029
1030 static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1031 {
1032         return smk_seq_next(s, v, pos, &smk_net4addr_list);
1033 }
1034
1035 /*
1036  * Print host/label pairs
1037  */
1038 static int net4addr_seq_show(struct seq_file *s, void *v)
1039 {
1040         struct list_head *list = v;
1041         struct smk_net4addr *skp =
1042                         list_entry_rcu(list, struct smk_net4addr, list);
1043         char *kp = SMACK_CIPSO_OPTION;
1044
1045         if (skp->smk_label != NULL)
1046                 kp = skp->smk_label->smk_known;
1047         seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
1048                         skp->smk_masks, kp);
1049
1050         return 0;
1051 }
1052
1053 static const struct seq_operations net4addr_seq_ops = {
1054         .start = net4addr_seq_start,
1055         .next  = net4addr_seq_next,
1056         .show  = net4addr_seq_show,
1057         .stop  = smk_seq_stop,
1058 };
1059
1060 /**
1061  * smk_open_net4addr - open() for /smack/netlabel
1062  * @inode: inode structure representing file
1063  * @file: "netlabel" file pointer
1064  *
1065  * Connect our net4addr_seq_* operations with /smack/netlabel
1066  * file_operations
1067  */
1068 static int smk_open_net4addr(struct inode *inode, struct file *file)
1069 {
1070         return seq_open(file, &net4addr_seq_ops);
1071 }
1072
1073 /**
1074  * smk_net4addr_insert
1075  * @new : netlabel to insert
1076  *
1077  * This helper insert netlabel in the smack_net4addrs list
1078  * sorted by netmask length (longest to smallest)
1079  * locked by &smk_net4addr_lock in smk_write_net4addr
1080  *
1081  */
1082 static void smk_net4addr_insert(struct smk_net4addr *new)
1083 {
1084         struct smk_net4addr *m;
1085         struct smk_net4addr *m_next;
1086
1087         if (list_empty(&smk_net4addr_list)) {
1088                 list_add_rcu(&new->list, &smk_net4addr_list);
1089                 return;
1090         }
1091
1092         m = list_entry_rcu(smk_net4addr_list.next,
1093                            struct smk_net4addr, list);
1094
1095         /* the comparison '>' is a bit hacky, but works */
1096         if (new->smk_masks > m->smk_masks) {
1097                 list_add_rcu(&new->list, &smk_net4addr_list);
1098                 return;
1099         }
1100
1101         list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
1102                 if (list_is_last(&m->list, &smk_net4addr_list)) {
1103                         list_add_rcu(&new->list, &m->list);
1104                         return;
1105                 }
1106                 m_next = list_entry_rcu(m->list.next,
1107                                         struct smk_net4addr, list);
1108                 if (new->smk_masks > m_next->smk_masks) {
1109                         list_add_rcu(&new->list, &m->list);
1110                         return;
1111                 }
1112         }
1113 }
1114
1115
1116 /**
1117  * smk_write_net4addr - write() for /smack/netlabel
1118  * @file: file pointer, not actually used
1119  * @buf: where to get the data from
1120  * @count: bytes sent
1121  * @ppos: where to start
1122  *
1123  * Accepts only one net4addr per write call.
1124  * Returns number of bytes written or error code, as appropriate
1125  */
1126 static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
1127                                 size_t count, loff_t *ppos)
1128 {
1129         struct smk_net4addr *snp;
1130         struct sockaddr_in newname;
1131         char *smack;
1132         struct smack_known *skp = NULL;
1133         char *data;
1134         char *host = (char *)&newname.sin_addr.s_addr;
1135         int rc;
1136         struct netlbl_audit audit_info;
1137         struct in_addr mask;
1138         unsigned int m;
1139         unsigned int masks;
1140         int found;
1141         u32 mask_bits = (1<<31);
1142         __be32 nsa;
1143         u32 temp_mask;
1144
1145         /*
1146          * Must have privilege.
1147          * No partial writes.
1148          * Enough data must be present.
1149          * "<addr/mask, as a.b.c.d/e><space><label>"
1150          * "<addr, as a.b.c.d><space><label>"
1151          */
1152         if (!smack_privileged(CAP_MAC_ADMIN))
1153                 return -EPERM;
1154         if (*ppos != 0)
1155                 return -EINVAL;
1156         if (count < SMK_NETLBLADDRMIN)
1157                 return -EINVAL;
1158
1159         data = memdup_user_nul(buf, count);
1160         if (IS_ERR(data))
1161                 return PTR_ERR(data);
1162
1163         smack = kzalloc(count + 1, GFP_KERNEL);
1164         if (smack == NULL) {
1165                 rc = -ENOMEM;
1166                 goto free_data_out;
1167         }
1168
1169         rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
1170                 &host[0], &host[1], &host[2], &host[3], &masks, smack);
1171         if (rc != 6) {
1172                 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1173                         &host[0], &host[1], &host[2], &host[3], smack);
1174                 if (rc != 5) {
1175                         rc = -EINVAL;
1176                         goto free_out;
1177                 }
1178                 m = BEBITS;
1179                 masks = 32;
1180         }
1181         if (masks > BEBITS) {
1182                 rc = -EINVAL;
1183                 goto free_out;
1184         }
1185
1186         /*
1187          * If smack begins with '-', it is an option, don't import it
1188          */
1189         if (smack[0] != '-') {
1190                 skp = smk_import_entry(smack, 0);
1191                 if (IS_ERR(skp)) {
1192                         rc = PTR_ERR(skp);
1193                         goto free_out;
1194                 }
1195         } else {
1196                 /*
1197                  * Only the -CIPSO option is supported for IPv4
1198                  */
1199                 if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
1200                         rc = -EINVAL;
1201                         goto free_out;
1202                 }
1203         }
1204
1205         for (m = masks, temp_mask = 0; m > 0; m--) {
1206                 temp_mask |= mask_bits;
1207                 mask_bits >>= 1;
1208         }
1209         mask.s_addr = cpu_to_be32(temp_mask);
1210
1211         newname.sin_addr.s_addr &= mask.s_addr;
1212         /*
1213          * Only allow one writer at a time. Writes should be
1214          * quite rare and small in any case.
1215          */
1216         mutex_lock(&smk_net4addr_lock);
1217
1218         nsa = newname.sin_addr.s_addr;
1219         /* try to find if the prefix is already in the list */
1220         found = 0;
1221         list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
1222                 if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
1223                         found = 1;
1224                         break;
1225                 }
1226         }
1227         smk_netlabel_audit_set(&audit_info);
1228
1229         if (found == 0) {
1230                 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1231                 if (snp == NULL)
1232                         rc = -ENOMEM;
1233                 else {
1234                         rc = 0;
1235                         snp->smk_host.s_addr = newname.sin_addr.s_addr;
1236                         snp->smk_mask.s_addr = mask.s_addr;
1237                         snp->smk_label = skp;
1238                         snp->smk_masks = masks;
1239                         smk_net4addr_insert(snp);
1240                 }
1241         } else {
1242                 /*
1243                  * Delete the unlabeled entry, only if the previous label
1244                  * wasn't the special CIPSO option
1245                  */
1246                 if (snp->smk_label != NULL)
1247                         rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1248                                         &snp->smk_host, &snp->smk_mask,
1249                                         PF_INET, &audit_info);
1250                 else
1251                         rc = 0;
1252                 snp->smk_label = skp;
1253         }
1254
1255         /*
1256          * Now tell netlabel about the single label nature of
1257          * this host so that incoming packets get labeled.
1258          * but only if we didn't get the special CIPSO option
1259          */
1260         if (rc == 0 && skp != NULL)
1261                 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1262                         &snp->smk_host, &snp->smk_mask, PF_INET,
1263                         snp->smk_label->smk_secid, &audit_info);
1264
1265         if (rc == 0)
1266                 rc = count;
1267
1268         mutex_unlock(&smk_net4addr_lock);
1269
1270 free_out:
1271         kfree(smack);
1272 free_data_out:
1273         kfree(data);
1274
1275         return rc;
1276 }
1277
1278 static const struct file_operations smk_net4addr_ops = {
1279         .open           = smk_open_net4addr,
1280         .read           = seq_read,
1281         .llseek         = seq_lseek,
1282         .write          = smk_write_net4addr,
1283         .release        = seq_release,
1284 };
1285
1286 #if IS_ENABLED(CONFIG_IPV6)
1287 /*
1288  * Seq_file read operations for /smack/netlabel6
1289  */
1290
1291 static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
1292 {
1293         return smk_seq_start(s, pos, &smk_net6addr_list);
1294 }
1295
1296 static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1297 {
1298         return smk_seq_next(s, v, pos, &smk_net6addr_list);
1299 }
1300
1301 /*
1302  * Print host/label pairs
1303  */
1304 static int net6addr_seq_show(struct seq_file *s, void *v)
1305 {
1306         struct list_head *list = v;
1307         struct smk_net6addr *skp =
1308                          list_entry(list, struct smk_net6addr, list);
1309
1310         if (skp->smk_label != NULL)
1311                 seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
1312                                 skp->smk_label->smk_known);
1313
1314         return 0;
1315 }
1316
1317 static const struct seq_operations net6addr_seq_ops = {
1318         .start = net6addr_seq_start,
1319         .next  = net6addr_seq_next,
1320         .show  = net6addr_seq_show,
1321         .stop  = smk_seq_stop,
1322 };
1323
1324 /**
1325  * smk_open_net6addr - open() for /smack/netlabel
1326  * @inode: inode structure representing file
1327  * @file: "netlabel" file pointer
1328  *
1329  * Connect our net6addr_seq_* operations with /smack/netlabel
1330  * file_operations
1331  */
1332 static int smk_open_net6addr(struct inode *inode, struct file *file)
1333 {
1334         return seq_open(file, &net6addr_seq_ops);
1335 }
1336
1337 /**
1338  * smk_net6addr_insert
1339  * @new : entry to insert
1340  *
1341  * This inserts an entry in the smack_net6addrs list
1342  * sorted by netmask length (longest to smallest)
1343  * locked by &smk_net6addr_lock in smk_write_net6addr
1344  *
1345  */
1346 static void smk_net6addr_insert(struct smk_net6addr *new)
1347 {
1348         struct smk_net6addr *m_next;
1349         struct smk_net6addr *m;
1350
1351         if (list_empty(&smk_net6addr_list)) {
1352                 list_add_rcu(&new->list, &smk_net6addr_list);
1353                 return;
1354         }
1355
1356         m = list_entry_rcu(smk_net6addr_list.next,
1357                            struct smk_net6addr, list);
1358
1359         if (new->smk_masks > m->smk_masks) {
1360                 list_add_rcu(&new->list, &smk_net6addr_list);
1361                 return;
1362         }
1363
1364         list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
1365                 if (list_is_last(&m->list, &smk_net6addr_list)) {
1366                         list_add_rcu(&new->list, &m->list);
1367                         return;
1368                 }
1369                 m_next = list_entry_rcu(m->list.next,
1370                                         struct smk_net6addr, list);
1371                 if (new->smk_masks > m_next->smk_masks) {
1372                         list_add_rcu(&new->list, &m->list);
1373                         return;
1374                 }
1375         }
1376 }
1377
1378
1379 /**
1380  * smk_write_net6addr - write() for /smack/netlabel
1381  * @file: file pointer, not actually used
1382  * @buf: where to get the data from
1383  * @count: bytes sent
1384  * @ppos: where to start
1385  *
1386  * Accepts only one net6addr per write call.
1387  * Returns number of bytes written or error code, as appropriate
1388  */
1389 static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
1390                                 size_t count, loff_t *ppos)
1391 {
1392         struct smk_net6addr *snp;
1393         struct in6_addr newname;
1394         struct in6_addr fullmask;
1395         struct smack_known *skp = NULL;
1396         char *smack;
1397         char *data;
1398         int rc = 0;
1399         int found = 0;
1400         int i;
1401         unsigned int scanned[8];
1402         unsigned int m;
1403         unsigned int mask = 128;
1404
1405         /*
1406          * Must have privilege.
1407          * No partial writes.
1408          * Enough data must be present.
1409          * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
1410          * "<addr, as a:b:c:d:e:f:g:h><space><label>"
1411          */
1412         if (!smack_privileged(CAP_MAC_ADMIN))
1413                 return -EPERM;
1414         if (*ppos != 0)
1415                 return -EINVAL;
1416         if (count < SMK_NETLBLADDRMIN)
1417                 return -EINVAL;
1418
1419         data = memdup_user_nul(buf, count);
1420         if (IS_ERR(data))
1421                 return PTR_ERR(data);
1422
1423         smack = kzalloc(count + 1, GFP_KERNEL);
1424         if (smack == NULL) {
1425                 rc = -ENOMEM;
1426                 goto free_data_out;
1427         }
1428
1429         i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
1430                         &scanned[0], &scanned[1], &scanned[2], &scanned[3],
1431                         &scanned[4], &scanned[5], &scanned[6], &scanned[7],
1432                         &mask, smack);
1433         if (i != 10) {
1434                 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
1435                                 &scanned[0], &scanned[1], &scanned[2],
1436                                 &scanned[3], &scanned[4], &scanned[5],
1437                                 &scanned[6], &scanned[7], smack);
1438                 if (i != 9) {
1439                         rc = -EINVAL;
1440                         goto free_out;
1441                 }
1442         }
1443         if (mask > 128) {
1444                 rc = -EINVAL;
1445                 goto free_out;
1446         }
1447         for (i = 0; i < 8; i++) {
1448                 if (scanned[i] > 0xffff) {
1449                         rc = -EINVAL;
1450                         goto free_out;
1451                 }
1452                 newname.s6_addr16[i] = htons(scanned[i]);
1453         }
1454
1455         /*
1456          * If smack begins with '-', it is an option, don't import it
1457          */
1458         if (smack[0] != '-') {
1459                 skp = smk_import_entry(smack, 0);
1460                 if (IS_ERR(skp)) {
1461                         rc = PTR_ERR(skp);
1462                         goto free_out;
1463                 }
1464         } else {
1465                 /*
1466                  * Only -DELETE is supported for IPv6
1467                  */
1468                 if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
1469                         rc = -EINVAL;
1470                         goto free_out;
1471                 }
1472         }
1473
1474         for (i = 0, m = mask; i < 8; i++) {
1475                 if (m >= 16) {
1476                         fullmask.s6_addr16[i] = 0xffff;
1477                         m -= 16;
1478                 } else if (m > 0) {
1479                         fullmask.s6_addr16[i] = (1 << m) - 1;
1480                         m = 0;
1481                 } else
1482                         fullmask.s6_addr16[i] = 0;
1483                 newname.s6_addr16[i] &= fullmask.s6_addr16[i];
1484         }
1485
1486         /*
1487          * Only allow one writer at a time. Writes should be
1488          * quite rare and small in any case.
1489          */
1490         mutex_lock(&smk_net6addr_lock);
1491         /*
1492          * Try to find the prefix in the list
1493          */
1494         list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
1495                 if (mask != snp->smk_masks)
1496                         continue;
1497                 for (found = 1, i = 0; i < 8; i++) {
1498                         if (newname.s6_addr16[i] !=
1499                             snp->smk_host.s6_addr16[i]) {
1500                                 found = 0;
1501                                 break;
1502                         }
1503                 }
1504                 if (found == 1)
1505                         break;
1506         }
1507         if (found == 0) {
1508                 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1509                 if (snp == NULL)
1510                         rc = -ENOMEM;
1511                 else {
1512                         snp->smk_host = newname;
1513                         snp->smk_mask = fullmask;
1514                         snp->smk_masks = mask;
1515                         snp->smk_label = skp;
1516                         smk_net6addr_insert(snp);
1517                 }
1518         } else {
1519                 snp->smk_label = skp;
1520         }
1521
1522         if (rc == 0)
1523                 rc = count;
1524
1525         mutex_unlock(&smk_net6addr_lock);
1526
1527 free_out:
1528         kfree(smack);
1529 free_data_out:
1530         kfree(data);
1531
1532         return rc;
1533 }
1534
1535 static const struct file_operations smk_net6addr_ops = {
1536         .open           = smk_open_net6addr,
1537         .read           = seq_read,
1538         .llseek         = seq_lseek,
1539         .write          = smk_write_net6addr,
1540         .release        = seq_release,
1541 };
1542 #endif /* CONFIG_IPV6 */
1543
1544 /**
1545  * smk_read_doi - read() for /smack/doi
1546  * @filp: file pointer, not actually used
1547  * @buf: where to put the result
1548  * @count: maximum to send along
1549  * @ppos: where to start
1550  *
1551  * Returns number of bytes read or error code, as appropriate
1552  */
1553 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1554                             size_t count, loff_t *ppos)
1555 {
1556         char temp[80];
1557         ssize_t rc;
1558
1559         if (*ppos != 0)
1560                 return 0;
1561
1562         sprintf(temp, "%d", smk_cipso_doi_value);
1563         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1564
1565         return rc;
1566 }
1567
1568 /**
1569  * smk_write_doi - write() for /smack/doi
1570  * @file: file pointer, not actually used
1571  * @buf: where to get the data from
1572  * @count: bytes sent
1573  * @ppos: where to start
1574  *
1575  * Returns number of bytes written or error code, as appropriate
1576  */
1577 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1578                              size_t count, loff_t *ppos)
1579 {
1580         char temp[80];
1581         int i;
1582
1583         if (!smack_privileged(CAP_MAC_ADMIN))
1584                 return -EPERM;
1585
1586         if (count >= sizeof(temp) || count == 0)
1587                 return -EINVAL;
1588
1589         if (copy_from_user(temp, buf, count) != 0)
1590                 return -EFAULT;
1591
1592         temp[count] = '\0';
1593
1594         if (sscanf(temp, "%d", &i) != 1)
1595                 return -EINVAL;
1596
1597         smk_cipso_doi_value = i;
1598
1599         smk_cipso_doi();
1600
1601         return count;
1602 }
1603
1604 static const struct file_operations smk_doi_ops = {
1605         .read           = smk_read_doi,
1606         .write          = smk_write_doi,
1607         .llseek         = default_llseek,
1608 };
1609
1610 /**
1611  * smk_read_direct - read() for /smack/direct
1612  * @filp: file pointer, not actually used
1613  * @buf: where to put the result
1614  * @count: maximum to send along
1615  * @ppos: where to start
1616  *
1617  * Returns number of bytes read or error code, as appropriate
1618  */
1619 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1620                                size_t count, loff_t *ppos)
1621 {
1622         char temp[80];
1623         ssize_t rc;
1624
1625         if (*ppos != 0)
1626                 return 0;
1627
1628         sprintf(temp, "%d", smack_cipso_direct);
1629         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1630
1631         return rc;
1632 }
1633
1634 /**
1635  * smk_write_direct - write() for /smack/direct
1636  * @file: file pointer, not actually used
1637  * @buf: where to get the data from
1638  * @count: bytes sent
1639  * @ppos: where to start
1640  *
1641  * Returns number of bytes written or error code, as appropriate
1642  */
1643 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1644                                 size_t count, loff_t *ppos)
1645 {
1646         struct smack_known *skp;
1647         char temp[80];
1648         int i;
1649
1650         if (!smack_privileged(CAP_MAC_ADMIN))
1651                 return -EPERM;
1652
1653         if (count >= sizeof(temp) || count == 0)
1654                 return -EINVAL;
1655
1656         if (copy_from_user(temp, buf, count) != 0)
1657                 return -EFAULT;
1658
1659         temp[count] = '\0';
1660
1661         if (sscanf(temp, "%d", &i) != 1)
1662                 return -EINVAL;
1663
1664         /*
1665          * Don't do anything if the value hasn't actually changed.
1666          * If it is changing reset the level on entries that were
1667          * set up to be direct when they were created.
1668          */
1669         if (smack_cipso_direct != i) {
1670                 mutex_lock(&smack_known_lock);
1671                 list_for_each_entry_rcu(skp, &smack_known_list, list)
1672                         if (skp->smk_netlabel.attr.mls.lvl ==
1673                             smack_cipso_direct)
1674                                 skp->smk_netlabel.attr.mls.lvl = i;
1675                 smack_cipso_direct = i;
1676                 mutex_unlock(&smack_known_lock);
1677         }
1678
1679         return count;
1680 }
1681
1682 static const struct file_operations smk_direct_ops = {
1683         .read           = smk_read_direct,
1684         .write          = smk_write_direct,
1685         .llseek         = default_llseek,
1686 };
1687
1688 /**
1689  * smk_read_mapped - read() for /smack/mapped
1690  * @filp: file pointer, not actually used
1691  * @buf: where to put the result
1692  * @count: maximum to send along
1693  * @ppos: where to start
1694  *
1695  * Returns number of bytes read or error code, as appropriate
1696  */
1697 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1698                                size_t count, loff_t *ppos)
1699 {
1700         char temp[80];
1701         ssize_t rc;
1702
1703         if (*ppos != 0)
1704                 return 0;
1705
1706         sprintf(temp, "%d", smack_cipso_mapped);
1707         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1708
1709         return rc;
1710 }
1711
1712 /**
1713  * smk_write_mapped - write() for /smack/mapped
1714  * @file: file pointer, not actually used
1715  * @buf: where to get the data from
1716  * @count: bytes sent
1717  * @ppos: where to start
1718  *
1719  * Returns number of bytes written or error code, as appropriate
1720  */
1721 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1722                                 size_t count, loff_t *ppos)
1723 {
1724         struct smack_known *skp;
1725         char temp[80];
1726         int i;
1727
1728         if (!smack_privileged(CAP_MAC_ADMIN))
1729                 return -EPERM;
1730
1731         if (count >= sizeof(temp) || count == 0)
1732                 return -EINVAL;
1733
1734         if (copy_from_user(temp, buf, count) != 0)
1735                 return -EFAULT;
1736
1737         temp[count] = '\0';
1738
1739         if (sscanf(temp, "%d", &i) != 1)
1740                 return -EINVAL;
1741
1742         /*
1743          * Don't do anything if the value hasn't actually changed.
1744          * If it is changing reset the level on entries that were
1745          * set up to be mapped when they were created.
1746          */
1747         if (smack_cipso_mapped != i) {
1748                 mutex_lock(&smack_known_lock);
1749                 list_for_each_entry_rcu(skp, &smack_known_list, list)
1750                         if (skp->smk_netlabel.attr.mls.lvl ==
1751                             smack_cipso_mapped)
1752                                 skp->smk_netlabel.attr.mls.lvl = i;
1753                 smack_cipso_mapped = i;
1754                 mutex_unlock(&smack_known_lock);
1755         }
1756
1757         return count;
1758 }
1759
1760 static const struct file_operations smk_mapped_ops = {
1761         .read           = smk_read_mapped,
1762         .write          = smk_write_mapped,
1763         .llseek         = default_llseek,
1764 };
1765
1766 /**
1767  * smk_read_ambient - read() for /smack/ambient
1768  * @filp: file pointer, not actually used
1769  * @buf: where to put the result
1770  * @cn: maximum to send along
1771  * @ppos: where to start
1772  *
1773  * Returns number of bytes read or error code, as appropriate
1774  */
1775 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1776                                 size_t cn, loff_t *ppos)
1777 {
1778         ssize_t rc;
1779         int asize;
1780
1781         if (*ppos != 0)
1782                 return 0;
1783         /*
1784          * Being careful to avoid a problem in the case where
1785          * smack_net_ambient gets changed in midstream.
1786          */
1787         mutex_lock(&smack_ambient_lock);
1788
1789         asize = strlen(smack_net_ambient->smk_known) + 1;
1790
1791         if (cn >= asize)
1792                 rc = simple_read_from_buffer(buf, cn, ppos,
1793                                              smack_net_ambient->smk_known,
1794                                              asize);
1795         else
1796                 rc = -EINVAL;
1797
1798         mutex_unlock(&smack_ambient_lock);
1799
1800         return rc;
1801 }
1802
1803 /**
1804  * smk_write_ambient - write() for /smack/ambient
1805  * @file: file pointer, not actually used
1806  * @buf: where to get the data from
1807  * @count: bytes sent
1808  * @ppos: where to start
1809  *
1810  * Returns number of bytes written or error code, as appropriate
1811  */
1812 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1813                                  size_t count, loff_t *ppos)
1814 {
1815         struct smack_known *skp;
1816         char *oldambient;
1817         char *data;
1818         int rc = count;
1819
1820         if (!smack_privileged(CAP_MAC_ADMIN))
1821                 return -EPERM;
1822
1823         data = memdup_user_nul(buf, count);
1824         if (IS_ERR(data))
1825                 return PTR_ERR(data);
1826
1827         skp = smk_import_entry(data, count);
1828         if (IS_ERR(skp)) {
1829                 rc = PTR_ERR(skp);
1830                 goto out;
1831         }
1832
1833         mutex_lock(&smack_ambient_lock);
1834
1835         oldambient = smack_net_ambient->smk_known;
1836         smack_net_ambient = skp;
1837         smk_unlbl_ambient(oldambient);
1838
1839         mutex_unlock(&smack_ambient_lock);
1840
1841 out:
1842         kfree(data);
1843         return rc;
1844 }
1845
1846 static const struct file_operations smk_ambient_ops = {
1847         .read           = smk_read_ambient,
1848         .write          = smk_write_ambient,
1849         .llseek         = default_llseek,
1850 };
1851
1852 /*
1853  * Seq_file operations for /smack/onlycap
1854  */
1855 static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
1856 {
1857         return smk_seq_start(s, pos, &smack_onlycap_list);
1858 }
1859
1860 static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
1861 {
1862         return smk_seq_next(s, v, pos, &smack_onlycap_list);
1863 }
1864
1865 static int onlycap_seq_show(struct seq_file *s, void *v)
1866 {
1867         struct list_head *list = v;
1868         struct smack_known_list_elem *sklep =
1869                 list_entry_rcu(list, struct smack_known_list_elem, list);
1870
1871         seq_puts(s, sklep->smk_label->smk_known);
1872         seq_putc(s, ' ');
1873
1874         return 0;
1875 }
1876
1877 static const struct seq_operations onlycap_seq_ops = {
1878         .start = onlycap_seq_start,
1879         .next  = onlycap_seq_next,
1880         .show  = onlycap_seq_show,
1881         .stop  = smk_seq_stop,
1882 };
1883
1884 static int smk_open_onlycap(struct inode *inode, struct file *file)
1885 {
1886         return seq_open(file, &onlycap_seq_ops);
1887 }
1888
1889 /**
1890  * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
1891  * The caller must hold appropriate mutex to prevent concurrent modifications
1892  * to the public list.
1893  * Private list is assumed to be not accessible to other threads yet.
1894  *
1895  * @public: public list
1896  * @private: private list
1897  */
1898 static void smk_list_swap_rcu(struct list_head *public,
1899                               struct list_head *private)
1900 {
1901         struct list_head *first, *last;
1902
1903         if (list_empty(public)) {
1904                 list_splice_init_rcu(private, public, synchronize_rcu);
1905         } else {
1906                 /* Remember public list before replacing it */
1907                 first = public->next;
1908                 last = public->prev;
1909
1910                 /* Publish private list in place of public in RCU-safe way */
1911                 private->prev->next = public;
1912                 private->next->prev = public;
1913                 rcu_assign_pointer(public->next, private->next);
1914                 public->prev = private->prev;
1915
1916                 synchronize_rcu();
1917
1918                 /* When all readers are done with the old public list,
1919                  * attach it in place of private */
1920                 private->next = first;
1921                 private->prev = last;
1922                 first->prev = private;
1923                 last->next = private;
1924         }
1925 }
1926
1927 /**
1928  * smk_parse_label_list - parse list of Smack labels, separated by spaces
1929  *
1930  * @data: the string to parse
1931  * @private: destination list
1932  *
1933  * Returns zero on success or error code, as appropriate
1934  */
1935 static int smk_parse_label_list(char *data, struct list_head *list)
1936 {
1937         char *tok;
1938         struct smack_known *skp;
1939         struct smack_known_list_elem *sklep;
1940
1941         while ((tok = strsep(&data, " ")) != NULL) {
1942                 if (!*tok)
1943                         continue;
1944
1945                 skp = smk_import_entry(tok, 0);
1946                 if (IS_ERR(skp))
1947                         return PTR_ERR(skp);
1948
1949                 sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
1950                 if (sklep == NULL)
1951                         return -ENOMEM;
1952
1953                 sklep->smk_label = skp;
1954                 list_add(&sklep->list, list);
1955         }
1956
1957         return 0;
1958 }
1959
1960 /**
1961  * smk_destroy_label_list - destroy a list of smack_known_list_elem
1962  * @head: header pointer of the list to destroy
1963  */
1964 void smk_destroy_label_list(struct list_head *list)
1965 {
1966         struct smack_known_list_elem *sklep;
1967         struct smack_known_list_elem *sklep2;
1968
1969         list_for_each_entry_safe(sklep, sklep2, list, list)
1970                 kfree(sklep);
1971
1972         INIT_LIST_HEAD(list);
1973 }
1974
1975 /**
1976  * smk_write_onlycap - write() for smackfs/onlycap
1977  * @file: file pointer, not actually used
1978  * @buf: where to get the data from
1979  * @count: bytes sent
1980  * @ppos: where to start
1981  *
1982  * Returns number of bytes written or error code, as appropriate
1983  */
1984 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1985                                  size_t count, loff_t *ppos)
1986 {
1987         char *data;
1988         LIST_HEAD(list_tmp);
1989         int rc;
1990
1991         if (!smack_privileged(CAP_MAC_ADMIN))
1992                 return -EPERM;
1993
1994         data = memdup_user_nul(buf, count);
1995         if (IS_ERR(data))
1996                 return PTR_ERR(data);
1997
1998         rc = smk_parse_label_list(data, &list_tmp);
1999         kfree(data);
2000
2001         /*
2002          * Clear the smack_onlycap on invalid label errors. This means
2003          * that we can pass a null string to unset the onlycap value.
2004          *
2005          * Importing will also reject a label beginning with '-',
2006          * so "-usecapabilities" will also work.
2007          *
2008          * But do so only on invalid label, not on system errors.
2009          * The invalid label must be first to count as clearing attempt.
2010          */
2011         if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2012                 mutex_lock(&smack_onlycap_lock);
2013                 smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
2014                 mutex_unlock(&smack_onlycap_lock);
2015                 rc = count;
2016         }
2017
2018         smk_destroy_label_list(&list_tmp);
2019
2020         return rc;
2021 }
2022
2023 static const struct file_operations smk_onlycap_ops = {
2024         .open           = smk_open_onlycap,
2025         .read           = seq_read,
2026         .write          = smk_write_onlycap,
2027         .llseek         = seq_lseek,
2028         .release        = seq_release,
2029 };
2030
2031 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2032 /**
2033  * smk_read_unconfined - read() for smackfs/unconfined
2034  * @filp: file pointer, not actually used
2035  * @buf: where to put the result
2036  * @cn: maximum to send along
2037  * @ppos: where to start
2038  *
2039  * Returns number of bytes read or error code, as appropriate
2040  */
2041 static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
2042                                         size_t cn, loff_t *ppos)
2043 {
2044         char *smack = "";
2045         ssize_t rc = -EINVAL;
2046         int asize;
2047
2048         if (*ppos != 0)
2049                 return 0;
2050
2051         if (smack_unconfined != NULL)
2052                 smack = smack_unconfined->smk_known;
2053
2054         asize = strlen(smack) + 1;
2055
2056         if (cn >= asize)
2057                 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
2058
2059         return rc;
2060 }
2061
2062 /**
2063  * smk_write_unconfined - write() for smackfs/unconfined
2064  * @file: file pointer, not actually used
2065  * @buf: where to get the data from
2066  * @count: bytes sent
2067  * @ppos: where to start
2068  *
2069  * Returns number of bytes written or error code, as appropriate
2070  */
2071 static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
2072                                         size_t count, loff_t *ppos)
2073 {
2074         char *data;
2075         struct smack_known *skp;
2076         int rc = count;
2077
2078         if (!smack_privileged(CAP_MAC_ADMIN))
2079                 return -EPERM;
2080
2081         data = memdup_user_nul(buf, count);
2082         if (IS_ERR(data))
2083                 return PTR_ERR(data);
2084
2085         /*
2086          * Clear the smack_unconfined on invalid label errors. This means
2087          * that we can pass a null string to unset the unconfined value.
2088          *
2089          * Importing will also reject a label beginning with '-',
2090          * so "-confine" will also work.
2091          *
2092          * But do so only on invalid label, not on system errors.
2093          */
2094         skp = smk_import_entry(data, count);
2095         if (PTR_ERR(skp) == -EINVAL)
2096                 skp = NULL;
2097         else if (IS_ERR(skp)) {
2098                 rc = PTR_ERR(skp);
2099                 goto freeout;
2100         }
2101
2102         smack_unconfined = skp;
2103
2104 freeout:
2105         kfree(data);
2106         return rc;
2107 }
2108
2109 static const struct file_operations smk_unconfined_ops = {
2110         .read           = smk_read_unconfined,
2111         .write          = smk_write_unconfined,
2112         .llseek         = default_llseek,
2113 };
2114 #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
2115
2116 /**
2117  * smk_read_logging - read() for /smack/logging
2118  * @filp: file pointer, not actually used
2119  * @buf: where to put the result
2120  * @cn: maximum to send along
2121  * @ppos: where to start
2122  *
2123  * Returns number of bytes read or error code, as appropriate
2124  */
2125 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
2126                                 size_t count, loff_t *ppos)
2127 {
2128         char temp[32];
2129         ssize_t rc;
2130
2131         if (*ppos != 0)
2132                 return 0;
2133
2134         sprintf(temp, "%d\n", log_policy);
2135         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2136         return rc;
2137 }
2138
2139 /**
2140  * smk_write_logging - write() for /smack/logging
2141  * @file: file pointer, not actually used
2142  * @buf: where to get the data from
2143  * @count: bytes sent
2144  * @ppos: where to start
2145  *
2146  * Returns number of bytes written or error code, as appropriate
2147  */
2148 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
2149                                 size_t count, loff_t *ppos)
2150 {
2151         char temp[32];
2152         int i;
2153
2154         if (!smack_privileged(CAP_MAC_ADMIN))
2155                 return -EPERM;
2156
2157         if (count >= sizeof(temp) || count == 0)
2158                 return -EINVAL;
2159
2160         if (copy_from_user(temp, buf, count) != 0)
2161                 return -EFAULT;
2162
2163         temp[count] = '\0';
2164
2165         if (sscanf(temp, "%d", &i) != 1)
2166                 return -EINVAL;
2167         if (i < 0 || i > 3)
2168                 return -EINVAL;
2169         log_policy = i;
2170         return count;
2171 }
2172
2173
2174
2175 static const struct file_operations smk_logging_ops = {
2176         .read           = smk_read_logging,
2177         .write          = smk_write_logging,
2178         .llseek         = default_llseek,
2179 };
2180
2181 /*
2182  * Seq_file read operations for /smack/load-self
2183  */
2184
2185 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
2186 {
2187         struct task_smack *tsp = smack_cred(current_cred());
2188
2189         return smk_seq_start(s, pos, &tsp->smk_rules);
2190 }
2191
2192 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2193 {
2194         struct task_smack *tsp = smack_cred(current_cred());
2195
2196         return smk_seq_next(s, v, pos, &tsp->smk_rules);
2197 }
2198
2199 static int load_self_seq_show(struct seq_file *s, void *v)
2200 {
2201         struct list_head *list = v;
2202         struct smack_rule *srp =
2203                 list_entry_rcu(list, struct smack_rule, list);
2204
2205         smk_rule_show(s, srp, SMK_LABELLEN);
2206
2207         return 0;
2208 }
2209
2210 static const struct seq_operations load_self_seq_ops = {
2211         .start = load_self_seq_start,
2212         .next  = load_self_seq_next,
2213         .show  = load_self_seq_show,
2214         .stop  = smk_seq_stop,
2215 };
2216
2217
2218 /**
2219  * smk_open_load_self - open() for /smack/load-self2
2220  * @inode: inode structure representing file
2221  * @file: "load" file pointer
2222  *
2223  * For reading, use load_seq_* seq_file reading operations.
2224  */
2225 static int smk_open_load_self(struct inode *inode, struct file *file)
2226 {
2227         return seq_open(file, &load_self_seq_ops);
2228 }
2229
2230 /**
2231  * smk_write_load_self - write() for /smack/load-self
2232  * @file: file pointer, not actually used
2233  * @buf: where to get the data from
2234  * @count: bytes sent
2235  * @ppos: where to start - must be 0
2236  *
2237  */
2238 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
2239                               size_t count, loff_t *ppos)
2240 {
2241         struct task_smack *tsp = smack_cred(current_cred());
2242
2243         return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2244                                     &tsp->smk_rules_lock, SMK_FIXED24_FMT);
2245 }
2246
2247 static const struct file_operations smk_load_self_ops = {
2248         .open           = smk_open_load_self,
2249         .read           = seq_read,
2250         .llseek         = seq_lseek,
2251         .write          = smk_write_load_self,
2252         .release        = seq_release,
2253 };
2254
2255 /**
2256  * smk_user_access - handle access check transaction
2257  * @file: file pointer
2258  * @buf: data from user space
2259  * @count: bytes sent
2260  * @ppos: where to start - must be 0
2261  */
2262 static ssize_t smk_user_access(struct file *file, const char __user *buf,
2263                                 size_t count, loff_t *ppos, int format)
2264 {
2265         struct smack_parsed_rule rule;
2266         char *data;
2267         int res;
2268
2269         data = simple_transaction_get(file, buf, count);
2270         if (IS_ERR(data))
2271                 return PTR_ERR(data);
2272
2273         if (format == SMK_FIXED24_FMT) {
2274                 if (count < SMK_LOADLEN)
2275                         return -EINVAL;
2276                 res = smk_parse_rule(data, &rule, 0);
2277         } else {
2278                 /*
2279                  * simple_transaction_get() returns null-terminated data
2280                  */
2281                 res = smk_parse_long_rule(data, &rule, 0, 3);
2282         }
2283
2284         if (res >= 0)
2285                 res = smk_access(rule.smk_subject, rule.smk_object,
2286                                  rule.smk_access1, NULL);
2287         else if (res != -ENOENT)
2288                 return res;
2289
2290         /*
2291          * smk_access() can return a value > 0 in the "bringup" case.
2292          */
2293         data[0] = res >= 0 ? '1' : '0';
2294         data[1] = '\0';
2295
2296         simple_transaction_set(file, 2);
2297
2298         if (format == SMK_FIXED24_FMT)
2299                 return SMK_LOADLEN;
2300         return count;
2301 }
2302
2303 /**
2304  * smk_write_access - handle access check transaction
2305  * @file: file pointer
2306  * @buf: data from user space
2307  * @count: bytes sent
2308  * @ppos: where to start - must be 0
2309  */
2310 static ssize_t smk_write_access(struct file *file, const char __user *buf,
2311                                 size_t count, loff_t *ppos)
2312 {
2313         return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
2314 }
2315
2316 static const struct file_operations smk_access_ops = {
2317         .write          = smk_write_access,
2318         .read           = simple_transaction_read,
2319         .release        = simple_transaction_release,
2320         .llseek         = generic_file_llseek,
2321 };
2322
2323
2324 /*
2325  * Seq_file read operations for /smack/load2
2326  */
2327
2328 static int load2_seq_show(struct seq_file *s, void *v)
2329 {
2330         struct list_head *list = v;
2331         struct smack_rule *srp;
2332         struct smack_known *skp =
2333                 list_entry_rcu(list, struct smack_known, list);
2334
2335         list_for_each_entry_rcu(srp, &skp->smk_rules, list)
2336                 smk_rule_show(s, srp, SMK_LONGLABEL);
2337
2338         return 0;
2339 }
2340
2341 static const struct seq_operations load2_seq_ops = {
2342         .start = load2_seq_start,
2343         .next  = load2_seq_next,
2344         .show  = load2_seq_show,
2345         .stop  = smk_seq_stop,
2346 };
2347
2348 /**
2349  * smk_open_load2 - open() for /smack/load2
2350  * @inode: inode structure representing file
2351  * @file: "load2" file pointer
2352  *
2353  * For reading, use load2_seq_* seq_file reading operations.
2354  */
2355 static int smk_open_load2(struct inode *inode, struct file *file)
2356 {
2357         return seq_open(file, &load2_seq_ops);
2358 }
2359
2360 /**
2361  * smk_write_load2 - write() for /smack/load2
2362  * @file: file pointer, not actually used
2363  * @buf: where to get the data from
2364  * @count: bytes sent
2365  * @ppos: where to start - must be 0
2366  *
2367  */
2368 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2369                                 size_t count, loff_t *ppos)
2370 {
2371         /*
2372          * Must have privilege.
2373          */
2374         if (!smack_privileged(CAP_MAC_ADMIN))
2375                 return -EPERM;
2376
2377         return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2378                                     SMK_LONG_FMT);
2379 }
2380
2381 static const struct file_operations smk_load2_ops = {
2382         .open           = smk_open_load2,
2383         .read           = seq_read,
2384         .llseek         = seq_lseek,
2385         .write          = smk_write_load2,
2386         .release        = seq_release,
2387 };
2388
2389 /*
2390  * Seq_file read operations for /smack/load-self2
2391  */
2392
2393 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2394 {
2395         struct task_smack *tsp = smack_cred(current_cred());
2396
2397         return smk_seq_start(s, pos, &tsp->smk_rules);
2398 }
2399
2400 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2401 {
2402         struct task_smack *tsp = smack_cred(current_cred());
2403
2404         return smk_seq_next(s, v, pos, &tsp->smk_rules);
2405 }
2406
2407 static int load_self2_seq_show(struct seq_file *s, void *v)
2408 {
2409         struct list_head *list = v;
2410         struct smack_rule *srp =
2411                 list_entry_rcu(list, struct smack_rule, list);
2412
2413         smk_rule_show(s, srp, SMK_LONGLABEL);
2414
2415         return 0;
2416 }
2417
2418 static const struct seq_operations load_self2_seq_ops = {
2419         .start = load_self2_seq_start,
2420         .next  = load_self2_seq_next,
2421         .show  = load_self2_seq_show,
2422         .stop  = smk_seq_stop,
2423 };
2424
2425 /**
2426  * smk_open_load_self2 - open() for /smack/load-self2
2427  * @inode: inode structure representing file
2428  * @file: "load" file pointer
2429  *
2430  * For reading, use load_seq_* seq_file reading operations.
2431  */
2432 static int smk_open_load_self2(struct inode *inode, struct file *file)
2433 {
2434         return seq_open(file, &load_self2_seq_ops);
2435 }
2436
2437 /**
2438  * smk_write_load_self2 - write() for /smack/load-self2
2439  * @file: file pointer, not actually used
2440  * @buf: where to get the data from
2441  * @count: bytes sent
2442  * @ppos: where to start - must be 0
2443  *
2444  */
2445 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2446                               size_t count, loff_t *ppos)
2447 {
2448         struct task_smack *tsp = smack_cred(current_cred());
2449
2450         return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2451                                     &tsp->smk_rules_lock, SMK_LONG_FMT);
2452 }
2453
2454 static const struct file_operations smk_load_self2_ops = {
2455         .open           = smk_open_load_self2,
2456         .read           = seq_read,
2457         .llseek         = seq_lseek,
2458         .write          = smk_write_load_self2,
2459         .release        = seq_release,
2460 };
2461
2462 /**
2463  * smk_write_access2 - handle access check transaction
2464  * @file: file pointer
2465  * @buf: data from user space
2466  * @count: bytes sent
2467  * @ppos: where to start - must be 0
2468  */
2469 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2470                                         size_t count, loff_t *ppos)
2471 {
2472         return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2473 }
2474
2475 static const struct file_operations smk_access2_ops = {
2476         .write          = smk_write_access2,
2477         .read           = simple_transaction_read,
2478         .release        = simple_transaction_release,
2479         .llseek         = generic_file_llseek,
2480 };
2481
2482 /**
2483  * smk_write_revoke_subj - write() for /smack/revoke-subject
2484  * @file: file pointer
2485  * @buf: data from user space
2486  * @count: bytes sent
2487  * @ppos: where to start - must be 0
2488  */
2489 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2490                                 size_t count, loff_t *ppos)
2491 {
2492         char *data;
2493         const char *cp;
2494         struct smack_known *skp;
2495         struct smack_rule *sp;
2496         struct list_head *rule_list;
2497         struct mutex *rule_lock;
2498         int rc = count;
2499
2500         if (*ppos != 0)
2501                 return -EINVAL;
2502
2503         if (!smack_privileged(CAP_MAC_ADMIN))
2504                 return -EPERM;
2505
2506         if (count == 0 || count > SMK_LONGLABEL)
2507                 return -EINVAL;
2508
2509         data = memdup_user(buf, count);
2510         if (IS_ERR(data))
2511                 return PTR_ERR(data);
2512
2513         cp = smk_parse_smack(data, count);
2514         if (IS_ERR(cp)) {
2515                 rc = PTR_ERR(cp);
2516                 goto out_data;
2517         }
2518
2519         skp = smk_find_entry(cp);
2520         if (skp == NULL)
2521                 goto out_cp;
2522
2523         rule_list = &skp->smk_rules;
2524         rule_lock = &skp->smk_rules_lock;
2525
2526         mutex_lock(rule_lock);
2527
2528         list_for_each_entry_rcu(sp, rule_list, list)
2529                 sp->smk_access = 0;
2530
2531         mutex_unlock(rule_lock);
2532
2533 out_cp:
2534         kfree(cp);
2535 out_data:
2536         kfree(data);
2537
2538         return rc;
2539 }
2540
2541 static const struct file_operations smk_revoke_subj_ops = {
2542         .write          = smk_write_revoke_subj,
2543         .read           = simple_transaction_read,
2544         .release        = simple_transaction_release,
2545         .llseek         = generic_file_llseek,
2546 };
2547
2548 /**
2549  * smk_init_sysfs - initialize /sys/fs/smackfs
2550  *
2551  */
2552 static int smk_init_sysfs(void)
2553 {
2554         return sysfs_create_mount_point(fs_kobj, "smackfs");
2555 }
2556
2557 /**
2558  * smk_write_change_rule - write() for /smack/change-rule
2559  * @file: file pointer
2560  * @buf: data from user space
2561  * @count: bytes sent
2562  * @ppos: where to start - must be 0
2563  */
2564 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2565                                 size_t count, loff_t *ppos)
2566 {
2567         /*
2568          * Must have privilege.
2569          */
2570         if (!smack_privileged(CAP_MAC_ADMIN))
2571                 return -EPERM;
2572
2573         return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2574                                     SMK_CHANGE_FMT);
2575 }
2576
2577 static const struct file_operations smk_change_rule_ops = {
2578         .write          = smk_write_change_rule,
2579         .read           = simple_transaction_read,
2580         .release        = simple_transaction_release,
2581         .llseek         = generic_file_llseek,
2582 };
2583
2584 /**
2585  * smk_read_syslog - read() for smackfs/syslog
2586  * @filp: file pointer, not actually used
2587  * @buf: where to put the result
2588  * @cn: maximum to send along
2589  * @ppos: where to start
2590  *
2591  * Returns number of bytes read or error code, as appropriate
2592  */
2593 static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2594                                 size_t cn, loff_t *ppos)
2595 {
2596         struct smack_known *skp;
2597         ssize_t rc = -EINVAL;
2598         int asize;
2599
2600         if (*ppos != 0)
2601                 return 0;
2602
2603         if (smack_syslog_label == NULL)
2604                 skp = &smack_known_star;
2605         else
2606                 skp = smack_syslog_label;
2607
2608         asize = strlen(skp->smk_known) + 1;
2609
2610         if (cn >= asize)
2611                 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2612                                                 asize);
2613
2614         return rc;
2615 }
2616
2617 /**
2618  * smk_write_syslog - write() for smackfs/syslog
2619  * @file: file pointer, not actually used
2620  * @buf: where to get the data from
2621  * @count: bytes sent
2622  * @ppos: where to start
2623  *
2624  * Returns number of bytes written or error code, as appropriate
2625  */
2626 static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2627                                 size_t count, loff_t *ppos)
2628 {
2629         char *data;
2630         struct smack_known *skp;
2631         int rc = count;
2632
2633         if (!smack_privileged(CAP_MAC_ADMIN))
2634                 return -EPERM;
2635
2636         data = memdup_user_nul(buf, count);
2637         if (IS_ERR(data))
2638                 return PTR_ERR(data);
2639
2640         skp = smk_import_entry(data, count);
2641         if (IS_ERR(skp))
2642                 rc = PTR_ERR(skp);
2643         else
2644                 smack_syslog_label = skp;
2645
2646         kfree(data);
2647         return rc;
2648 }
2649
2650 static const struct file_operations smk_syslog_ops = {
2651         .read           = smk_read_syslog,
2652         .write          = smk_write_syslog,
2653         .llseek         = default_llseek,
2654 };
2655
2656 /*
2657  * Seq_file read operations for /smack/relabel-self
2658  */
2659
2660 static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
2661 {
2662         struct task_smack *tsp = smack_cred(current_cred());
2663
2664         return smk_seq_start(s, pos, &tsp->smk_relabel);
2665 }
2666
2667 static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2668 {
2669         struct task_smack *tsp = smack_cred(current_cred());
2670
2671         return smk_seq_next(s, v, pos, &tsp->smk_relabel);
2672 }
2673
2674 static int relabel_self_seq_show(struct seq_file *s, void *v)
2675 {
2676         struct list_head *list = v;
2677         struct smack_known_list_elem *sklep =
2678                 list_entry(list, struct smack_known_list_elem, list);
2679
2680         seq_puts(s, sklep->smk_label->smk_known);
2681         seq_putc(s, ' ');
2682
2683         return 0;
2684 }
2685
2686 static const struct seq_operations relabel_self_seq_ops = {
2687         .start = relabel_self_seq_start,
2688         .next  = relabel_self_seq_next,
2689         .show  = relabel_self_seq_show,
2690         .stop  = smk_seq_stop,
2691 };
2692
2693 /**
2694  * smk_open_relabel_self - open() for /smack/relabel-self
2695  * @inode: inode structure representing file
2696  * @file: "relabel-self" file pointer
2697  *
2698  * Connect our relabel_self_seq_* operations with /smack/relabel-self
2699  * file_operations
2700  */
2701 static int smk_open_relabel_self(struct inode *inode, struct file *file)
2702 {
2703         return seq_open(file, &relabel_self_seq_ops);
2704 }
2705
2706 /**
2707  * smk_write_relabel_self - write() for /smack/relabel-self
2708  * @file: file pointer, not actually used
2709  * @buf: where to get the data from
2710  * @count: bytes sent
2711  * @ppos: where to start - must be 0
2712  *
2713  */
2714 static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
2715                                 size_t count, loff_t *ppos)
2716 {
2717         struct task_smack *tsp = smack_cred(current_cred());
2718         char *data;
2719         int rc;
2720         LIST_HEAD(list_tmp);
2721
2722         /*
2723          * Must have privilege.
2724          */
2725         if (!smack_privileged(CAP_MAC_ADMIN))
2726                 return -EPERM;
2727
2728         /*
2729          * Enough data must be present.
2730          */
2731         if (*ppos != 0)
2732                 return -EINVAL;
2733
2734         data = memdup_user_nul(buf, count);
2735         if (IS_ERR(data))
2736                 return PTR_ERR(data);
2737
2738         rc = smk_parse_label_list(data, &list_tmp);
2739         kfree(data);
2740
2741         if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2742                 smk_destroy_label_list(&tsp->smk_relabel);
2743                 list_splice(&list_tmp, &tsp->smk_relabel);
2744                 return count;
2745         }
2746
2747         smk_destroy_label_list(&list_tmp);
2748         return rc;
2749 }
2750
2751 static const struct file_operations smk_relabel_self_ops = {
2752         .open           = smk_open_relabel_self,
2753         .read           = seq_read,
2754         .llseek         = seq_lseek,
2755         .write          = smk_write_relabel_self,
2756         .release        = seq_release,
2757 };
2758
2759 /**
2760  * smk_read_ptrace - read() for /smack/ptrace
2761  * @filp: file pointer, not actually used
2762  * @buf: where to put the result
2763  * @count: maximum to send along
2764  * @ppos: where to start
2765  *
2766  * Returns number of bytes read or error code, as appropriate
2767  */
2768 static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2769                                size_t count, loff_t *ppos)
2770 {
2771         char temp[32];
2772         ssize_t rc;
2773
2774         if (*ppos != 0)
2775                 return 0;
2776
2777         sprintf(temp, "%d\n", smack_ptrace_rule);
2778         rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2779         return rc;
2780 }
2781
2782 /**
2783  * smk_write_ptrace - write() for /smack/ptrace
2784  * @file: file pointer
2785  * @buf: data from user space
2786  * @count: bytes sent
2787  * @ppos: where to start - must be 0
2788  */
2789 static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2790                                 size_t count, loff_t *ppos)
2791 {
2792         char temp[32];
2793         int i;
2794
2795         if (!smack_privileged(CAP_MAC_ADMIN))
2796                 return -EPERM;
2797
2798         if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2799                 return -EINVAL;
2800
2801         if (copy_from_user(temp, buf, count) != 0)
2802                 return -EFAULT;
2803
2804         temp[count] = '\0';
2805
2806         if (sscanf(temp, "%d", &i) != 1)
2807                 return -EINVAL;
2808         if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2809                 return -EINVAL;
2810         smack_ptrace_rule = i;
2811
2812         return count;
2813 }
2814
2815 static const struct file_operations smk_ptrace_ops = {
2816         .write          = smk_write_ptrace,
2817         .read           = smk_read_ptrace,
2818         .llseek         = default_llseek,
2819 };
2820
2821 /**
2822  * smk_fill_super - fill the smackfs superblock
2823  * @sb: the empty superblock
2824  * @fc: unused
2825  *
2826  * Fill in the well known entries for the smack filesystem
2827  *
2828  * Returns 0 on success, an error code on failure
2829  */
2830 static int smk_fill_super(struct super_block *sb, struct fs_context *fc)
2831 {
2832         int rc;
2833
2834         static const struct tree_descr smack_files[] = {
2835                 [SMK_LOAD] = {
2836                         "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2837                 [SMK_CIPSO] = {
2838                         "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2839                 [SMK_DOI] = {
2840                         "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2841                 [SMK_DIRECT] = {
2842                         "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2843                 [SMK_AMBIENT] = {
2844                         "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2845                 [SMK_NET4ADDR] = {
2846                         "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
2847                 [SMK_ONLYCAP] = {
2848                         "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2849                 [SMK_LOGGING] = {
2850                         "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2851                 [SMK_LOAD_SELF] = {
2852                         "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2853                 [SMK_ACCESSES] = {
2854                         "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2855                 [SMK_MAPPED] = {
2856                         "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2857                 [SMK_LOAD2] = {
2858                         "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2859                 [SMK_LOAD_SELF2] = {
2860                         "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2861                 [SMK_ACCESS2] = {
2862                         "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2863                 [SMK_CIPSO2] = {
2864                         "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2865                 [SMK_REVOKE_SUBJ] = {
2866                         "revoke-subject", &smk_revoke_subj_ops,
2867                         S_IRUGO|S_IWUSR},
2868                 [SMK_CHANGE_RULE] = {
2869                         "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2870                 [SMK_SYSLOG] = {
2871                         "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
2872                 [SMK_PTRACE] = {
2873                         "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
2874 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2875                 [SMK_UNCONFINED] = {
2876                         "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2877 #endif
2878 #if IS_ENABLED(CONFIG_IPV6)
2879                 [SMK_NET6ADDR] = {
2880                         "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
2881 #endif /* CONFIG_IPV6 */
2882                 [SMK_RELABEL_SELF] = {
2883                         "relabel-self", &smk_relabel_self_ops,
2884                                 S_IRUGO|S_IWUGO},
2885                 /* last one */
2886                         {""}
2887         };
2888
2889         rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2890         if (rc != 0) {
2891                 printk(KERN_ERR "%s failed %d while creating inodes\n",
2892                         __func__, rc);
2893                 return rc;
2894         }
2895
2896         return 0;
2897 }
2898
2899 /**
2900  * smk_get_tree - get the smackfs superblock
2901  * @fc: The mount context, including any options
2902  *
2903  * Just passes everything along.
2904  *
2905  * Returns what the lower level code does.
2906  */
2907 static int smk_get_tree(struct fs_context *fc)
2908 {
2909         return get_tree_single(fc, smk_fill_super);
2910 }
2911
2912 static const struct fs_context_operations smk_context_ops = {
2913         .get_tree       = smk_get_tree,
2914 };
2915
2916 /**
2917  * smk_init_fs_context - Initialise a filesystem context for smackfs
2918  * @fc: The blank mount context
2919  */
2920 static int smk_init_fs_context(struct fs_context *fc)
2921 {
2922         fc->ops = &smk_context_ops;
2923         return 0;
2924 }
2925
2926 static struct file_system_type smk_fs_type = {
2927         .name           = "smackfs",
2928         .init_fs_context = smk_init_fs_context,
2929         .kill_sb        = kill_litter_super,
2930 };
2931
2932 static struct vfsmount *smackfs_mount;
2933
2934 static int __init smk_preset_netlabel(struct smack_known *skp)
2935 {
2936         skp->smk_netlabel.domain = skp->smk_known;
2937         skp->smk_netlabel.flags =
2938                 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2939         return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2940                                 &skp->smk_netlabel, strlen(skp->smk_known));
2941 }
2942
2943 /**
2944  * init_smk_fs - get the smackfs superblock
2945  *
2946  * register the smackfs
2947  *
2948  * Do not register smackfs if Smack wasn't enabled
2949  * on boot. We can not put this method normally under the
2950  * smack_init() code path since the security subsystem get
2951  * initialized before the vfs caches.
2952  *
2953  * Returns true if we were not chosen on boot or if
2954  * we were chosen and filesystem registration succeeded.
2955  */
2956 static int __init init_smk_fs(void)
2957 {
2958         int err;
2959         int rc;
2960
2961         if (smack_enabled == 0)
2962                 return 0;
2963
2964         err = smk_init_sysfs();
2965         if (err)
2966                 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2967
2968         err = register_filesystem(&smk_fs_type);
2969         if (!err) {
2970                 smackfs_mount = kern_mount(&smk_fs_type);
2971                 if (IS_ERR(smackfs_mount)) {
2972                         printk(KERN_ERR "smackfs:  could not mount!\n");
2973                         err = PTR_ERR(smackfs_mount);
2974                         smackfs_mount = NULL;
2975                 }
2976         }
2977
2978         smk_cipso_doi();
2979         smk_unlbl_ambient(NULL);
2980
2981         rc = smk_preset_netlabel(&smack_known_floor);
2982         if (err == 0 && rc < 0)
2983                 err = rc;
2984         rc = smk_preset_netlabel(&smack_known_hat);
2985         if (err == 0 && rc < 0)
2986                 err = rc;
2987         rc = smk_preset_netlabel(&smack_known_huh);
2988         if (err == 0 && rc < 0)
2989                 err = rc;
2990         rc = smk_preset_netlabel(&smack_known_star);
2991         if (err == 0 && rc < 0)
2992                 err = rc;
2993         rc = smk_preset_netlabel(&smack_known_web);
2994         if (err == 0 && rc < 0)
2995                 err = rc;
2996
2997         return err;
2998 }
2999
3000 __initcall(init_smk_fs);