Skip to content

Commit f053cf7

Browse files
ebiggerstytso
authored andcommitted
ext4: fix error handling in ext4_end_enable_verity()
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: c93d8f8 ("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>
1 parent c915fb8 commit f053cf7

1 file changed

Lines changed: 55 additions & 34 deletions

File tree

fs/ext4/verity.c

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -201,55 +201,76 @@ static int ext4_end_enable_verity(struct file *filp, const void *desc,
201201
struct inode *inode = file_inode(filp);
202202
const int credits = 2; /* superblock and inode for ext4_orphan_del() */
203203
handle_t *handle;
204+
struct ext4_iloc iloc;
204205
int err = 0;
205-
int err2;
206206

207-
if (desc != NULL) {
208-
/* Succeeded; write the verity descriptor. */
209-
err = ext4_write_verity_descriptor(inode, desc, desc_size,
210-
merkle_tree_size);
211-
212-
/* Write all pages before clearing VERITY_IN_PROGRESS. */
213-
if (!err)
214-
err = filemap_write_and_wait(inode->i_mapping);
215-
}
207+
/*
208+
* If an error already occurred (which fs/verity/ signals by passing
209+
* desc == NULL), then only clean-up is needed.
210+
*/
211+
if (desc == NULL)
212+
goto cleanup;
216213

217-
/* If we failed, truncate anything we wrote past i_size. */
218-
if (desc == NULL || err)
219-
ext4_truncate(inode);
214+
/* Append the verity descriptor. */
215+
err = ext4_write_verity_descriptor(inode, desc, desc_size,
216+
merkle_tree_size);
217+
if (err)
218+
goto cleanup;
220219

221220
/*
222-
* We must always clean up by clearing EXT4_STATE_VERITY_IN_PROGRESS and
223-
* deleting the inode from the orphan list, even if something failed.
224-
* If everything succeeded, we'll also set the verity bit in the same
225-
* transaction.
221+
* Write all pages (both data and verity metadata). Note that this must
222+
* happen before clearing EXT4_STATE_VERITY_IN_PROGRESS; otherwise pages
223+
* beyond i_size won't be written properly. For crash consistency, this
224+
* also must happen before the verity inode flag gets persisted.
226225
*/
226+
err = filemap_write_and_wait(inode->i_mapping);
227+
if (err)
228+
goto cleanup;
227229

228-
ext4_clear_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
230+
/*
231+
* Finally, set the verity inode flag and remove the inode from the
232+
* orphan list (in a single transaction).
233+
*/
229234

230235
handle = ext4_journal_start(inode, EXT4_HT_INODE, credits);
231236
if (IS_ERR(handle)) {
232-
ext4_orphan_del(NULL, inode);
233-
return PTR_ERR(handle);
237+
err = PTR_ERR(handle);
238+
goto cleanup;
234239
}
235240

236-
err2 = ext4_orphan_del(handle, inode);
237-
if (err2)
238-
goto out_stop;
241+
err = ext4_orphan_del(handle, inode);
242+
if (err)
243+
goto stop_and_cleanup;
239244

240-
if (desc != NULL && !err) {
241-
struct ext4_iloc iloc;
245+
err = ext4_reserve_inode_write(handle, inode, &iloc);
246+
if (err)
247+
goto stop_and_cleanup;
242248

243-
err = ext4_reserve_inode_write(handle, inode, &iloc);
244-
if (err)
245-
goto out_stop;
246-
ext4_set_inode_flag(inode, EXT4_INODE_VERITY);
247-
ext4_set_inode_flags(inode, false);
248-
err = ext4_mark_iloc_dirty(handle, inode, &iloc);
249-
}
250-
out_stop:
249+
ext4_set_inode_flag(inode, EXT4_INODE_VERITY);
250+
ext4_set_inode_flags(inode, false);
251+
err = ext4_mark_iloc_dirty(handle, inode, &iloc);
252+
if (err)
253+
goto stop_and_cleanup;
254+
255+
ext4_journal_stop(handle);
256+
257+
ext4_clear_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
258+
return 0;
259+
260+
stop_and_cleanup:
251261
ext4_journal_stop(handle);
252-
return err ?: err2;
262+
cleanup:
263+
/*
264+
* Verity failed to be enabled, so clean up by truncating any verity
265+
* metadata that was written beyond i_size (both from cache and from
266+
* disk), removing the inode from the orphan list (if it wasn't done
267+
* already), and clearing EXT4_STATE_VERITY_IN_PROGRESS.
268+
*/
269+
truncate_inode_pages(inode->i_mapping, inode->i_size);
270+
ext4_truncate(inode);
271+
ext4_orphan_del(NULL, inode);
272+
ext4_clear_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
273+
return err;
253274
}
254275

255276
static int ext4_get_verity_descriptor_location(struct inode *inode,

0 commit comments

Comments
 (0)