* [PATCH] net: datagram: fix integer overflow in zerocopy_sg_from_iter
@ 2026-07-28 6:42 Jiangshan Yi
2026-07-28 15:30 ` Joe Damato
0 siblings, 1 reply; 3+ messages in thread
From: Jiangshan Yi @ 2026-07-28 6:42 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni
Cc: horms, stefanha, mst, will, ralf, viro, kuniyu, linux-kernel,
netdev, 13667453960, Jiangshan Yi, stable
zerocopy_sg_from_iter() computes the copy length as:
copy = min_t(int, skb_headlen(skb), iov_iter_count(from));
iov_iter_count() returns size_t. When it exceeds INT_MAX (e.g. via
io_uring provided buffers), the int cast wraps negative, wins the min()
comparison, and the negative copy propagates into
skb_copy_datagram_from_iter(), which can trigger WARN_ON or corrupt
data.
Use min_t(size_t, ...) so the comparison is done in the correct type.
The result is always <= skb_headlen(skb), which fits in int.
Fixes: 3a654f975bf9 ("new helpers: skb_copy_datagram_from_iter() and zerocopy_sg_from_iter()")
Cc: stable@vger.kernel.org
Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
---
net/core/datagram.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/core/datagram.c b/net/core/datagram.c
index c285c6465923..fd8e17d25a3d 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -783,7 +783,7 @@ EXPORT_SYMBOL(__zerocopy_sg_from_iter);
*/
int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
{
- int copy = min_t(int, skb_headlen(skb), iov_iter_count(from));
+ int copy = min_t(size_t, skb_headlen(skb), iov_iter_count(from));
/* copy up to skb headlen */
if (skb_copy_datagram_from_iter(skb, 0, from, copy))
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] net: datagram: fix integer overflow in zerocopy_sg_from_iter
2026-07-28 6:42 [PATCH] net: datagram: fix integer overflow in zerocopy_sg_from_iter Jiangshan Yi
@ 2026-07-28 15:30 ` Joe Damato
2026-07-29 2:15 ` Jiangshan Yi
0 siblings, 1 reply; 3+ messages in thread
From: Joe Damato @ 2026-07-28 15:30 UTC (permalink / raw)
To: Jiangshan Yi
Cc: davem, edumazet, kuba, pabeni, horms, stefanha, mst, will, ralf,
viro, kuniyu, linux-kernel, netdev, 13667453960, stable
On Tue, Jul 28, 2026 at 02:42:35PM +0800, Jiangshan Yi wrote:
> zerocopy_sg_from_iter() computes the copy length as:
>
> copy = min_t(int, skb_headlen(skb), iov_iter_count(from));
>
> iov_iter_count() returns size_t. When it exceeds INT_MAX (e.g. via
> io_uring provided buffers), the int cast wraps negative, wins the min()
idk I looked at the code and it looks to be capped at INT_MAX, so i'm not sure
that the io_uring example could ever happen ?
I think the commit message misstates what is actually possible
> comparison, and the negative copy propagates into
> skb_copy_datagram_from_iter(), which can trigger WARN_ON or corrupt
> data.
>
> Use min_t(size_t, ...) so the comparison is done in the correct type.
> The result is always <= skb_headlen(skb), which fits in int.
> Fixes: 3a654f975bf9 ("new helpers: skb_copy_datagram_from_iter() and zerocopy_sg_from_iter()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
> ---
> net/core/datagram.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/core/datagram.c b/net/core/datagram.c
> index c285c6465923..fd8e17d25a3d 100644
> --- a/net/core/datagram.c
> +++ b/net/core/datagram.c
> @@ -783,7 +783,7 @@ EXPORT_SYMBOL(__zerocopy_sg_from_iter);
> */
> int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
> {
> - int copy = min_t(int, skb_headlen(skb), iov_iter_count(from));
> + int copy = min_t(size_t, skb_headlen(skb), iov_iter_count(from));
I don't get this.
Wouldn't copy need to also be a size_t to be consistent with the general
"idea" of this patch ?
If you leave copy as an int aren't you just shifting the alleged overflow to
the assignment ?
Even if you changed copy to a size_t then.....
>
> /* copy up to skb headlen */
> if (skb_copy_datagram_from_iter(skb, 0, from, copy))
this would still be a problem because len here is an int ?
I feel like if you really wanna fix this you would have to use min3 or
something instead of min_t ?
But, I'd probably just leave it and not propose a patch to change this at all.
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: Re: [PATCH] net: datagram: fix integer overflow in zerocopy_sg_from_iter
2026-07-28 15:30 ` Joe Damato
@ 2026-07-29 2:15 ` Jiangshan Yi
0 siblings, 0 replies; 3+ messages in thread
From: Jiangshan Yi @ 2026-07-29 2:15 UTC (permalink / raw)
To: joe
Cc: 13667453960, davem, edumazet, horms, kuba, kuniyu, linux-kernel,
mst, netdev, pabeni, ralf, stable, stefanha, viro, will,
yijiangshan
Hi Joe,
Thanks for the review, you're right on all points.
I verified that import_ubuf(), __import_iovec(), and import_single_range()
all cap at MAX_RW_COUNT (INT_MAX & PAGE_MASK), which is below INT_MAX.
Both callers of zerocopy_sg_from_iter() (tun.c and tap.c) require
msg_control to be non-NULL for zerocopy, which means the socket sendmsg
path -- always through import_iovec. So the count is always capped, and
my "io_uring provided buffers" claim in the commit message was wrong.
You're also right about the type inconsistency -- leaving copy as int
defeats the purpose, and skb_copy_datagram_from_iter()'s int len remains
a problem regardless.
I did look into io_import_fixed(), the only iov_iter init path without a
MAX_RW_COUNT cap (used by WRITE_FIXED, SEND_ZC with FIXED_BUF, and
URING_CMD). It can produce count > INT_MAX. But this is by design --
BVEC iterators reference pre-registered pinned pages for zero-copy, so
the cap for copy_from_user limits doesn't apply. I traced the consumer
paths (generic_perform_write, tcp_sendmsg, udp_sendmsg, TUN/TAP, block
layer, etc.) and they all handle large counts safely via their own size
limits or chunked processing.
I'll withdraw this patch. Thanks for pushing me to actually understand
the code rather than surface-level pattern matching.
Best,
Jiangshan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-29 2:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-28 6:42 [PATCH] net: datagram: fix integer overflow in zerocopy_sg_from_iter Jiangshan Yi
2026-07-28 15:30 ` Joe Damato
2026-07-29 2:15 ` Jiangshan Yi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome