How to: Prevent a fork bomb by limiting user process

Earlier, I wrote about fork bomb, few readers like to know about getting protection against such attacks:

How do I protect my system from a fork bomb under Linux?

Limiting user processes is important for running a stable system. To limit user process just add user name or group or all users to /etc/security/limits.conf file and impose process limitations.

Understanding /etc/security/limits.conf file

Each line describes a limit for a user in the form:
<domain> <type> <item> <value>
Where:

  • <domain> can be:
    • an user name
    • a group name, with @group syntax
    • the wildcard *, for default entry
    • the wildcard %, can be also used with %group syntax, for maxlogin limit
  • <type> can have the two values:
    • "soft" for enforcing the soft limits
    • "hard" for enforcing hard limits
  • <item> can be one of the following:
    • core - limits the core file size (KB)
  • <value> can be one of the following:
    • core - limits the core file size (KB)
    • data - max data size (KB)
    • fsize - maximum filesize (KB)
    • memlock - max locked-in-memory address space (KB)
    • nofile - max number of open files
    • rss - max resident set size (KB)
    • stack - max stack size (KB)
    • cpu - max CPU time (MIN)
    • nproc - max number of processes
    • as - address space limit
    • maxlogins - max number of logins for this user
    • maxsyslogins - max number of logins on the system
    • priority - the priority to run user process with
    • locks - max number of file locks the user can hold
    • sigpending - max number of pending signals
    • msgqueue - max memory used by POSIX message queues (bytes)
    • nice - max nice priority allowed to raise to
    • rtprio - max realtime priority
    • chroot - change root to directory (Debian-specific)

Login as the root and open configuration file:
# vi /etc/security/limits.conf
Following will prevent a "fork bomb":
vivek hard nproc 300
@student hard nproc 50
@faculty soft nproc 100
@pusers hard nproc 200

Above will prevent anyone in the student group from having more than 50 processes, faculty and pusers group limit is set to 100 and 200. Vivek can create only 300 process. Please note that KDE and Gnome desktop system can launch many process.

Save and close the file. Test your new system by dropping a form bomb:
$ :(){ :|:& };:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 14 comments… read them below or add one }

1 RuBiCK 11.27.07 at 9:55 pm

Could you tell me what it’s the difference between hard and soft limits?

People told me soft is like warning and hard is real max limit, but I’m not sure

2 vivek 11.28.07 at 4:40 am

@RuBiCK,

Yup, you are correct about soft and hard limit. For example, following will prevent anyone in the student group from having more than 50 processes, and a warning will be given at 30 processes.
@student soft nproc 30
@student hard nproc 50

HTH

3 yoooo 11.28.07 at 7:43 am

:) thanks for this mini howto

4 Igor 11.28.07 at 12:50 pm

Could you explain how does that form bomb work?

5 JV 11.28.07 at 12:52 pm

Is there a reason to limit core dump file sizes? I am usually in the process of doing so mainly because I don’t like to set anything to unlimited

6 vivek 11.28.07 at 12:54 pm
7 mastrboy 12.08.07 at 11:50 pm

is there a way to activate these settings on a running system? Currently i have not found any other solution that to reboot to make the settings active :(

(using debian etch)

8 MaoP 12.09.07 at 8:39 am

man ulimit
google linux sysctl limit proccess

9 sandoz 12.17.07 at 2:03 pm

Actually soft limits work like hard limits except, that the user can change them up to the hard limit.

Say:
@student soft nproc 30
@student hard nproc 50

@students can run 30 process. After that starting processes will fail. But an
ulimit -Su 50
will make it possible for them to run 50 processes, in that shell until the next logout.

To make changes work, the user has to logout and login again. All user already logged in are able to work as before.

sandoz

10 ATOzTOA 01.30.08 at 4:19 am

Tried the Fork BOmb… Worked perfectly :)

11 Sergei Vasilyev 08.14.08 at 1:09 pm

I wonder how to limit number of used cpu cores per user or per user process in case when process is multithreaded and server has multiply number of CPU.

12 Joshi 12.03.08 at 6:41 pm

hi Sergei,

i think this can be done via:
apt-get install cpulimit

cheers
joshi

13 Robert Delahunt 01.04.09 at 2:56 pm

I don’t see any info for doing it without PAM, so here’s some info (for us Slackware people, etc, and others not using PAM):

Put this in /etc/profile.conf:

ulimit -u 100

where this is the limit of processes anyone can run. Be warned that it could cause problems if you don’t know how many typical processes you run, so play with ps aux | wc -l and other stuff to check how many you would need. Cheers!

14 Samuel Huckins 06.11.09 at 2:20 am

@Robert Delahunt: While I am on Ubuntu 9.04, your suggestion was the only one that worked. For me setting hard and soft limits for users in /etc/security/limits/conf had no effect. I had to place ulimit -u NUM in /etc/profile for it to stick. Thanks!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , ,

Previous post: Measure network performance: Find bandwidth, jitter and datagram loss with iperf utility

Next post: Linux mobile phone manager – Wammu