Skip to contentSkip to conversation

netfilter: xt_bpf: don't include linux/filter.h from uapi header

PATCH In review Pablo Neira Ayuso 4 participants 8 messages Jul 29, 2014 – Jul 31, 2014

Subsystems (6)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 netfilter: xt_bpf: don't include linux/filter.h from uapi header

In e6f30c7 ("netfilter: x_tables: add xt_bpf match"), the internal linux/filter.h header slipped through in the user exposed xt_bpf.h header as included file.

Fix this by defining struct sk_filter; so we skip the casting in kernelspace. This is safe since userspace has no way to lurk with that internal pointer.

---
@David: This patch is very small and it can probably go into net, but it's
quite late for changes in -rc7 probably. Your call :-). Thanks.

 include/uapi/linux/netfilter/xt_bpf.h |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/xt_bpf.h +2 -1
index 5dda450..93fca65 100644
@@ -1,11 +1,12 @@
 #ifndef _XT_BPF_H
 #define _XT_BPF_H
 
-#include <linux/filter.h>
 #include <linux/types.h>
 
 #define XT_BPF_MAX_NUM_INSTR	64
 
+struct sk_filter;
+
 struct xt_bpf_info {
 	__u16 bpf_program_num_elem;
 	struct sock_filter bpf_program[XT_BPF_MAX_NUM_INSTR];
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH 2/2 net: filter: don't release unattached filter through call_rcu()

sk_unattached_filter_destroy() does not always need to release the filter object via rcu. Since this filter is never attached to the socket, the caller should be responsible for releasing the filter in a safe way, which may not necessarily imply rcu.

This is a short summary of clients of this function:

1) xt_bpf.c and cls_bpf.c use the bpf matchers from rules, these rules are removed from the packet path before the filter is released. Thus, the framework makes sure the filter is safely removed.

2) In the ppp driver, the ppp_lock ensures serialization between the xmit and filter attachment/detachment path. This doesn't use rcu so deferred release via rcu makes no sense.

3) In the isdn/ppp driver, it is called from isdn_ppp_release() the isdn_ppp_ioctl(). This driver uses mutex and spinlocks, no rcu. Thus, deferred rcu makes no sense to me either, the deferred releases may be just masking the effects of wrong locking strategy, which should be fixed in the driver itself.

4) In the team driver, this is the only place where the rcu synchronization with unattached filter is used. Therefore, this patch introduces synchronize_rcu() which is called from the genetlink path to make sure the filter doesn't go away while packets are still walking over it. I think we can revisit this once struct bpf_prog (that only wraps specific bpf code bits) is in place, then add some specific struct rcu_head in the scope of the team driver if Jiri thinks this is needed.

Deferred rcu release for unattached filters was originally introduced in 302d663 ("filter: Allow to create sk-unattached filters").

---
 drivers/net/team/team_mode_loadbalance.c |    6 +++++-
 net/core/filter.c                        |   11 ++++++++---
 2 files changed, 13 insertions(+), 4 deletions(-)
drivers/net/team/team_mode_loadbalance.c +5 -1
index a58dfeb..7106f34 100644
@@ -293,11 +293,15 @@ static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
 		__fprog_destroy(lb_priv->ex->orig_fprog);
 		orig_fp = rcu_dereference_protected(lb_priv->fp,
 						lockdep_is_held(&team->lock));
-		sk_unattached_filter_destroy(orig_fp);
 	}
 
 	rcu_assign_pointer(lb_priv->fp, fp);
 	lb_priv->ex->orig_fprog = fprog;
+
+	if (orig_fp) {
+		synchronize_rcu();
+		sk_unattached_filter_destroy(orig_fp);
+	}
 	return 0;
 }
 
net/core/filter.c +8 -3
index f3b2d5e..42c1944 100644
@@ -841,6 +841,12 @@ static void sk_release_orig_filter(struct sk_filter *fp)
 	}
 }
 
