[Bug] nvme: kernel heap memory leak per inactive namespace in nvme_scan_device() exhausts system heap and can brick boot
Maintainer antworten meist innerhalb von 2 Tagen
@usualtreee arbeitet bereits daran.
Seit 23.10.2026.
Bewertung
Dieses Issue wurde noch nicht bewertet.
Beschreibung
Title: [Bug] nvme: kernel heap memory leak per inactive namespace in nvme_scan_device() exhausts system heap and can brick boot
RT-Thread Version
v5.2.0, v5.2.1, v5.2.2, v5.3.0 and current master (introduced by commit 945114fd5, 2024-11-24, "[DM/FEATURE] Support NVME (#9591)")
Affected area
Device drivers (components/drivers/nvme/nvme.c)
Hardware-BSP vendor
QEMU (qemu-virt64-aarch64 BSP, default .config already enables RT_USING_NVME + RT_NVME_PCI); any board with an NVMe controller
Architecture
ARM64 (also affects any arch using this driver)
Board and hardware details
QEMU virt machine (-M virt) with a stock nvme PCI device (QEMU 9.2.3, unmodified). No special hardware is needed: any NVMe controller whose Identify Controller nn (Number of Namespaces) field is larger than its count of active namespaces triggers the leak. Note QEMU itself always reports nn = 256 while exposing 1 namespace, which is legal per the NVMe spec, so the leak reproduces with a fully compliant device response.
Develop Toolchain
GCC (aarch64 toolchain), scons, QEMU 9.2.3
Describe the bug
nvme_scan_device() (components/drivers/nvme/nvme.c) iterates nsid = 1 .. nn using the device-controlled nn field from the Identify Controller response, and allocates a 4480-byte struct rt_nvme_device (rt_calloc) before issuing the per-namespace Identify command:
for (rt_uint32_t nsid = 1; nsid <= number_of_ns; ++nsid)
{
struct rt_nvme_device *ndev = rt_calloc(1, sizeof(*ndev)); /* 4480 B */
...
if ((err = nvme_identify(nvme, nsid, 0, id)))
{
goto _free_res; /* leak: ndev not freed */
}
if (!id->nsze)
{
continue; /* leak: ndev not freed */ <-- main path
}
...
Every namespace that reports nsze == 0 (inactive/allocated-but-not-active namespaces — a perfectly legal response; e.g. QEMU returns an all-zero structure for every non-attached nsid), and every failed identify, leaks 4480 bytes (4536 bytes with the memheap header) of the kernel system heap. The leaked memory is never recovered; the reference is simply dropped.
Measured impact, full-chain on the official qemu-virt64-aarch64 BSP with the default config (free via the free msh command):
| device response | kernel heap available after boot |
|---|---|
| no NVMe device | 66,941,784 B |
nn = 1 (single active ns) |
66,931,592 B (disk nvme0n1 registered, healthy) |
stock QEMU (nn = 256, 1 active ns) |
65,774,912 B — 1,156,872 B (≈1.1 MiB) leaked = 255 × 4536 B |
So with the stock, unmodified QEMU NVMe device (a legal nn = 256 response), every boot loses ~1.8 % of a 64 MiB kernel heap. Repeated controller re-registration (hot-plug / surprise removal + rescan) accumulates the leak linearly.
With a larger device-controlled nn (malformed or malicious controller firmware, e.g. nn = 0xFFFFFF), the loop keeps allocating until the kernel heap is fully exhausted, which happens deterministically before main() can start the shell — the system never reaches the msh prompt (boot bricked):
=== OOM POINT === nsid=14758 avail=600 maxused=67108264 (64 MiB heap)
#0 nvme_scan_device (nvme=0xffff000000263b78, number_of_ns=16384) at components/drivers/nvme/nvme.c:1079
#1 rt_nvme_controller_register (nvme=0xffff000000263b78) at components/drivers/nvme/nvme.c:1216
#2 pci_nvme_probe (pdev=0xffff000000263838) at components/drivers/nvme/nvme-pci.c:95
#3 pci_probe (dev=0xffff00000000038) at components/drivers/pci/pci.c:1050
(serial console output stops permanently after hello rt-thread; no msh /> prompt is ever printed)
To reproduce
- Build the official
qemu-virt64-aarch64BSP (default.config,RT_USING_NVMEis already on). - Create a raw disk image, boot with a stock QEMU nvme device (QEMU reports
nn=256for 1 active namespace — no device model modification needed):dd if=/dev/zero of=/tmp/nvme.img bs=1M count=16 qemu-system-aarch64 -M virt,gic-version=2 -cpu max -m 128M \ -kernel rtthread.bin -serial <console> \ -drive file=/tmp/nvme.img,if=none,id=nvme0 \ -device nvme,drive=nvme0 - Run
freein msh and compare with a boot without the nvme device: ~1.1 MiB of the kernel heap is gone after a single boot;list_blkshows the disk otherwise working normally. - GDB confirmation (gdb-multiarch): break at the
if (!id->nsze) continue;branch (nvme.c:1091) — it is hit once per inactive nsid (2..256 with stock QEMU), andsystem_heap->available_sizedrops by exactly 4536 bytes on each hit.
Expected behaviour
ndev must be allocated only after the namespace is known to be active (nsze != 0 and identify succeeded), or freed on the nsze == 0 / identify-error paths; nn should also not be trusted as a loop bound without an upper limit (e.g. cap by mnan or a sane maximum, and/or use the Active Namespace ID list, CNS 02h).
Actual behaviour
4480 bytes leaked per inactive namespace per controller registration; kernel heap exhaustion and an unbootable system with a large device-reported nn.
Other additional context
- CWE-401 (Missing Release of Memory after Effective Lifetime).
- CVSS 3.1: AV:P/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H = 4.9 (Medium).
- Attack prerequisites (explicit): the attacker must attach a malformed/malicious NVMe controller (AV:P) for the heap-exhaustion/brick outcome; the per-boot 1.1 MiB leak however requires no attacker at all — any compliant drive reporting
nnlarger than its active namespace count triggers it (QEMU's stock nvme device is one example, tested here). - Same device-controlled-metadata parser family as previously reported mtd-cfi (#11272) and usbhost (#11287/#11288) issues, which were accepted.
- No existing CVE/issue covers this (searched NVD, this repo's issues/PRs/branches; nvme-related items are #9591 feature, #10672 errno fix, #11650 DMA-address fix).
- Verified present in master (blob 260e1fb04) as of 2026-10-23; introduced in 945114fd5 (first release containing it: v5.2.0).
- Vorherrschende Sprache
- C
- Sterne
- 12.4k
- Forks
- 5.5k
- Ø Merge
- 3 T. 7 Std.
- Gemergte PRs (30 T.)
- 79
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus RT-Thread/rt-thread
-
Arch: ARM/AArch64 Doc
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
RT-Thread/rt-thread#12082 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 2 Tagen
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 67/100
RT-Thread/rt-thread#11951 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
-
[Bug] [libc] timer_gettime 拆秒前将总纳秒截断为 uint32_t,剩余时间查询错误Evtl. wieder frei @Yunlk hat das vor 28 Tagen übernommen, und es ist kein Pull Request offen. Offenin progress
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
RT-Thread/rt-thread#11924 · 7 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
[Bug] dfs_v2 tmpfs: ftruncate(fd, 0) frees the file buffer but reports success — use-after-free read/write and double free from a legal POSIX callEvtl. vergeben @remukisama hat das vor 33 Tagen übernommen. Offenin progress
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
RT-Thread/rt-thread#11902 · 4 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
[bsp][stm32][bluepill] README「快速上手」缺少重新生成 MDK 工程这一步,按文档操作无法编译通过Evtl. wieder frei @moment-NEW hat das vor 50 Tagen übernommen, und es ist kein Pull Request offen. Offenin progress
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
RT-Thread/rt-thread#11818 · 4 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
Alle Issues in RT-Thread/rt-thread
Ähnliche Issues
-
Builder: a structure opened without calculation data shows the Diffraction (fibre) view by defaultOffenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
FriendsofECCE/ECCE#1245 · 1 Kommentar ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
apache/nuttx-apps#4108 · 4 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 84/100
VirtualBox/virtualbox#996 ·
Maintainer antworten meist innerhalb von 6 Tagen
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 82/100
trezor/trezor-firmware#8357 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag