Blame SOURCES/CVE-2022-3564.patch

369dfe
From 63f63d3ce535ca59a9da05ba3004187033495b7d Mon Sep 17 00:00:00 2001
369dfe
From: Ryan Sullivan <rysulliv@redhat.com>
369dfe
Date: Mon, 23 Jan 2023 11:41:37 -0500
369dfe
Subject: [KPATCH CVE-2022-3564] kpatch fixes for CVE-2022-3564
369dfe
369dfe
Kernels:
369dfe
3.10.0-1160.71.1.el7
369dfe
3.10.0-1160.76.1.el7
369dfe
3.10.0-1160.80.1.el7
369dfe
3.10.0-1160.81.1.el7
369dfe
369dfe
369dfe
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/49
369dfe
Approved-by: Yannick Cote (@ycote1)
369dfe
Approved-by: Joe Lawrence (@joe.lawrence)
369dfe
Changes since last build:
369dfe
[x86_64]:
369dfe
l2cap_core.o: changed function: l2cap_data_channel
369dfe
l2cap_core.o: changed function: l2cap_rx_state_recv
369dfe
nf_tables_api.o: changed function: __nf_tables_abort
369dfe
nf_tables_api.o: changed function: nf_tables_delsetelem
369dfe
nf_tables_api.o: changed function: nf_tables_newset
369dfe
nf_tables_api.o: changed function: nft_delrule
369dfe
nf_tables_api.o: changed function: nft_set_destroy
369dfe
nf_tables_api.o: changed function: nft_validate_register_store
369dfe
nf_tables_api.o: new function: nf_tables_activate_set
369dfe
nf_tables_api.o: new function: nf_tables_deactivate_set
369dfe
nf_tables_api.o: new function: nft_rule_expr_deactivate
369dfe
nft_dynset.o: new function: klp_is_nft_dynset
369dfe
nft_dynset.o: new function: nft_dynset_activate
369dfe
nft_dynset.o: new function: nft_dynset_deactivate
369dfe
nft_lookup.o: new function: klp_is_nft_lookup
369dfe
nft_lookup.o: new function: nft_lookup_activate
369dfe
nft_lookup.o: new function: nft_lookup_deactivate
369dfe
369dfe
[ppc64le]:
369dfe
l2cap_core.o: changed function: l2cap_data_channel
369dfe
l2cap_core.o: changed function: l2cap_rx_state_recv
369dfe
nf_tables_api.o: changed function: __nf_tables_abort
369dfe
nf_tables_api.o: changed function: nf_tables_bind_check_setelem
369dfe
nf_tables_api.o: changed function: nf_tables_bind_set
369dfe
nf_tables_api.o: changed function: nf_tables_commit
369dfe
nf_tables_api.o: changed function: nf_tables_delrule
369dfe
nf_tables_api.o: changed function: nf_tables_delsetelem
369dfe
nf_tables_api.o: changed function: nf_tables_newset
369dfe
nf_tables_api.o: changed function: nf_tables_unbind_set
369dfe
nf_tables_api.o: changed function: nft_add_set_elem
369dfe
nf_tables_api.o: changed function: nft_delrule_by_chain
369dfe
nf_tables_api.o: changed function: nft_unregister_afinfo
369dfe
nf_tables_api.o: changed function: nft_validate_register_store
369dfe
nf_tables_api.o: new function: nf_tables_activate_set
369dfe
nf_tables_api.o: new function: nf_tables_deactivate_set
369dfe
nft_dynset.o: new function: klp_is_nft_dynset
369dfe
nft_dynset.o: new function: nft_dynset_activate
369dfe
nft_dynset.o: new function: nft_dynset_deactivate
369dfe
nft_lookup.o: new function: klp_is_nft_lookup
369dfe
nft_lookup.o: new function: nft_lookup_activate
369dfe
nft_lookup.o: new function: nft_lookup_deactivate
369dfe
369dfe
---------------------------
369dfe
369dfe
Modifications: none
369dfe
369dfe
commit ce15ab7416bca0378f144b0aa9183abb532bfb7d
369dfe
Author: Wander Lairson Costa <wander@redhat.com>
369dfe
Date:   Mon Jan 9 14:37:35 2023 -0300
369dfe
369dfe
    Bluetooth: L2CAP: Fix use-after-free caused by l2cap_reassemble_sdu
369dfe
369dfe
    Bugzilla: https://bugzilla.redhat.com/2152941
369dfe
    CVE: CVE-2022-3564
369dfe
369dfe
    commit 3aff8aaca4e36dc8b17eaa011684881a80238966
369dfe
    Author: Maxim Mikityanskiy <maxtram95@gmail.com>
369dfe
    Date:   Wed Oct 5 00:27:18 2022 +0300
369dfe
369dfe
        Bluetooth: L2CAP: Fix use-after-free caused by l2cap_reassemble_sdu
369dfe
369dfe
        Fix the race condition between the following two flows that run in
369dfe
        parallel:
