From patchwork Fri Mar 10 11:23:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Sutter X-Patchwork-Id: 1755172 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nwl.cc header.i=@nwl.cc header.a=rsa-sha256 header.s=mail2022 header.b=i0RQ/0eq; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by legolas.ozlabs.org (Postfix) with ESMTP id 4PY3bS0d27z2469 for ; Fri, 10 Mar 2023 22:24:04 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230176AbjCJLYB (ORCPT ); Fri, 10 Mar 2023 06:24:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229959AbjCJLYA (ORCPT ); Fri, 10 Mar 2023 06:24:00 -0500 Received: from orbyte.nwl.cc (orbyte.nwl.cc [IPv6:2001:41d0:e:133a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34E02BCBA2 for ; Fri, 10 Mar 2023 03:23:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nwl.cc; s=mail2022; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=HyAmSxyGLydAq3qIffRhAyrcdvU3qClJCKEdvzuLrAE=; b=i0RQ/0eqDyS6bXyB6tCfhjYohy xSe65srTaLwHWC7Iezcuqy/M5Zr+cVFv5g9BUL9Hm8t2SIKPY0evVWpq9mynVkaKDHCEXkM0E3lv6 x5ARK2uMT6jCDxeU/DxRsNX1z8Xw4VoWTUF4OY4no1vsePwFIz75Y+SYqXVIEm6yGruPps/jpZApz ax0ZXj/mUXSKFXacmqzcyIx0JOphi/E77vUDQaGXbSye6lLzTKctuxQHv0t8SqJvigUyeCT94UFMv QhgLUyN2g2AJJ1mumMth7PncHUkC8NWPpL5LVm7xnm4q5Z0bu1b09Zzld1x46bz3qc5cAHdgx0sZs 4NIzeQXA==; Received: from localhost ([::1] helo=xic) by orbyte.nwl.cc with esmtp (Exim 4.94.2) (envelope-from ) id 1paaqr-0001NC-KR; Fri, 10 Mar 2023 12:23:57 +0100 From: Phil Sutter To: Pablo Neira Ayuso Cc: netfilter-devel@vger.kernel.org Subject: [nft PATCH v2] Reject invalid chain priority values in user space Date: Fri, 10 Mar 2023 12:23:48 +0100 Message-Id: <20230310112348.32373-1-phil@nwl.cc> X-Mailer: git-send-email 2.38.0 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: netfilter-devel@vger.kernel.org The kernel doesn't accept nat type chains with a priority of -200 or below. Catch this and provide a better error message than the kernel's EOPNOTSUPP. Signed-off-by: Phil Sutter --- Changes since v1: - Pull mpz_export_data() call out of the conditional. - Check priority value before calling strcmp(), it's less expensive. - Reword the error message as suggested. --- src/evaluate.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/evaluate.c b/src/evaluate.c index d24f8b66b0de8..21831201519dd 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -4842,6 +4842,8 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) } if (chain->flags & CHAIN_F_BASECHAIN) { + int priority; + chain->hook.num = str2hooknum(chain->handle.family, chain->hook.name); if (chain->hook.num == NF_INET_NUMHOOKS) @@ -4854,6 +4856,13 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) return __stmt_binary_error(ctx, &chain->priority.loc, NULL, "invalid priority expression %s in this context.", expr_name(chain->priority.expr)); + + mpz_export_data(&priority, chain->priority.expr->value, + BYTEORDER_HOST_ENDIAN, sizeof(int)); + if (priority <= -200 && !strcmp(chain->type.str, "nat")) + return __stmt_binary_error(ctx, &chain->priority.loc, NULL, + "Chains of type \"nat\" must have a priority value above -200."); + if (chain->policy) { expr_set_context(&ctx->ectx, &policy_type, NFT_NAME_MAXLEN * BITS_PER_BYTE);