Skip to contentSkip to conversation

serial: core: don't kfree device managed data

PATCH In review Dan Carpenter 3 participants 14 messages Jun 6, 2023 – Jun 14, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 serial: core: don't kfree device managed data

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

Fixes 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
---
 drivers/tty/serial/serial_base_bus.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/tty/serial/serial_base_bus.c +2 -6
index 1b37833b8f66..f125d16f3d16 100644
@@ -98,7 +98,7 @@ struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
 				      serial_base_ctrl_release,
 				      port->ctrl_id);
 	if (err)
-		goto err_free_ctrl_dev;
+		goto err_put_device;
 
 	err = device_add(&ctrl_dev->dev);
 	if (err)
@@ -108,8 +108,6 @@ struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
 
 err_put_device:
 	put_device(&ctrl_dev->dev);
-err_free_ctrl_dev:
-	kfree(ctrl_dev);
 
 	return ERR_PTR(err);
 }
@@ -140,7 +138,7 @@ struct serial_port_device *serial_base_port_add(struct uart_port *port,
 				      serial_base_port_release,
 				      port->line);
 	if (err)
-		goto err_free_port_dev;
+		goto err_put_device;
 
 	port_dev->port = port;
 
@@ -152,8 +150,6 @@ struct serial_port_device *serial_base_port_add(struct uart_port *port,
 
 err_put_device:
 	put_device(&port_dev->dev);
-err_free_port_dev:
-	kfree(port_dev);
 
 	return ERR_PTR(err);
 }
Signature · 1 line
2.39.2
#2 lore raw
Quoted 10 lines · Tony Lindgren
Quoted 5 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

Fixes 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")

Thanks for catching it:

Be careful, we can now get memory leaks :-)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Tony Lindgren
Quoted 3 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Fixes 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")

Thanks for catching it:

Be careful, we can now get memory leaks :-)

I'll start doing Reviewed-by-my-hazy-understanding :)

Regards,

Tony

#4 lore raw
In reply to Dan Carpenter
Quoted 2 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

...

These labels are also called without device being even added. So, this is not good enough as far as I can tell.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

...

These labels are also called without device being even added. So, this is not good enough as far as I can tell.

I guess you mean the possibe error returned from the call to serial_base_device_init()?

If serial_base_device_init() fails, we return error and end up doing the put_device().

We have serial_base_device_init() call device_initialize(), is that not enough for put_device()?

Regards,

Tony

#6 lore raw
Quoted 4 lines · Tony Lindgren
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

...

Quoted 11 lines
Quoted 2 lines

These labels are also called without device being even added. So, this is not good enough as far as I can tell.

I guess you mean the possibe error returned from the call to serial_base_device_init()?

If serial_base_device_init() fails, we return error and end up doing the put_device().

We have serial_base_device_init() call device_initialize(), is that not enough for put_device()?

It's not. The error is returned when device release callback is not assigned yet.

And also just noticed since we return deferred probe, the message there should be ratelimited or given only _once().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 14 lines · Andy Shevchenko
Quoted 4 lines · Tony Lindgren
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

...

Quoted 6 lines
Quoted 2 lines

These labels are also called without device being even added. So, this is not good enough as far as I can tell.

I guess you mean the possibe error returned from the call to serial_base_device_init()?

Quoted 8 lines
Quoted 5 lines

If serial_base_device_init() fails, we return error and end up doing the put_device().

We have serial_base_device_init() call device_initialize(), is that not enough for put_device()?

It's not. The error is returned when device release callback is not assigned yet.

OK thanks for clarifying.

Quoted 2 lines

And also just noticed since we return deferred probe, the message there should be ratelimited or given only _once().

We should be OK with debug here like Dan suggested.

Regards,

Tony

#8 lore raw
In reply to Andy Shevchenko
Quoted 9 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

...

These labels are also called without device being even added. So, this is not good enough as far as I can tell.

put_device() matches with the device_initialize() in serial_base_device_init(). If we wanted to undo a device_add() the function is device_del().

I originally wrote this patch last week but only resent it now because of an issue with my mail set up. I see that serial_base_device_init() has actually changed and there is an issue now where the -EPROBE_DEFER path leaks.

I think making callers handle -EPROBE_DEFER as a special case would be an ongoing source of bugs. So really I'd prefer something like this:

regards,
dan carpenter

