Skip to content

mtd: ubi: Check lengths and PEB numbers read from flash

Ana Bobillo u-boot 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest6 patches 6 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6mtd: ubi: validate data_size in the VID headerMAINTAINERS drivers/mtd/ubi/io.c test/dm/Makefile +1 more+146 -01 reply
2/6mtd: ubispl: check data_size before loading a LEBdrivers/mtd/ubispl/ubispl.c+12 -01 reply
3/6mtd: ubispl: check the PEB numbers read from the fastmapdrivers/mtd/ubispl/ubispl.c+14 -11 reply
4/6mtd: ubi: fastmap: check PEB numbers read from the fastmapdrivers/mtd/ubi/fastmap.c+60 -131 reply
5/6mtd: ubi: fastmap: bound the size of a volume's EBA tabledrivers/mtd/ubi/fastmap.c drivers/mtd/ubispl/ubispl.c+29 -21 reply
6/6mtd: ubi: vtbl: check the size of a static volume's last LEBdrivers/mtd/ubi/vtbl.c test/dm/ubi.c+76 -01 reply

Reviews trailers per patch

All 6 patches have a Reviewed-by or Acked-by.

PatchHeiko Schocher
1/6R-b
2/6R-b
3/6R-b
4/6R-b
5/6R-b
6/6R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 0/6] mtd: ubi: Check lengths and PEB numbers read from flash

UBI takes the sizes and PEB numbers it works with from headers on the flash and from the fastmap. This series checks six of them that are used without a bound, in the UBI core and in the SPL loader.

Patch 1: validate_vid_hdr() does not check that a VID header's data_size fits in a LEB, which Linux has done since commit 281fda27673f ("UBI: Validate data_size"), and ubi_compare_lebs() reads data_size bytes into ubi->peb_buf, which holds one PEB. Apply the Linux check. A new sandbox test (dm ubi_vid_hdr_data_size) writes a static volume of one full LEB on the sandbox NAND, checks that it attaches and reads back, then rewrites its VID header with a data_size one byte over the
LEB: 'ubi part' attaches it without the fix and refuses it with it.

Patches 2 and 3 are for the SPL loader in drivers/mtd/ubispl. ubi_load_block() reads data_size bytes of a PEB to the load address without checking data_size against the LEB size, and ubi_attach_fastmap() uses the PEB numbers of the fastmap's used list and EBA tables as indexes into the fm_used bitmap without checking them against the PEB count. Both now treat an out-of-range value as corruption, so the loader moves on to another copy of the LEB or to a full scan, as it already does when a CRC does not match.

Patches 4 and 5 are for the fastmap code of the UBI core. ubi_attach_fastmap() hands the PEB numbers of the fastmap's lists to ubi_wl_init(), which uses them as indexes into ubi->lookuptbl; patch 4 checks every PEB number taken from the fastmap with one helper and falls back to a full scan when one is outside the device. Patch 5 checks that each volume's EBA table header fits in the fastmap and that its reserved_pebs is not above the PEB count before fm_pos is advanced by it, as on 32-bit builds the advance can wrap around and pass the end-of-buffer check. The SPL loader parses the EBA tables the same way, so patch 5 changes both.

Patch 6: check_av() does not check the size of a static volume's last LEB against the volume's usable LEB size, which is smaller than the LEB size when the volume has data padding, and a fastmap's value is not checked at all. The size goes into used_bytes, which bounds what 'ubi read' and ubi_volume_read() map to LEBs and look up in the EBA table. A second sandbox test (dm ubi_static_last_leb_size) writes a static volume with data padding and one full LEB, checks that it attaches and reads back, then sets the LEB's data_size one byte over the usable LEB
size: 'ubi part' attaches it without the fix and refuses it with it.

Both tests fail on current main and pass with the series, and patch 6's test also fails with only patches 1 to 5 applied; with the series applied, 'ut dm' on sandbox shows no new failures. The fastmap paths (patches 3 to 5) have no sandbox test: sandbox_defconfig does not enable fastmap. They were checked by building sandbox with CONFIG_MTD_UBI_FASTMAP=y, where both tests also pass, and igep00x0_defconfig (32-bit ARM, UBI fastmap and SPL_UBI): both build without new warnings.

Linux and barebox share the code of patches 4 to 6: the checks of patches 4 and 6 have been proposed for Linux, and all three for barebox.

Patch 6's test builds on the test file that patch 1 adds; otherwise the patches do not depend on each other.

CI: Azure build 13722 passed, all jobs, with this series on
https://github.com/u-boot/u-boot/pull/1064, a CI-only pull request, merged with main ee9fc27f2a2:
https://dev.azure.com/u-boot/u-boot/_build/results?buildId=13722

Ana Bobillo (6):
mtd: ubi: validate data_size in the VID header
mtd: ubispl: check data_size before loading a LEB
mtd: ubispl: check the PEB numbers read from the fastmap
mtd: ubi: fastmap: check PEB numbers read from the fastmap
mtd: ubi: fastmap: bound the size of a volume's EBA table
mtd: ubi: vtbl: check the size of a static volume's last LEB

 MAINTAINERS                 |   1 +
 drivers/mtd/ubi/fastmap.c   |  87 ++++++++++++---
 drivers/mtd/ubi/io.c        |   5 +
 drivers/mtd/ubi/vtbl.c      |   6 ++
 drivers/mtd/ubispl/ubispl.c |  44 +++++++-
 test/dm/Makefile            |   3 +
 test/dm/ubi.c               | 207 ++++++++++++++++++++++++++++++++++++
 7 files changed, 337 insertions(+), 16 deletions(-)
 create mode 100644 test/dm/ubi.c

base-commit: 3973f3c4d24df75ca90d7c0a9f03e5e5c9b48f32