af_unix: return 0 for SIOCATMARK on listeners
Subsystems (3)
PATCH 1/1 af_unix: return 0 for SIOCATMARK on listeners
AF_UNIX listeners cannot receive out-of-band data, but SIOCATMARK currently peeks at sk_receive_queue. That queue contains connection requests, and unix_accept() can dequeue and free its head under sk_receive_queue.lock without taking u->iolock. As a result, SIOCATMARK can read the freed skb.
Take u->iolock and the state lock in stream receive order. Return 0 for TCP_LISTEN, and keep both locks over the receive-queue inspection. This prevents a concurrent listen() transition between the state check and the queue access.
---
This is the listener case left after d119775f2bad ("af_unix: Reject SIOCATMARK on non-stream sockets"). The earlier queue-lock proposal for the same lifetime problem was redirected toward rejecting unsupported sockets:
https://lore.kernel.org/netdev/f6cbbc8da90e95584847b5ceb60aae830d1631c2.1775731983.git.wangjiexun2025@gmail.com/
net-next 387f48167305 ("af_unix: Remove MSG_OOB.") removes SIOCATMARK along with AF_UNIX OOB, so this is only for net and stable.
Tested under UML with KASAN as uid 65534 with no capabilities: one thread loops SIOCATMARK on a listener while others connect and accept. Stock 7.3.6 and a mainline snapshot hit a slab-use-after-free in unix_ioctl() (freed by unix_accept()) within 30-45 s, as does the patch reverted. With the patch, 300 s runs (about 24M ioctls and 36k accepts on 7.3.6) were clean. A second test races listen() against SIOCATMARK on the same socket: 20/20 clean. tools/testing/selftests/net/af_unix msg_oob passed 60/60. A reproducer is available if it helps.
This was found and the fix prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the analysis and the patch before signing off.
net/unix/af_unix.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/unix/af_unix.c +6 -0
@@ -3344,6 +3344,10 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)return -EOPNOTSUPP ;mutex_lock (&u->iolock); +unix_state_lock (sk); + +if (sk->sk_state ==TCP_LISTEN ) + goto out_unlock; skb =skb_peek (&sk->sk_receive_queue);if (skb) {
@@ -3358,6 +3362,8 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) answ =1 ; } +out_unlock: +unix_state_unlock (sk);mutex_unlock (&u->iolock); err =put_user (answ, (int __user *)arg);
Signature · 1 line
2.43.0
Kuniyuki Iwashima
maintainerQuoted 20 lines · Dickson Wu
AF_UNIXlisteners cannot receive out-of-band data, but SIOCATMARK currently peeks atsk_receive_queue. That queue contains connection requests, andunix_accept()can dequeue and free its head under sk_receive_queue.lock without takingu->iolock. As a result, SIOCATMARK can read the freed skb.Take
u->iolockand the state lock in stream receive order. Return 0 forTCP_LISTEN, and keep both locks over the receive-queue inspection. This prevents a concurrentlisten()transition between the state check and the queue access.Fixes e400cfa38bb0 ("af_unix: Fix wrong ioctl(SIOCATMARK) when consumed OOB skb is at the head.")Signed-off-by Dickson Wu <[email protected]>---
This is the listener case left afterd119775f2bad("af_unix: Reject SIOCATMARK on non-stream sockets"). The earlier queue-lock proposal for the same lifetime problem was redirected toward rejecting unsupported sockets:
https://lore.kernel.org/netdev/f6cbbc8da90e95584847b5ceb60aae830d1631c2.1775731983.git.wangjiexun2025@gmail.com/
Quoted 2 lines
net-next
387f48167305("af_unix: RemoveMSG_OOB.") removes SIOCATMARK along withAF_UNIXOOB, so this is only for net and stable.
not worth a conflict.
pw-bot: rejected