[RFC PATCH v2 05/22] selinux: avoid nontransitive comparison
From: Christian Göttsche
Date: Mon Dec 16 2024 - 11:42:46 EST
From: Christian Göttsche <cgzones@xxxxxxxxxxxxxx>
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@xxxxxxxxxxxxxx>
---
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 3ba5506a3fff..eb944582d7a6 100644
--- a/security/selinux/ss/policydb.c
+++ b/security/selinux/ss/policydb.c
@@ -37,6 +37,8 @@
#include "mls.h"
#include "services.h"
+#define spaceship_cmp(a, b) (((a) > (b)) - ((a) < (b)))
+
#ifdef CONFIG_SECURITY_SELINUX_DEBUG
/* clang-format off */
static const char *const symtab_name[SYM_NUM] = {
@@ -426,11 +428,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 = spaceship_cmp(ft1->ttype, ft2->ttype);
if (v)
return v;
- v = ft1->tclass - ft2->tclass;
+ v = spaceship_cmp(ft1->tclass, ft2->tclass);
if (v)
return v;
@@ -461,15 +463,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 = spaceship_cmp(key1->source_type, key2->source_type);
if (v)
return v;
- v = key1->target_type - key2->target_type;
+ v = spaceship_cmp(key1->target_type, key2->target_type);
if (v)
return v;
- v = key1->target_class - key2->target_class;
+ v = spaceship_cmp(key1->target_class, key2->target_class);
return v;
}
@@ -498,15 +500,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 = spaceship_cmp(key1->role, key2->role);
if (v)
return v;
- v = key1->type - key2->type;
+ v = spaceship_cmp(key1->type, key2->type);
if (v)
return v;
- return key1->tclass - key2->tclass;
+ return spaceship_cmp(key1->tclass, key2->tclass);
}
static const struct hashtab_key_params roletr_key_params = {
--
2.45.2