Skip to contentSkip to conversation

misc-utils: docfdisk.c: validate partition size to prevent arithmetic overflow

PATCH Applied Anton Moryakov 2 participants 2 messages Oct 28, 2025 – Apr 13 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc-utils: docfdisk.c: validate partition size to prevent arithmetic overflow

report of the static analyzer:
Possible integer overflow: right operand is tainted.
An integer overflow may occur due to arithmetic operation (addition) between variable 'block' and value { [1, 4294967295] } of 'nblocks[i]', when 'block' is equal to '1'

correct explained:
Added bounds check before incrementing block counter to ensure that adding nblocks[i] does not exceed totblocks. This prevents potential integer overflow when user-specified partition sizes are too large, which could lead to incorrect partition table layout and device corruption. The validation ensures safe arithmetic by checking block + nblocks[i] <= totblocks using unsigned comparison.

---
 misc-utils/docfdisk.c | 4 ++++
 1 file changed, 4 insertions(+)
misc-utils/docfdisk.c +4 -0
index 486ce29..df070b2 100644
@@ -255,6 +255,10 @@ int main(int argc, char **argv)
 		ip->firstUnit = cpu_to_le32(block);
 		if (!nblocks[i])
 			nblocks[i] = totblocks - block;
+		if (nblocks[i] > totblocks || block > totblocks - nblocks[i]) {
+			printf("Requested partition size exceeds available device space.\n");
+        	return 1;
+    	}
 		ip->virtualUnits = cpu_to_le32(nblocks[i]);
 		block += nblocks[i];
 		ip->lastUnit = cpu_to_le32(block-1);
Signature · 6 lines
2.39.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/