Skip to contentSkip to conversation

lib: fdtdec: validate bloblist FDT before consuming libfdt size

PATCH In review Raymond Mao 3 participants 3 messages May 6 – May 8 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 lib: fdtdec: validate bloblist FDT before consuming libfdt size

From: Raymond Mao <[email protected]>

Coverity Scan defects are observed in fdtdec_apply_bloblist_dtos(), since the live FDT taken from the bloblist is passed to libfdt helpers which consume header size/offset fields:
- fdt_open_into()
- fdt_pack()

Validate the bloblist FDT with fdt_check_full() before calling fdt_open_into() and again after applying overlays before calling fdt_pack(). This makes the libfdt consumers operate on a checked FDT blob while keeping the existing flow unchanged.

Also normalize libfdt return codes from this path to errno values, including the overlay callback path through bloblist_apply_blobs().

Fixes b70cbbfbf94f ("fdtdec: apply DT overlays from bloblist")

Addresses-Coverity-ID: CID 645837: (TAINTED_SCALAR)

---
changes in v2:
- Rebased on master.
changes in v3:
- replace the local helper fdtdec_get_valid_fdt_size() with fdt_check_full().
- remove the check after fdt_pack().
- add helper to map FDT_ERR to errno, and normalize return codes of fdtdec_apply_dto_blob() and fdtdec_apply_bloblist_dtos().

 lib/fdtdec.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
lib/fdtdec.c +33 -5
index 2d66860f6ed..c67b6e8c133 100644
@@ -1729,19 +1729,38 @@ static int fdtdec_match_dto_compatible(const void *base, const void *dto)
 	return 0;
 }
 
+static inline int fdtdec_ret_to_errno(int ret)
+{
+	switch (ret) {
+	case -FDT_ERR_NOTFOUND:
+		return -ENOENT;
+	case -FDT_ERR_EXISTS:
+		return -EEXIST;
+	case -FDT_ERR_NOSPACE:
+	case -FDT_ERR_NOPHANDLES:
+		return -ENOSPC;
+	default:
+		return -EINVAL;
+	}
+}
+
 static int fdtdec_apply_dto_blob(void **blob, __maybe_unused int size)
 {
 	int ret;
 
 	ret = fdt_check_header(*blob);
 	if (ret)
-		return ret;
+		return fdtdec_ret_to_errno(ret);
 
 	ret = fdtdec_match_dto_compatible(gd->fdt_blob, *blob);
 	if (ret)
 		return ret;
 
-	return fdt_overlay_apply_verbose((void *)gd->fdt_blob, *blob);
+	ret = fdt_overlay_apply_verbose((void *)gd->fdt_blob, *blob);
+	if (ret)
+		return fdtdec_ret_to_errno(ret);
+
+	return 0;
 }
 
 static int fdtdec_apply_bloblist_dtos(void)
@@ -1760,6 +1779,10 @@ static int fdtdec_apply_bloblist_dtos(void)
 	if (live_fdt != gd->fdt_blob)
 		return -ENOENT;
 
+	ret = fdt_check_full(live_fdt, blob_size);
+	if (ret)
+		return fdtdec_ret_to_errno(ret);
+
 	/* Calculate the allowed padded size */
 	padded_size = fdt_totalsize(live_fdt) + CONFIG_SYS_FDT_PAD;
 	max_size = bloblist_get_total_size() - bloblist_get_size() + blob_size;
@@ -1772,20 +1795,25 @@ static int fdtdec_apply_bloblist_dtos(void)
 		if (ret)
 			return ret;
 
+		blob_size = padded_size;
 		ret = fdt_open_into(live_fdt, live_fdt, padded_size);
 		if (ret)
-			return ret;
+			return fdtdec_ret_to_errno(ret);
 	}
 
 	ret = bloblist_apply_blobs(BLOBLISTT_FDT_OVERLAY, fdtdec_apply_dto_blob);
 	if (ret)
 		return ret;
 
-	/* Shrink the blob to the actual FDT size */
+	ret = fdt_check_full(live_fdt, blob_size);
+	if (ret)
+		return fdtdec_ret_to_errno(ret);
+
 	ret = fdt_pack(live_fdt);
 	if (ret)
-		return ret;
+		return fdtdec_ret_to_errno(ret);
 
+	/* Shrink the blob to the actual FDT size */
 	return bloblist_resize(BLOBLISTT_CONTROL_FDT, fdt_totalsize(live_fdt));
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v3 1/1
#1 lore raw

Hi Raymond,

thank you for a quick rework!

Quoted 13 lines · Raymond Mao

From: Raymond Mao <[email protected]>

Coverity Scan defects are observed in fdtdec_apply_bloblist_dtos(), since the live FDT taken from the bloblist is passed to libfdt helpers which consume header size/offset fields:
- fdt_open_into()
- fdt_pack()

Validate the bloblist FDT with fdt_check_full() before calling fdt_open_into() and again after applying overlays before calling fdt_pack(). This makes the libfdt consumers operate on a checked FDT blob while keeping the existing flow unchanged.

