Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3240122pxt; Mon, 9 Aug 2021 21:43:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJypht/x0JJD/6ZvXO5Z0gZsIKDbvhJq4ZfyLIBriNpCSGXwUR+fkPs2yUBvXSM5SM5gSgMz X-Received: by 2002:a5d:938a:: with SMTP id c10mr199213iol.0.1628570605491; Mon, 09 Aug 2021 21:43:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628570605; cv=none; d=google.com; s=arc-20160816; b=yu3VnAyolr56tLkHN9Duv0dP9dImwwx6U+yRTHTJDsk7xBQYtiY+RzXitWTiPJninj DGkFnck+k7iaZ+VsAyO7iUNn2nRFUt15CmIjKLqMx9EUze3+6ol/IeMUmblCCVrp94CN G4S63COqFH9lve9w6tUcaBzxRbY8ovZfKMDM1CLTj1bjn5/5/ZWhCgn6VMKE8wyDLNNT 0FWKVmN8d9nF93Ln/V3EvZf2HC5ja/iBWylk/hwZg1cHuVXSP9rjy1JEA/zlngL41a48 kM04U1htcZhJVYUAzS0Emi/ihvXHKz+gN9osaTCmnBjawV3ljCTxdQTrEtxaTJWVNZtQ e1xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=uTXrH73w9jPk5scC3g+gjRDrp8YlbvCrndKMEMaWRSs=; b=LvyzPKGmPPogs2egue5dapOqGBXfrXziwI/Ik+FQf7XDFnReOlxO63YnMZyjuMDM9l B2O83tG83YyIsMMCawlWqaMVU7alInPIS5TRmq0CfjuGfXqXd2S2FacxExOXqPcTiJJb uK0QCkC1R1X7Q9+FCWAlucMe3wttGC/1uw0vrLql5UEu0+O4fbvHo0sB4tfFBHUTQc48 f5+AvjJntmf5IMZgZR4lhaGACnFm1RjYVq8872VH9dsuWXLPru5+kQ+yGWNCqD7I0cR1 AAdArMsTqyTKf/FzGTIPYzKg5b4dVXmChKMtBkueGWb5ky0tdTFZxlcRfmkdq3hqIgYv f30g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sv5JqZaf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z12si11926125ilb.144.2021.08.09.21.43.14; Mon, 09 Aug 2021 21:43:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sv5JqZaf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236753AbhHIWdO (ORCPT + 99 others); Mon, 9 Aug 2021 18:33:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:40214 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233675AbhHIWdN (ORCPT ); Mon, 9 Aug 2021 18:33:13 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 43CA060E76; Mon, 9 Aug 2021 22:32:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628548372; bh=xXqBnT2glCcN1LrdDZFL0noW64vg4x2wLurWXjIrCZE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=sv5JqZafThkJ55HJPd/tc0dS0kHCpZcjOB5csPwDFbxKT3EbeJ3/greDkvxkInLIm CkvQXJcCJcFs0Aqe0Lv+DpRDeE88Fzfwapi4+SQ/7hQ8im6gDsjyO6sZoVo2JrFv57 vQ4Es96hc15qyFulz+V0eZPDB+uD7pj2BZhB+CBA+rRPE7KkMabl8biU1Ms9d9ONkX FrXaJo9woe2hX50qcitsVAaZfi1sQzX8GreH5uRMeDB9sJhlLRvZRt4JUkU4AQwnH7 TSLTdxA8h8bV6WolT7ckn7105xtK7KWBBpXL5cgYhDXYpu1AfqM1NTWEAtyxaQB77O XJ2c2UP5XQ7fw== Date: Mon, 9 Aug 2021 15:32:51 -0700 From: Jakub Kicinski To: Wen Yang Cc: davem@davemloft.net, David Ahern , Hideaki YOSHIFUJI , Baoyou Xie , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ipv4: return early for possible invalid uaddr Message-ID: <20210809153251.4c51c3cd@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com> In-Reply-To: <20210807171938.38501-1-wenyang@linux.alibaba.com> References: <20210807171938.38501-1-wenyang@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 8 Aug 2021 01:19:38 +0800 Wen Yang wrote: > The inet_dgram_connect() first calls inet_autobind() to select an > ephemeral port, then checks uaddr in udp_pre_connect() or > __ip4_datagram_connect(), but the port is not released until the socket > is closed. > > We should return early for invalid uaddr to improve performance and > simplify the code a bit, The performance improvement would be if the benchmark is calling connect with invalid arguments? That seems like an extremely rare scenario in real life. > and also switch from a mix of tabs and spaces to just tabs. Please never mix unrelated whitespace cleanup into patches making real code changes. > diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c > index 5464818..97b6fc4 100644 > --- a/net/ipv4/af_inet.c > +++ b/net/ipv4/af_inet.c > @@ -569,6 +569,11 @@ int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr, > if (uaddr->sa_family == AF_UNSPEC) > return sk->sk_prot->disconnect(sk, flags); > > + if (uaddr->sa_family != AF_INET) > + return -EAFNOSUPPORT; And what about IPv6 which also calls this function? > + if (addr_len < sizeof(struct sockaddr_in)) > + return -EINVAL; > + > if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) { > err = sk->sk_prot->pre_connect(sk, uaddr, addr_len); > if (err)