* [PATCH v2] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
2026-05-19 4:20 [PATCH] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame Muhammad Bilal
@ 2026-05-19 7:25 ` Muhammad Bilal
2026-05-19 13:51 ` [PATCH] " Luiz Augusto von Dentz
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Muhammad Bilal @ 2026-05-19 7:25 UTC (permalink / raw)
To: linux-bluetooth
Cc: netdev, linux-kernel, marcel, luiz.dentz, kees, kuba, stable,
Muhammad Bilal
rfcomm_recv_frame() casts skb->data to struct rfcomm_hdr * and
immediately dereferences hdr->addr and hdr->ctrl without first
validating that skb->len is large enough to hold the header. A
remote device can send a crafted short RFCOMM frame over L2CAP to
trigger an out-of-bounds read before any session state is checked.
The FCS trimming code that follows compounds the problem:
skb->len--; skb->tail--;
If skb->len is already zero the decrement wraps to UINT_MAX, causing
skb_tail_pointer() to return a pointer far outside the skb and
producing a second out-of-bounds read when the FCS byte is consumed.
Add a minimum length check before the header pointer is assigned. A
well-formed RFCOMM frame requires at least addr(1) + ctrl(1) +
len(1) + fcs(1) = sizeof(struct rfcomm_hdr) + 1 bytes. This single
guard prevents both the header out-of-bounds read and the skb->len
integer underflow.
Note: SeungJu Cheon posted a related patch that adds equivalent
length checks inside the individual MCC sub-handlers
(rfcomm_recv_pn, rfcomm_recv_rpn, rfcomm_recv_rls, rfcomm_recv_msc,
rfcomm_recv_mcc). That fix and this one are complementary and
independent; neither subsumes the other.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Muhammad Bilal <meatuni001@gmail.com>
---
net/bluetooth/rfcomm/core.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index d11bd5337..6b300237c 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -1741,7 +1741,7 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk
static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
struct sk_buff *skb)
{
- struct rfcomm_hdr *hdr = (void *) skb->data;
+ struct rfcomm_hdr *hdr;
u8 type, dlci, fcs;
if (!s) {
@@ -1750,10 +1750,17 @@ static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
return s;
}
+ /* Minimum valid frame: addr(1) + ctrl(1) + len(1) + fcs(1) */
+ if (skb->len < sizeof(*hdr) + 1) {
+ kfree_skb(skb);
+ return s;
+ }
+
+ hdr = (void *) skb->data;
dlci = __get_dlci(hdr->addr);
type = __get_type(hdr->ctrl);
- /* Trim FCS */
+ /* Trim FCS - safe: skb->len >= sizeof(*hdr) + 1 >= 1 */
skb->len--; skb->tail--;
fcs = *(u8 *)skb_tail_pointer(skb);
--
2.54.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
2026-05-19 4:20 [PATCH] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame Muhammad Bilal
2026-05-19 7:25 ` [PATCH v2] " Muhammad Bilal
@ 2026-05-19 13:51 ` Luiz Augusto von Dentz
2026-05-19 18:17 ` [PATCH v3] " Muhammad Bilal
2026-05-19 18:48 ` [PATCH v4] " Muhammad Bilal
3 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2026-05-19 13:51 UTC (permalink / raw)
To: Muhammad Bilal
Cc: linux-bluetooth, netdev, linux-kernel, Marcel Holtmann, Kees Cook,
Jakub Kicinski
Hi Muhammad,
On Tue, May 19, 2026 at 12:20 AM Muhammad Bilal <meatuni001@gmail.com> wrote:
>
> rfcomm_recv_frame() casts skb->data to struct rfcomm_hdr * and
> immediately dereferences hdr->addr and hdr->ctrl without first
> validating that skb->len is large enough to hold the header. A
> remote device can send a crafted short RFCOMM frame over L2CAP to
> trigger an out-of-bounds read before any session state is checked.
>
> The FCS trimming code that follows compounds the problem:
>
> skb->len--; skb->tail--;
>
> If skb->len is already zero the decrement wraps to UINT_MAX, causing
> skb_tail_pointer() to return a pointer far outside the skb and
> producing a second out-of-bounds read when the FCS byte is consumed.
>
> Add a minimum length check before the header pointer is assigned. A
> well-formed RFCOMM frame requires at least addr(1) + ctrl(1) +
> len(1) + fcs(1) = sizeof(struct rfcomm_hdr) + 1 bytes. This single
> guard prevents both the header out-of-bounds read and the skb->len
> integer underflow.
>
> Note: SeungJu Cheon posted a related patch that adds equivalent
> length checks inside the individual MCC sub-handlers
> (rfcomm_recv_pn, rfcomm_recv_rpn, rfcomm_recv_rls, rfcomm_recv_msc,
> rfcomm_recv_mcc). That fix and this one are complementary and
> independent; neither subsumes the other.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Muhammad Bilal <meatuni001@gmail.com>
> ---
> net/bluetooth/rfcomm/core.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
> index d11bd5337..6b300237c 100644
> --- a/net/bluetooth/rfcomm/core.c
> +++ b/net/bluetooth/rfcomm/core.c
> @@ -1741,7 +1741,7 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk
> static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
> struct sk_buff *skb)
> {
> - struct rfcomm_hdr *hdr = (void *) skb->data;
> + struct rfcomm_hdr *hdr;
> u8 type, dlci, fcs;
>
> if (!s) {
> @@ -1750,10 +1750,17 @@ static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
> return s;
> }
>
> + /* Minimum valid frame: addr(1) + ctrl(1) + len(1) + fcs(1) */
> + if (skb->len < sizeof(*hdr) + 1) {
> + kfree_skb(skb);
> + return s;
> + }
> +
> + hdr = (void *) skb->data;
Let's replace this with skb_pull_data.
> dlci = __get_dlci(hdr->addr);
> type = __get_type(hdr->ctrl);
>
> - /* Trim FCS */
> + /* Trim FCS - safe: skb->len >= sizeof(*hdr) + 1 >= 1 */
> skb->len--; skb->tail--;
It might be a good idea to check if we can use skb_pull_data here as
well, instead of changing the tail to then use skb_tail_pointer.
> fcs = *(u8 *)skb_tail_pointer(skb);
>
> --
> 2.54.0
>
--
Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
2026-05-19 4:20 [PATCH] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame Muhammad Bilal
2026-05-19 7:25 ` [PATCH v2] " Muhammad Bilal
2026-05-19 13:51 ` [PATCH] " Luiz Augusto von Dentz
@ 2026-05-19 18:17 ` Muhammad Bilal
2026-05-19 18:25 ` Luiz Augusto von Dentz
2026-05-19 18:48 ` [PATCH v4] " Muhammad Bilal
3 siblings, 1 reply; 6+ messages in thread
From: Muhammad Bilal @ 2026-05-19 18:17 UTC (permalink / raw)
To: linux-bluetooth
Cc: netdev, linux-kernel, Marcel Holtmann, Luiz Augusto von Dentz,
Kees Cook, Jakub Kicinski, stable, Muhammad Bilal
rfcomm_recv_frame() casts skb->data to struct rfcomm_hdr * and
immediately dereferences hdr->addr and hdr->ctrl without first
validating that skb->len is large enough to hold the header. A
remote device can send a crafted short RFCOMM frame over L2CAP to
trigger an out-of-bounds read before any session state is checked.
The FCS trimming code that follows compounds the problem:
skb->len--; skb->tail--;
If skb->len is already zero the decrement wraps to UINT_MAX, causing
skb_tail_pointer() to return a pointer far outside the skb and
producing a second out-of-bounds read when the FCS byte is consumed.
Replace the open-coded cast with skb_pull_data() which validates
skb->len against sizeof(*hdr) and advances skb->data atomically.
Save the original skb->data as frame_start before the pull so that
__check_fcs() receives the header bytes as required by the RFCOMM
FCS specification. Guard against a missing FCS byte with an explicit
skb->len < 1 check. Replace the unsafe skb->tail decrement and
skb_tail_pointer() call with a direct end-of-data index and skb_trim().
Note: SeungJu Cheon posted a related patch that adds equivalent
length checks inside the individual MCC sub-handlers
(rfcomm_recv_pn, rfcomm_recv_rpn, rfcomm_recv_rls, rfcomm_recv_msc,
rfcomm_recv_mcc). That fix and this one are complementary and
independent; neither subsumes the other.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Muhammad Bilal <meatuni001@gmail.com>
---
v3:
- Replace open-coded cast with skb_pull_data() per Luiz's review
- Save frame_start before skb_pull_data(); pass it to __check_fcs()
to preserve correct FCS validation over the header bytes
- Replace skb->tail decrement with skb_trim() per Luiz's review
v2:
- Fix GitLint B3: replace tab with spaces in commit body
- Add Cc: stable@vger.kernel.org
---
net/bluetooth/rfcomm/core.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index d11bd5337..66eee8a86 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -1741,23 +1741,29 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk
static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
struct sk_buff *skb)
{
- struct rfcomm_hdr *hdr = (void *) skb->data;
+ struct rfcomm_hdr *hdr;
+ u8 *frame_start;
u8 type, dlci, fcs;
if (!s) {
- /* no session, so free socket data */
kfree_skb(skb);
return s;
}
+ frame_start = skb->data;
+ hdr = skb_pull_data(skb, sizeof(*hdr));
+ if (!hdr || skb->len < 1) {
+ kfree_skb(skb);
+ return s;
+ }
dlci = __get_dlci(hdr->addr);
type = __get_type(hdr->ctrl);
/* Trim FCS */
- skb->len--; skb->tail--;
- fcs = *(u8 *)skb_tail_pointer(skb);
+ fcs = skb->data[skb->len - 1];
+ skb_trim(skb, skb->len - 1);
- if (__check_fcs(skb->data, type, fcs)) {
+ if (__check_fcs(frame_start, type, fcs)) {
BT_ERR("bad checksum in packet");
kfree_skb(skb);
return s;
--
2.54.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v3] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
2026-05-19 18:17 ` [PATCH v3] " Muhammad Bilal
@ 2026-05-19 18:25 ` Luiz Augusto von Dentz
0 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2026-05-19 18:25 UTC (permalink / raw)
To: Muhammad Bilal
Cc: linux-bluetooth, netdev, linux-kernel, Marcel Holtmann, Kees Cook,
Jakub Kicinski, stable
Hi Muhammad,
On Tue, May 19, 2026 at 2:18 PM Muhammad Bilal <meatuni001@gmail.com> wrote:
>
> rfcomm_recv_frame() casts skb->data to struct rfcomm_hdr * and
> immediately dereferences hdr->addr and hdr->ctrl without first
> validating that skb->len is large enough to hold the header. A
> remote device can send a crafted short RFCOMM frame over L2CAP to
> trigger an out-of-bounds read before any session state is checked.
>
> The FCS trimming code that follows compounds the problem:
>
> skb->len--; skb->tail--;
>
> If skb->len is already zero the decrement wraps to UINT_MAX, causing
> skb_tail_pointer() to return a pointer far outside the skb and
> producing a second out-of-bounds read when the FCS byte is consumed.
>
> Replace the open-coded cast with skb_pull_data() which validates
> skb->len against sizeof(*hdr) and advances skb->data atomically.
> Save the original skb->data as frame_start before the pull so that
> __check_fcs() receives the header bytes as required by the RFCOMM
> FCS specification. Guard against a missing FCS byte with an explicit
> skb->len < 1 check. Replace the unsafe skb->tail decrement and
> skb_tail_pointer() call with a direct end-of-data index and skb_trim().
>
> Note: SeungJu Cheon posted a related patch that adds equivalent
> length checks inside the individual MCC sub-handlers
> (rfcomm_recv_pn, rfcomm_recv_rpn, rfcomm_recv_rls, rfcomm_recv_msc,
> rfcomm_recv_mcc). That fix and this one are complementary and
> independent; neither subsumes the other.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@vger.kernel.org
> Signed-off-by: Muhammad Bilal <meatuni001@gmail.com>
>
> ---
> v3:
> - Replace open-coded cast with skb_pull_data() per Luiz's review
> - Save frame_start before skb_pull_data(); pass it to __check_fcs()
> to preserve correct FCS validation over the header bytes
> - Replace skb->tail decrement with skb_trim() per Luiz's review
> v2:
> - Fix GitLint B3: replace tab with spaces in commit body
> - Add Cc: stable@vger.kernel.org
> ---
> net/bluetooth/rfcomm/core.c | 16 +++++++++++-----
> 1 file changed, 11 insertions(+), 5 deletions(-)
>
> diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
> index d11bd5337..66eee8a86 100644
> --- a/net/bluetooth/rfcomm/core.c
> +++ b/net/bluetooth/rfcomm/core.c
> @@ -1741,23 +1741,29 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk
> static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
> struct sk_buff *skb)
> {
> - struct rfcomm_hdr *hdr = (void *) skb->data;
> + struct rfcomm_hdr *hdr;
> + u8 *frame_start;
> u8 type, dlci, fcs;
>
> if (!s) {
> - /* no session, so free socket data */
Doesn't seem relevant to remove this comment.
> kfree_skb(skb);
> return s;
> }
>
> + frame_start = skb->data;
> + hdr = skb_pull_data(skb, sizeof(*hdr));
> + if (!hdr || skb->len < 1) {
> + kfree_skb(skb);
> + return s;
> + }
Add a empty line after if blocks.
> dlci = __get_dlci(hdr->addr);
> type = __get_type(hdr->ctrl);
>
> /* Trim FCS */
> - skb->len--; skb->tail--;
> - fcs = *(u8 *)skb_tail_pointer(skb);
> + fcs = skb->data[skb->len - 1];
> + skb_trim(skb, skb->len - 1);
>
> - if (__check_fcs(skb->data, type, fcs)) {
> + if (__check_fcs(frame_start, type, fcs)) {
> BT_ERR("bad checksum in packet");
> kfree_skb(skb);
> return s;
> --
> 2.54.0
Other than that looks good.
--
Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v4] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
2026-05-19 4:20 [PATCH] Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame Muhammad Bilal
` (2 preceding siblings ...)
2026-05-19 18:17 ` [PATCH v3] " Muhammad Bilal
@ 2026-05-19 18:48 ` Muhammad Bilal
3 siblings, 0 replies; 6+ messages in thread
From: Muhammad Bilal @ 2026-05-19 18:48 UTC (permalink / raw)
To: linux-bluetooth
Cc: netdev, linux-kernel, Marcel Holtmann, Luiz Augusto von Dentz,
Kees Cook, Jakub Kicinski, stable, Muhammad Bilal
rfcomm_recv_frame() casts skb->data to struct rfcomm_hdr * and
immediately dereferences hdr->addr and hdr->ctrl without first
validating that skb->len is large enough to hold the header. A
remote device can send a crafted short RFCOMM frame over L2CAP to
trigger an out-of-bounds read before any session state is checked.
The FCS trimming code that follows compounds the problem:
skb->len--; skb->tail--;
If skb->len is already zero the decrement wraps to UINT_MAX, causing
skb_tail_pointer() to return a pointer far outside the skb and
producing a second out-of-bounds read when the FCS byte is consumed.
Replace the open-coded cast with skb_pull_data() which validates
skb->len against sizeof(*hdr) and advances skb->data atomically.
Save the original skb->data as frame_start before the pull so that
__check_fcs() receives the header bytes as required by the RFCOMM
FCS specification. Guard against a missing FCS byte with an explicit
skb->len < 1 check. Replace the unsafe skb->tail decrement and
skb_tail_pointer() call with a direct end-of-data index and skb_trim().
Note: SeungJu Cheon posted a related patch that adds equivalent
length checks inside the individual MCC sub-handlers
(rfcomm_recv_pn, rfcomm_recv_rpn, rfcomm_recv_rls, rfcomm_recv_msc,
rfcomm_recv_mcc). That fix and this one are complementary and
independent; neither subsumes the other.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Muhammad Bilal <meatuni001@gmail.com>
---
v4:
- Keep no-session comment and add blank line after header/len guard
v3:
- Replace open-coded cast with skb_pull_data() per Luiz's review
- Save frame_start before skb_pull_data(); pass it to __check_fcs()
to preserve correct FCS validation over the header bytes
- Replace skb->tail decrement with skb_trim() per Luiz's review
v2:
- Fix GitLint B3: replace tab with spaces in commit body
- Add Cc: stable@vger.kernel.org
---
net/bluetooth/rfcomm/core.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index d11bd5337..e78ce11fa 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -1741,7 +1741,8 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk
static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
struct sk_buff *skb)
{
- struct rfcomm_hdr *hdr = (void *) skb->data;
+ struct rfcomm_hdr *hdr;
+ u8 *frame_start;
u8 type, dlci, fcs;
if (!s) {
@@ -1750,14 +1751,21 @@ static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
return s;
}
+ frame_start = skb->data;
+ hdr = skb_pull_data(skb, sizeof(*hdr));
+ if (!hdr || skb->len < 1) {
+ kfree_skb(skb);
+ return s;
+ }
+
dlci = __get_dlci(hdr->addr);
type = __get_type(hdr->ctrl);
/* Trim FCS */
- skb->len--; skb->tail--;
- fcs = *(u8 *)skb_tail_pointer(skb);
+ fcs = skb->data[skb->len - 1];
+ skb_trim(skb, skb->len - 1);
- if (__check_fcs(skb->data, type, fcs)) {
+ if (__check_fcs(frame_start, type, fcs)) {
BT_ERR("bad checksum in packet");
kfree_skb(skb);
return s;
--
2.54.0
^ permalink raw reply related [flat|nested] 6+ messages in thread