Skip to content

Commit 0c2d64f

Browse files
vonschtorvalds
authored andcommitted
rlimit: permit setting RLIMIT_NOFILE to RLIM_INFINITY
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>
1 parent b4236f8 commit 0c2d64f

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎kernel/sys.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,14 +1450,22 @@ asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
14501450
return -EINVAL;
14511451
if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
14521452
return -EFAULT;
1453-
if (new_rlim.rlim_cur > new_rlim.rlim_max)
1454-
return -EINVAL;
14551453
old_rlim = current->signal->rlim + resource;
14561454
if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
14571455
!capable(CAP_SYS_RESOURCE))
14581456
return -EPERM;
1459-
if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
1460-
return -EPERM;
1457+
1458+
if (resource == RLIMIT_NOFILE) {
1459+
if (new_rlim.rlim_max == RLIM_INFINITY)
1460+
new_rlim.rlim_max = sysctl_nr_open;
1461+
if (new_rlim.rlim_cur == RLIM_INFINITY)
1462+
new_rlim.rlim_cur = sysctl_nr_open;
1463+
if (new_rlim.rlim_max > sysctl_nr_open)
1464+
return -EPERM;
1465+
}
1466+
1467+
if (new_rlim.rlim_cur > new_rlim.rlim_max)
1468+
return -EINVAL;
14611469

14621470
retval = security_task_setrlimit(resource, &new_rlim);
14631471
if (retval)

0 commit comments

Comments
 (0)