Skip to contentSkip to conversation

netfilter: fix mangle tables back

PATCH In review Alexey Dobriyan 3 participants 9 messages Feb 11, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: fix mangle tables back

Calling POST_ROUTING hook with NULL input device is not going to work.

---

 net/ipv4/netfilter/iptable_mangle.c  |    2 +-
 net/ipv6/netfilter/ip6table_mangle.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -85,7 +85,7 @@ iptable_mangle_hook(unsigned int hook,
 		     const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ipt_local_hook(hook, skb, in, out, okfn);
 
 	/* PREROUTING/INPUT/FORWARD: */
net/ipv6/netfilter/ip6table_mangle.c +1 -1
@@ -79,7 +79,7 @@ ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
 		     const struct net_device *in, const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ip6t_local_out_hook(hook, skb, out, okfn);
 
 	/* INPUT/FORWARD */
#1 lore raw

net/ipv4/netfilter/iptable_mangle.c iptable_mangle_hook(unsigned int hook, · line 91

Quoted 13 lines · Alexey Dobriyan

Calling POST_ROUTING hook with NULL input device is not going to work.

net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -85,7 +85,7 @@ iptable_mangle_hook(unsigned int hook,
 		     const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ipt_local_hook(hook, skb, in, out, okfn);
 
 	/* PREROUTING/INPUT/FORWARD: */

postrouting did not call ipt_local_hook before, so why now?

Quoted 12 lines
net/ipv6/netfilter/ip6table_mangle.c +1 -1
@@ -79,7 +79,7 @@ ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
 		     const struct net_device *in, const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ip6t_local_out_hook(hook, skb, out, okfn);
 
 	/* INPUT/FORWARD */
#2 lore raw
Quoted 14 lines · Jan Engelhardt
Quoted 12 lines · Alexey Dobriyan

Calling POST_ROUTING hook with NULL input device is not going to work.

net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -85,7 +85,7 @@ iptable_mangle_hook(unsigned int hook,
 		     const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ipt_local_hook(hook, skb, in, out, okfn);
 
Quoted 3 lines

net/ipv4/netfilter/iptable_mangle.c iptable_mangle_hook(unsigned int hook, · line 91

Quoted 1 line
net/ipv4/netfilter/iptable_mangle.c +0 -0
 	/* PREROUTING/INPUT/FORWARD: */

postrouting did not call ipt_local_hook before, so why now?

What Alexey meant is that

	/* PREROUTING/INPUT/FORWARD: */
	return ipt_do_table(skb, hook, in, out,
			    dev_net(in)->ipv4.iptable_mangle);

dev_net(in) for a NULL device won't work. Passing them to the local hook won't work either however since we perform rerouting there. I'm confused now why this didn't crash here so far ...

#3 lore raw
Quoted 21 lines · Patrick McHardy
Quoted 14 lines · Jan Engelhardt
Quoted 12 lines · Alexey Dobriyan

Calling POST_ROUTING hook with NULL input device is not going to work.

net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -85,7 +85,7 @@ iptable_mangle_hook(unsigned int hook,
 		     const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ipt_local_hook(hook, skb, in, out, okfn);
 
Quoted 3 lines

net/ipv4/netfilter/iptable_mangle.c iptable_mangle_hook(unsigned int hook, · line 91

Quoted 1 line
net/ipv4/netfilter/iptable_mangle.c +0 -0
 	/* PREROUTING/INPUT/FORWARD: */

postrouting did not call ipt_local_hook before, so why now?

What Alexey meant is that

Quoted 7 lines
	/* PREROUTING/INPUT/FORWARD: */
	return ipt_do_table(skb, hook, in, out,
			    dev_net(in)->ipv4.iptable_mangle);

dev_net(in) for a NULL device won't work. Passing them to the local hook won't work either however since we perform rerouting there. I'm confused now why this didn't crash here so far ...

Before, ipt_post_routing_hook just called

	return ipt_do_table(skb, hook, in, out,
		dev_net(out)->ipv4.iptable_mangle);

Not caring about whether in and out are NULL or not, because ipt_do_table checks for NULL.
Now, iptable_mangle_hook still just calls ipt_do_table, so nothing really changed, thus nothing broke.

Thus the confusion this patch introduces.

#4 lore raw
Quoted 28 lines · Jan Engelhardt
Quoted 19 lines · Patrick McHardy
Quoted 14 lines · Jan Engelhardt
Quoted 12 lines · Alexey Dobriyan

Calling POST_ROUTING hook with NULL input device is not going to work.

net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -85,7 +85,7 @@ iptable_mangle_hook(unsigned int hook,
 		     const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ipt_local_hook(hook, skb, in, out, okfn);
 
Quoted 2 lines

net/ipv4/netfilter/iptable_mangle.c iptable_mangle_hook(unsigned int hook, · line 91

Quoted 1 line
net/ipv4/netfilter/iptable_mangle.c +0 -0
 	/* PREROUTING/INPUT/FORWARD: */

postrouting did not call ipt_local_hook before, so why now?

What Alexey meant is that

Quoted 7 lines
	/* PREROUTING/INPUT/FORWARD: */
	return ipt_do_table(skb, hook, in, out,
			    dev_net(in)->ipv4.iptable_mangle);

dev_net(in) for a NULL device won't work. Passing them to the local hook won't work either however since we perform rerouting there. I'm confused now why this didn't crash here so far ...

Quoted 7 lines

Before, ipt_post_routing_hook just called

	return ipt_do_table(skb, hook, in, out,
		dev_net(out)->ipv4.iptable_mangle);

Not caring about whether in and out are NULL or not, because ipt_do_table checks for NULL.

Its using the device to get to the table:

dev_net(in)->ipv4.iptable_mangle

The only reason why it doesn't crash for me is that the machine I'm testing this on doesn't use network namespaces.

Please fix this up.

#5 lore raw
In reply to Patrick McHardy
Quoted 21 lines · Patrick McHardy
Quoted 14 lines · Jan Engelhardt
Quoted 12 lines · Alexey Dobriyan

Calling POST_ROUTING hook with NULL input device is not going to work.

net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -85,7 +85,7 @@ iptable_mangle_hook(unsigned int hook,
 		     const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ipt_local_hook(hook, skb, in, out, okfn);
 
Quoted 3 lines

net/ipv4/netfilter/iptable_mangle.c iptable_mangle_hook(unsigned int hook, · line 91

Quoted 1 line
net/ipv4/netfilter/iptable_mangle.c +0 -0
 	/* PREROUTING/INPUT/FORWARD: */

postrouting did not call ipt_local_hook before, so why now?

What Alexey meant is that

Quoted 7 lines
	/* PREROUTING/INPUT/FORWARD: */
	return ipt_do_table(skb, hook, in, out,
			    dev_net(in)->ipv4.iptable_mangle);

dev_net(in) for a NULL device won't work. Passing them to the local hook won't work either however since we perform rerouting there. I'm confused now why this didn't crash here so far ...

It did crashed, that's why I noticed it.
But now I can't reproduce it too. Hopefully this patch is correct.

[PATCH] netfilter: fix mangle tables

In POST_ROUTING hook, calling dev_net(in) is going to oops.

---

 net/ipv4/netfilter/iptable_mangle.c  |    4 +++-
 net/ipv6/netfilter/ip6table_mangle.c |    4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
net/ipv4/netfilter/iptable_mangle.c +3 -1
@@ -87,7 +87,9 @@ iptable_mangle_hook(unsigned int hook,
 {
 	if (hook == NF_INET_LOCAL_OUT)
 		return ipt_local_hook(hook, skb, in, out, okfn);
-
+	if (hook == NF_INET_POST_ROUTING)
+		return ipt_do_table(skb, hook, in, out,
+				    dev_net(out)->ipv4.iptable_mangle);
 	/* PREROUTING/INPUT/FORWARD: */
 	return ipt_do_table(skb, hook, in, out,
 			    dev_net(in)->ipv4.iptable_mangle);
net/ipv6/netfilter/ip6table_mangle.c +3 -1
@@ -81,7 +81,9 @@ ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
 {
 	if (hook == NF_INET_LOCAL_OUT)
 		return ip6t_local_out_hook(hook, skb, out, okfn);
-
+	if (hook == NF_INET_POST_ROUTING)
+		return ip6t_do_table(skb, hook, in, out,
+				     dev_net(out)->ipv6.ip6table_mangle);
 	/* INPUT/FORWARD */
 	return ip6t_do_table(skb, hook, in, out,
 			     dev_net(in)->ipv6.ip6table_mangle);
#6 lore raw
Quoted 24 lines · Alexey Dobriyan
Quoted 23 lines · Patrick McHardy
Quoted 14 lines · Jan Engelhardt
Quoted 12 lines · Alexey Dobriyan

Calling POST_ROUTING hook with NULL input device is not going to work.

net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -85,7 +85,7 @@ iptable_mangle_hook(unsigned int hook,
 		     const struct net_device *out,
 		     int (*okfn)(struct sk_buff *))
 {
-	if (hook == NF_INET_LOCAL_OUT)
+	if (hook == NF_INET_LOCAL_OUT || hook == NF_INET_POST_ROUTING)
 		return ipt_local_hook(hook, skb, in, out, okfn);
 
Quoted 2 lines

net/ipv4/netfilter/iptable_mangle.c iptable_mangle_hook(unsigned int hook, · line 93

Quoted 1 line
net/ipv4/netfilter/iptable_mangle.c +0 -0
 	/* PREROUTING/INPUT/FORWARD: */

postrouting did not call ipt_local_hook before, so why now?

What Alexey meant is that

	/* PREROUTING/INPUT/FORWARD: */
	return ipt_do_table(skb, hook, in, out,
			    dev_net(in)->ipv4.iptable_mangle);
Quoted 6 lines
Quoted 3 lines

dev_net(in) for a NULL device won't work. Passing them to the local hook won't work either however since we perform rerouting there. I'm confused now why this didn't crash here so far ...

It did crashed, that's why I noticed it.
But now I can't reproduce it too. Hopefully this patch is correct.

It looks correct to me. Will try to reproduce the crash just to make sure.

#7 lore raw
Quoted 2 lines · Patrick McHardy

It looks correct to me. Will try to reproduce the crash just to make sure.

Just CONFIG_NET_NS=y and it will crash.
Arrgh!

#8 lore raw
Quoted 5 lines · Alexey Dobriyan
Quoted 2 lines · Patrick McHardy

It looks correct to me. Will try to reproduce the crash just to make sure.

Just CONFIG_NET_NS=y and it will crash.

Indeed. Patch applied, thanks Alexey.