[2/2] ext4: add detection of i_nlink
diff mbox

Message ID 1484732769-31670-2-git-send-email-yi.zhang@huawei.com
State New
Headers show

Commit Message

zhangyi (F) Jan. 18, 2017, 9:46 a.m. UTC
Because of the disk and hardware issue, the inode->i_nlink of ext4
becomes zero abnormally but the dentry is still positive, it will
cause memory corruption after the following process:

 1) Due to the inode->i_nlink is 0, this inode will be added into
the orhpan list,
 2) ext4_rename() cover this inode, and drop_nlink() will underflow
the inode->i_nlink to 0xFFFFFFFF,
 3) iput() add this inode to LRU,
 4) evict() will call destroy_inode() to destroy this inode but
skip removing it from the orphan list,
 5) after this, the inode's memory address space will be used by
other module, when the ext4 filesystem change the orphan list, it will
trample other module's data and then may cause oops.

This patch detect inode->i_nlink in i_op->valitate, if it becomes zero
abnormally, we call ext4_error and return -EFSCORRUPTED.

Signed-off-by: yi zhang <yi.zhang@huawei.com>
---
 fs/ext4/ext4.h    |  1 +
 fs/ext4/file.c    |  1 +
 fs/ext4/inode.c   | 10 ++++++++++
 fs/ext4/namei.c   |  2 ++
 fs/ext4/symlink.c |  3 +++
 5 files changed, 17 insertions(+)

Comments

kernel test robot Jan. 18, 2017, 6:14 p.m. UTC | #1
Hi yi,

[auto build test WARNING on ext4/dev]
[also build test WARNING on v4.10-rc4 next-20170118]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/yi-zhang/vfs-add-detection-of-inode-validation/20170119-013142
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev
config: i386-randconfig-i1-201703 (attached as .config)
compiler: gcc-4.8 (Debian 4.8.4-1) 4.8.4
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All warnings (new ones prefixed by >>):

   fs/ext4/inode.c: In function 'ext4_validate':
>> fs/ext4/inode.c:5388:3: warning: format '%lu' expects argument of type 'long unsigned int', but argument 6 has type 'unsigned int' [-Wformat=]
      EXT4_ERROR_INODE(inode, "bad nlink value: %lu", inode->i_nlink);
      ^

vim +5388 fs/ext4/inode.c

  5372		 * allocation is done, we will have i_blocks inconsistent with
  5373		 * on-disk file blocks.
  5374		 * We always keep i_blocks updated together with real
  5375		 * allocation. But to not confuse with user, stat
  5376		 * will return the blocks that include the delayed allocation
  5377		 * blocks for this file.
  5378		 */
  5379		delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  5380					   EXT4_I(inode)->i_reserved_data_blocks);
  5381		stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
  5382		return 0;
  5383	}
  5384	
  5385	int ext4_validate(struct inode *inode)
  5386	{
  5387		if (inode->i_nlink == 0) {
> 5388			EXT4_ERROR_INODE(inode, "bad nlink value: %lu", inode->i_nlink);
  5389			return -EFSCORRUPTED;
  5390		}
  5391	
  5392		return 0;
  5393	}
  5394	
  5395	static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
  5396					   int pextents)

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kernel test robot Jan. 18, 2017, 7:17 p.m. UTC | #2
Hi yi,

[auto build test WARNING on ext4/dev]
[also build test WARNING on v4.10-rc4 next-20170118]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/yi-zhang/vfs-add-detection-of-inode-validation/20170119-013142
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev
config: x86_64-randconfig-s1-01190013 (attached as .config)
compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   fs/ext4/inode.c: In function 'ext4_validate':
>> fs/ext4/inode.c:5388: warning: format '%lu' expects type 'long unsigned int', but argument 6 has type 'unsigned int'
   fs/ext4/inode.o: warning: objtool: ext4_inode_journal_mode()+0xc0: function has unreachable instruction

vim +5388 fs/ext4/inode.c

  5372		 * allocation is done, we will have i_blocks inconsistent with
  5373		 * on-disk file blocks.
  5374		 * We always keep i_blocks updated together with real
  5375		 * allocation. But to not confuse with user, stat
  5376		 * will return the blocks that include the delayed allocation
  5377		 * blocks for this file.
  5378		 */
  5379		delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  5380					   EXT4_I(inode)->i_reserved_data_blocks);
  5381		stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
  5382		return 0;
  5383	}
  5384	
  5385	int ext4_validate(struct inode *inode)
  5386	{
  5387		if (inode->i_nlink == 0) {
> 5388			EXT4_ERROR_INODE(inode, "bad nlink value: %lu", inode->i_nlink);
  5389			return -EFSCORRUPTED;
  5390		}
  5391	
  5392		return 0;
  5393	}
  5394	
  5395	static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
  5396					   int pextents)

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

Patch
diff mbox

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 2163c1e..451f3b1f 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2472,6 +2472,7 @@  extern int  ext4_write_inode(struct inode *, struct writeback_control *);
 extern int  ext4_setattr(struct dentry *, struct iattr *);
 extern int  ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
 				struct kstat *stat);
+extern int ext4_validate(struct inode *inode);
 extern void ext4_evict_inode(struct inode *);
 extern void ext4_clear_inode(struct inode *);
 extern int  ext4_sync_inode(handle_t *, struct inode *);
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index d663d3d..8c6c702 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -762,5 +762,6 @@  const struct inode_operations ext4_file_inode_operations = {
 	.get_acl	= ext4_get_acl,
 	.set_acl	= ext4_set_acl,
 	.fiemap		= ext4_fiemap,
+	.validate	= ext4_validate,
 };
 
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 88d57af..6f5c393 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5382,6 +5382,16 @@  int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
 	return 0;
 }
 
+int ext4_validate(struct inode *inode)
+{
+	if (inode->i_nlink == 0) {
+		EXT4_ERROR_INODE(inode, "bad nlink value: %lu", inode->i_nlink);
+		return -EFSCORRUPTED;
+	}
+
+	return 0;
+}
+
 static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
 				   int pextents)
 {
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index eadba91..0396fe2 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -3888,6 +3888,7 @@  const struct inode_operations ext4_dir_inode_operations = {
 	.get_acl	= ext4_get_acl,
 	.set_acl	= ext4_set_acl,
 	.fiemap         = ext4_fiemap,
+	.validate	= ext4_validate,
 };
 
 const struct inode_operations ext4_special_inode_operations = {
@@ -3895,4 +3896,5 @@  const struct inode_operations ext4_special_inode_operations = {
 	.listxattr	= ext4_listxattr,
 	.get_acl	= ext4_get_acl,
 	.set_acl	= ext4_set_acl,
+	.validate	= ext4_validate,
 };
diff --git a/fs/ext4/symlink.c b/fs/ext4/symlink.c
index 73b184d..92377d0 100644
--- a/fs/ext4/symlink.c
+++ b/fs/ext4/symlink.c
@@ -86,16 +86,19 @@  const struct inode_operations ext4_encrypted_symlink_inode_operations = {
 	.get_link	= ext4_encrypted_get_link,
 	.setattr	= ext4_setattr,
 	.listxattr	= ext4_listxattr,
+	.validate	= ext4_validate,
 };
 
 const struct inode_operations ext4_symlink_inode_operations = {
 	.get_link	= page_get_link,
 	.setattr	= ext4_setattr,
 	.listxattr	= ext4_listxattr,
+	.validate	= ext4_validate,
 };
 
 const struct inode_operations ext4_fast_symlink_inode_operations = {
 	.get_link	= simple_get_link,
 	.setattr	= ext4_setattr,
 	.listxattr	= ext4_listxattr,
+	.validate	= ext4_validate,
 };