mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] phonet: take correct lock to peek at the RX queue
@ 2024-02-10 12:50 Rémi Denis-Courmont
  2024-02-10 12:50 ` [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use Rémi Denis-Courmont
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Rémi Denis-Courmont @ 2024-02-10 12:50 UTC (permalink / raw)
  To: courmisch, davem, edumazet, kuba, pabeni; +Cc: netdev, linux-kernel

From: Rémi Denis-Courmont <courmisch@gmail.com>

Reported-by: Luosili <rootlab@huawei.com>
Signed-off-by: Rémi Denis-Courmont <courmisch@gmail.com>
---
 net/phonet/datagram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/phonet/datagram.c b/net/phonet/datagram.c
index 3aa50dc7535b..976fe250b509 100644
--- a/net/phonet/datagram.c
+++ b/net/phonet/datagram.c
@@ -34,10 +34,10 @@ static int pn_ioctl(struct sock *sk, int cmd, int *karg)
 
 	switch (cmd) {
 	case SIOCINQ:
-		lock_sock(sk);
+		spin_lock_bh(&sk->sk_receive_queue.lock);
 		skb = skb_peek(&sk->sk_receive_queue);
 		*karg = skb ? skb->len : 0;
-		release_sock(sk);
+		spin_unlock_bh(&sk->sk_receive_queue.lock);
 		return 0;
 
 	case SIOCPNADDRESOURCE:
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use
  2024-02-10 12:50 [PATCH 1/2] phonet: take correct lock to peek at the RX queue Rémi Denis-Courmont
@ 2024-02-10 12:50 ` Rémi Denis-Courmont
  2024-02-13 12:14   ` Paolo Abeni
  2024-02-12  9:34 ` [PATCH 1/2] phonet: take correct lock to peek at the RX queue Eric Dumazet
  2024-02-13 12:12 ` Paolo Abeni
  2 siblings, 1 reply; 5+ messages in thread
From: Rémi Denis-Courmont @ 2024-02-10 12:50 UTC (permalink / raw)
  To: courmisch, davem, edumazet, kuba, pabeni; +Cc: netdev, linux-kernel

From: Rémi Denis-Courmont <courmisch@gmail.com>

The receive queues are protected by their respective spin-lock, not
the socket lock. This could lead to skb_peek() unexpectedly
returning NULL or a pointer to an already dequeued socket buffer.

Signed-off-by: Rémi Denis-Courmont <courmisch@gmail.com>
---
 net/phonet/pep.c | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/net/phonet/pep.c b/net/phonet/pep.c
index faba31f2eff2..3dd5f52bc1b5 100644
--- a/net/phonet/pep.c
+++ b/net/phonet/pep.c
@@ -917,6 +917,37 @@ static int pep_sock_enable(struct sock *sk, struct sockaddr *addr, int len)
 	return 0;
 }
 
+static unsigned int pep_first_packet_length(struct sock *sk)
+{
+	struct pep_sock *pn = pep_sk(sk);
+	struct sk_buff_head *q;
+	struct sk_buff *skb;
+	unsigned int len = 0;
+	bool found = false;
+
+	if (sock_flag(sk, SOCK_URGINLINE)) {
+		q = &pn->ctrlreq_queue;
+		spin_lock_bh(&q->lock);
+		skb = skb_peek(q);
+		if (skb) {
+			len = skb->len;
+			found = true;
+		}
+		spin_unlock_bh(&q->lock);
+	}
+
+	if (likely(!found)) {
+		q = &sk->sk_receive_queue;
+		spin_lock_bh(&q->lock);
+		skb = skb_peek(q);
+		if (skb)
+			len = skb->len;
+		spin_unlock_bh(&q->lock);
+	}
+
+	return len;
+}
+
 static int pep_ioctl(struct sock *sk, int cmd, int *karg)
 {
 	struct pep_sock *pn = pep_sk(sk);
@@ -929,15 +960,7 @@ static int pep_ioctl(struct sock *sk, int cmd, int *karg)
 			break;
 		}
 
-		lock_sock(sk);
-		if (sock_flag(sk, SOCK_URGINLINE) &&
-		    !skb_queue_empty(&pn->ctrlreq_queue))
-			*karg = skb_peek(&pn->ctrlreq_queue)->len;
-		else if (!skb_queue_empty(&sk->sk_receive_queue))
-			*karg = skb_peek(&sk->sk_receive_queue)->len;
-		else
-			*karg = 0;
-		release_sock(sk);
+		*karg = pep_first_packet_length(sk);
 		ret = 0;
 		break;
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] phonet: take correct lock to peek at the RX queue
  2024-02-10 12:50 [PATCH 1/2] phonet: take correct lock to peek at the RX queue Rémi Denis-Courmont
  2024-02-10 12:50 ` [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use Rémi Denis-Courmont
@ 2024-02-12  9:34 ` Eric Dumazet
  2024-02-13 12:12 ` Paolo Abeni
  2 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2024-02-12  9:34 UTC (permalink / raw)
  To: Rémi Denis-Courmont
  Cc: courmisch, davem, kuba, pabeni, netdev, linux-kernel

On Sat, Feb 10, 2024 at 1:50 PM Rémi Denis-Courmont <remi@remlab.net> wrote:
>
> From: Rémi Denis-Courmont <courmisch@gmail.com>
>
> Reported-by: Luosili <rootlab@huawei.com>
> Signed-off-by: Rémi Denis-Courmont <courmisch@gmail.com>
> ---
>  net/phonet/datagram.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>

Fixes: 107d0d9b8d9a ("Phonet: Phonet datagram transport protocol")
Reviewed-by: Eric Dumazet <edumazet@google.com>

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] phonet: take correct lock to peek at the RX queue
  2024-02-10 12:50 [PATCH 1/2] phonet: take correct lock to peek at the RX queue Rémi Denis-Courmont
  2024-02-10 12:50 ` [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use Rémi Denis-Courmont
  2024-02-12  9:34 ` [PATCH 1/2] phonet: take correct lock to peek at the RX queue Eric Dumazet
