diff mbox series

Bluetooth: L2CAP: Fix memory leak in vhci_write

Message ID 20221017074432.12177-1-yin31149@gmail.com (mailing list archive)
State Superseded
Headers show
Series Bluetooth: L2CAP: Fix memory leak in vhci_write | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/checkpatch fail Bluetooth: L2CAP: Fix memory leak in vhci_write\WARNING:COMMIT_LOG_LONG_LINE: Possible unwrapped commit description (prefer a maximum 75 chars per line) #124: Reported-and-tested-by: syzbot+8f819e36e01022991cfa@syzkaller.appspotmail.com total: 0 errors, 1 warnings, 0 checks, 19 lines checked NOTE: For some of the reported defects, checkpatch may be able to mechanically convert to the typical style using --fix or --fix-inplace. /github/workspace/src/13008275.patch has style problems, please review. NOTE: Ignored message types: UNKNOWN_COMMIT_ID NOTE: If any of the errors are false positives, please report them to the maintainer, see CHECKPATCH in MAINTAINERS.
tedd_an/gitlint fail Bluetooth: L2CAP: Fix memory leak in vhci_write 14: B1 Line exceeds max length (84>80): " [<ffffffff833f742f>] bt_skb_alloc include/net/bluetooth/bluetooth.h:469 [inline]"
tedd_an/subjectprefix success PASS
tedd_an/buildkernel success Build Kernel PASS
tedd_an/buildkernel32 success Build Kernel32 PASS
tedd_an/incremental_build success Pass
tedd_an/testrunnersetup success Test Runner Setup PASS
tedd_an/testrunnerl2cap-tester success Total: 40, Passed: 40 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunneriso-tester success Total: 55, Passed: 55 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunnerbnep-tester success Total: 1, Passed: 1 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunnermgmt-tester success Total: 494, Passed: 494 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunnerrfcomm-tester success Total: 11, Passed: 11 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunnersco-tester success Total: 12, Passed: 12 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunnerioctl-tester success Total: 28, Passed: 28 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunnermesh-tester success Total: 10, Passed: 10 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunnersmp-tester success Total: 8, Passed: 8 (100.0%), Failed: 0, Not Run: 0
tedd_an/testrunneruserchan-tester success Total: 4, Passed: 4 (100.0%), Failed: 0, Not Run: 0

Commit Message

