stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev,
	Ryusuke Konishi <konishi.ryusuke@gmail.com>,
	syzbot+f816fa82f8783f7a02bb@syzkaller.appspotmail.com,
	Shigeru Yoshida <syoshida@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH 5.10 70/95] nilfs2: fix use-after-free bug of ns_writer on remount
Date: Mon, 14 Nov 2022 13:46:04 +0100	[thread overview]
Message-ID: <20221114124445.454760373@linuxfoundation.org> (raw)
In-Reply-To: <20221114124442.530286937@linuxfoundation.org>

From: Ryusuke Konishi <konishi.ryusuke@gmail.com>

commit 8cccf05fe857a18ee26e20d11a8455a73ffd4efd upstream.

If a nilfs2 filesystem is downgraded to read-only due to metadata
corruption on disk and is remounted read/write, or if emergency read-only
remount is performed, detaching a log writer and synchronizing the
filesystem can be done at the same time.

In these cases, use-after-free of the log writer (hereinafter
nilfs->ns_writer) can happen as shown in the scenario below:

 Task1                               Task2
 --------------------------------    ------------------------------
 nilfs_construct_segment
   nilfs_segctor_sync
     init_wait
     init_waitqueue_entry
     add_wait_queue
     schedule
                                     nilfs_remount (R/W remount case)
				       nilfs_attach_log_writer
                                         nilfs_detach_log_writer
                                           nilfs_segctor_destroy
                                             kfree
     finish_wait
       _raw_spin_lock_irqsave
         __raw_spin_lock_irqsave
           do_raw_spin_lock
             debug_spin_lock_before  <-- use-after-free

While Task1 is sleeping, nilfs->ns_writer is freed by Task2.  After Task1
waked up, Task1 accesses nilfs->ns_writer which is already freed.  This
scenario diagram is based on the Shigeru Yoshida's post [1].

This patch fixes the issue by not detaching nilfs->ns_writer on remount so
that this UAF race doesn't happen.  Along with this change, this patch
also inserts a few necessary read-only checks with superblock instance
where only the ns_writer pointer was used to check if the filesystem is
read-only.

Link: https://syzkaller.appspot.com/bug?id=79a4c002e960419ca173d55e863bd09e8112df8b
Link: https://lkml.kernel.org/r/20221103141759.1836312-1-syoshida@redhat.com [1]
Link: https://lkml.kernel.org/r/20221104142959.28296-1-konishi.ryusuke@gmail.com
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Reported-by: syzbot+f816fa82f8783f7a02bb@syzkaller.appspotmail.com
Reported-by: Shigeru Yoshida <syoshida@redhat.com>
Tested-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 fs/nilfs2/segment.c |   15 ++++++++-------
 fs/nilfs2/super.c   |    2 --
 2 files changed, 8 insertions(+), 9 deletions(-)

