822-v6.11-0011-nvmem-u-boot-env-error-if-NVMEM-device-is-too-small.patch 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. From 8679e8b4a1ebdb40c4429e49368d29353e07b601 Mon Sep 17 00:00:00 2001
  2. From: John Thomson <[email protected]>
  3. Date: Mon, 2 Sep 2024 15:25:08 +0100
  4. Subject: [PATCH] nvmem: u-boot-env: error if NVMEM device is too small
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Verify data size before trying to parse it to avoid reading out of
  9. buffer. This could happen in case of problems at MTD level or invalid DT
  10. bindings.
  11. Signed-off-by: John Thomson <[email protected]>
  12. Cc: stable <[email protected]>
  13. Fixes: d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
  14. [rmilecki: simplify commit description & rebase]
  15. Signed-off-by: Rafał Miłecki <[email protected]>
  16. Signed-off-by: Srinivas Kandagatla <[email protected]>
  17. Link: https://lore.kernel.org/r/[email protected]
  18. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  19. ---
  20. drivers/nvmem/u-boot-env.c | 7 +++++++
  21. 1 file changed, 7 insertions(+)
  22. --- a/drivers/nvmem/u-boot-env.c
  23. +++ b/drivers/nvmem/u-boot-env.c
  24. @@ -176,6 +176,13 @@ static int u_boot_env_parse(struct u_boo
  25. data_offset = offsetof(struct u_boot_env_image_broadcom, data);
  26. break;
  27. }
  28. +
  29. + if (dev_size < data_offset) {
  30. + dev_err(dev, "Device too small for u-boot-env\n");
  31. + err = -EIO;
  32. + goto err_kfree;
  33. + }
  34. +
  35. crc32_addr = (__le32 *)(buf + crc32_offset);
  36. crc32 = le32_to_cpu(*crc32_addr);
  37. crc32_data_len = dev_size - crc32_data_offset;