All of lore.kernel.org
 help / color / mirror / Atom feed
From: Theodore Ts'o <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: Theodore Ts'o <tytso@mit.edu>
Subject: [PATCH 03/28] ext4: Add block validity check when truncating indirect block mapped inodes
Date: Tue,  2 Mar 2010 13:18:20 -0500	[thread overview]
Message-ID: <1267553925-6308-4-git-send-email-tytso@mit.edu> (raw)
In-Reply-To: <1267553925-6308-1-git-send-email-tytso@mit.edu>

Add checks to ext4_free_branches() to make sure a block number found
in an indirect block are valid before trying to free it.  If a bad
block number is found, stop freeing the indirect block immediately,
since the file system is corrupt and we will need to run fsck anyway.
This also avoids spamming the logs, and specifically avoids
driver-level "attempt to access beyond end of device" errors obscure
what is really going on.

If you get *really*, *really*, *really* unlucky, without this patch, a
supposed indirect block containing garbage might contain a reference
to a primary block group descriptor, in which case
ext4_free_branches() could end up zero'ing out a block group
descriptor block, and if then one of the block bitmaps for a block
group described by that bg descriptor block is not in memory, and is
read in by ext4_read_block_bitmap().  This function calls
ext4_valid_block_bitmap(), which assumes that bg_inode_table() was
validated at mount time and hasn't been modified since.  Since this
assumption is no longer valid, it's possible for the value
(ext4_inode_table(sb, desc) - group_first_block) to go negative, which
will cause ext4_find_next_zero_bit() to trigger a kernel GPF.

Addresses-Google-Bug: #2220436

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
---
 fs/ext4/ext4.h    |    1 +
 fs/ext4/inode.c   |   39 ++++++++++++++++++++++++++++++---------
 fs/ext4/mballoc.c |    7 ++++---
 3 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 602d5ad..307ecd1 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -377,6 +377,7 @@ struct ext4_new_group_data {
  */
 #define EXT4_FREE_BLOCKS_METADATA	0x0001
 #define EXT4_FREE_BLOCKS_FORGET		0x0002
+#define EXT4_FREE_BLOCKS_VALIDATED	0x0004
 
 /*
  * ioctl commands
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 2059c34..3e8afd9 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -4130,18 +4130,27 @@ no_top:
  * We release `count' blocks on disk, but (last - first) may be greater
  * than `count' because there can be holes in there.
  */
-static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
-			      struct buffer_head *bh,
-			      ext4_fsblk_t block_to_free,
-			      unsigned long count, __le32 *first,
-			      __le32 *last)
+static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
+			     struct buffer_head *bh,
+			     ext4_fsblk_t block_to_free,
+			     unsigned long count, __le32 *first,
+			     __le32 *last)
 {
 	__le32 *p;
-	int	flags = EXT4_FREE_BLOCKS_FORGET;
+	int	flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED;
 
 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
 		flags |= EXT4_FREE_BLOCKS_METADATA;
 
+	if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
+				   count)) {
+		ext4_error(inode->i_sb, __func__, "inode #%lu: "
+			   "attempt to clear blocks %llu len %lu, invalid",
+			   inode->i_ino, (unsigned long long) block_to_free,
+			   count);
+		return 1;
+	}
+
 	if (try_to_extend_transaction(handle, inode)) {
 		if (bh) {
 			BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
@@ -4160,6 +4169,7 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
 		*p = 0;
 
 	ext4_free_blocks(handle, inode, 0, block_to_free, count, flags);
+	return 0;
 }
 
 /**
@@ -4215,9 +4225,10 @@ static void ext4_free_data(handle_t *handle, struct inode *inode,
 			} else if (nr == block_to_free + count) {
 				count++;
 			} else {
-				ext4_clear_blocks(handle, inode, this_bh,
-						  block_to_free,
-						  count, block_to_free_p, p);
+				if (ext4_clear_blocks(handle, inode, this_bh,
+						      block_to_free, count,
+						      block_to_free_p, p))
+					break;
 				block_to_free = nr;
 				block_to_free_p = p;
 				count = 1;
@@ -4281,6 +4292,16 @@ static void ext4_free_branches(handle_t *handle, struct inode *inode,
 			if (!nr)
 				continue;		/* A hole */
 
+			if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
+						   nr, 1)) {
+				ext4_error(inode->i_sb, __func__,
+					   "indirect mapped block in inode "
+					   "#%lu invalid (level %d, blk #%lu)",
+					   inode->i_ino, depth,
+					   (unsigned long) nr);
+				break;
+			}
+
 			/* Go read the buffer for the next level down */
 			bh = sb_bread(inode->i_sb, nr);
 
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index d34afad..d129c10 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -4476,10 +4476,11 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
 
 	sbi = EXT4_SB(sb);
 	es = EXT4_SB(sb)->s_es;
-	if (!ext4_data_block_valid(sbi, block, count)) {
+	if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
+	    !ext4_data_block_valid(sbi, block, count)) {
 		ext4_error(sb, __func__,
-			    "Freeing blocks not in datazone - "
-			    "block = %llu, count = %lu", block, count);
+			   "Freeing blocks not in datazone - "
+			   "block = %llu, count = %lu", block, count);
 		goto error_return;
 	}
 
