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.
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>
31 * smackfs pseudo filesystem.
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 */
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)
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;
119 *pos = SEQ_READ_FINISHED;
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);
134 if (srp->smk_access & MAY_READ)
136 if (srp->smk_access & MAY_WRITE)
138 if (srp->smk_access & MAY_EXEC)
140 if (srp->smk_access & MAY_APPEND)
142 if (srp->smk_access == 0)
150 static void load_seq_stop(struct seq_file *s, void *v)
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))
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
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);
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
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;
223 newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
224 newp->smk_rule = *srp;
225 newp->smk_next = smack_list;
229 mutex_unlock(&smack_list_lock);
235 * smk_write_load - write() for /smack/load
236 * @filp: file pointer, not actually used
237 * @buf: where to get the data from
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;
257 * Must have privilege.
259 * Enough data must be present.
261 if (!capable(CAP_MAC_ADMIN))
265 if (count != SMK_LOADLEN)
268 data = kzalloc(count, GFP_KERNEL);
272 if (copy_from_user(data, buf, count) != 0) {
277 rule.smk_subject = smk_import(data, 0);
278 if (rule.smk_subject == NULL)
281 rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
282 if (rule.smk_object == NULL)
287 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
292 rule.smk_access |= MAY_READ;
298 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
303 rule.smk_access |= MAY_WRITE;
309 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
314 rule.smk_access |= MAY_EXEC;
320 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
325 rule.smk_access |= MAY_READ;
331 smk_set_access(&rule);
339 static const struct file_operations smk_load_ops = {
340 .open = smk_open_load,
343 .write = smk_write_load,
344 .release = smk_release_load,
348 * smk_cipso_doi - initialize the CIPSO domain
350 void smk_cipso_doi(void)
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);
361 printk(KERN_WARNING "%s:%d remove rc = %d\n",
362 __func__, __LINE__, rc);
364 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
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);
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)
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);
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);
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)
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)
427 *pos = SEQ_READ_FINISHED;
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;
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) {
455 seq_printf(s, "%c%d", sep, cat);
466 static void cipso_seq_stop(struct seq_file *s, void *v)
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
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
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];
510 ssize_t rc = -EINVAL;
517 * Must have privilege.
519 * Enough data must be present.
521 if (!capable(CAP_MAC_ADMIN))
525 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
528 data = kzalloc(count + 1, GFP_KERNEL);
532 if (copy_from_user(data, buf, count) != 0) {
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);
549 rule += SMK_LABELLEN;;
550 ret = sscanf(rule, "%d", &maplevel);
551 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
554 rule += SMK_DIGITLEN;
555 ret = sscanf(rule, "%d", &catlen);
556 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
559 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
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)
570 smack_catset_bit(cat, mapcatset);
573 if (skp->smk_cipso == NULL) {
574 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
581 spin_lock_bh(&skp->smk_cipsolock);
584 scp = skp->smk_cipso;
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);
595 mutex_unlock(&smack_cipso_lock);
601 static const struct file_operations smk_cipso_ops = {
602 .open = smk_open_cipso,
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)
627 sprintf(temp, "%d", smk_cipso_doi_value);
628 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
634 * smk_write_doi - write() for /smack/doi
635 * @filp: file pointer, not actually used
636 * @buf: where to get the data from
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)
648 if (!capable(CAP_MAC_ADMIN))
651 if (count >= sizeof(temp) || count == 0)
654 if (copy_from_user(temp, buf, count) != 0)
659 if (sscanf(temp, "%d", &i) != 1)
662 smk_cipso_doi_value = i;
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)
692 sprintf(temp, "%d", smack_cipso_direct);
693 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
699 * smk_write_direct - write() for /smack/direct
700 * @filp: file pointer, not actually used
701 * @buf: where to get the data from
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)
713 if (!capable(CAP_MAC_ADMIN))
716 if (count >= sizeof(temp) || count == 0)
719 if (copy_from_user(temp, buf, count) != 0)
724 if (sscanf(temp, "%d", &i) != 1)
727 smack_cipso_direct = i;
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)
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;
763 rc = simple_read_from_buffer(buf, cn, ppos,
764 smack_net_ambient, asize);
768 mutex_unlock(&smack_ambient_lock);
774 * smk_write_ambient - write() for /smack/ambient
775 * @filp: file pointer, not actually used
776 * @buf: where to get the data from
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];
789 if (!capable(CAP_MAC_ADMIN))
792 if (count >= SMK_LABELLEN)
795 if (copy_from_user(in, buf, count) != 0)
798 smack = smk_import(in, count);
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);
813 static const struct file_operations smk_ambient_ops = {
814 .read = smk_read_ambient,
815 .write = smk_write_ambient,
818 struct option_names {
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)
853 if (count < SMK_LABELLEN)
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);
867 rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound));
873 * smk_write_nltype - write() for /smack/nltype
874 * @filp: file pointer, not actually used
875 * @buf: where to get the data from
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)
888 if (!capable(CAP_MAC_ADMIN))
894 if (copy_from_user(bound, buf, count) != 0)
898 cp = strchr(bound, ' ');
901 cp = strchr(bound, '\n');
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;
912 * Not a valid choice.
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
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)
935 struct inode *root_inode;
937 static struct tree_descr smack_files[] = {
939 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
941 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
943 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
945 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
947 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
949 {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR},
953 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
955 printk(KERN_ERR "%s failed %d while creating inodes\n",
960 root_inode = sb->s_root->d_inode;
961 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
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 = {
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)
1004 err = register_filesystem(&smk_fs_type);
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);
1016 smk_unlbl_ambient(NULL);
1021 __initcall(init_smk_fs);