Skip to contentSkip to conversation

expression: don't try to import empty string

PATCH In review Florian Westphal 2 participants 4 messages Mar 27, 2025 – Mar 31, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 expression: don't try to import empty string

The bogon will trigger the assertion in mpz_import_data:
src/expression.c:418: constant_expr_alloc: Assertion `(((len) + (8) - 1) / (8)) > 0' failed.

---
 src/expression.c                              |  2 +-
 .../bogons/nft-j-f/constant_expr_alloc_assert | 38 +++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 tests/shell/testcases/bogons/nft-j-f/constant_expr_alloc_assert
src/expression.c +1 -1
index 156a66eb37f0..f230f5ad8935 100644
@@ -494,7 +494,7 @@ struct expr *constant_expr_alloc(const struct location *loc,
 	expr->flags = EXPR_F_CONSTANT | EXPR_F_SINGLETON;
 
 	mpz_init2(expr->value, len);
-	if (data != NULL)
+	if (data != NULL && len)
 		mpz_import_data(expr->value, data, byteorder,
 				div_round_up(len, BITS_PER_BYTE));
 
tests/shell/testcases/bogons/nft-j-f/constant_expr_alloc_assert new file +38 -0
new file mode 100644
index 000000000000..9c40030212ef
@@ -0,0 +1,38 @@
+{
+  "nftables": [
+    {
+      "table": {
+        "family": "ip",
+        "name": "t",
+        "handle": 0
+      }
+    },
+    {
+      "chain": {
+        "family": "ip",
+        "table": "t",
+        "name": "testchain",
+        "handle": 0
+      }
+    },
+    {
+      "map": {
+        "family": "ip",
+        "name": "testmap",
+        "table": "t",
+        "type": "ipv4_addr",
+        "handle": 0,
+        "map": "verdict",
+        "elem": [
+          [
+            {
+              "jump": {
+                "target": ""
+              }
+            }
+          ]
+        ]
+      }
+    }
+  ]
+}
Signature · 1 line
2.48.1
#1 lore raw

Hi Florian,

Quoted 2 lines · Florian Westphal

The bogon will trigger the assertion in mpz_import_data:
src/expression.c:418: constant_expr_alloc: Assertion `(((len) + (8) - 1) / (8)) > 0' failed.

I took a quick look searching for {s:s} in src/parser_json.c

The common idiom is json_parse_err() then a helper parser function to validate the string.

It seems it is missing in this case. Maybe tigthen json parser instead?

Caller invoking constant_expr_alloc() with data != NULL but no len looks broken to me.

Maybe take both patches if you prefer?

Quoted 68 lines
---
 src/expression.c                              |  2 +-
 .../bogons/nft-j-f/constant_expr_alloc_assert | 38 +++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 tests/shell/testcases/bogons/nft-j-f/constant_expr_alloc_assert
src/expression.c +1 -1
index 156a66eb37f0..f230f5ad8935 100644
@@ -494,7 +494,7 @@ struct expr *constant_expr_alloc(const struct location *loc,
 	expr->flags = EXPR_F_CONSTANT | EXPR_F_SINGLETON;
 
 	mpz_init2(expr->value, len);
-	if (data != NULL)
+	if (data != NULL && len)
 		mpz_import_data(expr->value, data, byteorder,
 				div_round_up(len, BITS_PER_BYTE));
 
tests/shell/testcases/bogons/nft-j-f/constant_expr_alloc_assert new file +38 -0
new file mode 100644
index 000000000000..9c40030212ef
@@ -0,0 +1,38 @@
+{
+  "nftables": [
+    {
+      "table": {
+        "family": "ip",
+        "name": "t",
+        "handle": 0
+      }
+    },
+    {
+      "chain": {
+        "family": "ip",
+        "table": "t",
+        "name": "testchain",
+        "handle": 0
+      }
+    },
+    {
+      "map": {
+        "family": "ip",
+        "name": "testmap",
+        "table": "t",
+        "type": "ipv4_addr",
+        "handle": 0,
+        "map": "verdict",
+        "elem": [
+          [
+            {
+              "jump": {
+                "target": ""
+              }
+            }
+          ]
+        ]
+      }
+    }
+  ]
+}
Signature · 1 line
2.48.1
src/parser_json.c +3 -0
index 04d762741e4a..ef7740840710 100644
@@ -1350,6 +1350,9 @@ static struct expr *json_parse_verdict_expr(struct json_ctx *ctx,
 		    json_unpack_err(ctx, root, "{s:s}", "target", &chain))
 			return NULL;
 
+		if (!chain || chain[0] == '\0')
+			return NULL;
+
 		return verdict_expr_alloc(int_loc, verdict_tbl[i].verdict,
 					  json_alloc_chain_expr(chain));
 	}
author maintainer
#2 lore raw
Quoted 8 lines · Pablo Neira Ayuso

Hi Florian,

Quoted 2 lines · Florian Westphal

The bogon will trigger the assertion in mpz_import_data:
src/expression.c:418: constant_expr_alloc: Assertion `(((len) + (8) - 1) / (8)) > 0' failed.

I took a quick look searching for {s:s} in src/parser_json.c

Quoted 7 lines

The common idiom is json_parse_err() then a helper parser function to validate the string.

It seems it is missing in this case. Maybe tigthen json parser instead?

Caller invoking constant_expr_alloc() with data != NULL but no len looks broken to me.

return constant_expr_alloc(int_loc, &string_type, BYTEORDER_HOST_ENDIAN,
strlen(chain) * BITS_PER_BYTE, chain);

chain name is '""'.

There are other spots where we possibly call into constant_expr_alloc() with a 0 argument.

I think it would be a lot more work and bloat to add all the checks on the json side while its a one-liner in constant_expr_alloc().

I could also add json_constant_expr_alloc() but it seems kinda silly to me.

maintainer Reviewed-by
#3 lore raw
Quoted 17 lines · Florian Westphal
Quoted 8 lines · Pablo Neira Ayuso

Hi Florian,

Quoted 2 lines · Florian Westphal

The bogon will trigger the assertion in mpz_import_data:
src/expression.c:418: constant_expr_alloc: Assertion `(((len) + (8) - 1) / (8)) > 0' failed.

I took a quick look searching for {s:s} in src/parser_json.c

Quoted 7 lines

The common idiom is json_parse_err() then a helper parser function to validate the string.

It seems it is missing in this case. Maybe tigthen json parser instead?

Caller invoking constant_expr_alloc() with data != NULL but no len looks broken to me.

Quoted 7 lines

return constant_expr_alloc(int_loc, &string_type, BYTEORDER_HOST_ENDIAN,
strlen(chain) * BITS_PER_BYTE, chain);

chain name is '""'.

There are other spots where we possibly call into constant_expr_alloc() with a 0 argument.

Quoted 2 lines

I think it would be a lot more work and bloat to add all the checks on the json side while its a one-liner in constant_expr_alloc().

I don't think this is needed, the idiom in src/parser_json.c is to:

1) fetch string
2) validate it

For example:

        if (!json_unpack(root, "{s:s}", "ttl", &ttl)) {
                if (!strcmp(ttl, "loose")) {
                        ttlval = 1;
                } else if (!strcmp(ttl, "skip")) {
                        ttlval = 2;
                } else {
                        json_error(ctx, "Invalid osf ttl option '%s'.", ttl);
                        return NULL;
                }
        }
Quoted 1 line

I could also add json_constant_expr_alloc() but it seems kinda silly to me.

I agree, I don't think json_constant_expr_alloc is needed.