Skip to contentSkip to conversation

af_unix: Hold receive queue lock in ioctl(SIOCATMARK)

PATCH In review Ren Wei 3 participants 3 messages Apr 10 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/1 af_unix: Hold receive queue lock in ioctl(SIOCATMARK)

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->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
index b23c33df8b46..54f12d5cda37 100644
@@ -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
#2 lore raw
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->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 +2 -0
index b23c33df8b46..54f12d5cda37 100644
@@ -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
index 5d4baac499af8d0a9cc36f652acf129a2e3619b2..1c3b80b25ef8b37d8e6553066fb7da20d81294a5

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 and
next_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);
#3 lore raw
In reply to Ren Wei

From: 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->iolock does 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.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 +0 -0
index b23c33df8b46..54f12d5cda37 100644

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
index eebabf0bd850..868b26c963ab 100644
@@ -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<---