Skip to content

Fork bomb prevention #6479

Closed
Closed

Description

@yoshiokatsuneo

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.

On ubuntu 14.04, we need to update kernel package to 3.13.0-27.50 or later to use "memory. kmem.limit_in_bytes" cgroups settings
Ref: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1309586

Thanks.

Activity

  1. gdm85 commented on Jun 23, 2014

    @gdm85
    Contributor

    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)?

  2. fjakobs commented on Aug 16, 2014

    @fjakobs

    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

    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

  3. jessfraz commented on Feb 25, 2015

    @jessfraz
    Contributor

    ping @crosbymichael im going to label as black-belt hope thats cool

  4. added
    kind/enhancement
    on Feb 25, 2015
  5. nkfritze commented on Feb 27, 2015

    @nkfritze

    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 $((2
    22))> /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.

  6. ernetas commented on Feb 27, 2015

    @ernetas

    @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.

  7. cpuguy83 commented on Feb 27, 2015

    @cpuguy83
    Member

    #9437 will allow you to set default ulimits for all containers as well as set custom ulimits per container.

  8. nkfritze commented on Feb 27, 2015

    @nkfritze

    That sounds very promising, thank you! Investigating.

  9. thaJeztah commented on Mar 4, 2015

    @thaJeztah
    Member

    @cpuguy83 will this be solved now that #9437 has been merged?

  10. cpuguy83 commented on Mar 4, 2015

    @cpuguy83
    Member

    @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

  11. frol commented on Apr 23, 2015

    @frol

    @cpuguy83 ulimit sets limits for UID/GID, not per container. This option worked for me fine until I went from one container to dozens.

  12. cpuguy83 commented on Apr 23, 2015

    @cpuguy83
    Member

    There must be something in what we're doing with libcontainer to set this... my understanding is that ulimit is per process.

  13. cpuguy83 commented on Apr 23, 2015

    @cpuguy83
    Member

    The nproc limit is a bit of a special case, in that it does sum over all the processes of a user. Nonetheless, it still applies per-process: when a process calls fork to create a new process, the call is denied if the number of processes belonging to the process's euid is would be larger than the process's RLIMIT_NPROC value.
    http://unix.stackexchange.com/questions/55319/are-limits-conf-values-applied-on-a-per-process-basis

    :( that's troublesome.

  14. frol commented on Apr 23, 2015

    @frol

    @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.

    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:

  15. 19 remaining items

  16. frol commented on Aug 22, 2015

    @frol

    @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.

  17. cpuguy83 commented on Aug 24, 2015

    @cpuguy83
    Member

    @frol fair enough, thanks!

  18. haches commented on Sep 3, 2015

    @haches

    @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.

  19. frol commented on Sep 3, 2015

    @frol

    @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).

  20. LK4D4 commented on Nov 13, 2015

    @LK4D4
    Contributor

    We merged PID cgroup support to libcontainer, so I think now we can implement it's support in Docker as well.

  21. funkytaco commented on Apr 4, 2016

    @funkytaco

    nproc cgroup is supported in Docker 1.10, right? So this should be solved?

  22. cpuguy83 commented on Apr 4, 2016

    @cpuguy83
    Member

    Yep, thanks.

  23. yoshiokatsuneo commented on Apr 4, 2016

    @yoshiokatsuneo
    Author
  24. cpuguy83 commented on Apr 4, 2016

    @cpuguy83
    Member

    @yoshiokatsuneo True, but it's already merged.

  25. jamespic commented on Dec 16, 2016

    @jamespic

    @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.

  26. frol commented on Dec 16, 2016

    @frol

    @jamespic I think that broken host OS qualifies for "doesn't work nicely with kernel-level cryptography" even if the fork bomb is defused.

  27. Osndok commented on Apr 9, 2017

    @Osndok

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    exp/expertkind/enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions