From patchwork Fri Mar 9 19:30:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10271843 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 256D36016D for ; Fri, 9 Mar 2018 19:30:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0839729F20 for ; Fri, 9 Mar 2018 19:30:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F041929F37; Fri, 9 Mar 2018 19:30:24 +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 8603329F20 for ; Fri, 9 Mar 2018 19:30:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751282AbeCITaY (ORCPT ); Fri, 9 Mar 2018 14:30:24 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:34680 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751249AbeCITaW (ORCPT ); Fri, 9 Mar 2018 14:30:22 -0500 Received: by mail-pl0-f67.google.com with SMTP id u13-v6so5770791plq.1 for ; Fri, 09 Mar 2018 11:30:22 -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=6Y8DpmP3Fe50wr/xwcGlr5n5dOjyHDLyNHNJoucx7zk=; b=UxT+8ZPg/ELFjlPug1TiG4N2CjlVo1oMTkhCVFoc7PiDd7DNwjBeV/e9rQe2RJVycA 6ugFCWHRBx4lQ8N2WbV5+PJpohrJZni4pzOxOJSS03a8wJ0Q1J7a83m4S9OLEz//y72k LjWVzyUB/rpM34KTL+NhN/e3qKbT8oQUX20iU= 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=6Y8DpmP3Fe50wr/xwcGlr5n5dOjyHDLyNHNJoucx7zk=; b=aR4naejvAh/ZItUK/4zacpocJ6eWOQePRcWHxUOljIIhAPBIh6x9kzUDQIkl2qo7TX iAKOBhEOgdv4p2humgxjIdZz17vxBfxq2r74Qi9zZ++QBNoo+PPQeBLOSS6Zeo2vMgfl +wUUQVJBYIrtqNV4HcAa0gy6nunUkM1uIVOkc2tsWEbIfzYB1PYaTHQeeVMLrCUyzsUe wy2oNatfGrGCxeYINgdnZJH1TC0/Q6Gq914xt7c8qPJ0wdkVsPZ6etOZkqGjSIGtdnvg gpgoClkG/zJ20Tc+E4WkrJ7mqo2cuWXbDqZwA0X4M6uJ3RgOE14fYgQBGLkljd87x/sx pCtg== X-Gm-Message-State: APf1xPCaCYtG2g4weg68oJfM4dOSlti3cj91gMMzOtplQUAEKI0y35Og RN2kNyTFjMahGCnCBe0DTSwJgD99bcA= X-Google-Smtp-Source: AG47ELsjaDzxlA7RSD65c0e+e5OwvYhSo1iLxK8JYICVTr9fLu5kIvaopl2c0cwpigd6BcFgJyOIhw== X-Received: by 2002:a17:902:5984:: with SMTP id p4-v6mr29397893pli.38.1520623822399; Fri, 09 Mar 2018 11:30:22 -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 d12sm3865777pfn.42.2018.03.09.11.30.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Mar 2018 11:30:21 -0800 (PST) Date: Fri, 9 Mar 2018 11:30:20 -0800 From: Kees Cook To: James Morris Cc: linux-kernel@vger.kernel.org, Linus Torvalds , LSM List , "Serge E. Hallyn" , Mimi Zohar , linux-integrity , Paul Moore , Stephen Smalley Subject: [PATCH v2] exec: Set file unwritable before LSM check Message-ID: <20180309193020.GA5149@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 race between the Time of Check (the call to security_kernel_read_file() which could read the file and make access policy decisions) and the Time of Use (starting with kernel_read_file()'s reading of the file contents). In theory, file contents could change between the two. Signed-off-by: Kees Cook --- v2: Clarify the ToC/ToU race (Linus) Only loadpin and SELinux currently 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) {