* [PATCH nft] evaluate: fix double free on dtype release
@ 2023-12-05 12:08 Florian Westphal
2023-12-05 14:19 ` Pablo Neira Ayuso
0 siblings, 1 reply; 4+ messages in thread
From: Florian Westphal @ 2023-12-05 12:08 UTC (permalink / raw)
To: netfilter-devel; +Cc: Florian Westphal
We release ->dtype twice, will either segfault or assert
on dtype->refcount != 0 check in datatype_free().
Signed-off-by: Florian Westphal <fw@strlen.de>
---
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
diff --git a/src/evaluate.c b/src/evaluate.c
index 16ad6473db1a..58cc811aca9a 100644
--- a/src/evaluate.c
+++ b/src/evaluate.c
@@ -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;
diff --git a/tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert b/tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert
new file mode 100644
index 000000000000..b7a9a1cc7e8b
--- /dev/null
+++ b/tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert
@@ -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.
+ }
+}
--
2.41.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH nft] evaluate: fix double free on dtype release
2023-12-05 12:08 [PATCH nft] evaluate: fix double free on dtype release Florian Westphal
@ 2023-12-05 14:19 ` Pablo Neira Ayuso
2023-12-05 16:53 ` Thomas Haller
0 siblings, 1 reply; 4+ messages in thread
From: Pablo Neira Ayuso @ 2023-12-05 14:19 UTC (permalink / raw)
To: Florian Westphal; +Cc: netfilter-devel
On Tue, Dec 05, 2023 at 01:08:17PM +0100, Florian Westphal wrote:
> We release ->dtype twice, will either segfault or assert
> on dtype->refcount != 0 check in datatype_free().
>
> Signed-off-by: Florian Westphal <fw@strlen.de>
> ---
> 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
>
> diff --git a/src/evaluate.c b/src/evaluate.c
> index 16ad6473db1a..58cc811aca9a 100644
> --- a/src/evaluate.c
> +++ b/src/evaluate.c
> @@ -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.
> prefix->byteorder = base->byteorder;
> prefix->len = base->len;
> prefix->flags |= EXPR_F_CONSTANT;
> diff --git a/tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert b/tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert
> new file mode 100644
> index 000000000000..b7a9a1cc7e8b
> --- /dev/null
> +++ b/tests/shell/testcases/bogons/nft-f/double-free-on-binop-dtype_assert
> @@ -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.
> + }
> +}
> --
> 2.41.0
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH nft] evaluate: fix double free on dtype release
2023-12-05 14:19 ` Pablo Neira Ayuso
@ 2023-12-05 16:53 ` Thomas Haller
2023-12-05 17:20 ` Pablo Neira Ayuso
0 siblings, 1 reply; 4+ messages in thread
From: Thomas Haller @ 2023-12-05 16:53 UTC (permalink / raw)
To: Pablo Neira Ayuso, Florian Westphal; +Cc: netfilter-devel
On Tue, 2023-12-05 at 15:19 +0100, Pablo Neira Ayuso wrote:
> On Tue, Dec 05, 2023 at 01:08:17PM +0100, Florian Westphal wrote:
> >
> > - 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH nft] evaluate: fix double free on dtype release
2023-12-05 16:53 ` Thomas Haller
@ 2023-12-05 17:20 ` Pablo Neira Ayuso
0 siblings, 0 replies; 4+ messages in thread
From: Pablo Neira Ayuso @ 2023-12-05 17:20 UTC (permalink / raw)
To: Thomas Haller; +Cc: Florian Westphal, netfilter-devel
On Tue, Dec 05, 2023 at 05:53:06PM +0100, Thomas Haller wrote:
> On Tue, 2023-12-05 at 15:19 +0100, Pablo Neira Ayuso wrote:
> > On Tue, Dec 05, 2023 at 01:08:17PM +0100, Florian Westphal wrote:
> > >
> > > - 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.
datatype_get() is perfectly fine for this case as you point out.
No update of prefix->prefix datatype is done indeed.
Thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-12-05 17:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-05 12:08 [PATCH nft] evaluate: fix double free on dtype release Florian Westphal
2023-12-05 14:19 ` Pablo Neira Ayuso
2023-12-05 16:53 ` Thomas Haller
2023-12-05 17:20 ` Pablo Neira Ayuso
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.