forkbombing Linux distributions
The following quote is from the article "Linux Kernel Security, Again"
(http://www.securityfocus.com/columnists/308):
"Don't get me wrong. Linux doesn't suck. But I do believe that the Linux kernel team (and some of the Linux distributions that are still vulnerable to fork bombing) need to take proactive security a little more seriously. I'm griping for a reason here -- things need to be change."
Sure enough, I created the following script and ran it as a non-root user:
#!/bin/bash
$0 & $0 &
and ran it on Fedora Core 3 with kernel 2.6.11.5 (the box is an Athlon XP 2500+ Barton with 512M on an nForce2 board). The system locked up tighter than a drum. However... After about two minutes the system "unlocked" and responsiveness returned to normal. I can see where this would be an issue on a production system, especially if you could kick off a new fork bomb to continuously lock the system.
Is this really a kernel issue? Or is there a better way in userland to stop this kind of crap?
Regards
Quoted 2 lines · William Beebe
Is this really a kernel issue? Or is there a better way in userland to stop this kind of crap?
man ulimit
Dave
Thanks. That's what I thought. Sorry for the annoyance.
Quoted 10 lines · Dave Jones
Quoted 2 lines · William Beebe
Is this really a kernel issue? Or is there a better way in userland to stop this kind of crap?
man ulimit
Dave
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"William" == William Beebe <[email protected]> writes:
William> Sure enough, I created the following script and ran it as a William> non-root user:
William> #!/bin/bash $0 & $0 &
There are two approaches to fixing this.
1. Rate limit fork(). Unfortunately some legitimate usges do a lot of forking, and you don't really want to slow them down.
2. Limit (per user) the number of processes allowed. This is what's currently done; and if you as administrator want to you can set RLIMIT_NPROC in /etc/security/limits.conf
On an almost-single-user system such as most desktops, there isn't much point in setting this. On shared systems, it can be useful.
Signature · 2 lines
Dr Peter Chubb http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au The technical we do immediately, the political takes *forever*
Quoted 14 lines · Peter Chubb
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"William" == William Beebe <[email protected]> writes:
William> Sure enough, I created the following script and ran it as a William> non-root user:
William> #!/bin/bash $0 & $0 &
There are two approaches to fixing this.
1. Rate limitfork(). Unfortunately some legitimate usges do a lot of forking, and you don't really want to slow them down.
2. Limit (per user) the number of processes allowed. This is what's currently done; and if you as administrator want to you can setRLIMIT_NPROCin/etc/security/limits.conf
Quoted 2 lines
On an almost-single-user system such as most desktops, there isn't much point in setting this. On shared systems, it can be useful.
Had to try it out of curiosity, five ssh logins at the time, but I hit Ctrl-S on the terminal running forkbomb, then other terminals responsive and I could recover, do 'killall forkbomb'.
Even 'top' segfaulted. Machine didn't die though.
slackware-current running 2.4.29-hf5
Just checked logs, messages: --> kernel: VFS: file-max limit 52427 reached nothing in syslog or debug
Cheers,
Grant.
Quoted 6 lines
Quoted 6 lines
William> Sure enough, I created the following script and ran it as a William> non-root user:
William> #!/bin/bash $0 & $0 &
Quoted 7 lines
Quoted 3 lines
There are two approaches to fixing this.
1. Rate limitfork()
2. Limit (per user) the number of processes allowedHad to try it out of curiosity, five ssh logins at the time, but I hit Ctrl-S on the terminal running forkbomb, then other terminals responsive and I could recover, do 'killall forkbomb'.
By the time you killed a handful of procs, the other half spawned new ones.
You can try stopping forkbombs by "killall -STOP nameofprog" and then "killall -9 nameofprog".
But you probably won't get to run killall in case of a thrasher running within the limits of ulimit -m and ulimit -u:
perl -e 'fork,$_="x"x 10E6 while 1'
Jan Engelhardt
Alright, I noticed this article scared a few of my friends, so I decided to figure out on my own a way to prevent fork bombing from completely disabling my machine.
This is only one way to do this, and it's not particularly elegant, but it gets the job done. If you want something more elegant, try using ulimit or /etc/limits instead. Me? This is good enough.
Create, or edit the file /etc/sysctl.conf
In the file, find a line or otherwise create one labelled:
kernel.threads-max = 250
Now make sure at startup something runs
sysctl -p
- on my slackware 10.1 system I had to edit /etc/rc.d/rc.local and add a
line specifically to do this.
Mind that this isn't the best solution. This limits all users, everything to 250 procs, you cannot run more. If your running system or server uses more, adjust the number accordingly.
An example of an attack this stops in its tracks:
:(){ :|:&};:
(as a command to bash)
Attacks this *limits* and enables the user to do something about:
Create a file, and put in it:
#!/bin/bash
$0 & $0 &
chmod +x it, then run it.
This will prevent it from exceeding the procs limits, but it will *not* completely stop it. The only way to kill it off successfully is to killall -9 the script name repeatedly. Note that you'll occasionally be unable to run killall since the forkbomb will be hitting the limit very often.
Like I said, this is not an elegant solution, however it does increase the ability of the person owning the machine to do something about it.
Of course, you should always use a bat on the user if nothing else works. ;)
Hikaru
Quoted 3 lines
This will prevent it from exceeding the procs limits, but it will *not* completely stop it.
What if the few procs that he may spawn also grab so much memory so your machine disappears in swap-t(h)rashing?
Quoted 2 lines
The only way to kill it off successfully is to killall -9 the script name repeatedly.
As said earlier, killall -STOP first
=> keeps the number of processes constant (so he can't spawn any new ones)
Quoted 1 line
Of course, you should always use a bat on the user if nothing else works. ;)
Use a keylogger if you distrust, and after a bombing, look who set us up the bomb.
Jan Engelhardt
A message that is not in the archive (sent off-list or not yet received): 20050322124812.GB18256@roll
Quoted 6 lines · Jan Engelhardt
Quoted 3 lines
This will prevent it from exceeding the procs limits, but it will *not* completely stop it.
What if the few procs that he may spawn also grab so much memory so your machine disappears in swap-t(h)rashing?
While I have figured out how it'd be possible in theory to prevent things from grabbing so much memory that your computer enters swap death, I haven't been able to figure out what reasonable defaults would be for myself or others. Soooo, I suggest everyone who is worried about this check the manpage for 'limits' which tells you how to do this. My machine runs various rediculously large and small programs - I'm not sure a forkbomb could be stopped without hindering the usage of some of the games on my desktop machine.
On a server or something with multiple users however, I'm sure you could configure each user independently with resource limits. Most servers don't have users that play games which take up 90% of the ram. :)
In any case, I was forced by various smarter-than-I people to come up with a better solution to our problem as they were able to make forkbombs that did a much better job of driving me crazy. :)
If you edit or create /etc/limits and set as the only line
* U250
It'll do the same thing as the sysctl hack, except root will still be able to run programs. Programs like ps and kill/killall.
If you've actually implemented the sysctl.conf hack I spoke of previously, I suggest setting it back to whatever it used to be before, or deleting the line from /etc/sysctl.conf altogether.
/etc/limits does a better job at stopping forkbombs.
This is an example of a program in C my friends gave me that forkbombs. My previous sysctl.conf hack can't stop this, but the /etc/limits solution enables the owner of the computer to do something about it as root.
int main() { while(1) { fork(); } }
Hikaru
Quoted 35 lines · On Tue, 22 Mar 2005 07:50:25 -0500, [email protected] <[email protected]>…
Quoted 6 lines · Jan Engelhardt
Quoted 3 lines
This will prevent it from exceeding the procs limits, but it will *not* completely stop it.
What if the few procs that he may spawn also grab so much memory so your machine disappears in swap-t(h)rashing?
While I have figured out how it'd be possible in theory to prevent things from grabbing so much memory that your computer enters swap death, I haven't been able to figure out what reasonable defaults would be for myself or others. Soooo, I suggest everyone who is worried about this check the manpage for 'limits' which tells you how to do this. My machine runs various rediculously large and small programs - I'm not sure a forkbomb could be stopped without hindering the usage of some of the games on my desktop machine.
On a server or something with multiple users however, I'm sure you could configure each user independently with resource limits. Most servers don't have users that play games which take up 90% of the ram. :)
In any case, I was forced by various smarter-than-I people to come up with a better solution to our problem as they were able to make forkbombs that did a much better job of driving me crazy. :)
If you edit or create
/etc/limitsand set as the only line* U250
It'll do the same thing as the sysctl hack, except root will still be able to run programs. Programs like ps and kill/killall.
If you've actually implemented the sysctl.conf hack I spoke of previously, I suggest setting it back to whatever it used to be before, or deleting the line from
/etc/sysctl.confaltogether.
Quoted 8 lines
/etc/limitsdoes a better job at stopping forkbombs.This is an example of a program in C my friends gave me that forkbombs. My previous sysctl.conf hack can't stop this, but the
/etc/limitssolution enables the owner of the computer to do something about it as root.int
main(){ while(1) {fork(); } }
I find that this forkbomb doesnt always kill the machine. Trying a small forkbomb, I saw that either the forkbomb process, or the parent process (of forkbomb) will be killed after a while (by the kernel) because of "out of memory" error. The problem is that which process would be chosen to kill? (I have no idea on how kernel choose the would-be-kill process).
If the kernel choose to kill the parent process, or the forkbomb itself, damage can be afford. Otherwise, if the more important processes are killed (like kernel threads or other daemons), things would be much more serious.
Any idea?
Thank you,
aq
Quoted 11 lines · aq
Quoted 8 lines · On Tue, 22 Mar 2005 07:50:25 -0500, [email protected] <[email protected]>…
While I have figured out how it'd be possible in theory to prevent things from grabbing so much memory that your computer enters swap death, I haven't been able to figure out what reasonable defaults would be for myself or others. Soooo, I suggest everyone who is worried about this check the manpage for 'limits' which tells you how to do this. My machine runs various rediculously large and small programs - I'm not sure a forkbomb could be stopped without hindering the usage of some of the games on my desktop machine.
See patch below.
Quoted 1 line
Quoted 1 line
/etc/limitsdoes a better job at stopping forkbombs.
but does not limit processes that are started from the boot scripts. So if a buggy non-root service is exploited, an attacker would be able to easily shut down the system.
Quoted 5 lines
Quoted 5 lines
This is an example of a program in C my friends gave me that forkbombs. My previous sysctl.conf hack can't stop this, but the
/etc/limitssolution enables the owner of the computer to do something about it as root.int
main(){ while(1) {fork(); } }
I guess that "fork twice and exit" is worse than this?
Quoted 6 lines
I find that this forkbomb doesnt always kill the machine. Trying a small forkbomb, I saw that either the forkbomb process, or the parent process (of forkbomb) will be killed after a while (by the kernel) because of "out of memory" error. The problem is that which process would be chosen to kill? (I have no idea on how kernel choose the would-be-kill process).
It kills the process that reaches the limit (max proc's / out of mem)?
Quoted 6 lines
If the kernel choose to kill the parent process, or the forkbomb itself, damage can be afford. Otherwise, if the more important processes are killed (like kernel threads or other daemons), things would be much more serious.
Any idea?
Limit the default maximum of user processes. If someone needs more, let the sysadmin raise it (with ulimit -u, /etc/limits, sysctl.conf whatever)
This should do the trick:
fork.c 2005-03-21 15:22:50.000000000 +0100 renamed from fork.c.orig 2005-03-02 08:37:48.000000000 +0100 +1 -1
@@ -119,7 +119,7 @@ * value: the thread structures can take up at most half * of memory. */ - max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); + max_threads = mempages / (16 * THREAD_SIZE / PAGE_SIZE); /* * we need to allow at least 20 threads to boot a system
--
Natanael Copa
Quoted 7 lines · Natanael Copa
Quoted 5 lines
Quoted 5 lines
This is an example of a program in C my friends gave me that forkbombs. My previous sysctl.conf hack can't stop this, but the
/etc/limitssolution enables the owner of the computer to do something about it as root.int
main(){ while(1) {fork(); } }I guess that "fork twice and exit" is worse than this?
you meant code like this
int main() { while(1) { fork(); fork(); exit(); } }
is more harmful ? I dont see why (?)
Quoted 8 lines
Quoted 6 lines
I find that this forkbomb doesnt always kill the machine. Trying a small forkbomb, I saw that either the forkbomb process, or the parent process (of forkbomb) will be killed after a while (by the kernel) because of "out of memory" error. The problem is that which process would be chosen to kill? (I have no idea on how kernel choose the would-be-kill process).
It kills the process that reaches the limit (max proc's / out of mem)?
If so, forkbomb doesnt cause much problem like they said, since eventually it would be killed once it reach the limit of memory. the system will recover automatically after awhile.
Quoted 9 lines
Limit the default maximum of user processes. If someone needs more, let the sysadmin raise it (with ulimit -u,
/etc/limits, sysctl.conf whatever)This should do the trick:
fork.c 2005-03-21 15:22:50.000000000 +0100 line 122
Quoted 5 lines
fork.c 2005-03-21 15:22:50.000000000 +0100 renamed from fork.c.orig 2005-03-02 08:37:48.000000000 +0100 +1
-1@@ -119,7 +119,7 @@ * value: the thread structures can take up at most half * of memory. */ - max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); + max_threads = mempages / (16 * THREAD_SIZE / PAGE_SIZE);
I dont see any advantages of halving the max_threads like this at all. That doesnt solve the problem. You should focus elsewhere.
thank you,
aq
Quoted 4 lines
If so, forkbomb doesnt cause much problem like they said, since eventually it would be killed once it reach the limit of memory. the system will recover automatically after awhile.
I doubt that! Maybe OOM strikes one process out, but as already said, when that happens, it makes room for further spawns.
Jan Engelhardt
Quoted 7 lines · aq
Quoted 6 lines · Natanael Copa
Quoted 5 lines
Quoted 5 lines
This is an example of a program in C my friends gave me that forkbombs. My previous sysctl.conf hack can't stop this, but the
/etc/limitssolution enables the owner of the computer to do something about it as root.int
main(){ while(1) {fork(); } }
Quoted 7 lines
Quoted 1 line
I guess that "fork twice and exit" is worse than this?
you meant code like this
int
main(){ while(1) {fork();fork();exit(); } }is more harmful ? I dont see why (?)
Because the parent disappears. When things like killall tries to kill the process its already gone but there are 2 new with new pids.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
I find that this forkbomb doesnt always kill the machine. Trying a small forkbomb, I saw that either the forkbomb process, or the parent process (of forkbomb) will be killed after a while (by the kernel) because of "out of memory" error. The problem is that which process would be chosen to kill? (I have no idea on how kernel choose the would-be-kill process).
Quoted 5 lines
Quoted 1 line
It kills the process that reaches the limit (max proc's / out of mem)?
If so, forkbomb doesnt cause much problem like they said, since eventually it would be killed once it reach the limit of memory. the system will recover automatically after awhile.
well, the problem here has that stupid fork bombs like:
:() { :|:& };:
brings down almost all linux distro's while other *nixes survives.
So in theory, you are right, but in real world the system dies. (when I tried here the system was completely dead for at least 30mins wihtout any possiblity to even move the mouse) It would probably recover after hours or days, but who waits so long if the production server does not respond?
Quoted 6 lines
Quoted 6 lines
Limit the default maximum of user processes. If someone needs more, let the sysadmin raise it (with ulimit -u,
/etc/limits, sysctl.conf whatever)This should do the trick:
Quoted 10 lines
fork.c 2005-03-21 15:22:50.000000000 +0100 line 122
Quoted 8 lines
fork.c 2005-03-21 15:22:50.000000000 +0100 renamed from fork.c.orig 2005-03-02 08:37:48.000000000 +0100 +1
-1@@ -119,7 +119,7 @@ * value: the thread structures can take up at most half * of memory. */ - max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); + max_threads = mempages / (16 * THREAD_SIZE / PAGE_SIZE);I dont see any advantages of halving the
max_threadslike this at all.
If you take a look at the lines below this patch you will find:
max_threads = mempages / (16 * THREAD_SIZE / PAGE_SIZE);
/*
* we need to allow at least 20 threads to boot a system
*/
if(max_threads < 20)
max_threads = 20;
init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;Default RLIMIT_NPROC (ulimit -u) is calculated from max_threads so maximum allowed proc's is halved too.
Quoted 1 line
That doesnt solve the problem.
It actually does. Try yourself.
This was actually the default in 2.4.7-acl
http://marc.theaimsgroup.com/?l=linux-kernel&m=99617386529767&w=2
I don't know why it was doubled again afterwards.
IMHO its better to set a lower default and let the sysadmin raise the limit if needed instead of the opposite.
Quoted 1 line
You should focus elsewhere.
Any suggestions what I should focus on if not on default maximum user processes?
BTW... its not first time this is discussed:
http://marc.theaimsgroup.com/?t=105769009100003&r=1&w=2
However, the limits should apply to the daemons started from boot scripts too, not only the logged in users. That is why I'd like to see the default max_threads limit halved.
--
Natanael Copa
Quoted 5 lines · Natanael Copa
well, the problem here has that stupid fork bombs like:
:() { :|:& };:
brings down almost all linux distro's while other *nixes survives.
I have seen a SunFire machine with 4GB RAM running Solaris grind to a complete halt from a fork bomb.
Signature · 2 lines
Måns Rullgård [email protected]
Quoted 2 lines
brings down almost all linux distro's while other *nixes survives.
Let's see if this can be confirmed.
Jan Engelhardt
Quoted 3 lines · Jan Engelhardt
Quoted 1 line
brings down almost all linux distro's while other *nixes survives.
Let's see if this can be confirmed.
open/free/netbsd survives. I guess OSX does too.
Gentoo (non-hardened), Red Hat, Mandrake, FC2 are vulnerable.
Debian stable survives but they set the default proc limit to 256. Looks like Suse also is not vulerable. (I wonder if the daemons started from booscripts are vulnerable though)
Solaris 10 seems to be vulnerable.
http://www.securityfocus.com/columnists/308
I think it would be nice if Linux could be mentioned together with the *bsd's instead of the commercial *nixes next time :)
--
Natananael Copa
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
brings down almost all linux distro's while other *nixes survives.
Let's see if this can be confirmed.
open/free/netbsd survives. I guess OSX does too.
Confirmed. My OpenBSD install copes very well with forkbombs. However, I _was able_ to spawn a lot of shells by default. The essence is that the number of processes/threads within a _session group_ (correct word?) is limited. That way, you can start a ton of "/bin/sh"s from one another, i.e.:
\__ login jengelh
\__ /bin/sh
\__ /bin/sh
\__ /bin/sh...
So I think that if you add a setsid() to your forkbomb,
you could once again be able to bring a system - BSD this time - down. Just a guess at this time, I would need to write a prog first :p
Jan Engelhardt
Quoted 12 lines · Jan Engelhardt
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
brings down almost all linux distro's while other *nixes survives.
Let's see if this can be confirmed.
open/free/netbsd survives. I guess OSX does too.
Confirmed. My OpenBSD install copes very well with forkbombs. However, I
_wasable_to spawn a lot of shells by default. The essence is that the number of processes/threads within a_sessiongroup_(correct word?) is limited. That way, you can start a ton of "/bin/sh"s from one another, i.e.:
Quoted 8 lines
\__ login jengelh
\__ /bin/sh \__ /bin/sh \__ /bin/sh...
So I think that if you add a
setsid()to your forkbomb,
you could once again be able to bring a system - BSD this time - down.
I seriously doubt that. Try raising your maxproc setting (sysctl kern.maxproc?) to something insane and try bombing again.
I tried to bring the box down by raising the limit to something similar linux default and run the classic ":() { :|:& };:" However, the bomb was stopped by maximum number of pipes and BSD survived.
If you don't hit the maximum number of processes you will hit another limit.
--
Natanael Copa
Quoted 3 lines · Jan Engelhardt
Quoted 1 line
brings down almost all linux distro's while other *nixes survives.
Let's see if this can be confirmed.
Here at my school we have the workstations running Debian testing. We have edited /etc/security/limits.conf to have a much more restrictive startup environment for user processes, limiting to 100 processes per user and clamping maximum CPU time to 4 hours per process. It's not failsafe, but we also have all of the kernel threads set at realtime levels, with the IRQ threads specifically set at SCHED_RR 99, and we have a sulogin-type process on tty12 at SCHED_RR 99.
Even in the event of the worst kind of forkbomb, the terminal is as responsive as if nothing else were running and allows us to kill the offending processes easily, because when the scheduler refuses to interrupt the killall process to run anything else, no other forkbomb processes get started.
I suppose a similar situation could be set up with a user-accessible server and a rate-limited SSH daemon if necessary, although a ttyS0 console via a console server might work better. In any case, I think that while there could perhaps be a better interface for user-limits in the kernel, the existing one works fine for most purposes, when combined with appropriate administrative tools.
Cheers,
Kyle Moffett
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCM/CS/IT/U d- s++: a18 C++++>$ UB/L/X/*++++(+)>$ P+++(++++)>$ L++++(+++) E W++(+) N+++(++) o? K? w--- O? M++ V? PS+() PE+(-) Y+ PGP+++ t+(+++) 5 X R? tv-(--) b++++(++) DI+ D+ G e->++++$ h!*()>++$ r !y?(-) ------END GEEK CODE BLOCK------
Quoted 9 lines · Kyle Moffett
Quoted 3 lines · Jan Engelhardt
Quoted 1 line
brings down almost all linux distro's while other *nixes survives.
Let's see if this can be confirmed.
Here at my school we have the workstations running Debian testing. We have edited
/etc/security/limits.confto have a much more restrictive startup environment for user processes, limiting to 100 processes per user and clamping maximum CPU time to 4 hours per process.
Thats great. I was was thinking of the default settings. (its even possible to lock down a windows machine to be "secure")
Also the daemons started from bootscripts that is not aware of PAM is not affected by those settings. So an exploited security flaw in a service would allow an attacker to bring the system down even if the service is running as non-root.
Try running this from a boot script and you'll see that even if this process is setuid, it will be able to fork more than 100 processes per user:
/* this program should be started as root but it changes uid */
#define TTL 300
#define MAX 65536
#define UID 65534
int pids[MAX];
int main(int argc, char *argv[]) {
int count = 0; pid_t pid;
if (setuid(UID) < 0) {
perror("setuid");
exit(1);
}
while ((pid = fork()) >= 0 && count < MAX) {
if (pid == 0) sleep(TTL);
pids[count++] = pid;
}
printf("Forked %i new processes\n", count);
while (count--) kill(pids[count], SIGTERM);
return 0;
}Quoted 4 lines
In any case, I think
that while there could perhaps be a better interface for user-limits in the kernel, the existing one works fine for most purposes, when combined with appropriate administrative tools.
My point is, the default max allowed processes per user is too high. It better to open up a restrictive default than locking down an generous default.
--
Natanael Copa
Quoted 10 lines · Natanael Copa
Quoted 9 lines · aq
Quoted 7 lines · Natanael Copa
Quoted 5 lines
Quoted 5 lines
This is an example of a program in C my friends gave me that forkbombs. My previous sysctl.conf hack can't stop this, but the
/etc/limitssolution enables the owner of the computer to do something about it as root.int
main(){ while(1) {fork(); } }I guess that "fork twice and exit" is worse than this?
Quoted 9 lines
Quoted 5 lines
you meant code like this
int
main(){ while(1) {fork();fork();exit(); } }is more harmful ? I dont see why (?)
Because the parent disappears. When things like killall tries to kill the process its already gone but there are 2 new with new pids.
are you sure? the above forkbomb will stop quickly after just several spawns because of exit().
I agree that make kernel more restrictive by default is a good approach.
thank you,
aq
Quoted 3 lines
int
main(){ while(1) {fork();fork();exit(); } }
...
the above forkbomb will stop quickly
Yep.
Try this forkbomb:
int main() { while(1) { if (!fork()) continue; if (!fork()) continue; exit(); } }
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373, 1.925.600.0401Quoted 10 lines · Paul Jackson
Quoted 3 lines
int
main(){ while(1) {fork();fork();exit(); } }
...
the above forkbomb will stop quicklyYep.
Try this forkbomb:
int
main(){ while(1) { if (!fork()) continue; if (!fork()) continue;exit(); } }
yep, that is better. but system can still be recovered by killall.
a little "sleep" will render the system completely useless, like this:
int main() { while(1) { if (!fork()) continue; if (!fork()) continue; sleep(5); exit(0); } }
thank you,
aq
Quoted 12 lines · aq
Quoted 10 lines · Paul Jackson
Quoted 3 lines
int
main(){ while(1) {fork();fork();exit(); } }
...
the above forkbomb will stop quicklyYep.
Try this forkbomb:
int
main(){ while(1) { if (!fork()) continue; if (!fork()) continue;exit(); } }
Quoted 6 lines
yep, that is better. but system can still be recovered by killall.
a little "sleep" will render the system completely useless, like this:
int
main(){ while(1) { if (!fork()) continue; if (!fork()) continue; sleep(5); exit(0); } }
Interesting.
With the patch I suggested earlier, reducing default max_threads to the half in kernel/fork.c, my system survived. (without
touching /etc/security/limits.conf) Mail notification died because it couldn't start any new threads but that was the only thing that happened.
--
Natanael Copa
Quoted 1 line · aq
I agree that make kernel more restrictive by default is a good approach.
Thank you! For a moment I thought I was the only human on this planet who thought that.
Next question is where and how and what is an appropiate limit? I have not heard any better suggestions than this:
fork.c 2005-03-21 15:22:50.000000000 +0100 renamed from fork.c.orig 2005-03-02 08:37:48.000000000 +0100 +1 -1
@@ -119,7 +119,7 @@ * value: the thread structures can take up at most half * of memory. */ - max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); + max_threads = mempages / (16 * THREAD_SIZE / PAGE_SIZE); /* * we need to allow at least 20 threads to boot a system
(FYI: A few lines below the default RLIMIT_NPROC is calculated from max_threads/2)
This would give default maximum number of processes from the amount of low memory:
RAM RLIMIT_NPROC 64MiB 256 128MiB 512 256MiB 1024 512MiB 2048 1GiB 4096
That would be sufficent for the users to play their games, compile ther stuff etc while it would protect everyone from that classic shell fork bomb by default.
Actually, Alan Cox tried this in the 2.4.7-ac1 kernel
http://marc.theaimsgroup.com/?l=linux-kernel&m=99617009115570&w=2
but I have no idea why it was raised to the double afterwards.
--
Natanael Copa
Quoted 6 lines · Natanael Copa
Quoted 1 line · aq
Quoted 1 line
/etc/limitsdoes a better job at stopping forkbombs.but does not limit processes that are started from the boot scripts. So if a buggy non-root service is exploited, an attacker would be able to easily shut down the system.
That's easy to fix: set limits from initrd or initramfs.
Erik
Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 -- | Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
Quoted 9 lines · Erik Mouw
Quoted 6 lines · Natanael Copa
Quoted 1 line · aq
Quoted 1 line
/etc/limitsdoes a better job at stopping forkbombs.but does not limit processes that are started from the boot scripts. So if a buggy non-root service is exploited, an attacker would be able to easily shut down the system.
That's easy to fix: set limits from initrd or initramfs.
..or run "ulimit -u" early in the boot scripts.
What I suggest is doing the reverse. Let the kernel be restrictive by default and let distro's or sysadmins open up if they need more processes.
--
Natanael Copa
Quoted 2 lines · Jan Engelhardt
What if the few procs that he may spawn also grab so much memory so your machine disappears in swap-t(h)rashing?
The number of processes is counted per user, but CPU time and memory consumption is counted per process.
Going around RLIMIT_CPU is too easy by simply forkbombing. This renders RLIMIT_CPU unusable.
The memory limits aren't good enough either: if you set them low enough that memory-forkbombs are unperilous for RLIMIT_NPROC*RLIMIT_DATA, it's probably too low for serious applications.
Now what about per-user (or per-session) CPU and memory limits?
Another idea: RLIMIT_FORK (number of allowed fork() calls in that session). While that may not be useful for interactive login sessions, I can imagine several situations where it could help (like qmail child processes).
Max
Quoted 6 lines · Max Kellermann
Quoted 2 lines · Jan Engelhardt
What if the few procs that he may spawn also grab so much memory so your machine disappears in swap-t(h)rashing?
The number of processes is counted per user, but CPU time and memory consumption is counted per process.
So limiting maximum number of processes will automatically limit CPU time and memory consumption per user?
--
Natanael Copa
Quoted 6 lines · Natanael Copa
Quoted 2 lines · Max Kellermann
The number of processes is counted per user, but CPU time and memory consumption is counted per process.
So limiting maximum number of processes will automatically limit CPU time and memory consumption per user?
No. I was talking about RLIMIT_CPU and RLIMIT_DATA, compared to RLIMIT_NPROC. RLIMIT_NPROC limits the number of processes for that user, nothing else (slightly simplified explanation).
Max
Quoted 11 lines · Max Kellermann
Quoted 6 lines · Natanael Copa
Quoted 2 lines · Max Kellermann
The number of processes is counted per user, but CPU time and memory consumption is counted per process.
So limiting maximum number of processes will automatically limit CPU time and memory consumption per user?
No. I was talking about
RLIMIT_CPUandRLIMIT_DATA, compared toRLIMIT_NPROC.RLIMIT_NPROClimits the number of processes for that user, nothing else (slightly simplified explanation).
Yes, but ifRLIMIT_NPROC is per user and RLIMIT_CPU is per proc
the theoretical CPU limit per user is RLIMIT_NPROC * RLIMIT_CPU. So if you half the RLIMIT_NPROC you will half the theoretical maximum CPU limit per user.
Same with memory.
I don't know if that really solves anything, but a misbehaving process (fork bomb) would need to consume the double RAM or CPU to do the same "damage" if RLIMIT_NPROC is halved.
--
Natanael Copa
Quoted 8 lines · Natanael Copa
Yes, but if
RLIMIT_NPROCis per user andRLIMIT_CPUis per procthe theoretical CPU limit per user is
RLIMIT_NPROC*RLIMIT_CPU. So if you half theRLIMIT_NPROCyou will half the theoretical maximum CPU limit per user.Same with memory.
It's even worse with RLIMIT_CPU. Imagine a process forks RLIMIT_NPROC-1 child processes. These consume all their CPU time, get killed with SIGXCPU, and the parent process spawns new child processes again with fresh RLIMIT_CPU counters (the parent process idled meanwhile, consuming none of its assigned CPU cycles). Again and again.
You see, RLIMIT_CPU is worthless in its current implementation.
Max
Quoted 1 line · Max Kellermann
You see,
RLIMIT_CPUis worthless in its current implementation.
You are right. Limiting CPU is probably not a good solution anyway.
http://marc.theaimsgroup.com/?l=linux-kernel&m=105808941823955&w=2
--
Natanael Copa
I'm confused, are hard limits to RLIMIT_NPROC imposed on services spawned by init before a user logs in?
Quoted 2 lines · Tux
I'm confused, are hard limits to
RLIMIT_NPROCimposed on services spawned by init before a user logs in?
There are no "hard" limits to RLIMIT_NPROC. However, on fork, childern inherits the parents limits. Non-root users can not raise the limit, just lower it. So unless limits are set in the bootscripts, the defaults set in kernel/fork.c will be used on services.
--
Natanael Copa