Repository navigation
libnvme, cmds-list: avoid passing NULL to qsort() on empty inputs - #4480
Merged
Merged
Conversation
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>
Collaborator
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With UBSan enabled,
qsort()aborts on glibc'snonnullcheck in two places where it's called with aNULLbase pointer and count 0:libnvmf_vendor_load()inlibnvme/src/nvme/vendor.cwhen none of thevendor.ddirectories contain.conffiles, which abortslibnvme - vendor,nvme-cli - fabrics, andnvme-discoverd - handoverin the test suite.strset_iterate_sorted_()inplugins/cmds-list/cmds-list-print-stdout.cwhen a set is empty, which abortsnvme listandnvme list -von 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 skipqsort()unless there's more than one element, and I've added empty-directory and empty-sysfs cases tolibnvme/tests/vendor.candtests/cli/nvme_print_test.py.