KVM: Add trace markers
[linux-2.6/linux-2.6-openrd.git] / security / smack / smackfs.c
blob6ba283783b70af178d7c66bca1e44eb632674f2b
1 /*
2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
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.
8 * Authors:
9 * Casey Schaufler <casey@schaufler-ca.com>
10 * Ahmed S. Darwish <darwish.07@gmail.com>
12 * Special thanks to the authors of selinuxfs.
14 * Karl MacMillan <kmacmillan@tresys.com>
15 * James Morris <jmorris@redhat.com>
19 #include <linux/kernel.h>
20 #include <linux/vmalloc.h>
21 #include <linux/security.h>
22 #include <linux/mutex.h>
23 #include <net/netlabel.h>
24 #include <net/cipso_ipv4.h>
25 #include <linux/seq_file.h>
26 #include <linux/ctype.h>
27 #include <linux/audit.h>
28 #include "smack.h"
31 * smackfs pseudo filesystem.
34 enum smk_inos {
35 SMK_ROOT_INO = 2,
36 SMK_LOAD = 3, /* load policy */
37 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
38 SMK_DOI = 5, /* CIPSO DOI */
39 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
40 SMK_AMBIENT = 7, /* internet ambient label */
41 SMK_NLTYPE = 8, /* label scheme to use by default */
45 * List locks
47 static DEFINE_MUTEX(smack_list_lock);
48 static DEFINE_MUTEX(smack_cipso_lock);
49 static DEFINE_MUTEX(smack_ambient_lock);
52 * This is the "ambient" label for network traffic.
53 * If it isn't somehow marked, use this.
54 * It can be reset via smackfs/ambient
56 char *smack_net_ambient = smack_known_floor.smk_known;
59 * This is the default packet marking scheme for network traffic.
60 * It can be reset via smackfs/nltype
62 int smack_net_nltype = NETLBL_NLTYPE_CIPSOV4;
65 * This is the level in a CIPSO header that indicates a
66 * smack label is contained directly in the category set.
67 * It can be reset via smackfs/direct
69 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
71 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
72 struct smk_list_entry *smack_list;
74 #define SEQ_READ_FINISHED 1
77 * Values for parsing cipso rules
78 * SMK_DIGITLEN: Length of a digit field in a rule.
79 * SMK_CIPSOMIN: Minimum possible cipso rule length.
80 * SMK_CIPSOMAX: Maximum possible cipso rule length.
82 #define SMK_DIGITLEN 4
83 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
84 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
87 * Values for parsing MAC rules
88 * SMK_ACCESS: Maximum possible combination of access permissions
89 * SMK_ACCESSLEN: Maximum length for a rule access field
90 * SMK_LOADLEN: Smack rule length
92 #define SMK_ACCESS "rwxa"
93 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
94 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
98 * Seq_file read operations for /smack/load
101 static void *load_seq_start(struct seq_file *s, loff_t *pos)
103 if (*pos == SEQ_READ_FINISHED)
104 return NULL;
106 return smack_list;
109 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
111 struct smk_list_entry *skp = ((struct smk_list_entry *) v)->smk_next;
113 if (skp == NULL)
114 *pos = SEQ_READ_FINISHED;
116 return skp;
119 static int load_seq_show(struct seq_file *s, void *v)
121 struct smk_list_entry *slp = (struct smk_list_entry *) v;
122 struct smack_rule *srp = &slp->smk_rule;
124 seq_printf(s, "%s %s", (char *)srp->smk_subject,
125 (char *)srp->smk_object);
127 seq_putc(s, ' ');
129 if (srp->smk_access & MAY_READ)
130 seq_putc(s, 'r');
131 if (srp->smk_access & MAY_WRITE)
132 seq_putc(s, 'w');
133 if (srp->smk_access & MAY_EXEC)
134 seq_putc(s, 'x');
135 if (srp->smk_access & MAY_APPEND)
136 seq_putc(s, 'a');
137 if (srp->smk_access == 0)
138 seq_putc(s, '-');
140 seq_putc(s, '\n');
142 return 0;
145 static void load_seq_stop(struct seq_file *s, void *v)
147 /* No-op */
150 static struct seq_operations load_seq_ops = {
151 .start = load_seq_start,
152 .next = load_seq_next,
153 .show = load_seq_show,
154 .stop = load_seq_stop,
158 * smk_open_load - open() for /smack/load
159 * @inode: inode structure representing file
160 * @file: "load" file pointer
162 * For reading, use load_seq_* seq_file reading operations.
164 static int smk_open_load(struct inode *inode, struct file *file)
166 return seq_open(file, &load_seq_ops);
170 * smk_set_access - add a rule to the rule list
171 * @srp: the new rule to add
173 * Looks through the current subject/object/access list for
174 * the subject/object pair and replaces the access that was
175 * there. If the pair isn't found add it with the specified
176 * access.
178 static void smk_set_access(struct smack_rule *srp)
180 struct smk_list_entry *sp;
181 struct smk_list_entry *newp;
183 mutex_lock(&smack_list_lock);
185 for (sp = smack_list; sp != NULL; sp = sp->smk_next)
186 if (sp->smk_rule.smk_subject == srp->smk_subject &&
187 sp->smk_rule.smk_object == srp->smk_object) {
188 sp->smk_rule.smk_access = srp->smk_access;
189 break;
192 if (sp == NULL) {
193 newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
194 newp->smk_rule = *srp;
195 newp->smk_next = smack_list;
196 smack_list = newp;
199 mutex_unlock(&smack_list_lock);
201 return;
205 * smk_write_load - write() for /smack/load
206 * @filp: file pointer, not actually used
207 * @buf: where to get the data from
208 * @count: bytes sent
209 * @ppos: where to start - must be 0
211 * Get one smack access rule from above.
212 * The format is exactly:
213 * char subject[SMK_LABELLEN]
214 * char object[SMK_LABELLEN]
215 * char access[SMK_ACCESSLEN]
217 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
219 static ssize_t smk_write_load(struct file *file, const char __user *buf,
220 size_t count, loff_t *ppos)
222 struct smack_rule rule;
223 char *data;
224 int rc = -EINVAL;
227 * Must have privilege.
228 * No partial writes.
229 * Enough data must be present.
231 if (!capable(CAP_MAC_ADMIN))
232 return -EPERM;
233 if (*ppos != 0)
234 return -EINVAL;
235 if (count != SMK_LOADLEN)
236 return -EINVAL;
238 data = kzalloc(count, GFP_KERNEL);
239 if (data == NULL)
240 return -ENOMEM;
242 if (copy_from_user(data, buf, count) != 0) {
243 rc = -EFAULT;
244 goto out;
247 rule.smk_subject = smk_import(data, 0);
248 if (rule.smk_subject == NULL)
249 goto out;
251 rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
252 if (rule.smk_object == NULL)
253 goto out;
255 rule.smk_access = 0;
257 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
258 case '-':
259 break;
260 case 'r':
261 case 'R':
262 rule.smk_access |= MAY_READ;
263 break;
264 default:
265 goto out;
268 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
269 case '-':
270 break;
271 case 'w':
272 case 'W':
273 rule.smk_access |= MAY_WRITE;
274 break;
275 default:
276 goto out;
279 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
280 case '-':
281 break;
282 case 'x':
283 case 'X':
284 rule.smk_access |= MAY_EXEC;
285 break;
286 default:
287 goto out;
290 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
291 case '-':
292 break;
293 case 'a':
294 case 'A':
295 rule.smk_access |= MAY_READ;
296 break;
297 default:
298 goto out;
301 smk_set_access(&rule);
302 rc = count;
304 out:
305 kfree(data);
306 return rc;
309 static const struct file_operations smk_load_ops = {
310 .open = smk_open_load,
311 .read = seq_read,
312 .llseek = seq_lseek,
313 .write = smk_write_load,
314 .release = seq_release,
318 * smk_cipso_doi - initialize the CIPSO domain
320 void smk_cipso_doi(void)
322 int rc;
323 struct cipso_v4_doi *doip;
324 struct netlbl_audit audit_info;
326 audit_info.loginuid = audit_get_loginuid(current);
327 audit_info.secid = smack_to_secid(current->security);
329 rc = netlbl_cfg_map_del(NULL, &audit_info);
330 if (rc != 0)
331 printk(KERN_WARNING "%s:%d remove rc = %d\n",
332 __func__, __LINE__, rc);
334 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
335 if (doip == NULL)
336 panic("smack: Failed to initialize cipso DOI.\n");
337 doip->map.std = NULL;
338 doip->doi = smk_cipso_doi_value;
339 doip->type = CIPSO_V4_MAP_PASS;
340 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
341 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
342 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
344 rc = netlbl_cfg_cipsov4_add_map(doip, NULL, &audit_info);
345 if (rc != 0)
346 printk(KERN_WARNING "%s:%d add rc = %d\n",
347 __func__, __LINE__, rc);
351 * smk_unlbl_ambient - initialize the unlabeled domain
353 void smk_unlbl_ambient(char *oldambient)
355 int rc;
356 struct netlbl_audit audit_info;
358 audit_info.loginuid = audit_get_loginuid(current);
359 audit_info.secid = smack_to_secid(current->security);
361 if (oldambient != NULL) {
362 rc = netlbl_cfg_map_del(oldambient, &audit_info);
363 if (rc != 0)
364 printk(KERN_WARNING "%s:%d remove rc = %d\n",
365 __func__, __LINE__, rc);
368 rc = netlbl_cfg_unlbl_add_map(smack_net_ambient, &audit_info);
369 if (rc != 0)
370 printk(KERN_WARNING "%s:%d add rc = %d\n",
371 __func__, __LINE__, rc);
375 * Seq_file read operations for /smack/cipso
378 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
380 if (*pos == SEQ_READ_FINISHED)
381 return NULL;
383 return smack_known;
386 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
388 struct smack_known *skp = ((struct smack_known *) v)->smk_next;
391 * Omit labels with no associated cipso value
393 while (skp != NULL && !skp->smk_cipso)
394 skp = skp->smk_next;
396 if (skp == NULL)
397 *pos = SEQ_READ_FINISHED;
399 return skp;
403 * Print cipso labels in format:
404 * label level[/cat[,cat]]
406 static int cipso_seq_show(struct seq_file *s, void *v)
408 struct smack_known *skp = (struct smack_known *) v;
409 struct smack_cipso *scp = skp->smk_cipso;
410 char *cbp;
411 char sep = '/';
412 int cat = 1;
413 int i;
414 unsigned char m;
416 if (scp == NULL)
417 return 0;
419 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
421 cbp = scp->smk_catset;
422 for (i = 0; i < SMK_LABELLEN; i++)
423 for (m = 0x80; m != 0; m >>= 1) {
424 if (m & cbp[i]) {
425 seq_printf(s, "%c%d", sep, cat);
426 sep = ',';
428 cat++;
431 seq_putc(s, '\n');
433 return 0;
436 static void cipso_seq_stop(struct seq_file *s, void *v)
438 /* No-op */
441 static struct seq_operations cipso_seq_ops = {
442 .start = cipso_seq_start,
443 .stop = cipso_seq_stop,
444 .next = cipso_seq_next,
445 .show = cipso_seq_show,
449 * smk_open_cipso - open() for /smack/cipso
450 * @inode: inode structure representing file
451 * @file: "cipso" file pointer
453 * Connect our cipso_seq_* operations with /smack/cipso
454 * file_operations
456 static int smk_open_cipso(struct inode *inode, struct file *file)
458 return seq_open(file, &cipso_seq_ops);
462 * smk_write_cipso - write() for /smack/cipso
463 * @filp: file pointer, not actually used
464 * @buf: where to get the data from
465 * @count: bytes sent
466 * @ppos: where to start
468 * Accepts only one cipso rule per write call.
469 * Returns number of bytes written or error code, as appropriate
471 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
472 size_t count, loff_t *ppos)
474 struct smack_known *skp;
475 struct smack_cipso *scp = NULL;
476 char mapcatset[SMK_LABELLEN];
477 int maplevel;
478 int cat;
479 int catlen;
480 ssize_t rc = -EINVAL;
481 char *data = NULL;
482 char *rule;
483 int ret;
484 int i;
487 * Must have privilege.
488 * No partial writes.
489 * Enough data must be present.
491 if (!capable(CAP_MAC_ADMIN))
492 return -EPERM;
493 if (*ppos != 0)
494 return -EINVAL;
495 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
496 return -EINVAL;
498 data = kzalloc(count + 1, GFP_KERNEL);
499 if (data == NULL)
500 return -ENOMEM;
502 if (copy_from_user(data, buf, count) != 0) {
503 rc = -EFAULT;
504 goto unlockedout;
507 data[count] = '\0';
508 rule = data;
510 * Only allow one writer at a time. Writes should be
511 * quite rare and small in any case.
513 mutex_lock(&smack_cipso_lock);
515 skp = smk_import_entry(rule, 0);
516 if (skp == NULL)
517 goto out;
519 rule += SMK_LABELLEN;;
520 ret = sscanf(rule, "%d", &maplevel);
521 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
522 goto out;
524 rule += SMK_DIGITLEN;
525 ret = sscanf(rule, "%d", &catlen);
526 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
527 goto out;
529 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
530 goto out;
532 memset(mapcatset, 0, sizeof(mapcatset));
534 for (i = 0; i < catlen; i++) {
535 rule += SMK_DIGITLEN;
536 ret = sscanf(rule, "%d", &cat);
537 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
538 goto out;
540 smack_catset_bit(cat, mapcatset);
543 if (skp->smk_cipso == NULL) {
544 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
545 if (scp == NULL) {
546 rc = -ENOMEM;
547 goto out;
551 spin_lock_bh(&skp->smk_cipsolock);
553 if (scp == NULL)
554 scp = skp->smk_cipso;
555 else
556 skp->smk_cipso = scp;
558 scp->smk_level = maplevel;
559 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
561 spin_unlock_bh(&skp->smk_cipsolock);
563 rc = count;
564 out:
565 mutex_unlock(&smack_cipso_lock);
566 unlockedout:
567 kfree(data);
568 return rc;
571 static const struct file_operations smk_cipso_ops = {
572 .open = smk_open_cipso,
573 .read = seq_read,
574 .llseek = seq_lseek,
575 .write = smk_write_cipso,
576 .release = seq_release,
580 * smk_read_doi - read() for /smack/doi
581 * @filp: file pointer, not actually used
582 * @buf: where to put the result
583 * @count: maximum to send along
584 * @ppos: where to start
586 * Returns number of bytes read or error code, as appropriate
588 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
589 size_t count, loff_t *ppos)
591 char temp[80];
592 ssize_t rc;
594 if (*ppos != 0)
595 return 0;
597 sprintf(temp, "%d", smk_cipso_doi_value);
598 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
600 return rc;
604 * smk_write_doi - write() for /smack/doi
605 * @filp: file pointer, not actually used
606 * @buf: where to get the data from
607 * @count: bytes sent
608 * @ppos: where to start
610 * Returns number of bytes written or error code, as appropriate
612 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
613 size_t count, loff_t *ppos)
615 char temp[80];
616 int i;
618 if (!capable(CAP_MAC_ADMIN))
619 return -EPERM;
621 if (count >= sizeof(temp) || count == 0)
622 return -EINVAL;
624 if (copy_from_user(temp, buf, count) != 0)
625 return -EFAULT;
627 temp[count] = '\0';
629 if (sscanf(temp, "%d", &i) != 1)
630 return -EINVAL;
632 smk_cipso_doi_value = i;
634 smk_cipso_doi();
636 return count;
639 static const struct file_operations smk_doi_ops = {
640 .read = smk_read_doi,
641 .write = smk_write_doi,
645 * smk_read_direct - read() for /smack/direct
646 * @filp: file pointer, not actually used
647 * @buf: where to put the result
648 * @count: maximum to send along
649 * @ppos: where to start
651 * Returns number of bytes read or error code, as appropriate
653 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
654 size_t count, loff_t *ppos)
656 char temp[80];
657 ssize_t rc;
659 if (*ppos != 0)
660 return 0;
662 sprintf(temp, "%d", smack_cipso_direct);
663 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
665 return rc;
669 * smk_write_direct - write() for /smack/direct
670 * @filp: file pointer, not actually used
671 * @buf: where to get the data from
672 * @count: bytes sent
673 * @ppos: where to start
675 * Returns number of bytes written or error code, as appropriate
677 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
678 size_t count, loff_t *ppos)
680 char temp[80];
681 int i;
683 if (!capable(CAP_MAC_ADMIN))
684 return -EPERM;
686 if (count >= sizeof(temp) || count == 0)
687 return -EINVAL;
689 if (copy_from_user(temp, buf, count) != 0)
690 return -EFAULT;
692 temp[count] = '\0';
694 if (sscanf(temp, "%d", &i) != 1)
695 return -EINVAL;
697 smack_cipso_direct = i;
699 return count;
702 static const struct file_operations smk_direct_ops = {
703 .read = smk_read_direct,
704 .write = smk_write_direct,
708 * smk_read_ambient - read() for /smack/ambient
709 * @filp: file pointer, not actually used
710 * @buf: where to put the result
711 * @cn: maximum to send along
712 * @ppos: where to start
714 * Returns number of bytes read or error code, as appropriate
716 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
717 size_t cn, loff_t *ppos)
719 ssize_t rc;
720 int asize;
722 if (*ppos != 0)
723 return 0;
725 * Being careful to avoid a problem in the case where
726 * smack_net_ambient gets changed in midstream.
728 mutex_lock(&smack_ambient_lock);
730 asize = strlen(smack_net_ambient) + 1;
732 if (cn >= asize)
733 rc = simple_read_from_buffer(buf, cn, ppos,
734 smack_net_ambient, asize);
735 else
736 rc = -EINVAL;
738 mutex_unlock(&smack_ambient_lock);
740 return rc;
744 * smk_write_ambient - write() for /smack/ambient
745 * @filp: file pointer, not actually used
746 * @buf: where to get the data from
747 * @count: bytes sent
748 * @ppos: where to start
750 * Returns number of bytes written or error code, as appropriate
752 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
753 size_t count, loff_t *ppos)
755 char in[SMK_LABELLEN];
756 char *oldambient;
757 char *smack;
759 if (!capable(CAP_MAC_ADMIN))
760 return -EPERM;
762 if (count >= SMK_LABELLEN)
763 return -EINVAL;
765 if (copy_from_user(in, buf, count) != 0)
766 return -EFAULT;
768 smack = smk_import(in, count);
769 if (smack == NULL)
770 return -EINVAL;
772 mutex_lock(&smack_ambient_lock);
774 oldambient = smack_net_ambient;
775 smack_net_ambient = smack;
776 smk_unlbl_ambient(oldambient);
778 mutex_unlock(&smack_ambient_lock);
780 return count;
783 static const struct file_operations smk_ambient_ops = {
784 .read = smk_read_ambient,
785 .write = smk_write_ambient,
788 struct option_names {
789 int o_number;
790 char *o_name;
791 char *o_alias;
794 static struct option_names netlbl_choices[] = {
795 { NETLBL_NLTYPE_RIPSO,
796 NETLBL_NLTYPE_RIPSO_NAME, "ripso" },
797 { NETLBL_NLTYPE_CIPSOV4,
798 NETLBL_NLTYPE_CIPSOV4_NAME, "cipsov4" },
799 { NETLBL_NLTYPE_CIPSOV4,
800 NETLBL_NLTYPE_CIPSOV4_NAME, "cipso" },
801 { NETLBL_NLTYPE_CIPSOV6,
802 NETLBL_NLTYPE_CIPSOV6_NAME, "cipsov6" },
803 { NETLBL_NLTYPE_UNLABELED,
804 NETLBL_NLTYPE_UNLABELED_NAME, "unlabeled" },
808 * smk_read_nltype - read() for /smack/nltype
809 * @filp: file pointer, not actually used
810 * @buf: where to put the result
811 * @count: maximum to send along
812 * @ppos: where to start
814 * Returns number of bytes read or error code, as appropriate
816 static ssize_t smk_read_nltype(struct file *filp, char __user *buf,
817 size_t count, loff_t *ppos)
819 char bound[40];
820 ssize_t rc;
821 int i;
823 if (count < SMK_LABELLEN)
824 return -EINVAL;
826 if (*ppos != 0)
827 return 0;
829 sprintf(bound, "unknown");
831 for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
832 if (smack_net_nltype == netlbl_choices[i].o_number) {
833 sprintf(bound, "%s", netlbl_choices[i].o_name);
834 break;
837 rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound));
839 return rc;
843 * smk_write_nltype - write() for /smack/nltype
844 * @filp: file pointer, not actually used
845 * @buf: where to get the data from
846 * @count: bytes sent
847 * @ppos: where to start
849 * Returns number of bytes written or error code, as appropriate
851 static ssize_t smk_write_nltype(struct file *file, const char __user *buf,
852 size_t count, loff_t *ppos)
854 char bound[40];
855 char *cp;
856 int i;
858 if (!capable(CAP_MAC_ADMIN))
859 return -EPERM;
861 if (count >= 40)
862 return -EINVAL;
864 if (copy_from_user(bound, buf, count) != 0)
865 return -EFAULT;
867 bound[count] = '\0';
868 cp = strchr(bound, ' ');
869 if (cp != NULL)
870 *cp = '\0';
871 cp = strchr(bound, '\n');
872 if (cp != NULL)
873 *cp = '\0';
875 for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
876 if (strcmp(bound, netlbl_choices[i].o_name) == 0 ||
877 strcmp(bound, netlbl_choices[i].o_alias) == 0) {
878 smack_net_nltype = netlbl_choices[i].o_number;
879 return count;
882 * Not a valid choice.
884 return -EINVAL;
887 static const struct file_operations smk_nltype_ops = {
888 .read = smk_read_nltype,
889 .write = smk_write_nltype,
893 * smk_fill_super - fill the /smackfs superblock
894 * @sb: the empty superblock
895 * @data: unused
896 * @silent: unused
898 * Fill in the well known entries for /smack
900 * Returns 0 on success, an error code on failure
902 static int smk_fill_super(struct super_block *sb, void *data, int silent)
904 int rc;
905 struct inode *root_inode;
907 static struct tree_descr smack_files[] = {
908 [SMK_LOAD] =
909 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
910 [SMK_CIPSO] =
911 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
912 [SMK_DOI] =
913 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
914 [SMK_DIRECT] =
915 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
916 [SMK_AMBIENT] =
917 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
918 [SMK_NLTYPE] =
919 {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR},
920 /* last one */ {""}
923 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
924 if (rc != 0) {
925 printk(KERN_ERR "%s failed %d while creating inodes\n",
926 __func__, rc);
927 return rc;
930 root_inode = sb->s_root->d_inode;
931 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
933 return 0;
937 * smk_get_sb - get the smackfs superblock
938 * @fs_type: passed along without comment
939 * @flags: passed along without comment
940 * @dev_name: passed along without comment
941 * @data: passed along without comment
942 * @mnt: passed along without comment
944 * Just passes everything along.
946 * Returns what the lower level code does.
948 static int smk_get_sb(struct file_system_type *fs_type,
949 int flags, const char *dev_name, void *data,
950 struct vfsmount *mnt)
952 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
955 static struct file_system_type smk_fs_type = {
956 .name = "smackfs",
957 .get_sb = smk_get_sb,
958 .kill_sb = kill_litter_super,
961 static struct vfsmount *smackfs_mount;
964 * init_smk_fs - get the smackfs superblock
966 * register the smackfs
968 * Do not register smackfs if Smack wasn't enabled
969 * on boot. We can not put this method normally under the
970 * smack_init() code path since the security subsystem get
971 * initialized before the vfs caches.
973 * Returns true if we were not chosen on boot or if
974 * we were chosen and filesystem registration succeeded.
976 static int __init init_smk_fs(void)
978 int err;
980 if (!security_module_enable(&smack_ops))
981 return 0;
983 err = register_filesystem(&smk_fs_type);
984 if (!err) {
985 smackfs_mount = kern_mount(&smk_fs_type);
986 if (IS_ERR(smackfs_mount)) {
987 printk(KERN_ERR "smackfs: could not mount!\n");
988 err = PTR_ERR(smackfs_mount);
989 smackfs_mount = NULL;
993 smk_cipso_doi();
994 smk_unlbl_ambient(NULL);
996 return err;
999 __initcall(init_smk_fs);