Skip to contentSkip to conversation

bcachefs: fix iov_iter count underflow on sub-block dio read

PATCH In review Brian Foster 3 participants 3 messages Feb 15, 2024 – Feb 16, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bcachefs: fix iov_iter count underflow on sub-block dio read

bch2_direct_IO_read() checks the request offset and size for sector alignment and then falls through to a couple calculations to shrink the size of the request based on the inode size. The problem is that these checks round up to the fs block size, which runs the risk of underflowing iter->count if the block size happens to be large enough. This is triggered by fstest generic/361 with a 4k block size, which subsequently leads to a crash. To avoid this crash, check that the shorten length doesn't exceed the overall length of the iter.

---

Ok, I think Su expressed preference for this variant over v1. I'm off for a bit after today so I'm throwing this up onto CI in the event that we'd like to at least get the crash problem fixed. If there's more to look into here, let me know and I'll add it to the todo list after I'm back..

Brian

v2:
- Check for underflow instead of removing shorten entirely.
v1: https://lore.kernel.org/linux-bcachefs/[email protected]/

 fs/bcachefs/fs-io-direct.c | 2 ++
 1 file changed, 2 insertions(+)
fs/bcachefs/fs-io-direct.c +2 -0
index e3b219e19e10..33cb6da3a5ad 100644
@@ -88,6 +88,8 @@ static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
 		return ret;
 
 	shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c));
+	if (shorten >= iter->count)
+		shorten = 0;
 	iter->count -= shorten;
 
 	bio = bio_alloc_bioset(NULL,
Signature · 1 line
2.42.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Brian Foster

bch2_direct_IO_read() checks the request offset and size for sector alignment and then falls through to a couple calculations to shrink the size of the request based on the inode size. The problem is that these checks round up to the fs block size, which runs the risk of underflowing iter->count if the block size happens to be large enough. This is triggered by fstest generic/361 with a 4k block size, which subsequently leads to a crash. To avoid this crash, check that the shorten length doesn't exceed the overall length of the iter.

Quoted 2 lines

LGTM.

Since imap.google.com is blocked by GFW, I use my personal email address in mu4e + emacs hosting on remote machine daily.
I am no clear what is required(gpg?) if I use my personal address to give a review by in my employer address.

--
Su

Quoted 32 lines

---

Ok, I think Su expressed preference for this variant over v1. I'm off for a bit after today so I'm throwing this up onto CI in the event that we'd like to at least get the crash problem fixed. If there's more to look into here, let me know and I'll add it to the todo list after I'm back..

Brian

v2:
- Check for underflow instead of removing shorten entirely.
v1: https://lore.kernel.org/linux-bcachefs/[email protected]/

 fs/bcachefs/fs-io-direct.c | 2 ++
 1 file changed, 2 insertions(+)
fs/bcachefs/fs-io-direct.c +2 -0
index e3b219e19e10..33cb6da3a5ad 100644
@@ -88,6 +88,8 @@ static int bch2_direct_IO_read(struct kiocb 
 *req, struct iov_iter *iter)
 		return ret;
 
 	shorten = iov_iter_count(iter) - round_up(ret, 
 block_bytes(c));
+	if (shorten >= iter->count)
+		shorten = 0;
 	iter->count -= shorten;

bio = bio_alloc_bioset(NULL,

#2 lore raw
Quoted 15 lines · Su Yue
Quoted 10 lines · Brian Foster

bch2_direct_IO_read() checks the request offset and size for sector alignment and then falls through to a couple calculations to shrink the size of the request based on the inode size. The problem is that these checks round up to the fs block size, which runs the risk of underflowing iter->count if the block size happens to be large enough. This is triggered by fstest generic/361 with a 4k block size, which subsequently leads to a crash. To avoid this crash, check that the shorten length doesn't exceed the overall length of the iter.

Quoted 2 lines
Quoted 7 lines

LGTM.

Since imap.google.com is blocked by GFW, I use my personal email address in mu4e + emacs hosting on remote machine daily.
I am no clear what is required(gpg?) if I use my personal address to give a review by in my employer address.

Most people don't gpg sign their regular emails, just pull requests, so I'm not going to sweat it :)