Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1798471pxp; Mon, 7 Mar 2022 02:35:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxV0gLfEk098vdhSh+8cqXNAbdwVuFSnh99StCVtky1ihRe9Sczxj/VbN8hcwpJpHeL/XpW X-Received: by 2002:a17:903:240a:b0:14e:dad4:5ce4 with SMTP id e10-20020a170903240a00b0014edad45ce4mr11875224plo.125.1646649356776; Mon, 07 Mar 2022 02:35:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646649356; cv=none; d=google.com; s=arc-20160816; b=NTysA6IEZzGD8rhi14FJRDTbsWJaKCdi0EqnFvslkMEhxqdPkBi0Yo6KdNB8fNrb6Q RbE5pRgtZfvrfyuooR3uzfpjZnmlJpIimGLPli1tWfwDA+cBpE+MjpvvS76fUCACX9Pt k2NYVlOPSXyhdz6YRFmoZzJXDHWNhO/LLBw6HXnhNKUUYpjpfH54KD1OgCrQZPmAFGAl IuacKhDKaZxguERQ86u7GzYmvolSpOB+md3X7NlWQPKf+L94e5ASo3vqpCssohbb0OJy UCUozP6WaApJnTu0y9iWMeJxjKjarO4rlodLvjtMaY9s2EaI5LcbEwi/13zkMWUP5WJA YD2g== 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=XWyajRDmZeHqWF8JcbzJC4SIFI6N4BXP7GzRVkjOt/k=; b=CkUr0rvRALLgNZ1pEk5qx2lFIiakC62hWtZDUcjVeBshHzgaTsloBd3JOJTC8anyhg j9bQqH4au3XYj0MsqqxDq0ULNCPSm1ZcBg8AQgBbJYrYECKkNmaCJy7v0FmR2be14Hvx XEOdomFgVMnWW9zwwHcKEkzoQlQd+hYhXmekZf3pobqQhDViLpp/I3SqFbBpOw/d16h2 gREnVwvchuZOVLAhVm51PTZVFKdrjTeBxt8qNk1GIFEeHZboQpitUzMkXRaPeTPNqNpS t2GbsovdzQ1WAY2pPcoAIPNl10a/nFLOzohS/IMh6d4sSa0PV5+RwYnPu6mXvHXRp/uF 7VKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HjvqQNQy; 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 u7-20020a655c07000000b00373c8b78073si11802190pgr.79.2022.03.07.02.35.41; Mon, 07 Mar 2022 02:35:56 -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=HjvqQNQy; 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 S240713AbiCGKIf (ORCPT + 99 others); Mon, 7 Mar 2022 05:08:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239668AbiCGJuB (ORCPT ); Mon, 7 Mar 2022 04:50:01 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B4A9710EC; Mon, 7 Mar 2022 01:43:39 -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 08DF9B810CF; Mon, 7 Mar 2022 09:43:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 444A7C340F6; Mon, 7 Mar 2022 09:43:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646646192; bh=G9AhXJMncP9M8uGCSalT39DSnLh5dH33AtDnouCfyEc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HjvqQNQy7tb8XrBSlUG8i6k2smLL0+oWzCeCMyHDIL0lhCbKKHYmrEpYZprp4MfUH 6yry1H37woybgN1rzq5DiokADKanZPfM3syOXd+re/u1/B/rGvnwxhSHs6gz7JK/T5 GzKVvBztn3N18EcJRUkfUgftmpV7Hn7fsHnh4Xso= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Joe Stringer , Florian Westphal Subject: [PATCH 5.15 162/262] netfilter: nf_queue: handle socket prefetch Date: Mon, 7 Mar 2022 10:18:26 +0100 Message-Id: <20220307091707.003096337@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220307091702.378509770@linuxfoundation.org> References: <20220307091702.378509770@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 3b836da4081fa585cf6c392f62557496f2cb0efe upstream. In case someone combines bpf socket assign and nf_queue, then we will queue an skb who references a struct sock that did not have its reference count incremented. As we leave rcu protection, there is no guarantee that skb->sk is still valid. For refcount-less skb->sk case, try to increment the reference count and then override the destructor. In case of failure we have two choices: orphan the skb and 'delete' preselect or let nf_queue() drop the packet. Do the latter, it should not happen during normal operation. Fixes: cf7fbe660f2d ("bpf: Add socket assign support") Acked-by: Joe Stringer Signed-off-by: Florian Westphal Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nf_queue.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) --- a/net/netfilter/nf_queue.c +++ b/net/netfilter/nf_queue.c @@ -180,6 +180,18 @@ static int __nf_queue(struct sk_buff *sk break; } + if (skb_sk_is_prefetched(skb)) { + struct sock *sk = skb->sk; + + if (!sk_is_refcounted(sk)) { + if (!refcount_inc_not_zero(&sk->sk_refcnt)) + return -ENOTCONN; + + /* drop refcount on skb_orphan */ + skb->destructor = sock_edemux; + } + } + entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC); if (!entry) return -ENOMEM;