ext4: add bounds check in ext4_xattr_ibody_get() to prevent out-of-bounds access
Subsystems (2)
PATCH 1/1 ext4: add bounds check in ext4_xattr_ibody_get() to prevent out-of-bounds access
When mounting a corrupted ext4 filesystem, the inode's i_extra_isize can be set to a value that leaves insufficient space in the inode for the inline xattr header and entries. While ext4_iget() validates that i_extra_isize fits within the inode size, it does not account for the additional sizeof(ext4_xattr_ibody_header) needed by IHDR/IFIRST.
This results in IFIRST(header) pointing at or beyond ITAIL(raw_inode), leaving no room for even the 4-byte terminator entry. When xattr_find_entry() is subsequently called, IS_LAST_ENTRY() reads 4 bytes from this out-of-bounds pointer, triggering a use-after-free.
For example, with EXT4_INODE_SIZE=256 and i_extra_isize=124:
- ext4_iget() check: 128 + 124 = 252 <= 256, passes
- IFIRST = offset 252 + 4 (xattr header) = 256
- ITAIL = 256
- IS_LAST_ENTRY() reads 4 bytes at offset 256, past the inode buffer
Fix this by validating in ext4_xattr_ibody_get() that there is enough space between IFIRST(header) and ITAIL for at least a 4-byte read before calling xattr_find_entry(). Return -EFSCORRUPTED if the inline xattr region is too small.
--- fs/ext4/xattr.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/ext4/xattr.c +7 -0
@@ -652,6 +652,13 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, header =IHDR (inode, raw_inode); end =ITAIL (inode, raw_inode); entry =IFIRST (header); + +if ((void *)entry +sizeof (__u32) > end) { +EXT4_ERROR_INODE (inode,"inline xattr region overflow" ); + error = -EFSCORRUPTED ; + goto cleanup; + } + error =xattr_find_entry (inode, &entry, end, name_index, name,0 );if (error) goto cleanup;
Signature · 1 line
2.34.1
Quoted 50 lines · Deepanshu Kartikey
When mounting a corrupted ext4 filesystem, the inode's
i_extra_isizecan be set to a value that leaves insufficient space in the inode for the inline xattr header and entries. Whileext4_iget()validates thati_extra_isizefits within the inode size, it does not account for the additionalsizeof(ext4_xattr_ibody_header)needed by IHDR/IFIRST.This results in IFIRST(header) pointing at or beyond
ITAIL(raw_inode), leaving no room for even the 4-byte terminator entry. Whenxattr_find_entry()is subsequently called,IS_LAST_ENTRY()reads 4 bytes from this out-of-bounds pointer, triggering a use-after-free.For example, with EXT4_INODE_SIZE=256 and i_extra_isize=124:
-ext4_iget()check: 128 + 124 = 252 <= 256, passes
- IFIRST = offset 252 + 4 (xattr header) = 256
- ITAIL = 256
-IS_LAST_ENTRY()reads 4 bytes at offset 256, past the inode bufferFix this by validating in
ext4_xattr_ibody_get()that there is enough space between IFIRST(header) and ITAIL for at least a 4-byte read before callingxattr_find_entry(). Return-EFSCORRUPTEDif the inline xattr region is too small.Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/ext4/xattr.c | 7 +++++++ 1 file changed, 7 insertions(+)fs/ext4/xattr.c +7
-1@@ -652,6 +652,13 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, header =IHDR (inode, raw_inode); end =ITAIL (inode, raw_inode); entry =IFIRST (header); + +if ((void *)entry +sizeof (__u32) > end) { +EXT4_ERROR_INODE (inode,"inline xattr region overflow" ); + error = -EFSCORRUPTED ; + goto cleanup; + } + error =xattr_find_entry (inode, &entry, end, name_index, name,0 );if (error) goto cleanup; --
Quoted 2 lines
2.34.1
Hi,
Gentle ping on this patch. It has been about two weeks since I submitted it.
This fixes a KASAN use-after-free in xattr_find_entry() reported by syzbot. The issue occurs when a corrupted ext4 filesystem has an i_extra_isize value that leaves insufficient space for inline xattr entries. The syzbot test has confirmed the fix.
syzbot report: https://syzkaller.appspot.com/bug?extid=fb32afec111a7d61b939
Please let me know if there are any concerns or if any changes are needed.
Thanks,
Deepanshu Kartikey
Theodore Tso
maintainerQuoted 5 lines · Deepanshu Kartikey
When mounting a corrupted ext4 filesystem, the inode's
i_extra_isizecan be set to a value that leaves insufficient space in the inode for the inline xattr header and entries. Whileext4_iget()validates thati_extra_isizefits within the inode size, it does not account for the additionalsizeof(ext4_xattr_ibody_header)needed by IHDR/IFIRST.
Actually, it does more than that. It also calls xattr_check_inode() which should validate the xattr block in the inode.
So instead of adding the check in ext4_xattr_ibody_get(), we should fix the check in __xattr_check_inode(). This is preferable since it's more efficient than checking every time we try to fetch an extended attribute, instead of validating it when the inode is read from the inode table block.
- Ted
Quoted 5 lines · Theodore Tso
Actually, it does more than that. It also calls
xattr_check_inode()which should validate the xattr block in the inode.
Quoted 7 lines
So instead of adding the check in
ext4_xattr_ibody_get(), we should fix the check in__xattr_check_inode(). This is preferable since it's more efficient than checking every time we try to fetch an extended attribute, instead of validating it when the inode is read from the inode table block.- Ted
Subject: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to prevent out-of-bounds access
Hi Ted,
Thank you for the review. I tried moving the fix to check_xattrs() as you suggested, but the syzbot reproducer still crashes. I added printk statements to trace the code path and found the root cause.
The issue is that __xattr_check_inode() runs once during ext4_iget(), but ext4_xattr_ibody_get() re-reads the inode from disk via ext4_get_inode_loc() on every call. The reproducer exploits this by shrinking the loop device after mount, causing the re-read to return corrupted data.
Here is the relevant debug output showing the sequence:
1) Inode 12 is loaded, __xattr_check_inode passes with gap=92:
DEBUG: inode 12: calling ext4_iget_extra_inode
DEBUG: inode 12: __xattr_check_inode called,
IFIRST=ffff88805b9f9fa4 end=ffff88805b9fa000 gap=92
2) The loop device is then shrunk:
loop0: detected capacity change from 1024 to 64
3) First access after shrink, inode re-read still looks okay:
DEBUG: inode 12: ibody_get
IFIRST=ffff88806edbcfa4 end=ffff88806edbd000 gap=92
4) Second access, re-read returns corrupted data with gap=-4:
DEBUG: inode 12: ibody_get
IFIRST=ffff88806edbd004 end=ffff88806edbd000 gap=-4
5) Crash follows immediately in xattr_find_entry().
Note that IFIRST and end are at different addresses between steps 1 and 4 - ext4_get_inode_loc() returned a different buffer_head with corrupted i_extra_isize, pushing IFIRST 4 bytes past end. The initial __xattr_check_inode() validation cannot protect against this because it validated the original buffer, not the corrupted re-read.
I think we need both fixes:
1) The bounds fix in check_xattrs() as you suggested, changing
(void *)next >= end to (void *)next + sizeof(u32) > end
2) A bounds check in ext4_xattr_ibody_get() before calling xattr_find_entry(), to catch corrupted re-reads
Should I submit a v2 with both fixes as a patch series?
Thanks,
Deepanshu Kartikey
Theodore Tso
maintainerSYZKALLER BUG: messing with a mounted file system via loop ioctls (was: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to) prevent out-of-bounds access
Quoted 10 lines · Deepanshu Kartikey
Thank you for the review. I tried moving the fix to
check_xattrs()as you suggested, but the syzbot reproducer still crashes. I added printk statements to trace the code path and found the root cause.The issue is that
__xattr_check_inode()runs once duringext4_iget(), butext4_xattr_ibody_get()re-reads the inode from disk viaext4_get_inode_loc()on every call. The reproducer exploits this by shrinking the loop device after mount, causing the re-read to return corrupted data.
I consider this more a defect in Syzkaller than in ext4. Syzkaller already unsets CONFIG_BLK_DEV_WRITE_MOUNTED because modifying a mounted file system is not considered a valid security concern. Unfortunately, the syzkaller fuzzer which corrupts a mounted file system by messing with a loop device using a privileged ioctl bypasses !CONFIG_DEV_BLK_WRITE_MOUNTED.
I'll accept a change to add a to check_xattrs(), which will protect against static corruptions, but adding an extra check to a hotpath (this would slow down SELinux, which aggressively needs to read the file's sid) to protect against a corruption issue which requires root privs is not something I'm interested in.
My priority is improving ext4 --- not reducing the syzkaller reports against ext4 to zero, since there are false positives like this. For people who care about reducing the syzkaller report gamification, my suggestion is to either extend CONFIG_BLK_DEV_WRITE_MOUNTED to prevent these loop device reconfiguration while a file system is mounted on that loop device, or to fix syzkaller to not create fuzzers like this.
Cheers,
- Ted
Quoted 44 lines
Here is the relevant debug output showing the sequence:
1) Inode 12 is loaded,
__xattr_check_inodepasses with gap=92:DEBUG: inode 12: calling
ext4_iget_extra_inode
DEBUG: inode 12:__xattr_check_inodecalled,IFIRST=ffff88805b9f9fa4 end=ffff88805b9fa000 gap=922) The loop device is then shrunk:
loop0: detected capacity change from 1024 to 64
3) First access after shrink, inode re-read still looks okay:
DEBUG: inode 12:
ibody_getIFIRST=ffff88806edbcfa4 end=ffff88806edbd000 gap=924) Second access, re-read returns corrupted data with gap=-4:
DEBUG: inode 12:
ibody_getIFIRST=ffff88806edbd004 end=ffff88806edbd000 gap=-45) Crash follows immediately in
xattr_find_entry().Note that IFIRST and end are at different addresses between steps 1 and 4 -
ext4_get_inode_loc()returned a differentbuffer_headwith corruptedi_extra_isize, pushing IFIRST 4 bytes past end. The initial__xattr_check_inode()validation cannot protect against this because it validated the original buffer, not the corrupted re-read.I think we need both fixes:
1) The bounds fix in
check_xattrs()as you suggested, changing(void *)next >= end to (void *)next + sizeof(u32) > end2) A bounds check in
ext4_xattr_ibody_get()before callingxattr_find_entry(), to catch corrupted re-readsShould I submit a v2 with both fixes as a patch series?
Thanks,
Deepanshu Kartikey
Re: SYZKALLER BUG: messing with a mounted file system via loop ioctls (was: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to) prevent out-of-bounds access
Quoted 33 lines · Theodore Tso
Quoted 10 lines · Deepanshu Kartikey
Thank you for the review. I tried moving the fix to
check_xattrs()as you suggested, but the syzbot reproducer still crashes. I added printk statements to trace the code path and found the root cause.The issue is that
__xattr_check_inode()runs once duringext4_iget(), butext4_xattr_ibody_get()re-reads the inode from disk viaext4_get_inode_loc()on every call. The reproducer exploits this by shrinking the loop device after mount, causing the re-read to return corrupted data.I consider this more a defect in Syzkaller than in ext4. Syzkaller already unsets
CONFIG_BLK_DEV_WRITE_MOUNTEDbecause modifying a mounted file system is not considered a valid security concern. Unfortunately, the syzkaller fuzzer which corrupts a mounted file system by messing with a loop device using a privileged ioctl bypasses !CONFIG_DEV_BLK_WRITE_MOUNTED.I'll accept a change to add a to
check_xattrs(), which will protect against static corruptions, but adding an extra check to a hotpath (this would slow down SELinux, which aggressively needs to read the file's sid) to protect against a corruption issue which requires root privs is not something I'm interested in.My priority is improving ext4 --- not reducing the syzkaller reports against ext4 to zero, since there are false positives like this. For people who care about reducing the syzkaller report gamification, my suggestion is to either extend
CONFIG_BLK_DEV_WRITE_MOUNTEDto prevent these loop device reconfiguration while a file system is mounted on that loop device, or to fix syzkaller to not create fuzzers like this.
Quoted 4 lines
Cheers,
- Ted
Thanks for the clarification. I have sent the patch v2 with check in check_xattrs.
Deepanshu
Re: SYZKALLER BUG: messing with a mounted file system via loop ioctls (was: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to) prevent out-of-bounds access
Quoted 28 lines · Deepanshu Kartikey
Quoted 26 lines · Theodore Tso
Quoted 10 lines · Deepanshu Kartikey
Thank you for the review. I tried moving the fix to
check_xattrs()as you suggested, but the syzbot reproducer still crashes. I added printk statements to trace the code path and found the root cause.The issue is that
__xattr_check_inode()runs once duringext4_iget(), butext4_xattr_ibody_get()re-reads the inode from disk viaext4_get_inode_loc()on every call. The reproducer exploits this by shrinking the loop device after mount, causing the re-read to return corrupted data.I consider this more a defect in Syzkaller than in ext4. Syzkaller already unsets
CONFIG_BLK_DEV_WRITE_MOUNTEDbecause modifying a mounted file system is not considered a valid security concern. Unfortunately, the syzkaller fuzzer which corrupts a mounted file system by messing with a loop device using a privileged ioctl bypasses !CONFIG_DEV_BLK_WRITE_MOUNTED.I'll accept a change to add a to
check_xattrs(), which will protect against static corruptions, but adding an extra check to a hotpath (this would slow down SELinux, which aggressively needs to read the file's sid) to protect against a corruption issue which requires root privs is not something I'm interested in.
Quoted 6 lines
Quoted 6 lines
My priority is improving ext4 --- not reducing the syzkaller reports against ext4 to zero, since there are false positives like this. For people who care about reducing the syzkaller report gamification, my suggestion is to either extend
CONFIG_BLK_DEV_WRITE_MOUNTEDto prevent these loop device reconfiguration while a file system is mounted on that loop device, or to fix syzkaller to not create fuzzers like this.
Hi Ted,
Thanks for the report.
https://syzkaller.appspot.com/text?tag=ReproSyz&x=17a43b3a580000
Do you mean LOOP_SET_STATUS64 ioctl? It's the only ioctl in the repro:
ioctl$LOOP_SET_STATUS64(r0, 0x4c04, ...)
Does it mean it should also be prohibited under CONFIG_BLK_DEV_WRITE_MOUNTED to avoid kernel memory corruption? Or at least some changes should be prohibited? E.g. I guess changing lo_offset is equivalent to writing to the mounted device.
Theodore Tso
maintainerRe: SYZKALLER BUG: messing with a mounted file system via loop ioctls (was: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to) prevent out-of-bounds access
Quoted 7 lines · Dmitry Vyukov
Thanks for the report.
https://syzkaller.appspot.com/text?tag=ReproSyz&x=17a43b3a580000
Do you mean
LOOP_SET_STATUS64ioctl? It's the only ioctl in the repro:ioctl$LOOP_SET_STATUS64(r0,
0x4c04, ...)
The loop LOOP_SET_STATUS ioctl is also problematic.
Quoted 4 lines
Does it mean it should also be prohibited under
CONFIG_BLK_DEV_WRITE_MOUNTEDto avoid kernel memory corruption? Or at least some changes should be prohibited? E.g. I guess changinglo_offsetis equivalent to writing to the mounted device.
Yes, that's correct. LOOP_SET_STATUS[64] can set lo_offset and lo_sizelimit which effectively mutates the loop device while it is mounted.
The fix is something like:
drivers/block/loop.c +10 -0
@@ -1238,6 +1238,16 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) err = -ENXIO ; goto out_unlock; } +#ifndef CONFIG_BLK_DEV_WRITE_MOUNTED) +/* +* bdev_writes_blocked is a static function defiend in +* block/bdev.c, so this won't work as-is. +*/ +if (bdev_writes_blocked (lo->lo_device)) { + err = -EBUSY ; + goto out_unlock; + } +#endif if (lo->lo_offset != info->lo_offset || lo->lo_sizelimit != info->lo_sizelimit) {
Turning this into a real patch that exports bdev_writes_blocked() is left as an exercise to the reader.
- Ted
Re: SYZKALLER BUG: messing with a mounted file system via loop ioctls (was: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to) prevent out-of-bounds access
Quoted 45 lines · Theodore Tso
Quoted 7 lines · Dmitry Vyukov
Thanks for the report.
https://syzkaller.appspot.com/text?tag=ReproSyz&x=17a43b3a580000
Do you mean
LOOP_SET_STATUS64ioctl? It's the only ioctl in the repro:ioctl$LOOP_SET_STATUS64(r0,
0x4c04, ...)The loop
LOOP_SET_STATUSioctl is also problematic.Quoted 4 lines
Does it mean it should also be prohibited under
CONFIG_BLK_DEV_WRITE_MOUNTEDto avoid kernel memory corruption? Or at least some changes should be prohibited? E.g. I guess changinglo_offsetis equivalent to writing to the mounted device.Yes, that's correct. LOOP_SET_STATUS[64] can set
lo_offsetandlo_sizelimitwhich effectively mutates the loop device while it is mounted.The fix is something like:
drivers/block/loop.c +10
-0@@ -1238,6 +1238,16 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) err = -ENXIO ; goto out_unlock; } +#ifndef CONFIG_BLK_DEV_WRITE_MOUNTED) +/* +* bdev_writes_blocked is a static function defiend in +* block/bdev.c, so this won't work as-is. +*/ +if (bdev_writes_blocked (lo->lo_device)) { + err = -EBUSY ; + goto out_unlock; + } +#endif if (lo->lo_offset != info->lo_offset || lo->lo_sizelimit != info->lo_sizelimit) {
Quoted 4 lines
Turning this into a real patch that exports
bdev_writes_blocked()is left as an exercise to the reader.- Ted
Hi Ted and Dmitry
Thanks for the sketch. I'll look into turning this into a proper patch, exporting bdev_writes_blocked() and fixing up the LOOP_SET_STATUS path.
Deepanshu
Theodore Tso
maintainerRe: SYZKALLER BUG: messing with a mounted file system via loop ioctls (was: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to) prevent out-of-bounds access
Quoted 4 lines · Deepanshu Kartikey
Thanks for the sketch. I'll look into turning this into a proper patch, exporting
bdev_writes_blocked()and fixing up theLOOP_SET_STATUSpath.
Thank you! Much appreciated!!
- Ted
Re: SYZKALLER BUG: messing with a mounted file system via loop ioctls (was: Re: [PATCH] ext4: add bounds check in ext4_xattr_ibody_get() to) prevent out-of-bounds access
In reply to Theodore TsoQuoted 25 lines · Theodore Tso
The fix is something like:
drivers/block/loop.c +10
-0@@ -1238,6 +1238,16 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) err = -ENXIO ; goto out_unlock; } +#ifndef CONFIG_BLK_DEV_WRITE_MOUNTED) +/* +* bdev_writes_blocked is a static function defiend in +* block/bdev.c, so this won't work as-is. +*/ +if (bdev_writes_blocked (lo->lo_device)) { + err = -EBUSY ; + goto out_unlock; + } +#endif if (lo->lo_offset != info->lo_offset || lo->lo_sizelimit != info->lo_sizelimit) {
Quoted 4 lines
Turning this into a real patch that exports
bdev_writes_blocked()is left as an exercise to the reader.- Ted
I have sent the patch
Thanks
Deepanshu