Hawkins Jiawei Oct. 17, 2022, 7:44 a.m. UTC
Syzkaller reports a memory leak as follows:
====================================
BUG: memory leak
unreferenced object 0xffff88810d81ac00 (size 240):
  [...]
  hex dump (first 32 bytes):
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<ffffffff838733d9>] __alloc_skb+0x1f9/0x270 net/core/skbuff.c:418
    [<ffffffff833f742f>] alloc_skb include/linux/skbuff.h:1257 [inline]
    [<ffffffff833f742f>] bt_skb_alloc include/net/bluetooth/bluetooth.h:469 [inline]
    [<ffffffff833f742f>] vhci_get_user drivers/bluetooth/hci_vhci.c:391 [inline]
    [<ffffffff833f742f>] vhci_write+0x5f/0x230 drivers/bluetooth/hci_vhci.c:511
    [<ffffffff815e398d>] call_write_iter include/linux/fs.h:2192 [inline]
    [<ffffffff815e398d>] new_sync_write fs/read_write.c:491 [inline]
    [<ffffffff815e398d>] vfs_write+0x42d/0x540 fs/read_write.c:578
    [<ffffffff815e3cdd>] ksys_write+0x9d/0x160 fs/read_write.c:631
    [<ffffffff845e0645>] do_syscall_x64 arch/x86/entry/common.c:50 [inline]
    [<ffffffff845e0645>] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80
    [<ffffffff84600087>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
====================================

HCI core will uses hci_rx_work() to process frame, which is queued to
the hdev->rx_q tail in hci_recv_frame() by HCI driver.

Yet the problem is that, HCI core does not free the skb after handling
ACL data packets. To be more specific, when start fragment does not
contain the L2CAP length, HCI core just reads possible bytes and
finishes frame process in l2cap_recv_acldata(), without freeing the skb,
which triggers the above memory leak.

This patch solves it by releasing the relative skb, after processing the
above case in l2cap_recv_acldata()

Fixes: 4d7ea8ee90e4 ("Bluetooth: L2CAP: Fix handling fragmented length")
Link: https://lore.kernel.org/all/0000000000000d0b1905e6aaef64@google.com/
Reported-and-tested-by: syzbot+8f819e36e01022991cfa@syzkaller.appspotmail.com
Signed-off-by: Hawkins Jiawei <yin31149@gmail.com>
---
 net/bluetooth/l2cap_core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Comments

bluez.test.bot@gmail.com Oct. 17, 2022, 8:37 a.m. UTC | #1
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=685727

---Test result---

Test Summary:
CheckPatch                    FAIL      0.99 seconds
GitLint                       FAIL      0.49 seconds
SubjectPrefix                 PASS      0.31 seconds
BuildKernel                   PASS      44.65 seconds
BuildKernel32                 PASS      40.74 seconds
Incremental Build with patchesPASS      58.75 seconds
TestRunner: Setup             PASS      661.99 seconds
TestRunner: l2cap-tester      PASS      21.06 seconds
TestRunner: iso-tester        PASS      21.23 seconds
TestRunner: bnep-tester       PASS      8.15 seconds
TestRunner: mgmt-tester       PASS      133.51 seconds
TestRunner: rfcomm-tester     PASS      12.73 seconds
TestRunner: sco-tester        PASS      11.87 seconds
TestRunner: ioctl-tester      PASS      13.82 seconds
TestRunner: mesh-tester       PASS      10.06 seconds
TestRunner: smp-tester        PASS      11.82 seconds
TestRunner: userchan-tester   PASS      8.53 seconds

Details
##############################
Test: CheckPatch - FAIL - 0.99 seconds
Run checkpatch.pl script with rule in .checkpatch.conf
Bluetooth: L2CAP: Fix memory leak in vhci_write\WARNING:COMMIT_LOG_LONG_LINE: Possible unwrapped commit description (prefer a maximum 75 chars per line)
#124: 
Reported-and-tested-by: syzbot+8f819e36e01022991cfa@syzkaller.appspotmail.com

total: 0 errors, 1 warnings, 0 checks, 19 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/13008275.patch has style problems, please review.

NOTE: Ignored message types: UNKNOWN_COMMIT_ID

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: GitLint - FAIL - 0.49 seconds
Run gitlint with rule in .gitlint
Bluetooth: L2CAP: Fix memory leak in vhci_write
14: B1 Line exceeds max length (84>80): "    [<ffffffff833f742f>] bt_skb_alloc include/net/bluetooth/bluetooth.h:469 [inline]"




---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz Oct. 17, 2022, 8:01 p.m. UTC | #2
Hi Hawkins,

On Mon, Oct 17, 2022 at 12:47 AM Hawkins Jiawei <yin31149@gmail.com> wrote:
>
> Syzkaller reports a memory leak as follows:
> ====================================
> BUG: memory leak
> unreferenced object 0xffff88810d81ac00 (size 240):
>   [...]
>   hex dump (first 32 bytes):
>     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>   backtrace:
>     [<ffffffff838733d9>] __alloc_skb+0x1f9/0x270 net/core/skbuff.c:418
>     [<ffffffff833f742f>] alloc_skb include/linux/skbuff.h:1257 [inline]
>     [<ffffffff833f742f>] bt_skb_alloc include/net/bluetooth/bluetooth.h:469 [inline]
>     [<ffffffff833f742f>] vhci_get_user drivers/bluetooth/hci_vhci.c:391 [inline]
>     [<ffffffff833f742f>] vhci_write+0x5f/0x230 drivers/bluetooth/hci_vhci.c:511
>     [<ffffffff815e398d>] call_write_iter include/linux/fs.h:2192 [inline]
>     [<ffffffff815e398d>] new_sync_write fs/read_write.c:491 [inline]
>     [<ffffffff815e398d>] vfs_write+0x42d/0x540 fs/read_write.c:578
>     [<ffffffff815e3cdd>] ksys_write+0x9d/0x160 fs/read_write.c:631
>     [<ffffffff845e0645>] do_syscall_x64 arch/x86/entry/common.c:50 [inline]
>     [<ffffffff845e0645>] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80
>     [<ffffffff84600087>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
> ====================================
>
> HCI core will uses hci_rx_work() to process frame, which is queued to
> the hdev->rx_q tail in hci_recv_frame() by HCI driver.
>
> Yet the problem is that, HCI core does not free the skb after handling
> ACL data packets. To be more specific, when start fragment does not
> contain the L2CAP length, HCI core just reads possible bytes and
> finishes frame process in l2cap_recv_acldata(), without freeing the skb,
> which triggers the above memory leak.
>
> This patch solves it by releasing the relative skb, after processing the
> above case in l2cap_recv_acldata()
>
> Fixes: 4d7ea8ee90e4 ("Bluetooth: L2CAP: Fix handling fragmented length")
> Link: https://lore.kernel.org/all/0000000000000d0b1905e6aaef64@google.com/
> Reported-and-tested-by: syzbot+8f819e36e01022991cfa@syzkaller.appspotmail.com
> Signed-off-by: Hawkins Jiawei <yin31149@gmail.com>
> ---
>  net/bluetooth/l2cap_core.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index 1f34b82ca0ec..e0a00854c02e 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -8426,9 +8426,8 @@ void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
>                  * expected length.
>                  */
>                 if (skb->len < L2CAP_LEN_SIZE) {
> -                       if (l2cap_recv_frag(conn, skb, conn->mtu) < 0)
> -                               goto drop;
> -                       return;
> +                       l2cap_recv_frag(conn, skb, conn->mtu);
> +                       goto drop;

Let us use break; instead of goto drop since we have copied the skb into rx_sbk.

>                 }
>
>                 len = get_unaligned_le16(skb->data) + L2CAP_HDR_SIZE;
> @@ -8472,7 +8471,7 @@ void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
>
>                         /* Header still could not be read just continue */
>                         if (conn->rx_skb->len < L2CAP_LEN_SIZE)
> -                               return;
> +                               goto drop;
>                 }
>
>                 if (skb->len > conn->rx_len) {
> --
> 2.25.1
>
Hawkins Jiawei Oct. 18, 2022, 1:16 a.m. UTC | #3
On Tue, 18 Oct 2022 at 04:01, Luiz Augusto von Dentz <luiz.dentz@gmail.com> wrote:
>
> Hi Hawkins,
>
> On Mon, Oct 17, 2022 at 12:47 AM Hawkins Jiawei <yin31149@gmail.com> wrote:
> >
> > Syzkaller reports a memory leak as follows:
> > ====================================
> > BUG: memory leak
> > unreferenced object 0xffff88810d81ac00 (size 240):
> >   [...]
> >   hex dump (first 32 bytes):
> >     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
> >     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
> >   backtrace:
> >     [<ffffffff838733d9>] __alloc_skb+0x1f9/0x270 net/core/skbuff.c:418
> >     [<ffffffff833f742f>] alloc_skb include/linux/skbuff.h:1257 [inline]
> >     [<ffffffff833f742f>] bt_skb_alloc include/net/bluetooth/bluetooth.h:469 [inline]
> >     [<ffffffff833f742f>] vhci_get_user drivers/bluetooth/hci_vhci.c:391 [inline]
> >     [<ffffffff833f742f>] vhci_write+0x5f/0x230 drivers/bluetooth/hci_vhci.c:511
> >     [<ffffffff815e398d>] call_write_iter include/linux/fs.h:2192 [inline]
> >     [<ffffffff815e398d>] new_sync_write fs/read_write.c:491 [inline]
> >     [<ffffffff815e398d>] vfs_write+0x42d/0x540 fs/read_write.c:578
> >     [<ffffffff815e3cdd>] ksys_write+0x9d/0x160 fs/read_write.c:631
> >     [<ffffffff845e0645>] do_syscall_x64 arch/x86/entry/common.c:50 [inline]
> >     [<ffffffff845e0645>] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80
> >     [<ffffffff84600087>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
> > ====================================
> >
> > HCI core will uses hci_rx_work() to process frame, which is queued to
> > the hdev->rx_q tail in hci_recv_frame() by HCI driver.
> >
> > Yet the problem is that, HCI core does not free the skb after handling
> > ACL data packets. To be more specific, when start fragment does not
> > contain the L2CAP length, HCI core just reads possible bytes and
> > finishes frame process in l2cap_recv_acldata(), without freeing the skb,
> > which triggers the above memory leak.
> >
> > This patch solves it by releasing the relative skb, after processing the
> > above case in l2cap_recv_acldata()
> >
> > Fixes: 4d7ea8ee90e4 ("Bluetooth: L2CAP: Fix handling fragmented length")
> > Link: https://lore.kernel.org/all/0000000000000d0b1905e6aaef64@google.com/
> > Reported-and-tested-by: syzbot+8f819e36e01022991cfa@syzkaller.appspotmail.com
> > Signed-off-by: Hawkins Jiawei <yin31149@gmail.com>
> > ---
> >  net/bluetooth/l2cap_core.c | 7 +++----
> >  1 file changed, 3 insertions(+), 4 deletions(-)
> >
> > diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> > index 1f34b82ca0ec..e0a00854c02e 100644
> > --- a/net/bluetooth/l2cap_core.c
> > +++ b/net/bluetooth/l2cap_core.c
> > @@ -8426,9 +8426,8 @@ void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
> >                  * expected length.
> >                  */
> >                 if (skb->len < L2CAP_LEN_SIZE) {
> > -                       if (l2cap_recv_frag(conn, skb, conn->mtu) < 0)
> > -                               goto drop;
> > -                       return;
> > +                       l2cap_recv_frag(conn, skb, conn->mtu);
> > +                       goto drop;
>
> Let us use break; instead of goto drop since we have copied the skb into rx_sbk.
Thanks for your suggestion. I will refactor this patch as you suggested.

>
> >                 }
> >
> >                 len = get_unaligned_le16(skb->data) + L2CAP_HDR_SIZE;
> > @@ -8472,7 +8471,7 @@ void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
> >
> >                         /* Header still could not be read just continue */
> >                         if (conn->rx_skb->len < L2CAP_LEN_SIZE)
> > -                               return;
> > +                               goto drop;
> >                 }
> >
> >                 if (skb->len > conn->rx_len) {
> > --
> > 2.25.1
> >
>
>
> --
> Luiz Augusto von Dentz
diff mbox series

Patch

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 1f34b82ca0ec..e0a00854c02e 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -8426,9 +8426,8 @@  void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
 		 * expected length.
 		 */
 		if (skb->len < L2CAP_LEN_SIZE) {
-			if (l2cap_recv_frag(conn, skb, conn->mtu) < 0)
-				goto drop;
-			return;
+			l2cap_recv_frag(conn, skb, conn->mtu);
+			goto drop;
 		}
 
 		len = get_unaligned_le16(skb->data) + L2CAP_HDR_SIZE;
@@ -8472,7 +8471,7 @@  void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
 
 			/* Header still could not be read just continue */
 			if (conn->rx_skb->len < L2CAP_LEN_SIZE)
-				return;
+				goto drop;
 		}
 
 		if (skb->len > conn->rx_len) {