Skip to content

Commit 4c81ea9

Browse files
committed
net: annotate data-races around sk->sk_forward_alloc
jira VULN-65244 cve-pre CVE-2025-22058 commit-author Eric Dumazet <edumazet@google.com> commit 5e6300e Every time sk->sk_forward_alloc is read locklessly, add a READ_ONCE(). Add sk_forward_alloc_add() helper to centralize updates, to reduce number of WRITE_ONCE(). Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> (cherry picked from commit 5e6300e) Signed-off-by: Brett Mastbergen <bmastbergen@ciq.com>
1 parent 9e166df commit 4c81ea9

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

include/net/sock.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,12 @@ static inline void sk_wmem_queued_add(struct sock *sk, int val)
10331033
WRITE_ONCE(sk->sk_wmem_queued, sk->sk_wmem_queued + val);
10341034
}
10351035

1036+
static inline void sk_forward_alloc_add(struct sock *sk, int val)
1037+
{
1038+
/* Paired with lockless reads of sk->sk_forward_alloc */
1039+
WRITE_ONCE(sk->sk_forward_alloc, sk->sk_forward_alloc + val);
1040+
}
1041+
10361042
void sk_stream_write_space(struct sock *sk);
10371043

10381044
/* OOB backlog add */
@@ -1369,7 +1375,7 @@ static inline int sk_forward_alloc_get(const struct sock *sk)
13691375
if (sk->sk_prot->forward_alloc_get)
13701376
return sk->sk_prot->forward_alloc_get(sk);
13711377
#endif
1372-
return sk->sk_forward_alloc;
1378+
return READ_ONCE(sk->sk_forward_alloc);
13731379
}
13741380

13751381
static inline bool __sk_stream_memory_free(const struct sock *sk, int wake)
@@ -1668,7 +1674,7 @@ static inline void sk_mem_charge(struct sock *sk, int size)
16681674
{
16691675
if (!sk_has_account(sk))
16701676
return;
1671-
sk->sk_forward_alloc -= size;
1677+
sk_forward_alloc_add(sk, -size);
16721678
}
16731679

16741680
/* the following macros control memory reclaiming in mptcp_rmem_uncharge()
@@ -1680,7 +1686,7 @@ static inline void sk_mem_uncharge(struct sock *sk, int size)
16801686
{
16811687
if (!sk_has_account(sk))
16821688
return;
1683-
sk->sk_forward_alloc += size;
1689+
sk_forward_alloc_add(sk, size);
16841690
sk_mem_reclaim(sk);
16851691
}
16861692

net/core/sock.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ static int sock_reserve_memory(struct sock *sk, int bytes)
10211021
mem_cgroup_uncharge_skmem(sk->sk_memcg, pages);
10221022
return -ENOMEM;
10231023
}
1024-
sk->sk_forward_alloc += pages << PAGE_SHIFT;
1024+
sk_forward_alloc_add(sk, pages << PAGE_SHIFT);
10251025

10261026
sk->sk_reserved_mem += pages << PAGE_SHIFT;
10271027

@@ -2974,10 +2974,10 @@ int __sk_mem_schedule(struct sock *sk, int size, int kind)
29742974
{
29752975
int ret, amt = sk_mem_pages(size);
29762976

2977-
sk->sk_forward_alloc += amt << PAGE_SHIFT;
2977+
sk_forward_alloc_add(sk, amt << PAGE_SHIFT);
29782978
ret = __sk_mem_raise_allocated(sk, size, amt, kind);
29792979
if (!ret)
2980-
sk->sk_forward_alloc -= amt << PAGE_SHIFT;
2980+
sk_forward_alloc_add(sk, -(amt << PAGE_SHIFT));
29812981
return ret;
29822982
}
29832983
EXPORT_SYMBOL(__sk_mem_schedule);
@@ -3010,7 +3010,7 @@ EXPORT_SYMBOL(__sk_mem_reduce_allocated);
30103010
void __sk_mem_reclaim(struct sock *sk, int amount)
30113011
{
30123012
amount >>= PAGE_SHIFT;
3013-
sk->sk_forward_alloc -= amount << PAGE_SHIFT;
3013+
sk_forward_alloc_add(sk, -(amount << PAGE_SHIFT));
30143014
__sk_mem_reduce_allocated(sk, amount);
30153015
}
30163016
EXPORT_SYMBOL(__sk_mem_reclaim);

net/ipv4/tcp_output.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3386,7 +3386,7 @@ void sk_forced_mem_schedule(struct sock *sk, int size)
33863386
if (delta <= 0)
33873387
return;
33883388
amt = sk_mem_pages(delta);
3389-
sk->sk_forward_alloc += amt << PAGE_SHIFT;
3389+
sk_forward_alloc_add(sk, amt << PAGE_SHIFT);
33903390
sk_memory_allocated_add(sk, amt);
33913391

33923392
if (mem_cgroup_sockets_enabled && sk->sk_memcg)

net/ipv4/udp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,9 +1462,9 @@ static void udp_rmem_release(struct sock *sk, int size, int partial,
14621462
spin_lock(&sk_queue->lock);
14631463

14641464

1465-
sk->sk_forward_alloc += size;
1465+
sk_forward_alloc_add(sk, size);
14661466
amt = (sk->sk_forward_alloc - partial) & ~(PAGE_SIZE - 1);
1467-
sk->sk_forward_alloc -= amt;
1467+
sk_forward_alloc_add(sk, -amt);
14681468

14691469
if (amt)
14701470
__sk_mem_reduce_allocated(sk, amt >> PAGE_SHIFT);
@@ -1570,7 +1570,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
15701570
sk->sk_forward_alloc += delta;
15711571
}
15721572

1573-
sk->sk_forward_alloc -= size;
1573+
sk_forward_alloc_add(sk, -size);
15741574

15751575
/* no need to setup a destructor, we will explicitly release the
15761576
* forward allocated memory on dequeue

net/mptcp/protocol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
17941794
}
17951795

17961796
/* data successfully copied into the write queue */
1797-
sk->sk_forward_alloc -= total_ts;
1797+
sk_forward_alloc_add(sk, -total_ts);
17981798
copied += psize;
17991799
dfrag->data_len += psize;
18001800
frag_truesize += psize;
@@ -3198,7 +3198,7 @@ void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
31983198
/* move all the rx fwd alloc into the sk_mem_reclaim_final in
31993199
* inet_sock_destruct() will dispose it
32003200
*/
3201-
sk->sk_forward_alloc += msk->rmem_fwd_alloc;
3201+
sk_forward_alloc_add(sk, msk->rmem_fwd_alloc);
32023202
msk->rmem_fwd_alloc = 0;
32033203
mptcp_token_destroy(msk);
32043204
mptcp_pm_free_anno_list(msk);
@@ -3479,7 +3479,7 @@ static void mptcp_shutdown(struct sock *sk, int how)
34793479

34803480
static int mptcp_forward_alloc_get(const struct sock *sk)
34813481
{
3482-
return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3482+
return READ_ONCE(sk->sk_forward_alloc) + mptcp_sk(sk)->rmem_fwd_alloc;
34833483
}
34843484

34853485
static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)

0 commit comments

Comments
 (0)