[Bug] nvme: kernel heap memory leak per inactive namespace in nvme_scan_device() exhausts system heap and can brick boot
Los mantenedores suelen responder en 2 días
@usualtreee ya está trabajando en esto.
Desde el 23/10/2026.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
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).
- Lenguaje dominante
- C
- Estrellas
- 12.4k
- Forks
- 5.5k
- Merge medio
- 3 d 7 h
- PR fusionados (30 d)
- 79
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de RT-Thread/rt-thread
-
Arch: ARM/AArch64 Doc
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
RT-Thread/rt-thread#12082 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
RT-Thread/rt-thread#11951 · 2 comentarios ·
Los mantenedores suelen responder en 2 días
-
[Bug] [libc] timer_gettime 拆秒前将总纳秒截断为 uint32_t,剩余时间查询错误Quizá libre de nuevo @Yunlk la tomó hace 28 días y no hay ningún pull request abierto. Abiertoin progress
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
RT-Thread/rt-thread#11924 · 7 comentarios · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
[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 callPosiblemente ocupada @remukisama la tomó hace 33 días. Abiertoin progress
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
RT-Thread/rt-thread#11902 · 4 comentarios · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
[bsp][stm32][bluepill] README「快速上手」缺少重新生成 MDK 工程这一步,按文档操作无法编译通过Quizá libre de nuevo @moment-NEW la tomó hace 50 días y no hay ningún pull request abierto. Abiertoin progress
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
RT-Thread/rt-thread#11818 · 4 comentarios · 1 asignado ·
Los mantenedores suelen responder en 2 días
Todos los issues de RT-Thread/rt-thread
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
apache/nuttx-apps#4108 · 4 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
trezor/trezor-firmware#8357 ·
Los mantenedores suelen responder en 2 días
-
ivorysql_ora: DBMS_SESSION lacks SLEEP (documented, not covered by #2763)Posiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día
-
good first issue kernel
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
JackFurton/who-would-build-a-kernel-in-java#316 ·
Los mantenedores suelen responder en 1 día
-
Bandwidth selector rotary direction is inconsistent with the regular menu on portable modelsAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
PE5PVB/TEF6686_ESP32#970 ·