Skip to contentSkip to conversation

af_unix: return 0 for SIOCATMARK on listeners

PATCH In review Dickson Wu 2 participants 2 messages Nov 27 – Nov 29

Subsystems (3)
lore
Share & tools
lore

Dickson Wu

author
#0 lore raw

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.

Fixes e400cfa38bb0 ("af_unix: Fix wrong ioctl(SIOCATMARK) when consumed OOB skb is at the head.")

---
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
index 91c1d6cad497..1ba12617d55a 100644
@@ -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
#1 lore raw
Quoted 20 lines · Dickson Wu

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.

Fixes e400cfa38bb0 ("af_unix: Fix wrong ioctl(SIOCATMARK) when consumed OOB skb is at the head.")

---
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/

Quoted 2 lines

net-next 387f48167305 ("af_unix: Remove MSG_OOB.") removes SIOCATMARK along with AF_UNIX OOB, so this is only for net and stable.

not worth a conflict.

pw-bot: rejected