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