Skip to contentSkip to conversation

libsepol/cil: support IPv4/IPv6 address embedding

PATCH In review Christian Göttsche 2 participants 5 messages Nov 30, 2021 – Dec 17, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol/cil: support IPv4/IPv6 address embedding

Accept IPv4 addresses embedded in IPv6, like ::ffff:127.0.0.1. This allows using those in nodecon statements leading to fine grained access control:

    type=AVC msg=audit(11/29/21 20:27:44.437:419) : avc:  granted  { node_bind } for  pid=27500 comm=intercept saddr=::ffff:127.0.0.1 src=46293 scontext=xuser_u:xuser_r:xuser_t:s0 tcontext=system_u:object_r:lo_node_t:s0 tclass=tcp_socket

This does effect policies in the traditional language due to CIL usage in semodule(8).

Also print on conversion failures the address in question.

---
 libsepol/cil/src/cil_build_ast.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
libsepol/cil/src/cil_build_ast.c +4 -4
index 9c34be23..eccb331b 100644
@@ -5668,10 +5668,10 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
 		goto exit;
 	}
 
-	if (strchr(addr_node->data, '.') != NULL) {
-		addr->family = AF_INET;
-	} else {
+	if (strchr(addr_node->data, ':') != NULL) {
 		addr->family = AF_INET6;
+	} else {
+		addr->family = AF_INET;
 	}
 
 	rc = inet_pton(addr->family, addr_node->data, &addr->ip);
@@ -5683,7 +5683,7 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
 	return SEPOL_OK;
 
 exit:
-	cil_log(CIL_ERR, "Bad ip address or netmask\n"); 
+	cil_log(CIL_ERR, "Bad ip address or netmask: %s\n", (addr_node && addr_node->data) ? (const char *)addr_node->data : "n/a");
 	return rc;
 }
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 checkpolicy: warn on bogus IP address or netmask in nodecon statement

Warn if the netmask is not contiguous or the address has host bits set, e.g.:

    127.0.0.0 255.255.245.0
    127.0.0.1 255.255.255.0
---
 checkpolicy/policy_define.c | 50 +++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
checkpolicy/policy_define.c +50 -0
index d3eb6111..b2ae3263 100644
@@ -5290,6 +5290,14 @@ int define_ipv4_node_context()
 		goto out;
 	}
 
+	if (mask.s_addr != 0 && ((~mask.s_addr + 1) & ~mask.s_addr) != 0) {
+		yywarn("ipv4 mask is not contiguous");
+	}
+
+	if ((~mask.s_addr & addr.s_addr) != 0) {
+		yywarn("host bits in ipv4 address set");
+	}
+
 	newc = malloc(sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
@@ -5325,6 +5333,40 @@ out:
 	return rc;
 }
 
