Skip to contentSkip to conversation

evaluate: fix shift exponent underflow in concatenation evaluation

PATCH New Pablo Neira Ayuso 1 participant 2 messages Dec 22, 2022 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/2 evaluate: fix shift exponent underflow in concatenation evaluation

There is an underflow of the index that iterates over the concatenation:

../include/datatype.h:292:15: runtime error: shift exponent 4294967290 is too large for 32-bit type 'unsigned int'

set the datatype to invalid which is fine to evaluate a concatenation in a set/map statement.

Update b8e1940aa190 ("tests: add a test case for map update from packet path with concat") so it does not need a workaround to work.

---
v2: complete commit description (incomplete / broken sentence in previous patch)

 src/evaluate.c                                                | 2 +-
 .../testcases/maps/dumps/typeof_maps_concat_update_0.nft      | 2 +-
 tests/shell/testcases/maps/typeof_maps_concat_update_0        | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
src/evaluate.c +1 -1
index c04cb91d3919..70adb847d475 100644
@@ -1357,7 +1357,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 			dsize = key->len;
 			bo = key->byteorder;
 			off--;
-		} else if (dtype == NULL) {
+		} else if (dtype == NULL || off == 0) {
 			tmp = datatype_lookup(TYPE_INVALID);
 		} else {
 			tmp = concat_subtype_lookup(type, --off);
tests/shell/testcases/maps/dumps/typeof_maps_concat_update_0.nft +1 -1
index d91b795fa000..a2c3c139936b 100644
@@ -1,6 +1,6 @@
 table ip foo {
 	map pinned {
-		typeof ip daddr . tcp dport : ip daddr . tcp dport
+		typeof ip saddr . ct original proto-dst : ip daddr . tcp dport
 		size 65535
 		flags dynamic,timeout
 		timeout 6m
tests/shell/testcases/maps/typeof_maps_concat_update_0 +2 -2
index 645ae142a5af..e996f14e1830 100755
@@ -4,13 +4,13 @@
 
 EXPECTED="table ip foo {
  map pinned {
-	typeof ip daddr . tcp dport : ip daddr . tcp dport
+	typeof ip saddr . ct original proto-dst : ip daddr . tcp dport
 	size 65535
 	flags dynamic,timeout
         timeout 6m
   }
   chain pr {
-     meta l4proto tcp update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
+     update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
   }
 }"
 
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 ct: use inet_service_type for proto-src and proto-dst

Instead of using the invalid type.

Problem was uncovered by this ruleset:

 table ip foo {
        map pinned {
                typeof ip daddr . ct original proto-dst : ip daddr . tcp dport
                size 65535
                flags dynamic,timeout
                timeout 6m
        }

        chain pr {
                meta l4proto tcp update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
        }

}

resulting in the following misleading error:

map-broken.nft:10:51-82: Error: datatype mismatch: expected concatenation of (IPv4 address), expression has type concatenation of (IPv4 address, internet network service)

                meta l4proto tcp update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
                                 ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 src/ct.c                                                      | 4 ++--
 .../testcases/maps/dumps/typeof_maps_concat_update_0.nft      | 1 +
 tests/shell/testcases/maps/typeof_maps_concat_update_0        | 1 +
 3 files changed, 4 insertions(+), 2 deletions(-)
src/ct.c +2 -2
index e246d3039240..64327561d089 100644
@@ -271,10 +271,10 @@ const struct ct_template ct_templates[__NFT_CT_MAX] = {
 	[NFT_CT_PROTOCOL]	= CT_TEMPLATE("protocol",   &inet_protocol_type,
 					      BYTEORDER_BIG_ENDIAN,
 					      BITS_PER_BYTE),
-	[NFT_CT_PROTO_SRC]	= CT_TEMPLATE("proto-src",  &invalid_type,
+	[NFT_CT_PROTO_SRC]	= CT_TEMPLATE("proto-src",  &inet_service_type,
 					      BYTEORDER_BIG_ENDIAN,
 					      2 * BITS_PER_BYTE),
-	[NFT_CT_PROTO_DST]	= CT_TEMPLATE("proto-dst",  &invalid_type,
+	[NFT_CT_PROTO_DST]	= CT_TEMPLATE("proto-dst",  &inet_service_type,
 					      BYTEORDER_BIG_ENDIAN,
 					      2 * BITS_PER_BYTE),
 	[NFT_CT_LABELS]		= CT_TEMPLATE("label", &ct_label_type,
tests/shell/testcases/maps/dumps/typeof_maps_concat_update_0.nft +1 -0
index a2c3c139936b..f8b574f4e0cb 100644
@@ -8,5 +8,6 @@ table ip foo {
 
 	chain pr {
 		update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
+		update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
 	}
 }
tests/shell/testcases/maps/typeof_maps_concat_update_0 +1 -0
index e996f14e1830..2a52ea0e3220 100755
@@ -11,6 +11,7 @@ EXPECTED="table ip foo {
   }
   chain pr {
      update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
+     meta l4proto tcp update @pinned { ip saddr . ct original proto-dst timeout 1m30s : ip daddr . tcp dport }
   }
 }"
 
Signature · 1 line
2.30.2