Skip to contentSkip to conversation

libsepol: add netif_wildcard polcap

PATCH In review Christian Göttsche 2 participants 7 messages Jun 16, 2025 – Jun 23, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v3 2/5 libsepol: add netif_wildcard polcap

From: Christian Göttsche <[email protected]>

The kernel supports wildcard in network interface names with a new policy capability 'netif_wildcard'.

---
v2: add patch

---
 libsepol/include/sepol/policydb/polcaps.h | 1 +
 libsepol/src/polcaps.c                    | 1 +
 2 files changed, 2 insertions(+)
libsepol/include/sepol/policydb/polcaps.h +1 -0
index 1aa9b30a..7bebe4da 100644
@@ -17,6 +17,7 @@ enum {
 	POLICYDB_CAP_IOCTL_SKIP_CLOEXEC,
 	POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
 	POLICYDB_CAP_NETLINK_XPERM,
+	POLICYDB_CAP_NETIF_WILDCARD,
 	__POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
libsepol/src/polcaps.c +1 -0
index 6b28c84e..ec6c902d 100644
@@ -16,6 +16,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = {
 	[POLICYDB_CAP_IOCTL_SKIP_CLOEXEC]		= "ioctl_skip_cloexec",
 	[POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT]	= "userspace_initial_context",
 	[POLICYDB_CAP_NETLINK_XPERM]			= "netlink_xperm",
+	[POLICYDB_CAP_NETIF_WILDCARD]			= "netif_wildcard",
 };
 
 int sepol_polcap_getnum(const char *name)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v3 1/5 libsepol: update sort order for netifcon definitions

From: Christian Göttsche <[email protected]>

Order netifcon definitions with a wildcard interface name last in object contexts, so the kernel tries to match literal names first.

---
 libsepol/cil/src/cil_post.c     | 36 ++++++++++++++++++++++++++++++---
 libsepol/src/kernel_to_common.c | 36 ++++++++++++++++++++++++++++++---
 2 files changed, 66 insertions(+), 6 deletions(-)
libsepol/cil/src/cil_post.c +33 -3
index d63a5496..70e5b734 100644
@@ -316,10 +316,40 @@ int cil_post_genfscon_compare(const void *a, const void *b)
 
 int cil_post_netifcon_compare(const void *a, const void *b)
 {
-	struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
-	struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
+	/* keep in sync with kernel_to_common.c:netif_data_cmp() */
+	const struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
+	const struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
+	const char *a_name = anetifcon->interface_str;
+	const char *b_name = bnetifcon->interface_str;
+	size_t a_stem = strcspn(a_name, "*?");
+	size_t b_stem = strcspn(b_name, "*?");
+	size_t a_len = strlen(a_name);
+	size_t b_len = strlen(b_name);
+	int a_iswildcard = a_stem != a_len;
+	int b_iswildcard = b_stem != b_len;
+	int rc;
+
+	/* order non-wildcards first */
+	rc = spaceship_cmp(a_iswildcard, b_iswildcard);
+	if (rc)
+		return rc;
+
+	/* order non-wildcards alphabetically */
+	if (!a_iswildcard)
+		return strcmp(a_name, b_name);
+
+	/* order by decreasing stem length */
+	rc = spaceship_cmp(a_stem, b_stem);
+	if (rc)
+		return -rc;
+
+	/* order '?' (0x3f) before '*' (0x2A) */
+	rc = spaceship_cmp(a_name[a_stem], b_name[b_stem]);
+	if (rc)
+		return -rc;
 
-	return  strcmp(anetifcon->interface_str, bnetifcon->interface_str);
+	/* order alphabetically */
+	return strcmp(a_name, b_name);
 }
 
 int cil_post_ibendportcon_compare(const void *a, const void *b)
libsepol/src/kernel_to_common.c +33 -3
index 44f0be23..e4338ec6 100644
@@ -441,10 +441,40 @@ static int portcon_data_cmp(const void *a, const void *b)
 
 static int netif_data_cmp(const void *a, const void *b)
 {
-	struct ocontext *const *aa = a;
-	struct ocontext *const *bb = b;
+	/* keep in sync with cil_post.c:cil_post_netifcon_compare() */
+	const struct ocontext *const *aa = a;
+	const struct ocontext *const *bb = b;
+	const char *a_name = (*aa)->u.name;
+	const char *b_name = (*bb)->u.name;
+	size_t a_stem = strcspn(a_name, "*?");
+	size_t b_stem = strcspn(b_name, "*?");
+	size_t a_len = strlen(a_name);
+	size_t b_len = strlen(b_name);
+	int a_iswildcard = a_stem != a_len;
+	int b_iswildcard = b_stem != b_len;
+	int rc;
 
-	return strcmp((*aa)->u.name, (*bb)->u.name);
+	/* order non-wildcards first */
+	rc = spaceship_cmp(a_iswildcard, b_iswildcard);
+	if (rc)
+		return rc;
+
+	/* order non-wildcards alphabetically */
+	if (!a_iswildcard)
+		return strcmp(a_name, b_name);
+
+	/* order by decreasing stem length */
+	rc = spaceship_cmp(a_stem, b_stem);
+	if (rc)
+		return -rc;
+
+	/* order '?' (0x3f) before '*' (0x2A) */
+	rc = spaceship_cmp(a_name[a_stem], b_name[b_stem]);
+	if (rc)
+		return -rc;
+
+	/* order alphabetically */
+	return strcmp(a_name, b_name);
 }
 
 static int node_data_cmp(const void *a, const void *b)
Signature · 1 line
2.49.0
Acked-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] libsepol: update sort order for netifcon definitions

