af_unix: Hold receive queue lock in ioctl(SIOCATMARK)
Subsystems (3)
PATCH 1/1 af_unix: Hold receive queue lock in ioctl(SIOCATMARK)
In reply to a message not in the archiveFrom: Jiexun Wang <[email protected]>
unix_ioctl() peeks at the receive queue and may check both the head skb and its successor while deciding whether SIOCATMARK should report the mark. However, u->iolock does not stabilize receive-queue element lifetime. Queue teardown paths can purge or splice the queue under sk->sk_receive_queue.lock and free the skb while unix_ioctl() still uses it.
Take sk->sk_receive_queue.lock while inspecting the queue so the skb and next_skb stay alive for the whole decision.
--- net/unix/af_unix.c | 3 +++ 1 file changed, 3 insertions(+)
net/unix/af_unix.c +3 -0
@@ -3301,6 +3301,8 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)int answ =0 ;mutex_lock (&u->iolock); +/* The receive queue lock keeps skb and next_skb alive. */ +spin_lock (&sk->sk_receive_queue.lock); skb =skb_peek (&sk->sk_receive_queue);if (skb) {
@@ -3315,6 +3317,7 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) answ =1 ; } +spin_unlock (&sk->sk_receive_queue.lock);mutex_unlock (&u->iolock); err =put_user (answ, (int __user *)arg);
Signature · 1 line
2.34.1
Quoted 40 lines · Ren Wei
From: Jiexun Wang <[email protected]>
unix_ioctl()peeks at the receive queue and may check both the head skb and its successor while deciding whether SIOCATMARK should report the mark. However,u->iolockdoes not stabilize receive-queue element lifetime. Queue teardown paths can purge or splice the queue undersk->sk_receive_queue.lockand free the skb whileunix_ioctl()still uses it.Take
sk->sk_receive_queue.lockwhile inspecting the queue so the skb andnext_skbstay alive for the whole decision.Fixes 314001f0bf92 ("af_unix: Add OOB support")Reported-by Yifan Wu <[email protected]>Reported-by Juefei Pu <[email protected]>Co-developed-by Yuan Tan <[email protected]>Signed-off-by Yuan Tan <[email protected]>Suggested-by Xin Liu <[email protected]>Tested-by Ren Wei <[email protected]>Signed-off-by Jiexun Wang <[email protected]>Signed-off-by Ren Wei <[email protected]>--- net/unix/af_unix.c | 3 +++ 1 file changed, 3 insertions(+)net/unix/af_unix.c +2
-0@@ -3301,6 +3301,8 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)int answ =0 ;mutex_lock (&u->iolock); +/* The receive queue lock keeps skb and next_skb alive. */ +spin_lock (&sk->sk_receive_queue.lock); skb =skb_peek (&sk->sk_receive_queue);if (skb) {
net/unix/af_unix.c static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) · line 3323
Quoted 7 lines
net/unix/af_unix.c +1
-0@@ -3315,6 +3317,7 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) answ =1 ; } +spin_unlock (&sk->sk_receive_queue.lock);mutex_unlock (&u->iolock); err =put_user (answ, (int __user *)arg);
Ok, but I think we also should remove the READ_ONCE() now we have proper locking.
net/unix/af_unix.c +0 -0
100644
net/unix/af_unix.c +0 -0
@@ -3301,10 +3301,12 @@ static int unix_ioctl(struct socket *sock,
unsigned int cmd, unsigned long arg)
int answ = 0;
mutex_lock(&u->iolock);+ /* The receive queue lock keeps skb andnext_skb alive. */
+ spin_lock(&sk->sk_receive_queue.lock);
skb = skb_peek(&sk->sk_receive_queue);
if (skb) {- struct sk_buff *oob_skb = READ_ONCE(u->oob_skb);
+ struct sk_buff *oob_skb = u->oob_skb;
struct sk_buff *next_skb;
next_skb = skb_peek_next(skb,&sk->sk_receive_queue);
@@ -3314,7 +3316,7 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
(!oob_skb || next_skb == oob_skb)))
answ = 1;
}
-
+ spin_unlock(&sk->sk_receive_queue.lock);
mutex_unlock(&u->iolock);
err = put_user(answ, (int __user *)arg);Kuniyuki Iwashima
maintainerFrom: Ren Wei <[email protected]>
Date: Fri, 10 Apr 2026 14:31:57 +0800
Quoted 6 lines
From: Jiexun Wang <[email protected]>
unix_ioctl()peeks at the receive queue and may check both the head skb and its successor while deciding whether SIOCATMARK should report the mark. However,u->iolockdoes not stabilize receive-queue element lifetime. Queue teardown paths can purge or splice the queue under
Please be more specific here.
Quoted 24 lines
sk->sk_receive_queue.lockand free the skb whileunix_ioctl()still uses it.Take
sk->sk_receive_queue.lockwhile inspecting the queue so the skb andnext_skbstay alive for the whole decision.Fixes 314001f0bf92 ("af_unix: Add OOB support")Reported-by Yifan Wu <[email protected]>Reported-by Juefei Pu <[email protected]>Co-developed-by Yuan Tan <[email protected]>Signed-off-by Yuan Tan <[email protected]>Suggested-by Xin Liu <[email protected]>Tested-by Ren Wei <[email protected]>Signed-off-by Jiexun Wang <[email protected]>Signed-off-by Ren Wei <[email protected]>--- net/unix/af_unix.c | 3 +++ 1 file changed, 3 insertions(+)net/unix/af_unix.c +0
-0
net/unix/af_unix.c static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) · line 3305
Quoted 5 lines
net/unix/af_unix.c +2
-0@@ -3301,6 +3301,8 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)int answ =0 ;mutex_lock (&u->iolock); +/* The receive queue lock keeps skb and next_skb alive. */ +spin_lock (&sk->sk_receive_queue.lock);
I think this is not the correct fix.
SIOCATMARK is apparently for MSG_OOB,
$ man 3 sockatmark
and non SOCK_STREAM sockets do not support MSG_OOB and should not abuse SIOCATMARK.
---8<---
net/unix/af_unix.c +3 -0
@@ -3299,6 +3299,9 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)struct sk_buff *skb;int answ =0 ; +if (sk->sk_type !=SOCK_STREAM ) +return -EOPNOTSUPP ; +mutex_lock (&u->iolock); skb =skb_peek (&sk->sk_receive_queue);
---8<---