Skip to contentSkip to conversation

new cgroup controller "fork"

PATCH In review Max Kellermann 6 participants 9 messages Feb 17, 2011 – Feb 18, 2011 3 messages not in the archive

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

PATCH 1/1 new cgroup controller "fork"

Can limit the number of fork()/clone() calls in a cgroup. It is useful as a safeguard against fork bombs.

---
 Documentation/cgroups/fork.txt |   30 +++++++
 include/linux/cgroup_fork.h    |   26 ++++++
 include/linux/cgroup_subsys.h  |    6 +
 init/Kconfig                   |    6 +
 kernel/Makefile                |    1 
 kernel/cgroup_fork.c           |  180 ++++++++++++++++++++++++++++++++++++++++
 kernel/fork.c                  |    5 +
 7 files changed, 254 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/cgroups/fork.txt
 create mode 100644 include/linux/cgroup_fork.h
 create mode 100644 kernel/cgroup_fork.c
Documentation/cgroups/fork.txt new file +30 -0
new file mode 100644
index 0000000..dfbf291
@@ -0,0 +1,30 @@
+The "fork" Controller
+---------------------
+
+The "fork" controller limits the number of times a new child process
+or thread can be created.  It maintains a per-group counter which gets
+decremented on each fork() / clone().  When the counter reaches zero,
+no process in the cgroup is allowed to create new child
+processes/threads, even if existing ones quit.
+
+This has been proven useful in a shared hosting environment.  A new
+temporary cgroup is created for each CGI process, and the maximum fork
+count is configured to a sensible value.  Since CGIs are expected to
+run for only a short time with predictable resource usage, this may be
+an appropriate tool to limit the damage that a freaked CGI can do.
+
+Initially, the counter is set to -1, which is a magic value for
+"disabled" - no limits are imposed on the processes in the group.  To
+set a new value, type (in the working directory of that control
+group):
+
+ echo 16 > fork.remaining
+
+This examples allows 16 forks in the control group.  0 means no
+further forks are allowed.  The limit may be lowered or increased or
+even disabled at any time by a process with write permissions to the
+attribute.
+
+To check if a fork is allowed, the controller walks the cgroup
+hierarchy up, and verifies all ancestors.  The counter of all
+ancestors is decreased.
include/linux/cgroup_fork.h new file +26 -0
new file mode 100644
index 0000000..4ac66b3
@@ -0,0 +1,26 @@
+#ifndef _LINUX_CGROUP_FORK_H
+#define _LINUX_CGROUP_FORK_H
+
+#ifdef CONFIG_CGROUP_FORK
+
+/**
+ * Checks if another fork is allowed.  Call this before creating a new
+ * child process.
+ *
+ * @return 0 on success, a negative errno value if forking should be
+ * denied
+ */
+int
+cgroup_fork_pre_fork(void);
+
+#else /* !CONFIG_CGROUP_FORK */
+
+static inline int
+cgroup_fork_pre_fork(void)
+{
+	return 0;
+}
+
+#endif /* !CONFIG_CGROUP_FORK */
+
+#endif /* !_LINUX_CGROUP_FORK_H */
include/linux/cgroup_subsys.h +6 -0
index ccefff0..8ead7f9 100644
@@ -66,3 +66,9 @@ SUBSYS(blkio)
 #endif
 
 /* */
+
+#ifdef CONFIG_CGROUP_FORK
+SUBSYS(fork)
+#endif
+
+/* */
init/Kconfig +6 -0
index 17e2cfb..ef53a85 100644
@@ -596,6 +596,12 @@ config CGROUP_FREEZER
 	  Provides a way to freeze and unfreeze all tasks in a
 	  cgroup.
 
+config CGROUP_FORK
+	bool "fork controller for cgroups"
+	help
+	  Limits the number of fork() calls in a cgroup.  An application
+	  for this is to make a cgroup safe against fork bombs.
+
 config CGROUP_DEVICE
 	bool "Device controller for cgroups"
 	help
