Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp217852ybi; Fri, 26 Jul 2019 08:37:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqya6WISvCo7wPOMCEnS7mmZj6eGOXMUzmdB5Hbth7yATXvldfXeBQ1fH/VGvCyfNlTGNDIs X-Received: by 2002:a62:8745:: with SMTP id i66mr22480473pfe.259.1564155472596; Fri, 26 Jul 2019 08:37:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564155472; cv=none; d=google.com; s=arc-20160816; b=rNlcmvlJCCI5vlFwwh33AoHH08aHDZ4miVl+F7uH40X2KHHRyFGFUh2KBQdPPFJxOG lABW11O4Q++pzuRiZvR/YUdWh5nGacQD45bCvMK1f8auDPQPcXpjYub5N5g/0ubfSaM4 IHXJDsTtp0RqS2A0Lmgp6jq336CVM9mfwsF+ntKx4IfaQBImpJUTCaH+dUKL3w9VpR+U uExzxG1u9ivo5CU/ml0OsHRsL5F0JwjBPyHVbOYdNeGhwB/N0a+xAxQM0BHxNpwAiKmB WlTxx18UPAo8VGDwfpC7OtNS0h79XEywzrd9tsQyuoN4UsFfADpeDxKDY/AVCYjoLhQU c3SA== 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=VBl77593lcrIjFXeTRSG2SB3wfeOGW08de3ppd2Srj0=; b=yYFdXdrythUDYQm9mUQbMbwknijnzs3qk/eD3W3FFhnqxICln82MeLkJge/YR8HllJ jAhrmYdKJ+g1Bv2q8ZtEBNLqFJYJlB+dm8I1hqz6KE2B5Ep4Jw8cLshOk+rq6hwIK4PG eQSrLWikMOvOxh8KkcZQy6C1uelUxCOQ+lsVQzc901GoR/cYI2Bt8KKUttcOsZ0j076L XEqhCSXfEqEitXVv+LaXkNa+aT0cG+C/vMXy5jTlZFb2vj9Pg2gdGIKzp3ehXmkxRNjX kr5zCulxI4an0eZem/I7jPuD+y1kBu1QDgyhYnr2x5q52EpXadFbSUyhKXZyVXwPwmxm HvhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=y0TNo1FL; 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 d38si20345207pgl.519.2019.07.26.08.37.38; Fri, 26 Jul 2019 08:37:52 -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=y0TNo1FL; 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 S2388127AbfGZPcG (ORCPT + 99 others); Fri, 26 Jul 2019 11:32:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:47146 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389370AbfGZPcC (ORCPT ); Fri, 26 Jul 2019 11:32:02 -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 766AE205F4; Fri, 26 Jul 2019 15:32:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564155121; bh=zRkOIPNoyE8ivzzuFEeKn7cn1eMbQrjTjT9edSUPa4c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y0TNo1FLvOZbpYo3JDZIf3D9aufs5uVIu8d98jJWkgv899OmiOfi285orqZItJAjx GLShbBgvwQLJyazrxse8Zmy1/N/ZcslBhzngMwT8jPbu/LS+dqQuxvjP+IJNu0Wn4l VCQ1E47HtCj4m0uOeVutM48Yp45Y2ybeVmQdKAvo= 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.19 10/50] net: make skb_dst_force return true when dst is refcounted Date: Fri, 26 Jul 2019 17:24:45 +0200 Message-Id: <20190726152301.681782579@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190726152300.760439618@linuxfoundation.org> References: <20190726152300.760439618@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 @@ -313,8 +313,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); @@ -325,6 +326,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 @@ -174,6 +174,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, @@ -182,7 +187,6 @@ static int __nf_queue(struct sk_buff *sk }; nf_queue_entry_get_refs(entry); - skb_dst_force(skb); switch (entry->state.pf) { case AF_INET: