From patchwork Wed Nov 23 17:38:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 13054042 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E429C3A59F for ; Wed, 23 Nov 2022 17:39:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239180AbiKWRj4 (ORCPT ); Wed, 23 Nov 2022 12:39:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238995AbiKWRjW (ORCPT ); Wed, 23 Nov 2022 12:39:22 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46BE69B39B for ; Wed, 23 Nov 2022 09:39:14 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id v7so13628707wmn.0 for ; Wed, 23 Nov 2022 09:39:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SmDkIrBVfUMbGOBltM7PoGg9g03wcI36X1ZwAu/jynQ=; b=LD4MRU/DCHliVq/YHIZWlJjin2QNdAQoGk6lgZc7akHPAmqjUFE+ut9mFMd5QKCTyf PJf2edhebPqnUqt4K8K299b4GrfwP7I5Copin0aWgc4XFCrjzWezRndf8zYDKvqNkCKw HdbrVFAo/kax408LxOrsyGVtsD9aBrZCeMyT3xf5JTdnUFpwUGLHhTCm8fpesn0qb5T/ N3xgajw6m/aydB1XrJQaiBdcYpFNlAw1O8ABAZeXPMQAoBFMV4pxPD6PnCV7qJ7xHEn0 Uj1z+rJP0hrEReE8EHSMJDn4spd8ohJxIRV08XYc0y6yrZ8jHJsjibrjIdViRC2PraV5 jiiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SmDkIrBVfUMbGOBltM7PoGg9g03wcI36X1ZwAu/jynQ=; b=y8dG94HcCqf+3ZTjpSIQqGKW5D84VbI/1AouuRzY+8QobpB145bt/1KTdYX60LAVj1 7NrrtMw/dhEuCMMNIyxEr0WDvJncP5iBsd7QRI8fqtRb/8DHtBROAWvSwZXal3zxL+eW ImIdpigS0u8ApkL2kWJQgFB5LoIvM0TNnoHxKYWoUomTYbYWN9XqM1Xe7weAvvNpYfU7 NIQVRzIFzJbXkC0O7d/bcLpjm1w7MjfHOTTt17Bf5nFOY+jqA1d34cTbwJG2aObD/Bb/ dO77RzOcqWQ1801x958ReTyxtyQ/f6NearHBiu9OK/opsyfPnO+nYM3JDihftWzlG04U TKYQ== X-Gm-Message-State: ANoB5pm3+Kt3GeYX9diiFbnyaPXRHcx69YOTYf1ytMNKuopkBhp2yyFd JlZdT2mj9j+EqWGuTfogmtIw7A== X-Google-Smtp-Source: AA0mqf7ymQbNbUEzGQzIwWAlN38NB9tz8tam6wmSGZCHApcPTqMq/u7sSBgUVZX6mslnPfL8DK6IeA== X-Received: by 2002:a05:600c:2296:b0:3cf:baa6:8ca5 with SMTP id 22-20020a05600c229600b003cfbaa68ca5mr7025631wmf.178.1669225152774; Wed, 23 Nov 2022 09:39:12 -0800 (PST) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id v10-20020adfe28a000000b0023647841c5bsm17464636wri.60.2022.11.23.09.39.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Nov 2022 09:39:12 -0800 (PST) From: Dmitry Safonov To: linux-kernel@vger.kernel.org, David Ahern , Eric Dumazet , Peter Zijlstra Cc: Dmitry Safonov , Ard Biesheuvel , Bob Gilligan , "David S. Miller" , Dmitry Safonov <0x7f454c46@gmail.com>, Francesco Ruggeri , Hideaki YOSHIFUJI , Jakub Kicinski , Jason Baron , Josh Poimboeuf , Paolo Abeni , Salam Noureddine , Steven Rostedt , netdev@vger.kernel.org Subject: [PATCH v6 4/5] net/tcp: Do cleanup on tcp_md5_key_copy() failure Date: Wed, 23 Nov 2022 17:38:58 +0000 Message-Id: <20221123173859.473629-5-dima@arista.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221123173859.473629-1-dima@arista.com> References: <20221123173859.473629-1-dima@arista.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org If the kernel was short on (atomic) memory and failed to allocate it - don't proceed to creation of request socket. Otherwise the socket would be unsigned and userspace likely doesn't expect that the TCP is not MD5-signed anymore. Signed-off-by: Dmitry Safonov Acked-by: Jakub Kicinski Reviewed-by: Eric Dumazet --- net/ipv4/tcp_ipv4.c | 9 ++------- net/ipv6/tcp_ipv6.c | 15 ++++++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 2ae6a061f36e..e214098087fe 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1630,13 +1630,8 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, addr = (union tcp_md5_addr *)&newinet->inet_daddr; key = tcp_md5_do_lookup(sk, l3index, addr, AF_INET); if (key) { - /* - * We're using one, so create a matching key - * on the newsk structure. If we fail to get - * memory, then we end up not copying the key - * across. Shucks. - */ - tcp_md5_key_copy(newsk, addr, AF_INET, 32, l3index, key); + if (tcp_md5_key_copy(newsk, addr, AF_INET, 32, l3index, key)) + goto put_and_exit; sk_gso_disable(newsk); } #endif diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 83304d6a6bd0..21486b4a9774 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1376,13 +1376,14 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * /* Copy over the MD5 key from the original socket */ key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr, l3index); if (key) { - /* We're using one, so create a matching key - * on the newsk structure. If we fail to get - * memory, then we end up not copying the key - * across. Shucks. - */ - tcp_md5_key_copy(newsk, (union tcp_md5_addr *)&newsk->sk_v6_daddr, - AF_INET6, 128, l3index, key); + const union tcp_md5_addr *addr; + + addr = (union tcp_md5_addr *)&newsk->sk_v6_daddr; + if (tcp_md5_key_copy(newsk, addr, AF_INET6, 128, l3index, key)) { + inet_csk_prepare_forced_close(newsk); + tcp_done(newsk); + goto out; + } } #endif