369dfe
369dfe
        1. l2cap_reassemble_sdu -> chan->ops->recv (l2cap_sock_recv_cb) ->
369dfe
           __sock_queue_rcv_skb.
369dfe
369dfe
        2. bt_sock_recvmsg -> skb_recv_datagram, skb_free_datagram.
369dfe
369dfe
        An SKB can be queued by the first flow and immediately dequeued and
369dfe
        freed by the second flow, therefore the callers of l2cap_reassemble_sdu
369dfe
        can't use the SKB after that function returns. However, some places
369dfe
        continue accessing struct l2cap_ctrl that resides in the SKB's CB for a
369dfe
        short time after l2cap_reassemble_sdu returns, leading to a
369dfe
        use-after-free condition (the stack trace is below, line numbers for
369dfe
        kernel 5.19.8).
369dfe
369dfe
        Fix it by keeping a local copy of struct l2cap_ctrl.
369dfe
369dfe
        BUG: KASAN: use-after-free in l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
369dfe
        Read of size 1 at addr ffff88812025f2f0 by task kworker/u17:3/43169
369dfe
369dfe
        Workqueue: hci0 hci_rx_work [bluetooth]
369dfe
        Call Trace:
369dfe
         <TASK>
369dfe
         dump_stack_lvl (lib/dump_stack.c:107 (discriminator 4))
369dfe
         print_report.cold (mm/kasan/report.c:314 mm/kasan/report.c:429)
369dfe
         ? l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
369dfe
         kasan_report (mm/kasan/report.c:162 mm/kasan/report.c:493)
369dfe
         ? l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
369dfe
         l2cap_rx_state_recv (net/bluetooth/l2cap_core.c:6906) bluetooth
369dfe
         l2cap_rx (net/bluetooth/l2cap_core.c:7236 net/bluetooth/l2cap_core.c:7271) bluetooth
369dfe
         ret_from_fork (arch/x86/entry/entry_64.S:306)
369dfe
         </TASK>
369dfe
369dfe
        Allocated by task 43169:
369dfe
         kasan_save_stack (mm/kasan/common.c:39)
369dfe
         __kasan_slab_alloc (mm/kasan/common.c:45 mm/kasan/common.c:436 mm/kasan/common.c:469)
369dfe
         kmem_cache_alloc_node (mm/slab.h:750 mm/slub.c:3243 mm/slub.c:3293)
369dfe
         __alloc_skb (net/core/skbuff.c:414)
369dfe
         l2cap_recv_frag (./include/net/bluetooth/bluetooth.h:425 net/bluetooth/l2cap_core.c:8329) bluetooth
369dfe
         l2cap_recv_acldata (net/bluetooth/l2cap_core.c:8442) bluetooth
369dfe
         hci_rx_work (net/bluetooth/hci_core.c:3642 net/bluetooth/hci_core.c:3832) bluetooth
369dfe
         process_one_work (kernel/workqueue.c:2289)
369dfe
         worker_thread (./include/linux/list.h:292 kernel/workqueue.c:2437)
369dfe
         kthread (kernel/kthread.c:376)
369dfe
         ret_from_fork (arch/x86/entry/entry_64.S:306)
369dfe
369dfe
        Freed by task 27920:
369dfe
         kasan_save_stack (mm/kasan/common.c:39)
369dfe
         kasan_set_track (mm/kasan/common.c:45)
369dfe
         kasan_set_free_info (mm/kasan/generic.c:372)
369dfe
         ____kasan_slab_free (mm/kasan/common.c:368 mm/kasan/common.c:328)
369dfe
         slab_free_freelist_hook (mm/slub.c:1780)
369dfe
         kmem_cache_free (mm/slub.c:3536 mm/slub.c:3553)
369dfe
         skb_free_datagram (./include/net/sock.h:1578 ./include/net/sock.h:1639 net/core/datagram.c:323)
369dfe
         bt_sock_recvmsg (net/bluetooth/af_bluetooth.c:295) bluetooth
369dfe
         l2cap_sock_recvmsg (net/bluetooth/l2cap_sock.c:1212) bluetooth
369dfe
         sock_read_iter (net/socket.c:1087)
369dfe
         new_sync_read (./include/linux/fs.h:2052 fs/read_write.c:401)
369dfe
         vfs_read (fs/read_write.c:482)
369dfe
         ksys_read (fs/read_write.c:620)
369dfe
         do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
369dfe
         entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120)
369dfe
369dfe
        Link: https://lore.kernel.org/linux-bluetooth/CAKErNvoqga1WcmoR3-0875esY6TVWFQDandbVZncSiuGPBQXLA@mail.gmail.com/T/#u
369dfe
        Fixes: d2a7ac5d5d3a ("Bluetooth: Add the ERTM receive state machine")
369dfe
        Fixes: 4b51dae96731 ("Bluetooth: Add streaming mode receive and incoming packet classifier")
369dfe
        Signed-off-by: Maxim Mikityanskiy <maxtram95@gmail.com>
