Repository navigation
mtd: spinand: end the ID tables with a NULL entry - #343
Conversation
nand_scan_with_ids() walks the table it is given until it reaches an entry whose name is NULL. spinand_flash_ids has no such entry, in this driver or in the SPI020 driver, so for a chip that matches no entry nand_detect() runs past the end of the table and reads the memory after it as more entries. Add the terminator to both tables. drv_nand_read_id() bounds its own walk with ARRAY_SIZE(), which now counts the terminator. Its mfr_id and dev_id are 0, so a flash that answers 00 00, because it is absent or not responding, would match it, and spinand.c would then set page_size to 0 and divide by it. The review of OpenIPC#339 pointed this out and suggested stopping the loop at the entry with a NULL name, which both copies now do. An ID that matched no entry still made drv_nand_read_id() return success. spinand.c then went on with page and block sizes it never set, as flash_info comes from kmalloc(). Print the ID and return -ENODEV instead, so the probe ends in drv_nand_identify() with the existing "failed to identify flash" message. That error path freed mtd, which points into info, and info comes from devm_kzalloc(). The kfree() corrupted the slab, and later in the boot the kernel oopsed in clk_debug_init(). Drop it in both drivers; devres frees info when the probe fails. Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M) started by kexec: the flash is found and named, there are 10 partitions, ECC strength and bitflip threshold read 1, UBI attaches and both UBIFS volumes mount. Tested in the nt98566 machine of OpenIPC's qemu-bench with the same camera's flash image: the FORESEE ID cd 70 probes and the root filesystem mounts; for the IDs 12 34, 00 00 and ff ff the probe fails with "flash ID 0x.. 0x.. is not in the ID table" and the boot goes on until it cannot mount root, as expected. Without this change those three IDs end in the clk_debug_init() oops. The SPI020 driver is built only for NA51068; it was compile-tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PR Summary by QodoPrevent out-of-bounds SPI NAND ID scans and unsafe probe cleanup
AI Description
Merge Risk
|
Code Review by Qodo
1. Unknown flash probes leak memory
|
openipc-ai
left a comment
There was a problem hiding this comment.
Approving. This is the follow-up from #339, done the way I asked: the terminator, with drv_nand_read_id() stopping at name == NULL rather than at ARRAY_SIZE().
What I checked:
- The lookup. It ends either on a match, where
breakleavesion the entry, or on the sentinel, where.nameis NULL and the else arm fails.00 00can no longer reach the sentinel's zero geometry, andff ff,12 34or anything else unlisted fails the same way. Both tables now end in{ NULL }, andnand_scan_with_ids()is the only other user of either table, sonand_detect()stops at the end too. - The error path.
drv_nand_identify()maps any non-E_OKto-EIOand the probe reports-ENODEV, so the new message is the only visible change. After identificationchip_idis set, soNAND_CMD_READIDfrom the NAND core takes thememcpypath and never re-enters the lookup. kfree(mtd).mtdisnand_to_mtd(&info->nand_chip), inside thedevm_kzalloc()'dinfo. Freeing it was freeing an interior pointer of a devres allocation, which fits the laterclk_debug_init()oops you saw. Dropping it is right. The earlier gotos tofail_free_mtdnow just return, and devres freesinfo.- What changes for a camera. A listed chip goes through exactly the same steps as before. An unlisted chip used to hang the boot (before #339) or run on with unset geometry and a corrupted slab (since #339). It now fails the probe with a readable line on the console, and the boot continues.
One leftover of the same defect: spinand_remove() still ends in kfree(mtd) in both drivers (spinand.c and spi020_nand.c). It's reached only on unbind, since the driver is built in, so it doesn't block. It's the same two-line fix if you push again, or a follow-up otherwise.
Both SPI NAND probes allocate info->flash_info with kmalloc() and no path frees it: not the probe's error labels and not remove. Since #343 an ID that is not in the table fails the probe, so every such probe leaks the allocation, and so did every earlier failure (a READID timeout, a failed nand_scan_with_ids()). Allocate it with devm_kzalloc() on the platform device, like info itself, so devres releases it on a failed probe and on unbind. The allocation is now zeroed. Every field the probe and the I/O path read is written first except num_blocks, which nothing sets: drv_nand_identify() copies it into total_block_cnt, the number of erase_count[4096] entries the static_info proc file prints. That count was whatever kmalloc() returned, and could walk the file past the end of the array. It is now 0, so the file lists no blocks.
The probe stores info with platform_set_drvdata(), but remove, suspend and resume read the driver data back as a struct mtd_info and take info from mtd->priv. The pointer is the same address, since nand_chip is the first member of info and mtd the first member of nand_chip. But nothing sets mtd->priv (the probe sets chip->priv), so info comes back NULL. Unbinding the device, which sysfs allows even for this built-in driver, dereferences NULL in free_irq(info->irq, info). Past that, remove would kfree(mtd), an interior pointer of the devm_kzalloc()'d info: the same slab corruption #343 removed from the probe's error path. Read the driver data as info, get mtd with nand_to_mtd(), and drop the kfree(mtd) from remove. devres frees info. suspend and resume get the same lookup; they are built only with CONFIG_PM, which the NT9856x configs leave off. Not changed: remove leaves /proc/nvt_info/nvt_flash in place, so a second bind warns that the proc entry is already registered, logs "create nand static proc failed", and keeps the old entries.
nand_scan_with_ids()walks the ID table until an entry with a NULL name. Neitherspinand_flash_ids(NT9856x) nor the SPI020 copy had one, so for a chip that matches no entrynand_detect()reads past the end of the table. This adds the terminator to both tables.Following the review advice on #339,
drv_nand_read_id()in both drivers now stops at the entry with a NULL name. Bounding it withARRAY_SIZE()would count the terminator, and a flash answering00 00(absent or not responding) would match it and divide by a zero page size. An ID that matches no entry now fails the probe with-ENODEVand the message "flash ID 0x.. 0x.. is not in the ID table". Before, the driver carried on with geometry it never set.The probe's error path no longer calls
kfree()onmtd. That pointer is inside thedevm_kzalloc()'dinfo, and freeing it corrupted the slab: with an unlisted flash the kernel later oopsed inclk_debug_init().Tested:
cd 70probes and mounts root; IDs12 34,00 00andff fffail the probe cleanly and the boot continues until it cannot mount root. Without this change those IDs end in theclk_debug_init()oops.Depends only on #339, which is merged.
🤖 Generated with Claude Code