Resource limit test in binfmt_elf

Michael Meskes (meskes@informatik.rwth-aachen.de)
Wed, 12 Jun 1996 14:52:38 +0200 (MET DST)


I took some time and implemented a resource limit check in binfmt_elf using
some calculations done in other positions in this file (they are still done
after the text, maybe it suffices to do them only once) and the test in
binfmt_aout. It works well for me. A binary with statically allocated 128MB
simply stops with 'Out of memory'.

Michael

--- linux/fs/binfmt_elf.c.old Wed Jun 12 10:09:48 1996
+++ linux/fs/binfmt_elf.c Wed Jun 12 14:51:49 1996
@@ -7,6 +7,7 @@
* Tools".
*
* Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
+ * Added resource limits check: Michael Meskes (meskes@informatik.rwth-aachen.de)
*/

#include <linux/module.h>
@@ -353,6 +354,7 @@
unsigned int start_code, end_code, end_data;
unsigned int elf_stack;
char passed_fileno[6];
+ unsigned long rlim;

ibcs2_interpreter = 0;
status = 0;
@@ -515,6 +517,39 @@
return -E2BIG;
}
}
+
+ /* MM: Check if we extend resource limits */
+ /* First calculate how much memory we need */
+ elf_ppnt = elf_phdata;
+ for(i=0;i < elf_ex.e_phnum; i++){
+ if(elf_ppnt->p_type == PT_LOAD) {
+ k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
+ if((elf_ppnt->p_flags & PF_X) && end_code < k)
+ end_code = k;
+ k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
+ if(k > elf_brk) elf_brk = k;
+ }
+ elf_ppnt++;
+ }
+
+ /* Okay, check initial limits. This avoids letting people circumvent
+ * size limits imposed on them by creating programs with large
+ * arrays in the data or bss.
+ */
+ rlim = current->rlim[RLIMIT_DATA].rlim_cur;
+ if (rlim >= RLIM_INFINITY)
+ rlim = ~0;
+#if 0
+ printk("(end_code) %x\n" , end_code);
+ printk("(start_brk) %x\n" , elf_brk);
+ printk("(difference) %x %d\n", elf_brk - end_code, elf_brk - end_code);
+ printk("(rlim) %lx %ld\n", rlim, rlim);
+#endif
+
+ if (elf_brk - end_code > rlim)
+ return -ENOMEM;
+ elf_brk = end_code = 0;
+ /* MM: End */

/* OK, This is the point of no return */
flush_old_exec(bprm);

-- 
Michael Meskes                   |    _____ ________ __  ____
meskes@informatik.rwth-aachen.de |   / ___// ____/ // / / __ \___  __________
meskes@sanet.de                  |   \__ \/ /_  / // /_/ /_/ / _ \/ ___/ ___/
meskes@debian.org                |  ___/ / __/ /__  __/\__, /  __/ /  (__  )
Use Debian Linux!		 | /____/_/      /_/  /____/\___/_/  /____/