Skip to contentSkip to conversation

udf: balloc: prevent integer overflow in udf_bitmap_free_blocks()

PATCH Superseded Roman Smirnov 3 participants 4 messages Jun 10, 2024 – Jun 11, 2024 UDF filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udf: balloc: prevent integer overflow in udf_bitmap_free_blocks()

An overflow may occur if the function is called with the last block and an offset greater than zero. It is necessary to add a check to avoid this.

Found by Linux Verification Center (linuxtesting.org) with Svace.

---
 fs/udf/balloc.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/udf/balloc.c +7 -0
index ab3ffc355949..cd83bbc7d890 100644
@@ -151,6 +151,13 @@ static void udf_bitmap_free_blocks(struct super_block *sb,
 	block = bloc->logicalBlockNum + offset +
 		(sizeof(struct spaceBitmapDesc) << 3);
 
+	if (block < offset + (sizeof(struct spaceBitmapDesc) << 3)) {
+		udf_debug("integer overflow: %u + %u + %zu",
+			  bloc->logicalBlockNum, offset,
+			  sizeof(struct spaceBitmapDesc) << 3);
+		goto error_return;
+	}
+
 	do {
 		overflow = 0;
 		block_group = block >> (sb->s_blocksize_bits + 3);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 16 lines · Roman Smirnov

An overflow may occur if the function is called with the last block and an offset greater than zero. It is necessary to add a check to avoid this.

Found by Linux Verification Center (linuxtesting.org) with Svace.

---
 fs/udf/balloc.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/udf/balloc.c +0 -0
index ab3ffc355949..cd83bbc7d890 100644

fs/udf/balloc.c static void udf_bitmap_free_blocks(struct super_block *sb, · line 152

Quoted 2 lines
fs/udf/balloc.c +0 -0
@@ -151,6 +151,13 @@ static void udf_bitmap_free_blocks(struct super_block *sb,
 	block = bloc->logicalBlockNum + offset +
 		(sizeof(struct spaceBitmapDesc) << 3);

Hm, I can't say I understand this code well... Is the block variable here
counted in blocks or bytes?

Quoted 1 line

As I've already said, we hardly need an empty line here -- just
don't add an empty line after your *if*...

fs/udf/balloc.c static void udf_bitmap_free_blocks(struct super_block *sb, · line 154

Quoted 1 line
fs/udf/balloc.c +1 -0
+	if (block < offset + (sizeof(struct spaceBitmapDesc) << 3)) {

Thinking about this again, this addition may overflow 32 bits as well,
so it's better to compare with block with bloc->logicalBlockNum...

[...]

MBR, Sergey

#2 lore raw
In reply to Roman Smirnov
Quoted 16 lines · Roman Smirnov

An overflow may occur if the function is called with the last block and an offset greater than zero. It is necessary to add a check to avoid this.

Found by Linux Verification Center (linuxtesting.org) with Svace.

---
 fs/udf/balloc.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/udf/balloc.c +0 -0
index ab3ffc355949..cd83bbc7d890 100644

fs/udf/balloc.c static void udf_bitmap_free_blocks(struct super_block *sb, · line 152

Quoted 2 lines
fs/udf/balloc.c +0 -0
@@ -151,6 +151,13 @@ static void udf_bitmap_free_blocks(struct super_block *sb,
 	block = bloc->logicalBlockNum + offset +
 		(sizeof(struct spaceBitmapDesc) << 3);

Hm, I can't say I understand this code well... Is the block variable here
counted in blocks or bytes?

Quoted 1 line

As I've already said, we hardly need an empty line here -- just
don't add an empty line after your *if*...

fs/udf/balloc.c static void udf_bitmap_free_blocks(struct super_block *sb, · line 154

Quoted 1 line
fs/udf/balloc.c +1 -0
+	if (block < offset + (sizeof(struct spaceBitmapDesc) << 3)) {

Thinking about this again, this addition may overflow 32 bits as well,
so it's better to compare block with bloc->logicalBlockNum...

[...]

MBR, Sergey

#3 lore raw
In reply to Roman Smirnov
Quoted 7 lines · Roman Smirnov

An overflow may occur if the function is called with the last block and an offset greater than zero. It is necessary to add a check to avoid this.

Found by Linux Verification Center (linuxtesting.org) with Svace.

Thanks for the patch! Actually there are overflow checks just a few lines above the place you modify:

        if (bloc->logicalBlockNum + count < count ||
            (bloc->logicalBlockNum + count) > partmap->s_partition_len) {

So please update those to take 'offset' into account instead. Also please use check_add_overflow() for the integer overflow check.

Honza

Quoted 25 lines
---
 fs/udf/balloc.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/udf/balloc.c +7 -0
index ab3ffc355949..cd83bbc7d890 100644
@@ -151,6 +151,13 @@ static void udf_bitmap_free_blocks(struct super_block *sb,
 	block = bloc->logicalBlockNum + offset +
 		(sizeof(struct spaceBitmapDesc) << 3);
 
+	if (block < offset + (sizeof(struct spaceBitmapDesc) << 3)) {
+		udf_debug("integer overflow: %u + %u + %zu",
+			  bloc->logicalBlockNum, offset,
+			  sizeof(struct spaceBitmapDesc) << 3);
+		goto error_return;
+	}
+
 	do {
 		overflow = 0;
 		block_group = block >> (sb->s_blocksize_bits + 3);
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR