* [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data
@ 2024-09-24 22:39 Kuntal Nayak
2024-09-24 22:39 ` [PATCH v5.10] xfs: No need for inode number error injection in __xfs_dir3_data_check Kuntal Nayak
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Kuntal Nayak @ 2024-09-24 22:39 UTC (permalink / raw)
To: leah.rumancik, jwong, linux-xfs, linux-kernel, gregkh
Cc: ajay.kaher, alexey.makhalov, vasavi.sirnapalli, lei lu,
Dave Chinner, Darrick J . Wong, Chandan Babu R, Kuntal Nayak
From: lei lu <llfamsec@gmail.com>
[ Upstream commit fb63435b7c7dc112b1ae1baea5486e0a6e27b196 ]
There is a lack of verification of the space occupied by fixed members
of xlog_op_header in the xlog_recover_process_data.
We can create a crafted image to trigger an out of bounds read by
following these steps:
1) Mount an image of xfs, and do some file operations to leave records
2) Before umounting, copy the image for subsequent steps to simulate
abnormal exit. Because umount will ensure that tail_blk and
head_blk are the same, which will result in the inability to enter
xlog_recover_process_data
3) Write a tool to parse and modify the copied image in step 2
4) Make the end of the xlog_op_header entries only 1 byte away from
xlog_rec_header->h_size
5) xlog_rec_header->h_num_logops++
6) Modify xlog_rec_header->h_crc
Fix:
Add a check to make sure there is sufficient space to access fixed members
of xlog_op_header.
Signed-off-by: lei lu <llfamsec@gmail.com>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Chandan Babu R <chandanbabu@kernel.org>
Signed-off-by: Kuntal Nayak <kuntal.nayak@broadcom.com>
---
fs/xfs/xfs_log_recover.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index e61f28ce3..eafe76f30 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2419,7 +2419,10 @@ xlog_recover_process_data(
ohead = (struct xlog_op_header *)dp;
dp += sizeof(*ohead);
- ASSERT(dp <= end);
+ if (dp > end) {
+ xfs_warn(log->l_mp, "%s: op header overrun", __func__);
+ return -EFSCORRUPTED;
+ }
/* errors will abort recovery */
error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
--
2.39.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5.10] xfs: No need for inode number error injection in __xfs_dir3_data_check
2024-09-24 22:39 [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Kuntal Nayak
@ 2024-09-24 22:39 ` Kuntal Nayak
2024-09-24 22:39 ` [PATCH v5.10] xfs: don't walk off the end of a directory data block Kuntal Nayak
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Kuntal Nayak @ 2024-09-24 22:39 UTC (permalink / raw)
To: leah.rumancik, jwong, linux-xfs, linux-kernel, gregkh
Cc: ajay.kaher, alexey.makhalov, vasavi.sirnapalli, Dave Chinner,
Darrick J . Wong, Christoph Hellwig, Kuntal Nayak
From: Dave Chinner <dchinner@redhat.com>
[ Upstream commit 39d3c0b5968b5421922e2fc939b6d6158df8ac1c ]
We call xfs_dir_ino_validate() for every dir entry in a directory
when doing validity checking of the directory. It calls
xfs_verify_dir_ino() then emits a corruption report if bad or does
error injection if good. It is extremely costly:
43.27% [kernel] [k] xfs_dir3_leaf_check_int
10.28% [kernel] [k] __xfs_dir3_data_check
6.61% [kernel] [k] xfs_verify_dir_ino
4.16% [kernel] [k] xfs_errortag_test
4.00% [kernel] [k] memcpy
3.48% [kernel] [k] xfs_dir_ino_validate
7% of the cpu usage in this directory traversal workload is
xfs_dir_ino_validate() doing absolutely nothing.
We don't need error injection to simulate a bad inode numbers in the
directory structure because we can do that by fuzzing the structure
on disk.
And we don't need a corruption report, because the
__xfs_dir3_data_check() will emit one if the inode number is bad.
So just call xfs_verify_dir_ino() directly here, and get rid of all
this unnecessary overhead:
40.30% [kernel] [k] xfs_dir3_leaf_check_int
10.98% [kernel] [k] __xfs_dir3_data_check
8.10% [kernel] [k] xfs_verify_dir_ino
4.42% [kernel] [k] memcpy
2.22% [kernel] [k] xfs_dir2_data_get_ftype
1.52% [kernel] [k] do_raw_spin_lock
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Kuntal Nayak <kuntal.nayak@broadcom.com>
---
fs/xfs/libxfs/xfs_dir2_data.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/xfs/libxfs/xfs_dir2_data.c b/fs/xfs/libxfs/xfs_dir2_data.c
index 375b3edb2..e67fa086f 100644
--- a/fs/xfs/libxfs/xfs_dir2_data.c
+++ b/fs/xfs/libxfs/xfs_dir2_data.c
@@ -218,7 +218,7 @@ __xfs_dir3_data_check(
*/
if (dep->namelen == 0)
return __this_address;
- if (xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)))
+ if (!xfs_verify_dir_ino(mp, be64_to_cpu(dep->inumber)))
return __this_address;
if (offset + xfs_dir2_data_entsize(mp, dep->namelen) > end)
return __this_address;
--
2.39.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5.10] xfs: don't walk off the end of a directory data block
2024-09-24 22:39 [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Kuntal Nayak
2024-09-24 22:39 ` [PATCH v5.10] xfs: No need for inode number error injection in __xfs_dir3_data_check Kuntal Nayak
@ 2024-09-24 22:39 ` Kuntal Nayak
2024-09-27 6:52 ` [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Greg KH
2024-09-27 6:52 ` Greg KH
3 siblings, 0 replies; 7+ messages in thread
From: Kuntal Nayak @ 2024-09-24 22:39 UTC (permalink / raw)
To: leah.rumancik, jwong, linux-xfs, linux-kernel, gregkh
Cc: ajay.kaher, alexey.makhalov, vasavi.sirnapalli, lei lu,
Darrick J . Wong, Chandan Babu R, Kuntal Nayak
From: lei lu <llfamsec@gmail.com>
[ Upstream commit 0c7fcdb6d06cdf8b19b57c17605215b06afa864a ]
This adds sanity checks for xfs_dir2_data_unused and xfs_dir2_data_entry
to make sure don't stray beyond valid memory region. Before patching, the
loop simply checks that the start offset of the dup and dep is within the
range. So in a crafted image, if last entry is xfs_dir2_data_unused, we
can change dup->length to dup->length-1 and leave 1 byte of space. In the
next traversal, this space will be considered as dup or dep. We may
encounter an out of bound read when accessing the fixed members.
In the patch, we make sure that the remaining bytes large enough to hold
an unused entry before accessing xfs_dir2_data_unused and
xfs_dir2_data_unused is XFS_DIR2_DATA_ALIGN byte aligned. We also make
sure that the remaining bytes large enough to hold a dirent with a
single-byte name before accessing xfs_dir2_data_entry.
Signed-off-by: lei lu <llfamsec@gmail.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Chandan Babu R <chandanbabu@kernel.org>
Signed-off-by: Kuntal Nayak <kuntal.nayak@broadcom.com>
---
fs/xfs/libxfs/xfs_dir2_data.c | 31 ++++++++++++++++++++++++++-----
fs/xfs/libxfs/xfs_dir2_priv.h | 7 +++++++
2 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_dir2_data.c b/fs/xfs/libxfs/xfs_dir2_data.c
index e67fa086f..72e4c3678 100644
--- a/fs/xfs/libxfs/xfs_dir2_data.c
+++ b/fs/xfs/libxfs/xfs_dir2_data.c
@@ -177,6 +177,14 @@ __xfs_dir3_data_check(
while (offset < end) {
struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
+ unsigned int reclen;
+
+ /*
+ * Are the remaining bytes large enough to hold an
+ * unused entry?
+ */
+ if (offset > end - xfs_dir2_data_unusedsize(1))
+ return __this_address;
/*
* If it's unused, look for the space in the bestfree table.
@@ -186,9 +194,13 @@ __xfs_dir3_data_check(
if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
xfs_failaddr_t fa;
+ reclen = xfs_dir2_data_unusedsize(
+ be16_to_cpu(dup->length));
if (lastfree != 0)
return __this_address;
- if (offset + be16_to_cpu(dup->length) > end)
+ if (be16_to_cpu(dup->length) != reclen)
+ return __this_address;
+ if (offset + reclen > end)
return __this_address;
if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
offset)
@@ -206,10 +218,18 @@ __xfs_dir3_data_check(
be16_to_cpu(bf[2].length))
return __this_address;
}
- offset += be16_to_cpu(dup->length);
+ offset += reclen;
lastfree = 1;
continue;
}
+
+ /*
+ * This is not an unused entry. Are the remaining bytes
+ * large enough for a dirent with a single-byte name?
+ */
+ if (offset > end - xfs_dir2_data_entsize(mp, 1))
+ return __this_address;
+
/*
* It's a real entry. Validate the fields.
* If this is a block directory then make sure it's
@@ -218,9 +238,10 @@ __xfs_dir3_data_check(
*/
if (dep->namelen == 0)
return __this_address;
- if (!xfs_verify_dir_ino(mp, be64_to_cpu(dep->inumber)))
+ reclen = xfs_dir2_data_entsize(mp, dep->namelen);
+ if (offset + reclen > end)
return __this_address;
- if (offset + xfs_dir2_data_entsize(mp, dep->namelen) > end)
+ if (!xfs_verify_dir_ino(mp, be64_to_cpu(dep->inumber)))
return __this_address;
if (be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)) != offset)
return __this_address;
@@ -244,7 +265,7 @@ __xfs_dir3_data_check(
if (i >= be32_to_cpu(btp->count))
return __this_address;
}
- offset += xfs_dir2_data_entsize(mp, dep->namelen);
+ offset += reclen;
}
/*
* Need to have seen all the entries and all the bestfree slots.
diff --git a/fs/xfs/libxfs/xfs_dir2_priv.h b/fs/xfs/libxfs/xfs_dir2_priv.h
index 44c6a77cb..e46063cde 100644
--- a/fs/xfs/libxfs/xfs_dir2_priv.h
+++ b/fs/xfs/libxfs/xfs_dir2_priv.h
@@ -186,6 +186,13 @@ void xfs_dir2_sf_put_ftype(struct xfs_mount *mp,
extern int xfs_readdir(struct xfs_trans *tp, struct xfs_inode *dp,
struct dir_context *ctx, size_t bufsize);
+static inline unsigned int
+xfs_dir2_data_unusedsize(
+ unsigned int len)
+{
+ return round_up(len, XFS_DIR2_DATA_ALIGN);
+}
+
static inline unsigned int
xfs_dir2_data_entsize(
struct xfs_mount *mp,
--
2.39.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data
2024-09-24 22:39 [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Kuntal Nayak
2024-09-24 22:39 ` [PATCH v5.10] xfs: No need for inode number error injection in __xfs_dir3_data_check Kuntal Nayak
2024-09-24 22:39 ` [PATCH v5.10] xfs: don't walk off the end of a directory data block Kuntal Nayak
@ 2024-09-27 6:52 ` Greg KH
2024-09-27 6:52 ` Greg KH
3 siblings, 0 replies; 7+ messages in thread
From: Greg KH @ 2024-09-27 6:52 UTC (permalink / raw)
To: Kuntal Nayak
Cc: leah.rumancik, linux-xfs, linux-kernel, ajay.kaher,
alexey.makhalov, vasavi.sirnapalli, lei lu, Dave Chinner,
Darrick J . Wong, Chandan Babu R
On Tue, Sep 24, 2024 at 03:39:56PM -0700, Kuntal Nayak wrote:
> From: lei lu <llfamsec@gmail.com>
>
> [ Upstream commit fb63435b7c7dc112b1ae1baea5486e0a6e27b196 ]
For xfs patches, we can only take them from the xfs developers, so
please work with them to get acks that this series is properly tested
and approved by them for acceptance into the stable trees.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data
2024-09-24 22:39 [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Kuntal Nayak
` (2 preceding siblings ...)
2024-09-27 6:52 ` [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Greg KH
@ 2024-09-27 6:52 ` Greg KH
2024-10-07 19:47 ` Kuntal Nayak
3 siblings, 1 reply; 7+ messages in thread
From: Greg KH @ 2024-09-27 6:52 UTC (permalink / raw)
To: Kuntal Nayak
Cc: leah.rumancik, linux-xfs, linux-kernel, ajay.kaher,
alexey.makhalov, vasavi.sirnapalli, lei lu, Dave Chinner,
Darrick J . Wong, Chandan Babu R
On Tue, Sep 24, 2024 at 03:39:56PM -0700, Kuntal Nayak wrote:
> From: lei lu <llfamsec@gmail.com>
>
> [ Upstream commit fb63435b7c7dc112b1ae1baea5486e0a6e27b196 ]
Also, what is the ordering here? Should I just guess?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data
2024-09-27 6:52 ` Greg KH
@ 2024-10-07 19:47 ` Kuntal Nayak
2024-10-07 20:53 ` Leah Rumancik
0 siblings, 1 reply; 7+ messages in thread
From: Kuntal Nayak @ 2024-10-07 19:47 UTC (permalink / raw)
To: Greg KH, linux-xfs
Cc: leah.rumancik, linux-kernel, ajay.kaher, alexey.makhalov,
vasavi.sirnapalli, lei lu, Dave Chinner, Darrick J . Wong,
Chandan Babu R
Thank you, Greg, for getting back to me. Following is the order for patches,
1. xfs: No need for inode number error injection in __xfs_dir3_data_check
2. xfs: don't walk off the end of a directory data block
3. xfs: add bounds checking to xlog_recover_process_data
Hello xfs-team, could you kindly assist me in reviewing the 3 patches
listed above for LTS v5.10?
------
Sincerely,
Kuntal
On Fri, Sep 27, 2024 at 1:00 AM Greg KH <gregkh@linuxfoundation.org> wrote:
>
> On Tue, Sep 24, 2024 at 03:39:56PM -0700, Kuntal Nayak wrote:
> > From: lei lu <llfamsec@gmail.com>
> >
> > [ Upstream commit fb63435b7c7dc112b1ae1baea5486e0a6e27b196 ]
>
> Also, what is the ordering here? Should I just guess?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data
2024-10-07 19:47 ` Kuntal Nayak
@ 2024-10-07 20:53 ` Leah Rumancik
0 siblings, 0 replies; 7+ messages in thread
From: Leah Rumancik @ 2024-10-07 20:53 UTC (permalink / raw)
To: Kuntal Nayak
Cc: Greg KH, linux-xfs, linux-kernel, ajay.kaher, alexey.makhalov,
vasavi.sirnapalli, lei lu, Dave Chinner, Darrick J . Wong,
Chandan Babu R, Amir Goldstein
Hi Kuntal!
Thanks for proposing these patches. The current process for
backporting to xfs requires that patches are tested for any
regressions via xfstests. I believe Amir was last in charge of 5.10.y.
I think he is still on vacation, but even once he returns, I'm not
sure if he will be maintaining this branch any longer so it seems
5.10.y might be left unsupported when it comes to XFS. If you'd like
to take over for 5.10.y to keep backports flowing, we'd be happy to
have you join our efforts :)
- leah
On Mon, Oct 7, 2024 at 12:48 PM Kuntal Nayak <kuntal.nayak@broadcom.com> wrote:
>
> Thank you, Greg, for getting back to me. Following is the order for patches,
>
> 1. xfs: No need for inode number error injection in __xfs_dir3_data_check
> 2. xfs: don't walk off the end of a directory data block
> 3. xfs: add bounds checking to xlog_recover_process_data
>
>
> Hello xfs-team, could you kindly assist me in reviewing the 3 patches
> listed above for LTS v5.10?
>
> ------
> Sincerely,
> Kuntal
>
> On Fri, Sep 27, 2024 at 1:00 AM Greg KH <gregkh@linuxfoundation.org> wrote:
> >
> > On Tue, Sep 24, 2024 at 03:39:56PM -0700, Kuntal Nayak wrote:
> > > From: lei lu <llfamsec@gmail.com>
> > >
> > > [ Upstream commit fb63435b7c7dc112b1ae1baea5486e0a6e27b196 ]
> >
> > Also, what is the ordering here? Should I just guess?
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-10-07 20:53 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-24 22:39 [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Kuntal Nayak
2024-09-24 22:39 ` [PATCH v5.10] xfs: No need for inode number error injection in __xfs_dir3_data_check Kuntal Nayak
2024-09-24 22:39 ` [PATCH v5.10] xfs: don't walk off the end of a directory data block Kuntal Nayak
2024-09-27 6:52 ` [PATCH v5.10] xfs: add bounds checking to xlog_recover_process_data Greg KH
2024-09-27 6:52 ` Greg KH
2024-10-07 19:47 ` Kuntal Nayak
2024-10-07 20:53 ` Leah Rumancik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox