[PATCH v2] acl: reject rules with inverted range

Konstantin Ananyev konstantin.v.ananyev at yandex.ru
Thu Nov 12 10:58:16 CET 2026




> rte_acl_build() doesn't check that for a RANGE field the low bound
> (value) doesn't exceed the high bound (mask_range). Field types are
> known only from the rte_acl_config passed to rte_acl_build(), so
> rte_acl_add_rules() doesn't validate ranges. Such a rule is accepted,
> and then:
> - for a 1-byte field the build crashes with a NULL pointer
>    dereference in acl_gen_node();
> - for a 2-byte field (e.g. port range 80..22) the build succeeds,
>    but the rule matches every value of the field instead of none.
>
> The check was done only for the legacy ipv4vlan rules
> (acl_ipv4vlan_check_rule(), now in app/test). Validate RANGE fields
> of all rules in acl_check_bld_param(), together with the rest of the
> build parameters, and fail rte_acl_build() with -EINVAL.
> Add a test case with the generic API.
>
> Fixes: dc276b5780c2 ("acl: new library")
> Cc: stable at dpdk.org
>
> Signed-off-by: Andrey Mokrushin <a.mokrushin at ideco.ru>
> ---
> v2:
> - move range validation from acl_build_rules() to acl_check_bld_param()
>    so rules are validated once per rte_acl_build() call (Dmitry Kozlyuk);
> - reword commit message: field types are known only at build time
>    (Konstantin Ananyev).
>
>   app/test/test_acl.c | 26 ++++++++++++++++++
>   lib/acl/acl_bld.c   | 67 ++++++++++++++++++++++++++++++++++++++++++++-
>   2 files changed, 92 insertions(+), 1 deletion(-)
>
> diff --git a/app/test/test_acl.c b/app/test/test_acl.c
> index 5d207c8..47786bc 100644
> --- a/app/test/test_acl.c
> +++ b/app/test/test_acl.c
> @@ -1274,6 +1274,7 @@ test_invalid_rules(void)
>   	int ret;
>   
>   	struct rte_acl_ipv4vlan_rule rule;
> +	struct acl_ipv4vlan_rule rv;
>   
>   	acx = rte_acl_create(&acl_param);
>   	if (acx == NULL) {
> @@ -1338,6 +1339,31 @@ test_invalid_rules(void)
>   		goto err;
>   	}
>   
> +	/* test inverted high/low destination ports added through the
> +	 * generic API: rte_acl_add_rules() doesn't know the field types,
> +	 * so the rule has to be rejected by rte_acl_build().
> +	 */
> +	rule = acl_rule;
> +	rule.data.userdata = 1;
> +	rule.dst_port_low = 0xfff0;
> +	rule.dst_port_high = 0x0010;
> +
> +	acl_ipv4vlan_convert_rule(&rule, &rv);
> +	ret = rte_acl_add_rules(acx, (struct rte_acl_rule *)&rv, 1);
> +	if (ret != 0) {
> +		printf("Line %i: Adding rules to ACL context failed!\n",
> +			__LINE__);
> +		goto err;
> +	}
> +
> +	ret = rte_acl_ipv4vlan_build(acx, ipv4_7tuple_layout,
> +		RTE_ACL_MAX_CATEGORIES);
> +	if (ret != -EINVAL) {
> +		printf("Line %i: Building ACL context "
> +				"should have failed!\n", __LINE__);
> +		goto err;
> +	}
> +
>   	rte_acl_free(acx);
>   
>   	return 0;
> diff --git a/lib/acl/acl_bld.c b/lib/acl/acl_bld.c
> index 99d1dbc..534a0e1 100644
> --- a/lib/acl/acl_bld.c
> +++ b/lib/acl/acl_bld.c
> @@ -1548,6 +1548,71 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx,
>   	return rc;
>   }
>   
> +/*
> + * Check that for all RANGE fields lower bound doesn't exceed upper bound.
> + */
> +static int
> +acl_check_rule_range(const struct rte_acl_config *cfg,
> +	const struct rte_acl_rule *rule)
> +{
> +	uint32_t n;
> +	uint64_t lo, hi;
> +	const struct rte_acl_field *fld;
> +
> +	for (n = 0; n != cfg->num_fields; n++) {
> +		if (cfg->defs[n].type != RTE_ACL_FIELD_TYPE_RANGE)
> +			continue;
> +
> +		fld = rule->field + cfg->defs[n].field_index;
> +		switch (cfg->defs[n].size) {
> +		case sizeof(uint8_t):
> +			lo = fld->value.u8;
> +			hi = fld->mask_range.u8;
> +			break;
> +		case sizeof(uint16_t):
> +			lo = fld->value.u16;
> +			hi = fld->mask_range.u16;
> +			break;
> +		case sizeof(uint32_t):
> +			lo = fld->value.u32;
> +			hi = fld->mask_range.u32;
> +			break;
> +		default:
> +			lo = fld->value.u64;
> +			hi = fld->mask_range.u64;
> +			break;
> +		}
> +
> +		if (lo > hi)
> +			return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +/*
> + * Check that rules added to the context are valid for the given config.
> + */
> +static int
> +acl_check_rules(const struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)
> +{
> +	uint32_t i;
> +	const struct rte_acl_rule *rule;
> +
> +	for (i = 0; i != ctx->num_rules; i++) {
> +		rule = (const struct rte_acl_rule *)
> +			((uintptr_t)ctx->rules + ctx->rule_sz * i);
> +		if (acl_check_rule_range(cfg, rule) != 0) {
> +			ACL_LOG(ERR,
> +			"ACL context: %s, rule[%u] (userdata: %u): low bound exceeds high bound of a range field",
> +			ctx->name, i, rule->data.userdata);
> +			return -EINVAL;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
>   /*
>    * Check that parameters for acl_build() are valid.
>    */
> @@ -1588,7 +1653,7 @@ acl_check_bld_param(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)
>   		}
>   	}
>   
> -	return 0;
> +	return acl_check_rules(ctx, cfg);
>   }

Acked-by: Konstantin Ananyev <konstantin.v.ananyev at yandex.ru>




More information about the dev mailing list