forkbombing Linux distributions
Subsystems (4)
Hi
I made some tests and almost all Linux distros brings down while freebsd survive!Forkbombing is a big problem but i don't think that something like
max_threads = mempages / (16 * THREAD_SIZE / PAGE_SIZE);
is good solution!!!
How about add max_user_threads to the kernel? It could be tunable via proc filesystem. Limit is set only for users.
I made a fast:) patch - see below - and test it on 2.6.11, 2.6.11ac4,2.6.12rc1...works great!!!New forks are stoped in copy_process() before dup_task_struct() and EAGAIN is returned. System works without any problems and root can killall -9 forkbomb.
Regards,
Jacek Luczak
kernel/fork.c +37 -0
@@ -57,6 +57,8 @@int max_threads;/* tunable limit on nr_threads */ +int max_user_threads;/* tunable limit on nr_threads per user */ + DEFINE_PER_CPU(unsigned long, process_counts) = 0;__cacheline_aligned DEFINE_RWLOCK (tasklist_lock );/* outer */
@@ -146,6 +148,21 @@if (max_threads <20 ) max_threads =20 ; +/* +* The default maximum number of threads per user. +* +* FIXME: this value is based on my experiments and is +* rather good on desktop system; it should be fixed to +* the more universal value. +*/ + max_user_threads =300 ; + +/* +* default value is too high - set to max_threads +*/ +if (max_threads < max_user_threads) + max_user_threads = max_threads; + init_task.signal->rlim[RLIMIT_NPROC ].rlim_cur = max_threads/2 ; init_task.signal->rlim[RLIMIT_NPROC ].rlim_max = max_threads/2 ; init_task.signal->rlim[RLIMIT_SIGPENDING] =
@@ -179,6 +196,16 @@return tsk; } +/* +* This is used to get number of user processes +* from current running task. +*/ +static inline int get_user_processes (void ) +{ +return atomic_read (¤t->user->processes); +} +#define user_nr_processes get_user_processes() +#ifdef CONFIG_MMU static inline int dup_mmap (struct mm_struct * mm,struct mm_struct * oldmm) {
@@ -869,6 +896,13 @@ goto fork_out; retval = -ENOMEM ; + +/* +* Stop creation of new user process if limit is reached. +*/ +if ( (current->user != &root_user) && (user_nr_processes >= max_user_threads) ) + goto max_user_fork; + p =dup_task_struct (current);if (!p) goto fork_out;
@@ -1109,6 +1143,9 @@return ERR_PTR (retval);return p; +max_user_fork: + retval = -EAGAIN ; +return ERR_PTR (retval); bad_fork_cleanup_namespace:exit_namespace (p); bad_fork_cleanup_keys:
kernel/sysctl.c +9 -0
@@ -56,6 +56,7 @@extern int sysctl_overcommit_memory;extern int sysctl_overcommit_ratio;extern int max_threads; +extern int max_user_threads;extern int sysrq_enabled;extern int core_uses_pid;extern char core_pattern[];
@@ -642,6 +643,14 @@ .mode = 0644, .proc_handler = &proc_dointvec, }, + { + .ctl_name = KERN_MAX_USER_THREADS, + .procname = "user_threads_max", + .data = &max_user_threads, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, { .ctl_name = 0 } };
include/linux/sysctl.h +1 -0
@@ -136,6 +136,7 @@KERN_UNKNOWN_NMI_PANIC =66 ,/* int: unknown nmi panic flag */ KERN_BOOTLOADER_TYPE =67 ,/* int: boot loader type */ KERN_RANDOMIZE =68 ,/* int: randomize virtual address space */ + KERN_MAX_USER_THREADS=69, /* int: Maximum nr of threads per user in the system */ };
begin:vcard
fn;quoted-printable:Jacek =C5=81uczak n;quoted-printable:=C5=81uczak;Jacek adr:;;Prof. Z. Szafrana 4a;Zielona Gora;;65-516;Poland email;internet:[email protected] title:Linux Registered User # 337142 x-mozilla-html:FALSE url:http://pin.if.uz.zgora.pl/~difrost version:2.1
end:vcard
Quoted 7 lines · Jacek Łuczak
Hi
I made some tests and almost all Linux distros brings down while freebsd survive!Forkbombing is a big problem but i don't think that something like
max_threads= mempages / (16 *THREAD_SIZE/PAGE_SIZE);
Quoted 8 lines
is good solution!!!
How about addmax_user_threadsto the kernel? It could be tunable via proc filesystem. Limit is set only for users.
I made a fast:) patch - see below - and test it on 2.6.11, 2.6.11ac4,2.6.12rc1...works great!!!New forks are stoped incopy_process()beforedup_task_struct()and EAGAIN is returned. System works without any problems and root can killall -9 forkbomb.
I really liked this approach because:
* it is similar to other *nixes. (freebsd, openbsd)
* it is easily tuneable (/proc or systcl)
* it is stupid simple - small chance that things can go wrong.
* this solves *many* things in comparation to possible problems it causes.
Only thing that could be a problem that I come to think of is that you cannot raise the limit through /etc/security/limits.conf or similar. Eg. you migh want all setuid() services/daemons run with a low limit but you want give user Bob more processes. (I don't know if this is a realistic situation though)
The default value could be something like:
max_user_threads = max_threads / 2
or:
max_user_threads = max_threads / 4;
With a lower limit to 20 or something, just like max_threads (in case you try run Linux on 2MiB RAM)
If a fixed value (like 300, 512, 2000) is used then will probably systems with low amount of RAM be vulerable to the forkbomb attack.
--
Natanael Copa
Quoted 6 lines · Natanael Copa
Quoted 3 lines · Jacek Łuczak
I made some tests and almost all Linux distros brings down while freebsd survive!Forkbombing is a big problem but i don't think that something like
I really liked this approach because:
Christ, why is this idiotic thread still going? No one is going to change the kernel, because the problem is trivial to solve in userspace! Didn't you ever look up what a ulimit is?
If you consider your distro's default ulimits unreasonable, file a bug report with them. But no one is going to make Linux "restrictive by default" to make life easier for people who don't bother to RTFM.
Lee
Quoted 1 line · Lee Revell
Didn't you ever look up what a ulimit is?
ofcourse i did. I just think that ulimit (or other userspace tools) should be used to *raise* the limit if you need more. Not the reverse.
Quoted 3 lines
If you consider your distro's default ulimits unreasonable, file a bug report with them. But no one is going to make Linux "restrictive by default" to make life easier for people who don't bother to RTFM.
I already suggested ulimit solutions for my distro. They think that if this is needed the kernel dev's would do something (ie its a kernel problem) while the kernel dev's says this is a userspace prob.
I wouldn't bother if this was a problem for one or two distros only. Now, almost all distros seems to be vulnerable by default.
I wouldn't bother if other *nixes would set this limit in userspace. (the BSD's set the limit lower in kernel and let users who need more raise with userland tools)
I wouldn't bother if this wouldn't give Linux a bad reputation.
I'm Sorry if I made some people upset.
--
Natanael Copa
Natanael Copa napisał(a):
Quoted 26 lines · Lee Revell
Quoted 3 lines
Didn't you ever look up what a ulimit is?
ofcourse i did. I just think that ulimit (or other userspace tools) should be used to *raise* the limit if you need more. Not the reverse.
Quoted 3 lines
If you consider your distro's default ulimits unreasonable, file a bug report with them. But no one is going to make Linux "restrictive by default" to make life easier for people who don't bother to RTFM.
I already suggested ulimit solutions for my distro. They think that if this is needed the kernel dev's would do something (ie its a kernel problem) while the kernel dev's says this is a userspace prob.
I wouldn't bother if this was a problem for one or two distros only. Now, almost all distros seems to be vulnerable by default.
I wouldn't bother if other *nixes would set this limit in userspace. (the BSD's set the limit lower in kernel and let users who need more raise with userland tools)
Quoted 8 lines
I wouldn't bother if this wouldn't give Linux a bad reputation.
I'm Sorry if I made some people upset.
--
Natanael Copa
You have absolutely right!!! Even if 'good' ulimit is set there isn't anything bad in adding ulimit-like mechanism into kernel.
Long live ... hmm... kernel, not ulimit:)
Best regards,
Jacek