Quoted 7 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Order netifcon definitions with a wildcard interface name last in object contexts, so the kernel tries to match literal names first.

For these five patches.

Quoted 105 lines
---
 libsepol/cil/src/cil_post.c     | 36 ++++++++++++++++++++++++++++++---
 libsepol/src/kernel_to_common.c | 36 ++++++++++++++++++++++++++++++---
 2 files changed, 66 insertions(+), 6 deletions(-)
libsepol/cil/src/cil_post.c +33 -3
index d63a5496..70e5b734 100644
@@ -316,10 +316,40 @@ int cil_post_genfscon_compare(const void *a, const void *b)
 
 int cil_post_netifcon_compare(const void *a, const void *b)
 {
-       struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
-       struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
+       /* keep in sync with kernel_to_common.c:netif_data_cmp() */
+       const struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
+       const struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
+       const char *a_name = anetifcon->interface_str;
+       const char *b_name = bnetifcon->interface_str;
+       size_t a_stem = strcspn(a_name, "*?");
+       size_t b_stem = strcspn(b_name, "*?");
+       size_t a_len = strlen(a_name);
+       size_t b_len = strlen(b_name);
+       int a_iswildcard = a_stem != a_len;
+       int b_iswildcard = b_stem != b_len;
+       int rc;
+
+       /* order non-wildcards first */
+       rc = spaceship_cmp(a_iswildcard, b_iswildcard);
+       if (rc)
+               return rc;
+
+       /* order non-wildcards alphabetically */
+       if (!a_iswildcard)
+               return strcmp(a_name, b_name);
+
+       /* order by decreasing stem length */
+       rc = spaceship_cmp(a_stem, b_stem);
+       if (rc)
+               return -rc;
+
+       /* order '?' (0x3f) before '*' (0x2A) */
+       rc = spaceship_cmp(a_name[a_stem], b_name[b_stem]);
+       if (rc)
+               return -rc;
 
-       return  strcmp(anetifcon->interface_str, bnetifcon->interface_str);
+       /* order alphabetically */
+       return strcmp(a_name, b_name);
 }
 
 int cil_post_ibendportcon_compare(const void *a, const void *b)
