diff mbox series

serial: 8250_aspeed_vuart: Fix potential NULL dereference in aspeed_vuart_probe

Message ID 20220404143842.16960-1-linmq006@gmail.com (mailing list archive)
State New, archived
Headers show
Series serial: 8250_aspeed_vuart: Fix potential NULL dereference in aspeed_vuart_probe | expand

Commit Message

Miaoqian Lin April 4, 2022, 2:38 p.m. UTC
platform_get_resource() may fail and return NULL, so we should
better check it's return value to avoid a NULL pointer dereference.

Fixes: 54da3e381c2b ("serial: 8250_aspeed_vuart: use UPF_IOREMAP to set up register mapping")
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
 drivers/tty/serial/8250/8250_aspeed_vuart.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Greg KH April 4, 2022, 2:45 p.m. UTC | #1
On Mon, Apr 04, 2022 at 02:38:40PM +0000, Miaoqian Lin wrote:
> platform_get_resource() may fail and return NULL, so we should
> better check it's return value to avoid a NULL pointer dereference.
> 
> Fixes: 54da3e381c2b ("serial: 8250_aspeed_vuart: use UPF_IOREMAP to set up register mapping")
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
>  drivers/tty/serial/8250/8250_aspeed_vuart.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/tty/serial/8250/8250_aspeed_vuart.c b/drivers/tty/serial/8250/8250_aspeed_vuart.c
> index 93fe10c680fb..9d2a7856784f 100644
> --- a/drivers/tty/serial/8250/8250_aspeed_vuart.c
> +++ b/drivers/tty/serial/8250/8250_aspeed_vuart.c
> @@ -429,6 +429,8 @@ static int aspeed_vuart_probe(struct platform_device *pdev)
>  	timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
>  
>  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	if (!res)
> +		return -EINVAL;

How did you test this change was correct?
Miaoqian Lin April 4, 2022, 3:25 p.m. UTC | #2
On Mon, Apr 04, 2022 at 04:45:50PM +0200, Greg Kroah-Hartman wrote:
> On Mon, Apr 04, 2022 at 02:38:40PM +0000, Miaoqian Lin wrote:
> > platform_get_resource() may fail and return NULL, so we should
> > better check it's return value to avoid a NULL pointer dereference.
> > 
> > Fixes: 54da3e381c2b ("serial: 8250_aspeed_vuart: use UPF_IOREMAP to set up register mapping")
> > Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> > ---
> >  drivers/tty/serial/8250/8250_aspeed_vuart.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/drivers/tty/serial/8250/8250_aspeed_vuart.c b/drivers/tty/serial/8250/8250_aspeed_vuart.c
> > index 93fe10c680fb..9d2a7856784f 100644
> > --- a/drivers/tty/serial/8250/8250_aspeed_vuart.c
> > +++ b/drivers/tty/serial/8250/8250_aspeed_vuart.c
> > @@ -429,6 +429,8 @@ static int aspeed_vuart_probe(struct platform_device *pdev)
> >  	timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
> >  
> >  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > +	if (!res)
> > +		return -EINVAL;
> 
> How did you test this change was correct?

Hi,

   I look into the implementation of platform_get_resource(),
and do cross-check the usages of it in the codebase, especially
the usages in other probe function. 
And I go through some simliar bugfix commits in the revision history——
add check for return value of platform_get_resource() in probe
functions,to learn the way to fix this kind of bugs. 
But sorry I don't have the corresponding device for running test.
diff mbox series

Patch

diff --git a/drivers/tty/serial/8250/8250_aspeed_vuart.c b/drivers/tty/serial/8250/8250_aspeed_vuart.c
index 93fe10c680fb..9d2a7856784f 100644
--- a/drivers/tty/serial/8250/8250_aspeed_vuart.c
+++ b/drivers/tty/serial/8250/8250_aspeed_vuart.c
@@ -429,6 +429,8 @@  static int aspeed_vuart_probe(struct platform_device *pdev)
 	timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
 
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -EINVAL;
 
 	memset(&port, 0, sizeof(port));
 	port.port.private_data = vuart;