Skip to contentSkip to conversation

netfilter: bridge: unshare bridge info before change it

PATCH RESEND Superseded Gao feng 2 participants 6 messages Nov 17, 2014 – Nov 20, 2014

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gao feng

author
#0 lore raw

PATCH RESEND 1/1 netfilter: bridge: unshare bridge info before change it

Many packets may share the same bridge information, we should unshare the bridge info before we change it, otherwise other packets will go to PF_INET(6)/PRE_ROUTING second time or the pkt_type of other packets will be incorrect.

The problem occurs in below case.

Firstly setup NFQUEUE rule on ipv4 PREROUTING chain.

When gso packet came in from bridge, br_nf_pre_routing will allocate nf_bridge_info for this gso packet. and call setup_pre_routing to setup nf_bridge_info.(such as nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING)

Then this packet goes to ipv4 prerouting chain, nfqnl_enqueue_packet will call skb_segment to segment this gso packet. in skb_segment, the new packets will copy gso packet's header(__copy_skb_header), so there will be many packets share the same nf_bridge_info.

When these segmented packets being reinjected into kernel, they will continue going through bridge netfilter, br_nf_pre_routing_finish will clean the BRNF_NF_BRIDGE_PREROUTING for the first packet, setup it for the secondary packet, clean it for the third packet...

If the dest of these packets is local machine, they will come into br_pass_frame_up. then go to ipv4 prerouting chain again through netif_receive_skb. so ip_sabotage_in will not stop half of these packets.

---
 include/linux/netfilter_bridge.h | 54 +++++++++++++++++++++-
 net/bridge/br_netfilter.c        | 98 ++++++++++++++++++----------------------
 2 files changed, 97 insertions(+), 55 deletions(-)
include/linux/netfilter_bridge.h +52 -2
index c755e49..dca7337 100644
@@ -81,14 +81,64 @@ static inline unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
 	return 0;
 }
 
