Skip to contentSkip to conversation

netfilter: nft_compat: check match/targetinfo attr size

PATCH In review Florian Westphal 2 participants 4 messages Mar 8, 2016 – Mar 10, 2016

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nft_compat: check match/targetinfo attr size

We copy accoring to ->target|matchsize, so check that the netlink attribute (which can include padding and might be larger) contains enough data.

---
 net/netfilter/nft_compat.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/netfilter/nft_compat.c +6 -0
index 454841b..6228c42 100644
@@ -660,6 +660,9 @@ nft_match_select_ops(const struct nft_ctx *ctx,
 	if (IS_ERR(match))
 		return ERR_PTR(-ENOENT);
 
+	if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO]))
+		return ERR_PTR(-EINVAL);
+
 	/* This is the first time we use this match, allocate operations */
 	nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
 	if (nft_match == NULL)
@@ -740,6 +743,9 @@ nft_target_select_ops(const struct nft_ctx *ctx,
 	if (IS_ERR(target))
 		return ERR_PTR(-ENOENT);
 
+	if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO]))
+		return ERR_PTR(-EINVAL);
+
 	/* This is the first time we use this target, allocate operations */
 	nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
 	if (nft_target == NULL)
Signature · 1 line
2.4.10
author maintainer
#2 lore raw
Quoted 9 lines · Pablo Neira Ayuso
Quoted 5 lines · Florian Westphal

We copy accoring to ->target|matchsize, so check that the netlink attribute (which can include padding and might be larger) contains enough data.

I think xt_check_match() and xt_check_target() already validate this for us.

But AFAICS we copy before this:

nft_target_init:

size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
...
target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
-> memcpy(out, in, t->targetsize);

xt_check_target(&par, size, proto, inv); // checks size vs. targetsize

'target' is sized based on target->targetsize in
nft_target_select_ops().

So if tb[NFTA_TARGET_INFO] is != t->targetsize we might copy more data than whats in 'in', no?

#3 lore raw
Quoted 20 lines · Florian Westphal
Quoted 9 lines · Pablo Neira Ayuso
Quoted 5 lines · Florian Westphal

We copy accoring to ->target|matchsize, so check that the netlink attribute (which can include padding and might be larger) contains enough data.

I think xt_check_match() and xt_check_target() already validate this for us.

But AFAICS we copy before this:

nft_target_init:

size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
...

target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
   -> memcpy(out, in, t->targetsize);
Quoted 7 lines

xt_check_target(&par, size, proto, inv); // checks size vs. targetsize

'target' is sized based on target->targetsize in
nft_target_select_ops().

So if tb[NFTA_TARGET_INFO] is != t->targetsize we might copy more data than whats in 'in', no?

Right, if the user passes less information, then we would be copying more than what we have.

I'm going to place this in the nf-next, thanks Florian.