You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using docker with native driver. (It is much faster than lxc driver.)
Now, docker command have option "-m" to limit memory usage.
But, there is no way to prevent "fork bomb", I think.
Nowadays, linux kernel support "memory. kmem.limit_in_bytes" cgroups settings to prevent fork bomb.
It is quite nice if there is some way to set "memory. kmem.limit_in_bytes" cgroups settings .
Or, probably it is even nice if there is generic way to specify cgroups settings.
One workaround is to use "ulimit -u", but it does not work well for root user or if there are multiple container with same user.
I am also interested in this, and I think it's interesting for all security-sensitive scenarios (IaaS/PaaS). @crosbymichael can we create a 'security' label for issues (different than /project/security)?
WARNING: Current implementation lacks reclaim support. That means allocation
attempts will fail when close to the limit even if there are plenty of
kmem available for reclaim. That makes this option unusable in real
life so DO NOT SELECT IT unless for development purposes.
another solution I found is task limiting dwengen/linux@7b6814f but that doesn't seem to make it into the kernel
then at that point it can grow enough that, if we limit the memory, it will be limited by that before it takes down the host. For testing, was using docker 1.5, launching a container with:
docker run -t -i --rm --memory=200M --memory-swap=400M ubuntu:14.04 /bin/bash
and then triggering a forkbomb in it. Inside, the container becomes unusable immediately. Outside, on the host, it doesn't die (if we don't expand the pid/thread space, host does die here), but we're very slowly leaking memory. Over several hours, it grew to using >2G of memory (virtual), and was still growing. Not sure if there's some other setting I need to look into, for now, continuing to try to lock it down so it can't do this.
@nfritze you could try working with ulimit to set reasonable limits for processes running within Docker. However, because you may be running multiple processes and some may need more, others may need less resources, and even worse, you cannot increase some of the limits within Docker after they have been decreased, this may or may not work for you. For me, what worked best was leaving everything as is, except for setting strict ulimit settings for processes that I was actually expecting to cause issues.
But this only works if you can set VERY strict ulimit settings. Otherwise, in my testing, both the host and the container were suffering from severe performance degradation (making it very difficult to strace/gdb (in the beginning, I didn't even knew which part of my code exactly was causing the fork bomb) or debug in any other way what's exactly happening) and sometimes even crashing, if the limits were too high.
TL;DR: ulimit... that's the best you could do, from what I've tried.
@cpuguy83 It is. And kernel devs suggested to use memory.kmem.limit_in_bytes to prevent things like fork-bombs, but it doesn't seem to me like a very good approach.
@cpuguy83 While --kernel-memory provides at least usable solution (in contrast to ulimit nproc), it is still only a half of the story. We cannot do anything with it just yet, but cgroup PIDs subsystem (CGROUP_PIDS kernel setting) is going to get merged to Linux Kernel 4.3. In my opinion, only when PIDs subsystem is there and Docker support for it is added, only then we can claim the fork-bomb prevention is covered.
@frol@cpuguy83 Could you elaborate on how --kernel-memory can be used to prevent fork bombs? I couldn't find any good information on this. Will this be included in the next release? Thanks a lot.
@haches I set kernel memory limit to 1MB (I haven't used --kernel-memory option yet since I have worked around this issue with cgmemtime). It might not work nice when your containered app uses kernel memory not only for processes, but also, for example, for kernel-level cryptography (I'm not very familiar with the topic of kernel memory, though).
@frol It's an understatement to say that it doesn't work nice with kernel-level cryptography. I tried running a fork bomb in docker with --kernel-memory=4M on a system with ecryptfs. When I ran it, it successfully defused, but my kernel remounted my root fs read-only, for reasons that weren't apparent at the time. When I rebooted, it dropped me into an initramfs shell, and wouldn't boot until I fscked the borked fs.
Activity
gdm85 commentedon Jun 23, 2014
I am also interested in this, and I think it's interesting for all security-sensitive scenarios (IaaS/PaaS).
@crosbymichael can we create a 'security' label for issues (different than /project/security)?
fjakobs commentedon Aug 16, 2014
I'm also interested in this however there is a scary warning regarding kmem in the kernel docs https://github.com/torvalds/linux/blob/master/Documentation/cgroups/memory.txt#L273
another solution I found is task limiting dwengen/linux@7b6814f but that doesn't seem to make it into the kernel
jessfraz commentedon Feb 25, 2015
ping @crosbymichael im going to label as black-belt hope thats cool
nkfritze commentedon Feb 27, 2015
Have been looking into this as well on our docker setup. If we bump the pid/kernel-thread address space on the host a bit to give it breathing room:
echo $((222))> /proc/sys/kernel/pid_max
echo $((222))> /proc/sys/kernel/threads-max
then at that point it can grow enough that, if we limit the memory, it will be limited by that before it takes down the host. For testing, was using docker 1.5, launching a container with:
docker run -t -i --rm --memory=200M --memory-swap=400M ubuntu:14.04 /bin/bash
and then triggering a forkbomb in it. Inside, the container becomes unusable immediately. Outside, on the host, it doesn't die (if we don't expand the pid/thread space, host does die here), but we're very slowly leaking memory. Over several hours, it grew to using >2G of memory (virtual), and was still growing. Not sure if there's some other setting I need to look into, for now, continuing to try to lock it down so it can't do this.
Any other suggestions welcome.
ernetas commentedon Feb 27, 2015
@nfritze you could try working with ulimit to set reasonable limits for processes running within Docker. However, because you may be running multiple processes and some may need more, others may need less resources, and even worse, you cannot increase some of the limits within Docker after they have been decreased, this may or may not work for you. For me, what worked best was leaving everything as is, except for setting strict ulimit settings for processes that I was actually expecting to cause issues.
But this only works if you can set VERY strict ulimit settings. Otherwise, in my testing, both the host and the container were suffering from severe performance degradation (making it very difficult to strace/gdb (in the beginning, I didn't even knew which part of my code exactly was causing the fork bomb) or debug in any other way what's exactly happening) and sometimes even crashing, if the limits were too high.
TL;DR: ulimit... that's the best you could do, from what I've tried.
cpuguy83 commentedon Feb 27, 2015
#9437 will allow you to set default ulimits for all containers as well as set custom ulimits per container.
nkfritze commentedon Feb 27, 2015
That sounds very promising, thank you! Investigating.
thaJeztah commentedon Mar 4, 2015
@cpuguy83 will this be solved now that #9437 has been merged?
cpuguy83 commentedon Mar 4, 2015
@yoshiokatsuneo does the new ulimit options from #9437 resolve this issue for you?
Setting ulimits should prevent fork bomb, but this is specifically asking for allowing to set kmem.limit_in_bytes
frol commentedon Apr 23, 2015
@cpuguy83 ulimit sets limits for UID/GID, not per container. This option worked for me fine until I went from one container to dozens.
cpuguy83 commentedon Apr 23, 2015
There must be something in what we're doing with libcontainer to set this... my understanding is that ulimit is per process.
cpuguy83 commentedon Apr 23, 2015
:( that's troublesome.
frol commentedon Apr 23, 2015
@cpuguy83 It is. And kernel devs suggested to use
memory.kmem.limit_in_bytesto prevent things like fork-bombs, but it doesn't seem to me like a very good approach.There were 2 tries to implement nproc in cgroups, but both were rejected, but today I discovered the new version of this: https://lkml.org/lkml/2015/2/22/204
Relevant links:
19 remaining items
frol commentedon Aug 22, 2015
@cpuguy83 While
--kernel-memoryprovides at least usable solution (in contrast toulimit nproc), it is still only a half of the story. We cannot do anything with it just yet, but cgroup PIDs subsystem (CGROUP_PIDS kernel setting) is going to get merged to Linux Kernel 4.3. In my opinion, only when PIDs subsystem is there and Docker support for it is added, only then we can claim the fork-bomb prevention is covered.cpuguy83 commentedon Aug 24, 2015
@frol fair enough, thanks!
haches commentedon Sep 3, 2015
@frol @cpuguy83 Could you elaborate on how
--kernel-memorycan be used to prevent fork bombs? I couldn't find any good information on this. Will this be included in the next release? Thanks a lot.frol commentedon Sep 3, 2015
@haches I set kernel memory limit to 1MB (I haven't used
--kernel-memoryoption yet since I have worked around this issue withcgmemtime). It might not work nice when your containered app uses kernel memory not only for processes, but also, for example, for kernel-level cryptography (I'm not very familiar with the topic of kernel memory, though).LK4D4 commentedon Nov 13, 2015
We merged PID cgroup support to libcontainer, so I think now we can implement it's support in Docker as well.
funkytaco commentedon Apr 4, 2016
nproc cgroup is supported in Docker 1.10, right? So this should be solved?
cpuguy83 commentedon Apr 4, 2016
Yep, thanks.
yoshiokatsuneo commentedon Apr 4, 2016
@funkytaco @cpuguy83 Actually, not yet on Docker1.10, but it will come on 1.11.
https://blog.docker.com/2016/02/docker-engine-1-10-security/
cpuguy83 commentedon Apr 4, 2016
@yoshiokatsuneo True, but it's already merged.
jamespic commentedon Dec 16, 2016
@frol It's an understatement to say that it doesn't work nice with kernel-level cryptography. I tried running a fork bomb in docker with
--kernel-memory=4Mon a system with ecryptfs. When I ran it, it successfully defused, but my kernel remounted my root fs read-only, for reasons that weren't apparent at the time. When I rebooted, it dropped me into an initramfs shell, and wouldn't boot until Ifscked the borked fs.frol commentedon Dec 16, 2016
@jamespic I think that broken host OS qualifies for "doesn't work nicely with kernel-level cryptography" even if the fork bomb is defused.
Osndok commentedon Apr 9, 2017
To those interested in the operation of fork bombs, I would be interested in your comments & criticism of this psuedocode (no actual patch yet):
http://osndok.com/pubfiles/forkbomb-patch.html