2006-10-19 14:35:49

by Marcus Meissner

[permalink] [raw]
Subject: [PATCH] binfmt_elf: randomize PIE binaries


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

Signed-off-by: Marcus Meissner <[email protected]>

----
binfmt_elf.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)


--- linux-2.6.18/fs/binfmt_elf.c.xx 2006-10-19 11:21:49.000000000 +0200
+++ linux-2.6.18/fs/binfmt_elf.c 2006-10-19 11:24:58.000000000 +0200
@@ -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,


2006-10-19 20:24:09

by Dave Jones

[permalink] [raw]
Subject: Re: [PATCH] binfmt_elf: randomize PIE binaries

On Thu, Oct 19, 2006 at 04:35:47PM +0200, Marcus Meissner wrote:

> --- linux-2.6.18/fs/binfmt_elf.c.xx 2006-10-19 11:21:49.000000000 +0200
> +++ linux-2.6.18/fs/binfmt_elf.c 2006-10-19 11:24:58.000000000 +0200
> @@ -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,


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

--
http://www.codemonkey.org.uk

2006-10-20 11:54:17

by Marcus Meissner

[permalink] [raw]
Subject: Re: [PATCH] binfmt_elf: randomize PIE binaries

On Thu, Oct 19, 2006 at 04:23:54PM -0400, Dave Jones wrote:
> On Thu, Oct 19, 2006 at 04:35:47PM +0200, Marcus Meissner wrote:
>
> > --- linux-2.6.18/fs/binfmt_elf.c.xx 2006-10-19 11:21:49.000000000 +0200
> > +++ linux-2.6.18/fs/binfmt_elf.c 2006-10-19 11:24:58.000000000 +0200
> > @@ -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,
>
>
> 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?

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