Re: [PATCH v2 8/8] exec: Use sane stack rlimit under secureexec

From: Ben Hutchings
Date: Mon Jul 10 2017 - 10:09:45 EST


On Mon, 2017-07-10 at 00:57 -0700, Kees Cook wrote:
> For a secureexec, before memory layout selection has happened, reset the
> stack rlimit to something sane to avoid the caller having control over
> the resulting layouts.
>
> $ ulimit -s
> 8192
> $ ulimit -s unlimited
> $ /bin/sh -c 'ulimit -s'
> unlimited
> $ sudo /bin/sh -c 'ulimit -s'
> 8192
>
> Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
> ---
> Âfs/exec.c | 10 ++++++++++
> Â1 file changed, 10 insertions(+)
>
> diff --git a/fs/exec.c b/fs/exec.c
> index e0186db02f90..1e3463854a16 100644
> --- a/fs/exec.c
> +++ b/fs/exec.c
> @@ -1343,6 +1343,16 @@ void setup_new_exec(struct linux_binprm * bprm)
> Â
> Â /* Make sure parent cannot signal privileged process. */
> Â current->pdeath_signal = 0;
> +
> + /*
> + Â* For secureexec, reset the stack limit to sane default to
> + Â* avoid bad behavior from the prior rlimits. This has to
> + Â* happen before arch_pick_mmap_layout(), which examines
> + Â* RLIMIT_STACK, but after the point of no return to avoid
> + Â* needing to clean up the change on failure.
> + Â*/
> + if (current->signal->rlim[RLIMIT_STACK].rlim_cur > _STK_LIM)
> + current->signal->rlim[RLIMIT_STACK].rlim_cur = _STK_LIM;

As setup_new_exec() is called before install_exec_creds(), I think this
leaves a window where the real user can change the limit again with
prlimit().

Ben.

> Â }
> Â
> Â arch_pick_mmap_layout(current->mm);
--
Ben Hutchings
Absolutum obsoletum. (If it works, it's out of date.) - Stafford Beer

Attachment: signature.asc
Description: This is a digitally signed message part