aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Snook <csnook@redhat.com>2006-09-29 05:01:33 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2006-09-29 12:18:23 -0400
commit4e6fd33b75602ced4c5d43e99a10a1d13f33d4f4 (patch)
treef22573e7d57532c4393ae26bea08866decad2813
parent8c6ba51bdad0de0dc2a2e2415d4dac2d0db6c1a1 (diff)
[PATCH] enforce RLIMIT_NOFILE in poll()
POSIX states that poll() shall fail with EINVAL if nfds > OPEN_MAX. In this context, POSIX is referring to sysconf(OPEN_MAX), which is the value of current->signal->rlim[RLIMIT_NOFILE].rlim_cur in the linux kernel, not the compile-time constant which happens to also be named OPEN_MAX. In the current code, an application may poll up to max_fdset file descriptors, even if this exceeds RLIMIT_NOFILE. The current code also breaks applications which poll more than max_fdset descriptors, which worked circa 2.4.18 when the check was against NR_OPEN, which is 1024*1024. This patch enforces the limit precisely as POSIX defines, even if RLIMIT_NOFILE has been changed at run time with ulimit -n. To elaborate on the rationale for this, there are three cases: 1) RLIMIT_NOFILE is at the default value of 1024 In this (default) case, the patch changes nothing. Calls with nfds > 1024 fail with EINVAL both before and after the patch, and calls with nfds <= 1024 pass the check both before and after the patch, since 1024 is the initial value of max_fdset. 2) RLIMIT_NOFILE has been raised above the default In this case, poll() becomes more permissive, allowing polling up to RLIMIT_NOFILE file descriptors even if less than 1024 have been opened. The patch won't introduce new errors here. If an application somehow depends on poll() failing when it polls with duplicate or invalid file descriptors, it's already broken, since this is already allowed below 1024, and will also work above 1024 if enough file descriptors have been open at some point to cause max_fdset to have been increased above nfds. 3) RLIMIT_NOFILE has been lowered below the default In this case, the system administrator or the user has gone out of their way to protect the system from inefficient (or malicious) applications wasting kernel memory. The current code allows polling up to 1024 file descriptors even if RLIMIT_NOFILE is much lower, which is not what the user or administrator intended. Well-written applications which only poll valid, unique file descriptors will never notice the difference, because they'll hit the limit on open() first. If an application gets broken because of the patch in this case, then it was already poorly/maliciously designed, and allowing it to work in the past was a violation of POSIX and a DoS risk on low-resource systems. With this patch, poll() will permit exactly what POSIX suggests, no more, no less, and for any run-time value set with ulimit -n, not just 256 or 1024. There are existing apps which which poll a large number of file descriptors, some of which may be invalid, and if those numbers stradle 1024, they currently fail with or without the patch in -mm, though they worked fine under 2.4.18. Signed-off-by: Chris Snook <csnook@redhat.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--fs/select.c8
1 files changed, 1 insertions, 7 deletions
diff --git a/fs/select.c b/fs/select.c
index 33b72ba0f86f..dcbc1112b7ec 100644
--- a/fs/select.c
+++ b/fs/select.c
@@ -658,8 +658,6 @@ int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
658 unsigned int i; 658 unsigned int i;
659 struct poll_list *head; 659 struct poll_list *head;
660 struct poll_list *walk; 660 struct poll_list *walk;
661 struct fdtable *fdt;
662 int max_fdset;
663 /* Allocate small arguments on the stack to save memory and be 661 /* Allocate small arguments on the stack to save memory and be
664 faster - use long to make sure the buffer is aligned properly 662 faster - use long to make sure the buffer is aligned properly
665 on 64 bit archs to avoid unaligned access */ 663 on 64 bit archs to avoid unaligned access */
@@ -667,11 +665,7 @@ int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
667 struct poll_list *stack_pp = NULL; 665 struct poll_list *stack_pp = NULL;
668 666
669 /* Do a sanity check on nfds ... */ 667 /* Do a sanity check on nfds ... */
670 rcu_read_lock(); 668 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
671 fdt = files_fdtable(current->files);
672 max_fdset = fdt->max_fdset;
673 rcu_read_unlock();
674 if (nfds > max_fdset && nfds > OPEN_MAX)
675 return -EINVAL; 669 return -EINVAL;
676 670
677 poll_initwait(&table); 671 poll_initwait(&table);