[Bug] nvme: kernel heap memory leak per inactive namespace in nvme_scan_device() exhausts system heap and can brick boot

未關閉
#11,919 5 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

維護者通常 2 天內回覆

@usualtreee 已經在處理了。

開始於 2026年10月23日。

評估

這個 Issue 還沒有評估資料。

描述

in progress

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

  1. Build the official qemu-virt64-aarch64 BSP (default .config, RT_USING_NVME is already on).
  2. Create a raw disk image, boot with a stock QEMU nvme device (QEMU reports nn=256 for 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
    
  3. Run free in msh and compare with a boot without the nvme device: ~1.1 MiB of the kernel heap is gone after a single boot; list_blk shows the disk otherwise working normally.
  4. 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), and system_heap->available_size drops 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 nn larger 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).
主要語言
C
星號
12.4k
分支
5.5k
平均合併
3 天 7 小時
30 天內合併 PR
79

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

RT-Thread/rt-thread 的其他 Issue

查看 RT-Thread/rt-thread 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。