pstore: ram_core: fix possible overflow in persistent_ram_init_ecc()
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.
---
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
@@ -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
Kees Cook
AppliedQuoted 10 lines · Sergey Shtylyov
In
persistent_ram_init_ecc(), on 64-bit archesDIV_ROUND_UP()will return 64-bit value sincepersistent_ram_zone::buffer_sizehas typesize_twhich is derived from the 64-bit *unsigned long*, while theecc_blocksvariable 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 thesize_ttypedecc_totalvariable further below since there's no cast to any 64-bit type before multiplication. Declaring theecc_blocksvariable 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
On 12/2/23 11:47 PM, Kees Cook wrote:
[...]
Quoted 9 lines
Quoted 9 lines
In
persistent_ram_init_ecc(), on 64-bit archesDIV_ROUND_UP()will return 64-bit value sincepersistent_ram_zone::buffer_sizehas typesize_twhich is derived from the 64-bit *unsigned long*, while theecc_blocksvariable 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 thesize_ttypedecc_totalvariable further below since there's no cast to any 64-bit type before multiplication. Declaring theecc_blocksvariable 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, sosize_t doesn't seem appropriate...
Quoted 6 lines
Applied to for-linus/pstore, thanks!
[1/1] pstore:
ram_core: fix possible overflow inpersistent_ram_init_ecc()https://git.kernel.org/kees/c/c92116e01d32Take care,
Thanks anyway. :-)
MBR, Sergey