kernel/Makefile +1 -0
index 353d3fe..b58cc01 100644
@@ -61,6 +61,7 @@ obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CGROUPS) += cgroup.o
 obj-$(CONFIG_CGROUP_FREEZER) += cgroup_freezer.o
+obj-$(CONFIG_CGROUP_FORK) += cgroup_fork.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CGROUP_NS) += ns_cgroup.o
 obj-$(CONFIG_UTS_NS) += utsname.o
kernel/cgroup_fork.c new file +180 -0

180 lines not on this page · raw message

kernel/fork.c +5 -0
index 25e4291..35836e6 100644
@@ -32,6 +32,7 @@
 #include <linux/capability.h>
 #include <linux/cpu.h>
 #include <linux/cgroup.h>
+#include <linux/cgroup_fork.h>
 #include <linux/security.h>
 #include <linux/hugetlb.h>
 #include <linux/swap.h>
@@ -1024,6 +1025,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 				current->signal->flags & SIGNAL_UNKILLABLE)
 		return ERR_PTR(-EINVAL);
 
+	retval = cgroup_fork_pre_fork();
+	if (retval)
+		goto fork_out;
+
 	retval = security_task_create(clone_flags);
 	if (retval)
 		goto fork_out;
#2 lore raw
In reply to a message not in the archive
Quoted 3 lines · On Thu, 17 Feb 2011 14:31:52 +0100 Max Kellermann…

Can limit the number of fork()/clone() calls in a cgroup. It is useful as a safeguard against fork bombs.

brief comments below.

Quoted 1 line

<snip>

Quoted 5 lines
kernel/cgroup_fork.c new file +5 -0
+static int
+cgroup_fork_remaining_write(struct cgroup *cgroup, struct cftype *cft,
+			    s64 value)
+{
+	struct cgroup_fork *t = cgroup_fork_group(cgroup);

kernel/cgroup_fork.c line 127

Quoted 10 lines
kernel/cgroup_fork.c new file +10 -0
+
+	if (value < -1 || value > (1L << 30))
+		return -EINVAL;
+
+	spin_lock_irq(&t->lock);
+	t->remaining = (int)value;
+	spin_unlock_irq(&t->lock);
+
+	return 0;
+}

I wonder allowing to set the limit to Root cgroup may imply the system death. How about disabling to set value to Root cgroup ?

