All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Ye Bin <yebin10@huawei.com>,
	Jan Kara <jack@suse.cz>, Theodore Tso <tytso@mit.edu>,
	stable@kernel.org
Subject: [PATCH 5.10 76/86] ext4: fix use-after-free in ext4_search_dir
Date: Tue, 26 Apr 2022 10:21:44 +0200	[thread overview]
Message-ID: <20220426081743.403331226@linuxfoundation.org> (raw)
In-Reply-To: <20220426081741.202366502@linuxfoundation.org>

From: Ye Bin <yebin10@huawei.com>

commit c186f0887fe7061a35cebef024550ec33ef8fbd8 upstream.

We got issue as follows:
EXT4-fs (loop0): mounted filesystem without journal. Opts: ,errors=continue
==================================================================
BUG: KASAN: use-after-free in ext4_search_dir fs/ext4/namei.c:1394 [inline]
BUG: KASAN: use-after-free in search_dirblock fs/ext4/namei.c:1199 [inline]
BUG: KASAN: use-after-free in __ext4_find_entry+0xdca/0x1210 fs/ext4/namei.c:1553
Read of size 1 at addr ffff8881317c3005 by task syz-executor117/2331

CPU: 1 PID: 2331 Comm: syz-executor117 Not tainted 5.10.0+ #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014
Call Trace:
 __dump_stack lib/dump_stack.c:83 [inline]
 dump_stack+0x144/0x187 lib/dump_stack.c:124
 print_address_description+0x7d/0x630 mm/kasan/report.c:387
 __kasan_report+0x132/0x190 mm/kasan/report.c:547
 kasan_report+0x47/0x60 mm/kasan/report.c:564
 ext4_search_dir fs/ext4/namei.c:1394 [inline]
 search_dirblock fs/ext4/namei.c:1199 [inline]
 __ext4_find_entry+0xdca/0x1210 fs/ext4/namei.c:1553
 ext4_lookup_entry fs/ext4/namei.c:1622 [inline]
 ext4_lookup+0xb8/0x3a0 fs/ext4/namei.c:1690
 __lookup_hash+0xc5/0x190 fs/namei.c:1451
 do_rmdir+0x19e/0x310 fs/namei.c:3760
 do_syscall_64+0x33/0x40 arch/x86/entry/common.c:46
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x445e59
Code: 4d c7 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 0f 83 1b c7 fb ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007fff2277fac8 EFLAGS: 00000246 ORIG_RAX: 0000000000000054
RAX: ffffffffffffffda RBX: 0000000000400280 RCX: 0000000000445e59
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 00000000200000c0
RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000002
R10: 00007fff2277f990 R11: 0000000000000246 R12: 0000000000000000
R13: 431bde82d7b634db R14: 0000000000000000 R15: 0000000000000000

The buggy address belongs to the page:
page:0000000048cd3304 refcount:0 mapcount:0 mapping:0000000000000000 index:0x1 pfn:0x1317c3
flags: 0x200000000000000()
raw: 0200000000000000 ffffea0004526588 ffffea0004528088 0000000000000000
raw: 0000000000000001 0000000000000000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff8881317c2f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 ffff8881317c2f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff8881317c3000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
                   ^
 ffff8881317c3080: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
 ffff8881317c3100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
==================================================================

ext4_search_dir:
  ...
  de = (struct ext4_dir_entry_2 *)search_buf;
  dlimit = search_buf + buf_size;
  while ((char *) de < dlimit) {
  ...
    if ((char *) de + de->name_len <= dlimit &&
	 ext4_match(dir, fname, de)) {
	    ...
    }
  ...
    de_len = ext4_rec_len_from_disk(de->rec_len, dir->i_sb->s_blocksize);
    if (de_len <= 0)
      return -1;
    offset += de_len;
    de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  }

Assume:
de=0xffff8881317c2fff
dlimit=0x0xffff8881317c3000

If read 'de->name_len' which address is 0xffff8881317c3005, obviously is
out of range, then will trigger use-after-free.
To solve this issue, 'dlimit' must reserve 8 bytes, as we will read
'de->name_len' to judge if '(char *) de + de->name_len' out of range.

Signed-off-by: Ye Bin <yebin10@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20220324064816.1209985-1-yebin10@huawei.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: stable@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 fs/ext4/ext4.h  |    4 ++++
 fs/ext4/namei.c |    4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)

