Driver and fscrypt support for HW-wrapped inline encryption keys
Subsystems (10)
Eric Biggers
maintainerv12 cover Driver and fscrypt support for HW-wrapped inline encryption keys
This patchset is based on linux-block/for-next and is also available at:
git fetch https://git.kernel.org/pub/scm/fs/fscrypt/linux.git wrapped-keys-v12
Now that the block layer support for hardware-wrapped inline encryption keys has been applied for 6.15
(https://lore.kernel.org/r/[email protected]),
this series refreshes the remaining patches. They add the support for hardware-wrapped inline encryption keys to the Qualcomm ICE and UFS drivers and to fscrypt. All tested on SM8650 with xfstests.
TBD whether these will land in 6.15 too, or wait until 6.16 when the block patches that patches 2-4 depend on will have landed.
Changed in v12:
- Rebased onto linux-block/for-next
- Fixed endianness error in drivers/soc/qcom/ice.c
- Added Acked-bys
- Updated the fscrypt patch to go back to having just FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED (as in v8) instead of FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED_V0 and FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED_V1. Upon further discussion it seemed the partial V0 compatibility was not going to be as helpful as I had hoped, so instead we'll just have the single new version that does things properly. Note, I've updated my wip-wrapped-keys branch of fscryptctl accordingly.
Changed in v11:
- Rebased onto v6.14-rc1. Dropped the patches that were upstreamed in 6.14, and put the block patches first in the series again.
- Significantly cleaned up the patch "soc: qcom: ice: add HWKM support to the ICE driver". Some of the notable changes were dropping the unnecessary support for HWKM v1, and replacing qcom_ice_using_hwkm() with qcom_ice_get_supported_key_type().
- Consistently used and documented the EBADMSG error code for invalid hardware-wrapped keys.
- Other minor cleanups.
Changed in v10:
- Fixed bugs in qcom_scm_derive_sw_secret() and cqhci_crypto_init().
- Added "ufs: qcom: fix crypto key eviction" and
"mmc: sdhci-msm: fix crypto key eviction".
- Split removing ufs_hba_variant_ops::program_key into its own patch.
- Minor cleanups.
- Added Tested-by.
Changed in v9 (relative to v7 patchset from Bartosz Golaszewski):
- ufs-qcom and sdhci-msm now just initialize the blk_crypto_profile themselves, like what ufs-exynos was doing. This avoids needing to add all the host-specific hooks for wrapped key support to the MMC and UFS core drivers.
- When passing the blk_crypto_key further down the stack, it now replaces parameters like the algorithm ID, to avoid creating two sources of truth.
- The module parameter qcom_ice.use_wrapped_keys should work correctly now.
- The fscrypt support no longer uses a policy flag to indicate when a file is protected by a HW-wrapped key, since it was already implied by the file's key identifier being that of a HW-wrapped key. Originally there was an issue where raw and HW-wrapped keys could share key identifiers, but I had fixed that earlier by introducing a new HKDF context byte.
- The term "standard keys" is no longer used. Now "raw keys" is consistently used instead. I've found that people find the term "raw keys" to be more intuitive. Also HW-wrapped keys could in principle be standardized.
- I've reordered the patchset to place preparatory patches that don't depend on the actual HW-wrapped key support first.
For older changelogs, see
https://lore.kernel.org/r/[email protected] and
https://lore.kernel.org/r/[email protected]
Eric Biggers (3):
soc: qcom: ice: make qcom_ice_program_key() take struct blk_crypto_key
ufs: qcom: add support for wrapped keys
fscrypt: add support for hardware-wrapped keys
Gaurav Kashyap (1):
soc: qcom: ice: add HWKM support to the ICE driver
Documentation/filesystems/fscrypt.rst | 187 +++++++++++--- drivers/mmc/host/sdhci-msm.c | 16 +- drivers/soc/qcom/ice.c | 350 ++++++++++++++++++++++++-- drivers/ufs/host/ufs-qcom.c | 57 ++++- fs/crypto/fscrypt_private.h | 75 +++++- fs/crypto/hkdf.c | 4 +- fs/crypto/inline_crypt.c | 44 +++- fs/crypto/keyring.c | 138 +++++++--- fs/crypto/keysetup.c | 63 ++++- fs/crypto/keysetup_v1.c | 4 +- include/soc/qcom/ice.h | 34 ++- include/uapi/linux/fscrypt.h | 6 +- 12 files changed, 809 insertions(+), 169 deletions(-)
base-commit: 352245090aa60dbaa11b4f7da18f31caf42aeb82
Signature · 1 line
2.48.1
Eric Biggers
maintainerPATCH v12 1/4 soc: qcom: ice: make qcom_ice_program_key() take struct blk_crypto_key
From: Eric Biggers <[email protected]>
qcom_ice_program_key() currently accepts the key as an array of bytes, algorithm ID, key size enum, and data unit size. However both callers have a struct blk_crypto_key which contains all that information. Thus they both have similar code that converts the blk_crypto_key into the form that qcom_ice_program_key() wants. Once wrapped key support is added, the key type would need to be added to the arguments too.
Therefore, this patch changes qcom_ice_program_key() to take in all this information as a struct blk_crypto_key directly. The calling code is updated accordingly. This ends up being much simpler, and it makes the key type be passed down automatically once wrapped key support is added.
Based on a patch by Gaurav Kashyap <[email protected]> that replaced the byte array argument only. This patch makes the blk_crypto_key replace other arguments like the algorithm ID too, ensuring that there remains only one source of truth.
--- drivers/mmc/host/sdhci-msm.c | 11 +---------- drivers/soc/qcom/ice.c | 23 ++++++++++++----------- drivers/ufs/host/ufs-qcom.c | 11 +---------- include/soc/qcom/ice.h | 22 +++------------------- 4 files changed, 17 insertions(+), 50 deletions(-)
drivers/mmc/host/sdhci-msm.c +1 -10
@@ -1960,20 +1960,11 @@ static int sdhci_msm_ice_keyslot_program(struct blk_crypto_profile *profile, unsigned int slot) {struct sdhci_msm_host *msm_host =sdhci_msm_host_from_crypto_profile (profile); -/* Only AES-256-XTS has been tested so far. */ -if (key->crypto_cfg.crypto_mode !=BLK_ENCRYPTION_MODE_AES_256_XTS ) -return -EOPNOTSUPP ; - -return qcom_ice_program_key (msm_host->ice, -QCOM_ICE_CRYPTO_ALG_AES_XTS , -QCOM_ICE_CRYPTO_KEY_SIZE_256 , - key->bytes, - key->crypto_cfg.data_unit_size /512 , - slot); +return qcom_ice_program_key (msm_host->ice, slot, key); }static int sdhci_msm_ice_keyslot_evict (struct blk_crypto_profile *profile,const struct blk_crypto_key *key,unsigned int slot)
drivers/soc/qcom/ice.c +12 -11
@@ -159,41 +159,42 @@ int qcom_ice_suspend(struct qcom_ice *ice)return 0 ; }EXPORT_SYMBOL_GPL (qcom_ice_suspend); -int qcom_ice_program_key (struct qcom_ice *ice, -u8 algorithm_id,u8 key_size, -const u8 crypto_key[],u8 data_unit_size, -int slot) +int qcom_ice_program_key (struct qcom_ice *ice,unsigned int slot, +const struct blk_crypto_key *blk_key) {struct device *dev = ice->dev;union {u8 bytes[AES_256_XTS_KEY_SIZE ];u32 words[AES_256_XTS_KEY_SIZE /sizeof (u32)]; } key;int i;int err;/* Only AES-256-XTS has been tested so far. */ -if (algorithm_id !=QCOM_ICE_CRYPTO_ALG_AES_XTS || - key_size !=QCOM_ICE_CRYPTO_KEY_SIZE_256 ) { -dev_err_ratelimited (dev, -"Unhandled crypto capability; algorithm_id=%d, key_size=%d\n" , - algorithm_id, key_size); +if (blk_key->crypto_cfg.crypto_mode != +BLK_ENCRYPTION_MODE_AES_256_XTS ) { +dev_err_ratelimited (dev,"Unsupported crypto mode: %d\n" , + blk_key->crypto_cfg.crypto_mode);return -EINVAL ; } -memcpy (key.bytes, crypto_key,AES_256_XTS_KEY_SIZE ); +if (blk_key->size !=AES_256_XTS_KEY_SIZE ) { +dev_err_ratelimited (dev,"Incorrect key size\n" ); +return -EINVAL ; + } +memcpy (key.bytes, blk_key->bytes,AES_256_XTS_KEY_SIZE );/* The SCM call requires that the key words are encoded in big endian */ for (i =0 ; i <ARRAY_SIZE (key.words); i++)__cpu_to_be32s (&key.words[i]); err =qcom_scm_ice_set_key (slot, key.bytes,AES_256_XTS_KEY_SIZE ,QCOM_SCM_ICE_CIPHER_AES_256_XTS , - data_unit_size); + blk_key->crypto_cfg.data_unit_size /512 );memzero_explicit (&key,sizeof (key));return err; }
drivers/ufs/host/ufs-qcom.c +1 -10
@@ -193,21 +193,12 @@ static int ufs_qcom_ice_keyslot_program(struct blk_crypto_profile *profile, {struct ufs_hba *hba =ufs_hba_from_crypto_profile (profile);struct ufs_qcom_host *host =ufshcd_get_variant (hba);int err; -/* Only AES-256-XTS has been tested so far. */ -if (key->crypto_cfg.crypto_mode !=BLK_ENCRYPTION_MODE_AES_256_XTS ) -return -EOPNOTSUPP ; -ufshcd_hold (hba); - err =qcom_ice_program_key (host->ice, -QCOM_ICE_CRYPTO_ALG_AES_XTS , -QCOM_ICE_CRYPTO_KEY_SIZE_256 , - key->bytes, - key->crypto_cfg.data_unit_size /512 , - slot); + err =qcom_ice_program_key (host->ice, slot, key);ufshcd_release (hba);return err; } static int ufs_qcom_ice_keyslot_evict(struct blk_crypto_profile *profile,
include/soc/qcom/ice.h +3 -19
@@ -4,34 +4,18 @@*/ #ifndef __QCOM_ICE_H__#define __QCOM_ICE_H__ +#include <linux/blk-crypto.h> #include <linux/types.h> struct qcom_ice ; -enum qcom_ice_crypto_key_size { -QCOM_ICE_CRYPTO_KEY_SIZE_INVALID =0x0 , -QCOM_ICE_CRYPTO_KEY_SIZE_128 =0x1 , -QCOM_ICE_CRYPTO_KEY_SIZE_192 =0x2 , -QCOM_ICE_CRYPTO_KEY_SIZE_256 =0x3 , -QCOM_ICE_CRYPTO_KEY_SIZE_512 =0x4 , -}; - -enum qcom_ice_crypto_alg { -QCOM_ICE_CRYPTO_ALG_AES_XTS =0x0 , -QCOM_ICE_CRYPTO_ALG_BITLOCKER_AES_CBC =0x1 , -QCOM_ICE_CRYPTO_ALG_AES_ECB =0x2 , -QCOM_ICE_CRYPTO_ALG_ESSIV_AES_CBC =0x3 , -}; -int qcom_ice_enable (struct qcom_ice *ice);int qcom_ice_resume (struct qcom_ice *ice);int qcom_ice_suspend (struct qcom_ice *ice); -int qcom_ice_program_key (struct qcom_ice *ice, -u8 algorithm_id,u8 key_size, -const u8 crypto_key[],u8 data_unit_size, -int slot); +int qcom_ice_program_key (struct qcom_ice *ice,unsigned int slot, +const struct blk_crypto_key *blk_key);int qcom_ice_evict_key (struct qcom_ice *ice,int slot);struct qcom_ice *of_qcom_ice_get (struct device *dev);#endif /* __QCOM_ICE_H__ */
Signature · 1 line
2.48.1
Eric Biggers
maintainerPATCH v12 2/4 soc: qcom: ice: add HWKM support to the ICE driver
In reply to Eric BiggersFrom: Gaurav Kashyap <[email protected]>
Qualcomm's Inline Crypto Engine (ICE) version 3.2 and later includes a key management hardware block called the Hardware Key Manager (HWKM). Add support for HWKM to the ICE driver. HWKM provides hardware-wrapped key support where the ICE (storage) keys are not exposed to software and instead are protected in hardware. Later patches will wire up this feature to ufs-qcom and sdhci-msm using the support added in this patch.
HWKM and legacy mode are currently mutually exclusive. The selection of which mode to use has to be made before the storage driver(s) registers any inline encryption capable disk(s) with the block layer (i.e., generally at boot time) so that the appropriate crypto capabilities can be advertised to upper layers. Therefore, make the ICE driver select HWKM mode when the all of the following are true:
- The new module parameter qcom_ice.use_wrapped_keys=1 is specified.
- HWKM is present and is at least v2, i.e. ICE is v3.2.1 or later.
- The SCM calls needed to fully use HWKM are supported by TrustZone.
[EB: merged related patches; fixed the module parameter to work
correctly; dropped unnecessary support for HWKM v1; fixed error handling; improved log messages, comments, and commit message; fixed naming; merged enable and init functions; and other cleanups]
--- drivers/mmc/host/sdhci-msm.c | 5 + drivers/soc/qcom/ice.c | 327 +++++++++++++++++++++++++++++++++-- drivers/ufs/host/ufs-qcom.c | 5 + include/soc/qcom/ice.h | 12 ++ 4 files changed, 339 insertions(+), 10 deletions(-)
drivers/mmc/host/sdhci-msm.c +5 -0
@@ -1880,10 +1880,15 @@ static int sdhci_msm_ice_init(struct sdhci_msm_host *msm_host, }if (IS_ERR_OR_NULL (ice))return PTR_ERR_OR_ZERO (ice); +if (qcom_ice_get_supported_key_type (ice) !=BLK_CRYPTO_KEY_TYPE_RAW ) { +dev_warn (dev,"Wrapped keys not supported. Disabling inline encryption support.\n" ); +return 0 ; + } + msm_host->ice = ice;/* Initialize the blk_crypto_profile */ caps.reg_val =cpu_to_le32 (cqhci_readl (cq_host,CQHCI_CCAP ));
drivers/soc/qcom/ice.c +317 -10
431 lines not on this page · raw message
drivers/ufs/host/ufs-qcom.c +5 -0
@@ -132,10 +132,15 @@ static int ufs_qcom_ice_init(struct ufs_qcom_host *host) }if (IS_ERR_OR_NULL (ice))return PTR_ERR_OR_ZERO (ice); +if (qcom_ice_get_supported_key_type (ice) !=BLK_CRYPTO_KEY_TYPE_RAW ) { +dev_warn (dev,"Wrapped keys not supported. Disabling inline encryption support.\n" ); +return 0 ; + } + host->ice = ice;/* Initialize the blk_crypto_profile */ caps.reg_val =cpu_to_le32 (ufshcd_readl (hba,REG_UFS_CCAP ));
include/soc/qcom/ice.h +12 -0
@@ -15,7 +15,19 @@ int qcom_ice_enable(struct qcom_ice *ice);int qcom_ice_resume (struct qcom_ice *ice);int qcom_ice_suspend (struct qcom_ice *ice);int qcom_ice_program_key (struct qcom_ice *ice,unsigned int slot,const struct blk_crypto_key *blk_key);int qcom_ice_evict_key (struct qcom_ice *ice,int slot); +enum blk_crypto_key_type qcom_ice_get_supported_key_type (struct qcom_ice *ice); +int qcom_ice_derive_sw_secret (struct qcom_ice *ice, +const u8 *eph_key,size_t eph_key_size, +u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE ]); +int qcom_ice_generate_key (struct qcom_ice *ice, +u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE ]); +int qcom_ice_prepare_key (struct qcom_ice *ice, +const u8 *lt_key,size_t lt_key_size, +u8 eph_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE ]); +int qcom_ice_import_key (struct qcom_ice *ice, +const u8 *raw_key,size_t raw_key_size, +u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE ]);struct qcom_ice *of_qcom_ice_get (struct device *dev);#endif /* __QCOM_ICE_H__ */
Signature · 1 line
2.48.1
Eric Biggers
maintainerPATCH v12 3/4 ufs: qcom: add support for wrapped keys
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Wire up the wrapped key support for ufs-qcom by implementing the needed methods in struct blk_crypto_ll_ops and setting the appropriate flag in blk_crypto_profile::key_types_supported.
For more information about this feature and how to use it, refer to the sections about hardware-wrapped keys in Documentation/block/inline-encryption.rst and Documentation/filesystems/fscrypt.rst.
Based on patches by Gaurav Kashyap <[email protected]>. Reworked to use the custom crypto profile support.
--- drivers/ufs/host/ufs-qcom.c | 51 ++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-)
drivers/ufs/host/ufs-qcom.c +45 -6
@@ -132,15 +132,10 @@ static int ufs_qcom_ice_init(struct ufs_qcom_host *host) }if (IS_ERR_OR_NULL (ice))return PTR_ERR_OR_ZERO (ice); -if (qcom_ice_get_supported_key_type (ice) !=BLK_CRYPTO_KEY_TYPE_RAW ) { -dev_warn (dev,"Wrapped keys not supported. Disabling inline encryption support.\n" ); -return 0 ; - } - host->ice = ice;/* Initialize the blk_crypto_profile */ caps.reg_val =cpu_to_le32 (ufshcd_readl (hba,REG_UFS_CCAP ));
@@ -150,11 +145,11 @@ static int ufs_qcom_ice_init(struct ufs_qcom_host *host)if (err)return err; profile->ll_ops = ufs_qcom_crypto_ops; profile->max_dun_bytes_supported =8 ; - profile->key_types_supported =BLK_CRYPTO_KEY_TYPE_RAW ; + profile->key_types_supported =qcom_ice_get_supported_key_type (ice); profile->dev = dev;/* * Currently this driver only supports AES-256-XTS. All known versions * of ICE support it, but to be safe make sure it is really declared in
@@ -218,13 +213,57 @@ static int ufs_qcom_ice_keyslot_evict(struct blk_crypto_profile *profile, err =qcom_ice_evict_key (host->ice, slot);ufshcd_release (hba);return err; } +static int ufs_qcom_ice_derive_sw_secret (struct blk_crypto_profile *profile, +const u8 *eph_key,size_t eph_key_size, +u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE ]) +{ +struct ufs_hba *hba =ufs_hba_from_crypto_profile (profile); +struct ufs_qcom_host *host =ufshcd_get_variant (hba); + +return qcom_ice_derive_sw_secret (host->ice, eph_key, eph_key_size, + sw_secret); +} + +static int ufs_qcom_ice_import_key (struct blk_crypto_profile *profile, +const u8 *raw_key,size_t raw_key_size, +u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE ]) +{ +struct ufs_hba *hba =ufs_hba_from_crypto_profile (profile); +struct ufs_qcom_host *host =ufshcd_get_variant (hba); + +return qcom_ice_import_key (host->ice, raw_key, raw_key_size, lt_key); +} + +static int ufs_qcom_ice_generate_key (struct blk_crypto_profile *profile, +u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE ]) +{ +struct ufs_hba *hba =ufs_hba_from_crypto_profile (profile); +struct ufs_qcom_host *host =ufshcd_get_variant (hba); + +return qcom_ice_generate_key (host->ice, lt_key); +} + +static int ufs_qcom_ice_prepare_key (struct blk_crypto_profile *profile, +const u8 *lt_key,size_t lt_key_size, +u8 eph_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE ]) +{ +struct ufs_hba *hba =ufs_hba_from_crypto_profile (profile); +struct ufs_qcom_host *host =ufshcd_get_variant (hba); + +return qcom_ice_prepare_key (host->ice, lt_key, lt_key_size, eph_key); +} +static const struct blk_crypto_ll_ops ufs_qcom_crypto_ops = { .keyslot_program = ufs_qcom_ice_keyslot_program, .keyslot_evict = ufs_qcom_ice_keyslot_evict, + .derive_sw_secret = ufs_qcom_ice_derive_sw_secret, + .import_key = ufs_qcom_ice_import_key, + .generate_key = ufs_qcom_ice_generate_key, + .prepare_key = ufs_qcom_ice_prepare_key, };#else static inline void ufs_qcom_ice_enable (struct ufs_qcom_host *host)
Signature · 1 line
2.48.1
Eric Biggers
maintainerPATCH v12 4/4 fscrypt: add support for hardware-wrapped keys
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add support for hardware-wrapped keys to fscrypt. Such keys are protected from certain attacks, such as cold boot attacks. For more information, see the "Hardware-wrapped keys" section of Documentation/block/inline-encryption.rst.
To support hardware-wrapped keys in fscrypt, we allow the fscrypt master keys to be hardware-wrapped. File contents encryption is done by passing the wrapped key to the inline encryption hardware via blk-crypto. Other fscrypt operations such as filenames encryption continue to be done by the kernel, using the "software secret" which the hardware derives. For more information, see the documentation which this patch adds to Documentation/filesystems/fscrypt.rst.
Note that this feature doesn't require any filesystem-specific changes. However it does depend on inline encryption support, and thus currently it is only applicable to ext4 and f2fs.
The version of this feature introduced by this patch is mostly equivalent to the version that has existed downstream in the Android Common Kernels since 2020. However, a couple fixes are included. First, the flags field in struct fscrypt_add_key_arg is now placed in the proper location. Second, key identifiers for HW-wrapped keys are now derived using a distinct HKDF context byte; this fixes a bug where a raw key could have the same identifier as a HW-wrapped key. Note that as a result of these fixes, the version of this feature introduced by this patch is not UAPI or on-disk format compatible with the version in the Android Common Kernels, though the divergence is limited to just those specific fixes. This version should be used going forwards.
This patch has been heavily rewritten from the original version by Gaurav Kashyap <[email protected]> and
Barani Muthukumaran <[email protected]>.
--- Documentation/filesystems/fscrypt.rst | 187 ++++++++++++++++++++------ fs/crypto/fscrypt_private.h | 75 +++++++++-- fs/crypto/hkdf.c | 4 +- fs/crypto/inline_crypt.c | 44 +++++- fs/crypto/keyring.c | 138 +++++++++++++------ fs/crypto/keysetup.c | 63 +++++++-- fs/crypto/keysetup_v1.c | 4 +- include/uapi/linux/fscrypt.h | 6 +- 8 files changed, 413 insertions(+), 108 deletions(-)
Documentation/filesystems/fscrypt.rst +149 -38
@@ -68,11 +68,11 @@ an authorized user later accessing the filesystem. Online attacks -------------- fscrypt (and storage encryption in general) can only provide limited -protection, if any at all, against online attacks. In detail: +protection against online attacks. In detail: Side-channel attacks ~~~~~~~~~~~~~~~~~~~~ fscrypt is only resistant to side-channel attacks, such as timing or
@@ -97,20 +97,27 @@ system itself, is *not* protected by the mathematical properties of encryption but rather only by the correctness of the kernel. Therefore, any encryption-specific access control checks would merely be enforced by kernel *code* and therefore would be largely redundant with the wide variety of access control mechanisms already available.) -Kernel memory compromise -~~~~~~~~~~~~~~~~~~~~~~~~ +Read-only kernel memory compromise +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Unless `hardware-wrapped keys`_ are used, an attacker who gains the +ability to read from arbitrary kernel memory, e.g. by mounting a +physical attack or by exploiting a kernel security vulnerability, can +compromise all fscrypt keys that are currently in-use. This also +extends to cold boot attacks; if the system is suddenly powered off, +keys the system was using may remain in memory for a short time. -An attacker who compromises the system enough to read from arbitrary -memory, e.g. by mounting a physical attack or by exploiting a kernel -security vulnerability, can compromise all encryption keys that are -currently in use. +However, if hardware-wrapped keys are used, then the fscrypt master +keys and file contents encryption keys (but not other types of fscrypt +subkeys such as filenames encryption keys) are protected from +compromises of arbitrary kernel memory. -However, fscrypt allows encryption keys to be removed from the kernel, -which may protect them from later compromise. +In addition, fscrypt allows encryption keys to be removed from the +kernel, which may protect them from later compromise. In more detail, the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl (or the FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS ioctl) can wipe a master encryption key from kernel memory. If it does so, it will also try to evict all cached inodes which had been "unlocked" using the key,
@@ -143,10 +150,28 @@ However, these ioctls have some limitations: - Secret keys might still exist in CPU registers, in crypto accelerator hardware (if used by the crypto API to implement any of the algorithms), or in other places not explicitly considered here. +Full system compromise +~~~~~~~~~~~~~~~~~~~~~~ + +An attacker who gains "root" access and/or the ability to execute +arbitrary kernel code can freely exfiltrate data that is protected by +any in-use fscrypt keys. Thus, usually fscrypt provides no meaningful +protection in this scenario. (Data that is protected by a key that is +absent throughout the entire attack remains protected, modulo the +limitations of key removal mentioned above in the case where the key +was removed prior to the attack.) + +However, if `hardware-wrapped keys`_ are used, such attackers will be +unable to exfiltrate the master keys or file contents keys in a form +that will be usable after the system is powered off. This may be +useful if the attacker is significantly time-limited and/or +bandwidth-limited, so they can only exfiltrate some data and need to +rely on a later offline attack to exfiltrate the rest of it. + Limitations of v1 policies ~~~~~~~~~~~~~~~~~~~~~~~~~~ v1 encryption policies have some weaknesses with respect to online attacks:
@@ -169,10 +194,14 @@ this reason among others, it is recommended to use v2 encryption policies on all new encrypted directories. Key hierarchy ============= +Note: this section assumes the use of raw keys rather than +hardware-wrapped keys. The use of hardware-wrapped keys modifies the +key hierarchy slightly. For details, see `Hardware-wrapped keys`_. + Master Keys ----------- Each encrypted directory tree is protected by a *master key*. Master keys can be up to 64 bytes long, and must be at least as long as the
@@ -834,11 +863,13 @@ a pointer to struct fscrypt_add_key_arg, defined as follows:: struct fscrypt_add_key_arg { struct fscrypt_key_specifier key_spec; __u32 raw_size; __u32 key_id; - __u32 __reserved[8]; + #define FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED 0x00000001 + __u32 flags; + __u32 __reserved[7]; __u8 raw[]; }; #define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1 #define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2
@@ -853,11 +884,11 @@ a pointer to struct fscrypt_add_key_arg, defined as follows:: } u; }; struct fscrypt_provisioning_key_payload { __u32 type; - __u32 __reserved; + __u32 flags; __u8 raw[]; }; struct fscrypt_add_key_arg must be zeroed, then initialized as follows:
@@ -881,28 +912,36 @@ as follows: - ``raw_size`` must be the size of the ``raw`` key provided, in bytes. Alternatively, if ``key_id`` is nonzero, this field must be 0, since in that case the size is implied by the specified Linux keyring key. -- ``key_id`` is 0 if the raw key is given directly in the ``raw`` - field. Otherwise ``key_id`` is the ID of a Linux keyring key of - type "fscrypt-provisioning" whose payload is - struct fscrypt_provisioning_key_payload whose ``raw`` field contains - the raw key and whose ``type`` field matches ``key_spec.type``. - Since ``raw`` is variable-length, the total size of this key's - payload must be ``sizeof(struct fscrypt_provisioning_key_payload)`` - plus the raw key size. The process must have Search permission on - this key. - - Most users should leave this 0 and specify the raw key directly. - The support for specifying a Linux keyring key is intended mainly to +- ``key_id`` is 0 if the key is given directly in the ``raw`` field. + Otherwise ``key_id`` is the ID of a Linux keyring key of type + "fscrypt-provisioning" whose payload is struct + fscrypt_provisioning_key_payload whose ``raw`` field contains the + key, whose ``type`` field matches ``key_spec.type``, and whose + ``flags`` field matches ``flags``. Since ``raw`` is + variable-length, the total size of this key's payload must be + ``sizeof(struct fscrypt_provisioning_key_payload)`` plus the number + of key bytes. The process must have Search permission on this key. + + Most users should leave this 0 and specify the key directly. The + support for specifying a Linux keyring key is intended mainly to allow re-adding keys after a filesystem is unmounted and re-mounted, - without having to store the raw keys in userspace memory. + without having to store the keys in userspace memory. + +- ``flags`` contains optional flags from ``<linux/fscrypt.h>``: + + - FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED: This denotes that the key is a + hardware-wrapped key. See `Hardware-wrapped keys`_. This flag + can't be used if FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR is used. - ``raw`` is a variable-length field which must contain the actual key, ``raw_size`` bytes long. Alternatively, if ``key_id`` is - nonzero, then this field is unused. + nonzero, then this field is unused. Note that despite being named + ``raw``, if FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED is specified then it + will contain a wrapped key, not a raw key. For v2 policy keys, the kernel keeps track of which user (identified by effective user ID) added the key, and only allows the key to be removed by that user --- or by "root", if they use `FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS`_.
@@ -910,34 +949,37 @@ removed by that user --- or by "root", if they use However, if another user has added the key, it may be desirable to prevent that other user from unexpectedly removing it. Therefore, FS_IOC_ADD_ENCRYPTION_KEY may also be used to add a v2 policy key *again*, even if it's already added by other user(s). In this case, FS_IOC_ADD_ENCRYPTION_KEY will just install a claim to the key for the -current user, rather than actually add the key again (but the raw key -must still be provided, as a proof of knowledge). +current user, rather than actually add the key again (but the key must +still be provided, as a proof of knowledge). FS_IOC_ADD_ENCRYPTION_KEY returns 0 if either the key or a claim to the key was either added or already exists. FS_IOC_ADD_ENCRYPTION_KEY can fail with the following errors: - ``EACCES``: FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR was specified, but the caller does not have the CAP_SYS_ADMIN capability in the initial - user namespace; or the raw key was specified by Linux key ID but the + user namespace; or the key was specified by Linux key ID but the process lacks Search permission on the key. +- ``EBADMSG``: invalid hardware-wrapped key - ``EDQUOT``: the key quota for this user would be exceeded by adding the key - ``EINVAL``: invalid key size or key specifier type, or reserved bits were set -- ``EKEYREJECTED``: the raw key was specified by Linux key ID, but the - key has the wrong type -- ``ENOKEY``: the raw key was specified by Linux key ID, but no key - exists with that ID +- ``EKEYREJECTED``: the key was specified by Linux key ID, but the key + has the wrong type +- ``ENOKEY``: the key was specified by Linux key ID, but no key exists + with that ID - ``ENOTTY``: this type of filesystem does not implement encryption - ``EOPNOTSUPP``: the kernel was not configured with encryption support for this filesystem, or the filesystem superblock has not - had encryption enabled on it + had encryption enabled on it; or a hardware wrapped key was specified + but the filesystem does not support inline encryption or the hardware + does not support hardware-wrapped keys Legacy method ~~~~~~~~~~~~~ For v1 encryption policies, a master encryption key can also be
@@ -996,13 +1038,12 @@ These two ioctls differ only in cases where v2 policy keys are added or removed by non-root users. These ioctls don't work on keys that were added via the legacy process-subscribed keyrings mechanism. -Before using these ioctls, read the `Kernel memory compromise`_ -section for a discussion of the security goals and limitations of -these ioctls. +Before using these ioctls, read the `Online attacks`_ section for a +discussion of the security goals and limitations of these ioctls. FS_IOC_REMOVE_ENCRYPTION_KEY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The FS_IOC_REMOVE_ENCRYPTION_KEY ioctl removes a claim to a master
@@ -1318,19 +1359,89 @@ encryption when possible; it doesn't force its use. fscrypt will still fall back to using the kernel crypto API on files where the inline encryption hardware doesn't have the needed crypto capabilities (e.g. support for the needed encryption algorithm and data unit size) and where blk-crypto-fallback is unusable. (For blk-crypto-fallback to be usable, it must be enabled in the kernel configuration with -CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK=y.) +CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK=y, and the file must be +protected by a raw key rather than a hardware-wrapped key.) Currently fscrypt always uses the filesystem block size (which is usually 4096 bytes) as the data unit size. Therefore, it can only use inline encryption hardware that supports that data unit size. Inline encryption doesn't affect the ciphertext or other aspects of the on-disk format, so users may freely switch back and forth between -using "inlinecrypt" and not using "inlinecrypt". +using "inlinecrypt" and not using "inlinecrypt". An exception is that +files that are protected by a hardware-wrapped key can only be +encrypted/decrypted by the inline encryption hardware and therefore +can only be accessed when the "inlinecrypt" mount option is used. For +more information about hardware-wrapped keys, see below. + +Hardware-wrapped keys +--------------------- + +fscrypt supports using *hardware-wrapped keys* when the inline +encryption hardware supports it. Such keys are only present in kernel +memory in wrapped (encrypted) form; they can only be unwrapped +(decrypted) by the inline encryption hardware and are temporally bound +to the current boot. This prevents the keys from being compromised if +kernel memory is leaked. This is done without limiting the number of +keys that can be used and while still allowing the execution of +cryptographic tasks that are tied to the same key but can't use inline +encryption hardware, e.g. filenames encryption. + +Note that hardware-wrapped keys aren't specific to fscrypt; they are a +block layer feature (part of *blk-crypto*). For more details about +hardware-wrapped keys, see the block layer documentation at +:ref:`Documentation/block/inline-encryption.rst +<hardware_wrapped_keys>`. The rest of this section just focuses on +the details of how fscrypt can use hardware-wrapped keys. + +fscrypt supports hardware-wrapped keys by allowing the fscrypt master +keys to be hardware-wrapped keys as an alternative to raw keys. To +add a hardware-wrapped key with `FS_IOC_ADD_ENCRYPTION_KEY`_, +userspace must specify FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED in the +``flags`` field of struct fscrypt_add_key_arg and also in the +``flags`` field of struct fscrypt_provisioning_key_payload when +applicable. The key must be in ephemerally-wrapped form, not +long-term wrapped form. + +Some limitations apply. First, files protected by a hardware-wrapped +key are tied to the system's inline encryption hardware. Therefore +they can only be accessed when the "inlinecrypt" mount option is used, +and they can't be included in portable filesystem images. Second, +currently the hardware-wrapped key support is only compatible with +`IV_INO_LBLK_64 policies`_ and `IV_INO_LBLK_32 policies`_, as it +assumes that there is just one file contents encryption key per +fscrypt master key rather than one per file. Future work may address +this limitation by passing per-file nonces down the storage stack to +allow the hardware to derive per-file keys. + +Implementation-wise, to encrypt/decrypt the contents of files that are +protected by a hardware-wrapped key, fscrypt uses blk-crypto, +attaching the hardware-wrapped key to the bio crypt contexts. As is +the case with raw keys, the block layer will program the key into a +keyslot when it isn't already in one. However, when programming a +hardware-wrapped key, the hardware doesn't program the given key +directly into a keyslot but rather unwraps it (using the hardware's +ephemeral wrapping key) and derives the inline encryption key from it. +The inline encryption key is the key that actually gets programmed +into a keyslot, and it is never exposed to software. + +However, fscrypt doesn't just do file contents encryption; it also +uses its master keys to derive filenames encryption keys, key +identifiers, and sometimes some more obscure types of subkeys such as +dirhash keys. So even with file contents encryption out of the +picture, fscrypt still needs a raw key to work with. To get such a +key from a hardware-wrapped key, fscrypt asks the inline encryption +hardware to derive a cryptographically isolated "software secret" from +the hardware-wrapped key. fscrypt uses this "software secret" to key +its KDF to derive all subkeys other than file contents keys. + +Note that this implies that the hardware-wrapped key feature only +protects the file contents encryption keys. It doesn't protect other +fscrypt subkeys such as filenames encryption keys. Direct I/O support ================== For direct I/O on an encrypted file to work, the following conditions
fs/crypto/fscrypt_private.h +65 -10
182 lines not on this page · raw message
fs/crypto/hkdf.c +3 -1
@@ -2,11 +2,13 @@/* * Implementation of HKDF ("HMAC-based Extract-and-Expand Key Derivation * Function"), aka RFC 5869. See also the original paper (Krawczyk 2010): * "Cryptographic Extraction and Key Derivation: The HKDF Scheme". * -* This is used to derive keys from the fscrypt master keys. +* This is used to derive keys from the fscrypt master keys (or from the +* "software secrets" which hardware derives from the fscrypt master keys, in +* the case that the fscrypt master keys are hardware-wrapped keys). * * Copyright 2019 Google LLC */ #include <crypto/hash.h>
fs/crypto/inline_crypt.c +38 -6
99 lines not on this page · raw message
fs/crypto/keyring.c +97 -41
272 lines not on this page · raw message
fs/crypto/keysetup.c +55 -8
152 lines not on this page · raw message
fs/crypto/keysetup_v1.c +2 -2
@@ -116,11 +116,11 @@ find_and_lock_process_key(const char *prefix, goto invalid; payload = (const struct fscrypt_key *)ukp->data;if (ukp->datalen !=sizeof (struct fscrypt_key ) || - payload->size <1 || payload->size >FSCRYPT_MAX_KEY_SIZE ) { + payload->size <1 || payload->size >sizeof (payload->raw)) {fscrypt_warn (NULL ,"key with description '%s' has invalid payload" , key->description); goto invalid; }
@@ -147,11 +147,11 @@ struct fscrypt_direct_key {struct hlist_node dk_node;refcount_t dk_refcount;const struct fscrypt_mode *dk_mode;struct fscrypt_prepared_key dk_key;u8 dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; -u8 dk_raw[FSCRYPT_MAX_KEY_SIZE ]; +u8 dk_raw[FSCRYPT_MAX_RAW_KEY_SIZE ]; };static void free_direct_key (struct fscrypt_direct_key *dk) {if (dk) {
include/uapi/linux/fscrypt.h +4 -2
@@ -117,20 +117,22 @@ struct fscrypt_key_specifier {* Payload of Linux keyring key of type "fscrypt-provisioning", referenced by * fscrypt_add_key_arg::key_id as an alternative to fscrypt_add_key_arg::raw. */ struct fscrypt_provisioning_key_payload {__u32 type; -__u32 __reserved; +__u32 flags;__u8 raw[]; };/* Struct passed to FS_IOC_ADD_ENCRYPTION_KEY */ struct fscrypt_add_key_arg {struct fscrypt_key_specifier key_spec;__u32 raw_size;__u32 key_id; -__u32 __reserved[8 ]; +#define FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED 0x00000001 +__u32 flags; +__u32 __reserved[7 ];__u8 raw[]; };/* Struct passed to FS_IOC_REMOVE_ENCRYPTION_KEY */ struct fscrypt_remove_key_arg {
Signature · 1 line
2.48.1
Quoted 12 lines · Eric Biggers
This patchset is based on linux-block/for-next and is also available at:
git fetch https://git.kernel.org/pub/scm/fs/fscrypt/linux.git wrapped-keys-v12
Now that the block layer support for hardware-wrapped inline encryption keys has been applied for 6.15
(https://lore.kernel.org/r/[email protected]),
this series refreshes the remaining patches. They add the support for hardware-wrapped inline encryption keys to the Qualcomm ICE and UFS drivers and to fscrypt. All tested on SM8650 with xfstests.
Quoted 3 lines
TBD whether these will land in 6.15 too, or wait until 6.16 when the block patches that patches 2-4 depend on will have landed.
Could Jens provide an immutable branch with these patches? I don't think there's a reason to delay it for another 3 months TBH.
Bartosz
Eric Biggers
maintainerQuoted 13 lines · Bartosz Golaszewski
Quoted 12 lines · Eric Biggers
This patchset is based on linux-block/for-next and is also available at:
git fetch https://git.kernel.org/pub/scm/fs/fscrypt/linux.git wrapped-keys-v12
Now that the block layer support for hardware-wrapped inline encryption keys has been applied for 6.15
(https://lore.kernel.org/r/[email protected]),
this series refreshes the remaining patches. They add the support for hardware-wrapped inline encryption keys to the Qualcomm ICE and UFS drivers and to fscrypt. All tested on SM8650 with xfstests.
Quoted 6 lines
Quoted 3 lines
TBD whether these will land in 6.15 too, or wait until 6.16 when the block patches that patches 2-4 depend on will have landed.
Could Jens provide an immutable branch with these patches? I don't think there's a reason to delay it for another 3 months TBH.
They don't seem to be on an immutable branch, so I'll just wait until the next cycle, rather than trying to do something weird where I rebase the fscrypt tree onto the block tree and also include driver patches. TBH, I've already been waiting 5 years to land this, so an extra 9 weeks is not a big deal :-)
The first patch "soc: qcom: ice: make qcom_ice_program_key() take struct blk_crypto_key" does not depend on the block ones though, and it could land in 6.15. Bjorn, would you like to take that patch through your tree to get it out of the way?
- Eric
Eric Biggers
maintainerQuoted 12 lines · Eric Biggers
Quoted 6 lines
Quoted 3 lines
TBD whether these will land in 6.15 too, or wait until 6.16 when the block patches that patches 2-4 depend on will have landed.
Could Jens provide an immutable branch with these patches? I don't think there's a reason to delay it for another 3 months TBH.
They don't seem to be on an immutable branch, so I'll just wait until the next cycle, rather than trying to do something weird where I rebase the fscrypt tree onto the block tree and also include driver patches. TBH, I've already been waiting 5 years to land this, so an extra 9 weeks is not a big deal :-)
Quoted 4 lines
The first patch "soc: qcom: ice: make
qcom_ice_program_key()take structblk_crypto_key" does not depend on the block ones though, and it could land in 6.15. Bjorn, would you like to take that patch through your tree to get it out of the way?
Bjorn, could you apply patch 1 to your tree? Thanks!
- Eric