diff mbox series

[v2] Bluetooth: hci_sync: fix memory leak in hci_update_adv_data()

Message ID 20230110064420.3409168-1-shaozhengchao@huawei.com (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series [v2] Bluetooth: hci_sync: fix memory leak in hci_update_adv_data() | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 22 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

shaozhengchao Jan. 10, 2023, 6:44 a.m. UTC
When hci_cmd_sync_queue() failed in hci_update_adv_data(), inst_ptr is
not freed, which will cause memory leak. ERR_PTR/PTR_ERR is used to
replace memory allocation to simplify code.

Fixes: 651cd3d65b0f ("Bluetooth: convert hci_update_adv_data to hci_sync")
Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
---
v2: Use ERR_PTR/PTR_ERR to replace memory allocation
---
 net/bluetooth/hci_sync.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

Comments

shaozhengchao Jan. 10, 2023, 7:05 a.m. UTC | #1
Sorry for Repeatedly senting. Please ignore this patch.

On 2023/1/10 14:44, Zhengchao Shao wrote:
> When hci_cmd_sync_queue() failed in hci_update_adv_data(), inst_ptr is
> not freed, which will cause memory leak. ERR_PTR/PTR_ERR is used to
> replace memory allocation to simplify code.
> 
> Fixes: 651cd3d65b0f ("Bluetooth: convert hci_update_adv_data to hci_sync")
> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
> ---
> v2: Use ERR_PTR/PTR_ERR to replace memory allocation
> ---
>   net/bluetooth/hci_sync.c | 10 ++--------
>   1 file changed, 2 insertions(+), 8 deletions(-)
> 
> diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
> index 9e2d7e4b850c..8744bbecac9e 100644
> --- a/net/bluetooth/hci_sync.c
> +++ b/net/bluetooth/hci_sync.c
> @@ -6187,20 +6187,14 @@ int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
>   
>   static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
>   {
> -	u8 instance = *(u8 *)data;
> -
> -	kfree(data);
> +	u8 instance = PTR_ERR(data);
>   
>   	return hci_update_adv_data_sync(hdev, instance);
>   }
>   
>   int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
>   {
> -	u8 *inst_ptr = kmalloc(1, GFP_KERNEL);
> -
> -	if (!inst_ptr)
> -		return -ENOMEM;
> +	u8 *inst_ptr = ERR_PTR(instance);
>   
> -	*inst_ptr = instance;
>   	return hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
>   }
diff mbox series

Patch

diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index 9e2d7e4b850c..8744bbecac9e 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -6187,20 +6187,14 @@  int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
 
 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
 {
-	u8 instance = *(u8 *)data;
-
-	kfree(data);
+	u8 instance = PTR_ERR(data);
 
 	return hci_update_adv_data_sync(hdev, instance);
 }
 
 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
 {
-	u8 *inst_ptr = kmalloc(1, GFP_KERNEL);
-
-	if (!inst_ptr)
-		return -ENOMEM;
+	u8 *inst_ptr = ERR_PTR(instance);
 
-	*inst_ptr = instance;
 	return hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);
 }