From patchwork Mon Jul 31 23:51:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9873369 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 733CA6016C for ; Mon, 31 Jul 2017 23:53:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D53828521 for ; Mon, 31 Jul 2017 23:53:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 51EB7285D3; Mon, 31 Jul 2017 23:53:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D60F28599 for ; Mon, 31 Jul 2017 23:53:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751918AbdGaXwp (ORCPT ); Mon, 31 Jul 2017 19:52:45 -0400 Received: from mail-pf0-f173.google.com ([209.85.192.173]:33287 "EHLO mail-pf0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751825AbdGaXvz (ORCPT ); Mon, 31 Jul 2017 19:51:55 -0400 Received: by mail-pf0-f173.google.com with SMTP id d67so541943pfc.0 for ; Mon, 31 Jul 2017 16:51:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QlO6iGOkYRVZCrUQYmHXVuwycAhjw07Pd52u2TWaT9c=; b=WdDkJ59T3kqmtdQcDyat3MFsErxdazw2rydhwO7KBuTY46YwSQEd2yk2IFC6wNPGM8 K6Sdlf7NB9AKzt6p8fy6gYO5waydqVi/qR98q4coaFkLrNet67GMZGREwnFBPZeMYNiD 1fhuagE+ZxnF6f4gLvbDYf+v3+3eIQwvxmtS4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QlO6iGOkYRVZCrUQYmHXVuwycAhjw07Pd52u2TWaT9c=; b=Lnh3BM+Bh/9suk5DMbPNxrIrEK60D6u5bhRUOY57HwSleiqp7Iao+sII0vV+7UJAmY JbHw5WNXISxOH7ehB1oHnw7CWZhyKHuMniTV4CGwDWsplCPp7cftSMwwyG06HfNZqOli KhWAXOOUXlYOHsu7S20LBptEyUTcSkX2qxYydxNYuvaNtHU4VpYpfyf/NkkFfpDvk1um VHUE+QiHRbQTWVwyvifIgKMStAYoH0Wln1z+JCmS/ytmlmbwfrdfe/wTdsdePcQyyMbh CCz4tuPLWAVaX11skgNPLBqEvOoxZ6A5I6p00XgyO/3gGjVcVFo7oErP4OtgmROkE/aG MNhg== X-Gm-Message-State: AIVw113NdkT7z+PfEf/a22OV6KLh+4SvlJKGrZSSLEsluuRAyyvcuaJY BXWgBgRlnssUbVqy X-Received: by 10.98.86.154 with SMTP id h26mr16843855pfj.250.1501545115293; Mon, 31 Jul 2017 16:51:55 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id 64sm48990000pff.168.2017.07.31.16.51.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jul 2017 16:51:53 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Linus Torvalds , David Howells , "Eric W. Biederman" , John Johansen , "Serge E. Hallyn" , Paul Moore , Stephen Smalley , Casey Schaufler , Tetsuo Handa , James Morris , Andy Lutomirski , linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 14/15] exec: Use sane stack rlimit under secureexec Date: Mon, 31 Jul 2017 16:51:32 -0700 Message-Id: <1501545093-56634-15-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1501545093-56634-1-git-send-email-keescook@chromium.org> References: <1501545093-56634-1-git-send-email-keescook@chromium.org> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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 Cc: Linus Torvalds Signed-off-by: Kees Cook Reviewed-by: James Morris Acked-by: Serge Hallyn --- fs/exec.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index 3ae8a40c587b..cddd2a2cbc1f 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1329,6 +1329,18 @@ void setup_new_exec(struct linux_binprm * bprm) */ bprm->secureexec |= bprm->cap_elevated; + if (bprm->secureexec) { + /* + * 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; + } + arch_pick_mmap_layout(current->mm); current->sas_ss_sp = current->sas_ss_size = 0;