Limiting the number of user processes under Linux (or how I learned to stop worrying and love the fork bomb)
 http://gentoo-wiki.com/SECURITY_Limit_User_Processes
Some weeks ago there was a controversial discussion at Kriptopolis (a Spanish site mainly dedicated to computer security) about a supposed Denial of Service (DoS) vulnerability present in many Linux distributions and some BSDs. In the end, the vulnerability was a mere shell-based fork bomb that a local user would be able to trigger in most desktop Linux distributions, because it’s not a common practice to limit the number of user processes. This is the cryptic piece of code that may probably lock your system after some seconds:
:(){ :|:& };:
Code explanation
Its usage of special characters may make it difficult to understand for some people, and impossible to understand for those unfamiliarized with Bourne shell scripts. A shell function can be defined in two ways: either function function_name { code ; } or function_name () { code ; }. The code above uses the second form to define a function named : (a colon). The body of the function runs the function twice recursively in a pipe which is sent to the background and, after the function is defined, it is called by invoking its name as a command. If we call this function spawn_two we could write it this way:
spawn_two() { spawn_two | spawn_two & }; spawn_two
Why is this called a fork bomb? In POSIX, the system call fork is used to create new processes in a system. A fork bomb is a program of some kind that starts creating processes rapidly, and all of them remain in the system (that is, they don’t finish immediately). If there is no established system limit in the number of processes a user may run, this process creation routine will eventually take all the system resources and lock the machine for a long time, usually forcing a hard reboot when it becomes unresponsive.
This special piece of code is very nasty, and its composition has been calculated precisely. In particular, you’ll notice that the function calls itself twice, using a pipe, and sends the pipe to the background. Each of these steps has a purpose. If it simply called itself, the shell process would automatically start eating all available CPU time, while the amount of memory used by the shell would start increasing, but you could kill this routine at any time pressing Ctrl+C and it wouldn’t create any new process. If you add the ampersand at the end, you’ll trigger the creation of a subshell to run the function, achieving a fork. But the parent function call would finish immediately after creating this subshell (the subshell would be sent to the background and the function would then finish). New processes would be created continuously, but processes would finish continuously too, and the process count in the system would barely increase. If you instead called the function twice, using the pipe, without sending it to the background, you’d create a fork bomb:
:(){ :|:; };:
Using & instead of a semicolon inside the function body serves the purpose of making it nasty, because the subshells are created as background processes while the control returns to the original shell. You can’t cancel the process creation routine with Ctrl+C, and if you exit the shell you used to launch the routine, the process creation will still continue. It’s almost impossible to stop it.
Fork bombs are sometimes created by mistake, specially when you are learning the use of fork during a programming course. These fork bombs have the collaretal effect of triggering a Doh! exclamation that can be heard from miles away. The exact distance is proportional to the boot time and the number of users in the system. Fortunately, there are ways to limit the number of user processes in a system. These limits can protect you mainly from your own mistakes. If a remote attacker is able to trigger a fork bomb in your system, you probably have a more serious problem than simply the lack of this limit.
System calls involving resource limits
In POSIX systems, programs can use setrlimit() to set resource limits and getrlimit() to get them. There are two limits, the soft limit and the hard limit. Only privileged processes may surpass the soft limit and go up to the hard limit, so in the usual case both limits have the same value or the soft limit is the only one that matters. Use man setrlimit to get the gory details. Resource limits are preserved via fork and exec, so the key to limit the whole system is to establish them from a process that is as close to the process tree root as possible. While we are interested in setting the maximum number of processes per user, there are more types of resource limits, including the size of core dumps, the number of open files, the number of pending signals and many more.
System commands and facilities to set limits
There are at least three common ways of establishing resource limits, depending on your system and how strict you want to get regarding who will have limits and what will those limits be. The Gentoo wiki has an entry on limiting the number of user processes which mentions two of those ways.
The configuration file /etc/security/limits.conf is read by PAM. Its syntax is very flexible and allows setting general limits as well as specific limits for users and groups. Any application and login system using PAM will benefit from this central configuration point. Unfortunately (in this case), Slackware does not ship PAM and I can’t report on how effective this configuration point is, and if its settings are used when logging in from virtual terminals as well as graphical login managers. It probably works on both and it’s the mechanism you should try to use if your system features PAM.
The shadow package (the one that provides login, su, chsh, passwd, useradd, etc) uses the file /etc/limits. Its syntax differs from the previous configuration file and it’s not as flexible or powerful, but it should be more than enough for basic usage. This file is used, in my system, by login when you log in using a virtual terminal, because login is invoked by agetty, but it doesn’t seem to be used by my graphical login manager, which is KDM. For this reason, my X11 session wouldn’t be limited if I relied on /etc/limits.
The third and most flexible way of setting resource limits is via the shell built-in ulimit command, if it exists. Bash, for example, has this command. It’s a built-in command and not an external program for obvious reasons. Just like the cd command is a shell built-in because it needs to run the chdir system call inside the shell process (running it from a child process wouldn’t make sense), ulimit will always be a built-in command if it exists, so it sets the limits for the current shell and all its subprocesses. Most shells read /etc/profile when they are started normally, so you can call ulimit from it or from any file “sourced” by it. Under Bash, use help ulimit to get a brief description of the command. Being able to call ulimit from the shell is also flexible, while inconvenient, in the sense that you can trigger the call depending on many conditions. You can selectively run ulimit depending on the username or group. It’s as flexible as a shell script is.
Example: In my Slackware system I considered this was the best way to set a limit in the number of processes, so I created a file called /etc/profile.d/_ulimit.sh and run ulimit -u 256 from it. It works in both virtual terminals and X11 sessions, setting a limit of 256 processes per user.
Note that when you manage a multiuser system you need to make sure that your limits are enforced whatever the login mechanism and shell are. You may also want to restrict the shell your users may establish via chsh by restricting the contents of /etc/shells to shells in which you know your mechanism works. In multiuser systems you should take this seriously because a fork bomb (by mistake or not) can potentially harm many users. In the same way multiuser systems usually enforce disk quotas, other resource limits should also be in place.
Appropriate values
There is no universal value that will fit every situation. Some people probably won’t want to establish a limit. Many Linux and BSD distributions don’t have any limit set because they’re oriented to desktop usage (a handful of users, one at a time) and may not want to establish a limit in the number of processes they may run, in the same way that they don’t set any disk quotas by default. But, if you want to protect the system from your own mistakes, you should try to use a number high enough for your typical needs but not very high. In the Kriptopolis discussion people mentioned their systems crashing with the limit set to 1024 or 512 processes, but I don’t trust those comments, unless they’re testing on a very old machine. Mine had absolutely no problem with 1024 or 512 processes, but I set the limit, as you saw, to 256. Under normal usage, check the number of processes you have running on your machine. Right now I checked and I have 32 processes. Hence, 256 is a pretty conservative while safe number. The syntax of ps is awfully platform specific, but ps --no-headers -U $(whoami) | wc -l gives me that number in my system.
Threads
At least in Linux 2.6 systems with NPTL, the limit does not really apply to the number of processes, but to the number of threads. See the code for my pthread_bomb.c:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *create_and_join(void *unused)
{
pthread_t self = pthread_self();
pthread_t subthread;
if (pthread_create(&subthread, NULL, create_and_join, NULL) != 0) {
printf("Thread %lu: thread creation failedn", self);
return NULL;
}
printf("Thread %lu: created thread %lun", self, subthread);
pthread_join(subthread, NULL);
return NULL;
}
int main()
{
create_and_join(NULL);
return 0;
}
It can be compiled with something like gcc -pthread -Wall -O2 -o pthread_bomb pthread_bomb.c but remember that, due to the multithreaded nature of the program, the message about the thread creation failure may not appear in the last line.
Observation
You may have noticed how some shells, specially bash, implement a number of typical commands as shell built-ins, despite the fact that they exist as independent programs in your system. This goes agains the old Unix philosopy “one program for one task”. Sometimes the shell built-ins help it being more efficient but sometimes they’re created for security reasons. If you’re enforcing a limit in the number of processes but reach that limit by accident, the shell built-in command kill can help you send signals. If the shell relied on the external kill command, it would need to create a new process to run it, and that may not be possible.


