Skip to contentSkip to conversation

media: mali-c55: Fix scaler factor overflow for large crop sizes

PATCH In review David Carlier 3 participants 3 messages Jun 18 – Jun 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 media: mali-c55: Fix scaler factor overflow for large crop sizes

The horizontal and vertical scaling factors multiply the crop dimensions by MALI_C55_RSZ_SCALER_FACTOR, a Q4.20 factor of (1 << 20). Both operands are 32-bit, so the multiplication wraps before the result is stored in the u64 scale variables. For any crop dimension of 4096 or more (the maximum is 8192) the value overflows; an 8192 to 4096 downscale yields a TINC of zero, so the scaler never advances and the output is corrupted.

Define MALI_C55_RSZ_SCALER_FACTOR as a 64-bit constant so the multiplication is performed in 64-bit.

Fixes d5f281f3dd29 ("media: mali-c55: Add Mali-C55 ISP driver")

---

Notes:
v2: Define the macro as a 64-bit constant (BIT_ULL) instead of casting
each multiplication, per Dan's review.

 drivers/media/platform/arm/mali-c55/mali-c55-resizer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/arm/mali-c55/mali-c55-resizer.c +1 -1
index c4f46651d..6706939b4 100644
@@ -15,7 +15,7 @@
 #include "mali-c55-registers.h"
 
 /* Scaling factor in Q4.20 format. */
-#define MALI_C55_RSZ_SCALER_FACTOR	(1U << 20)
+#define MALI_C55_RSZ_SCALER_FACTOR	BIT_ULL(20)
 
 #define MALI_C55_RSZ_COEFS_BANKS	8
 #define MALI_C55_RSZ_COEFS_ENTRIES	64
Signature · 1 line
2.53.0
maintainer Reviewed-by on v2 1/1
#1 lore raw

Hi David

Quoted 7 lines · David Carlier

The horizontal and vertical scaling factors multiply the crop dimensions by MALI_C55_RSZ_SCALER_FACTOR, a Q4.20 factor of (1 << 20). Both operands are 32-bit, so the multiplication wraps before the result is stored in the u64 scale variables. For any crop dimension of 4096 or more (the maximum is 8192) the value overflows; an 8192 to 4096 downscale yields a TINC of zero, so the scaler never advances and the output is corrupted.

Quoted 6 lines

Define MALI_C55_RSZ_SCALER_FACTOR as a 64-bit constant so the multiplication is performed in 64-bit.

Fixes d5f281f3dd29 ("media: mali-c55: Add Mali-C55 ISP driver")

Thanks very much:

Quoted 22 lines

---

Notes:
v2: Define the macro as a 64-bit constant (BIT_ULL) instead of casting
each multiplication, per Dan's review.

  drivers/media/platform/arm/mali-c55/mali-c55-resizer.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/arm/mali-c55/mali-c55-resizer.c +1 -1
index c4f46651d..6706939b4 100644
@@ -15,7 +15,7 @@
  #include "mali-c55-registers.h"
 
  /* Scaling factor in Q4.20 format. */
-#define MALI_C55_RSZ_SCALER_FACTOR	(1U << 20)
+#define MALI_C55_RSZ_SCALER_FACTOR	BIT_ULL(20)
 
  #define MALI_C55_RSZ_COEFS_BANKS	8
  #define MALI_C55_RSZ_COEFS_ENTRIES	64
Reviewed-by on v2 1/1
#2 lore raw
In reply to David Carlier
Quoted 7 lines · David Carlier

The horizontal and vertical scaling factors multiply the crop dimensions by MALI_C55_RSZ_SCALER_FACTOR, a Q4.20 factor of (1 << 20). Both operands are 32-bit, so the multiplication wraps before the result is stored in the u64 scale variables. For any crop dimension of 4096 or more (the maximum is 8192) the value overflows; an 8192 to 4096 downscale yields a TINC of zero, so the scaler never advances and the output is corrupted.

Quoted 6 lines

Define MALI_C55_RSZ_SCALER_FACTOR as a 64-bit constant so the multiplication is performed in 64-bit.

Fixes d5f281f3dd29 ("media: mali-c55: Add Mali-C55 ISP driver")

LGTM

Yours,
Linus Walleij