* [PATCH] Bluetooth: hci_sync: fix memory leak in hci_update_adv_data()
@ 2023-01-09 1:26 Zhengchao Shao
2023-01-09 21:11 ` Luiz Augusto von Dentz
0 siblings, 1 reply; 3+ messages in thread
From: Zhengchao Shao @ 2023-01-09 1:26 UTC (permalink / raw)
To: linux-bluetooth, netdev, marcel, johan.hedberg, luiz.dentz,
davem, edumazet, kuba, pabeni
Cc: brian.gix, weiyongjun1, yuehaibing, shaozhengchao
When hci_cmd_sync_queue() failed in hci_update_adv_data(), inst_ptr is
not freed, which will cause memory leak. Add release process to error
path.
Fixes: 651cd3d65b0f ("Bluetooth: convert hci_update_adv_data to hci_sync")
Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
---
net/bluetooth/hci_sync.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index 9e2d7e4b850c..1485501bd72f 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -6197,10 +6197,15 @@ static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
{
u8 *inst_ptr = kmalloc(1, GFP_KERNEL);
+ int ret;
if (!inst_ptr)
return -ENOMEM;
*inst_ptr = instance;
- return hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
+ ret = hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
+ if (ret)
+ kfree(inst_ptr);
+
+ return ret;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] Bluetooth: hci_sync: fix memory leak in hci_update_adv_data()
2023-01-09 1:26 [PATCH] Bluetooth: hci_sync: fix memory leak in hci_update_adv_data() Zhengchao Shao
@ 2023-01-09 21:11 ` Luiz Augusto von Dentz
2023-01-10 6:18 ` shaozhengchao
0 siblings, 1 reply; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2023-01-09 21:11 UTC (permalink / raw)
To: Zhengchao Shao
Cc: linux-bluetooth, netdev, marcel, johan.hedberg, davem, edumazet,
kuba, pabeni, brian.gix, weiyongjun1, yuehaibing
Hi Zhengchao,
On Sun, Jan 8, 2023 at 5:17 PM Zhengchao Shao <shaozhengchao@huawei.com> wrote:
>
> When hci_cmd_sync_queue() failed in hci_update_adv_data(), inst_ptr is
> not freed, which will cause memory leak. Add release process to error
> path.
>
> Fixes: 651cd3d65b0f ("Bluetooth: convert hci_update_adv_data to hci_sync")
> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
> ---
> net/bluetooth/hci_sync.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
> index 9e2d7e4b850c..1485501bd72f 100644
> --- a/net/bluetooth/hci_sync.c
> +++ b/net/bluetooth/hci_sync.c
> @@ -6197,10 +6197,15 @@ static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
> int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
> {
> u8 *inst_ptr = kmalloc(1, GFP_KERNEL);
> + int ret;
>
> if (!inst_ptr)
> return -ENOMEM;
>
> *inst_ptr = instance;
> - return hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
> + ret = hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
> + if (ret)
> + kfree(inst_ptr);
> +
> + return ret;
> }
While this is correct I do wonder why we haven't used ERR_PTR/PTR_ERR
like we did with the likes of abort_conn_sync, that way we don't have
to allocate anything which makes things a lot simpler.
> --
> 2.34.1
>
--
Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Bluetooth: hci_sync: fix memory leak in hci_update_adv_data()
2023-01-09 21:11 ` Luiz Augusto von Dentz
@ 2023-01-10 6:18 ` shaozhengchao
0 siblings, 0 replies; 3+ messages in thread
From: shaozhengchao @ 2023-01-10 6:18 UTC (permalink / raw)
To: Luiz Augusto von Dentz
Cc: linux-bluetooth, netdev, marcel, johan.hedberg, davem, edumazet,
kuba, pabeni, brian.gix, weiyongjun1, yuehaibing
On 2023/1/10 5:11, Luiz Augusto von Dentz wrote:
> Hi Zhengchao,
>
> On Sun, Jan 8, 2023 at 5:17 PM Zhengchao Shao <shaozhengchao@huawei.com> wrote:
>>
>> When hci_cmd_sync_queue() failed in hci_update_adv_data(), inst_ptr is
>> not freed, which will cause memory leak. Add release process to error
>> path.
>>
>> Fixes: 651cd3d65b0f ("Bluetooth: convert hci_update_adv_data to hci_sync")
>> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
>> ---
>> net/bluetooth/hci_sync.c | 7 ++++++-
>> 1 file changed, 6 insertions(+), 1 deletion(-)
>>
>> diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
>> index 9e2d7e4b850c..1485501bd72f 100644
>> --- a/net/bluetooth/hci_sync.c
>> +++ b/net/bluetooth/hci_sync.c
>> @@ -6197,10 +6197,15 @@ static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
>> int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
>> {
>> u8 *inst_ptr = kmalloc(1, GFP_KERNEL);
>> + int ret;
>>
>> if (!inst_ptr)
>> return -ENOMEM;
>>
>> *inst_ptr = instance;
>> - return hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
>> + ret = hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
>> + if (ret)
>> + kfree(inst_ptr);
>> +
>> + return ret;
>> }
>
> While this is correct I do wonder why we haven't used ERR_PTR/PTR_ERR
> like we did with the likes of abort_conn_sync, that way we don't have
> to allocate anything which makes things a lot simpler.
>
Hi Luiz:
Thank you for your advice. I think it is better to use
ERR_PTR/PTR_ERR to replace allocation. I will send V2.
Zhengchao Shao
>> --
>> 2.34.1
>>
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-01-10 6:18 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-09 1:26 [PATCH] Bluetooth: hci_sync: fix memory leak in hci_update_adv_data() Zhengchao Shao
2023-01-09 21:11 ` Luiz Augusto von Dentz
2023-01-10 6:18 ` shaozhengchao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).