* [PATCH] btrfs: fix too long loop when defragging a 1 byte file
@ 2022-01-17 16:28 fdmanana
2022-01-17 17:39 ` David Sterba
2022-01-18 0:19 ` Qu Wenruo
0 siblings, 2 replies; 3+ messages in thread
From: fdmanana @ 2022-01-17 16:28 UTC (permalink / raw)
To: linux-btrfs
From: Filipe Manana <fdmanana@suse.com>
When attempting to defrag a file with a single byte, we can end up in a
too long loop, which is nearly infinite because at btrfs_defrag_file()
we end up with the variable last_byte assigned with a value of
18446744073709551615 (which is (u64)-1). The problem comes from the fact
we end up doing:
last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
So if last_byte was assigned 0, which is i_size - 1, we underflow and
end up with the value 18446744073709551615.
This is trivial to reproduce and the following script triggers it:
$ cat test.sh
#!/bin/bash
DEV=/dev/sdj
MNT=/mnt/sdj
mkfs.btrfs -f $DEV
mount $DEV $MNT
echo -n "X" > $MNT/foobar
btrfs filesystem defragment $MNT/foobar
umount $MNT
So fix this by not decrementing last_byte by 1 before doing the sector
size round up. Also, to make it easier to follow, make the round up right
after computing last_byte.
Fixes: 7b508037d4cac3 ("btrfs: defrag: use defrag_one_cluster() to implement btrfs_defrag_file()")
Reported-by: Anthony Ruhier <aruhier@mailbox.org>
Link: https://lore.kernel.org/linux-btrfs/0a269612-e43f-da22-c5bc-b34b1b56ebe8@mailbox.org/
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
fs/btrfs/ioctl.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index a5bd6926f7ff..6ad2bc2e5af3 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1518,12 +1518,16 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
if (range->start + range->len > range->start) {
/* Got a specific range */
- last_byte = min(isize, range->start + range->len) - 1;
+ last_byte = min(isize, range->start + range->len);
} else {
/* Defrag until file end */
- last_byte = isize - 1;
+ last_byte = isize;
}
+ /* Align the range */
+ cur = round_down(range->start, fs_info->sectorsize);
+ last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
+
/*
* If we were not given a ra, allocate a readahead context. As
* readahead is just an optimization, defrag will work without it so
@@ -1536,10 +1540,6 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
file_ra_state_init(ra, inode->i_mapping);
}
- /* Align the range */
- cur = round_down(range->start, fs_info->sectorsize);
- last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
-
while (cur < last_byte) {
u64 cluster_end;
--
2.33.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] btrfs: fix too long loop when defragging a 1 byte file
2022-01-17 16:28 [PATCH] btrfs: fix too long loop when defragging a 1 byte file fdmanana
@ 2022-01-17 17:39 ` David Sterba
2022-01-18 0:19 ` Qu Wenruo
1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2022-01-17 17:39 UTC (permalink / raw)
To: fdmanana; +Cc: linux-btrfs
On Mon, Jan 17, 2022 at 04:28:29PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> When attempting to defrag a file with a single byte, we can end up in a
> too long loop, which is nearly infinite because at btrfs_defrag_file()
> we end up with the variable last_byte assigned with a value of
> 18446744073709551615 (which is (u64)-1). The problem comes from the fact
> we end up doing:
>
> last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
>
> So if last_byte was assigned 0, which is i_size - 1, we underflow and
> end up with the value 18446744073709551615.
>
> This is trivial to reproduce and the following script triggers it:
>
> $ cat test.sh
> #!/bin/bash
>
> DEV=/dev/sdj
> MNT=/mnt/sdj
>
> mkfs.btrfs -f $DEV
> mount $DEV $MNT
>
> echo -n "X" > $MNT/foobar
>
> btrfs filesystem defragment $MNT/foobar
>
> umount $MNT
>
> So fix this by not decrementing last_byte by 1 before doing the sector
> size round up. Also, to make it easier to follow, make the round up right
> after computing last_byte.
>
> Fixes: 7b508037d4cac3 ("btrfs: defrag: use defrag_one_cluster() to implement btrfs_defrag_file()")
> Reported-by: Anthony Ruhier <aruhier@mailbox.org>
> Link: https://lore.kernel.org/linux-btrfs/0a269612-e43f-da22-c5bc-b34b1b56ebe8@mailbox.org/
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
Thank you very much, I'll try to get it to out ASAP so it could get
released in the next week stable update.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] btrfs: fix too long loop when defragging a 1 byte file
2022-01-17 16:28 [PATCH] btrfs: fix too long loop when defragging a 1 byte file fdmanana
2022-01-17 17:39 ` David Sterba
@ 2022-01-18 0:19 ` Qu Wenruo
1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2022-01-18 0:19 UTC (permalink / raw)
To: fdmanana, linux-btrfs
On 2022/1/18 00:28, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> When attempting to defrag a file with a single byte, we can end up in a
> too long loop, which is nearly infinite because at btrfs_defrag_file()
> we end up with the variable last_byte assigned with a value of
> 18446744073709551615 (which is (u64)-1). The problem comes from the fact
> we end up doing:
>
> last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
>
> So if last_byte was assigned 0, which is i_size - 1, we underflow and
> end up with the value 18446744073709551615.
>
> This is trivial to reproduce and the following script triggers it:
>
> $ cat test.sh
> #!/bin/bash
>
> DEV=/dev/sdj
> MNT=/mnt/sdj
>
> mkfs.btrfs -f $DEV
> mount $DEV $MNT
>
> echo -n "X" > $MNT/foobar
>
> btrfs filesystem defragment $MNT/foobar
>
> umount $MNT
>
> So fix this by not decrementing last_byte by 1 before doing the sector
> size round up. Also, to make it easier to follow, make the round up right
> after computing last_byte.
>
> Fixes: 7b508037d4cac3 ("btrfs: defrag: use defrag_one_cluster() to implement btrfs_defrag_file()")
> Reported-by: Anthony Ruhier <aruhier@mailbox.org>
> Link: https://lore.kernel.org/linux-btrfs/0a269612-e43f-da22-c5bc-b34b1b56ebe8@mailbox.org/
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks,
Qu
> ---
> fs/btrfs/ioctl.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index a5bd6926f7ff..6ad2bc2e5af3 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -1518,12 +1518,16 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
>
> if (range->start + range->len > range->start) {
> /* Got a specific range */
> - last_byte = min(isize, range->start + range->len) - 1;
> + last_byte = min(isize, range->start + range->len);
> } else {
> /* Defrag until file end */
> - last_byte = isize - 1;
> + last_byte = isize;
> }
>
> + /* Align the range */
> + cur = round_down(range->start, fs_info->sectorsize);
> + last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
> +
> /*
> * If we were not given a ra, allocate a readahead context. As
> * readahead is just an optimization, defrag will work without it so
> @@ -1536,10 +1540,6 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
> file_ra_state_init(ra, inode->i_mapping);
> }
>
> - /* Align the range */
> - cur = round_down(range->start, fs_info->sectorsize);
> - last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
> -
> while (cur < last_byte) {
> u64 cluster_end;
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-01-18 0:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-17 16:28 [PATCH] btrfs: fix too long loop when defragging a 1 byte file fdmanana
2022-01-17 17:39 ` David Sterba
2022-01-18 0:19 ` Qu Wenruo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).