Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp1433903pxt; Sat, 7 Aug 2021 10:31:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJQy8QSdCXz44QsM+2gZCgY/y3pplup+xxqsltUVmxNAqfdsHvfCq1l7+cNND71zRnN0a8 X-Received: by 2002:a17:907:20e7:: with SMTP id rh7mr337011ejb.390.1628357490765; Sat, 07 Aug 2021 10:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628357490; cv=none; d=google.com; s=arc-20160816; b=lxYiHPBYXHAS4lIkkcmgMp8mO3Sz1YUDjePfmHOFPHRUvnLpirlSuGwRM0uWTpbamJ HNe4PwP3uyD798jN24c5ljw0ykU0IsaxDRnZM/hIRHfo5U7pPRkzejNL7wyyN6LZQYrZ ruePiGxY6HTTASrhfXsgv4dspE2hrRB08V8zyBGgS+3zJHCSz3a77OF6DDg93G46x3Ab 5X5lyFK1Zxnr7S2CA8qAezaKE6fjyVtvF3HDLyEgjSNOEphaNMbotfWIMGgZb68/uXrg VTh9wugMzsyeZFr8mFt2+KQvBxb49Qi79sg4I3L0yfts7anMZL7Tl8YUtgYH49DadKLQ GRlg== 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 :message-id:date:subject:cc:to:from; bh=tk7D7yRFD7JBk/UPqkdK9fcTXlzBuKyExeOBSt3cupw=; b=F4mSJd3IQlegDJIxddyECZU0xH63t0oZaEAYIfm/NqaZLittW2WuEioeQne6GXEUYw l6hxcepVITr+9BCKDk00i2e8a8JkKf0BDTG/V3HxxFWryyxrbQkOnAoUqfH6txUQhjRY 1X7SGyGFOCiKgACeQPNTwHMNmiVtPzLQ+euxJd7CENkDg9oOchDJVT2Y1DUHmAXqrwzn GMXmYkL8EHa2a1E2LYaFDMbCNcuPqNaBJ54y+EbHm+FdfFTUXn/k/r1ZzuOP+G1JD+3I aCNEfb4hxhiI8vFS+DyoTY7tAkgY2LKMMyAPOTd8qoy0opd/sKvWZ0Hnj7D5fLObE56A I31Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id zh15si12659349ejb.57.2021.08.07.10.31.05; Sat, 07 Aug 2021 10:31:30 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229697AbhHGRVJ (ORCPT + 99 others); Sat, 7 Aug 2021 13:21:09 -0400 Received: from out30-57.freemail.mail.aliyun.com ([115.124.30.57]:51811 "EHLO out30-57.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbhHGRVH (ORCPT ); Sat, 7 Aug 2021 13:21:07 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04423;MF=wenyang@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0UiEXaLV_1628356842; Received: from localhost(mailfrom:wenyang@linux.alibaba.com fp:SMTPD_---0UiEXaLV_1628356842) by smtp.aliyun-inc.com(127.0.0.1); Sun, 08 Aug 2021 01:20:47 +0800 From: Wen Yang To: davem@davemloft.net, David Ahern , Jakub Kicinski , Hideaki YOSHIFUJI Cc: Wen Yang , Baoyou Xie , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] ipv4: return early for possible invalid uaddr Date: Sun, 8 Aug 2021 01:19:38 +0800 Message-Id: <20210807171938.38501-1-wenyang@linux.alibaba.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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, and also switch from a mix of tabs and spaces to just tabs. Signed-off-by: Wen Yang Cc: Baoyou Xie Cc: "David S. Miller" Cc: Hideaki YOSHIFUJI Cc: David Ahern Cc: Jakub Kicinski Cc: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- net/ipv4/af_inet.c | 27 ++++++++++++++++----------- net/ipv4/datagram.c | 7 ------- net/ipv4/udp.c | 7 ------- 3 files changed, 16 insertions(+), 25 deletions(-) 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; + 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) @@ -1136,23 +1141,23 @@ static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned lon .prot = &udp_prot, .ops = &inet_dgram_ops, .flags = INET_PROTOSW_PERMANENT, - }, + }, - { + { .type = SOCK_DGRAM, .protocol = IPPROTO_ICMP, .prot = &ping_prot, .ops = &inet_sockraw_ops, .flags = INET_PROTOSW_REUSE, - }, - - { - .type = SOCK_RAW, - .protocol = IPPROTO_IP, /* wild card */ - .prot = &raw_prot, - .ops = &inet_sockraw_ops, - .flags = INET_PROTOSW_REUSE, - } + }, + + { + .type = SOCK_RAW, + .protocol = IPPROTO_IP, /* wild card */ + .prot = &raw_prot, + .ops = &inet_sockraw_ops, + .flags = INET_PROTOSW_REUSE, + } }; #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array) diff --git a/net/ipv4/datagram.c b/net/ipv4/datagram.c index 4a8550c..81aae1d 100644 --- a/net/ipv4/datagram.c +++ b/net/ipv4/datagram.c @@ -27,13 +27,6 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len int oif; int err; - - if (addr_len < sizeof(*usin)) - return -EINVAL; - - if (usin->sin_family != AF_INET) - return -EAFNOSUPPORT; - sk_dst_reset(sk); oif = sk->sk_bound_dev_if; diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 62cd4cd..1ef0770 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1928,13 +1928,6 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) { - /* This check is replicated from __ip4_datagram_connect() and - * intended to prevent BPF program called below from accessing bytes - * that are out of the bound specified by user in addr_len. - */ - if (addr_len < sizeof(struct sockaddr_in)) - return -EINVAL; - return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); } EXPORT_SYMBOL(udp_pre_connect); -- 1.8.3.1