@ 2024-02-13 12:12 ` Paolo Abeni
  2 siblings, 0 replies; 5+ messages in thread
From: Paolo Abeni @ 2024-02-13 12:12 UTC (permalink / raw)
  To: Rémi Denis-Courmont, courmisch, davem, edumazet, kuba
  Cc: netdev, linux-kernel

On Sat, 2024-02-10 at 14:50 +0200, Rémi Denis-Courmont wrote:
> From: Rémi Denis-Courmont <courmisch@gmail.com>
> 
> Reported-by: Luosili <rootlab@huawei.com>
> Signed-off-by: Rémi Denis-Courmont <courmisch@gmail.com>

Looks good, but you need to add a non empty commit message. Even
something quite similar to the one included in patch 2/2.

you can retain Eric's ack when post v2.

Pleas also include the correct fixes tag.

Thanks

Paolo


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use
  2024-02-10 12:50 ` [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use Rémi Denis-Courmont
@ 2024-02-13 12:14   ` Paolo Abeni
  0 siblings, 0 replies; 5+ messages in thread
From: Paolo Abeni @ 2024-02-13 12:14 UTC (permalink / raw)
  To: Rémi Denis-Courmont, courmisch, davem, edumazet, kuba
  Cc: netdev, linux-kernel

On Sat, 2024-02-10 at 14:50 +0200, Rémi Denis-Courmont wrote:
> From: Rémi Denis-Courmont <courmisch@gmail.com>
> 
> The receive queues are protected by their respective spin-lock, not
> the socket lock. This could lead to skb_peek() unexpectedly
> returning NULL or a pointer to an already dequeued socket buffer.
> 
> Signed-off-by: Rémi Denis-Courmont <courmisch@gmail.com>

Please provide a suitable fixes tag here.

Also include the target tree into the subj prefix when you post the v2.

Thank,

Paolo


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-13 12:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-10 12:50 [PATCH 1/2] phonet: take correct lock to peek at the RX queue Rémi Denis-Courmont
2024-02-10 12:50 ` [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use Rémi Denis-Courmont
2024-02-13 12:14   ` Paolo Abeni
2024-02-12  9:34 ` [PATCH 1/2] phonet: take correct lock to peek at the RX queue Eric Dumazet
2024-02-13 12:12 ` Paolo Abeni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®