All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 01/14] selinux: avoid nontransitive comparison
@ 2025-05-11 17:30 Christian Göttsche
  2025-05-11 17:30 ` [PATCH v3 02/14] selinux: use u16 for security classes Christian Göttsche
                   ` (14 more replies)
  0 siblings, 15 replies; 37+ messages in thread
From: Christian Göttsche @ 2025-05-11 17:30 UTC (permalink / raw)
  To: selinux
  Cc: Christian Göttsche, Paul Moore, Stephen Smalley,
	Ondrej Mosnacek, linux-kernel

From: Christian Göttsche <cgzones@googlemail.com>

Avoid using nontransitive comparison to prevent unexpected sorting
results due to (well-defined) overflows.
See https://www.qualys.com/2024/01/30/qsort.txt for a related issue in
glibc's qsort(3).

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
v3: rename macro to cmp_int()
---
 security/selinux/ss/policydb.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
index 9ea971943713..dc701a7f8652 100644
--- a/security/selinux/ss/policydb.c
+++ b/security/selinux/ss/policydb.c
@@ -37,6 +37,8 @@
 #include "mls.h"
 #include "services.h"
 
+#define cmp_int(a, b) (((a) > (b)) - ((a) < (b)))
+
 #ifdef CONFIG_SECURITY_SELINUX_DEBUG
 /* clang-format off */
 static const char *const symtab_name[SYM_NUM] = {
@@ -424,11 +426,11 @@ static int filenametr_cmp(const void *k1, const void *k2)
 	const struct filename_trans_key *ft2 = k2;
 	int v;
 
-	v = ft1->ttype - ft2->ttype;
+	v = cmp_int(ft1->ttype, ft2->ttype);
 	if (v)
 		return v;
 
-	v = ft1->tclass - ft2->tclass;
+	v = cmp_int(ft1->tclass, ft2->tclass);
 	if (v)
 		return v;
 
@@ -459,15 +461,15 @@ static int rangetr_cmp(const void *k1, const void *k2)
 	const struct range_trans *key1 = k1, *key2 = k2;
 	int v;
 
-	v = key1->source_type - key2->source_type;
+	v = cmp_int(key1->source_type, key2->source_type);
 	if (v)
 		return v;
 
-	v = key1->target_type - key2->target_type;
+	v = cmp_int(key1->target_type, key2->target_type);
 	if (v)
 		return v;
 
-	v = key1->target_class - key2->target_class;
+	v = cmp_int(key1->target_class, key2->target_class);
 
 	return v;
 }
@@ -496,15 +498,15 @@ static int role_trans_cmp(const void *k1, const void *k2)
 	const struct role_trans_key *key1 = k1, *key2 = k2;
 	int v;
 
-	v = key1->role - key2->role;
+	v = cmp_int(key1->role, key2->role);
 	if (v)
 		return v;
 
-	v = key1->type - key2->type;
+	v = cmp_int(key1->type, key2->type);
 	if (v)
 		return v;
 
-	return key1->tclass - key2->tclass;
+	return cmp_int(key1->tclass, key2->tclass);
 }
 
 static const struct hashtab_key_params roletr_key_params = {
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2026-05-06 23:43 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-11 17:30 [PATCH v3 01/14] selinux: avoid nontransitive comparison Christian Göttsche
2025-05-11 17:30 ` [PATCH v3 02/14] selinux: use u16 for security classes Christian Göttsche
2025-05-13 19:44   ` Stephen Smalley
2025-05-17 21:53     ` David Laight
2025-05-11 17:30 ` [PATCH v3 03/14] selinux: more strict policy parsing Christian Göttsche
2025-05-14 14:15   ` Stephen Smalley
2025-05-14 14:25     ` Stephen Smalley
2026-05-06 23:43   ` [PATCH v3 3/14] " Paul Moore
2025-05-11 17:30 ` [PATCH v3 04/14] selinux: check length fields in policies Christian Göttsche
2025-05-14 17:26   ` Stephen Smalley
2025-05-11 17:30 ` [PATCH v3 05/14] selinux: validate constraints Christian Göttsche
2025-05-14 17:51   ` Stephen Smalley
2025-05-11 17:30 ` [PATCH v3 06/14] selinux: pre-validate conditional expressions Christian Göttsche
2025-05-14 17:55   ` Stephen Smalley
2025-05-11 17:30 ` [PATCH v3 07/14] selinux: check type attr map overflows Christian Göttsche
2025-05-14 18:15   ` Stephen Smalley
2026-05-06 23:43   ` [PATCH v3 7/14] " Paul Moore
2025-05-11 17:30 ` [PATCH v3 08/14] selinux: reorder policydb_index() Christian Göttsche
2025-05-14 18:24   ` Stephen Smalley
2026-05-06 23:43   ` [PATCH v3 8/14] " Paul Moore
2025-05-11 17:30 ` [PATCH v3 09/14] selinux: beef up isvalid checks Christian Göttsche
2025-05-14 18:59   ` Stephen Smalley
2025-05-11 17:30 ` [PATCH v3 10/14] selinux: validate symbols Christian Göttsche
2025-05-14 19:06   ` Stephen Smalley
2026-05-06 23:43   ` Paul Moore
2025-05-11 17:30 ` [PATCH v3 11/14] selinux: more strict bounds check Christian Göttsche
2025-05-14 19:11   ` Stephen Smalley
2026-05-01 17:15   ` Stephen Smalley
2025-05-11 17:30 ` [PATCH v3 12/14] selinux: check for simple types Christian Göttsche
2025-05-14 19:22   ` Stephen Smalley
2025-05-11 17:30 ` [PATCH v3 13/14] selinux: restrict policy strings Christian Göttsche
2025-05-14 19:40   ` Stephen Smalley
2025-05-14 20:02     ` James Carter
2025-05-11 17:30 ` [PATCH v3 14/14] selinux: harden MLS context string generation against overflows Christian Göttsche
2025-05-14 19:53   ` Stephen Smalley
2025-05-11 17:30 ` [PATCH v3 00/14] selinux: harden against malformed policies Christian Göttsche
2025-05-13 19:40 ` [PATCH v3 01/14] selinux: avoid nontransitive comparison Stephen Smalley

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.