Quoted 33 lines
kernel/cgroup_fork.c new file +33 -0
+
+static const struct cftype cgroup_fork_files[] =  {
+	{
+		.name = "remaining",
+		.read_s64 = cgroup_fork_remaining_read,
+		.write_s64 = cgroup_fork_remaining_write,
+	},
+};
+
+static int
+cgroup_fork_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
+{
+	if (cgroup->parent == NULL)
+		/* cannot limit the root cgroup */
+		return 0;
+
+	return cgroup_add_files(cgroup, ss, cgroup_fork_files,
+				ARRAY_SIZE(cgroup_fork_files));
+}
+
+struct cgroup_subsys fork_subsys = {
+	.name = "fork",
+	.create = cgroup_fork_create,
+	.destroy = cgroup_fork_destroy,
+	.fork = cgroup_fork_fork,
+	.populate = cgroup_fork_populate,
+	.subsys_id = fork_subsys_id,
+};
+
+int
+cgroup_fork_pre_fork(void)
+{
+	struct cgroup_fork *t;

kernel/cgroup_fork.c line 170

Quoted 10 lines
kernel/cgroup_fork.c new file +10 -0
+	int err = 0;
+
+	rcu_read_lock();
+
+	t = cgroup_fork_current();
+	while (t->css.cgroup->parent != NULL && err == 0) {
+		spin_lock_irq(&t->lock);
+
+		if (t->remaining == 0)
+			err = -EPERM;

IIRC, fork()'s error code is EAGAIN or ENOMEM. The exisiting limit of rlimit() returns EAGAIN.

How about -EAGAIN here ? I think it's not good to add new error code for system calls.

Thanks,
-Kame

#4 lore raw
In reply to Max Kellermann
Quoted 3 lines · On Thu, 17 Feb 2011 14:31:52 +0100 Max Kellermann <[email protected]> wrote:

Can limit the number of fork()/clone() calls in a cgroup. It is useful as a safeguard against fork bombs.

brief comments below.

Quoted 1 line

<snip>

Quoted 5 lines
kernel/cgroup_fork.c new file +5 -0
+static int
+cgroup_fork_remaining_write(struct cgroup *cgroup, struct cftype *cft,
+			    s64 value)
+{
+	struct cgroup_fork *t = cgroup_fork_group(cgroup);

kernel/cgroup_fork.c line 127

Quoted 10 lines
kernel/cgroup_fork.c new file +10 -0
+
+	if (value < -1 || value > (1L << 30))
+		return -EINVAL;
+
+	spin_lock_irq(&t->lock);
+	t->remaining = (int)value;
+	spin_unlock_irq(&t->lock);
+
+	return 0;
+}

I wonder allowing to set the limit to Root cgroup may imply the system death. How about disabling to set value to Root cgroup ?

Quoted 33 lines
kernel/cgroup_fork.c new file +33 -0
+
+static const struct cftype cgroup_fork_files[] =  {
+	{
+		.name = "remaining",
+		.read_s64 = cgroup_fork_remaining_read,
+		.write_s64 = cgroup_fork_remaining_write,
+	},
+};
+
+static int
+cgroup_fork_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
+{
+	if (cgroup->parent == NULL)
+		/* cannot limit the root cgroup */
+		return 0;
+
+	return cgroup_add_files(cgroup, ss, cgroup_fork_files,
+				ARRAY_SIZE(cgroup_fork_files));
+}
+
+struct cgroup_subsys fork_subsys = {
+	.name = "fork",
+	.create = cgroup_fork_create,
+	.destroy = cgroup_fork_destroy,
+	.fork = cgroup_fork_fork,
+	.populate = cgroup_fork_populate,
+	.subsys_id = fork_subsys_id,
+};
+
+int
+cgroup_fork_pre_fork(void)
+{
+	struct cgroup_fork *t;

kernel/cgroup_fork.c line 170

Quoted 10 lines
kernel/cgroup_fork.c new file +10 -0
+	int err = 0;
+
+	rcu_read_lock();
+
+	t = cgroup_fork_current();
+	while (t->css.cgroup->parent != NULL && err == 0) {
+		spin_lock_irq(&t->lock);
+
+		if (t->remaining == 0)
+			err = -EPERM;

IIRC, fork()'s error code is EAGAIN or ENOMEM. The exisiting limit of rlimit() returns EAGAIN.

How about -EAGAIN here ? I think it's not good to add new error code for system calls.

Thanks,
-Kame

#5 lore raw
Quoted 2 lines · KAMEZAWA Hiroyuki

I wonder allowing to set the limit to Root cgroup may imply the system death. How about disabling to set value to Root cgroup ?

That is taken care of already:

Quoted 6 lines

kernel/cgroup_fork.c line 142

Quoted 6 lines
kernel/cgroup_fork.c new file +6 -0
+static int
+cgroup_fork_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
+{
+	if (cgroup->parent == NULL)
+		/* cannot limit the root cgroup */
+		return 0;

The attribute simply doesn't exist in the root cgroup.

Also watch the loop condition in cgroup_fork_pre_fork() closely, the root cgroup isn't checked (even if you could find a way to configure it):

Quoted 9 lines

kernel/cgroup_fork.c line 166

Quoted 2 lines
kernel/cgroup_fork.c new file +2 -0
+	t = cgroup_fork_current();
+	while (t->css.cgroup->parent != NULL && err == 0) {

IIRC, fork()'s error code is EAGAIN or ENOMEM. The exisiting limit of rlimit() returns EAGAIN.

How about -EAGAIN here ? I think it's not good to add new error code for system calls.

EPERM seemed appropriate to me, because the administrator disallows more than N forks. If there are practical reasons for changing it to EAGAIN or ENOMEM, I'm ok with that. Thanks for the hint.

Max

#7 lore raw
In reply to a message not in the archive
Quoted 2 lines · KAMEZAWA Hiroyuki

I wonder allowing to set the limit to Root cgroup may imply the system death. How about disabling to set value to Root cgroup ?

That is taken care of already:

Quoted 6 lines

kernel/cgroup_fork.c line 142

Quoted 6 lines
kernel/cgroup_fork.c new file +6 -0
+static int
+cgroup_fork_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
+{
+	if (cgroup->parent == NULL)
+		/* cannot limit the root cgroup */
+		return 0;

The attribute simply doesn't exist in the root cgroup.

Also watch the loop condition in cgroup_fork_pre_fork() closely, the root cgroup isn't checked (even if you could find a way to configure it):

Quoted 9 lines

kernel/cgroup_fork.c line 166

Quoted 2 lines
kernel/cgroup_fork.c new file +2 -0
+	t = cgroup_fork_current();
+	while (t->css.cgroup->parent != NULL && err == 0) {

IIRC, fork()'s error code is EAGAIN or ENOMEM. The exisiting limit of rlimit() returns EAGAIN.

How about -EAGAIN here ? I think it's not good to add new error code for system calls.

EPERM seemed appropriate to me, because the administrator disallows more than N forks. If there are practical reasons for changing it to EAGAIN or ENOMEM, I'm ok with that. Thanks for the hint.

Max

#3 lore raw
In reply to a message not in the archive
Quoted 2 lines · Max Kellermann

Can limit the number of fork()/clone() calls in a cgroup.  It is useful as a safeguard against fork bombs.

I'd be inclined to simplify this a bit - avoid impacting the fork() path twice, with cgroup_fork_pre_fork() and cgroup_fork_fork() and just do the checks and decrements in a single pass. (In the event of hitting a limit, you may need to make another partial pass up the tree to restore the charged fork attempts)

Yes, it's true that you might charge for a fork() that later failed for some other reason, but this will very rare (except on a machine that's already screwed for other reasons) so that I don't think anyone would complain about it. Especially if you explicitly document "fork.remaining" as number of permitted "fork attempts".

Also, it would be slightly clearer to use fork_cgroup_* rather than cgroup_fork_* - this makes it clearer that it's part of a cgroups subsystem called "fork", rather than part of the cgroups core framework.

I don't think that you need to make your spinlock IRQ-safe - AFAICS nothing accesses it from the interrupt path.

Paul

#8 lore raw
In reply to Max Kellermann
Quoted 2 lines · Max Kellermann

Can limit the number of fork()/clone() calls in a cgroup.  It is useful as a safeguard against fork bombs.

I'd be inclined to simplify this a bit - avoid impacting the fork() path twice, with cgroup_fork_pre_fork() and cgroup_fork_fork() and just do the checks and decrements in a single pass. (In the event of hitting a limit, you may need to make another partial pass up the tree to restore the charged fork attempts)

Yes, it's true that you might charge for a fork() that later failed for some other reason, but this will very rare (except on a machine that's already screwed for other reasons) so that I don't think anyone would complain about it. Especially if you explicitly document "fork.remaining" as number of permitted "fork attempts".

Also, it would be slightly clearer to use fork_cgroup_* rather than cgroup_fork_* - this makes it clearer that it's part of a cgroups subsystem called "fork", rather than part of the cgroups core framework.

I don't think that you need to make your spinlock IRQ-safe - AFAICS nothing accesses it from the interrupt path.

Paul

#9 lore raw
Quoted 5 lines · Paul Menage

I'd be inclined to simplify this a bit - avoid impacting the fork() path twice, with cgroup_fork_pre_fork() and cgroup_fork_fork() and just do the checks and decrements in a single pass. (In the event of hitting a limit, you may need to make another partial pass up the tree to restore the charged fork attempts)

I have implemented it, but I don't like your idea. It actually complicates the code. It tries to do two things at once, and running again until it hits the failed cgroup seems somewhat fragile. I believe the overhead for doing two separate runs in case of success is negligible compared to the cost of sys_fork().

(Documentation not adjusted yet in the new patch)

Quoted 4 lines

Also, it would be slightly clearer to use fork_cgroup_* rather than cgroup_fork_* - this makes it clearer that it's part of a cgroups subsystem called "fork", rather than part of the cgroups core framework.

Changed, but I've preserved the file name cgroup_fork.c. Do you want me to change that, too? (What about cgroup_freezer.c and the config option names CONFIG_CGROUP_*?)

Quoted 2 lines

I don't think that you need to make your spinlock IRQ-safe - AFAICS nothing accesses it from the interrupt path.

Changed.

Quoted 2 lines · KAMEZAWA Hiroyuki

How about -EAGAIN here ? I think it's not good to add new error code for system calls.

Changed that, but that got me a funny quirk while testing:

 bear:~# ls
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: Resource temporarily unavailable
 bear:~# 

Generally, I don't think EAGAIN is a good errno code for "adminstrative limit exceeded". EAGAIN's meaning is "try again later". Usually there is something like poll() to wait until the resource is available - but a process cannot wait for the adminstrator to raise the configured limits. You could blame that quirk on bash, because it does not consider that divergent definition of EAGAIN for fork()..

The changed patch follows for further discussion; I'll repost the complete patch with description again once we agree that it's finished.

Max

Documentation/cgroups/fork.txt new file +30 -0
new file mode 100644
index 0000000..dfbf291
@@ -0,0 +1,30 @@
+The "fork" Controller
+---------------------
+
+The "fork" controller limits the number of times a new child process
+or thread can be created.  It maintains a per-group counter which gets
+decremented on each fork() / clone().  When the counter reaches zero,
+no process in the cgroup is allowed to create new child
+processes/threads, even if existing ones quit.
+
+This has been proven useful in a shared hosting environment.  A new
+temporary cgroup is created for each CGI process, and the maximum fork
+count is configured to a sensible value.  Since CGIs are expected to
+run for only a short time with predictable resource usage, this may be
+an appropriate tool to limit the damage that a freaked CGI can do.
+
+Initially, the counter is set to -1, which is a magic value for
+"disabled" - no limits are imposed on the processes in the group.  To
+set a new value, type (in the working directory of that control
+group):
+
+ echo 16 > fork.remaining
+
+This examples allows 16 forks in the control group.  0 means no
+further forks are allowed.  The limit may be lowered or increased or
+even disabled at any time by a process with write permissions to the
+attribute.
+
+To check if a fork is allowed, the controller walks the cgroup
+hierarchy up, and verifies all ancestors.  The counter of all
+ancestors is decreased.
include/linux/cgroup_fork.h new file +26 -0
new file mode 100644
index 0000000..aef1dbd
@@ -0,0 +1,26 @@
+#ifndef _LINUX_CGROUP_FORK_H
+#define _LINUX_CGROUP_FORK_H
+
+#ifdef CONFIG_CGROUP_FORK
+
+/**
+ * Checks if another fork is allowed.  Call this before creating a new
+ * child process.
+ *
+ * @return 0 on success, a negative errno value if forking should be
+ * denied
+ */
+int
+fork_cgroup_pre_fork(void);
+
+#else /* !CONFIG_CGROUP_FORK */
+
+static inline int
+fork_cgroup_pre_fork(void)
+{
+	return 0;
+}
+
+#endif /* !CONFIG_CGROUP_FORK */
+
+#endif /* !_LINUX_CGROUP_FORK_H */
include/linux/cgroup_subsys.h +6 -0
index ccefff0..8ead7f9 100644
@@ -66,3 +66,9 @@ SUBSYS(blkio)
 #endif
 
 /* */
+
+#ifdef CONFIG_CGROUP_FORK
+SUBSYS(fork)
+#endif
+
+/* */
init/Kconfig +6 -0
index 17e2cfb..ef53a85 100644
@@ -596,6 +596,12 @@ config CGROUP_FREEZER
 	  Provides a way to freeze and unfreeze all tasks in a
 	  cgroup.
 
+config CGROUP_FORK
+	bool "fork controller for cgroups"
+	help
+	  Limits the number of fork() calls in a cgroup.  An application
+	  for this is to make a cgroup safe against fork bombs.
+
 config CGROUP_DEVICE
 	bool "Device controller for cgroups"
 	help
kernel/Makefile +1 -0
index 353d3fe..b58cc01 100644
@@ -61,6 +61,7 @@ obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CGROUPS) += cgroup.o
 obj-$(CONFIG_CGROUP_FREEZER) += cgroup_freezer.o
+obj-$(CONFIG_CGROUP_FORK) += cgroup_fork.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CGROUP_NS) += ns_cgroup.o
 obj-$(CONFIG_UTS_NS) += utsname.o
kernel/cgroup_fork.c new file +186 -0

186 lines not on this page · raw message

kernel/fork.c +5 -0
index 25e4291..0f06202 100644
@@ -32,6 +32,7 @@
 #include <linux/capability.h>
 #include <linux/cpu.h>
 #include <linux/cgroup.h>
+#include <linux/cgroup_fork.h>
 #include <linux/security.h>
 #include <linux/hugetlb.h>
 #include <linux/swap.h>
@@ -1024,6 +1025,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 				current->signal->flags & SIGNAL_UNKILLABLE)
 		return ERR_PTR(-EINVAL);
 
+	retval = fork_cgroup_pre_fork();
+	if (retval)
+		goto fork_out;
+
 	retval = security_task_create(clone_flags);
 	if (retval)
 		goto fork_out;
#11 lore raw
In reply to a message not in the archive
Quoted 5 lines · Paul Menage

I'd be inclined to simplify this a bit - avoid impacting the fork() path twice, with cgroup_fork_pre_fork() and cgroup_fork_fork() and just do the checks and decrements in a single pass. (In the event of hitting a limit, you may need to make another partial pass up the tree to restore the charged fork attempts)

I have implemented it, but I don't like your idea. It actually complicates the code. It tries to do two things at once, and running again until it hits the failed cgroup seems somewhat fragile. I believe the overhead for doing two separate runs in case of success is negligible compared to the cost of sys_fork().

(Documentation not adjusted yet in the new patch)

Quoted 4 lines

Also, it would be slightly clearer to use fork_cgroup_* rather than cgroup_fork_* - this makes it clearer that it's part of a cgroups subsystem called "fork", rather than part of the cgroups core framework.

Changed, but I've preserved the file name cgroup_fork.c. Do you want me to change that, too? (What about cgroup_freezer.c and the config option names CONFIG_CGROUP_*?)

Quoted 2 lines

I don't think that you need to make your spinlock IRQ-safe - AFAICS nothing accesses it from the interrupt path.

Changed.

Quoted 2 lines · KAMEZAWA Hiroyuki

How about -EAGAIN here ? I think it's not good to add new error code for system calls.

Changed that, but that got me a funny quirk while testing:

 bear:~# ls
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: retry: Resource temporarily unavailable
 -bash: fork: Resource temporarily unavailable
 bear:~# 

Generally, I don't think EAGAIN is a good errno code for "adminstrative limit exceeded". EAGAIN's meaning is "try again later". Usually there is something like poll() to wait until the resource is available - but a process cannot wait for the adminstrator to raise the configured limits. You could blame that quirk on bash, because it does not consider that divergent definition of EAGAIN for fork()..

The changed patch follows for further discussion; I'll repost the complete patch with description again once we agree that it's finished.

Max

Documentation/cgroups/fork.txt new file +30 -0
new file mode 100644
index 0000000..dfbf291
@@ -0,0 +1,30 @@
+The "fork" Controller
+---------------------
+
+The "fork" controller limits the number of times a new child process
+or thread can be created.  It maintains a per-group counter which gets
+decremented on each fork() / clone().  When the counter reaches zero,
+no process in the cgroup is allowed to create new child
+processes/threads, even if existing ones quit.
+
+This has been proven useful in a shared hosting environment.  A new
+temporary cgroup is created for each CGI process, and the maximum fork
+count is configured to a sensible value.  Since CGIs are expected to
+run for only a short time with predictable resource usage, this may be
+an appropriate tool to limit the damage that a freaked CGI can do.
+
+Initially, the counter is set to -1, which is a magic value for
+"disabled" - no limits are imposed on the processes in the group.  To
+set a new value, type (in the working directory of that control
+group):
+
+ echo 16 > fork.remaining
+
+This examples allows 16 forks in the control group.  0 means no
+further forks are allowed.  The limit may be lowered or increased or
+even disabled at any time by a process with write permissions to the
+attribute.
+
+To check if a fork is allowed, the controller walks the cgroup
+hierarchy up, and verifies all ancestors.  The counter of all
+ancestors is decreased.
include/linux/cgroup_fork.h new file +26 -0
new file mode 100644
index 0000000..aef1dbd
@@ -0,0 +1,26 @@
+#ifndef _LINUX_CGROUP_FORK_H
+#define _LINUX_CGROUP_FORK_H
+
+#ifdef CONFIG_CGROUP_FORK
+
+/**
+ * Checks if another fork is allowed.  Call this before creating a new
+ * child process.
+ *
+ * @return 0 on success, a negative errno value if forking should be
+ * denied
+ */
+int
+fork_cgroup_pre_fork(void);
+
+#else /* !CONFIG_CGROUP_FORK */
+
+static inline int
+fork_cgroup_pre_fork(void)
+{
+	return 0;
+}
+
+#endif /* !CONFIG_CGROUP_FORK */
+
+#endif /* !_LINUX_CGROUP_FORK_H */
include/linux/cgroup_subsys.h +6 -0
index ccefff0..8ead7f9 100644
@@ -66,3 +66,9 @@ SUBSYS(blkio)
 #endif
 
 /* */
+
+#ifdef CONFIG_CGROUP_FORK
+SUBSYS(fork)
+#endif
+
+/* */
init/Kconfig +6 -0
index 17e2cfb..ef53a85 100644
@@ -596,6 +596,12 @@ config CGROUP_FREEZER
 	  Provides a way to freeze and unfreeze all tasks in a
 	  cgroup.
 
+config CGROUP_FORK
+	bool "fork controller for cgroups"
+	help
+	  Limits the number of fork() calls in a cgroup.  An application
+	  for this is to make a cgroup safe against fork bombs.
+
 config CGROUP_DEVICE
 	bool "Device controller for cgroups"
 	help
kernel/Makefile +1 -0
index 353d3fe..b58cc01 100644
@@ -61,6 +61,7 @@ obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CGROUPS) += cgroup.o
 obj-$(CONFIG_CGROUP_FREEZER) += cgroup_freezer.o
+obj-$(CONFIG_CGROUP_FORK) += cgroup_fork.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CGROUP_NS) += ns_cgroup.o
 obj-$(CONFIG_UTS_NS) += utsname.o
kernel/cgroup_fork.c new file +186 -0

186 lines not on this page · raw message

kernel/fork.c +5 -0
index 25e4291..0f06202 100644
@@ -32,6 +32,7 @@
 #include <linux/capability.h>
 #include <linux/cpu.h>
 #include <linux/cgroup.h>
+#include <linux/cgroup_fork.h>
 #include <linux/security.h>
 #include <linux/hugetlb.h>
 #include <linux/swap.h>
@@ -1024,6 +1025,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 				current->signal->flags & SIGNAL_UNKILLABLE)
 		return ERR_PTR(-EINVAL);
 
+	retval = fork_cgroup_pre_fork();
+	if (retval)
+		goto fork_out;
+
 	retval = security_task_create(clone_flags);
 	if (retval)
 		goto fork_out;