From patchwork Thu Sep 28 21:26:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9976771 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 2E65660375 for ; Thu, 28 Sep 2017 21:30:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 204611FF83 for ; Thu, 28 Sep 2017 21:30:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 154392975A; Thu, 28 Sep 2017 21:30:26 +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.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 9FE651FF83 for ; Thu, 28 Sep 2017 21:30:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751918AbdI1VaD (ORCPT ); Thu, 28 Sep 2017 17:30:03 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:35775 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751822AbdI1V3x (ORCPT ); Thu, 28 Sep 2017 17:29:53 -0400 Received: by mail-pf0-f194.google.com with SMTP id i23so2500958pfi.2; Thu, 28 Sep 2017 14:29:53 -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=SXNIYU7PtbmNIWCXQTCtlDfErON7+taSYIPmNs92GV0=; b=nCNuIancpnDeZrHiO97cQU7yG8ScMBBmNPCrhu1WszqbHhy1ghQQzF0oUNwZx+YPrT fmUkkKz+tjOFqcdi1v4Jg8Ff44RFVZTCLeegkLdJ/5V/wtNc1egAtiX6+aMVr0emeBn8 Ixr5jIuiYXdxKcMcdnFqgqSqtGvofj6voyJBHZOrPb2iTkEJRD8MdUwzGh5615MKV8Zd ha2MuAm4UDu//k1d369Vi37C6ltjkIP93HsdvAs3KbCZKs+mrUkiA2aCsm01CDet4SUI RtD9wLCxcd4HkcJZpb+O6YMmrGuIoyVZToQTHnF2n2L0DtgMYD448SbrR2sKHrEzARhD o29Q== 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=SXNIYU7PtbmNIWCXQTCtlDfErON7+taSYIPmNs92GV0=; b=VdGJriYSPCdeFxi4NauY9LhdAmfLyNHJ4sFRoh4lgbEPdcnMdPI0FO3HpXhVzJGEev 7lbv0Px5FMb16knzrEHYz1XOCwOCGsqEoETNTsMaAktCC640Ci6IT9DwfiYTLCP7Mcvi ohrq/5OW1zaZTTNxExstoTBv5jvm+QISs+fkYzrgIZVB25xdzQeK+UZpQ4gc8p8Aoh9t gdDfiXUauLIib/pq6O8vGPaYE+AcqexKaREkD7irAOHW3YL1nM7gz4CJDIlqdzccaJ2P 7pNmgYt1CXWgwuyZDhWUW7X22h2MNFbusdn3sAKJMkAJa88xvQEdWHtHQLG01MpNsimO kP4w== X-Gm-Message-State: AHPjjUhpuwMrLR6vNKd5dD93pfyiuReRfc+kQikCeMHScC9fO8hRUq63 jpiOeEgtg6JJCmrdVdi4absb8Ly1 X-Google-Smtp-Source: AOwi7QBFwBA9Xio2/G3raxRKWlf1Wxs8Jpmw0+Hb0tayKJp0bIWhGQFJS8MdbEz5eWWxrFybOPMpHg== X-Received: by 10.84.217.206 with SMTP id d14mr5153305plj.157.1506634192576; Thu, 28 Sep 2017 14:29:52 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.174.81]) by smtp.gmail.com with ESMTPSA id o128sm3810672pga.5.2017.09.28.14.29.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 28 Sep 2017 14:29:52 -0700 (PDT) From: Eric Biggers To: keyrings@vger.kernel.org Cc: David Howells , Michael Halcrow , linux-cachefs@redhat.com, ecryptfs@vger.kernel.org, linux-fscrypt@vger.kernel.org, linux-security-module@vger.kernel.org, Eric Biggers Subject: [PATCH 7/7] ecryptfs: move key payload accessor functions into keystore.c Date: Thu, 28 Sep 2017 14:26:02 -0700 Message-Id: <20170928212602.41744-8-ebiggers3@gmail.com> X-Mailer: git-send-email 2.14.2.822.g60be5d43e6-goog In-Reply-To: <20170928212602.41744-1-ebiggers3@gmail.com> References: <20170928212602.41744-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. Signed-off-by: Eric Biggers Reviewed-by: James Morris --- 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