--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2159,6 +2159,10 @@ static inline int ext4_forced_shutdown(s
  * Structure of a directory entry
  */
 #define EXT4_NAME_LEN 255
+/*
+ * Base length of the ext4 directory entry excluding the name length
+ */
+#define EXT4_BASE_DIR_LEN (sizeof(struct ext4_dir_entry_2) - EXT4_NAME_LEN)
 
 struct ext4_dir_entry {
 	__le32	inode;			/* Inode number */
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1388,10 +1388,10 @@ int ext4_search_dir(struct buffer_head *
 
 	de = (struct ext4_dir_entry_2 *)search_buf;
 	dlimit = search_buf + buf_size;
-	while ((char *) de < dlimit) {
+	while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
 		/* this code is executed quadratically often */
 		/* do minimal checking `by hand' */
-		if ((char *) de + de->name_len <= dlimit &&
+		if (de->name + de->name_len <= dlimit &&
 		    ext4_match(dir, fname, de)) {
 			/* found a match - just to be sure, do
 			 * a full check */



  parent reply	other threads:[~2022-04-26  8:58 UTC|newest]

Thread overview: 99+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-26  8:20 [PATCH 5.10 00/86] 5.10.113-rc1 review Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 01/86] etherdevice: Adjust ether_addr* prototypes to silence -Wstringop-overead Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 02/86] mm: page_alloc: fix building error on -Werror=array-compare Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 03/86] tracing: Dump stacktrace trigger to the corresponding instance Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 04/86] perf tools: Fix segfault accessing sample_id xyarray Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 05/86] gfs2: assign rgrp glock before compute_bitstructs Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 06/86] net/sched: cls_u32: fix netns refcount changes in u32_change() Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 07/86] ALSA: usb-audio: Clear MIDI port active flag after draining Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 08/86] ALSA: hda/realtek: Add quirk for Clevo NP70PNP Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 09/86] dm: fix mempool NULL pointer race when completing IO Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 10/86] ASoC: atmel: Remove system clock tree configuration for at91sam9g20ek Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 11/86] ASoC: msm8916-wcd-digital: Check failure for devm_snd_soc_register_component Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 12/86] ASoC: codecs: wcd934x: do not switch off SIDO Buck when codec is in use Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 13/86] dmaengine: imx-sdma: Fix error checking in sdma_event_remap Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 14/86] dmaengine: mediatek:Fix PM usage reference leak of mtk_uart_apdma_alloc_chan_resources Greg Kroah-Hartman
2022-04-27 20:28   ` Pavel Machek
2022-04-27 20:32     ` Pavel Machek
2022-04-26  8:20 ` [PATCH 5.10 15/86] spi: spi-mtk-nor: initialize spi controller after resume Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 16/86] esp: limit skb_page_frag_refill use to a single page Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 17/86] igc: Fix infinite loop in release_swfw_sync Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 18/86] igc: Fix BUG: scheduling while atomic Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 19/86] rxrpc: Restore removed timer deletion Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 20/86] net/smc: Fix sock leak when release after smc_shutdown() Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 21/86] net/packet: fix packet_sock xmit return value checking Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 22/86] ip6_gre: Avoid updating tunnel->tun_hlen in __gre6_xmit() Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 23/86] ip6_gre: Fix skb_under_panic " Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 24/86] net/sched: cls_u32: fix possible leak in u32_init_knode() Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 25/86] l3mdev: l3mdev_master_upper_ifindex_by_index_rcu should be using netdev_master_upper_dev_get_rcu Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 26/86] ipv6: make ip6_rt_gc_expire an atomic_t Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 27/86] netlink: reset network and mac headers in netlink_dump() Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 28/86] net: stmmac: Use readl_poll_timeout_atomic() in atomic state Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 29/86] dmaengine: idxd: add RO check for wq max_batch_size write Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 30/86] dmaengine: idxd: add RO check for wq max_transfer_size write Greg Kroah-Hartman
2022-04-26  8:20 ` [PATCH 5.10 31/86] selftests: mlxsw: vxlan_flooding: Prevent flooding of unwanted packets Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 32/86] arm64/mm: Remove [PUD|PMD]_TABLE_BIT from [pud|pmd]_bad() Greg Kroah-Hartman
2022-04-26  8:21   ` Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 33/86] arm64: mm: fix p?d_leaf() Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 34/86] ARM: vexpress/spc: Avoid negative array index when !SMP Greg Kroah-Hartman
2022-04-26  8:21   ` Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 35/86] reset: tegra-bpmp: Restore Handle errors in BPMP response Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 36/86] platform/x86: samsung-laptop: Fix an unsigned comparison which can never be negative Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 37/86] ALSA: usb-audio: Fix undefined behavior due to shift overflowing the constant Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 38/86] arm64: dts: imx: Fix imx8*-var-som touchscreen property sizes Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 39/86] vxlan: fix error return code in vxlan_fdb_append Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 40/86] cifs: Check the IOCB_DIRECT flag, not O_DIRECT Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 41/86] net: atlantic: Avoid out-of-bounds indexing Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 42/86] mt76: Fix undefined behavior due to shift overflowing the constant Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 43/86] brcmfmac: sdio: " Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 44/86] dpaa_eth: Fix missing of_node_put in dpaa_get_ts_info() Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 45/86] drm/msm/mdp5: check the return of kzalloc() Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 46/86] net: macb: Restart tx only if queue pointer is lagging Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 47/86] scsi: qedi: Fix failed disconnect handling Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 48/86] stat: fix inconsistency between struct stat and struct compat_stat Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 49/86] nvme: add a quirk to disable namespace identifiers Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 50/86] nvme-pci: disable namespace identifiers for Qemu controllers Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 51/86] EDAC/synopsys: Read the error count from the correct register Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 52/86] mm, hugetlb: allow for "high" userspace addresses Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 53/86] oom_kill.c: futex: delay the OOM reaper to allow time for proper futex cleanup Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 54/86] mm/mmu_notifier.c: fix race in mmu_interval_notifier_remove() Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 55/86] ata: pata_marvell: Check the bmdma_addr beforing reading Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 56/86] dma: at_xdmac: fix a missing check on list iterator Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 57/86] net: atlantic: invert deep par in pm functions, preventing null derefs Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 58/86] xtensa: patch_text: Fixup last cpu should be master Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 59/86] xtensa: fix a7 clobbering in coprocessor context load/store Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 60/86] openvswitch: fix OOB access in reserve_sfa_size() Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 61/86] gpio: Request interrupts after IRQ is initialized Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 62/86] ASoC: soc-dapm: fix two incorrect uses of list iterator Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 63/86] e1000e: Fix possible overflow in LTR decoding Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 64/86] ARC: entry: fix syscall_trace_exit argument Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 65/86] arm_pmu: Validate single/group leader events Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 66/86] sched/pelt: Fix attach_entity_load_avg() corner case Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 67/86] perf/core: Fix perf_mmap fail when CONFIG_PERF_USE_VMALLOC enabled Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 68/86] drm/panel/raspberrypi-touchscreen: Avoid NULL deref if not initialised Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 69/86] drm/panel/raspberrypi-touchscreen: Initialise the bridge in prepare Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 70/86] KVM: PPC: Fix TCE handling for VFIO Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 71/86] drm/vc4: Use pm_runtime_resume_and_get to fix pm_runtime_get_sync() usage Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 72/86] powerpc/perf: Fix power9 event alternatives Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 73/86] perf report: Set PERF_SAMPLE_DATA_SRC bit for Arm SPE event Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 74/86] ext4: fix fallocate to use file_modified to update permissions consistently Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 75/86] ext4: fix symlink file size not match to file content Greg Kroah-Hartman
2022-04-26  8:21 ` Greg Kroah-Hartman [this message]
2022-04-26  8:21 ` [PATCH 5.10 77/86] ext4: limit length to bitmap_maxbytes - blocksize in punch_hole Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 78/86] ext4, doc: fix incorrect h_reserved size Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 79/86] ext4: fix overhead calculation to account for the reserved gdt blocks Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 80/86] ext4: force overhead calculation if the s_overhead_cluster makes no sense Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 81/86] can: isotp: stop timeout monitoring when no first frame was sent Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 82/86] jbd2: fix a potential race while discarding reserved buffers after an abort Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 83/86] spi: atmel-quadspi: Fix the buswidth adjustment between spi-mem and controller Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 84/86] staging: ion: Prevent incorrect reference counting behavour Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 85/86] block/compat_ioctl: fix range check in BLKGETSIZE Greg Kroah-Hartman
2022-04-26  8:21 ` [PATCH 5.10 86/86] Revert "net: micrel: fix KS8851_MLL Kconfig" Greg Kroah-Hartman
2022-04-26 16:20 ` [PATCH 5.10 00/86] 5.10.113-rc1 review Jon Hunter
2022-04-26 17:12 ` Florian Fainelli
2022-04-26 19:58 ` Pavel Machek
2022-04-26 20:12 ` Guenter Roeck
2022-04-26 20:14 ` Shuah Khan
2022-04-27  1:54 ` Slade Watkins
2022-04-27  8:08 ` Naresh Kamboju
2022-04-27 11:09 ` Sudip Mukherjee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220426081743.403331226@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=jack@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tytso@mit.edu \
    --cc=yebin10@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.