stable.vger.kernel.org archive mirror
 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, Junxiao Bi <junxiao.bi@oracle.com>,
	Joseph Qi <joseph.qi@linux.alibaba.com>, Jan Kara <jack@suse.cz>,
	Mark Fasheh <mark@fasheh.com>, Joel Becker <jlbec@evilplan.org>,
	Changwei Ge <gechangwei@live.cn>, Gang He <ghe@suse.com>,
	Jun Piao <piaojun@huawei.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH 5.4 54/78] ocfs2: fix data corruption by fallocate
Date: Tue,  8 Jun 2021 20:27:23 +0200	[thread overview]
Message-ID: <20210608175937.106368315@linuxfoundation.org> (raw)
In-Reply-To: <20210608175935.254388043@linuxfoundation.org>

From: Junxiao Bi <junxiao.bi@oracle.com>

commit 6bba4471f0cc1296fe3c2089b9e52442d3074b2e upstream.

When fallocate punches holes out of inode size, if original isize is in
the middle of last cluster, then the part from isize to the end of the
cluster will be zeroed with buffer write, at that time isize is not yet
updated to match the new size, if writeback is kicked in, it will invoke
ocfs2_writepage()->block_write_full_page() where the pages out of inode
size will be dropped.  That will cause file corruption.  Fix this by
zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted
coverted image file easily.

    qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
             -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out
of inode size, then extend the inode size.

    fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0
    fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html
v2: https://lore.kernel.org/linux-fsdevel/20210525093034.GB4112@quack2.suse.cz/T/

Link: https://lkml.kernel.org/r/20210528210648.9124-1-junxiao.bi@oracle.com
Signed-off-by: Junxiao Bi <junxiao.bi@oracle.com>
Reviewed-by: Joseph Qi <joseph.qi@linux.alibaba.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Mark Fasheh <mark@fasheh.com>
Cc: Joel Becker <jlbec@evilplan.org>
Cc: Changwei Ge <gechangwei@live.cn>
Cc: Gang He <ghe@suse.com>
Cc: Jun Piao <piaojun@huawei.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 fs/ocfs2/file.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 5 deletions(-)

--- a/fs/ocfs2/file.c
+++ b/fs/ocfs2/file.c
@@ -1856,6 +1856,45 @@ out:
 }
 
 /*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+					u64 start, u64 len)
+{
+	int ret;
+	u64 start_block, end_block, nr_blocks;
+	u64 p_block, offset;
+	u32 cluster, p_cluster, nr_clusters;
+	struct super_block *sb = inode->i_sb;
+	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+	if (start + len < end)
+		end = start + len;
+
+	start_block = ocfs2_blocks_for_bytes(sb, start);
+	end_block = ocfs2_blocks_for_bytes(sb, end);
+	nr_blocks = end_block - start_block;
+	if (!nr_blocks)
+		return 0;
+
+	cluster = ocfs2_bytes_to_clusters(sb, start);
+	ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+				&nr_clusters, NULL);
+	if (ret)
+		return ret;
+	if (!p_cluster)
+		return 0;
+
+	offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+	p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+	return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
+/*
  * Parts of this function taken from xfs_change_file_space()
  */
 static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(str
 {
 	int ret;
 	s64 llen;
-	loff_t size;
+	loff_t size, orig_isize;
 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
 	struct buffer_head *di_bh = NULL;
 	handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(str
 		goto out_inode_unlock;
 	}
 
+	orig_isize = i_size_read(inode);
 	switch (sr->l_whence) {
 	case 0: /*SEEK_SET*/
 		break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(str
 		sr->l_start += f_pos;
 		break;
 	case 2: /*SEEK_END*/
-		sr->l_start += i_size_read(inode);
+		sr->l_start += orig_isize;
 		break;
 	default:
 		ret = -EINVAL;
@@ -1957,6 +1997,14 @@ static int __ocfs2_change_file_space(str
 	default:
 		ret = -EINVAL;
 	}
+
+	/* zeroout eof blocks in the cluster. */
+	if (!ret && change_size && orig_isize < size) {
+		ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+					size - orig_isize);
+		if (!ret)
+			i_size_write(inode, size);
+	}
 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
 	if (ret) {
 		mlog_errno(ret);
@@ -1973,9 +2021,6 @@ static int __ocfs2_change_file_space(str
 		goto out_inode_unlock;
 	}
 
-	if (change_size && i_size_read(inode) < size)
-		i_size_write(inode, size);
-
 	inode->i_ctime = inode->i_mtime = current_time(inode);
 	ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
 	if (ret < 0)



  parent reply	other threads:[~2021-06-08 18:46 UTC|newest]

Thread overview: 87+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-08 18:26 [PATCH 5.4 00/78] 5.4.125-rc1 review Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 01/78] btrfs: tree-checker: do not error out if extent ref hash doesnt match Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 02/78] net: usb: cdc_ncm: dont spew notifications Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 03/78] ALSA: usb: update old-style static const declaration Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 04/78] nl80211: validate key indexes for cfg80211_registered_device Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 05/78] hwmon: (dell-smm-hwmon) Fix index values Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 06/78] netfilter: conntrack: unregister ipv4 sockopts on error unwind Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 07/78] efi: Allow EFI_MEMORY_XP and EFI_MEMORY_RO both to be cleared Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 08/78] efi: cper: fix snprintf() use in cper_dimm_err_location() Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 09/78] vfio/pci: Fix error return code in vfio_ecap_init() Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 10/78] vfio/pci: zap_vma_ptes() needs MMU Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 11/78] samples: vfio-mdev: fix error handing in mdpy_fb_probe() Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 12/78] vfio/platform: fix module_put call in error flow Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 13/78] ipvs: ignore IP_VS_SVC_F_HASHED flag when adding service Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 14/78] HID: pidff: fix error return code in hid_pidff_init() Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 15/78] HID: i2c-hid: fix format string mismatch Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 16/78] net/sched: act_ct: Fix ct template allocation for zone 0 Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 17/78] ACPICA: Clean up context mutex during object deletion Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 18/78] netfilter: nft_ct: skip expectations for confirmed conntrack Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 19/78] netfilter: nfnetlink_cthelper: hit EBUSY on updates if size mismatches Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 20/78] ieee802154: fix error return code in ieee802154_add_iface() Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 21/78] ieee802154: fix error return code in ieee802154_llsec_getparams() Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 22/78] ixgbevf: add correct exception tracing for XDP Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 23/78] ipv6: Fix KASAN: slab-out-of-bounds Read in fib6_nh_flush_exceptions Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 24/78] ice: write register with correct offset Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 25/78] ice: Fix VFR issues for AVF drivers that expect ATQLEN cleared Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 26/78] ice: Allow all LLDP packets from PF to Tx Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 27/78] i2c: qcom-geni: Add shutdown callback for i2c Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 28/78] i40e: optimize for XDP_REDIRECT in xsk path Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 29/78] i40e: add correct exception tracing for XDP Greg Kroah-Hartman
2021-06-08 18:26 ` [PATCH 5.4 30/78] arm64: dts: ls1028a: fix memory node Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 31/78] arm64: dts: zii-ultra: fix 12V_MAIN voltage Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 32/78] ARM: dts: imx7d-meerkat96: Fix the tuning-step property Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 33/78] ARM: dts: imx7d-pico: " Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 34/78] ARM: dts: imx: emcon-avari: Fix nxp,pca8574 #gpio-cells Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 35/78] bus: ti-sysc: Fix flakey idling of uarts and stop using swsup_sidle_act Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 36/78] tipc: add extack messages for bearer/media failure Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 37/78] tipc: fix unique bearer names sanity check Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 38/78] Bluetooth: fix the erroneous flush_work() order Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 39/78] Bluetooth: use correct lock to prevent UAF of hdev object Greg Kroah-Hartman
2021-06-14 14:15   ` Eric Dumazet
2021-06-16 15:01     ` Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 40/78] net: caif: added cfserl_release function Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 41/78] net: caif: add proper error handling Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 42/78] net: caif: fix memory leak in caif_device_notify Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 43/78] net: caif: fix memory leak in cfusbl_device_notify Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 44/78] HID: i2c-hid: Skip ELAN power-on command after reset Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 45/78] HID: magicmouse: fix NULL-deref on disconnect Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 46/78] HID: multitouch: require Finger field to mark Win8 reports as MT Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 47/78] ALSA: timer: Fix master timer notification Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 48/78] ALSA: hda: Fix for mute key LED for HP Pavilion 15-CK0xx Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 49/78] ARM: dts: imx6dl-yapp4: Fix RGMII connection to QCA8334 switch Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 50/78] ARM: dts: imx6q-dhcom: Add PU,VDD1P1,VDD2P5 regulators Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 51/78] ext4: fix bug on in ext4_es_cache_extent as ext4_split_extent_at failed Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 52/78] usb: dwc2: Fix build in periphal-only mode Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 53/78] pid: take a reference when initializing `cad_pid` Greg Kroah-Hartman
2021-06-08 18:27 ` Greg Kroah-Hartman [this message]
2021-06-08 18:27 ` [PATCH 5.4 55/78] nfc: fix NULL ptr dereference in llcp_sock_getname() after failed connect Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 56/78] drm/amdgpu: Dont query CE and UE errors Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 57/78] drm/amdgpu: make sure we unpin the UVD BO Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 58/78] x86/apic: Mark _all_ legacy interrupts when IO/APIC is missing Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 59/78] btrfs: mark ordered extent and inode with error if we fail to finish Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 60/78] btrfs: fix error handling in btrfs_del_csums Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 61/78] btrfs: return errors from btrfs_del_csums in cleanup_ref_head Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 62/78] btrfs: fixup error handling in fixup_inode_link_counts Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 63/78] mm, hugetlb: fix simple resv_huge_pages underflow on UFFDIO_COPY Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 64/78] bnxt_en: Remove the setting of dev_port Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 65/78] mm: add thp_order Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 66/78] XArray: add xa_get_order Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 67/78] XArray: add xas_split Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 68/78] mm/filemap: fix storing to a THP shadow entry Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 69/78] btrfs: fix unmountable seed device after fstrim Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 70/78] KVM: SVM: Truncate GPR value for DR and CR accesses in !64-bit mode Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 71/78] KVM: arm64: Fix debug register indexing Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 72/78] x86/kvm: Teardown PV features on boot CPU as well Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 73/78] x86/kvm: Disable kvmclock on all CPUs on shutdown Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 74/78] x86/kvm: Disable all PV features on crash Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 75/78] lib/lz4: explicitly support in-place decompression Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 76/78] xen-pciback: redo VF placement in the virtual topology Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 77/78] i2c: qcom-geni: Suspend and resume the bus during SYSTEM_SLEEP_PM ops Greg Kroah-Hartman
2021-06-08 18:27 ` [PATCH 5.4 78/78] neighbour: allow NUD_NOARP entries to be forced GCed Greg Kroah-Hartman
2021-06-09  2:55 ` [PATCH 5.4 00/78] 5.4.125-rc1 review Shuah Khan
2021-06-09  9:33 ` Jon Hunter
2021-06-09 10:15 ` Naresh Kamboju
2021-06-09 18:40 ` Florian Fainelli
2021-06-09 18:49 ` Guenter Roeck
2021-06-10  9:01 ` 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=20210608175937.106368315@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=gechangwei@live.cn \
    --cc=ghe@suse.com \
    --cc=jack@suse.cz \
    --cc=jlbec@evilplan.org \
    --cc=joseph.qi@linux.alibaba.com \
    --cc=junxiao.bi@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark@fasheh.com \
    --cc=piaojun@huawei.com \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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).