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: 10271785 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 313D46016D for ; Fri, 9 Mar 2018 19:07:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 208AA29ED3 for ; Fri, 9 Mar 2018 19:07:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 14EBA29EE5; Fri, 9 Mar 2018 19:07:45 +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 B739C29ED3 for ; Fri, 9 Mar 2018 19:07:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932299AbeCITHl (ORCPT ); Fri, 9 Mar 2018 14:07:41 -0500 Received: from mail-pl0-f68.google.com ([209.85.160.68]:40661 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932568AbeCITHj (ORCPT ); Fri, 9 Mar 2018 14:07:39 -0500 Received: by mail-pl0-f68.google.com with SMTP id i6-v6so5726070plt.7 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=HdlKEAf1gCOl6lzEMNkkvKVm5jndKl6R+gVdh1DOp/blmVmoKPKCi8QeRmdTmaU15b cCsqQkTq83RbL8AhMzdiTlQlKKvL6n+0obqniBN+lMd3UOX5chFAGDT5FRUG5nAcOcpj n5lFEKqCTV+Q9W6bO7aK17bp2I8/y1iQxA5OAbLGIFuqo1g/0BYeXDkoXhE8vh1bFZRU WtEvAlfwa9UUHe0jQ1s1XyHnBzLCwCxCHLEinJXj8BW+xXIyPmnimn1auLTu/qmErwJC uTgXPzblb4Xwfda+D2cx36HcKOriqxhpk/onMcsTFXiA6aWUaqsFmHKojNRVUrPUHmRb vgNQ== X-Gm-Message-State: APf1xPATbFZPul6nqoA+pH3Y2lebyr9Ahc8W91EYvmJ9yBz3EGwU1veV jUqNbNYQpeFQenvudCG84j0VGA== 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: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: 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) {