object: fix crashes with out-of-tree nft
Florian Westphal
maintainercover object: fix crashes with out-of-tree nft
While working on ct helper object support I encountered crashes in libnftnl. This occurs when nft sets unknown object types not supported by libnftnl.
The two patches avoid segfaults in case unsupported types are requested.
Florian Westphal
maintainerPATCH 1/2 object: don't set NFTNL_OBJ_TYPE unless obj->ops is non-null
If nft sets an invalid type, nftnl_obj_ops_lookup will return NULL. In this case we must not set NFTNL_OBJ_TYPE flag, else we later get crash in nftnl_obj_nlmsg_build_payload as it dereferences obj->ops.
--- src/object.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
src/object.c +4 -1
@@ -83,6 +83,8 @@ void nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,break ;case NFTNL_OBJ_TYPE : obj->ops =nftnl_obj_ops_lookup (*((uint32_t *)data)); +if (!obj->ops) +return ;break ;case NFTNL_OBJ_FAMILY : obj->family = *((uint32_t *)data);
@@ -250,7 +252,8 @@ int nftnl_obj_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_obj *obj)uint32_t type =ntohl (mnl_attr_get_u32 (tb[NFTA_OBJ_TYPE ])); obj->ops =nftnl_obj_ops_lookup (type); - obj->flags |= (1 <<NFTNL_OBJ_TYPE ); +if (obj->ops) + obj->flags |= (1 <<NFTNL_OBJ_TYPE ); }if (tb[NFTA_OBJ_DATA ]) {if (obj->ops) {
Signature · 1 line
2.10.2
Re: [PATCH libnftnl 1/2] object: don't set NFTNL_OBJ_TYPE unless obj->ops is non-null
Quoted 5 lines · Florian Westphal
If nft sets an invalid type,
nftnl_obj_ops_lookupwill return NULL. In this case we must not setNFTNL_OBJ_TYPEflag, else we later get crash innftnl_obj_nlmsg_build_payloadas it dereferencesobj->ops.Signed-off-by Florian Westphal <[email protected]>
Florian Westphal
maintainerPATCH 2/2 object: fix crash when object ops is null
In reply to Florian Westphalwhen debugging nft with invalid object type (during development), this will crash here with null deref. Print (unknown) instead if obj->ops is null.
--- src/object.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/object.c +2 -1
@@ -396,10 +396,11 @@ static int nftnl_obj_snprintf_dflt(char *buf, size_t size,const struct nftnl_obj *obj, uint32_t type, uint32_t flags) { + const char *name = obj->ops ? obj->ops->name : "(unknown)";int ret, len = size, offset =0 ; ret =snprintf (buf, size,"table %s name %s use %u [ %s " , - obj->table, obj->name, obj->use, obj->ops->name); + obj->table, obj->name, obj->use, name);SNPRINTF_BUFFER_SIZE (ret, size, len, offset);if (obj->ops) {
Signature · 1 line
2.10.2
Re: [PATCH libnftnl 2/2] object: fix crash when object ops is null
Quoted 5 lines · Florian Westphal
when debugging nft with invalid object type (during development), this will crash here with null deref. Print (unknown) instead if
obj->opsis null.Signed-off-by Florian Westphal <[email protected]>
Thanks Florian.