From patchwork Tue Apr 18 17:04:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sweet Tea Dorminy X-Patchwork-Id: 13215954 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9590EC77B78 for ; Tue, 18 Apr 2023 17:05:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232277AbjDRRFJ (ORCPT ); Tue, 18 Apr 2023 13:05:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230445AbjDRRFI (ORCPT ); Tue, 18 Apr 2023 13:05:08 -0400 Received: from box.fidei.email (box.fidei.email [IPv6:2605:2700:0:2:a800:ff:feba:dc44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03C011719 for ; Tue, 18 Apr 2023 10:05:07 -0700 (PDT) Received: from authenticated-user (box.fidei.email [71.19.144.250]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by box.fidei.email (Postfix) with ESMTPSA id 348828023D; Tue, 18 Apr 2023 13:05:07 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=dorminy.me; s=mail; t=1681837507; bh=DP5hlVvNjw2JROMJ6s6bshEcbxvK3Nx2/UIgbRvXRGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vPH5aeFJ58RltTuRH9zBm+ERNqoW8WC4NNqmD2rIqwWQWCi4UV+d4CJhQfGebCupz S2v29m5XqGT43l+FGKUhR55U13N+vJ1T/OqNISv3yAZgh7OCqnstYVncei5aO7zw8i sDyw7lyynMXNNl5kM59rg/n82uelgrh9ZSkF7r//ros+radoLpPgKAIQrussj2QbHb pmCIzRhYk/fy3W4PiAUDagzk+o5tECgQvjJ94cNWA2WCr1hXsQAxh5rjhtJ7GpL/lm dl1XCDjadrTNICHJTnY2wOmvDjRwmIBKIu0Nw/6ZjMBFFaeUEHH+P7HX2uXo1NOVWK S/3kWlui7gZow== From: Sweet Tea Dorminy To: Eric Biggers , "Theodore Y. Ts'o" , Jaegeuk Kim , linux-fscrypt@vger.kernel.org, kernel-team@meta.com Cc: Sweet Tea Dorminy Subject: [PATCH 11/11] fscrypt: factor helper for locking master key Date: Tue, 18 Apr 2023 13:04:36 -0400 Message-Id: <0624b444f5a952f27b9de209f28ce3c3387e2f35.1681837291.git.sweettea-kernel@dorminy.me> In-Reply-To: <1edeb5c4936667b6493b50776cd1cbf5e4cf2fdd.1681837291.git.sweettea-kernel@dorminy.me> References: <1edeb5c4936667b6493b50776cd1cbf5e4cf2fdd.1681837291.git.sweettea-kernel@dorminy.me> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org When keys are prepared at the point of use, using a pooled prepared key, we'll need to lock and check the existence of the master key secret in multiple places. So go on and factor out the helper. Signed-off-by: Sweet Tea Dorminy --- fs/crypto/keysetup.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c index 55c416df6a71..9cd60e09b0c5 100644 --- a/fs/crypto/keysetup.c +++ b/fs/crypto/keysetup.c @@ -106,6 +106,17 @@ select_encryption_mode(const union fscrypt_policy *policy, return ERR_PTR(-EINVAL); } +static int lock_master_key(struct fscrypt_master_key *mk) +{ + down_read(&mk->mk_sem); + + /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */ + if (!is_master_key_secret_present(&mk->mk_secret)) + return -ENOKEY; + + return 0; +} + /* * Prepare the crypto transform object or blk-crypto key in @prep_key, given the * raw key, encryption mode (@ci->ci_mode), flag indicating which encryption @@ -569,13 +580,10 @@ static int find_and_lock_master_key(const struct fscrypt_info *ci, *mk_ret = NULL; return 0; } - down_read(&mk->mk_sem); - /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */ - if (!is_master_key_secret_present(&mk->mk_secret)) { - err = -ENOKEY; + err = lock_master_key(mk); + if (err) goto out_release_key; - } if (!fscrypt_valid_master_key_size(mk, ci)) { err = -ENOKEY;