Skip to contentSkip to conversation

af_unix: Clear stale u->oob_skb.

PATCH Superseded Kuniyuki Iwashima 2 participants 3 messages Apr 5, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 af_unix: Clear stale u->oob_skb.

syzkaller started to report deadlock of unix_gc_lock after commit 4090fa373f0e ("af_unix: Replace garbage collection algorithm."), but it just uncovers the bug that has been there since commit 314001f0bf92 ("af_unix: Add OOB support").

The repro basically does the following.

from socket import *
from array import array

c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.sendmsg([b'a'], [(SOL_SOCKET, SCM_RIGHTS, array("i", [c2.fileno()]))], MSG_OOB) c2.recv(1) # blocked as no normal data in recv queue

c2.close() # done async and unblock recv()
c1.close() # done async and trigger GC

A socket sends its file descriptor to itself as OOB data and tries to receive normal data, but finally recv() fails due to async close().

The problem here is wrong handling of OOB skb in manage_oob(). When recvmsg() is called without MSG_OOB, manage_oob() is called to check if the peeked skb is OOB skb. In such a case, manage_oob() pops it out of the receive queue but does not clear unix_sock(sk)->oob_skb. This is wrong in terms of uAPI.

Let's say we send "hello" with MSG_OOB, and "world" without MSG_OOB. The 'o' is handled as OOB data. When recv() is called twice without MSG_OOB, the OOB data should be lost.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM, 0)
c1.send(b'hello', MSG_OOB) # 'o' is OOB data

5

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'world')

5

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5) # OOB data is not received

b'hell'

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5) # OOB date is skippeed

b'world'

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5, MSG_OOB) # This should return an error

b'o'

In the same situation, TCP actually returns -EINVAL for the last recv().

Also, if we do not clear unix_sk(sk)->oob_skb, unix_poll() always set EPOLLPRI even though the data has passed through by previous recv().

To avoid these issues, we must clear unix_sk(sk)->oob_skb when dequeuing it from recv queue.

The reason why the old GC did not trigger the deadlock is because the old GC relied on the receive queue to detect the loop.

When it is triggered, the socket with OOB data is marked as GC candidate because file refcount == inflight count (1). However, after traversing all inflight sockets, the socket still has a positive inflight count (1), thus the socket is excluded from candidates. Then, the old GC lose the chance to garbage-collect the socket.

With the old GC, the repro continues to create true garbage that will never be freed nor detected by kmemleak as it's linked to the global inflight list. That's why we couldn't even notice the issue.

Fixes 314001f0bf92 ("af_unix: Add OOB support")
Reported-by [email protected]
---
 net/unix/af_unix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/unix/af_unix.c +3 -1
index 5b41e2321209..8f105cf535be 100644
@@ -2665,7 +2665,9 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
 				}
 			} else if (!(flags & MSG_PEEK)) {
 				skb_unlink(skb, &sk->sk_receive_queue);
-				consume_skb(skb);
+				WRITE_ONCE(u->oob_skb, NULL);
+				kfree_skb(skb);
+				kfree_skb(skb);
 				skb = skb_peek(&sk->sk_receive_queue);
 			}
 		}
Signature · 1 line
2.30.2
#1 lore raw
Quoted 79 lines · Kuniyuki Iwashima

syzkaller started to report deadlock of unix_gc_lock after commit 4090fa373f0e ("af_unix: Replace garbage collection algorithm."), but it just uncovers the bug that has been there since commit 314001f0bf92 ("af_unix: Add OOB support").

The repro basically does the following.

from socket import *
from array import array

c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.sendmsg([b'a'], [(SOL_SOCKET, SCM_RIGHTS, array("i", [c2.fileno()]))], MSG_OOB) c2.recv(1) # blocked as no normal data in recv queue

c2.close() # done async and unblock recv()
c1.close() # done async and trigger GC

A socket sends its file descriptor to itself as OOB data and tries to receive normal data, but finally recv() fails due to async close().

The problem here is wrong handling of OOB skb in manage_oob(). When recvmsg() is called without MSG_OOB, manage_oob() is called to check if the peeked skb is OOB skb. In such a case, manage_oob() pops it out of the receive queue but does not clear unix_sock(sk)->oob_skb. This is wrong in terms of uAPI.

Let's say we send "hello" with MSG_OOB, and "world" without MSG_OOB. The 'o' is handled as OOB data. When recv() is called twice without MSG_OOB, the OOB data should be lost.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM, 0)
c1.send(b'hello', MSG_OOB) # 'o' is OOB data

5

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'world')

5

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5) # OOB data is not received

b'hell'

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5) # OOB date is skippeed

b'world'

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5, MSG_OOB) # This should return an error

b'o'

In the same situation, TCP actually returns -EINVAL for the last recv().

Also, if we do not clear unix_sk(sk)->oob_skb, unix_poll() always set EPOLLPRI even though the data has passed through by previous recv().

To avoid these issues, we must clear unix_sk(sk)->oob_skb when dequeuing it from recv queue.