--- a/fs/nilfs2/segment.c
+++ b/fs/nilfs2/segment.c
@@ -322,7 +322,7 @@ void nilfs_relax_pressure_in_lock(struct
 	struct the_nilfs *nilfs = sb->s_fs_info;
 	struct nilfs_sc_info *sci = nilfs->ns_writer;
 
-	if (!sci || !sci->sc_flush_request)
+	if (sb_rdonly(sb) || unlikely(!sci) || !sci->sc_flush_request)
 		return;
 
 	set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
@@ -2248,7 +2248,7 @@ int nilfs_construct_segment(struct super
 	struct nilfs_transaction_info *ti;
 	int err;
 
-	if (!sci)
+	if (sb_rdonly(sb) || unlikely(!sci))
 		return -EROFS;
 
 	/* A call inside transactions causes a deadlock. */
@@ -2287,7 +2287,7 @@ int nilfs_construct_dsync_segment(struct
 	struct nilfs_transaction_info ti;
 	int err = 0;
 
-	if (!sci)
+	if (sb_rdonly(sb) || unlikely(!sci))
 		return -EROFS;
 
 	nilfs_transaction_lock(sb, &ti, 0);
@@ -2783,11 +2783,12 @@ int nilfs_attach_log_writer(struct super
 
 	if (nilfs->ns_writer) {
 		/*
-		 * This happens if the filesystem was remounted
-		 * read/write after nilfs_error degenerated it into a
-		 * read-only mount.
+		 * This happens if the filesystem is made read-only by
+		 * __nilfs_error or nilfs_remount and then remounted
+		 * read/write.  In these cases, reuse the existing
+		 * writer.
 		 */
-		nilfs_detach_log_writer(sb);
+		return 0;
 	}
 
 	nilfs->ns_writer = nilfs_segctor_new(sb, root);
--- a/fs/nilfs2/super.c
+++ b/fs/nilfs2/super.c
@@ -1133,8 +1133,6 @@ static int nilfs_remount(struct super_bl
 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
 		goto out;
 	if (*flags & SB_RDONLY) {
-		/* Shutting down log writer */
-		nilfs_detach_log_writer(sb);
 		sb->s_flags |= SB_RDONLY;
 
 		/*



  parent reply	other threads:[~2022-11-14 12:50 UTC|newest]

Thread overview: 106+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-14 12:44 [PATCH 5.10 00/95] 5.10.155-rc1 review Greg Kroah-Hartman
2022-11-14 12:44 ` [PATCH 5.10 01/95] fuse: fix readdir cache race Greg Kroah-Hartman
2022-11-14 12:44 ` [PATCH 5.10 02/95] hwspinlock: qcom: correct MMIO max register for newer SoCs Greg Kroah-Hartman
2022-11-14 12:44 ` [PATCH 5.10 03/95] phy: stm32: fix an error code in probe Greg Kroah-Hartman
2022-11-14 12:44 ` [PATCH 5.10 04/95] wifi: cfg80211: silence a sparse RCU warning Greg Kroah-Hartman
2022-11-14 12:44 ` [PATCH 5.10 05/95] wifi: cfg80211: fix memory leak in query_regdb_file() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 06/95] bpf, sockmap: Fix the sk->sk_forward_alloc warning of sk_stream_kill_queues Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 07/95] bpftool: Fix NULL pointer dereference when pin {PROG, MAP, LINK} without FILE Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 08/95] HID: hyperv: fix possible memory leak in mousevsc_probe() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 09/95] bpf: Support for pointers beyond pkt_end Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 10/95] bpf: Add helper macro bpf_for_each_reg_in_vstate Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 11/95] bpf: Fix wrong reg type conversion in release_reference() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 12/95] net: gso: fix panic on frag_list with mixed head alloc types Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 13/95] macsec: delete new rxsc when offload fails Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 14/95] macsec: fix secy->n_rx_sc accounting Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 15/95] macsec: fix detection of RXSCs when toggling offloading Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 16/95] macsec: clear encryption keys from the stack after setting up offload Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 17/95] net: tun: Fix memory leaks of napi_get_frags Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 18/95] bnxt_en: Fix possible crash in bnxt_hwrm_set_coal() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 19/95] bnxt_en: fix potentially incorrect return value for ndo_rx_flow_steer Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 20/95] net: fman: Unregister ethernet device on removal Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 21/95] capabilities: fix undefined behavior in bit shift for CAP_TO_MASK Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 22/95] KVM: s390x: fix SCK locking Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 23/95] KVM: s390: pv: dont allow userspace to set the clock under PV Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 24/95] net: lapbether: fix issue of dev reference count leakage in lapbeth_device_event() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 25/95] hamradio: fix issue of dev reference count leakage in bpq_device_event() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 26/95] drm/vc4: Fix missing platform_unregister_drivers() call in vc4_drm_register() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 27/95] tcp: prohibit TCP_REPAIR_OPTIONS if data was already sent Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 28/95] ipv6: addrlabel: fix infoleak when sending struct ifaddrlblmsg to network Greg Kroah-Hartman
2022-11-14 12:48   ` syzbot
2022-11-14 12:45 ` [PATCH 5.10 29/95] can: af_can: fix NULL pointer dereference in can_rx_register() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 30/95] net: stmmac: dwmac-meson8b: fix meson8b_devm_clk_prepare_enable() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 31/95] net: broadcom: Fix BCMGENET Kconfig Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 32/95] tipc: fix the msg->req tlv len check in tipc_nl_compat_name_table_dump_header Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 33/95] dmaengine: pxa_dma: use platform_get_irq_optional Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 34/95] dmaengine: mv_xor_v2: Fix a resource leak in mv_xor_v2_remove() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 35/95] drivers: net: xgene: disable napi when register irq failed in xgene_enet_open() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 36/95] perf stat: Fix printing os->prefix in CSV metrics output Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 37/95] net: marvell: prestera: fix memory leak in prestera_rxtx_switch_init() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 38/95] net: nixge: disable napi when enable interrupts failed in nixge_open() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 39/95] net/mlx5: Allow async trigger completion execution on single CPU systems Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 40/95] net/mlx5e: E-Switch, Fix comparing termination table instance Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 41/95] net: cpsw: disable napi in cpsw_ndo_open() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 42/95] net: cxgb3_main: disable napi when bind qsets failed in cxgb_up() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 43/95] cxgb4vf: shut down the adapter when t4vf_update_port_info() failed in cxgb4vf_open() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 44/95] net: phy: mscc: macsec: clear encryption keys when freeing a flow Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 45/95] net: atlantic: macsec: clear encryption keys from the stack Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 46/95] ethernet: s2io: disable napi when start nic failed in s2io_card_up() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 47/95] net: mv643xx_eth: disable napi when init rxq or txq failed in mv643xx_eth_open() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 48/95] ethernet: tundra: free irq when alloc ring failed in tsi108_open() Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 49/95] net: macvlan: fix memory leaks of macvlan_common_newlink Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 50/95] riscv: process: fix kernel info leakage Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 51/95] riscv: vdso: fix build with llvm Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 52/95] riscv: Enable CMA support Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 53/95] riscv: Separate memory init from paging init Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 54/95] riscv: fix reserved memory setup Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 55/95] arm64: efi: Fix handling of misaligned runtime regions and drop warning Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 56/95] MIPS: jump_label: Fix compat branch range check Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 57/95] mmc: cqhci: Provide helper for resetting both SDHCI and CQHCI Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 58/95] mmc: sdhci-of-arasan: Fix SDHCI_RESET_ALL for CQHCI Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 59/95] mmc: sdhci_am654: " Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 60/95] mmc: sdhci-tegra: " Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 61/95] ALSA: hda/hdmi - enable runtime pm for more AMD display audio Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 62/95] ALSA: hda/ca0132: add quirk for EVGA Z390 DARK Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 63/95] ALSA: hda: fix potential memleak in add_widget_node Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 64/95] ALSA: hda/realtek: Add Positivo C6300 model quirk Greg Kroah-Hartman
2022-11-14 12:45 ` [PATCH 5.10 65/95] ALSA: usb-audio: Add quirk entry for M-Audio Micro Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 66/95] ALSA: usb-audio: Add DSD support for Accuphase DAC-60 Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 67/95] vmlinux.lds.h: Fix placement of .data..decrypted section Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 68/95] ata: libata-scsi: fix SYNCHRONIZE CACHE (16) command failure Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 69/95] nilfs2: fix deadlock in nilfs_count_free_blocks() Greg Kroah-Hartman
2022-11-14 12:46 ` Greg Kroah-Hartman [this message]
2022-11-14 12:46 ` [PATCH 5.10 71/95] drm/i915/dmabuf: fix sg_table handling in map_dma_buf Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 72/95] platform/x86: hp_wmi: Fix rfkill causing soft blocked wifi Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 73/95] btrfs: selftests: fix wrong error check in btrfs_free_dummy_root() Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 74/95] mms: sdhci-esdhc-imx: Fix SDHCI_RESET_ALL for CQHCI Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 75/95] udf: Fix a slab-out-of-bounds write bug in udf_find_entry() Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 76/95] mm/memremap.c: map FS_DAX device memory as decrypted Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 77/95] can: j1939: j1939_send_one(): fix missing CAN header initialization Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 78/95] cert host tools: Stop complaining about deprecated OpenSSL functions Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 79/95] dmaengine: at_hdmac: Fix at_lli struct definition Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 80/95] dmaengine: at_hdmac: Dont start transactions at tx_submit level Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 81/95] dmaengine: at_hdmac: Start transfer for cyclic channels in issue_pending Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 82/95] dmaengine: at_hdmac: Fix premature completion of desc " Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 83/95] dmaengine: at_hdmac: Do not call the complete callback on device_terminate_all Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 84/95] dmaengine: at_hdmac: Protect atchan->status with the channel lock Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 85/95] dmaengine: at_hdmac: Fix concurrency problems by removing atc_complete_all() Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 86/95] dmaengine: at_hdmac: Fix concurrency over descriptor Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 87/95] dmaengine: at_hdmac: Free the memset buf without holding the chan lock Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 88/95] dmaengine: at_hdmac: Fix concurrency over the active list Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 89/95] dmaengine: at_hdmac: Fix descriptor handling when issuing it to hardware Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 90/95] dmaengine: at_hdmac: Fix completion of unissued descriptor in case of errors Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 91/95] dmaengine: at_hdmac: Dont allow CPU to reorder channel enable Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 92/95] dmaengine: at_hdmac: Fix impossible condition Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 93/95] dmaengine: at_hdmac: Check return code of dma_async_device_register Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 94/95] net: tun: call napi_schedule_prep() to ensure we own a napi Greg Kroah-Hartman
2022-11-14 12:46 ` [PATCH 5.10 95/95] mmc: sdhci-esdhc-imx: Convert the driver to DT-only Greg Kroah-Hartman
2022-11-14 18:35 ` [PATCH 5.10 00/95] 5.10.155-rc1 review Pavel Machek
2022-11-14 20:20 ` Shuah Khan
2022-11-14 21:06 ` Slade Watkins
2022-11-14 21:44 ` Florian Fainelli
2022-11-15  0:12 ` Guenter Roeck
2022-11-15 10:50 ` Sudip Mukherjee
2022-11-15 10:58 ` Naresh Kamboju
2022-11-15 11:49 ` Rudi Heitbaum
2022-11-16  1:44 ` zhouzhixiu

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=20221114124445.454760373@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=konishi.ryusuke@gmail.com \
    --cc=patches@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=syoshida@redhat.com \
    --cc=syzbot+f816fa82f8783f7a02bb@syzkaller.appspotmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).