Skip to content

Commit b5e2008

Browse files
Rémi Denis-Courmontvijay-suman
authored andcommitted
phonet/pep: fix racy skb_queue_empty() use
[ 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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: Sasha Levin <[email protected]> [Harshit: backport to 5.15.y, clean cherrypick from 6.1.y commit] Signed-off-by: Harshit Mogalapalli <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 7d3914a477eed92b48c493a8631cc4554ab4fd4f) Signed-off-by: Vijayendra Suman <[email protected]>
1 parent c58a8f0 commit b5e2008

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

net/phonet/pep.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,37 @@ static int pep_sock_enable(struct sock *sk, struct sockaddr *addr, int len)
916916
return 0;
917917
}
918918

919+
static unsigned int pep_first_packet_length(struct sock *sk)
920+
{
921+
struct pep_sock *pn = pep_sk(sk);
922+
struct sk_buff_head *q;
923+
struct sk_buff *skb;
924+
unsigned int len = 0;
925+
bool found = false;
926+
927+
if (sock_flag(sk, SOCK_URGINLINE)) {
928+
q = &pn->ctrlreq_queue;
929+
spin_lock_bh(&q->lock);
930+
skb = skb_peek(q);
931+
if (skb) {
932+
len = skb->len;
933+
found = true;
934+
}
935+
spin_unlock_bh(&q->lock);
936+
}
937+
938+
if (likely(!found)) {
939+
q = &sk->sk_receive_queue;
940+
spin_lock_bh(&q->lock);
941+
skb = skb_peek(q);
942+
if (skb)
943+
len = skb->len;
944+
spin_unlock_bh(&q->lock);
945+
}
946+
947+
return len;
948+
}
949+
919950
static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
920951
{
921952
struct pep_sock *pn = pep_sk(sk);
@@ -929,15 +960,7 @@ static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
929960
break;
930961
}
931962

932-
lock_sock(sk);
933-
if (sock_flag(sk, SOCK_URGINLINE) &&
934-
!skb_queue_empty(&pn->ctrlreq_queue))
935-
answ = skb_peek(&pn->ctrlreq_queue)->len;
936-
else if (!skb_queue_empty(&sk->sk_receive_queue))
937-
answ = skb_peek(&sk->sk_receive_queue)->len;
938-
else
939-
answ = 0;
940-
release_sock(sk);
963+
answ = pep_first_packet_length(sk);
941964
ret = put_user(answ, (int __user *)arg);
942965
break;
943966

0 commit comments

Comments
 (0)