Subject: setrlimit(RLIMIT_STACK,..) check for limit < stack usage
To: None <>
From: Jaromir Dolecek <>
List: tech-kern
Date: 11/18/2001 00:01:32
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII

I've found ancient kern/3045, which basically points out that
it's quite pointless to allow setrlimit(RLIMIT_STACK, ...) to
set the stack size to value below current usage. The caller
would SIGSEGV the moment it would get back and access any
local variable. I think that it would be more sensible
to return EINVAL in this case.

Opinions? I'd probably commit the appended patch in couple
of days, unless someone sees any problem with the change.

Jaromir Dolecek <>
-=  Those who would give up liberty for a little temporary safety deserve  =-
-=  neither liberty nor safety, and will lose both.  -- Benjamin Franklin  =-

Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=ISO-8859-2
Content-Disposition: attachment; filename=res.diff

Index: kern_resource.c
RCS file: /cvsroot/syssrc/sys/kern/kern_resource.c,v
retrieving revision 1.61
diff -u -p -r1.61 kern_resource.c
--- kern_resource.c	2001/11/12 15:25:14	1.61
+++ kern_resource.c	2001/11/17 22:55:18
@@ -293,6 +293,17 @@ dosetrlimit(p, cred, which, limp)
 			limp->rlim_max = maxsmap;
+		 * Return EINVAL if the new limit is lower than current
+		 * usage. Otherwise, the process would get SIGSEGV the moment
+		 * it would try to access anything on it's current stack.
+		 * This is not what the caller intended, unless on crack
+		 * or mistaken.
+		 */
+		if (limp->rlim_cur < p->p_vmspace->vm_dsize * PAGE_SIZE
+		    || limp->rlim_max < p->p_vmspace->vm_dsize * PAGE_SIZE)
+			return (EINVAL);
+		/*
 		 * Stack is allocated to the max at exec time with
 		 * only "rlim_cur" bytes accessible (In other words,
 		 * allocates stack dividing two contiguous regions at