Skip to content

Commit

Permalink
libsemanage: skip sort of empty arrays
Browse files Browse the repository at this point in the history
UBSAN triggers if the first argument of qsort(3) is NULL, even if the
associated passed size is 0 and thus no actual dereference will happen.

Sort only arrays with at least two elements.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
  • Loading branch information
cgzones authored and jwcart2 committed Nov 21, 2024
1 parent bd2fd2c commit 6d55e80
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions libsemanage/src/database_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ static int dbase_join_cache(semanage_handle_t * handle, dbase_join_t * dbase)
goto err;

/* Sort for quicker merge later */
if (rcount1 > 0) {
if (rcount1 > 1) {
qsort(records1, rcount1, sizeof(record1_t *), rtable1->compare2_qsort);
}
if (rcount2 > 0) {
if (rcount2 > 1) {
qsort(records2, rcount2, sizeof(record2_t *), rtable2->compare2_qsort);
}

Expand Down
3 changes: 2 additions & 1 deletion libsemanage/src/ibendports_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ int semanage_ibendport_validate_local(semanage_handle_t *handle)
if (semanage_ibendport_list_local(handle, &ibendports, &nibendports) < 0)
goto err;

qsort(ibendports, nibendports, sizeof(semanage_ibendport_t *), semanage_ibendport_compare2_qsort);
if (nibendports > 1)
qsort(ibendports, nibendports, sizeof(semanage_ibendport_t *), semanage_ibendport_compare2_qsort);

/* Test each ibendport */
while (i < nibendports) {
Expand Down
3 changes: 2 additions & 1 deletion libsemanage/src/ibpkeys_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ int semanage_ibpkey_validate_local(semanage_handle_t *handle)
if (semanage_ibpkey_list_local(handle, &ibpkeys, &nibpkeys) < 0)
goto err;

qsort(ibpkeys, nibpkeys, sizeof(semanage_ibpkey_t *), semanage_ibpkey_compare2_qsort);
if (nibpkeys > 1)
qsort(ibpkeys, nibpkeys, sizeof(semanage_ibpkey_t *), semanage_ibpkey_compare2_qsort);

/* Test each ibpkey for overlap */
while (i < nibpkeys) {
Expand Down
2 changes: 1 addition & 1 deletion libsemanage/src/policy_components.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ int semanage_base_merge_components(semanage_handle_t * handle)
goto err;

/* Sort records on MODE_SORT */
if (mode & MODE_SORT) {
if ((mode & MODE_SORT) && nrecords > 1) {
qsort(records, nrecords, sizeof(record_t *), rtable->compare2_qsort);
}

Expand Down
4 changes: 3 additions & 1 deletion libsemanage/src/ports_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ int semanage_port_validate_local(semanage_handle_t * handle)
/* List and sort the ports */
if (semanage_port_list_local(handle, &ports, &nports) < 0)
goto err;
qsort(ports, nports, sizeof(semanage_port_t *), semanage_port_compare2_qsort);

if (nports > 1)
qsort(ports, nports, sizeof(semanage_port_t *), semanage_port_compare2_qsort);

/* Test each port for overlap */
while (i < nports) {
Expand Down
2 changes: 1 addition & 1 deletion libsemanage/src/utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int semanage_list_sort(semanage_list_t ** l)
size_t count = 0;
size_t i = 0;

if (!l)
if (!l || !(*l)->next)
return 0;

for (temp = *l; temp; temp = temp->next)
Expand Down

0 comments on commit 6d55e80

Please sign in to comment.