libsepol/src/kernel_to_common.c +33 -4
index 44f0be23..e4338ec6 100644
@@ -441,10 +441,40 @@ static int portcon_data_cmp(const void *a, const void *b)
 
 static int netif_data_cmp(const void *a, const void *b)
 {
-       struct ocontext *const *aa = a;
-       struct ocontext *const *bb = b;
+       /* keep in sync with cil_post.c:cil_post_netifcon_compare() */
+       const struct ocontext *const *aa = a;
+       const struct ocontext *const *bb = b;
+       const char *a_name = (*aa)->u.name;
+       const char *b_name = (*bb)->u.name;
+       size_t a_stem = strcspn(a_name, "*?");
+       size_t b_stem = strcspn(b_name, "*?");
+       size_t a_len = strlen(a_name);
+       size_t b_len = strlen(b_name);
+       int a_iswildcard = a_stem != a_len;
+       int b_iswildcard = b_stem != b_len;
+       int rc;
 
-       return strcmp((*aa)->u.name, (*bb)->u.name);
+       /* order non-wildcards first */
+       rc = spaceship_cmp(a_iswildcard, b_iswildcard);
+       if (rc)
+               return rc;
+
+       /* order non-wildcards alphabetically */
+       if (!a_iswildcard)
+               return strcmp(a_name, b_name);
+
+       /* order by decreasing stem length */
+       rc = spaceship_cmp(a_stem, b_stem);
+       if (rc)
+               return -rc;
+
+       /* order '?' (0x3f) before '*' (0x2A) */
+       rc = spaceship_cmp(a_name[a_stem], b_name[b_stem]);
+       if (rc)
+               return -rc;
+
+       /* order alphabetically */
+       return strcmp(a_name, b_name);
 }
 
 static int node_data_cmp(const void *a, const void *b)
--

2.49.0

#3 lore raw

Re: [PATCH v3 1/5] libsepol: update sort order for netifcon definitions

Quoted 6 lines · James Carter
Quoted 3 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Quoted 8 lines
Quoted 4 lines

Order netifcon definitions with a wildcard interface name last in object contexts, so the kernel tries to match literal names first.

For these five patches.

These five patches have been merged.
Thanks,
Jim

Quoted 105 lines
Quoted 105 lines
---
 libsepol/cil/src/cil_post.c     | 36 ++++++++++++++++++++++++++++++---
 libsepol/src/kernel_to_common.c | 36 ++++++++++++++++++++++++++++++---
 2 files changed, 66 insertions(+), 6 deletions(-)
libsepol/cil/src/cil_post.c +33 -3
index d63a5496..70e5b734 100644
@@ -316,10 +316,40 @@ int cil_post_genfscon_compare(const void *a, const void *b)
 
 int cil_post_netifcon_compare(const void *a, const void *b)
 {
-       struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
-       struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
+       /* keep in sync with kernel_to_common.c:netif_data_cmp() */
+       const struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
+       const struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
+       const char *a_name = anetifcon->interface_str;
+       const char *b_name = bnetifcon->interface_str;
+       size_t a_stem = strcspn(a_name, "*?");
+       size_t b_stem = strcspn(b_name, "*?");
+       size_t a_len = strlen(a_name);
+       size_t b_len = strlen(b_name);
+       int a_iswildcard = a_stem != a_len;
+       int b_iswildcard = b_stem != b_len;
+       int rc;
+
+       /* order non-wildcards first */
+       rc = spaceship_cmp(a_iswildcard, b_iswildcard);
+       if (rc)
+               return rc;
+
+       /* order non-wildcards alphabetically */
+       if (!a_iswildcard)
+               return strcmp(a_name, b_name);
+
+       /* order by decreasing stem length */
+       rc = spaceship_cmp(a_stem, b_stem);
+       if (rc)
+               return -rc;
+
+       /* order '?' (0x3f) before '*' (0x2A) */
+       rc = spaceship_cmp(a_name[a_stem], b_name[b_stem]);
+       if (rc)
+               return -rc;
 
-       return  strcmp(anetifcon->interface_str, bnetifcon->interface_str);
+       /* order alphabetically */
+       return strcmp(a_name, b_name);
 }
 
 int cil_post_ibendportcon_compare(const void *a, const void *b)
