Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b357a364 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller
Browse files

inet: fix possible panic in reqsk_queue_unlink()



[ 3897.923145] BUG: unable to handle kernel NULL pointer dereference at
 0000000000000080
[ 3897.931025] IP: [<ffffffffa9f27686>] reqsk_timer_handler+0x1a6/0x243

There is a race when reqsk_timer_handler() and tcp_check_req() call
inet_csk_reqsk_queue_unlink() on the same req at the same time.

Before commit fa76ce73 ("inet: get rid of central tcp/dccp listener
timer"), listener spinlock was held and race could not happen.

To solve this bug, we change reqsk_queue_unlink() to not assume req
must be found, and we return a status, to conditionally release a
refcount on the request sock.

This also means tcp_check_req() in non fastopen case might or not
consume req refcount, so tcp_v6_hnd_req() & tcp_v4_hnd_req() have
to properly handle this.

(Same remark for dccp_check_req() and its callers)

inet_csk_reqsk_queue_drop() is now too big to be inlined, as it is
called 4 times in tcp and 3 times in dccp.

Fixes: fa76ce73 ("inet: get rid of central tcp/dccp listener timer")
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reported-by: default avatarYuchung Cheng <ycheng@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1d8dc3d3
Loading
Loading
Loading
Loading
+1 −19
Original line number Diff line number Diff line
@@ -279,12 +279,6 @@ static inline void inet_csk_reqsk_queue_add(struct sock *sk,
void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
				   unsigned long timeout);

static inline void inet_csk_reqsk_queue_removed(struct sock *sk,
						struct request_sock *req)
{
	reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
}

static inline void inet_csk_reqsk_queue_added(struct sock *sk,
					      const unsigned long timeout)
{
@@ -306,19 +300,7 @@ static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk)
	return reqsk_queue_is_full(&inet_csk(sk)->icsk_accept_queue);
}

static inline void inet_csk_reqsk_queue_unlink(struct sock *sk,
					       struct request_sock *req)
{
	reqsk_queue_unlink(&inet_csk(sk)->icsk_accept_queue, req);
}

static inline void inet_csk_reqsk_queue_drop(struct sock *sk,
					     struct request_sock *req)
{
	inet_csk_reqsk_queue_unlink(sk, req);
	inet_csk_reqsk_queue_removed(sk, req);
	reqsk_put(req);
}
void inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req);

void inet_csk_destroy_sock(struct sock *sk);
void inet_csk_prepare_forced_close(struct sock *sk);
+0 −18
Original line number Diff line number Diff line
@@ -212,24 +212,6 @@ static inline int reqsk_queue_empty(struct request_sock_queue *queue)
	return queue->rskq_accept_head == NULL;
}

static inline void reqsk_queue_unlink(struct request_sock_queue *queue,
				      struct request_sock *req)
{
	struct listen_sock *lopt = queue->listen_opt;
	struct request_sock **prev;

	spin_lock(&queue->syn_wait_lock);

	prev = &lopt->syn_table[req->rsk_hash];
	while (*prev != req)
		prev = &(*prev)->dl_next;
	*prev = req->dl_next;

	spin_unlock(&queue->syn_wait_lock);
	if (del_timer(&req->rsk_timer))
		reqsk_put(req);
}

static inline void reqsk_queue_add(struct request_sock_queue *queue,
				   struct request_sock *req,
				   struct sock *parent,
+2 −1
Original line number Diff line number Diff line
@@ -453,6 +453,7 @@ static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
						       iph->saddr, iph->daddr);
	if (req) {
		nsk = dccp_check_req(sk, skb, req);
		if (!nsk)
			reqsk_put(req);
		return nsk;
	}
+2 −1
Original line number Diff line number Diff line
@@ -301,6 +301,7 @@ static struct sock *dccp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
				   &iph->daddr, inet6_iif(skb));
	if (req) {
		nsk = dccp_check_req(sk, skb, req);
		if (!nsk)
			reqsk_put(req);
		return nsk;
	}
+1 −2
Original line number Diff line number Diff line
@@ -186,8 +186,7 @@ struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
	if (child == NULL)
		goto listen_overflow;

	inet_csk_reqsk_queue_unlink(sk, req);
	inet_csk_reqsk_queue_removed(sk, req);
	inet_csk_reqsk_queue_drop(sk, req);
	inet_csk_reqsk_queue_add(sk, req, child);
out:
	return child;
Loading