diff mbox series

serial: 8250_lpss: Fix missing check for return value of pci_get_slot()

Message ID 20230614153226.117768-1-cymi20@fudan.edu.cn
State New
Headers show
Series serial: 8250_lpss: Fix missing check for return value of pci_get_slot() | expand

Commit Message

Chenyuan Mi June 14, 2023, 3:32 p.m. UTC
The pci_get_slot() function may return NULL, which may
cause null pointer deference, and most other callsites of
pci_get_slot() do Null check. Add Null check for return
value of pci_get_slot().

Found by our static analysis tool.

Signed-off-by: Chenyuan Mi <cymi20@fudan.edu.cn>
---
 drivers/tty/serial/8250/8250_lpss.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Andy Shevchenko June 14, 2023, 4:08 p.m. UTC | #1
On Wed, Jun 14, 2023 at 08:32:26AM -0700, Chenyuan Mi wrote:
> The pci_get_slot() function may return NULL, which may
> cause null pointer deference, and most other callsites of
> pci_get_slot() do Null check. Add Null check for return
> value of pci_get_slot().
> 
> Found by our static analysis tool.

...

>  	dma_dev = pci_get_slot(pdev->bus, PCI_DEVFN(PCI_SLOT(pdev->devfn), 0));
> +	if (!dma_dev)
> +		return -ENODEV;

This adds (almost) a dead code. The function 0 must be present in accordance
with the PCI specification (even earliest version of it state that).
If pci_get_slot() returns a NULL, in this case it means that something, much
bigger issue, happens and this check won't help us to do anything anyway.
Greg Kroah-Hartman June 14, 2023, 4:28 p.m. UTC | #2
On Wed, Jun 14, 2023 at 08:32:26AM -0700, Chenyuan Mi wrote:
> The pci_get_slot() function may return NULL, which may
> cause null pointer deference, and most other callsites of
> pci_get_slot() do Null check. Add Null check for return
> value of pci_get_slot().
> 
> Found by our static analysis tool.

Please read Documentation/process/researcher-guidelines.rst for how to
do this properly.  Until then, we obviously can not take these types of
patches.

greg k-h
diff mbox series

Patch

diff --git a/drivers/tty/serial/8250/8250_lpss.c b/drivers/tty/serial/8250/8250_lpss.c
index 0e43bdfb7459..05af5865ee1b 100644
--- a/drivers/tty/serial/8250/8250_lpss.c
+++ b/drivers/tty/serial/8250/8250_lpss.c
@@ -137,6 +137,8 @@  static int byt_serial_setup(struct lpss8250 *lpss, struct uart_port *port)
 	}
 
 	dma_dev = pci_get_slot(pdev->bus, PCI_DEVFN(PCI_SLOT(pdev->devfn), 0));
+	if (!dma_dev)
+		return -ENODEV;
 
 	param->dma_dev = &dma_dev->dev;
 	param->m_master = 0;