Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp820273pxb; Tue, 3 Nov 2020 13:28:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJyNapnL0AMK6gZGvutT6zJR999MykGSYMdzb8ZyEVT5lAalL8OLwikxHGW8ZFUv7T36VYVt X-Received: by 2002:a17:906:76d3:: with SMTP id q19mr20714746ejn.162.1604438915721; Tue, 03 Nov 2020 13:28:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604438915; cv=none; d=google.com; s=arc-20160816; b=qwu7zaF8ZGclODVu6vjNyzCZSBFibq/8X7AMezxVriwqTo2AQWyCeVfeSWCO5dOgD/ 9Mh0+A72lfpiMCQL6Akhj5AFj4w7L12cy9NaOP1AQDOBzut0VeZF57wO3occuHqCh1JM rqUEDygBQXJzGPi1uFWl/IXi2XKOGVVzsRS2EWgYOSPMUIrHrhBWZhbI/I4Xvg/F2xKv LQ6TrRknw1tBuuTjySfoVOFIDGLfMlML2cz9SRcoBaN2/5Un+fH2QtE/EOCiQgIP2CWX IHEdCuC5wutAhHzUnOQoTZkLUxpipn0rAyO8CwF3YSjKjEeAB6f9unz31Pv3IrBlIoL3 93bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=JWiygNaKGgkez2UqgX7VptpoIuijXK0VsmazYRHwJTM=; b=bu/nLY+vcBqs4hnt8X8U+MT0kMGFVl3UNYipXfFKMIzN2acLXFYYvchx574FRfI5BK IHMx2tQX9GEjnf7CtoMcsUoZpfTvq36BcUObBZ0eKhGxrXQBaLSUySEyRDUheAwPz91V JndYbYZocl65HoIbxz9Z4j155BGJbl0LZvqFn6/iQJF/ZVT9basm0Ol/xGYBdlNP0VgE i+xFjA2H9/e6jQGOApyPfcAOGLcHOm8nAphA1i9ZNPc0+/HOGg54sOaM+N3cZm35e8fd 5Fiu/P9JKQTGD4enzMyNOFEt4CebNKc3WbpGUIT60IzLtOYyjJr9DMLf0smsxFZSEPKS 2OEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EOZekDwU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g5si69608ejb.287.2020.11.03.13.28.12; Tue, 03 Nov 2020 13:28:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EOZekDwU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387597AbgKCVCU (ORCPT + 99 others); Tue, 3 Nov 2020 16:02:20 -0500 Received: from mail.kernel.org ([198.145.29.99]:39146 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387562AbgKCVCM (ORCPT ); Tue, 3 Nov 2020 16:02:12 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6545B205ED; Tue, 3 Nov 2020 21:02:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604437330; bh=mnO+NWe+VUlFb0OX4jGXnxmON27sUidwJ6J5ZEA+yQM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EOZekDwU3sx8mST22OnDQSOu0IHuWspuB3dBkRWynAlOjdUtIsi1ASrJ/MLZCf8EB bqJqo/030Tw9vaM+KuA4dlM3hIbm9QbhGWb0nqZxHCQkVAL/vR07w5x9xc2MhOFE2C ZCLW5Frgvq/H90U77pY/G+3BWf4xLHnsu20DZzIQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , "linux-fscrypt@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, Theodore Tso" , Eric Biggers , Theodore Ts'o Subject: [PATCH 4.19 030/191] fscrypt: clean up and improve dentry revalidation Date: Tue, 3 Nov 2020 21:35:22 +0100 Message-Id: <20201103203236.626034349@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203232.656475008@linuxfoundation.org> References: <20201103203232.656475008@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers commit 6cc248684d3d23bbd073ae2fa73d3416c0558909 upstream. Make various improvements to fscrypt dentry revalidation: - Don't try to handle the case where the per-directory key is removed, as this can't happen without the inode (and dentries) being evicted. - Flag ciphertext dentries rather than plaintext dentries, since it's ciphertext dentries that need the special handling. - Avoid doing unnecessary work for non-ciphertext dentries. - When revalidating ciphertext dentries, try to set up the directory's i_crypt_info to make sure the key is really still absent, rather than invalidating all negative dentries as the previous code did. An old comment suggested we can't do this for locking reasons, but AFAICT this comment was outdated and it actually works fine. Signed-off-by: Eric Biggers Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/crypto/crypto.c | 58 ++++++++++++++++++++++++------------------------ fs/crypto/hooks.c | 4 +-- include/linux/dcache.h | 2 - include/linux/fscrypt.h | 6 +--- 4 files changed, 35 insertions(+), 35 deletions(-) --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c @@ -314,45 +314,47 @@ int fscrypt_decrypt_page(const struct in EXPORT_SYMBOL(fscrypt_decrypt_page); /* - * Validate dentries for encrypted directories to make sure we aren't - * potentially caching stale data after a key has been added or - * removed. + * Validate dentries in encrypted directories to make sure we aren't potentially + * caching stale dentries after a key has been added. */ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) { struct dentry *dir; - int dir_has_key, cached_with_key; + int err; + int valid; + + /* + * Plaintext names are always valid, since fscrypt doesn't support + * reverting to ciphertext names without evicting the directory's inode + * -- which implies eviction of the dentries in the directory. + */ + if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME)) + return 1; + + /* + * Ciphertext name; valid if the directory's key is still unavailable. + * + * Although fscrypt forbids rename() on ciphertext names, we still must + * use dget_parent() here rather than use ->d_parent directly. That's + * because a corrupted fs image may contain directory hard links, which + * the VFS handles by moving the directory's dentry tree in the dcache + * each time ->lookup() finds the directory and it already has a dentry + * elsewhere. Thus ->d_parent can be changing, and we must safely grab + * a reference to some ->d_parent to prevent it from being freed. + */ if (flags & LOOKUP_RCU) return -ECHILD; dir = dget_parent(dentry); - if (!IS_ENCRYPTED(d_inode(dir))) { - dput(dir); - return 0; - } - - spin_lock(&dentry->d_lock); - cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY; - spin_unlock(&dentry->d_lock); - dir_has_key = (d_inode(dir)->i_crypt_info != NULL); + err = fscrypt_get_encryption_info(d_inode(dir)); + valid = !fscrypt_has_encryption_key(d_inode(dir)); dput(dir); - /* - * If the dentry was cached without the key, and it is a - * negative dentry, it might be a valid name. We can't check - * if the key has since been made available due to locking - * reasons, so we fail the validation so ext4_lookup() can do - * this check. - * - * We also fail the validation if the dentry was created with - * the key present, but we no longer have the key, or vice versa. - */ - if ((!cached_with_key && d_is_negative(dentry)) || - (!cached_with_key && dir_has_key) || - (cached_with_key && !dir_has_key)) - return 0; - return 1; + if (err < 0) + return err; + + return valid; } const struct dentry_operations fscrypt_d_ops = { --- a/fs/crypto/hooks.c +++ b/fs/crypto/hooks.c @@ -101,9 +101,9 @@ int __fscrypt_prepare_lookup(struct inod if (err) return err; - if (fscrypt_has_encryption_key(dir)) { + if (!fscrypt_has_encryption_key(dir)) { spin_lock(&dentry->d_lock); - dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY; + dentry->d_flags |= DCACHE_ENCRYPTED_NAME; spin_unlock(&dentry->d_lock); } --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -210,7 +210,7 @@ struct dentry_operations { #define DCACHE_MAY_FREE 0x00800000 #define DCACHE_FALLTHRU 0x01000000 /* Fall through to lower layer */ -#define DCACHE_ENCRYPTED_WITH_KEY 0x02000000 /* dir is encrypted with a valid key */ +#define DCACHE_ENCRYPTED_NAME 0x02000000 /* Encrypted name (dir key was unavailable) */ #define DCACHE_OP_REAL 0x04000000 #define DCACHE_PAR_LOOKUP 0x10000000 /* being looked up (with parent locked shared) */ --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -145,10 +145,8 @@ static inline int fscrypt_prepare_rename * filenames are presented in encrypted form. Therefore, we'll try to set up * the directory's encryption key, but even without it the lookup can continue. * - * To allow invalidating stale dentries if the directory's encryption key is - * added later, we also install a custom ->d_revalidate() method and use the - * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a - * plaintext name (flag set) or a ciphertext name (flag cleared). + * This also installs a custom ->d_revalidate() method which will invalidate the + * dentry if it was created without the key and the key is later added. * * Return: 0 on success, -errno if a problem occurred while setting up the * encryption key