Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2573753pxp; Mon, 7 Mar 2022 19:14:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJxglcN6TBEN17yYzU6jU6mIbd9wwH47/ru9SG2Fw2IH0vTFUbQcDT9A/dsddbc3ezcg8NQg X-Received: by 2002:a17:907:72c3:b0:6ce:5256:1125 with SMTP id du3-20020a17090772c300b006ce52561125mr11829152ejc.697.1646709290569; Mon, 07 Mar 2022 19:14:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646709290; cv=none; d=google.com; s=arc-20160816; b=KXkufSBPX4W0o6XEhaF/q+5lkr+xsd1uYKJf8FnL6ZoIXlZGWQma5N/h0Tg4lu/nLV fiwuKythOa5ms8Nh7Ds94bSbh1OhiTKdVzuRjwSqa/x6bQZGnUQAewcv6qrVUVs4olSo FUKchSSnRNE0FLx8dw6BSJecTT0mxUBkQgcmmJ47aLmvq+xt2xXXU3qposVXmnCQIjpc BtLF2ftqqtUtInOcsbNqY5ZexGL8GkS0eHDfQoKa+RePiekQM45cpgpyDzBT6jkku92T 034xA/Q7zTr9zkFW8JE1CUv+efRth2lYHm2ZCYpsvVqpgyVu2bmcKfBmDO8fYQtsQV1b uzPQ== 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=UiCZ3ID+pvEv7z/HBAXwGK5Va1ec/uhCKnaEOtu326w=; b=FN6m+cWQZFUA9xnya+2ha83fVfnFoPHwAhODIO8Elde3CtqmtPB3FrlxugpzNQ3Uoj s8PiT5puiI8TZdzPNJoUwEdNVUnwN0rhvdfW32VI8oaMjI+QobCkh7JEY32zhM17twbX 4X2DqVdrekzxWGc3ZdOK1pkX9bbUCvGghTwNnG1tkvjh0fs4eEfjPYH7pk1L6hoEq082 KcO+DXby0szi/ypFeL31goyOlV20vxUJUipY2RfwYXQibF+iuxVqZfeIfZNdu2oRNYMb ki+MAk2+lS64CHiT+7z45tDTTS4V55+pLAuEYkoKVojhNwq7ZXRRAtM+weKlAYd4ilsY HDhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vVQK7THo; 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 oz7-20020a1709077d8700b006db0f179ec7si4897771ejc.616.2022.03.07.19.14.09; Mon, 07 Mar 2022 19:14:50 -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=vVQK7THo; 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 S235739AbiCGKZq (ORCPT + 99 others); Mon, 7 Mar 2022 05:25:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240026AbiCGKEf (ORCPT ); Mon, 7 Mar 2022 05:04:35 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6BF366AF4; Mon, 7 Mar 2022 01:52:06 -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 ams.source.kernel.org (Postfix) with ESMTPS id 58E75B8102B; Mon, 7 Mar 2022 09:52:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A14BBC340F3; Mon, 7 Mar 2022 09:52:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646646725; bh=w/ntj/P8FjG0egwwy9HfVWJPl4ZMUNQq0IVluBcr0ME=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vVQK7THo8PqJK6ovBee1YhZ0ucY68y1Q/qDNXOZzR9jVyYDbOIbq5A62T0NHUB7nj aabEvtkCvIv5MQAz+4lhjlt9HUJVkXrDISaAGXkyCvRxTRqkz9s44GRSF+Z73sBiMM vYsjv93A6RLhmoa+yHCxEHzZ1v2QBBt8A3KL2ACI= 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 5.16 074/186] netfilter: nf_queue: fix possible use-after-free Date: Mon, 7 Mar 2022 10:18:32 +0100 Message-Id: <20220307091656.159231396@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220307091654.092878898@linuxfoundation.org> References: <20220307091654.092878898@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 | 13 +++++++++---- net/netfilter/nfnetlink_queue.c | 12 +++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) --- a/include/net/netfilter/nf_queue.h +++ b/include/net/netfilter/nf_queue.h @@ -37,7 +37,7 @@ void nf_register_queue_handler(const str void nf_unregister_queue_handler(void); 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_free(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 @@ -96,19 +96,21 @@ static void __nf_queue_entry_init_physde } /* 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; + dev_hold(state->in); dev_hold(state->out); - if (state->sk) - sock_hold(state->sk); #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) dev_hold(entry->physin); dev_hold(entry->physout); #endif + return true; } EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs); @@ -196,7 +198,10 @@ static int __nf_queue(struct sk_buff *sk __nf_queue_entry_init_physdevs(entry); - nf_queue_entry_get_refs(entry); + if (!nf_queue_entry_get_refs(entry)) { + kfree(entry); + return -ENOTCONN; + } switch (entry->state.pf) { case AF_INET: --- a/net/netfilter/nfnetlink_queue.c +++ b/net/netfilter/nfnetlink_queue.c @@ -710,9 +710,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)