Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3907829ybi; Mon, 29 Jul 2019 15:06:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqyEAkj5iMtEUuxeD5U5t0czLqw6L/lgVfVN1ZIP93fAKgmSweWHv5n/YiHmu1LeHiT70kjJ X-Received: by 2002:a65:68c9:: with SMTP id k9mr72988333pgt.17.1564437986094; Mon, 29 Jul 2019 15:06:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564437986; cv=none; d=google.com; s=arc-20160816; b=Qi7YfH14W17/7Bd9Vz9MAhrEBtWJ6bmVzeBf51k1hea0FyobY5nCSzhq68a7CKUmQ2 C/p5U1WKmY+v81AESralvssIerd46yxXK+hSjQnglHbBSNLvNvJQtWWvTvxjsWczHhYn D6a70Fg1HuuHs6LNNwTlSHuJlLjXUT5LnAsuPeFt7o0kd7QkHRl4bdvlOiUXQ6D+1QkM PYghzSWHlIRQCvYGD5PjuRKJwNMph0BHvh7UEw62DVGBvTCx9fWJLUVtHxO/uDtOYyPY CuUuLyoryWKdQCHGGwI+anJO/yvI+2mFpEloNRSWKdRBuEUmWa2sCo5duG1ZefEHwpln bqXA== 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=OEpWvkkWvlmASe6UvW1TYgS7lrPVA86IWzmEsdPcUkw=; b=z4yxw+9FmskiztwaIXazrbAW4A9WFE9OnHk9qhCHRuTbM9p9nj03uW0CRoLbtUtd9i 3Cxho2/kde2kmjhdJqRQqWvzEWLQDLXQR62fzt6efsYcX67B3lO5i8h8SE4RY4vuq7Hd nvVNWYZ3EsCq5NornjmIbEZK5jAmMrRfx9Lxa9ORFVODxyA5i3LRsVTHhUcau5MM/ht6 xEIBt9lq2OTjJTgpV9+7a8Mx5vRf3LsMEjJ//HulnbwmtSqg4S+iRan0Xk5/GEGPX3Kv jUiOKKvkROdaKPAit/3YV8QezrzMesQNKMbzDs+FilfTs9mjnqNy9AWmt/bo80YGjv1Q xP+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=R3oXEyQS; 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 j190si30463917pge.92.2019.07.29.15.06.10; Mon, 29 Jul 2019 15:06:26 -0700 (PDT) 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=R3oXEyQS; 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 S2387977AbfG2Tdz (ORCPT + 99 others); Mon, 29 Jul 2019 15:33:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:48090 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729938AbfG2Tdp (ORCPT ); Mon, 29 Jul 2019 15:33:45 -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 E729C217D6; Mon, 29 Jul 2019 19:33:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564428825; bh=aEnpgOy44uHJZY4dNXHZpaFZVQC+Ewy6WCVunC76dXo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R3oXEyQSO8c90muPj5Py9bdc/LR7aJHussJhUcRY8wFXsjItLOpqf84B7rG+4csWm FDWdPsiogcY9bPXK9+0rkTsd1AbAAgZ4Z9mbqJuw2SMxn1R2s4ZA8vNJSnSzvAQmH1 s+QWT4TPQDyIZq78eXhqyDzIeTDLjLBnUPyP2KhQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Florian Westphal , "David S. Miller" Subject: [PATCH 4.14 199/293] net: make skb_dst_force return true when dst is refcounted Date: Mon, 29 Jul 2019 21:21:30 +0200 Message-Id: <20190729190839.726480062@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190820.321094988@linuxfoundation.org> References: <20190729190820.321094988@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: Florian Westphal [ Upstream commit b60a77386b1d4868f72f6353d35dabe5fbe981f2 ] netfilter did not expect that skb_dst_force() can cause skb to lose its dst entry. I got a bug report with a skb->dst NULL dereference in netfilter output path. The backtrace contains nf_reinject(), so the dst might have been cleared when skb got queued to userspace. Other users were fixed via if (skb_dst(skb)) { skb_dst_force(skb); if (!skb_dst(skb)) goto handle_err; } But I think its preferable to make the 'dst might be cleared' part of the function explicit. In netfilter case, skb with a null dst is expected when queueing in prerouting hook, so drop skb for the other hooks. v2: v1 of this patch returned true in case skb had no dst entry. Eric said: Say if we have two skb_dst_force() calls for some reason on the same skb, only the first one will return false. This now returns false even when skb had no dst, as per Erics suggestion, so callers might need to check skb_dst() first before skb_dst_force(). Signed-off-by: Florian Westphal Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- include/net/dst.h | 5 ++++- net/netfilter/nf_queue.c | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) --- a/include/net/dst.h +++ b/include/net/dst.h @@ -329,8 +329,9 @@ static inline bool dst_hold_safe(struct * @skb: buffer * * If dst is not yet refcounted and not destroyed, grab a ref on it. + * Returns true if dst is refcounted. */ -static inline void skb_dst_force(struct sk_buff *skb) +static inline bool skb_dst_force(struct sk_buff *skb) { if (skb_dst_is_noref(skb)) { struct dst_entry *dst = skb_dst(skb); @@ -341,6 +342,8 @@ static inline void skb_dst_force(struct skb->_skb_refdst = (unsigned long)dst; } + + return skb->_skb_refdst != 0UL; } --- a/net/netfilter/nf_queue.c +++ b/net/netfilter/nf_queue.c @@ -138,6 +138,11 @@ static int __nf_queue(struct sk_buff *sk goto err; } + if (!skb_dst_force(skb) && state->hook != NF_INET_PRE_ROUTING) { + status = -ENETDOWN; + goto err; + } + *entry = (struct nf_queue_entry) { .skb = skb, .state = *state, @@ -146,7 +151,6 @@ static int __nf_queue(struct sk_buff *sk }; nf_queue_entry_get_refs(entry); - skb_dst_force(skb); afinfo->saveroute(skb, entry); status = qh->outfn(entry, queuenum);