Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1775966pxp; Mon, 7 Mar 2022 02:03:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJwMxanETRewVfA+QqViGQNByyrTPOtm9um5oOmFOXcazo2FqzvjANeee9cdKQk7BmIEiwCf X-Received: by 2002:a05:6402:484:b0:415:d931:cb2f with SMTP id k4-20020a056402048400b00415d931cb2fmr10329860edv.287.1646647389120; Mon, 07 Mar 2022 02:03:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646647389; cv=none; d=google.com; s=arc-20160816; b=Rj/+U+YD3l0cbNGjwLAJurslQAh8QrWU1R6wEm7tRKozur3dQsgHRLrEx8A4c3NXEl caHYAZedzmMSVyYphyRVbNChdJyfJ88vNa6jHmnn3FNtLAkahkGRRudCUtiVvSZQrbk1 MIgTXDIJm1qGuYbIrYNKiQtUyx/vL+TvOPVcG00d6ai+OwMXIr+Z4dthTwTks0d/+dkr U7dPBWXKNpuu30sSy5uN8+hTtOb9On8z8jF2UGHv7uI7/9wosk12/ZVvz4Ui4d49DHlr eh63y21DZqFSC1a+SU1OhOOX1Q5SWjkQJFKehu2uxE/kkch86tXY9KwyKQHEAigZxVCg L+oQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zO7cQ0892XRVseT3l8PuRyvfHGPIjWb7V/0I8XWafiQ=; b=n1LiPuZzOTg3pNlKwQ18pwn/gm5Cu45lUrZd2LBxGigW/CDjF1mM91f24K7F9SCr/i lD6/qDZwQ5U5/Ut0tAdn8Q1cp6fbbNuSyoL+spB0FzENJbksSN8k8kb6GQuyI9DEu0ya aapAtrNAs0QrCLJyMm68njjZbo4W7yNthyvv8bDucQUbsubRFZ1pECp8oEic9FLgWY28 nP3CuBUmMZgZqshqZr8hUSYWfWDdElnVyWe5iaVAN2xoYpJ4EE6iyaWGrR3taDOgEm0d EafiPxG2ntHDjGFZflq9bIgaJeXF3FaMtZUXaXsTdihkh1X1cD+GKPsoGSKXhrBlydse TK9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KrLqE4V9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m8-20020a50ef08000000b00415edd49c51si6566047eds.491.2022.03.07.02.02.45; Mon, 07 Mar 2022 02:03:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KrLqE4V9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236750AbiCGJ0r (ORCPT + 99 others); Mon, 7 Mar 2022 04:26:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237098AbiCGJXZ (ORCPT ); Mon, 7 Mar 2022 04:23:25 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C063B65821; Mon, 7 Mar 2022 01:21:50 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4823E61027; Mon, 7 Mar 2022 09:21:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5915DC340F3; Mon, 7 Mar 2022 09:21:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646644909; bh=Fi3GZLIsbutc8SXukSmt00AA/xd56Py1HYugwVIKGAg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KrLqE4V9wxPTSV4IRlyFmYMdluadsHOIgaGg1SNS+taPXettuL6bg+78rdx9Pea9i STZBrQmVxzK0Ma9WsHPVjyvOBgL3DcZdTiswHTBLxtRTAaEQou4oBf24q+FDvb4+ON c++xJJeDPf9UPLZkwx9HT4zoOX11r5gJUb8q1YrA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Eric Dumazet , Florian Westphal Subject: [PATCH 4.14 19/42] netfilter: nf_queue: fix possible use-after-free Date: Mon, 7 Mar 2022 10:18:53 +0100 Message-Id: <20220307091636.710014074@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220307091636.146155347@linuxfoundation.org> References: <20220307091636.146155347@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Florian Westphal commit c3873070247d9e3c7a6b0cf9bf9b45e8018427b1 upstream. Eric Dumazet says: The sock_hold() side seems suspect, because there is no guarantee that sk_refcnt is not already 0. On failure, we cannot queue the packet and need to indicate an error. The packet will be dropped by the caller. v2: split skb prefetch hunk into separate change Fixes: 271b72c7fa82c ("udp: RCU handling for Unicast packets.") Reported-by: Eric Dumazet Reviewed-by: Eric Dumazet Signed-off-by: Florian Westphal Signed-off-by: Greg Kroah-Hartman --- include/net/netfilter/nf_queue.h | 2 +- net/netfilter/nf_queue.c | 12 ++++++++++-- net/netfilter/nfnetlink_queue.c | 12 +++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) --- a/include/net/netfilter/nf_queue.h +++ b/include/net/netfilter/nf_queue.h @@ -32,7 +32,7 @@ void nf_register_queue_handler(struct ne void nf_unregister_queue_handler(struct net *net); void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict); -void nf_queue_entry_get_refs(struct nf_queue_entry *entry); +bool nf_queue_entry_get_refs(struct nf_queue_entry *entry); void nf_queue_entry_release_refs(struct nf_queue_entry *entry); static inline void init_hashrandom(u32 *jhash_initval) --- a/net/netfilter/nf_queue.c +++ b/net/netfilter/nf_queue.c @@ -80,10 +80,13 @@ void nf_queue_entry_release_refs(struct EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs); /* Bump dev refs so they don't vanish while packet is out */ -void nf_queue_entry_get_refs(struct nf_queue_entry *entry) +bool nf_queue_entry_get_refs(struct nf_queue_entry *entry) { struct nf_hook_state *state = &entry->state; + if (state->sk && !refcount_inc_not_zero(&state->sk->sk_refcnt)) + return false; + if (state->in) dev_hold(state->in); if (state->out) @@ -102,6 +105,7 @@ void nf_queue_entry_get_refs(struct nf_q dev_hold(physdev); } #endif + return true; } EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs); @@ -159,7 +163,11 @@ static int __nf_queue(struct sk_buff *sk .size = sizeof(*entry) + afinfo->route_key_size, }; - nf_queue_entry_get_refs(entry); + if (!nf_queue_entry_get_refs(entry)) { + kfree(entry); + return -ENOTCONN; + } + afinfo->saveroute(skb, entry); status = qh->outfn(entry, queuenum); --- a/net/netfilter/nfnetlink_queue.c +++ b/net/netfilter/nfnetlink_queue.c @@ -693,9 +693,15 @@ static struct nf_queue_entry * nf_queue_entry_dup(struct nf_queue_entry *e) { struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC); - if (entry) - nf_queue_entry_get_refs(entry); - return entry; + + if (!entry) + return NULL; + + if (nf_queue_entry_get_refs(entry)) + return entry; + + kfree(entry); + return NULL; } #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)