Skip to contentSkip to conversation

netfilter: xtables: lightweight process control group matching

PATCH In review Daniel Borkmann 11 participants 17 messages Oct 4, 2013 – Oct 22, 2013 7 messages not in the archive

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

PATCH 1/1 netfilter: xtables: lightweight process control group matching

It would be useful e.g. in a server or desktop environment to have a facility in the notion of fine-grained "per application" or "per application group" firewall policies. Probably, users in the mobile/ embedded area (e.g. Android based) with different security policy requirements for application groups could have great benefit from that as well. For example, with a little bit of configuration effort, an admin could whitelist well-known applications, and thus block otherwise unwanted "hard-to-track" applications like [1] from a user's machine.

Implementation of PID-based matching would not be appropriate as they frequently change, and child tracking would make that even more complex and ugly. Cgroups would be a perfect candidate for accomplishing that as they associate a set of tasks with a set of parameters for one or more subsystems, in our case the netfilter subsystem, which, of course, can be combined with other cgroup subsystems into something more complex.

As mentioned, to overcome this constraint, such processes could be placed into one or multiple cgroups where different fine-grained rules can be defined depending on the application scenario, while e.g. everything else that is not part of that could be dropped (or vice versa), thus making life harder for unwanted processes to communicate to the outside world. So, we make use of cgroups here to track jobs and limit their resources in terms of iptables policies; in other words, limiting what they are allowed to communicate.

We have similar cgroup facilities in networking for traffic classifier, and netprio cgroups. This feature adds a lightweight cgroup id matching in terms of network security resp. network traffic isolation as part of netfilter's xtables subsystem.

Minimal, basic usage example (many other iptables options can be applied obviously):

1) Configuring cgroups:

mkdir /sys/fs/cgroup/net_filter
mount -t cgroup -o net_filter net_filter /sys/fs/cgroup/net_filter mkdir /sys/fs/cgroup/net_filter/0
echo 1 > /sys/fs/cgroup/net_filter/0/net_filter.fwid

2) Configuring netfilter:

  iptables -A OUTPUT -m cgroup ! --cgroup 1 -j DROP

3) Running applications:

ping 208.67.222.222 <pid:1799>
echo 1799 > /sys/fs/cgroup/net_filter/0/tasks
64 bytes from 208.67.222.222: icmp_seq=44 ttl=49 time=11.9 ms
...

ping 208.67.220.220 <pid:1804>
ping: sendmsg: Operation not permitted
...
echo 1804 > /sys/fs/cgroup/net_filter/0/tasks
64 bytes from 208.67.220.220: icmp_seq=89 ttl=56 time=19.0 ms
...

Of course, real-world deployments would make use of cgroups user space toolsuite, or custom daemons dynamically moving applications from/to net_filter cgroups.

[1] http://www.blackhat.com/presentations/bh-europe-06/bh-eu-06-biondi/bh-eu-06-biondi-up.pdf

---
 Documentation/cgroups/00-INDEX           |   2 +
 Documentation/cgroups/net_filter.txt     |  27 +++++
 include/linux/cgroup_subsys.h            |   5 +
 include/net/netfilter/xt_cgroup.h        |  58 ++++++++++
 include/net/sock.h                       |   3 +
 include/uapi/linux/netfilter/Kbuild      |   1 +
 include/uapi/linux/netfilter/xt_cgroup.h |  11 ++
 net/core/scm.c                           |   2 +
 net/core/sock.c                          |  14 +++
 net/netfilter/Kconfig                    |   8 ++
 net/netfilter/Makefile                   |   1 +
 net/netfilter/xt_cgroup.c                | 182 +++++++++++++++++++++++++++++++
 12 files changed, 314 insertions(+)
 create mode 100644 Documentation/cgroups/net_filter.txt
 create mode 100644 include/net/netfilter/xt_cgroup.h
 create mode 100644 include/uapi/linux/netfilter/xt_cgroup.h
 create mode 100644 net/netfilter/xt_cgroup.c
Documentation/cgroups/00-INDEX +2 -0
index bc461b6..14424d2 100644
@@ -20,6 +20,8 @@ memory.txt
 	- Memory Resource Controller; design, accounting, interface, testing.
 net_cls.txt
 	- Network classifier cgroups details and usages.
+net_filter.txt
+	- Network firewalling (netfilter) cgroups details and usages.
 net_prio.txt
 	- Network priority cgroups details and usages.
 resource_counter.txt
Documentation/cgroups/net_filter.txt new file +27 -0
new file mode 100644
index 0000000..0e21822
@@ -0,0 +1,27 @@
+Netfilter cgroup
+----------------
+
+The netfilter cgroup provides an interface to aggregate jobs
+to a particular netfilter tag, that can be used to apply
+various iptables/netfilter policies for those jobs in order
+to limit resources/abilities for network communication.
+
+Creating a net_filter cgroups instance creates a net_filter.fwid
+file. The value of net_filter.fwid is initialized to 0 on
+default (so only global iptables/netfilter policies apply).
+You can write a unique decimal fwid tag into net_filter.fwid
+file, and use that tag along with iptables' --cgroup option.
+
+Minimal/basic usage example:
+
+1) Configuring cgroup:
+
+ mkdir /sys/fs/cgroup/net_filter
+ mount -t cgroup -o net_filter net_filter /sys/fs/cgroup/net_filter
+ mkdir /sys/fs/cgroup/net_filter/0
+ echo 1 > /sys/fs/cgroup/net_filter/0/net_filter.fwid
+ echo [pid] > /sys/fs/cgroup/net_filter/0/tasks
+
+2) Configuring netfilter:
+
+ iptables -A OUTPUT -m cgroup ! --cgroup 1 -p tcp --dport 80 -j DROP
include/linux/cgroup_subsys.h +5 -0
index b613ffd..ef58217 100644
@@ -50,6 +50,11 @@ SUBSYS(net_prio)
 #if IS_SUBSYS_ENABLED(CONFIG_CGROUP_HUGETLB)
 SUBSYS(hugetlb)
 #endif
+
+#if IS_SUBSYS_ENABLED(CONFIG_NETFILTER_XT_MATCH_CGROUP)
+SUBSYS(net_filter)
+#endif
+
 /*
  * DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS.
  */
include/net/netfilter/xt_cgroup.h new file +58 -0

58 lines not on this page · raw message

