* [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow
@ 2023-07-18 6:56 Sherry Sun
2023-07-18 9:33 ` Jiri Slaby
0 siblings, 1 reply; 4+ messages in thread
From: Sherry Sun @ 2023-07-18 6:56 UTC (permalink / raw)
To: gregkh, jirislaby, ilpo.jarvinen, shenwei.wang
Cc: linux-serial, linux-kernel, linux-imx
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")
Signed-off-by: Sherry Sun <sherry.sun@nxp.com>
---
drivers/tty/serial/fsl_lpuart.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
index c1980ea52666..07b3b26732db 100644
--- a/drivers/tty/serial/fsl_lpuart.c
+++ b/drivers/tty/serial/fsl_lpuart.c
@@ -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)
--
2.17.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow 2023-07-18 6:56 [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow Sherry Sun @ 2023-07-18 9:33 ` Jiri Slaby 2023-07-18 9:56 ` Ilpo Järvinen 0 siblings, 1 reply; 4+ messages in thread From: Jiri Slaby @ 2023-07-18 9:33 UTC (permalink / raw) To: Sherry Sun, gregkh, ilpo.jarvinen, shenwei.wang Cc: linux-serial, linux-kernel, linux-imx On 18. 07. 23, 8:56, Sherry Sun wrote: > 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") > Signed-off-by: Sherry Sun <sherry.sun@nxp.com> > --- > drivers/tty/serial/fsl_lpuart.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c > index c1980ea52666..07b3b26732db 100644 > --- a/drivers/tty/serial/fsl_lpuart.c > +++ b/drivers/tty/serial/fsl_lpuart.c > @@ -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, -- js suse labs ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow 2023-07-18 9:33 ` Jiri Slaby @ 2023-07-18 9:56 ` Ilpo Järvinen 2023-07-18 11:03 ` Sherry Sun 0 siblings, 1 reply; 4+ messages in thread From: Ilpo Järvinen @ 2023-07-18 9:56 UTC (permalink / raw) To: Sherry Sun Cc: Greg Kroah-Hartman, shenwei.wang, linux-serial, LKML, linux-imx, Jiri Slaby On Tue, 18 Jul 2023, Jiri Slaby wrote: > On 18. 07. 23, 8:56, Sherry Sun wrote: > > 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") > > Signed-off-by: Sherry Sun <sherry.sun@nxp.com> > > --- > > drivers/tty/serial/fsl_lpuart.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/drivers/tty/serial/fsl_lpuart.c > > b/drivers/tty/serial/fsl_lpuart.c > > index c1980ea52666..07b3b26732db 100644 > > --- a/drivers/tty/serial/fsl_lpuart.c > > +++ b/drivers/tty/serial/fsl_lpuart.c > > @@ -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. 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. -- i. ^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow 2023-07-18 9:56 ` Ilpo Järvinen @ 2023-07-18 11:03 ` Sherry Sun 0 siblings, 0 replies; 4+ messages in thread From: Sherry Sun @ 2023-07-18 11:03 UTC (permalink / raw) To: Ilpo Järvinen, Jiri Slaby Cc: Greg Kroah-Hartman, Shenwei Wang, linux-serial, LKML, dl-linux-imx > -----Original Message----- > From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> > Sent: 2023年7月18日 17:56 > To: Sherry Sun <sherry.sun@nxp.com> > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>; Shenwei Wang > <shenwei.wang@nxp.com>; linux-serial <linux-serial@vger.kernel.org>; > LKML <linux-kernel@vger.kernel.org>; dl-linux-imx <linux-imx@nxp.com>; > Jiri Slaby <jirislaby@kernel.org> > Subject: Re: [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow > > On Tue, 18 Jul 2023, Jiri Slaby wrote: > > > On 18. 07. 23, 8:56, Sherry Sun wrote: > > > 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") > > > Signed-off-by: Sherry Sun <sherry.sun@nxp.com> > > > --- > > > drivers/tty/serial/fsl_lpuart.c | 2 +- > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > diff --git a/drivers/tty/serial/fsl_lpuart.c > > > b/drivers/tty/serial/fsl_lpuart.c index c1980ea52666..07b3b26732db > > > 100644 > > > --- a/drivers/tty/serial/fsl_lpuart.c > > > +++ b/drivers/tty/serial/fsl_lpuart.c > > > @@ -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. > > 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-07-18 11:03 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2023-07-18 6:56 [PATCH] tty: serial: fsl_lpuart: Fix possible integer overflow Sherry Sun 2023-07-18 9:33 ` Jiri Slaby 2023-07-18 9:56 ` Ilpo Järvinen 2023-07-18 11:03 ` Sherry Sun
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox