* [PATCH 1/4] ext4: ext4_search_dir should return a proper error
2024-08-21 15:23 [PATCH 0/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem Thadeu Lima de Souza Cascardo
@ 2024-08-21 15:23 ` Thadeu Lima de Souza Cascardo
2024-08-21 15:23 ` [PATCH 2/4] ext4: return error on ext4_find_inline_entry Thadeu Lima de Souza Cascardo
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2024-08-21 15:23 UTC (permalink / raw)
To: linux-ext4
Cc: Theodore Ts'o, linux-kernel, Tao Ma, Andreas Dilger,
kernel-dev, Thadeu Lima de Souza Cascardo
ext4_search_dir currently returns -1 in case of a failure, while it returns
0 when the name is not found. In such failure cases, it should return an
error code instead.
This becomes even more important when ext4_find_inline_entry returns an
error code as well in the next commit.
-EFSCORRUPTED seems appropriate as such error code as these failures would
be caused by unexpected record lengths and is in line with other instances
of ext4_check_dir_entry failures.
In the case of ext4_dx_find_entry, the current use of ERR_BAD_DX_DIR was
left as is to reduce the risk of regressions.
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
fs/ext4/namei.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 6a95713f9193..8af437ac3051 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1482,7 +1482,7 @@ static bool ext4_match(struct inode *parent,
}
/*
- * Returns 0 if not found, -1 on failure, and 1 on success
+ * Returns 0 if not found, -EFSCORRUPTED on failure, and 1 on success
*/
int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
struct inode *dir, struct ext4_filename *fname,
@@ -1503,7 +1503,7 @@ int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
* a full check */
if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
buf_size, offset))
- return -1;
+ return -EFSCORRUPTED;
*res_dir = de;
return 1;
}
@@ -1511,7 +1511,7 @@ int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
de_len = ext4_rec_len_from_disk(de->rec_len,
dir->i_sb->s_blocksize);
if (de_len <= 0)
- return -1;
+ return -EFSCORRUPTED;
offset += de_len;
de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
}
@@ -1663,8 +1663,10 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
goto cleanup_and_exit;
} else {
brelse(bh);
- if (i < 0)
+ if (i < 0) {
+ ret = ERR_PTR(i);
goto cleanup_and_exit;
+ }
}
next:
if (++block >= nblocks)
@@ -1758,7 +1760,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
if (retval == 1)
goto success;
brelse(bh);
- if (retval == -1) {
+ if (retval < 0) {
bh = ERR_PTR(ERR_BAD_DX_DIR);
goto errout;
}
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 2/4] ext4: return error on ext4_find_inline_entry
2024-08-21 15:23 [PATCH 0/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem Thadeu Lima de Souza Cascardo
2024-08-21 15:23 ` [PATCH 1/4] ext4: ext4_search_dir should return a proper error Thadeu Lima de Souza Cascardo
@ 2024-08-21 15:23 ` Thadeu Lima de Souza Cascardo
2024-08-21 15:23 ` [PATCH 3/4] ext4: explicitly exit when ext4_find_inline_entry returns an error Thadeu Lima de Souza Cascardo
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2024-08-21 15:23 UTC (permalink / raw)
To: linux-ext4
Cc: Theodore Ts'o, linux-kernel, Tao Ma, Andreas Dilger,
kernel-dev, Thadeu Lima de Souza Cascardo
In case of errors when reading an inode from disk or traversing inline
directory entries, return an error-encoded ERR_PTR instead of returning
NULL. ext4_find_inline_entry only caller, __ext4_find_entry already returns
such encoded errors.
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
fs/ext4/inline.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index e7a09a99837b..7b98b1bf1dc9 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1669,8 +1669,9 @@ struct buffer_head *ext4_find_inline_entry(struct inode *dir,
void *inline_start;
int inline_size;
- if (ext4_get_inode_loc(dir, &iloc))
- return NULL;
+ ret = ext4_get_inode_loc(dir, &iloc);
+ if (ret)
+ return ERR_PTR(ret);
down_read(&EXT4_I(dir)->xattr_sem);
if (!ext4_has_inline_data(dir)) {
@@ -1701,7 +1702,10 @@ struct buffer_head *ext4_find_inline_entry(struct inode *dir,
out:
brelse(iloc.bh);
- iloc.bh = NULL;
+ if (ret < 0)
+ iloc.bh = ERR_PTR(ret);
+ else
+ iloc.bh = NULL;
out_find:
up_read(&EXT4_I(dir)->xattr_sem);
return iloc.bh;
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 3/4] ext4: explicitly exit when ext4_find_inline_entry returns an error
2024-08-21 15:23 [PATCH 0/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem Thadeu Lima de Souza Cascardo
2024-08-21 15:23 ` [PATCH 1/4] ext4: ext4_search_dir should return a proper error Thadeu Lima de Souza Cascardo
2024-08-21 15:23 ` [PATCH 2/4] ext4: return error on ext4_find_inline_entry Thadeu Lima de Souza Cascardo
@ 2024-08-21 15:23 ` Thadeu Lima de Souza Cascardo
2024-08-21 15:23 ` [PATCH 4/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem Thadeu Lima de Souza Cascardo
2024-09-05 14:53 ` [PATCH 0/4] " Theodore Ts'o
4 siblings, 0 replies; 6+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2024-08-21 15:23 UTC (permalink / raw)
To: linux-ext4
Cc: Theodore Ts'o, linux-kernel, Tao Ma, Andreas Dilger,
kernel-dev, Thadeu Lima de Souza Cascardo
__ext4_find_entry currently ignores the return of ext4_find_inline_entry,
except for returning the bh or NULL when has_inline_data is 1.
Even though has_inline_data is set to 1 before calling
ext4_find_inline_entry and would only be set to 0 when that function
returns NULL, check for an encoded error return explicitly in order to
exit.
That makes the code more readable, not requiring that one assumes the cases
when has_inline_data is 1.
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
fs/ext4/namei.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 8af437ac3051..14163b32eddb 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1574,7 +1574,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
&has_inline_data);
if (inlined)
*inlined = has_inline_data;
- if (has_inline_data)
+ if (has_inline_data || IS_ERR(ret))
goto cleanup_and_exit;
}
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 4/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem
2024-08-21 15:23 [PATCH 0/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem Thadeu Lima de Souza Cascardo
` (2 preceding siblings ...)
2024-08-21 15:23 ` [PATCH 3/4] ext4: explicitly exit when ext4_find_inline_entry returns an error Thadeu Lima de Souza Cascardo
@ 2024-08-21 15:23 ` Thadeu Lima de Souza Cascardo
2024-09-05 14:53 ` [PATCH 0/4] " Theodore Ts'o
4 siblings, 0 replies; 6+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2024-08-21 15:23 UTC (permalink / raw)
To: linux-ext4
Cc: Theodore Ts'o, linux-kernel, Tao Ma, Andreas Dilger,
kernel-dev, Thadeu Lima de Souza Cascardo,
syzbot+0c2508114d912a54ee79
When looking up for an entry in an inlined directory, if e_value_offs is
changed underneath the filesystem by some change in the block device, it
will lead to an out-of-bounds access that KASAN detects as an UAF.
EXT4-fs (loop0): mounted filesystem 00000000-0000-0000-0000-000000000000 r/w without journal. Quota mode: none.
loop0: detected capacity change from 2048 to 2047
==================================================================
BUG: KASAN: use-after-free in ext4_search_dir+0xf2/0x1c0 fs/ext4/namei.c:1500
Read of size 1 at addr ffff88803e91130f by task syz-executor269/5103
CPU: 0 UID: 0 PID: 5103 Comm: syz-executor269 Not tainted 6.11.0-rc4-syzkaller #0
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2~bpo12+1 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:93 [inline]
dump_stack_lvl+0x241/0x360 lib/dump_stack.c:119
print_address_description mm/kasan/report.c:377 [inline]
print_report+0x169/0x550 mm/kasan/report.c:488
kasan_report+0x143/0x180 mm/kasan/report.c:601
ext4_search_dir+0xf2/0x1c0 fs/ext4/namei.c:1500
ext4_find_inline_entry+0x4be/0x5e0 fs/ext4/inline.c:1697
__ext4_find_entry+0x2b4/0x1b30 fs/ext4/namei.c:1573
ext4_lookup_entry fs/ext4/namei.c:1727 [inline]
ext4_lookup+0x15f/0x750 fs/ext4/namei.c:1795
lookup_one_qstr_excl+0x11f/0x260 fs/namei.c:1633
filename_create+0x297/0x540 fs/namei.c:3980
do_symlinkat+0xf9/0x3a0 fs/namei.c:4587
__do_sys_symlinkat fs/namei.c:4610 [inline]
__se_sys_symlinkat fs/namei.c:4607 [inline]
__x64_sys_symlinkat+0x95/0xb0 fs/namei.c:4607
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f3e73ced469
Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 21 18 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fff4d40c258 EFLAGS: 00000246 ORIG_RAX: 000000000000010a
RAX: ffffffffffffffda RBX: 0032656c69662f2e RCX: 00007f3e73ced469
RDX: 0000000020000200 RSI: 00000000ffffff9c RDI: 00000000200001c0
RBP: 0000000000000000 R08: 00007fff4d40c290 R09: 00007fff4d40c290
R10: 0023706f6f6c2f76 R11: 0000000000000246 R12: 00007fff4d40c27c
R13: 0000000000000003 R14: 431bde82d7b634db R15: 00007fff4d40c2b0
</TASK>
Calling ext4_xattr_ibody_find right after reading the inode with
ext4_get_inode_loc will lead to a check of the validity of the xattrs,
avoiding this problem.
Reported-by: syzbot+0c2508114d912a54ee79@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=0c2508114d912a54ee79
Fixes: e8e948e7802a ("ext4: let ext4_find_entry handle inline data")
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
fs/ext4/inline.c | 31 +++++++++++++++++++++----------
1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index 7b98b1bf1dc9..44a5f6df59ec 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1664,25 +1664,36 @@ struct buffer_head *ext4_find_inline_entry(struct inode *dir,
struct ext4_dir_entry_2 **res_dir,
int *has_inline_data)
{
+ struct ext4_xattr_ibody_find is = {
+ .s = { .not_found = -ENODATA, },
+ };
+ struct ext4_xattr_info i = {
+ .name_index = EXT4_XATTR_INDEX_SYSTEM,
+ .name = EXT4_XATTR_SYSTEM_DATA,
+ };
int ret;
- struct ext4_iloc iloc;
void *inline_start;
int inline_size;
- ret = ext4_get_inode_loc(dir, &iloc);
+ ret = ext4_get_inode_loc(dir, &is.iloc);
if (ret)
return ERR_PTR(ret);
down_read(&EXT4_I(dir)->xattr_sem);
+
+ ret = ext4_xattr_ibody_find(dir, &i, &is);
+ if (ret)
+ goto out;
+
if (!ext4_has_inline_data(dir)) {
*has_inline_data = 0;
goto out;
}
- inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
+ inline_start = (void *)ext4_raw_inode(&is.iloc)->i_block +
EXT4_INLINE_DOTDOT_SIZE;
inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
- ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
+ ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
dir, fname, 0, res_dir);
if (ret == 1)
goto out_find;
@@ -1692,23 +1703,23 @@ struct buffer_head *ext4_find_inline_entry(struct inode *dir,
if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
goto out;
- inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
+ inline_start = ext4_get_inline_xattr_pos(dir, &is.iloc);
inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
- ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
+ ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
dir, fname, 0, res_dir);
if (ret == 1)
goto out_find;
out:
- brelse(iloc.bh);
+ brelse(is.iloc.bh);
if (ret < 0)
- iloc.bh = ERR_PTR(ret);
+ is.iloc.bh = ERR_PTR(ret);
else
- iloc.bh = NULL;
+ is.iloc.bh = NULL;
out_find:
up_read(&EXT4_I(dir)->xattr_sem);
- return iloc.bh;
+ return is.iloc.bh;
}
int ext4_delete_inline_entry(handle_t *handle,
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 0/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem
2024-08-21 15:23 [PATCH 0/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem Thadeu Lima de Souza Cascardo
` (3 preceding siblings ...)
2024-08-21 15:23 ` [PATCH 4/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem Thadeu Lima de Souza Cascardo
@ 2024-09-05 14:53 ` Theodore Ts'o
4 siblings, 0 replies; 6+ messages in thread
From: Theodore Ts'o @ 2024-09-05 14:53 UTC (permalink / raw)
To: linux-ext4, Thadeu Lima de Souza Cascardo
Cc: Theodore Ts'o, linux-kernel, Tao Ma, Andreas Dilger, kernel-dev
On Wed, 21 Aug 2024 12:23:20 -0300, Thadeu Lima de Souza Cascardo wrote:
> This patchset changes some of the error paths involving dir entries lookups
> and recheck that xattrs are valid after an inode is potentially reread from
> disk.
>
> Thadeu Lima de Souza Cascardo (4):
> ext4: ext4_search_dir should return a proper error
> ext4: return error on ext4_find_inline_entry
> ext4: explicitly exit when ext4_find_inline_entry returns an error
> ext4: avoid OOB when system.data xattr changes underneath the
> filesystem
>
> [...]
Applied, thanks!
[1/4] ext4: ext4_search_dir should return a proper error
commit: cd69f8f9de280e331c9e6ff689ced0a688a9ce8f
[2/4] ext4: return error on ext4_find_inline_entry
commit: 4d231b91a944f3cab355fce65af5871fb5d7735b
[3/4] ext4: explicitly exit when ext4_find_inline_entry returns an error
commit: 51e14e78b5fb3e6f839393cd2d34386ee7b69af3
[4/4] ext4: avoid OOB when system.data xattr changes underneath the filesystem
commit: c6b72f5d82b1017bad80f9ebf502832fc321d796
Best regards,
--
Theodore Ts'o <tytso@mit.edu>
^ permalink raw reply [flat|nested] 6+ messages in thread