From patchwork Sun Oct 8 18:09:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Theodore Ts'o X-Patchwork-Id: 823021 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=thunk.org header.i=@thunk.org header.b="MoydxfzD"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3y9BHB4f90z9t61 for ; Mon, 9 Oct 2017 05:09:58 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753455AbdJHSJ4 (ORCPT ); Sun, 8 Oct 2017 14:09:56 -0400 Received: from imap.thunk.org ([74.207.234.97]:49450 "EHLO imap.thunk.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753250AbdJHSJz (ORCPT ); Sun, 8 Oct 2017 14:09:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=thunk.org; s=ef5046eb; h=Content-Type:MIME-Version:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=haNg2LbTaPUaODEG0e2HRJqUxnPp/HJYVy7HwTzHeCY=; b=MoydxfzD6Ewk9y9YlZgU9H1xX9 Gga/D3QcukAii7BN0G6wBBuS0mn5H3by6iswGO0h37iGSRRPziYTGzz4judtuP9JcekqrVPefiQow 0CaXV7So+B+pBiChdQbGFi5oyLd20RAdozzxxfVoRpMxHKswc3hr3pyGC+Z51tZGP1Kk=; Received: from root (helo=callcc.thunk.org) by imap.thunk.org with local-esmtp (Exim 4.89) (envelope-from ) id 1e1G1S-0000Me-6d; Sun, 08 Oct 2017 18:09:54 +0000 Received: by callcc.thunk.org (Postfix, from userid 15806) id 36F74C00C17; Sun, 8 Oct 2017 14:09:53 -0400 (EDT) Date: Sun, 8 Oct 2017 14:09:53 -0400 From: Theodore Ts'o To: stable@vger.kernel.org Cc: linux-ext4@vger.kernel.org Subject: [FOR STABLE 4.1] ext4 crypto: don't regenerate the per-inode encryption key unnecessarily Message-ID: <20171008180953.rpnxvpggqggc334m@thunk.org> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170609 (1.8.3) X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: tytso@thunk.org X-SA-Exim-Scanned: No (on imap.thunk.org); SAEximRunCond expanded to false Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org This isn't a backport of the upstream commit; rather, it's a complete rewrite. An explanation of why is included in the patch description. From e421d2269a66540cddddfd8d85902e7ecf4605df Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sun, 8 Oct 2017 13:32:21 -0400 Subject: [PATCH] ext4 crypto: don't regenerate the per-inode encryption key unnecessarily [ Relevant upstream commit: 1b53cf9815bb4744958d41f3795d5d5a1d365e2d ] This fixes the same problem as upstream commit 1b53cf9815bb: "fscrypt: remove broken support for detecting keyring key revocation". Specifically, key revocations racing with readpage operations will cause the kernel to crash and burn with a BUG_ON or a NULL pointer dereference in a block I/O callback stemming from an ext4_readpage() operation. This fix is needed to fix prevent xfstests test runs from crashing while running the generic/421 test. The root cause is different in the 4.1 kernel, however, since the 4.1's encryption handling is so _primitive_ compared to later kernels. The code isn't actually explicitly checking for revoked keys. Instead, the code is neededly regenerating the per-file encryption key on every mmap() or open() or directory operation (in the case of a directory inode). Yelch! If the file is already opened and actively being read, and there is a racing open() after the user's master key has been revoked, there will be the same net effect as the problem fixed by upstream commit 1b53cf9815bb --- the per-file key will be marked as invalid and this will cause a BUG_ON. In the AOSP 3.18 and 4.4 android-common kernels, the more modern version of ext4 encryption have been backported, including a backport of upstream commit 1b53cf9815bb. This is a dozen plus commits, and isn't really suitable for the Upstream LTS kernel. So instead, this is the simplest bug which fixes the same high-level issue as the upstream commit, without dragging in all of the other non-bug fixes improvements to the ext4 encryption code found in newer kernels. Signed-off-by: Theodore Ts'o --- fs/ext4/crypto_key.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c index 52170d0b7c40..c8c3bf0286be 100644 --- a/fs/ext4/crypto_key.c +++ b/fs/ext4/crypto_key.c @@ -99,9 +99,14 @@ int ext4_generate_encryption_key(struct inode *inode) struct ext4_encryption_context ctx; struct user_key_payload *ukp; struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); - int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, - &ctx, sizeof(ctx)); + int res; + + if (ext4_has_encryption_key(inode)) + return 0; + + res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, + &ctx, sizeof(ctx)); if (res != sizeof(ctx)) { if (res > 0)