Skip to contentSkip to conversation

tty: serial: fsl_lpuart: Fix possible integer overflow

PATCH In review Sherry Sun 3 participants 4 messages Jul 18, 2023 TTY layer and serial drivers

lore
Share & tools
lore

Sherry Sun

author
#0 lore raw

PATCH 1/1 tty: serial: fsl_lpuart: Fix possible integer overflow

This patch addresses the following Coverity report, fix it by casting sport->port.frame_time to type u64.

CID 32305660: Unintentional integer overflow (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression sport->port.frame_time * 8U with type unsigned int (32 bits, unsigned) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type u64 (64 bits, unsigned).

Fixes cf9aa72d2f91 ("tty: serial: fsl_lpuart: optimize the timer based EOP logic")
---
 drivers/tty/serial/fsl_lpuart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/fsl_lpuart.c +1 -1
index c1980ea52666..07b3b26732db 100644
@@ -1373,7 +1373,7 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
 
 	sport->last_residue = 0;
 	sport->dma_rx_timeout = max(nsecs_to_jiffies(
-		sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);
+		(u64)sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);
 
 	ring->buf = kzalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
 	if (!ring->buf)
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 20 lines · Sherry Sun

This patch addresses the following Coverity report, fix it by casting sport->port.frame_time to type u64.

CID 32305660: Unintentional integer overflow (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression sport->port.frame_time * 8U with type unsigned int (32 bits, unsigned) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type u64 (64 bits, unsigned).

Fixes cf9aa72d2f91 ("tty: serial: fsl_lpuart: optimize the timer based EOP logic")
---
  drivers/tty/serial/fsl_lpuart.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/fsl_lpuart.c +0 -0
index c1980ea52666..07b3b26732db 100644

drivers/tty/serial/fsl_lpuart.c static inline int lpuart_start_rx_dma(struct lpuart_port *sport) · line 1376

Quoted 5 lines
drivers/tty/serial/fsl_lpuart.c +1 -1
@@ -1373,7 +1373,7 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
 
  	sport->last_residue = 0;
  	sport->dma_rx_timeout = max(nsecs_to_jiffies(
-		sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);
+		(u64)sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);

Can you explain how that can overflow? In the worst case (1 start bit, 8 data bits, 2 stop bits, parity bit, address bit, 50 bauds), frame_time would contain:
13*1e9/50 = 260,000,000. (260 ms)

Then the multiplication above is:
260,000,000*8 = 2,080,000,000. (2 seconds)

which is still less than 2^32-1.

thanks,

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 34 lines · Jiri Slaby
Quoted 22 lines · Sherry Sun

This patch addresses the following Coverity report, fix it by casting sport->port.frame_time to type u64.

CID 32305660: Unintentional integer overflow (OVERFLOW_BEFORE_WIDEN) Potentially overflowing expression sport->port.frame_time * 8U with type unsigned int (32 bits, unsigned) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type u64 (64 bits, unsigned).

Fixes cf9aa72d2f91 ("tty: serial: fsl_lpuart: optimize the timer based EOP

logic")

---
  drivers/tty/serial/fsl_lpuart.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/fsl_lpuart.c +0 -0
index c1980ea52666..07b3b26732db 100644

drivers/tty/serial/fsl_lpuart.c static inline int lpuart_start_rx_dma(struct · line 1376

Quoted 5 lines
drivers/tty/serial/fsl_lpuart.c +1 -1
@@ -1373,7 +1373,7 @@ static inline int lpuart_start_rx_dma(struct
 lpuart_port *sport)
    	sport->last_residue = 0;
  	sport->dma_rx_timeout = max(nsecs_to_jiffies(
-		sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);
+		(u64)sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);

Can you explain how that can overflow? In the worst case (1 start bit, 8 data bits, 2 stop bits, parity bit, address bit, 50 bauds), frame_time would contain:
13*1e9/50 = 260,000,000. (260 ms)

Quoted 4 lines

Then the multiplication above is:
260,000,000*8 = 2,080,000,000. (2 seconds)

which is still less than 2^32-1.

I was wondering the same thing.

This isn't a real bug. All findings from code analysis tools must be carefully evaluated to filter wheat out of chaff and this falls into the latter category. Please make sure next time you understand and explain also in the changelog how the problem can be manifested for real before sending this kind of patches.

Signature · 1 line
 i.
author
#3 lore raw
Quoted 57 lines

-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: 2023年7月18日 17:56
To: Sherry Sun <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>; Shenwei Wang <[email protected]>; linux-serial <[email protected]>; LKML <[email protected]>; dl-linux-imx <[email protected]>; Jiri Slaby <[email protected]>
Subject: Re: [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow

Quoted 6 lines · Jiri Slaby
Quoted 5 lines · Sherry Sun

This patch addresses the following Coverity report, fix it by casting
sport->port.frame_time to type u64.

CID 32305660: Unintentional integer overflow

(OVERFLOW_BEFORE_WIDEN)

Quoted 31 lines
Quoted 19 lines

Potentially overflowing expression sport->port.frame_time * 8U with type unsigned int (32 bits, unsigned) is evaluated using 32-bit arithmetic, and then used in a context that expects an expression of type u64 (64 bits, unsigned).

Fixes cf9aa72d2f91 ("tty: serial: fsl_lpuart: optimize the timer

based EOP
logic")

---
  drivers/tty/serial/fsl_lpuart.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/tty/serial/fsl_lpuart.c index c1980ea52666..07b3b26732db 100644

drivers/tty/serial/fsl_lpuart.c static inline int lpuart_start_rx_dma(struct · line 1376

Quoted 5 lines
drivers/tty/serial/fsl_lpuart.c +1 -1
@@ -1373,7 +1373,7 @@ static inline int lpuart_start_rx_dma(struct
 lpuart_port *sport)
    	sport->last_residue = 0;
  	sport->dma_rx_timeout = max(nsecs_to_jiffies(
-		sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);
+		(u64)sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);

Can you explain how that can overflow? In the worst case (1 start bit, 8 data bits, 2 stop bits, parity bit, address bit, 50 bauds), frame_time would
contain:
13*1e9/50 = 260,000,000. (260 ms)

Quoted 4 lines

Then the multiplication above is:
260,000,000*8 = 2,080,000,000. (2 seconds)

which is still less than 2^32-1.

Quoted 8 lines

I was wondering the same thing.

This isn't a real bug. All findings from code analysis tools must be carefully evaluated to filter wheat out of chaff and this falls into the latter category. Please make sure next time you understand and explain also in the changelog how the problem can be manifested for real before sending this kind of patches.

Hi Ilpo and Jiri,
You are right, now the DMA_RX_IDLE_CHARS is 8, so even the worst case frametime won't overflow uint32. Thanks for the reminder, I will drop the patch and pay attention next time.

Best Regards
Sherry