public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: forkbombing Linux distributions
@ 2005-03-22 17:09 Natanael Copa
  0 siblings, 0 replies; 51+ messages in thread
From: Natanael Copa @ 2005-03-22 17:09 UTC (permalink / raw)
  To: linux-kernel

Hi list!

(I'm new to this list so I'm sorry this mail has not correct thread id)

I have been following this forkbombing discussions and I would like to
point out a few things:

* When setting limits /etc/limits (or /etc/security/limits.conf) you
will prevent logged in users to fork too many processes. However, this
setting will not prevent a missbehaving daemon that is started from a
bootscript to fork too many processes, even if running as non root.

* Linux is very generous allowing maximum numbers of processes for
non-root users by default in comparation to other *nixes.

The kernel defaults is calculated from the amount of RAM in
kernel/fork.c with in those lines:

        max_threads = mempages / (8 * 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;

The forkbomb is mentioned already in 2001-06-18 by Rik van Riel that
suggested mempages / (16 * THREAD_SIZE / PAGE_SIZE)

http://marc.theaimsgroup.com/?l=linux-kernel&m=99283072806620&w=2
http://marc.theaimsgroup.com/?l=linux-kernel&m=99617386529767&w=2

But I cannot find out why it was set back again to 8 * ... I think this
is the main reason that almost all distros are vulerable to the stupid
fork bomb attack.

Would it be an idea to set it back to:

mempages / (16 * THREAD_SIZE / PAGE_SIZE)

and let the sysadmins raise the limit with /proc/sys/kernel/threads-max
if they need more?

--
Natanael Copa



^ permalink raw reply	[flat|nested] 51+ messages in thread
* Re: forkbombing Linux distributions
@ 2005-03-30 17:40 Jacek Łuczak
  2005-03-31 10:00 ` Natanael Copa
  0 siblings, 1 reply; 51+ messages in thread
From: Jacek Łuczak @ 2005-03-30 17:40 UTC (permalink / raw)
  To: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 627 bytes --]

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

[-- Attachment #2: user_threads_limit.patch --]
[-- Type: text/plain, Size: 3009 bytes --]

--- linux-2.6.12-rc1/kernel/fork.c	2005-03-29 00:53:37.000000000 +0200
+++ linux/kernel/fork.c	2005-03-29 00:54:19.000000000 +0200
@@ -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(&current->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:
--- linux-2.6.12-rc1/kernel/sysctl.c	2005-03-29 00:53:38.000000000 +0200
+++ linux/kernel/sysctl.c	2005-03-29 00:54:19.000000000 +0200
@@ -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 }
 };
--- linux-2.6.12-rc1/include/linux/sysctl.h	2005-03-29 00:54:06.000000000 +0200
+++ linux/include/linux/sysctl.h	2005-03-29 00:54:36.000000000 +0200
@@ -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 */
 };
 
 

[-- Attachment #3: difrost.vcf --]
[-- Type: text/x-vcard, Size: 304 bytes --]

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:difrost@pin.if.uz.zgora.pl
title:Linux Registered User # 337142
x-mozilla-html:FALSE
url:http://pin.if.uz.zgora.pl/~difrost
version:2.1
end:vcard


^ permalink raw reply	[flat|nested] 51+ messages in thread
* Re: forkbombing Linux distributions
@ 2005-03-28 17:28 Matthieu Castet
  2005-03-28 17:56 ` folkert
                   ` (2 more replies)
  0 siblings, 3 replies; 51+ messages in thread
From: Matthieu Castet @ 2005-03-28 17:28 UTC (permalink / raw)
  To: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 514 bytes --]

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

yes, if you want to run application like openoffice.org you need at
least 200Mo. If you want that your system is usable, you need at least 40 process per user. So 40*200 = 8Go, and it don't think you have all this memory...

I think per user limit could be a solution.

attached a small fork-memory bombing.

Matthieu

[-- Attachment #2: kha.c --]
[-- Type: text/x-csrc, Size: 64 bytes --]

int main()
{
	while(1){
		while(fork()){
			malloc(1);
		}
	}
}

^ permalink raw reply	[flat|nested] 51+ messages in thread
* forkbombing Linux distributions
@ 2005-03-21  3:06 William Beebe
  2005-03-21  3:22 ` Dave Jones
                   ` (3 more replies)
  0 siblings, 4 replies; 51+ messages in thread
From: William Beebe @ 2005-03-21  3:06 UTC (permalink / raw)
  To: linux-kernel

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

^ permalink raw reply	[flat|nested] 51+ messages in thread

end of thread, other threads:[~2005-04-05 10:22 UTC | newest]

Thread overview: 51+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-22 17:09 forkbombing Linux distributions Natanael Copa
  -- strict thread matches above, loose matches on Subject: below --
2005-03-30 17:40 Jacek Łuczak
2005-03-31 10:00 ` Natanael Copa
2005-03-31 17:11   ` Lee Revell
2005-04-05  9:47     ` Natanael Copa
2005-04-05 10:18       ` Jacek Luczak
2005-03-28 17:28 Matthieu Castet
2005-03-28 17:56 ` folkert
2005-03-28 19:33   ` Jan Engelhardt
2005-03-28 19:39     ` folkert
2005-03-28 20:35       ` Renate Meijer
2005-03-29 12:31 ` Natanael Copa
2005-03-30 23:46 ` Felipe Alfaro Solana
2005-03-31  6:55   ` Natanael Copa
2005-03-31  7:09     ` Jacek Łuczak
2005-03-21  3:06 William Beebe
2005-03-21  3:22 ` Dave Jones
2005-03-21  3:26   ` William Beebe
2005-03-21  3:27 ` Peter Chubb
2005-03-21  5:14   ` Grant Coady
2005-03-21  7:41     ` Jan Engelhardt
2005-03-22 11:26 ` Hikaru1
2005-03-22 11:49   ` Jan Engelhardt
     [not found]     ` <20050322124812.GB18256@roll>
2005-03-22 12:50       ` Hikaru1
2005-03-23 10:56         ` aq
2005-03-23 12:37           ` Natanael Copa
2005-03-23 13:04             ` aq
2005-03-23 13:38               ` Jan Engelhardt
2005-03-23 13:54               ` Natanael Copa
2005-03-23 14:20                 ` Måns Rullgård
2005-03-23 14:43                 ` Jan Engelhardt
2005-03-23 15:04                   ` Natanael Copa
2005-03-24  7:07                     ` Jan Engelhardt
2005-03-24 10:05                       ` Natanael Copa
2005-03-23 19:38                   ` Kyle Moffett
2005-03-23 20:26                     ` Natanael Copa
2005-03-23 17:05                 ` aq
2005-03-23 18:05                   ` Paul Jackson
2005-03-23 18:44                     ` aq
2005-03-23 20:15                       ` Natanael Copa
2005-03-23 20:48                   ` Natanael Copa
2005-03-23 13:45             ` Erik Mouw
2005-03-23 14:03               ` Natanael Copa
2005-03-23 13:53     ` Max Kellermann
2005-03-23 14:23       ` Natanael Copa
2005-03-23 14:27         ` Max Kellermann
2005-03-23 14:44           ` Natanael Copa
2005-03-23 14:52             ` Max Kellermann
2005-03-23 15:18               ` Natanael Copa
2005-03-26 10:37 ` Tux
2005-03-28  8:03   ` Natanael Copa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox