Skip to content

libnvme, cmds-list: avoid passing NULL to qsort() on empty inputs - #4480

Merged
igaw merged 2 commits into
linux-nvme:masterfrom
sagaragas:fix/qsort-null-ubsan
Dec 2, 2026
Merged

igaw merged 2 commits into
linux-nvme:masterfrom
sagaragas:fix/qsort-null-ubsan

Conversation

@sagaragas

Copy link
Copy Markdown
Contributor

With UBSan enabled, qsort() aborts on glibc's nonnull check in two places where it's called with a NULL base pointer and count 0:

  • libnvmf_vendor_load() in libnvme/src/nvme/vendor.c when none of the vendor.d directories contain .conf files, which aborts libnvme - vendor, nvme-cli - fabrics, and nvme-discoverd - handover in the test suite.
  • strset_iterate_sorted_() in plugins/cmds-list/cmds-list-print-stdout.c when a set is empty, which aborts nvme list and nvme list -v on a system with no NVMe devices.

I checked the remaining qsort() calls in the tree. They all either allocate up front or already guard on a non-zero count. Both fixes skip qsort() unless there's more than one element, and I've added empty-directory and empty-sysfs cases to libnvme/tests/vendor.c and tests/cli/nvme_print_test.py.

When none of the vendor.d directories contain .conf files, files stays
NULL and nr is 0. Passing NULL to qsort() is undefined behavior and
aborts under UBSan with a nonnull-attribute violation in the vendor,
fabrics, and handover tests.

Guard the qsort() call with nr > 1 and cover existing empty directories
in test-vendor.

Fixes: 527a6ce ("libnvme: apply the vendor defaults of vendor.d after a connect")
Signed-off-by: Sagar Narayan <113566055+sagaragas@users.noreply.github.com>
When a strset is empty (or the initial realloc fails), nc.names stays
NULL and nc.count is 0. Passing NULL to qsort() is undefined behavior
and aborts under UBSan in `nvme list` and `nvme list -v` when no NVMe
devices are present.

Guard the qsort() call with nc.count > 1 and exercise the list commands
against an empty sysfs tree in nvme_print_test.py.

Fixes: 61fa4f0 ("nvme-print-stdout: sort entries numerically")
Signed-off-by: Sagar Narayan <113566055+sagaragas@users.noreply.github.com>
@igaw
igaw merged commit 4b25952 into linux-nvme:master Dec 2, 2026
39 checks passed
@igaw

igaw commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants