Skip to content

libnvme: zero-init ns in __nvme_transport_handle_open_direct()#3348

Closed
Mateusz-Nowicki-Embedded wants to merge 1 commit into
linux-nvme:masterfrom
Mateusz-Nowicki-Embedded:fix-ioctl-probe-init-ns
Closed

libnvme: zero-init ns in __nvme_transport_handle_open_direct()#3348
Mateusz-Nowicki-Embedded wants to merge 1 commit into
linux-nvme:masterfrom
Mateusz-Nowicki-Embedded:fix-ioctl-probe-init-ns

Conversation

@Mateusz-Nowicki-Embedded
Copy link
Copy Markdown

@Mateusz-Nowicki-Embedded Mateusz-Nowicki-Embedded commented May 12, 2026

Minimal fix for an uninitialized-variable info leak in the ioctl_probing path.

When opening /dev/nvmeX (controller char dev), the local ns variable is left uninitialized after the "nvme%dn%d" sscanf
returns 1, then copied into dummy.nsid and submitted as a real admin SQE. A few bytes of caller-stack memory end up in
device-visible memory (typically the high half of a user-space pointer on x86_64, 0x00007fXX...).

This is the smallest possible fix — just initialize ns = 0 so the leaked bytes become zeros. The dummy probe is still sent and still rejected by the controller as before.

Alternative: see #3349 for a more invasive fix that also removes the dummy submission entirely
(probe via NULL argp +ENOTTY/EFAULT distinction).

When __nvme_transport_handle_open_direct() is called for a controller
character device (e.g. /dev/nvmeX), sscanf() of the device name against
"nvme%dn%d" matches only the controller id, returns 1, and leaves `ns`
uninitialised.  The ioctl_probing path then propagates that stack value
via dummy.nsid into the NVME_IOCTL_ADMIN64_CMD submission, which the
kernel forwards verbatim as the SQE's NSID into the admin Submission
Queue ring.

The NSID field of the SQE is therefore filled with whatever stack
contents happened to live in `ns` at that point.  On x86_64 these are
consistently in the 0x00007fXX range, which corresponds to the high
half of user-space pointers and leaks a few bytes of caller stack into
device-visible memory.

Initialise ns to 0 so the probe NSID is deterministic.  The probe is
still submitted to the controller and still gets rejected with Invalid
Queue Identifier (the probe carries opcode 0 / CDW10 = 0, i.e. a
Delete I/O SQ targeting SQID 0); only the previously leaked bytes are
now zeros.

Signed-off-by: Mateusz Nowicki <mateusz.nowicki@posteo.net>
@igaw
Copy link
Copy Markdown
Collaborator

igaw commented May 12, 2026

unfortunately, it's not that simple as it turns out: #3312

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented May 13, 2026

Fixed with #3357. Thanks for your report! Highly appreciated!

@igaw igaw closed this May 13, 2026
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