libsepol/src/kernel_to_common.c +33 -4
index 44f0be23..e4338ec6 100644
@@ -441,10 +441,40 @@ static int portcon_data_cmp(const void *a, const void *b)
 
 static int netif_data_cmp(const void *a, const void *b)
 {
-       struct ocontext *const *aa = a;
-       struct ocontext *const *bb = b;
+       /* keep in sync with cil_post.c:cil_post_netifcon_compare() */
+       const struct ocontext *const *aa = a;
+       const struct ocontext *const *bb = b;
+       const char *a_name = (*aa)->u.name;
+       const char *b_name = (*bb)->u.name;
+       size_t a_stem = strcspn(a_name, "*?");
+       size_t b_stem = strcspn(b_name, "*?");
+       size_t a_len = strlen(a_name);
+       size_t b_len = strlen(b_name);
+       int a_iswildcard = a_stem != a_len;
+       int b_iswildcard = b_stem != b_len;
+       int rc;
 
-       return strcmp((*aa)->u.name, (*bb)->u.name);
+       /* order non-wildcards first */
+       rc = spaceship_cmp(a_iswildcard, b_iswildcard);
+       if (rc)
+               return rc;
+
+       /* order non-wildcards alphabetically */
+       if (!a_iswildcard)
+               return strcmp(a_name, b_name);
+
+       /* order by decreasing stem length */
+       rc = spaceship_cmp(a_stem, b_stem);
+       if (rc)
+               return -rc;
+
+       /* order '?' (0x3f) before '*' (0x2A) */
+       rc = spaceship_cmp(a_name[a_stem], b_name[b_stem]);
+       if (rc)
+               return -rc;
+
+       /* order alphabetically */
+       return strcmp(a_name, b_name);
 }
 
 static int node_data_cmp(const void *a, const void *b)
--

2.49.0

#4 lore raw

PATCH v3 3/5 checkpolicy: add support for wildcard netifcon names

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Support network interface names containing wildcards.

---
v3: also support names starting with a digit (tokenized as filesystem
name)

---
 checkpolicy/policy_parse.y                         | 10 +++++++++-
 checkpolicy/policy_scan.l                          |  1 +
 checkpolicy/tests/policy_allonce.conf              |  6 +++++-
 checkpolicy/tests/policy_allonce.expected.conf     |  6 +++++-
 checkpolicy/tests/policy_allonce.expected_opt.conf |  6 +++++-
 5 files changed, 25 insertions(+), 4 deletions(-)
checkpolicy/policy_parse.y +9 -1
index a88a6a7f..0089e541 100644
@@ -154,6 +154,7 @@ typedef int (* require_func_t)(int pass);
 %token PERMISSIVE
 %token NEVERAUDIT
 %token FILESYSTEM
+%token NETIFNAME
 %token DEFAULT_USER DEFAULT_ROLE DEFAULT_TYPE DEFAULT_RANGE
 %token LOW_HIGH LOW HIGH GLBLUB
 %token INVALID_CHAR
@@ -750,7 +751,7 @@ opt_netif_contexts      : netif_contexts
 netif_contexts		: netif_context_def
 			| netif_contexts netif_context_def
 			;
-netif_context_def	: NETIFCON identifier security_context_def security_context_def
+netif_context_def	: NETIFCON netifname security_context_def security_context_def
 			{if (define_netif_context()) YYABORT;}
 			;
 opt_node_contexts       : node_contexts 
@@ -903,6 +904,13 @@ path     		: PATH
 filename		: FILENAME
 			{ yytext[strlen(yytext) - 1] = '\0'; if (insert_id(yytext + 1,0)) YYABORT; }
 			;
