evaluate: fix double free on dtype release
Florian Westphal
maintainerPATCH 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
@@ -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
@@ -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
Pablo Neira Ayuso
maintainerQuoted 15 lines · Florian Westphal
We release ->dtype twice, will either segfault or assert on
dtype->refcount!= 0 check indatatype_free().Signed-off-by Florian Westphal <[email protected]>--- 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_assertsrc/evaluate.c +0
-0
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
-0prefix->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@@ -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
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 casebase->dtypegets 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
Pablo Neira Ayuso
maintainerQuoted 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 casebase->dtypegets updated for whatever reason.Hi,
That seems unnecessary.
Quoted 8 lines
struct datatypeis 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 onmasterthe 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.