+static void __sk_filter_release(struct sk_filter *fp)
+{
+	sk_release_orig_filter(fp);
+	sk_filter_free(fp);
+}
+
 /**
  * 	sk_filter_release_rcu - Release a socket filter by rcu_head
  *	@rcu: rcu_head that contains the sk_filter to free
@@ -849,8 +855,7 @@ static void sk_filter_release_rcu(struct rcu_head *rcu)
 {
 	struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
 
-	sk_release_orig_filter(fp);
-	sk_filter_free(fp);
+	__sk_filter_release(fp);
 }
 
 /**
@@ -1050,7 +1055,7 @@ EXPORT_SYMBOL_GPL(sk_unattached_filter_create);
 
 void sk_unattached_filter_destroy(struct sk_filter *fp)
 {
-	sk_filter_release(fp);
+	__sk_filter_release(fp);
 }
 EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy);
 
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH net-next 2/2] net: filter: don't release unattached filter through call_rcu()

Quoted 21 lines · Pablo Neira Ayuso

sk_unattached_filter_destroy() does not always need to release the filter object via rcu. Since this filter is never attached to the socket, the caller should be responsible for releasing the filter in a safe way, which may not necessarily imply rcu.

This is a short summary of clients of this function:

1) xt_bpf.c and cls_bpf.c use the bpf matchers from rules, these rules are removed from the packet path before the filter is released. Thus, the framework makes sure the filter is safely removed.

2) In the ppp driver, the ppp_lock ensures serialization between the xmit and filter attachment/detachment path. This doesn't use rcu so deferred release via rcu makes no sense.

3) In the isdn/ppp driver, it is called from isdn_ppp_release() the isdn_ppp_ioctl(). This driver uses mutex and spinlocks, no rcu. Thus, deferred rcu makes no sense to me either, the deferred releases may be just masking the effects of wrong locking strategy, which should be fixed in the driver itself.

Quoted 8 lines

4) In the team driver, this is the only place where the rcu synchronization with unattached filter is used. Therefore, this patch introduces synchronize_rcu() which is called from the genetlink path to make sure the filter doesn't go away while packets are still walking over it. I think we can revisit this once struct bpf_prog (that only wraps specific bpf code bits) is in place, then add some specific struct rcu_head in the scope of the team driver if Jiri thinks this is needed.

Correct.
There are also:
5) ptp_classifer - which is ok as well, since there is no filter_destroy.

6) seccomp - there I think it's also ok, since filter is called from syscall and filter freeing is done on task exit

maintainer
#3 lore raw

Re: [PATCH net-next 2/2] net: filter: don't release unattached filter through call_rcu()

In reply to Pablo Neira Ayuso

From: Pablo Neira Ayuso <[email protected]>
Date: Tue, 29 Jul 2014 17:36:28 +0200

Quoted 30 lines

sk_unattached_filter_destroy() does not always need to release the filter object via rcu. Since this filter is never attached to the socket, the caller should be responsible for releasing the filter in a safe way, which may not necessarily imply rcu.

This is a short summary of clients of this function:

1) xt_bpf.c and cls_bpf.c use the bpf matchers from rules, these rules are removed from the packet path before the filter is released. Thus, the framework makes sure the filter is safely removed.

2) In the ppp driver, the ppp_lock ensures serialization between the xmit and filter attachment/detachment path. This doesn't use rcu so deferred release via rcu makes no sense.

3) In the isdn/ppp driver, it is called from isdn_ppp_release() the isdn_ppp_ioctl(). This driver uses mutex and spinlocks, no rcu. Thus, deferred rcu makes no sense to me either, the deferred releases may be just masking the effects of wrong locking strategy, which should be fixed in the driver itself.

4) In the team driver, this is the only place where the rcu synchronization with unattached filter is used. Therefore, this patch introduces synchronize_rcu() which is called from the genetlink path to make sure the filter doesn't go away while packets are still walking over it. I think we can revisit this once struct bpf_prog (that only wraps specific bpf code bits) is in place, then add some specific struct rcu_head in the scope of the team driver if Jiri thinks this is needed.

Quoted 4 lines

Deferred rcu release for unattached filters was originally introduced in 302d663 ("filter: Allow to create sk-unattached filters").

Also applied, thanks.

#4 lore raw
In reply to Pablo Neira Ayuso
Quoted 3 lines

In e6f30c7 ("netfilter: x_tables: add xt_bpf match"), the internal linux/filter.h header slipped through in the user exposed xt_bpf.h header as included file.

is that true? #include <linux/filter.h> should include include/uapi/linux/filter.h in userspace builds.

include/uapi/linux/netfilter/xt_bpf.h line 12

Quoted 6 lines
include/uapi/linux/netfilter/xt_bpf.h +2 -0
+struct sk_filter;
+
 struct xt_bpf_info {
        __u16 bpf_program_num_elem;
        struct sock_filter bpf_program[XT_BPF_MAX_NUM_INSTR];

I think include/uapi/linux/filter.h is still needed for the definition of struct sock_filter.

The uapi file does not declare sk_filter, so we do need to add the forward declaration. Thanks, Pablo.

author maintainer
#5 lore raw
Quoted 6 lines · Willem de Bruijn
Quoted 3 lines

In e6f30c7 ("netfilter: x_tables: add xt_bpf match"), the internal linux/filter.h header slipped through in the user exposed xt_bpf.h header as included file.

is that true? #include <linux/filter.h> should include include/uapi/linux/filter.h in userspace builds.

You're right. I'm going to send a v2.

We should also cache the linux/filter.h header in the iptables sources, would you send a patch for that? Thanks.

#6 lore raw
Quoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines · Willem de Bruijn
Quoted 3 lines

In e6f30c7 ("netfilter: x_tables: add xt_bpf match"), the internal linux/filter.h header slipped through in the user exposed xt_bpf.h header as included file.

is that true? #include <linux/filter.h> should include include/uapi/linux/filter.h in userspace builds.

You're right. I'm going to send a v2.

Thanks for fixing my bug. I agree that it's better to do this independent of the sk_filter/bpf_prog patch.

Quoted 2 lines

We should also cache the linux/filter.h header in the iptables sources, would you send a patch for that? Thanks.

Will do. Should I just import the latest version of the file, or is there a sync policy, such as that all kernel headers come from the same kernel release?

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#7 lore raw
Quoted 14 lines · Willem de Bruijn
Quoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines · Willem de Bruijn
Quoted 3 lines

In e6f30c7 ("netfilter: x_tables: add xt_bpf match"), the internal linux/filter.h header slipped through in the user exposed xt_bpf.h header as included file.

is that true? #include <linux/filter.h> should include include/uapi/linux/filter.h in userspace builds.

You're right. I'm going to send a v2.

Thanks for fixing my bug. I agree that it's better to do this independent of the sk_filter/bpf_prog patch.

Quoted 6 lines
Quoted 2 lines

We should also cache the linux/filter.h header in the iptables sources, would you send a patch for that? Thanks.

Will do. Should I just import the latest version of the file, or is there a sync policy, such as that all kernel headers come from the same kernel release?

Right. The kernel header which is available after make headers_install. Thanks.