[ARM] 4872/1: Replaces buggy macro in S3C2410 irq include
[linux-2.6/libata-dev.git] / security / smack / smackfs.c
blobafe7c9b0732ac6c16ae6677bbfe1ca0ca5ecf799
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_CIPSOMIN: Minimum possible cipso rule length.
85 * SMK_CIPSOMAX: Maximum possible cipso rule length.
87 #define SMK_DIGITLEN 4
88 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
89 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
92 * Values for parsing MAC rules
93 * SMK_ACCESS: Maximum possible combination of access permissions
94 * SMK_ACCESSLEN: Maximum length for a rule access field
95 * SMK_LOADLEN: Smack rule length
97 #define SMK_ACCESS "rwxa"
98 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
99 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
103 * Seq_file read operations for /smack/load
106 static void *load_seq_start(struct seq_file *s, loff_t *pos)
108 if (*pos == SEQ_READ_FINISHED)
109 return NULL;
111 return smack_list;
114 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
116 struct smk_list_entry *skp = ((struct smk_list_entry *) v)->smk_next;
118 if (skp == NULL)
119 *pos = SEQ_READ_FINISHED;
121 return skp;
124 static int load_seq_show(struct seq_file *s, void *v)
126 struct smk_list_entry *slp = (struct smk_list_entry *) v;
127 struct smack_rule *srp = &slp->smk_rule;
129 seq_printf(s, "%s %s", (char *)srp->smk_subject,
130 (char *)srp->smk_object);
132 seq_putc(s, ' ');
134 if (srp->smk_access & MAY_READ)
135 seq_putc(s, 'r');
136 if (srp->smk_access & MAY_WRITE)
137 seq_putc(s, 'w');
138 if (srp->smk_access & MAY_EXEC)
139 seq_putc(s, 'x');
140 if (srp->smk_access & MAY_APPEND)
141 seq_putc(s, 'a');
142 if (srp->smk_access == 0)
143 seq_putc(s, '-');
145 seq_putc(s, '\n');
147 return 0;
150 static void load_seq_stop(struct seq_file *s, void *v)
152 /* No-op */
155 static struct seq_operations load_seq_ops = {
156 .start = load_seq_start,
157 .next = load_seq_next,
158 .show = load_seq_show,
159 .stop = load_seq_stop,
163 * smk_open_load - open() for /smack/load
164 * @inode: inode structure representing file
165 * @file: "load" file pointer
167 * For reading, use load_seq_* seq_file reading operations.
169 static int smk_open_load(struct inode *inode, struct file *file)
171 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
172 return seq_open(file, &load_seq_ops);
174 if (down_interruptible(&smack_write_sem))
175 return -ERESTARTSYS;
177 return 0;
181 * smk_release_load - release() for /smack/load
182 * @inode: inode structure representing file
183 * @file: "load" file pointer
185 * For a reading session, use the seq_file release
186 * implementation.
187 * Otherwise, we are at the end of a writing session so
188 * clean everything up.
190 static int smk_release_load(struct inode *inode, struct file *file)
192 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
193 return seq_release(inode, file);
195 up(&smack_write_sem);
196 return 0;
200 * smk_set_access - add a rule to the rule list
201 * @srp: the new rule to add
203 * Looks through the current subject/object/access list for
204 * the subject/object pair and replaces the access that was
205 * there. If the pair isn't found add it with the specified
206 * access.
208 static void smk_set_access(struct smack_rule *srp)
210 struct smk_list_entry *sp;
211 struct smk_list_entry *newp;
213 mutex_lock(&smack_list_lock);
215 for (sp = smack_list; sp != NULL; sp = sp->smk_next)
216 if (sp->smk_rule.smk_subject == srp->smk_subject &&
217 sp->smk_rule.smk_object == srp->smk_object) {
218 sp->smk_rule.smk_access = srp->smk_access;
219 break;
222 if (sp == NULL) {
223 newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
224 newp->smk_rule = *srp;
225 newp->smk_next = smack_list;
226 smack_list = newp;
229 mutex_unlock(&smack_list_lock);
231 return;
235 * smk_write_load - write() for /smack/load
236 * @filp: file pointer, not actually used
237 * @buf: where to get the data from
238 * @count: bytes sent
239 * @ppos: where to start - must be 0
241 * Get one smack access rule from above.
242 * The format is exactly:
243 * char subject[SMK_LABELLEN]
244 * char object[SMK_LABELLEN]
245 * char access[SMK_ACCESSLEN]
247 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
249 static ssize_t smk_write_load(struct file *file, const char __user *buf,
250 size_t count, loff_t *ppos)
252 struct smack_rule rule;
253 char *data;
254 int rc = -EINVAL;
257 * Must have privilege.
258 * No partial writes.
259 * Enough data must be present.
261 if (!capable(CAP_MAC_ADMIN))
262 return -EPERM;
263 if (*ppos != 0)
264 return -EINVAL;
265 if (count != SMK_LOADLEN)
266 return -EINVAL;
268 data = kzalloc(count, GFP_KERNEL);
269 if (data == NULL)
270 return -ENOMEM;
272 if (copy_from_user(data, buf, count) != 0) {
273 rc = -EFAULT;
274 goto out;
277 rule.smk_subject = smk_import(data, 0);
278 if (rule.smk_subject == NULL)
279 goto out;
281 rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
282 if (rule.smk_object == NULL)
283 goto out;
285 rule.smk_access = 0;
287 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
288 case '-':
289 break;
290 case 'r':
291 case 'R':
292 rule.smk_access |= MAY_READ;
293 break;
294 default:
295 goto out;
298 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
299 case '-':
300 break;
301 case 'w':
302 case 'W':
303 rule.smk_access |= MAY_WRITE;
304 break;
305 default:
306 goto out;
309 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
310 case '-':
311 break;
312 case 'x':
313 case 'X':
314 rule.smk_access |= MAY_EXEC;
315 break;
316 default:
317 goto out;
320 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
321 case '-':
322 break;
323 case 'a':
324 case 'A':
325 rule.smk_access |= MAY_READ;
326 break;
327 default:
328 goto out;
331 smk_set_access(&rule);
332 rc = count;
334 out:
335 kfree(data);
336 return rc;
339 static const struct file_operations smk_load_ops = {
340 .open = smk_open_load,
341 .read = seq_read,
342 .llseek = seq_lseek,
343 .write = smk_write_load,
344 .release = smk_release_load,
348 * smk_cipso_doi - initialize the CIPSO domain
350 void smk_cipso_doi(void)
352 int rc;
353 struct cipso_v4_doi *doip;
354 struct netlbl_audit audit_info;
356 audit_info.loginuid = audit_get_loginuid(current);
357 audit_info.secid = smack_to_secid(current->security);
359 rc = netlbl_cfg_map_del(NULL, &audit_info);
360 if (rc != 0)
361 printk(KERN_WARNING "%s:%d remove rc = %d\n",
362 __func__, __LINE__, rc);
364 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
365 if (doip == NULL)
366 panic("smack: Failed to initialize cipso DOI.\n");
367 doip->map.std = NULL;
368 doip->doi = smk_cipso_doi_value;
369 doip->type = CIPSO_V4_MAP_PASS;
370 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
371 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
372 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
374 rc = netlbl_cfg_cipsov4_add_map(doip, NULL, &audit_info);
375 if (rc != 0)
376 printk(KERN_WARNING "%s:%d add rc = %d\n",
377 __func__, __LINE__, rc);
381 * smk_unlbl_ambient - initialize the unlabeled domain
383 void smk_unlbl_ambient(char *oldambient)
385 int rc;
386 struct netlbl_audit audit_info;
388 audit_info.loginuid = audit_get_loginuid(current);
389 audit_info.secid = smack_to_secid(current->security);
391 if (oldambient != NULL) {
392 rc = netlbl_cfg_map_del(oldambient, &audit_info);
393 if (rc != 0)
394 printk(KERN_WARNING "%s:%d remove rc = %d\n",
395 __func__, __LINE__, rc);
398 rc = netlbl_cfg_unlbl_add_map(smack_net_ambient, &audit_info);
399 if (rc != 0)
400 printk(KERN_WARNING "%s:%d add rc = %d\n",
401 __func__, __LINE__, rc);
405 * Seq_file read operations for /smack/cipso
408 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
410 if (*pos == SEQ_READ_FINISHED)
411 return NULL;
413 return smack_known;
416 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
418 struct smack_known *skp = ((struct smack_known *) v)->smk_next;
421 * Omit labels with no associated cipso value
423 while (skp != NULL && !skp->smk_cipso)
424 skp = skp->smk_next;
426 if (skp == NULL)
427 *pos = SEQ_READ_FINISHED;
429 return skp;
433 * Print cipso labels in format:
434 * label level[/cat[,cat]]
436 static int cipso_seq_show(struct seq_file *s, void *v)
438 struct smack_known *skp = (struct smack_known *) v;
439 struct smack_cipso *scp = skp->smk_cipso;
440 char *cbp;
441 char sep = '/';
442 int cat = 1;
443 int i;
444 unsigned char m;
446 if (scp == NULL)
447 return 0;
449 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
451 cbp = scp->smk_catset;
452 for (i = 0; i < SMK_LABELLEN; i++)
453 for (m = 0x80; m != 0; m >>= 1) {
454 if (m & cbp[i]) {
455 seq_printf(s, "%c%d", sep, cat);
456 sep = ',';
458 cat++;
461 seq_putc(s, '\n');
463 return 0;
466 static void cipso_seq_stop(struct seq_file *s, void *v)
468 /* No-op */
471 static struct seq_operations cipso_seq_ops = {
472 .start = cipso_seq_start,
473 .stop = cipso_seq_stop,
474 .next = cipso_seq_next,
475 .show = cipso_seq_show,
479 * smk_open_cipso - open() for /smack/cipso
480 * @inode: inode structure representing file
481 * @file: "cipso" file pointer
483 * Connect our cipso_seq_* operations with /smack/cipso
484 * file_operations
486 static int smk_open_cipso(struct inode *inode, struct file *file)
488 return seq_open(file, &cipso_seq_ops);
492 * smk_write_cipso - write() for /smack/cipso
493 * @filp: file pointer, not actually used
494 * @buf: where to get the data from
495 * @count: bytes sent
496 * @ppos: where to start
498 * Accepts only one cipso rule per write call.
499 * Returns number of bytes written or error code, as appropriate
501 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
502 size_t count, loff_t *ppos)
504 struct smack_known *skp;
505 struct smack_cipso *scp = NULL;
506 char mapcatset[SMK_LABELLEN];
507 int maplevel;
508 int cat;
509 int catlen;
510 ssize_t rc = -EINVAL;
511 char *data = NULL;
512 char *rule;
513 int ret;
514 int i;
517 * Must have privilege.
518 * No partial writes.
519 * Enough data must be present.
521 if (!capable(CAP_MAC_ADMIN))
522 return -EPERM;
523 if (*ppos != 0)
524 return -EINVAL;
525 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
526 return -EINVAL;
528 data = kzalloc(count + 1, GFP_KERNEL);
529 if (data == NULL)
530 return -ENOMEM;
532 if (copy_from_user(data, buf, count) != 0) {
533 rc = -EFAULT;
534 goto unlockedout;
537 data[count] = '\0';
538 rule = data;
540 * Only allow one writer at a time. Writes should be
541 * quite rare and small in any case.
543 mutex_lock(&smack_cipso_lock);
545 skp = smk_import_entry(rule, 0);
546 if (skp == NULL)
547 goto out;
549 rule += SMK_LABELLEN;;
550 ret = sscanf(rule, "%d", &maplevel);
551 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
552 goto out;
554 rule += SMK_DIGITLEN;
555 ret = sscanf(rule, "%d", &catlen);
556 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
557 goto out;
559 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
560 goto out;
562 memset(mapcatset, 0, sizeof(mapcatset));
564 for (i = 0; i < catlen; i++) {
565 rule += SMK_DIGITLEN;
566 ret = sscanf(rule, "%d", &cat);
567 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
568 goto out;
570 smack_catset_bit(cat, mapcatset);
573 if (skp->smk_cipso == NULL) {
574 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
575 if (scp == NULL) {
576 rc = -ENOMEM;
577 goto out;
581 spin_lock_bh(&skp->smk_cipsolock);
583 if (scp == NULL)
584 scp = skp->smk_cipso;
585 else
586 skp->smk_cipso = scp;
588 scp->smk_level = maplevel;
589 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
591 spin_unlock_bh(&skp->smk_cipsolock);
593 rc = count;
594 out:
595 mutex_unlock(&smack_cipso_lock);
596 unlockedout:
597 kfree(data);
598 return rc;
601 static const struct file_operations smk_cipso_ops = {
602 .open = smk_open_cipso,
603 .read = seq_read,
604 .llseek = seq_lseek,
605 .write = smk_write_cipso,
606 .release = seq_release,
610 * smk_read_doi - read() for /smack/doi
611 * @filp: file pointer, not actually used
612 * @buf: where to put the result
613 * @count: maximum to send along
614 * @ppos: where to start
616 * Returns number of bytes read or error code, as appropriate
618 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
619 size_t count, loff_t *ppos)
621 char temp[80];
622 ssize_t rc;
624 if (*ppos != 0)
625 return 0;
627 sprintf(temp, "%d", smk_cipso_doi_value);
628 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
630 return rc;
634 * smk_write_doi - write() for /smack/doi
635 * @filp: file pointer, not actually used
636 * @buf: where to get the data from
637 * @count: bytes sent
638 * @ppos: where to start
640 * Returns number of bytes written or error code, as appropriate
642 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
643 size_t count, loff_t *ppos)
645 char temp[80];
646 int i;
648 if (!capable(CAP_MAC_ADMIN))
649 return -EPERM;
651 if (count >= sizeof(temp) || count == 0)
652 return -EINVAL;
654 if (copy_from_user(temp, buf, count) != 0)
655 return -EFAULT;
657 temp[count] = '\0';
659 if (sscanf(temp, "%d", &i) != 1)
660 return -EINVAL;
662 smk_cipso_doi_value = i;
664 smk_cipso_doi();
666 return count;
669 static const struct file_operations smk_doi_ops = {
670 .read = smk_read_doi,
671 .write = smk_write_doi,
675 * smk_read_direct - read() for /smack/direct
676 * @filp: file pointer, not actually used
677 * @buf: where to put the result
678 * @count: maximum to send along
679 * @ppos: where to start
681 * Returns number of bytes read or error code, as appropriate
683 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
684 size_t count, loff_t *ppos)
686 char temp[80];
687 ssize_t rc;
689 if (*ppos != 0)
690 return 0;
692 sprintf(temp, "%d", smack_cipso_direct);
693 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
695 return rc;
699 * smk_write_direct - write() for /smack/direct
700 * @filp: file pointer, not actually used
701 * @buf: where to get the data from
702 * @count: bytes sent
703 * @ppos: where to start
705 * Returns number of bytes written or error code, as appropriate
707 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
708 size_t count, loff_t *ppos)
710 char temp[80];
711 int i;
713 if (!capable(CAP_MAC_ADMIN))
714 return -EPERM;
716 if (count >= sizeof(temp) || count == 0)
717 return -EINVAL;
719 if (copy_from_user(temp, buf, count) != 0)
720 return -EFAULT;
722 temp[count] = '\0';
724 if (sscanf(temp, "%d", &i) != 1)
725 return -EINVAL;
727 smack_cipso_direct = i;
729 return count;
732 static const struct file_operations smk_direct_ops = {
733 .read = smk_read_direct,
734 .write = smk_write_direct,
738 * smk_read_ambient - read() for /smack/ambient
739 * @filp: file pointer, not actually used
740 * @buf: where to put the result
741 * @cn: maximum to send along
742 * @ppos: where to start
744 * Returns number of bytes read or error code, as appropriate
746 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
747 size_t cn, loff_t *ppos)
749 ssize_t rc;
750 int asize;
752 if (*ppos != 0)
753 return 0;
755 * Being careful to avoid a problem in the case where
756 * smack_net_ambient gets changed in midstream.
758 mutex_lock(&smack_ambient_lock);
760 asize = strlen(smack_net_ambient) + 1;
762 if (cn >= asize)
763 rc = simple_read_from_buffer(buf, cn, ppos,
764 smack_net_ambient, asize);
765 else
766 rc = -EINVAL;
768 mutex_unlock(&smack_ambient_lock);
770 return rc;
774 * smk_write_ambient - write() for /smack/ambient
775 * @filp: file pointer, not actually used
776 * @buf: where to get the data from
777 * @count: bytes sent
778 * @ppos: where to start
780 * Returns number of bytes written or error code, as appropriate
782 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
783 size_t count, loff_t *ppos)
785 char in[SMK_LABELLEN];
786 char *oldambient;
787 char *smack;
789 if (!capable(CAP_MAC_ADMIN))
790 return -EPERM;
792 if (count >= SMK_LABELLEN)
793 return -EINVAL;
795 if (copy_from_user(in, buf, count) != 0)
796 return -EFAULT;
798 smack = smk_import(in, count);
799 if (smack == NULL)
800 return -EINVAL;
802 mutex_lock(&smack_ambient_lock);
804 oldambient = smack_net_ambient;
805 smack_net_ambient = smack;
806 smk_unlbl_ambient(oldambient);
808 mutex_unlock(&smack_ambient_lock);
810 return count;
813 static const struct file_operations smk_ambient_ops = {
814 .read = smk_read_ambient,
815 .write = smk_write_ambient,
818 struct option_names {
819 int o_number;
820 char *o_name;
821 char *o_alias;
824 static struct option_names netlbl_choices[] = {
825 { NETLBL_NLTYPE_RIPSO,
826 NETLBL_NLTYPE_RIPSO_NAME, "ripso" },
827 { NETLBL_NLTYPE_CIPSOV4,
828 NETLBL_NLTYPE_CIPSOV4_NAME, "cipsov4" },
829 { NETLBL_NLTYPE_CIPSOV4,
830 NETLBL_NLTYPE_CIPSOV4_NAME, "cipso" },
831 { NETLBL_NLTYPE_CIPSOV6,
832 NETLBL_NLTYPE_CIPSOV6_NAME, "cipsov6" },
833 { NETLBL_NLTYPE_UNLABELED,
834 NETLBL_NLTYPE_UNLABELED_NAME, "unlabeled" },
838 * smk_read_nltype - read() for /smack/nltype
839 * @filp: file pointer, not actually used
840 * @buf: where to put the result
841 * @count: maximum to send along
842 * @ppos: where to start
844 * Returns number of bytes read or error code, as appropriate
846 static ssize_t smk_read_nltype(struct file *filp, char __user *buf,
847 size_t count, loff_t *ppos)
849 char bound[40];
850 ssize_t rc;
851 int i;
853 if (count < SMK_LABELLEN)
854 return -EINVAL;
856 if (*ppos != 0)
857 return 0;
859 sprintf(bound, "unknown");
861 for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
862 if (smack_net_nltype == netlbl_choices[i].o_number) {
863 sprintf(bound, "%s", netlbl_choices[i].o_name);
864 break;
867 rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound));
869 return rc;
873 * smk_write_nltype - write() for /smack/nltype
874 * @filp: file pointer, not actually used
875 * @buf: where to get the data from
876 * @count: bytes sent
877 * @ppos: where to start
879 * Returns number of bytes written or error code, as appropriate
881 static ssize_t smk_write_nltype(struct file *file, const char __user *buf,
882 size_t count, loff_t *ppos)
884 char bound[40];
885 char *cp;
886 int i;
888 if (!capable(CAP_MAC_ADMIN))
889 return -EPERM;
891 if (count >= 40)
892 return -EINVAL;
894 if (copy_from_user(bound, buf, count) != 0)
895 return -EFAULT;
897 bound[count] = '\0';
898 cp = strchr(bound, ' ');
899 if (cp != NULL)
900 *cp = '\0';
901 cp = strchr(bound, '\n');
902 if (cp != NULL)
903 *cp = '\0';
905 for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
906 if (strcmp(bound, netlbl_choices[i].o_name) == 0 ||
907 strcmp(bound, netlbl_choices[i].o_alias) == 0) {
908 smack_net_nltype = netlbl_choices[i].o_number;
909 return count;
912 * Not a valid choice.
914 return -EINVAL;
917 static const struct file_operations smk_nltype_ops = {
918 .read = smk_read_nltype,
919 .write = smk_write_nltype,
923 * smk_fill_super - fill the /smackfs superblock
924 * @sb: the empty superblock
925 * @data: unused
926 * @silent: unused
928 * Fill in the well known entries for /smack
930 * Returns 0 on success, an error code on failure
932 static int smk_fill_super(struct super_block *sb, void *data, int silent)
934 int rc;
935 struct inode *root_inode;
937 static struct tree_descr smack_files[] = {
938 [SMK_LOAD] =
939 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
940 [SMK_CIPSO] =
941 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
942 [SMK_DOI] =
943 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
944 [SMK_DIRECT] =
945 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
946 [SMK_AMBIENT] =
947 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
948 [SMK_NLTYPE] =
949 {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR},
950 /* last one */ {""}
953 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
954 if (rc != 0) {
955 printk(KERN_ERR "%s failed %d while creating inodes\n",
956 __func__, rc);
957 return rc;
960 root_inode = sb->s_root->d_inode;
961 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
963 return 0;
967 * smk_get_sb - get the smackfs superblock
968 * @fs_type: passed along without comment
969 * @flags: passed along without comment
970 * @dev_name: passed along without comment
971 * @data: passed along without comment
972 * @mnt: passed along without comment
974 * Just passes everything along.
976 * Returns what the lower level code does.
978 static int smk_get_sb(struct file_system_type *fs_type,
979 int flags, const char *dev_name, void *data,
980 struct vfsmount *mnt)
982 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
985 static struct file_system_type smk_fs_type = {
986 .name = "smackfs",
987 .get_sb = smk_get_sb,
988 .kill_sb = kill_litter_super,
991 static struct vfsmount *smackfs_mount;
994 * init_smk_fs - get the smackfs superblock
996 * register the smackfs
998 * Returns 0 unless the registration fails.
1000 static int __init init_smk_fs(void)
1002 int err;
1004 err = register_filesystem(&smk_fs_type);
1005 if (!err) {
1006 smackfs_mount = kern_mount(&smk_fs_type);
1007 if (IS_ERR(smackfs_mount)) {
1008 printk(KERN_ERR "smackfs: could not mount!\n");
1009 err = PTR_ERR(smackfs_mount);
1010 smackfs_mount = NULL;
1014 sema_init(&smack_write_sem, 1);
1015 smk_cipso_doi();
1016 smk_unlbl_ambient(NULL);
1018 return err;
1021 __initcall(init_smk_fs);