pcmcia: ds: fix refcount leak in pcmcia_device_add()
PATCH 1/2 pcmcia: ds: fix refcount leak in pcmcia_device_add()
If device_register() returns error, the refcount of function_config need be put.
--- drivers/pcmcia/ds.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pcmcia/ds.c +3 -1
@@ -574,10 +574,12 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,pcmcia_device_query (p_dev);if (device_register (&p_dev->dev)) - goto err_unreg; + goto err_put_ref;return p_dev; + err_put_ref: +kref_put (&p_dev->function_config->ref, pcmcia_release_function); err_unreg:mutex_lock (&s->ops_mutex);list_del (&p_dev->socket_device_list);
Signature · 1 line
2.25.1
PATCH 2/2 pcmcia: ds: fix possible name leak in error path in pcmcia_device_add()
Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it need bee freed.
As comment of device_register() says, it should use put_device() to give up the reference in the error path, some resources is going to freed in pcmcia_release_dev(), so don't use error label, just return NULL after calling put_device().
Before device_initialize(), it can not call put_device(), so move the dev_set_name() before device_register().
--- drivers/pcmcia/ds.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/pcmcia/ds.c +10 -4
@@ -513,9 +513,6 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,/* by default don't allow DMA */ p_dev->dma_mask =0 ; p_dev->dev.dma_mask = &p_dev->dma_mask; -dev_set_name (&p_dev->dev,"%d.%d" , p_dev->socket->sock, p_dev->device_no); -if (!dev_name (&p_dev->dev)) - goto err_free; p_dev->devname =kasprintf (GFP_KERNEL ,"pcmcia%s" ,dev_name (&p_dev->dev));if (!p_dev->devname) goto err_free;
@@ -573,8 +570,17 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,pcmcia_device_query (p_dev); -if (device_register (&p_dev->dev)) +dev_set_name (&p_dev->dev,"%d.%d" , p_dev->socket->sock, p_dev->device_no); +if (!dev_name (&p_dev->dev)) goto err_put_ref; +if (device_register (&p_dev->dev)) { +mutex_lock (&s->ops_mutex); +list_del (&p_dev->socket_device_list); + s->device_count--; +mutex_unlock (&s->ops_mutex); +put_device (&p_dev->dev); +return NULL ; + }return p_dev;
Signature · 1 line
2.25.1
Dominik Brodowski
maintainerHi,
Am Sat, Nov 12, 2022 at 05:29:23PM +0800 schrieb Yang Yingliang:
Quoted 30 lines
If
device_register()returns error, the refcount offunction_configneed be put.Fixes 360b65b95bae ("[PATCH] pcmcia: make config_t independent, add reference counting")Signed-off-by Yang Yingliang <[email protected]>--- drivers/pcmcia/ds.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/pcmcia/ds.c +3
-1@@ -574,10 +574,12 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,pcmcia_device_query (p_dev);if (device_register (&p_dev->dev)) - goto err_unreg; + goto err_put_ref;return p_dev; + err_put_ref: +kref_put (&p_dev->function_config->ref, pcmcia_release_function); err_unreg:mutex_lock (&s->ops_mutex);list_del (&p_dev->socket_device_list);Signature · 1 line
2.25.1
Indeed, there's a reference leak here in this failure path. However, you rightly note in your subsequent patch:
Am Sat, Nov 12, 2022 at 05:29:24PM +0800 schrieb Yang Yingliang:
Quoted 4 lines
As comment of
device_register()says, it should useput_device()to give up the reference in the error path, some resources is going to freed inpcmcia_release_dev(), so don't use error label, just return NULL after callingput_device().
Therefore, I'd suggest to combine both things:
From: Yang Yingliang <[email protected]>
Subject: [PATCH] pcmcia: ds: fix refcount leak in pcmcia_device_add()
As the comment of device_register() says, it should use put_device() to give up the reference in the error path. Then, insofar resources will be freed in pcmcia_release_dev(), the error path is no longer needed. In particular, this means that the (previously missing) dropping of the reference to &p_dev->function_config->ref is now handled by pcmcia_release_dev().
drivers/pcmcia/ds.c +8 -2
@@ -573,8 +573,14 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,pcmcia_device_query (p_dev); -if (device_register (&p_dev->dev)) - goto err_unreg; +if (device_register (&p_dev->dev)) { +mutex_lock (&s->ops_mutex); +list_del (&p_dev->socket_device_list); + s->device_count--; +mutex_unlock (&s->ops_mutex); +put_device (&p_dev->dev); +return NULL ; + }return p_dev;
What do you think?
Best,
Dominik
Dominik Brodowski
maintainerRe: [PATCH 2/2] pcmcia: ds: fix possible name leak in error path in pcmcia_device_add()
In reply to Yang YingliangAm Sat, Nov 12, 2022 at 05:29:24PM +0800 schrieb Yang Yingliang:
Quoted 33 lines
Afer commit
1fa5ae857bb1("driver core: get rid of struct device'sbus_idstring array"), the name of device is allocated dynamically, it need bee freed.As comment of
device_register()says, it should useput_device()to give up the reference in the error path, some resources is going to freed inpcmcia_release_dev(), so don't use error label, just return NULL after callingput_device().Before
device_initialize(), it can not callput_device(), so move thedev_set_name()beforedevice_register().Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")Signed-off-by Yang Yingliang <[email protected]>--- drivers/pcmcia/ds.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)drivers/pcmcia/ds.c +0
-3@@ -513,9 +513,6 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,/* by default don't allow DMA */ p_dev->dma_mask =0 ; p_dev->dev.dma_mask = &p_dev->dma_mask; -dev_set_name (&p_dev->dev,"%d.%d" , p_dev->socket->sock, p_dev->device_no); -if (!dev_name (&p_dev->dev)) - goto err_free; p_dev->devname =kasprintf (GFP_KERNEL ,"pcmcia%s" ,dev_name (&p_dev->dev));if (!p_dev->devname) goto err_free;
drivers/pcmcia/ds.c static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, · line 576
Quoted 8 lines
drivers/pcmcia/ds.c +3
-1@@ -573,8 +570,17 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,pcmcia_device_query (p_dev); -if (device_register (&p_dev->dev)) +dev_set_name (&p_dev->dev,"%d.%d" , p_dev->socket->sock, p_dev->device_no); +if (!dev_name (&p_dev->dev)) goto err_put_ref; +if (device_register (&p_dev->dev)) {
Thanks for your patch! I totally see your point. However, err_put_ref puts the "wrong" reference (to &p_dev->function_config->ref), which doesn't help with the issue you detected, as that requires &p_dev->dev to be dropped. What about the following instead?
From: Yang Yingliang <[email protected]>
Subject: [PATCH] pcmcia: ds: fix possible name leak in error path in
pcmcia_device_add()
Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically. Therefore, it needs to be freed, which is done by the driver core for us once all references to the device are gone. Therefore, move the dev_set_name() call immediately before the call device_register(), which either succeeds (then the freeing will be done upon subsequent remvoal), or puts the reference in the error call. Also, it is not unusual that the return value of dev_set_name is not checked.
drivers/pcmcia/ds.c +1 -3
@@ -513,9 +513,6 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,/* by default don't allow DMA */ p_dev->dma_mask =0 ; p_dev->dev.dma_mask = &p_dev->dma_mask; -dev_set_name (&p_dev->dev,"%d.%d" , p_dev->socket->sock, p_dev->device_no); -if (!dev_name (&p_dev->dev)) - goto err_free; p_dev->devname =kasprintf (GFP_KERNEL ,"pcmcia%s" ,dev_name (&p_dev->dev));if (!p_dev->devname) goto err_free;
@@ -573,6 +570,7 @@ static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,pcmcia_device_query (p_dev); +dev_set_name (&p_dev->dev,"%d.%d" , p_dev->socket->sock, p_dev->device_no);if (device_register (&p_dev->dev)) {mutex_lock (&s->ops_mutex);list_del (&p_dev->socket_device_list);