-- 
1.6.6.1.1.g974db.dirty


  parent reply	other threads:[~2010-03-02 18:18 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-02 18:18 [PATCH 00/28] Ext4 patch queue for the 2.6.34 merge window Theodore Ts'o
2010-03-02 18:18 ` [PATCH 01/28] ext4: fix async i/o writes beyond 4GB to a sparse file Theodore Ts'o
2010-03-02 18:18 ` [PATCH 02/28] ext4: Fix optional-arg mount options Theodore Ts'o
2010-03-02 18:18 ` Theodore Ts'o [this message]
2010-03-02 18:18 ` [PATCH 04/28] ext4: Add new tracepoint for jbd2_cleanup_journal_tail Theodore Ts'o
2010-03-02 18:18 ` [PATCH 05/28] ext4: Add new tracepoints to debug delayed allocation space functions Theodore Ts'o
2010-03-02 18:18 ` [PATCH 06/28] ext4: Use slab allocator for sub-page sized allocations Theodore Ts'o
2010-03-02 18:18 ` [PATCH 07/28] ext4: Use bitops to read/modify EXT4_I(inode)->i_state Theodore Ts'o
2010-03-02 18:18 ` [PATCH 08/28] ext4: Reserve INCOMPAT_EA_INODE and INCOMPAT_DIRDATA feature codepoints Theodore Ts'o
2010-03-02 18:18 ` [PATCH 09/28] ext4: move __func__ into a macro for ext4_warning, ext4_error Theodore Ts'o
2010-03-02 18:18 ` [PATCH 10/28] ext4: add missing error checking to ext4_expand_extra_isize_ea() Theodore Ts'o
2010-03-02 18:18 ` [PATCH 11/28] ext4: correctly calculate number of blocks for fiemap Theodore Ts'o
2010-03-03  7:47   ` Akira Fujita
2010-03-03  8:34     ` Leonard Michlmayr
2010-03-03 17:52     ` tytso
2010-03-04  5:40       ` Akira Fujita
2010-03-04 21:44         ` Leonard Michlmayr
2010-03-04 22:28           ` [incomplete PATCH] ext4: avoid overflow in fiemap Leonard Michlmayr
2010-03-07  3:38             ` tytso
2010-03-04 23:38           ` [PATCH 11/28] ext4: correctly calculate number of blocks for fiemap Eric Sandeen
2010-03-05 16:46             ` Leonard Michlmayr
2010-03-04 22:08         ` tytso
2010-03-04 23:47           ` Eric Sandeen
2010-03-02 18:18 ` [PATCH 12/28] jbd2: delay discarding buffers in journal_unmap_buffer Theodore Ts'o
2010-03-02 18:18 ` [PATCH 13/28] ext4: Fix BUG_ON at fs/buffer.c:652 in no journal mode Theodore Ts'o
2010-03-02 18:18 ` [PATCH 14/28] ext4: Add flag to files with blocks intentionally past EOF Theodore Ts'o
2010-03-02 18:18 ` [PATCH 15/28] ext4: mount flags manipulation cleanup Theodore Ts'o
2010-03-02 18:18 ` [PATCH 16/28] ext4: trivial quota cleanup Theodore Ts'o
2010-03-02 18:18 ` [PATCH 17/28] jbd2: clean up an assertion in jbd2_journal_commit_transaction() Theodore Ts'o
2010-03-02 18:18 ` [PATCH 18/28] ext4: Fix fencepost error in chosing choosing group vs file preallocation Theodore Ts'o
2010-03-02 18:18 ` [PATCH 19/28] ext4: deprecate obsoleted mount options Theodore Ts'o
2010-03-02 18:18 ` [PATCH 20/28] ext4: fix error handling in migrate Theodore Ts'o
2010-03-02 18:18 ` [PATCH 21/28] ext4: explicitly remove inode from orphan list after failed direct io Theodore Ts'o
2010-03-02 18:18 ` [PATCH 22/28] ext4: Handle non empty on-disk orphan link Theodore Ts'o
2010-03-02 18:18 ` [PATCH 23/28] ext4: make "offset" consistent in ext4_check_dir_entry() Theodore Ts'o
2010-03-02 18:18 ` [PATCH 24/28] ext4: mechanical change on dio get_block code in prepare for it to be used by buffer write Theodore Ts'o
2010-03-02 18:18 ` [PATCH 25/28] ext4: use ext4_get_block_write in " Theodore Ts'o
2010-03-02 18:18 ` [PATCH 26/28] ext4: Use direct_IO_no_locking in ext4 dio read Theodore Ts'o
2010-03-02 18:18 ` [PATCH 27/28] ext4: Convert BUG_ON checks to use ext4_error() instead Theodore Ts'o
2010-03-06 13:03   ` Aneesh Kumar K. V
2010-03-07  2:45     ` tytso
2010-03-07  5:51       ` Eric Sandeen
2010-03-07 17:36         ` Andreas Dilger
2010-03-08 17:46       ` Frank Mayhar
2010-03-02 18:18 ` [PATCH 28/28] ext4: Fix ext4_quota_write cross block boundary behaviour Theodore Ts'o
2010-03-02 18:26 ` [PATCH 00/28] Ext4 patch queue for the 2.6.34 merge window tytso

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=1267553925-6308-4-git-send-email-tytso@mit.edu \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.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 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.