+static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
+{
+	skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
+	if (likely(skb->nf_bridge))
+		atomic_set(&(skb->nf_bridge->use), 1);
+
+	return skb->nf_bridge;
+}
+
+static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
+{
+	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
+
+	if (atomic_read(&nf_bridge->use) > 1) {
+		struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
+
+		if (tmp) {
+			memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
+			atomic_set(&tmp->use, 1);
+		}
+		nf_bridge_put(nf_bridge);
+		nf_bridge = tmp;
+	}
+	return nf_bridge;
+}
+
+static inline struct nf_bridge_info *
+nf_bridge_set_mask(struct sk_buff *skb, unsigned int mask)
+{
+	if (!nf_bridge_unshare(skb))
+		return NULL;
+
+	skb->nf_bridge->mask |= mask;
+	return skb->nf_bridge;
+}
+
+static inline struct nf_bridge_info *
+nf_bridge_unset_mask(struct sk_buff *skb, unsigned int mask)
+{
+	if (!nf_bridge_unshare(skb))
+		return NULL;
+
+	skb->nf_bridge->mask &= ~mask;
+	return skb->nf_bridge;
+}
+
 int br_handle_frame_finish(struct sk_buff *skb);
 /* Only used in br_device.c */
 static inline int br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
 {
-	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
+	struct nf_bridge_info *nf_bridge;
 
 	skb_pull(skb, ETH_HLEN);
-	nf_bridge->mask ^= BRNF_BRIDGED_DNAT;
+	nf_bridge = nf_bridge_unset_mask(skb, BRNF_BRIDGED_DNAT);
+	if (nf_bridge == NULL) {
+		kfree_skb(skb);
+		return 0;
+	}
 	skb_copy_to_linear_data_offset(skb, -(ETH_HLEN-ETH_ALEN),
 				       skb->nf_bridge->data, ETH_HLEN-ETH_ALEN);
 	skb->dev = nf_bridge->physindev;
net/bridge/br_netfilter.c +45 -53

177 lines not on this page · raw message

Signature · 1 line
1.9.3
#1 lore raw
Quoted 5 lines · Gao feng
net/bridge/br_netfilter.c +0 -0
index 1a4f32c..b4612b9 100644

net/bridge/br_netfilter.c static int br_nf_forward_finish(struct sk_buff *skb) · line 650

Quoted 9 lines
net/bridge/br_netfilter.c +5 -1
@@ -653,7 +643,11 @@ static int br_nf_forward_finish(struct sk_buff *skb)
 		in = nf_bridge->physindev;
 		if (nf_bridge->mask & BRNF_PKT_TYPE) {
 			skb->pkt_type = PACKET_OTHERHOST;
-			nf_bridge->mask ^= BRNF_PKT_TYPE;
+
+			if (!nf_bridge_unset_mask(skb, BRNF_PKT_TYPE)) {
+				kfree_skb(skb);
+				return 0;
+			}

This can now release the packet and, thus, drop it.

However, br_nf_forward_ip() always returns NF_STOLEN.

Could you revisit the error paths and confirm they are correct?

Thanks.

Quoted 3 lines
net/bridge/br_netfilter.c +0 -0
 		}
 		nf_bridge_update_protocol(skb);
 	} else {
author
#2 lore raw
Quoted 18 lines · Pablo Neira Ayuso
Quoted 5 lines · Gao feng
net/bridge/br_netfilter.c +0 -0
index 1a4f32c..b4612b9 100644

net/bridge/br_netfilter.c static int br_nf_forward_finish(struct sk_buff *skb) · line 650

Quoted 9 lines
net/bridge/br_netfilter.c +5 -1
@@ -653,7 +643,11 @@ static int br_nf_forward_finish(struct sk_buff *skb)
 		in = nf_bridge->physindev;
 		if (nf_bridge->mask & BRNF_PKT_TYPE) {
 			skb->pkt_type = PACKET_OTHERHOST;
-			nf_bridge->mask ^= BRNF_PKT_TYPE;
+
+			if (!nf_bridge_unset_mask(skb, BRNF_PKT_TYPE)) {
+				kfree_skb(skb);
+				return 0;
+			}

This can now release the packet and, thus, drop it.

Quoted 3 lines

However, br_nf_forward_ip() always returns NF_STOLEN.

Could you revisit the error paths and confirm they are correct?

Since br_nf_forward_ip() returns NF_STOLEN, br_nf_forward_finish should make sure this skb will be released. if unser_mask for this skb is failed, we free it. if unset successed, the hooks on NF_BR_FORWARD or br_forward_finish will free this skb.

I check the error paths again, find a missing null check of nf_bridge. I will post v2 patch.

#3 lore raw
Quoted 19 lines · Gao feng
Quoted 18 lines · Pablo Neira Ayuso
Quoted 5 lines · Gao feng
net/bridge/br_netfilter.c +0 -0
index 1a4f32c..b4612b9 100644

net/bridge/br_netfilter.c static int br_nf_forward_finish(struct sk_buff *skb) · line 650

Quoted 9 lines
net/bridge/br_netfilter.c +5 -1
@@ -653,7 +643,11 @@ static int br_nf_forward_finish(struct sk_buff *skb)
 		in = nf_bridge->physindev;
 		if (nf_bridge->mask & BRNF_PKT_TYPE) {
 			skb->pkt_type = PACKET_OTHERHOST;
-			nf_bridge->mask ^= BRNF_PKT_TYPE;
+
+			if (!nf_bridge_unset_mask(skb, BRNF_PKT_TYPE)) {
+				kfree_skb(skb);
+				return 0;
+			}

This can now release the packet and, thus, drop it.

Quoted 8 lines
Quoted 3 lines

However, br_nf_forward_ip() always returns NF_STOLEN.

Could you revisit the error paths and confirm they are correct?

Since br_nf_forward_ip() returns NF_STOLEN, br_nf_forward_finish should make sure this skb will be released. if unser_mask for this skb is failed, we free it. if unset successed, the hooks on NF_BR_FORWARD or br_forward_finish will free this skb.

OK, but I think this should return NF_DROP instead of NF_STOLEN so you don't explicitly need to kfree_skb it.

#4 lore raw
Quoted 24 lines · Pablo Neira Ayuso
Quoted 23 lines · Gao feng
Quoted 21 lines · Pablo Neira Ayuso
Quoted 5 lines · Gao feng
net/bridge/br_netfilter.c +0 -0
index 1a4f32c..b4612b9 100644

net/bridge/br_netfilter.c static int br_nf_forward_finish(struct sk_buff *skb) · line 650

Quoted 9 lines
net/bridge/br_netfilter.c +5 -1
@@ -653,7 +643,11 @@ static int br_nf_forward_finish(struct sk_buff *skb)
 		in = nf_bridge->physindev;
 		if (nf_bridge->mask & BRNF_PKT_TYPE) {
 			skb->pkt_type = PACKET_OTHERHOST;
-			nf_bridge->mask ^= BRNF_PKT_TYPE;
+
+			if (!nf_bridge_unset_mask(skb, BRNF_PKT_TYPE)) {
+				kfree_skb(skb);
+				return 0;
+			}

This can now release the packet and, thus, drop it.

However, br_nf_forward_ip() always returns NF_STOLEN.

Could you revisit the error paths and confirm they are correct?

Quoted 7 lines
Quoted 4 lines

Since br_nf_forward_ip() returns NF_STOLEN, br_nf_forward_finish should make sure this skb will be released. if unser_mask for this skb is failed, we free it. if unset successed, the hooks on NF_BR_FORWARD or br_forward_finish will free this skb.

OK, but I think this should return NF_DROP instead of NF_STOLEN so you don't explicitly need to kfree_skb it.

After a further look, I noticed what I suggest needs to replace:

        NF_HOOK_THRESH(...);
        return 0;

by:

        return NF_HOOK_THRESH(...);

And review the return value of all the existing okfn() functions so they return netfilter verdicts.

The existing code always returns NF_STOLEN from many other hooks, not only when dropped but also when accepted to make this work...

I'm going to have a look at your v2. Thanks.

author
#5 lore raw
Quoted 35 lines · Pablo Neira Ayuso
Quoted 24 lines · Pablo Neira Ayuso
Quoted 23 lines · Gao feng
Quoted 21 lines · Pablo Neira Ayuso
Quoted 5 lines · Gao feng
net/bridge/br_netfilter.c +0 -0
index 1a4f32c..b4612b9 100644

net/bridge/br_netfilter.c static int br_nf_forward_finish(struct sk_buff *skb) · line 650

Quoted 9 lines
net/bridge/br_netfilter.c +5 -1
@@ -653,7 +643,11 @@ static int br_nf_forward_finish(struct sk_buff *skb)
 		in = nf_bridge->physindev;
 		if (nf_bridge->mask & BRNF_PKT_TYPE) {
 			skb->pkt_type = PACKET_OTHERHOST;
-			nf_bridge->mask ^= BRNF_PKT_TYPE;
+
+			if (!nf_bridge_unset_mask(skb, BRNF_PKT_TYPE)) {
+				kfree_skb(skb);
+				return 0;
+			}

This can now release the packet and, thus, drop it.

However, br_nf_forward_ip() always returns NF_STOLEN.

Could you revisit the error paths and confirm they are correct?

Quoted 7 lines
Quoted 4 lines

Since br_nf_forward_ip() returns NF_STOLEN, br_nf_forward_finish should make sure this skb will be released. if unser_mask for this skb is failed, we free it. if unset successed, the hooks on NF_BR_FORWARD or br_forward_finish will free this skb.

OK, but I think this should return NF_DROP instead of NF_STOLEN so you don't explicitly need to kfree_skb it.

After a further look, I noticed what I suggest needs to replace:

Quoted 9 lines
        NF_HOOK_THRESH(...);
        return 0;

by:

        return NF_HOOK_THRESH(...);

And review the return value of all the existing okfn() functions so they return netfilter verdicts.

NF_HOOK(_THRESH) will do kfree_skb if some hooks return NF_DROP, and the return value of NF_HOOK_THRESH is not NF_DROP/NF_ACCEPT/NF_STOP.... it is the return value of nf_hook_slow.

If we call NF_HOOK/NF_HOOK_THRESH in some hooks, such as NF_HOOK(, NF_INET_FORWARD) in br_nf_forward_ip, since packets may be dropped by INET FORWARD CHAIN, we cannot return any NF_value but NF_STOLEN in br_nf_forward_ip, this packet cannot be handled by the hooks next to br_nf_forward_ip since this packet may already be freed.

Quoted 3 lines

The existing code always returns NF_STOLEN from many other hooks, not only when dropped but also when accepted to make this work...

I think this the right way.

Quoted 3 lines

I'm going to have a look at your v2. Thanks.
.