Skip to contentSkip to conversation

net: bluetooth: Fix null pointer dereference in hci_event_packet()

PATCH In review Anmol Karn 4 participants 12 messages Sep 10, 2020 – Oct 1, 2020

Subsystems (4)
lore
Share & tools
lore

Anmol Karn

author
#0 lore raw

PATCH 1/1 net: bluetooth: Fix null pointer dereference in hci_event_packet()

Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr' as NULL. Fix it by adding pointer check for it.

---
 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +5 -0
index 4b7fc430793c..871e16804433 100644
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {
+		hci_dev_unlock(hdev);
+		return;
+	}
+
 	if (ev->status) {
 		hci_conn_del(hcon);
 		hci_dev_unlock(hdev);
Signature · 5 lines
2.28.0
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw
Quoted 15 lines · Anmol Karn

Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr' as NULL. Fix it by adding pointer check for it.

---
 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..871e16804433 100644

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4943

Quoted 8 lines
net/bluetooth/hci_event.c +5 -0
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {
+		hci_dev_unlock(hdev);
+		return;
+	}
+

In patches that fix a NULL pointer dereference, please include a brief explanation of why the pointer can be NULL, including what it means semantically; and why the proposed change is the best fix for the problem.

Also, why IS_ERR_OR_NULL()?

- Eric
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author
#2 lore raw
Quoted 19 lines · Eric Biggers
Quoted 18 lines · Anmol Karn

Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr' as NULL. Fix it by adding pointer check for it.

---
 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..871e16804433 100644
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
Quoted 6 lines

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4943

Quoted 5 lines
net/bluetooth/hci_event.c +5 -0
+	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {
+		hci_dev_unlock(hdev);
+		return;
+	}
+

Hello Sir,

Quoted 4 lines

In patches that fix a NULL pointer dereference, please include a brief explanation of why the pointer can be NULL, including what it means semantically; and why the proposed change is the best fix for the problem.

I will surely add more explaination in v2.

Quoted 2 lines

Also, why IS_ERR_OR_NULL()?

I used IS_ERR_OR_NULL() to check if the 'hcon->amp_mgr' is a valid pointer or not, and unregister the 'hcon' and signal error, but will make changes in v2 with only NULL check included, if you think it's incorrect to use IS_ERR check here along with NULL.

Thanks,
Anmol Karn
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#3 lore raw
In reply to Anmol Karn
Quoted 15 lines · Anmol Karn

Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr' as NULL. Fix it by adding pointer check for it.

---
 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..871e16804433 100644

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4939

Quoted 4 lines
net/bluetooth/hci_event.c +1 -0
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {

It can't be an error pointer. Shouldn't we call hci_conn_del() on this path? Try to find the Fixes tag to explain how this bug was introduced.

(Don't rush to send a v2. The patch requires quite a bit more digging and detective work before it is ready).

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4946

Quoted 7 lines
net/bluetooth/hci_event.c +4 -0
+		hci_dev_unlock(hdev);
+		return;
+	}
+
 	if (ev->status) {
 		hci_conn_del(hcon);
 		hci_dev_unlock(hdev);

regards,
dan carpenter

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author
#4 lore raw
Quoted 27 lines · Dan Carpenter
Quoted 15 lines · Anmol Karn

Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr' as NULL. Fix it by adding pointer check for it.

---
 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..871e16804433 100644

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4939

Quoted 4 lines
net/bluetooth/hci_event.c +1 -0
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {

It can't be an error pointer. Shouldn't we call hci_conn_del() on this path? Try to find the Fixes tag to explain how this bug was introduced.

(Don't rush to send a v2. The patch requires quite a bit more digging and detective work before it is ready).

Quoted 11 lines

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4946

Quoted 7 lines
net/bluetooth/hci_event.c +4 -0
+		hci_dev_unlock(hdev);
+		return;
+	}
+
 	if (ev->status) {
 		hci_conn_del(hcon);
 		hci_dev_unlock(hdev);

regards,
dan carpenter

Sure sir, will work on it, thanks for your review.

Anmol Karn
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author
#5 lore raw
In reply to Dan Carpenter
Quoted 27 lines · Dan Carpenter
Quoted 15 lines · Anmol Karn

Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr' as NULL. Fix it by adding pointer check for it.

---
 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..871e16804433 100644

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4939

Quoted 4 lines
net/bluetooth/hci_event.c +1 -0
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {

It can't be an error pointer. Shouldn't we call hci_conn_del() on this path? Try to find the Fixes tag to explain how this bug was introduced.

(Don't rush to send a v2. The patch requires quite a bit more digging and detective work before it is ready).

Quoted 11 lines

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4946

Quoted 7 lines
net/bluetooth/hci_event.c +4 -0
+		hci_dev_unlock(hdev);
+		return;
+	}
+
 	if (ev->status) {
 		hci_conn_del(hcon);
 		hci_dev_unlock(hdev);

regards,
dan carpenter

Sir,

I need little advice in continuing with this Patch,

I have looked into the Bisected logs and the problem occurs from this commit:

941992d29447 ("ethernet: amd: use IS_ENABLED() instead of checking for built-in or module")

Here is a diff of patch which i modified from last patch,

net/bluetooth/hci_event.c +6 -0
index 4b7fc430793c..6ce435064e0b 100644
@@ -4936,6 +4936,12 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
                return;
        }
 
+       if (!hcon->amp_mgr) {
+               hci_conn_del(hcon);
+               hci_dev_unlock(hdev);
+               return;
+       }
+
        if (ev->status) {
                hci_conn_del(hcon);
                hci_dev_unlock(hdev);

The value of 'hcon->amp_mgr' getting NULL due to hci_conn_hash_lookup_handle call , and there is not any checks there for the members of hcon, which enables hci_phy_link_complete_evt() to dereference 'hcon->amp_mgr' as NULL.

please suggest improvements to this patch.

Regards,
Anmol
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#6 lore raw
Quoted 40 lines · Anmol Karn
Quoted 27 lines · Dan Carpenter
Quoted 15 lines · Anmol Karn

Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr' as NULL. Fix it by adding pointer check for it.

---
 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..871e16804433 100644

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4939

Quoted 4 lines
net/bluetooth/hci_event.c +1 -0
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {

It can't be an error pointer. Shouldn't we call hci_conn_del() on this path? Try to find the Fixes tag to explain how this bug was introduced.

(Don't rush to send a v2. The patch requires quite a bit more digging and detective work before it is ready).

Quoted 11 lines

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4947

Quoted 7 lines
net/bluetooth/hci_event.c +4 -0
+		hci_dev_unlock(hdev);
+		return;
+	}
+
 	if (ev->status) {
 		hci_conn_del(hcon);
 		hci_dev_unlock(hdev);

regards,
dan carpenter

Quoted 8 lines

Sir,

I need little advice in continuing with this Patch,

I have looked into the Bisected logs and the problem occurs from this commit:

941992d29447 ("ethernet: amd: use IS_ENABLED() instead of checking for built-in or module")

That's just the patch which made the code testable by syzbot. It didn't introduce the bug.

Quoted 8 lines

Here is a diff of patch which i modified from last patch,

net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..6ce435064e0b 100644

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4941

Quoted 6 lines
net/bluetooth/hci_event.c +3 -0
@@ -4936,6 +4936,12 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
                return;
        }
 
+       if (!hcon->amp_mgr) {
+               hci_conn_del(hcon);
+               hci_dev_unlock(hdev);

I have no idea if calling hci_conn_del() is really the correct, thing. I don't know the code at all. Anyway, do some research and figure out for sure what the correct thing is.

Also look for similar bugs in other places where hcon->amp_mgr is dereferenced. For example, amp_read_loc_assoc_final_data() seems to have a similar bug.

regards,
dan carpenter

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author
#7 lore raw

Hello Sir,

Quoted 19 lines
Quoted 4 lines

I have looked into the Bisected logs and the problem occurs from this commit:

941992d29447 ("ethernet: amd: use IS_ENABLED() instead of checking for built-in or module")

That's just the patch which made the code testable by syzbot. It didn't introduce the bug.

Quoted 11 lines

Here is a diff of patch which i modified from last patch,

net/bluetooth/hci_event.c +0 -0
index 4b7fc430793c..6ce435064e0b 100644
@@ -4936,6 +4936,12 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
                return;
        }
 
Quoted 7 lines

net/bluetooth/hci_event.c static void hci_phy_link_complete_evt(struct hci_dev *hdev, · line 4941

Quoted 3 lines
net/bluetooth/hci_event.c +3 -0
+       if (!hcon->amp_mgr) {
+               hci_conn_del(hcon);
+               hci_dev_unlock(hdev);

I have no idea if calling hci_conn_del() is really the correct, thing. I don't know the code at all. Anyway, do some research and figure out for sure what the correct thing is.

I have created my patch on the basis of the already applied conditions handling in this function, i.e whenever NULL dereference occurs, connection cleanup is required hence, hci_conn_del() is used here. Will see if anything else could be done.

Quoted 5 lines

Also look for similar bugs in other places where hcon->amp_mgr is dereferenced. For example, amp_read_loc_assoc_final_data() seems to have a similar bug.

Sure sir will look into it.

Quoted 3 lines

regards,
dan carpenter

Thanks,
Anmol
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author
#8 lore raw

PATCH 1/1 net: bluetooth: Fix null pointer dereference in hci_event_packet()

In reply to Dan Carpenter

AMP_MGR is getting derefernced in hci_phy_link_complete_evt(), when called from hci_event_packet() and there is a possibility, that hcon->amp_mgr may not be found when accessing after initialization of hcon.

- net/bluetooth/hci_event.c:4945
The bug seems to get triggered in this line:

bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;

Fix it by adding a NULL check for the hcon->amp_mgr before checking the ev-status.

Fixes d5e911928bd8 ("Bluetooth: AMP: Process Physical Link Complete evt")
Reported-and-tested-by [email protected]

---
Cahnge in v2:
- Replaced IS_ERR_OR_NULL check with NULL check only (Suggested by: Dan Carpenter <[email protected]>)
- Added "Fixes:" tag (Suggested by: Dan Carpenter <[email protected]>)

 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +5 -0
index 4b7fc430793c..b084142c578e 100644
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (!hcon->amp_mgr) {
+		hci_dev_unlock(hdev);
+		return 0;
+	}
+
 	if (ev->status) {
 		hci_conn_del(hcon);
 		hci_dev_unlock(hdev);
Signature · 1 line
2.28.0
author
#9 lore raw

PATCH 1/1 net: bluetooth: Fix null pointer dereference in hci_event_packet()

AMP_MGR is getting derefernced in hci_phy_link_complete_evt(), when called from hci_event_packet() and there is a possibility, that hcon->amp_mgr may not be found when accessing after initialization of hcon.

- net/bluetooth/hci_event.c:4945
The bug seems to get triggered in this line:

bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;

Fix it by adding a NULL check for the hcon->amp_mgr before checking the ev-status.

Fixes d5e911928bd8 ("Bluetooth: AMP: Process Physical Link Complete evt")
Reported-and-tested-by [email protected]

---
Change in v3:
- changed return o; to return; (Reported-by: kernel test robot <[email protected]>
)

 net/bluetooth/hci_event.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/hci_event.c +5 -0
index 4b7fc430793c..bbe0ca42cad2 100644
@@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 		return;
 	}
 
+	if (!hcon->amp_mgr) {
+		hci_dev_unlock(hdev);
+		return;
+	}
+
 	if (ev->status) {
 		hci_conn_del(hcon);
 		hci_dev_unlock(hdev);
Signature · 1 line
2.28.0
maintainer
#10 lore raw

Hi Anmol,

Quoted 13 lines

AMP_MGR is getting derefernced in hci_phy_link_complete_evt(), when called from hci_event_packet() and there is a possibility, that hcon->amp_mgr may not be found when accessing after initialization of hcon.

- net/bluetooth/hci_event.c:4945
The bug seems to get triggered in this line:

bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;

Fix it by adding a NULL check for the hcon->amp_mgr before checking the ev-status.

Fixes d5e911928bd8 ("Bluetooth: AMP: Process Physical Link Complete evt")
Reported-and-tested-by [email protected]
Quoted 7 lines

---
Change in v3:
- changed return o; to return; (Reported-by: kernel test robot <[email protected]>
)

net/bluetooth/hci_event.c | 5 +++++

1 file changed, 5 insertions(+)

patch has been applied to bluetooth-next tree.

Regards

Marcel

author
#11 lore raw
Quoted 20 lines · Marcel Holtmann

Hi Anmol,

Quoted 18 lines

AMP_MGR is getting derefernced in hci_phy_link_complete_evt(), when called from hci_event_packet() and there is a possibility, that hcon->amp_mgr may not be found when accessing after initialization of hcon.

- net/bluetooth/hci_event.c:4945
The bug seems to get triggered in this line:

bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;

Fix it by adding a NULL check for the hcon->amp_mgr before checking the ev-status.

Fixes d5e911928bd8 ("Bluetooth: AMP: Process Physical Link Complete evt")
Reported-and-tested-by [email protected]

---
Change in v3:
- changed return o; to return; (Reported-by: kernel test robot <[email protected]>
)

Quoted 9 lines
Quoted 2 lines

net/bluetooth/hci_event.c | 5 +++++

1 file changed, 5 insertions(+)

patch has been applied to bluetooth-next tree.

Regards

Marcel

Thank you :)

Best,
Anmol
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees