From patchwork Wed Jan 17 14:13:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Draszik?= X-Patchwork-Id: 10169351 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 41CB8603ED for ; Wed, 17 Jan 2018 14:14:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3114D26AE3 for ; Wed, 17 Jan 2018 14:14:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 256FF285AF; Wed, 17 Jan 2018 14:14:21 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable 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 A98EF26AE3 for ; Wed, 17 Jan 2018 14:14:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752694AbeAQOOI (ORCPT ); Wed, 17 Jan 2018 09:14:08 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:35248 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753333AbeAQOOG (ORCPT ); Wed, 17 Jan 2018 09:14:06 -0500 Received: by mail-wm0-f68.google.com with SMTP id r78so16108414wme.0; Wed, 17 Jan 2018 06:14:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bF0WuB2i3LNQN0HTbGctIg72oOKfgeeilRquW6W+slM=; b=Zoxco7hqt9JIG1n9u3IRoHn56WfFGhe3NfToXRPYCxjKww5QRLNahD9kFKg8MvDVNz 2RH+AOABHJ5obaICAciQJsTMAjydZ+Yfcyqrie/kMaa7+EtBYrhzhoXFvK0eZfvA7u8A Ws+amPOnX0CDJ/oSNPNinJprCMxqklAhBrGwRhkWnXQjt4meqyZ1F1p+ucOdgLi15L+4 RcdXihvJEFnP6VT2OpUeIOCGj7UST/bVdrLuS4WORfuBG5202AJpThxNZNAKhvtE4lNI upNzFvlGa/N0JLvpktR5JJbIdipqM/LNe1hBo7a7JzpR16xLKGY35XwQ6mtrAWOCxEtD gggg== X-Gm-Message-State: AKwxytcUMvo/5ZGajM23gWhUgFGZ6DeI0kmHTAIcYirkxzvpb79Zp20k FOEKOHUQrYKkEWxG/ojz5aoMRNtdR7w= X-Google-Smtp-Source: ACJfBos/2mO8Oc9cP+EWLZOCW6rQm9jhxyH3+nFI+1X2TejveRzipcncc+s1lRn7xCZGOrYMfSVo7g== X-Received: by 10.80.219.135 with SMTP id p7mr3189395edk.198.1516198444779; Wed, 17 Jan 2018 06:14:04 -0800 (PST) Received: from tfsielt31850.tycofs.com ([77.107.218.170]) by smtp.gmail.com with ESMTPSA id b30sm3490416ede.53.2018.01.17.06.14.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Jan 2018 06:14:03 -0800 (PST) From: =?UTF-8?q?Andr=C3=A9=20Draszik?= To: linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Andr=C3=A9=20Draszik?= , "Theodore Y. Ts'o" , Jaegeuk Kim , linux-fscrypt@vger.kernel.org, Eric Biggers Subject: [PATCH v2 1/2] fscrypt: add support for the encrypted key type Date: Wed, 17 Jan 2018 14:13:18 +0000 Message-Id: <20180117141319.8060-1-git@andred.net> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180111040022.GA943@zzz.localdomain> References: <20180111040022.GA943@zzz.localdomain> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP We now try to acquire the key according to the encryption policy from both key types, 'logon' as well as 'encrypted'. Signed-off-by: André Draszik Cc: "Theodore Y. Ts'o" Cc: Jaegeuk Kim Cc: linux-fscrypt@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: Eric Biggers --- changes in v2: * dropped the previously added 'fscrypt' encrypted-key, and just use the 'default' format --- fs/crypto/keyinfo.c | 72 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c index 5e6e846f5a24..925af599f954 100644 --- a/fs/crypto/keyinfo.c +++ b/fs/crypto/keyinfo.c @@ -10,6 +10,7 @@ */ #include +#include #include #include #include @@ -66,14 +67,20 @@ static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE], return res; } -static int validate_user_key(struct fscrypt_info *crypt_info, +static inline struct key *fscrypt_get_encrypted_key(const char *sig) +{ + if (IS_ENABLED(CONFIG_ENCRYPTED_KEYS)) + return request_key(&key_type_encrypted, sig, NULL); + return ERR_PTR(-ENOKEY); +} + +static int validate_keyring_key(struct fscrypt_info *crypt_info, struct fscrypt_context *ctx, u8 *raw_key, const char *prefix, int min_keysize) { char *description; struct key *keyring_key; - struct fscrypt_key *master_key; - const struct user_key_payload *ukp; + struct fscrypt_key *master_key, master_key_; int res; description = kasprintf(GFP_NOFS, "%s%*phN", prefix, @@ -83,28 +90,45 @@ static int validate_user_key(struct fscrypt_info *crypt_info, return -ENOMEM; keyring_key = request_key(&key_type_logon, description, NULL); + if (IS_ERR(keyring_key)) + keyring_key = fscrypt_get_encrypted_key(description); kfree(description); if (IS_ERR(keyring_key)) return PTR_ERR(keyring_key); down_read(&keyring_key->sem); - if (keyring_key->type != &key_type_logon) { + if (keyring_key->type == &key_type_logon) { + const struct user_key_payload *ukp; + + ukp = user_key_payload_locked(keyring_key); + if (!ukp) { + /* key was revoked before we acquired its semaphore */ + res = -EKEYREVOKED; + goto out; + } + if (ukp->datalen != sizeof(struct fscrypt_key)) { + res = -EINVAL; + goto out; + } + master_key = (struct fscrypt_key *)ukp->data; + } else if (keyring_key->type == &key_type_encrypted) { + const struct encrypted_key_payload *ekp; + + ekp = keyring_key->payload.data[0]; + master_key = &master_key_; + + master_key->mode = 0; + memcpy (master_key->raw, ekp->decrypted_data, + min (sizeof (master_key->raw), + (size_t) ekp->decrypted_datalen)); + master_key->size = ekp->decrypted_datalen; + } else { printk_once(KERN_WARNING - "%s: key type must be logon\n", __func__); + "%s: key type must be logon or encrypted\n", + __func__); res = -ENOKEY; goto out; } - ukp = user_key_payload_locked(keyring_key); - if (!ukp) { - /* key was revoked before we acquired its semaphore */ - res = -EKEYREVOKED; - goto out; - } - if (ukp->datalen != sizeof(struct fscrypt_key)) { - res = -EINVAL; - goto out; - } - master_key = (struct fscrypt_key *)ukp->data; BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE); if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE @@ -113,9 +137,13 @@ static int validate_user_key(struct fscrypt_info *crypt_info, "%s: key size incorrect: %d\n", __func__, master_key->size); res = -ENOKEY; - goto out; + goto out_clear_key; } res = derive_key_aes(ctx->nonce, master_key, raw_key); + +out_clear_key: + if (master_key == &master_key_) + memzero_explicit(master_key->raw, sizeof (master_key->raw)); out: up_read(&keyring_key->sem); key_put(keyring_key); @@ -302,12 +330,12 @@ int fscrypt_get_encryption_info(struct inode *inode) if (!raw_key) goto out; - res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX, - keysize); + res = validate_keyring_key(crypt_info, &ctx, raw_key, + FS_KEY_DESC_PREFIX, keysize); if (res && inode->i_sb->s_cop->key_prefix) { - int res2 = validate_user_key(crypt_info, &ctx, raw_key, - inode->i_sb->s_cop->key_prefix, - keysize); + int res2 = validate_keyring_key(crypt_info, &ctx, raw_key, + inode->i_sb->s_cop->key_prefix, + keysize); if (res2) { if (res2 == -ENOKEY) res = -ENOKEY;