serial: core: don't kfree device managed data
Subsystems (2)
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.
--- drivers/tty/serial/serial_base_bus.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/tty/serial/serial_base_bus.c +2 -6
@@ -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
Quoted 5 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()are a double free bug.Fixes 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")Signed-off-by Dan Carpenter <[email protected]>
Thanks for catching it:
Quoted 10 lines · Tony Lindgren
Quoted 5 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()are a double free bug.Fixes 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")Signed-off-by Dan Carpenter <[email protected]>Thanks for catching it:
Reviewed-by Tony Lindgren <[email protected]>
Be careful, we can now get memory leaks :-)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Tony Lindgren
Quoted 3 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()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")Signed-off-by Dan Carpenter <[email protected]>Thanks for catching it:
Reviewed-by Tony Lindgren <[email protected]>Be careful, we can now get memory leaks :-)
I'll start doing Reviewed-by-my-hazy-understanding :)
Regards,
Tony
Quoted 2 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()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
Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()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
Quoted 4 lines · Tony Lindgren
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()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 theput_device().We have
serial_base_device_init()calldevice_initialize(), is that not enough forput_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
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 callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()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 theput_device().We have
serial_base_device_init()calldevice_initialize(), is that not enough forput_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
Quoted 9 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()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
@@ -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); }
Quoted 4 lines · Dan Carpenter
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
The
put_device()function will callserial_base_ctrl_release()orserial_base_port_release()so thesekfrees()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 thedevice_initialize()inserial_base_device_init(). If we wanted to undo adevice_add()the function isdevice_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 carpenterdrivers/tty/serial/serial_base_bus.c +5
-5@@ -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
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
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
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
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