19 #include <linux/kernel.h>
23 #include <linux/slab.h>
27 #include <linux/ctype.h>
28 #include <linux/audit.h>
123 #define SMK_DIGITLEN 4
124 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
125 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
133 #define SMK_OACCESS "rwxa"
134 #define SMK_ACCESS "rwxat"
135 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
136 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
137 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
138 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
144 static inline void smack_catset_bit(
unsigned int cat,
char *catsetp)
149 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
156 static void smk_netlabel_audit_set(
struct netlbl_audit *nap)
167 #define SMK_NETLBLADDRMIN 9
185 struct mutex *rule_lock)
196 list_for_each_entry_rcu(sp, rule_list,
list) {
205 list_add_rcu(&srp->
list, rule_list);
223 static int smk_fill_rule(
const char *subject,
const char *
object,
260 for (cp = access; *cp !=
'\0'; cp++) {
300 static int smk_parse_rule(
const char *
data,
struct smack_rule *rule,
int import)
318 static int smk_parse_long_rule(
const char *
data,
struct smack_rule *rule,
341 if (
sscanf(data,
"%s %s %s", subject,
object, access) == 3)
342 rc = smk_fill_rule(subject,
object, access, rule,
import, 0);
352 #define SMK_FIXED24_FMT 0
353 #define SMK_LONG_FMT 1
371 size_t count, loff_t *ppos,
420 if (smk_parse_long_rule(data, rule, 1))
428 if (smk_parse_rule(data, rule, 1))
433 if (rule_list ==
NULL) {
447 if (!smk_set_access(rule, rule_list, rule_lock)) {
452 list_add_rcu(&smlp->
list, &smack_rule_list);
470 static void *smk_seq_start(
struct seq_file *
s, loff_t *
pos,
485 if (list_empty(list))
493 static void *smk_seq_next(
struct seq_file *s,
void *
v, loff_t *pos,
498 if (list_is_last(list, head)) {
506 static void smk_seq_stop(
struct seq_file *s,
void *v)
547 static void *load2_seq_start(
struct seq_file *s, loff_t *pos)
549 return smk_seq_start(s, pos, &smack_rule_list);
552 static void *load2_seq_next(
struct seq_file *s,
void *v, loff_t *pos)
554 return smk_seq_next(s, v, pos, &smack_rule_list);
557 static int load_seq_show(
struct seq_file *s,
void *v)
569 .start = load2_seq_start,
570 .next = load2_seq_next,
571 .show = load_seq_show,
572 .stop = smk_seq_stop,
584 return seq_open(file, &load_seq_ops);
596 size_t count, loff_t *ppos)
606 return smk_write_rules_list(file, buf, count, ppos,
NULL,
NULL,
611 .open = smk_open_load,
614 .write = smk_write_load,
621 static void smk_cipso_doi(
void)
627 smk_netlabel_audit_set(&nai);
632 __func__, __LINE__, rc);
636 panic(
"smack: Failed to initialize cipso DOI.\n");
638 doip->
doi = smk_cipso_doi_value;
647 __func__, __LINE__, rc);
654 __func__, __LINE__, rc);
664 static void smk_unlbl_ambient(
char *oldambient)
669 smk_netlabel_audit_set(&nai);
671 if (oldambient !=
NULL) {
675 __func__, __LINE__, rc);
684 __func__, __LINE__, rc);
691 static void *cipso_seq_start(
struct seq_file *s, loff_t *pos)
693 return smk_seq_start(s, pos, &smack_known_list);
696 static void *cipso_seq_next(
struct seq_file *s,
void *v, loff_t *pos)
698 return smk_seq_next(s, v, pos, &smack_known_list);
705 static int cipso_seq_show(
struct seq_file *s,
void *v)
739 .start = cipso_seq_start,
740 .next = cipso_seq_next,
741 .show = cipso_seq_show,
742 .stop = smk_seq_stop,
753 static int smk_open_cipso(
struct inode *
inode,
struct file *file)
755 return seq_open(file, &cipso_seq_ops);
769 static ssize_t smk_set_cipso(
struct file *file,
const char __user *buf,
770 size_t count, loff_t *ppos,
int format)
823 ret =
sscanf(rule,
"%d", &maplevel);
828 ret =
sscanf(rule,
"%d", &catlen);
836 memset(mapcatset, 0,
sizeof(mapcatset));
838 for (i = 0; i < catlen; i++) {
840 ret =
sscanf(rule,
"%u", &cat);
844 smack_catset_bit(cat, mapcatset);
849 netlbl_secattr_catmap_free(skp->
smk_netlabel.attr.mls.cat);
872 static ssize_t smk_write_cipso(
struct file *file,
const char __user *buf,
873 size_t count, loff_t *ppos)
879 .open = smk_open_cipso,
882 .write = smk_write_cipso,
894 static int cipso2_seq_show(
struct seq_file *s,
void *v)
917 .start = cipso_seq_start,
918 .next = cipso_seq_next,
919 .show = cipso2_seq_show,
920 .stop = smk_seq_stop,
931 static int smk_open_cipso2(
struct inode *
inode,
struct file *file)
933 return seq_open(file, &cipso2_seq_ops);
946 static ssize_t smk_write_cipso2(
struct file *file,
const char __user *buf,
947 size_t count, loff_t *ppos)
949 return smk_set_cipso(file, buf, count, ppos,
SMK_LONG_FMT);
953 .open = smk_open_cipso2,
956 .write = smk_write_cipso2,
964 static void *netlbladdr_seq_start(
struct seq_file *s, loff_t *pos)
966 return smk_seq_start(s, pos, &smk_netlbladdr_list);
969 static void *netlbladdr_seq_next(
struct seq_file *s,
void *v, loff_t *pos)
971 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
973 #define BEBITS (sizeof(__be32) * 8)
978 static int netlbladdr_seq_show(
struct seq_file *s,
void *v)
983 unsigned char *hp = (
char *) &skp->
smk_host.sin_addr.s_addr;
987 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
990 hp[0], hp[1], hp[2], hp[3], maskn, skp->
smk_label);
996 .start = netlbladdr_seq_start,
997 .next = netlbladdr_seq_next,
998 .show = netlbladdr_seq_show,
999 .stop = smk_seq_stop,
1010 static int smk_open_netlbladdr(
struct inode *
inode,
struct file *file)
1012 return seq_open(file, &netlbladdr_seq_ops);
1028 if (list_empty(&smk_netlbladdr_list)) {
1029 list_add_rcu(&new->list, &smk_netlbladdr_list);
1037 if (new->smk_mask.s_addr > m->
smk_mask.s_addr) {
1038 list_add_rcu(&new->list, &smk_netlbladdr_list);
1042 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1043 if (list_is_last(&m->
list, &smk_netlbladdr_list)) {
1044 list_add_rcu(&new->list, &m->
list);
1047 m_next = list_entry_rcu(m->
list.next,
1049 if (new->smk_mask.s_addr > m_next->
smk_mask.s_addr) {
1050 list_add_rcu(&new->list, &m->
list);
1067 static ssize_t smk_write_netlbladdr(
struct file *file,
const char __user *buf,
1068 size_t count, loff_t *ppos)
1075 char *
host = (
char *)&newname.sin_addr.s_addr;
1109 if (smack ==
NULL) {
1116 rc =
sscanf(data,
"%hhd.%hhd.%hhd.%hhd/%d %s",
1117 &host[0], &host[1], &host[2], &host[3], &m, smack);
1119 rc =
sscanf(data,
"%hhd.%hhd.%hhd.%hhd %s",
1120 &host[0], &host[1], &host[2], &host[3], smack);
1135 if (smack[0] !=
'-') {
1151 for (temp_mask = 0; m > 0; m--) {
1157 newname.sin_addr.s_addr &=
mask.s_addr;
1164 nsa = newname.sin_addr.s_addr;
1167 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
1168 if (skp->
smk_host.sin_addr.s_addr == nsa &&
1174 smk_netlabel_audit_set(&audit_info);
1182 skp->
smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1185 smk_netlbladdr_insert(skp);
1223 .open = smk_open_netlbladdr,
1226 .write = smk_write_netlbladdr,
1239 static ssize_t smk_read_doi(
struct file *filp,
char __user *buf,
1240 size_t count, loff_t *ppos)
1248 sprintf(temp,
"%d", smk_cipso_doi_value);
1263 static ssize_t smk_write_doi(
struct file *file,
const char __user *buf,
1264 size_t count, loff_t *ppos)
1272 if (count >=
sizeof(temp) || count == 0)
1280 if (
sscanf(temp,
"%d", &i) != 1)
1283 smk_cipso_doi_value =
i;
1291 .read = smk_read_doi,
1292 .write = smk_write_doi,
1305 static ssize_t smk_read_direct(
struct file *filp,
char __user *buf,
1306 size_t count, loff_t *ppos)
1329 static ssize_t smk_write_direct(
struct file *file,
const char __user *buf,
1330 size_t count, loff_t *ppos)
1339 if (count >=
sizeof(temp) || count == 0)
1347 if (
sscanf(temp,
"%d", &i) != 1)
1357 list_for_each_entry_rcu(skp, &smack_known_list, list)
1369 .
read = smk_read_direct,
1370 .write = smk_write_direct,
1383 static ssize_t smk_read_mapped(
struct file *filp,
char __user *buf,
1384 size_t count, loff_t *ppos)
1407 static ssize_t smk_write_mapped(
struct file *file,
const char __user *buf,
1408 size_t count, loff_t *ppos)
1417 if (count >=
sizeof(temp) || count == 0)
1425 if (
sscanf(temp,
"%d", &i) != 1)
1435 list_for_each_entry_rcu(skp, &smack_known_list, list)
1447 .
read = smk_read_mapped,
1448 .write = smk_write_mapped,
1461 static ssize_t smk_read_ambient(
struct file *filp,
char __user *buf,
1462 size_t cn, loff_t *ppos)
1497 static ssize_t smk_write_ambient(
struct file *file,
const char __user *buf,
1498 size_t count, loff_t *ppos)
1518 if (smack ==
NULL) {
1527 smk_unlbl_ambient(oldambient);
1537 .read = smk_read_ambient,
1538 .write = smk_write_ambient,
1551 static ssize_t smk_read_onlycap(
struct file *filp,
char __user *buf,
1552 size_t cn, loff_t *ppos)
1564 asize =
strlen(smack) + 1;
1581 static ssize_t smk_write_onlycap(
struct file *file,
const char __user *buf,
1582 size_t count, loff_t *ppos)
1585 char *sp = smk_of_task(
current->cred->security);
1623 .read = smk_read_onlycap,
1624 .write = smk_write_onlycap,
1637 static ssize_t smk_read_logging(
struct file *filp,
char __user *buf,
1638 size_t count, loff_t *ppos)
1660 static ssize_t smk_write_logging(
struct file *file,
const char __user *buf,
1661 size_t count, loff_t *ppos)
1669 if (count >=
sizeof(temp) || count == 0)
1677 if (
sscanf(temp,
"%d", &i) != 1)
1688 .read = smk_read_logging,
1689 .write = smk_write_logging,
1697 static void *load_self_seq_start(
struct seq_file *s, loff_t *pos)
1701 return smk_seq_start(s, pos, &tsp->
smk_rules);
1704 static void *load_self_seq_next(
struct seq_file *s,
void *v, loff_t *pos)
1708 return smk_seq_next(s, v, pos, &tsp->
smk_rules);
1711 static int load_self_seq_show(
struct seq_file *s,
void *v)
1723 .start = load_self_seq_start,
1724 .next = load_self_seq_next,
1725 .show = load_self_seq_show,
1726 .stop = smk_seq_stop,
1737 static int smk_open_load_self(
struct inode *
inode,
struct file *file)
1739 return seq_open(file, &load_self_seq_ops);
1750 static ssize_t smk_write_load_self(
struct file *file,
const char __user *buf,
1751 size_t count, loff_t *ppos)
1755 return smk_write_rules_list(file, buf, count, ppos, &tsp->
smk_rules,
1760 .open = smk_open_load_self,
1763 .write = smk_write_load_self,
1774 static ssize_t smk_user_access(
struct file *file,
const char __user *buf,
1775 size_t count, loff_t *ppos,
int format)
1784 return PTR_ERR(data);
1789 res = smk_parse_rule(data, &rule, 0);
1797 memcpy(cod, data, count);
1799 res = smk_parse_long_rule(cod, &rule, 0);
1806 res =
smk_access(rule.smk_subject, rule.smk_object, rule.smk_access,
1808 data[0] = res == 0 ?
'1' :
'0';
1825 static ssize_t smk_write_access(
struct file *file,
const char __user *buf,
1826 size_t count, loff_t *ppos)
1832 .write = smk_write_access,
1843 static int load2_seq_show(
struct seq_file *s,
void *v)
1855 .start = load2_seq_start,
1856 .next = load2_seq_next,
1857 .show = load2_seq_show,
1858 .stop = smk_seq_stop,
1868 static int smk_open_load2(
struct inode *
inode,
struct file *file)
1870 return seq_open(file, &load2_seq_ops);
1881 static ssize_t smk_write_load2(
struct file *file,
const char __user *buf,
1882 size_t count, loff_t *ppos)
1890 return smk_write_rules_list(file, buf, count, ppos,
NULL,
NULL,
1895 .open = smk_open_load2,
1898 .write = smk_write_load2,
1906 static void *load_self2_seq_start(
struct seq_file *s, loff_t *pos)
1910 return smk_seq_start(s, pos, &tsp->
smk_rules);
1913 static void *load_self2_seq_next(
struct seq_file *s,
void *v, loff_t *pos)
1917 return smk_seq_next(s, v, pos, &tsp->
smk_rules);
1920 static int load_self2_seq_show(
struct seq_file *s,
void *v)
1932 .start = load_self2_seq_start,
1933 .next = load_self2_seq_next,
1934 .show = load_self2_seq_show,
1935 .stop = smk_seq_stop,
1945 static int smk_open_load_self2(
struct inode *
inode,
struct file *file)
1947 return seq_open(file, &load_self2_seq_ops);
1958 static ssize_t smk_write_load_self2(
struct file *file,
const char __user *buf,
1959 size_t count, loff_t *ppos)
1963 return smk_write_rules_list(file, buf, count, ppos, &tsp->
smk_rules,
1968 .open = smk_open_load_self2,
1971 .write = smk_write_load_self2,
1982 static ssize_t smk_write_access2(
struct file *file,
const char __user *buf,
1983 size_t count, loff_t *ppos)
1985 return smk_user_access(file, buf, count, ppos,
SMK_LONG_FMT);
1989 .write = smk_write_access2,
2002 static ssize_t smk_write_revoke_subj(
struct file *file,
const char __user *buf,
2003 size_t count, loff_t *ppos)
2006 const char *cp =
NULL;
2010 struct mutex *rule_lock;
2048 list_for_each_entry_rcu(sp, rule_list, list)
2060 .
write = smk_write_revoke_subj,
2076 static int smk_fill_super(
struct super_block *
sb,
void *data,
int silent)
2079 struct inode *root_inode;
2113 "revoke-subject", &smk_revoke_subj_ops,
2126 root_inode = sb->
s_root->d_inode;
2143 int flags,
const char *dev_name,
void *data)
2145 return mount_single(fs_type, flags, data, smk_fill_super);
2154 static struct vfsmount *smackfs_mount;
2178 static int __init init_smk_fs(
void)
2189 if (IS_ERR(smackfs_mount)) {
2191 err = PTR_ERR(smackfs_mount);
2192 smackfs_mount =
NULL;
2197 smk_unlbl_ambient(
NULL);
2200 if (err == 0 && rc < 0)
2203 if (err == 0 && rc < 0)
2206 if (err == 0 && rc < 0)
2209 if (err == 0 && rc < 0)
2212 if (err == 0 && rc < 0)
2215 if (err == 0 && rc < 0)