Skip to content
/ linux Public

Commit 1c7ee23

Browse files
goongasSasha Levin
authored andcommitted
smack: /smack/doi: accept previously used values
[ Upstream commit 33d589e ] 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> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 661d872 commit 1c7ee23

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

security/smack/smackfs.c

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ enum smk_inos {
6868
static DEFINE_MUTEX(smack_cipso_lock);
6969
static DEFINE_MUTEX(smack_ambient_lock);
7070
static DEFINE_MUTEX(smk_net4addr_lock);
71+
static DEFINE_MUTEX(smk_cipso_doi_lock);
7172
#if IS_ENABLED(CONFIG_IPV6)
7273
static DEFINE_MUTEX(smk_net6addr_lock);
7374
#endif /* CONFIG_IPV6 */
@@ -139,7 +140,7 @@ struct smack_parsed_rule {
139140
int smk_access2;
140141
};
141142

142-
static u32 smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
143+
static u32 smk_cipso_doi_value = CIPSO_V4_DOI_UNKNOWN;
143144

144145
/*
145146
* Values for parsing cipso rules
@@ -679,43 +680,60 @@ static const struct file_operations smk_load_ops = {
679680
};
680681

681682
/**
682-
* smk_cipso_doi - initialize the CIPSO domain
683+
* smk_cipso_doi - set netlabel maps
684+
* @ndoi: new value for our CIPSO DOI
685+
* @gfp_flags: kmalloc allocation context
683686
*/
684-
static void smk_cipso_doi(void)
687+
static int
688+
smk_cipso_doi(u32 ndoi, gfp_t gfp_flags)
685689
{
686-
int rc;
690+
int rc = 0;
687691
struct cipso_v4_doi *doip;
688692
struct netlbl_audit nai;
689693

690-
smk_netlabel_audit_set(&nai);
694+
mutex_lock(&smk_cipso_doi_lock);
691695

692-
rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
693-
if (rc != 0)
694-
printk(KERN_WARNING "%s:%d remove rc = %d\n",
695-
__func__, __LINE__, rc);
696+
if (smk_cipso_doi_value == ndoi)
697+
goto clr_doi_lock;
698+
699+
smk_netlabel_audit_set(&nai);
696700

697-
doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL | __GFP_NOFAIL);
701+
doip = kmalloc(sizeof(struct cipso_v4_doi), gfp_flags);
702+
if (!doip) {
703+
rc = -ENOMEM;
704+
goto clr_doi_lock;
705+
}
698706
doip->map.std = NULL;
699-
doip->doi = smk_cipso_doi_value;
707+
doip->doi = ndoi;
700708
doip->type = CIPSO_V4_MAP_PASS;
701709
doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
702710
for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
703711
doip->tags[rc] = CIPSO_V4_TAG_INVALID;
704712

705713
rc = netlbl_cfg_cipsov4_add(doip, &nai);
706-
if (rc != 0) {
707-
printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
708-
__func__, __LINE__, rc);
714+
if (rc) {
709715
kfree(doip);
710-
return;
716+
goto clr_doi_lock;
711717
}
712-
rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
713-
if (rc != 0) {
714-
printk(KERN_WARNING "%s:%d map add rc = %d\n",
715-
__func__, __LINE__, rc);
716-
netlbl_cfg_cipsov4_del(doip->doi, &nai);
717-
return;
718+
719+
if (smk_cipso_doi_value != CIPSO_V4_DOI_UNKNOWN) {
720+
rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
721+
if (rc && rc != -ENOENT)
722+
goto clr_ndoi_def;
723+
724+
netlbl_cfg_cipsov4_del(smk_cipso_doi_value, &nai);
718725
}
726+
727+
rc = netlbl_cfg_cipsov4_map_add(ndoi, NULL, NULL, NULL, &nai);
728+
if (rc) {
729+
smk_cipso_doi_value = CIPSO_V4_DOI_UNKNOWN; // no default map
730+
clr_ndoi_def: netlbl_cfg_cipsov4_del(ndoi, &nai);
731+
} else
732+
smk_cipso_doi_value = ndoi;
733+
734+
clr_doi_lock:
735+
mutex_unlock(&smk_cipso_doi_lock);
736+
return rc;
719737
}
720738

721739
/**
@@ -1617,11 +1635,8 @@ static ssize_t smk_write_doi(struct file *file, const char __user *buf,
16171635

16181636
if (u == CIPSO_V4_DOI_UNKNOWN || u > U32_MAX)
16191637
return -EINVAL;
1620-
smk_cipso_doi_value = u;
1621-
1622-
smk_cipso_doi();
16231638

1624-
return count;
1639+
return smk_cipso_doi(u, GFP_KERNEL) ? : count;
16251640
}
16261641

16271642
static const struct file_operations smk_doi_ops = {
@@ -2998,6 +3013,7 @@ static int __init init_smk_fs(void)
29983013
{
29993014
int err;
30003015
int rc;
3016+
struct netlbl_audit nai;
30013017

30023018
if (smack_enabled == 0)
30033019
return 0;
@@ -3016,7 +3032,10 @@ static int __init init_smk_fs(void)
30163032
}
30173033
}
30183034

3019-
smk_cipso_doi();
3035+
smk_netlabel_audit_set(&nai);
3036+
(void) netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
3037+
(void) smk_cipso_doi(SMACK_CIPSO_DOI_DEFAULT,
3038+
GFP_KERNEL | __GFP_NOFAIL);
30203039
smk_unlbl_ambient(NULL);
30213040

30223041
rc = smack_populate_secattr(&smack_known_floor);

0 commit comments

Comments
 (0)