Skip to content

Commit 33d589e

Browse files
goongascschaufler
authored andcommitted
smack: /smack/doi: accept previously used values
Writing to /smack/doi a value that has ever been written there in the past disables networking for non-ambient labels. E.g. # cat /smack/doi 3 # netlabelctl -p cipso list Configured CIPSO mappings (1) DOI value : 3 mapping type : PASS_THROUGH # netlabelctl -p map list Configured NetLabel domain mappings (3) domain: "_" (IPv4) protocol: UNLABELED domain: DEFAULT (IPv4) protocol: CIPSO, DOI = 3 domain: DEFAULT (IPv6) protocol: UNLABELED # cat /smack/ambient _ # cat /proc/$$/attr/smack/current _ # ping -c1 10.1.95.12 64 bytes from 10.1.95.12: icmp_seq=1 ttl=64 time=0.964 ms # echo foo >/proc/$$/attr/smack/current # ping -c1 10.1.95.12 64 bytes from 10.1.95.12: icmp_seq=1 ttl=64 time=0.956 ms unknown option 86 # echo 4 >/smack/doi # echo 3 >/smack/doi !> [ 214.050395] smk_cipso_doi:691 cipso add rc = -17 # echo 3 >/smack/doi !> [ 249.402261] smk_cipso_doi:678 remove rc = -2 !> [ 249.402261] smk_cipso_doi:691 cipso add rc = -17 # ping -c1 10.1.95.12 !!> ping: 10.1.95.12: Address family for hostname not supported # echo _ >/proc/$$/attr/smack/current # ping -c1 10.1.95.12 64 bytes from 10.1.95.12: icmp_seq=1 ttl=64 time=0.617 ms This happens because Smack keeps decommissioned DOIs, fails to re-add them, and consequently refuses to add the “default” domain map: # netlabelctl -p cipso list Configured CIPSO mappings (2) DOI value : 3 mapping type : PASS_THROUGH DOI value : 4 mapping type : PASS_THROUGH # netlabelctl -p map list Configured NetLabel domain mappings (2) domain: "_" (IPv4) protocol: UNLABELED !> (no ipv4 map for default domain here) domain: DEFAULT (IPv6) protocol: UNLABELED Fix by clearing decommissioned DOI definitions and serializing concurrent DOI updates with a new lock. Also: - allow /smack/doi to live unconfigured, since adding a map (netlbl_cfg_cipsov4_map_add) may fail. CIPSO_V4_DOI_UNKNOWN(0) indicates the unconfigured DOI - add new DOI before removing the old default map, so the old map remains if the add fails (2008-02-04, Casey Schaufler) Fixes: e114e47 ("Smack: Simplified Mandatory Access Control Kernel") Signed-off-by: Konstantin Andreev <andreev@swemel.ru> Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
1 parent 19c013e commit 33d589e

1 file changed

Lines changed: 45 additions & 26 deletions

File tree

