Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
Subsystems (5)
PATCH 1/1 Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
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.
--- net/bluetooth/rfcomm/core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
net/bluetooth/rfcomm/core.c +9 -2
@@ -1741,7 +1741,7 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct skstatic 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);
Signature · 1 line
2.54.0
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=1097025
---Test result---
Test Summary:
CheckPatch PASS 0.50 seconds GitLint FAIL 0.22 seconds SubjectPrefix PASS 0.08 seconds BuildKernel PASS 29.00 seconds CheckAllWarning PASS 28.96 seconds CheckSparse PASS 27.22 seconds BuildKernel32 PASS 25.35 seconds TestRunnerSetup PASS 568.42 seconds TestRunner_rfcomm-tester PASS 63.54 seconds IncrementalBuild PASS 24.24 seconds
Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
11: B3 Line contains hard tab characters (\t): " skb->len--; skb->tail--;"
https://github.com/bluez/bluetooth-next/pull/213
---
Regards,
Linux Bluetooth
PATCH v2 1/1 Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
In reply to Muhammad Bilalrfcomm_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.
--- net/bluetooth/rfcomm/core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
net/bluetooth/rfcomm/core.c +9 -2
@@ -1741,7 +1741,7 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct skstatic 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);
Signature · 1 line
2.54.0
Luiz Augusto von Dentz
maintainerHi Muhammad,
Quoted 47 lines · Muhammad Bilal
rfcomm_recv_frame()castsskb->datato structrfcomm_hdr* and immediately dereferenceshdr->addrandhdr->ctrlwithout first validating thatskb->lenis 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->lenis already zero the decrement wraps toUINT_MAX, causingskb_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 theskb->leninteger 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 <[email protected]>--- net/bluetooth/rfcomm/core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)net/bluetooth/rfcomm/core.c +1
-1@@ -1741,7 +1741,7 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct skstatic 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) {
net/bluetooth/rfcomm/core.c static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s, · line 1759
Quoted 10 lines
net/bluetooth/rfcomm/core.c +7
-0@@ -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.
net/bluetooth/rfcomm/core.c static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s, · line 1764
Quoted 6 lines
net/bluetooth/rfcomm/core.c +1
-1dlci =__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.
Quoted 5 lines
net/bluetooth/rfcomm/core.c +0
-1fcs = *(u8 *)skb_tail_pointer (skb); --2.54.0
Signature · 1 line
Luiz Augusto von Dentz
PATCH v3 1/1 Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
In reply to Muhammad Bilalrfcomm_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.
---
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: [email protected]
--- net/bluetooth/rfcomm/core.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
net/bluetooth/rfcomm/core.c +11 -5
@@ -1741,23 +1741,29 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct skstatic 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;
Signature · 1 line
2.54.0
Luiz Augusto von Dentz
maintainerHi Muhammad,
Quoted 51 lines · Muhammad Bilal
rfcomm_recv_frame()castsskb->datato structrfcomm_hdr* and immediately dereferenceshdr->addrandhdr->ctrlwithout first validating thatskb->lenis 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->lenis already zero the decrement wraps toUINT_MAX, causingskb_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 validatesskb->lenagainstsizeof(*hdr)and advancesskb->dataatomically. Save the originalskb->dataasframe_startbefore 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 explicitskb->len< 1 check. Replace the unsafeskb->taildecrement andskb_tail_pointer()call with a direct end-of-data index andskb_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")Signed-off-by Muhammad Bilal <[email protected]>---
v3:
- Replace open-coded cast withskb_pull_data()per Luiz's review
- Saveframe_startbeforeskb_pull_data(); pass it to__check_fcs()to preserve correct FCS validation over the header bytes
- Replaceskb->taildecrement withskb_trim()per Luiz's review
v2:
- Fix GitLint B3: replace tab with spaces in commit body
- Add Cc: [email protected]--- net/bluetooth/rfcomm/core.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)net/bluetooth/rfcomm/core.c +0
-0
net/bluetooth/rfcomm/core.c static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk · line 1748
Quoted 10 lines
net/bluetooth/rfcomm/core.c +2
-2@@ -1741,23 +1741,29 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct skstatic 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.
net/bluetooth/rfcomm/core.c static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk · line 1758
Quoted 10 lines
net/bluetooth/rfcomm/core.c +6
-0kfree_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.
Quoted 15 lines
net/bluetooth/rfcomm/core.c +3
-4dlci =__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; --
Quoted 1 line
2.54.0
Other than that looks good.
Signature · 1 line
Luiz Augusto von Dentz
PATCH v4 1/1 Bluetooth: RFCOMM: add minimum length check in rfcomm_recv_frame
In reply to Muhammad Bilalrfcomm_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.
---
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: [email protected]
--- net/bluetooth/rfcomm/core.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
net/bluetooth/rfcomm/core.c +12 -4
@@ -1741,7 +1741,8 @@ static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct skstatic 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;
Signature · 1 line
2.54.0
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=1097567
---Test result---
Test Summary:
CheckPatch PASS 0.73 seconds GitLint PASS 0.55 seconds SubjectPrefix PASS 0.12 seconds BuildKernel PASS 25.91 seconds CheckAllWarning PASS 29.40 seconds CheckSparse PASS 26.90 seconds BuildKernel32 PASS 24.98 seconds TestRunnerSetup PASS 526.66 seconds TestRunner_rfcomm-tester PASS 63.53 seconds IncrementalBuild PASS 24.66 seconds
https://github.com/bluez/bluetooth-next/pull/218
---
Regards,
Linux Bluetooth