Commit Graph

751 Commits

Author SHA1 Message Date
CKI Backport Bot add3b8a969 Bluetooth: hci_core: Fix sleeping function called from invalid context
JIRA: https://issues.redhat.com/browse/RHEL-74112
CVE: CVE-2024-57894

commit 4d94f05558271654670d18c26c912da0c1c15549
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Tue Dec 3 16:07:32 2024 -0500

    Bluetooth: hci_core: Fix sleeping function called from invalid context

    This reworks hci_cb_list to not use mutex hci_cb_list_lock to avoid bugs
    like the bellow:

    BUG: sleeping function called from invalid context at kernel/locking/mutex.c:585
    in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 5070, name: kworker/u9:2
    preempt_count: 0, expected: 0
    RCU nest depth: 1, expected: 0
    4 locks held by kworker/u9:2/5070:
     #0: ffff888015be3948 ((wq_completion)hci0#2){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3229 [inline]
     #0: ffff888015be3948 ((wq_completion)hci0#2){+.+.}-{0:0}, at: process_scheduled_works+0x8e0/0x1770 kernel/workqueue.c:3335
     #1: ffffc90003b6fd00 ((work_completion)(&hdev->rx_work)){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3230 [inline]
     #1: ffffc90003b6fd00 ((work_completion)(&hdev->rx_work)){+.+.}-{0:0}, at: process_scheduled_works+0x91b/0x1770 kernel/workqueue.c:3335
     #2: ffff8880665d0078 (&hdev->lock){+.+.}-{3:3}, at: hci_le_create_big_complete_evt+0xcf/0xae0 net/bluetooth/hci_event.c:6914
     #3: ffffffff8e132020 (rcu_read_lock){....}-{1:2}, at: rcu_lock_acquire include/linux/rcupdate.h:298 [inline]
     #3: ffffffff8e132020 (rcu_read_lock){....}-{1:2}, at: rcu_read_lock include/linux/rcupdate.h:750 [inline]
     #3: ffffffff8e132020 (rcu_read_lock){....}-{1:2}, at: hci_le_create_big_complete_evt+0xdb/0xae0 net/bluetooth/hci_event.c:6915
    CPU: 0 PID: 5070 Comm: kworker/u9:2 Not tainted 6.8.0-syzkaller-08073-g480e035fc4c7 #0
    Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/27/2024
    Workqueue: hci0 hci_rx_work
    Call Trace:
     <TASK>
     __dump_stack lib/dump_stack.c:88 [inline]
     dump_stack_lvl+0x241/0x360 lib/dump_stack.c:114
     __might_resched+0x5d4/0x780 kernel/sched/core.c:10187
     __mutex_lock_common kernel/locking/mutex.c:585 [inline]
     __mutex_lock+0xc1/0xd70 kernel/locking/mutex.c:752
     hci_connect_cfm include/net/bluetooth/hci_core.h:2004 [inline]
     hci_le_create_big_complete_evt+0x3d9/0xae0 net/bluetooth/hci_event.c:6939
     hci_event_func net/bluetooth/hci_event.c:7514 [inline]
     hci_event_packet+0xa53/0x1540 net/bluetooth/hci_event.c:7569
     hci_rx_work+0x3e8/0xca0 net/bluetooth/hci_core.c:4171
     process_one_work kernel/workqueue.c:3254 [inline]
     process_scheduled_works+0xa00/0x1770 kernel/workqueue.c:3335
     worker_thread+0x86d/0xd70 kernel/workqueue.c:3416
     kthread+0x2f0/0x390 kernel/kthread.c:388
     ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147
     ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:243
     </TASK>

    Reported-by: syzbot+2fb0835e0c9cefc34614@syzkaller.appspotmail.com
    Tested-by: syzbot+2fb0835e0c9cefc34614@syzkaller.appspotmail.com
    Closes: https://syzkaller.appspot.com/bug?extid=2fb0835e0c9cefc34614
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2025-01-15 15:36:09 +00:00
Bastien Nocera 33c883021f Bluetooth: l2cap: always unlock channel in l2cap_conless_channel()
JIRA: https://issues.redhat.com/browse/RHEL-61734

commit c531e63871c0b50c8c4e62c048535a08886fba3e
Author: Dmitry Antipov <dmantipov@yandex.ru>
Date:   Wed Jul 31 12:19:36 2024 +0300

    Bluetooth: l2cap: always unlock channel in l2cap_conless_channel()

    Add missing call to 'l2cap_chan_unlock()' on receive error handling
    path in 'l2cap_conless_channel()'.

    Fixes: a24cce144b ("Bluetooth: Fix reference counting of global L2CAP channels")
    Reported-by: syzbot+45ac74737e866894acb0@syzkaller.appspotmail.com
    Closes: https://syzkaller.appspot.com/bug?extid=45ac74737e866894acb0
    Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-12-11 15:25:18 +01:00
Bastien Nocera 82aa8ca820 Bluetooth: L2CAP: Fix rejecting L2CAP_CONN_PARAM_UPDATE_REQ
JIRA: https://issues.redhat.com/browse/RHEL-61734

commit 806a5198c05987b748b50f3d0c0cfb3d417381a4
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon May 20 16:03:07 2024 -0400

    Bluetooth: L2CAP: Fix rejecting L2CAP_CONN_PARAM_UPDATE_REQ

    This removes the bogus check for max > hcon->le_conn_max_interval since
    the later is just the initial maximum conn interval not the maximum the
    stack could support which is really 3200=4000ms.

    In order to pass GAP/CONN/CPUP/BV-05-C one shall probably enter values
    of the following fields in IXIT that would cause hci_check_conn_params
    to fail:

    TSPX_conn_update_int_min
    TSPX_conn_update_int_max
    TSPX_conn_update_peripheral_latency
    TSPX_conn_update_supervision_timeout

    Link: https://github.com/bluez/bluez/issues/847
    Fixes: e4b019515f95 ("Bluetooth: Enforce validation on max value of connection interval")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-12-11 15:25:08 +01:00
Bastien Nocera b61911679a Bluetooth: L2CAP: Avoid -Wflex-array-member-not-at-end warnings
JIRA: https://issues.redhat.com/browse/RHEL-61734

commit 1c08108f3014881ad5f4c35a2abaf9c65475035d
Author: Gustavo A. R. Silva <gustavoars@kernel.org>
Date:   Wed Mar 27 10:23:51 2024 -0600

    Bluetooth: L2CAP: Avoid -Wflex-array-member-not-at-end warnings

    -Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
    ready to enable it globally.

    There are currently a couple of objects (`req` and `rsp`), in a couple
    of structures, that contain flexible structures (`struct l2cap_ecred_conn_req`
    and `struct l2cap_ecred_conn_rsp`), for example:

    struct l2cap_ecred_rsp_data {
            struct {
                    struct l2cap_ecred_conn_rsp rsp;
                    __le16 scid[L2CAP_ECRED_MAX_CID];
            } __packed pdu;
            int count;
    };

    in the struct above, `struct l2cap_ecred_conn_rsp` is a flexible
    structure:

    struct l2cap_ecred_conn_rsp {
            __le16 mtu;
            __le16 mps;
            __le16 credits;
            __le16 result;
            __le16 dcid[];
    };

    So, in order to avoid ending up with a flexible-array member in the
    middle of another structure, we use the `struct_group_tagged()` (and
    `__struct_group()` when the flexible structure is `__packed`) helper
    to separate the flexible array from the rest of the members in the
    flexible structure:

    struct l2cap_ecred_conn_rsp {
            struct_group_tagged(l2cap_ecred_conn_rsp_hdr, hdr,

            ... the rest of members

            );
            __le16 dcid[];
    };

    With the change described above, we now declare objects of the type of
    the tagged struct, in this example `struct l2cap_ecred_conn_rsp_hdr`,
    without embedding flexible arrays in the middle of other structures:

    struct l2cap_ecred_rsp_data {
            struct {
                    struct l2cap_ecred_conn_rsp_hdr rsp;
                    __le16 scid[L2CAP_ECRED_MAX_CID];
            } __packed pdu;
            int count;
    };

    Also, when the flexible-array member needs to be accessed, we use
    `container_of()` to retrieve a pointer to the flexible structure.

    We also use the `DEFINE_RAW_FLEX()` helper for a couple of on-stack
    definitions of a flexible structure where the size of the flexible-array
    member is known at compile-time.

    So, with these changes, fix the following warnings:
    net/bluetooth/l2cap_core.c:1260:45: warning: structure containing a
    flexible array member is not at the end of another structure
    [-Wflex-array-member-not-at-end]
    net/bluetooth/l2cap_core.c:3740:45: warning: structure containing a
    flexible array member is not at the end of another structure
    [-Wflex-array-member-not-at-end]
    net/bluetooth/l2cap_core.c:4999:45: warning: structure containing a
    flexible array member is not at the end of another structure
    [-Wflex-array-member-not-at-end]
    net/bluetooth/l2cap_core.c:7116:47: warning: structure containing a
    flexible array member is not at the end of another structure
    [-Wflex-array-member-not-at-end]

    Link: https://github.com/KSPP/linux/issues/202
    Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-12-11 15:25:03 +01:00
Bastien Nocera 960c66101c Bluetooth: hci_sync: Use advertised PHYs on hci_le_ext_create_conn_sync
JIRA: https://issues.redhat.com/browse/RHEL-61734

commit 2e7ed5f5e69b6fe93dd3c6b651d041e0a7a456d1
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Fri Apr 5 16:40:33 2024 -0400

    Bluetooth: hci_sync: Use advertised PHYs on hci_le_ext_create_conn_sync

    The extended advertising reports do report the PHYs so this store then
    in hci_conn so it can be later used in hci_le_ext_create_conn_sync to
    narrow the PHYs to be scanned since the controller will also perform a
    scan having a smaller set of PHYs shall reduce the time it takes to
    find and connect peers.

    Fixes: 288c90224eec ("Bluetooth: Enable all supported LE PHY by default")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-12-11 15:25:01 +01:00
Rado Vrbovsky dc02fffe75 Merge: CVE-2024-49950: Bluetooth: L2CAP: Fix uaf in l2cap_connect
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5519

JIRA: https://issues.redhat.com/browse/RHEL-63624
CVE: CVE-2024-49950

```
Bluetooth: L2CAP: Fix uaf in l2cap_connect

[Syzbot reported]
BUG: KASAN: slab-use-after-free in l2cap_connect.constprop.0+0x10d8/0x1270 net/bluetooth/l2cap_core.c:3949
Read of size 8 at addr ffff8880241e9800 by task kworker/u9:0/54

CPU: 0 UID: 0 PID: 54 Comm: kworker/u9:0 Not tainted 6.11.0-rc6-syzkaller-00268-g788220eee30d #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/06/2024
Workqueue: hci2 hci_rx_work
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:93 [inline]
 dump_stack_lvl+0x116/0x1f0 lib/dump_stack.c:119
 print_address_description mm/kasan/report.c:377 [inline]
 print_report+0xc3/0x620 mm/kasan/report.c:488
 kasan_report+0xd9/0x110 mm/kasan/report.c:601
 l2cap_connect.constprop.0+0x10d8/0x1270 net/bluetooth/l2cap_core.c:3949
 l2cap_connect_req net/bluetooth/l2cap_core.c:4080 [inline]
 l2cap_bredr_sig_cmd net/bluetooth/l2cap_core.c:4772 [inline]
 l2cap_sig_channel net/bluetooth/l2cap_core.c:5543 [inline]
 l2cap_recv_frame+0xf0b/0x8eb0 net/bluetooth/l2cap_core.c:6825
 l2cap_recv_acldata+0x9b4/0xb70 net/bluetooth/l2cap_core.c:7514
 hci_acldata_packet net/bluetooth/hci_core.c:3791 [inline]
 hci_rx_work+0xaab/0x1610 net/bluetooth/hci_core.c:4028
 process_one_work+0x9c5/0x1b40 kernel/workqueue.c:3231
 process_scheduled_works kernel/workqueue.c:3312 [inline]
 worker_thread+0x6c8/0xed0 kernel/workqueue.c:3389
 kthread+0x2c1/0x3a0 kernel/kthread.c:389
 ret_from_fork+0x45/0x80 arch/x86/kernel/process.c:147
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244
...

Freed by task 5245:
 kasan_save_stack+0x33/0x60 mm/kasan/common.c:47
 kasan_save_track+0x14/0x30 mm/kasan/common.c:68
 kasan_save_free_info+0x3b/0x60 mm/kasan/generic.c:579
 poison_slab_object+0xf7/0x160 mm/kasan/common.c:240
 __kasan_slab_free+0x32/0x50 mm/kasan/common.c:256
 kasan_slab_free include/linux/kasan.h:184 [inline]
 slab_free_hook mm/slub.c:2256 [inline]
 slab_free mm/slub.c:4477 [inline]
 kfree+0x12a/0x3b0 mm/slub.c:4598
 l2cap_conn_free net/bluetooth/l2cap_core.c:1810 [inline]
 kref_put include/linux/kref.h:65 [inline]
 l2cap_conn_put net/bluetooth/l2cap_core.c:1822 [inline]
 l2cap_conn_del+0x59d/0x730 net/bluetooth/l2cap_core.c:1802
 l2cap_connect_cfm+0x9e6/0xf80 net/bluetooth/l2cap_core.c:7241
 hci_connect_cfm include/net/bluetooth/hci_core.h:1960 [inline]
 hci_conn_failed+0x1c3/0x370 net/bluetooth/hci_conn.c:1265
 hci_abort_conn_sync+0x75a/0xb50 net/bluetooth/hci_sync.c:5583
 abort_conn_sync+0x197/0x360 net/bluetooth/hci_conn.c:2917
 hci_cmd_sync_work+0x1a4/0x410 net/bluetooth/hci_sync.c:328
 process_one_work+0x9c5/0x1b40 kernel/workqueue.c:3231
 process_scheduled_works kernel/workqueue.c:3312 [inline]
 worker_thread+0x6c8/0xed0 kernel/workqueue.c:3389
 kthread+0x2c1/0x3a0 kernel/kthread.c:389
 ret_from_fork+0x45/0x80 arch/x86/kernel/process.c:147
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244

Reported-by: syzbot+c12e2f941af1feb5632c@syzkaller.appspotmail.com
Tested-by: syzbot+c12e2f941af1feb5632c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c12e2f941af1feb5632c
Fixes: 7b064edae3 ("Bluetooth: Fix authentication if acl data comes before remote feature evt")
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
(cherry picked from commit 333b4fd11e89b29c84c269123f871883a30be586)
```

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>

