From patchwork Tue Aug 1 19:16:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9875301 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 A1A2760365 for ; Tue, 1 Aug 2017 19:18:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 92D3C2871F for ; Tue, 1 Aug 2017 19:18:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 873D128722; Tue, 1 Aug 2017 19:18:44 +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 03FBC2871F for ; Tue, 1 Aug 2017 19:18:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752269AbdHATSm (ORCPT ); Tue, 1 Aug 2017 15:18:42 -0400 Received: from mail-pg0-f53.google.com ([74.125.83.53]:34441 "EHLO mail-pg0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752043AbdHATQs (ORCPT ); Tue, 1 Aug 2017 15:16:48 -0400 Received: by mail-pg0-f53.google.com with SMTP id u185so11615215pgb.1 for ; Tue, 01 Aug 2017 12:16:48 -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=eRUmHNitiLx9Fm8TY4rF6UZVC3V2sF5sGvWeeIDkc14=; b=bCKObpG6IEGTWbkZNb/qWjy1guKjMPKlx5roW/GdoJyLpGUWVHWdeOqSGbzOhGauDN xJcJqs9D6khkgaM5AamLIUCpCBnKGID6rMYMLi2t1MYNltHlnpiV/udgfMmLnfV3CKkp zzbwbxi9cKH5WWEp6OSC2P8DxGHTAjWZk1CFU= 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=eRUmHNitiLx9Fm8TY4rF6UZVC3V2sF5sGvWeeIDkc14=; b=SQbzvk6yA7eopgPwqvh6X84GlyxQCwzMGs5UTSNHhGKaeybNEbr8FLzT/i2Z0zNyE0 PiKYY0/BXZcGoo3av99s0YIEHlx/ELeyNYR4YsbzG1AIa2gYttV80T35qV5zXjCoNHdY qjurEEvI/EyrPsvMMpCxcNjvwDBwBknVoEfd20IHVOA1xj0Ku4OnjvT59H3XFkB5+pNS IUUt2kkix35JaR29W9ZWqsuE/jj2e/GkgVZ774lQXiKNr+5XY94jvfSDCKrkKDg8DNew rdRmH26XK0rQGkw0ygMOO0MIe3NOPxO9St6cd0rKDaHzPl5fGqRLHY3L73T53z56tptO UA6g== X-Gm-Message-State: AIVw1132DjkUL1EQEL6ZDQZmkkfnQnF6q9AbQkmH6pJXciXVRyIajam7 /GSTHd+g9l4IXyO5 X-Received: by 10.98.192.205 with SMTP id g74mr19971712pfk.1.1501615007997; Tue, 01 Aug 2017 12:16:47 -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 h14sm57148089pgn.34.2017.08.01.12.16.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Aug 2017 12:16:45 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Linus Torvalds , Andrew Morton , James Morris , "Serge E. Hallyn" , Andy Lutomirski , "Eric W. Biederman" , John Johansen , Paul Moore , Casey Schaufler , Stephen Smalley , Tetsuo Handa , David Howells , linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v5 08/15] commoncap: Move cap_elevated calculation into bprm_set_creds Date: Tue, 1 Aug 2017 12:16:31 -0700 Message-Id: <1501614998-62619-9-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1501614998-62619-1-git-send-email-keescook@chromium.org> References: <1501614998-62619-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 Instead of a separate function, open-code the cap_elevated test, which lets us entirely remove bprm->cap_effective (to use the local "effective" variable instead), and more accurately examine euid/egid changes via the existing local "is_setid". The following LTP tests were run to validate the changes: # ./runltp -f syscalls -s cap # ./runltp -f securebits # ./runltp -f cap_bounds # ./runltp -f filecaps All kernel selftests for capabilities and exec continue to pass as well. Signed-off-by: Kees Cook Reviewed-by: James Morris Acked-by: Serge Hallyn Reviewed-by: Andy Lutomirski --- include/linux/binfmts.h | 3 --- security/commoncap.c | 52 ++++++++++--------------------------------------- 2 files changed, 10 insertions(+), 45 deletions(-) diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h index 213c61fa3780..fb44d6180ca0 100644 --- a/include/linux/binfmts.h +++ b/include/linux/binfmts.h @@ -31,9 +31,6 @@ struct linux_binprm { * binfmt_script/misc). */ called_set_creds:1, - cap_effective:1,/* true if has elevated effective capabilities, - * false if not; except for init which inherits - * its parent's caps anyway */ /* * True if most recent call to the commoncaps bprm_set_creds * hook (due to multiple prepare_binprm() calls from the diff --git a/security/commoncap.c b/security/commoncap.c index abb6050c8083..d8e26fb9781d 100644 --- a/security/commoncap.c +++ b/security/commoncap.c @@ -285,15 +285,6 @@ int cap_capset(struct cred *new, return 0; } -/* - * Clear proposed capability sets for execve(). - */ -static inline void bprm_clear_caps(struct linux_binprm *bprm) -{ - cap_clear(bprm->cred->cap_permitted); - bprm->cap_effective = false; -} - /** * cap_inode_need_killpriv - Determine if inode change affects privileges * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV @@ -443,7 +434,7 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c int rc = 0; struct cpu_vfs_cap_data vcaps; - bprm_clear_caps(bprm); + cap_clear(bprm->cred->cap_permitted); if (!file_caps_enabled) return 0; @@ -476,13 +467,11 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c out: if (rc) - bprm_clear_caps(bprm); + cap_clear(bprm->cred->cap_permitted); return rc; } -static int is_secureexec(struct linux_binprm *bprm); - /** * cap_bprm_set_creds - Set up the proposed credentials for execve(). * @bprm: The execution parameters, including the proposed creds @@ -587,8 +576,6 @@ int cap_bprm_set_creds(struct linux_binprm *bprm) if (WARN_ON(!cap_ambient_invariant_ok(new))) return -EPERM; - bprm->cap_effective = effective; - /* * Audit candidate if current->cap_effective is set * @@ -617,35 +604,16 @@ int cap_bprm_set_creds(struct linux_binprm *bprm) return -EPERM; /* Check for privilege-elevated exec. */ - bprm->cap_elevated = is_secureexec(bprm); - - return 0; -} - -/** - * is_secureexec - Determine whether a secure execution is required - * @bprm: The execution parameters - * - * Determine whether a secure execution is required, return 1 if it is, and 0 - * if it is not. - * - * The credentials have been committed by this point, and so are no longer - * available through @bprm->cred. - */ -static int is_secureexec(struct linux_binprm *bprm) -{ - const struct cred *cred = bprm->cred; - kuid_t root_uid = make_kuid(cred->user_ns, 0); - - if (!uid_eq(cred->uid, root_uid)) { - if (bprm->cap_effective) - return 1; - if (!cap_issubset(cred->cap_permitted, cred->cap_ambient)) - return 1; + bprm->cap_elevated = 0; + if (is_setid) { + bprm->cap_elevated = 1; + } else if (!uid_eq(new->uid, root_uid)) { + if (effective || + !cap_issubset(new->cap_permitted, new->cap_ambient)) + bprm->cap_elevated = 1; } - return (!uid_eq(cred->euid, cred->uid) || - !gid_eq(cred->egid, cred->gid)); + return 0; } /**