Skip to contentSkip to conversation

Bluetooth: HIDP: add missing length check for incoming frames

PATCH Superseded Jiale Yao 6 participants 9 messages Jul 17 – Aug 16

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jiale Yao

author
#0 lore raw

PATCH 1/1 Bluetooth: HIDP: add missing length check for incoming frames

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Add pskb_may_pull(skb, 1) guards before both reads, matching the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 4 ++++
 1 file changed, 4 insertions(+)
net/bluetooth/hidp/core.c +4 -0
index 0e24c5e2955e..6c7da8aca732 100644
@@ -565,6 +565,8 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 
 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+	if (!pskb_may_pull(skb, 1))
+		return;
 	hdr = skb->data[0];
 	skb_pull(skb, 1);
 
@@ -601,6 +603,8 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
 
 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+	if (!pskb_may_pull(skb, 1))
+		return;
 	hdr = skb->data[0];
 	skb_pull(skb, 1);
 
Signature · 1 line
2.34.1
#1 lore raw

Hi,

Quoted 21 lines · Jiale Yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Add pskb_may_pull(skb, 1) guards before both reads, matching the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 4 ++++
 1 file changed, 4 insertions(+)
net/bluetooth/hidp/core.c +0 -0
index 0e24c5e2955e..6c7da8aca732 100644

