Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1735336iob; Thu, 5 May 2022 07:20:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylXVShTbooSJeH4oP8kVZMvQJvCi2ESTGg9exUjzRGVLbLQIoUk1AICrA0roEpZvheinSI X-Received: by 2002:a65:6955:0:b0:380:64fd:a2dd with SMTP id w21-20020a656955000000b0038064fda2ddmr21916412pgq.383.1651760450129; Thu, 05 May 2022 07:20:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651760450; cv=none; d=google.com; s=arc-20160816; b=vBWN3ckyrW1GPw9BOsvvcQEFXqqIY3GPJOF1O+2QnfFvDP6cK4FQJN/QSVCuUydRx7 wQrt8mhf+i58LxUsqcCOP7/c7J47TLedCrDptieO2VlqhHabJSkhR5yX2Xh4C+4Fi0Ti S/DfxM9bI2kea0MNmRim2+HPpaE/kp32jFuTTt/IKO4lkDa7tl7zFVlhodvK3zetU0pq xAAIa0WMy5NcJ1MeaAR3XKdwAfgP8Qmmak0VjowAwkKzKAtdScNW7rfiN2C/7zzTRiv3 dNndvmgaMtyjxc3fectUJ6372E7nkFRyR9DLEdk2Qj+GNKipfqO6LvybdXHOGtmyOHdg hzBw== 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=BVsAG5rS+Ee8K6hWYQGsmpVajGd76b9krpurwo1kb0Y=; b=osxTVxgwGjrS3UdlArz+cx23qKaFUPO6/3N/s8Pym4HxUc0ZkBiSfK+CpREmhCDV8q n8FMo9dEMPgGrKTN+KC4aj3Gh0TKtk0dcWxnl+oOA9MGpRi8oLwgCohkbwUHGOzYSjoH w1KT8/IlUuiOEibZneCGUNSJZo5rYE1q2FePko/x1+Qr5eCkhFg2pRCAex5jDFQntZuJ 5It+IkzW87zrTFgGcbur++tWKD0Ta5WwCc/1nnQpbQCMbwpakftq27I6y9m4JZlsnL86 Pae+NxpUDwNCD/73Fxrl6vc48uO0J+CR0OBlvSJ2+YWQrSQe2NVtUTPG9b+HvumFHStj e4Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GA3MboEV; 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 y23-20020a1709027c9700b00153b2d16420si1833215pll.40.2022.05.05.07.20.31; Thu, 05 May 2022 07:20:50 -0700 (PDT) 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=GA3MboEV; 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 S1354419AbiEDQ60 (ORCPT + 99 others); Wed, 4 May 2022 12:58:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354447AbiEDQyW (ORCPT ); Wed, 4 May 2022 12:54:22 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 469B54968B; Wed, 4 May 2022 09:49:35 -0700 (PDT) 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 1E96961701; Wed, 4 May 2022 16:49:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D14EC385A5; Wed, 4 May 2022 16:49:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651682974; bh=ri5FZx4k/xV0y56XosfN461TREudlontDU4CQIoNdhY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GA3MboEVM8t3ogjgLkReGoWBWwDKeXqH9SGSfSjTfyAjD/s8w1/GhZer+DWARpCkT +UQDmVqtMBdibfzT49RJ1bpmhXJAMMxc25Ed9mhQL7Feg0OOCbKdqGcqrPlsKi3hoU aRt36VZ2LpyzG/CHsz9Xk8id1EtQspq9Fzh29pGE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Florian Westphal , Pablo Neira Ayuso , Topi Miettinen Subject: [PATCH 5.4 77/84] netfilter: nft_socket: only do sk lookups when indev is available Date: Wed, 4 May 2022 18:44:58 +0200 Message-Id: <20220504152933.610244490@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504152927.744120418@linuxfoundation.org> References: <20220504152927.744120418@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.7 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 743b83f15d4069ea57c3e40996bf4a1077e0cdc1 upstream. Check if the incoming interface is available and NFT_BREAK in case neither skb->sk nor input device are set. Because nf_sk_lookup_slow*() assume packet headers are in the 'in' direction, use in postrouting is not going to yield a meaningful result. Same is true for the forward chain, so restrict the use to prerouting, input and output. Use in output work if a socket is already attached to the skb. Fixes: 554ced0a6e29 ("netfilter: nf_tables: add support for native socket matching") Reported-and-tested-by: Topi Miettinen Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nft_socket.c | 52 ++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 14 deletions(-) --- a/net/netfilter/nft_socket.c +++ b/net/netfilter/nft_socket.c @@ -14,6 +14,32 @@ struct nft_socket { }; }; +static struct sock *nft_socket_do_lookup(const struct nft_pktinfo *pkt) +{ + const struct net_device *indev = nft_in(pkt); + const struct sk_buff *skb = pkt->skb; + struct sock *sk = NULL; + + if (!indev) + return NULL; + + switch (nft_pf(pkt)) { + case NFPROTO_IPV4: + sk = nf_sk_lookup_slow_v4(nft_net(pkt), skb, indev); + break; +#if IS_ENABLED(CONFIG_NF_TABLES_IPV6) + case NFPROTO_IPV6: + sk = nf_sk_lookup_slow_v6(nft_net(pkt), skb, indev); + break; +#endif + default: + WARN_ON_ONCE(1); + break; + } + + return sk; +} + static void nft_socket_eval(const struct nft_expr *expr, struct nft_regs *regs, const struct nft_pktinfo *pkt) @@ -27,20 +53,7 @@ static void nft_socket_eval(const struct sk = NULL; if (!sk) - switch(nft_pf(pkt)) { - case NFPROTO_IPV4: - sk = nf_sk_lookup_slow_v4(nft_net(pkt), skb, nft_in(pkt)); - break; -#if IS_ENABLED(CONFIG_NF_TABLES_IPV6) - case NFPROTO_IPV6: - sk = nf_sk_lookup_slow_v6(nft_net(pkt), skb, nft_in(pkt)); - break; -#endif - default: - WARN_ON_ONCE(1); - regs->verdict.code = NFT_BREAK; - return; - } + sk = nft_socket_do_lookup(pkt); if (!sk) { regs->verdict.code = NFT_BREAK; @@ -123,6 +136,16 @@ static int nft_socket_dump(struct sk_buf return 0; } +static int nft_socket_validate(const struct nft_ctx *ctx, + const struct nft_expr *expr, + const struct nft_data **data) +{ + return nft_chain_validate_hooks(ctx->chain, + (1 << NF_INET_PRE_ROUTING) | + (1 << NF_INET_LOCAL_IN) | + (1 << NF_INET_LOCAL_OUT)); +} + static struct nft_expr_type nft_socket_type; static const struct nft_expr_ops nft_socket_ops = { .type = &nft_socket_type, @@ -130,6 +153,7 @@ static const struct nft_expr_ops nft_soc .eval = nft_socket_eval, .init = nft_socket_init, .dump = nft_socket_dump, + .validate = nft_socket_validate, }; static struct nft_expr_type nft_socket_type __read_mostly = {