All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thiemo Nagel <thiemo.nagel@ph.tum.de>
To: Theodore Ts'o <tytso@mit.edu>
Cc: Ext4 Developers List <linux-ext4@vger.kernel.org>,
	"Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
Subject: [PATCH v2] ext4: add checks of block references for non-extent inodes
Date: Fri, 20 Mar 2009 15:15:56 +0100	[thread overview]
Message-ID: <49C3A51C.1030201@ph.tum.de> (raw)
In-Reply-To: <49B94396.2020800@ph.tum.de>

[-- Attachment #1: Type: text/plain, Size: 993 bytes --]

Dear Ted,

I'm sending an improved patch as I've come to the conclusion that the 
previous patch is too lenient in two ways:
* off-by-one in the check of the upper block limit
* it shouldn't stop when encountering a reference to block number zero 
because, if I'm not mistaken, references behind it still might be 
accessed in sparse files / when seeking behind the end of a file.

On the other hand, I decided to drop the check against 
s_first_data_block at the low end to improve performance, since the 
purpose of the patch is to prevent access to blocks outside the 
filesystem, and not to do the best-possible consistency check against 
indirect blocks, which probably is better done in fsck.

Anyways, in case you would be interested in having more checks here (eg. 
as a compile-time option), I have available a more sophisticated patch 
which also checks for non-zero block references behind the end of the file.

Kind regards,

Signed-off-by: Thiemo Nagel <thiemo.nagel@ph.tum.de>



[-- Attachment #2: add-blockref-checks.patch2 --]
[-- Type: text/plain, Size: 1670 bytes --]

--- linux-2.6.29-rc7/fs/ext4/inode.c.orig	2009-03-20 11:35:45.000000000 +0100
+++ linux-2.6.29-rc7/fs/ext4/inode.c	2009-03-20 13:48:25.000000000 +0100
@@ -371,6 +371,34 @@
 	return n;
 }
 
+static int __ext4_check_blockref(const char *function, struct inode *inode,
+				 unsigned int *p, unsigned int max) {
+
+	unsigned int maxblocks = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es);
+	unsigned int *bref = p;
+	while (bref < p+max) {
+		if (unlikely(*bref >= maxblocks)) {
+			ext4_error(inode->i_sb, function,
+				   "block reference %u >= max (%u) "
+				   "in inode #%lu, offset=%u",
+				   *bref, maxblocks,
+				   inode->i_ino, bref-p);
+ 			return -EIO;
+ 		}
+		bref++;
+ 	}
+ 	return 0;
+}
+
+
+#define ext4_check_indirect_blockref(inode, bh)                         \
+        __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data,  \
+			      EXT4_ADDR_PER_BLOCK((inode)->i_sb))
+
+#define ext4_check_inode_blockref(inode)                                \
+        __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data,   \
+			      EXT4_NDIR_BLOCKS)
+
 /**
  *	ext4_get_branch - read the chain of indirect blocks leading to data
  *	@inode: inode in question
@@ -418,6 +446,9 @@
 		bh = sb_bread(sb, le32_to_cpu(p->key));
 		if (!bh)
 			goto failure;
+		if (ext4_check_indirect_blockref(inode, bh))
+			goto failure;
+                  
 		add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
 		/* Reader: end */
 		if (!p->key)
@@ -4302,11 +4333,13 @@
 	if (ei->i_flags & EXT4_EXTENTS_FL) {
 		/* Validate extent which is part of inode */
 		ret = ext4_ext_check_inode(inode);
-		if (ret) {
-			brelse(bh);
-			goto bad_inode;
-		}

  parent reply	other threads:[~2009-03-20 14:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-12 17:17 [PATCH] ext4: add checks of block references for non-extent inodes Thiemo Nagel
2009-03-12 17:20 ` [PATCH] ext4: check block references only when read from disk Thiemo Nagel
2009-03-12 17:22   ` Thiemo Nagel
2009-03-20 14:15 ` Thiemo Nagel [this message]
2009-03-27 21:05   ` [PATCH v2] ext4: add checks of block references for non-extent inodes Theodore Tso
2009-03-28  1:10   ` Theodore Tso
2009-03-30 10:43   ` Aneesh Kumar K.V
2009-03-31  8:41   ` Thiemo Nagel
2009-03-31 12:37     ` Theodore Tso
2009-03-31 12:50       ` Thiemo Nagel

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=49C3A51C.1030201@ph.tum.de \
    --to=thiemo.nagel@ph.tum.de \
    --cc=aneesh.kumar@linux.vnet.ibm.com \
    --cc=linux-ext4@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 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.