Quoted 6 lines

Also normalize libfdt return codes from this path to errno values, including the overlay callback path through bloblist_apply_blobs().

Fixes b70cbbfbf94f ("fdtdec: apply DT overlays from bloblist")

Addresses-Coverity-ID: CID 645837: (TAINTED_SCALAR)

Quoted 98 lines

---
changes in v2:
- Rebased on master.
changes in v3:
- replace the local helper fdtdec_get_valid_fdt_size() with fdt_check_full().
- remove the check after fdt_pack().
- add helper to map FDT_ERR to errno, and normalize return codes of fdtdec_apply_dto_blob() and fdtdec_apply_bloblist_dtos().

 lib/fdtdec.c | 38 +++++++++++++++++++++++++++++++++-----

 1 file changed, 33 insertions(+), 5 deletions(-)
lib/fdtdec.c +0 -0
index 2d66860f6ed..c67b6e8c133 100644
@@ -1729,19 +1729,38 @@ static int fdtdec_match_dto_compatible(const void *base, const void *dto)
  	return 0;
  }

lib/fdtdec.c +15 -0
+static inline int fdtdec_ret_to_errno(int ret)
+{
+	switch (ret) {
+	case -FDT_ERR_NOTFOUND:
+		return -ENOENT;
+	case -FDT_ERR_EXISTS:
+		return -EEXIST;
+	case -FDT_ERR_NOSPACE:
+	case -FDT_ERR_NOPHANDLES:
+		return -ENOSPC;
+	default:
+		return -EINVAL;
+	}
+}
+

 static int fdtdec_apply_dto_blob(void **blob, __maybe_unused int size) {
  int ret;

  ret = fdt_check_header(*blob);
  if (ret)

lib/fdtdec.c +1 -1
-		return ret;
+		return fdtdec_ret_to_errno(ret);


  ret = fdtdec_match_dto_compatible(gd->fdt_blob, *blob); if (ret)
  return ret;

lib/fdtdec.c +5 -1
-	return fdt_overlay_apply_verbose((void *)gd->fdt_blob, *blob);
+	ret = fdt_overlay_apply_verbose((void *)gd->fdt_blob, *blob);
+	if (ret)
+		return fdtdec_ret_to_errno(ret);
+
+	return 0;

 }

 static int fdtdec_apply_bloblist_dtos(void)

lib/fdtdec.c +0 -0
@@ -1760,6 +1779,10 @@ static int fdtdec_apply_bloblist_dtos(void)
  	if (live_fdt != gd->fdt_blob)
  		return -ENOENT;

lib/fdtdec.c +4 -0
+	ret = fdt_check_full(live_fdt, blob_size);
+	if (ret)
+		return fdtdec_ret_to_errno(ret);
+

  /* Calculate the allowed padded size */
  padded_size = fdt_totalsize(live_fdt) + CONFIG_SYS_FDT_PAD; max_size = bloblist_get_total_size() - bloblist_get_size() + blob_size;

lib/fdtdec.c +0 -0
@@ -1772,20 +1795,25 @@ static int fdtdec_apply_bloblist_dtos(void)
  		if (ret)
  			return ret;

lib/fdtdec.c +1 -0
+		blob_size = padded_size;

  ret = fdt_open_into(live_fdt, live_fdt, padded_size); if (ret)

lib/fdtdec.c +1 -1
-			return ret;
+			return fdtdec_ret_to_errno(ret);

  }

  ret = bloblist_apply_blobs(BLOBLISTT_FDT_OVERLAY, fdtdec_apply_dto_blob); if (ret)
  return ret;

lib/fdtdec.c +4 -1
-	/* Shrink the blob to the actual FDT size */
+	ret = fdt_check_full(live_fdt, blob_size);
+	if (ret)
+		return fdtdec_ret_to_errno(ret);
+

  ret = fdt_pack(live_fdt);
  if (ret)

lib/fdtdec.c +1 -1
-		return ret;
+		return fdtdec_ret_to_errno(ret);

lib/fdtdec.c +1 -0
+	/* Shrink the blob to the actual FDT size */

  return bloblist_resize(BLOBLISTT_CONTROL_FDT, fdt_totalsize(live_fdt)); }

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#2 lore raw
In reply to Raymond Mao
Quoted 6 lines · Raymond Mao

Coverity Scan defects are observed in fdtdec_apply_bloblist_dtos(), since the live FDT taken from the bloblist is passed to libfdt helpers which consume header size/offset fields:
- fdt_open_into()
- fdt_pack()

Quoted 6 lines

Validate the bloblist FDT with fdt_check_full() before calling fdt_open_into() and again after applying overlays before calling fdt_pack(). This makes the libfdt consumers operate on a checked FDT blob while keeping the existing flow unchanged.

[...]

Applied to u-boot/master, thanks!

[1/1] lib: fdtdec: validate bloblist FDT before consuming libfdt size

      commit: 0a31d3128ee6928d7eac5b830ef79d0a27e3f0a7
Signature · 1 line
Tom