Skip to contentSkip to conversation

netfilter: nf_tables: fix loop checking with end interval elements

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Feb 7, 2014

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/2 netfilter: nf_tables: fix loop checking with end interval elements

Fix access to uninitialized data for end interval elements. The element data part is uninitialized in interval end elements.

---
v2: don't remove inline from nft_chain_stats.

 net/netfilter/nf_tables_api.c |    3 +++
 1 file changed, 3 insertions(+)
net/netfilter/nf_tables_api.c +3 -0
index d0c790e3e..adce01e 100644
@@ -2998,6 +2998,9 @@ static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
 					const struct nft_set_iter *iter,
 					const struct nft_set_elem *elem)
 {
+	if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
+		return 0;
+
 	switch (elem->data.verdict) {
 	case NFT_JUMP:
 	case NFT_GOTO:
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH v2 2/2 netfilter: nf_tables: fix oops when using gotos

Fix an oops if you use the "goto" operation, you will end up in non-base chain with no counters and no default policy.

---
v2: recover inline to nft_chain_stats.

 net/netfilter/nf_tables_core.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_core.c +8 -3
index 0d879fc..13fecde 100644
@@ -67,18 +67,24 @@ struct nft_jumpstack {
 	int			rulenum;
 };
 
-static inline void
+static inline int
 nft_chain_stats(const struct nft_chain *this, const struct nft_pktinfo *pkt,
 		struct nft_jumpstack *jumpstack, unsigned int stackptr)
 {
 	struct nft_stats __percpu *stats;
 	const struct nft_chain *chain = stackptr ? jumpstack[0].chain : this;
 
+	/* You can reach this through goto chain */
+	if (!(chain->flags & NFT_BASE_CHAIN))
+		return NF_ACCEPT;
+
 	rcu_read_lock_bh();
 	stats = rcu_dereference(nft_base_chain(chain)->stats);
 	__this_cpu_inc(stats->pkts);
 	__this_cpu_add(stats->bytes, pkt->skb->len);
 	rcu_read_unlock_bh();
+
+	return nft_base_chain(chain)->policy;
 }
 
 enum nft_trace {
@@ -209,12 +215,11 @@ next_rule:
 		rulenum = jumpstack[stackptr].rulenum;
 		goto next_rule;
 	}
-	nft_chain_stats(chain, pkt, jumpstack, stackptr);
 
 	if (unlikely(pkt->skb->nf_trace))
 		nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_POLICY);
 
-	return nft_base_chain(chain)->policy;
+	return nft_chain_stats(chain, pkt, jumpstack, stackptr);
 }
 EXPORT_SYMBOL_GPL(nft_do_chain);
 
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 2/2 v2] netfilter: nf_tables: fix oops when using gotos

Quoted 41 lines · Pablo Neira Ayuso

Fix an oops if you use the "goto" operation, you will end up in non-base chain with no counters and no default policy.

---
v2: recover inline to nft_chain_stats.

 net/netfilter/nf_tables_core.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_core.c +7 -1
index 0d879fc..13fecde 100644
@@ -67,18 +67,24 @@ struct nft_jumpstack {
 	int			rulenum;
 };
 
-static inline void
+static inline int
 nft_chain_stats(const struct nft_chain *this, const struct nft_pktinfo *pkt,
 		struct nft_jumpstack *jumpstack, unsigned int stackptr)
 {
 	struct nft_stats __percpu *stats;
 	const struct nft_chain *chain = stackptr ? jumpstack[0].chain : this;
 
+	/* You can reach this through goto chain */
+	if (!(chain->flags & NFT_BASE_CHAIN))
+		return NF_ACCEPT;
+
 	rcu_read_lock_bh();
 	stats = rcu_dereference(nft_base_chain(chain)->stats);
 	__this_cpu_inc(stats->pkts);
 	__this_cpu_add(stats->bytes, pkt->skb->len);
 	rcu_read_unlock_bh();
+
+	return nft_base_chain(chain)->policy;
 }
 
 enum nft_trace {

net/netfilter/nf_tables_core.c next_rule: · line 222

Quoted 10 lines
net/netfilter/nf_tables_core.c +1 -2
@@ -209,12 +215,11 @@ next_rule:
 		rulenum = jumpstack[stackptr].rulenum;
 		goto next_rule;
 	}
-	nft_chain_stats(chain, pkt, jumpstack, stackptr);
 
 	if (unlikely(pkt->skb->nf_trace))
 		nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_POLICY);
 
-	return nft_base_chain(chain)->policy;
+	return nft_chain_stats(chain, pkt, jumpstack, stackptr);

I think we should just inline the function manually, it doesn't help readability to have it take care of goto-specific cases and handle verdicts/policies.

Also, what about NFT_TRACE_POLICY for the goto case? Should be generate a message even through there is no policy?