Skip to content

Commit 943d9d6

Browse files
committed
udp: Fix multiple wraparounds of sk->sk_rmem_alloc.
jira LE-4076 Rebuild_History Non-Buildable kernel-6.12.0-55.30.1.el10_0 commit-author Kuniyuki Iwashima <kuniyu@amazon.com> commit 5a465a0 __udp_enqueue_schedule_skb() has the following condition: if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) goto drop; sk->sk_rcvbuf is initialised by net.core.rmem_default and later can be configured by SO_RCVBUF, which is limited by net.core.rmem_max, or SO_RCVBUFFORCE. If we set INT_MAX to sk->sk_rcvbuf, the condition is always false as sk->sk_rmem_alloc is also signed int. Then, the size of the incoming skb is added to sk->sk_rmem_alloc unconditionally. This results in integer overflow (possibly multiple times) on sk->sk_rmem_alloc and allows a single socket to have skb up to net.core.udp_mem[1]. For example, if we set a large value to udp_mem[1] and INT_MAX to sk->sk_rcvbuf and flood packets to the socket, we can see multiple overflows: # cat /proc/net/sockstat | grep UDP: UDP: inuse 3 mem 7956736 <-- (7956736 << 12) bytes > INT_MAX * 15 ^- PAGE_SHIFT # ss -uam State Recv-Q ... UNCONN -1757018048 ... <-- flipping the sign repeatedly skmem:(r2537949248,rb2147483646,t0,tb212992,f1984,w0,o0,bl0,d0) Previously, we had a boundary check for INT_MAX, which was removed by commit 6a1f12d ("udp: relax atomic operation on sk->sk_rmem_alloc"). A complete fix would be to revert it and cap the right operand by INT_MAX: rmem = atomic_add_return(size, &sk->sk_rmem_alloc); if (rmem > min(size + (unsigned int)sk->sk_rcvbuf, INT_MAX)) goto uncharge_drop; but we do not want to add the expensive atomic_add_return() back just for the corner case. Casting rmem to unsigned int prevents multiple wraparounds, but we still allow a single wraparound. # cat /proc/net/sockstat | grep UDP: UDP: inuse 3 mem 524288 <-- (INT_MAX + 1) >> 12 # ss -uam State Recv-Q ... UNCONN -2147482816 ... <-- INT_MAX + 831 bytes skmem:(r2147484480,rb2147483646,t0,tb212992,f3264,w0,o0,bl0,d14468947) So, let's define rmem and rcvbuf as unsigned int and check skb->truesize only when rcvbuf is large enough to lower the overflow possibility. Note that we still have a small chance to see overflow if multiple skbs to the same socket are processed on different core at the same time and each size does not exceed the limit but the total size does. Note also that we must ignore skb->truesize for a small buffer as explained in commit 363dc73 ("udp: be less conservative with sock rmem accounting"). Fixes: 6a1f12d ("udp: relax atomic operation on sk->sk_rmem_alloc") Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Reviewed-by: Willem de Bruijn <willemb@google.com> Link: https://patch.msgid.link/20250401184501.67377-2-kuniyu@amazon.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> (cherry picked from commit 5a465a0) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent eaf2f83 commit 943d9d6

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

net/ipv4/udp.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,17 +1514,25 @@ static int udp_rmem_schedule(struct sock *sk, int size)
15141514
int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
15151515
{
15161516
struct sk_buff_head *list = &sk->sk_receive_queue;
1517-
int rmem, err = -ENOMEM;
1517+
unsigned int rmem, rcvbuf;
15181518
spinlock_t *busy = NULL;
1519-
int size, rcvbuf;
1519+
int size, err = -ENOMEM;
15201520

1521-
/* Immediately drop when the receive queue is full.
1522-
* Always allow at least one packet.
1523-
*/
15241521
rmem = atomic_read(&sk->sk_rmem_alloc);
15251522
rcvbuf = READ_ONCE(sk->sk_rcvbuf);
1526-
if (rmem > rcvbuf)
1527-
goto drop;
1523+
size = skb->truesize;
1524+
1525+
/* Immediately drop when the receive queue is full.
1526+
* Cast to unsigned int performs the boundary check for INT_MAX.
1527+
*/
1528+
if (rmem + size > rcvbuf) {
1529+
if (rcvbuf > INT_MAX >> 1)
1530+
goto drop;
1531+
1532+
/* Always allow at least one packet for small buffer. */
1533+
if (rmem > rcvbuf)
1534+
goto drop;
1535+
}
15281536

15291537
/* Under mem pressure, it might be helpful to help udp_recvmsg()
15301538
* having linear skbs :
@@ -1534,10 +1542,10 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
15341542
*/
15351543
if (rmem > (rcvbuf >> 1)) {
15361544
skb_condense(skb);
1537-
1545+
size = skb->truesize;
15381546
busy = busylock_acquire(sk);
15391547
}
1540-
size = skb->truesize;
1548+
15411549
udp_set_dev_scratch(skb);
15421550

15431551
atomic_add(size, &sk->sk_rmem_alloc);

0 commit comments

Comments
 (0)