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