From patchwork Fri Mar 8 21:09:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guillaume Nault X-Patchwork-Id: 1053713 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44GKrd70zKz9s47 for ; Sat, 9 Mar 2019 08:09:53 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726452AbfCHVJw (ORCPT ); Fri, 8 Mar 2019 16:09:52 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:45639 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726258AbfCHVJv (ORCPT ); Fri, 8 Mar 2019 16:09:51 -0500 Received: by mail-wr1-f68.google.com with SMTP id o7so2695566wrp.12 for ; Fri, 08 Mar 2019 13:09:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=eGW1GFpy5Y1BhKdVcd0BANqC2x4EbZJjzDvbxxqAkWs=; b=ubC/rpLpN5SgxPYYR/f7+wK4S53QwtWMYYlqFmxrKg581R4N7JQ61StxMrE/K93XSK JpITyvHXvzOTYjODgBFCTItghOcurfikRW85tk8LRdsQp+tkvMVqCHW0yjx7OjkcxXLw S3/+vFQO7YrtnRNBbLEV7SNhNy8sAGpfqRvlZ1r6MDGmrkaKjOQLazs6ivGIr7l7FZN/ olY7eSxSoH359kXCz7sHa3z6awYK6Q9t8mkyf7TTjJN2Q/eT/WCpll02OlmROLNawfV0 6NvxORaT/NvTGU9x7pYGEYCtihkhbxhJ//0gKwCGXOVBjZ5rk7eF45GBHsFliECpERKU Skqg== X-Gm-Message-State: APjAAAXnnGbDRND+4KRroTKmxpk5Dnja0Po1mA5XgvI7yQBHZzEq8GRT WMZZpeGv/y3xZjta4lCGgkGqCo4TSh8= X-Google-Smtp-Source: APXvYqzlBtvjDNT6xdhc3noQbNEQ7NY8mOOmCYpaiTyw+zigLOumLr7SvVvN8IpVesVRBGccXGMFVA== X-Received: by 2002:adf:f011:: with SMTP id j17mr12634444wro.166.1552079389621; Fri, 08 Mar 2019 13:09:49 -0800 (PST) Received: from pc-2.home (2a01cb05850ddf00045dd60e6368f84b.ipv6.abo.wanadoo.fr. [2a01:cb05:850d:df00:45d:d60e:6368:f84b]) by smtp.gmail.com with ESMTPSA id d16sm8181228wrx.29.2019.03.08.13.09.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 13:09:49 -0800 (PST) Date: Fri, 8 Mar 2019 22:09:47 +0100 From: Guillaume Nault To: netdev@vger.kernel.org Cc: Eric Dumazet Subject: [PATCH net] tcp: handle inet_csk_reqsk_queue_add() failures Message-ID: <9b8502f9cec31c971e480ee2281f5cd7088b50df.1552077823.git.gnault@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Commit 7716682cc58e ("tcp/dccp: fix another race at listener dismantle") let inet_csk_reqsk_queue_add() fail, and adjusted {tcp,dccp}_check_req() accordingly. However, TFO and syncookies weren't modified, thus leaking allocated resources on error. Contrary to tcp_check_req(), in both syncookies and TFO cases, we need to drop the request socket. Also, since the child socket is created with inet_csk_clone_lock(), we have to unlock it and drop an extra reference (->sk_refcount is initially set to 2 and inet_csk_reqsk_queue_add() drops only one ref). For TFO, we also need to revert the work done by tcp_try_fastopen() (with reqsk_fastopen_remove()). Fixes: 7716682cc58e ("tcp/dccp: fix another race at listener dismantle") Signed-off-by: Guillaume Nault Signed-off-by: Eric Dumazet --- Note for stable backports: this patch relies on da8ab57863ed ("tcp/dccp: remove reqsk_put() from inet_child_forget()"), to prevent inet_child_forget() from dropping a reference from the request socket. Therefore, for trees older than 4.14, commit da8ab57863ed has to be backported before this patch. net/ipv4/syncookies.c | 7 ++++++- net/ipv4/tcp_input.c | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c index 606f868d9f3f..e531344611a0 100644 --- a/net/ipv4/syncookies.c +++ b/net/ipv4/syncookies.c @@ -216,7 +216,12 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb, refcount_set(&req->rsk_refcnt, 1); tcp_sk(child)->tsoffset = tsoff; sock_rps_save_rxhash(child, skb); - inet_csk_reqsk_queue_add(sk, req, child); + if (!inet_csk_reqsk_queue_add(sk, req, child)) { + bh_unlock_sock(child); + sock_put(child); + child = NULL; + reqsk_put(req); + } } else { reqsk_free(req); } diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 4eb0c8ca3c60..5def3c48870e 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -6498,7 +6498,13 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops, af_ops->send_synack(fastopen_sk, dst, &fl, req, &foc, TCP_SYNACK_FASTOPEN); /* Add the child socket directly into the accept queue */ - inet_csk_reqsk_queue_add(sk, req, fastopen_sk); + if (!inet_csk_reqsk_queue_add(sk, req, fastopen_sk)) { + reqsk_fastopen_remove(fastopen_sk, req, false); + bh_unlock_sock(fastopen_sk); + sock_put(fastopen_sk); + reqsk_put(req); + goto drop; + } sk->sk_data_ready(sk); bh_unlock_sock(fastopen_sk); sock_put(fastopen_sk);