Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5991340ybc; Wed, 27 Nov 2019 13:02:29 -0800 (PST) X-Google-Smtp-Source: APXvYqwQslqFq2sZX5k/NmzgyRb1PXjxFeRyxzMyHw+4nQCDkb6QERmq+HaydXWDCIjlrALT2EK+ X-Received: by 2002:a17:906:e0d5:: with SMTP id gl21mr49736182ejb.292.1574888549542; Wed, 27 Nov 2019 13:02:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574888549; cv=none; d=google.com; s=arc-20160816; b=qVrtbyaCPLqVjlwbTJFpE1XX/CSq9isnYFdrsmI9CLljFn8BRWoSxzsL36dEf7TcQ8 hQJDtChXlHo/L3jeKJk7BJLuApX/xLoeKzhbDT/dvbOkvlKL3SL8h8W7ao7yUFu/0rRG /vucEfYDNDAIK35suEg+fxU19D0Jmf7Xcw8Nni3vlTiS2J0OJOc8OZGWAHAl+uJ3wIQc mQe60sx5/eF3rbbfOuqYLanvHx+IHaOqcJZ5TU7gbZA+29kWfSxZOMcA5E5YykZ942Ai GnVJ0xOO+NXQlGnnRiyP6cZyOoJxcWEdQ8LLqulVhb8BcZVJ9g+TXEz21iwiDIi/9vb9 XvAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=CajgHMpEZuc+sMo/QpPdG8MlHTxlQZByxdveC0GWZSU=; b=X9jdNmCJf6fNgr50VXWgwc3FnDQzmApTwsEf8anvFO19wl0FKCydAM+9T1giyjFmnh dX8FGl9QLMTx8uq5Pvb8Ayq/L2wzjZlaaO1CyCjePF/bIls1FrdxAVDuaStgRBMowNif mH+/rNLSi3IC6S5CQzguzzzTF//dwzmN9hm7TW0D7HYENDJLxZOUGjTlSu5sZOYw1N89 H7QjDSfUAz5oQv2t6eHY6yVFIz8UNaUjcIvgO8+i2yG2inDxH18XX/FbqA8StnyFsej/ 8lKVKL2PiyF8OMAUY+XebrsP8Gv6EVc8zP1beqZUbdBwWT/9ynVXtujsqmJyPoQpOkHg laag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qf3OgGxx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id sa8si4157204ejb.279.2019.11.27.13.02.05; Wed, 27 Nov 2019 13:02:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qf3OgGxx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727298AbfK0VBI (ORCPT + 99 others); Wed, 27 Nov 2019 16:01:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:53192 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731666AbfK0VBH (ORCPT ); Wed, 27 Nov 2019 16:01:07 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D89492158A; Wed, 27 Nov 2019 21:01:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574888466; bh=yyh2hufHdLJUI04XCdrrkkLGWjEaQEeLU+E5VS1K7/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qf3OgGxxX2a8ppRPdvcygIjsGstRUm8LW5EZbesl/5hhiBSE8gWSc8Nqt2Li4NzA8 mtmifeAXu+SQFAwrcTwcZu32pAhdrBkhab/+EmJmHGZoOpQI/7dtwlkl3RqQASPmIZ QrH3Xu6ZhyQKkwD8WG0hMZmqG8u/S4kE/CJZPEco= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "J. Bruce Fields" , Trond Myklebust , Sasha Levin Subject: [PATCH 4.19 102/306] sunrpc: safely reallow resvport min/max inversion Date: Wed, 27 Nov 2019 21:29:12 +0100 Message-Id: <20191127203122.712686321@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203114.766709977@linuxfoundation.org> References: <20191127203114.766709977@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: J. Bruce Fields [ Upstream commit 826799e66e8683e5698e140bb9ef69afc8c0014e ] Commits ffb6ca33b04b and e08ea3a96fc7 prevent setting xprt_min_resvport greater than xprt_max_resvport, but may also break simple code that sets one parameter then the other, if the new range does not overlap the old. Also it looks racy to me, unless there's some serialization I'm not seeing. Granted it would probably require malicious privileged processes (unless there's a chance these might eventually be settable in unprivileged containers), but still it seems better not to let userspace panic the kernel. Simpler seems to be to allow setting the parameters to whatever you want but interpret xprt_min_resvport > xprt_max_resvport as the empty range. Fixes: ffb6ca33b04b "sunrpc: Prevent resvport min/max inversion..." Fixes: e08ea3a96fc7 "sunrpc: Prevent rexvport min/max inversion..." Signed-off-by: J. Bruce Fields Signed-off-by: Trond Myklebust Signed-off-by: Sasha Levin --- net/sunrpc/xprtsock.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/net/sunrpc/xprtsock.c b/net/sunrpc/xprtsock.c index c0d7875a64ffc..9dc059dea689d 100644 --- a/net/sunrpc/xprtsock.c +++ b/net/sunrpc/xprtsock.c @@ -129,7 +129,7 @@ static struct ctl_table xs_tunables_table[] = { .mode = 0644, .proc_handler = proc_dointvec_minmax, .extra1 = &xprt_min_resvport_limit, - .extra2 = &xprt_max_resvport + .extra2 = &xprt_max_resvport_limit }, { .procname = "max_resvport", @@ -137,7 +137,7 @@ static struct ctl_table xs_tunables_table[] = { .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_minmax, - .extra1 = &xprt_min_resvport, + .extra1 = &xprt_min_resvport_limit, .extra2 = &xprt_max_resvport_limit }, { @@ -1776,11 +1776,17 @@ static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) spin_unlock_bh(&xprt->transport_lock); } -static unsigned short xs_get_random_port(void) +static int xs_get_random_port(void) { - unsigned short range = xprt_max_resvport - xprt_min_resvport + 1; - unsigned short rand = (unsigned short) prandom_u32() % range; - return rand + xprt_min_resvport; + unsigned short min = xprt_min_resvport, max = xprt_max_resvport; + unsigned short range; + unsigned short rand; + + if (max < min) + return -EADDRINUSE; + range = max - min + 1; + rand = (unsigned short) prandom_u32() % range; + return rand + min; } /** @@ -1836,9 +1842,9 @@ static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) transport->srcport = xs_sock_getport(sock); } -static unsigned short xs_get_srcport(struct sock_xprt *transport) +static int xs_get_srcport(struct sock_xprt *transport) { - unsigned short port = transport->srcport; + int port = transport->srcport; if (port == 0 && transport->xprt.resvport) port = xs_get_random_port(); @@ -1859,7 +1865,7 @@ static int xs_bind(struct sock_xprt *transport, struct socket *sock) { struct sockaddr_storage myaddr; int err, nloop = 0; - unsigned short port = xs_get_srcport(transport); + int port = xs_get_srcport(transport); unsigned short last; /* @@ -1877,8 +1883,8 @@ static int xs_bind(struct sock_xprt *transport, struct socket *sock) * transport->xprt.resvport == 1) xs_get_srcport above will * ensure that port is non-zero and we will bind as needed. */ - if (port == 0) - return 0; + if (port <= 0) + return port; memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); do { @@ -3319,12 +3325,8 @@ static int param_set_uint_minmax(const char *val, static int param_set_portnr(const char *val, const struct kernel_param *kp) { - if (kp->arg == &xprt_min_resvport) - return param_set_uint_minmax(val, kp, - RPC_MIN_RESVPORT, - xprt_max_resvport); return param_set_uint_minmax(val, kp, - xprt_min_resvport, + RPC_MIN_RESVPORT, RPC_MAX_RESVPORT); } -- 2.20.1