Add patches to fix kernel crashes on corrupted file systems
[ext4-patch-queue.git] / disallow-metadata-inodes-in-ext4_iget
blob9976e1bdd06c85e8f3a7230eff92102d656c78ec
1 ext4: add ext4_iget_normal() which is to be used for dir tree lookups
3 If there is a corrupted file system which has directory entries that
4 point at reserved, metadata inodes, prohibit them from being used by
5 treating them the same way we treat Boot Loader inodes --- that is,
6 mark them to be bad inodes.  This prohibits them from being opened,
7 deleted, or modified via chmod, chown, utimes, etc.
9 In particular, this prevents a corrupted file system which has a
10 directory entry which points at the journal inode from being deleted
11 and its blocks released, after which point Much Hilarity Ensues.
13 Reported-by: Sami Liedes <sami.liedes@iki.fi>
14 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
15 Cc: stable@vger.kernel.org
17 ---
18  fs/ext4/ext4.h  |  1 +
19  fs/ext4/inode.c | 10 ++++++++++
20  fs/ext4/namei.c |  4 ++--
21  fs/ext4/super.c |  2 +-
22  4 files changed, 14 insertions(+), 3 deletions(-)
24 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
25 index 1eb5b7b..012e89b 100644
26 --- a/fs/ext4/ext4.h
27 +++ b/fs/ext4/ext4.h
28 @@ -2109,6 +2109,7 @@ int do_journal_get_write_access(handle_t *handle,
29  #define CONVERT_INLINE_DATA     2
31  extern struct inode *ext4_iget(struct super_block *, unsigned long);
32 +extern struct inode *ext4_iget_normal(struct super_block *, unsigned long);
33  extern int  ext4_write_inode(struct inode *, struct writeback_control *);
34  extern int  ext4_setattr(struct dentry *, struct iattr *);
35  extern int  ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
36 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
37 index 59983b2..437622c 100644
38 --- a/fs/ext4/inode.c
39 +++ b/fs/ext4/inode.c
40 @@ -4104,6 +4104,16 @@ bad_inode:
41         return ERR_PTR(ret);
42  }
44 +struct inode *ext4_iget_normal(struct super_block *sb, unsigned long ino)
46 +       struct inode *ret_inode = ext4_iget(sb, ino);
48 +       if (ret_inode && !IS_ERR(ret_inode) &&
49 +           ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
50 +               make_bad_inode(ret_inode);
51 +       return ret_inode;
54  static int ext4_inode_blocks_set(handle_t *handle,
55                                 struct ext4_inode *raw_inode,
56                                 struct ext4_inode_info *ei)
57 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
58 index a2a9d40..7037ecf 100644
59 --- a/fs/ext4/namei.c
60 +++ b/fs/ext4/namei.c
61 @@ -1417,7 +1417,7 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
62                                          dentry);
63                         return ERR_PTR(-EIO);
64                 }
65 -               inode = ext4_iget(dir->i_sb, ino);
66 +               inode = ext4_iget_normal(dir->i_sb, ino);
67                 if (inode == ERR_PTR(-ESTALE)) {
68                         EXT4_ERROR_INODE(dir,
69                                          "deleted inode referenced: %u",
70 @@ -1450,7 +1450,7 @@ struct dentry *ext4_get_parent(struct dentry *child)
71                 return ERR_PTR(-EIO);
72         }
74 -       return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
75 +       return d_obtain_alias(ext4_iget_normal(child->d_inode->i_sb, ino));
76  }
78  /*
79 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
80 index 1070d6e..a0811cc 100644
81 --- a/fs/ext4/super.c
82 +++ b/fs/ext4/super.c
83 @@ -1001,7 +1001,7 @@ static struct inode *ext4_nfs_get_inode(struct super_block *sb,
84          * Currently we don't know the generation for parent directory, so
85          * a generation of 0 means "accept any"
86          */
87 -       inode = ext4_iget(sb, ino);
88 +       inode = ext4_iget_normal(sb, ino);
89         if (IS_ERR(inode))
90                 return ERR_CAST(inode);
91         if (generation && inode->i_generation != generation) {