Skip to content

Commit 7d2a894

Browse files
Rémi Denis-CourmontPaolo Abeni
authored andcommitted
phonet/pep: fix racy skb_queue_empty() use
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]>
1 parent 3b2d9bc commit 7d2a894

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
@@ -917,6 +917,37 @@ static int pep_sock_enable(struct sock *sk, struct sockaddr *addr, int len)
917917
return 0;
918918
}
919919

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

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

0 commit comments

Comments
 (0)