The reason why the old GC did not trigger the deadlock is because the old GC relied on the receive queue to detect the loop.

When it is triggered, the socket with OOB data is marked as GC candidate because file refcount == inflight count (1). However, after traversing all inflight sockets, the socket still has a positive inflight count (1), thus the socket is excluded from candidates. Then, the old GC lose the chance to garbage-collect the socket.

With the old GC, the repro continues to create true garbage that will never be freed nor detected by kmemleak as it's linked to the global inflight list. That's why we couldn't even notice the issue.

Fixes 314001f0bf92 ("af_unix: Add OOB support")
Reported-by [email protected]
---
 net/unix/af_unix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/unix/af_unix.c +0 -0
index 5b41e2321209..8f105cf535be 100644

net/unix/af_unix.c static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, · line 2669

Quoted 6 lines
net/unix/af_unix.c +2 -1
@@ -2665,7 +2665,9 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
                                }
                        } else if (!(flags & MSG_PEEK)) {
                                skb_unlink(skb, &sk->sk_receive_queue);
-                               consume_skb(skb);
+                               WRITE_ONCE(u->oob_skb, NULL);
+                               kfree_skb(skb);

I dunno, this duplicate kfree_skb() is quite unusual and would deserve a comment.

I would perhaps use

   if (!WARN_ON_ONCE(skb_unref(skb))
      kfree_skb(skb);
Quoted 7 lines
net/unix/af_unix.c +1 -1
+                               kfree_skb(skb);
                                skb = skb_peek(&sk->sk_receive_queue);
                        }
                }
--

2.30.2

#2 lore raw

From: Eric Dumazet <[email protected]>
Date: Fri, 5 Apr 2024 23:06:32 +0200

Quoted 90 lines · Kuniyuki Iwashima
Quoted 79 lines

syzkaller started to report deadlock of unix_gc_lock after commit 4090fa373f0e ("af_unix: Replace garbage collection algorithm."), but it just uncovers the bug that has been there since commit 314001f0bf92 ("af_unix: Add OOB support").

The repro basically does the following.

from socket import *
from array import array

c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.sendmsg([b'a'], [(SOL_SOCKET, SCM_RIGHTS, array("i", [c2.fileno()]))], MSG_OOB) c2.recv(1) # blocked as no normal data in recv queue

c2.close() # done async and unblock recv()
c1.close() # done async and trigger GC

A socket sends its file descriptor to itself as OOB data and tries to receive normal data, but finally recv() fails due to async close().

The problem here is wrong handling of OOB skb in manage_oob(). When recvmsg() is called without MSG_OOB, manage_oob() is called to check if the peeked skb is OOB skb. In such a case, manage_oob() pops it out of the receive queue but does not clear unix_sock(sk)->oob_skb. This is wrong in terms of uAPI.

Let's say we send "hello" with MSG_OOB, and "world" without MSG_OOB. The 'o' is handled as OOB data. When recv() is called twice without MSG_OOB, the OOB data should be lost.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM, 0)
c1.send(b'hello', MSG_OOB) # 'o' is OOB data

5

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'world')

5

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5) # OOB data is not received

b'hell'

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5) # OOB date is skippeed

b'world'

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5, MSG_OOB) # This should return an error

b'o'

In the same situation, TCP actually returns -EINVAL for the last recv().

Also, if we do not clear unix_sk(sk)->oob_skb, unix_poll() always set EPOLLPRI even though the data has passed through by previous recv().

To avoid these issues, we must clear unix_sk(sk)->oob_skb when dequeuing it from recv queue.

The reason why the old GC did not trigger the deadlock is because the old GC relied on the receive queue to detect the loop.

When it is triggered, the socket with OOB data is marked as GC candidate because file refcount == inflight count (1). However, after traversing all inflight sockets, the socket still has a positive inflight count (1), thus the socket is excluded from candidates. Then, the old GC lose the chance to garbage-collect the socket.

With the old GC, the repro continues to create true garbage that will never be freed nor detected by kmemleak as it's linked to the global inflight list. That's why we couldn't even notice the issue.

Fixes 314001f0bf92 ("af_unix: Add OOB support")
Reported-by [email protected]
---
 net/unix/af_unix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/unix/af_unix.c +0 -0
index 5b41e2321209..8f105cf535be 100644

net/unix/af_unix.c static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, · line 2669

Quoted 6 lines
net/unix/af_unix.c +2 -1
@@ -2665,7 +2665,9 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
                                }
                        } else if (!(flags & MSG_PEEK)) {
                                skb_unlink(skb, &sk->sk_receive_queue);
-                               consume_skb(skb);
+                               WRITE_ONCE(u->oob_skb, NULL);
+                               kfree_skb(skb);

I dunno, this duplicate kfree_skb() is quite unusual and would deserve a comment.

Quoted 5 lines

I would perhaps use

   if (!WARN_ON_ONCE(skb_unref(skb))
      kfree_skb(skb);

Ah, this is what I wanted..! Somehow I was wondering if I should use either kfree_skb() or refcount_dec() directly :S

Will post v2, thanks!