drivers/tty/serial/serial_base_bus.c +5 -5
index 9354af7c11af..15fa0576d362 100644
@@ -50,17 +50,17 @@ static int serial_base_device_init(struct uart_port *port,
 				   void (*release)(struct device *dev),
 				   int id)
 {
-	if (!serial_base_initialized) {
-		dev_err(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
-		return -EPROBE_DEFER;
-	}
-
 	device_initialize(dev);
 	dev->type = type;
 	dev->parent = parent_dev;
 	dev->bus = &serial_base_bus_type;
 	dev->release = release;
 
+	if (!serial_base_initialized) {
+		dev_err(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
+		return -EPROBE_DEFER;
+	}
+
 	return dev_set_name(dev, "%s.%s.%d", type->name, dev_name(port->dev), id);
 }
 
#9 lore raw
Quoted 4 lines · Dan Carpenter
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter

The put_device() function will call serial_base_ctrl_release() or serial_base_port_release() so these kfrees() are a double free bug.

...

Quoted 7 lines
Quoted 2 lines

These labels are also called without device being even added. So, this is not good enough as far as I can tell.

put_device() matches with the device_initialize() in serial_base_device_init(). If we wanted to undo a device_add() the function is device_del().

Quoted 7 lines

I originally wrote this patch last week but only resent it now because of an issue with my mail set up. I see that serial_base_device_init() has actually changed and there is an issue now where the -EPROBE_DEFER path leaks.

I think making callers handle -EPROBE_DEFER as a special case would be an ongoing source of bugs. So really I'd prefer something like this:

I'm okay with the above, but it seems at the same time we need to limit the messages:

	dev_err_once(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
Quoted 31 lines

regards,
dan carpenter

drivers/tty/serial/serial_base_bus.c +5 -5
index 9354af7c11af..15fa0576d362 100644
@@ -50,17 +50,17 @@ static int serial_base_device_init(struct uart_port *port,
 				   void (*release)(struct device *dev),
 				   int id)
 {
-	if (!serial_base_initialized) {
-		dev_err(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
-		return -EPROBE_DEFER;
-	}
-
 	device_initialize(dev);
 	dev->type = type;
 	dev->parent = parent_dev;
 	dev->bus = &serial_base_bus_type;
 	dev->release = release;
 
+	if (!serial_base_initialized) {
+		dev_err(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
+		return -EPROBE_DEFER;
+	}
+
 	return dev_set_name(dev, "%s.%s.%d", type->name, dev_name(port->dev), id);
 }
 
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
Quoted 6 lines · Andy Shevchenko

I'm okay with the above, but it seems at the same time we need to limit the messages:

	dev_err_once(port->dev, "uart_add_one_port() called before arch_initcall()?\n");

Yeah. I would prefer if that was only printed as a debug message. -EPROBE_DEFER is supposed to be a normal part of the process.

regards,
dan carpenter

#11 lore raw
Quoted 10 lines · Dan Carpenter
Quoted 6 lines · Andy Shevchenko

I'm okay with the above, but it seems at the same time we need to limit the messages:

	dev_err_once(port->dev, "uart_add_one_port() called before arch_initcall()?\n");

Yeah. I would prefer if that was only printed as a debug message. -EPROBE_DEFER is supposed to be a normal part of the process.

Debug here should do the trick yeah.

Regards,

Tony

#12 lore raw

Hi Dan,

Quoted 6 lines · Tony Lindgren
Quoted 5 lines · Dan Carpenter
Quoted 4 lines · Andy Shevchenko

I'm okay with the above, but it seems at the same time we need to limit the messages:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
	dev_err_once(port->dev, "uart_add_one_port() called before arch_initcall()?\n");

Yeah. I would prefer if that was only printed as a debug message. -EPROBE_DEFER is supposed to be a normal part of the process.

Debug here should do the trick yeah.

Just wondering.. Are you going to send this fix or do you want me to type it up?

Regards,

Tony

#13 lore raw
Quoted 12 lines · Tony Lindgren

Hi Dan,

Quoted 9 lines · Tony Lindgren
Quoted 8 lines · Dan Carpenter
Quoted 6 lines · Andy Shevchenko

I'm okay with the above, but it seems at the same time we need to limit the messages:

	dev_err_once(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Yeah. I would prefer if that was only printed as a debug message. -EPROBE_DEFER is supposed to be a normal part of the process.

Debug here should do the trick yeah.

Just wondering.. Are you going to send this fix or do you want me to type it up?

I've sent it now. Thanks!

regards,
dan carpenter