Skip to contentSkip to conversation

misc-utils: Fix integer overflow in docfdisk.c

PATCH In review Anton Moryakov 2 participants 2 messages Dec 17, 2024 – Dec 18, 2024 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc-utils: Fix integer overflow in docfdisk.c

Report of the static analyzer:
Possible integer underflow: left operand is tainted. An integer underflow may occur due to arithmetic operation (subtraction) between variable 'block' and value '1', when 'block' is tainted { [-2147483648, 2147483647] }

Corrections explained:
Added the block <= 0 check, which prevents integer underflow when calculating block - 1 and ensures that block is always positive.

Triggers found by static analyzer Svace.

---
 misc-utils/docfdisk.c | 4 ++++
 1 file changed, 4 insertions(+)
misc-utils/docfdisk.c +4 -0
index 486ce29..e473ceb 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 (block <= 0) {
+			fprintf(stderr, "Error: Invalid block value (%d). Block must be greater than 0.\n", block);
+			return -1;
+		}
 		ip->virtualUnits = cpu_to_le32(nblocks[i]);
 		block += nblocks[i];
 		ip->lastUnit = cpu_to_le32(block-1);
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#1 lore raw
Quoted 19 lines · Anton Moryakov

Report of the static analyzer:
Possible integer underflow: left operand is tainted. An integer underflow may occur due to arithmetic operation (subtraction) between variable 'block' and value '1', when 'block' is tainted { [-2147483648, 2147483647] }

Corrections explained:
Added the block <= 0 check, which prevents integer underflow when calculating block - 1 and ensures that block is always positive.

Triggers found by static analyzer Svace.

---
  misc-utils/docfdisk.c | 4 ++++
  1 file changed, 4 insertions(+)
misc-utils/docfdisk.c +0 -0
index 486ce29..e473ceb 100644

misc-utils/docfdisk.c int main(int argc, char **argv) · line 261

Quoted 7 lines
misc-utils/docfdisk.c +4 -0
@@ -255,6 +255,10 @@ int main(int argc, char **argv)
  		ip->firstUnit = cpu_to_le32(block);
  		if (!nblocks[i])
  			nblocks[i] = totblocks - block;
+		if (block <= 0) {
+			fprintf(stderr, "Error: Invalid block value (%d). Block must be greater than 0.\n", block);
+			return -1;
+		}

In the previous code, 'block' is already initialized as:
block = mhoffs / unitsize;
block++;
So I don't think underflow could happen.

misc-utils/docfdisk.c int main(int argc, char **argv) · line 264

Quoted 4 lines
misc-utils/docfdisk.c +0 -0
  		ip->virtualUnits = cpu_to_le32(nblocks[i]);
  		block += nblocks[i];
  		ip->lastUnit = cpu_to_le32(block-1);

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