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_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)
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;
106 *pos = SEQ_READ_FINISHED;
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);
121 if (srp->smk_access & MAY_READ)
123 if (srp->smk_access & MAY_WRITE)
125 if (srp->smk_access & MAY_EXEC)
127 if (srp->smk_access & MAY_APPEND)
129 if (srp->smk_access == 0)
137 static void load_seq_stop(struct seq_file *s, void *v)
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))
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
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);
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
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;
210 newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
211 newp->smk_rule = *srp;
212 newp->smk_next = smack_list;
216 mutex_unlock(&smack_list_lock);
222 * smk_write_load - write() for /smack/load
223 * @filp: file pointer, not actually used
224 * @buf: where to get the data from
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;
248 * Must have privilege.
250 * Enough data must be present.
252 if (!capable(CAP_MAC_ADMIN))
256 if (count < MINIMUM_LOAD)
259 data = kzalloc(count, GFP_KERNEL);
263 if (copy_from_user(data, buf, count) != 0) {
268 rule.smk_subject = smk_import(data, 0);
269 if (rule.smk_subject == NULL)
272 rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
273 if (rule.smk_object == NULL)
278 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
283 rule.smk_access |= MAY_READ;
289 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
294 rule.smk_access |= MAY_WRITE;
300 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
305 rule.smk_access |= MAY_EXEC;
311 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
316 rule.smk_access |= MAY_READ;
322 smk_set_access(&rule);
330 static const struct file_operations smk_load_ops = {
331 .open = smk_open_load,
334 .write = smk_write_load,
335 .release = smk_release_load,
339 * smk_cipso_doi - initialize the CIPSO domain
341 void smk_cipso_doi(void)
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);
352 printk(KERN_WARNING "%s:%d remove rc = %d\n",
353 __func__, __LINE__, rc);
355 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
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);
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)
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);
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);
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)
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)
418 *pos = SEQ_READ_FINISHED;
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;
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) {
446 seq_printf(s, "%c%d", sep, cat);
457 static void cipso_seq_stop(struct seq_file *s, void *v)
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
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
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];
501 ssize_t rc = -EINVAL;
508 * Must have privilege.
510 * Enough data must be present.
512 if (!capable(CAP_MAC_ADMIN))
516 if (count <= SMK_CIPSOMIN)
519 data = kzalloc(count + 1, GFP_KERNEL);
523 if (copy_from_user(data, buf, count) != 0) {
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);
540 rule += SMK_LABELLEN;;
541 ret = sscanf(rule, "%d", &maplevel);
542 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
545 rule += SMK_DIGITLEN;
546 ret = sscanf(rule, "%d", &catlen);
547 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
550 if (count <= (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
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)
561 smack_catset_bit(cat, mapcatset);
564 if (skp->smk_cipso == NULL) {
565 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
572 spin_lock_bh(&skp->smk_cipsolock);
575 scp = skp->smk_cipso;
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);
586 mutex_unlock(&smack_cipso_lock);
592 static const struct file_operations smk_cipso_ops = {
593 .open = smk_open_cipso,
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)
618 sprintf(temp, "%d", smk_cipso_doi_value);
619 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
625 * smk_write_doi - write() for /smack/doi
626 * @filp: file pointer, not actually used
627 * @buf: where to get the data from
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)
639 if (!capable(CAP_MAC_ADMIN))
642 if (count >= sizeof(temp) || count == 0)
645 if (copy_from_user(temp, buf, count) != 0)
650 if (sscanf(temp, "%d", &i) != 1)
653 smk_cipso_doi_value = i;
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)
683 sprintf(temp, "%d", smack_cipso_direct);
684 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
690 * smk_write_direct - write() for /smack/direct
691 * @filp: file pointer, not actually used
692 * @buf: where to get the data from
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)
704 if (!capable(CAP_MAC_ADMIN))
707 if (count >= sizeof(temp) || count == 0)
710 if (copy_from_user(temp, buf, count) != 0)
715 if (sscanf(temp, "%d", &i) != 1)
718 smack_cipso_direct = i;
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)
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;
754 rc = simple_read_from_buffer(buf, cn, ppos,
755 smack_net_ambient, asize);
759 mutex_unlock(&smack_ambient_lock);
765 * smk_write_ambient - write() for /smack/ambient
766 * @filp: file pointer, not actually used
767 * @buf: where to get the data from
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];
780 if (!capable(CAP_MAC_ADMIN))
783 if (count >= SMK_LABELLEN)
786 if (copy_from_user(in, buf, count) != 0)
789 smack = smk_import(in, count);
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);
804 static const struct file_operations smk_ambient_ops = {
805 .read = smk_read_ambient,
806 .write = smk_write_ambient,
809 struct option_names {
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)
844 if (count < SMK_LABELLEN)
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);
858 rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound));
864 * smk_write_nltype - write() for /smack/nltype
865 * @filp: file pointer, not actually used
866 * @buf: where to get the data from
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)
879 if (!capable(CAP_MAC_ADMIN))
885 if (copy_from_user(bound, buf, count) != 0)
889 cp = strchr(bound, ' ');
892 cp = strchr(bound, '\n');
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;
903 * Not a valid choice.
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
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)
926 struct inode *root_inode;
928 static struct tree_descr smack_files[] = {
930 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
932 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
934 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
936 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
938 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
940 {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR},
944 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
946 printk(KERN_ERR "%s failed %d while creating inodes\n",
951 root_inode = sb->s_root->d_inode;
952 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
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 = {
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)
995 err = register_filesystem(&smk_fs_type);
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);
1007 smk_unlbl_ambient(NULL);
1012 __initcall(init_smk_fs);