net/bluetooth/hidp/core.c static void hidp_recv_ctrl_frame(struct hidp_session *session, · line 569

Quoted 5 lines
net/bluetooth/hidp/core.c +2 -0
@@ -565,6 +565,8 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

We can probably replace this with skb_pull_data.

net/bluetooth/hidp/core.c static void hidp_recv_intr_frame(struct hidp_session *session, · line 607

Quoted 9 lines
net/bluetooth/hidp/core.c +2 -0
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
@@ -601,6 +603,8 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

Ditto.

Quoted 6 lines
net/bluetooth/hidp/core.c +0 -1
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
--

2.34.1

Signature · 1 line
Luiz Augusto von Dentz
#8 lore raw
In reply to Jiale Yao

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1129697

---Test result---

Test Summary:

CheckPatch                    PASS      0.58 seconds
VerifyFixes                   PASS      0.10 seconds
VerifySignedoff               PASS      0.11 seconds
GitLint                       PASS      0.27 seconds
SubjectPrefix                 PASS      0.16 seconds
BuildKernel                   PASS      22.57 seconds
CheckAllWarning               PASS      25.42 seconds
CheckSparse                   PASS      25.04 seconds
BuildKernel32                 PASS      22.34 seconds
CheckKernelLLVM               SKIP      0.00 seconds
TestRunnerSetup               PASS      408.54 seconds
TestRunner_l2cap-tester       PASS      56.29 seconds
TestRunner_iso-tester         PASS      78.41 seconds
TestRunner_bnep-tester        PASS      17.10 seconds
TestRunner_mgmt-tester        FAIL      204.30 seconds
TestRunner_rfcomm-tester      PASS      23.90 seconds
TestRunner_sco-tester         PASS      30.72 seconds
TestRunner_ioctl-tester       PASS      23.78 seconds
TestRunner_mesh-tester        FAIL      23.76 seconds
TestRunner_smp-tester         PASS      21.47 seconds
TestRunner_userchan-tester    PASS      18.22 seconds
TestRunner_6lowpan-tester     PASS      20.95 seconds
IncrementalBuild              PASS      21.80 seconds

Details
##############################
Test: CheckKernelLLVM - SKIP
Desc: Build kernel with LLVM + context analysis Output:
Clang not found
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4

Failed Test Cases
Read Exp Feature - Success Failed 0.232 seconds
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0

Failed Test Cases

Mesh - Send cancel - 1                               Timed out    1.977 seconds
Mesh - Send cancel - 2                               Timed out    1.988 seconds

https://github.com/bluez/bluetooth-next/pull/450

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Luiz Augusto von Dentz

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Use skb_pull_data(skb, 1) which combines the length check and pull in one operation, matching the existing pattern in hidp_input_report() and the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
net/bluetooth/hidp/core.c +12 -9
index 0e24c5e2955e..bb61602b02e1 100644
@@ -560,16 +560,18 @@ static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
 static void hidp_recv_ctrl_frame(struct hidp_session *session,
 					struct sk_buff *skb)
 {
-	unsigned char hdr, type, param;
+	unsigned char *hdr;
+	unsigned char type, param;
 	int free_skb = 1;
 
 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-	hdr = skb->data[0];
-	skb_pull(skb, 1);
+	hdr = skb_pull_data(skb, 1);
+	if (!hdr)
+		return;
 
-	type = hdr & HIDP_HEADER_TRANS_MASK;
-	param = hdr & HIDP_HEADER_PARAM_MASK;
+	type = *hdr & HIDP_HEADER_TRANS_MASK;
+	param = *hdr & HIDP_HEADER_PARAM_MASK;
 
 	switch (type) {
 	case HIDP_TRANS_HANDSHAKE:
@@ -597,14 +599,15 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 static void hidp_recv_intr_frame(struct hidp_session *session,
 				struct sk_buff *skb)
 {
-	unsigned char hdr;
+	unsigned char *hdr;
 
 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-	hdr = skb->data[0];
-	skb_pull(skb, 1);
+	hdr = skb_pull_data(skb, 1);
+	if (!hdr)
+		return;
 
-	if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
+	if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
 		hidp_set_timer(session);
 
 		if (session->input)
Signature · 1 line
2.34.1
Quoted 54 lines · Luiz Augusto von Dentz

Hi,

Quoted 21 lines · Jiale Yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Add pskb_may_pull(skb, 1) guards before both reads, matching the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 4 ++++
 1 file changed, 4 insertions(+)
net/bluetooth/hidp/core.c +0 -0
index 0e24c5e2955e..6c7da8aca732 100644

net/bluetooth/hidp/core.c static void hidp_recv_ctrl_frame(struct hidp_session *session, · line 569

Quoted 5 lines
net/bluetooth/hidp/core.c +2 -0
@@ -565,6 +565,8 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

We can probably replace this with skb_pull_data.

net/bluetooth/hidp/core.c static void hidp_recv_intr_frame(struct hidp_session *session, · line 607

Quoted 9 lines
net/bluetooth/hidp/core.c +2 -0
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
@@ -601,6 +603,8 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

Ditto.

Quoted 6 lines
net/bluetooth/hidp/core.c +0 -1
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
--

2.34.1

Signature · 1 line
Luiz Augusto von Dentz
#3 lore raw

Hi,

Quoted 33 lines · jiale yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Use skb_pull_data(skb, 1) which combines the length check and pull in one operation, matching the existing pattern in hidp_input_report() and the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
net/bluetooth/hidp/core.c +1 -1
index 0e24c5e2955e..bb61602b02e1 100644
@@ -560,16 +560,18 @@ static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
 static void hidp_recv_ctrl_frame(struct hidp_session *session,
                                        struct sk_buff *skb)
 {
-       unsigned char hdr, type, param;
+       unsigned char *hdr;

net/bluetooth/hidp/core.c static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb, · line 571

Quoted 10 lines
net/bluetooth/hidp/core.c +4 -2
+       unsigned char type, param;
        int free_skb = 1;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               return;

https://sashiko.dev/#/patchset/20260717153238.2002330-1-yaojiale02%40163.com

It seems that by early returning we are leaking the skb.

Quoted 97 lines
net/bluetooth/hidp/core.c +7 -7
-       type = hdr & HIDP_HEADER_TRANS_MASK;
-       param = hdr & HIDP_HEADER_PARAM_MASK;
+       type = *hdr & HIDP_HEADER_TRANS_MASK;
+       param = *hdr & HIDP_HEADER_PARAM_MASK;
 
        switch (type) {
        case HIDP_TRANS_HANDSHAKE:
@@ -597,14 +599,15 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 static void hidp_recv_intr_frame(struct hidp_session *session,
                                struct sk_buff *skb)
 {
-       unsigned char hdr;
+       unsigned char *hdr;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               return;
 
-       if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
+       if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
                hidp_set_timer(session);
 
                if (session->input)
--

2.34.1

Quoted 54 lines · Luiz Augusto von Dentz

Hi,

Quoted 21 lines · Jiale Yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Add pskb_may_pull(skb, 1) guards before both reads, matching the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 4 ++++
 1 file changed, 4 insertions(+)
net/bluetooth/hidp/core.c +0 -0
index 0e24c5e2955e..6c7da8aca732 100644

net/bluetooth/hidp/core.c static void hidp_recv_ctrl_frame(struct hidp_session *session, · line 569

Quoted 5 lines
net/bluetooth/hidp/core.c +2 -0
@@ -565,6 +565,8 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

We can probably replace this with skb_pull_data.

net/bluetooth/hidp/core.c static void hidp_recv_intr_frame(struct hidp_session *session, · line 607

Quoted 9 lines
net/bluetooth/hidp/core.c +2 -0
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
@@ -601,6 +603,8 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

Ditto.

Quoted 6 lines
net/bluetooth/hidp/core.c +0 -1
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
--

2.34.1

--
Luiz Augusto von Dentz

Signature · 1 line
Luiz Augusto von Dentz
#4 lore raw

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Use skb_pull_data(skb, 1) which combines the length check and pull in one operation, matching the existing pattern in hidp_input_report() and the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
net/bluetooth/hidp/core.c +15 -10
index 0e24c5e2955e..e29fe66f01d4 100644
@@ -560,16 +560,18 @@ static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
 static void hidp_recv_ctrl_frame(struct hidp_session *session,
 					struct sk_buff *skb)
 {
-	unsigned char hdr, type, param;
+	unsigned char *hdr;
+	unsigned char type, param;
 	int free_skb = 1;
 
 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-	hdr = skb->data[0];
-	skb_pull(skb, 1);
+	hdr = skb_pull_data(skb, 1);
+	if (!hdr)
+		goto failed;
 
-	type = hdr & HIDP_HEADER_TRANS_MASK;
-	param = hdr & HIDP_HEADER_PARAM_MASK;
+	type = *hdr & HIDP_HEADER_TRANS_MASK;
+	param = *hdr & HIDP_HEADER_PARAM_MASK;
 
 	switch (type) {
 	case HIDP_TRANS_HANDSHAKE:
@@ -590,6 +592,7 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 		break;
 	}
 
+failed:
 	if (free_skb)
 		kfree_skb(skb);
 }
@@ -597,14 +600,15 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 static void hidp_recv_intr_frame(struct hidp_session *session,
 				struct sk_buff *skb)
 {
-	unsigned char hdr;
+	unsigned char *hdr;
 
 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-	hdr = skb->data[0];
-	skb_pull(skb, 1);
+	hdr = skb_pull_data(skb, 1);
+	if (!hdr)
+		goto failed;
 
-	if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
+	if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
 		hidp_set_timer(session);
 
 		if (session->input)
@@ -616,9 +620,10 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
 			BT_DBG("report len %d", skb->len);
 		}
 	} else {
-		BT_DBG("Unsupported protocol header 0x%02x", hdr);
+		BT_DBG("Unsupported protocol header 0x%02x", *hdr);
 	}
 
+failed:
 	kfree_skb(skb);
 }
 
Signature · 1 line
2.34.1
Quoted 153 lines · Luiz Augusto von Dentz

Hi,

Quoted 33 lines · jiale yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Use skb_pull_data(skb, 1) which combines the length check and pull in one operation, matching the existing pattern in hidp_input_report() and the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
net/bluetooth/hidp/core.c +1 -1
index 0e24c5e2955e..bb61602b02e1 100644
@@ -560,16 +560,18 @@ static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
 static void hidp_recv_ctrl_frame(struct hidp_session *session,
                                        struct sk_buff *skb)
 {
-       unsigned char hdr, type, param;
+       unsigned char *hdr;

net/bluetooth/hidp/core.c static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb, · line 571

Quoted 10 lines
net/bluetooth/hidp/core.c +4 -2
+       unsigned char type, param;
        int free_skb = 1;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               return;

https://sashiko.dev/#/patchset/20260717153238.2002330-1-yaojiale02%40163.com

It seems that by early returning we are leaking the skb.

Quoted 97 lines
net/bluetooth/hidp/core.c +7 -7
-       type = hdr & HIDP_HEADER_TRANS_MASK;
-       param = hdr & HIDP_HEADER_PARAM_MASK;
+       type = *hdr & HIDP_HEADER_TRANS_MASK;
+       param = *hdr & HIDP_HEADER_PARAM_MASK;
 
        switch (type) {
        case HIDP_TRANS_HANDSHAKE:
@@ -597,14 +599,15 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 static void hidp_recv_intr_frame(struct hidp_session *session,
                                struct sk_buff *skb)
 {
-       unsigned char hdr;
+       unsigned char *hdr;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               return;
 
-       if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
+       if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
                hidp_set_timer(session);
 
                if (session->input)
--

2.34.1

Quoted 54 lines · Luiz Augusto von Dentz

Hi,

Quoted 21 lines · Jiale Yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Add pskb_may_pull(skb, 1) guards before both reads, matching the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 4 ++++
 1 file changed, 4 insertions(+)
net/bluetooth/hidp/core.c +0 -0
index 0e24c5e2955e..6c7da8aca732 100644

net/bluetooth/hidp/core.c static void hidp_recv_ctrl_frame(struct hidp_session *session, · line 569

Quoted 5 lines
net/bluetooth/hidp/core.c +2 -0
@@ -565,6 +565,8 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

We can probably replace this with skb_pull_data.

net/bluetooth/hidp/core.c static void hidp_recv_intr_frame(struct hidp_session *session, · line 607

Quoted 9 lines
net/bluetooth/hidp/core.c +2 -0
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
@@ -601,6 +603,8 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

Ditto.

Quoted 6 lines
net/bluetooth/hidp/core.c +0 -1
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
--

2.34.1

--
Luiz Augusto von Dentz

Signature · 1 line
Luiz Augusto von Dentz
Reviewed-by
#5 lore raw

Hi Jiale,

Looks correct. Both goto failed paths free the skb properly now, and the intr_frame goto also avoids a NULL hdr deref.

Thanks,
Muhammad Bilal

Quoted 248 lines · jiale yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Use skb_pull_data(skb, 1) which combines the length check and pull in one operation, matching the existing pattern in hidp_input_report() and the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
net/bluetooth/hidp/core.c +15 -11
index 0e24c5e2955e..e29fe66f01d4 100644
@@ -560,16 +560,18 @@ static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
 static void hidp_recv_ctrl_frame(struct hidp_session *session,
                                        struct sk_buff *skb)
 {
-       unsigned char hdr, type, param;
+       unsigned char *hdr;
+       unsigned char type, param;
        int free_skb = 1;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               goto failed;
 
-       type = hdr & HIDP_HEADER_TRANS_MASK;
-       param = hdr & HIDP_HEADER_PARAM_MASK;
+       type = *hdr & HIDP_HEADER_TRANS_MASK;
+       param = *hdr & HIDP_HEADER_PARAM_MASK;
 
        switch (type) {
        case HIDP_TRANS_HANDSHAKE:
@@ -590,6 +592,7 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
                break;
        }
 
+failed:
        if (free_skb)
                kfree_skb(skb);
 }
@@ -597,14 +600,15 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 static void hidp_recv_intr_frame(struct hidp_session *session,
                                struct sk_buff *skb)
 {
-       unsigned char hdr;
+       unsigned char *hdr;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               goto failed;
 
-       if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
+       if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
                hidp_set_timer(session);
 
                if (session->input)
@@ -616,9 +620,10 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
                        BT_DBG("report len %d", skb->len);
                }
        } else {
-               BT_DBG("Unsupported protocol header 0x%02x", hdr);
+               BT_DBG("Unsupported protocol header 0x%02x", *hdr);
        }
 
+failed:
        kfree_skb(skb);
 }
 
--

2.34.1

Quoted 153 lines · Luiz Augusto von Dentz

Hi,

Quoted 33 lines · jiale yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Use skb_pull_data(skb, 1) which combines the length check and pull in one operation, matching the existing pattern in hidp_input_report() and the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
net/bluetooth/hidp/core.c +1 -1
index 0e24c5e2955e..bb61602b02e1 100644
@@ -560,16 +560,18 @@ static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
 static void hidp_recv_ctrl_frame(struct hidp_session *session,
                                        struct sk_buff *skb)
 {
-       unsigned char hdr, type, param;
+       unsigned char *hdr;

net/bluetooth/hidp/core.c static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb, · line 571

Quoted 10 lines
net/bluetooth/hidp/core.c +4 -2
+       unsigned char type, param;
        int free_skb = 1;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               return;

https://sashiko.dev/#/patchset/20260717153238.2002330-1-yaojiale02%40163.com

It seems that by early returning we are leaking the skb.

Quoted 42 lines
net/bluetooth/hidp/core.c +7 -7
-       type = hdr & HIDP_HEADER_TRANS_MASK;
-       param = hdr & HIDP_HEADER_PARAM_MASK;
+       type = *hdr & HIDP_HEADER_TRANS_MASK;
+       param = *hdr & HIDP_HEADER_PARAM_MASK;
 
        switch (type) {
        case HIDP_TRANS_HANDSHAKE:
@@ -597,14 +599,15 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 static void hidp_recv_intr_frame(struct hidp_session *session,
                                struct sk_buff *skb)
 {
-       unsigned char hdr;
+       unsigned char *hdr;
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
-       hdr = skb->data[0];
-       skb_pull(skb, 1);
+       hdr = skb_pull_data(skb, 1);
+       if (!hdr)
+               return;
 
-       if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
+       if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
                hidp_set_timer(session);
 
                if (session->input)
--

2.34.1

Quoted 55 lines
Quoted 54 lines · Luiz Augusto von Dentz

Hi,

Quoted 21 lines · Jiale Yao

In hidp_recv_ctrl_frame() and hidp_recv_intr_frame(), skb->data[0] is read without verifying that skb->len >= 1. A zero-length L2CAP PDU delivered via the HIDP control or interrupt channel causes an out-of-bounds read.

Add pskb_may_pull(skb, 1) guards before both reads, matching the fix in commit 6770d3a8acdf ("Bluetooth: bnep: reject short frames before parsing") which addressed the same class of bug in BNEP.

Assisted-by Claude:deepseek-v4-pro
---
 net/bluetooth/hidp/core.c | 4 ++++
 1 file changed, 4 insertions(+)
net/bluetooth/hidp/core.c +0 -0
index 0e24c5e2955e..6c7da8aca732 100644

net/bluetooth/hidp/core.c static void hidp_recv_ctrl_frame(struct hidp_session *session, · line 569

Quoted 5 lines
net/bluetooth/hidp/core.c +2 -0
@@ -565,6 +565,8 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

We can probably replace this with skb_pull_data.

net/bluetooth/hidp/core.c static void hidp_recv_intr_frame(struct hidp_session *session, · line 607

Quoted 9 lines
net/bluetooth/hidp/core.c +2 -0
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
@@ -601,6 +603,8 @@ static void hidp_recv_intr_frame(struct hidp_session *session,
 
        BT_DBG("session %p skb %p len %u", session, skb, skb->len);
 
+       if (!pskb_may_pull(skb, 1))
+               return;

Ditto.

Quoted 6 lines
net/bluetooth/hidp/core.c +0 -1
        hdr = skb->data[0];
        skb_pull(skb, 1);
 
--

2.34.1

--
Luiz Augusto von Dentz

--
Luiz Augusto von Dentz

#6 lore raw
In reply to jiale yao
kernel test robot noticed the following build warnings:

Hi jiale,

kernel test robot noticed the following build warnings:

[auto build test WARNING on kees/for-next/pstore]
[also build test WARNING on kees/for-next/kspp]
[cannot apply to bluetooth-next/master bluetooth/master linus/master v7.2-rc7 next-20260812] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/jiale-yao/Re-Re-PATCH-v2-Bluetooth-HIDP-add-missing-length-check-for-incoming-frames/20260814-100503
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore
patch link:    https://lore.kernel.org/r/220b6be5.d3f.19f78e165aa.Coremail.19888972804%40163.com
patch subject: Re:Re: [PATCH v2] Bluetooth: HIDP: add missing length check for incoming frames
config: powerpc-allmodconfig (https://download.01.org/0day-ci/archive/20260814/[email protected]/config)
compiler: powerpc64-linux-gcc (GCC) 16.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260814/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from include/asm-generic/bug.h:31,
from arch/powerpc/include/asm/bug.h:116, from include/linux/bug.h:5,
from include/linux/thread_info.h:13, from include/asm-generic/preempt.h:5, from arch/powerpc/include/asm/preempt.h:5, from include/linux/preempt.h:79, from include/linux/spinlock.h:56, from include/linux/kref.h:16,
from net/bluetooth/hidp/core.c:24:
net/bluetooth/hidp/core.c: In function 'hidp_recv_intr_frame':

>> net/bluetooth/hidp/core.c:612:24: warning: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'unsigned char *' [-Wformat=]
     612 |                 BT_DBG("Unsupported protocol header 0x%02x", hdr);
         |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/printk.h:401:21: note: in definition of macro 'pr_fmt'
     401 | #define pr_fmt(fmt) fmt
         |                     ^~~
   include/linux/dynamic_debug.h:259:9: note: in expansion of macro '__dynamic_func_call_cls'
     259 |         __dynamic_func_call_cls(__UNIQUE_ID(ddebug), cls, fmt, func, ##__VA_ARGS__)
         |         ^~~~~~~~~~~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:261:9: note: in expansion of macro '_dynamic_func_call_cls'
     261 |         _dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
         |         ^~~~~~~~~~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:280:9: note: in expansion of macro '_dynamic_func_call'
     280 |         _dynamic_func_call(fmt, __dynamic_pr_debug,             \
         |         ^~~~~~~~~~~~~~~~~~
   include/linux/printk.h:635:9: note: in expansion of macro 'dynamic_pr_debug'
     635 |         dynamic_pr_debug(fmt, ##__VA_ARGS__)
         |         ^~~~~~~~~~~~~~~~
   include/net/bluetooth/bluetooth.h:287:33: note: in expansion of macro 'pr_debug'
     287 | #define BT_DBG(fmt, ...)        pr_debug(fmt "\n", ##__VA_ARGS__)
         |                                 ^~~~~~~~
   net/bluetooth/hidp/core.c:612:17: note: in expansion of macro 'BT_DBG'
     612 |                 BT_DBG("Unsupported protocol header 0x%02x", hdr);
         |                 ^~~~~~
   net/bluetooth/hidp/core.c:612:58: note: format string is defined here
     612 |                 BT_DBG("Unsupported protocol header 0x%02x", hdr);
         |                                                       ~~~^
         |                                                          |
         |                                                          unsigned int
         |                                                       %02hhn

vim +612 net/bluetooth/hidp/core.c

^1da177e4c3f415 Linus Torvalds   2005-04-16  588  
91f5cca3d1b4341 Andrew Morton    2008-02-05  589  static void hidp_recv_intr_frame(struct hidp_session *session,
91f5cca3d1b4341 Andrew Morton    2008-02-05  590  				struct sk_buff *skb)
^1da177e4c3f415 Linus Torvalds   2005-04-16  591  {
afd2c613d3369fb jiale yao        2026-07-19  592  	unsigned char *hdr;
^1da177e4c3f415 Linus Torvalds   2005-04-16  593  
093dabb4f1aff98 Kai Ye           2021-06-03  594  	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
^1da177e4c3f415 Linus Torvalds   2005-04-16  595  
afd2c613d3369fb jiale yao        2026-07-19  596  	hdr = skb_pull_data(skb, 1);
afd2c613d3369fb jiale yao        2026-07-19  597  	if (!hdr)
afd2c613d3369fb jiale yao        2026-07-19  598  		return;
^1da177e4c3f415 Linus Torvalds   2005-04-16  599  
afd2c613d3369fb jiale yao        2026-07-19  600  	if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
^1da177e4c3f415 Linus Torvalds   2005-04-16  601  		hidp_set_timer(session);
e1aaadd4d8162a2 Marcel Holtmann  2007-02-17  602  
^1da177e4c3f415 Linus Torvalds   2005-04-16  603  		if (session->input)
^1da177e4c3f415 Linus Torvalds   2005-04-16  604  			hidp_input_report(session, skb);
e1aaadd4d8162a2 Marcel Holtmann  2007-02-17  605  
e1aaadd4d8162a2 Marcel Holtmann  2007-02-17  606  		if (session->hid) {
a4b1b5877b514b2 David Rheinsberg 2013-12-19  607  			hidp_process_report(session, HID_INPUT_REPORT,
a4b1b5877b514b2 David Rheinsberg 2013-12-19  608  					    skb->data, skb->len, 1);
e1aaadd4d8162a2 Marcel Holtmann  2007-02-17  609  			BT_DBG("report len %d", skb->len);
e1aaadd4d8162a2 Marcel Holtmann  2007-02-17  610  		}
^1da177e4c3f415 Linus Torvalds   2005-04-16  611  	} else {
^1da177e4c3f415 Linus Torvalds   2005-04-16 @612  		BT_DBG("Unsupported protocol header 0x%02x", hdr);
^1da177e4c3f415 Linus Torvalds   2005-04-16  613  	}
^1da177e4c3f415 Linus Torvalds   2005-04-16  614  
^1da177e4c3f415 Linus Torvalds   2005-04-16  615  	kfree_skb(skb);
^1da177e4c3f415 Linus Torvalds   2005-04-16  616  }
^1da177e4c3f415 Linus Torvalds   2005-04-16  617  

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#7 lore raw
In reply to jiale yao
kernel test robot noticed the following build warnings:

Hi jiale,

kernel test robot noticed the following build warnings:

[auto build test WARNING on kees/for-next/pstore]
[also build test WARNING on kees/for-next/kspp]
[cannot apply to bluetooth-next/master bluetooth/master linus/master v7.2-rc7 next-20260814] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/jiale-yao/Re-Re-PATCH-v2-Bluetooth-HIDP-add-missing-length-check-for-incoming-frames/20260814-100503
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore
patch link:    https://lore.kernel.org/r/220b6be5.d3f.19f78e165aa.Coremail.19888972804%40163.com
patch subject: Re:Re: [PATCH v2] Bluetooth: HIDP: add missing length check for incoming frames
config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20260816/[email protected]/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 844a18e753e822736c9805ab779144b647a2c186)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260816/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> net/bluetooth/hidp/core.c:612:48: warning: format specifies type 'unsigned int' but the argument has type 'unsigned char *' [-Wformat]
     612 |                 BT_DBG("Unsupported protocol header 0x%02x", hdr);
         |                                                       ~~~~   ^~~
         |                                                       %2s
   include/net/bluetooth/bluetooth.h:287:47: note: expanded from macro 'BT_DBG'
     287 | #define BT_DBG(fmt, ...)        pr_debug(fmt "\n", ##__VA_ARGS__)
         |                                          ~~~         ^~~~~~~~~~~
   include/linux/printk.h:641:38: note: expanded from macro 'pr_debug'
     641 |         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
         |                                     ~~~     ^~~~~~~~~~~
   include/linux/printk.h:134:18: note: expanded from macro 'no_printk'
     134 |                 _printk(fmt, ##__VA_ARGS__);            \
         |                         ~~~    ^~~~~~~~~~~

1 warning generated.

vim +612 net/bluetooth/hidp/core.c

^1da177e4c3f41 Linus Torvalds   2005-04-16  588  
91f5cca3d1b434 Andrew Morton    2008-02-05  589  static void hidp_recv_intr_frame(struct hidp_session *session,
91f5cca3d1b434 Andrew Morton    2008-02-05  590  				struct sk_buff *skb)
^1da177e4c3f41 Linus Torvalds   2005-04-16  591  {
afd2c613d3369f jiale yao        2026-07-19  592  	unsigned char *hdr;
^1da177e4c3f41 Linus Torvalds   2005-04-16  593  
093dabb4f1aff9 Kai Ye           2021-06-03  594  	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
^1da177e4c3f41 Linus Torvalds   2005-04-16  595  
afd2c613d3369f jiale yao        2026-07-19  596  	hdr = skb_pull_data(skb, 1);
afd2c613d3369f jiale yao        2026-07-19  597  	if (!hdr)
afd2c613d3369f jiale yao        2026-07-19  598  		return;
^1da177e4c3f41 Linus Torvalds   2005-04-16  599  
afd2c613d3369f jiale yao        2026-07-19  600  	if (*hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
^1da177e4c3f41 Linus Torvalds   2005-04-16  601  		hidp_set_timer(session);
e1aaadd4d8162a Marcel Holtmann  2007-02-17  602  
^1da177e4c3f41 Linus Torvalds   2005-04-16  603  		if (session->input)
^1da177e4c3f41 Linus Torvalds   2005-04-16  604  			hidp_input_report(session, skb);
e1aaadd4d8162a Marcel Holtmann  2007-02-17  605  
e1aaadd4d8162a Marcel Holtmann  2007-02-17  606  		if (session->hid) {
a4b1b5877b514b David Rheinsberg 2013-12-19  607  			hidp_process_report(session, HID_INPUT_REPORT,
a4b1b5877b514b David Rheinsberg 2013-12-19  608  					    skb->data, skb->len, 1);
e1aaadd4d8162a Marcel Holtmann  2007-02-17  609  			BT_DBG("report len %d", skb->len);
e1aaadd4d8162a Marcel Holtmann  2007-02-17  610  		}
^1da177e4c3f41 Linus Torvalds   2005-04-16  611  	} else {
^1da177e4c3f41 Linus Torvalds   2005-04-16 @612  		BT_DBG("Unsupported protocol header 0x%02x", hdr);
^1da177e4c3f41 Linus Torvalds   2005-04-16  613  	}
^1da177e4c3f41 Linus Torvalds   2005-04-16  614  
^1da177e4c3f41 Linus Torvalds   2005-04-16  615  	kfree_skb(skb);
^1da177e4c3f41 Linus Torvalds   2005-04-16  616  }
^1da177e4c3f41 Linus Torvalds   2005-04-16  617  

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki