linux-kernel.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, Yunlei He <heyunlei@hihonor.com>,
	Eric Biggers <ebiggers@google.com>, Theodore Tso <tytso@mit.edu>
Subject: [PATCH 5.4 70/71] ext4: fix error handling in ext4_end_enable_verity()
Date: Mon, 24 May 2021 17:26:16 +0200	[thread overview]
Message-ID: <20210524152328.717859629@linuxfoundation.org> (raw)
In-Reply-To: <20210524152326.447759938@linuxfoundation.org>

From: Eric Biggers <ebiggers@google.com>

commit f053cf7aa66cd9d592b0fc967f4d887c2abff1b7 upstream.

ext4 didn't properly clean up if verity failed to be enabled on a file:

- It left verity metadata (pages past EOF) in the page cache, which
  would be exposed to userspace if the file was later extended.

- It didn't truncate the verity metadata at all (either from cache or
  from disk) if an error occurred while setting the verity bit.

Fix these bugs by adding a call to truncate_inode_pages() and ensuring
that we truncate the verity metadata (both from cache and from disk) in
all error paths.  Also rework the code to cleanly separate the success
path from the error paths, which makes it much easier to understand.

Reported-by: Yunlei He <heyunlei@hihonor.com>
Fixes: c93d8f885809 ("ext4: add basic fs-verity support")
Cc: stable@vger.kernel.org # v5.4+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Link: https://lore.kernel.org/r/20210302200420.137977-2-ebiggers@kernel.org
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 fs/ext4/verity.c |   93 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 57 insertions(+), 36 deletions(-)

--- a/fs/ext4/verity.c
+++ b/fs/ext4/verity.c
@@ -198,55 +198,76 @@ static int ext4_end_enable_verity(struct
 	struct inode *inode = file_inode(filp);
 	const int credits = 2; /* superblock and inode for ext4_orphan_del() */
 	handle_t *handle;
+	struct ext4_iloc iloc;
 	int err = 0;
-	int err2;
 
-	if (desc != NULL) {
-		/* Succeeded; write the verity descriptor. */
-		err = ext4_write_verity_descriptor(inode, desc, desc_size,
-						   merkle_tree_size);
-
-		/* Write all pages before clearing VERITY_IN_PROGRESS. */
-		if (!err)
-			err = filemap_write_and_wait(inode->i_mapping);
-	}
+	/*
+	 * If an error already occurred (which fs/verity/ signals by passing
+	 * desc == NULL), then only clean-up is needed.
+	 */
+	if (desc == NULL)
+		goto cleanup;
 
-	/* If we failed, truncate anything we wrote past i_size. */
-	if (desc == NULL || err)
-		ext4_truncate(inode);
+	/* Append the verity descriptor. */
+	err = ext4_write_verity_descriptor(inode, desc, desc_size,
+					   merkle_tree_size);
+	if (err)
+		goto cleanup;
 
 	/*
-	 * We must always clean up by clearing EXT4_STATE_VERITY_IN_PROGRESS and
-	 * deleting the inode from the orphan list, even if something failed.
-	 * If everything succeeded, we'll also set the verity bit in the same
-	 * transaction.
+	 * Write all pages (both data and verity metadata).  Note that this must
+	 * happen before clearing EXT4_STATE_VERITY_IN_PROGRESS; otherwise pages
+	 * beyond i_size won't be written properly.  For crash consistency, this
+	 * also must happen before the verity inode flag gets persisted.
 	 */
+	err = filemap_write_and_wait(inode->i_mapping);
+	if (err)
+		goto cleanup;
 
-	ext4_clear_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
+	/*
+	 * Finally, set the verity inode flag and remove the inode from the
+	 * orphan list (in a single transaction).
+	 */
 
 	handle = ext4_journal_start(inode, EXT4_HT_INODE, credits);
 	if (IS_ERR(handle)) {
-		ext4_orphan_del(NULL, inode);
-		return PTR_ERR(handle);
+		err = PTR_ERR(handle);
+		goto cleanup;
 	}
 
-	err2 = ext4_orphan_del(handle, inode);
-	if (err2)
-		goto out_stop;
-
-	if (desc != NULL && !err) {
-		struct ext4_iloc iloc;
-
-		err = ext4_reserve_inode_write(handle, inode, &iloc);
-		if (err)
-			goto out_stop;
-		ext4_set_inode_flag(inode, EXT4_INODE_VERITY);
-		ext4_set_inode_flags(inode);
-		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
-	}
-out_stop:
+	err = ext4_orphan_del(handle, inode);
+	if (err)
+		goto stop_and_cleanup;
+
+	err = ext4_reserve_inode_write(handle, inode, &iloc);
+	if (err)
+		goto stop_and_cleanup;
+
+	ext4_set_inode_flag(inode, EXT4_INODE_VERITY);
+	ext4_set_inode_flags(inode);
+	err = ext4_mark_iloc_dirty(handle, inode, &iloc);
+	if (err)
+		goto stop_and_cleanup;
+
 	ext4_journal_stop(handle);
-	return err ?: err2;
+
+	ext4_clear_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
+	return 0;
+
+stop_and_cleanup:
+	ext4_journal_stop(handle);
+cleanup:
+	/*
+	 * Verity failed to be enabled, so clean up by truncating any verity
+	 * metadata that was written beyond i_size (both from cache and from
+	 * disk), removing the inode from the orphan list (if it wasn't done
+	 * already), and clearing EXT4_STATE_VERITY_IN_PROGRESS.
+	 */
+	truncate_inode_pages(inode->i_mapping, inode->i_size);
+	ext4_truncate(inode);
+	ext4_orphan_del(NULL, inode);
+	ext4_clear_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
+	return err;
 }
 
 static int ext4_get_verity_descriptor_location(struct inode *inode,



  parent reply	other threads:[~2021-05-24 15:56 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-24 15:25 [PATCH 5.4 00/71] 5.4.122-rc1 review Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 01/71] firmware: arm_scpi: Prevent the ternary sign expansion bug Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 02/71] openrisc: Fix a memory leak Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 03/71] RDMA/siw: Properly check send and receive CQ pointers Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 04/71] RDMA/siw: Release xarray entry Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 05/71] RDMA/rxe: Clear all QP fields if creation failed Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 06/71] scsi: ufs: core: Increase the usable queue depth Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 07/71] scsi: qla2xxx: Fix error return code in qla82xx_write_flash_dword() Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 08/71] RDMA/mlx5: Recover from fatal event in dual port mode Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 09/71] RDMA/core: Dont access cm_id after its destruction Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 10/71] platform/mellanox: mlxbf-tmfifo: Fix a memory barrier issue Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 11/71] platform/x86: dell-smbios-wmi: Fix oops on rmmod dell_smbios Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 12/71] RDMA/uverbs: Fix a NULL vs IS_ERR() bug Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 13/71] ptrace: make ptrace() fail if the tracee changed its pid unexpectedly Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 14/71] nvmet: seset ns->file when open fails Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 15/71] locking/mutex: clear MUTEX_FLAGS if wait_list is empty due to signal Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 16/71] btrfs: avoid RCU stalls while running delayed iputs Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 17/71] cifs: fix memory leak in smb2_copychunk_range Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 18/71] ALSA: dice: fix stream format for TC Electronic Konnekt Live at high sampling transfer frequency Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 19/71] ALSA: intel8x0: Dont update period unless prepared Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 20/71] ALSA: line6: Fix racy initialization of LINE6 MIDI Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 21/71] ALSA: dice: fix stream format at middle sampling rate for Alesis iO 26 Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 22/71] ALSA: firewire-lib: fix calculation for size of IR context payload Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 23/71] ALSA: usb-audio: Validate MS endpoint descriptors Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 24/71] ALSA: bebob/oxfw: fix Kconfig entry for Mackie d.2 Pro Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 25/71] ALSA: hda: fixup headset for ASUS GU502 laptop Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 26/71] Revert "ALSA: sb8: add a check for request_region" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 27/71] ALSA: firewire-lib: fix check for the size of isochronous packet payload Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 28/71] ALSA: hda/realtek: reset eapd coeff to default value for alc287 Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 29/71] ALSA: hda/realtek: Add some CLOVE SSIDs of ALC293 Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 30/71] ALSA: hda/realtek: Fix silent headphone output on ASUS UX430UA Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 31/71] ALSA: hda/realtek: Add fixup for HP OMEN laptop Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 32/71] ALSA: hda/realtek: Add fixup for HP Spectre x360 15-df0xxx Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 33/71] uio_hv_generic: Fix a memory leak in error handling paths Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 34/71] Revert "rapidio: fix a NULL pointer dereference when create_workqueue() fails" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 35/71] rapidio: handle create_workqueue() failure Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 36/71] Revert "serial: mvebu-uart: Fix to avoid a potential NULL pointer dereference" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 37/71] drm/amdgpu: disable 3DCGCG on picasso/raven1 to avoid compute hang Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 38/71] drm/amdgpu: update gc golden setting for Navi12 Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 39/71] drm/amdgpu: update sdma " Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 40/71] mmc: sdhci-pci-gli: increase 1.8V regulator wait Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 41/71] xen-pciback: reconfigure also from backend watch handler Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 42/71] dm snapshot: fix a crash when an origin has no snapshots Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 43/71] dm snapshot: fix crash with transient storage and zero chunk size Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 44/71] Revert "video: hgafb: fix potential NULL pointer dereference" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 45/71] Revert "net: stmicro: fix a missing check of clk_prepare" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 46/71] Revert "leds: lp5523: fix a missing check of return value of lp55xx_read" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 47/71] Revert "hwmon: (lm80) fix a missing check of bus read in lm80 probe" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 48/71] Revert "video: imsttfb: fix potential NULL pointer dereferences" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 49/71] Revert "ecryptfs: replace BUG_ON with error handling code" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 50/71] Revert "scsi: ufs: fix a missing check of devm_reset_control_get" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 51/71] Revert "gdrom: fix a memory leak bug" Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 52/71] cdrom: gdrom: deallocate struct gdrom_unit fields in remove_gdrom Greg Kroah-Hartman
2021-05-24 15:25 ` [PATCH 5.4 53/71] cdrom: gdrom: initialize global variable at init time Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 54/71] Revert "media: rcar_drif: fix a memory disclosure" Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 55/71] Revert "rtlwifi: fix a potential NULL pointer dereference" Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 56/71] Revert "qlcnic: Avoid " Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 57/71] Revert "niu: fix missing checks of niu_pci_eeprom_read" Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 58/71] ethernet: sun: niu: fix missing checks of niu_pci_eeprom_read() Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 59/71] net: stmicro: handle clk_prepare() failure during init Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 60/71] scsi: ufs: handle cleanup correctly on devm_reset_control_get error Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 61/71] net: rtlwifi: properly check for alloc_workqueue() failure Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 62/71] ics932s401: fix broken handling of errors when word reading fails Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 63/71] leds: lp5523: check return value of lp5xx_read and jump to cleanup code Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 64/71] qlcnic: Add null check after calling netdev_alloc_skb Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 65/71] video: hgafb: fix potential NULL pointer dereference Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 66/71] vgacon: Record video mode changes with VT_RESIZEX Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 67/71] vt: Fix character height handling " Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 68/71] tty: vt: always invoke vc->vc_sw->con_resize callback Greg Kroah-Hartman
2021-05-24 15:26 ` [PATCH 5.4 69/71] nvme-multipath: fix double initialization of ANA state Greg Kroah-Hartman
2021-05-24 15:26 ` Greg Kroah-Hartman [this message]
2021-05-24 15:26 ` [PATCH 5.4 71/71] Bluetooth: L2CAP: Fix handling LE modes by L2CAP_OPTIONS Greg Kroah-Hartman
2021-05-24 21:32 ` [PATCH 5.4 00/71] 5.4.122-rc1 review Florian Fainelli
2021-05-24 22:05 ` Shuah Khan
2021-05-25  7:35 ` Naresh Kamboju
2021-05-25 14:23 ` Sudip Mukherjee
2021-05-25 18:28 ` Guenter Roeck
2021-05-26  7:08   ` Greg Kroah-Hartman
2021-05-25 21:26 ` Guenter Roeck
2021-05-26  1:02 ` Samuel Zou

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=20210524152328.717859629@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ebiggers@google.com \
    --cc=heyunlei@hihonor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tytso@mit.edu \
    /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).