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.
12 * Special thanks to the authors of selinuxfs.
19 #include <linux/kernel.h>
20 #include <linux/vmalloc.h>
21 #include <linux/security.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <net/net_namespace.h>
25 #include <net/netlabel.h>
26 #include <net/cipso_ipv4.h>
27 #include <linux/seq_file.h>
28 #include <linux/ctype.h>
29 #include <linux/audit.h>
33 * smackfs pseudo filesystem.
38 SMK_LOAD = 3, /* load policy */
39 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
40 SMK_DOI = 5, /* CIPSO DOI */
41 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
42 SMK_AMBIENT = 7, /* internet ambient label */
43 SMK_NETLBLADDR = 8, /* single label hosts */
44 SMK_ONLYCAP = 9, /* the only "capable" label */
45 SMK_LOGGING = 10, /* logging */
51 static DEFINE_MUTEX(smack_list_lock);
52 static DEFINE_MUTEX(smack_cipso_lock);
53 static DEFINE_MUTEX(smack_ambient_lock);
54 static DEFINE_MUTEX(smk_netlbladdr_lock);
57 * This is the "ambient" label for network traffic.
58 * If it isn't somehow marked, use this.
59 * It can be reset via smackfs/ambient
61 char *smack_net_ambient = smack_known_floor.smk_known;
64 * This is the level in a CIPSO header that indicates a
65 * smack label is contained directly in the category set.
66 * It can be reset via smackfs/direct
68 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
71 * Unless a process is running with this label even
72 * having CAP_MAC_OVERRIDE isn't enough to grant
73 * privilege to violate MAC policy. If no label is
74 * designated (the NULL case) capabilities apply to
75 * everyone. It is expected that the hat (^) label
76 * will be used if any label is used.
81 * Certain IP addresses may be designated as single label hosts.
82 * Packets are sent there unlabeled, but only from tasks that
83 * can write to the specified label.
86 LIST_HEAD(smk_netlbladdr_list);
87 LIST_HEAD(smack_rule_list);
89 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
91 const char *smack_cipso_option = SMACK_CIPSO_OPTION;
94 #define SEQ_READ_FINISHED 1
97 * Values for parsing cipso rules
98 * SMK_DIGITLEN: Length of a digit field in a rule.
99 * SMK_CIPSOMIN: Minimum possible cipso rule length.
100 * SMK_CIPSOMAX: Maximum possible cipso rule length.
102 #define SMK_DIGITLEN 4
103 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
104 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
107 * Values for parsing MAC rules
108 * SMK_ACCESS: Maximum possible combination of access permissions
109 * SMK_ACCESSLEN: Maximum length for a rule access field
110 * SMK_LOADLEN: Smack rule length
112 #define SMK_ACCESS "rwxa"
113 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
114 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
117 * smk_netlabel_audit_set - fill a netlbl_audit struct
118 * @nap: structure to fill
120 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
122 nap->loginuid = audit_get_loginuid(current);
123 nap->sessionid = audit_get_sessionid(current);
124 nap->secid = smack_to_secid(current_security());
128 * Values for parsing single label host rules
130 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
132 #define SMK_NETLBLADDRMIN 9
133 #define SMK_NETLBLADDRMAX 42
136 * Seq_file read operations for /smack/load
139 static void *load_seq_start(struct seq_file *s, loff_t *pos)
141 if (*pos == SEQ_READ_FINISHED)
143 if (list_empty(&smack_rule_list))
145 return smack_rule_list.next;
148 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
150 struct list_head *list = v;
152 if (list_is_last(list, &smack_rule_list)) {
153 *pos = SEQ_READ_FINISHED;
159 static int load_seq_show(struct seq_file *s, void *v)
161 struct list_head *list = v;
162 struct smack_rule *srp =
163 list_entry(list, struct smack_rule, list);
165 seq_printf(s, "%s %s", (char *)srp->smk_subject,
166 (char *)srp->smk_object);
170 if (srp->smk_access & MAY_READ)
172 if (srp->smk_access & MAY_WRITE)
174 if (srp->smk_access & MAY_EXEC)
176 if (srp->smk_access & MAY_APPEND)
178 if (srp->smk_access == 0)
186 static void load_seq_stop(struct seq_file *s, void *v)
191 static const struct seq_operations load_seq_ops = {
192 .start = load_seq_start,
193 .next = load_seq_next,
194 .show = load_seq_show,
195 .stop = load_seq_stop,
199 * smk_open_load - open() for /smack/load
200 * @inode: inode structure representing file
201 * @file: "load" file pointer
203 * For reading, use load_seq_* seq_file reading operations.
205 static int smk_open_load(struct inode *inode, struct file *file)
207 return seq_open(file, &load_seq_ops);
211 * smk_set_access - add a rule to the rule list
212 * @srp: the new rule to add
214 * Looks through the current subject/object/access list for
215 * the subject/object pair and replaces the access that was
216 * there. If the pair isn't found add it with the specified
219 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
220 * during the allocation of the new pair to add.
222 static int smk_set_access(struct smack_rule *srp)
224 struct smack_rule *sp;
227 mutex_lock(&smack_list_lock);
230 list_for_each_entry_rcu(sp, &smack_rule_list, list) {
231 if (sp->smk_subject == srp->smk_subject &&
232 sp->smk_object == srp->smk_object) {
234 sp->smk_access = srp->smk_access;
239 list_add_rcu(&srp->list, &smack_rule_list);
241 mutex_unlock(&smack_list_lock);
247 * smk_write_load - write() for /smack/load
248 * @file: file pointer, not actually used
249 * @buf: where to get the data from
251 * @ppos: where to start - must be 0
253 * Get one smack access rule from above.
254 * The format is exactly:
255 * char subject[SMK_LABELLEN]
256 * char object[SMK_LABELLEN]
257 * char access[SMK_ACCESSLEN]
259 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
261 static ssize_t smk_write_load(struct file *file, const char __user *buf,
262 size_t count, loff_t *ppos)
264 struct smack_rule *rule;
269 * Must have privilege.
271 * Enough data must be present.
273 if (!capable(CAP_MAC_ADMIN))
276 if (*ppos != 0 || count != SMK_LOADLEN)
279 data = kzalloc(count, GFP_KERNEL);
283 if (copy_from_user(data, buf, count) != 0) {
288 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
294 rule->smk_subject = smk_import(data, 0);
295 if (rule->smk_subject == NULL)
298 rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
299 if (rule->smk_object == NULL)
302 rule->smk_access = 0;
304 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
309 rule->smk_access |= MAY_READ;
315 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
320 rule->smk_access |= MAY_WRITE;
326 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
331 rule->smk_access |= MAY_EXEC;
337 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
342 rule->smk_access |= MAY_APPEND;
348 rc = smk_set_access(rule);
361 static const struct file_operations smk_load_ops = {
362 .open = smk_open_load,
365 .write = smk_write_load,
366 .release = seq_release,
370 * smk_cipso_doi - initialize the CIPSO domain
372 static void smk_cipso_doi(void)
375 struct cipso_v4_doi *doip;
376 struct netlbl_audit nai;
378 smk_netlabel_audit_set(&nai);
380 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
382 printk(KERN_WARNING "%s:%d remove rc = %d\n",
383 __func__, __LINE__, rc);
385 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
387 panic("smack: Failed to initialize cipso DOI.\n");
388 doip->map.std = NULL;
389 doip->doi = smk_cipso_doi_value;
390 doip->type = CIPSO_V4_MAP_PASS;
391 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
392 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
393 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
395 rc = netlbl_cfg_cipsov4_add(doip, &nai);
397 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
398 __func__, __LINE__, rc);
402 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
404 printk(KERN_WARNING "%s:%d map add rc = %d\n",
405 __func__, __LINE__, rc);
412 * smk_unlbl_ambient - initialize the unlabeled domain
413 * @oldambient: previous domain string
415 static void smk_unlbl_ambient(char *oldambient)
418 struct netlbl_audit nai;
420 smk_netlabel_audit_set(&nai);
422 if (oldambient != NULL) {
423 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
425 printk(KERN_WARNING "%s:%d remove rc = %d\n",
426 __func__, __LINE__, rc);
429 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
432 printk(KERN_WARNING "%s:%d add rc = %d\n",
433 __func__, __LINE__, rc);
437 * Seq_file read operations for /smack/cipso
440 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
442 if (*pos == SEQ_READ_FINISHED)
444 if (list_empty(&smack_known_list))
447 return smack_known_list.next;
450 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
452 struct list_head *list = v;
455 * labels with no associated cipso value wont be printed
458 if (list_is_last(list, &smack_known_list)) {
459 *pos = SEQ_READ_FINISHED;
467 * Print cipso labels in format:
468 * label level[/cat[,cat]]
470 static int cipso_seq_show(struct seq_file *s, void *v)
472 struct list_head *list = v;
473 struct smack_known *skp =
474 list_entry(list, struct smack_known, list);
475 struct smack_cipso *scp = skp->smk_cipso;
485 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
487 cbp = scp->smk_catset;
488 for (i = 0; i < SMK_LABELLEN; i++)
489 for (m = 0x80; m != 0; m >>= 1) {
491 seq_printf(s, "%c%d", sep, cat);
502 static void cipso_seq_stop(struct seq_file *s, void *v)
507 static const struct seq_operations cipso_seq_ops = {
508 .start = cipso_seq_start,
509 .stop = cipso_seq_stop,
510 .next = cipso_seq_next,
511 .show = cipso_seq_show,
515 * smk_open_cipso - open() for /smack/cipso
516 * @inode: inode structure representing file
517 * @file: "cipso" file pointer
519 * Connect our cipso_seq_* operations with /smack/cipso
522 static int smk_open_cipso(struct inode *inode, struct file *file)
524 return seq_open(file, &cipso_seq_ops);
528 * smk_write_cipso - write() for /smack/cipso
529 * @file: file pointer, not actually used
530 * @buf: where to get the data from
532 * @ppos: where to start
534 * Accepts only one cipso rule per write call.
535 * Returns number of bytes written or error code, as appropriate
537 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
538 size_t count, loff_t *ppos)
540 struct smack_known *skp;
541 struct smack_cipso *scp = NULL;
542 char mapcatset[SMK_LABELLEN];
546 ssize_t rc = -EINVAL;
553 * Must have privilege.
555 * Enough data must be present.
557 if (!capable(CAP_MAC_ADMIN))
561 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
564 data = kzalloc(count + 1, GFP_KERNEL);
568 if (copy_from_user(data, buf, count) != 0) {
573 /* labels cannot begin with a '-' */
574 if (data[0] == '-') {
581 * Only allow one writer at a time. Writes should be
582 * quite rare and small in any case.
584 mutex_lock(&smack_cipso_lock);
586 skp = smk_import_entry(rule, 0);
590 rule += SMK_LABELLEN;
591 ret = sscanf(rule, "%d", &maplevel);
592 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
595 rule += SMK_DIGITLEN;
596 ret = sscanf(rule, "%d", &catlen);
597 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
600 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
603 memset(mapcatset, 0, sizeof(mapcatset));
605 for (i = 0; i < catlen; i++) {
606 rule += SMK_DIGITLEN;
607 ret = sscanf(rule, "%d", &cat);
608 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
611 smack_catset_bit(cat, mapcatset);
614 if (skp->smk_cipso == NULL) {
615 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
622 spin_lock_bh(&skp->smk_cipsolock);
625 scp = skp->smk_cipso;
627 skp->smk_cipso = scp;
629 scp->smk_level = maplevel;
630 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
632 spin_unlock_bh(&skp->smk_cipsolock);
636 mutex_unlock(&smack_cipso_lock);
642 static const struct file_operations smk_cipso_ops = {
643 .open = smk_open_cipso,
646 .write = smk_write_cipso,
647 .release = seq_release,
651 * Seq_file read operations for /smack/netlabel
654 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
656 if (*pos == SEQ_READ_FINISHED)
658 if (list_empty(&smk_netlbladdr_list))
660 return smk_netlbladdr_list.next;
663 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
665 struct list_head *list = v;
667 if (list_is_last(list, &smk_netlbladdr_list)) {
668 *pos = SEQ_READ_FINISHED;
674 #define BEBITS (sizeof(__be32) * 8)
677 * Print host/label pairs
679 static int netlbladdr_seq_show(struct seq_file *s, void *v)
681 struct list_head *list = v;
682 struct smk_netlbladdr *skp =
683 list_entry(list, struct smk_netlbladdr, list);
684 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
686 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
688 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
690 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
691 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
696 static void netlbladdr_seq_stop(struct seq_file *s, void *v)
701 static const struct seq_operations netlbladdr_seq_ops = {
702 .start = netlbladdr_seq_start,
703 .stop = netlbladdr_seq_stop,
704 .next = netlbladdr_seq_next,
705 .show = netlbladdr_seq_show,
709 * smk_open_netlbladdr - open() for /smack/netlabel
710 * @inode: inode structure representing file
711 * @file: "netlabel" file pointer
713 * Connect our netlbladdr_seq_* operations with /smack/netlabel
716 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
718 return seq_open(file, &netlbladdr_seq_ops);
722 * smk_netlbladdr_insert
723 * @new : netlabel to insert
725 * This helper insert netlabel in the smack_netlbladdrs list
726 * sorted by netmask length (longest to smallest)
727 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
730 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
732 struct smk_netlbladdr *m, *m_next;
734 if (list_empty(&smk_netlbladdr_list)) {
735 list_add_rcu(&new->list, &smk_netlbladdr_list);
739 m = list_entry_rcu(smk_netlbladdr_list.next,
740 struct smk_netlbladdr, list);
742 /* the comparison '>' is a bit hacky, but works */
743 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
744 list_add_rcu(&new->list, &smk_netlbladdr_list);
748 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
749 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
750 list_add_rcu(&new->list, &m->list);
753 m_next = list_entry_rcu(m->list.next,
754 struct smk_netlbladdr, list);
755 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
756 list_add_rcu(&new->list, &m->list);
764 * smk_write_netlbladdr - write() for /smack/netlabel
765 * @file: file pointer, not actually used
766 * @buf: where to get the data from
768 * @ppos: where to start
770 * Accepts only one netlbladdr per write call.
771 * Returns number of bytes written or error code, as appropriate
773 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
774 size_t count, loff_t *ppos)
776 struct smk_netlbladdr *skp;
777 struct sockaddr_in newname;
778 char smack[SMK_LABELLEN];
780 char data[SMK_NETLBLADDRMAX + 1];
781 char *host = (char *)&newname.sin_addr.s_addr;
783 struct netlbl_audit audit_info;
787 u32 mask_bits = (1<<31);
792 * Must have privilege.
794 * Enough data must be present.
795 * "<addr/mask, as a.b.c.d/e><space><label>"
796 * "<addr, as a.b.c.d><space><label>"
798 if (!capable(CAP_MAC_ADMIN))
802 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
804 if (copy_from_user(data, buf, count) != 0)
809 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
810 &host[0], &host[1], &host[2], &host[3], &m, smack);
812 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
813 &host[0], &host[1], &host[2], &host[3], smack);
821 /* if smack begins with '-', its an option, don't import it */
822 if (smack[0] != '-') {
823 sp = smk_import(smack, 0);
827 /* check known options */
828 if (strcmp(smack, smack_cipso_option) == 0)
829 sp = (char *)smack_cipso_option;
834 for (temp_mask = 0; m > 0; m--) {
835 temp_mask |= mask_bits;
838 mask.s_addr = cpu_to_be32(temp_mask);
840 newname.sin_addr.s_addr &= mask.s_addr;
842 * Only allow one writer at a time. Writes should be
843 * quite rare and small in any case.
845 mutex_lock(&smk_netlbladdr_lock);
847 nsa = newname.sin_addr.s_addr;
848 /* try to find if the prefix is already in the list */
850 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
851 if (skp->smk_host.sin_addr.s_addr == nsa &&
852 skp->smk_mask.s_addr == mask.s_addr) {
857 smk_netlabel_audit_set(&audit_info);
860 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
865 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
866 skp->smk_mask.s_addr = mask.s_addr;
868 smk_netlbladdr_insert(skp);
871 /* we delete the unlabeled entry, only if the previous label
872 * wasnt the special CIPSO option */
873 if (skp->smk_label != smack_cipso_option)
874 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
875 &skp->smk_host.sin_addr, &skp->smk_mask,
876 PF_INET, &audit_info);
883 * Now tell netlabel about the single label nature of
884 * this host so that incoming packets get labeled.
885 * but only if we didn't get the special CIPSO option
887 if (rc == 0 && sp != smack_cipso_option)
888 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
889 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
890 smack_to_secid(skp->smk_label), &audit_info);
895 mutex_unlock(&smk_netlbladdr_lock);
900 static const struct file_operations smk_netlbladdr_ops = {
901 .open = smk_open_netlbladdr,
904 .write = smk_write_netlbladdr,
905 .release = seq_release,
909 * smk_read_doi - read() for /smack/doi
910 * @filp: file pointer, not actually used
911 * @buf: where to put the result
912 * @count: maximum to send along
913 * @ppos: where to start
915 * Returns number of bytes read or error code, as appropriate
917 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
918 size_t count, loff_t *ppos)
926 sprintf(temp, "%d", smk_cipso_doi_value);
927 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
933 * smk_write_doi - write() for /smack/doi
934 * @file: file pointer, not actually used
935 * @buf: where to get the data from
937 * @ppos: where to start
939 * Returns number of bytes written or error code, as appropriate
941 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
942 size_t count, loff_t *ppos)
947 if (!capable(CAP_MAC_ADMIN))
950 if (count >= sizeof(temp) || count == 0)
953 if (copy_from_user(temp, buf, count) != 0)
958 if (sscanf(temp, "%d", &i) != 1)
961 smk_cipso_doi_value = i;
968 static const struct file_operations smk_doi_ops = {
969 .read = smk_read_doi,
970 .write = smk_write_doi,
974 * smk_read_direct - read() for /smack/direct
975 * @filp: file pointer, not actually used
976 * @buf: where to put the result
977 * @count: maximum to send along
978 * @ppos: where to start
980 * Returns number of bytes read or error code, as appropriate
982 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
983 size_t count, loff_t *ppos)
991 sprintf(temp, "%d", smack_cipso_direct);
992 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
998 * smk_write_direct - write() for /smack/direct
999 * @file: file pointer, not actually used
1000 * @buf: where to get the data from
1001 * @count: bytes sent
1002 * @ppos: where to start
1004 * Returns number of bytes written or error code, as appropriate
1006 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1007 size_t count, loff_t *ppos)
1012 if (!capable(CAP_MAC_ADMIN))
1015 if (count >= sizeof(temp) || count == 0)
1018 if (copy_from_user(temp, buf, count) != 0)
1023 if (sscanf(temp, "%d", &i) != 1)
1026 smack_cipso_direct = i;
1031 static const struct file_operations smk_direct_ops = {
1032 .read = smk_read_direct,
1033 .write = smk_write_direct,
1037 * smk_read_ambient - read() for /smack/ambient
1038 * @filp: file pointer, not actually used
1039 * @buf: where to put the result
1040 * @cn: maximum to send along
1041 * @ppos: where to start
1043 * Returns number of bytes read or error code, as appropriate
1045 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1046 size_t cn, loff_t *ppos)
1054 * Being careful to avoid a problem in the case where
1055 * smack_net_ambient gets changed in midstream.
1057 mutex_lock(&smack_ambient_lock);
1059 asize = strlen(smack_net_ambient) + 1;
1062 rc = simple_read_from_buffer(buf, cn, ppos,
1063 smack_net_ambient, asize);
1067 mutex_unlock(&smack_ambient_lock);
1073 * smk_write_ambient - write() for /smack/ambient
1074 * @file: file pointer, not actually used
1075 * @buf: where to get the data from
1076 * @count: bytes sent
1077 * @ppos: where to start
1079 * Returns number of bytes written or error code, as appropriate
1081 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1082 size_t count, loff_t *ppos)
1084 char in[SMK_LABELLEN];
1088 if (!capable(CAP_MAC_ADMIN))
1091 if (count >= SMK_LABELLEN)
1094 if (copy_from_user(in, buf, count) != 0)
1097 smack = smk_import(in, count);
1101 mutex_lock(&smack_ambient_lock);
1103 oldambient = smack_net_ambient;
1104 smack_net_ambient = smack;
1105 smk_unlbl_ambient(oldambient);
1107 mutex_unlock(&smack_ambient_lock);
1112 static const struct file_operations smk_ambient_ops = {
1113 .read = smk_read_ambient,
1114 .write = smk_write_ambient,
1118 * smk_read_onlycap - read() for /smack/onlycap
1119 * @filp: file pointer, not actually used
1120 * @buf: where to put the result
1121 * @cn: maximum to send along
1122 * @ppos: where to start
1124 * Returns number of bytes read or error code, as appropriate
1126 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1127 size_t cn, loff_t *ppos)
1130 ssize_t rc = -EINVAL;
1136 if (smack_onlycap != NULL)
1137 smack = smack_onlycap;
1139 asize = strlen(smack) + 1;
1142 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1148 * smk_write_onlycap - write() for /smack/onlycap
1149 * @file: file pointer, not actually used
1150 * @buf: where to get the data from
1151 * @count: bytes sent
1152 * @ppos: where to start
1154 * Returns number of bytes written or error code, as appropriate
1156 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1157 size_t count, loff_t *ppos)
1159 char in[SMK_LABELLEN];
1160 char *sp = current->cred->security;
1162 if (!capable(CAP_MAC_ADMIN))
1166 * This can be done using smk_access() but is done
1167 * explicitly for clarity. The smk_access() implementation
1168 * would use smk_access(smack_onlycap, MAY_WRITE)
1170 if (smack_onlycap != NULL && smack_onlycap != sp)
1173 if (count >= SMK_LABELLEN)
1176 if (copy_from_user(in, buf, count) != 0)
1180 * Should the null string be passed in unset the onlycap value.
1181 * This seems like something to be careful with as usually
1182 * smk_import only expects to return NULL for errors. It
1183 * is usually the case that a nullstring or "\n" would be
1184 * bad to pass to smk_import but in fact this is useful here.
1186 smack_onlycap = smk_import(in, count);
1191 static const struct file_operations smk_onlycap_ops = {
1192 .read = smk_read_onlycap,
1193 .write = smk_write_onlycap,
1197 * smk_read_logging - read() for /smack/logging
1198 * @filp: file pointer, not actually used
1199 * @buf: where to put the result
1200 * @cn: maximum to send along
1201 * @ppos: where to start
1203 * Returns number of bytes read or error code, as appropriate
1205 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1206 size_t count, loff_t *ppos)
1214 sprintf(temp, "%d\n", log_policy);
1215 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1220 * smk_write_logging - write() for /smack/logging
1221 * @file: file pointer, not actually used
1222 * @buf: where to get the data from
1223 * @count: bytes sent
1224 * @ppos: where to start
1226 * Returns number of bytes written or error code, as appropriate
1228 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1229 size_t count, loff_t *ppos)
1234 if (!capable(CAP_MAC_ADMIN))
1237 if (count >= sizeof(temp) || count == 0)
1240 if (copy_from_user(temp, buf, count) != 0)
1245 if (sscanf(temp, "%d", &i) != 1)
1255 static const struct file_operations smk_logging_ops = {
1256 .read = smk_read_logging,
1257 .write = smk_write_logging,
1260 * smk_fill_super - fill the /smackfs superblock
1261 * @sb: the empty superblock
1265 * Fill in the well known entries for /smack
1267 * Returns 0 on success, an error code on failure
1269 static int smk_fill_super(struct super_block *sb, void *data, int silent)
1272 struct inode *root_inode;
1274 static struct tree_descr smack_files[] = {
1276 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
1278 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1280 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1282 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1284 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
1286 {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
1288 {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
1290 {"logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
1294 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1296 printk(KERN_ERR "%s failed %d while creating inodes\n",
1301 root_inode = sb->s_root->d_inode;
1302 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1308 * smk_get_sb - get the smackfs superblock
1309 * @fs_type: passed along without comment
1310 * @flags: passed along without comment
1311 * @dev_name: passed along without comment
1312 * @data: passed along without comment
1313 * @mnt: passed along without comment
1315 * Just passes everything along.
1317 * Returns what the lower level code does.
1319 static int smk_get_sb(struct file_system_type *fs_type,
1320 int flags, const char *dev_name, void *data,
1321 struct vfsmount *mnt)
1323 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
1326 static struct file_system_type smk_fs_type = {
1328 .get_sb = smk_get_sb,
1329 .kill_sb = kill_litter_super,
1332 static struct vfsmount *smackfs_mount;
1335 * init_smk_fs - get the smackfs superblock
1337 * register the smackfs
1339 * Do not register smackfs if Smack wasn't enabled
1340 * on boot. We can not put this method normally under the
1341 * smack_init() code path since the security subsystem get
1342 * initialized before the vfs caches.
1344 * Returns true if we were not chosen on boot or if
1345 * we were chosen and filesystem registration succeeded.
1347 static int __init init_smk_fs(void)
1351 if (!security_module_enable(&smack_ops))
1354 err = register_filesystem(&smk_fs_type);
1356 smackfs_mount = kern_mount(&smk_fs_type);
1357 if (IS_ERR(smackfs_mount)) {
1358 printk(KERN_ERR "smackfs: could not mount!\n");
1359 err = PTR_ERR(smackfs_mount);
1360 smackfs_mount = NULL;
1365 smk_unlbl_ambient(NULL);
1370 __initcall(init_smk_fs);