Skip to content

mtd: spinand: bound the ID table walk by its entry count - #339

Merged
openipc-ai merged 1 commit into
OpenIPC:novatek-nt9856xfrom
andrein:nvt-spinand-id-array-size
Nov 29, 2026
Merged

openipc-ai merged 1 commit into
OpenIPC:novatek-nt9856xfrom
andrein:nvt-spinand-id-array-size

Conversation

@andrein

@andrein andrein commented Nov 29, 2026

Copy link
Copy Markdown

This is the follow-up requested in the review of #335. drv_nand_read_id() bounded its walk over spinand_flash_ids with sizeof(), which counts bytes (1000 for the 25-entry table). With a uint8_t index the loop cannot reach that bound. For a chip missing from the table it reads the memory after the table as more entries and normally never finishes, with the driver lock held. This PR bounds the loop with ARRAY_SIZE(), and makes the same change in the identical function in the SPI020 driver (spi020_nand.c).

Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M): the flash is detected from the table, ECC strength and bitflip threshold read 1, and UBI and UBIFS mount. spi020_nand.c was compile-tested only (NA51068).

The table also has no NULL terminator, which nand_scan_with_ids() expects. That is left for a separate change.

🤖 Generated with Claude Code

drv_nand_read_id() looks the chip's ID up in spinand_flash_ids with a
loop bounded by sizeof(spinand_flash_ids), which is the size of the
table in bytes. On NT9856x the table has 25 entries of 40 bytes, so
the bound is 1000. A listed chip matches early and the loop breaks,
but for an unlisted chip the walk continues past the last entry and
reads the memory after the table as more entries. The index is a
uint8_t, so it wraps at 256 before it can reach 1000, and the loop
ends only if some bytes past the table happen to match the ID. In
practice the probe spins forever with info->lock held.

Bound the loop with ARRAY_SIZE() instead. The SPI020 driver has a copy
of the same function with the same loop over its own table; change it
the same way.

Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M) started by kexec:
the chip is found and named from the table, ECC strength and bitflip
threshold read 1, and UBI attaches and both UBIFS volumes mount. The
SPI020 driver is built only for NA51068; it was compile-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

mtd: spinand: bound ID lookups by table entry count

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Bound ID lookups by table entries to prevent out-of-bounds reads and probe hangs for unlisted
 chips.
• Apply the same fix to both SPI NAND drivers.
• Validate NT98566 detection and UBIFS mounts; compile-test the SPI020 driver.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • Both changed read-ID loops determine the same bounded end-to-end outcome: identifying the attached SPI NAND and exposing it through the MTD NAND layer.
  • The spinand variant additionally applies the matched table entry to page, device, block, pages-per-block and ECC geometry before registration; these are implementation hops within one storage integration, not independent capabilities.
Sources

