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/net_namespace.h>
24 #include <net/netlabel.h>
25 #include <net/cipso_ipv4.h>
26 #include <linux/seq_file.h>
27 #include <linux/ctype.h>
28 #include <linux/audit.h>
32 * smackfs pseudo filesystem.
37 SMK_LOAD = 3, /* load policy */
38 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
39 SMK_DOI = 5, /* CIPSO DOI */
40 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
41 SMK_AMBIENT = 7, /* internet ambient label */
42 SMK_NETLBLADDR = 8, /* single label hosts */
43 SMK_ONLYCAP = 9, /* the only "capable" label */
44 SMK_LOGGING = 10, /* logging */
50 static DEFINE_MUTEX(smack_list_lock);
51 static DEFINE_MUTEX(smack_cipso_lock);
52 static DEFINE_MUTEX(smack_ambient_lock);
53 static DEFINE_MUTEX(smk_netlbladdr_lock);
56 * This is the "ambient" label for network traffic.
57 * If it isn't somehow marked, use this.
58 * It can be reset via smackfs/ambient
60 char *smack_net_ambient = smack_known_floor.smk_known;
63 * This is the level in a CIPSO header that indicates a
64 * smack label is contained directly in the category set.
65 * It can be reset via smackfs/direct
67 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
70 * Unless a process is running with this label even
71 * having CAP_MAC_OVERRIDE isn't enough to grant
72 * privilege to violate MAC policy. If no label is
73 * designated (the NULL case) capabilities apply to
74 * everyone. It is expected that the hat (^) label
75 * will be used if any label is used.
80 * Certain IP addresses may be designated as single label hosts.
81 * Packets are sent there unlabeled, but only from tasks that
82 * can write to the specified label.
85 LIST_HEAD(smk_netlbladdr_list);
86 LIST_HEAD(smack_rule_list);
88 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
90 const char *smack_cipso_option = SMACK_CIPSO_OPTION;
93 #define SEQ_READ_FINISHED 1
96 * Values for parsing cipso rules
97 * SMK_DIGITLEN: Length of a digit field in a rule.
98 * SMK_CIPSOMIN: Minimum possible cipso rule length.
99 * SMK_CIPSOMAX: Maximum possible cipso rule length.
101 #define SMK_DIGITLEN 4
102 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
103 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
106 * Values for parsing MAC rules
107 * SMK_ACCESS: Maximum possible combination of access permissions
108 * SMK_ACCESSLEN: Maximum length for a rule access field
109 * SMK_LOADLEN: Smack rule length
111 #define SMK_ACCESS "rwxa"
112 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
113 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
116 * smk_netlabel_audit_set - fill a netlbl_audit struct
117 * @nap: structure to fill
119 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
121 nap->loginuid = audit_get_loginuid(current);
122 nap->sessionid = audit_get_sessionid(current);
123 nap->secid = smack_to_secid(current_security());
127 * Values for parsing single label host rules
129 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
131 #define SMK_NETLBLADDRMIN 9
132 #define SMK_NETLBLADDRMAX 42
135 * Seq_file read operations for /smack/load
138 static void *load_seq_start(struct seq_file *s, loff_t *pos)
140 if (*pos == SEQ_READ_FINISHED)
142 if (list_empty(&smack_rule_list))
144 return smack_rule_list.next;
147 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
149 struct list_head *list = v;
151 if (list_is_last(list, &smack_rule_list)) {
152 *pos = SEQ_READ_FINISHED;
158 static int load_seq_show(struct seq_file *s, void *v)
160 struct list_head *list = v;
161 struct smack_rule *srp =
162 list_entry(list, struct smack_rule, list);
164 seq_printf(s, "%s %s", (char *)srp->smk_subject,
165 (char *)srp->smk_object);
169 if (srp->smk_access & MAY_READ)
171 if (srp->smk_access & MAY_WRITE)
173 if (srp->smk_access & MAY_EXEC)
175 if (srp->smk_access & MAY_APPEND)
177 if (srp->smk_access == 0)
185 static void load_seq_stop(struct seq_file *s, void *v)
190 static struct seq_operations load_seq_ops = {
191 .start = load_seq_start,
192 .next = load_seq_next,
193 .show = load_seq_show,
194 .stop = load_seq_stop,
198 * smk_open_load - open() for /smack/load
199 * @inode: inode structure representing file
200 * @file: "load" file pointer
202 * For reading, use load_seq_* seq_file reading operations.
204 static int smk_open_load(struct inode *inode, struct file *file)
206 return seq_open(file, &load_seq_ops);
210 * smk_set_access - add a rule to the rule list
211 * @srp: the new rule to add
213 * Looks through the current subject/object/access list for
214 * the subject/object pair and replaces the access that was
215 * there. If the pair isn't found add it with the specified
218 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
219 * during the allocation of the new pair to add.
221 static int smk_set_access(struct smack_rule *srp)
223 struct smack_rule *sp;
226 mutex_lock(&smack_list_lock);
229 list_for_each_entry_rcu(sp, &smack_rule_list, list) {
230 if (sp->smk_subject == srp->smk_subject &&
231 sp->smk_object == srp->smk_object) {
233 sp->smk_access = srp->smk_access;
238 list_add_rcu(&srp->list, &smack_rule_list);
240 mutex_unlock(&smack_list_lock);
246 * smk_write_load - write() for /smack/load
247 * @file: file pointer, not actually used
248 * @buf: where to get the data from
250 * @ppos: where to start - must be 0
252 * Get one smack access rule from above.
253 * The format is exactly:
254 * char subject[SMK_LABELLEN]
255 * char object[SMK_LABELLEN]
256 * char access[SMK_ACCESSLEN]
258 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
260 static ssize_t smk_write_load(struct file *file, const char __user *buf,
261 size_t count, loff_t *ppos)
263 struct smack_rule *rule;
268 * Must have privilege.
270 * Enough data must be present.
272 if (!capable(CAP_MAC_ADMIN))
275 if (*ppos != 0 || count != SMK_LOADLEN)
278 data = kzalloc(count, GFP_KERNEL);
282 if (copy_from_user(data, buf, count) != 0) {
287 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
293 rule->smk_subject = smk_import(data, 0);
294 if (rule->smk_subject == NULL)
297 rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
298 if (rule->smk_object == NULL)
301 rule->smk_access = 0;
303 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
308 rule->smk_access |= MAY_READ;
314 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
319 rule->smk_access |= MAY_WRITE;
325 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
330 rule->smk_access |= MAY_EXEC;
336 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
341 rule->smk_access |= MAY_APPEND;
347 rc = smk_set_access(rule);
360 static const struct file_operations smk_load_ops = {
361 .open = smk_open_load,
364 .write = smk_write_load,
365 .release = seq_release,
369 * smk_cipso_doi - initialize the CIPSO domain
371 static void smk_cipso_doi(void)
374 struct cipso_v4_doi *doip;
375 struct netlbl_audit nai;
377 smk_netlabel_audit_set(&nai);
379 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
381 printk(KERN_WARNING "%s:%d remove rc = %d\n",
382 __func__, __LINE__, rc);
384 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
386 panic("smack: Failed to initialize cipso DOI.\n");
387 doip->map.std = NULL;
388 doip->doi = smk_cipso_doi_value;
389 doip->type = CIPSO_V4_MAP_PASS;
390 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
391 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
392 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
394 rc = netlbl_cfg_cipsov4_add(doip, &nai);
396 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
397 __func__, __LINE__, rc);
401 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
403 printk(KERN_WARNING "%s:%d map add rc = %d\n",
404 __func__, __LINE__, rc);
411 * smk_unlbl_ambient - initialize the unlabeled domain
412 * @oldambient: previous domain string
414 static void smk_unlbl_ambient(char *oldambient)
417 struct netlbl_audit nai;
419 smk_netlabel_audit_set(&nai);
421 if (oldambient != NULL) {
422 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
424 printk(KERN_WARNING "%s:%d remove rc = %d\n",
425 __func__, __LINE__, rc);
428 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
431 printk(KERN_WARNING "%s:%d add rc = %d\n",
432 __func__, __LINE__, rc);
436 * Seq_file read operations for /smack/cipso
439 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
441 if (*pos == SEQ_READ_FINISHED)
443 if (list_empty(&smack_known_list))
446 return smack_known_list.next;
449 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
451 struct list_head *list = v;
454 * labels with no associated cipso value wont be printed
457 if (list_is_last(list, &smack_known_list)) {
458 *pos = SEQ_READ_FINISHED;
466 * Print cipso labels in format:
467 * label level[/cat[,cat]]
469 static int cipso_seq_show(struct seq_file *s, void *v)
471 struct list_head *list = v;
472 struct smack_known *skp =
473 list_entry(list, struct smack_known, list);
474 struct smack_cipso *scp = skp->smk_cipso;
484 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
486 cbp = scp->smk_catset;
487 for (i = 0; i < SMK_LABELLEN; i++)
488 for (m = 0x80; m != 0; m >>= 1) {
490 seq_printf(s, "%c%d", sep, cat);
501 static void cipso_seq_stop(struct seq_file *s, void *v)
506 static struct seq_operations cipso_seq_ops = {
507 .start = cipso_seq_start,
508 .stop = cipso_seq_stop,
509 .next = cipso_seq_next,
510 .show = cipso_seq_show,
514 * smk_open_cipso - open() for /smack/cipso
515 * @inode: inode structure representing file
516 * @file: "cipso" file pointer
518 * Connect our cipso_seq_* operations with /smack/cipso
521 static int smk_open_cipso(struct inode *inode, struct file *file)
523 return seq_open(file, &cipso_seq_ops);
527 * smk_write_cipso - write() for /smack/cipso
528 * @file: file pointer, not actually used
529 * @buf: where to get the data from
531 * @ppos: where to start
533 * Accepts only one cipso rule per write call.
534 * Returns number of bytes written or error code, as appropriate
536 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
537 size_t count, loff_t *ppos)
539 struct smack_known *skp;
540 struct smack_cipso *scp = NULL;
541 char mapcatset[SMK_LABELLEN];
545 ssize_t rc = -EINVAL;
552 * Must have privilege.
554 * Enough data must be present.
556 if (!capable(CAP_MAC_ADMIN))
560 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
563 data = kzalloc(count + 1, GFP_KERNEL);
567 if (copy_from_user(data, buf, count) != 0) {
572 /* labels cannot begin with a '-' */
573 if (data[0] == '-') {
580 * Only allow one writer at a time. Writes should be
581 * quite rare and small in any case.
583 mutex_lock(&smack_cipso_lock);
585 skp = smk_import_entry(rule, 0);
589 rule += SMK_LABELLEN;
590 ret = sscanf(rule, "%d", &maplevel);
591 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
594 rule += SMK_DIGITLEN;
595 ret = sscanf(rule, "%d", &catlen);
596 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
599 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
602 memset(mapcatset, 0, sizeof(mapcatset));
604 for (i = 0; i < catlen; i++) {
605 rule += SMK_DIGITLEN;
606 ret = sscanf(rule, "%d", &cat);
607 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
610 smack_catset_bit(cat, mapcatset);
613 if (skp->smk_cipso == NULL) {
614 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
621 spin_lock_bh(&skp->smk_cipsolock);
624 scp = skp->smk_cipso;
626 skp->smk_cipso = scp;
628 scp->smk_level = maplevel;
629 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
631 spin_unlock_bh(&skp->smk_cipsolock);
635 mutex_unlock(&smack_cipso_lock);
641 static const struct file_operations smk_cipso_ops = {
642 .open = smk_open_cipso,
645 .write = smk_write_cipso,
646 .release = seq_release,
650 * Seq_file read operations for /smack/netlabel
653 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
655 if (*pos == SEQ_READ_FINISHED)
657 if (list_empty(&smk_netlbladdr_list))
659 return smk_netlbladdr_list.next;
662 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
664 struct list_head *list = v;
666 if (list_is_last(list, &smk_netlbladdr_list)) {
667 *pos = SEQ_READ_FINISHED;
673 #define BEBITS (sizeof(__be32) * 8)
676 * Print host/label pairs
678 static int netlbladdr_seq_show(struct seq_file *s, void *v)
680 struct list_head *list = v;
681 struct smk_netlbladdr *skp =
682 list_entry(list, struct smk_netlbladdr, list);
683 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
685 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
687 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
689 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
690 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
695 static void netlbladdr_seq_stop(struct seq_file *s, void *v)
700 static struct seq_operations netlbladdr_seq_ops = {
701 .start = netlbladdr_seq_start,
702 .stop = netlbladdr_seq_stop,
703 .next = netlbladdr_seq_next,
704 .show = netlbladdr_seq_show,
708 * smk_open_netlbladdr - open() for /smack/netlabel
709 * @inode: inode structure representing file
710 * @file: "netlabel" file pointer
712 * Connect our netlbladdr_seq_* operations with /smack/netlabel
715 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
717 return seq_open(file, &netlbladdr_seq_ops);
721 * smk_netlbladdr_insert
722 * @new : netlabel to insert
724 * This helper insert netlabel in the smack_netlbladdrs list
725 * sorted by netmask length (longest to smallest)
726 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
729 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
731 struct smk_netlbladdr *m, *m_next;
733 if (list_empty(&smk_netlbladdr_list)) {
734 list_add_rcu(&new->list, &smk_netlbladdr_list);
738 m = list_entry_rcu(smk_netlbladdr_list.next,
739 struct smk_netlbladdr, list);
741 /* the comparison '>' is a bit hacky, but works */
742 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
743 list_add_rcu(&new->list, &smk_netlbladdr_list);
747 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
748 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
749 list_add_rcu(&new->list, &m->list);
752 m_next = list_entry_rcu(m->list.next,
753 struct smk_netlbladdr, list);
754 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
755 list_add_rcu(&new->list, &m->list);
763 * smk_write_netlbladdr - write() for /smack/netlabel
764 * @file: file pointer, not actually used
765 * @buf: where to get the data from
767 * @ppos: where to start
769 * Accepts only one netlbladdr per write call.
770 * Returns number of bytes written or error code, as appropriate
772 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
773 size_t count, loff_t *ppos)
775 struct smk_netlbladdr *skp;
776 struct sockaddr_in newname;
777 char smack[SMK_LABELLEN];
779 char data[SMK_NETLBLADDRMAX + 1];
780 char *host = (char *)&newname.sin_addr.s_addr;
782 struct netlbl_audit audit_info;
786 u32 mask_bits = (1<<31);
791 * Must have privilege.
793 * Enough data must be present.
794 * "<addr/mask, as a.b.c.d/e><space><label>"
795 * "<addr, as a.b.c.d><space><label>"
797 if (!capable(CAP_MAC_ADMIN))
801 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
803 if (copy_from_user(data, buf, count) != 0)
808 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
809 &host[0], &host[1], &host[2], &host[3], &m, smack);
811 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
812 &host[0], &host[1], &host[2], &host[3], smack);
820 /* if smack begins with '-', its an option, don't import it */
821 if (smack[0] != '-') {
822 sp = smk_import(smack, 0);
826 /* check known options */
827 if (strcmp(smack, smack_cipso_option) == 0)
828 sp = (char *)smack_cipso_option;
833 for (temp_mask = 0; m > 0; m--) {
834 temp_mask |= mask_bits;
837 mask.s_addr = cpu_to_be32(temp_mask);
839 newname.sin_addr.s_addr &= mask.s_addr;
841 * Only allow one writer at a time. Writes should be
842 * quite rare and small in any case.
844 mutex_lock(&smk_netlbladdr_lock);
846 nsa = newname.sin_addr.s_addr;
847 /* try to find if the prefix is already in the list */
849 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
850 if (skp->smk_host.sin_addr.s_addr == nsa &&
851 skp->smk_mask.s_addr == mask.s_addr) {
856 smk_netlabel_audit_set(&audit_info);
859 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
864 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
865 skp->smk_mask.s_addr = mask.s_addr;
867 smk_netlbladdr_insert(skp);
870 /* we delete the unlabeled entry, only if the previous label
871 * wasnt the special CIPSO option */
872 if (skp->smk_label != smack_cipso_option)
873 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
874 &skp->smk_host.sin_addr, &skp->smk_mask,
875 PF_INET, &audit_info);
882 * Now tell netlabel about the single label nature of
883 * this host so that incoming packets get labeled.
884 * but only if we didn't get the special CIPSO option
886 if (rc == 0 && sp != smack_cipso_option)
887 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
888 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
889 smack_to_secid(skp->smk_label), &audit_info);
894 mutex_unlock(&smk_netlbladdr_lock);
899 static const struct file_operations smk_netlbladdr_ops = {
900 .open = smk_open_netlbladdr,
903 .write = smk_write_netlbladdr,
904 .release = seq_release,
908 * smk_read_doi - read() for /smack/doi
909 * @filp: file pointer, not actually used
910 * @buf: where to put the result
911 * @count: maximum to send along
912 * @ppos: where to start
914 * Returns number of bytes read or error code, as appropriate
916 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
917 size_t count, loff_t *ppos)
925 sprintf(temp, "%d", smk_cipso_doi_value);
926 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
932 * smk_write_doi - write() for /smack/doi
933 * @file: file pointer, not actually used
934 * @buf: where to get the data from
936 * @ppos: where to start
938 * Returns number of bytes written or error code, as appropriate
940 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
941 size_t count, loff_t *ppos)
946 if (!capable(CAP_MAC_ADMIN))
949 if (count >= sizeof(temp) || count == 0)
952 if (copy_from_user(temp, buf, count) != 0)
957 if (sscanf(temp, "%d", &i) != 1)
960 smk_cipso_doi_value = i;
967 static const struct file_operations smk_doi_ops = {
968 .read = smk_read_doi,
969 .write = smk_write_doi,
973 * smk_read_direct - read() for /smack/direct
974 * @filp: file pointer, not actually used
975 * @buf: where to put the result
976 * @count: maximum to send along
977 * @ppos: where to start
979 * Returns number of bytes read or error code, as appropriate
981 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
982 size_t count, loff_t *ppos)
990 sprintf(temp, "%d", smack_cipso_direct);
991 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
997 * smk_write_direct - write() for /smack/direct
998 * @file: file pointer, not actually used
999 * @buf: where to get the data from
1000 * @count: bytes sent
1001 * @ppos: where to start
1003 * Returns number of bytes written or error code, as appropriate
1005 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1006 size_t count, loff_t *ppos)
1011 if (!capable(CAP_MAC_ADMIN))
1014 if (count >= sizeof(temp) || count == 0)
1017 if (copy_from_user(temp, buf, count) != 0)
1022 if (sscanf(temp, "%d", &i) != 1)
1025 smack_cipso_direct = i;
1030 static const struct file_operations smk_direct_ops = {
1031 .read = smk_read_direct,
1032 .write = smk_write_direct,
1036 * smk_read_ambient - read() for /smack/ambient
1037 * @filp: file pointer, not actually used
1038 * @buf: where to put the result
1039 * @cn: maximum to send along
1040 * @ppos: where to start
1042 * Returns number of bytes read or error code, as appropriate
1044 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1045 size_t cn, loff_t *ppos)
1053 * Being careful to avoid a problem in the case where
1054 * smack_net_ambient gets changed in midstream.
1056 mutex_lock(&smack_ambient_lock);
1058 asize = strlen(smack_net_ambient) + 1;
1061 rc = simple_read_from_buffer(buf, cn, ppos,
1062 smack_net_ambient, asize);
1066 mutex_unlock(&smack_ambient_lock);
1072 * smk_write_ambient - write() for /smack/ambient
1073 * @file: file pointer, not actually used
1074 * @buf: where to get the data from
1075 * @count: bytes sent
1076 * @ppos: where to start
1078 * Returns number of bytes written or error code, as appropriate
1080 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1081 size_t count, loff_t *ppos)
1083 char in[SMK_LABELLEN];
1087 if (!capable(CAP_MAC_ADMIN))
1090 if (count >= SMK_LABELLEN)
1093 if (copy_from_user(in, buf, count) != 0)
1096 smack = smk_import(in, count);
1100 mutex_lock(&smack_ambient_lock);
1102 oldambient = smack_net_ambient;
1103 smack_net_ambient = smack;
1104 smk_unlbl_ambient(oldambient);
1106 mutex_unlock(&smack_ambient_lock);
1111 static const struct file_operations smk_ambient_ops = {
1112 .read = smk_read_ambient,
1113 .write = smk_write_ambient,
1117 * smk_read_onlycap - read() for /smack/onlycap
1118 * @filp: file pointer, not actually used
1119 * @buf: where to put the result
1120 * @cn: maximum to send along
1121 * @ppos: where to start
1123 * Returns number of bytes read or error code, as appropriate
1125 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1126 size_t cn, loff_t *ppos)
1129 ssize_t rc = -EINVAL;
1135 if (smack_onlycap != NULL)
1136 smack = smack_onlycap;
1138 asize = strlen(smack) + 1;
1141 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1147 * smk_write_onlycap - write() for /smack/onlycap
1148 * @file: file pointer, not actually used
1149 * @buf: where to get the data from
1150 * @count: bytes sent
1151 * @ppos: where to start
1153 * Returns number of bytes written or error code, as appropriate
1155 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1156 size_t count, loff_t *ppos)
1158 char in[SMK_LABELLEN];
1159 char *sp = current->cred->security;
1161 if (!capable(CAP_MAC_ADMIN))
1165 * This can be done using smk_access() but is done
1166 * explicitly for clarity. The smk_access() implementation
1167 * would use smk_access(smack_onlycap, MAY_WRITE)
1169 if (smack_onlycap != NULL && smack_onlycap != sp)
1172 if (count >= SMK_LABELLEN)
1175 if (copy_from_user(in, buf, count) != 0)
1179 * Should the null string be passed in unset the onlycap value.
1180 * This seems like something to be careful with as usually
1181 * smk_import only expects to return NULL for errors. It
1182 * is usually the case that a nullstring or "\n" would be
1183 * bad to pass to smk_import but in fact this is useful here.
1185 smack_onlycap = smk_import(in, count);
1190 static const struct file_operations smk_onlycap_ops = {
1191 .read = smk_read_onlycap,
1192 .write = smk_write_onlycap,
1196 * smk_read_logging - read() for /smack/logging
1197 * @filp: file pointer, not actually used
1198 * @buf: where to put the result
1199 * @cn: maximum to send along
1200 * @ppos: where to start
1202 * Returns number of bytes read or error code, as appropriate
1204 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1205 size_t count, loff_t *ppos)
1213 sprintf(temp, "%d\n", log_policy);
1214 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1219 * smk_write_logging - write() for /smack/logging
1220 * @file: file pointer, not actually used
1221 * @buf: where to get the data from
1222 * @count: bytes sent
1223 * @ppos: where to start
1225 * Returns number of bytes written or error code, as appropriate
1227 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1228 size_t count, loff_t *ppos)
1233 if (!capable(CAP_MAC_ADMIN))
1236 if (count >= sizeof(temp) || count == 0)
1239 if (copy_from_user(temp, buf, count) != 0)
1244 if (sscanf(temp, "%d", &i) != 1)
1254 static const struct file_operations smk_logging_ops = {
1255 .read = smk_read_logging,
1256 .write = smk_write_logging,
1259 * smk_fill_super - fill the /smackfs superblock
1260 * @sb: the empty superblock
1264 * Fill in the well known entries for /smack
1266 * Returns 0 on success, an error code on failure
1268 static int smk_fill_super(struct super_block *sb, void *data, int silent)
1271 struct inode *root_inode;
1273 static struct tree_descr smack_files[] = {
1275 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
1277 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1279 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1281 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1283 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
1285 {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
1287 {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
1289 {"logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
1293 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1295 printk(KERN_ERR "%s failed %d while creating inodes\n",
1300 root_inode = sb->s_root->d_inode;
1301 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1307 * smk_get_sb - get the smackfs superblock
1308 * @fs_type: passed along without comment
1309 * @flags: passed along without comment
1310 * @dev_name: passed along without comment
1311 * @data: passed along without comment
1312 * @mnt: passed along without comment
1314 * Just passes everything along.
1316 * Returns what the lower level code does.
1318 static int smk_get_sb(struct file_system_type *fs_type,
1319 int flags, const char *dev_name, void *data,
1320 struct vfsmount *mnt)
1322 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
1325 static struct file_system_type smk_fs_type = {
1327 .get_sb = smk_get_sb,
1328 .kill_sb = kill_litter_super,
1331 static struct vfsmount *smackfs_mount;
1334 * init_smk_fs - get the smackfs superblock
1336 * register the smackfs
1338 * Do not register smackfs if Smack wasn't enabled
1339 * on boot. We can not put this method normally under the
1340 * smack_init() code path since the security subsystem get
1341 * initialized before the vfs caches.
1343 * Returns true if we were not chosen on boot or if
1344 * we were chosen and filesystem registration succeeded.
1346 static int __init init_smk_fs(void)
1350 if (!security_module_enable(&smack_ops))
1353 err = register_filesystem(&smk_fs_type);
1355 smackfs_mount = kern_mount(&smk_fs_type);
1356 if (IS_ERR(smackfs_mount)) {
1357 printk(KERN_ERR "smackfs: could not mount!\n");
1358 err = PTR_ERR(smackfs_mount);
1359 smackfs_mount = NULL;
1364 smk_unlbl_ambient(NULL);
1369 __initcall(init_smk_fs);