Skip to contentSkip to conversation

evaluate: fix double free on dtype release

PATCH In review Florian Westphal 3 participants 4 messages Dec 5, 2023 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 evaluate: fix double free on dtype release

We release ->dtype twice, will either segfault or assert on dtype->refcount != 0 check in datatype_free().

---
 src/evaluate.c                                              | 2 +-
 .../bogons/nft-f/double-free-on-binop-dtype_assert          | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert
src/evaluate.c +1 -1
index 16ad6473db1a..58cc811aca9a 100644
@@ -1171,7 +1171,7 @@ static int expr_evaluate_prefix(struct eval_ctx *ctx, struct expr **expr)
 	base = prefix->prefix;
 	assert(expr_is_constant(base));
 
-	prefix->dtype	  = base->dtype;
+	prefix->dtype	  = datatype_get(base->dtype);
 	prefix->byteorder = base->byteorder;
 	prefix->len	  = base->len;
 	prefix->flags	 |= EXPR_F_CONSTANT;
tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert new file +6 -0
new file mode 100644
index 000000000000..b7a9a1cc7e8b
@@ -0,0 +1,6 @@
+table inet t {
+	chain c {
+		udp length . @th,160,118 vmap { 47-63 . 0xe3731353631303331313037353532/3 : accept }
+		jump noexist # only here so this fails to load after patch.
+	}
+}
Signature · 1 line
2.41.0
#1 lore raw
Quoted 15 lines · Florian Westphal

We release ->dtype twice, will either segfault or assert on dtype->refcount != 0 check in datatype_free().

---
 src/evaluate.c                                              | 2 +-
 .../bogons/nft-f/double-free-on-binop-dtype_assert          | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert
src/evaluate.c +0 -0
index 16ad6473db1a..58cc811aca9a 100644

src/evaluate.c static int expr_evaluate_prefix(struct eval_ctx *ctx, struct expr **expr) · line 1174

Quoted 5 lines
src/evaluate.c +1 -1
@@ -1171,7 +1171,7 @@ static int expr_evaluate_prefix(struct eval_ctx *ctx, struct expr **expr)
 	base = prefix->prefix;
 	assert(expr_is_constant(base));
 
-	prefix->dtype	  = base->dtype;
+	prefix->dtype	  = datatype_get(base->dtype);

I prefer datatype_clone() just in case base->dtype gets updated for whatever reason.

Quoted 19 lines
src/evaluate.c +0 -0
 	prefix->byteorder = base->byteorder;
 	prefix->len	  = base->len;
 	prefix->flags	 |= EXPR_F_CONSTANT;
tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert new file +6 -0
new file mode 100644
index 000000000000..b7a9a1cc7e8b
@@ -0,0 +1,6 @@
+table inet t {
+	chain c {
+		udp length . @th,160,118 vmap { 47-63 . 0xe3731353631303331313037353532/3 : accept }
+		jump noexist # only here so this fails to load after patch.
+	}
+}
Signature · 1 line
2.41.0
#2 lore raw
Quoted 7 lines · Pablo Neira Ayuso

src/evaluate.c static int expr_evaluate_prefix(struct eval_ctx *ctx, struct expr **expr) · line 1174

Quoted 3 lines · Florian Westphal

src/evaluate.c +1 -1
-	prefix->dtype	  = base->dtype;
+	prefix->dtype	  = datatype_get(base->dtype);

I prefer datatype_clone() just in case base->dtype gets updated for whatever reason.

Hi,

That seems unnecessary.

struct datatype is a ref-counted, immutable data structure. That is a great feature and callers should rely on it.

In "[PATCH nft 0/5] more various cleanups related to struct datatype" all modifications move inside "datatype.c". This makes it clearer that modifications happen during initialization only. Regardless, also on master the instance is never mutated, after passing around the pointer.

Thomas

#3 lore raw
Quoted 13 lines · Thomas Haller
Quoted 7 lines · Pablo Neira Ayuso

src/evaluate.c static int expr_evaluate_prefix(struct eval_ctx *ctx, struct expr **expr) · line 1174

Quoted 3 lines · Florian Westphal

src/evaluate.c +1 -1
-	prefix->dtype	  = base->dtype;
+	prefix->dtype	  = datatype_get(base->dtype);

I prefer datatype_clone() just in case base->dtype gets updated for whatever reason.

Hi,

That seems unnecessary.

Quoted 8 lines

struct datatype is a ref-counted, immutable data structure. That is a great feature and callers should rely on it.

In "[PATCH nft 0/5] more various cleanups related to struct datatype" all modifications move inside "datatype.c". This makes it clearer that modifications happen during initialization only. Regardless, also on master the instance is never mutated, after passing around the pointer.

datatype_get() is perfectly fine for this case as you point out. No update of prefix->prefix datatype is done indeed.

Thanks.