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