Skip to content

Commit b8a23e8

Browse files
edumazetdavem330
authored andcommitted
caif: fix leaks and race in caif_queue_rcv_skb()
1) If sk_filter() is applied, skb was leaked (not freed) 2) Testing SOCK_DEAD twice is racy : packet could be freed while already queued. 3) Remove obsolete comment about caching skb->len Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e3426ca commit b8a23e8

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

‎net/caif/caif_socket.c‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,13 @@ static void caif_flow_ctrl(struct sock *sk, int mode)
121121
* Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
122122
* not dropped, but CAIF is sending flow off instead.
123123
*/
124-
static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
124+
static void caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
125125
{
126126
int err;
127127
unsigned long flags;
128128
struct sk_buff_head *list = &sk->sk_receive_queue;
129129
struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
130+
bool queued = false;
130131

131132
if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
132133
(unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
@@ -139,29 +140,25 @@ static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
139140

140141
err = sk_filter(sk, skb);
141142
if (err)
142-
return err;
143+
goto out;
144+
143145
if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
144146
set_rx_flow_off(cf_sk);
145147
net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
146148
caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
147149
}
148150
skb->dev = NULL;
149151
skb_set_owner_r(skb, sk);
150-
/* Cache the SKB length before we tack it onto the receive
151-
* queue. Once it is added it no longer belongs to us and
152-
* may be freed by other threads of control pulling packets
153-
* from the queue.
154-
*/
155152
spin_lock_irqsave(&list->lock, flags);
156-
if (!sock_flag(sk, SOCK_DEAD))
153+
queued = !sock_flag(sk, SOCK_DEAD);
154+
if (queued)
157155
__skb_queue_tail(list, skb);
158156
spin_unlock_irqrestore(&list->lock, flags);
159-
160-
if (!sock_flag(sk, SOCK_DEAD))
157+
out:
158+
if (queued)
161159
sk->sk_data_ready(sk);
162160
else
163161
kfree_skb(skb);
164-
return 0;
165162
}
166163

167164
/* Packet Receive Callback function called from CAIF Stack */

0 commit comments

Comments
 (0)