Skip to contentSkip to conversation

libselinux/utils: drop reachable assert in sefcontext_compile

PATCH In review Christian Göttsche 3 participants 4 messages Dec 3, 2024 – Dec 4, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libselinux/utils: drop reachable assert in sefcontext_compile

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

The two asserts following qsort(3) where useful during development to ensure the comparison function and the corresponding pointer handling were correct. They however do not take into account an empty file context definition file containing no definitions and thus stab->nel being NULL. Drop the two asserts.

Also return early to not depend on whether calloc(3) called with a size of zero returns NULL or a special value.

Fixes 92306daf ("libselinux: rework selabel_file(5) database")

---
v2: fix condition from not zero to equal to zero

---
 libselinux/utils/sefcontext_compile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/utils/sefcontext_compile.c +3 -2
index 23d31274..e5da51ea 100644
@@ -188,6 +188,9 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
 	if (len != 1)
 		return -1;
 
+	if (stab->nel == 0)
+		return 0;
+
 	/* sort entries by id */
 	sids = calloc(stab->nel, sizeof(*sids));
 	if (!sids)
@@ -203,8 +206,6 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
 	}
 	assert(index == stab->nel);
 	qsort(sids, stab->nel, sizeof(struct security_id), security_id_compare);
-	assert(sids[0].id == 1);
-	assert(sids[stab->nel - 1].id == stab->nel);
 
 	/* write raw contexts sorted by id */
 	for (uint32_t i = 0; i < stab->nel; i++) {
Signature · 1 line
2.45.2
Tested-by on v2 1/1
#1 lore raw
Quoted 8 lines · Christian Göttsche

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

The two asserts following qsort(3) where useful during development to ensure the comparison function and the corresponding pointer handling were correct. They however do not take into account an empty file context definition file containing no definitions and thus stab->nel being NULL. Drop the two asserts.

Quoted 7 lines

Also return early to not depend on whether calloc(3) called with a size of zero returns NULL or a special value.

Fixes 92306daf ("libselinux: rework selabel_file(5) database")

Works for me. Thanks!

Quoted 32 lines

---
v2: fix condition from not zero to equal to zero

---
 libselinux/utils/sefcontext_compile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/utils/sefcontext_compile.c +3 -2
index 23d31274..e5da51ea 100644
@@ -188,6 +188,9 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
 	if (len != 1)
 		return -1;
 
+	if (stab->nel == 0)
+		return 0;
+
 	/* sort entries by id */
 	sids = calloc(stab->nel, sizeof(*sids));
 	if (!sids)
@@ -203,8 +206,6 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
 	}
 	assert(index == stab->nel);
 	qsort(sids, stab->nel, sizeof(struct security_id), security_id_compare);
-	assert(sids[0].id == 1);
-	assert(sids[stab->nel - 1].id == stab->nel);
 
 	/* write raw contexts sorted by id */
 	for (uint32_t i = 0; i < stab->nel; i++) {
Signature · 1 line
2.45.2
Acked-by
#2 lore raw
Quoted 14 lines · Petr Lautrbach
Quoted 11 lines · Christian Göttsche

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

The two asserts following qsort(3) where useful during development to ensure the comparison function and the corresponding pointer handling were correct. They however do not take into account an empty file context definition file containing no definitions and thus stab->nel being NULL. Drop the two asserts.

Also return early to not depend on whether calloc(3) called with a size of zero returns NULL or a special value.

Quoted 9 lines
Quoted 4 lines
Fixes 92306daf ("libselinux: rework selabel_file(5) database")

Works for me. Thanks!

Quoted 34 lines
Quoted 32 lines

---
v2: fix condition from not zero to equal to zero

---
 libselinux/utils/sefcontext_compile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/utils/sefcontext_compile.c +3 -3
index 23d31274..e5da51ea 100644
@@ -188,6 +188,9 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
      if (len != 1)
              return -1;
 
+     if (stab->nel == 0)
+             return 0;
+
      /* sort entries by id */
      sids = calloc(stab->nel, sizeof(*sids));
      if (!sids)
@@ -203,8 +206,6 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
      }
      assert(index == stab->nel);
      qsort(sids, stab->nel, sizeof(struct security_id), security_id_compare);
-     assert(sids[0].id == 1);
-     assert(sids[stab->nel - 1].id == stab->nel);
 
      /* write raw contexts sorted by id */
      for (uint32_t i = 0; i < stab->nel; i++) {
--

2.45.2

#3 lore raw
Quoted 21 lines · James Carter
Quoted 19 lines · Petr Lautrbach
Quoted 15 lines · Christian Göttsche

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

The two asserts following qsort(3) where useful during development to ensure the comparison function and the corresponding pointer handling were correct. They however do not take into account an empty file context definition file containing no definitions and thus stab->nel being NULL. Drop the two asserts.

Also return early to not depend on whether calloc(3) called with a size of zero returns NULL or a special value.

Fixes 92306daf ("libselinux: rework selabel_file(5) database")
Quoted 7 lines
Quoted 4 lines

Works for me. Thanks!

Merged.
Thanks,
Jim

Quoted 34 lines
Quoted 34 lines
Quoted 32 lines

---
v2: fix condition from not zero to equal to zero

---
 libselinux/utils/sefcontext_compile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/utils/sefcontext_compile.c +3 -3
index 23d31274..e5da51ea 100644
@@ -188,6 +188,9 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
      if (len != 1)
              return -1;
 
+     if (stab->nel == 0)
+             return 0;
+
      /* sort entries by id */
      sids = calloc(stab->nel, sizeof(*sids));
      if (!sids)
@@ -203,8 +206,6 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
      }
      assert(index == stab->nel);
      qsort(sids, stab->nel, sizeof(struct security_id), security_id_compare);
-     assert(sids[0].id == 1);
-     assert(sids[stab->nel - 1].id == stab->nel);
 
      /* write raw contexts sorted by id */
      for (uint32_t i = 0; i < stab->nel; i++) {
--

2.45.2