369dfe
        Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
369dfe
369dfe
    Signed-off-by: Wander Lairson Costa <wander@redhat.com>
369dfe
369dfe
Signed-off-by: Ryan Sullivan <rysulliv@redhat.com>
369dfe
---
369dfe
 net/bluetooth/l2cap_core.c | 48 ++++++++++++++++++++++++++++++++------
369dfe
 1 file changed, 41 insertions(+), 7 deletions(-)
369dfe
369dfe
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
369dfe
index 6e94fd907d89..4338810b0d58 100644
369dfe
--- a/net/bluetooth/l2cap_core.c
369dfe
+++ b/net/bluetooth/l2cap_core.c
369dfe
@@ -6197,6 +6197,7 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
369dfe
 			       struct l2cap_ctrl *control,
369dfe
 			       struct sk_buff *skb, u8 event)
369dfe
 {
369dfe
+	struct l2cap_ctrl local_control;
369dfe
 	int err = 0;
369dfe
 	bool skb_in_use = false;
369dfe
 
369dfe
@@ -6221,15 +6222,32 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
369dfe
 			chan->buffer_seq = chan->expected_tx_seq;
369dfe
 			skb_in_use = true;
369dfe
 
369dfe
+			/* l2cap_reassemble_sdu may free skb, hence invalidate
369dfe
+			 * control, so make a copy in advance to use it after
369dfe
+			 * l2cap_reassemble_sdu returns and to avoid the race
369dfe
+			 * condition, for example:
369dfe
+			 *
369dfe
+			 * The current thread calls:
369dfe
+			 *   l2cap_reassemble_sdu
369dfe
+			 *     chan->ops->recv == l2cap_sock_recv_cb
369dfe
+			 *       __sock_queue_rcv_skb
369dfe
+			 * Another thread calls:
369dfe
+			 *   bt_sock_recvmsg
369dfe
+			 *     skb_recv_datagram
369dfe
+			 *     skb_free_datagram
369dfe
+			 * Then the current thread tries to access control, but
369dfe
+			 * it was freed by skb_free_datagram.
369dfe
+			 */
369dfe
+			local_control = *control;
369dfe
 			err = l2cap_reassemble_sdu(chan, skb, control);
369dfe
 			if (err)
369dfe
 				break;
369dfe
 
369dfe
-			if (control->final) {
369dfe
+			if (local_control.final) {
369dfe
 				if (!test_and_clear_bit(CONN_REJ_ACT,
369dfe
 							&chan->conn_state)) {
369dfe
-					control->final = 0;
369dfe
-					l2cap_retransmit_all(chan, control);
369dfe
+					local_control.final = 0;
369dfe
+					l2cap_retransmit_all(chan, &local_control);
369dfe
 					l2cap_ertm_send(chan);
369dfe
 				}
369dfe
 			}
369dfe
@@ -6609,11 +6627,27 @@ static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
369dfe
 static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
369dfe
 			   struct sk_buff *skb)
369dfe
 {
369dfe
+	/* l2cap_reassemble_sdu may free skb, hence invalidate control, so store
369dfe
+	 * the txseq field in advance to use it after l2cap_reassemble_sdu
369dfe
+	 * returns and to avoid the race condition, for example:
369dfe
+	 *
369dfe
+	 * The current thread calls:
369dfe
+	 *   l2cap_reassemble_sdu
369dfe
+	 *     chan->ops->recv == l2cap_sock_recv_cb
369dfe
+	 *       __sock_queue_rcv_skb
369dfe
+	 * Another thread calls:
369dfe
+	 *   bt_sock_recvmsg
369dfe
+	 *     skb_recv_datagram
369dfe
+	 *     skb_free_datagram
369dfe
+	 * Then the current thread tries to access control, but it was freed by
369dfe
+	 * skb_free_datagram.
369dfe
+	 */
369dfe
+	u16 txseq = control->txseq;
369dfe
+
369dfe
 	BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
369dfe
 	       chan->rx_state);
369dfe
 
369dfe
-	if (l2cap_classify_txseq(chan, control->txseq) ==
369dfe
-	    L2CAP_TXSEQ_EXPECTED) {
369dfe
+	if (l2cap_classify_txseq(chan, txseq) == L2CAP_TXSEQ_EXPECTED) {
369dfe
 		l2cap_pass_to_tx(chan, control);
369dfe
 
369dfe
 		BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
369dfe
@@ -6636,8 +6670,8 @@ static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
369dfe
 		}
369dfe
 	}
369dfe
 
369dfe
-	chan->last_acked_seq = control->txseq;
369dfe
-	chan->expected_tx_seq = __next_seq(chan, control->txseq);
369dfe
+	chan->last_acked_seq = txseq;
369dfe
+	chan->expected_tx_seq = __next_seq(chan, txseq);
369dfe
 
369dfe
 	return 0;
369dfe
 }
369dfe
-- 
369dfe
2.40.1
369dfe
369dfe