sched: make early bootup sched_clock() use safer
[wrt350n-kernel.git] / security / smack / smackfs.c
blob358c92c1a153df7f943d09fdc3634ffffc47a632
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 * Disable concurrent writing open() operations
79 static struct semaphore smack_write_sem;
82 * Values for parsing cipso rules
83 * SMK_DIGITLEN: Length of a digit field in a rule.
84 * SMK_CIPSOMEN: Minimum possible cipso rule length.
86 #define SMK_DIGITLEN 4
87 #define SMK_CIPSOMIN (SMK_MAXLEN + 2 * SMK_DIGITLEN)
90 * Seq_file read operations for /smack/load
93 static void *load_seq_start(struct seq_file *s, loff_t *pos)
95 if (*pos == SEQ_READ_FINISHED)
96 return NULL;
98 return smack_list;
101 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
103 struct smk_list_entry *skp = ((struct smk_list_entry *) v)->smk_next;
105 if (skp == NULL)
106 *pos = SEQ_READ_FINISHED;
108 return skp;
111 static int load_seq_show(struct seq_file *s, void *v)
113 struct smk_list_entry *slp = (struct smk_list_entry *) v;
114 struct smack_rule *srp = &slp->smk_rule;
116 seq_printf(s, "%s %s", (char *)srp->smk_subject,
117 (char *)srp->smk_object);
119 seq_putc(s, ' ');
121 if (srp->smk_access & MAY_READ)
122 seq_putc(s, 'r');
123 if (srp->smk_access & MAY_WRITE)
124 seq_putc(s, 'w');
125 if (srp->smk_access & MAY_EXEC)
126 seq_putc(s, 'x');
127 if (srp->smk_access & MAY_APPEND)
128 seq_putc(s, 'a');
129 if (srp->smk_access == 0)
130 seq_putc(s, '-');
132 seq_putc(s, '\n');
134 return 0;
137 static void load_seq_stop(struct seq_file *s, void *v)
139 /* No-op */
142 static struct seq_operations load_seq_ops = {
143 .start = load_seq_start,
144 .next = load_seq_next,
145 .show = load_seq_show,
146 .stop = load_seq_stop,
150 * smk_open_load - open() for /smack/load
151 * @inode: inode structure representing file
152 * @file: "load" file pointer
154 * For reading, use load_seq_* seq_file reading operations.
156 static int smk_open_load(struct inode *inode, struct file *file)
158 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
159 return seq_open(file, &load_seq_ops);
161 if (down_interruptible(&smack_write_sem))
162 return -ERESTARTSYS;
164 return 0;
168 * smk_release_load - release() for /smack/load
169 * @inode: inode structure representing file
170 * @file: "load" file pointer
172 * For a reading session, use the seq_file release
173 * implementation.
174 * Otherwise, we are at the end of a writing session so
175 * clean everything up.
177 static int smk_release_load(struct inode *inode, struct file *file)
179 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
180 return seq_release(inode, file);
182 up(&smack_write_sem);
183 return 0;
187 * smk_set_access - add a rule to the rule list
188 * @srp: the new rule to add
190 * Looks through the current subject/object/access list for
191 * the subject/object pair and replaces the access that was
192 * there. If the pair isn't found add it with the specified
193 * access.
195 static void smk_set_access(struct smack_rule *srp)
197 struct smk_list_entry *sp;
198 struct smk_list_entry *newp;
200 mutex_lock(&smack_list_lock);
202 for (sp = smack_list; sp != NULL; sp = sp->smk_next)
203 if (sp->smk_rule.smk_subject == srp->smk_subject &&
204 sp->smk_rule.smk_object == srp->smk_object) {
205 sp->smk_rule.smk_access = srp->smk_access;
206 break;
209 if (sp == NULL) {
210 newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
211 newp->smk_rule = *srp;
212 newp->smk_next = smack_list;
213 smack_list = newp;
216 mutex_unlock(&smack_list_lock);
218 return;
222 * smk_write_load - write() for /smack/load
223 * @filp: file pointer, not actually used
224 * @buf: where to get the data from
225 * @count: bytes sent
226 * @ppos: where to start - must be 0
228 * Get one smack access rule from above.
229 * The format is exactly:
230 * char subject[SMK_LABELLEN]
231 * char object[SMK_LABELLEN]
232 * char access[SMK_ACCESSKINDS]
234 * Anything following is commentary and ignored.
236 * writes must be SMK_LABELLEN+SMK_LABELLEN+4 bytes.
238 #define MINIMUM_LOAD (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSKINDS)
240 static ssize_t smk_write_load(struct file *file, const char __user *buf,
241 size_t count, loff_t *ppos)
243 struct smack_rule rule;
244 char *data;
245 int rc = -EINVAL;
248 * Must have privilege.
249 * No partial writes.
250 * Enough data must be present.
252 if (!capable(CAP_MAC_ADMIN))
253 return -EPERM;
254 if (*ppos != 0)
255 return -EINVAL;
256 if (count < MINIMUM_LOAD)
257 return -EINVAL;
259 data = kzalloc(count, GFP_KERNEL);
260 if (data == NULL)
261 return -ENOMEM;
263 if (copy_from_user(data, buf, count) != 0) {
264 rc = -EFAULT;
265 goto out;
268 rule.smk_subject = smk_import(data, 0);
269 if (rule.smk_subject == NULL)
270 goto out;
272 rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
273 if (rule.smk_object == NULL)
274 goto out;
276 rule.smk_access = 0;
278 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
279 case '-':
280 break;
281 case 'r':
282 case 'R':
283 rule.smk_access |= MAY_READ;
284 break;
285 default:
286 goto out;
289 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
290 case '-':
291 break;
292 case 'w':
293 case 'W':
294 rule.smk_access |= MAY_WRITE;
295 break;
296 default:
297 goto out;
300 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
301 case '-':
302 break;
303 case 'x':
304 case 'X':
305 rule.smk_access |= MAY_EXEC;
306 break;
307 default:
308 goto out;
311 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
312 case '-':
313 break;
314 case 'a':
315 case 'A':
316 rule.smk_access |= MAY_READ;
317 break;
318 default:
319 goto out;
322 smk_set_access(&rule);
323 rc = count;
325 out:
326 kfree(data);
327 return rc;
330 static const struct file_operations smk_load_ops = {
331 .open = smk_open_load,
332 .read = seq_read,
333 .llseek = seq_lseek,
334 .write = smk_write_load,
335 .release = smk_release_load,
339 * smk_cipso_doi - initialize the CIPSO domain
341 void smk_cipso_doi(void)
343 int rc;
344 struct cipso_v4_doi *doip;
345 struct netlbl_audit audit_info;
347 audit_info.loginuid = audit_get_loginuid(current);
348 audit_info.secid = smack_to_secid(current->security);
350 rc = netlbl_cfg_map_del(NULL, &audit_info);
351 if (rc != 0)
352 printk(KERN_WARNING "%s:%d remove rc = %d\n",
353 __func__, __LINE__, rc);
355 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
356 if (doip == NULL)
357 panic("smack: Failed to initialize cipso DOI.\n");
358 doip->map.std = NULL;
359 doip->doi = smk_cipso_doi_value;
360 doip->type = CIPSO_V4_MAP_PASS;
361 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
362 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
363 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
365 rc = netlbl_cfg_cipsov4_add_map(doip, NULL, &audit_info);
366 if (rc != 0)
367 printk(KERN_WARNING "%s:%d add rc = %d\n",
368 __func__, __LINE__, rc);
372 * smk_unlbl_ambient - initialize the unlabeled domain
374 void smk_unlbl_ambient(char *oldambient)
376 int rc;
377 struct netlbl_audit audit_info;
379 audit_info.loginuid = audit_get_loginuid(current);
380 audit_info.secid = smack_to_secid(current->security);
382 if (oldambient != NULL) {
383 rc = netlbl_cfg_map_del(oldambient, &audit_info);
384 if (rc != 0)
385 printk(KERN_WARNING "%s:%d remove rc = %d\n",
386 __func__, __LINE__, rc);
389 rc = netlbl_cfg_unlbl_add_map(smack_net_ambient, &audit_info);
390 if (rc != 0)
391 printk(KERN_WARNING "%s:%d add rc = %d\n",
392 __func__, __LINE__, rc);
396 * Seq_file read operations for /smack/cipso
399 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
401 if (*pos == SEQ_READ_FINISHED)
402 return NULL;
404 return smack_known;
407 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
409 struct smack_known *skp = ((struct smack_known *) v)->smk_next;
412 * Omit labels with no associated cipso value
414 while (skp != NULL && !skp->smk_cipso)
415 skp = skp->smk_next;
417 if (skp == NULL)
418 *pos = SEQ_READ_FINISHED;
420 return skp;
424 * Print cipso labels in format:
425 * label level[/cat[,cat]]
427 static int cipso_seq_show(struct seq_file *s, void *v)
429 struct smack_known *skp = (struct smack_known *) v;
430 struct smack_cipso *scp = skp->smk_cipso;
431 char *cbp;
432 char sep = '/';
433 int cat = 1;
434 int i;
435 unsigned char m;
437 if (scp == NULL)
438 return 0;
440 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
442 cbp = scp->smk_catset;
443 for (i = 0; i < SMK_LABELLEN; i++)
444 for (m = 0x80; m != 0; m >>= 1) {
445 if (m & cbp[i]) {
446 seq_printf(s, "%c%d", sep, cat);
447 sep = ',';
449 cat++;
452 seq_putc(s, '\n');
454 return 0;
457 static void cipso_seq_stop(struct seq_file *s, void *v)
459 /* No-op */
462 static struct seq_operations cipso_seq_ops = {
463 .start = cipso_seq_start,
464 .stop = cipso_seq_stop,
465 .next = cipso_seq_next,
466 .show = cipso_seq_show,
470 * smk_open_cipso - open() for /smack/cipso
471 * @inode: inode structure representing file
472 * @file: "cipso" file pointer
474 * Connect our cipso_seq_* operations with /smack/cipso
475 * file_operations
477 static int smk_open_cipso(struct inode *inode, struct file *file)
479 return seq_open(file, &cipso_seq_ops);
483 * smk_write_cipso - write() for /smack/cipso
484 * @filp: file pointer, not actually used
485 * @buf: where to get the data from
486 * @count: bytes sent
487 * @ppos: where to start
489 * Accepts only one cipso rule per write call.
490 * Returns number of bytes written or error code, as appropriate
492 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
493 size_t count, loff_t *ppos)
495 struct smack_known *skp;
496 struct smack_cipso *scp = NULL;
497 char mapcatset[SMK_LABELLEN];
498 int maplevel;
499 int cat;
500 int catlen;
501 ssize_t rc = -EINVAL;
502 char *data = NULL;
503 char *rule;
504 int ret;
505 int i;
508 * Must have privilege.
509 * No partial writes.
510 * Enough data must be present.
512 if (!capable(CAP_MAC_ADMIN))
513 return -EPERM;
514 if (*ppos != 0)
515 return -EINVAL;
516 if (count <= SMK_CIPSOMIN)
517 return -EINVAL;
519 data = kzalloc(count + 1, GFP_KERNEL);
520 if (data == NULL)
521 return -ENOMEM;
523 if (copy_from_user(data, buf, count) != 0) {
524 rc = -EFAULT;
525 goto unlockedout;
528 data[count] = '\0';
529 rule = data;
531 * Only allow one writer at a time. Writes should be
532 * quite rare and small in any case.
534 mutex_lock(&smack_cipso_lock);
536 skp = smk_import_entry(rule, 0);
537 if (skp == NULL)
538 goto out;
540 rule += SMK_LABELLEN;;
541 ret = sscanf(rule, "%d", &maplevel);
542 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
543 goto out;
545 rule += SMK_DIGITLEN;
546 ret = sscanf(rule, "%d", &catlen);
547 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
548 goto out;
550 if (count <= (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
551 goto out;
553 memset(mapcatset, 0, sizeof(mapcatset));
555 for (i = 0; i < catlen; i++) {
556 rule += SMK_DIGITLEN;
557 ret = sscanf(rule, "%d", &cat);
558 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
559 goto out;
561 smack_catset_bit(cat, mapcatset);
564 if (skp->smk_cipso == NULL) {
565 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
566 if (scp == NULL) {
567 rc = -ENOMEM;
568 goto out;
572 spin_lock_bh(&skp->smk_cipsolock);
574 if (scp == NULL)
575 scp = skp->smk_cipso;
576 else
577 skp->smk_cipso = scp;
579 scp->smk_level = maplevel;
580 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
582 spin_unlock_bh(&skp->smk_cipsolock);
584 rc = count;
585 out:
586 mutex_unlock(&smack_cipso_lock);
587 unlockedout:
588 kfree(data);
589 return rc;
592 static const struct file_operations smk_cipso_ops = {
593 .open = smk_open_cipso,
594 .read = seq_read,
595 .llseek = seq_lseek,
596 .write = smk_write_cipso,
597 .release = seq_release,
601 * smk_read_doi - read() for /smack/doi
602 * @filp: file pointer, not actually used
603 * @buf: where to put the result
604 * @count: maximum to send along
605 * @ppos: where to start
607 * Returns number of bytes read or error code, as appropriate
609 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
610 size_t count, loff_t *ppos)
612 char temp[80];
613 ssize_t rc;
615 if (*ppos != 0)
616 return 0;
618 sprintf(temp, "%d", smk_cipso_doi_value);
619 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
621 return rc;
625 * smk_write_doi - write() for /smack/doi
626 * @filp: file pointer, not actually used
627 * @buf: where to get the data from
628 * @count: bytes sent
629 * @ppos: where to start
631 * Returns number of bytes written or error code, as appropriate
633 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
634 size_t count, loff_t *ppos)
636 char temp[80];
637 int i;
639 if (!capable(CAP_MAC_ADMIN))
640 return -EPERM;
642 if (count >= sizeof(temp) || count == 0)
643 return -EINVAL;
645 if (copy_from_user(temp, buf, count) != 0)
646 return -EFAULT;
648 temp[count] = '\0';
650 if (sscanf(temp, "%d", &i) != 1)
651 return -EINVAL;
653 smk_cipso_doi_value = i;
655 smk_cipso_doi();
657 return count;
660 static const struct file_operations smk_doi_ops = {
661 .read = smk_read_doi,
662 .write = smk_write_doi,
666 * smk_read_direct - read() for /smack/direct
667 * @filp: file pointer, not actually used
668 * @buf: where to put the result
669 * @count: maximum to send along
670 * @ppos: where to start
672 * Returns number of bytes read or error code, as appropriate
674 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
675 size_t count, loff_t *ppos)
677 char temp[80];
678 ssize_t rc;
680 if (*ppos != 0)
681 return 0;
683 sprintf(temp, "%d", smack_cipso_direct);
684 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
686 return rc;
690 * smk_write_direct - write() for /smack/direct
691 * @filp: file pointer, not actually used
692 * @buf: where to get the data from
693 * @count: bytes sent
694 * @ppos: where to start
696 * Returns number of bytes written or error code, as appropriate
698 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
699 size_t count, loff_t *ppos)
701 char temp[80];
702 int i;
704 if (!capable(CAP_MAC_ADMIN))
705 return -EPERM;
707 if (count >= sizeof(temp) || count == 0)
708 return -EINVAL;
710 if (copy_from_user(temp, buf, count) != 0)
711 return -EFAULT;
713 temp[count] = '\0';
715 if (sscanf(temp, "%d", &i) != 1)
716 return -EINVAL;
718 smack_cipso_direct = i;
720 return count;
723 static const struct file_operations smk_direct_ops = {
724 .read = smk_read_direct,
725 .write = smk_write_direct,
729 * smk_read_ambient - read() for /smack/ambient
730 * @filp: file pointer, not actually used
731 * @buf: where to put the result
732 * @cn: maximum to send along
733 * @ppos: where to start
735 * Returns number of bytes read or error code, as appropriate
737 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
738 size_t cn, loff_t *ppos)
740 ssize_t rc;
741 int asize;
743 if (*ppos != 0)
744 return 0;
746 * Being careful to avoid a problem in the case where
747 * smack_net_ambient gets changed in midstream.
749 mutex_lock(&smack_ambient_lock);
751 asize = strlen(smack_net_ambient) + 1;
753 if (cn >= asize)
754 rc = simple_read_from_buffer(buf, cn, ppos,
755 smack_net_ambient, asize);
756 else
757 rc = -EINVAL;
759 mutex_unlock(&smack_ambient_lock);
761 return rc;
765 * smk_write_ambient - write() for /smack/ambient
766 * @filp: file pointer, not actually used
767 * @buf: where to get the data from
768 * @count: bytes sent
769 * @ppos: where to start
771 * Returns number of bytes written or error code, as appropriate
773 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
774 size_t count, loff_t *ppos)
776 char in[SMK_LABELLEN];
777 char *oldambient;
778 char *smack;
780 if (!capable(CAP_MAC_ADMIN))
781 return -EPERM;
783 if (count >= SMK_LABELLEN)
784 return -EINVAL;
786 if (copy_from_user(in, buf, count) != 0)
787 return -EFAULT;
789 smack = smk_import(in, count);
790 if (smack == NULL)
791 return -EINVAL;
793 mutex_lock(&smack_ambient_lock);
795 oldambient = smack_net_ambient;
796 smack_net_ambient = smack;
797 smk_unlbl_ambient(oldambient);
799 mutex_unlock(&smack_ambient_lock);
801 return count;
804 static const struct file_operations smk_ambient_ops = {
805 .read = smk_read_ambient,
806 .write = smk_write_ambient,
809 struct option_names {
810 int o_number;
811 char *o_name;
812 char *o_alias;
815 static struct option_names netlbl_choices[] = {
816 { NETLBL_NLTYPE_RIPSO,
817 NETLBL_NLTYPE_RIPSO_NAME, "ripso" },
818 { NETLBL_NLTYPE_CIPSOV4,
819 NETLBL_NLTYPE_CIPSOV4_NAME, "cipsov4" },
820 { NETLBL_NLTYPE_CIPSOV4,
821 NETLBL_NLTYPE_CIPSOV4_NAME, "cipso" },
822 { NETLBL_NLTYPE_CIPSOV6,
823 NETLBL_NLTYPE_CIPSOV6_NAME, "cipsov6" },
824 { NETLBL_NLTYPE_UNLABELED,
825 NETLBL_NLTYPE_UNLABELED_NAME, "unlabeled" },
829 * smk_read_nltype - read() for /smack/nltype
830 * @filp: file pointer, not actually used
831 * @buf: where to put the result
832 * @count: maximum to send along
833 * @ppos: where to start
835 * Returns number of bytes read or error code, as appropriate
837 static ssize_t smk_read_nltype(struct file *filp, char __user *buf,
838 size_t count, loff_t *ppos)
840 char bound[40];
841 ssize_t rc;
842 int i;
844 if (count < SMK_LABELLEN)
845 return -EINVAL;
847 if (*ppos != 0)
848 return 0;
850 sprintf(bound, "unknown");
852 for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
853 if (smack_net_nltype == netlbl_choices[i].o_number) {
854 sprintf(bound, "%s", netlbl_choices[i].o_name);
855 break;
858 rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound));
860 return rc;
864 * smk_write_nltype - write() for /smack/nltype
865 * @filp: file pointer, not actually used
866 * @buf: where to get the data from
867 * @count: bytes sent
868 * @ppos: where to start
870 * Returns number of bytes written or error code, as appropriate
872 static ssize_t smk_write_nltype(struct file *file, const char __user *buf,
873 size_t count, loff_t *ppos)
875 char bound[40];
876 char *cp;
877 int i;
879 if (!capable(CAP_MAC_ADMIN))
880 return -EPERM;
882 if (count >= 40)
883 return -EINVAL;
885 if (copy_from_user(bound, buf, count) != 0)
886 return -EFAULT;
888 bound[count] = '\0';
889 cp = strchr(bound, ' ');
890 if (cp != NULL)
891 *cp = '\0';
892 cp = strchr(bound, '\n');
893 if (cp != NULL)
894 *cp = '\0';
896 for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
897 if (strcmp(bound, netlbl_choices[i].o_name) == 0 ||
898 strcmp(bound, netlbl_choices[i].o_alias) == 0) {
899 smack_net_nltype = netlbl_choices[i].o_number;
900 return count;
903 * Not a valid choice.
905 return -EINVAL;
908 static const struct file_operations smk_nltype_ops = {
909 .read = smk_read_nltype,
910 .write = smk_write_nltype,
914 * smk_fill_super - fill the /smackfs superblock
915 * @sb: the empty superblock
916 * @data: unused
917 * @silent: unused
919 * Fill in the well known entries for /smack
921 * Returns 0 on success, an error code on failure
923 static int smk_fill_super(struct super_block *sb, void *data, int silent)
925 int rc;
926 struct inode *root_inode;
928 static struct tree_descr smack_files[] = {
929 [SMK_LOAD] =
930 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
931 [SMK_CIPSO] =
932 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
933 [SMK_DOI] =
934 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
935 [SMK_DIRECT] =
936 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
937 [SMK_AMBIENT] =
938 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
939 [SMK_NLTYPE] =
940 {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR},
941 /* last one */ {""}
944 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
945 if (rc != 0) {
946 printk(KERN_ERR "%s failed %d while creating inodes\n",
947 __func__, rc);
948 return rc;
951 root_inode = sb->s_root->d_inode;
952 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
954 return 0;
958 * smk_get_sb - get the smackfs superblock
959 * @fs_type: passed along without comment
960 * @flags: passed along without comment
961 * @dev_name: passed along without comment
962 * @data: passed along without comment
963 * @mnt: passed along without comment
965 * Just passes everything along.
967 * Returns what the lower level code does.
969 static int smk_get_sb(struct file_system_type *fs_type,
970 int flags, const char *dev_name, void *data,
971 struct vfsmount *mnt)
973 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
976 static struct file_system_type smk_fs_type = {
977 .name = "smackfs",
978 .get_sb = smk_get_sb,
979 .kill_sb = kill_litter_super,
982 static struct vfsmount *smackfs_mount;
985 * init_smk_fs - get the smackfs superblock
987 * register the smackfs
989 * Returns 0 unless the registration fails.
991 static int __init init_smk_fs(void)
993 int err;
995 err = register_filesystem(&smk_fs_type);
996 if (!err) {
997 smackfs_mount = kern_mount(&smk_fs_type);
998 if (IS_ERR(smackfs_mount)) {
999 printk(KERN_ERR "smackfs: could not mount!\n");
1000 err = PTR_ERR(smackfs_mount);
1001 smackfs_mount = NULL;
1005 sema_init(&smack_write_sem, 1);
1006 smk_cipso_doi();
1007 smk_unlbl_ambient(NULL);
1009 return err;
1012 __initcall(init_smk_fs);