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