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