From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg0-f47.google.com ([74.125.83.47]:34086 "EHLO mail-pg0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750941AbdGGT5J (ORCPT ); Fri, 7 Jul 2017 15:57:09 -0400 Received: by mail-pg0-f47.google.com with SMTP id t186so21931848pgb.1 for ; Fri, 07 Jul 2017 12:57:08 -0700 (PDT) From: Kees Cook To: Linus Torvalds Cc: Kees Cook , Andy Lutomirski , David Howells , Serge Hallyn , John Johansen , Casey Schaufler , "Eric W. Biederman" , Michal Hocko , Ben Hutchings , Hugh Dickins , Oleg Nesterov , "Jason A. Donenfeld" , Rik van Riel , Alexander Viro , James Morris , Greg Ungerer , Ingo Molnar , Nicolas Pitre , Stephen Smalley , Paul Moore , Vivek Goyal , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Tetsuo Handa , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH 2/2] exec: Use sane stack rlimit for setuid exec Date: Fri, 7 Jul 2017 12:57:00 -0700 Message-Id: <1499457420-83038-3-git-send-email-keescook@chromium.org> In-Reply-To: <1499457420-83038-1-git-send-email-keescook@chromium.org> References: <1499457420-83038-1-git-send-email-keescook@chromium.org> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: Before memory layout selection and credentials having been updated, reset stack rlimit to something sane for setuid execs to avoid having the caller having control over memory 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 --- fs/exec.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index 1e8d647d8e7c..2b072cf79f6d 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1335,6 +1335,16 @@ void setup_new_exec(struct linux_binprm * bprm) if (security_bprm_secureexec(bprm)) { /* Record for AT_SECURE. */ bprm->secureexec = 1; + + /* + * If this is a setuid execution, 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 not return + * to avoid cleaning up the change on failure. + */ + if (current->signal->rlim[RLIMIT_STACK].rlim_cur > _STK_LIM) + current->signal->rlim[RLIMIT_STACK].rlim_cur = _STK_LIM; } arch_pick_mmap_layout(current->mm); -- 2.7.4