net/netdev: correct the ioctl command validation logic #17919
Merged
+13
−14
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.
Summary
This PR fixes the validation order in netdev ioctl handlers to ensure correct error codes are returned according to POSIX standards.
Problem
Currently, the ioctl handlers for Bluetooth, IEEE 802.15.4, and packet radio devices check the argument pointer (
arg != 0ul) before validating the command type. This causes incorrect error codes to be returned:-EINVALinstead of-ENOTTYAccording to POSIX standards:
-ENOTTY: The specified request does not apply to the kind of object that the file descriptor references-EINVAL: The request or arg parameter is not validSolution
This patch reorders the validation logic in three ioctl handlers:
netdev_bluetooth_ioctl()netdev_iee802154_ioctl()netdev_pktradio_ioctl()Changes made:
_*IOCVALID(cmd)) firstarg != 0ul)-ENOTTYfor invalid commands (not supported by this device type)-EINVALfor invalid arguments (NULL pointer when data is required)Affected Functions
Impact
Correctness: Positive - Returns correct POSIX-compliant error codes.
Compatibility: Positive - Applications relying on proper error codes will work correctly.
Stability: Neutral - No change in stability, only error code correctness.
Performance: Neutral - Same number of checks, just reordered.
Code Quality: Positive - Improves POSIX compliance and code clarity.
Testing
Test Configuration
Host OS: Ubuntu 22.04 x86_64
Target: sim:matter (x86_64 simulator)
CONFIG_WIRELESS_BLUETOOTH=y, CONFIG_WIRELESS_IEEE802154=y, CONFIG_WIRELESS_PKTRADIO=y
Test Cases
Test 1: Invalid Bluetooth command with valid argument
Expected: Returns -1 with errno = ENOTTY
Result: PASS - Correct error code returned
Test 2: Valid Bluetooth command with NULL argument
Expected: Returns -1 with errno = EINVAL
Result: PASS - Correct error code returned
Test 3: Valid command with valid argument
Expected: Command processed normally (returns 0 or valid result)
Result: PASS - Normal operation unchanged
Test 4: IEEE 802.15.4 and Packet Radio validation
Similar tests performed for IEEE 802.15.4 and packet radio ioctl handlers with identical results.
Test Logs
Verification Checklist
Code compiles without warnings
Invalid commands return -ENOTTY (not supported)
NULL arguments return -EINVAL (invalid argument)
Valid commands with valid arguments work normally
No regression in existing functionality
POSIX compliance improved
Same behavior across all three affected handlers (Bluetooth, IEEE 802.15.4, Packet Radio)
Comments updated to reflect correct logic