Skip to contentSkip to conversation

libsepol: drop confusing BUG_ON macro

PATCH In review Nicolas Iooss 2 participants 6 messages Oct 3, 2020 – Oct 19, 2020 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol: drop confusing BUG_ON macro

Contrary to Linux kernel, BUG_ON() does not halt the execution, in libsepol/src/services.c. Instead it displays an error message and continues the execution.

This means that this code does not prevent an out-of-bound write from happening:

    case CEXPR_AND:
        BUG_ON(sp < 1);
        sp--;
        s[sp] &= s[sp + 1];

Use if(...){BUG();rc=-EINVAL;goto out;} constructions instead, to make sure that the array access is always in-bound.

This issue has been found using clang's static analyzer:
https://558-118970575-gh.circle-artifacts.com/0/output-scan-build/2020-10-02-065849-6375-1/report-50a861.html#EndPath

---
 libsepol/src/services.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
libsepol/src/services.c +15 -4
index 90da1f4efef3..beb0711f6680 100644
@@ -67,7 +67,6 @@
 #include "flask.h"
 
 #define BUG() do { ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
-#define BUG_ON(x) do { if (x) ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
 
 static int selinux_enforcing = 1;
 
@@ -469,18 +468,30 @@ static int constraint_expr_eval_reason(context_struct_t *scontext,
 		/* Now process each expression of the constraint */
 		switch (e->expr_type) {
 		case CEXPR_NOT:
-			BUG_ON(sp < 0);
+			if (sp < 0) {
+				BUG();
+				rc = -EINVAL;
+				goto out;
+			}
 			s[sp] = !s[sp];
 			cat_expr_buf(expr_list[expr_counter], "not");
 			break;
 		case CEXPR_AND:
-			BUG_ON(sp < 1);
+			if (sp < 1) {
+				BUG();
+				rc = -EINVAL;
+				goto out;
+			}
 			sp--;
 			s[sp] &= s[sp + 1];
 			cat_expr_buf(expr_list[expr_counter], "and");
 			break;
 		case CEXPR_OR:
-			BUG_ON(sp < 1);
+			if (sp < 1) {
+				BUG();
+				rc = -EINVAL;
+				goto out;
+			}
 			sp--;
 			s[sp] |= s[sp + 1];
 			cat_expr_buf(expr_list[expr_counter], "or");
Signature · 1 line
2.28.0
#1 lore raw

PATCH 2/2 libsepol: silence potential NULL pointer dereference warning

When find_avtab_node() is called with key->specified & AVTAB_XPERMS and xperms=NULL, xperms is being dereferenced. This is detected as a "NULL pointer dereference issue" by static analyzers.

Even though it does not make much sense to call find_avtab_node() in a way which triggers the NULL pointer dereference issue, static analyzers have a hard time with calls such as:

    node = find_avtab_node(handle, avtab, &avkey, cond, NULL);

... where xperms=NULL.

So, make the function report an error instead of crashing.

Here is an example of report from clang's static analyzer:
https://558-118970575-gh.circle-artifacts.com/0/output-scan-build/2020-10-02-065849-6375-1/report-d86a57.html#EndPath

---
 libsepol/src/expand.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
libsepol/src/expand.c +14 -9
index 19e48c507236..eac7e4507d02 100644
@@ -1570,17 +1570,22 @@ static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,
 
 	/* AVTAB_XPERMS entries are not necessarily unique */
 	if (key->specified & AVTAB_XPERMS) {
-		node = avtab_search_node(avtab, key);
-		while (node) {
-			if ((node->datum.xperms->specified == xperms->specified) &&
-				(node->datum.xperms->driver == xperms->driver)) {
-				match = 1;
-				break;
+		if (xperms == NULL) {
+			ERR(handle, "searching xperms NULL");
+			node = NULL;
+		} else {
+			node = avtab_search_node(avtab, key);
+			while (node) {
+				if ((node->datum.xperms->specified == xperms->specified) &&
+					(node->datum.xperms->driver == xperms->driver)) {
+					match = 1;
+					break;
+				}
+				node = avtab_search_node_next(node, key->specified);
 			}
-			node = avtab_search_node_next(node, key->specified);
+			if (!match)
+				node = NULL;
 		}
-		if (!match)
-			node = NULL;
 	} else {
 		node = avtab_search_node(avtab, key);
 	}
Signature · 1 line
2.28.0
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] libsepol: silence potential NULL pointer dereference warning

Quoted 11 lines · Nicolas Iooss

When find_avtab_node() is called with key->specified & AVTAB_XPERMS and xperms=NULL, xperms is being dereferenced. This is detected as a "NULL pointer dereference issue" by static analyzers.

Even though it does not make much sense to call find_avtab_node() in a way which triggers the NULL pointer dereference issue, static analyzers have a hard time with calls such as:

    node = find_avtab_node(handle, avtab, &avkey, cond, NULL);
Quoted 8 lines

... where xperms=NULL.

So, make the function report an error instead of crashing.

Here is an example of report from clang's static analyzer:
https://558-118970575-gh.circle-artifacts.com/0/output-scan-build/2020-10-02-065849-6375-1/report-d86a57.html#EndPath

Quoted 43 lines
---
 libsepol/src/expand.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
libsepol/src/expand.c +14 -10
index 19e48c507236..eac7e4507d02 100644
@@ -1570,17 +1570,22 @@ static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,
 
        /* AVTAB_XPERMS entries are not necessarily unique */
        if (key->specified & AVTAB_XPERMS) {
-               node = avtab_search_node(avtab, key);
-               while (node) {
-                       if ((node->datum.xperms->specified == xperms->specified) &&
-                               (node->datum.xperms->driver == xperms->driver)) {
-                               match = 1;
-                               break;
+               if (xperms == NULL) {
+                       ERR(handle, "searching xperms NULL");
+                       node = NULL;
+               } else {
+                       node = avtab_search_node(avtab, key);
+                       while (node) {
+                               if ((node->datum.xperms->specified == xperms->specified) &&
+                                       (node->datum.xperms->driver == xperms->driver)) {
+                                       match = 1;
+                                       break;
+                               }
+                               node = avtab_search_node_next(node, key->specified);
                        }
-                       node = avtab_search_node_next(node, key->specified);
+                       if (!match)
+                               node = NULL;
                }
-               if (!match)
-                       node = NULL;
        } else {
                node = avtab_search_node(avtab, key);
        }
--

2.28.0

#3 lore raw

Re: [PATCH 2/2] libsepol: silence potential NULL pointer dereference warning

Quoted 15 lines · James Carter
Quoted 13 lines · Nicolas Iooss

When find_avtab_node() is called with key->specified & AVTAB_XPERMS and xperms=NULL, xperms is being dereferenced. This is detected as a "NULL pointer dereference issue" by static analyzers.

Even though it does not make much sense to call find_avtab_node() in a way which triggers the NULL pointer dereference issue, static analyzers have a hard time with calls such as:

    node = find_avtab_node(handle, avtab, &avkey, cond, NULL);

... where xperms=NULL.

Quoted 8 lines
Quoted 6 lines

So, make the function report an error instead of crashing.

Here is an example of report from clang's static analyzer:
https://558-118970575-gh.circle-artifacts.com/0/output-scan-build/2020-10-02-065849-6375-1/report-d86a57.html#EndPath

Thanks. Merged.

Nicolas

Quoted 43 lines
Quoted 43 lines
---
 libsepol/src/expand.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
libsepol/src/expand.c +14 -10
index 19e48c507236..eac7e4507d02 100644
@@ -1570,17 +1570,22 @@ static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,
 
        /* AVTAB_XPERMS entries are not necessarily unique */
        if (key->specified & AVTAB_XPERMS) {
-               node = avtab_search_node(avtab, key);
-               while (node) {
-                       if ((node->datum.xperms->specified == xperms->specified) &&
-                               (node->datum.xperms->driver == xperms->driver)) {
-                               match = 1;
-                               break;
+               if (xperms == NULL) {
+                       ERR(handle, "searching xperms NULL");
+                       node = NULL;
+               } else {
+                       node = avtab_search_node(avtab, key);
+                       while (node) {
+                               if ((node->datum.xperms->specified == xperms->specified) &&
+                                       (node->datum.xperms->driver == xperms->driver)) {
+                                       match = 1;
+                                       break;
+                               }
+                               node = avtab_search_node_next(node, key->specified);
                        }
-                       node = avtab_search_node_next(node, key->specified);
+                       if (!match)
+                               node = NULL;
                }
-               if (!match)
-                       node = NULL;
        } else {
                node = avtab_search_node(avtab, key);
        }
--

2.28.0

Acked-by on v1 1/2
#4 lore raw
In reply to Nicolas Iooss
Quoted 13 lines · Nicolas Iooss

Contrary to Linux kernel, BUG_ON() does not halt the execution, in libsepol/src/services.c. Instead it displays an error message and continues the execution.

This means that this code does not prevent an out-of-bound write from happening:

    case CEXPR_AND:
        BUG_ON(sp < 1);
        sp--;
        s[sp] &= s[sp + 1];
Quoted 7 lines

Use if(...){BUG();rc=-EINVAL;goto out;} constructions instead, to make sure that the array access is always in-bound.

This issue has been found using clang's static analyzer:
https://558-118970575-gh.circle-artifacts.com/0/output-scan-build/2020-10-02-065849-6375-1/report-50a861.html#EndPath

Quoted 53 lines
---
 libsepol/src/services.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
libsepol/src/services.c +15 -5
index 90da1f4efef3..beb0711f6680 100644
@@ -67,7 +67,6 @@
 #include "flask.h"
 
 #define BUG() do { ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
-#define BUG_ON(x) do { if (x) ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
 
 static int selinux_enforcing = 1;
 
@@ -469,18 +468,30 @@ static int constraint_expr_eval_reason(context_struct_t *scontext,
                /* Now process each expression of the constraint */
                switch (e->expr_type) {
                case CEXPR_NOT:
-                       BUG_ON(sp < 0);
+                       if (sp < 0) {
+                               BUG();
+                               rc = -EINVAL;
+                               goto out;
+                       }
                        s[sp] = !s[sp];
                        cat_expr_buf(expr_list[expr_counter], "not");
                        break;
                case CEXPR_AND:
-                       BUG_ON(sp < 1);
+                       if (sp < 1) {
+                               BUG();
+                               rc = -EINVAL;
+                               goto out;
+                       }
                        sp--;
                        s[sp] &= s[sp + 1];
                        cat_expr_buf(expr_list[expr_counter], "and");
                        break;
                case CEXPR_OR:
-                       BUG_ON(sp < 1);
+                       if (sp < 1) {
+                               BUG();
+                               rc = -EINVAL;
+                               goto out;
+                       }
                        sp--;
                        s[sp] |= s[sp + 1];
                        cat_expr_buf(expr_list[expr_counter], "or");
--

2.28.0

#5 lore raw
Quoted 18 lines · James Carter
Quoted 16 lines · Nicolas Iooss

Contrary to Linux kernel, BUG_ON() does not halt the execution, in libsepol/src/services.c. Instead it displays an error message and continues the execution.

This means that this code does not prevent an out-of-bound write from happening:

    case CEXPR_AND:
        BUG_ON(sp < 1);
        sp--;
        s[sp] &= s[sp + 1];

Use if(...){BUG();rc=-EINVAL;goto out;} constructions instead, to make sure that the array access is always in-bound.

Quoted 6 lines
Quoted 4 lines

This issue has been found using clang's static analyzer:
https://558-118970575-gh.circle-artifacts.com/0/output-scan-build/2020-10-02-065849-6375-1/report-50a861.html#EndPath

Thanks. Merged.

Nicolas

Quoted 54 lines
Quoted 54 lines
---
 libsepol/src/services.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
libsepol/src/services.c +15 -5
index 90da1f4efef3..beb0711f6680 100644
@@ -67,7 +67,6 @@
 #include "flask.h"
 
 #define BUG() do { ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
-#define BUG_ON(x) do { if (x) ERR(NULL, "Badness at %s:%d", __FILE__, __LINE__); } while (0)
 
 static int selinux_enforcing = 1;
 
@@ -469,18 +468,30 @@ static int constraint_expr_eval_reason(context_struct_t *scontext,
                /* Now process each expression of the constraint */
                switch (e->expr_type) {
                case CEXPR_NOT:
-                       BUG_ON(sp < 0);
+                       if (sp < 0) {
+                               BUG();
+                               rc = -EINVAL;
+                               goto out;
+                       }
                        s[sp] = !s[sp];
                        cat_expr_buf(expr_list[expr_counter], "not");
                        break;
                case CEXPR_AND:
-                       BUG_ON(sp < 1);
+                       if (sp < 1) {
+                               BUG();
+                               rc = -EINVAL;
+                               goto out;
+                       }
                        sp--;
                        s[sp] &= s[sp + 1];
                        cat_expr_buf(expr_list[expr_counter], "and");
                        break;
                case CEXPR_OR:
-                       BUG_ON(sp < 1);
+                       if (sp < 1) {
+                               BUG();
+                               rc = -EINVAL;
+                               goto out;
+                       }
                        sp--;
                        s[sp] |= s[sp + 1];
                        cat_expr_buf(expr_list[expr_counter], "or");
--

2.28.0