Skip to contentSkip to conversation

Allow set RLIMIT_NOFILE to RLIM_INFINITY

PATCH In review Adam Tkac 3 participants 7 messages Sep 9, 2008 – Sep 12, 2008

lore
Share & tools
lore

Adam Tkac

author
#0 lore raw

PATCH 1/1 Allow set RLIMIT_NOFILE to RLIM_INFINITY

Hi all,

when process wants set limit of open files to RLIM_INFINITY it gets EPERM even if it has CAP_SYS_RESOURCE capability. Attached patch should fix the problem. Please add me to CC of your responses because I'm not member of list.

Regards, Adam

Signature · 1 line
Adam Tkac
kernel/sys.c +8 -2
@@ -1458,8 +1458,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	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;
+	}
 
 	retval = security_task_setrlimit(resource, &new_rlim);
 	if (retval)
#1 lore raw
Quoted 18 lines · On Tue, 9 Sep 2008 09:14:07 +0200 Adam Tkac <[email protected]> wrote:

when process wants set limit of open files to RLIM_INFINITY it gets EPERM even if it has CAP_SYS_RESOURCE capability. Attached patch should fix the problem. Please add me to CC of your responses because I'm not member of list.

Regards, Adam

Signature · 4 lines
Adam Tkac


[linux26-openfiles.patch  text/plain (634B)]
kernel/sys.c +0 -0
@@ -1458,8 +1458,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
 	    !capable(CAP_SYS_RESOURCE))
 		return -EPERM;

kernel/sys.c asmlinkage long sys_setrlimit(unsigned i · line 1468

Quoted 10 lines
kernel/sys.c +8 -2
-	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;
+	}

The kernel has had this behaviour for a long time. 2.6.13 had:

	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
	    !capable(CAP_SYS_RESOURCE))
		return -EPERM;
	if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
			return -EPERM;

I don't immediately see a problem with your change, but what makes you believe that it is needed? Is there some standard which we're violating? Is there some operational situation in which the current behaviour is causing a problem?

Thanks.

author
#2 lore raw
Quoted 40 lines · Andrew Morton
Quoted 18 lines · On Tue, 9 Sep 2008 09:14:07 +0200 Adam Tkac <[email protected]> wrote:

when process wants set limit of open files to RLIM_INFINITY it gets EPERM even if it has CAP_SYS_RESOURCE capability. Attached patch should fix the problem. Please add me to CC of your responses because I'm not member of list.

Regards, Adam

Signature · 4 lines
Adam Tkac


[linux26-openfiles.patch  text/plain (634B)]
kernel/sys.c +0 -0
@@ -1458,8 +1458,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
 	    !capable(CAP_SYS_RESOURCE))
 		return -EPERM;

kernel/sys.c asmlinkage long sys_setrlimit(unsigned i · line 1468

Quoted 10 lines
kernel/sys.c +8 -2
-	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;
+	}

The kernel has had this behaviour for a long time. 2.6.13 had:

	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
	    !capable(CAP_SYS_RESOURCE))
		return -EPERM;
	if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
			return -EPERM;
Quoted 6 lines

I don't immediately see a problem with your change, but what makes you believe that it is needed? Is there some standard which we're violating? Is there some operational situation in which the current behaviour is causing a problem?

Thanks.

Well, this change is not _absolutely_ needed because everyone who wants unlimited file descriptors he could set it to NR_OPEN. Look on example (from BIND):

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

I think that when you allow set RLIMIT_NOFILE to RLIM_INFINITY you increase portability - you don't have to check if OS is linux and then use different schema for limits.

Regards, Adam

Signature · 1 line
Adam Tkac
#3 lore raw
Quoted 64 lines · On Thu, 11 Sep 2008 09:54:38 +0200 Adam Tkac <[email protected]> wrote:
Quoted 40 lines · Andrew Morton
Quoted 18 lines · On Tue, 9 Sep 2008 09:14:07 +0200 Adam Tkac <[email protected]> wrote:

when process wants set limit of open files to RLIM_INFINITY it gets EPERM even if it has CAP_SYS_RESOURCE capability. Attached patch should fix the problem. Please add me to CC of your responses because I'm not member of list.

Regards, Adam

Signature · 4 lines
Adam Tkac


[linux26-openfiles.patch  text/plain (634B)]
kernel/sys.c +0 -0
@@ -1458,8 +1458,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
 	    !capable(CAP_SYS_RESOURCE))
 		return -EPERM;

kernel/sys.c asmlinkage long sys_setrlimit(unsigned i · line 1468

Quoted 10 lines
kernel/sys.c +8 -2
-	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;
+	}

The kernel has had this behaviour for a long time. 2.6.13 had:

	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
	    !capable(CAP_SYS_RESOURCE))
		return -EPERM;
	if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
			return -EPERM;
Quoted 6 lines

I don't immediately see a problem with your change, but what makes you believe that it is needed? Is there some standard which we're violating? Is there some operational situation in which the current behaviour is causing a problem?

Thanks.

Well, this change is not _absolutely_ needed because everyone who wants unlimited file descriptors he could set it to NR_OPEN. Look on example (from BIND):

...
#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);
        }
Quoted 6 lines

#elif ...

I think that when you allow set RLIMIT_NOFILE to RLIM_INFINITY you increase portability - you don't have to check if OS is linux and then use different schema for limits.

OK.

I updated the changelog as below.

Please send a Signed-off-by: for thsi change, as per section 12 of Documentation/SubmittingPatches.

Thanks.

From: Adam Tkac <[email protected]>

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 <[email protected]>

---

 kernel/sys.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/sys.c renamed from kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity +8 -2
diff -puN kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity kernel/sys.c
@@ -1532,8 +1532,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	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;
+	}
 
 	retval = security_task_setrlimit(resource, &new_rlim);
 	if (retval)

_

author
#4 lore raw

PATCH 1/1 Allow set 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.

---

 kernel/sys.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/sys.c renamed from kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity +8 -2
diff -puN kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity kernel/sys.c
@@ -1532,8 +1532,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	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;
+	}
 
 	retval = security_task_setrlimit(resource, &new_rlim);
 	if (retval)
#5 lore raw
Quoted 6 lines · Adam Tkac

diff -puN kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity kernel/sys.c

kernel/sys.c renamed from kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity +0 -0
@@ -1532,8 +1532,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
 	    !capable(CAP_SYS_RESOURCE))

kernel/sys.c asmlinkage long sys_setrlimit(unsigned i · line 1541

Quoted 10 lines
kernel/sys.c renamed from kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity +7 -2
 		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;

This makes it possible to set cur > nr_open (when max = INF but nr_open < cur < INF). You need to check that cur <= max after adjustment.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
author
#6 lore raw
Quoted 22 lines · Andreas Schwab
Quoted 6 lines · Adam Tkac

diff -puN kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity kernel/sys.c

kernel/sys.c renamed from kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity +0 -0
@@ -1532,8 +1532,14 @@ asmlinkage long sys_setrlimit(unsigned i
 	if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
 	    !capable(CAP_SYS_RESOURCE))

kernel/sys.c asmlinkage long sys_setrlimit(unsigned i · line 1541

Quoted 10 lines
kernel/sys.c renamed from kernel/sys.c~rlimit-permit-setting-rlimit_nofile-to-rlim_infinity +7 -2
 		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;

This makes it possible to set cur > nr_open (when max = INF but nr_open < cur < INF). You need to check that cur <= max after adjustment.

Quoted 2 lines

Andreas.

Right you are. Improved patch is attached, when cur > max && cur != INF after adjustment EINVAL is returned. (cur can be also set to max but it seems like bad solution for me)

Signature · 1 line
Adam Tkac

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.

kernel/sys.c renamed from kernel/sys.c.openfiles +12 -4
@@ -1469,14 +1469,22 @@ asmlinkage long sys_setrlimit(unsigned i
 		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)