linux-nvme / nvme-cli

NVMe management command line interface.
https://nvmexpress.org
GNU General Public License v2.0
1.44k stars 648 forks source link

nvme: telemetry: report the correct error if the ioctl() fails. #2352

Closed maurizio-lombardi closed 2 months ago

maurizio-lombardi commented 3 months ago

It's wrong to assume that if the ioctl() returns a non-zero number then the errno variable is set. The ioctl() might return an NVMe Status error to inform the caller that the requested log page is not supported, in that case errno is left untouched.

The original code didn't handle this case and returned "-errno" even when the latter was zero. The caller interpreted this as a successful operation and this might lead to improperly dereferencing the log page pointer.

$ nvme telemetry-log /dev/nvme0 --output-file=telemetry_log.bin ERROR: get_telemetry_log: : write failed with error : Bad address

Fix this bug by returning the NVMe status if errno is zero:

$ nvme telemetry-log /dev/nvme0 --output-file=telemetry_log.bin NVMe status: Invalid Log Page: The log page indicated is invalid(0x109) Failed to acquire telemetry log 265!

igaw commented 2 months ago

I've went through the code base to convince myself how bad the station with the errno is... it's bad. A complete mess. Anyway, this seems reasonable to do here as we do this for most other commands already.

igaw commented 2 months ago

Thanks!