Skip to content

Commit

Permalink
libsepol: ensure transitivity in compare functions
Browse files Browse the repository at this point in the history
Ensure comparison functions used by qsort(3) fulfill transitivity, since
otherwise the resulting array might not be sorted correctly or worse[1]
in case of integer overflows.

[1]: https://www.qualys.com/2024/01/30/qsort.txt

Signed-off-by: Christian Göttsche <[email protected]>
Acked-by: James Carter <[email protected]>
  • Loading branch information
cgzones authored and jwcart2 committed Mar 4, 2024
1 parent 3dc1116 commit b52e27a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libsepol/src/kernel_to_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ static int ibendport_data_cmp(const void *a, const void *b)
if (rc)
return rc;

return (*aa)->u.ibendport.port - (*bb)->u.ibendport.port;
return spaceship_cmp((*aa)->u.ibendport.port, (*bb)->u.ibendport.port);
}

static int pirq_data_cmp(const void *a, const void *b)
Expand Down
2 changes: 1 addition & 1 deletion libsepol/src/module_to_cil.c
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ static int class_perm_cmp(const void *a, const void *b)
const struct class_perm_datum *aa = a;
const struct class_perm_datum *bb = b;

return aa->val - bb->val;
return spaceship_cmp(aa->val, bb->val);
}

static int common_to_cil(char *key, void *data, void *UNUSED(arg))
Expand Down

0 comments on commit b52e27a

Please sign in to comment.