From patchwork Fri Mar 9 19:07:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10271783 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 B54AC6016D for ; Fri, 9 Mar 2018 19:07:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A56ED29ED3 for ; Fri, 9 Mar 2018 19:07:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 99F8929EE5; Fri, 9 Mar 2018 19:07:43 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI 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 3DFDF29ED3 for ; Fri, 9 Mar 2018 19:07:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932571AbeCITHl (ORCPT ); Fri, 9 Mar 2018 14:07:41 -0500 Received: from mail-pl0-f65.google.com ([209.85.160.65]:38264 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932299AbeCITHj (ORCPT ); Fri, 9 Mar 2018 14:07:39 -0500 Received: by mail-pl0-f65.google.com with SMTP id m22-v6so5740422pls.5 for ; Fri, 09 Mar 2018 11:07:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=XPnhkR3yYcdYp+B+WILTqKF3LG7H8K7ybZUQ7te2n+Y=; b=GanskipK7N3TspOlrlmMSlz0aN7LuwHGXERY3GAJv+jLX9MjWs1LJzgINlYFQpdnp6 vd9yEaRoGo1hnJiGb1tpDJGPnIRLna2MWbzu4rowUS8vcH+qgRd36PGOjK1+D8AsuLRB 0Pz8JorWnO/qVWsEibP3X+/CJZRuWwhruB8jk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=XPnhkR3yYcdYp+B+WILTqKF3LG7H8K7ybZUQ7te2n+Y=; b=gGQIoy9vKzLUsSs/9aO78wYDr/KkKH07LRR1kAB91iD6p4dfwJ4fN/rTOHEvUoW4ih KaEcfh8J+y2bMTEiSP3NikMiU6XN1jyFPllw1YStlhW/ihZeNT8tAdj+pJLNUcpqOgux QhI7oFkKfH2lHsVTAizGjCkXryzuGKpy5uP2nAknb4s1yutVyj9ktCMVkUNXHuf1O2Xc w+9ugJOX1EQn6sMZVxG/t1jF2SZtIEB7gmwhSeJBt/MuFw2rDEIgrOr3rbPFFkQxsXGH pUWJsYsBrD3LbyRtXdLvzotepBTa3a2fi484xWfHilxO5rWFeo3sxyDLnGEvqXRrZbT2 mOAA== X-Gm-Message-State: APf1xPAih0j7nHDJDGaZ4ob+tZgO6tbKxa6Nhd0kP2UDKyv/s++HW+kH 0opYDAlBdFD0+kVtr/9/x1MOLM96bE0= X-Google-Smtp-Source: AG47ELvgSeBVv5edRUR3SpWEzG7unw0lDFu9+to6VXtWluEvGLFYEmBfa2BeijUlAHpDNIE9kkxckQ== X-Received: by 2002:a17:902:51ee:: with SMTP id y101-v6mr29174156plh.157.1520622458524; Fri, 09 Mar 2018 11:07:38 -0800 (PST) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id n13sm4341371pfg.45.2018.03.09.11.07.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Mar 2018 11:07:37 -0800 (PST) Date: Fri, 9 Mar 2018 11:07:36 -0800 From: Kees Cook To: James Morris Cc: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, "Serge E. Hallyn" , Linus Torvalds , Mimi Zohar , linux-integrity@vger.kernel.org, Paul Moore , Stephen Smalley Subject: [PATCH] exec: Set file unwritable before LSM check Message-ID: <20180309190736.GA4657@beast> MIME-Version: 1.0 Content-Disposition: inline Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The LSM check should happen after the file has been confirmed to be unchanging. Without this, we could have a ToCToU issue between the LSM verification and the actual contents of the file later. Signed-off-by: Kees Cook --- Only loadpin and SELinux implement this hook. From what I can see, this won't change anything for either of them. IMA calls kernel_read_file(), but looking there it seems those callers won't be negatively impacted either. Can folks double-check this and send an Ack please? --- fs/exec.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 7eb8d21bcab9..a919a827d181 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -895,13 +895,13 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, if (!S_ISREG(file_inode(file)->i_mode) || max_size < 0) return -EINVAL; - ret = security_kernel_read_file(file, id); + ret = deny_write_access(file); if (ret) return ret; - ret = deny_write_access(file); + ret = security_kernel_read_file(file, id); if (ret) - return ret; + goto out; i_size = i_size_read(file_inode(file)); if (max_size > 0 && i_size > max_size) {