ext4: fix error code return to user-space in ext4_get_branch()
authorLuís Henriques <lhenriques@suse.de>
Wed, 9 Nov 2022 18:14:45 +0000 (18:14 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 18 Jan 2023 08:26:39 +0000 (09:26 +0100)
commit 26d75a16af285a70863ba6a81f85d81e7e65da50 upstream.

If a block is out of range in ext4_get_branch(), -ENOMEM will be returned
to user-space.  Obviously, this error code isn't really useful.  This
patch fixes it by making sure the right error code (-EFSCORRUPTED) is
propagated to user-space.  EUCLEAN is more informative than ENOMEM.

Signed-off-by: Luís Henriques <lhenriques@suse.de>
Link: https://lore.kernel.org/r/20221109181445.17843-1-lhenriques@suse.de
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: stable@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ext4/indirect.c

index a5442528a60d201785658ea833059e6bb4b23a3c..0cc0d22c08569d43c3e69ce5b4e552aee1af972e 100644 (file)
@@ -148,6 +148,7 @@ static Indirect *ext4_get_branch(struct inode *inode, int depth,
        struct super_block *sb = inode->i_sb;
        Indirect *p = chain;
        struct buffer_head *bh;
+       unsigned int key;
        int ret = -EIO;
 
        *err = 0;
@@ -156,7 +157,13 @@ static Indirect *ext4_get_branch(struct inode *inode, int depth,
        if (!p->key)
                goto no_block;
        while (--depth) {
-               bh = sb_getblk(sb, le32_to_cpu(p->key));
+               key = le32_to_cpu(p->key);
+               if (key > ext4_blocks_count(EXT4_SB(sb)->s_es)) {
+                       /* the block was out of range */
+                       ret = -EFSCORRUPTED;
+                       goto failure;
+               }
+               bh = sb_getblk(sb, key);
                if (unlikely(!bh)) {
                        ret = -ENOMEM;
                        goto failure;