accel/qaic: Use check_add_overflow in sahara for 64b types
Subsystems (3)
PATCH v2 1/1 accel/qaic: Use check_add_overflow in sahara for 64b types
From: Zack McKevitt <[email protected]>
Use check_add_overflow instead of size_add in sahara when 64b types are being added to ensure compatibility with 32b systems. The size_add function parameters are of size_t, so 64b data types may be truncated when cast to size_t on 32b systems. When using check_add_overflow, no type casts are made, making it a more portable option.
---
Changes in V2:
- Use explicit casts with check_*_overflow() calls
- Replace min() with min_t()
- Link to V1: https://lore.kernel.org/all/[email protected]
--- drivers/accel/qaic/sahara.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/accel/qaic/sahara.c +13 -8
@@ -538,6 +538,7 @@ static void sahara_parse_dump_table(struct sahara_context *context)struct sahara_memory_dump_meta_v1 *dump_meta;u64 table_nents;u64 dump_length; +u64 mul_bytes;int ret;u64 i;
@@ -551,8 +552,9 @@ static void sahara_parse_dump_table(struct sahara_context *context) dev_table[i].description[SAHARA_TABLE_ENTRY_STR_LEN -1 ] =0 ; dev_table[i].filename[SAHARA_TABLE_ENTRY_STR_LEN -1 ] =0 ; - dump_length =size_add (dump_length,le64_to_cpu (dev_table[i].length)); -if (dump_length ==SIZE_MAX ) { +if (check_add_overflow (dump_length, +le64_to_cpu (dev_table[i].length), + &dump_length)) {/* Discard the dump */ sahara_send_reset (context);return ;
@@ -568,14 +570,17 @@ static void sahara_parse_dump_table(struct sahara_context *context) dev_table[i].filename); } - dump_length =size_add (dump_length,sizeof (*dump_meta)); -if (dump_length ==SIZE_MAX ) { +if (check_add_overflow (dump_length, (u64 )sizeof (*dump_meta), &dump_length)) {/* Discard the dump */ sahara_send_reset (context);return ; } - dump_length =size_add (dump_length,size_mul (sizeof (*image_out_table), table_nents)); -if (dump_length ==SIZE_MAX ) { +if (check_mul_overflow ((u64 )sizeof (*image_out_table), table_nents, &mul_bytes)) { +/* Discard the dump */ +sahara_send_reset (context); +return ; + } +if (check_add_overflow (dump_length, mul_bytes, &dump_length)) {/* Discard the dump */ sahara_send_reset (context);return ;
@@ -615,7 +620,7 @@ static void sahara_parse_dump_table(struct sahara_context *context)/* Request the first chunk of the first image */ context->dump_image = &image_out_table[0 ]; - dump_length =min (context->dump_image->length,SAHARA_READ_MAX_SIZE ); + dump_length =min_t (u64, context->dump_image->length,SAHARA_READ_MAX_SIZE );/* Avoid requesting EOI sized data so that we can identify errors */ if (dump_length ==SAHARA_END_OF_IMAGE_LENGTH ) dump_length =SAHARA_END_OF_IMAGE_LENGTH /2 ;
@@ -663,7 +668,7 @@ static void sahara_parse_dump_image(struct sahara_context *context)/* Get next image chunk */ dump_length = context->dump_image->length - context->dump_image_offset; - dump_length =min (dump_length,SAHARA_READ_MAX_SIZE ); + dump_length =min_t (u64, dump_length,SAHARA_READ_MAX_SIZE );/* Avoid requesting EOI sized data so that we can identify errors */ if (dump_length ==SAHARA_END_OF_IMAGE_LENGTH ) dump_length =SAHARA_END_OF_IMAGE_LENGTH /2 ;
Signature · 1 line
2.43.0
Jeff Hugo
maintainerQuoted 11 lines · Youssef Samir
From: Zack McKevitt <[email protected]>
Use
check_add_overflowinstead ofsize_addin sahara when 64b types are being added to ensure compatibility with 32b systems. Thesize_addfunction parameters are ofsize_t, so 64b data types may be truncated when cast tosize_ton 32b systems. When usingcheck_add_overflow, no type casts are made, making it a more portable option.Signed-off-by Zack McKevitt <[email protected]>Signed-off-by Youssef Samir <[email protected]>
I think you are missing two reviewed-by tags from v1.
Quoted 4 lines
---
Changes in V2:
- Use explicit casts with check_*_overflow() calls
- Replacemin()withmin_t()
Fixing typecast warnings with explicit casts seems good, but min->min_t seems outside of scope. It doesn't match with the commit text, and I don't see a good way to include it without making this patch look like it should be split into two. Thus, lets do the typecast fixes for the min usage as a follow up.
-Jeff
Will send a V3 with only the explicit cast, and send a separate patch for the min_t() change. Sorry for the inconvenience.
-Youssef
Quoted 28 lines · Jeff Hugo
Quoted 11 lines · Youssef Samir
From: Zack McKevitt <[email protected]>
Use
check_add_overflowinstead ofsize_addin sahara when 64b types are being added to ensure compatibility with 32b systems. Thesize_addfunction parameters are ofsize_t, so 64b data types may be truncated when cast tosize_ton 32b systems. When usingcheck_add_overflow, no type casts are made, making it a more portable option.Signed-off-by Zack McKevitt <[email protected]>Signed-off-by Youssef Samir <[email protected]>I think you are missing two reviewed-by tags from v1.
Quoted 4 lines
---
Changes in V2:
- Use explicit casts with check_*_overflow() calls
- Replacemin()withmin_t()Fixing typecast warnings with explicit casts seems good, but
min->min_tseems outside of scope. It doesn't match with the commit text, and I don't see a good way to include it without making this patch look like it should be split into two. Thus, lets do the typecast fixes for the min usage as a follow up.-Jeff