From 0b1452dee4d0c89de134630c5ae714cb613b2d2e Mon Sep 17 00:00:00 2001 From: Kevin Lim Date: Wed, 12 Mar 2025 11:28:03 +0800 Subject: [PATCH] serial: core: add checking for platform device Commit 978ca75850f2 ("serial: core: create anonymous parent device") introduced a NULL pointer dereference when attempting to access the platform_device structure of the parent. This commit adds a check for parent's device type before getting its platform_device structure. Fixes: 978ca75850f2 ("serial: core: create anonymous parent device") Signed-off-by: Kevin Lim --- drivers/tty/serial/serial_core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index a595e4d303a55..23aaa299c2716 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -46,6 +46,10 @@ static DEFINE_MUTEX(port_mutex); */ static struct lock_class_key port_lock_key; +static struct device_type anon_dev_type = { + .name = "anon_port", +}; + #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) /* @@ -3396,6 +3400,7 @@ int serial_core_register_port(struct uart_driver *drv, struct uart_port *port) { struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev = NULL; int ret; + bool is_parent_anon_port = false; mutex_lock(&port_mutex); @@ -3419,6 +3424,8 @@ int serial_core_register_port(struct uart_driver *drv, struct uart_port *port) } port->dev = &pdev->dev; + port->dev->type = &anon_dev_type; + is_parent_anon_port = true; } /* Inititalize a serial core controller device if needed */ @@ -3457,7 +3464,7 @@ int serial_core_register_port(struct uart_driver *drv, struct uart_port *port) err_unregister_ctrl_dev: serial_base_ctrl_device_remove(new_ctrl_dev); - if (strncmp(to_platform_device(port->dev)->name, "anon_port", 9) == 0) + if (is_parent_anon_port) platform_device_del(to_platform_device(port->dev)); err_unlock: @@ -3490,7 +3497,7 @@ void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id)) serial_base_ctrl_device_remove(ctrl_dev); - if (strncmp(to_platform_device(phys_dev)->name, "anon_port", 9) == 0) + if (phys_dev->type == &anon_dev_type) platform_device_del(to_platform_device(phys_dev)); mutex_unlock(&port_mutex);