unix_bpf: fix a potential deadlock in unix_dgram_bpf_recvmsg()
Subsystems (4)
PATCH 1/1 unix_bpf: fix a potential deadlock in unix_dgram_bpf_recvmsg()
From: Cong Wang <[email protected]>
As Eric noticed, __unix_dgram_recvmsg() may acquire u->iolock too, so we have to release it before calling this function.
--- net/unix/unix_bpf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/unix/unix_bpf.c +6 -5
@@ -53,8 +53,9 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,mutex_lock (&u->iolock);if (!skb_queue_empty (&sk->sk_receive_queue) &&sk_psock_queue_empty (psock)) { - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock); +sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } msg_bytes_ready:
@@ -68,13 +69,13 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,if (data) {if (!sk_psock_queue_empty (psock)) goto msg_bytes_ready; - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock); +sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } copied = -EAGAIN ; } ret = copied; -out:mutex_unlock (&u->iolock);sk_psock_put (sk, psock);return ret;
Signature · 1 line
2.27.0
John Fastabend
maintainerQuoted 21 lines · Cong Wang
From: Cong Wang <[email protected]>
As Eric noticed,
__unix_dgram_recvmsg()may acquireu->iolocktoo, so we have to release it before calling this function.Fixes 9825d866ce0d ("af_unix: Implement unix_dgram_bpf_recvmsg()")Reported-by Eric Dumazet <[email protected]>Cc John Fastabend <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Jakub Sitnicki <[email protected]>Cc Lorenz Bauer <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/unix/unix_bpf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)net/unix/unix_bpf.c +0
-0
net/unix/unix_bpf.c static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg, · line 59
Quoted 9 lines
net/unix/unix_bpf.c +3
-2@@ -53,8 +53,9 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,mutex_lock (&u->iolock);if (!skb_queue_empty (&sk->sk_receive_queue) &&sk_psock_queue_empty (psock)) { - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock); +sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); }
Is there a reason to grab the mutex_lock(u->iolock) above the skb_queue_emptyaand sk_psock_queue_empty checks?
Could it be move here just above the msg_bytes_ready label?
Quoted 22 lines
net/unix/unix_bpf.c +3
-3msg_bytes_ready:@@ -68,13 +69,13 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,if (data) {if (!sk_psock_queue_empty (psock)) goto msg_bytes_ready; - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock); +sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } copied = -EAGAIN ; } ret = copied; -out:mutex_unlock (&u->iolock);sk_psock_put (sk, psock);return ret;Signature · 1 line
2.27.0
Quoted 5 lines · John Fastabend
Is there a reason to grab the
mutex_lock(u->iolock)above theskb_queue_emptyaandsk_psock_queue_emptychecks?Could it be move here just above the
msg_bytes_readylabel?
The check of the receive queue is more accurate with lock.
Thanks.
Quoted 39 lines · Cong Wang
From: Cong Wang <[email protected]>
As Eric noticed,
__unix_dgram_recvmsg()may acquireu->iolocktoo, so we have to release it before calling this function.Fixes 9825d866ce0d ("af_unix: Implement unix_dgram_bpf_recvmsg()")Reported-by Eric Dumazet <[email protected]>Cc John Fastabend <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Jakub Sitnicki <[email protected]>Cc Lorenz Bauer <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/unix/unix_bpf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)net/unix/unix_bpf.c +4
-4@@ -53,8 +53,9 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,mutex_lock (&u->iolock);if (!skb_queue_empty (&sk->sk_receive_queue) &&sk_psock_queue_empty (psock)) { - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock); +sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } msg_bytes_ready:@@ -68,13 +69,13 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,if (data) {if (!sk_psock_queue_empty (psock)) goto msg_bytes_ready; - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock);
net/unix/unix_bpf.c static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg, · line 81
Quoted 10 lines
net/unix/unix_bpf.c +2
-1+sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } copied = -EAGAIN ; } ret = copied; -out:mutex_unlock (&u->iolock);sk_psock_put (sk, psock);return ret;
Nit: Can be just return copied. ret became useless.
Quoted 53 lines · Jakub Sitnicki
Quoted 39 lines · Cong Wang
From: Cong Wang <[email protected]>
As Eric noticed,
__unix_dgram_recvmsg()may acquireu->iolocktoo, so we have to release it before calling this function.Fixes 9825d866ce0d ("af_unix: Implement unix_dgram_bpf_recvmsg()")Reported-by Eric Dumazet <[email protected]>Cc John Fastabend <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Jakub Sitnicki <[email protected]>Cc Lorenz Bauer <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/unix/unix_bpf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)net/unix/unix_bpf.c +4
-4@@ -53,8 +53,9 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,mutex_lock (&u->iolock);if (!skb_queue_empty (&sk->sk_receive_queue) &&sk_psock_queue_empty (psock)) { - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock); +sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } msg_bytes_ready:@@ -68,13 +69,13 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,if (data) {if (!sk_psock_queue_empty (psock)) goto msg_bytes_ready; - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock);net/unix/unix_bpf.c static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg, · line 81
Quoted 10 lines
net/unix/unix_bpf.c +2
-1+sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } copied = -EAGAIN ; } ret = copied; -out:mutex_unlock (&u->iolock);sk_psock_put (sk, psock);return ret;Nit: Can be just
return copied.retbecame useless.
Quoted 1 line
Acked-by Jakub Sitnicki <[email protected]>
Worth doing the small cleanup pointed out by Jakub but feel free to add my ack.
Quoted 55 lines · John Fastabend
Quoted 53 lines · Jakub Sitnicki
Quoted 39 lines · Cong Wang
From: Cong Wang <[email protected]>
As Eric noticed,
__unix_dgram_recvmsg()may acquireu->iolocktoo, so we have to release it before calling this function.Fixes 9825d866ce0d ("af_unix: Implement unix_dgram_bpf_recvmsg()")Reported-by Eric Dumazet <[email protected]>Cc John Fastabend <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Jakub Sitnicki <[email protected]>Cc Lorenz Bauer <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/unix/unix_bpf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)net/unix/unix_bpf.c +4
-4@@ -53,8 +53,9 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,mutex_lock (&u->iolock);if (!skb_queue_empty (&sk->sk_receive_queue) &&sk_psock_queue_empty (psock)) { - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock); +sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } msg_bytes_ready:@@ -68,13 +69,13 @@ static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg,if (data) {if (!sk_psock_queue_empty (psock)) goto msg_bytes_ready; - ret =__unix_dgram_recvmsg (sk, msg, len, flags); - goto out; +mutex_unlock (&u->iolock);net/unix/unix_bpf.c static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg, · line 81
Quoted 10 lines
net/unix/unix_bpf.c +2
-1+sk_psock_put (sk, psock); +return __unix_dgram_recvmsg (sk, msg, len, flags); } copied = -EAGAIN ; } ret = copied; -out:mutex_unlock (&u->iolock);sk_psock_put (sk, psock);return ret;Nit: Can be just
return copied.retbecame useless.
Quoted 5 lines
Quoted 1 line
Acked-by Jakub Sitnicki <[email protected]>Worth doing the small cleanup pointed out by Jakub but feel free to add my ack.
I cleaned it up while applying. Applied to bpf-next, thanks.
Quoted 1 line
Acked-by John Fastabend <[email protected]>