Skip to content

Commit

Permalink
phonet/pep: fix racy skb_queue_empty() use
Browse files Browse the repository at this point in the history
[ Upstream commit 7d2a894 ]

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.

Fixes: 9641458 ("Phonet: Pipe End Point for Phonet Pipes protocol")
Signed-off-by: Rémi Denis-Courmont <courmisch@gmail.com>
Link: https://lore.kernel.org/r/20240218081214.4806-2-remi@remlab.net
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
[Harshit: backport to 5.15.y, clean cherrypick from 6.1.y commit]
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Rémi Denis-Courmont authored and Greg Kroah-Hartman committed May 2, 2025
1 parent 6cc52df commit 7d3914a
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions net/phonet/pep.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,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, unsigned long arg)
{
struct pep_sock *pn = pep_sk(sk);
Expand All @@ -929,15 +960,7 @@ static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
break;
}

lock_sock(sk);
if (sock_flag(sk, SOCK_URGINLINE) &&
!skb_queue_empty(&pn->ctrlreq_queue))
answ = skb_peek(&pn->ctrlreq_queue)->len;
else if (!skb_queue_empty(&sk->sk_receive_queue))
answ = skb_peek(&sk->sk_receive_queue)->len;
else
answ = 0;
release_sock(sk);
answ = pep_first_packet_length(sk);
ret = put_user(answ, (int __user *)arg);
break;

Expand Down

0 comments on commit 7d3914a

Please sign in to comment.