drivers/mtd/spiflash/spi020_nand.c[676-676]

		for (i = 0; i < ARRAY_SIZE(spinand_flash_ids); i++) {

drivers/mtd/spiflash/spinand.c[1417-1417]

	ret = nand_scan_with_ids(mtd, 1, spinand_flash_ids);

drivers/mtd/spiflash/spinand.c[842-842]

		for (i = 0; i < ARRAY_SIZE(spinand_flash_ids); i++) {

drivers/mtd/spiflash/spinand.c[847-852]

				info->flash_info->page_size = spinand_flash_ids[i].pagesize;
				info->flash_info->device_size = spinand_flash_ids[i].chipsize;
				info->flash_info->block_size = spinand_flash_ids[i].erasesize;
				info->flash_info->page_per_block = \
					(spinand_flash_ids[i].erasesize / spinand_flash_ids[i].pagesize);
                info->nand_chip.ecc.strength = spinand_flash_ids[i].ecc.strength_ds;
Behavioral complexity 🟢 Low 🧩 Straightforward

  • Before, each driver bounded the ID-table loop by sizeof(spinand_flash_ids), which is a byte size rather than an entry count.
  • After, ARRAY_SIZE bounds iteration by table elements.
  • The new correctness condition is that every table entry is visited exactly once without reading beyond the array; no new state machine, ordering protocol or transformation was introduced.
Sources

drivers/mtd/spiflash/spi020_nand.c[676-676]

		for (i = 0; i < ARRAY_SIZE(spinand_flash_ids); i++) {

drivers/mtd/spiflash/spinand.c[842-842]

		for (i = 0; i < ARRAY_SIZE(spinand_flash_ids); i++) {
Potential damage 🔴 High 🩹 Integrity

  • If identification regresses, a supported chip can be rejected or assigned incorrect geometry/ECC settings; subsequent reads, writes or erases could fail or corrupt persistent NAND data.
  • The consequence is limited to the selected SPI NAND device and its MTD consumers.
  • Reverting stops the changed traversal behavior, but any writes or erases performed with incorrect geometry may require state repair or data restoration; no automatic repair path was established.
Sources

drivers/mtd/spiflash/spinand.c[847-852]

				info->flash_info->page_size = spinand_flash_ids[i].pagesize;
				info->flash_info->device_size = spinand_flash_ids[i].chipsize;
				info->flash_info->block_size = spinand_flash_ids[i].erasesize;
				info->flash_info->page_per_block = \
					(spinand_flash_ids[i].erasesize / spinand_flash_ids[i].pagesize);
                info->nand_chip.ecc.strength = spinand_flash_ids[i].ecc.strength_ds;

drivers/mtd/spiflash/spinand.c[1417-1417]

	ret = nand_scan_with_ids(mtd, 1, spinand_flash_ids);
Assessed commit 5644888
Diagram

graph TD
  A["Read chip ID"] --> B["Bounded lookup"] --> C{"ID match?"} -->|yes| D["Apply chip details"]
  T["Flash ID table"] --> B
  C -->|no| E["Advance or finish"]
Loading
High-Level Assessment

Use ARRAY_SIZE in both existing loops: it expresses the intended entry-count bound without changing lookup behavior. A terminator-based walk would require changing the tables and does not address this narrowly scoped fix as directly. The separate nand_scan_with_ids() terminator issue remains out of scope.

Files changed (2) +2 / -2

Bug fix (2) +2 / -2
spi020_nand.cBound SPI020 flash ID lookup by entry count +1/-1

Bound SPI020 flash ID lookup by entry count

• Replaces the byte-size loop limit with ARRAY_SIZE so an unmatched ID cannot make this lookup read beyond its table. This driver was compile-tested only.

drivers/mtd/spiflash/spi020_nand.c

spinand.cBound SPI NAND flash ID lookup by entry count +1/-1

Bound SPI NAND flash ID lookup by entry count

• Limits the lookup to actual table entries, preventing an unmatched ID from walking past the array and potentially hanging probe while holding the driver lock. Detection and UBIFS mounts were tested on NT98566 hardware.

drivers/mtd/spiflash/spinand.c

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can switch off images and animations for a plain-text comment

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@openipc-ai openipc-ai left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Thanks for the quick follow-up. I checked each claim against the branch:

  • i is uint8_t in both copies of drv_nand_read_id(), and struct nand_flash_dev is 40 bytes on ARM32. With 25 entries that makes the bound 1000, which i can never reach, so the walk wraps over the table plus about 9 KB after it, forever, with info->lock held. The SPI020 table has 15 entries, so the same applies there.
  • Both tables are real arrays, so ARRAY_SIZE()'s __must_be_array check is satisfied. No other sizeof(spinand_flash_ids) walk is left under drivers/mtd.
  • A listed chip matches before the old bound mattered, so for every board that boots today the loop does exactly the same work.

One thing to watch when you add the NULL terminator nand_scan_with_ids() expects. The sentinel adds an entry whose mfr_id/dev_id are 0, and ARRAY_SIZE() counts it. A flash that answers 00 00 (absent, or not responding) would then match the sentinel in this loop and set page_size = 0, and page_per_block divides by it. Bound this loop with ARRAY_SIZE() - 1, or stop it at name == NULL, in the same change.

@openipc-ai
openipc-ai merged commit 93c48f3 into OpenIPC:novatek-nt9856x Nov 29, 2026
openipc-ai pushed a commit that referenced this pull request Nov 29, 2026
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 #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>
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