June 16th, 2011 13:55
Related Websites…
[…]here are some other links to sites that we find everyday so here are some popular sites we like today[…]…
August 3rd, 2011 09:28
Connected Websites…
[…]a few various other resources on the internet which are worth looking at about this subject consist of[…]…
September 6th, 2011 10:15
My personal related sites…
[…]several new websites on the internet we love, even though they’re not associated with mine. Check all of them out[…]…
October 7th, 2011 12:44
Cheats for Black Ops…
[…]the very best cod secrets-and-cheats, tutorials, techniques and unlock glitches[…]…
October 11th, 2011 17:08
My personal related sites…
[…]a small number of new websites on the web we like, even when they aren’t related to mine. Check them out[…]…
March 11th, 2012 03:48
Gabste9…
Fantastic blog post, saw on…
March 16th, 2012 11:02
Information about elektroninen tupakka…
I saw this really great post today….
March 16th, 2012 21:48
This is my Excerpt…
This web site is simply great.Thanks for sharing this very good web site. Very inspiring! (as always, btw)…
March 18th, 2012 14:26
Facebook Likes…
Free Facebook Likes for your site…
March 20th, 2012 13:00
{Enjoyed} {reading | reading through | looking at | examining | looking through | studying} this, {very} good stuff, {thanks | thankyou | regards | appreciate it}. {”I will do my best. That is all I can do. I ask for your help-and God’s.” by Lyndo…
I dugg some of you post as I cogitated they were very beneficial extremely helpful…
March 20th, 2012 19:02
Learn Piano Online…
[…] I found this to be an excellent post, and am linking to your blog from my piano learning online website. All the best. […]…
March 20th, 2012 21:54
[…]The information mentioned in the article are some of the best available […]……
[…]The information mentioned in the article are some of the greatest available […]……
March 21st, 2012 08:57
Hi…
I was reading through some of your posts on this internet site and I conceive this website is rattling informative ! Retain posting ….
March 22nd, 2012 15:12
Google Plus Votes…
Get free Google plus Votes…
March 23rd, 2012 18:33
Websites you should visit…
[…]below you’ll find the link to some sites that we think you should visit[…]……
March 24th, 2012 01:38
Bing results…
While browsing Bing I discovered this page in the search results and I didn’t think it match…
March 26th, 2012 14:49
Learn Piano Online…
[…] Hi there. If you’ve ever wanted to learn how to play the piano, now is the perfect time to learn. Come Check out our site https://www.facebook.com/LearnPianoHere […]…
March 27th, 2012 04:58
Just Looking…
When I was browsing yesterday I saw a great post concerning…
March 27th, 2012 09:30
Learn Piano Online…
[…] Hi there. If you’ve ever wanted to learn how to play the piano, now is the perfect time to learn. Come Check out our site https://www.facebook.com/LearnPianoHere […]…
March 28th, 2012 12:57
canyon country appliance repair…
Its like you read my mind! You seem to know so much approximately this, such as you wrote the e-book in it or something. I believe that you can do with a few percent to force the message house a little bit, but instead of that, this is magnificent blog…
March 29th, 2012 18:20
Very interesting {points|details} you have {mentioned|noted|observed|remarked}, {thanks|thank you|regards|appreciate it} for {posting|putting up}….
I like this website it’s a master piece! Glad I detected this on google….
March 30th, 2012 05:35
Bing results…
While browsing Bing I discovered this page in the search results and I didn’t think it match…
March 31st, 2012 00:35
Yahoo News…
When checking out Yahoo News yesterday I found this…
April 6th, 2012 23:12
Flash Card Machine…
Hello, just wanted to tell you, I liked this article. It was helpful. Keep on posting!…
April 11th, 2012 23:42
Visitor suggestions…
[…] may probably have drained the lack of this points influenced by way of your self on […]…
April 12th, 2012 19:08
Excelent…
I like this web site very much, Its a very nice billet to read and get information. …
April 14th, 2012 18:40
Nice…
Great post. I was checking continuously this blog and I am impressed! Extremely helpful info specially the last part
I care for such info a lot. I was seeking this particular info for a long time. Thank you and good luck….
April 14th, 2012 18:50
You should check this out…
[…] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well […]……
April 14th, 2012 21:01
Bodybuilding Diet Plan For Men…
I like this website very much, Its a real nice spot to read and get info. “The only normal people are the ones you don’t know very well.” by Joe Ancis….
April 15th, 2012 02:17
Google Hot Trends…
Real good information can be found on web blog….
April 15th, 2012 06:03
Nice…
Great post. I was checking constantly this blog and I am impressed! Extremely helpful info specially the last part
I care for such information a lot. I was looking for this particular info for a long time. Thank you and best of luck….
April 15th, 2012 09:56
Your article…
Some truly good info , Sword lily I found this. “Always be ready to speak your mind and a base man will avoid you.” by William Blake….
April 15th, 2012 12:34
Bigtit milf pussy gaping…
http://pornanza.com/video/21849/Bigtit-milf-pussy-gaping…
April 15th, 2012 13:00
Awesome…
It’s perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I want to suggest you few interesting things or suggestions. Perhaps you can write next articles referring to this article. I wish to r…
April 16th, 2012 12:39
Great read…
Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our area library but I think I learned more clear from this post. I’m very glad to see such fantastic info being share…
April 16th, 2012 20:47
Great website…
[…] Let me reveal a number of pertinent information […]…
April 17th, 2012 21:17
Just saying hello…
Wow! This can be one particular of the most beneficial blogs We have ever arrive across on this subject. Actually Fantastic. I’m also an expert in this topic so I can understand your effort….
April 17th, 2012 23:33
Your site…
Thank you for sharing excellent informations. Your web site is so cool. I am impressed by the details that you have on this blog. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my fri…
April 18th, 2012 06:42
Interesting post…
Usually I don’t learn article on blogs, but I would like to say that this write-up very pressured me to check out and do so! Your writing taste has been amazed me. Thank you, very great article….
April 18th, 2012 07:48
Interesting article…
Hi my loved one! I want to say that this article is amazing, nice written and come with almost all vital infos. I’d like to see extra posts like this….
April 18th, 2012 12:10
Chiropractors Toronto…
thank you for all your efforts that you have put in this. Very interesting information. “A good man can be stupid and still be good. But a bad man must have brains.” by Maxim Gorky. …
April 18th, 2012 16:12
Nice post…
Rattling nice pattern and excellent written content , hardly anything else we need : D….
April 18th, 2012 21:18
Writing style…
Hello, you used to write magnificent, but the last several posts have been kinda boring… I miss your great writings. Past several posts are just a bit out of track! come on!…
April 18th, 2012 23:46
You should check this out…
[…] check out the sites we follow, together with this one, as it connotes our choices from the web […]…
April 19th, 2012 06:28
Your site…
Thanks for sharing superb informations. Your website is very cool. I’m impressed by the details that you’ve on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my friend, …
April 19th, 2012 08:15
Great post…
Great write-up, I’m regular visitor of one’s blog, maintain up the nice operate, and It’s going to be a regular visitor for a long time….
April 19th, 2012 08:22
Great post…
Great write-up, I am regular visitor of one’s site, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time….
April 19th, 2012 16:40
Great …
Not so bad. Intriguing things right here …
April 19th, 2012 18:56
Hello …
You will be my role models. Thank you for your article …
April 19th, 2012 20:52
Hello …
Stunning essay, obtained the enjoyment of reading …
April 19th, 2012 21:27
Excelent …
I’ll complain that you have copied materials from one more source …
April 23rd, 2012 00:19
Just reading…
Hey very cool web site!! Man .. Excellent .. Wonderful .. I’ll bookmark your web site and take the feeds also…I’m happy to search out so many helpful info here in the submit, we’d like work out extra strategies on this regard, thanks for sharing….
April 23rd, 2012 05:04
Great post…
Good write-up, I’m normal visitor of one’s blog, maintain up the excellent operate, and It’s going to be a regular visitor for a long time….
April 23rd, 2012 05:41
Related Sites……
[…] Sites of interest we have a link to […]…
April 23rd, 2012 06:18
The article…
I’ve been surfing on-line more than 3 hours today, but I never found any fascinating article like yours. It’s pretty value enough for me. In my opinion, if all website owners and bloggers made good content as you probably did, the web will probably b…
April 23rd, 2012 08:01
It is great…
hi!,I love your writing so a lot! share we communicate extra approximately your article on AOL? I need a specialist in this area to unravel my problem. Maybe that’s you! Having a look ahead to see you….
April 23rd, 2012 09:31
bonsai…
I am constantly thought about this, appreciate it for posting ….
April 24th, 2012 00:18
Bodybuilding Diet Plan…
I visited a lot of website but I think this one has something extra in it. “I get a standing ovation jaust standing.” by George Burns….
April 24th, 2012 12:27
Outstanding post!…
Great advice, will take on board!…
April 24th, 2012 12:54
Outstanding post!…
Great advice, will take on board!…
April 24th, 2012 17:44
Fine article…
What i don’t understood is in reality how you’re now not actually much more well-preferred than you might be now. You’re very intelligent. You know thus significantly in the case of this matter, produced me for my part consider it from so many numer…
April 24th, 2012 18:25
Sources……
[…] yet deliberating with an actual distinct fashion that you persuaded who […]…
April 25th, 2012 04:08
obrazy nowoczesne …
I intended to create you that little bit of note to finally say thanks yet again on your stunning pointers you have shared here. It is certainly shockingly open-handed with people like you to grant unreservedly all that a number of people might have su…
April 25th, 2012 04:38
The article…
Excellent read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch! “Remember It is 10 times harder to command the ear th…
April 25th, 2012 06:35
{Baby Changing Table|Commercial Baby Changing Table|White Baby Changing Table|Baby Furniture Changing Table|Baby Changing Table Dresser|Baby Changing Table Plans|Baby Changing Table Pad|Baby Dresser Changing Table|Baby Doll Changing Table|Wall Mounte…
Baby Dresser Changing Table…
April 25th, 2012 08:51
Ogrodzenia Plastikowe …
My partner and I stumbled over here diverse internet site and thought I need to examine things out. …
April 25th, 2012 16:58
Welcome !!!…
Here are some of the sites we recommend for our visitors: http://alltvhere.com/…
April 26th, 2012 09:46
Recent Blogrolls……
[…] The info spoke of inside the post are some of the most rewarding in existence […]…
April 26th, 2012 10:15
Cebulki KwiatĂłw …
This is the worst write-up of all, I’ve study …
April 26th, 2012 12:31
sztachety …
There are actually quite a lot of details like that to take into consideration. That could be a great point to bring up. I offer the thoughts above as general inspiration however clearly there are questions just like the one you carry up the place an i…
April 26th, 2012 12:56
Tire Repair…
thank you for all your efforts that you have put in this. Very interesting info. “An unpopular rule is never long maintained.” by Seneca….
April 27th, 2012 17:50
sztachety plastikowe …
I’ll complain that you have copied material from yet another supply …
April 27th, 2012 22:33
archiwizacja online …
Subscribed to your blog, thanks …
April 29th, 2012 16:21
Want to say…
Very interesting topic, appreciate it for putting up….
April 30th, 2012 17:27
ogrodzenia …
Thanks for what you’ve. This can be the most effective post I’ve read …
April 30th, 2012 19:05
Bonsai…
I can’t figure out how do I subscribe to your blog …
May 1st, 2012 10:43
backup …
I was very happy to find this internet-site.I wished to thanks in your time for this excellent read!! I positively enjoying each little little bit of it and I’ve you bookmarked to take a look at new stuff you blog post. …
May 2nd, 2012 01:47
Read was satisfying, read here……
[…] I’d tried over a number of blogs for fresh ideas and I did […]…
May 4th, 2012 05:53
Hello …
I critically delight in your posts. Thanks …
May 7th, 2012 19:06
ogrodzenia …
This really is the worst write-up of all, I’ve study …
May 9th, 2012 07:54
yes …
There is certainly noticeably a bundle to comprehend this. I presume you have produced precise nice points in capabilities also. …
May 10th, 2012 02:29
thanks for the great post…
http://www.youtube.com/watch?v=T2psHqynkZg…
May 11th, 2012 02:03
Great blog…
Very interesting informations I found on this website. Thanks….
May 11th, 2012 07:23
ogrodzenia …
You might be the worst writer …
May 11th, 2012 07:32
obrazy nowoczesne …
Spot on with this write-up, I actually suppose this website needs rather more consideration. I’ll most likely be once more to learn far more, thanks for that info. …
May 11th, 2012 08:17
It is really worh recommendation…
[…]one of our visitors not too long ago encouraged the following website[…]…
May 11th, 2012 13:10
ogrodzenia plastikowe …
in the event you want, I’ll create you posts. Copywriter searching for perform …
May 11th, 2012 15:22
archiwizacja …
I can`t seriously assist but admire your weblog web-site, your site is adorable and good …
May 11th, 2012 20:27
Read was interesting, stay in touch……
[…] please visit the sites many of us follow, together with this one, as it represents our picks from the web […]…
May 11th, 2012 21:28
Coolest blog entry……
[…] while sites we backlink just below are noticeably not related to ours, we think they’re really worth a go over, and so have a peek […]…
May 11th, 2012 23:17
Cebulki KwiatĂłw …
Reading by way of your good content material, will help me to do so from time to time. …
May 12th, 2012 15:24
yes …
Thank you for what you have. This can be the most effective submit I’ve study …
May 13th, 2012 20:53
Awesome site…
[…] The following are some of the sites that we strongly recommend for our guests […]…
May 15th, 2012 09:36
Interesting post…
Normally I don’t read post on blogs, however I wish to say that this write-up very compelled me to check out and do so! Your writing taste has been surprised me. Thanks, very nice post….
May 16th, 2012 02:20
Tumblr and Digg…
Tumblr and Digg just now linked to this super interesting site…
May 16th, 2012 05:35
Your post…
Very interesting points you have noted , thankyou for putting up. “The surest way to get rid of a bore is to lend money to him.” by Paul Louis Courier….
May 16th, 2012 08:57
archiwizacja …
I can??t genuinely aid but appreciate your blog web site, your site is adorable and nice …
May 16th, 2012 10:33
It is really hard these days to find enough support…
My friend is truly forever but in a rash manner proclaiming that in all honesty that it is difficult to really easily get some good online support, but there is …
May 16th, 2012 17:09
Cebulki Kwiatowe …
I’ll complain which you have copied materials from another source …
May 16th, 2012 17:55
serwis HP PoznaĹ„…
I’m impressed, I must say. Actually not often do I encounter a weblog that’s both educative and entertaining, and let me let you know, you’ve got hit the nail on the head. Your thought is outstanding; the difficulty is one thing that not enough pe…
May 16th, 2012 18:58
sztachety …
Thanks, this is the worst factor I’ve study …
May 16th, 2012 21:24
It is quite hard to find good help…
I am really regularly proclaiming that its hard to find good honest help, but here is …
May 17th, 2012 06:45
Nice Focus……
[…] below you will find the link for some sites that a number of us presume you must check out […]…
May 17th, 2012 10:14
obrazy malowane …
I have several query for you, write to these I do not e-mail …
May 17th, 2012 11:30
backup online…
I will not talk about your competence, the post just disgusting …
May 18th, 2012 02:48
Bing results…
While browsing Bing I discovered this page in the search results and I didn’t think it match…
May 18th, 2012 02:49
Fine article…
What i don’t understood is in reality how you are no longer really much more neatly-liked than you may be now. You are very intelligent. You understand therefore considerably relating to this subject, made me for my part consider it from a lot of vari…