From patchwork Mon Oct 9 19:51:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9994163 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 9E0C460230 for ; Mon, 9 Oct 2017 19:52:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 919692878E for ; Mon, 9 Oct 2017 19:52:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 86771287D1; Mon, 9 Oct 2017 19:52:51 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham 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 056262878E for ; Mon, 9 Oct 2017 19:52:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755297AbdJITws (ORCPT ); Mon, 9 Oct 2017 15:52:48 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:34295 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755055AbdJITwq (ORCPT ); Mon, 9 Oct 2017 15:52:46 -0400 Received: by mail-pf0-f194.google.com with SMTP id b85so9696055pfj.1; Mon, 09 Oct 2017 12:52:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GNWf79au9dgs6V66zwP0hniM562x/6yIIx3mQrwURuk=; b=B0i9gtYrHZvB5jegc4Rm+zNnTfWHYz3Gmukk7HGb7KHd4Ho6LT+o3EkpPWs5I8u8K5 C5XWRcS8V8RJTXNE418tVwXhp4qWd7XlBUCiFu0PrQ6yRJ9QvYcv6GZcsLKUpVZdnsvn 4alkHPk102mAtYIFtLHyM//TbZWmvF9FSjAxa3XN/HKB6t5d+WJkTjnwKHcPANNH7ihq uydU0LWYP+fyzC3l1GD111mCCRIk23CtOhZKVxtz100BySe2I/9ZhLgZY52gtZR37WIB Q4G+y9y4z0td1GgbWhbd6UTqipY48m+uDBkwNg42q3DG9MB6eK+5YmCwrTGglTb403C0 evDQ== 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; bh=GNWf79au9dgs6V66zwP0hniM562x/6yIIx3mQrwURuk=; b=Sz/I6tYGFa2ScVpwjtjRySemwe1+qo42uV5P0zG74L60xoP1ShchaSuPP4KWi8c82t lMikZ+T5HfgN0UKNqx9Bg+nZcutnRC6XFtQAZ6fBPg2hvEXxTtn3zDfk89ZUmNGDPHxF N5IJ4MLILOtPOqRf2smHq0uNo2XQb5pVJh2lucvtTlqU5ZAevkaTpa4VDRoulIh40W22 Bm6wbG+dai4sIUXaLj9RILh0G1f1+j+yl13KuQVvdcPOxiyzbo47ospAtUiq3jCvrj/J BoqgSRk6bVaBj6JKB+6ut6fodlj+dsVECmOQoS8JIZava14DfU2lwochPpgHJsoa/eVG g6Sw== X-Gm-Message-State: AMCzsaU4tSR2GlpA5TaKJVEhE7KkKx/qPYq9rftOk7easETcbIBt8CFY 3BZuf1nVS1b7NczYqOd01DxfEGAt X-Google-Smtp-Source: AOwi7QBGGVuJkJBKh5Cad2xG/4xQeFIIisn6uDIuUKUcRFKzTC72EBYM23X2LcGxoRPCGd7xAYrwaQ== X-Received: by 10.84.204.133 with SMTP id b5mr9844239ple.149.1507578765954; Mon, 09 Oct 2017 12:52:45 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.174.81]) by smtp.gmail.com with ESMTPSA id z26sm17260715pfa.49.2017.10.09.12.52.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 09 Oct 2017 12:52:45 -0700 (PDT) From: Eric Biggers To: ecryptfs@vger.kernel.org, Tyler Hicks Cc: keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, Eric Biggers , Michael Halcrow Subject: [PATCH v2 3/3] ecryptfs: move key payload accessor functions into keystore.c Date: Mon, 9 Oct 2017 12:51:29 -0700 Message-Id: <20171009195129.68610-3-ebiggers3@gmail.com> X-Mailer: git-send-email 2.14.2.920.gcf0c67979c-goog In-Reply-To: <20171009195129.68610-1-ebiggers3@gmail.com> References: <20171009195129.68610-1-ebiggers3@gmail.com> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers As experience has shown, accessing the 'struct key' payload is very error-prone, since we need to hold the key semaphore and properly validate everything. Fortunately eCryptfs only does it from one place, in ecryptfs_verify_auth_tok_from_key() in keystore.c. Therefore, move the payload accessor functions like ecryptfs_get_key_payload_data() out of ecryptfs_kernel.h and into keystore.c so that people might be less tempted to use them directly. Reviewed-by: James Morris Cc: Michael Halcrow Signed-off-by: Eric Biggers --- Changed since v1: added Reviewed-by and resent in series with just the ecryptfs patches. Can this please be taken through the ecryptfs tree? fs/ecryptfs/ecryptfs_kernel.h | 60 ------------------------------------------- fs/ecryptfs/keystore.c | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h index 945844d5f0ef..f2e339a6f9e9 100644 --- a/fs/ecryptfs/ecryptfs_kernel.h +++ b/fs/ecryptfs/ecryptfs_kernel.h @@ -29,8 +29,6 @@ #define ECRYPTFS_KERNEL_H #include -#include -#include #include #include #include @@ -80,64 +78,6 @@ struct ecryptfs_page_crypt_context { } param; }; -#if defined(CONFIG_ENCRYPTED_KEYS) || defined(CONFIG_ENCRYPTED_KEYS_MODULE) -static inline struct ecryptfs_auth_tok * -ecryptfs_get_encrypted_key_payload_data(struct key *key) -{ - struct encrypted_key_payload *payload; - - if (key->type != &key_type_encrypted) - return NULL; - - payload = key->payload.data[0]; - if (!payload) - return ERR_PTR(-EKEYREVOKED); - - if (payload->payload_datalen != sizeof(struct ecryptfs_auth_tok)) - return ERR_PTR(-EINVAL); - - return (struct ecryptfs_auth_tok *)payload->payload_data; -} - -static inline struct key *ecryptfs_get_encrypted_key(char *sig) -{ - return request_key(&key_type_encrypted, sig, NULL); -} - -#else -static inline struct ecryptfs_auth_tok * -ecryptfs_get_encrypted_key_payload_data(struct key *key) -{ - return NULL; -} - -static inline struct key *ecryptfs_get_encrypted_key(char *sig) -{ - return ERR_PTR(-ENOKEY); -} - -#endif /* CONFIG_ENCRYPTED_KEYS */ - -static inline struct ecryptfs_auth_tok * -ecryptfs_get_key_payload_data(struct key *key) -{ - struct ecryptfs_auth_tok *auth_tok; - struct user_key_payload *ukp; - - auth_tok = ecryptfs_get_encrypted_key_payload_data(key); - if (auth_tok) - return auth_tok; - - ukp = user_key_payload_locked(key); - if (!ukp) - return ERR_PTR(-EKEYREVOKED); - - if (ukp->datalen != sizeof(struct ecryptfs_auth_tok)) - return ERR_PTR(-EINVAL); - - return (struct ecryptfs_auth_tok *)ukp->data; -} - #define ECRYPTFS_MAX_KEYSET_SIZE 1024 #define ECRYPTFS_MAX_CIPHER_NAME_SIZE 31 #define ECRYPTFS_MAX_NUM_ENC_KEYS 64 diff --git a/fs/ecryptfs/keystore.c b/fs/ecryptfs/keystore.c index 95e20ab67df3..cb801bdcbae2 100644 --- a/fs/ecryptfs/keystore.c +++ b/fs/ecryptfs/keystore.c @@ -27,6 +27,8 @@ #include #include +#include +#include #include #include #include @@ -454,6 +456,64 @@ static int ecryptfs_verify_version(u16 version) return rc; } +#if defined(CONFIG_ENCRYPTED_KEYS) || defined(CONFIG_ENCRYPTED_KEYS_MODULE) +static inline struct ecryptfs_auth_tok * +ecryptfs_get_encrypted_key_payload_data(struct key *key) +{ + struct encrypted_key_payload *payload; + + if (key->type != &key_type_encrypted) + return NULL; + + payload = key->payload.data[0]; + if (!payload) + return ERR_PTR(-EKEYREVOKED); + + if (payload->payload_datalen != sizeof(struct ecryptfs_auth_tok)) + return ERR_PTR(-EINVAL); + + return (struct ecryptfs_auth_tok *)payload->payload_data; +} + +static inline struct key *ecryptfs_get_encrypted_key(char *sig) +{ + return request_key(&key_type_encrypted, sig, NULL); +} + +#else +static inline struct ecryptfs_auth_tok * +ecryptfs_get_encrypted_key_payload_data(struct key *key) +{ + return NULL; +} + +static inline struct key *ecryptfs_get_encrypted_key(char *sig) +{ + return ERR_PTR(-ENOKEY); +} + +#endif /* CONFIG_ENCRYPTED_KEYS */ + +static struct ecryptfs_auth_tok * +ecryptfs_get_key_payload_data(struct key *key) +{ + struct ecryptfs_auth_tok *auth_tok; + struct user_key_payload *ukp; + + auth_tok = ecryptfs_get_encrypted_key_payload_data(key); + if (auth_tok) + return auth_tok; + + ukp = user_key_payload_locked(key); + if (!ukp) + return ERR_PTR(-EKEYREVOKED); + + if (ukp->datalen != sizeof(struct ecryptfs_auth_tok)) + return ERR_PTR(-EINVAL); + + return (struct ecryptfs_auth_tok *)ukp->data; +} + /** * ecryptfs_verify_auth_tok_from_key * @auth_tok_key: key containing the authentication token