more misc LLM fixes
Florian Westphal
maintainercover more misc LLM fixes
Another round of LLM findings. These commits are all unrelated, except that they are LLM findigs with no observed bug reports.
1) Check mnl_nlmsg_batch_start() for a NULL return. Avoid potential NULL pointer dereference in src/common.
2) Fix uninitialised attr arrays by zero-initialising tb[] arrays.
3) Move several MNL_STRING attribute types to MNL_NUL_STRING so mnl_attr_validate enforces NUL termination. Harden future netlink-attribute parsing against non-kernel sources that may omit the terminator.
4) Validate NFTA_EXPR_NAME parse success. Upgrade string validation to NUL_STRING, since the kernel always appends a trailing NUL.
5) Forbid re-setting or clearing the object type field to prevent inconsistent state and segfaults. Check the correct subtype before freeing to guard against a latent union overlap crash.
6) Drop trailing semicolon from the xfree macro. Callers already append their own ;, so the macro's is redundant.
7) Remove the obsolete set_elem member from the ruleset union. Its last consumer (the removed json interface) no longer exists, and the shared nftnl_set_free path masked a latent type-confusion bug.
8) Clear field_len array before copying to avoid reading stale values from a prior call. Fix a bug where shrinking the array on a second invocation leaks leftover field counts.
9) Guard the s->expr_list traversal against the empty-list case to avoid returning a pointer into the middle of nftnl_set. Initialize data_len to 0.
10) Fix use-after-free by calling list_del() before freeing expressions in set_elem. Prevents dangling next/prev pointers from corrupting a subsequent list_add on the same list.
11) Clamp buf->end to the actually copied size in udata_buf_put. Prevent nftnl_udata_buf_space() from observing an end pointer past the buffer.
12) Remove all remaining MNL_STRING users, switch to MNL_NUL_STRING and mnl_attr_put_strz. Enforce NUL-termination checking on all string attributes to prevent illegal strlen/strdup on unchecked data.
Florian Westphal (12):
src: common: mnl_nlmsg_batch_start may return NULL
src: fix uninitialised attr arrays
src: move several MNL_STRING types to MNL_NUL_STRING
src: expr: make sure NFTA_EXPR_NAME was parsed successfuly
src: object: don't permit to re-set or clear object type field
include: drop semicolon at end of xfree macro
src: ruleset: remove set_elem member from union
src: set: clear field_len array before copying
src: set: check for empty list explicitly
src: set_elem: list_del before freeing
src: udata: clamp buf->end to actally copied size
src: remove all remaining MNL_STRING users
examples/nft-compat-get.c | 2 +- include/utils.h | 2 +- src/chain.c | 4 ++-- src/common.c | 4 ++++ src/expr.c | 5 ++++- src/expr/data_reg.c | 4 ++-- src/expr/dynset.c | 2 +- src/expr/flow_offload.c | 2 +- src/expr/log.c | 2 +- src/expr/lookup.c | 2 +- src/expr/objref.c | 6 +++--- src/flowtable.c | 2 +- src/obj/ct_helper.c | 4 ++-- src/obj/secmark.c | 4 ++-- src/object.c | 19 +++++++++++++++---- src/rule.c | 2 +- src/ruleset.c | 3 +-- src/set.c | 13 +++++++------ src/set_elem.c | 10 +++++++--- src/table.c | 2 +- src/trace.c | 6 +++--- src/udata.c | 5 ++++- src/utils.c | 5 +++-- 23 files changed, 68 insertions(+), 42 deletions(-)
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 1/12 src: common: mnl_nlmsg_batch_start may return NULL
LLM spots that we did not check for a NULL return value from mnl_nlmsg_batch_start().
--- src/common.c | 4 ++++ 1 file changed, 4 insertions(+)
src/common.c +4 -0
@@ -120,6 +120,10 @@ int nftnl_batch_is_supported(void) } b =mnl_nlmsg_batch_start (buf,sizeof (buf)); +if (!b) { +mnl_socket_close (nl); +return -1 ; + }nftnl_batch_begin (mnl_nlmsg_batch_current (b), seq++);mnl_nlmsg_batch_next (b);
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 2/12 src: fix uninitialised attr arrays
In reply to Florian WestphalLLM finding: tb[] arrays must be zero-initialised.
--- src/expr/data_reg.c | 2 +- src/trace.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
src/expr/data_reg.c +1 -1
@@ -166,7 +166,7 @@ static int nftnl_verdict_parse_cb(const struct nlattr *attr, void *data)static int nftnl_parse_verdict (union nftnl_data_reg *data,const struct nlattr *attr,int *type) { -struct nlattr *tb[NFTA_VERDICT_MAX +1 ]; +struct nlattr *tb[NFTA_VERDICT_MAX +1 ] = {};if (mnl_attr_parse_nested (attr, nftnl_verdict_parse_cb, tb) <0 )return -1 ;
src/trace.c +1 -1
@@ -320,7 +320,7 @@ static int nftnl_trace_parse_verdict_cb(const struct nlattr *attr, void *data)static int nftnl_trace_parse_verdict (const struct nlattr *attr,struct nftnl_trace *t) { -struct nlattr *tb[NFTA_VERDICT_MAX +1 ]; +struct nlattr *tb[NFTA_VERDICT_MAX +1 ] = {};if (mnl_attr_parse_nested (attr, nftnl_trace_parse_verdict_cb, tb) <0 )return -1 ;
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 3/12 src: move several MNL_STRING types to MNL_NUL_STRING
In reply to Florian WestphalLLM finding:mnl_attr_get_str() returns a pointer into a fixed-length TLV payload with no guaranteed NUL terminator.
This is the classic 'NLA_STRING' vs. 'NUL_STRING'.
libmnl only checks for terminating NUL when asked to validate a NUL_STRING attribute. As we parse nf_tables data coming from kernel, we can actually rely on the kernel including the terminating NUL character for us, even for NLA_STRING.
Just switch those affected attributes (strdup() after mnl_attr_validate(... MNL_STRING) to MNL_NUL_STRING.
This doesn't break any existing users, closes the finding and hardens future users that might parse netlink attributes coming from a different source than the kernel.
--- src/chain.c | 2 +- src/expr/data_reg.c | 2 +- src/set.c | 2 +- src/utils.c | 5 +++-- 4 files changed, 6 insertions(+), 5 deletions(-)
src/chain.c +1 -1
@@ -613,7 +613,7 @@ static int nftnl_chain_parse_hook_cb(const struct nlattr *attr, void *data)abi_breakage ();break ;case NFTA_HOOK_DEV : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ; }
src/expr/data_reg.c +1 -1
@@ -155,7 +155,7 @@ static int nftnl_verdict_parse_cb(const struct nlattr *attr, void *data)abi_breakage ();break ;case NFTA_VERDICT_CHAIN : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ; }
src/set.c +1 -1
@@ -491,7 +491,7 @@ static int nftnl_set_parse_attr_cb(const struct nlattr *attr, void *data)case NFTA_SET_TABLE :case NFTA_SET_NAME :case NFTA_SET_TYPE : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_SET_HANDLE :
src/utils.c +3 -2
@@ -200,14 +200,15 @@ void nftnl_attr_put_ifname(struct nlmsghdr *nlh, const char *ifname)char *nftnl_attr_get_ifname (const struct nlattr *attr) { +uint16_t dlen =mnl_attr_get_payload_len (attr);const char *dev =mnl_attr_get_str (attr);char buf[IFNAMSIZ ];switch (mnl_attr_get_type (attr)) {case NFTA_DEVICE_NAME : -return strdup (dev); +return strndup (dev, dlen);case NFTA_DEVICE_PREFIX : -snprintf (buf,IFNAMSIZ ,"%s*" , dev); +snprintf (buf,IFNAMSIZ ,"%.*s*" , (int )dlen, dev);return strdup (buf);default :return NULL ;
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 4/12 src: expr: make sure NFTA_EXPR_NAME was parsed successfuly
In reply to Florian WestphalLLM finding. Not a bug atm, the kernel always includes this. While at it, upgrade to NUL_STRING. Kernel always sets a trailing NUL even for NLA_STRING, so we can enforce this.
--- src/expr.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
src/expr.c +4 -1
@@ -251,7 +251,7 @@ static int nftnl_rule_parse_expr_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_EXPR_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_EXPR_DATA :
@@ -272,6 +272,9 @@ struct nftnl_expr *nftnl_expr_parse(struct nlattr *attr)if (mnl_attr_parse_nested (attr, nftnl_rule_parse_expr_cb, tb) <0 ) goto err1; +if (!tb[NFTA_EXPR_NAME ]) + goto err1; + expr =nftnl_expr_alloc (mnl_attr_get_str (tb[NFTA_EXPR_NAME ]));if (expr ==NULL ) goto err1;
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 5/12 src: object: don't permit to re-set or clear object type field
In reply to Florian WestphalLLM finding:
If obj->ops was previously valid (object already typed) and the caller passes an invalid type, obj->ops is nulled. The object is left in an inconsistent state. flags still has the NFTNL_OBJ_TYPE bit set.
Subsequent default-path sets will fail. get/build/output calls that dereference obj->ops will segfault.
Don't permit identity change.
The object type uses a set of common flags plus a set of type specific flags. The latter overlaps.
Permitting identity switch is just asking for trouble. Moreover, there is a latent crash bug:
If a future type introduces a flag overlapping with NFTNL_OBJ_TUNNEL_OPTS, we free a pointer inside union.
Check we have the correct subtype before freeing. This is NOT a bug at this time, nftables neither switches ops nor does it call nftnl_obj_unset().
--- src/object.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
src/object.c +14 -3
@@ -56,7 +56,8 @@ void nftnl_obj_free(const struct nftnl_obj *obj)xfree (obj->name);if (obj->flags & (1 <<NFTNL_OBJ_USERDATA ))nftnl_mem_blob_free (&obj->user_data); -if (obj->flags & (1 <<NFTNL_OBJ_TUNNEL_OPTS )) { +if (obj->ops && obj->ops->type ==NFT_OBJECT_TUNNEL && + obj->flags & (1 <<NFTNL_OBJ_TUNNEL_OPTS )) {nftnl_tunnel_opts_free (obj->data.tunnel.tun_opts);xfree (obj->data.tunnel.tun_opts); }
@@ -86,13 +87,21 @@ void nftnl_obj_unset(struct nftnl_obj *obj, uint16_t attr)case NFTNL_OBJ_USERDATA :nftnl_mem_blob_free (&obj->user_data);break ; -case NFTNL_OBJ_TYPE :case NFTNL_OBJ_FAMILY :case NFTNL_OBJ_USE :case NFTNL_OBJ_HANDLE :break ; +case NFTNL_OBJ_TYPE : +/* don't allow to morph one object type +* to another. +*/ +return ;default : -break ; +/* object type specific. Clearing e.g. +* NFTNL_OBJ_TUNNEL_OPTS results +* in memory leak. +*/ +return ; } obj->flags &= ~(1 << attr);
@@ -121,6 +130,8 @@ int nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,return nftnl_set_str_attr (&obj->name, &obj->flags, attr, data, data_len);case NFTNL_OBJ_TYPE : +if (obj->ops)/* don't permit object type change */ +return -1 ; obj->ops =nftnl_obj_ops_lookup (*((uint32_t *)data));if (!obj->ops)return -1 ;
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 6/12 include: drop semicolon at end of xfree macro
In reply to Florian Westphal... not required, callers do 'xfree(x);' already.
--- include/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/utils.h +1 -1
@@ -19,7 +19,7 @@#define __noreturn __attribute__((__noreturn__)) -#define xfree (ptr) free((void *)ptr); +#define xfree (ptr) free((void *)ptr)#define div_round_up (n, d) (((n) + (d) - 1) / (d))
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 7/12 src: ruleset: remove set_elem member from union
In reply to Florian WestphalLLM flags this bug:
case NFTNL_RULESET_SET:
case NFTNL_RULESET_SET_ELEMS:
nftnl_set_free(ctx->set);
break;... because it hints at type confusion, SET_ELEMS
should call nftnl_set_elem_free(ctx->set_elem).
However, this entire API is obsolete, nftables doesn't use this anymore. The last user of NFTNL_RULESET_SET_ELEMS went away when the json interface was removed from libnftnl.
Drop the member from the union.
--- src/ruleset.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
src/ruleset.c +1 -2
@@ -36,7 +36,6 @@ struct nftnl_parse_ctx {struct nftnl_chain *chain;struct nftnl_rule *rule;struct nftnl_set *set; -struct nftnl_set_elem *set_elem; };void *data;
@@ -158,9 +157,9 @@ void nftnl_ruleset_ctx_free(const struct nftnl_parse_ctx *ctx)nftnl_rule_free (ctx->rule);break ;case NFTNL_RULESET_SET : -case NFTNL_RULESET_SET_ELEMS :nftnl_set_free (ctx->set);break ; +case NFTNL_RULESET_SET_ELEMS :case NFTNL_RULESET_RULESET :case NFTNL_RULESET_UNSPEC :break ;
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 8/12 src: set: clear field_len array before copying
In reply to Florian WestphalLLM flagged:
if (data_len > sizeof(s->desc.field_len))
return -1;
memcpy(&s->desc.field_len, data, data_len);
for (s->desc.field_count = 0;
s->desc.field_count < NFT_REG32_COUNT;
s->desc.field_count++) {
if (!s->desc.field_len[s->desc.field_count]) // herei.e. it relies on the inital zero state.
If for any reason a 2nd call is made that shinks field_len array, the loop would read stale count from previous incarnation.
--- src/set.c | 1 + 1 file changed, 1 insertion(+)
src/set.c +1 -0
@@ -188,6 +188,7 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,if (data_len >sizeof (s->desc.field_len))return -1 ; +memset (&s->desc.field_len,0 ,sizeof (s->desc.field_len));memcpy (&s->desc.field_len, data, data_len); for (s->desc.field_count = 0; s->desc.field_count < NFT_REG32_COUNT;
Signature · 1 line
2.55.0
Phil Sutter
reviewerRe: [PATCH libftnl 08/12] src: set: clear field_len array before copying
Quoted 10 lines · Florian Westphal
LLM flagged:
if (data_len > sizeof(s->desc.field_len)) return -1; memcpy(&s->desc.field_len, data, data_len); for (s->desc.field_count = 0; s->desc.field_count < NFT_REG32_COUNT; s->desc.field_count++) { if (!s->desc.field_len[s->desc.field_count]) // here
Quoted 3 lines
i.e. it relies on the inital zero state.
If for any reason a 2nd call is made that shinks
field_len
~~~~~~
*shrinks
Quoted 25 lines
array, the loop would read stale count from previous incarnation.
Signed-off-by Florian Westphal <[email protected]>--- src/set.c | 1 + 1 file changed, 1 insertion(+)src/set.c +1
-0@@ -188,6 +188,7 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,if (data_len >sizeof (s->desc.field_len))return -1 ; +memset (&s->desc.field_len,0 ,sizeof (s->desc.field_len));memcpy (&s->desc.field_len, data, data_len); for (s->desc.field_count = 0; s->desc.field_count < NFT_REG32_COUNT;Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 9/12 src: set: check for empty list explicitly
In reply to Florian WestphalLLM finding:
When s->expr_list is empty the for-init assigns expr = container_of(&s->expr_list, nftnl_expr, head).
Since head is at offset 0 in nftnl_expr, this yields expr == (nftnl_expr*)&s->expr_list: a pointer into the middle of the nftnl_set struct.
The function returns this bogus pointer.
Also set data_len to 0; normally users would use the _str getter which ignores the data_len anyway, but its better to initialise it to a known value.
--- src/set.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
src/set.c +5 -5
@@ -253,8 +253,6 @@ EXPORT_SYMBOL(nftnl_set_get_data);const void *nftnl_set_get_data (const struct nftnl_set *s,uint16_t attr,uint32_t *data_len) { -struct nftnl_expr *expr; -if (!(s->flags & (1 << attr)))return NULL ;
@@ -313,9 +311,11 @@ const void *nftnl_set_get_data(const struct nftnl_set *s, uint16_t attr,case NFTNL_SET_USERDATA :return nftnl_mem_blob_get (&s->user_data, data_len);case NFTNL_SET_EXPR : -list_for_each_entry (expr, &s->expr_list, head) -break ; -return expr; + *data_len =0 ; +if (list_empty (&s->expr_list)) +return NULL ; + + return list_entry(s->expr_list.next, struct nftnl_expr, head); }return NULL ; }
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 10/12 src: set_elem: list_del before freeing
In reply to Florian WestphalLLM finding:nftnl_expr_free() calls xfree(expr).
It never calls list_del(&expr->head).
After the loop, expr_list.next/.prev point into freed heap memory. Next list_add results in UaF.
Looks like noone does such re-use, but as the API allows it, make this safe.
--- src/set_elem.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
src/set_elem.c +6 -2
@@ -88,8 +88,10 @@ void nftnl_set_elem_unset(struct nftnl_set_elem *s, uint16_t attr)break ;case NFTNL_SET_ELEM_EXPR :case NFTNL_SET_ELEM_EXPRESSIONS : -list_for_each_entry_safe (expr, tmp, &s->expr_list, head) +list_for_each_entry_safe (expr, tmp, &s->expr_list, head) { +list_del (&expr->head);nftnl_expr_free (expr); + }break ;case NFTNL_SET_ELEM_OBJREF :xfree (s->objref);
@@ -167,8 +169,10 @@ __nftnl_set_elem_set(struct nftnl_set_elem *s, uint16_t attr, const void *data,return -1 ;break ;case NFTNL_SET_ELEM_EXPR : -list_for_each_entry_safe (expr, tmp, &s->expr_list, head) +list_for_each_entry_safe (expr, tmp, &s->expr_list, head) { +list_del (&expr->head);nftnl_expr_free (expr); + } expr = (void *)data;list_add (&expr->head, &s->expr_list);
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 11/12 src: udata: clamp buf->end to actally copied size
In reply to Florian WestphalLLM finding:
nftnl_udata_buf_space() doesn't tolerate buf->end moving past the size.
Clamp the end pointer in udata_buf_put so that it matches what was actually copied. After this, nftnl_udata_buf_space() will return 0 if the space was exhausted.
--- src/udata.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
src/udata.c +4 -1
@@ -53,7 +53,10 @@ EXPORT_SYMBOL(nftnl_udata_buf_put);void nftnl_udata_buf_put (struct nftnl_udata_buf *buf,const void *data,uint32_t len) { -memcpy (buf->data, data, len <= buf->size ? len : buf->size); +if (len > buf->size) + len = buf->size; + +memcpy (buf->data, data, len); buf->end = buf->data + len; }
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 12/12 src: remove all remaining MNL_STRING users
In reply to Florian WestphalSwitch everything over to MNL_NUL_STRING and mnl_attr_put_strz.
The kernel always emits NLA_STRING attributes with the trailing 0 (strlen() + 1), so this is safe for kernel-originating data.
For non-kernel-originating data, the lack of the NUL check means greater care needs to be taken on parser side, plain strlen, strdup, etc is illegal, a known source of bugs.
We already used mnl_attr_put_strz() almost everywhere, this converts the rest.
No bug report behing this, its related to other LLM findings where we did strlen/strdup on unchecked MNL_STRING attribute, see "src: move several MNL_STRING types to MNL_NUL_STRING".
--- examples/nft-compat-get.c | 2 +- src/chain.c | 2 +- src/expr/dynset.c | 2 +- src/expr/flow_offload.c | 2 +- src/expr/log.c | 2 +- src/expr/lookup.c | 2 +- src/expr/objref.c | 6 +++--- src/flowtable.c | 2 +- src/obj/ct_helper.c | 4 ++-- src/obj/secmark.c | 4 ++-- src/object.c | 2 +- src/rule.c | 2 +- src/set_elem.c | 2 +- src/table.c | 2 +- src/trace.c | 4 ++-- 15 files changed, 20 insertions(+), 20 deletions(-)
examples/nft-compat-get.c +1 -1
@@ -25,7 +25,7 @@ static int data_attr_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_COMPAT_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) { +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 ) {perror ("mnl_attr_validate" );return MNL_CB_ERROR ; }
src/chain.c +1 -1
@@ -530,7 +530,7 @@ static int nftnl_chain_parse_attr_cb(const struct nlattr *attr, void *data)case NFTA_CHAIN_NAME :case NFTA_CHAIN_TABLE :case NFTA_CHAIN_TYPE : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_CHAIN_HOOK :
src/expr/dynset.c +1 -1
@@ -131,7 +131,7 @@ static int nftnl_expr_dynset_cb(const struct nlattr *attr, void *data)abi_breakage ();break ;case NFTA_DYNSET_SET_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_DYNSET_EXPR :
src/expr/flow_offload.c +1 -1
@@ -53,7 +53,7 @@ static int nftnl_expr_flow_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_FLOW_TABLE_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ; }
src/expr/log.c +1 -1
@@ -100,7 +100,7 @@ static int nftnl_expr_log_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_LOG_PREFIX : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_LOG_GROUP :
src/expr/lookup.c +1 -1
@@ -96,7 +96,7 @@ static int nftnl_expr_lookup_cb(const struct nlattr *attr, void *data)abi_breakage ();break ;case NFTA_LOOKUP_SET : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ; }
src/expr/objref.c +3 -3
@@ -98,7 +98,7 @@ static int nftnl_expr_objref_cb(const struct nlattr *attr, void *data)break ;case NFTA_OBJREF_IMM_NAME :case NFTA_OBJREF_SET_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_OBJREF_SET_SREG :
@@ -121,12 +121,12 @@ static void nftnl_expr_objref_build(struct nlmsghdr *nlh,mnl_attr_put_u32 (nlh,NFTA_OBJREF_IMM_TYPE ,htonl (objref->imm.type));if (e->flags & (1 <<NFTNL_EXPR_OBJREF_IMM_NAME )) -mnl_attr_put_str (nlh,NFTA_OBJREF_IMM_NAME , objref->imm.name); +mnl_attr_put_strz (nlh,NFTA_OBJREF_IMM_NAME , objref->imm.name);if (e->flags & (1 <<NFTNL_EXPR_OBJREF_SET_SREG ))mnl_attr_put_u32 (nlh,NFTA_OBJREF_SET_SREG ,htonl (objref->set.sreg));if (e->flags & (1 <<NFTNL_EXPR_OBJREF_SET_NAME )) -mnl_attr_put_str (nlh,NFTA_OBJREF_SET_NAME , objref->set.name); +mnl_attr_put_strz (nlh,NFTA_OBJREF_SET_NAME , objref->set.name);if (e->flags & (1 <<NFTNL_EXPR_OBJREF_SET_ID ))mnl_attr_put_u32 (nlh,NFTA_OBJREF_SET_ID ,htonl (objref->set.id));
src/flowtable.c +1 -1
@@ -326,7 +326,7 @@ static int nftnl_flowtable_parse_attr_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_FLOWTABLE_NAME :case NFTA_FLOWTABLE_TABLE : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_FLOWTABLE_HOOK :
src/obj/ct_helper.c +2 -2
@@ -67,7 +67,7 @@ static int nftnl_obj_ct_helper_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_CT_HELPER_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();if (mnl_attr_get_payload_len (attr) >=sizeof (helper->name))abi_breakage ();
@@ -92,7 +92,7 @@ nftnl_obj_ct_helper_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)struct nftnl_obj_ct_helper *helper =nftnl_obj_data (e);if (e->flags & (1 <<NFTNL_OBJ_CT_HELPER_NAME )) -mnl_attr_put_str (nlh,NFTA_CT_HELPER_NAME , helper->name); +mnl_attr_put_strz (nlh,NFTA_CT_HELPER_NAME , helper->name);if (e->flags & (1 <<NFTNL_OBJ_CT_HELPER_L3PROTO ))mnl_attr_put_u16 (nlh,NFTA_CT_HELPER_L3PROTO ,htons (helper->l3proto));if (e->flags & (1 <<NFTNL_OBJ_CT_HELPER_L4PROTO ))
src/obj/secmark.c +2 -2
@@ -54,7 +54,7 @@ static int nftnl_obj_secmark_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_SECMARK_CTX : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();if (mnl_attr_get_payload_len (attr) >=sizeof (secmark->ctx))abi_breakage ();
@@ -71,7 +71,7 @@ nftnl_obj_secmark_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)struct nftnl_obj_secmark *secmark =nftnl_obj_data (e);if (e->flags & (1 <<NFTNL_OBJ_SECMARK_CTX )) -mnl_attr_put_str (nlh,NFTA_SECMARK_CTX , secmark->ctx); +mnl_attr_put_strz (nlh,NFTA_SECMARK_CTX , secmark->ctx); } static int
src/object.c +1 -1
@@ -315,7 +315,7 @@ static int nftnl_obj_parse_attr_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_OBJ_TABLE :case NFTA_OBJ_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_OBJ_HANDLE :
src/rule.c +1 -1
@@ -324,7 +324,7 @@ static int nftnl_rule_parse_attr_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_RULE_TABLE :case NFTA_RULE_CHAIN : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_RULE_HANDLE :
src/set_elem.c +1 -1
@@ -595,7 +595,7 @@ nftnl_set_elem_list_parse_attr_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_SET_ELEM_LIST_TABLE :case NFTA_SET_ELEM_LIST_SET : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_SET_ELEM_LIST_ELEMENTS :
src/table.c +1 -1
@@ -243,7 +243,7 @@ static int nftnl_table_parse_attr_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_TABLE_NAME : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_TABLE_HANDLE :
src/trace.c +2 -2
@@ -118,7 +118,7 @@ static int nftnl_trace_parse_attr_cb(const struct nlattr *attr, void *data)break ;case NFTA_TRACE_CHAIN :case NFTA_TRACE_TABLE : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage ();break ;case NFTA_TRACE_RULE_HANDLE :
@@ -308,7 +308,7 @@ static int nftnl_trace_parse_verdict_cb(const struct nlattr *attr, void *data) tb[type] = attr;break ;case NFTA_VERDICT_CHAIN : -if (mnl_attr_validate (attr,MNL_TYPE_STRING ) <0 ) +if (mnl_attr_validate (attr,MNL_TYPE_NUL_STRING ) <0 )abi_breakage (); tb[type] = attr;break ;
Signature · 1 line
2.55.0
Quoted 45 lines · Florian Westphal
Another round of LLM findings. These commits are all unrelated, except that they are LLM findigs with no observed bug reports.
1) Check
mnl_nlmsg_batch_start()for a NULL return. Avoid potential NULL pointer dereference in src/common.2) Fix uninitialised attr arrays by zero-initialising tb[] arrays.
3) Move several
MNL_STRINGattribute types toMNL_NUL_STRINGsomnl_attr_validateenforces NUL termination. Harden future netlink-attribute parsing against non-kernel sources that may omit the terminator.4) Validate
NFTA_EXPR_NAMEparse success. Upgrade string validation toNUL_STRING, since the kernel always appends a trailing NUL.5) Forbid re-setting or clearing the object type field to prevent inconsistent state and segfaults. Check the correct subtype before freeing to guard against a latent union overlap crash.
6) Drop trailing semicolon from the xfree macro. Callers already append their own ;, so the macro's is redundant.
7) Remove the obsolete
set_elemmember from the ruleset union. Its last consumer (the removed json interface) no longer exists, and the sharednftnl_set_freepath masked a latent type-confusion bug.8) Clear
field_lenarray before copying to avoid reading stale values from a prior call. Fix a bug where shrinking the array on a second invocation leaks leftover field counts.9) Guard the
s->expr_listtraversal against the empty-list case to avoid returning a pointer into the middle ofnftnl_set. Initializedata_lento 0.10) Fix use-after-free by calling
list_del()before freeing expressions inset_elem. Prevents dangling next/prev pointers from corrupting a subsequentlist_addon the same list.11) Clamp
buf->endto the actually copied size inudata_buf_put. Preventnftnl_udata_buf_space()from observing an end pointer past the buffer.12) Remove all remaining
MNL_STRINGusers, switch toMNL_NUL_STRINGandmnl_attr_put_strz. Enforce NUL-termination checking on all string attributes to prevent illegal strlen/strdup on unchecked data.
Quoted 13 lines
Florian Westphal (12):
src: common:mnl_nlmsg_batch_startmay return NULL
src: fix uninitialised attr arrays
src: move severalMNL_STRINGtypes toMNL_NUL_STRING
src: expr: make sureNFTA_EXPR_NAMEwas parsed successfuly
src: object: don't permit to re-set or clear object type field
include: drop semicolon at end of xfree macro
src: ruleset: removeset_elemmember from union
src: set: clearfield_lenarray before copying
src: set: check for empty list explicitly
src:set_elem:list_delbefore freeing
src: udata: clampbuf->endto actally copied size
src: remove all remainingMNL_STRINGusers
Thanks, Phil
Quoted 75 lines · Florian Westphal
Another round of LLM findings. These commits are all unrelated, except that they are LLM findigs with no observed bug reports.
1) Check
mnl_nlmsg_batch_start()for a NULL return. Avoid potential NULL pointer dereference in src/common.2) Fix uninitialised attr arrays by zero-initialising tb[] arrays.
3) Move several
MNL_STRINGattribute types toMNL_NUL_STRINGsomnl_attr_validateenforces NUL termination. Harden future netlink-attribute parsing against non-kernel sources that may omit the terminator.4) Validate
NFTA_EXPR_NAMEparse success. Upgrade string validation toNUL_STRING, since the kernel always appends a trailing NUL.5) Forbid re-setting or clearing the object type field to prevent inconsistent state and segfaults. Check the correct subtype before freeing to guard against a latent union overlap crash.
6) Drop trailing semicolon from the xfree macro. Callers already append their own ;, so the macro's is redundant.
7) Remove the obsolete
set_elemmember from the ruleset union. Its last consumer (the removed json interface) no longer exists, and the sharednftnl_set_freepath masked a latent type-confusion bug.8) Clear
field_lenarray before copying to avoid reading stale values from a prior call. Fix a bug where shrinking the array on a second invocation leaks leftover field counts.9) Guard the
s->expr_listtraversal against the empty-list case to avoid returning a pointer into the middle ofnftnl_set. Initializedata_lento 0.10) Fix use-after-free by calling
list_del()before freeing expressions inset_elem. Prevents dangling next/prev pointers from corrupting a subsequentlist_addon the same list.11) Clamp
buf->endto the actually copied size inudata_buf_put. Preventnftnl_udata_buf_space()from observing an end pointer past the buffer.12) Remove all remaining
MNL_STRINGusers, switch toMNL_NUL_STRINGandmnl_attr_put_strz. Enforce NUL-termination checking on all string attributes to prevent illegal strlen/strdup on unchecked data.Florian Westphal (12):
src: common:mnl_nlmsg_batch_startmay return NULL
src: fix uninitialised attr arrays
src: move severalMNL_STRINGtypes toMNL_NUL_STRING
src: expr: make sureNFTA_EXPR_NAMEwas parsed successfuly
src: object: don't permit to re-set or clear object type field
include: drop semicolon at end of xfree macro
src: ruleset: removeset_elemmember from union
src: set: clearfield_lenarray before copying
src: set: check for empty list explicitly
src:set_elem:list_delbefore freeing
src: udata: clampbuf->endto actally copied size
src: remove all remainingMNL_STRINGusersexamples/nft-compat-get.c | 2 +- include/utils.h | 2 +- src/chain.c | 4 ++-- src/common.c | 4 ++++ src/expr.c | 5 ++++- src/expr/data_reg.c | 4 ++-- src/expr/dynset.c | 2 +- src/expr/flow_offload.c | 2 +- src/expr/log.c | 2 +- src/expr/lookup.c | 2 +- src/expr/objref.c | 6 +++--- src/flowtable.c | 2 +- src/obj/ct_helper.c | 4 ++-- src/obj/secmark.c | 4 ++-- src/object.c | 19 +++++++++++++++---- src/rule.c | 2 +-
Quoted 9 lines
src/ruleset.c | 3 +-- src/set.c | 13 +++++++------ src/set_elem.c | 10 +++++++--- src/table.c | 2 +- src/trace.c | 6 +++--- src/udata.c | 5 ++++- src/utils.c | 5 +++-- 23 files changed, 68 insertions(+), 42 deletions(-)
For the whole series:
Thanks!