Received: by 10.223.185.111 with SMTP id b44csp675827wrg; Fri, 9 Mar 2018 11:31:46 -0800 (PST) X-Google-Smtp-Source: AG47ELufhViyye+1ZuhJebZTBH7H/mcaNM3zHSKDNPOic73teeZNHpv5GIcx5ygYLrk5Xq82G/DA X-Received: by 10.99.116.23 with SMTP id p23mr24617240pgc.178.1520623906653; Fri, 09 Mar 2018 11:31:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520623906; cv=none; d=google.com; s=arc-20160816; b=FoDd7CTbswF1TpCHuBwY4hovZjXnjzM9lK/tdBa0RABD2sBW2AS57Fh2WboT0x0F3r y4zHz12MyHWsUCduyCC3jpF29UC3R5JZhJWsEEKfSAw2TCQcyZuY2SHa9GNzvHhjhrvb C/MnwNrWqJ2uAm4hBMDDutM9MyFrRCiifiaD4me8uMV6rr0FQjWqtFrcYWlCsOf4EwiY 08swlszg2AN6NBiRVL4jOBUkagpIQYJfyn/yQEpUXCQ01D8XKWgQ5Hej2kj10PGPSH6r BUXAUO48iz6xcYHiwq9QcNeY+TKwe7EfiREDshc6WiVvZZDLse1Syai1sF+PwF+A56UH zsDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=6Y8DpmP3Fe50wr/xwcGlr5n5dOjyHDLyNHNJoucx7zk=; b=WHSmraPdKziOKI8QWRSj0pCCeqMd6oYQ6Bc2fBNZVgAzXkd0xe28g4tGYkpF/LQUbj LkZ7BVJ7ulpnNtnou69JxU2SE7glzOXxSMk+lwOTToXx17hQ2nxQFfoBrXkKzEFjBIV8 Vqkg86yb/3xdStyHRj45pbCe+0tzRUcU5p75FhkuWVXPnpVqXsRWjGs0op/7lbrogny6 KyVWI4dKvYi1JbmA+hpGWi8DQ/oN9BGy2rcQ1hnYmgq5/i4DONqgqDzDLFMxX/TLsHhX Y4SdmEiGf0g0j2DWnzAk9GKQaAzaj3+yFfbL6eOkyqeUFIInE+GrYUhgczX7am79KiC9 ngIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UxT+8ZPg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 131si1335567pfa.109.2018.03.09.11.31.32; Fri, 09 Mar 2018 11:31:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UxT+8ZPg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932593AbeCITa0 (ORCPT + 99 others); Fri, 9 Mar 2018 14:30:26 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:45796 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751278AbeCITaW (ORCPT ); Fri, 9 Mar 2018 14:30:22 -0500 Received: by mail-pl0-f67.google.com with SMTP id v9-v6so5749265plp.12 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=Jwbyztt8RbSLCd1bJpsRq44CltvECoArPbJT00h4fTjUAnjj/5hWbOv6s0fhOua6Fi pEwJYzHIgVLBNXf0VN1EX5GgVTSp39dEe6S59jKvJBPSRwofIXQClk70Nj6GlW8/slxf dVIbUmyuDQIWJW78ZF0z0uOX5ycHghPzsVgI3ILcfIGdulZlZ4itqBn1uZA2djQvA9hC lu6ht7kH/5YYDmxfHvFl7D9ccI1EECjpiqdJb2ONgYK5mUX21J7+T269e7Bu6Ihw4TXR I+FPlT64OFYVpEUors5W6//vyRPF0O7gg/xSSIs3amNoImAjRAcyi5jiqIqOC7wlyyQ3 oG1Q== X-Gm-Message-State: APf1xPCv/aSfOmGIEep0zRm1jjSdJvLAgb/EMAlOkXVU68GwKGiG+J8h rVtW4a+HfeWpVqW98aJ+TEbKtQ== 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-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) { -- 2.7.4 -- Kees Cook Pixel Security