---

Signed-off-by: Bastien Nocera <bnocera@redhat.com>

Omitted-Fix: 55abbd148dfb604ebf3f72d6c3dd2a8063d40718 (duplicate of `7967dc8f797f454d4f4acec15c7df0cdf4801617`)

<small>Created 2024-10-22 10:00 UTC by backporter - [KWF FAQ](https://red.ht/kernel_workflow_doc) - [Slack #team-kernel-workflow](https://redhat-internal.slack.com/archives/C04LRUPMJQ5) - [Source](https://gitlab.com/cki-project/kernel-workflow/-/blob/main/webhook/utils/backporter.py) - [Documentation](https://gitlab.com/cki-project/kernel-workflow/-/blob/main/docs/README.backporter.md) - [Report an issue](https://gitlab.com/cki-project/kernel-workflow/-/issues/new?issue%5Btitle%5D=backporter%20webhook%20issue)</small>

Approved-by: José Ignacio Tornos Martínez <jtornosm@redhat.com>
Approved-by: Daniel Horak <dhorak@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-11-28 20:19:04 +00:00
Bastien Nocera e5010cb8a2 Bluetooth: hci_conn: Always use sk_timeo as conn_timeout
JIRA: https://issues.redhat.com/browse/RHEL-63875
CVE: CVE-2024-50029

commit bf98feea5b65ced367a871cf35fc044dedbcfb85
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Wed Feb 7 15:26:20 2024 -0500

    Bluetooth: hci_conn: Always use sk_timeo as conn_timeout

    This aligns the use socket sk_timeo as conn_timeout when initiating a
    connection and then use it when scheduling the resulting HCI command,
    that way the command is actually aborted synchronously thus not
    blocking commands generated by hci_abort_conn_sync to inform the
    controller the connection is to be aborted.

    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-11-07 10:39:06 +01:00
CKI Backport Bot 4d924089ac Bluetooth: L2CAP: Fix uaf in l2cap_connect
JIRA: https://issues.redhat.com/browse/RHEL-63624
CVE: CVE-2024-49950

commit 333b4fd11e89b29c84c269123f871883a30be586
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon Sep 23 12:47:39 2024 -0400

    Bluetooth: L2CAP: Fix uaf in l2cap_connect

    [Syzbot reported]
    BUG: KASAN: slab-use-after-free in l2cap_connect.constprop.0+0x10d8/0x1270 net/bluetooth/l2cap_core.c:3949
    Read of size 8 at addr ffff8880241e9800 by task kworker/u9:0/54

    CPU: 0 UID: 0 PID: 54 Comm: kworker/u9:0 Not tainted 6.11.0-rc6-syzkaller-00268-g788220eee30d #0
    Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/06/2024
    Workqueue: hci2 hci_rx_work
    Call Trace:
     <TASK>
     __dump_stack lib/dump_stack.c:93 [inline]
     dump_stack_lvl+0x116/0x1f0 lib/dump_stack.c:119
     print_address_description mm/kasan/report.c:377 [inline]
     print_report+0xc3/0x620 mm/kasan/report.c:488
     kasan_report+0xd9/0x110 mm/kasan/report.c:601
     l2cap_connect.constprop.0+0x10d8/0x1270 net/bluetooth/l2cap_core.c:3949
     l2cap_connect_req net/bluetooth/l2cap_core.c:4080 [inline]
     l2cap_bredr_sig_cmd net/bluetooth/l2cap_core.c:4772 [inline]
     l2cap_sig_channel net/bluetooth/l2cap_core.c:5543 [inline]
     l2cap_recv_frame+0xf0b/0x8eb0 net/bluetooth/l2cap_core.c:6825
     l2cap_recv_acldata+0x9b4/0xb70 net/bluetooth/l2cap_core.c:7514
     hci_acldata_packet net/bluetooth/hci_core.c:3791 [inline]
     hci_rx_work+0xaab/0x1610 net/bluetooth/hci_core.c:4028
     process_one_work+0x9c5/0x1b40 kernel/workqueue.c:3231
     process_scheduled_works kernel/workqueue.c:3312 [inline]
     worker_thread+0x6c8/0xed0 kernel/workqueue.c:3389
     kthread+0x2c1/0x3a0 kernel/kthread.c:389
     ret_from_fork+0x45/0x80 arch/x86/kernel/process.c:147
     ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244
    ...

    Freed by task 5245:
     kasan_save_stack+0x33/0x60 mm/kasan/common.c:47
     kasan_save_track+0x14/0x30 mm/kasan/common.c:68
     kasan_save_free_info+0x3b/0x60 mm/kasan/generic.c:579
     poison_slab_object+0xf7/0x160 mm/kasan/common.c:240
     __kasan_slab_free+0x32/0x50 mm/kasan/common.c:256
     kasan_slab_free include/linux/kasan.h:184 [inline]
     slab_free_hook mm/slub.c:2256 [inline]
     slab_free mm/slub.c:4477 [inline]
     kfree+0x12a/0x3b0 mm/slub.c:4598
     l2cap_conn_free net/bluetooth/l2cap_core.c:1810 [inline]
     kref_put include/linux/kref.h:65 [inline]
     l2cap_conn_put net/bluetooth/l2cap_core.c:1822 [inline]
     l2cap_conn_del+0x59d/0x730 net/bluetooth/l2cap_core.c:1802
     l2cap_connect_cfm+0x9e6/0xf80 net/bluetooth/l2cap_core.c:7241
     hci_connect_cfm include/net/bluetooth/hci_core.h:1960 [inline]
     hci_conn_failed+0x1c3/0x370 net/bluetooth/hci_conn.c:1265
     hci_abort_conn_sync+0x75a/0xb50 net/bluetooth/hci_sync.c:5583
     abort_conn_sync+0x197/0x360 net/bluetooth/hci_conn.c:2917
     hci_cmd_sync_work+0x1a4/0x410 net/bluetooth/hci_sync.c:328
     process_one_work+0x9c5/0x1b40 kernel/workqueue.c:3231
     process_scheduled_works kernel/workqueue.c:3312 [inline]
     worker_thread+0x6c8/0xed0 kernel/workqueue.c:3389
     kthread+0x2c1/0x3a0 kernel/kthread.c:389
     ret_from_fork+0x45/0x80 arch/x86/kernel/process.c:147
     ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244

    Reported-by: syzbot+c12e2f941af1feb5632c@syzkaller.appspotmail.com
    Tested-by: syzbot+c12e2f941af1feb5632c@syzkaller.appspotmail.com
    Closes: https://syzkaller.appspot.com/bug?extid=c12e2f941af1feb5632c
    Fixes: 7b064edae3 ("Bluetooth: Fix authentication if acl data comes before remote feature evt")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2024-10-22 10:00:34 +00:00
Rado Vrbovsky 5dbb51086d Merge: CVE-2024-36968 Bluetooth: L2CAP: Fix div-by-zero in l2cap_le_flowctl_init()
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5069

Bluetooth: L2CAP: Fix div-by-zero in l2cap_le_flowctl_init()

JIRA: https://issues.redhat.com/browse/RHEL-41144
CVE: CVE-2024-36968

Depends: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/4996

```
commit a5b862c6a221459d54e494e88965b48dcfa6cc44
Author: Sungwoo Kim <iam@sung-woo.kim>
Date:   Sat May 4 15:23:29 2024 -0400

    Bluetooth: L2CAP: Fix div-by-zero in l2cap_le_flowctl_init()

    l2cap_le_flowctl_init() can cause both div-by-zero and an integer
    overflow since hdev->le_mtu may not fall in the valid range.

    Move MTU from hci_dev to hci_conn to validate MTU and stop the connection
    process earlier if MTU is invalid.
    Also, add a missing validation in read_buffer_size() and make it return
    an error value if the validation fails.
    Now hci_conn_add() returns ERR_PTR() as it can fail due to the both a
    kzalloc failure and invalid MTU value.

    divide error: 0000 [#1] PREEMPT SMP KASAN NOPTI
    CPU: 0 PID: 67 Comm: kworker/u5:0 Tainted: G        W          6.9.0-rc5+ #20
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
    Workqueue: hci0 hci_rx_work
    RIP: 0010:l2cap_le_flowctl_init+0x19e/0x3f0 net/bluetooth/l2cap_core.c:547
    Code: e8 17 17 0c 00 66 41 89 9f 84 00 00 00 bf 01 00 00 00 41 b8 02 00 00 00 4c
    89 fe 4c 89 e2 89 d9 e8 27 17 0c 00 44 89 f0 31 d2 <66> f7 f3 89 c3 ff c3 4d 8d
    b7 88 00 00 00 4c 89 f0 48 c1 e8 03 42
    RSP: 0018:ffff88810bc0f858 EFLAGS: 00010246
    RAX: 00000000000002a0 RBX: 0000000000000000 RCX: dffffc0000000000
    RDX: 0000000000000000 RSI: ffff88810bc0f7c0 RDI: ffffc90002dcb66f
    RBP: ffff88810bc0f880 R08: aa69db2dda70ff01 R09: 0000ffaaaaaaaaaa
    R10: 0084000000ffaaaa R11: 0000000000000000 R12: ffff88810d65a084
    R13: dffffc0000000000 R14: 00000000000002a0 R15: ffff88810d65a000
    FS:  0000000000000000(0000) GS:ffff88811ac00000(0000) knlGS:0000000000000000
    CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    CR2: 0000000020000100 CR3: 0000000103268003 CR4: 0000000000770ef0
    PKRU: 55555554
    Call Trace:
     <TASK>
     l2cap_le_connect_req net/bluetooth/l2cap_core.c:4902 [inline]
     l2cap_le_sig_cmd net/bluetooth/l2cap_core.c:5420 [inline]
     l2cap_le_sig_channel net/bluetooth/l2cap_core.c:5486 [inline]
     l2cap_recv_frame+0xe59d/0x11710 net/bluetooth/l2cap_core.c:6809
     l2cap_recv_acldata+0x544/0x10a0 net/bluetooth/l2cap_core.c:7506
     hci_acldata_packet net/bluetooth/hci_core.c:3939 [inline]
     hci_rx_work+0x5e5/0xb20 net/bluetooth/hci_core.c:4176
     process_one_work kernel/workqueue.c:3254 [inline]
     process_scheduled_works+0x90f/0x1530 kernel/workqueue.c:3335
     worker_thread+0x926/0xe70 kernel/workqueue.c:3416
     kthread+0x2e3/0x380 kernel/kthread.c:388
     ret_from_fork+0x5c/0x90 arch/x86/kernel/process.c:147
     ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244
     </TASK>
    Modules linked in:
    ---[ end trace 0000000000000000 ]---

    Fixes: 6ed58ec520 ("Bluetooth: Use LE buffers for LE traffic")
    Suggested-by: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
    Signed-off-by: Sungwoo Kim <iam@sung-woo.kim>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
```

Signed-off-by: Bastien Nocera <bnocera@redhat.com>

Approved-by: José Ignacio Tornos Martínez <jtornosm@redhat.com>
Approved-by: Tony Camuso <tcamuso@redhat.com>
Approved-by: David Marlin <dmarlin@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-10-16 12:13:04 +00:00
Rado Vrbovsky 5733c78a14 Merge: CVE-2024-41062 kernel: bluetooth/l2cap: sync sock recv cb and release
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5068

bluetooth/l2cap: sync sock recv cb and release

JIRA: https://issues.redhat.com/browse/RHEL-51202
CVE: CVE-2024-41062

```
commit 89e856e124f9ae548572c56b1b70c2255705f8fe
Author: Edward Adam Davis <eadavis@qq.com>
Date:   Sat Jun 15 09:45:54 2024 +0800

    bluetooth/l2cap: sync sock recv cb and release

    The problem occurs between the system call to close the sock and hci_rx_work,
    where the former releases the sock and the latter accesses it without lock protection.

               CPU0                       CPU1
               ----                       ----
               sock_close                 hci_rx_work
               l2cap_sock_release         hci_acldata_packet
               l2cap_sock_kill            l2cap_recv_frame
               sk_free                    l2cap_conless_channel
                                          l2cap_sock_recv_cb

    If hci_rx_work processes the data that needs to be received before the sock is
    closed, then everything is normal; Otherwise, the work thread may access the
    released sock when receiving data.

    Add a chan mutex in the rx callback of the sock to achieve synchronization between
    the sock release and recv cb.

    Sock is dead, so set chan data to NULL, avoid others use invalid sock pointer.

    Reported-and-tested-by: syzbot+b7f6f8c9303466e16c8a@syzkaller.appspotmail.com
    Signed-off-by: Edward Adam Davis <eadavis@qq.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
```

Signed-off-by: Bastien Nocera <bnocera@redhat.com>

Approved-by: José Ignacio Tornos Martínez <jtornosm@redhat.com>
Approved-by: Tony Camuso <tcamuso@redhat.com>
Approved-by: David Marlin <dmarlin@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-10-16 12:09:08 +00:00
Rado Vrbovsky 085053f78f Merge: CVE-2024-27399: Bluetooth: l2cap: fix null-ptr-deref in l2cap_chan_timeout
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/4954

JIRA: https://issues.redhat.com/browse/RHEL-36374  
CVE: CVE-2024-27399

```
Bluetooth: l2cap: fix null-ptr-deref in l2cap_chan_timeout

There is a race condition between l2cap_chan_timeout() and
l2cap_chan_del(). When we use l2cap_chan_del() to delete the
channel, the chan->conn will be set to null. But the conn could
be dereferenced again in the mutex_lock() of l2cap_chan_timeout().
As a result the null pointer dereference bug will happen. The
KASAN report triggered by POC is shown below:

[  472.074580] ==================================================================
[  472.075284] BUG: KASAN: null-ptr-deref in mutex_lock+0x68/0xc0
[  472.075308] Write of size 8 at addr 0000000000000158 by task kworker/0:0/7
[  472.075308]
[  472.075308] CPU: 0 PID: 7 Comm: kworker/0:0 Not tainted 6.9.0-rc5-00356-g78c0094a146b #36
[  472.075308] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu4
[  472.075308] Workqueue: events l2cap_chan_timeout
[  472.075308] Call Trace:
[  472.075308]  <TASK>
[  472.075308]  dump_stack_lvl+0x137/0x1a0
[  472.075308]  print_report+0x101/0x250
[  472.075308]  ? __virt_addr_valid+0x77/0x160
[  472.075308]  ? mutex_lock+0x68/0xc0
[  472.075308]  kasan_report+0x139/0x170
[  472.075308]  ? mutex_lock+0x68/0xc0
[  472.075308]  kasan_check_range+0x2c3/0x2e0
[  472.075308]  mutex_lock+0x68/0xc0
[  472.075308]  l2cap_chan_timeout+0x181/0x300
[  472.075308]  process_one_work+0x5d2/0xe00
[  472.075308]  worker_thread+0xe1d/0x1660
[  472.075308]  ? pr_cont_work+0x5e0/0x5e0
[  472.075308]  kthread+0x2b7/0x350
[  472.075308]  ? pr_cont_work+0x5e0/0x5e0
[  472.075308]  ? kthread_blkcg+0xd0/0xd0
[  472.075308]  ret_from_fork+0x4d/0x80
[  472.075308]  ? kthread_blkcg+0xd0/0xd0
[  472.075308]  ret_from_fork_asm+0x11/0x20
[  472.075308]  </TASK>
[  472.075308] ==================================================================
[  472.094860] Disabling lock debugging due to kernel taint
[  472.096136] BUG: kernel NULL pointer dereference, address: 0000000000000158
[  472.096136] #PF: supervisor write access in kernel mode
[  472.096136] #PF: error_code(0x0002) - not-present page
[  472.096136] PGD 0 P4D 0
[  472.096136] Oops: 0002 [#1] PREEMPT SMP KASAN NOPTI
[  472.096136] CPU: 0 PID: 7 Comm: kworker/0:0 Tainted: G    B              6.9.0-rc5-00356-g78c0094a146b #36
[  472.096136] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu4
[  472.096136] Workqueue: events l2cap_chan_timeout
[  472.096136] RIP: 0010:mutex_lock+0x88/0xc0
[  472.096136] Code: be 08 00 00 00 e8 f8 23 1f fd 4c 89 f7 be 08 00 00 00 e8 eb 23 1f fd 42 80 3c 23 00 74 08 48 88
[  472.096136] RSP: 0018:ffff88800744fc78 EFLAGS: 00000246
[  472.096136] RAX: 0000000000000000 RBX: 1ffff11000e89f8f RCX: ffffffff8457c865
[  472.096136] RDX: 0000000000000001 RSI: 0000000000000008 RDI: ffff88800744fc78
[  472.096136] RBP: 0000000000000158 R08: ffff88800744fc7f R09: 1ffff11000e89f8f
[  472.096136] R10: dffffc0000000000 R11: ffffed1000e89f90 R12: dffffc0000000000
[  472.096136] R13: 0000000000000158 R14: ffff88800744fc78 R15: ffff888007405a00
[  472.096136] FS:  0000000000000000(0000) GS:ffff88806d200000(0000) knlGS:0000000000000000
[  472.096136] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  472.096136] CR2: 0000000000000158 CR3: 000000000da32000 CR4: 00000000000006f0
[  472.096136] Call Trace:
[  472.096136]  <TASK>
[  472.096136]  ? __die_body+0x8d/0xe0
[  472.096136]  ? page_fault_oops+0x6b8/0x9a0
[  472.096136]  ? kernelmode_fixup_or_oops+0x20c/0x2a0
[  472.096136]  ? do_user_addr_fault+0x1027/0x1340
[  472.096136]  ? _printk+0x7a/0xa0
[  472.096136]  ? mutex_lock+0x68/0xc0
[  472.096136]  ? add_taint+0x42/0xd0
[  472.096136]  ? exc_page_fault+0x6a/0x1b0
[  472.096136]  ? asm_exc_page_fault+0x26/0x30
[  472.096136]  ? mutex_lock+0x75/0xc0
[  472.096136]  ? mutex_lock+0x88/0xc0
[  472.096136]  ? mutex_lock+0x75/0xc0
[  472.096136]  l2cap_chan_timeout+0x181/0x300
[  472.096136]  process_one_work+0x5d2/0xe00
[  472.096136]  worker_thread+0xe1d/0x1660
[  472.096136]  ? pr_cont_work+0x5e0/0x5e0
[  472.096136]  kthread+0x2b7/0x350
[  472.096136]  ? pr_cont_work+0x5e0/0x5e0
[  472.096136]  ? kthread_blkcg+0xd0/0xd0
[  472.096136]  ret_from_fork+0x4d/0x80
[  472.096136]  ? kthread_blkcg+0xd0/0xd0
[  472.096136]  ret_from_fork_asm+0x11/0x20
[  472.096136]  </TASK>
[  472.096136] Modules linked in:
[  472.096136] CR2: 0000000000000158
[  472.096136] ---[ end trace 0000000000000000 ]---
[  472.096136] RIP: 0010:mutex_lock+0x88/0xc0
[  472.096136] Code: be 08 00 00 00 e8 f8 23 1f fd 4c 89 f7 be 08 00 00 00 e8 eb 23 1f fd 42 80 3c 23 00 74 08 48 88
[  472.096136] RSP: 0018:ffff88800744fc78 EFLAGS: 00000246
[  472.096136] RAX: 0000000000000000 RBX: 1ffff11000e89f8f RCX: ffffffff8457c865
[  472.096136] RDX: 0000000000000001 RSI: 0000000000000008 RDI: ffff88800744fc78
[  472.096136] RBP: 0000000000000158 R08: ffff88800744fc7f R09: 1ffff11000e89f8f
[  472.132932] R10: dffffc0000000000 R11: ffffed1000e89f90 R12: dffffc0000000000
[  472.132932] R13: 0000000000000158 R14: ffff88800744fc78 R15: ffff888007405a00
[  472.132932] FS:  0000000000000000(0000) GS:ffff88806d200000(0000) knlGS:0000000000000000
[  472.132932] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  472.132932] CR2: 0000000000000158 CR3: 000000000da32000 CR4: 00000000000006f0
[  472.132932] Kernel panic - not syncing: Fatal exception
[  472.132932] Kernel Offset: disabled
[  472.132932] ---[ end Kernel panic - not syncing: Fatal exception ]---

Add a check to judge whether the conn is null in l2cap_chan_timeout()
in order to mitigate the bug.

Fixes: 3df91ea20e ("Bluetooth: Revert to mutexes from RCU list")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
(cherry picked from commit adf0398cee86643b8eacde95f17d073d022f782c)
```

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>

Approved-by: Jarod Wilson <jarod@redhat.com>
Approved-by: Bastien Nocera <bnocera@redhat.com>
Approved-by: David Marlin <dmarlin@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-10-02 08:40:15 +00:00
Bastien Nocera cbd07de332 Bluetooth: l2cap: Don't double set the HCI_CONN_MGMT_CONNECTED bit
JIRA: https://issues.redhat.com/browse/RHEL-41144
CVE: CVE-2024-36968

commit 600b0bbe73d3a9a264694da0e4c2c0800309141e
Author: Archie Pusaka <apusaka@chromium.org>
Date:   Thu Apr 4 18:50:23 2024 +0800

    Bluetooth: l2cap: Don't double set the HCI_CONN_MGMT_CONNECTED bit

    The bit is set and tested inside mgmt_device_connected(), therefore we
    must not set it just outside the function.

    Fixes: eeda1bf97bb5 ("Bluetooth: hci_event: Fix not indicating new connection for BIG Sync")
    Signed-off-by: Archie Pusaka <apusaka@chromium.org>
    Reviewed-by: Manish Mandlik <mmandlik@chromium.org>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-09-11 10:15:38 +02:00
Bastien Nocera 7007afb70e Bluetooth: L2CAP: Fix div-by-zero in l2cap_le_flowctl_init()
JIRA: https://issues.redhat.com/browse/RHEL-41144
CVE: CVE-2024-36968

commit a5b862c6a221459d54e494e88965b48dcfa6cc44
Author: Sungwoo Kim <iam@sung-woo.kim>
Date:   Sat May 4 15:23:29 2024 -0400

    Bluetooth: L2CAP: Fix div-by-zero in l2cap_le_flowctl_init()

    l2cap_le_flowctl_init() can cause both div-by-zero and an integer
    overflow since hdev->le_mtu may not fall in the valid range.

    Move MTU from hci_dev to hci_conn to validate MTU and stop the connection
    process earlier if MTU is invalid.
    Also, add a missing validation in read_buffer_size() and make it return
    an error value if the validation fails.
    Now hci_conn_add() returns ERR_PTR() as it can fail due to the both a
    kzalloc failure and invalid MTU value.

    divide error: 0000 [#1] PREEMPT SMP KASAN NOPTI
    CPU: 0 PID: 67 Comm: kworker/u5:0 Tainted: G        W          6.9.0-rc5+ #20
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
    Workqueue: hci0 hci_rx_work
    RIP: 0010:l2cap_le_flowctl_init+0x19e/0x3f0 net/bluetooth/l2cap_core.c:547
    Code: e8 17 17 0c 00 66 41 89 9f 84 00 00 00 bf 01 00 00 00 41 b8 02 00 00 00 4c
    89 fe 4c 89 e2 89 d9 e8 27 17 0c 00 44 89 f0 31 d2 <66> f7 f3 89 c3 ff c3 4d 8d
    b7 88 00 00 00 4c 89 f0 48 c1 e8 03 42
    RSP: 0018:ffff88810bc0f858 EFLAGS: 00010246
    RAX: 00000000000002a0 RBX: 0000000000000000 RCX: dffffc0000000000
    RDX: 0000000000000000 RSI: ffff88810bc0f7c0 RDI: ffffc90002dcb66f
    RBP: ffff88810bc0f880 R08: aa69db2dda70ff01 R09: 0000ffaaaaaaaaaa
    R10: 0084000000ffaaaa R11: 0000000000000000 R12: ffff88810d65a084
    R13: dffffc0000000000 R14: 00000000000002a0 R15: ffff88810d65a000
    FS:  0000000000000000(0000) GS:ffff88811ac00000(0000) knlGS:0000000000000000
    CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    CR2: 0000000020000100 CR3: 0000000103268003 CR4: 0000000000770ef0
    PKRU: 55555554
    Call Trace:
     <TASK>
     l2cap_le_connect_req net/bluetooth/l2cap_core.c:4902 [inline]
     l2cap_le_sig_cmd net/bluetooth/l2cap_core.c:5420 [inline]
     l2cap_le_sig_channel net/bluetooth/l2cap_core.c:5486 [inline]
     l2cap_recv_frame+0xe59d/0x11710 net/bluetooth/l2cap_core.c:6809
     l2cap_recv_acldata+0x544/0x10a0 net/bluetooth/l2cap_core.c:7506
     hci_acldata_packet net/bluetooth/hci_core.c:3939 [inline]
     hci_rx_work+0x5e5/0xb20 net/bluetooth/hci_core.c:4176
     process_one_work kernel/workqueue.c:3254 [inline]
     process_scheduled_works+0x90f/0x1530 kernel/workqueue.c:3335
     worker_thread+0x926/0xe70 kernel/workqueue.c:3416
     kthread+0x2e3/0x380 kernel/kthread.c:388
     ret_from_fork+0x5c/0x90 arch/x86/kernel/process.c:147
     ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244
     </TASK>
    Modules linked in:
    ---[ end trace 0000000000000000 ]---

    Fixes: 6ed58ec520 ("Bluetooth: Use LE buffers for LE traffic")
    Suggested-by: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
    Signed-off-by: Sungwoo Kim <iam@sung-woo.kim>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-09-11 10:15:38 +02:00
Bastien Nocera afe06cd638 Bluetooth: L2CAP: Fix deadlock
JIRA: https://issues.redhat.com/browse/RHEL-51202
CVE: CVE-2024-41062

commit f1a8f402f13f94263cf349216c257b2985100927
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon Jun 24 09:42:09 2024 -0400

    Bluetooth: L2CAP: Fix deadlock

    This fixes the following deadlock introduced by 39a92a55be13
    ("bluetooth/l2cap: sync sock recv cb and release")

    ============================================
    WARNING: possible recursive locking detected
    6.10.0-rc3-g4029dba6b6f1 #6823 Not tainted
    --------------------------------------------
    kworker/u5:0/35 is trying to acquire lock:
    ffff888002ec2510 (&chan->lock#2/1){+.+.}-{3:3}, at:
    l2cap_sock_recv_cb+0x44/0x1e0

    but task is already holding lock:
    ffff888002ec2510 (&chan->lock#2/1){+.+.}-{3:3}, at:
    l2cap_get_chan_by_scid+0xaf/0xd0

    other info that might help us debug this:
     Possible unsafe locking scenario:

           CPU0
           ----
      lock(&chan->lock#2/1);
      lock(&chan->lock#2/1);

     *** DEADLOCK ***

     May be due to missing lock nesting notation

    3 locks held by kworker/u5:0/35:
     #0: ffff888002b8a940 ((wq_completion)hci0#2){+.+.}-{0:0}, at:
    process_one_work+0x750/0x930
     #1: ffff888002c67dd0 ((work_completion)(&hdev->rx_work)){+.+.}-{0:0},
    at: process_one_work+0x44e/0x930
     #2: ffff888002ec2510 (&chan->lock#2/1){+.+.}-{3:3}, at:
    l2cap_get_chan_by_scid+0xaf/0xd0

    To fix the original problem this introduces l2cap_chan_lock at
    l2cap_conless_channel to ensure that l2cap_sock_recv_cb is called with
    chan->lock held.

    Fixes: 89e856e124f9 ("bluetooth/l2cap: sync sock recv cb and release")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-08-21 11:13:56 +02:00
Bastien Nocera 66c5ffd7cf Bluetooth: compute LE flow credits based on recvbuf space
JIRA: https://issues.redhat.com/browse/RHEL-51202
CVE: CVE-2024-41062

commit ce60b9231b66710b6ee24042ded26efee120ecfc
Author: Sebastian Urban <surban@surban.net>
Date:   Wed May 1 12:08:58 2024 +0200

    Bluetooth: compute LE flow credits based on recvbuf space

    Previously LE flow credits were returned to the
    sender even if the socket's receive buffer was
    full. This meant that no back-pressure
    was applied to the sender, thus it continued to
    send data, resulting in data loss without any
    error being reported. Furthermore, the amount
    of credits was essentially fixed to a small
    amount, leading to reduced performance.

    This is fixed by computing the number of returned
    LE flow credits based on the estimated available
    space in the receive buffer of an L2CAP socket.
    Consequently, if the receive buffer is full, no
    credits are returned until the buffer is read and
    thus cleared by user-space.

    Since the computation of available receive buffer
    space can only be performed approximately (due to
    sk_buff overhead) and the receive buffer size may
    be changed by user-space after flow credits have
    been sent, superfluous received data is temporary
    stored within l2cap_pinfo. This is necessary
    because Bluetooth LE provides no retransmission
    mechanism once the data has been acked by the
    physical layer.

    If receive buffer space estimation is not possible
    at the moment, we fall back to providing credits
    for one full packet as before. This is currently
    the case during connection setup, when MPS is not
    yet available.

    Fixes: b1c325c23d ("Bluetooth: Implement returning of LE L2CAP credits")
    Signed-off-by: Sebastian Urban <surban@surban.net>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-08-21 11:13:51 +02:00
Bastien Nocera f71efdc307 Bluetooth: fix connection setup in l2cap_connect
JIRA: https://issues.redhat.com/browse/RHEL-38459
CVE: CVE-2024-36013

commit c695439d198d30e10553a3b98360c5efe77b6903
Author: Pauli Virtanen <pav@iki.fi>
Date:   Sun Jun 9 18:06:20 2024 +0300

    Bluetooth: fix connection setup in l2cap_connect

    The amp_id argument of l2cap_connect() was removed in
    commit 84a4bb6548a2 ("Bluetooth: HCI: Remove HCI_AMP support")

    It was always called with amp_id == 0, i.e. AMP_ID_BREDR == 0x00 (ie.
    non-AMP controller).  In the above commit, the code path for amp_id != 0
    was preserved, although it should have used the amp_id == 0 one.

    Restore the previous behavior of the non-AMP code path, to fix problems
    with L2CAP connections.

    Fixes: 84a4bb6548a2 ("Bluetooth: HCI: Remove HCI_AMP support")
    Signed-off-by: Pauli Virtanen <pav@iki.fi>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-08-19 13:48:20 +02:00
Bastien Nocera d67f530cde Bluetooth: HCI: Remove HCI_AMP support
JIRA: https://issues.redhat.com/browse/RHEL-38459
CVE: CVE-2024-36013

commit 84a4bb6548a29326564f0e659fb8064503ecc1c7
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon May 6 18:33:52 2024 -0400

    Bluetooth: HCI: Remove HCI_AMP support

    Since BT_HS has been remove HCI_AMP controllers no longer has any use so
    remove it along with the capability of creating AMP controllers.

    Since we no longer need to differentiate between AMP and Primary
    controllers, as only HCI_PRIMARY is left, this also remove
    hdev->dev_type altogether.

    Fixes: e7b02296fb40 ("Bluetooth: Remove BT_HS")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-08-19 13:48:20 +02:00
Bastien Nocera de7b0dabf1 Bluetooth: L2CAP: Fix slab-use-after-free in l2cap_connect()
JIRA: https://issues.redhat.com/browse/RHEL-38459
CVE: CVE-2024-36013

commit 4d7b41c0e43995b0e992b9f8903109275744b658
Author: Sungwoo Kim <iam@sung-woo.kim>
Date:   Tue Apr 30 02:32:10 2024 -0400

    Bluetooth: L2CAP: Fix slab-use-after-free in l2cap_connect()

    Extend a critical section to prevent chan from early freeing.
    Also make the l2cap_connect() return type void. Nothing is using the
    returned value but it is ugly to return a potentially freed pointer.
    Making it void will help with backports because earlier kernels did use
    the return value. Now the compile will break for kernels where this
    patch is not a complete fix.

    Call stack summary:

    [use]
    l2cap_bredr_sig_cmd
      l2cap_connect
      ┌ mutex_lock(&conn->chan_lock);
      │ chan = pchan->ops->new_connection(pchan); <- alloc chan
      │ __l2cap_chan_add(conn, chan);
      │   l2cap_chan_hold(chan);
      │   list_add(&chan->list, &conn->chan_l);   ... (1)
      └ mutex_unlock(&conn->chan_lock);
        chan->conf_state              ... (4) <- use after free

    [free]
    l2cap_conn_del
    ┌ mutex_lock(&conn->chan_lock);
    │ foreach chan in conn->chan_l:            ... (2)
    │   l2cap_chan_put(chan);
    │     l2cap_chan_destroy
    │       kfree(chan)               ... (3) <- chan freed
    └ mutex_unlock(&conn->chan_lock);

    ==================================================================
    BUG: KASAN: slab-use-after-free in instrument_atomic_read
    include/linux/instrumented.h:68 [inline]
    BUG: KASAN: slab-use-after-free in _test_bit
    include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline]
    BUG: KASAN: slab-use-after-free in l2cap_connect+0xa67/0x11a0
    net/bluetooth/l2cap_core.c:4260
    Read of size 8 at addr ffff88810bf040a0 by task kworker/u3:1/311

    Fixes: 73ffa904b7 ("Bluetooth: Move conf_{req,rsp} stuff to struct l2cap_chan")
    Signed-off-by: Sungwoo Kim <iam@sung-woo.kim>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-08-19 13:48:19 +02:00
Bastien Nocera 0db5333ac9 Bluetooth: Remove BT_HS
JIRA: https://issues.redhat.com/browse/RHEL-38459
CVE: CVE-2024-36013

Conflicts: the one-line code conflict is in removed code, and avoids
us having pick up multi-system commit de4eda9de2d9 ("use less confusing
names for iov_iter direction initializers")

commit e7b02296fb400ee64822fbdd81a0718449066333
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Thu Feb 1 11:18:58 2024 -0500

    Bluetooth: Remove BT_HS

    High Speed, Alternate MAC and PHY (AMP) extension, has been removed from
    Bluetooth Core specification on 5.3:

    https://www.bluetooth.com/blog/new-core-specification-v5-3-feature-enhancements/

    Fixes: 244bc37759 ("Bluetooth: Add BT_HS config option")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2024-08-19 13:48:19 +02:00
CKI Backport Bot 6fc0774432 Bluetooth: l2cap: fix null-ptr-deref in l2cap_chan_timeout
JIRA: https://issues.redhat.com/browse/RHEL-36374
CVE: CVE-2024-27399

commit adf0398cee86643b8eacde95f17d073d022f782c
Author: Duoming Zhou <duoming@zju.edu.cn>
Date:   Thu May 2 20:57:36 2024 +0800

    Bluetooth: l2cap: fix null-ptr-deref in l2cap_chan_timeout

    There is a race condition between l2cap_chan_timeout() and
    l2cap_chan_del(). When we use l2cap_chan_del() to delete the
    channel, the chan->conn will be set to null. But the conn could
    be dereferenced again in the mutex_lock() of l2cap_chan_timeout().
    As a result the null pointer dereference bug will happen. The
    KASAN report triggered by POC is shown below:

    [  472.074580] ==================================================================
    [  472.075284] BUG: KASAN: null-ptr-deref in mutex_lock+0x68/0xc0
    [  472.075308] Write of size 8 at addr 0000000000000158 by task kworker/0:0/7
    [  472.075308]
    [  472.075308] CPU: 0 PID: 7 Comm: kworker/0:0 Not tainted 6.9.0-rc5-00356-g78c0094a146b #36
    [  472.075308] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu4
    [  472.075308] Workqueue: events l2cap_chan_timeout
    [  472.075308] Call Trace:
    [  472.075308]  <TASK>
    [  472.075308]  dump_stack_lvl+0x137/0x1a0
    [  472.075308]  print_report+0x101/0x250
    [  472.075308]  ? __virt_addr_valid+0x77/0x160
    [  472.075308]  ? mutex_lock+0x68/0xc0
    [  472.075308]  kasan_report+0x139/0x170
    [  472.075308]  ? mutex_lock+0x68/0xc0
    [  472.075308]  kasan_check_range+0x2c3/0x2e0
    [  472.075308]  mutex_lock+0x68/0xc0
    [  472.075308]  l2cap_chan_timeout+0x181/0x300
    [  472.075308]  process_one_work+0x5d2/0xe00
    [  472.075308]  worker_thread+0xe1d/0x1660
    [  472.075308]  ? pr_cont_work+0x5e0/0x5e0
    [  472.075308]  kthread+0x2b7/0x350
    [  472.075308]  ? pr_cont_work+0x5e0/0x5e0
    [  472.075308]  ? kthread_blkcg+0xd0/0xd0
    [  472.075308]  ret_from_fork+0x4d/0x80
    [  472.075308]  ? kthread_blkcg+0xd0/0xd0
    [  472.075308]  ret_from_fork_asm+0x11/0x20
    [  472.075308]  </TASK>
    [  472.075308] ==================================================================
    [  472.094860] Disabling lock debugging due to kernel taint
    [  472.096136] BUG: kernel NULL pointer dereference, address: 0000000000000158
    [  472.096136] #PF: supervisor write access in kernel mode
    [  472.096136] #PF: error_code(0x0002) - not-present page
    [  472.096136] PGD 0 P4D 0
    [  472.096136] Oops: 0002 [#1] PREEMPT SMP KASAN NOPTI
    [  472.096136] CPU: 0 PID: 7 Comm: kworker/0:0 Tainted: G    B              6.9.0-rc5-00356-g78c0094a146b #36
    [  472.096136] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu4
    [  472.096136] Workqueue: events l2cap_chan_timeout
    [  472.096136] RIP: 0010:mutex_lock+0x88/0xc0
    [  472.096136] Code: be 08 00 00 00 e8 f8 23 1f fd 4c 89 f7 be 08 00 00 00 e8 eb 23 1f fd 42 80 3c 23 00 74 08 48 88
    [  472.096136] RSP: 0018:ffff88800744fc78 EFLAGS: 00000246
    [  472.096136] RAX: 0000000000000000 RBX: 1ffff11000e89f8f RCX: ffffffff8457c865
    [  472.096136] RDX: 0000000000000001 RSI: 0000000000000008 RDI: ffff88800744fc78
    [  472.096136] RBP: 0000000000000158 R08: ffff88800744fc7f R09: 1ffff11000e89f8f
    [  472.096136] R10: dffffc0000000000 R11: ffffed1000e89f90 R12: dffffc0000000000
    [  472.096136] R13: 0000000000000158 R14: ffff88800744fc78 R15: ffff888007405a00
    [  472.096136] FS:  0000000000000000(0000) GS:ffff88806d200000(0000) knlGS:0000000000000000
    [  472.096136] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    [  472.096136] CR2: 0000000000000158 CR3: 000000000da32000 CR4: 00000000000006f0
    [  472.096136] Call Trace:
    [  472.096136]  <TASK>
    [  472.096136]  ? __die_body+0x8d/0xe0
    [  472.096136]  ? page_fault_oops+0x6b8/0x9a0
    [  472.096136]  ? kernelmode_fixup_or_oops+0x20c/0x2a0
    [  472.096136]  ? do_user_addr_fault+0x1027/0x1340
    [  472.096136]  ? _printk+0x7a/0xa0
    [  472.096136]  ? mutex_lock+0x68/0xc0
    [  472.096136]  ? add_taint+0x42/0xd0
    [  472.096136]  ? exc_page_fault+0x6a/0x1b0
    [  472.096136]  ? asm_exc_page_fault+0x26/0x30
    [  472.096136]  ? mutex_lock+0x75/0xc0
    [  472.096136]  ? mutex_lock+0x88/0xc0
    [  472.096136]  ? mutex_lock+0x75/0xc0
    [  472.096136]  l2cap_chan_timeout+0x181/0x300
    [  472.096136]  process_one_work+0x5d2/0xe00
    [  472.096136]  worker_thread+0xe1d/0x1660
    [  472.096136]  ? pr_cont_work+0x5e0/0x5e0
    [  472.096136]  kthread+0x2b7/0x350
    [  472.096136]  ? pr_cont_work+0x5e0/0x5e0
    [  472.096136]  ? kthread_blkcg+0xd0/0xd0
    [  472.096136]  ret_from_fork+0x4d/0x80
    [  472.096136]  ? kthread_blkcg+0xd0/0xd0
    [  472.096136]  ret_from_fork_asm+0x11/0x20
    [  472.096136]  </TASK>
    [  472.096136] Modules linked in:
    [  472.096136] CR2: 0000000000000158
    [  472.096136] ---[ end trace 0000000000000000 ]---
    [  472.096136] RIP: 0010:mutex_lock+0x88/0xc0
    [  472.096136] Code: be 08 00 00 00 e8 f8 23 1f fd 4c 89 f7 be 08 00 00 00 e8 eb 23 1f fd 42 80 3c 23 00 74 08 48 88
    [  472.096136] RSP: 0018:ffff88800744fc78 EFLAGS: 00000246
    [  472.096136] RAX: 0000000000000000 RBX: 1ffff11000e89f8f RCX: ffffffff8457c865
    [  472.096136] RDX: 0000000000000001 RSI: 0000000000000008 RDI: ffff88800744fc78
    [  472.096136] RBP: 0000000000000158 R08: ffff88800744fc7f R09: 1ffff11000e89f8f
    [  472.132932] R10: dffffc0000000000 R11: ffffed1000e89f90 R12: dffffc0000000000
    [  472.132932] R13: 0000000000000158 R14: ffff88800744fc78 R15: ffff888007405a00
    [  472.132932] FS:  0000000000000000(0000) GS:ffff88806d200000(0000) knlGS:0000000000000000
    [  472.132932] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    [  472.132932] CR2: 0000000000000158 CR3: 000000000da32000 CR4: 00000000000006f0
    [  472.132932] Kernel panic - not syncing: Fatal exception
    [  472.132932] Kernel Offset: disabled
    [  472.132932] ---[ end Kernel panic - not syncing: Fatal exception ]---

    Add a check to judge whether the conn is null in l2cap_chan_timeout()
    in order to mitigate the bug.

    Fixes: 3df91ea20e ("Bluetooth: Revert to mutexes from RCU list")
    Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2024-08-08 07:55:51 +00:00
David Marlin 13b620b9f6 Bluetooth: Enforce validation on max value of connection interval
JIRA: https://issues.redhat.com/browse/RHEL-30099

commit e4b019515f950b4e6e5b74b2e1bb03a90cb33039
Author: Kai-Heng Feng <kai.heng.feng@canonical.com>
Date:   Thu Jan 25 14:50:28 2024 +0800

    Bluetooth: Enforce validation on max value of connection interval

    Right now Linux BT stack cannot pass test case "GAP/CONN/CPUP/BV-05-C
    'Connection Parameter Update Procedure Invalid Parameters Central
    Responder'" in Bluetooth Test Suite revision GAP.TS.p44. [0]

    That was revoled by commit c49a8682fc ("Bluetooth: validate BLE
    connection interval updates"), but later got reverted due to devices
    like keyboards and mice may require low connection interval.

    So only validate the max value connection interval to pass the Test
    Suite, and let devices to request low connection interval if needed.

    [0] https://www.bluetooth.org/docman/handlers/DownloadDoc.ashx?doc_id=229869

    Fixes: 68d19d7d99 ("Revert "Bluetooth: validate BLE connection interval updates"")
    Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: David Marlin <dmarlin@redhat.com>
2024-05-22 21:37:33 -05:00
David Marlin 605a58af4a Bluetooth: L2CAP: Fix possible multiple reject send
JIRA: https://issues.redhat.com/browse/RHEL-30099

commit 96a3398b467ab8aada3df2f3a79f4b7835d068b8
Author: Frédéric Danis <frederic.danis@collabora.com>
Date:   Tue Dec 19 09:10:22 2023 +0100

    Bluetooth: L2CAP: Fix possible multiple reject send

    In case of an incomplete command or a command with a null identifier 2
    reject packets will be sent, one with the identifier and one with 0.
    Consuming the data of the command will prevent it.
    This allows to send a reject packet for each corrupted command in a
    multi-command packet.

    Signed-off-by: Frédéric Danis <frederic.danis@collabora.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: David Marlin <dmarlin@redhat.com>
2024-05-22 21:35:10 -05:00
David Marlin d0520167ca Bluetooth: L2CAP: Send reject on command corrupted request
JIRA: https://issues.redhat.com/browse/RHEL-30099

commit 78b99eb1faa7371bf9c534690f26a71b6996622d
Author: Frédéric Danis <frederic.danis@collabora.com>
Date:   Fri Dec 8 18:41:50 2023 +0100

    Bluetooth: L2CAP: Send reject on command corrupted request

    L2CAP/COS/CED/BI-02-C PTS test send a malformed L2CAP signaling packet
    with 2 commands in it (a connection request and an unknown command) and
    expect to get a connection response packet and a command reject packet.
    The second is currently not sent.

    Cc: stable@vger.kernel.org
    Signed-off-by: Frédéric Danis <frederic.danis@collabora.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: David Marlin <dmarlin@redhat.com>
2024-05-22 21:35:09 -05:00
Bastien Nocera 26411aba44 Bluetooth: L2CAP: Fix use-after-free
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit f752a0b334bb95fe9b42ecb511e0864e2768046f
Author: Zhengping Jiang <jiangzp@google.com>
Date:   Wed May 24 17:04:15 2023 -0700

    Bluetooth: L2CAP: Fix use-after-free

    Fix potential use-after-free in l2cap_le_command_rej.

    Signed-off-by: Zhengping Jiang <jiangzp@google.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-11-07 11:09:04 +01:00
Bastien Nocera f75de32d4f Bluetooth: L2CAP: Add missing checks for invalid DCID
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit 75767213f3d9b97f63694d02260b6a49a2271876
Author: Sungwoo Kim <iam@sung-woo.kim>
Date:   Sat Jun 3 08:28:09 2023 -0400

    Bluetooth: L2CAP: Add missing checks for invalid DCID

    When receiving a connect response we should make sure that the DCID is
    within the valid range and that we don't already have another channel
    allocated for the same DCID.
    Missing checks may violate the specification (BLUETOOTH CORE SPECIFICATION
    Version 5.4 | Vol 3, Part A, Page 1046).

    Fixes: 40624183c2 ("Bluetooth: L2CAP: Add missing checks for invalid LE DCID")
    Signed-off-by: Sungwoo Kim <iam@sung-woo.kim>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-11-07 11:09:01 +01:00
Bastien Nocera c414dc7b45 Bluetooth: Fix l2cap_disconnect_req deadlock
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit 02c5ea5246a44d6ffde0fddebfc1d56188052976
Author: Ying Hsu <yinghsu@chromium.org>
Date:   Wed May 31 03:44:56 2023 +0000

    Bluetooth: Fix l2cap_disconnect_req deadlock

    L2CAP assumes that the locks conn->chan_lock and chan->lock are
    acquired in the order conn->chan_lock, chan->lock to avoid
    potential deadlock.
    For example, l2sock_shutdown acquires these locks in the order:
      mutex_lock(&conn->chan_lock)
      l2cap_chan_lock(chan)

    However, l2cap_disconnect_req acquires chan->lock in
    l2cap_get_chan_by_scid first and then acquires conn->chan_lock
    before calling l2cap_chan_del. This means that these locks are
    acquired in unexpected order, which leads to potential deadlock:
      l2cap_chan_lock(c)
      mutex_lock(&conn->chan_lock)

    This patch releases chan->lock before acquiring the conn_chan_lock
    to avoid the potential deadlock.

    Fixes: a2a9339e1c9d ("Bluetooth: L2CAP: Fix use-after-free in l2cap_disconnect_{req,rsp}")
    Signed-off-by: Ying Hsu <yinghsu@chromium.org>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-11-07 11:09:01 +01:00
Bastien Nocera 5293cd45df Bluetooth: L2CAP: fix "bad unlock balance" in l2cap_disconnect_rsp
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit 25e97f7b1866e6b8503be349eeea44bb52d661ce
Author: Min Li <lm0963hack@gmail.com>
Date:   Mon Apr 17 10:27:54 2023 +0800

    Bluetooth: L2CAP: fix "bad unlock balance" in l2cap_disconnect_rsp

    conn->chan_lock isn't acquired before l2cap_get_chan_by_scid,
    if l2cap_get_chan_by_scid returns NULL, then 'bad unlock balance'
    is triggered.

    Reported-by: syzbot+9519d6b5b79cf7787cf3@syzkaller.appspotmail.com
    Link: https://lore.kernel.org/all/000000000000894f5f05f95e9f4d@google.com/
    Signed-off-by: Min Li <lm0963hack@gmail.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-11-07 11:08:59 +01:00
Bastien Nocera 59e017cd97 Bluetooth: L2CAP: Delay identity address updates
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit b8b23001b8025a61f0979578884a74faa825023e
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Wed Mar 8 16:16:31 2023 -0800

    Bluetooth: L2CAP: Delay identity address updates

    This delays the identity address updates to give time for userspace to
    process the new address otherwise there is a risk that userspace
    creates a duplicated device if the MGMT event is delayed for some
    reason.

    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-11-07 11:08:55 +01:00
Bastien Nocera 5ee09f8668 Bluetooth: L2CAP: Fix use-after-free in l2cap_disconnect_{req,rsp}
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit a2a9339e1c9deb7e1e079e12e27a0265aea8421a
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Thu Apr 6 09:33:09 2023 -0700

    Bluetooth: L2CAP: Fix use-after-free in l2cap_disconnect_{req,rsp}

    Similar to commit d0be8347c623 ("Bluetooth: L2CAP: Fix use-after-free
    caused by l2cap_chan_put"), just use l2cap_chan_hold_unless_zero to
    prevent referencing a channel that is about to be destroyed.

    Cc: stable@kernel.org
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Signed-off-by: Min Li <lm0963hack@gmail.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-11-07 11:08:54 +01:00
Bastien Nocera d660c0c04c Bluetooth: L2CAP: Fix responding with wrong PDU type
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit 9aa9d9473f1550d1936c31259720b3f1f4690576
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Wed Mar 8 14:20:34 2023 -0800

    Bluetooth: L2CAP: Fix responding with wrong PDU type

    L2CAP_ECRED_CONN_REQ shall be responded with L2CAP_ECRED_CONN_RSP not
    L2CAP_LE_CONN_RSP:

    L2CAP LE EATT Server - Reject - run
      Listening for connections
      New client connection with handle 0x002a
      Sending L2CAP Request from client
      Client received response code 0x15
      Unexpected L2CAP response code (expected 0x18)
    L2CAP LE EATT Server - Reject - test failed

    > ACL Data RX: Handle 42 flags 0x02 dlen 26
          LE L2CAP: Enhanced Credit Connection Request (0x17) ident 1 len 18
            PSM: 39 (0x0027)
            MTU: 64
            MPS: 64
            Credits: 5
            Source CID: 65
            Source CID: 66
            Source CID: 67
            Source CID: 68
            Source CID: 69
    < ACL Data TX: Handle 42 flags 0x00 dlen 16
          LE L2CAP: LE Connection Response (0x15) ident 1 len 8
            invalid size
            00 00 00 00 00 00 06 00

    L2CAP LE EATT Server - Reject - run
      Listening for connections
      New client connection with handle 0x002a
      Sending L2CAP Request from client
      Client received response code 0x18
    L2CAP LE EATT Server - Reject - test passed

    Fixes: 15f02b9105 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-10-23 09:05:33 +02:00
Bastien Nocera 225463b1f0 Bluetooth: L2CAP: Fix potential user-after-free
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit df5703348813235874d851934e957c3723d71644
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Wed Feb 1 14:01:11 2023 -0800

    Bluetooth: L2CAP: Fix potential user-after-free

    This fixes all instances of which requires to allocate a buffer calling
    alloc_skb which may release the chan lock and reacquire later which
    makes it possible that the chan is disconnected in the meantime.

    Fixes: a6a5568c03 ("Bluetooth: Lock the L2CAP channel when sending")
    Reported-by: Alexander Coffin <alex.coffin@matician.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-10-23 09:05:31 +02:00
Bastien Nocera 7baaa23bc5 Bluetooth: Add CONFIG_BT_LE_L2CAP_ECRED
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit 462fcd53924ccc21596d30edd0673fa7c939b392
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Thu Oct 27 16:18:04 2022 -0700

    Bluetooth: Add CONFIG_BT_LE_L2CAP_ECRED

    This adds CONFIG_BT_LE_L2CAP_ECRED which can be used to enable L2CAP
    Enhanced Credit Flow Control Mode by default, previously it was only
    possible to set it via module parameter (e.g. bluetooth.enable_ecred=1).

    Since L2CAP ECRED mode is required by the likes of EATT which is
    recommended for LE Audio this enables it by default.

    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Tested-By: Tedd Ho-Jeong An <tedd.an@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-10-23 09:05:24 +02:00
Bastien Nocera 2a7765b57c Bluetooth: L2CAP: Fix l2cap_global_chan_by_psm
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit f937b758a188d6fd328a81367087eddbb2fce50f
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon Oct 31 16:10:33 2022 -0700

    Bluetooth: L2CAP: Fix l2cap_global_chan_by_psm

    l2cap_global_chan_by_psm shall not return fixed channels as they are not
    meant to be connected by (S)PSM.

    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Reviewed-by: Tedd Ho-Jeong An <tedd.an@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-10-23 09:05:20 +02:00
Bastien Nocera 64a6d7a13f Bluetooth: L2CAP: Fix user-after-free
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit 35fcbc4243aad7e7d020b7c1dfb14bb888b20a4f
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Thu Sep 29 13:27:13 2022 -0700

    Bluetooth: L2CAP: Fix user-after-free

    This uses l2cap_chan_hold_unless_zero() after calling
    __l2cap_get_chan_blah() to prevent the following trace:

    Bluetooth: l2cap_core.c:static void l2cap_chan_destroy(struct kref
    *kref)
    Bluetooth: chan 0000000023c4974d
    Bluetooth: parent 00000000ae861c08
    ==================================================================
    BUG: KASAN: use-after-free in __mutex_waiter_is_first
    kernel/locking/mutex.c:191 [inline]
    BUG: KASAN: use-after-free in __mutex_lock_common
    kernel/locking/mutex.c:671 [inline]
    BUG: KASAN: use-after-free in __mutex_lock+0x278/0x400
    kernel/locking/mutex.c:729
    Read of size 8 at addr ffff888006a49b08 by task kworker/u3:2/389

    Link: https://lore.kernel.org/lkml/20220622082716.478486-1-lee.jones@linaro.org
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Signed-off-by: Sungwoo Kim <iam@sung-woo.kim>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-10-23 09:05:20 +02:00
Bastien Nocera 072f7720bc Bluetooth: L2CAP: initialize delayed works at l2cap_chan_create()
JIRA: https://issues.redhat.com/browse/RHEL-2530

commit 2d2cb3066f2c90cd8ca540b36ba7a55e7f2406e0
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Date:   Sun Sep 4 00:32:56 2022 +0900

    Bluetooth: L2CAP: initialize delayed works at l2cap_chan_create()

    syzbot is reporting cancel_delayed_work() without INIT_DELAYED_WORK() at
    l2cap_chan_del() [1], for CONF_NOT_COMPLETE flag (which meant to prevent
    l2cap_chan_del() from calling cancel_delayed_work()) is cleared by timer
    which fires before l2cap_chan_del() is called by closing file descriptor
    created by socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_L2CAP).

    l2cap_bredr_sig_cmd(L2CAP_CONF_REQ) and l2cap_bredr_sig_cmd(L2CAP_CONF_RSP)
    are calling l2cap_ertm_init(chan), and they call l2cap_chan_ready() (which
    clears CONF_NOT_COMPLETE flag) only when l2cap_ertm_init(chan) succeeded.

    l2cap_sock_init() does not call l2cap_ertm_init(chan), and it instead sets
    CONF_NOT_COMPLETE flag by calling l2cap_chan_set_defaults(). However, when
    connect() is requested, "command 0x0409 tx timeout" happens after 2 seconds
     from connect() request, and CONF_NOT_COMPLETE flag is cleared after 4
    seconds from connect() request, for l2cap_conn_start() from
    l2cap_info_timeout() callback scheduled by

      schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);

    in l2cap_connect() is calling l2cap_chan_ready().

    Fix this problem by initializing delayed works used by L2CAP_MODE_ERTM
    mode as soon as l2cap_chan_create() allocates a channel, like I did in
    commit be85972393 ("Bluetooth: initialize skb_queue_head at
    l2cap_chan_create()").

    Link: https://syzkaller.appspot.com/bug?extid=83672956c7aa6af698b3 [1]
    Reported-by: syzbot <syzbot+83672956c7aa6af698b3@syzkaller.appspotmail.com>
    Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Bastien Nocera <bnocera@redhat.com>
2023-10-23 09:05:19 +02:00
Wander Lairson Costa ceae2a8af1
Bluetooth: L2CAP: Fix u8 overflow
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2152860
CVE: CVE-2022-45934

commit bcd70260ef56e0aee8a4fc6cd214a419900b0765
Author: Sungwoo Kim <iam@sung-woo.kim>
Date:   Fri Nov 18 15:01:47 2022 -0500

    Bluetooth: L2CAP: Fix u8 overflow

    By keep sending L2CAP_CONF_REQ packets, chan->num_conf_rsp increases
    multiple times and eventually it will wrap around the maximum number
    (i.e., 255).
    This patch prevents this by adding a boundary check with
    L2CAP_MAX_CONF_RSP

    Btmon log:
    Bluetooth monitor ver 5.64
    = Note: Linux version 6.1.0-rc2 (x86_64)                               0.264594
    = Note: Bluetooth subsystem version 2.22                               0.264636
    @ MGMT Open: btmon (privileged) version 1.22                  {0x0001} 0.272191
    = New Index: 00:00:00:00:00:00 (Primary,Virtual,hci0)          [hci0] 13.877604
    @ RAW Open: 9496 (privileged) version 2.22                   {0x0002} 13.890741
    = Open Index: 00:00:00:00:00:00                                [hci0] 13.900426
    (...)
    > ACL Data RX: Handle 200 flags 0x00 dlen 1033             #32 [hci0] 14.273106
            invalid packet size (12 != 1033)
            08 00 01 00 02 01 04 00 01 10 ff ff              ............
    > ACL Data RX: Handle 200 flags 0x00 dlen 1547             #33 [hci0] 14.273561
            invalid packet size (14 != 1547)
            0a 00 01 00 04 01 06 00 40 00 00 00 00 00        ........@.....
    > ACL Data RX: Handle 200 flags 0x00 dlen 2061             #34 [hci0] 14.274390
            invalid packet size (16 != 2061)
            0c 00 01 00 04 01 08 00 40 00 00 00 00 00 00 04  ........@.......
    > ACL Data RX: Handle 200 flags 0x00 dlen 2061             #35 [hci0] 14.274932
            invalid packet size (16 != 2061)
            0c 00 01 00 04 01 08 00 40 00 00 00 07 00 03 00  ........@.......
    = bluetoothd: Bluetooth daemon 5.43                                   14.401828
    > ACL Data RX: Handle 200 flags 0x00 dlen 1033             #36 [hci0] 14.275753
            invalid packet size (12 != 1033)
            08 00 01 00 04 01 04 00 40 00 00 00              ........@...

    Signed-off-by: Sungwoo Kim <iam@sung-woo.kim>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Wander Lairson Costa <wander@redhat.com>
2023-05-17 18:50:19 -03:00
David Marlin 0fb996539f Bluetooth: L2CAP: Fix attempting to access uninitialized memory
Bugzilla: https://bugzilla.redhat.com/2148406
CVE: CVE-2022-42895

commit b1a2cd50c0357f243b7435a732b4e62ba3157a2e
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon Oct 31 16:10:52 2022 -0700

    Bluetooth: L2CAP: Fix attempting to access uninitialized memory

    On l2cap_parse_conf_req the variable efs is only initialized if
    remote_efs has been set.

    CVE: CVE-2022-42895
    CC: stable@vger.kernel.org
    Reported-by: Tamás Koczka <poprdi@google.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Reviewed-by: Tedd Ho-Jeong An <tedd.an@intel.com>

Signed-off-by: David Marlin <dmarlin@redhat.com>
2023-02-23 14:43:19 -06:00
Herton R. Krzesinski 21df431653 Merge: Bluetooth: L2CAP: Fix use-after-free caused by l2cap_reassemble_sdu
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/1870

Bugzilla: https://bugzilla.redhat.com/2152931

CVE: CVE-2022-3564

commit 3aff8aaca4e36dc8b17eaa011684881a80238966
Author: Maxim Mikityanskiy <maxtram95@gmail.com>
Date:   Wed Oct 5 00:27:18 2022 +0300

    Bluetooth: L2CAP: Fix use-after-free caused by l2cap_reassemble_sdu

    Fix the race condition between the following two flows that run in
    parallel:

    1. l2cap_reassemble_sdu -> chan->ops->recv (l2cap_sock_recv_cb) ->
       __sock_queue_rcv_skb.

    2. bt_sock_recvmsg -> skb_recv_datagram, skb_free_datagram.

    An SKB can be queued by the first flow and immediately dequeued and
    freed by the second flow, therefore the callers of l2cap_reassemble_sdu
    can't use the SKB after that function returns. However, some places
    continue accessing struct l2cap_ctrl that resides in the SKB's CB for a
    short time after l2cap_reassemble_sdu returns, leading to a
    use-after-free condition (the stack trace is below, line numbers for
    kernel 5.19.8).

    Fix it by keeping a local copy of struct l2cap_ctrl.

    BUG: KASAN: use-after-free in l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
    Read of size 1 at addr ffff88812025f2f0 by task kworker/u17:3/43169

    Workqueue: hci0 hci_rx_work [bluetooth]
    Call Trace:
     <TASK>
     dump_stack_lvl (lib/dump_stack.c:107 (discriminator 4))
     print_report.cold (mm/kasan/report.c:314 mm/kasan/report.c:429)
     ? l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
     kasan_report (mm/kasan/report.c:162 mm/kasan/report.c:493)
     ? l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
     l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
     l2cap_rx (net/bluetooth/l2cap_core.c:7236 net/bluetooth/l2cap_core.c:7271) bluetooth
     ret_from_fork (arch/x86/entry/entry_64.S:306)
     </TASK>

    Allocated by task 43169:
     kasan_save_stack (mm/kasan/common.c:39)
     __kasan_slab_alloc (mm/kasan/common.c:45 mm/kasan/common.c:436 mm/kasan/common.c:469)
     kmem_cache_alloc_node (mm/slab.h:750 mm/slub.c:3243 mm/slub.c:3293)
     __alloc_skb (net/core/skbuff.c:414)
     l2cap_recv_frag (./include/net/bluetooth/bluetooth.h:425 net/bluetooth/l2cap_core.c:8329) bluetooth
     l2cap_recv_acldata (net/bluetooth/l2cap_core.c:8442) bluetooth
     hci_rx_work (net/bluetooth/hci_core.c:3642 net/bluetooth/hci_core.c:3832) bluetooth
     process_one_work (kernel/workqueue.c:2289)
     worker_thread (./include/linux/list.h:292 kernel/workqueue.c:2437)
     kthread (kernel/kthread.c:376)
     ret_from_fork (arch/x86/entry/entry_64.S:306)

    Freed by task 27920:
     kasan_save_stack (mm/kasan/common.c:39)
     kasan_set_track (mm/kasan/common.c:45)
     kasan_set_free_info (mm/kasan/generic.c:372)
     ____kasan_slab_free (mm/kasan/common.c:368 mm/kasan/common.c:328)
     slab_free_freelist_hook (mm/slub.c:1780)
     kmem_cache_free (mm/slub.c:3536 mm/slub.c:3553)
     skb_free_datagram (./include/net/sock.h:1578 ./include/net/sock.h:1639 net/core/datagram.c:323)
     bt_sock_recvmsg (net/bluetooth/af_bluetooth.c:295) bluetooth
     l2cap_sock_recvmsg (net/bluetooth/l2cap_sock.c:1212) bluetooth
     sock_read_iter (net/socket.c:1087)
     new_sync_read (./include/linux/fs.h:2052 fs/read_write.c:401)
     vfs_read (fs/read_write.c:482)
     ksys_read (fs/read_write.c:620)
     do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
     entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120)

    Link: https://lore.kernel.org/linux-bluetooth/CAKErNvoqga1WcmoR3-0875esY6TVWFQDandbVZncSiuGPBQXLA@mail.gmail.com/T/#u
    Fixes: d2a7ac5d5d ("Bluetooth: Add the ERTM receive state machine")
    Fixes: 4b51dae967 ("Bluetooth: Add streaming mode receive and incoming packet classifier")
    Signed-off-by: Maxim Mikityanskiy <maxtram95@gmail.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Ricardo Robaina <rrobaina@redhat.com>

Approved-by: Rafael Aquini <aquini@redhat.com>
Approved-by: Joe Lawrence <joe.lawrence@redhat.com>
Approved-by: Jarod Wilson <jarod@redhat.com>
Approved-by: Prarit Bhargava <prarit@redhat.com>
Approved-by: John W. Linville <linville@redhat.com>
Approved-by: Phil Auld <pauld@redhat.com>
Approved-by: David Arcari <darcari@redhat.com>

Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
2023-02-22 14:53:00 +00:00
Wander Lairson Costa 8e4a6ebca7
Bluetooth: L2CAP: Fix memory leak in vhci_write
Bugzilla: https://bugzilla.redhat.com/2155874
CVE: CVE-2022-3619

commit 7c9524d929648935bac2bbb4c20437df8f9c3f42
Author: Hawkins Jiawei <yin31149@gmail.com>
Date:   Tue Oct 18 10:18:51 2022 +0800

    Bluetooth: L2CAP: Fix memory leak in vhci_write

    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 may 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 copies skb into conn->rx_skb 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: 4d7ea8ee90 ("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>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Wander Lairson Costa <wander@redhat.com>
2023-01-26 10:18:25 -03:00
Ricardo Robaina 7de3277fe8 Bluetooth: L2CAP: Fix use-after-free caused by l2cap_reassemble_sdu
Bugzilla: https://bugzilla.redhat.com/2152931
CVE: CVE-2022-3564

commit 3aff8aaca4e36dc8b17eaa011684881a80238966
Author: Maxim Mikityanskiy <maxtram95@gmail.com>
Date:   Wed Oct 5 00:27:18 2022 +0300

    Bluetooth: L2CAP: Fix use-after-free caused by l2cap_reassemble_sdu

    Fix the race condition between the following two flows that run in
    parallel:

    1. l2cap_reassemble_sdu -> chan->ops->recv (l2cap_sock_recv_cb) ->
       __sock_queue_rcv_skb.

    2. bt_sock_recvmsg -> skb_recv_datagram, skb_free_datagram.

    An SKB can be queued by the first flow and immediately dequeued and
    freed by the second flow, therefore the callers of l2cap_reassemble_sdu
    can't use the SKB after that function returns. However, some places
    continue accessing struct l2cap_ctrl that resides in the SKB's CB for a
    short time after l2cap_reassemble_sdu returns, leading to a
    use-after-free condition (the stack trace is below, line numbers for
    kernel 5.19.8).

    Fix it by keeping a local copy of struct l2cap_ctrl.

    BUG: KASAN: use-after-free in l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
    Read of size 1 at addr ffff88812025f2f0 by task kworker/u17:3/43169

    Workqueue: hci0 hci_rx_work [bluetooth]
    Call Trace:
     <TASK>
     dump_stack_lvl (lib/dump_stack.c:107 (discriminator 4))
     print_report.cold (mm/kasan/report.c:314 mm/kasan/report.c:429)
     ? l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
     kasan_report (mm/kasan/report.c:162 mm/kasan/report.c:493)
     ? l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
     l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
     l2cap_rx (net/bluetooth/l2cap_core.c:7236 net/bluetooth/l2cap_core.c:7271) bluetooth
     ret_from_fork (arch/x86/entry/entry_64.S:306)
     </TASK>

    Allocated by task 43169:
     kasan_save_stack (mm/kasan/common.c:39)
     __kasan_slab_alloc (mm/kasan/common.c:45 mm/kasan/common.c:436 mm/kasan/common.c:469)
     kmem_cache_alloc_node (mm/slab.h:750 mm/slub.c:3243 mm/slub.c:3293)
     __alloc_skb (net/core/skbuff.c:414)
     l2cap_recv_frag (./include/net/bluetooth/bluetooth.h:425 net/bluetooth/l2cap_core.c:8329) bluetooth
     l2cap_recv_acldata (net/bluetooth/l2cap_core.c:8442) bluetooth
     hci_rx_work (net/bluetooth/hci_core.c:3642 net/bluetooth/hci_core.c:3832) bluetooth
     process_one_work (kernel/workqueue.c:2289)
     worker_thread (./include/linux/list.h:292 kernel/workqueue.c:2437)
     kthread (kernel/kthread.c:376)
     ret_from_fork (arch/x86/entry/entry_64.S:306)

    Freed by task 27920:
     kasan_save_stack (mm/kasan/common.c:39)
     kasan_set_track (mm/kasan/common.c:45)
     kasan_set_free_info (mm/kasan/generic.c:372)
     ____kasan_slab_free (mm/kasan/common.c:368 mm/kasan/common.c:328)
     slab_free_freelist_hook (mm/slub.c:1780)
     kmem_cache_free (mm/slub.c:3536 mm/slub.c:3553)
     skb_free_datagram (./include/net/sock.h:1578 ./include/net/sock.h:1639 net/core/datagram.c:323)
     bt_sock_recvmsg (net/bluetooth/af_bluetooth.c:295) bluetooth
     l2cap_sock_recvmsg (net/bluetooth/l2cap_sock.c:1212) bluetooth
     sock_read_iter (net/socket.c:1087)
     new_sync_read (./include/linux/fs.h:2052 fs/read_write.c:401)
     vfs_read (fs/read_write.c:482)
     ksys_read (fs/read_write.c:620)
     do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
     entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120)

    Link: https://lore.kernel.org/linux-bluetooth/CAKErNvoqga1WcmoR3-0875esY6TVWFQDandbVZncSiuGPBQXLA@mail.gmail.com/T/#u
    Fixes: d2a7ac5d5d ("Bluetooth: Add the ERTM receive state machine")
    Fixes: 4b51dae967 ("Bluetooth: Add streaming mode receive and incoming packet classifier")
    Signed-off-by: Maxim Mikityanskiy <maxtram95@gmail.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Ricardo Robaina <rrobaina@redhat.com>
2023-01-11 14:57:13 -03:00
Herton R. Krzesinski e90d138749 Merge: Bluetooth misc fixes
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/1595

Description: Bluetooth misc fixes

Depends: http://bugzilla.redhat.com/2124521

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2140026

Testing: Trivial fixes, sanity only.

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>

Approved-by: David Arcari <darcari@redhat.com>
Approved-by: Tony Camuso <tcamuso@redhat.com>
Approved-by: John W. Linville <linville@redhat.com>
Approved-by: Lenny Szubowicz <lszubowi@redhat.com>
Approved-by: Prarit Bhargava <prarit@redhat.com>

Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
2023-01-02 15:29:19 +00:00
Herton R. Krzesinski fc585dc8eb Merge: Bluetooth: L2CAP: Fix accepting connection request for invalid SPSM
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/1726

Description: Fix accepting connection request for invalid SPSM
    
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2148402

CVE: CVE-2022-42896

Upstream status: Merged.

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>

Approved-by: Tony Camuso <tcamuso@redhat.com>
Approved-by: David Arcari <darcari@redhat.com>

Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
2022-12-23 00:37:13 +00:00
Gopal Tiwari af424ab54e Bluetooth: L2CAP: Fix accepting connection request for invalid SPSM
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2148402

CVE: CVE-2022-42896

commit 711f8c3fb3db61897080468586b970c87c61d9e4
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon Oct 31 16:10:32 2022 -0700

    Bluetooth: L2CAP: Fix accepting connection request for invalid SPSM

    The Bluetooth spec states that the valid range for SPSM is from
    0x0001-0x00ff so it is invalid to accept values outside of this range:

      BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 3, Part A
      page 1059:
      Table 4.15: L2CAP_LE_CREDIT_BASED_CONNECTION_REQ SPSM ranges

    CVE: CVE-2022-42896
    CC: stable@vger.kernel.org
    Reported-by: Tamás Koczka <poprdi@google.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Reviewed-by: Tedd Ho-Jeong An <tedd.an@intel.com>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-12-13 14:14:59 +05:30
Gopal Tiwari f0e8d1e208 Bluetooth: L2CAP: fix use-after-free in l2cap_conn_del()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2140026

commit 0d0e2d032811280b927650ff3c15fe5020e82533
Author: Zhengchao Shao <shaozhengchao@huawei.com>
Date:   Mon Oct 17 15:58:13 2022 +0800

    Bluetooth: L2CAP: fix use-after-free in l2cap_conn_del()

    When l2cap_recv_frame() is invoked to receive data, and the cid is
    L2CAP_CID_A2MP, if the channel does not exist, it will create a channel.
    However, after a channel is created, the hold operation of the channel
    is not performed. In this case, the value of channel reference counting
    is 1. As a result, after hci_error_reset() is triggered, l2cap_conn_del()
    invokes the close hook function of A2MP to release the channel. Then
     l2cap_chan_unlock(chan) will trigger UAF issue.

    The process is as follows:
    Receive data:
    l2cap_data_channel()
        a2mp_channel_create()  --->channel ref is 2
        l2cap_chan_put()       --->channel ref is 1

    Triger event:
        hci_error_reset()
            hci_dev_do_close()
            ...
            l2cap_disconn_cfm()
                l2cap_conn_del()
                    l2cap_chan_hold()    --->channel ref is 2
                    l2cap_chan_del()     --->channel ref is 1
                    a2mp_chan_close_cb() --->channel ref is 0, release channel
                    l2cap_chan_unlock()  --->UAF of channel

    The detailed Call Trace is as follows:
    BUG: KASAN: use-after-free in __mutex_unlock_slowpath+0xa6/0x5e0
    Read of size 8 at addr ffff8880160664b8 by task kworker/u11:1/7593
    Workqueue: hci0 hci_error_reset
    Call Trace:
     <TASK>
     dump_stack_lvl+0xcd/0x134
     print_report.cold+0x2ba/0x719
     kasan_report+0xb1/0x1e0
     kasan_check_range+0x140/0x190
     __mutex_unlock_slowpath+0xa6/0x5e0
     l2cap_conn_del+0x404/0x7b0
     l2cap_disconn_cfm+0x8c/0xc0
     hci_conn_hash_flush+0x11f/0x260
     hci_dev_close_sync+0x5f5/0x11f0
     hci_dev_do_close+0x2d/0x70
     hci_error_reset+0x9e/0x140
     process_one_work+0x98a/0x1620
worker_thread+0x665/0x1080
     kthread+0x2e4/0x3a0
     ret_from_fork+0x1f/0x30
     </TASK>

    Allocated by task 7593:
     kasan_save_stack+0x1e/0x40
     __kasan_kmalloc+0xa9/0xd0
     l2cap_chan_create+0x40/0x930
     amp_mgr_create+0x96/0x990
     a2mp_channel_create+0x7d/0x150
     l2cap_recv_frame+0x51b8/0x9a70
     l2cap_recv_acldata+0xaa3/0xc00
     hci_rx_work+0x702/0x1220
     process_one_work+0x98a/0x1620
     worker_thread+0x665/0x1080
     kthread+0x2e4/0x3a0
     ret_from_fork+0x1f/0x30

    Freed by task 7593:
     kasan_save_stack+0x1e/0x40
     kasan_set_track+0x21/0x30
     kasan_set_free_info+0x20/0x30
     ____kasan_slab_free+0x167/0x1c0
     slab_free_freelist_hook+0x89/0x1c0
     kfree+0xe2/0x580
     l2cap_chan_put+0x22a/0x2d0
     l2cap_conn_del+0x3fc/0x7b0
     l2cap_disconn_cfm+0x8c/0xc0
     hci_conn_hash_flush+0x11f/0x260
     hci_dev_close_sync+0x5f5/0x11f0
     hci_dev_do_close+0x2d/0x70
     hci_error_reset+0x9e/0x140
     process_one_work+0x98a/0x1620
     worker_thread+0x665/0x1080
     kthread+0x2e4/0x3a0
     ret_from_fork+0x1f/0x30

    Last potentially related work creation:
     kasan_save_stack+0x1e/0x40
     __kasan_record_aux_stack+0xbe/0xd0
     call_rcu+0x99/0x740
     netlink_release+0xe6a/0x1cf0
     __sock_release+0xcd/0x280
     sock_close+0x18/0x20
     __fput+0x27c/0xa90
     task_work_run+0xdd/0x1a0
     exit_to_user_mode_prepare+0x23c/0x250
     syscall_exit_to_user_mode+0x19/0x50
     do_syscall_64+0x42/0x80
     entry_SYSCALL_64_after_hwframe+0x63/0xcd

    Second to last potentially related work creation:
     kasan_save_stack+0x1e/0x40
     __kasan_record_aux_stack+0xbe/0xd0
     call_rcu+0x99/0x740
     netlink_release+0xe6a/0x1cf0
     __sock_release+0xcd/0x280
     sock_close+0x18/0x20
     __fput+0x27c/0xa90
     task_work_run+0xdd/0x1a0
     exit_to_user_mode_prepare+0x23c/0x250
     syscall_exit_to_user_mode+0x19/0x50
     do_syscall_64+0x42/0x80
     entry_SYSCALL_64_after_hwframe+0x63/0xcd

    Fixes: d0be8347c623 ("Bluetooth: L2CAP: Fix use-after-free caused by l2cap_chan_put")
    Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-11-09 11:40:56 +05:30
Gopal Tiwari 0296500751 Bluetooth: L2CAP: Fix build errors in some archs
Bugzilla: http://bugzilla.redhat.com/2124521

commit b840304fb46cdf7012722f456bce06f151b3e81b
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Fri Aug 12 15:33:57 2022 -0700

    Bluetooth: L2CAP: Fix build errors in some archs

    This attempts to fix the follow errors:

    In function 'memcmp',
        inlined from 'bacmp' at ./include/net/bluetooth/bluetooth.h:347:9,
        inlined from 'l2cap_global_chan_by_psm' at
        net/bluetooth/l2cap_core.c:2003:15:
    ./include/linux/fortify-string.h:44:33: error: '__builtin_memcmp'
    specified bound 6 exceeds source size 0 [-Werror=stringop-overread]
       44 | #define __underlying_memcmp     __builtin_memcmp
          |                                 ^
    ./include/linux/fortify-string.h:420:16: note: in expansion of macro
    '__underlying_memcmp'
      420 |         return __underlying_memcmp(p, q, size);
          |                ^~~~~~~~~~~~~~~~~~~
    In function 'memcmp',
        inlined from 'bacmp' at ./include/net/bluetooth/bluetooth.h:347:9,
        inlined from 'l2cap_global_chan_by_psm' at
        net/bluetooth/l2cap_core.c:2004:15:
    ./include/linux/fortify-string.h:44:33: error: '__builtin_memcmp'
    specified bound 6 exceeds source size 0 [-Werror=stringop-overread]
       44 | #define __underlying_memcmp     __builtin_memcmp
          |                                 ^
    ./include/linux/fortify-string.h:420:16: note: in expansion of macro
    '__underlying_memcmp'
      420 |         return __underlying_memcmp(p, q, size);
          |                ^~~~~~~~~~~~~~~~~~~

    Fixes: 332f1795ca20 ("Bluetooth: L2CAP: Fix l2cap_global_chan_by_psm regression")
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-10-18 10:32:41 +05:30
Gopal Tiwari b9aa9b1ad1 Bluetooth: L2CAP: Fix l2cap_global_chan_by_psm regression
Bugzilla: http://bugzilla.redhat.com/2124521

commit 332f1795ca202489c665a75e62e18ff6284de077
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Mon Aug 1 13:52:07 2022 -0700

    Bluetooth: L2CAP: Fix l2cap_global_chan_by_psm regression

    The patch d0be8347c623: "Bluetooth: L2CAP: Fix use-after-free caused
    by l2cap_chan_put" from Jul 21, 2022, leads to the following Smatch
    static checker warning:

            net/bluetooth/l2cap_core.c:1977 l2cap_global_chan_by_psm()
            error: we previously assumed 'c' could be null (see line 1996)

    Fixes: d0be8347c623 ("Bluetooth: L2CAP: Fix use-after-free caused by l2cap_chan_put")
    Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-10-18 10:32:39 +05:30
Gopal Tiwari 1c12ec3480 Bluetooth: L2CAP: Fix use-after-free caused by l2cap_chan_put
Bugzilla: http://bugzilla.redhat.com/2124521

commit d0be8347c623e0ac4202a1d4e0373882821f56b0
Author: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Date:   Thu Jul 21 09:10:50 2022 -0700

    Bluetooth: L2CAP: Fix use-after-free caused by l2cap_chan_put

    This fixes the following trace which is caused by hci_rx_work starting up
    *after* the final channel reference has been put() during sock_close() but
    *before* the references to the channel have been destroyed, so instead
    the code now rely on kref_get_unless_zero/l2cap_chan_hold_unless_zero to
    prevent referencing a channel that is about to be destroyed.

      refcount_t: increment on 0; use-after-free.
      BUG: KASAN: use-after-free in refcount_dec_and_test+0x20/0xd0
      Read of size 4 at addr ffffffc114f5bf18 by task kworker/u17:14/705

      CPU: 4 PID: 705 Comm: kworker/u17:14 Tainted: G S      W
      4.14.234-00003-g1fb6d0bd49a4-dirty #28
      Hardware name: Qualcomm Technologies, Inc. SM8150 V2 PM8150
      Google Inc. MSM sm8150 Flame DVT (DT)
      Workqueue: hci0 hci_rx_work
      Call trace:
       dump_backtrace+0x0/0x378
       show_stack+0x20/0x2c
       dump_stack+0x124/0x148
       print_address_description+0x80/0x2e8
       __kasan_report+0x168/0x188
       kasan_report+0x10/0x18
       __asan_load4+0x84/0x8c
       refcount_dec_and_test+0x20/0xd0
       l2cap_chan_put+0x48/0x12c
       l2cap_recv_frame+0x4770/0x6550
       l2cap_recv_acldata+0x44c/0x7a4
       hci_acldata_packet+0x100/0x188
       hci_rx_work+0x178/0x23c
       process_one_work+0x35c/0x95c
       worker_thread+0x4cc/0x960
       kthread+0x1a8/0x1c4
       ret_from_fork+0x10/0x18

    Cc: stable@kernel.org
    Reported-by: Lee Jones <lee.jones@linaro.org>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Tested-by: Lee Jones <lee.jones@linaro.org>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-10-18 10:32:39 +05:30
Gopal Tiwari 7436c909bb Bluetooth: use memset avoid memory leaks
Bugzilla: http://bugzilla.redhat.com/2124521

commit a5133fe87ed827ce94084eecb7830a6d451ef55c
Author: Xiaohui Zhang <xiaohuizhang@ruc.edu.cn>
Date:   Tue Jun 7 23:30:20 2022 +0800

    Bluetooth: use memset avoid memory leaks

    Similar to the handling of l2cap_ecred_connect in commit d3715b2333e9
    ("Bluetooth: use memset avoid memory leaks"), we thought a patch
    might be needed here as well.

    Use memset to initialize structs to prevent memory leaks
    in l2cap_le_connect

    Signed-off-by: Xiaohui Zhang <xiaohuizhang@ruc.edu.cn>
    Signed-off-by: Marcel Holtmann <marcel@holtmann.org>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-10-18 10:32:34 +05:30
Gopal Tiwari 7782a58192 Bluetooth: Don't assign twice the same value
Bugzilla: http://bugzilla.redhat.com/2124521

commit 1f667e157605a217f10fc45f1a7fb4a8354eb5e3
Author: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Date:   Wed Mar 2 21:18:35 2022 +0100

    Bluetooth: Don't assign twice the same value

    data.pid is set twice with the same value. Remove one of these redundant
    calls.

    Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
    Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
    Signed-off-by: Marcel Holtmann <marcel@holtmann.org>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-10-18 10:32:27 +05:30
Gopal Tiwari 64fb41072c Bluetooth: use memset avoid memory leaks
Bugzilla: http://bugzilla.redhat.com/2124521

commit d3715b2333e9a21692ba16ef8645eda584a9515d
Author: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
Date:   Fri Feb 25 07:41:52 2022 +0000

    Bluetooth: use memset avoid memory leaks

    Use memset to initialize structs to prevent memory leaks
    in l2cap_ecred_connect

    Reported-by: Zeal Robot <zealci@zte.com.cn>
    Signed-off-by: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
    Signed-off-by: Marcel Holtmann <marcel@holtmann.org>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-10-18 10:32:26 +05:30