Skip to contentSkip to conversation

libfdt: overlay: Bounds check local fixup offsets

PATCH Applied Gabe Dijkstra 2 participants 2 messages Nov 16 – Nov 17

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libfdt: overlay: Bounds check local fixup offsets

An unchecked /__local_fixups__ offset lets an overlay make fdt_overlay_apply() do a four byte read-modify-write up to 4 GiB past the property it names, outside the caller's overlay buffer. overlay_update_local_node_references() never compares the offset against the length of the property it is patching.

Fix this by rejecting an offset that does not name a whole cell inside the target property, there and in the identical pattern in overlay_update_node_conflicting_references(), and add a test case.

Assisted-by Aristotle (Harmonic) <[email protected]>

---
The check used to exist: before 24f60011fd ("libfdt: Simplify adjustment of values for local fixups") the store went through fdt_setprop_inplace_namelen_partial(), which rejected an out of range offset with -FDT_ERR_BADOVERLAY. That commit replaced the call with direct pointer arithmetic and the check went with it, so v1.7.1 through v1.8.1 and current main are all affected.

This is the same class of bug as fb7d79d7c6 ("libfdt: overlay: Fix out-of-bounds write on bad fixup offset"), and the fix mirrors it:
compare by subtracting from the length rather than adding to the offset, guard the subtraction against underflow, and return -FDT_ERR_BADOVERLAY for a malformed overlay. That commit hardened the shared helper, fdt_setprop_inplace_namelen_partial(), covering every fixup that reaches the blob through it; this one adds the equivalent check to /__local_fixups__.

I have a standalone reproducer that builds an overlay with the sequential-write API, checks it with fdt_check_full() and applies it to an empty base tree, exhibiting the out-of-bounds read-modify-write; happy to post it if that is useful.

 libfdt/fdt_overlay.c                          | 20 +++++++++++++--
 .../overlay_bad_fixup_local_fixups_offset.dts | 25 +++++++++++++++++++
 tests/run_tests.sh                            |  1 +
 3 files changed, 44 insertions(+), 2 deletions(-)
 create mode 100644 tests/overlay_bad_fixup_local_fixups_offset.dts
libfdt/fdt_overlay.c +18 -2
index 4b3e17b..b7da3dd 100644
@@ -234,8 +234,16 @@ static int overlay_update_local_node_references(void *fdto,
 
 		for (i = 0; i < fixup_len; i++) {
 			fdt32_t *refp;
+			uint32_t off = fdt32_ld_(fixup_val + i);
 
-			refp = (fdt32_t *)(tree_val + fdt32_ld_(fixup_val + i));
+			/* the fixup must reference a whole cell inside the
+			 * target property value
+			 */
+			if ((uint32_t)tree_len < sizeof(fdt32_t) ||
+			    off > (uint32_t)tree_len - sizeof(fdt32_t))
+				return -FDT_ERR_BADOVERLAY;
+
+			refp = (fdt32_t *)(tree_val + off);
 
 			/*
 			 * phandles to fixup can be unaligned, so use
@@ -636,8 +644,16 @@ static int overlay_update_node_conflicting_references(void *fdto, int tree_node,
 		for (i = 0; i < fixup_len; i++) {
 			fdt32_t *refp;
 			uint32_t valp;
+			uint32_t off = fdt32_ld_(fixup_val + i);
+
+			/* the fixup must reference a whole cell inside the
+			 * target property value
+			 */
+			if ((uint32_t)tree_len < sizeof(fdt32_t) ||
+			    off > (uint32_t)tree_len - sizeof(fdt32_t))
+				return -FDT_ERR_BADOVERLAY;
 
-			refp = (fdt32_t *)(tree_val + fdt32_ld_(fixup_val + i));
+			refp = (fdt32_t *)(tree_val + off);
 			valp = fdt32_ld(refp);
 
 			if (valp == fdto_phandle)
tests/overlay_bad_fixup_local_fixups_offset.dts new file +25 -0
new file mode 100644
index 0000000..34e3637
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/dts-v1/;
+
+/ {
+	fragment@0 {
+		target-path = "/";
+
+		__overlay__ {
+			test-phandle = <0>;
+		};
+	};
+
+	/*
+	 * The overlay is well formed apart from this: the fixup asks for
+	 * a phandle at byte offset 0x1000 of a four byte property.
+	 */
+	__local_fixups__ {
+		fragment@0 {
+			__overlay__ {
+				test-phandle = <0x1000>;
+			};
+		};
+	};
+};
tests/run_tests.sh +1 -0
index 5775e0c..ababc62 100755
@@ -274,6 +274,7 @@ BAD_FIXUP_TREES="bad_index \
 		empty \
 		empty_index \
 		index_trailing \
+		local_fixups_offset \
 		negative_index \
 		overflow_index \
 		path_empty_prop \

base-commit: 774adc25940050fb06f241b1a2a98e7421e6ed7b

Signature · 10 lines
2.34.1


-- 
*Confidentiality Notice: This email and any attachments are confidential 
and may contain privileged information intended solely for the use of the 
individual or entity to whom they are addressed. If you have received this 
message in error, please notify the sender immediately and delete the email 
and any attachments from your system. Unauthorized disclosure, copying, or 
distribution of this email or its contents is strictly prohibited.*
Applied
#1 lore raw
Quoted 29 lines · Gabe Dijkstra

An unchecked /__local_fixups__ offset lets an overlay make fdt_overlay_apply() do a four byte read-modify-write up to 4 GiB past the property it names, outside the caller's overlay buffer. overlay_update_local_node_references() never compares the offset against the length of the property it is patching.

Fix this by rejecting an offset that does not name a whole cell inside the target property, there and in the identical pattern in overlay_update_node_conflicting_references(), and add a test case.

Assisted-by Aristotle (Harmonic) <[email protected]>

---
The check used to exist: before 24f60011fd ("libfdt: Simplify adjustment of values for local fixups") the store went through fdt_setprop_inplace_namelen_partial(), which rejected an out of range offset with -FDT_ERR_BADOVERLAY. That commit replaced the call with direct pointer arithmetic and the check went with it, so v1.7.1 through v1.8.1 and current main are all affected.

This is the same class of bug as fb7d79d7c6 ("libfdt: overlay: Fix out-of-bounds write on bad fixup offset"), and the fix mirrors it:
compare by subtracting from the length rather than adding to the offset, guard the subtraction against underflow, and return -FDT_ERR_BADOVERLAY for a malformed overlay. That commit hardened the shared helper, fdt_setprop_inplace_namelen_partial(), covering every fixup that reaches the blob through it; this one adds the equivalent check to /__local_fixups__.

Quoted 4 lines

I have a standalone reproducer that builds an overlay with the sequential-write API, checks it with fdt_check_full() and applies it to an empty base tree, exhibiting the out-of-bounds read-modify-write; happy to post it if that is useful.

Merged, thanks.

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson