Skip to contentSkip to conversation

Reject invalid chain priority values in user space

PATCH Superseded Phil Sutter 2 participants 3 messages Mar 10, 2023 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 Reject invalid chain priority values in user space

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.

---
 src/evaluate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/evaluate.c +10 -0
index d24f8b66b0de8..af4844c1ef6cc 100644
@@ -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,14 @@ 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));
+
+		if (!strcmp(chain->type.str, "nat") &&
+		    (mpz_export_data(&priority, chain->priority.expr->value,
+				    BYTEORDER_HOST_ENDIAN, sizeof(int))) &&
+		    priority <= -200)
+			return __stmt_binary_error(ctx, &chain->priority.loc, NULL,
+						   "Nat type chains must have a priority value above -200.");
+
 		if (chain->policy) {
 			expr_set_context(&ctx->ectx, &policy_type,
 					 NFT_NAME_MAXLEN * BITS_PER_BYTE);
Signature · 1 line
2.38.0
#1 lore raw
Quoted 23 lines · Phil Sutter

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.

---
 src/evaluate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/evaluate.c +2 -0
index d24f8b66b0de8..af4844c1ef6cc 100644
@@ -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)

src/evaluate.c static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) · line 4859

Quoted 4 lines
src/evaluate.c +1 -0
@@ -4854,6 +4856,14 @@ 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));
+

maybe get this here to declutter the branch?

                mpz_export_data(&priority, chain->priority.expr->value,
                                BYTEORDER_HOST_ENDIAN, sizeof(int)));

this is in basechain context, so it should be fine.

src/evaluate.c static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) · line 4865

Quoted 6 lines
src/evaluate.c +6 -0
+		if (!strcmp(chain->type.str, "nat") &&
+		    (mpz_export_data(&priority, chain->priority.expr->value,
+				    BYTEORDER_HOST_ENDIAN, sizeof(int))) &&
+		    priority <= -200)
+			return __stmt_binary_error(ctx, &chain->priority.loc, NULL,
+						   "Nat type chains must have a priority value above -200.");
                                                    ^^^

I'd suggest lower case 'nat' which is what the user specifies in the chain declaration.

Thanks for addressing my feedback.

Quoted 7 lines
src/evaluate.c +1 -0
+
 		if (chain->policy) {
 			expr_set_context(&ctx->ectx, &policy_type,
 					 NFT_NAME_MAXLEN * BITS_PER_BYTE);
Signature · 1 line
2.38.0
author reviewer
#2 lore raw
Quoted 31 lines · Pablo Neira Ayuso
Quoted 23 lines · Phil Sutter

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.

---
 src/evaluate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/evaluate.c +2 -0
index d24f8b66b0de8..af4844c1ef6cc 100644
@@ -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)

src/evaluate.c static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) · line 4859

Quoted 4 lines
src/evaluate.c +1 -0
@@ -4854,6 +4856,14 @@ 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));
+

maybe get this here to declutter the branch?

Quoted 2 lines
                mpz_export_data(&priority, chain->priority.expr->value,
                                BYTEORDER_HOST_ENDIAN, sizeof(int)));

Will do. Initially I wanted to use mpz_get_si() but it expects a larger data size. It even works if one casts the return value to int, but I guess it won't anymore on Big Endian (and is a hack anyway).

Quoted 1 line

this is in basechain context, so it should be fine.

Yes, indeed. Also the call to evaluate_priority() ensures chain->priority.expr is as expected.

Quoted 10 lines

src/evaluate.c static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) · line 4865

Quoted 6 lines
src/evaluate.c +6 -0
+		if (!strcmp(chain->type.str, "nat") &&
+		    (mpz_export_data(&priority, chain->priority.expr->value,
+				    BYTEORDER_HOST_ENDIAN, sizeof(int))) &&
+		    priority <= -200)
+			return __stmt_binary_error(ctx, &chain->priority.loc, NULL,
+						   "Nat type chains must have a priority value above -200.");
                                                    ^^^

I'd suggest lower case 'nat' which is what the user specifies in the chain declaration.

I'll rewrite the sentence.

Quoted 1 line

Thanks for addressing my feedback.

Thanks for the quick review!