From: Greg Kroah-Hartman Date: Sat, 30 Jul 2022 14:45:36 +0000 (+0200) Subject: 4.9-stable patches X-Git-Tag: v5.4.209~57 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=39c334cba2d8c5280876bd88c5837538f24fa57a;p=thirdparty%2Fkernel%2Fstable-queue.git 4.9-stable patches added patches: bluetooth-l2cap-fix-use-after-free-caused-by-l2cap_chan_put.patch ntfs-fix-use-after-free-in-ntfs_ucsncmp.patch --- diff --git a/queue-4.9/bluetooth-l2cap-fix-use-after-free-caused-by-l2cap_chan_put.patch b/queue-4.9/bluetooth-l2cap-fix-use-after-free-caused-by-l2cap_chan_put.patch new file mode 100644 index 00000000000..28a3221c4c1 --- /dev/null +++ b/queue-4.9/bluetooth-l2cap-fix-use-after-free-caused-by-l2cap_chan_put.patch @@ -0,0 +1,264 @@ +From d0be8347c623e0ac4202a1d4e0373882821f56b0 Mon Sep 17 00:00:00 2001 +From: Luiz Augusto von Dentz +Date: Thu, 21 Jul 2022 09:10:50 -0700 +Subject: Bluetooth: L2CAP: Fix use-after-free caused by l2cap_chan_put + +From: Luiz Augusto von Dentz + +commit d0be8347c623e0ac4202a1d4e0373882821f56b0 upstream. + +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 +Signed-off-by: Luiz Augusto von Dentz +Tested-by: Lee Jones +Signed-off-by: Luiz Augusto von Dentz +Signed-off-by: Greg Kroah-Hartman +--- + include/net/bluetooth/l2cap.h | 1 + net/bluetooth/l2cap_core.c | 61 +++++++++++++++++++++++++++++++++--------- + 2 files changed, 49 insertions(+), 13 deletions(-) + +--- a/include/net/bluetooth/l2cap.h ++++ b/include/net/bluetooth/l2cap.h +@@ -798,6 +798,7 @@ enum { + }; + + void l2cap_chan_hold(struct l2cap_chan *c); ++struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c); + void l2cap_chan_put(struct l2cap_chan *c); + + static inline void l2cap_chan_lock(struct l2cap_chan *chan) +--- a/net/bluetooth/l2cap_core.c ++++ b/net/bluetooth/l2cap_core.c +@@ -113,7 +113,8 @@ static struct l2cap_chan *__l2cap_get_ch + } + + /* Find channel with given SCID. +- * Returns locked channel. */ ++ * Returns a reference locked channel. ++ */ + static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, + u16 cid) + { +@@ -121,15 +122,19 @@ static struct l2cap_chan *l2cap_get_chan + + mutex_lock(&conn->chan_lock); + c = __l2cap_get_chan_by_scid(conn, cid); +- if (c) +- l2cap_chan_lock(c); ++ if (c) { ++ /* Only lock if chan reference is not 0 */ ++ c = l2cap_chan_hold_unless_zero(c); ++ if (c) ++ l2cap_chan_lock(c); ++ } + mutex_unlock(&conn->chan_lock); + + return c; + } + + /* Find channel with given DCID. +- * Returns locked channel. ++ * Returns a reference locked channel. + */ + static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, + u16 cid) +@@ -138,8 +143,12 @@ static struct l2cap_chan *l2cap_get_chan + + mutex_lock(&conn->chan_lock); + c = __l2cap_get_chan_by_dcid(conn, cid); +- if (c) +- l2cap_chan_lock(c); ++ if (c) { ++ /* Only lock if chan reference is not 0 */ ++ c = l2cap_chan_hold_unless_zero(c); ++ if (c) ++ l2cap_chan_lock(c); ++ } + mutex_unlock(&conn->chan_lock); + + return c; +@@ -164,8 +173,12 @@ static struct l2cap_chan *l2cap_get_chan + + mutex_lock(&conn->chan_lock); + c = __l2cap_get_chan_by_ident(conn, ident); +- if (c) +- l2cap_chan_lock(c); ++ if (c) { ++ /* Only lock if chan reference is not 0 */ ++ c = l2cap_chan_hold_unless_zero(c); ++ if (c) ++ l2cap_chan_lock(c); ++ } + mutex_unlock(&conn->chan_lock); + + return c; +@@ -491,6 +504,16 @@ void l2cap_chan_hold(struct l2cap_chan * + kref_get(&c->kref); + } + ++struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c) ++{ ++ BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref)); ++ ++ if (!kref_get_unless_zero(&c->kref)) ++ return NULL; ++ ++ return c; ++} ++ + void l2cap_chan_put(struct l2cap_chan *c) + { + BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount)); +@@ -1803,7 +1826,10 @@ static struct l2cap_chan *l2cap_global_c + src_match = !bacmp(&c->src, src); + dst_match = !bacmp(&c->dst, dst); + if (src_match && dst_match) { +- l2cap_chan_hold(c); ++ c = l2cap_chan_hold_unless_zero(c); ++ if (!c) ++ continue; ++ + read_unlock(&chan_list_lock); + return c; + } +@@ -1818,7 +1844,7 @@ static struct l2cap_chan *l2cap_global_c + } + + if (c1) +- l2cap_chan_hold(c1); ++ c1 = l2cap_chan_hold_unless_zero(c1); + + read_unlock(&chan_list_lock); + +@@ -4194,6 +4220,7 @@ static inline int l2cap_config_req(struc + + unlock: + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + return err; + } + +@@ -4306,6 +4333,7 @@ static inline int l2cap_config_rsp(struc + + done: + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + return err; + } + +@@ -5034,6 +5062,7 @@ send_move_response: + l2cap_send_move_chan_rsp(chan, result); + + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + + return 0; + } +@@ -5126,6 +5155,7 @@ static void l2cap_move_continue(struct l + } + + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + } + + static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, +@@ -5155,6 +5185,7 @@ static void l2cap_move_fail(struct l2cap + l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); + + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + } + + static int l2cap_move_channel_rsp(struct l2cap_conn *conn, +@@ -5218,6 +5249,7 @@ static int l2cap_move_channel_confirm(st + l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); + + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + + return 0; + } +@@ -5253,6 +5285,7 @@ static inline int l2cap_move_channel_con + } + + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + + return 0; + } +@@ -5625,12 +5658,11 @@ static inline int l2cap_le_credits(struc + if (credits > max_credits) { + BT_ERR("LE credits overflow"); + l2cap_send_disconn_req(chan, ECONNRESET); +- l2cap_chan_unlock(chan); + + /* Return 0 so that we don't trigger an unnecessary + * command reject packet. + */ +- return 0; ++ goto unlock; + } + + chan->tx_credits += credits; +@@ -5643,7 +5675,9 @@ static inline int l2cap_le_credits(struc + if (chan->tx_credits) + chan->ops->resume(chan); + ++unlock: + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + + return 0; + } +@@ -6941,6 +6975,7 @@ drop: + + done: + l2cap_chan_unlock(chan); ++ l2cap_chan_put(chan); + } + + static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, +@@ -7345,7 +7380,7 @@ static struct l2cap_chan *l2cap_global_f + if (src_type != c->src_type) + continue; + +- l2cap_chan_hold(c); ++ c = l2cap_chan_hold_unless_zero(c); + read_unlock(&chan_list_lock); + return c; + } diff --git a/queue-4.9/ntfs-fix-use-after-free-in-ntfs_ucsncmp.patch b/queue-4.9/ntfs-fix-use-after-free-in-ntfs_ucsncmp.patch new file mode 100644 index 00000000000..75937219c57 --- /dev/null +++ b/queue-4.9/ntfs-fix-use-after-free-in-ntfs_ucsncmp.patch @@ -0,0 +1,107 @@ +From 38c9c22a85aeed28d0831f230136e9cf6fa2ed44 Mon Sep 17 00:00:00 2001 +From: ChenXiaoSong +Date: Thu, 7 Jul 2022 18:53:29 +0800 +Subject: ntfs: fix use-after-free in ntfs_ucsncmp() + +From: ChenXiaoSong + +commit 38c9c22a85aeed28d0831f230136e9cf6fa2ed44 upstream. + +Syzkaller reported use-after-free bug as follows: + +================================================================== +BUG: KASAN: use-after-free in ntfs_ucsncmp+0x123/0x130 +Read of size 2 at addr ffff8880751acee8 by task a.out/879 + +CPU: 7 PID: 879 Comm: a.out Not tainted 5.19.0-rc4-next-20220630-00001-gcc5218c8bd2c-dirty #7 +Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 +Call Trace: + + dump_stack_lvl+0x1c0/0x2b0 + print_address_description.constprop.0.cold+0xd4/0x484 + print_report.cold+0x55/0x232 + kasan_report+0xbf/0xf0 + ntfs_ucsncmp+0x123/0x130 + ntfs_are_names_equal.cold+0x2b/0x41 + ntfs_attr_find+0x43b/0xb90 + ntfs_attr_lookup+0x16d/0x1e0 + ntfs_read_locked_attr_inode+0x4aa/0x2360 + ntfs_attr_iget+0x1af/0x220 + ntfs_read_locked_inode+0x246c/0x5120 + ntfs_iget+0x132/0x180 + load_system_files+0x1cc6/0x3480 + ntfs_fill_super+0xa66/0x1cf0 + mount_bdev+0x38d/0x460 + legacy_get_tree+0x10d/0x220 + vfs_get_tree+0x93/0x300 + do_new_mount+0x2da/0x6d0 + path_mount+0x496/0x19d0 + __x64_sys_mount+0x284/0x300 + do_syscall_64+0x3b/0xc0 + entry_SYSCALL_64_after_hwframe+0x46/0xb0 +RIP: 0033:0x7f3f2118d9ea +Code: 48 8b 0d a9 f4 0b 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 76 f4 0b 00 f7 d8 64 89 01 48 +RSP: 002b:00007ffc269deac8 EFLAGS: 00000202 ORIG_RAX: 00000000000000a5 +RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f3f2118d9ea +RDX: 0000000020000000 RSI: 0000000020000100 RDI: 00007ffc269dec00 +RBP: 00007ffc269dec80 R08: 00007ffc269deb00 R09: 00007ffc269dec44 +R10: 0000000000000000 R11: 0000000000000202 R12: 000055f81ab1d220 +R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 + + +The buggy address belongs to the physical page: +page:0000000085430378 refcount:1 mapcount:1 mapping:0000000000000000 index:0x555c6a81d pfn:0x751ac +memcg:ffff888101f7e180 +anon flags: 0xfffffc00a0014(uptodate|lru|mappedtodisk|swapbacked|node=0|zone=1|lastcpupid=0x1fffff) +raw: 000fffffc00a0014 ffffea0001bf2988 ffffea0001de2448 ffff88801712e201 +raw: 0000000555c6a81d 0000000000000000 0000000100000000 ffff888101f7e180 +page dumped because: kasan: bad access detected + +Memory state around the buggy address: + ffff8880751acd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + ffff8880751ace00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +>ffff8880751ace80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + ^ + ffff8880751acf00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + ffff8880751acf80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +================================================================== + +The reason is that struct ATTR_RECORD->name_offset is 6485, end address of +name string is out of bounds. + +Fix this by adding sanity check on end address of attribute name string. + +[akpm@linux-foundation.org: coding-style cleanups] +[chenxiaosong2@huawei.com: cleanup suggested by Hawkins Jiawei] + Link: https://lkml.kernel.org/r/20220709064511.3304299-1-chenxiaosong2@huawei.com +Link: https://lkml.kernel.org/r/20220707105329.4020708-1-chenxiaosong2@huawei.com +Signed-off-by: ChenXiaoSong +Signed-off-by: Hawkins Jiawei +Cc: Anton Altaparmakov +Cc: ChenXiaoSong +Cc: Yongqiang Liu +Cc: Zhang Yi +Cc: Zhang Xiaoxu +Signed-off-by: Andrew Morton +Signed-off-by: Greg Kroah-Hartman +--- + fs/ntfs/attrib.c | 8 ++++++-- + 1 file changed, 6 insertions(+), 2 deletions(-) + +--- a/fs/ntfs/attrib.c ++++ b/fs/ntfs/attrib.c +@@ -606,8 +606,12 @@ static int ntfs_attr_find(const ATTR_TYP + a = (ATTR_RECORD*)((u8*)ctx->attr + + le32_to_cpu(ctx->attr->length)); + for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) { +- if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec + +- le32_to_cpu(ctx->mrec->bytes_allocated)) ++ u8 *mrec_end = (u8 *)ctx->mrec + ++ le32_to_cpu(ctx->mrec->bytes_allocated); ++ u8 *name_end = (u8 *)a + le16_to_cpu(a->name_offset) + ++ a->name_length * sizeof(ntfschar); ++ if ((u8*)a < (u8*)ctx->mrec || (u8*)a > mrec_end || ++ name_end > mrec_end) + break; + ctx->attr = a; + if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) || diff --git a/queue-4.9/series b/queue-4.9/series new file mode 100644 index 00000000000..700e3bdc436 --- /dev/null +++ b/queue-4.9/series @@ -0,0 +1,2 @@ +bluetooth-l2cap-fix-use-after-free-caused-by-l2cap_chan_put.patch +ntfs-fix-use-after-free-in-ntfs_ucsncmp.patch