Skip to contentSkip to conversation

Fix a potential infinite loop in extract_user_to_sg()

PATCH Applied David Howells 3 participants 4 messages Apr 25, 2024 – Apr 26, 2024

Subsystems (8)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH 1/1 Fix a potential infinite loop in extract_user_to_sg()

Fix extract_user_to_sg() so that it will break out of the loop if iov_iter_extract_pages() returns 0 rather than looping around forever.

[Note that I've included two fixes lines as the function got moved to a different file and renamed]

Fixes 85dd2c8ff368 ("netfs: Add a function to extract a UBUF or IOVEC into a BVEC iterator")
Fixes f5f82cd18732 ("Move netfs_extract_iter_to_sg() to lib/scatterlist.c")
cc Jeff Layton <[email protected]>
cc Steve French <[email protected]>
cc Herbert Xu <[email protected]>
cc "David S. Miller" <[email protected]>
cc Eric Dumazet <[email protected]>
cc Jakub Kicinski <[email protected]>
cc Paolo Abeni <[email protected]>
---
 lib/scatterlist.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/scatterlist.c +1 -1
index 68b45c82c37a..7bc2220fea80 100644
@@ -1124,7 +1124,7 @@ static ssize_t extract_user_to_sg(struct iov_iter *iter,
 	do {
 		res = iov_iter_extract_pages(iter, &pages, maxsize, sg_max,
 					     extraction_flags, &off);
-		if (res < 0)
+		if (res <= 0)
 			goto failed;
 
 		len = res;
maintainer
#1 lore raw
Quoted 2 lines · On Thu, 25 Apr 2024 09:39:32 +0100 David Howells wrote:

Fix extract_user_to_sg() so that it will break out of the loop if iov_iter_extract_pages() returns 0 rather than looping around forever.

Is "goto fail" the right way to break out here?
My intuition would be "break".

On a quick read it seems like res = 0 may occur if we run out of iterator, is passing maxsize > iter->count illegal?

author maintainer
#2 lore raw
Quoted 9 lines · Jakub Kicinski
Quoted 2 lines · On Thu, 25 Apr 2024 09:39:32 +0100 David Howells wrote:

Fix extract_user_to_sg() so that it will break out of the loop if iov_iter_extract_pages() returns 0 rather than looping around forever.

Is "goto fail" the right way to break out here?
My intuition would be "break".

On a quick read it seems like res = 0 may occur if we run out of iterator, is passing maxsize > iter->count illegal?

I would say that you're not allowed to ask for more than is in the iterator. In a number of places this is called, it's a clear failure if you can't get that the requested amount out of it - for example, if we're building a cifs message and have set all the fields in the header and are trying to encrypt the message.

David

#3 lore raw
In reply to David Howells
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 22 lines · On Thu, 25 Apr 2024 09:39:32 +0100 you wrote:

Fix extract_user_to_sg() so that it will break out of the loop if iov_iter_extract_pages() returns 0 rather than looping around forever.

[Note that I've included two fixes lines as the function got moved to a different file and renamed]

Fixes 85dd2c8ff368 ("netfs: Add a function to extract a UBUF or IOVEC into a BVEC iterator")
Fixes f5f82cd18732 ("Move netfs_extract_iter_to_sg() to lib/scatterlist.c")
cc Jeff Layton <[email protected]>
cc Steve French <[email protected]>
cc Herbert Xu <[email protected]>
cc "David S. Miller" <[email protected]>
cc Eric Dumazet <[email protected]>
cc Jakub Kicinski <[email protected]>
cc Paolo Abeni <[email protected]>
Quoted 1 line

[...]

Here is the summary with links:
- [net] Fix a potential infinite loop in extract_user_to_sg()

    https://git.kernel.org/netdev/net/c/6a30653b604a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html