security/smack/smackfs.c

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ enum smk_inos {
7070
static DEFINE_MUTEX(smack_cipso_lock);
7171
static DEFINE_MUTEX(smack_ambient_lock);
7272
static DEFINE_MUTEX(smk_net4addr_lock);
73+
static DEFINE_MUTEX(smk_cipso_doi_lock);
7374
#if IS_ENABLED(CONFIG_IPV6)
7475
static DEFINE_MUTEX(smk_net6addr_lock);
7576
#endif /* CONFIG_IPV6 */
@@ -141,7 +142,7 @@ struct smack_parsed_rule {
141142
int smk_access2;
142143
};
143144

144-
static u32 smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
145+
static u32 smk_cipso_doi_value = CIPSO_V4_DOI_UNKNOWN;
145146

146147
/*
147148
* Values for parsing cipso rules
@@ -663,43 +664,60 @@ static const struct file_operations smk_load_ops = {
663664
};
664665

665666
/**
666-
* smk_cipso_doi - initialize the CIPSO domain
667+
* smk_cipso_doi - set netlabel maps
668+
* @ndoi: new value for our CIPSO DOI
669+
* @gfp_flags: kmalloc allocation context
667670
*/
668-
static void smk_cipso_doi(void)
671+
static int
672+
smk_cipso_doi(u32 ndoi, gfp_t gfp_flags)
669673
{
670-
int rc;
674+
int rc = 0;
671675
struct cipso_v4_doi *doip;
672676
struct netlbl_audit nai;
673677

674-
smk_netlabel_audit_set(&nai);
678+
mutex_lock(&smk_cipso_doi_lock);
675679

676-
rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
677-
if (rc != 0)
678-
printk(KERN_WARNING "%s:%d remove rc = %d\n",
679-
__func__, __LINE__, rc);
680+
if (smk_cipso_doi_value == ndoi)
681+
goto clr_doi_lock;
682+
683+
smk_netlabel_audit_set(&nai);
680684

681-
doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL | __GFP_NOFAIL);
685+
doip = kmalloc(sizeof(struct cipso_v4_doi), gfp_flags);
686+
if (!doip) {
687+
rc = -ENOMEM;
688+
goto clr_doi_lock;
689+
}
682690
doip->map.std = NULL;
683-
doip->doi = smk_cipso_doi_value;
691+
doip->doi = ndoi;
684692
doip->type = CIPSO_V4_MAP_PASS;
685693
doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
686694
for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
687695
doip->tags[rc] = CIPSO_V4_TAG_INVALID;
688696

689697
rc = netlbl_cfg_cipsov4_add(doip, &nai);
690-
if (rc != 0) {
691-
printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
692-
__func__, __LINE__, rc);
698+
if (rc) {
693699
kfree(doip);
694-
return;
700+
goto clr_doi_lock;
695701
}
696-
rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
697-
if (rc != 0) {
698-
printk(KERN_WARNING "%s:%d map add rc = %d\n",
699-
__func__, __LINE__, rc);
700-
netlbl_cfg_cipsov4_del(doip->doi, &nai);
701-
return;
702+
703+
if (smk_cipso_doi_value != CIPSO_V4_DOI_UNKNOWN) {
704+
rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
705+
if (rc && rc != -ENOENT)
706+
goto clr_ndoi_def;
707+
708+
netlbl_cfg_cipsov4_del(smk_cipso_doi_value, &nai);
702709
}
710+
711+
rc = netlbl_cfg_cipsov4_map_add(ndoi, NULL, NULL, NULL, &nai);
712+
if (rc) {
713+
smk_cipso_doi_value = CIPSO_V4_DOI_UNKNOWN; // no default map
714+
clr_ndoi_def: netlbl_cfg_cipsov4_del(ndoi, &nai);
715+
} else
716+
smk_cipso_doi_value = ndoi;
717+
718+
clr_doi_lock:
719+
mutex_unlock(&smk_cipso_doi_lock);
720+
return rc;
703721
}
704722

705723
/**
@@ -1599,11 +1617,8 @@ static ssize_t smk_write_doi(struct file *file, const char __user *buf,
15991617

16001618
if (u == CIPSO_V4_DOI_UNKNOWN || u > U32_MAX)
16011619
return -EINVAL;
1602-
smk_cipso_doi_value = u;
1603-
1604-
smk_cipso_doi();
16051620

1606-
return count;
1621+
return smk_cipso_doi(u, GFP_KERNEL) ? : count;
16071622
}
16081623

16091624
static const struct file_operations smk_doi_ops = {
@@ -2984,6 +2999,7 @@ int __init init_smk_fs(void)
29842999
{
29853000
int err;
29863001
int rc;
3002+
struct netlbl_audit nai;
29873003

29883004
if (smack_enabled == 0)
29893005
return 0;
@@ -3002,7 +3018,10 @@ int __init init_smk_fs(void)
30023018
}
30033019
}
30043020

3005-
smk_cipso_doi();
3021+
smk_netlabel_audit_set(&nai);
3022+
(void) netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
3023+
(void) smk_cipso_doi(SMACK_CIPSO_DOI_DEFAULT,
3024+
GFP_KERNEL | __GFP_NOFAIL);
30063025
smk_unlbl_ambient(NULL);
30073026

30083027
rc = smack_populate_secattr(&smack_known_floor);

0 commit comments

Comments
 (0)