Skip to contentSkip to conversation

netfilter: nf_queue: don't re-enter same hook on packet reinjection

PATCH In review Pablo Neira Ayuso 4 participants 7 messages Oct 14, 2016 – Oct 18, 2016

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/1 netfilter: nf_queue: don't re-enter same hook on packet reinjection

Make sure we skip the current hook from where the packet was enqueued, otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")

---
v2: Make sure next hook is non-null, otherwise we are at the end of the
hook list and we can skip nf_iterate().

 net/netfilter/nf_queue.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nf_queue.c +2 -1
index 96964a0070e1..691e713d70f5 100644
@@ -185,8 +185,9 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	}
 
 	entry->state.thresh = INT_MIN;
+	hook_entry = rcu_dereference(hook_entry->next);
 
-	if (verdict == NF_ACCEPT) {
+	if (hook_entry && verdict == NF_ACCEPT) {
 	next_hook:
 		verdict = nf_iterate(skb, &entry->state, &hook_entry);
 	}
Signature · 1 line
2.1.4
#1 lore raw
Quoted 17 lines · Pablo Neira Ayuso

Make sure we skip the current hook from where the packet was enqueued, otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")

---
v2: Make sure next hook is non-null, otherwise we are at the end of the
hook list and we can skip nf_iterate().

 net/netfilter/nf_queue.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nf_queue.c +0 -0
index 96964a0070e1..691e713d70f5 100644

net/netfilter/nf_queue.c void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 193

Quoted 10 lines
net/netfilter/nf_queue.c +2 -1
@@ -185,8 +185,9 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	}
 
 	entry->state.thresh = INT_MIN;
+	hook_entry = rcu_dereference(hook_entry->next);
 
-	if (verdict == NF_ACCEPT) {
+	if (hook_entry && verdict == NF_ACCEPT) {
 	next_hook:
 		verdict = nf_iterate(skb, &entry->state, &hook_entry);
 	}

ACK. I thought switch case below could have a problem, but re-checked the first nf_queue leg, and it seems okay.

-Aaron

author maintainer
#2 lore raw
Quoted 24 lines · Aaron Conole
Quoted 22 lines · Pablo Neira Ayuso

Make sure we skip the current hook from where the packet was enqueued, otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")

---
v2: Make sure next hook is non-null, otherwise we are at the end of the
hook list and we can skip nf_iterate().

 net/netfilter/nf_queue.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nf_queue.c +1 -0
index 96964a0070e1..691e713d70f5 100644
@@ -185,8 +185,9 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	}
 
 	entry->state.thresh = INT_MIN;
+	hook_entry = rcu_dereference(hook_entry->next);
 
Quoted 8 lines

net/netfilter/nf_queue.c void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 193

Quoted 5 lines
net/netfilter/nf_queue.c +1 -1
-	if (verdict == NF_ACCEPT) {
+	if (hook_entry && verdict == NF_ACCEPT) {
 	next_hook:
 		verdict = nf_iterate(skb, &entry->state, &hook_entry);
 	}

ACK. I thought switch case below could have a problem, but re-checked
the first nf_queue leg, and it seems okay.

Argh, still not right. If we get a NF_QUEUE verdict to re-enqueue again, then hook_entry may become NULL.

	switch (verdict & NF_VERDICT_MASK) {
	case NF_ACCEPT:
	case NF_STOP:
		local_bh_disable();
		entry->state.okfn(entry->state.net, entry->state.sk, skb);
		local_bh_enable();
		break;
	case NF_QUEUE:
		RCU_INIT_POINTER(entry->state.hook_entries, hook_entry); <--

Attaching new patch.

Quoted 1 line

From c1a731c68791bcd504a7fe5d28f5f0fd59d66118 Mon Sep 17 00:00:00 2001

From: Pablo Neira Ayuso <[email protected]>
Date: Thu, 13 Oct 2016 08:14:03 +0200
Subject: [PATCH nf,v3] netfilter: nf_queue: don't re-enter same hook on packet
reinjection

If the packet is accepted, we have to skip the current hook from where the packet was enqueued. Thus, we can emulate the previous list_for_each_entry_continue() behaviour happening from nf_reinject(), otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")
---
 net/netfilter/nf_queue.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_queue.c +4 -2
index 96964a0070e1..0b5ac3c9c2bc 100644
@@ -187,8 +187,10 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	entry->state.thresh = INT_MIN;
 
 	if (verdict == NF_ACCEPT) {
-	next_hook:
-		verdict = nf_iterate(skb, &entry->state, &hook_entry);
+		hook_entry = rcu_dereference(hook_entry->next);
+		if (hook_entry)
+next_hook:
+			verdict = nf_iterate(skb, &entry->state, &hook_entry);
 	}
 
 	switch (verdict & NF_VERDICT_MASK) {
Signature · 1 line
2.1.4
#3 lore raw
Quoted 71 lines · Pablo Neira Ayuso
Quoted 24 lines · Aaron Conole
Quoted 22 lines · Pablo Neira Ayuso

Make sure we skip the current hook from where the packet was enqueued, otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")

---
v2: Make sure next hook is non-null, otherwise we are at the end of the
hook list and we can skip nf_iterate().

 net/netfilter/nf_queue.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nf_queue.c +1 -0
index 96964a0070e1..691e713d70f5 100644
@@ -185,8 +185,9 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	}
 
 	entry->state.thresh = INT_MIN;
+	hook_entry = rcu_dereference(hook_entry->next);
 
Quoted 8 lines

net/netfilter/nf_queue.c void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 193

Quoted 5 lines
net/netfilter/nf_queue.c +1 -1
-	if (verdict == NF_ACCEPT) {
+	if (hook_entry && verdict == NF_ACCEPT) {
 	next_hook:
 		verdict = nf_iterate(skb, &entry->state, &hook_entry);
 	}

ACK. I thought switch case below could have a problem, but re-checked
the first nf_queue leg, and it seems okay.

Argh, still not right. If we get a NF_QUEUE verdict to re-enqueue again, then hook_entry may become NULL.

	switch (verdict & NF_VERDICT_MASK) {
	case NF_ACCEPT:
	case NF_STOP:
		local_bh_disable();
		entry->state.okfn(entry->state.net, entry->state.sk, skb);
		local_bh_enable();
		break;
	case NF_QUEUE:
		RCU_INIT_POINTER(entry->state.hook_entries, hook_entry); <--

Attaching new patch.

From c1a731c68791bcd504a7fe5d28f5f0fd59d66118 Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Thu, 13 Oct 2016 08:14:03 +0200
Subject: [PATCH nf,v3] netfilter: nf_queue: don't re-enter same hook on packet
reinjection

If the packet is accepted, we have to skip the current hook from where the packet was enqueued. Thus, we can emulate the previous list_for_each_entry_continue() behaviour happening from nf_reinject(), otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")
---
 net/netfilter/nf_queue.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_queue.c +0 -0
index 96964a0070e1..0b5ac3c9c2bc 100644

net/netfilter/nf_queue.c void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 192

Quoted 8 lines
net/netfilter/nf_queue.c +3 -2
@@ -187,8 +187,10 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	entry->state.thresh = INT_MIN;
 
 	if (verdict == NF_ACCEPT) {
-	next_hook:
-		verdict = nf_iterate(skb, &entry->state, &hook_entry);
+		hook_entry = rcu_dereference(hook_entry->next);
+		if (hook_entry)
+next_hook:

Should the above two lines be transposed to this?

next_hook:
if (hook_entry)

Sorry if I'm misunderstanding it. Too many special cases for my tiny brain...

-Aaron

author maintainer
#4 lore raw
Quoted 1 line · Aaron Conole

Pablo Neira Ayuso <[email protected]> writes:

[...]

Quoted 33 lines
Quoted 22 lines

From c1a731c68791bcd504a7fe5d28f5f0fd59d66118 Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Thu, 13 Oct 2016 08:14:03 +0200
Subject: [PATCH nf,v3] netfilter: nf_queue: don't re-enter same hook on packet
reinjection

If the packet is accepted, we have to skip the current hook from where the packet was enqueued. Thus, we can emulate the previous list_for_each_entry_continue() behaviour happening from nf_reinject(), otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")
---
 net/netfilter/nf_queue.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_queue.c +0 -0
index 96964a0070e1..0b5ac3c9c2bc 100644

net/netfilter/nf_queue.c void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 192

Quoted 8 lines
net/netfilter/nf_queue.c +3 -2
@@ -187,8 +187,10 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	entry->state.thresh = INT_MIN;
 
 	if (verdict == NF_ACCEPT) {
-	next_hook:
-		verdict = nf_iterate(skb, &entry->state, &hook_entry);
+		hook_entry = rcu_dereference(hook_entry->next);
+		if (hook_entry)
+next_hook:

Should the above two lines be transposed to this?

Quoted 5 lines

next_hook:
if (hook_entry)

Sorry if I'm misunderstanding it. Too many special cases for my tiny brain...

Right, my patch is still not correct.

I think this should be it:

        if (verdict == NF_ACCEPT) {
next_hook:
                hook_entry = rcu_dereference(hook_entry->next);
                if (hook_entry)
                        verdict = nf_iterate(skb, &entry->state, &hook_entry);

So we jump to "next_hook" in case of NF_QUEUE verdict with bypass flag set on. In that case, we need to continue just after the current hook entry to emulate the behaviour that we previously have via list_for_each_entry_continue().

This NF_QUEUE handling is also broken from nf_hook_slow() path, right?

maintainer
#5 lore raw
Quoted 52 lines · Pablo Neira Ayuso
Quoted 1 line · Aaron Conole

Pablo Neira Ayuso <[email protected]> writes:

[...]

Quoted 33 lines
Quoted 22 lines

From c1a731c68791bcd504a7fe5d28f5f0fd59d66118 Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Thu, 13 Oct 2016 08:14:03 +0200
Subject: [PATCH nf,v3] netfilter: nf_queue: don't re-enter same hook on packet
reinjection

If the packet is accepted, we have to skip the current hook from where the packet was enqueued. Thus, we can emulate the previous list_for_each_entry_continue() behaviour happening from nf_reinject(), otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")
---
 net/netfilter/nf_queue.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_queue.c +0 -0
index 96964a0070e1..0b5ac3c9c2bc 100644

net/netfilter/nf_queue.c void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 192

Quoted 8 lines
net/netfilter/nf_queue.c +3 -2
@@ -187,8 +187,10 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	entry->state.thresh = INT_MIN;
 
 	if (verdict == NF_ACCEPT) {
-	next_hook:
-		verdict = nf_iterate(skb, &entry->state, &hook_entry);
+		hook_entry = rcu_dereference(hook_entry->next);
+		if (hook_entry)
+next_hook:

Should the above two lines be transposed to this?

Quoted 5 lines

next_hook:
if (hook_entry)

Sorry if I'm misunderstanding it. Too many special cases for my tiny brain...

Right, my patch is still not correct.

I think this should be it:

        if (verdict == NF_ACCEPT) {
next_hook:
                hook_entry = rcu_dereference(hook_entry->next);
                if (hook_entry)
                        verdict = nf_iterate(skb, &entry->state, &hook_entry);
Quoted 6 lines

So we jump to "next_hook" in case of NF_QUEUE verdict with bypass flag set on. In that case, we need to continue just after the current hook entry to emulate the behaviour that we previously have via list_for_each_entry_continue().

This NF_QUEUE handling is also broken from nf_hook_slow() path, right?

Yes. As you already indicate, list_for_each_entry_continue() resumes after the current elem, this isn't true anymore.

So for nf_queue we need to move to hook_entry->next in ACCEPT case, and, for nf_hook_slow, we need to do the same when hitting

(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))

     goto next_hook;

branch.

#6 lore raw
Quoted 45 lines · Florian Westphal
Quoted 44 lines · Pablo Neira Ayuso
Quoted 1 line · Aaron Conole

Pablo Neira Ayuso <[email protected]> writes:

[...]

Quoted 33 lines
Quoted 22 lines

From c1a731c68791bcd504a7fe5d28f5f0fd59d66118 Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Thu, 13 Oct 2016 08:14:03 +0200
Subject: [PATCH nf,v3] netfilter: nf_queue: don't re-enter same hook on packet
reinjection

If the packet is accepted, we have to skip the current hook from where the packet was enqueued. Thus, we can emulate the previous list_for_each_entry_continue() behaviour happening from nf_reinject(), otherwise the packets gets enqueued over and over again.

Fixes e3b37f11e6e4 ("netfilter: replace list_head with single linked list")
---
 net/netfilter/nf_queue.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_queue.c +0 -0
index 96964a0070e1..0b5ac3c9c2bc 100644

net/netfilter/nf_queue.c void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 192

Quoted 8 lines
net/netfilter/nf_queue.c +3 -2
@@ -187,8 +187,10 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	entry->state.thresh = INT_MIN;
 
 	if (verdict == NF_ACCEPT) {
-	next_hook:
-		verdict = nf_iterate(skb, &entry->state, &hook_entry);
+		hook_entry = rcu_dereference(hook_entry->next);
+		if (hook_entry)
+next_hook:

Should the above two lines be transposed to this?

Quoted 5 lines

next_hook:
if (hook_entry)

Sorry if I'm misunderstanding it. Too many special cases for my tiny brain...

Right, my patch is still not correct.

Quoted 8 lines
Quoted 8 lines

I think this should be it:

        if (verdict == NF_ACCEPT) {
next_hook:
                hook_entry = rcu_dereference(hook_entry->next);
                if (hook_entry)
                        verdict = nf_iterate(skb, &entry->state, &hook_entry);

Yes.

Quoted 10 lines
Quoted 6 lines

So we jump to "next_hook" in case of NF_QUEUE verdict with bypass flag set on. In that case, we need to continue just after the current hook entry to emulate the behaviour that we previously have via list_for_each_entry_continue().

This NF_QUEUE handling is also broken from nf_hook_slow() path, right?

Yes. As you already indicate, list_for_each_entry_continue() resumes after the current elem, this isn't true anymore.

Quoted 7 lines

So for nf_queue we need to move to hook_entry->next in ACCEPT case, and, for nf_hook_slow, we need to do the same when hitting

(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))

     goto next_hook;

branch.

Right. That looks correct.