From: Konstantin Khorenko <khorenko@virtuozzo.com>
To: Konstantin Khorenko <khorenko@virtuozzo.com>
Cc: devel@openvz.org
Subject: [Devel] [PATCH vz7] ms/net: lock the socket in sock_gettstamp()
Date: Wed, 18 Nov 2026 22:04:36 +0100 [thread overview]
Message-ID: <20261118-b4-prep-khorenko-vstor-155948-sock-gettstamp-lock-v1-1-edadf747dbc6@virtuozzo.com> (raw)
From: Eric Dumazet <edumazet@google.com>
sk->sk_flags must only be changed while holding the socket lock,
because sock_set_flag() and sock_reset_flag() use non atomic
operations (__set_bit() and __clear_bit()).
sock_gettstamp() is one of the last places where a bit of sk->sk_flags
is changed from a syscall without owning the socket lock, through
sock_enable_timestamp(sk, SOCK_TIMESTAMP).
sk_set_memalloc() and sk_clear_memalloc() also change sk->sk_flags
without the socket lock, but their callers (nbd, iscsi_tcp, nvme-tcp,
sunrpc, wireguard) need a careful audit, this will be addressed in a
separate patch.
Jungwoo Lee and Wongi Lee reported an UDP socket use-after-free
caused by this bug: a SIOCGSTAMPNS_NEW ioctl racing with bind()
can cancel the SOCK_RCU_FREE bit that udp_lib_get_port() just set,
because both threads perform a read-modify-write on the same word.
CPU 0 (bind) CPU 1 (SIOCGSTAMPNS_NEW)
-------------------------------- ----------------------------
read sk_flags = F read sk_flags = F
compute F | BIT(SOCK_RCU_FREE) compute F | BIT(SOCK_TIMESTAMP)
store F | BIT(SOCK_RCU_FREE)
sk_add_node_rcu(sk, ...)
store F | BIT(SOCK_TIMESTAMP)
After the lost update, SOCK_RCU_FREE is clear while the socket is
visible to lockless UDP receive lookups. sk_destruct() then frees
the socket immediately instead of waiting for a RCU grace period,
while the receive path still holds a reference-less pointer to it:
BUG: KASAN: slab-use-after-free in ipv4_pktinfo_prepare+0x30/0x410
Read of size 8 at addr ffff888008806610 by task exploit/207
CPU: 0 UID: 1000 PID: 207 Comm: exploit Not tainted 6.12.95+ #1
ipv4_pktinfo_prepare+0x30/0x410
udp_queue_rcv_one_skb+0x51c/0x1180
udp_unicast_rcv_skb+0x109/0x350
ip_protocol_deliver_rcu+0x14b/0x310
ip_local_deliver_finish+0x29d/0x390
ip_local_deliver+0x24d/0x2a0
Only grab the socket lock when SOCK_TIMESTAMP has to be set,
to keep the common case lockless.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: Jungwoo Lee <jwlee2217@gmail.com>
Reported-by: Wongi Lee <qw3rtyp0@gmail.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/20260915043055.3441600-1-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
(cherry picked from commit 9ed55f3dbef4f4adfe65eb03b0c35c53229a8490)
Backport notes:
- sock_gettstamp() does not exist here, the change goes to
sock_get_timestamp() and sock_get_timestampns();
- compat_sock_get_timestamp() and compat_sock_get_timestampns() get
the same change;
- ax25_ioctl(), ipx_ioctl() and nr_ioctl() call the helpers without
the socket lock.
ms commit c7cbdbf29f48 ("net: rework SIOCGSTAMP ioctl handling") in
v5.2 folded the per-protocol SIOCGSTAMP(NS) handling and the compat
helpers into sock_gettstamp(). Here every protocol ioctl still calls
one of the four helpers. The compat ones in net/compat.c set
SOCK_TIMESTAMP on their own and are used by ATM and X25.
ax25_ioctl(), ipx_ioctl() and nr_ioctl() call the helpers under
lock_sock(). With the lock taken inside, the nested lock_sock() would
wait forever in __lock_sock() for the socket the task itself owns. None
of the three is enabled in the vz7 configs, so this only keeps the code
correct.
c7cbdbf29f48 removed these cases from ax25 and netrom, which call
sock_gettstamp() without the lock since then, and IPX was gone from ms
before that. All other protocols here call the helpers unlocked too.
The closest stable backport is 5.10.y commit 18899e2e4023:
https://git.kernel.org/stable/c/18899e2e4023369a8f7739c2255a59a9748d8d17
CVE-2026-98276
https://virtuozzo.atlassian.net/browse/VSTOR-155948
Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
CVE-2026-98276 fix for the vz7 kernel.
https://virtuozzo.atlassian.net/browse/VSTOR-155948
Assisted-by: LLM
---
net/ax25/af_ax25.c | 13 +++++--------
net/compat.c | 16 ++++++++++++++--
net/core/sock.c | 16 ++++++++++++++--
net/ipx/af_ipx.c | 6 +++---
net/netrom/af_netrom.c | 11 ++---------
5 files changed, 38 insertions(+), 24 deletions(-)
diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c
index 1cf964d43259a..57d2424b47ba5 100644
--- a/net/ax25/af_ax25.c
+++ b/net/ax25/af_ax25.c
@@ -1686,6 +1686,11 @@ static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
void __user *argp = (void __user *)arg;
int res = 0;
+ if (cmd == SIOCGSTAMP)
+ return sock_get_timestamp(sk, argp);
+ if (cmd == SIOCGSTAMPNS)
+ return sock_get_timestampns(sk, argp);
+
lock_sock(sk);
switch (cmd) {
case TIOCOUTQ: {
@@ -1708,14 +1713,6 @@ static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
break;
}
- case SIOCGSTAMP:
- res = sock_get_timestamp(sk, argp);
- break;
-
- case SIOCGSTAMPNS:
- res = sock_get_timestampns(sk, argp);
- break;
-
case SIOCAX25ADDUID: /* Add a uid to the uid/call map table */
case SIOCAX25DELUID: /* Delete a uid from the uid/call map table */
case SIOCAX25GETUID: {
diff --git a/net/compat.c b/net/compat.c
index 9a93420d96dea..991341faa88f1 100644
--- a/net/compat.c
+++ b/net/compat.c
@@ -461,8 +461,14 @@ int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
ctv = (struct compat_timeval __user *) userstamp;
err = -ENOENT;
- if (!sock_flag(sk, SOCK_TIMESTAMP))
+ /* sk->sk_flags must only be changed under the socket lock,
+ * because sock_set_flag() uses non atomic operations.
+ */
+ if (!sock_flag(sk, SOCK_TIMESTAMP)) {
+ lock_sock(sk);
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
+ release_sock(sk);
+ }
tv = ktime_to_timeval(sk->sk_stamp);
if (tv.tv_sec == -1)
return err;
@@ -489,8 +495,14 @@ int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *usersta
ctv = (struct compat_timespec __user *) userstamp;
err = -ENOENT;
- if (!sock_flag(sk, SOCK_TIMESTAMP))
+ /* sk->sk_flags must only be changed under the socket lock,
+ * because sock_set_flag() uses non atomic operations.
+ */
+ if (!sock_flag(sk, SOCK_TIMESTAMP)) {
+ lock_sock(sk);
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
+ release_sock(sk);
+ }
ts = ktime_to_timespec(sk->sk_stamp);
if (ts.tv_sec == -1)
return err;
diff --git a/net/core/sock.c b/net/core/sock.c
index 130ac5ae31072..bd7ed76d4359e 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2614,8 +2614,14 @@ EXPORT_SYMBOL(lock_sock_fast);
int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
{
struct timeval tv;
- if (!sock_flag(sk, SOCK_TIMESTAMP))
+ /* sk->sk_flags must only be changed under the socket lock,
+ * because sock_set_flag() uses non atomic operations.
+ */
+ if (!sock_flag(sk, SOCK_TIMESTAMP)) {
+ lock_sock(sk);
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
+ release_sock(sk);
+ }
tv = ktime_to_timeval(sk->sk_stamp);
if (tv.tv_sec == -1)
return -ENOENT;
@@ -2630,8 +2636,14 @@ EXPORT_SYMBOL(sock_get_timestamp);
int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
{
struct timespec ts;
- if (!sock_flag(sk, SOCK_TIMESTAMP))
+ /* sk->sk_flags must only be changed under the socket lock,
+ * because sock_set_flag() uses non atomic operations.
+ */
+ if (!sock_flag(sk, SOCK_TIMESTAMP)) {
+ lock_sock(sk);
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
+ release_sock(sk);
+ }
ts = ktime_to_timespec(sk->sk_stamp);
if (ts.tv_sec == -1)
return -ENOENT;
diff --git a/net/ipx/af_ipx.c b/net/ipx/af_ipx.c
index 7d6d51073cf89..6ea1fc25c0641 100644
--- a/net/ipx/af_ipx.c
+++ b/net/ipx/af_ipx.c
@@ -1849,6 +1849,9 @@ static int ipx_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
struct sock *sk = sock->sk;
void __user *argp = (void __user *)arg;
+ if (cmd == SIOCGSTAMP)
+ return sock_get_timestamp(sk, argp);
+
lock_sock(sk);
switch (cmd) {
case TIOCOUTQ:
@@ -1895,9 +1898,6 @@ static int ipx_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
rc = get_user(ipx_sk(sk)->ipx_ncp_conn,
(const unsigned short __user *)argp);
break;
- case SIOCGSTAMP:
- rc = sock_get_timestamp(sk, argp);
- break;
case SIOCGIFDSTADDR:
case SIOCSIFDSTADDR:
case SIOCGIFBRDADDR:
diff --git a/net/netrom/af_netrom.c b/net/netrom/af_netrom.c
index a3322646a6674..9045640b56914 100644
--- a/net/netrom/af_netrom.c
+++ b/net/netrom/af_netrom.c
@@ -1193,7 +1193,6 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
struct sock *sk = sock->sk;
void __user *argp = (void __user *)arg;
- int ret;
switch (cmd) {
case TIOCOUTQ: {
@@ -1220,16 +1219,10 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
}
case SIOCGSTAMP:
- lock_sock(sk);
- ret = sock_get_timestamp(sk, argp);
- release_sock(sk);
- return ret;
+ return sock_get_timestamp(sk, argp);
case SIOCGSTAMPNS:
- lock_sock(sk);
- ret = sock_get_timestampns(sk, argp);
- release_sock(sk);
- return ret;
+ return sock_get_timestampns(sk, argp);
case SIOCGIFADDR:
case SIOCSIFADDR:
---
base-commit: 78c10c447efc7633a8e4260fe7dc33e1cb4bb4d2
change-id: 20261118-b4-prep-khorenko-vstor-155948-sock-gettstamp-lock-9e43db5fd8f0
Best regards,
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
next reply other threads:[~2026-11-18 21:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 21:04 Konstantin Khorenko [this message]
2026-11-18 21:17 ` Konstantin Khorenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118-b4-prep-khorenko-vstor-155948-sock-gettstamp-lock-v1-1-edadf747dbc6@virtuozzo.com \
--to=khorenko@virtuozzo.com \
--cc=devel@openvz.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox