Skip to contentSkip to conversation

pstore: ram_core: fix possible overflow in persistent_ram_init_ecc()

PATCH Applied Sergey Shtylyov 2 participants 3 messages Nov 5, 2023 – Dec 3, 2023 Pstore filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pstore: ram_core: fix possible overflow in persistent_ram_init_ecc()

In persistent_ram_init_ecc(), on 64-bit arches DIV_ROUND_UP() will return 64-bit value since persistent_ram_zone::buffer_size has type size_t which is derived from the 64-bit *unsigned long*, while the ecc_blocks variable this value gets assigned to has (always 32-bit) *int* type. Even if that value fits into *int* type, an overflow is still possible when calculating the size_t typed ecc_total variable further below since there's no cast to any 64-bit type before multiplication. Declaring the ecc_blocks variable as *unsigned long* should fix this mess...

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

Fixes 9cc05ad97c57 ("staging: android: persistent_ram: refactor ecc support")

---
The patch is against the 'master' branch of Linus Torvalds' repo.

 fs/pstore/ram_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/pstore/ram_core.c +1 -1
index 650e437b55e6..131c1923b8c0 100644
@@ -190,7 +190,7 @@ static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
 {
 	int numerr;
 	struct persistent_ram_buffer *buffer = prz->buffer;
-	int ecc_blocks;
+	unsigned long ecc_blocks;
 	size_t ecc_total;
 
 	if (!ecc_info || !ecc_info->ecc_size)
Signature · 1 line
2.26.3
Applied
#1 lore raw
Quoted 10 lines · Sergey Shtylyov

In persistent_ram_init_ecc(), on 64-bit arches DIV_ROUND_UP() will return 64-bit value since persistent_ram_zone::buffer_size has type size_t which is derived from the 64-bit *unsigned long*, while the ecc_blocks variable this value gets assigned to has (always 32-bit) *int* type. Even if that value fits into *int* type, an overflow is still possible when calculating the size_t typed ecc_total variable further below since there's no cast to any 64-bit type before multiplication. Declaring the ecc_blocks variable as *unsigned long* should fix this mess...

[...]

I changed this from unsigned long to size_t, which will do the same thing.

Applied to for-linus/pstore, thanks!

[1/1] pstore: ram_core: fix possible overflow in persistent_ram_init_ecc()

      https://git.kernel.org/kees/c/c92116e01d32

Take care,

Signature · 1 line
Kees Cook
#2 lore raw

On 12/2/23 11:47 PM, Kees Cook wrote:
[...]

Quoted 9 lines
Quoted 9 lines

In persistent_ram_init_ecc(), on 64-bit arches DIV_ROUND_UP() will return 64-bit value since persistent_ram_zone::buffer_size has type size_t which is derived from the 64-bit *unsigned long*, while the ecc_blocks variable this value gets assigned to has (always 32-bit) *int* type. Even if that value fits into *int* type, an overflow is still possible when calculating the size_t typed ecc_total variable further below since there's no cast to any 64-bit type before multiplication. Declaring the ecc_blocks variable as *unsigned long* should fix this mess...

Quoted 3 lines
Quoted 1 line

[...]

I changed this from unsigned long to size_t, which will do the same thing.

Not quite the same. And ecc_blocks is a block count, not a byte count, so
size_t doesn't seem appropriate...

Quoted 6 lines

Applied to for-linus/pstore, thanks!

[1/1] pstore: ram_core: fix possible overflow in persistent_ram_init_ecc()

      https://git.kernel.org/kees/c/c92116e01d32

Take care,

Thanks anyway. :-)

MBR, Sergey