+static int ipv6_is_mask_contiguous(const struct in6_addr *mask)
+{
+	int filled = 1;
+	unsigned i;
+
+	for (i = 0; i < 16; i++) {
+		if ((((~mask->s6_addr[i] & 0xFF) + 1) & (~mask->s6_addr[i] & 0xFF)) != 0) {
+			return 0;
+		}
+		if (!filled && mask->s6_addr[i] != 0) {
+			return 0;
+		}
+
+		if (filled && mask->s6_addr[i] != 0xFF) {
+			filled = 0;
+		}
+	}
+
+	return 1;
+}
+
+static int ipv6_has_host_bits_set(const struct in6_addr *addr, const struct in6_addr *mask)
+{
+	unsigned i;
+
+	for (i = 0; i < 16; i++) {
+		if ((addr->s6_addr[i] & ~mask->s6_addr[i]) != 0) {
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
 int define_ipv6_node_context(void)
 {
 	char *id;
@@ -5376,6 +5418,14 @@ int define_ipv6_node_context(void)
 		goto out;
 	}
 
+	if (!ipv6_is_mask_contiguous(&mask)) {
+		yywarn("ipv6 mask is not contiguous");
+	}
+
+	if (ipv6_has_host_bits_set(&addr, &mask)) {
+		yywarn("host bits in ipv6 address set");
+	}
+
 	newc = malloc(sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
Signature · 1 line
2.34.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] checkpolicy: warn on bogus IP address or netmask in nodecon statement

Quoted 8 lines · Christian Göttsche

Warn if the netmask is not contiguous or the address has host bits set, e.g.:

    127.0.0.0 255.255.245.0
    127.0.0.1 255.255.255.0
Quoted 82 lines
---
 checkpolicy/policy_define.c | 50 +++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
checkpolicy/policy_define.c +50 -1
index d3eb6111..b2ae3263 100644
@@ -5290,6 +5290,14 @@ int define_ipv4_node_context()
                goto out;
        }
 
+       if (mask.s_addr != 0 && ((~mask.s_addr + 1) & ~mask.s_addr) != 0) {
+               yywarn("ipv4 mask is not contiguous");
+       }
+
+       if ((~mask.s_addr & addr.s_addr) != 0) {
+               yywarn("host bits in ipv4 address set");
+       }
+
        newc = malloc(sizeof(ocontext_t));
        if (!newc) {
                yyerror("out of memory");
@@ -5325,6 +5333,40 @@ out:
        return rc;
 }
 
+static int ipv6_is_mask_contiguous(const struct in6_addr *mask)
+{
+       int filled = 1;
+       unsigned i;
+
+       for (i = 0; i < 16; i++) {
+               if ((((~mask->s6_addr[i] & 0xFF) + 1) & (~mask->s6_addr[i] & 0xFF)) != 0) {
+                       return 0;
+               }
+               if (!filled && mask->s6_addr[i] != 0) {
+                       return 0;
+               }
+
+               if (filled && mask->s6_addr[i] != 0xFF) {
+                       filled = 0;
+               }
+       }
+
+       return 1;
+}
+
+static int ipv6_has_host_bits_set(const struct in6_addr *addr, const struct in6_addr *mask)
+{
+       unsigned i;
+
+       for (i = 0; i < 16; i++) {
+               if ((addr->s6_addr[i] & ~mask->s6_addr[i]) != 0) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 int define_ipv6_node_context(void)
 {
        char *id;
@@ -5376,6 +5418,14 @@ int define_ipv6_node_context(void)
                goto out;
        }
 
+       if (!ipv6_is_mask_contiguous(&mask)) {
+               yywarn("ipv6 mask is not contiguous");
+       }
+
+       if (ipv6_has_host_bits_set(&addr, &mask)) {
+               yywarn("host bits in ipv6 address set");
+       }
+
        newc = malloc(sizeof(ocontext_t));
        if (!newc) {
                yyerror("out of memory");
--

2.34.1

Acked-by on v1 1/2
#3 lore raw
In reply to Christian Göttsche
Quoted 5 lines · Christian Göttsche

Accept IPv4 addresses embedded in IPv6, like ::ffff:127.0.0.1. This allows using those in nodecon statements leading to fine grained access control:

Quoted 8 lines
    type=AVC msg=audit(11/29/21 20:27:44.437:419) : avc:  granted  { node_bind } for  pid=27500 comm=intercept saddr=::ffff:127.0.0.1 src=46293 scontext=xuser_u:xuser_r:xuser_t:s0 tcontext=system_u:object_r:lo_node_t:s0 tclass=tcp_socket

This does effect policies in the traditional language due to CIL usage in semodule(8).

Also print on conversion failures the address in question.

Quoted 34 lines
---
 libsepol/cil/src/cil_build_ast.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
libsepol/cil/src/cil_build_ast.c +4 -5
index 9c34be23..eccb331b 100644
@@ -5668,10 +5668,10 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
                goto exit;
        }
 
-       if (strchr(addr_node->data, '.') != NULL) {
-               addr->family = AF_INET;
-       } else {
+       if (strchr(addr_node->data, ':') != NULL) {
                addr->family = AF_INET6;
+       } else {
+               addr->family = AF_INET;
        }
 
        rc = inet_pton(addr->family, addr_node->data, &addr->ip);
@@ -5683,7 +5683,7 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
        return SEPOL_OK;
 
 exit:
-       cil_log(CIL_ERR, "Bad ip address or netmask\n");
+       cil_log(CIL_ERR, "Bad ip address or netmask: %s\n", (addr_node && addr_node->data) ? (const char *)addr_node->data : "n/a");
        return rc;
 }
 
--

2.34.1

#4 lore raw
Quoted 10 lines · James Carter
Quoted 7 lines · Christian Göttsche

Accept IPv4 addresses embedded in IPv6, like ::ffff:127.0.0.1. This allows using those in nodecon statements leading to fine grained access control:

    type=AVC msg=audit(11/29/21 20:27:44.437:419) : avc:  granted  { node_bind } for  pid=27500 comm=intercept saddr=::ffff:127.0.0.1 src=46293 scontext=xuser_u:xuser_r:xuser_t:s0 tcontext=system_u:object_r:lo_node_t:s0 tclass=tcp_socket
Quoted 9 lines
Quoted 6 lines

This does effect policies in the traditional language due to CIL usage in semodule(8).

Also print on conversion failures the address in question.

Both of these have been merged.
Thanks,
Jim

Quoted 34 lines
Quoted 34 lines
---
 libsepol/cil/src/cil_build_ast.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
libsepol/cil/src/cil_build_ast.c +4 -5
index 9c34be23..eccb331b 100644
@@ -5668,10 +5668,10 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
                goto exit;
        }
 
-       if (strchr(addr_node->data, '.') != NULL) {
-               addr->family = AF_INET;
-       } else {
+       if (strchr(addr_node->data, ':') != NULL) {
                addr->family = AF_INET6;
+       } else {
+               addr->family = AF_INET;
        }
 
        rc = inet_pton(addr->family, addr_node->data, &addr->ip);
@@ -5683,7 +5683,7 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
        return SEPOL_OK;
 
 exit:
-       cil_log(CIL_ERR, "Bad ip address or netmask\n");
+       cil_log(CIL_ERR, "Bad ip address or netmask: %s\n", (addr_node && addr_node->data) ? (const char *)addr_node->data : "n/a");
        return rc;
 }
 
--

2.34.1