Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1966186ybh; Tue, 14 Jul 2020 11:52:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3By2fFp9zmNnzwX7l4JJBWsLHlUSxZV7WDWmbYzIbIeKdrmMDRDvCtMSJbu5aiMkjC8aN X-Received: by 2002:a17:906:97d7:: with SMTP id ef23mr6001030ejb.450.1594752750288; Tue, 14 Jul 2020 11:52:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594752750; cv=none; d=google.com; s=arc-20160816; b=YdKfjgDSiLHC60B4uevyPqJujUBSOLo58O7MQJbrYghePyla5z24xv4iHFHpO7IDI2 ogZYw34SAx35s9a7WLUA7krPLBSvb+3bf+trqKozOp4fF3bw+nSrKlwmRpW3jB35haxW x6K8sdpSbUPQDGN2RXeWwBblYr4fiFKRDTlVz450la3IKIxPom6KGqJTlamQUFqP0R+7 PfCYpPT5OSvJDTVtvNF/md1LzYatZWS6LCQiT+1Ya2qQTPvSIshx8DfydY0PYW0b9TZ9 eqOhN9ibZZjRMPiXbtxoPh7Lm/692NNTqHQ63SbEsY2Vi/JVgLv7a/mk0OtnqlD9kS0E hbIw== 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=BcCCPyHjm6cL/+ugjGKMZPhIIVWXZ7fE5IYgZuwNl7M=; b=My86QOYA/iaZ8FhX11VURiJ7uJYjmC2CthVfMNA/BHuaZda3Gj7LG3qqu/DmH3258T vv0xVo7JZUaOdqgtAyF4CXXirkz3RNTfN5u95YksvGVFbxOUq1yPCo0Y4aBGukjf8gnL sHUriXgABV8LuQlaIMJrpKTz2cTflfD03wfPtHkQTheFgcSQj4QAMAW7eMvyJ4bXqPxb K5IOFEkqeLOid/FDeDfy3HtqN8Jt+UdGA1XzjMIwa2CjqFst/aqtJzuY7s9NOhV6XN1G EJfnlQKlup1U4yI1FvRzBHoNnlfMNQUqsJWTo24YbpvMCt8dK8Y9iphEglb4j8iP65A9 YOEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ewk43zSr; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si11396310ejk.32.2020.07.14.11.52.07; Tue, 14 Jul 2020 11:52: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; dkim=pass header.i=@kernel.org header.s=default header.b=Ewk43zSr; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730282AbgGNSuN (ORCPT + 99 others); Tue, 14 Jul 2020 14:50:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:46180 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730268AbgGNSuJ (ORCPT ); Tue, 14 Jul 2020 14:50:09 -0400 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 106C121835; Tue, 14 Jul 2020 18:50:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594752608; bh=w1saTiJ4092Q9Rc1+4AWTRTNFivBt5Fd42VK/TVq+gw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ewk43zSr82kOvUvvqMiGHXXIbZEtJL5a/Y4JTXBxkF0woScXNPxJNogmX36Dt3zaI NS/3fWvmuEepu/X+lJQniQlSydGSwA2Y5SNWmCA5WzM53t91jL9lHdHLdKUUpJSaAN c7bAMbQ9bAEl7bMv4z6w+IUcjAr/SuKeOnMRkCjA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, John Fastabend , Alexei Starovoitov , Martin KaFai Lau , Sasha Levin Subject: [PATCH 5.4 041/109] bpf, sockmap: RCU dereferenced psock may be used outside RCU block Date: Tue, 14 Jul 2020 20:43:44 +0200 Message-Id: <20200714184107.488252468@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714184105.507384017@linuxfoundation.org> References: <20200714184105.507384017@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: John Fastabend [ Upstream commit 8025751d4d55a2f32be6bdf825b6a80c299875f5 ] If an ingress verdict program specifies message sizes greater than skb->len and there is an ENOMEM error due to memory pressure we may call the rcv_msg handler outside the strp_data_ready() caller context. This is because on an ENOMEM error the strparser will retry from a workqueue. The caller currently protects the use of psock by calling the strp_data_ready() inside a rcu_read_lock/unlock block. But, in above workqueue error case the psock is accessed outside the read_lock/unlock block of the caller. So instead of using psock directly we must do a look up against the sk again to ensure the psock is available. There is an an ugly piece here where we must handle the case where we paused the strp and removed the psock. On psock removal we first pause the strparser and then remove the psock. If the strparser is paused while an skb is scheduled on the workqueue the skb will be dropped on the flow and kfree_skb() is called. If the workqueue manages to get called before we pause the strparser but runs the rcvmsg callback after the psock is removed we will hit the unlikely case where we run the sockmap rcvmsg handler but do not have a psock. For now we will follow strparser logic and drop the skb on the floor with skb_kfree(). This is ugly because the data is dropped. To date this has not caused problems in practice because either the application controlling the sockmap is coordinating with the datapath so that skbs are "flushed" before removal or we simply wait for the sock to be closed before removing it. This patch fixes the describe RCU bug and dropping the skb doesn't make things worse. Future patches will improve this by allowing the normal case where skbs are not merged to skip the strparser altogether. In practice many (most?) use cases have no need to merge skbs so its both a code complexity hit as seen above and a performance issue. For example, in the Cilium case we always set the strparser up to return sbks 1:1 without any merging and have avoided above issues. Fixes: e91de6afa81c1 ("bpf: Fix running sk_skb program types with ktls") Signed-off-by: John Fastabend Signed-off-by: Alexei Starovoitov Acked-by: Martin KaFai Lau Link: https://lore.kernel.org/bpf/159312679888.18340.15248924071966273998.stgit@john-XPS-13-9370 Signed-off-by: Sasha Levin --- net/core/skmsg.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 70ea352e3a3b6..118cf1ace43a6 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -785,11 +785,18 @@ static void sk_psock_verdict_apply(struct sk_psock *psock, static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb) { - struct sk_psock *psock = sk_psock_from_strp(strp); + struct sk_psock *psock; struct bpf_prog *prog; int ret = __SK_DROP; + struct sock *sk; rcu_read_lock(); + sk = strp->sk; + psock = sk_psock(sk); + if (unlikely(!psock)) { + kfree_skb(skb); + goto out; + } prog = READ_ONCE(psock->progs.skb_verdict); if (likely(prog)) { skb_orphan(skb); @@ -798,6 +805,7 @@ static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb) ret = sk_psock_map_verd(ret, tcp_skb_bpf_redirect_fetch(skb)); } sk_psock_verdict_apply(psock, skb, ret); +out: rcu_read_unlock(); } -- 2.25.1