include/net/sock.h +3 -0
index e3bf213..f7da4b4 100644
@@ -387,6 +387,9 @@ struct sock {
 #if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
 	__u32			sk_cgrp_prioidx;
 #endif
+#if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_CGROUP)
+	__u32			sk_cgrp_fwid;
+#endif
 	struct pid		*sk_peer_pid;
 	const struct cred	*sk_peer_cred;
 	long			sk_rcvtimeo;
include/uapi/linux/netfilter/Kbuild +1 -0
index 1749154..94a4890 100644
@@ -37,6 +37,7 @@ header-y += xt_TEE.h
 header-y += xt_TPROXY.h
 header-y += xt_addrtype.h
 header-y += xt_bpf.h
+header-y += xt_cgroup.h
 header-y += xt_cluster.h
 header-y += xt_comment.h
 header-y += xt_connbytes.h
include/uapi/linux/netfilter/xt_cgroup.h new file +11 -0
new file mode 100644
index 0000000..43acb7e
@@ -0,0 +1,11 @@
+#ifndef _UAPI_XT_CGROUP_H
+#define _UAPI_XT_CGROUP_H
+
+#include <linux/types.h>
+
+struct xt_cgroup_info {
+	__u32 id;
+	__u32 invert;
+};
+
+#endif /* _UAPI_XT_CGROUP_H */
net/core/scm.c +2 -0
index b442e7e..f08672a 100644
@@ -36,6 +36,7 @@
 #include <net/sock.h>
 #include <net/compat.h>
 #include <net/scm.h>
+#include <net/netfilter/xt_cgroup.h>
 #include <net/cls_cgroup.h>
 
 
@@ -290,6 +291,7 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
 		/* Bump the usage count and install the file. */
 		sock = sock_from_file(fp[i], &err);
 		if (sock) {
+			sock_update_fwid(sock->sk);
 			sock_update_netprioidx(sock->sk);
 			sock_update_classid(sock->sk);
 		}
net/core/sock.c +14 -0
index 2bd9b3f..524a376 100644
@@ -125,6 +125,7 @@
 #include <linux/skbuff.h>
 #include <net/net_namespace.h>
 #include <net/request_sock.h>
+#include <net/netfilter/xt_cgroup.h>
 #include <net/sock.h>
 #include <linux/net_tstamp.h>
 #include <net/xfrm.h>
@@ -1337,6 +1338,18 @@ void sock_update_netprioidx(struct sock *sk)
 EXPORT_SYMBOL_GPL(sock_update_netprioidx);
 #endif
 
+#if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_CGROUP)
+void sock_update_fwid(struct sock *sk)
+{
+	u32 fwid;
+
+	fwid = task_fwid(current);
+	if (fwid != sk->sk_cgrp_fwid)
+		sk->sk_cgrp_fwid = fwid;
+}
+EXPORT_SYMBOL(sock_update_fwid);
+#endif
+
 /**
  *	sk_alloc - All socket objects are allocated here
  *	@net: the applicable net namespace
@@ -1363,6 +1376,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
 
 		sock_update_classid(sk);
 		sock_update_netprioidx(sk);
+		sock_update_fwid(sk);
 	}
 
 	return sk;
net/netfilter/Kconfig +8 -0
index 6e839b6..d276ff4 100644
@@ -806,6 +806,14 @@ config NETFILTER_XT_MATCH_BPF
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NETFILTER_XT_MATCH_CGROUP
+	tristate '"control group" match support'
+	depends on NETFILTER_ADVANCED
+	depends on CGROUPS
+	---help---
+	Socket/process control group matching allows you to match locally
+	generated packets based on which control group processes belong to.
+
 config NETFILTER_XT_MATCH_CLUSTER
 	tristate '"cluster" match support'
 	depends on NF_CONNTRACK
net/netfilter/Makefile +1 -0
index c3a0a12..12f014f 100644
@@ -124,6 +124,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_MULTIPORT) += xt_multiport.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_NFACCT) += xt_nfacct.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_OSF) += xt_osf.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_OWNER) += xt_owner.o
+obj-$(CONFIG_NETFILTER_XT_MATCH_CGROUP) += xt_cgroup.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_PHYSDEV) += xt_physdev.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_PKTTYPE) += xt_pkttype.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_POLICY) += xt_policy.o
net/netfilter/xt_cgroup.c new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

net/netfilter/xt_cgroup.c line 136

Quoted 9 lines · Daniel Borkmann
net/netfilter/xt_cgroup.c new file +9 -0
+static void cgroup_attach(struct cgroup_subsys_state *css,
+			  struct cgroup_taskset *tset)
+{
+	struct task_struct *p;
+	void *v;
+
+	cgroup_taskset_for_each(p, css, tset) {
+		task_lock(p);
+		v = (void *)(unsigned long) task_fwid(p);

Shouldn't v be css_nf_state(css)->fwid?

Quoted 4 lines
net/netfilter/xt_cgroup.c new file +4 -0
+		iterate_fd(p->files, 0, cgroup_fwid_update, v);
+		task_unlock(p);
+	}
+}
#2 lore raw
Quoted 12 lines · Gao feng

net/netfilter/xt_cgroup.c line 136

Quoted 9 lines · Daniel Borkmann
net/netfilter/xt_cgroup.c new file +9 -0
+static void cgroup_attach(struct cgroup_subsys_state *css,
+			  struct cgroup_taskset *tset)
+{
+	struct task_struct *p;
+	void *v;
+
+	cgroup_taskset_for_each(p, css, tset) {
+		task_lock(p);
+		v = (void *)(unsigned long) task_fwid(p);

Shouldn't v be css_nf_state(css)->fwid?

Nope, this is in line with net_cls and net_prio; the task has been moved there via cgroup backend already through cgroup_attach_task(), so we only need to update each of it's socket sk_cgrp_fwid parts. css is not strictly for net_filter. See also: 6a328d8c6f (cgroup:
net_cls: Rework update socket logic)

Quoted 4 lines
Quoted 4 lines
net/netfilter/xt_cgroup.c new file +4 -0
+		iterate_fd(p->files, 0, cgroup_fwid_update, v);
+		task_unlock(p);
+	}
+}

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 14 lines · Daniel Borkmann
Quoted 12 lines · Gao feng

net/netfilter/xt_cgroup.c line 136

Quoted 9 lines · Daniel Borkmann
net/netfilter/xt_cgroup.c new file +9 -0
+static void cgroup_attach(struct cgroup_subsys_state *css,
+              struct cgroup_taskset *tset)
+{
+    struct task_struct *p;
+    void *v;
+
+    cgroup_taskset_for_each(p, css, tset) {
+        task_lock(p);
+        v = (void *)(unsigned long) task_fwid(p);

Shouldn't v be css_nf_state(css)->fwid?

Quoted 2 lines

Nope, this is in line with net_cls and net_prio; the task has been moved there via cgroup backend already through cgroup_attach_task(),

Yes, these tasks have already been migrated to this cgroup.

Quoted 1 line

so we only need to update each of it's socket sk_cgrp_fwid parts.

Sorry, I still don't know in which situation that css_nf_state(css)->fwid isn't equal to task_fwid(p).

two threads write the same pid to different cgroup at the same time? it seems can not happen since we have cgroup_mutex protected.

Quoted 8 lines

css is not strictly for net_filter. See also: 6a328d8c6f (cgroup:
net_cls: Rework update socket logic)

Quoted 4 lines
Quoted 4 lines
net/netfilter/xt_cgroup.c new file +4 -0
+        iterate_fd(p->files, 0, cgroup_fwid_update, v);
+        task_unlock(p);
+    }
+}
maintainer
#5 lore raw
In reply to Daniel Borkmann

Hello,

Quoted 28 lines · Daniel Borkmann

It would be useful e.g. in a server or desktop environment to have a facility in the notion of fine-grained "per application" or "per application group" firewall policies. Probably, users in the mobile/ embedded area (e.g. Android based) with different security policy requirements for application groups could have great benefit from that as well. For example, with a little bit of configuration effort, an admin could whitelist well-known applications, and thus block otherwise unwanted "hard-to-track" applications like [1] from a user's machine.

Implementation of PID-based matching would not be appropriate as they frequently change, and child tracking would make that even more complex and ugly. Cgroups would be a perfect candidate for accomplishing that as they associate a set of tasks with a set of parameters for one or more subsystems, in our case the netfilter subsystem, which, of course, can be combined with other cgroup subsystems into something more complex.

As mentioned, to overcome this constraint, such processes could be placed into one or multiple cgroups where different fine-grained rules can be defined depending on the application scenario, while e.g. everything else that is not part of that could be dropped (or vice versa), thus making life harder for unwanted processes to communicate to the outside world. So, we make use of cgroups here to track jobs and limit their resources in terms of iptables policies; in other words, limiting what they are allowed to communicate.

Quoted 4 lines

We have similar cgroup facilities in networking for traffic classifier, and netprio cgroups. This feature adds a lightweight cgroup id matching in terms of network security resp. network traffic isolation as part of netfilter's xtables subsystem.

I don't think the two net cgroups were a good idea and definitely don't want to continue the trend. I think this is being done backwards. Wouldn't it be more logical to implement netfilter rule to match the target cgroup paths? It really doesn't make much sense to me to add separate controllers to just tag processes. Please classify tasks in cgroup and let netfilter match the cgroups.

Thanks.

Signature · 1 line
tejun
#6 lore raw

Hi Tejun,

Quoted 34 lines · Tejun Heo
Quoted 28 lines · Daniel Borkmann

It would be useful e.g. in a server or desktop environment to have a facility in the notion of fine-grained "per application" or "per application group" firewall policies. Probably, users in the mobile/ embedded area (e.g. Android based) with different security policy requirements for application groups could have great benefit from that as well. For example, with a little bit of configuration effort, an admin could whitelist well-known applications, and thus block otherwise unwanted "hard-to-track" applications like [1] from a user's machine.

Implementation of PID-based matching would not be appropriate as they frequently change, and child tracking would make that even more complex and ugly. Cgroups would be a perfect candidate for accomplishing that as they associate a set of tasks with a set of parameters for one or more subsystems, in our case the netfilter subsystem, which, of course, can be combined with other cgroup subsystems into something more complex.

As mentioned, to overcome this constraint, such processes could be placed into one or multiple cgroups where different fine-grained rules can be defined depending on the application scenario, while e.g. everything else that is not part of that could be dropped (or vice versa), thus making life harder for unwanted processes to communicate to the outside world. So, we make use of cgroups here to track jobs and limit their resources in terms of iptables policies; in other words, limiting what they are allowed to communicate.

Quoted 4 lines

We have similar cgroup facilities in networking for traffic classifier, and netprio cgroups. This feature adds a lightweight cgroup id matching in terms of network security resp. network traffic isolation as part of netfilter's xtables subsystem.

Quoted 6 lines

I don't think the two net cgroups were a good idea and definitely don't want to continue the trend. I think this is being done backwards. Wouldn't it be more logical to implement netfilter rule to match the target cgroup paths? It really doesn't make much sense to me to add separate controllers to just tag processes. Please classify tasks in cgroup and let netfilter match the cgroups.

Thanks for your feedback!

Could you elaborate on "Wouldn't it be more logical to implement netfilter rule to match the target cgroup paths?". I don't think (or hope) you mean some string comparison on the dentry path here? :) With our proposal, we have in the network stack's critical path only the following code that is being executed here to match the cgroup ...

static bool
cgroup_mt(const struct sk_buff *skb, struct xt_action_param *par)

{
	const struct xt_cgroup_info *info = par->matchinfo;

	if (skb->sk == NULL)
		return false;

	return (info->id == skb->sk->sk_cgrp_fwid) ^ info->invert;
}

... where ``info->id == skb->sk->sk_cgrp_fwid'' is the actual work, so very lightweight, which is good for high loads (1Gbit/s, 10Gbit/s and beyond), of course. Also, it would be intuitive for admins familiar with other subsystems to just set up and use these cgroup ids in iptabels. I'm not yet quite sure how your suggestion would look like, so you would need to setup some "dummy" subgroups first just to have a path that you can match on?

Thanks,

Daniel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw

Hello,

Quoted 5 lines · Daniel Borkmann

Could you elaborate on "Wouldn't it be more logical to implement netfilter rule to match the target cgroup paths?". I don't think (or hope) you mean some string comparison on the dentry path here? :) With our proposal, we have in the network stack's critical path only the following code that is being executed here to match the cgroup ...

Comparing path each time obviously doesn't make sense but you can determine the cgroup on config and hold onto the pointer while the rule exists.

Quoted 6 lines

... where ``info->id == skb->sk->sk_cgrp_fwid'' is the actual work, so very lightweight, which is good for high loads (1Gbit/s, 10Gbit/s and beyond), of course. Also, it would be intuitive for admins familiar with other subsystems to just set up and use these cgroup ids in iptabels. I'm not yet quite sure how your suggestion would look like, so you would need to setup some "dummy" subgroups first just to have a path that you can match on?

Currently, it's tricky because we have multiple hierarchies to consider and there isn't an efficient way to map from task to cgroup on a specific hierarchy. I'm not sure whether we should add another mapping table in css_set or just allow using path matching on the unified hierarchy. The latter should be cleaner and easier but more restrictive.

Anyways, it isn't manageable in the long term to keep adding controllers simply to tag tasks differently. If we want to do this, let's please work on a way to match a task's cgroup affiliation efficiently.

Thanks.

Signature · 1 line
tejun
#9 lore raw
Quoted 27 lines · Tejun Heo

Hello,

Quoted 5 lines · Daniel Borkmann

Could you elaborate on "Wouldn't it be more logical to implement netfilter rule to match the target cgroup paths?". I don't think (or hope) you mean some string comparison on the dentry path here? :) With our proposal, we have in the network stack's critical path only the following code that is being executed here to match the cgroup ...

Comparing path each time obviously doesn't make sense but you can determine the cgroup on config and hold onto the pointer while the rule exists.

Quoted 6 lines

... where ``info->id == skb->sk->sk_cgrp_fwid'' is the actual work, so very lightweight, which is good for high loads (1Gbit/s, 10Gbit/s and beyond), of course. Also, it would be intuitive for admins familiar with other subsystems to just set up and use these cgroup ids in iptabels. I'm not yet quite sure how your suggestion would look like, so you would need to setup some "dummy" subgroups first just to have a path that you can match on?

Currently, it's tricky because we have multiple hierarchies to consider and there isn't an efficient way to map from task to cgroup on a specific hierarchy. I'm not sure whether we should add another mapping table in css_set or just allow using path matching on the unified hierarchy. The latter should be cleaner and easier but more restrictive.

Quoted 4 lines

Anyways, it isn't manageable in the long term to keep adding controllers simply to tag tasks differently. If we want to do this, let's please work on a way to match a task's cgroup affiliation efficiently.

Agreed, let us solve that first, and then I go back to the netfilter module to bring netfilter and cgroups together.

Thanks,

Daniel

A message that is not in the archive (sent off-list or not yet received): 20131009170409.GH22495-Gd/HAXX7CRxy/[email protected]

#11 lore raw

Hi Tejun,

Quoted 25 lines · Tejun Heo
Quoted 5 lines · Daniel Borkmann

Could you elaborate on "Wouldn't it be more logical to implement netfilter rule to match the target cgroup paths?". I don't think (or hope) you mean some string comparison on the dentry path here? :) With our proposal, we have in the network stack's critical path only the following code that is being executed here to match the cgroup ...

Comparing path each time obviously doesn't make sense but you can determine the cgroup on config and hold onto the pointer while the rule exists.

Quoted 6 lines

... where ``info->id == skb->sk->sk_cgrp_fwid'' is the actual work, so very lightweight, which is good for high loads (1Gbit/s, 10Gbit/s and beyond), of course. Also, it would be intuitive for admins familiar with other subsystems to just set up and use these cgroup ids in iptabels. I'm not yet quite sure how your suggestion would look like, so you would need to setup some "dummy" subgroups first just to have a path that you can match on?

Currently, it's tricky because we have multiple hierarchies to consider and there isn't an efficient way to map from task to cgroup on a specific hierarchy. I'm not sure whether we should add another mapping table in css_set or just allow using path matching on the unified hierarchy. The latter should be cleaner and easier but more restrictive.

Quoted 4 lines

Anyways, it isn't manageable in the long term to keep adding controllers simply to tag tasks differently. If we want to do this, let's please work on a way to match a task's cgroup affiliation efficiently.

Here's a draft (!) of an alternative w/o using a new cgroup subsystem. I've tested it and it would basically work this way as well. I've used serial_nr as an identifier of cgroups here, as we'd actually want the xt_cgroup_info structure as small as possible for rule sets (since they can be large and are flat-copied to kernel). Logic in cgroup_mt() needs to change a bit as we cannot hold css_set_lock here. Anyway, iptables would match here against cgroup.serial (that can probably also be widely used otherwise). The way we do it here is to cache the corresponding task in socket structure, and walk all cgroups belonging to that task, comparing if serial_nr's match.

Still, I think my original patch is more clean, user friendly and intuitive, and has a better performance (main work is one comparison instead of walking all corresponding cgroups), so I'd still consider this the better tradeoff to go with, I think netfilter is a large enough candidate for a subsys. ;)

Thanks,
Daniel

Draft patch:

include/linux/cgroup.h +20 -0
index 3561d30..3c5e953 100644
@@ -399,6 +399,26 @@ struct cgroup_map_cb {
 };
 
 /*
+ * A cgroup can be associated with multiple css_sets as different tasks may
+ * belong to different cgroups on different hierarchies.  In the other
+ * direction, a css_set is naturally associated with multiple cgroups.
+ * This M:N relationship is represented by the following link structure
+ * which exists for each association and allows traversing the associations
+ * from both sides.
+ */
+struct cgrp_cset_link {
+	/* the cgroup and css_set this link associates */
+	struct cgroup		*cgrp;
+	struct css_set		*cset;
+
+	/* list of cgrp_cset_links anchored at cgrp->cset_links */
+	struct list_head	cset_link;
+
+	/* list of cgrp_cset_links anchored at css_set->cgrp_links */
+	struct list_head	cgrp_link;
+};
+
+/*
  * struct cftype: handler definitions for cgroup control files
  *
  * When reading/writing to a file:
include/net/sock.h +19 -0
index e3bf213..b035ba3 100644
@@ -406,6 +406,9 @@ struct sock {
 	__u32			sk_mark;
 	u32			sk_classid;
 	struct cg_proto		*sk_cgrp;
+#if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_CGROUP)
+	struct task_struct	*sk_task_cached;
+#endif
 	void			(*sk_state_change)(struct sock *sk);
 	void			(*sk_data_ready)(struct sock *sk, int bytes);
 	void			(*sk_write_space)(struct sock *sk);
@@ -2098,6 +2101,22 @@ static inline gfp_t gfp_any(void)
 	return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
 }
 
+static inline struct task_struct *sock_task(const struct sock *sk)
+{
+#if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_CGROUP)
+	return sk->sk_task_cached;
+#else
+	return NULL;
+#endif
+}
+
+static inline void sock_task_set(struct sock *sk, struct task_struct *task)
+{
+#if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_CGROUP)
+	sk->sk_task_cached = task;
+#endif
+}
+
 static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
 {
 	return noblock ? 0 : sk->sk_rcvtimeo;
include/uapi/linux/netfilter/Kbuild +1 -0
index 1749154..94a4890 100644
@@ -37,6 +37,7 @@ header-y += xt_TEE.h
 header-y += xt_TPROXY.h
 header-y += xt_addrtype.h
 header-y += xt_bpf.h
+header-y += xt_cgroup.h
 header-y += xt_cluster.h
 header-y += xt_comment.h
 header-y += xt_connbytes.h
include/uapi/linux/netfilter/xt_cgroup.h new file +11 -0
new file mode 100644
index 0000000..c59ff53
@@ -0,0 +1,11 @@
+#ifndef _UAPI_XT_CGROUP_H
+#define _UAPI_XT_CGROUP_H
+
+#include <linux/types.h>
+
+struct xt_cgroup_info {
+	__u64 serial_nr;
+	__u32 invert;
+};
+
+#endif /* _UAPI_XT_CGROUP_H */
kernel/cgroup.c +11 -20
index 2418b6e..1f9dc5b 100644
@@ -357,26 +357,6 @@ static void cgroup_release_agent(struct work_struct *work);
 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
 static void check_for_release(struct cgroup *cgrp);
 
-/*
- * A cgroup can be associated with multiple css_sets as different tasks may
- * belong to different cgroups on different hierarchies.  In the other
- * direction, a css_set is naturally associated with multiple cgroups.
- * This M:N relationship is represented by the following link structure
- * which exists for each association and allows traversing the associations
- * from both sides.
- */
-struct cgrp_cset_link {
-	/* the cgroup and css_set this link associates */
-	struct cgroup		*cgrp;
-	struct css_set		*cset;
-
-	/* list of cgrp_cset_links anchored at cgrp->cset_links */
-	struct list_head	cset_link;
-
-	/* list of cgrp_cset_links anchored at css_set->cgrp_links */
-	struct list_head	cgrp_link;
-};
-
 /* The default css_set - used by init and its children prior to any
  * hierarchies being mounted. It contains a pointer to the root state
  * for each subsystem. Also used to anchor the list of css_sets. Not
@@ -4163,6 +4143,12 @@ static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
 	return 0;
 }
 
+static u64 cgroup_serial_read(struct cgroup_subsys_state *css,
+			      struct cftype *cft)
+{
+	return css->cgroup->serial_nr;
+}
+
 static struct cftype cgroup_base_files[] = {
 	{
 		.name = "cgroup.procs",
@@ -4187,6 +4173,11 @@ static struct cftype cgroup_base_files[] = {
 		.flags = CFTYPE_ONLY_ON_ROOT,
 		.read_seq_string = cgroup_sane_behavior_show,
 	},
+	{
+		.name = "cgroup.serial",
+		.read_u64 = cgroup_serial_read,
+		.mode = S_IRUGO,
+	},
 
 	/*
 	 * Historical crazy stuff.  These don't have "cgroup."  prefix and
net/core/scm.c +1 -0
index b442e7e..9a40ab0 100644
@@ -292,6 +292,7 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
 		if (sock) {
 			sock_update_netprioidx(sock->sk);
 			sock_update_classid(sock->sk);
+			sock_task_set(sock->sk, current);
 		}
 		fd_install(new_fd, get_file(fp[i]));
 	}
net/core/sock.c +1 -0
index 2bd9b3f..ab53afc 100644
@@ -1359,6 +1359,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
 		sk->sk_prot = sk->sk_prot_creator = prot;
 		sock_lock_init(sk);
 		sock_net_set(sk, get_net(net));
+		sock_task_set(sk, current);
 		atomic_set(&sk->sk_wmem_alloc, 1);
 
 		sock_update_classid(sk);
net/netfilter/Kconfig +8 -0
index 6e839b6..d276ff4 100644
@@ -806,6 +806,14 @@ config NETFILTER_XT_MATCH_BPF
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NETFILTER_XT_MATCH_CGROUP
+	tristate '"control group" match support'
+	depends on NETFILTER_ADVANCED
+	depends on CGROUPS
+	---help---
+	Socket/process control group matching allows you to match locally
+	generated packets based on which control group processes belong to.
+
 config NETFILTER_XT_MATCH_CLUSTER
 	tristate '"cluster" match support'
 	depends on NF_CONNTRACK
net/netfilter/Makefile +1 -0
index c3a0a12..12f014f 100644
@@ -124,6 +124,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_MULTIPORT) += xt_multiport.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_NFACCT) += xt_nfacct.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_OSF) += xt_osf.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_OWNER) += xt_owner.o
+obj-$(CONFIG_NETFILTER_XT_MATCH_CGROUP) += xt_cgroup.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_PHYSDEV) += xt_physdev.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_PKTTYPE) += xt_pkttype.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_POLICY) += xt_policy.o
net/netfilter/xt_cgroup.c new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 1380910855-12325-1-git-send-email-dborkman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#13 lore raw
Quoted 7 lines · Daniel Borkmann

Implementation of PID-based matching would not be appropriate as they frequently change, and child tracking would make that even more complex and ugly. Cgroups would be a perfect candidate for accomplishing that as they associate a set of tasks with a set of parameters for one or more subsystems, in our case the netfilter subsystem, which, of course, can be combined with other cgroup subsystems into something more complex.

I am coming to this late. But two concrete suggestions.

1) process groups and sessions don't change as frequently as pids.

2) It is possible to put a set of processes in their own network namespace and pipe just the packets you want those processes to use into that network namespace. Using an ingress queueing filter makes that process very efficient even if you have to filter by port.

So I don't think you need cgroups to solve this problem at all.

Eric

A message that is not in the archive (sent off-list or not yet received): 87li1qp3l8.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#15 lore raw
Quoted 8 lines · Eric W. Biederman

I am coming to this late. But two concrete suggestions.

1) process groups and sessions don't change as frequently as pids.

2) It is possible to put a set of processes in their own network namespace and pipe just the packets you want those processes to use into that network namespace. Using an ingress queueing filter makes that process very efficient even if you have to filter by port.

Actually in our case we're filtering outgoing traffic, based on which local socket that originated from; so you wouldn't need all of that construct. Also, you wouldn't even need to have an a-prio knowledge of the application internals regarding their use of particular use of ports or protocols. I don't think that such a setup will have the same efficiency, ease of use, and power to distinguish the application the traffic came from in such a lightweight, protocol independent and easy way.

#16 lore raw

Hi Daniel

Quoted 11 lines · Daniel Borkmann
Quoted 8 lines · Eric W. Biederman

I am coming to this late. But two concrete suggestions.

1) process groups and sessions don't change as frequently as pids.

2) It is possible to put a set of processes in their own network namespace and pipe just the packets you want those processes to use into that network namespace. Using an ingress queueing filter makes that process very efficient even if you have to filter by port.

Quoted 7 lines

Actually in our case we're filtering outgoing traffic, based on which local socket that originated from; so you wouldn't need all of that construct. Also, you wouldn't even need to have an a-prio knowledge of the application internals regarding their use of particular use of ports or protocols. I don't think that such a setup will have the same efficiency, ease of use, and power to distinguish the application the traffic came from in such a lightweight, protocol independent and easy way.

Sorry for beeing late as well (and also stupid question)

Couldn't you use something from the LSM? I mean you allow the application to create the socket etc and then block later the traffic originated from that socket. Wouldn't it make more sense to block early?

cheers,
daniel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw
Quoted 20 lines · Daniel Wagner
Quoted 11 lines · Daniel Borkmann
Quoted 8 lines · Eric W. Biederman

I am coming to this late. But two concrete suggestions.

1) process groups and sessions don't change as frequently as pids.

2) It is possible to put a set of processes in their own network namespace and pipe just the packets you want those processes to use into that network namespace. Using an ingress queueing filter makes that process very efficient even if you have to filter by port.

Quoted 7 lines

Actually in our case we're filtering outgoing traffic, based on which local socket that originated from; so you wouldn't need all of that construct. Also, you wouldn't even need to have an a-prio knowledge of the application internals regarding their use of particular use of ports or protocols. I don't think that such a setup will have the same efficiency, ease of use, and power to distinguish the application the traffic came from in such a lightweight, protocol independent and easy way.

Quoted 6 lines

Sorry for beeing late as well (and also stupid question)

Couldn't you use something from the LSM? I mean you allow the application to create the socket etc and then block later the traffic originated from that socket. Wouldn't it make more sense to block early?

I gave one simple example for blocking in the commit message, that's true, but it is not limited to that, meaning we can have much different scenarios/policies that netfilter allows us than just blocking, e.g. fine grained settings where applications are allowed to connect/send traffic to, application traffic marking/ conntracking, application-specific packet mangling, and so on, just think of the whole netfilter universe.

#19 lore raw

Hello, Daniel:
can all your examples block early before doing network operations? What's the whole netfilter universe? Can you give us more clear examples?

Thanks

Quoted 20 lines · Daniel Wagner
Quoted 11 lines · Daniel Borkmann
Quoted 8 lines · Eric W. Biederman

I am coming to this late. But two concrete suggestions.

1) process groups and sessions don't change as frequently as pids.

2) It is possible to put a set of processes in their own network namespace and pipe just the packets you want those processes to use into that network namespace. Using an ingress queueing filter makes that process very efficient even if you have to filter by port.

Quoted 7 lines

Actually in our case we're filtering outgoing traffic, based on which
local socket that originated from; so you wouldn't need all of that construct. Also, you wouldn't even need to have an a-prio knowledge
of the application internals regarding their use of particular use of
ports or protocols. I don't think that such a setup will have the
same efficiency, ease of use, and power to distinguish the
application the traffic came from in such a lightweight, protocol independent and easy way.

Quoted 6 lines

Sorry for beeing late as well (and also stupid question)

Couldn't you use something from the LSM? I mean you allow the application to create the socket etc and then block later the traffic originated from that socket. Wouldn't it make more sense to block early?

I gave one simple example for blocking in the commit message, that's true, but it is not limited to that, meaning we can have much different scenarios/policies that netfilter allows us than just blocking, e.g. fine grained settings where applications are allowed to connect/send traffic to, application traffic marking/ conntracking, application-specific packet mangling, and so on, just think of the whole netfilter universe. --
To unsubscribe from this list: send the line "unsubscribe cgroups" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#20 lore raw
Quoted 2 lines · Xun

Hello, Daniel:
can all your examples block early before doing network operations? What's the whole netfilter universe? Can you give us more clear examples?

As you can see from the code, the netfilter hooks are located in NF_INET_LOCAL_OUT and NF_INET_POST_ROUTING.

Quoted 33 lines

Thanks

Quoted 20 lines · Daniel Wagner
Quoted 11 lines · Daniel Borkmann
Quoted 8 lines · Eric W. Biederman

I am coming to this late. But two concrete suggestions.

1) process groups and sessions don't change as frequently as pids.

2) It is possible to put a set of processes in their own network namespace and pipe just the packets you want those processes to use into that network namespace. Using an ingress queueing filter makes that process very efficient even if you have to filter by port.

Quoted 7 lines

Actually in our case we're filtering outgoing traffic, based on which
local socket that originated from; so you wouldn't need all of that construct. Also, you wouldn't even need to have an a-prio knowledge
of the application internals regarding their use of particular use of
ports or protocols. I don't think that such a setup will have the
same efficiency, ease of use, and power to distinguish the
application the traffic came from in such a lightweight, protocol independent and easy way.

Quoted 6 lines

Sorry for beeing late as well (and also stupid question)

Couldn't you use something from the LSM? I mean you allow the application to create the socket etc and then block later the traffic originated from that socket. Wouldn't it make more sense to block early?

I gave one simple example for blocking in the commit message, that's true, but it is not limited to that, meaning we can have much different scenarios/policies that netfilter allows us than just blocking, e.g. fine grained settings where applications are allowed to connect/send traffic to, application traffic marking/ conntracking, application-specific packet mangling, and so on, just think of the whole netfilter universe. --
To unsubscribe from this list: send the line "unsubscribe cgroups" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#21 lore raw
In reply to Ni, Xun

Hi Xun,

Quoted 2 lines · Xun

Hello, Daniel:
can all your examples block early before doing network operations?

I was referring to Linux Security Module which allows to define access policies for an application e.g. which ports are allowed to be used.

If the goal is just to block those ports you don't have to go through half of the networking stack to figure out via an iptable rules that this access is not allowed.

Quoted 2 lines

What's the whole netfilter universe? Can you give us more clear examples?

I am not sure if I understood your question correctly. In case you are asking what netfilter is I would like pointing you to the
http://www.netfilter.org/ project page.

cheers,
daniel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#23 lore raw
Quoted 30 lines · Daniel Borkmann
Quoted 22 lines · Daniel Wagner
Quoted 12 lines · Daniel Borkmann
Quoted 9 lines · Eric W. Biederman

I am coming to this late. But two concrete suggestions.

1) process groups and sessions don't change as frequently as pids.

2) It is possible to put a set of processes in their own network namespace and pipe just the packets you want those processes to use into that network namespace. Using an ingress queueing filter makes that process very efficient even if you have to filter by
port.

Quoted 8 lines

Actually in our case we're filtering outgoing traffic, based on which local socket that originated from; so you wouldn't need all of that construct. Also, you wouldn't even need to have an a-prio knowledge of the application internals regarding their use of particular use of ports or protocols. I don't think that such a setup will have the same efficiency, ease of use, and power to distinguish the application the traffic came from in such a lightweight, protocol independent and easy way.

Quoted 6 lines

Sorry for beeing late as well (and also stupid question)

Couldn't you use something from the LSM? I mean you allow the application to create the socket etc and then block later the traffic originated from that socket. Wouldn't it make more sense to block early?

Quoted 7 lines

I gave one simple example for blocking in the commit message, that's true, but it is not limited to that, meaning we can have much different scenarios/policies that netfilter allows us than just blocking, e.g. fine grained settings where applications are allowed to connect/send traffic to, application traffic marking/ conntracking, application-specific packet mangling, and so on, just think of the whole netfilter universe.

Oh, I didn't pay enough attention to the commit message. Sorry about that. Obviously, if fine grained settings is a must then blocking the write is not good enough.

cheers,
daniel