Skip to contentSkip to conversation

object: fix crashes with out-of-tree nft

PATCH In review Florian Westphal 2 participants 5 messages Feb 27, 2017 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover 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.

author maintainer
#1 lore raw

PATCH 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
index 9594d2f99071..62fa48afa2a1 100644
@@ -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
maintainer Acked-by on v1 1/2
#2 lore raw

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_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.

author maintainer
#3 lore raw

PATCH 2/2 object: fix crash when object ops is null

In reply to Florian Westphal

when 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
index 62fa48afa2a1..773eff6a5a18 100644
@@ -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