+netifname		: NETIFNAME
+			{ if (insert_id(yytext,0)) YYABORT; }
+			| IDENTIFIER
+			{ if (insert_id(yytext,0)) YYABORT; }
+			| FILESYSTEM
+			{ if (insert_id(yytext,0)) YYABORT; }
+                        ;
 number			: NUMBER 
 			{ unsigned long x;
 			  errno = 0;
checkpolicy/policy_scan.l +1 -0
index 3ab5c2c7..8f9d73d6 100644
@@ -300,6 +300,7 @@ GLBLUB				{ return(GLBLUB); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*  { return(IPV6_ADDR); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*"/"{digit}{1,3}	{ return(IPV6_CIDR); }
 {digit}+(\.({alnum}|[_.])*)?    { return(VERSION_IDENTIFIER); }
+({alnum}({alnum}|[\*\?_]){0,15}|[\*\?]({alnum}|[\*\?_]){1,15})	{ return(NETIFNAME); }
 #line[ ]{digit}+[ ]\"[^\n]*\"	{ set_source_line_and_file(yytext+6); }
 #line[ ]{digit}+	        {
 				  errno = 0;
checkpolicy/tests/policy_allonce.conf +5 -1
index 6565703c..4b1edb4f 100644
@@ -75,7 +75,11 @@ genfscon proc "/file1" -- USER1:ROLE1:TYPE1
 genfscon proc "/path/to/file" USER1:ROLE1:TYPE1
 portcon tcp 80 USER1:ROLE1:TYPE1
 portcon udp 100-200 USER1:ROLE1:TYPE1
-netifcon lo USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net? USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net* USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon netx* USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon 1o USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
 nodecon 127.0.0.1 255.255.255.255 USER1:ROLE1:TYPE1
 nodecon 192.168.42.0 255.255.0.0 USER1:ROLE1:TYPE1
 nodecon 127.0.0.1/24 USER1:ROLE1:TYPE1
checkpolicy/tests/policy_allonce.expected.conf +5 -1
index 5697bb6e..17eff98c 100644
@@ -80,7 +80,11 @@ genfscon proc "/file1" -- USER1:ROLE1:TYPE1
 genfscon proc "/path/to/file" USER1:ROLE1:TYPE1
 portcon tcp 80 USER1:ROLE1:TYPE1
 portcon udp 100-200 USER1:ROLE1:TYPE1
-netifcon lo USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon 1o USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon netx* USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net? USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net* USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
 nodecon 127.0.0.1 255.255.255.255 USER1:ROLE1:TYPE1
 nodecon 127.0.0.0 255.255.255.0 USER1:ROLE1:TYPE1
 nodecon 192.168.0.0 255.255.0.0 USER1:ROLE1:TYPE1
checkpolicy/tests/policy_allonce.expected_opt.conf +5 -1
index 478d1db8..6b0f73fe 100644
@@ -80,7 +80,11 @@ genfscon proc "/file1" -- USER1:ROLE1:TYPE1
 genfscon proc "/path/to/file" USER1:ROLE1:TYPE1
 portcon tcp 80 USER1:ROLE1:TYPE1
 portcon udp 100-200 USER1:ROLE1:TYPE1
-netifcon lo USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon 1o USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon netx* USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net? USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
+netifcon net* USER1:ROLE1:TYPE1 USER1:ROLE1:TYPE1
 nodecon 127.0.0.1 255.255.255.255 USER1:ROLE1:TYPE1
 nodecon 127.0.0.0 255.255.255.0 USER1:ROLE1:TYPE1
 nodecon 192.168.0.0 255.255.0.0 USER1:ROLE1:TYPE1
Signature · 1 line
2.49.0
#5 lore raw

PATCH v3 4/5 secilc/test: add test for wildcard netifcon statement

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

---
 secilc/test/policy.cil | 1 +
 1 file changed, 1 insertion(+)
secilc/test/policy.cil +1 -0
index d405a0ef..e3b07fc8 100644
@@ -289,6 +289,7 @@
 	(portcon sctp (1024 1035) system_u_bin_t_l2h)
 	(genfscon - "/usr/bin" system_u_bin_t_l2h)
 	(netifcon eth0 system_u_bin_t_l2h system_u_bin_t_l2h) ;different contexts?
+	(netifcon podman* system_u_bin_t_l2h system_u_bin_t_l2h)
 	(fsuse xattr ext3 system_u_bin_t_l2h)
 	
 	; XEN
Signature · 1 line
2.49.0
#6 lore raw

PATCH v3 5/5 libsepol/cil: reject wildcard only network interface names

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Like checkpolicy reject the network interface names "*" and "?". These patterns are already covered by the netif initial SID.

---
v3: add patch

---
 libsepol/cil/src/cil_verify.c | 5 +++++
 1 file changed, 5 insertions(+)
libsepol/cil/src/cil_verify.c +5 -0
index cde9dd41..9654d71d 100644
@@ -1242,6 +1242,11 @@ static int __cil_verify_netifcon(struct cil_db *db, struct cil_tree_node *node)
 		}
 	}
 
+	if (strcmp(netif->interface_str, "?") == 0 ||
+	    strcmp(netif->interface_str, "*") == 0) {
+		goto exit;
+	}
+
 	return SEPOL_OK;
 
 exit:
Signature · 1 line
2.49.0