Skip to content

Commit

Permalink
rlimit: permit setting RLIMIT_NOFILE to RLIM_INFINITY
Browse files Browse the repository at this point in the history
When a process wants to set the limit of open files to RLIM_INFINITY it
gets EPERM even if it has CAP_SYS_RESOURCE capability.

For example, BIND does:

...
#elif defined(NR_OPEN) && defined(__linux__)
        /*
         * Some Linux kernels don't accept RLIM_INFINIT; the maximum
         * possible value is the NR_OPEN defined in linux/fs.h.
         */
        if (resource == isc_resource_openfiles && rlim_value == RLIM_INFINITY) {
                rl.rlim_cur = rl.rlim_max = NR_OPEN;
                unixresult = setrlimit(unixresource, &rl);
                if (unixresult == 0)
                        return (ISC_R_SUCCESS);
        }
#elif ...

If we allow setting RLIMIT_NOFILE to RLIM_INFINITY we increase portability
- you don't have to check if OS is linux and then use different schema for
limits.

The spec says "Specifying RLIM_INFINITY as any resource limit value on a
successful call to setrlimit() shall inhibit enforcement of that resource
limit." and we're presently not doing that.

Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Adam Tkac authored and Linus Torvalds committed Oct 16, 2008
1 parent b4236f8 commit 0c2d64f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions kernel/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,14 +1450,22 @@ asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
return -EINVAL;
if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
return -EFAULT;
if (new_rlim.rlim_cur > new_rlim.rlim_max)
return -EINVAL;
old_rlim = current->signal->rlim + resource;
if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
!capable(CAP_SYS_RESOURCE))
return -EPERM;
if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
return -EPERM;

if (resource == RLIMIT_NOFILE) {
if (new_rlim.rlim_max == RLIM_INFINITY)
new_rlim.rlim_max = sysctl_nr_open;
if (new_rlim.rlim_cur == RLIM_INFINITY)
new_rlim.rlim_cur = sysctl_nr_open;
if (new_rlim.rlim_max > sysctl_nr_open)
return -EPERM;
}

if (new_rlim.rlim_cur > new_rlim.rlim_max)
return -EINVAL;

retval = security_task_setrlimit(resource, &new_rlim);
if (retval)
Expand Down

0 comments on commit 0c2d64f

Please sign in to comment.