Skip to contentSkip to conversation

binfmt_elf: randomize PIE binaries

PATCH In review Marcus Meissner 2 participants 3 messages Oct 19, 2006 – Oct 20, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 binfmt_elf: randomize PIE binaries

Randomizes -pie compiled binaries over the whole address range from 0 up to ELF_ET_DYN_BASE.

----

 binfmt_elf.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/binfmt_elf.c renamed from fs/binfmt_elf.c.xx +6 -1
@@ -856,7 +856,12 @@ static int load_elf_binary(struct linux_
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
-			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = randomize_range(0, ELF_ET_DYN_BASE,
+							    vaddr);
+			else
+				load_bias = ELF_ET_DYN_BASE - vaddr;
+			load_bias = ELF_PAGESTART(load_bias);
 		}
 
 		error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
#1 lore raw
Quoted 6 lines · Marcus Meissner
fs/binfmt_elf.c renamed from fs/binfmt_elf.c.xx +0 -0
@@ -856,7 +856,12 @@ static int load_elf_binary(struct linux_
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */

fs/binfmt_elf.c static int load_elf_binary(struct linux_ · line 867

Quoted 10 lines
fs/binfmt_elf.c renamed from fs/binfmt_elf.c.xx +6 -1
-			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = randomize_range(0, ELF_ET_DYN_BASE,
+							    vaddr);
+			else
+				load_bias = ELF_ET_DYN_BASE - vaddr;
+			load_bias = ELF_PAGESTART(load_bias);
 		}
 
 		error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,

vaddr seems odd to be using as the len for randomize_range doesn't it ? Should that be randomize_range(0, ELF_ET_DYN_BASE, ELF_ET_DYN_BASE - vaddr);

maybe ?

Also, when we get to the elf_map(), we add vaddr back again, but in the randomize_range case, we never subtracted it. hmm?

Dave

Signature · 1 line
http://www.codemonkey.org.uk
#2 lore raw
Quoted 23 lines · Dave Jones
Quoted 6 lines · Marcus Meissner
fs/binfmt_elf.c renamed from fs/binfmt_elf.c.xx +0 -0
@@ -856,7 +856,12 @@ static int load_elf_binary(struct linux_
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */

fs/binfmt_elf.c static int load_elf_binary(struct linux_ · line 867

Quoted 10 lines
fs/binfmt_elf.c renamed from fs/binfmt_elf.c.xx +6 -1
-			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = randomize_range(0, ELF_ET_DYN_BASE,
+							    vaddr);
+			else
+				load_bias = ELF_ET_DYN_BASE - vaddr;
+			load_bias = ELF_PAGESTART(load_bias);
 		}
 
 		error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,

vaddr seems odd to be using as the len for randomize_range doesn't it ? Should that be randomize_range(0, ELF_ET_DYN_BASE, ELF_ET_DYN_BASE - vaddr);

Quoted 4 lines

maybe ?

Also, when we get to the elf_map(), we add vaddr back again, but in the randomize_range case, we never subtracted it. hmm?

Yes, the code was not really doing what I actually intended. (It was working, but the range always started at vaddr).

I am using randomize_range (PAGE_SIZE, ELF_ET_DYN_BASE,0) (- vaddr) now after further comments from Arjan.

Any other good ideas for a range to do the randomization in are welcome.

Ciao, Marcus