fscrypt: filesystem-level keyring and v2 policy support
Subsystems (9)
cover fscrypt: filesystem-level keyring and v2 policy support
From: Eric Biggers <[email protected]>
Hello,
This patchset solves multiple interrelated problems with how filesystem encryption keys are managed (for ext4, f2fs, and ubifs), including:
(1) There is a visibility mismatch between the filesystem/VFS "view" of encrypted files (which is global) and the process-subscribed keyrings (which are not global). Relying on process-subscribed keyrings to provide the encryption keys on-demand makes it quite difficult to support even simple things like running 'sudo', if encrypted files need to be accessed.
(2) There is no API to securely remove an encryption key, which should wipe all secret keys from memory and revert the encrypted files to their ciphertext "view". Many users want this, even to the extent that they're already working around it using the very bad hack of 'echo 2 > /proc/sys/vm/drop_caches', or alternatively hacking in an ioctl to drop caches for a specific filesystem.
(3) The key derivation function (KDF) used to derive the per-file encryption keys is nonstandard and has a number of problems, such as being trivially reversible. We've wanted to replace it for some time now.
(4) There is no verification that the correct master key was supplied. This is actually a security vulnerability, as it allows malicious local users to associate the wrong key with files to which they have *read-only* access.
This patchset is based loosely on my earlier patchset "fscrypt: key verification and KDF improvement". However, while the earlier patchset solved problems (3) and (4) above, it ignored (1) and (2). Consequently, it ended up with a solution which probably would have had to be reworked when we also solved (1) and (2). For example, the 'key_hash' field was hacked on to the existing on-disk format just to solve (4), but really we need it a different way to solve (1) and (2) as well, at least for non-root users. There was also a filesystem-level key cache hacked on for caching the HMAC transforms for HKDF, but really it should be a real keyring which you can add and remove keys from, as we need that anyway for (1) and (2).
By considering all the problems together we end up with a solution which should be simpler in the end, notwithstanding the length of this patchset.
This patchset is organized as follows:
- Patches 1-6 introduce a filesystem-level crypto keyring and a new ioctl, FS_IOC_ADD_ENCRYPTION_KEY, which adds a master encryption key to it. This solves problem (1) above, though initially only for use cases where a privileged process sets up the keys. Patch 20 will make it unprivileged in some cases.
- Patches 7-10 add a new ioctl, FS_IOC_REMOVE_ENCRYPTION_KEY, which removes a master encryption key from the filesystem-level crypto keyring. It also evicts the inodes which had been "unlocked" using the key. This solves problem (2) above, though initially only for use cases where a privileged process sets up the keys. Patch 20 will make it unprivileged in some cases.
- Patch 11 adds an ioctl FS_IOC_GET_ENCRYPTION_KEY_STATUS which retrieves the status of a key in the filesystem-level crypto keyring.
- Patches 12-14 wire up the above ioctls to ext4, f2fs, and ubifs.
- Patches 15-25 introduce a new encryption policy version ("v2") where master_key_descriptor is replaced with master_key_identifier, which is a cryptographic hash of the master key. This allows opening the FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY ioctls up to non-root users. In turn, this avoids any need to rely on the process-subscribed keyrings and encounter their visibility problems, and it allows non-root users to securely remove their encryption keys. I also take the opportunity to replace the AES-ECB-based KDF with HKDF-SHA512, which is also used to compute the master_key_identifier so that we pass the master key into only a single cryptographic primitive.
Note that patches 1-14 can be reviewed (and potentially even merged) on their own, without patches 15-25. At just that point, the ioctls to manage filesystem-level keys will be usable for existing encrypted files, for privileged users only. However, to understand some of the decisions made when designing the ioctls, it will be helpful to see how the later patches extend the ioctls to also be usable for v2 encryption policies and by unprivileged users.
Please review all API and on-disk format changes carefully, as we will be locked into them once merged.
You can also get this patchset from git at:
Repository: https://github.com/ebiggers/linux.git
Branch: fscrypt-v2-policy-and-api_v1
It has received light testing. I've also made proof-of-concept changes to the 'fscrypt' userspace program to make it support v2 encryption policies and the filesystem-level keyring. You can find those userspace changes in git at:
Repository: https://github.com/ebiggers/fscrypt.git
Branch: v2-policy-support
To make the 'fscrypt' userspace program use v2 policies for new encrypted directories, add
"policy_version": "2"
to /etc/fscrypt.conf within the "options" section. (Again: for now please consider the userspace changes proof-of-concept quality only! So far I've been focusing on the kernel changes.)
It's intended that the other major users of filesystem-level encryption, including the Android and Chromium OS key management systems, will switch to the new API and encryption policy version as well.
Eric Biggers (25):
fs, fscrypt: move uapi definitions to new header <linux/fscrypt.h>
fscrypt: use FSCRYPT_ prefix for uapi constants
fscrypt: use FSCRYPT_* definitions, not FS_*
fscrypt: refactor finding and deriving key
fs: add ->s_master_keys to struct super_block
fscrypt: add FS_IOC_ADD_ENCRYPTION_KEY ioctl
fs/inode.c: export inode_lru_list_del()
fs/inode.c: rename and export dispose_list()
fs/dcache.c: add shrink_dcache_inode()
fscrypt: add FS_IOC_REMOVE_ENCRYPTION_KEY ioctl
fscrypt: add FS_IOC_GET_ENCRYPTION_KEY_STATUS ioctl
ext4 crypto: wire up new ioctls for managing encryption keys f2fs crypto: wire up new ioctls for managing encryption keys ubifs crypto: wire up new ioctls for managing encryption keys
fscrypt: add UAPI definitions to get/set v2 encryption policies
fscrypt: implement basic handling of v2 encryption policies
fscrypt: add an HKDF-SHA512 implementation
fscrypt: allow adding and removing keys for v2 encryption policies
fscrypt: use HKDF-SHA512 to derive the per-file keys for v2 policies
fscrypt: allow unprivileged users to add/remove keys for v2 policies
fscrypt: require that key be added when setting a v2 encryption policy ext4 crypto: wire up FS_IOC_GET_ENCRYPTION_POLICY_EX
f2fs crypto: wire up FS_IOC_GET_ENCRYPTION_POLICY_EX
ubifs crypto: wire up FS_IOC_GET_ENCRYPTION_POLICY_EX
fscrypt: document the new ioctls and policy version
Documentation/filesystems/fscrypt.rst | 575 ++++++++++-- fs/crypto/Kconfig | 2 + fs/crypto/crypto.c | 19 +- fs/crypto/fname.c | 4 +- fs/crypto/fscrypt_private.h | 196 +++- fs/crypto/keyinfo.c | 1619 ++++++++++++++++++++++++++++++--- fs/crypto/policy.c | 373 +++++--- fs/dcache.c | 33 + fs/ext4/ioctl.c | 22 + fs/f2fs/file.c | 21 +- fs/inode.c | 24 +- fs/super.c | 3 + fs/ubifs/ioctl.c | 24 +- include/linux/dcache.h | 1 + include/linux/fs.h | 6 + include/linux/fscrypt.h | 12 +- include/linux/fscrypt_notsupp.h | 23 + include/linux/fscrypt_supp.h | 4 + include/uapi/linux/fs.h | 50 +- include/uapi/linux/fscrypt.h | 159 ++++ 20 files changed, 2724 insertions(+), 446 deletions(-) create mode 100644 include/uapi/linux/fscrypt.h
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 1/25 fs, fscrypt: move uapi definitions to new header <linux/fscrypt.h>
From: Eric Biggers <[email protected]>
There are going to be more filesystem encryption definitions added, and we don't want to use a disproportionate amount of space in <linux/fs.h> for filesystem encryption stuff. So move the fscrypt definitions to a new header <linux/fscrypt.h>.
For compatibility with existing userspace programs which may be including <linux/fs.h>, <linux/fs.h> still includes the new header. (It's debatable whether we really need this, though; the filesystem encryption API is new enough that most if not all programs that are using it have to declare it themselves anyway.)
--- include/linux/fscrypt.h | 2 +- include/uapi/linux/fs.h | 50 +++-------------------------------------- include/uapi/linux/fscrypt.h | 53 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 include/uapi/linux/fscrypt.h
include/linux/fscrypt.h +1 -1
@@ -19,7 +19,7 @@#include <linux/bio.h> #include <linux/dcache.h> #include <crypto/skcipher.h> -#include <uapi/linux/fs.h> +#include <uapi/linux/fscrypt.h> #define FS_CRYPTO_BLOCK_SIZE 16
include/uapi/linux/fs.h +3 -47
62 lines not on this page · raw message
include/uapi/linux/fscrypt.h new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Re: [RFC PATCH 01/25] fs, fscrypt: move uapi definitions to new header <linux/fscrypt.h>
Quoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
There are going to be more filesystem encryption definitions added, and we don't want to use a disproportionate amount of space in <
linux/fs.h> for filesystem encryption stuff. So move the fscrypt definitions to a new header <linux/fscrypt.h>.
Quoted 7 lines
For compatibility with existing userspace programs which may be including <
linux/fs.h>, <linux/fs.h> still includes the new header. (It's debatable whether we really need this, though; the filesystem encryption API is new enough that most if not all programs that are using it have to declare it themselves anyway.)Signed-off-by Eric Biggers <[email protected]>
Quoted 150 lines
--- include/linux/fscrypt.h | 2 +- include/uapi/linux/fs.h | 50 +++-------------------------------------- include/uapi/linux/fscrypt.h | 53 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 include/uapi/linux/fscrypt.hinclude/linux/fscrypt.h +1
-1@@ -19,7 +19,7 @@#include <linux/bio.h> #include <linux/dcache.h> #include <crypto/skcipher.h> -#include <uapi/linux/fs.h> +#include <uapi/linux/fscrypt.h> #define FS_CRYPTO_BLOCK_SIZE 16include/uapi/linux/fs.h +3
-47@@ -12,6 +12,9 @@#include <linux/limits.h> #include <linux/ioctl.h> #include <linux/types.h> +#ifndef __KERNEL__ +#include <linux/fscrypt.h> +#endif /* * It's silly to have NR_OPEN bigger than NR_FILE, but you can change @@ -253,53 +256,6 @@ struct fsxattr {#define FS_IOC_FSGETXATTR _IOR ('X', 31, struct fsxattr)#define FS_IOC_FSSETXATTR _IOW ('X', 32, struct fsxattr) -/* -* File system encryption support -*/ -/* Policy provided via an ioctl on the topmost directory */ -#define FS_KEY_DESCRIPTOR_SIZE 8 - -#define FS_POLICY_FLAGS_PAD_4 0x00 -#define FS_POLICY_FLAGS_PAD_8 0x01 -#define FS_POLICY_FLAGS_PAD_16 0x02 -#define FS_POLICY_FLAGS_PAD_32 0x03 -#define FS_POLICY_FLAGS_PAD_MASK 0x03 -#define FS_POLICY_FLAGS_VALID 0x03 - -/* Encryption algorithms */ -#define FS_ENCRYPTION_MODE_INVALID 0 -#define FS_ENCRYPTION_MODE_AES_256_XTS 1 -#define FS_ENCRYPTION_MODE_AES_256_GCM 2 -#define FS_ENCRYPTION_MODE_AES_256_CBC 3 -#define FS_ENCRYPTION_MODE_AES_256_CTS 4 -#define FS_ENCRYPTION_MODE_AES_128_CBC 5 -#define FS_ENCRYPTION_MODE_AES_128_CTS 6 - -struct fscrypt_policy { -__u8 version; -__u8 contents_encryption_mode; -__u8 filenames_encryption_mode; -__u8 flags; -__u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE ]; -}; - -#define FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct fscrypt_policy) -#define FS_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16]) -#define FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct fscrypt_policy) - -/* Parameters for passing an encryption key into the kernel keyring */ -#define FS_KEY_DESC_PREFIX "fscrypt:" -#define FS_KEY_DESC_PREFIX_SIZE 8 - -/* Structure that userspace passes to the kernel keyring */ -#define FS_MAX_KEY_SIZE 64 - -struct fscrypt_key { -__u32 mode; -__u8 raw[FS_MAX_KEY_SIZE ]; -__u32 size; -}; -/* * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS) * include/uapi/linux/fscrypt.h new file +53
-0@@ -0,0 +1,53 @@ +#ifndef _UAPI_LINUX_FSCRYPT_H +#define _UAPI_LINUX_FSCRYPT_H + +#include <linux/types.h> + +/* +* File system encryption support +*/ +/* Policy provided via an ioctl on the topmost directory */ +#define FS_KEY_DESCRIPTOR_SIZE 8 + +#define FS_POLICY_FLAGS_PAD_4 0x00 +#define FS_POLICY_FLAGS_PAD_8 0x01 +#define FS_POLICY_FLAGS_PAD_16 0x02 +#define FS_POLICY_FLAGS_PAD_32 0x03 +#define FS_POLICY_FLAGS_PAD_MASK 0x03 +#define FS_POLICY_FLAGS_VALID 0x03 + +/* Encryption algorithms */ +#define FS_ENCRYPTION_MODE_INVALID 0 +#define FS_ENCRYPTION_MODE_AES_256_XTS 1 +#define FS_ENCRYPTION_MODE_AES_256_GCM 2 +#define FS_ENCRYPTION_MODE_AES_256_CBC 3 +#define FS_ENCRYPTION_MODE_AES_256_CTS 4 +#define FS_ENCRYPTION_MODE_AES_128_CBC 5 +#define FS_ENCRYPTION_MODE_AES_128_CTS 6 + +struct fscrypt_policy { +__u8 version; +__u8 contents_encryption_mode; +__u8 filenames_encryption_mode; +__u8 flags; +__u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE ]; +}; + +#define FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct fscrypt_policy) +#define FS_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16]) +#define FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct fscrypt_policy) + +/* Parameters for passing an encryption key into the kernel keyring */ +#define FS_KEY_DESC_PREFIX "fscrypt:" +#define FS_KEY_DESC_PREFIX_SIZE 8 + +/* Structure that userspace passes to the kernel keyring */ +#define FS_MAX_KEY_SIZE 64 + +struct fscrypt_key { +__u32 mode; +__u8 raw[FS_MAX_KEY_SIZE ]; +__u32 size; +}; + +#endif /* _UAPI_LINUX_FSCRYPT_H */ Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 2/25 fscrypt: use FSCRYPT_ prefix for uapi constants
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Prefix all filesystem encryption UAPI constants except the ioctl numbers with "FSCRYPT_" rather than with "FS_". This namespaces the constants more appropriately and makes it clear that they are related specifically to the filesystem encryption feature, and to the 'fscrypt_*' structures. With some of the old names like "FS_POLICY_FLAGS_VALID", it was not immediately clear that the constant had anything to do with encryption.
This is also useful because we'll be adding more encryption-related constants, e.g. for the policy version, and we'd otherwise have to choose whether to use unclear names like FS_POLICY_VERSION_* or inconsistent names like FS_ENCRYPTION_POLICY_VERSION_*.
For source compatibility with older userspace programs, keep the old names defined as aliases to the new ones. (It's debatable whether we really need this, though; the filesystem encryption API is new enough that most if not all programs that are using it have to declare it themselves anyway.)
--- Documentation/filesystems/fscrypt.rst | 14 ++++----- include/uapi/linux/fscrypt.h | 59 ++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 26 deletions(-)
Documentation/filesystems/fscrypt.rst +7 -7
@@ -251,14 +251,14 @@ empty directory or verifies that a directory or regular file already has the specified encryption policy. It takes in a pointer to a :c:type:`struct fscrypt_policy`, defined as follows:: - #define FS_KEY_DESCRIPTOR_SIZE 8 + #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 struct fscrypt_policy { __u8 version; __u8 contents_encryption_mode; __u8 filenames_encryption_mode; __u8 flags; - __u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; + __u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; }; This structure must be initialized as follows:
@@ -274,7 +274,7 @@ This structure must be initialized as follows: - ``flags`` must be set to a value from ``<linux/fs.h>`` which identifies the amount of NUL-padding to use when encrypting - filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3). + filenames. If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32 (0x3). - ``master_key_descriptor`` specifies how to find the master key in the keyring; see `Adding keys`_. It is up to userspace to choose a
@@ -374,11 +374,11 @@ followed by the 16-character lower case hex representation of the ``master_key_descriptor`` that was set in the encryption policy. The key payload must conform to the following structure:: - #define FS_MAX_KEY_SIZE 64 + #define FSCRYPT_MAX_KEY_SIZE 64 struct fscrypt_key { u32 mode; - u8 raw[FS_MAX_KEY_SIZE]; + u8 raw[FSCRYPT_MAX_KEY_SIZE]; u32 size; };
@@ -533,7 +533,7 @@ much confusion if an encryption policy were to be added to or removed from anything other than an empty directory.) The struct is defined as follows:: - #define FS_KEY_DESCRIPTOR_SIZE 8 + #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 #define FS_KEY_DERIVATION_NONCE_SIZE 16 struct fscrypt_context {
@@ -541,7 +541,7 @@ as follows:: u8 contents_encryption_mode; u8 filenames_encryption_mode; u8 flags; - u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; + u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; };
include/uapi/linux/fscrypt.h +40 -19
86 lines not on this page · raw message
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Re: [RFC PATCH 02/25] fscrypt: use FSCRYPT_ prefix for uapi constants
Quoted 14 lines · Eric Biggers
From: Eric Biggers <[email protected]>
Prefix all filesystem encryption UAPI constants except the ioctl numbers with "
FSCRYPT_" rather than with "FS_". This namespaces the constants more appropriately and makes it clear that they are related specifically to the filesystem encryption feature, and to the 'fscrypt_*' structures. With some of the old names like "FS_POLICY_FLAGS_VALID", it was not immediately clear that the constant had anything to do with encryption.This is also useful because we'll be adding more encryption-related constants, e.g. for the policy version, and we'd otherwise have to choose whether to use unclear names like FS_POLICY_VERSION_* or inconsistent names like FS_ENCRYPTION_POLICY_VERSION_*.
Quoted 7 lines
For source compatibility with older userspace programs, keep the old names defined as aliases to the new ones. (It's debatable whether we really need this, though; the filesystem encryption API is new enough that most if not all programs that are using it have to declare it themselves anyway.)
Signed-off-by Eric Biggers <[email protected]>
Quoted 162 lines
--- Documentation/filesystems/fscrypt.rst | 14 ++++----- include/uapi/linux/fscrypt.h | 59 ++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 26 deletions(-)Documentation/filesystems/fscrypt.rst +7
-7@@ -251,14 +251,14 @@ empty directory or verifies that a directory or regular file already has the specified encryption policy. It takes in a pointer to a :c:type:`struct fscrypt_policy`, defined as follows:: - #define FS_KEY_DESCRIPTOR_SIZE 8 + #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 struct fscrypt_policy { __u8 version; __u8 contents_encryption_mode; __u8 filenames_encryption_mode; __u8 flags; - __u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; + __u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; }; This structure must be initialized as follows:@@ -274,7 +274,7 @@ This structure must be initialized as follows: - ``flags`` must be set to a value from ``<linux/fs.h>`` which identifies the amount of NUL-padding to use when encrypting - filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3). + filenames. If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32 (0x3). - ``master_key_descriptor`` specifies how to find the master key in the keyring; see `Adding keys`_. It is up to userspace to choose a@@ -374,11 +374,11 @@ followed by the 16-character lower case hex representation of the ``master_key_descriptor`` that was set in the encryption policy. The key payload must conform to the following structure:: - #define FS_MAX_KEY_SIZE 64 + #define FSCRYPT_MAX_KEY_SIZE 64 struct fscrypt_key { u32 mode; - u8 raw[FS_MAX_KEY_SIZE]; + u8 raw[FSCRYPT_MAX_KEY_SIZE]; u32 size; };@@ -533,7 +533,7 @@ much confusion if an encryption policy were to be added to or removed from anything other than an empty directory.) The struct is defined as follows:: - #define FS_KEY_DESCRIPTOR_SIZE 8 + #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 #define FS_KEY_DERIVATION_NONCE_SIZE 16 struct fscrypt_context {@@ -541,7 +541,7 @@ as follows:: u8 contents_encryption_mode; u8 filenames_encryption_mode; u8 flags; - u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; + u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; };include/uapi/linux/fscrypt.h +40
-19@@ -7,30 +7,31 @@* File system encryption support */ /* Policy provided via an ioctl on the topmost directory */ -#define FS_KEY_DESCRIPTOR_SIZE 8 +#define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 -#define FS_POLICY_FLAGS_PAD_4 0x00 -#define FS_POLICY_FLAGS_PAD_8 0x01 -#define FS_POLICY_FLAGS_PAD_16 0x02 -#define FS_POLICY_FLAGS_PAD_32 0x03 -#define FS_POLICY_FLAGS_PAD_MASK 0x03 -#define FS_POLICY_FLAGS_VALID 0x03 +/* Encryption policy flags */ +#define FSCRYPT_POLICY_FLAGS_PAD_4 0x00 +#define FSCRYPT_POLICY_FLAGS_PAD_8 0x01 +#define FSCRYPT_POLICY_FLAGS_PAD_16 0x02 +#define FSCRYPT_POLICY_FLAGS_PAD_32 0x03 +#define FSCRYPT_POLICY_FLAGS_PAD_MASK 0x03 +#define FSCRYPT_POLICY_FLAGS_VALID 0x03/* Encryption algorithms */ -#define FS_ENCRYPTION_MODE_INVALID 0 -#define FS_ENCRYPTION_MODE_AES_256_XTS 1 -#define FS_ENCRYPTION_MODE_AES_256_GCM 2 -#define FS_ENCRYPTION_MODE_AES_256_CBC 3 -#define FS_ENCRYPTION_MODE_AES_256_CTS 4 -#define FS_ENCRYPTION_MODE_AES_128_CBC 5 -#define FS_ENCRYPTION_MODE_AES_128_CTS 6 +#define FSCRYPT_MODE_INVALID 0 +#define FSCRYPT_MODE_AES_256_XTS 1 +#define FSCRYPT_MODE_AES_256_GCM 2 +#define FSCRYPT_MODE_AES_256_CBC 3 +#define FSCRYPT_MODE_AES_256_CTS 4 +#define FSCRYPT_MODE_AES_128_CBC 5 +#define FSCRYPT_MODE_AES_128_CTS 6struct fscrypt_policy {__u8 version;__u8 contents_encryption_mode;__u8 filenames_encryption_mode;__u8 flags; -__u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE ]; +__u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; };#define FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct fscrypt_policy)@@ -38,16 +39,36 @@ struct fscrypt_policy {#define FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct fscrypt_policy)/* Parameters for passing an encryption key into the kernel keyring */ -#define FS_KEY_DESC_PREFIX "fscrypt:" -#define FS_KEY_DESC_PREFIX_SIZE 8 +#define FSCRYPT_KEY_DESC_PREFIX "fscrypt:" +#define FSCRYPT_KEY_DESC_PREFIX_SIZE 8/* Structure that userspace passes to the kernel keyring */ -#define FS_MAX_KEY_SIZE 64 +#define FSCRYPT_MAX_KEY_SIZE 64struct fscrypt_key {__u32 mode; -__u8 raw[FS_MAX_KEY_SIZE ]; +__u8 raw[FSCRYPT_MAX_KEY_SIZE ];__u32 size; }; +/**********************************************************************/ + +/* old names; don't add anything new here! */ +#define FS_POLICY_FLAGS_PAD_4 FSCRYPT_POLICY_FLAGS_PAD_4 +#define FS_POLICY_FLAGS_PAD_8 FSCRYPT_POLICY_FLAGS_PAD_8 +#define FS_POLICY_FLAGS_PAD_16 FSCRYPT_POLICY_FLAGS_PAD_16 +#define FS_POLICY_FLAGS_PAD_32 FSCRYPT_POLICY_FLAGS_PAD_32 +#define FS_POLICY_FLAGS_PAD_MASK FSCRYPT_POLICY_FLAGS_PAD_MASK +#define FS_POLICY_FLAGS_VALID FSCRYPT_POLICY_FLAGS_VALID +#define FS_KEY_DESCRIPTOR_SIZE FSCRYPT_KEY_DESCRIPTOR_SIZE +#define FS_ENCRYPTION_MODE_INVALID FSCRYPT_MODE_INVALID +#define FS_ENCRYPTION_MODE_AES_256_XTS FSCRYPT_MODE_AES_256_XTS +#define FS_ENCRYPTION_MODE_AES_256_GCM FSCRYPT_MODE_AES_256_GCM +#define FS_ENCRYPTION_MODE_AES_256_CBC FSCRYPT_MODE_AES_256_CBC +#define FS_ENCRYPTION_MODE_AES_256_CTS FSCRYPT_MODE_AES_256_CTS +#define FS_ENCRYPTION_MODE_AES_128_CBC FSCRYPT_MODE_AES_128_CBC +#define FS_ENCRYPTION_MODE_AES_128_CTS FSCRYPT_MODE_AES_128_CTS +#define FS_KEY_DESC_PREFIX FSCRYPT_KEY_DESC_PREFIX +#define FS_KEY_DESC_PREFIX_SIZE FSCRYPT_KEY_DESC_PREFIX_SIZE +#define FS_MAX_KEY_SIZE FSCRYPT_MAX_KEY_SIZE#endif /* _UAPI_LINUX_FSCRYPT_H */ Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 3/25 fscrypt: use FSCRYPT_* definitions, not FS_*
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Update the filesystem encryption kernel code to use the new names for the UAPI constants rather than the old names.
--- fs/crypto/fname.c | 4 ++-- fs/crypto/fscrypt_private.h | 4 ++-- fs/crypto/keyinfo.c | 36 +++++++++++++++++------------------- fs/crypto/policy.c | 14 +++++++------- include/linux/fscrypt.h | 8 ++++---- 5 files changed, 32 insertions(+), 34 deletions(-)
fs/crypto/fname.c +2 -2
@@ -46,7 +46,7 @@ static int fname_encrypt(struct inode *inode,int res =0 ;char iv[FS_CRYPTO_BLOCK_SIZE ];struct scatterlist sg; -int padding =4 << (ci->ci_flags &FS_POLICY_FLAGS_PAD_MASK ); +int padding =4 << (ci->ci_flags &FSCRYPT_POLICY_FLAGS_PAD_MASK );unsigned int lim;unsigned int cryptlen;
@@ -217,7 +217,7 @@ u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)struct fscrypt_info *ci = inode->i_crypt_info;if (ci) - padding =4 << (ci->ci_flags &FS_POLICY_FLAGS_PAD_MASK ); + padding =4 << (ci->ci_flags &FSCRYPT_POLICY_FLAGS_PAD_MASK ); ilen =max (ilen, (u32 )FS_CRYPTO_BLOCK_SIZE );return round_up (ilen, padding); }
fs/crypto/fscrypt_private.h +2 -2
@@ -43,7 +43,7 @@ struct fscrypt_context {u8 contents_encryption_mode;u8 filenames_encryption_mode;u8 flags; -u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE ]; +u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ];u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE ]; } __packed;
@@ -59,7 +59,7 @@ struct fscrypt_info {u8 ci_flags;struct crypto_skcipher *ci_ctfm;struct crypto_cipher *ci_essiv_tfm; -u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE ]; +u8 ci_master_key[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; }; typedef enum {
fs/crypto/keyinfo.c +17 -19
@@ -38,7 +38,7 @@ static void derive_crypt_complete(struct crypto_async_request *req, int rc)*/ static int derive_key_aes (u8 deriving_key[FS_AES_128_ECB_KEY_SIZE ],const struct fscrypt_key *source_key, -u8 derived_raw_key[FS_MAX_KEY_SIZE ]) +u8 derived_raw_key[FSCRYPT_MAX_KEY_SIZE ]) {int res =0 ;struct skcipher_request *req =NULL ;
@@ -91,7 +91,7 @@ static int validate_user_key(struct fscrypt_info *crypt_info,int res; description =kasprintf (GFP_NOFS ,"%s%*phN" , prefix, -FS_KEY_DESCRIPTOR_SIZE , +FSCRYPT_KEY_DESCRIPTOR_SIZE , ctx->master_key_descriptor);if (!description)return -ENOMEM ;
@@ -121,7 +121,8 @@ static int validate_user_key(struct fscrypt_info *crypt_info, 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 +if (master_key->size < min_keysize || + master_key->size >FSCRYPT_MAX_KEY_SIZE || master_key->size %AES_BLOCK_SIZE !=0 ) { printk_once(KERN_WARNING "%s: key size incorrect: %d\n",
@@ -140,14 +141,10 @@ static const struct {const char *cipher_str;int keysize; } available_modes[] = { - [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)", - FS_AES_256_XTS_KEY_SIZE }, - [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))", - FS_AES_256_CTS_KEY_SIZE }, - [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)", - FS_AES_128_CBC_KEY_SIZE }, - [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))", - FS_AES_128_CTS_KEY_SIZE }, + [FSCRYPT_MODE_AES_256_XTS] = { "xts(aes)", FS_AES_256_XTS_KEY_SIZE }, + [FSCRYPT_MODE_AES_256_CTS] = { "cts(cbc(aes))", FS_AES_256_CTS_KEY_SIZE }, + [FSCRYPT_MODE_AES_128_CBC] = { "cbc(aes)", FS_AES_128_CBC_KEY_SIZE }, + [FSCRYPT_MODE_AES_128_CTS] = { "cts(cbc(aes))", FS_AES_128_CTS_KEY_SIZE }, }; static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
@@ -278,9 +275,10 @@ int fscrypt_get_encryption_info(struct inode *inode)/* Fake up a context for an unencrypted directory */ memset (&ctx,0 ,sizeof (ctx)); ctx.format =FS_ENCRYPTION_CONTEXT_FORMAT_V1 ; - ctx.contents_encryption_mode =FS_ENCRYPTION_MODE_AES_256_XTS ; - ctx.filenames_encryption_mode =FS_ENCRYPTION_MODE_AES_256_CTS ; -memset (ctx.master_key_descriptor,0x42 ,FS_KEY_DESCRIPTOR_SIZE ); + ctx.contents_encryption_mode =FSCRYPT_MODE_AES_256_XTS ; + ctx.filenames_encryption_mode =FSCRYPT_MODE_AES_256_CTS ; +memset (ctx.master_key_descriptor,0x42 , +FSCRYPT_KEY_DESCRIPTOR_SIZE ); }else if (res !=sizeof (ctx)) {return -EINVAL ; }
@@ -288,7 +286,7 @@ int fscrypt_get_encryption_info(struct inode *inode)if (ctx.format !=FS_ENCRYPTION_CONTEXT_FORMAT_V1 )return -EINVAL ; -if (ctx.flags & ~FS_POLICY_FLAGS_VALID ) +if (ctx.flags & ~FSCRYPT_POLICY_FLAGS_VALID )return -EINVAL ; crypt_info =kmem_cache_alloc (fscrypt_info_cachep,GFP_NOFS );
@@ -312,12 +310,12 @@ int fscrypt_get_encryption_info(struct inode *inode)* crypto API as part of key derivation. */ res = -ENOMEM ; - raw_key =kmalloc (FS_MAX_KEY_SIZE ,GFP_NOFS ); + raw_key =kmalloc (FSCRYPT_MAX_KEY_SIZE ,GFP_NOFS );if (!raw_key) goto out; - res =validate_user_key (crypt_info, &ctx, raw_key,FS_KEY_DESC_PREFIX , - keysize); + res =validate_user_key (crypt_info, &ctx, raw_key, +FSCRYPT_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,
@@ -349,7 +347,7 @@ int fscrypt_get_encryption_info(struct inode *inode) goto out;if (S_ISREG (inode->i_mode) && - crypt_info->ci_data_mode =FS_ENCRYPTION_MODE_AES_128_CBC ) { + crypt_info->ci_data_mode =FSCRYPT_MODE_AES_128_CBC ) { res =init_essiv_generator (crypt_info, raw_key, keysize);if (res) { pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
fs/crypto/policy.c +7 -7
@@ -21,7 +21,7 @@ static bool is_encryption_context_consistent_with_policy( const struct fscrypt_policy *policy) {return memcmp (ctx->master_key_descriptor, policy->master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ) =0 && +FSCRYPT_KEY_DESCRIPTOR_SIZE ) =0 && (ctx->flags = policy->flags) && (ctx->contents_encryption_mode = policy->contents_encryption_mode) &&
@@ -36,13 +36,13 @@ static int create_encryption_context_from_policy(struct inode *inode, ctx.format =FS_ENCRYPTION_CONTEXT_FORMAT_V1 ;memcpy (ctx.master_key_descriptor, policy->master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ); +FSCRYPT_KEY_DESCRIPTOR_SIZE );if (!fscrypt_valid_enc_modes (policy->contents_encryption_mode, policy->filenames_encryption_mode))return -EINVAL ; -if (policy->flags & ~FS_POLICY_FLAGS_VALID ) +if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID )return -EINVAL ; ctx.contents_encryption_mode = policy->contents_encryption_mode;
@@ -125,7 +125,7 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) policy.filenames_encryption_mode = ctx.filenames_encryption_mode; policy.flags = ctx.flags;memcpy (policy.master_key_descriptor, ctx.master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ); +FSCRYPT_KEY_DESCRIPTOR_SIZE );if (copy_to_user (arg, &policy,sizeof (policy)))return -EFAULT ;
@@ -199,7 +199,7 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)if (parent_ci && child_ci) {return memcmp (parent_ci->ci_master_key, child_ci->ci_master_key, -FS_KEY_DESCRIPTOR_SIZE ) =0 && +FSCRYPT_KEY_DESCRIPTOR_SIZE ) =0 && (parent_ci->ci_data_mode = child_ci->ci_data_mode) && (parent_ci->ci_filename_mode = child_ci->ci_filename_mode) &&
@@ -216,7 +216,7 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)return memcmp (parent_ctx.master_key_descriptor, child_ctx.master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ) =0 && +FSCRYPT_KEY_DESCRIPTOR_SIZE ) =0 && (parent_ctx.contents_encryption_mode = child_ctx.contents_encryption_mode) && (parent_ctx.filenames_encryption_mode =
@@ -254,7 +254,7 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child, ctx.filenames_encryption_mode = ci->ci_filename_mode; ctx.flags = ci->ci_flags;memcpy (ctx.master_key_descriptor, ci->ci_master_key, -FS_KEY_DESCRIPTOR_SIZE ); +FSCRYPT_KEY_DESCRIPTOR_SIZE );get_random_bytes (ctx.nonce,FS_KEY_DERIVATION_NONCE_SIZE );BUILD_BUG_ON (sizeof (ctx) !=FSCRYPT_SET_CONTEXT_MAX_SIZE ); res = parent->i_sb->s_cop->set_context(child, &ctx,
include/linux/fscrypt.h +4 -4
@@ -99,12 +99,12 @@ static inline bool fscrypt_dummy_context_enabled(struct inode *inode)static inline bool fscrypt_valid_enc_modes (u32 contents_mode,u32 filenames_mode) { -if (contents_mode =FS_ENCRYPTION_MODE_AES_128_CBC && - filenames_mode =FS_ENCRYPTION_MODE_AES_128_CTS ) +if (contents_mode =FSCRYPT_MODE_AES_128_CBC && + filenames_mode =FSCRYPT_MODE_AES_128_CTS )return true; -if (contents_mode =FS_ENCRYPTION_MODE_AES_256_XTS && - filenames_mode =FS_ENCRYPTION_MODE_AES_256_CTS ) +if (contents_mode =FSCRYPT_MODE_AES_256_XTS && + filenames_mode =FSCRYPT_MODE_AES_256_CTS )return true;return false;
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Re: [RFC PATCH 03/25] fscrypt: use FSCRYPT_* definitions, not FS_*
Quoted 6 lines · Eric Biggers
From: Eric Biggers <[email protected]>
Update the filesystem encryption kernel code to use the new names for the UAPI constants rather than the old names.
Signed-off-by Eric Biggers <[email protected]>
Quoted 75 lines
--- fs/crypto/fname.c | 4 ++-- fs/crypto/fscrypt_private.h | 4 ++-- fs/crypto/keyinfo.c | 36 +++++++++++++++++------------------- fs/crypto/policy.c | 14 +++++++------- include/linux/fscrypt.h | 8 ++++---- 5 files changed, 32 insertions(+), 34 deletions(-)fs/crypto/fname.c +2
-2@@ -46,7 +46,7 @@ static int fname_encrypt(struct inode *inode,int res =0 ;char iv[FS_CRYPTO_BLOCK_SIZE ];struct scatterlist sg; -int padding =4 << (ci->ci_flags &FS_POLICY_FLAGS_PAD_MASK ); +int padding =4 << (ci->ci_flags &FSCRYPT_POLICY_FLAGS_PAD_MASK );unsigned int lim;unsigned int cryptlen;@@ -217,7 +217,7 @@ u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)struct fscrypt_info *ci = inode->i_crypt_info;if (ci) - padding =4 << (ci->ci_flags &FS_POLICY_FLAGS_PAD_MASK ); + padding =4 << (ci->ci_flags &FSCRYPT_POLICY_FLAGS_PAD_MASK ); ilen =max (ilen, (u32 )FS_CRYPTO_BLOCK_SIZE );return round_up (ilen, padding); }fs/crypto/fscrypt_private.h +2
-2@@ -43,7 +43,7 @@ struct fscrypt_context {u8 contents_encryption_mode;u8 filenames_encryption_mode;u8 flags; -u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE ]; +u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ];u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE ]; } __packed;@@ -59,7 +59,7 @@ struct fscrypt_info {u8 ci_flags;struct crypto_skcipher *ci_ctfm;struct crypto_cipher *ci_essiv_tfm; -u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE ]; +u8 ci_master_key[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; }; typedef enum {fs/crypto/keyinfo.c +2
-2@@ -38,7 +38,7 @@ static void derive_crypt_complete(struct crypto_async_request *req, int rc)*/ static int derive_key_aes (u8 deriving_key[FS_AES_128_ECB_KEY_SIZE ],const struct fscrypt_key *source_key, -u8 derived_raw_key[FS_MAX_KEY_SIZE ]) +u8 derived_raw_key[FSCRYPT_MAX_KEY_SIZE ]) {int res =0 ;struct skcipher_request *req =NULL ;@@ -91,7 +91,7 @@ static int validate_user_key(struct fscrypt_info *crypt_info,int res; description =kasprintf (GFP_NOFS ,"%s%*phN" , prefix, -FS_KEY_DESCRIPTOR_SIZE , +FSCRYPT_KEY_DESCRIPTOR_SIZE , ctx->master_key_descriptor);if (!description)return -ENOMEM ;
fs/crypto/keyinfo.c static int validate_user_key(struct fscrypt_info *crypt_info, · line 126
Quoted 7 lines
fs/crypto/keyinfo.c +2
-1@@ -121,7 +121,8 @@ static int validate_user_key(struct fscrypt_info *crypt_info, 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 +if (master_key->size < min_keysize || + master_key->size >FSCRYPT_MAX_KEY_SIZE || master_key->size %AES_BLOCK_SIZE !=0 ) {
Format nit: This makes the placement of "||" inconsistent across lines in this same expression.
Quoted 158 lines
fs/crypto/keyinfo.c +13
-16printk_once(KERN_WARNING "%s: key size incorrect: %d\n",@@ -140,14 +141,10 @@ static const struct {const char *cipher_str;int keysize; } available_modes[] = { - [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)", - FS_AES_256_XTS_KEY_SIZE }, - [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))", - FS_AES_256_CTS_KEY_SIZE }, - [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)", - FS_AES_128_CBC_KEY_SIZE }, - [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))", - FS_AES_128_CTS_KEY_SIZE }, + [FSCRYPT_MODE_AES_256_XTS] = { "xts(aes)", FS_AES_256_XTS_KEY_SIZE }, + [FSCRYPT_MODE_AES_256_CTS] = { "cts(cbc(aes))", FS_AES_256_CTS_KEY_SIZE }, + [FSCRYPT_MODE_AES_128_CBC] = { "cbc(aes)", FS_AES_128_CBC_KEY_SIZE }, + [FSCRYPT_MODE_AES_128_CTS] = { "cts(cbc(aes))", FS_AES_128_CTS_KEY_SIZE }, }; static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,@@ -278,9 +275,10 @@ int fscrypt_get_encryption_info(struct inode *inode)/* Fake up a context for an unencrypted directory */ memset (&ctx,0 ,sizeof (ctx)); ctx.format =FS_ENCRYPTION_CONTEXT_FORMAT_V1 ; - ctx.contents_encryption_mode =FS_ENCRYPTION_MODE_AES_256_XTS ; - ctx.filenames_encryption_mode =FS_ENCRYPTION_MODE_AES_256_CTS ; -memset (ctx.master_key_descriptor,0x42 ,FS_KEY_DESCRIPTOR_SIZE ); + ctx.contents_encryption_mode =FSCRYPT_MODE_AES_256_XTS ; + ctx.filenames_encryption_mode =FSCRYPT_MODE_AES_256_CTS ; +memset (ctx.master_key_descriptor,0x42 , +FSCRYPT_KEY_DESCRIPTOR_SIZE ); }else if (res !=sizeof (ctx)) {return -EINVAL ; }@@ -288,7 +286,7 @@ int fscrypt_get_encryption_info(struct inode *inode)if (ctx.format !=FS_ENCRYPTION_CONTEXT_FORMAT_V1 )return -EINVAL ; -if (ctx.flags & ~FS_POLICY_FLAGS_VALID ) +if (ctx.flags & ~FSCRYPT_POLICY_FLAGS_VALID )return -EINVAL ; crypt_info =kmem_cache_alloc (fscrypt_info_cachep,GFP_NOFS );@@ -312,12 +310,12 @@ int fscrypt_get_encryption_info(struct inode *inode)* crypto API as part of key derivation. */ res = -ENOMEM ; - raw_key =kmalloc (FS_MAX_KEY_SIZE ,GFP_NOFS ); + raw_key =kmalloc (FSCRYPT_MAX_KEY_SIZE ,GFP_NOFS );if (!raw_key) goto out; - res =validate_user_key (crypt_info, &ctx, raw_key,FS_KEY_DESC_PREFIX , - keysize); + res =validate_user_key (crypt_info, &ctx, raw_key, +FSCRYPT_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,@@ -349,7 +347,7 @@ int fscrypt_get_encryption_info(struct inode *inode) goto out;if (S_ISREG (inode->i_mode) && - crypt_info->ci_data_mode =FS_ENCRYPTION_MODE_AES_128_CBC ) { + crypt_info->ci_data_mode =FSCRYPT_MODE_AES_128_CBC ) { res =init_essiv_generator (crypt_info, raw_key, keysize);if (res) { pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",fs/crypto/policy.c +7
-7@@ -21,7 +21,7 @@ static bool is_encryption_context_consistent_with_policy( const struct fscrypt_policy *policy) {return memcmp (ctx->master_key_descriptor, policy->master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ) =0 && +FSCRYPT_KEY_DESCRIPTOR_SIZE ) =0 && (ctx->flags = policy->flags) && (ctx->contents_encryption_mode = policy->contents_encryption_mode) &&@@ -36,13 +36,13 @@ static int create_encryption_context_from_policy(struct inode *inode, ctx.format =FS_ENCRYPTION_CONTEXT_FORMAT_V1 ;memcpy (ctx.master_key_descriptor, policy->master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ); +FSCRYPT_KEY_DESCRIPTOR_SIZE );if (!fscrypt_valid_enc_modes (policy->contents_encryption_mode, policy->filenames_encryption_mode))return -EINVAL ; -if (policy->flags & ~FS_POLICY_FLAGS_VALID ) +if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID )return -EINVAL ; ctx.contents_encryption_mode = policy->contents_encryption_mode;@@ -125,7 +125,7 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) policy.filenames_encryption_mode = ctx.filenames_encryption_mode; policy.flags = ctx.flags;memcpy (policy.master_key_descriptor, ctx.master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ); +FSCRYPT_KEY_DESCRIPTOR_SIZE );if (copy_to_user (arg, &policy,sizeof (policy)))return -EFAULT ;@@ -199,7 +199,7 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)if (parent_ci && child_ci) {return memcmp (parent_ci->ci_master_key, child_ci->ci_master_key, -FS_KEY_DESCRIPTOR_SIZE ) =0 && +FSCRYPT_KEY_DESCRIPTOR_SIZE ) =0 && (parent_ci->ci_data_mode = child_ci->ci_data_mode) && (parent_ci->ci_filename_mode = child_ci->ci_filename_mode) &&@@ -216,7 +216,7 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)return memcmp (parent_ctx.master_key_descriptor, child_ctx.master_key_descriptor, -FS_KEY_DESCRIPTOR_SIZE ) =0 && +FSCRYPT_KEY_DESCRIPTOR_SIZE ) =0 && (parent_ctx.contents_encryption_mode = child_ctx.contents_encryption_mode) && (parent_ctx.filenames_encryption_mode =@@ -254,7 +254,7 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child, ctx.filenames_encryption_mode = ci->ci_filename_mode; ctx.flags = ci->ci_flags;memcpy (ctx.master_key_descriptor, ci->ci_master_key, -FS_KEY_DESCRIPTOR_SIZE ); +FSCRYPT_KEY_DESCRIPTOR_SIZE );get_random_bytes (ctx.nonce,FS_KEY_DERIVATION_NONCE_SIZE );BUILD_BUG_ON (sizeof (ctx) !=FSCRYPT_SET_CONTEXT_MAX_SIZE ); res = parent->i_sb->s_cop->set_context(child, &ctx,include/linux/fscrypt.h +4
-4@@ -99,12 +99,12 @@ static inline bool fscrypt_dummy_context_enabled(struct inode *inode)static inline bool fscrypt_valid_enc_modes (u32 contents_mode,u32 filenames_mode) { -if (contents_mode =FS_ENCRYPTION_MODE_AES_128_CBC && - filenames_mode =FS_ENCRYPTION_MODE_AES_128_CTS ) +if (contents_mode =FSCRYPT_MODE_AES_128_CBC && + filenames_mode =FSCRYPT_MODE_AES_128_CTS )return true; -if (contents_mode =FS_ENCRYPTION_MODE_AES_256_XTS && - filenames_mode =FS_ENCRYPTION_MODE_AES_256_CTS ) +if (contents_mode =FSCRYPT_MODE_AES_256_XTS && + filenames_mode =FSCRYPT_MODE_AES_256_CTS )return true;return false;Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 4/25 fscrypt: refactor finding and deriving key
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
In preparation for introducing a new way to find the master keys and derive the per-file keys, clean up the current method. This includes:
- Introduce a helper function find_and_derive_key() so that we don't have to add more code directly to fscrypt_get_encryption_info().
- Don't pass the 'struct fscrypt_key' directly into derive_key_aes(). This is in preparation for the case where we find the master key in a filesystem-level keyring, where (for good reasons) the key payload will *not* be formatted as the UAPI 'struct fscrypt_key'.
- Separate finding the key from key derivation. In particular, it *only* makes sense to fall back to the alternate key description prefix if searching for the "fscrypt:" prefix returns -ENOKEY. It doesn't make sense to do so when derive_key_aes() fails, for example.
- Improve the error messages for when the fscrypt_key is invalid.
- Rename 'raw_key' to 'derived_key' for clarity.
--- fs/crypto/keyinfo.c | 205 ++++++++++++++++++++++++++++------------------------ 1 file changed, 109 insertions(+), 96 deletions(-)
fs/crypto/keyinfo.c +109 -96
@@ -28,113 +28,138 @@ static void derive_crypt_complete(struct crypto_async_request *req, int rc)complete (&ecr->completion); } -/** -* derive_key_aes() - Derive a key using AES-128-ECB -* @deriving_key: Encryption key used for derivation. -* @source_key: Source key to which to apply derivation. -* @derived_raw_key: Derived raw key. +/* +* Key derivation function. This generates the derived key by encrypting the +* master key with AES-128-ECB using the nonce as the AES key. * -* Return: Zero on success; non-zero otherwise. +* The master key must be at least as long as the derived key. If the master +* key is longer, then only the first 'derived_keysize' bytes are used. */ -static int derive_key_aes (u8 deriving_key[FS_AES_128_ECB_KEY_SIZE ], -const struct fscrypt_key *source_key, -u8 derived_raw_key[FSCRYPT_MAX_KEY_SIZE ]) +static int derive_key_aes (const u8 *master_key, +const struct fscrypt_context *ctx, +u8 *derived_key,unsigned int derived_keysize) { -int res =0 ; +int err;struct skcipher_request *req =NULL ;DECLARE_FS_COMPLETION_RESULT (ecr);struct scatterlist src_sg, dst_sg; -struct crypto_skcipher *tfm =crypto_alloc_skcipher ("ecb(aes)" ,0 ,0 ); +struct crypto_skcipher *tfm; + + tfm =crypto_alloc_skcipher ("ecb(aes)" ,0 ,0 ); +if (IS_ERR (tfm)) +return PTR_ERR (tfm); -if (IS_ERR (tfm)) { - res =PTR_ERR (tfm); - tfm =NULL ; - goto out; - }crypto_skcipher_set_flags (tfm,CRYPTO_TFM_REQ_WEAK_KEY ); req =skcipher_request_alloc (tfm,GFP_NOFS );if (!req) { - res = -ENOMEM ; + err = -ENOMEM ; goto out; }skcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG |CRYPTO_TFM_REQ_MAY_SLEEP , derive_crypt_complete, &ecr); - res =crypto_skcipher_setkey (tfm, deriving_key, -FS_AES_128_ECB_KEY_SIZE ); -if (res <0 ) + +BUILD_BUG_ON (sizeof (ctx->nonce) !=FS_AES_128_ECB_KEY_SIZE ); + err =crypto_skcipher_setkey (tfm, ctx->nonce,sizeof (ctx->nonce)); +if (err) goto out; -sg_init_one (&src_sg, source_key->raw, source_key->size); -sg_init_one (&dst_sg, derived_raw_key, source_key->size); -skcipher_request_set_crypt (req, &src_sg, &dst_sg, source_key->size, +sg_init_one (&src_sg, master_key, derived_keysize); +sg_init_one (&dst_sg, derived_key, derived_keysize); +skcipher_request_set_crypt (req, &src_sg, &dst_sg, derived_keysize,NULL ); - res =crypto_skcipher_encrypt (req); -if (res = -EINPROGRESS || res = -EBUSY ) { + err =crypto_skcipher_encrypt (req); +if (err = -EINPROGRESS || err = -EBUSY ) {wait_for_completion (&ecr.completion); - res = ecr.res; + err = ecr.res; } out:skcipher_request_free (req);crypto_free_skcipher (tfm); -return res; +return err; } -static int validate_user_key (struct fscrypt_info *crypt_info, -struct fscrypt_context *ctx,u8 *raw_key, -const char *prefix,int min_keysize) +/* +* Search the current task's subscribed keyrings for a "logon" key with +* description prefix:descriptor, and if found acquire a read lock on it and +* return a pointer to its validated payload in *payload_ret. +*/ +static struct key * +find_and_lock_process_key (const char *prefix, +const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ], +unsigned int min_keysize, +const struct fscrypt_key **payload_ret) {char *description; -struct key *keyring_key; -struct fscrypt_key *master_key; +struct key *key;const struct user_key_payload *ukp; -int res; +const struct fscrypt_key *payload; description =kasprintf (GFP_NOFS ,"%s%*phN" , prefix, -FSCRYPT_KEY_DESCRIPTOR_SIZE , - ctx->master_key_descriptor); +FSCRYPT_KEY_DESCRIPTOR_SIZE , descriptor);if (!description) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); - keyring_key =request_key (&key_type_logon, description,NULL ); + key =request_key (&key_type_logon, description,NULL );kfree (description); -if (IS_ERR (keyring_key)) -return PTR_ERR (keyring_key); -down_read (&keyring_key->sem); - -if (keyring_key->type != &key_type_logon) { -printk_once (KERN_WARNING -"%s: key type must be logon\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 (IS_ERR (key)) +return key; + +down_read (&key->sem); + ukp =user_key_payload_locked (key); + +if (!ukp)/* was the key revoked before we acquired its semaphore? */ + 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 ) { +pr_warn_ratelimited ("fscrypt: key with description '%s' has invalid payload\n" , + key->description); + goto invalid; } -if (ukp->datalen !=sizeof (struct fscrypt_key )) { - res = -EINVAL ; - goto out; + +if (payload->size < min_keysize) { +pr_warn_ratelimited ("fscrypt: key with description '%s' is too short " +"(got %u bytes, need %u+ bytes)\n" , + key->description, + payload->size, min_keysize); + goto invalid; } - 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 >FSCRYPT_MAX_KEY_SIZE - || master_key->size %AES_BLOCK_SIZE !=0 ) { -printk_once (KERN_WARNING -"%s: key size incorrect: %d\n" , - __func__, master_key->size); - res = -ENOKEY ; - goto out; + + *payload_ret = payload; +return key; + +invalid: +up_read (&key->sem); +key_put (key); +return ERR_PTR (-ENOKEY ); +} + +/* Find the master key, then derive the inode's actual encryption key */ +static int find_and_derive_key (const struct inode *inode, +const struct fscrypt_context *ctx, +u8 *derived_key,unsigned int derived_keysize) +{ +struct key *key; +const struct fscrypt_key *payload; +int err; + + key =find_and_lock_process_key (FSCRYPT_KEY_DESC_PREFIX , + ctx->master_key_descriptor, + derived_keysize, &payload); +if (key =ERR_PTR (-ENOKEY ) && inode->i_sb->s_cop->key_prefix) { + key =find_and_lock_process_key (inode->i_sb->s_cop->key_prefix, + ctx->master_key_descriptor, + derived_keysize, &payload); } - res =derive_key_aes (ctx->nonce, master_key, raw_key); -out: -up_read (&keyring_key->sem); -key_put (keyring_key); -return res; +if (IS_ERR (key)) +return PTR_ERR (key); + err =derive_key_aes (payload->raw, ctx, derived_key, derived_keysize); +up_read (&key->sem); +key_put (key); +return err; } static const struct {
@@ -256,8 +281,8 @@ int fscrypt_get_encryption_info(struct inode *inode)struct fscrypt_context ctx;struct crypto_skcipher *ctfm;const char *cipher_str; -int keysize; -u8 *raw_key =NULL ; +unsigned int derived_keysize; +u8 *derived_key =NULL ;int res;if (inode->i_crypt_info)
@@ -301,7 +326,8 @@ int fscrypt_get_encryption_info(struct inode *inode)memcpy (crypt_info->ci_master_key, ctx.master_key_descriptor,sizeof (crypt_info->ci_master_key)); - res =determine_cipher_type (crypt_info, inode, &cipher_str, &keysize); + res =determine_cipher_type (crypt_info, inode, + &cipher_str, &derived_keysize);if (res) goto out;
@@ -310,24 +336,14 @@ int fscrypt_get_encryption_info(struct inode *inode)* crypto API as part of key derivation. */ res = -ENOMEM ; - raw_key =kmalloc (FSCRYPT_MAX_KEY_SIZE ,GFP_NOFS ); -if (!raw_key) + derived_key =kmalloc (FS_MAX_KEY_SIZE ,GFP_NOFS ); +if (!derived_key) goto out; - res =validate_user_key (crypt_info, &ctx, raw_key, -FSCRYPT_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); -if (res2) { -if (res2 = -ENOKEY ) - res = -ENOKEY ; - goto out; - } - }else if (res) { + res =find_and_derive_key (inode, &ctx, derived_key, derived_keysize); +if (res) goto out; - } + ctfm =crypto_alloc_skcipher (cipher_str,0 ,0 );if (!ctfm ||IS_ERR (ctfm)) { res = ctfm ?PTR_ERR (ctfm) : -ENOMEM ;
@@ -338,17 +354,14 @@ int fscrypt_get_encryption_info(struct inode *inode) crypt_info->ci_ctfm = ctfm;crypto_skcipher_clear_flags (ctfm, ~0 );crypto_skcipher_set_flags (ctfm,CRYPTO_TFM_REQ_WEAK_KEY ); -/* -* if the provided key is longer than keysize, we use the first -* keysize bytes of the derived key only -*/ - res =crypto_skcipher_setkey (ctfm, raw_key, keysize); + res =crypto_skcipher_setkey (ctfm, derived_key, derived_keysize);if (res) goto out;if (S_ISREG (inode->i_mode) && crypt_info->ci_data_mode =FSCRYPT_MODE_AES_128_CBC ) { - res =init_essiv_generator (crypt_info, raw_key, keysize); + res =init_essiv_generator (crypt_info, derived_key, + derived_keysize);if (res) {pr_debug ("%s: error %d (inode %lu) allocating essiv tfm\n" , __func__, res, inode->i_ino);
@@ -361,7 +374,7 @@ int fscrypt_get_encryption_info(struct inode *inode)if (res = -ENOKEY ) res =0 ;put_crypt_info (crypt_info); -kzfree (raw_key); +kzfree (derived_key);return res; }EXPORT_SYMBOL (fscrypt_get_encryption_info);
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Re: [RFC PATCH 04/25] fscrypt: refactor finding and deriving key
Quoted 13 lines · Eric Biggers
From: Eric Biggers <[email protected]>
In preparation for introducing a new way to find the master keys and derive the per-file keys, clean up the current method. This includes:
- Introduce a helper function
find_and_derive_key()so that we don't have to add more code directly tofscrypt_get_encryption_info().- Don't pass the 'struct
fscrypt_key' directly intoderive_key_aes(). This is in preparation for the case where we find the master key in a filesystem-level keyring, where (for good reasons) the key payload will *not* be formatted as the UAPI 'structfscrypt_key'.
Quoted 8 lines
- Separate finding the key from key derivation. In particular, it *only* makes sense to fall back to the alternate key description prefix if searching for the "fscrypt:" prefix returns
-ENOKEY. It doesn't make sense to do so whenderive_key_aes()fails, for example.- Improve the error messages for when the
fscrypt_keyis invalid.- Rename '
raw_key' to 'derived_key' for clarity.
With all the crypto code I've delt with where a 'key' is actually just a handle or a reference, I've developed a personal habit is to call the buffer that contains the actual key bytes 'raw' to emphasize that there's tangible secret material present.
That said, it's probably fine to call it 'derived_key' in this instance.
Quoted 2 lines
Signed-off-by Eric Biggers <[email protected]>
Quoted 93 lines
--- fs/crypto/keyinfo.c | 205 ++++++++++++++++++++++++++++------------------------ 1 file changed, 109 insertions(+), 96 deletions(-)fs/crypto/keyinfo.c +26
-30@@ -28,113 +28,138 @@ static void derive_crypt_complete(struct crypto_async_request *req, int rc)complete (&ecr->completion); } -/** -* derive_key_aes() - Derive a key using AES-128-ECB -* @deriving_key: Encryption key used for derivation. -* @source_key: Source key to which to apply derivation. -* @derived_raw_key: Derived raw key. +/* +* Key derivation function. This generates the derived key by encrypting the +* master key with AES-128-ECB using the nonce as the AES key. * -* Return: Zero on success; non-zero otherwise. +* The master key must be at least as long as the derived key. If the master +* key is longer, then only the first 'derived_keysize' bytes are used. */ -static int derive_key_aes (u8 deriving_key[FS_AES_128_ECB_KEY_SIZE ], -const struct fscrypt_key *source_key, -u8 derived_raw_key[FSCRYPT_MAX_KEY_SIZE ]) +static int derive_key_aes (const u8 *master_key, +const struct fscrypt_context *ctx, +u8 *derived_key,unsigned int derived_keysize) { -int res =0 ; +int err;struct skcipher_request *req =NULL ;DECLARE_FS_COMPLETION_RESULT (ecr);struct scatterlist src_sg, dst_sg; -struct crypto_skcipher *tfm =crypto_alloc_skcipher ("ecb(aes)" ,0 ,0 ); +struct crypto_skcipher *tfm; + + tfm =crypto_alloc_skcipher ("ecb(aes)" ,0 ,0 ); +if (IS_ERR (tfm)) +return PTR_ERR (tfm); -if (IS_ERR (tfm)) { - res =PTR_ERR (tfm); - tfm =NULL ; - goto out; - }crypto_skcipher_set_flags (tfm,CRYPTO_TFM_REQ_WEAK_KEY ); req =skcipher_request_alloc (tfm,GFP_NOFS );if (!req) { - res = -ENOMEM ; + err = -ENOMEM ; goto out; }skcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG |CRYPTO_TFM_REQ_MAY_SLEEP , derive_crypt_complete, &ecr); - res =crypto_skcipher_setkey (tfm, deriving_key, -FS_AES_128_ECB_KEY_SIZE ); -if (res <0 ) + +BUILD_BUG_ON (sizeof (ctx->nonce) !=FS_AES_128_ECB_KEY_SIZE ); + err =crypto_skcipher_setkey (tfm, ctx->nonce,sizeof (ctx->nonce)); +if (err) goto out; -sg_init_one (&src_sg, source_key->raw, source_key->size); -sg_init_one (&dst_sg, derived_raw_key, source_key->size); -skcipher_request_set_crypt (req, &src_sg, &dst_sg, source_key->size, +sg_init_one (&src_sg, master_key, derived_keysize); +sg_init_one (&dst_sg, derived_key, derived_keysize); +skcipher_request_set_crypt (req, &src_sg, &dst_sg, derived_keysize,NULL ); - res =crypto_skcipher_encrypt (req); -if (res = -EINPROGRESS || res = -EBUSY ) { + err =crypto_skcipher_encrypt (req); +if (err = -EINPROGRESS || err = -EBUSY ) {wait_for_completion (&ecr.completion); - res = ecr.res; + err = ecr.res; } out:skcipher_request_free (req);crypto_free_skcipher (tfm); -return res; +return err; } -static int validate_user_key (struct fscrypt_info *crypt_info, -struct fscrypt_context *ctx,u8 *raw_key, -const char *prefix,int min_keysize)
fs/crypto/keyinfo.c static void derive_crypt_complete(struct crypto_async_request *req, int rc) · line 91
Quoted 10 lines
fs/crypto/keyinfo.c +10
-0+/* +* Search the current task's subscribed keyrings for a "logon" key with +* description prefix:descriptor, and if found acquire a read lock on it and +* return a pointer to its validated payload in *payload_ret. +*/ +static struct key * +find_and_lock_process_key (const char *prefix, +const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ], +unsigned int min_keysize, +const struct fscrypt_key **payload_ret)
Since we've already crossed the threshold of returning at least one object via a ptr-to-ptr param, maybe doing that for struct key too and just making the return value of the function be an int err would be cleaner than PTR_ERR/ERR_PTR?
Quoted 52 lines
fs/crypto/keyinfo.c +21
-22{char *description; -struct key *keyring_key; -struct fscrypt_key *master_key; +struct key *key;const struct user_key_payload *ukp; -int res; +const struct fscrypt_key *payload; description =kasprintf (GFP_NOFS ,"%s%*phN" , prefix, -FSCRYPT_KEY_DESCRIPTOR_SIZE , - ctx->master_key_descriptor); +FSCRYPT_KEY_DESCRIPTOR_SIZE , descriptor);if (!description) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); - keyring_key =request_key (&key_type_logon, description,NULL ); + key =request_key (&key_type_logon, description,NULL );kfree (description); -if (IS_ERR (keyring_key)) -return PTR_ERR (keyring_key); -down_read (&keyring_key->sem); - -if (keyring_key->type != &key_type_logon) { -printk_once (KERN_WARNING -"%s: key type must be logon\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 (IS_ERR (key)) +return key; + +down_read (&key->sem); + ukp =user_key_payload_locked (key); + +if (!ukp)/* was the key revoked before we acquired its semaphore? */ + 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 ) { +pr_warn_ratelimited ("fscrypt: key with description '%s' has invalid payload\n" , + key->description); + goto invalid; }
fs/crypto/keyinfo.c static void derive_crypt_complete(struct crypto_async_request *req, int rc) · line 128
Quoted 10 lines
fs/crypto/keyinfo.c +7
-3-if (ukp->datalen !=sizeof (struct fscrypt_key )) { - res = -EINVAL ; - goto out; + +if (payload->size < min_keysize) { +pr_warn_ratelimited ("fscrypt: key with description '%s' is too short " +"(got %u bytes, need %u+ bytes)\n" , + key->description, + payload->size, min_keysize); + goto invalid;
A common (yet high-impact) mistake is to pass in only 256 bits of entropic key material in the 512-bit buffer for AES-256-XTS, leaving the second half all 0's. I've actually seen that done in pre-release code. It would be an easy check just to see if userspace did that, but then we're on the slippery slope of how much key strength validation we should do on the key, if we're going to do any at all.
Quoted 137 lines
fs/crypto/keyinfo.c +45
-41} - 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 >FSCRYPT_MAX_KEY_SIZE - || master_key->size %AES_BLOCK_SIZE !=0 ) { -printk_once (KERN_WARNING -"%s: key size incorrect: %d\n" , - __func__, master_key->size); - res = -ENOKEY ; - goto out; + + *payload_ret = payload; +return key; + +invalid: +up_read (&key->sem); +key_put (key); +return ERR_PTR (-ENOKEY ); +} + +/* Find the master key, then derive the inode's actual encryption key */ +static int find_and_derive_key (const struct inode *inode, +const struct fscrypt_context *ctx, +u8 *derived_key,unsigned int derived_keysize) +{ +struct key *key; +const struct fscrypt_key *payload; +int err; + + key =find_and_lock_process_key (FSCRYPT_KEY_DESC_PREFIX , + ctx->master_key_descriptor, + derived_keysize, &payload); +if (key =ERR_PTR (-ENOKEY ) && inode->i_sb->s_cop->key_prefix) { + key =find_and_lock_process_key (inode->i_sb->s_cop->key_prefix, + ctx->master_key_descriptor, + derived_keysize, &payload); } - res =derive_key_aes (ctx->nonce, master_key, raw_key); -out: -up_read (&keyring_key->sem); -key_put (keyring_key); -return res; +if (IS_ERR (key)) +return PTR_ERR (key); + err =derive_key_aes (payload->raw, ctx, derived_key, derived_keysize); +up_read (&key->sem); +key_put (key); +return err; } static const struct {@@ -256,8 +281,8 @@ int fscrypt_get_encryption_info(struct inode *inode)struct fscrypt_context ctx;struct crypto_skcipher *ctfm;const char *cipher_str; -int keysize; -u8 *raw_key =NULL ; +unsigned int derived_keysize; +u8 *derived_key =NULL ;int res;if (inode->i_crypt_info)@@ -301,7 +326,8 @@ int fscrypt_get_encryption_info(struct inode *inode)memcpy (crypt_info->ci_master_key, ctx.master_key_descriptor,sizeof (crypt_info->ci_master_key)); - res =determine_cipher_type (crypt_info, inode, &cipher_str, &keysize); + res =determine_cipher_type (crypt_info, inode, + &cipher_str, &derived_keysize);if (res) goto out;@@ -310,24 +336,14 @@ int fscrypt_get_encryption_info(struct inode *inode)* crypto API as part of key derivation. */ res = -ENOMEM ; - raw_key =kmalloc (FSCRYPT_MAX_KEY_SIZE ,GFP_NOFS ); -if (!raw_key) + derived_key =kmalloc (FS_MAX_KEY_SIZE ,GFP_NOFS ); +if (!derived_key) goto out; - res =validate_user_key (crypt_info, &ctx, raw_key, -FSCRYPT_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); -if (res2) { -if (res2 = -ENOKEY ) - res = -ENOKEY ; - goto out; - } - }else if (res) { + res =find_and_derive_key (inode, &ctx, derived_key, derived_keysize); +if (res) goto out; - } + ctfm =crypto_alloc_skcipher (cipher_str,0 ,0 );if (!ctfm ||IS_ERR (ctfm)) { res = ctfm ?PTR_ERR (ctfm) : -ENOMEM ;@@ -338,17 +354,14 @@ int fscrypt_get_encryption_info(struct inode *inode) crypt_info->ci_ctfm = ctfm;crypto_skcipher_clear_flags (ctfm, ~0 );crypto_skcipher_set_flags (ctfm,CRYPTO_TFM_REQ_WEAK_KEY ); -/* -* if the provided key is longer than keysize, we use the first -* keysize bytes of the derived key only -*/ - res =crypto_skcipher_setkey (ctfm, raw_key, keysize); + res =crypto_skcipher_setkey (ctfm, derived_key, derived_keysize);if (res) goto out;if (S_ISREG (inode->i_mode) && crypt_info->ci_data_mode =FSCRYPT_MODE_AES_128_CBC ) { - res =init_essiv_generator (crypt_info, raw_key, keysize); + res =init_essiv_generator (crypt_info, derived_key, + derived_keysize);if (res) {pr_debug ("%s: error %d (inode %lu) allocating essiv tfm\n" , __func__, res, inode->i_ino);@@ -361,7 +374,7 @@ int fscrypt_get_encryption_info(struct inode *inode)if (res = -ENOKEY ) res =0 ;put_crypt_info (crypt_info); -kzfree (raw_key); +kzfree (derived_key);return res; }EXPORT_SYMBOL (fscrypt_get_encryption_info);Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 5/25 fs: add ->s_master_keys to struct super_block
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add an ->s_master_keys keyring to 'struct super_block' for holding encryption keys which have been added to the filesystem. This keyring will be populated using a new fscrypt ioctl.
This is needed for several reasons, including:
- To solve the visibility problems of having filesystem encryption keys stored in process-subscribed keyrings, while the VFS state of the filesystem is actually global.
- To implement a proper API for removing keys, which among other things will require maintaining the list of inodes that are using each master key so that we can evict the inodes when the key is removed.
- To allow caching a crypto transform for each master key so that we don't have to repeatedly allocate one over and over.
See later patches for full details, including why it wouldn't be enough to add the concept of a "global keyring" to the keyrings API instead.
->s_master_keys will only be allocated when someone tries to add a key for the first time. Otherwise it will stay NULL.
Note that this could go in the filesystem-specific superblocks instead. However, we already have three filesystems using fs/crypto/, so it's useful to have it in the VFS.
--- fs/super.c | 3 +++ include/linux/fs.h | 4 ++++ 2 files changed, 7 insertions(+)
fs/super.c +3 -0
@@ -168,6 +168,9 @@ static void destroy_super(struct super_block *s)WARN_ON (!list_empty (&s->s_mounts));put_user_ns (s->s_user_ns);kfree (s->s_subtype); +#if IS_ENABLED (CONFIG_FS_ENCRYPTION ) +key_put (s->s_master_keys); +#endif call_rcu (&s->rcu, destroy_super_rcu); }
include/linux/fs.h +4 -0
@@ -1440,6 +1440,10 @@ struct super_block {spinlock_t s_inode_wblist_lock;struct list_head s_inodes_wb;/* writeback inodes */ + +#if IS_ENABLED (CONFIG_FS_ENCRYPTION ) +struct key *s_master_keys;/* master crypto keys in use */ +#endif } __randomize_layout;/* Helper functions so that in most cases filesystems will
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Re: [RFC PATCH 05/25] fs: add ->s_master_keys to struct super_block
Quoted 22 lines · Eric Biggers
From: Eric Biggers <[email protected]>
Add an ->s_master_keys keyring to 'struct
super_block' for holding encryption keys which have been added to the filesystem. This keyring will be populated using a new fscrypt ioctl.This is needed for several reasons, including:
- To solve the visibility problems of having filesystem encryption keys stored in process-subscribed keyrings, while the VFS state of the filesystem is actually global.
- To implement a proper API for removing keys, which among other things will require maintaining the list of inodes that are using each master key so that we can evict the inodes when the key is removed.
- To allow caching a crypto transform for each master key so that we don't have to repeatedly allocate one over and over.
See later patches for full details, including why it wouldn't be enough to add the concept of a "global keyring" to the keyrings API instead.
Quoted 8 lines
->s_master_keys will only be allocated when someone tries to add a key for the first time. Otherwise it will stay NULL.
Note that this could go in the filesystem-specific superblocks instead. However, we already have three filesystems using
fs/crypto/, so it's useful to have it in the VFS.Signed-off-by Eric Biggers <[email protected]>
Quoted 37 lines
--- fs/super.c | 3 +++ include/linux/fs.h | 4 ++++ 2 files changed, 7 insertions(+)fs/super.c +3
-0@@ -168,6 +168,9 @@ static void destroy_super(struct super_block *s)WARN_ON (!list_empty (&s->s_mounts));put_user_ns (s->s_user_ns);kfree (s->s_subtype); +#if IS_ENABLED (CONFIG_FS_ENCRYPTION ) +key_put (s->s_master_keys); +#endif call_rcu (&s->rcu, destroy_super_rcu); }include/linux/fs.h +4
-0@@ -1440,6 +1440,10 @@ struct super_block {spinlock_t s_inode_wblist_lock;struct list_head s_inodes_wb;/* writeback inodes */ + +#if IS_ENABLED (CONFIG_FS_ENCRYPTION ) +struct key *s_master_keys;/* master crypto keys in use */ +#endif } __randomize_layout;/* Helper functions so that in most cases filesystems will Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 6/25 fscrypt: add FS_IOC_ADD_ENCRYPTION_KEY ioctl
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add a new filesystem encryption ioctl, FS_IOC_ADD_ENCRYPTION_KEY. This ioctl adds a master encryption key to the filesystem encryption keyring ->s_master_keys.
When a process tries to access an encrypted file that has not yet been "unlocked" (set up with an ->i_crypt_info containing a crypto transform keyed by the file's derived key), fscrypt_get_encryption_info() will search for the master key in ->s_master_keys before falling back to the process-subscribed keyrings.
For now this ioctl is root-only, which is necessary in part because the keys are identified by master_key_descriptor, which is not cryptographically tied to the actual key payload. However, a later patch will introduce a new encryption policy version where the key is identified by a cryptographic hash. This will, in combination with other protections, make it possible for non-root users to use this ioctl in some situations.
Why we need this
~~~~~~~~~~~~~~~~
The main problem is that the "locked/unlocked" (ciphertext/plaintext) status of encrypted files is global, but currently the master keys are not. We only look for master keys in the process-subscribed keyrings; that is, the current thread keyring, process keyring, and session keyring, where the session keyring usually contains the user keyring. This means we have to put the master keys in the keyrings for individual users or for individual sessions. This causes much confusion as soon as a process with a different UID, such as a 'sudo' command, tries to access encrypted files. In such a situation, whether each individual inode appears "locked" or "unlocked" will depend on whether it was previously accessed and happens to still be in the inode cache, which is more or less nondeterministic.
It may seem that we should indeed provide each process its own "view" of the filesystem depending on whether it "has the key" or not. However that would be extremely difficult to do without a separate mount, due to the way the VFS caches work. Furthermore, it is actually missing the point of encryption because it would *not* be encryption that would provide the different "views", but rather kernel *code*. Thus, it would simply be an access control mechanism largely redundant with the many existing access control mechanisms such as UNIX file permissions and LSMs. The reality is that the confidentially of encrypted files *after the kernel already has the encryption key in memory* is only protected by the correctness of the kernel, not by the mathematical properties of encryption.
And at the end of the day, almost all users of filesystem encryption we are aware of do really need the global view, because they need encrypted files to be accessible to processes running under different UIDs. This can be as simple as needing to be able to run 'sudo', or it can be something more complex like Android's key management system where applications running under different UIDs as well as system processes need access to the same encrypted files.
As a result, some very ugly hacks have been added to try to emulate globally visible keys. The Android and Chromium OS key management systems simply create a "session" keyring in PID 1 and put all the keys in it, which abuses the "session" keyring to have nothing to do with a "session", but rather be a global keyring. This is fragile, as it means that the "session" keyring must never be changed. There have also been bugs involving processes that were forked before the "session" keyring was created, causing them to miss out on the keys.
Meanwhile, filesystem encryption tools written for general-purpose Linux distributions have no such ability to abuse the "session" keyring. They instead must implement "interesting" workarounds such as linking all the user keyrings into root's user keyring, as is done by the fscrypt userspace tool (see the design document at https://goo.gl/55cCrI). This raises security concerns, to say the least.
By having an API to add a key to the *filesystem* we'll be able to eliminate all the above hacks and better express the intended semantics:
the "locked/unlocked" status of an encrypted directory is global. And orthogonally to encryption, existing mechanisms such as file permissions and LSMs can and should continue to be used for the purpose of *access control*.
Why use a custom key type
~~~~~~~~~~~~~~~~~~~~~~~~~
The keys the new ioctl adds to ->s_master_keys are still "keys" in the sense of the keyrings service, but they have a custom key type rather than the "logon" key type we currently require when userspace provides a key via a process-subscribed keyring.
Judging just from this patch alone, the "logon" key type would be sufficient. However, later patches will be solving problems such as the nonstandard KDF and the lack of a key removal API. The solutions for these problems will require tracking information on a per-master-key basis. Therefore, we'll need a custom structure associated with each master key anyway. A custom key type lets us do that easily.
Why not use add_key()
~~~~~~~~~~~~~~~~~~~~~
Instead of adding a new ioctl() to add a key, we could have userspace use the add_key() system call. In combination with an ioctl which retrieves the key ID of ->s_master_keys, add_key() could be used to add a key to ->s_master_keys. Alternatively, we could add the concept of a "global keyring" or "namespace keyring" to the keyrings service, where that keyring would be searched in addition to the process-subscribed keyrings. Then, add_key() could add a key to that.
This actually makes sense given only the present patch. However, unfortunately it falls apart once we consider the follow-on changes.
First, we also need to add the ability to remove an encryption key, and it will need to have more specialized semantics than keyctl_unlink() or keyctl_revoke() can provide. For example, we must not only wipe the master key *secret* from memory, but we must also try to evict all the inodes which had been "unlocked" using the key. And it's possible that even though the master key secret was wiped, some inodes could not be evicted, since they may be busy. In that case, we still want to wipe the master key *secret* so that no more encrypted files can be "unlocked". But we also want to allow userspace to retry the request later, so that evicting the remaining inodes can be re-attempted. Alternatively, we want the same list of inodes to be picked up again if the secret happens to be added again. Trying to shoehorn these specific semantics into the keyrings API would be very difficult.
Later we also want to open up the add/remove key operations to non-root users by taking advantage of a new encryption policy version which includes a cryptographic hash of the master key. This is needed because otherwise we wouldn't be able to fully replace the process-subscribed keyrings and avoid all its problems mentioned earlier. But to actually make non-root use secure, we'll need to do some extra accounting where we keep track of all users who have added a given key, then only really remove a key after all users have removed it. Non-root users also cannot simply be given write permission to a global keyring. So again, it seems that trying to shoehorn the needed semantics into the keyrings API would just create problems.
Nevertheless, we do still use the keyrings service internally so that we reuse some code and get some "free" functionality such as having the keys show up in /proc/keys for debugging purposes.
--- fs/crypto/crypto.c | 12 +- fs/crypto/fscrypt_private.h | 3 + fs/crypto/keyinfo.c | 351 +++++++++++++++++++++++++++++++++++++++- include/linux/fscrypt_notsupp.h | 5 + include/linux/fscrypt_supp.h | 1 + include/uapi/linux/fscrypt.h | 41 +++-- 6 files changed, 397 insertions(+), 16 deletions(-)
fs/crypto/crypto.c +11 -1
@@ -24,6 +24,7 @@#include <linux/module.h> #include <linux/scatterlist.h> #include <linux/ratelimit.h> +#include <linux/key-type.h> #include <linux/dcache.h> #include <linux/namei.h> #include <crypto/aes.h>
@@ -449,6 +450,8 @@ int fscrypt_initialize(unsigned int cop_flags)*/ static int __init fscrypt_init (void ) { +int err = -ENOMEM ; + fscrypt_read_workqueue =alloc_workqueue ("fscrypt_read_queue" ,WQ_HIGHPRI ,0 );if (!fscrypt_read_workqueue)
@@ -462,14 +465,20 @@ static int __init fscrypt_init(void)if (!fscrypt_info_cachep) goto fail_free_ctx; + err =register_key_type (&key_type_fscrypt_mk); +if (err) + goto fail_free_info; +return 0 ; +fail_free_info: +kmem_cache_destroy (fscrypt_info_cachep); fail_free_ctx:kmem_cache_destroy (fscrypt_ctx_cachep); fail_free_queue:destroy_workqueue (fscrypt_read_workqueue); fail: -return -ENOMEM ; +return err; }module_init (fscrypt_init)
@@ -484,6 +493,7 @@ static void __exit fscrypt_exit(void)destroy_workqueue (fscrypt_read_workqueue);kmem_cache_destroy (fscrypt_ctx_cachep);kmem_cache_destroy (fscrypt_info_cachep); +unregister_key_type (&key_type_fscrypt_mk);fscrypt_essiv_cleanup (); }
fs/crypto/fscrypt_private.h +3 -0
@@ -27,6 +27,8 @@#define FS_KEY_DERIVATION_NONCE_SIZE 16 +#define FSCRYPT_MIN_KEY_SIZE 16 +/** * Encryption context for inode *
@@ -93,6 +95,7 @@ extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx, gfp_t gfp_flags);/* keyinfo.c */ +extern struct key_type key_type_fscrypt_mk; extern void __exit fscrypt_essiv_cleanup(void);#endif /* _FSCRYPT_PRIVATE_H */
fs/crypto/keyinfo.c +346 -5
376 lines not on this page · raw message
include/linux/fscrypt_notsupp.h +5 -0
@@ -84,6 +84,11 @@ static inline int fscrypt_inherit_context(struct inode *parent, }/* keyinfo.c */ +static inline int fscrypt_ioctl_add_key (struct file *filp,void __user *arg) +{ +return -EOPNOTSUPP ; +} +static inline int fscrypt_get_encryption_info (struct inode *inode) {return -EOPNOTSUPP ;
include/linux/fscrypt_supp.h +1 -0
@@ -42,6 +42,7 @@ extern int fscrypt_has_permitted_context(struct inode *, struct inode *);extern int fscrypt_inherit_context (struct inode *,struct inode *,void *,bool );/* keyinfo.c */ +extern int fscrypt_ioctl_add_key (struct file *filp,void __user *arg);extern int fscrypt_get_encryption_info (struct inode *);extern void fscrypt_put_encryption_info (struct inode *,struct fscrypt_info *);
include/uapi/linux/fscrypt.h +31 -10
53 lines not on this page · raw message
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Re: [RFC PATCH 06/25] fscrypt: add FS_IOC_ADD_ENCRYPTION_KEY ioctl
Quoted 6 lines · Eric Biggers
By having an API to add a key to the *filesystem* we'll be able to eliminate all the above hacks and better express the intended semantics:
the "locked/unlocked" status of an encrypted directory is global. And orthogonally to encryption, existing mechanisms such as file permissions and LSMs can and should continue to be used for the purpose of *access control*.
At some point I'd like to try to tackle the problem of making the encryption policy somehow *reflect* the access control policy.
For now this change cleans up a real mess and makes things much more manageable and predictable.
Quoted 1 line
Signed-off-by Eric Biggers <[email protected]>
Quoted 101 lines
--- fs/crypto/crypto.c | 12 +- fs/crypto/fscrypt_private.h | 3 + fs/crypto/keyinfo.c | 351 +++++++++++++++++++++++++++++++++++++++- include/linux/fscrypt_notsupp.h | 5 + include/linux/fscrypt_supp.h | 1 + include/uapi/linux/fscrypt.h | 41 +++-- 6 files changed, 397 insertions(+), 16 deletions(-)fs/crypto/crypto.c +11
-1@@ -24,6 +24,7 @@#include <linux/module.h> #include <linux/scatterlist.h> #include <linux/ratelimit.h> +#include <linux/key-type.h> #include <linux/dcache.h> #include <linux/namei.h> #include <crypto/aes.h> @@ -449,6 +450,8 @@ int fscrypt_initialize(unsigned int cop_flags)*/ static int __init fscrypt_init (void ) { +int err = -ENOMEM ; + fscrypt_read_workqueue =alloc_workqueue ("fscrypt_read_queue" ,WQ_HIGHPRI ,0 );if (!fscrypt_read_workqueue)@@ -462,14 +465,20 @@ static int __init fscrypt_init(void)if (!fscrypt_info_cachep) goto fail_free_ctx; + err =register_key_type (&key_type_fscrypt_mk); +if (err) + goto fail_free_info; +return 0 ; +fail_free_info: +kmem_cache_destroy (fscrypt_info_cachep); fail_free_ctx:kmem_cache_destroy (fscrypt_ctx_cachep); fail_free_queue:destroy_workqueue (fscrypt_read_workqueue); fail: -return -ENOMEM ; +return err; }module_init (fscrypt_init)@@ -484,6 +493,7 @@ static void __exit fscrypt_exit(void)destroy_workqueue (fscrypt_read_workqueue);kmem_cache_destroy (fscrypt_ctx_cachep);kmem_cache_destroy (fscrypt_info_cachep); +unregister_key_type (&key_type_fscrypt_mk);fscrypt_essiv_cleanup (); }fs/crypto/fscrypt_private.h +3
-0@@ -27,6 +27,8 @@#define FS_KEY_DERIVATION_NONCE_SIZE 16 +#define FSCRYPT_MIN_KEY_SIZE 16 +/** * Encryption context for inode * @@ -93,6 +95,7 @@ extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx, gfp_t gfp_flags);/* keyinfo.c */ +extern struct key_type key_type_fscrypt_mk; extern void __exit fscrypt_essiv_cleanup(void);#endif /* _FSCRYPT_PRIVATE_H */ fs/crypto/keyinfo.c +4
-1@@ -9,14 +9,307 @@*/ #include <keys/user-type.h> -#include <linux/scatterlist.h> +#include <linux/key-type.h> #include <linux/ratelimit.h> +#include <linux/scatterlist.h> +#include <linux/seq_file.h> #include <crypto/aes.h> #include <crypto/sha.h> #include "fscrypt_private.h" static struct crypto_shash *essiv_hash_tfm; +/*
fs/crypto/keyinfo.c line 32
Quoted 10 lines
fs/crypto/keyinfo.c +10
-0+* fscrypt_master_key_secret - secret key material of an in-use master key +*/ +struct fscrypt_master_key_secret { + +/* Size of the raw key in bytes */ +u32 size; + +/* The raw key */ +u8 raw[FSCRYPT_MAX_KEY_SIZE ]; +};
With structs fscrypt introduces, I suggest __randomize_layout wherever feasible.
fs/crypto/keyinfo.c line 35
Quoted 3 lines
fs/crypto/keyinfo.c +2
-0+#define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \ + (sizeof("fscrypt-") - 1 + sizeof(((struct super_block *)0)->s_id) + 1)
What function do the " - 1" and " + 1" parts serve here? Readability?
fs/crypto/keyinfo.c static int find_and_derive_key(const struct inode *inode, · line 478
Quoted 7 lines
fs/crypto/keyinfo.c +7
-0+ key =find_master_key (inode->i_sb, &mk_spec); +if (IS_ERR (key)) { +if (key !=ERR_PTR (-ENOKEY )) +return PTR_ERR (key); +/* +* As a legacy fallback, we search the current task's subscribed +* keyrings in addition to ->s_master_keys.
Please add an explicit comment that it's important for security that the ordering of these two searches be preserved.
fs/crypto/keyinfo.c static int find_and_derive_key(const struct inode *inode, · line 490
Quoted 12 lines
fs/crypto/keyinfo.c +12
-0+*/ +return find_and_derive_key_legacy (inode, ctx, derived_key, + derived_keysize); + } + mk = key->payload.data[0 ]; + +/* +* Require that the master key be at least as long as the derived key. +* Otherwise, the derived key cannot possibly contain as much entropy as +* that required by the encryption mode it will be used for. +*/ +if (mk->mk_secret.size < derived_keysize) {
As I've mentioned in a previous patch in this set, if we're going to get opinionated about source entropy, there's more we could measure/estimate than just the length.
RFC PATCH 7/25 fs/inode.c: export inode_lru_list_del()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
When a filesystem encryption key is removed, we need all files which had been "unlocked" (had ->i_crypt_info set up) with it to appear "locked" again. This is most easily done by evicting the inodes. This can currently be done using 'echo 2 > /proc/sys/vm/drop_caches'; however, that is overkill and not usable by non-root users. In preparation for allowing fs/crypto/ to evict just the needed inodes, export inode_lru_list_del() to modules.
--- fs/inode.c | 5 ++--- include/linux/fs.h | 1 + 2 files changed, 3 insertions(+), 3 deletions(-)
fs/inode.c +2 -3
@@ -420,13 +420,12 @@ void inode_add_lru(struct inode *inode)inode_lru_list_add (inode); } - -static void inode_lru_list_del (struct inode *inode) +void inode_lru_list_del (struct inode *inode) { -if (list_lru_del (&inode->i_sb->s_inode_lru, &inode->i_lru))this_cpu_dec (nr_unused); } +EXPORT_SYMBOL_GPL (inode_lru_list_del);/** * inode_sb_list_add - add inode to the superblock list of inodes
include/linux/fs.h +1 -0
@@ -2879,6 +2879,7 @@ static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };extern void unlock_new_inode (struct inode *);extern unsigned int get_next_ino (void );extern void evict_inodes (struct super_block *sb); +extern void inode_lru_list_del (struct inode *inode);extern void __iget (struct inode * inode);extern void iget_failed (struct inode *);
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Re: [RFC PATCH 07/25] fs/inode.c: export inode_lru_list_del()
Quoted 11 lines · Eric Biggers
From: Eric Biggers <[email protected]>
When a filesystem encryption key is removed, we need all files which had been "unlocked" (had ->i_crypt_info set up) with it to appear "locked" again. This is most easily done by evicting the inodes. This can currently be done using 'echo 2 >
/proc/sys/vm/drop_caches'; however, that is overkill and not usable by non-root users. In preparation for allowingfs/crypto/to evict just the needed inodes, exportinode_lru_list_del()to modules.Signed-off-by Eric Biggers <[email protected]>
Quoted 40 lines
--- fs/inode.c | 5 ++--- include/linux/fs.h | 1 + 2 files changed, 3 insertions(+), 3 deletions(-)fs/inode.c +2
-3@@ -420,13 +420,12 @@ void inode_add_lru(struct inode *inode)inode_lru_list_add (inode); } - -static void inode_lru_list_del (struct inode *inode) +void inode_lru_list_del (struct inode *inode) { -if (list_lru_del (&inode->i_sb->s_inode_lru, &inode->i_lru))this_cpu_dec (nr_unused); } +EXPORT_SYMBOL_GPL (inode_lru_list_del);/** * inode_sb_list_add - add inode to the superblock list of inodes include/linux/fs.h +1
-0@@ -2879,6 +2879,7 @@ static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };extern void unlock_new_inode (struct inode *);extern unsigned int get_next_ino (void );extern void evict_inodes (struct super_block *sb); +extern void inode_lru_list_del (struct inode *inode);extern void __iget (struct inode * inode);extern void iget_failed (struct inode *);Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 8/25 fs/inode.c: rename and export dispose_list()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
When a filesystem encryption key is removed, we need all files which had been "unlocked" (had ->i_crypt_info set up) with it to appear "locked" again. This is most easily done by evicting the inodes. This can currently be done using 'echo 2 > /proc/sys/vm/drop_caches'; however, that is overkill and not usable by non-root users. In preparation for allowing fs/crypto/ to evict just the needed inodes, export dispose_list() to modules. For clarity also rename it to evict_inode_list().
--- fs/inode.c | 19 ++++++++++--------- include/linux/fs.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-)
fs/inode.c +10 -9
@@ -570,13 +570,13 @@ static void evict(struct inode *inode) }/* -* dispose_list - dispose of the contents of a local list -* @head: the head of the list to free +* evict_inode_list - evict each inode in a local list of inodes +* @head: the head of the list * -* Dispose-list gets a local list with local inodes in it, so it doesn't +* This gets a local list with local inodes in it, so it doesn't * need to worry about list corruption and SMP locks. */ -static void dispose_list (struct list_head *head) +void evict_inode_list (struct list_head *head) {while (!list_empty (head)) {struct inode *inode;
@@ -588,6 +588,7 @@ static void dispose_list(struct list_head *head)cond_resched (); } } +EXPORT_SYMBOL_GPL (evict_inode_list);/** * evict_inodes - evict all evictable inodes for a superblock
@@ -628,13 +629,13 @@ void evict_inodes(struct super_block *sb)if (need_resched ()) {spin_unlock (&sb->s_inode_list_lock);cond_resched (); -dispose_list (&dispose); +evict_inode_list (&dispose); goto again; } }spin_unlock (&sb->s_inode_list_lock); -dispose_list (&dispose); +evict_inode_list (&dispose); }EXPORT_SYMBOL_GPL (evict_inodes);
@@ -679,7 +680,7 @@ int invalidate_inodes(struct super_block *sb, bool kill_dirty) }spin_unlock (&sb->s_inode_list_lock); -dispose_list (&dispose); +evict_inode_list (&dispose);return busy; }
@@ -763,7 +764,7 @@ static enum lru_status inode_lru_isolate(struct list_head *item,* Walk the superblock inode LRU for freeable inodes and attempt to free them. * This is called from the superblock shrinker function with a number of inodes * to trim from the LRU. Inodes to be freed are moved to a temporary list and -* then are freed outside inode_lock by dispose_list(). +* then are freed outside inode_lock by evict_inode_list(). */ long prune_icache_sb (struct super_block *sb,struct shrink_control *sc) {
@@ -772,7 +773,7 @@ long prune_icache_sb(struct super_block *sb, struct shrink_control *sc) freed =list_lru_shrink_walk (&sb->s_inode_lru, sc, inode_lru_isolate, &freeable); -dispose_list (&freeable); +evict_inode_list (&freeable);return freed; }
include/linux/fs.h +1 -0
@@ -2880,6 +2880,7 @@ extern void unlock_new_inode(struct inode *);extern unsigned int get_next_ino (void );extern void evict_inodes (struct super_block *sb);extern void inode_lru_list_del (struct inode *inode); +extern void evict_inode_list (struct list_head *head);extern void __iget (struct inode * inode);extern void iget_failed (struct inode *);
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 9/25 fs/dcache.c: add shrink_dcache_inode()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
When a filesystem encryption key is removed, we need all files which had been "unlocked" (had ->i_crypt_info set up) with it to appear "locked" again. This is most easily done by evicting the inodes. This can currently be done using 'echo 2 > /proc/sys/vm/drop_caches'; however, that is overkill and not usable by non-root users.
To evict just the needed inodes we also need the ability to evict those inodes' dentries, since an inode is pinned by its dentries. Therefore, add a function shrink_dcache_inode() which iterates through an inode's dentries and evicts any unused ones as well as any unused descendants (since there may be negative dentries pinning the inode's dentries).
--- fs/dcache.c | 33 +++++++++++++++++++++++++++++++++ include/linux/dcache.h | 1 + 2 files changed, 34 insertions(+)
fs/dcache.c +33 -0
@@ -1456,6 +1456,39 @@ void shrink_dcache_parent(struct dentry *parent) }EXPORT_SYMBOL (shrink_dcache_parent); +/** +* shrink_dcache_inode - prune dcache for inode +* @inode: inode to prune +* +* Evict all unused aliases of the specified inode from the dcache. This is +* intended to be used when trying to evict a specific inode, since inodes are +* pinned by their dentries. We also have to descend to ->d_subdirs for each +* alias, since aliases may be pinned by negative child dentries. +*/ +void shrink_dcache_inode (struct inode *inode) +{ +for (;;) { +struct select_data data; +struct dentry *dentry; + +INIT_LIST_HEAD (&data.dispose); + data.start =NULL ; + data.found =0 ; + +spin_lock (&inode->i_lock); +hlist_for_each_entry (dentry, &inode->i_dentry, d_u.d_alias) +d_walk (dentry, &data, select_collect,NULL ); +spin_unlock (&inode->i_lock); + +if (!data.found) +break ; + +shrink_dentry_list (&data.dispose); +cond_resched (); + } +} +EXPORT_SYMBOL (shrink_dcache_inode); +static enum d_walk_ret umount_check (void *_data,struct dentry *dentry) {/* it has busy descendents; complain about those instead */
include/linux/dcache.h +1 -0
@@ -245,6 +245,7 @@ extern struct dentry * d_obtain_alias(struct inode *);extern struct dentry *d_obtain_root (struct inode *);extern void shrink_dcache_sb (struct super_block *);extern void shrink_dcache_parent (struct dentry *); +extern void shrink_dcache_inode (struct inode *);extern void shrink_dcache_for_umount (struct super_block *);extern void d_invalidate (struct dentry *);
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 10/25 fscrypt: add FS_IOC_REMOVE_ENCRYPTION_KEY ioctl
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Problem
~~~~~~~
Many filesystem encryption users want the ability to remove encryption keys, causing the corresponding encrypted directories to appear "locked" (presented in ciphertext form) again. Moreover, users want removing an encryption key to *really* remove it, in the sense that the removed keys cannot be recovered even if kernel memory is compromised, e.g. by the exploit of a kernel security vulnerability or by a physical attack. This is desirable after a user logs out of the system, for example. In many cases users even already assume this to be the case and are surprised to hear when it's not.
It is *not* sufficient to simply unlink the master key from the keyring (or to revoke or invalidate it), since files are encrypted with per-file keys instead of with the master keys directly. Therefore, to really remove a key we must also remove the per-file keys, e.g. by evicting the corresponding inodes from the inode cache. This also would have the benefit of making encrypted files appear "locked" again.
Currently the workaround is to run:
sync
echo 2 > /proc/sys/vm/drop_caches
This is a very bad solution because it evicts all not-in-use inodes in the system rather than just the inodes associated with the key being removed. Moreover, it requires root privileges, so non-root users cannot lock their encrypted directories. Finally, the drop_caches sysctl was originally meant for debugging purposes only.
Nevertheless, the largest users of filesystem encryption (Android and Chromium OS) actually want this capability badly enough that they are actually using the drop_caches workaround. Similarly, the drop_caches workaround is also used in the PAM module provided by the fscrypt userspace tool (https://github.com/google/fscrypt). Needless to say, this is causing significant performance problems due to inodes for unencrypted system files being evicted. So a real solution is needed.
Solution
~~~~~~~~
To properly solve this problem, we need an API which removes and wipes the given master key, *and* removes and and wipes the corresponding per-file keys. This requires tracking which inodes have been "unlocked" using each master key. Originally that was not possible because the kernel didn't actually have a centralized notion of what a master key even was. But now that we have the filesystem-level keyring ->s_master_keys it is finally possible.
Add this API as a new ioctl, FS_IOC_REMOVE_ENCRYPTION_KEY. It is the counterpart of FS_IOC_ADD_ENCRYPTION_KEY.
FS_IOC_REMOVE_ENCRYPTION_KEY first wipes the master key's secret from memory. Then, it syncs the filesystem and tries to evict the list of inodes that had been "unlocked" with the key. Evicting the inodes has several effects, including:
- The actual keys used to encrypt the data (in ->i_crypt_info->ci_ctfm) are wiped from memory. Thus, they can no longer be recovered, even if kernel memory is later compromised.
- The encrypted files and directories once again appear "locked", i.e. in ciphertext or in "encrypted" form. This is highly desirable from a user interface perspective. It can also be desirable from a security perspective (although sometimes for the wrong reasons!).
- The pagecache pages are freed, which allows the plaintext file contents to be overwritten in memory later as the system continues running. Currently we do not actually wipe the pages on free, nor does the kernel more generally wipe memory on free either. Thus, for now we tolerate that an attacker who later gains access to kernel memory may be able to see portions of file contents and file names in plaintext in unallocated memory. Security-conscious users who do not mind a performance hit may ameliorate this by enabling page poisoning.
Of course, some inodes may still be in use when a master key is removed, and we cannot simply revoke random file descriptors, mmap's, etc. The approach we take is to skip in-use inodes, and notify userspace by returning -EBUSY if any inodes could not be evicted. Still, even in this case the master key secret is removed, so no more files can be unlocked with it. Moreover, most of the inodes should still be evicted as well. Userspace can then retry the ioctl later to evict the remaining inodes. Alternatively, if userspace adds the key again, then the refreshed secret will be associated with the existing list of inodes so that they are correctly tracked for future key removals.
For now, FS_IOC_REMOVE_ENCRYPTION_KEY has to be restricted to privileged users only, just like FS_IOC_ADD_ENCRYPTION_KEY. This is sufficient for use cases where all encryption keys are managed by a privileged process, e.g. as is the case on Android and Chromium OS.
But in the more general case, non-root users need to be able to both unlock *and* lock their own encrypted directories. As it turns out, we will indeed be able to support this through these ioctls, but non-root use will need to be tied to the use of a new encryption policy version which identifies the master key using a cryptographic hash. (See later patches.)
--- fs/crypto/fscrypt_private.h | 18 ++- fs/crypto/keyinfo.c | 347 ++++++++++++++++++++++++++++++++++++++-- fs/crypto/policy.c | 5 +- include/linux/fscrypt_notsupp.h | 6 + include/linux/fscrypt_supp.h | 1 + include/uapi/linux/fscrypt.h | 7 + 6 files changed, 367 insertions(+), 17 deletions(-)
fs/crypto/fscrypt_private.h +17 -1
@@ -61,7 +61,23 @@ struct fscrypt_info {u8 ci_flags;struct crypto_skcipher *ci_ctfm;struct crypto_cipher *ci_essiv_tfm; -u8 ci_master_key[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; +u8 ci_master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; + +/* +* The master key with which this inode was unlocked (decrypted). This +* will be NULL if the master key was found in a process-subscribed +* keyring rather than in the filesystem-level keyring. +*/ +struct key *ci_master_key; + +/* Link in list of inodes that were unlocked with the master key */ +struct list_head ci_master_key_link; + +/* +* Back-pointer to the inode, needed during key removal. Only set when +* ->ci_master_key is set. +*/ +struct inode *ci_inode; }; typedef enum {
fs/crypto/keyinfo.c +332 -14
471 lines not on this page · raw message
fs/crypto/policy.c +3 -2
@@ -198,7 +198,8 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) child_ci = child->i_crypt_info; if (parent_ci && child_ci) { - return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key, + return memcmp(parent_ci->ci_master_key_descriptor, + child_ci->ci_master_key_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) = 0 && (parent_ci->ci_data_mode = child_ci->ci_data_mode) && (parent_ci->ci_filename_mode =
@@ -253,7 +254,7 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child, ctx.contents_encryption_mode = ci->ci_data_mode; ctx.filenames_encryption_mode = ci->ci_filename_mode; ctx.flags = ci->ci_flags; -memcpy (ctx.master_key_descriptor, ci->ci_master_key, +memcpy (ctx.master_key_descriptor, ci->ci_master_key_descriptor,FSCRYPT_KEY_DESCRIPTOR_SIZE );get_random_bytes (ctx.nonce,FS_KEY_DERIVATION_NONCE_SIZE );BUILD_BUG_ON (sizeof (ctx) !=FSCRYPT_SET_CONTEXT_MAX_SIZE );
include/linux/fscrypt_notsupp.h +6 -0
@@ -89,6 +89,12 @@ static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)return -EOPNOTSUPP ; } +static inline int fscrypt_ioctl_remove_key (struct file *filp, +const void __user *arg) +{ +return -EOPNOTSUPP ; +} +static inline int fscrypt_get_encryption_info (struct inode *inode) {return -EOPNOTSUPP ;
include/linux/fscrypt_supp.h +1 -0
@@ -43,6 +43,7 @@ extern int fscrypt_inherit_context(struct inode *, struct inode *, void *, bool);/* keyinfo.c */ extern int fscrypt_ioctl_add_key (struct file *filp,void __user *arg); +extern int fscrypt_ioctl_remove_key (struct file *filp,const void __user *arg);extern int fscrypt_get_encryption_info (struct inode *);extern void fscrypt_put_encryption_info (struct inode *,struct fscrypt_info *);
include/uapi/linux/fscrypt.h +7 -0
@@ -66,10 +66,17 @@ struct fscrypt_add_key_args {__u8 raw[]; }; +/* Struct passed to FS_IOC_REMOVE_ENCRYPTION_KEY */ +struct fscrypt_remove_key_args { +__u64 reserved[3 ]; +struct fscrypt_key_specifier key_spec; +}; +#define FS_IOC_SET_ENCRYPTION_POLICY _IOR( 'f', 19, struct fscrypt_policy)#define FS_IOC_GET_ENCRYPTION_PWSALT _IOW( 'f', 20, __u8[16])#define FS_IOC_GET_ENCRYPTION_POLICY _IOW( 'f', 21, struct fscrypt_policy)#define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 22, struct fscrypt_add_key_args) +#define FS_IOC_REMOVE_ENCRYPTION_KEY _IOR( 'f', 23, struct fscrypt_remove_key_args)/**********************************************************************/
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 11/25 fscrypt: add FS_IOC_GET_ENCRYPTION_KEY_STATUS ioctl
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add a new ioctl, FS_IOC_GET_ENCRYPTION_KEY_STATUS. Given a key specified by 'struct fscrypt_key_specifier' (the same way a key is specified for the ioctls which add and remove keys), it returns status information in a 'struct fscrypt_get_key_status_args'.
The main motivation for this is that applications need to be able to check whether an encrypted directory is "unlocked" or not, so that they can add the key if it is not, and avoid adding the key (which may involve prompting the user for a passphrase) if it already is. It's possible to use some workarounds such as checking whether opening a regular file fails with ENOKEY, or checking whether the filenames "look like gibberish" or not. However, no workaround is usable in all cases.
It's also not a simple matter of locked/unlocked anymore because we also have a partially locked state, where FS_IOC_REMOVE_ENCRYPTION_KEY has removed the secret but some encrypted files are still in use. This difference can be important for applications. Moreover, after later patches some applications will also need a way to determine whether a key was added by the current user vs. by some other user.
Ideally we'd have been able to use keyctl_search() to check whether a key is present or not, rather than introducing a new ioctl. However, even if the keyrings permission system was fixed to allow granting read-only access to a keyring (currently the "Search" permission allows keyctl_invalidate()), it still wouldn't work out because the fscrypt master keys can be in states other than just present/absent, as described above. Moreover, we'd still have to at least add an ioctl which retrieves the ID of ->s_master_keys.
/proc/keys cannot really be the API either, since reading /proc/keys involves iterating through all keys on the system and is primarily meant as a debugging interface. We also don't necessarily want to grant everyone VIEW access to all the fscrypt keys as that would imply everyone being able to list them as well.
Therefore, a new ioctl to get an fscrypt key's status seems like the best solution. It is also consistent with the ioctls to add and remove keys.
--- fs/crypto/keyinfo.c | 64 +++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt_notsupp.h | 6 ++++ include/linux/fscrypt_supp.h | 1 + include/uapi/linux/fscrypt.h | 17 +++++++++++ 4 files changed, 88 insertions(+)
fs/crypto/keyinfo.c +64 -0
@@ -566,6 +566,70 @@ int fscrypt_ioctl_remove_key(struct file *filp, const void __user *uarg) }EXPORT_SYMBOL_GPL (fscrypt_ioctl_remove_key); +/* +* Retrieve the status of an fscrypt master encryption key. +* +* We set ->status to indicate whether the key is absent, present, or +* incompletely removed. "Incompletely removed" means that the master key +* secret has been removed, but some files which had been unlocked with it are +* still in use. This field allows applications to easily determine the state +* of an encrypted directory without using a hack such as trying to open a +* regular file in it (which can confuse the "incompletely removed" state with +* absent or present). +* +* Note: this ioctl only works with keys added to the filesystem-level keyring. +* It does *not* work with keys added via the old mechanism which involved +* process-subscribed keyrings. +*/ +int fscrypt_ioctl_get_key_status (struct file *filp,void __user *uarg) +{ +struct super_block *sb =file_inode (filp)->i_sb; +struct fscrypt_get_key_status_args arg; +struct key *key; +struct fscrypt_master_key *mk; +int err; + +if (copy_from_user (&arg, uarg,sizeof (arg))) +return -EFAULT ; + +if (memchr_inv (arg.reserved1,0 ,sizeof (arg.reserved1))) +return -EINVAL ; + +if (!valid_key_spec (&arg.key_spec)) +return -EINVAL ; + + arg.reserved2 =0 ; +memset (arg.reserved3,0 ,sizeof (arg.reserved3)); + + key =find_master_key (sb, &arg.key_spec); +if (IS_ERR (key)) { +if (key !=ERR_PTR (-ENOKEY )) +return PTR_ERR (key); + arg.status =FSCRYPT_KEY_STATUS_ABSENT ; + err =0 ; + goto out; + } + mk = key->payload.data[0 ]; +down_read (&key->sem); + +if (!is_master_key_secret_present (&mk->mk_secret)) { + arg.status =FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED ; + err =0 ; + goto out_release_key; + } + + arg.status =FSCRYPT_KEY_STATUS_PRESENT ; + err =0 ; +out_release_key: +up_read (&key->sem); +key_put (key); +out: +if (!err &©_to_user (uarg, &arg,sizeof (arg))) + err = -EFAULT ; +return err; +} +EXPORT_SYMBOL_GPL (fscrypt_ioctl_get_key_status); +static void derive_crypt_complete (struct crypto_async_request *req,int rc) {struct fscrypt_completion_result *ecr = req->data;
include/linux/fscrypt_notsupp.h +6 -0
@@ -95,6 +95,12 @@ static inline int fscrypt_ioctl_remove_key(struct file *filp,return -EOPNOTSUPP ; } +static inline int fscrypt_ioctl_get_key_status (struct file *filp, +void __user *arg) +{ +return -EOPNOTSUPP ; +} +static inline int fscrypt_get_encryption_info (struct inode *inode) {return -EOPNOTSUPP ;
include/linux/fscrypt_supp.h +1 -0
@@ -44,6 +44,7 @@ extern int fscrypt_inherit_context(struct inode *, struct inode *,/* keyinfo.c */ extern int fscrypt_ioctl_add_key (struct file *filp,void __user *arg);extern int fscrypt_ioctl_remove_key (struct file *filp,const void __user *arg); +extern int fscrypt_ioctl_get_key_status (struct file *filp,void __user *arg);extern int fscrypt_get_encryption_info (struct inode *);extern void fscrypt_put_encryption_info (struct inode *,struct fscrypt_info *);
include/uapi/linux/fscrypt.h +17 -0
@@ -72,11 +72,28 @@ struct fscrypt_remove_key_args {struct fscrypt_key_specifier key_spec; }; +/* Struct passed to FS_IOC_GET_ENCRYPTION_KEY_STATUS */ +struct fscrypt_get_key_status_args { +/* input */ +__u64 reserved1[3 ]; +struct fscrypt_key_specifier key_spec; + +/* output */ +__u32 status; +#define FSCRYPT_KEY_STATUS_ABSENT 1 +#define FSCRYPT_KEY_STATUS_PRESENT 2 +#define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3 +__u32 reserved2; + +__u64 reserved3[7 ]; +}; +#define FS_IOC_SET_ENCRYPTION_POLICY _IOR( 'f', 19, struct fscrypt_policy)#define FS_IOC_GET_ENCRYPTION_PWSALT _IOW( 'f', 20, __u8[16])#define FS_IOC_GET_ENCRYPTION_POLICY _IOW( 'f', 21, struct fscrypt_policy)#define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 22, struct fscrypt_add_key_args)#define FS_IOC_REMOVE_ENCRYPTION_KEY _IOR( 'f', 23, struct fscrypt_remove_key_args) +#define FS_IOC_GET_ENCRYPTION_KEY_STATUS _IOWR('f',24, struct fscrypt_get_key_status_args)/**********************************************************************/
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 12/25 ext4 crypto: wire up new ioctls for managing encryption keys
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
--- fs/ext4/ioctl.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
fs/ext4/ioctl.c +18 -0
@@ -978,6 +978,21 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)case EXT4_IOC_GET_ENCRYPTION_POLICY :return fscrypt_ioctl_get_policy (filp, (void __user *)arg); +case FS_IOC_ADD_ENCRYPTION_KEY : +if (!ext4_has_feature_encrypt (sb)) +return -EOPNOTSUPP ; +return fscrypt_ioctl_add_key (filp, (void __user *)arg); + +case FS_IOC_REMOVE_ENCRYPTION_KEY : +if (!ext4_has_feature_encrypt (sb)) +return -EOPNOTSUPP ; +return fscrypt_ioctl_remove_key (filp, (const void __user *)arg); + +case FS_IOC_GET_ENCRYPTION_KEY_STATUS : +if (!ext4_has_feature_encrypt (sb)) +return -EOPNOTSUPP ; +return fscrypt_ioctl_get_key_status (filp, (void __user *)arg); +case EXT4_IOC_FSGETXATTR : {struct fsxattr fa;
@@ -1102,6 +1117,9 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)case EXT4_IOC_SET_ENCRYPTION_POLICY :case EXT4_IOC_GET_ENCRYPTION_PWSALT :case EXT4_IOC_GET_ENCRYPTION_POLICY : +case FS_IOC_ADD_ENCRYPTION_KEY : +case FS_IOC_REMOVE_ENCRYPTION_KEY : +case FS_IOC_GET_ENCRYPTION_KEY_STATUS :case EXT4_IOC_SHUTDOWN :case FS_IOC_GETFSMAP :break ;
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 13/25 f2fs crypto: wire up new ioctls for managing encryption keys
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
--- fs/f2fs/file.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/f2fs/file.c +9 -0
@@ -2651,6 +2651,12 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)return f2fs_ioc_get_encryption_policy (filp, arg);case F2FS_IOC_GET_ENCRYPTION_PWSALT :return f2fs_ioc_get_encryption_pwsalt (filp, arg); +case FS_IOC_ADD_ENCRYPTION_KEY : +return fscrypt_ioctl_add_key (filp, (void __user *)arg); +case FS_IOC_REMOVE_ENCRYPTION_KEY : +return fscrypt_ioctl_remove_key (filp, (const void __user *)arg); +case FS_IOC_GET_ENCRYPTION_KEY_STATUS : +return fscrypt_ioctl_get_key_status (filp, (void __user *)arg);case F2FS_IOC_GARBAGE_COLLECT :return f2fs_ioc_gc (filp, arg);case F2FS_IOC_GARBAGE_COLLECT_RANGE :
@@ -2731,6 +2737,9 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)case F2FS_IOC_SET_ENCRYPTION_POLICY :case F2FS_IOC_GET_ENCRYPTION_PWSALT :case F2FS_IOC_GET_ENCRYPTION_POLICY : +case FS_IOC_ADD_ENCRYPTION_KEY : +case FS_IOC_REMOVE_ENCRYPTION_KEY : +case FS_IOC_GET_ENCRYPTION_KEY_STATUS :case F2FS_IOC_GARBAGE_COLLECT :case F2FS_IOC_GARBAGE_COLLECT_RANGE :case F2FS_IOC_WRITE_CHECKPOINT :
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 14/25 ubifs crypto: wire up new ioctls for managing encryption keys
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
--- fs/ubifs/ioctl.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
fs/ubifs/ioctl.c +12 -0
@@ -205,6 +205,15 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)#endif } +case FS_IOC_ADD_ENCRYPTION_KEY : +return fscrypt_ioctl_add_key (file, (void __user *)arg); + +case FS_IOC_REMOVE_ENCRYPTION_KEY : +return fscrypt_ioctl_remove_key (file, (const void __user *)arg); + +case FS_IOC_GET_ENCRYPTION_KEY_STATUS : +return fscrypt_ioctl_get_key_status (file, (void __user *)arg); +default :return -ENOTTY ; }
@@ -222,6 +231,9 @@ long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)break ;case FS_IOC_SET_ENCRYPTION_POLICY :case FS_IOC_GET_ENCRYPTION_POLICY : +case FS_IOC_ADD_ENCRYPTION_KEY : +case FS_IOC_REMOVE_ENCRYPTION_KEY : +case FS_IOC_GET_ENCRYPTION_KEY_STATUS :break ;default :return -ENOIOCTLCMD ;
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 15/25 fscrypt: add UAPI definitions to get/set v2 encryption policies
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Currently, fscrypt policies and xattrs identify the master key by master_key_descriptor, which is an arbitrary 8-byte value used to form the description of the keyring key. However, there is no verification that the key descriptor in any way corresponds with the key payload. Since ->i_crypt_info by necessity gets set up on a "first come, first serve" basis, this flaw allows a process with only read-only access to an encrypted file or directory to provide the wrong key, causing the file contents or directory listing to be corrupted. This is a bug with security implications which must be fixed.
To fix this bug without simply locking down adding keys to root, we must replace master_key_descriptor with a cryptographic hash of the key. We name the replacement master_key_identifier and make it 16 bytes long, which should provide enough collision resistance, and more importantly preimage resistance, without bloating the size of the encryption xattr too much.
This will be both an on-disk format and API change, since we'll need to define new versions of both the fscrypt_context and fscrypt_policy. This patch begins this process by defining the UAPI changes to manage v2 policies. (Note: we jump to version 2 even though the previous policy version number was 0 because the fscrypt_context was actually already using version 1, not version 0. It would be really confusing to have them always be 1 off from each other.)
The existing FS_IOC_SET_ENCRYPTION_POLICY will be used to set a v2 policy, as the kernel will be able to examine the 'version' field.
However, a new ioctl FS_IOC_GET_ENCRYPTION_POLICY_EX is needed to get a v2 policy, since the returned struct needs to be larger. This ioctl includes a size field as input, so that it can be used for both v1 and v2 policies as well as any new policy versions that may get added in the future.
--- include/uapi/linux/fscrypt.h | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-)
include/uapi/linux/fscrypt.h +35 -2
@@ -7,7 +7,6 @@* File system encryption support */ /* Policy provided via an ioctl on the topmost directory */ -#define FSCRYPT_KEY_DESCRIPTOR_SIZE 8/* Encryption policy flags */ #define FSCRYPT_POLICY_FLAGS_PAD_4 0x00
@@ -26,13 +25,22 @@#define FSCRYPT_MODE_AES_128_CBC 5#define FSCRYPT_MODE_AES_128_CTS 6 -struct fscrypt_policy { +/* +* Legacy policy version; no key verification (potentially insecure). +* For new encrypted directories, use fscrypt_policy_v2 instead. +* +* Careful: the .version field for this is actually 0, not 1. +*/ +#define FSCRYPT_POLICY_VERSION_LEGACY 0 +#define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 +struct fscrypt_policy_v1 {__u8 version;__u8 contents_encryption_mode;__u8 filenames_encryption_mode;__u8 flags;__u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; }; +#define fscrypt_policy fscrypt_policy_v1/* * Process-subscribed "logon" key description prefix and payload format.
@@ -47,6 +55,30 @@ struct fscrypt_key {__u32 size; }; +/* +* New policy version with HKDF and key verification (recommended). +*/ +#define FSCRYPT_POLICY_VERSION_2 2 +#define FSCRYPT_KEY_IDENTIFIER_SIZE 16 +struct fscrypt_policy_v2 { +__u8 version; +__u8 contents_encryption_mode; +__u8 filenames_encryption_mode; +__u8 flags; +__u8 reserved[4 ]; +__u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE ]; +}; + +/* Struct passed to FS_IOC_GET_ENCRYPTION_POLICY_EX */ +struct fscrypt_get_policy_ex_args { +__u64 size;/* input/output */ +union { +__u8 version; +struct fscrypt_policy_v1 v1; +struct fscrypt_policy_v2 v2; + } policy;/* output */ +}; +struct fscrypt_key_specifier {__u32 type;#define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1
@@ -91,6 +123,7 @@ struct fscrypt_get_key_status_args {#define FS_IOC_SET_ENCRYPTION_POLICY _IOR( 'f', 19, struct fscrypt_policy)#define FS_IOC_GET_ENCRYPTION_PWSALT _IOW( 'f', 20, __u8[16])#define FS_IOC_GET_ENCRYPTION_POLICY _IOW( 'f', 21, struct fscrypt_policy) +#define FS_IOC_GET_ENCRYPTION_POLICY_EX _IOWR('f', 21, __u8[9])/* size + version */ #define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 22, struct fscrypt_add_key_args)#define FS_IOC_REMOVE_ENCRYPTION_KEY _IOR( 'f', 23, struct fscrypt_remove_key_args)#define FS_IOC_GET_ENCRYPTION_KEY_STATUS _IOWR('f',24, struct fscrypt_get_key_status_args)
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 16/25 fscrypt: implement basic handling of v2 encryption policies
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Update the fscrypt internals to handle v2 encryption policies. This includes supporting getting and setting them, translating them to/from the on-disk fscrypt_context. It also includes storing either a v1 or v2 policy struct in the fscrypt_info for use by fscrypt_inherit_context() and fscrypt_has_permitted_context(). (Previously we were storing the individual fields, but it is a bit easier to store a policy struct.)
An fscrypt_policy_v1 (previously 'fscrypt_policy') maps to/from an fscrypt_context_v1 (previously 'fscrypt_context'), while an fscrypt_policy_v2 maps to/from an fscrypt_context_v2.
Key management for v2 policies will be implemented by later patches. For now, attempting to set up an inode's key just fails with EOPNOTSUPP.
--- fs/crypto/fname.c | 4 +- fs/crypto/fscrypt_private.h | 172 ++++++++++++++++--- fs/crypto/keyinfo.c | 70 ++++---- fs/crypto/policy.c | 368 ++++++++++++++++++++++++++++------------ include/linux/fscrypt.h | 2 +- include/linux/fscrypt_notsupp.h | 6 + include/linux/fscrypt_supp.h | 1 + 7 files changed, 452 insertions(+), 171 deletions(-)
fs/crypto/fname.c +2 -2
@@ -46,7 +46,7 @@ static int fname_encrypt(struct inode *inode,int res =0 ;char iv[FS_CRYPTO_BLOCK_SIZE ];struct scatterlist sg; -int padding =4 << (ci->ci_flags &FSCRYPT_POLICY_FLAGS_PAD_MASK ); +int padding =fscrypt_policy_fname_padding (&ci->ci_policy);unsigned int lim;unsigned int cryptlen;
@@ -217,7 +217,7 @@ u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)struct fscrypt_info *ci = inode->i_crypt_info;if (ci) - padding =4 << (ci->ci_flags &FSCRYPT_POLICY_FLAGS_PAD_MASK ); + padding =fscrypt_policy_fname_padding (&ci->ci_policy); ilen =max (ilen, (u32 )FS_CRYPTO_BLOCK_SIZE );return round_up (ilen, padding); }
fs/crypto/fscrypt_private.h +151 -21
200 lines not on this page · raw message
fs/crypto/keyinfo.c +34 -36
142 lines not on this page · raw message
fs/crypto/policy.c +249 -111
456 lines not on this page · raw message
include/linux/fscrypt.h +1 -1
@@ -86,7 +86,7 @@ struct fscrypt_operations { };/* Maximum value for the third parameter of fscrypt_operations.set_context(). */ -#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 +#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40static inline bool fscrypt_dummy_context_enabled (struct inode *inode) {
include/linux/fscrypt_notsupp.h +6 -0
@@ -70,6 +70,12 @@ static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)return -EOPNOTSUPP ; } +static inline int fscrypt_ioctl_get_policy_ex (struct file *filp, +void __user *arg) +{ +return -EOPNOTSUPP ; +} +static inline int fscrypt_has_permitted_context (struct inode *parent,struct inode *child) {
include/linux/fscrypt_supp.h +1 -0
@@ -38,6 +38,7 @@ static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)/* policy.c */ extern int fscrypt_ioctl_set_policy (struct file *,const void __user *);extern int fscrypt_ioctl_get_policy (struct file *,void __user *); +extern int fscrypt_ioctl_get_policy_ex (struct file *,void __user *);extern int fscrypt_has_permitted_context (struct inode *,struct inode *);extern int fscrypt_inherit_context (struct inode *,struct inode *,void *,bool );
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 17/25 fscrypt: add an HKDF-SHA512 implementation
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
For v2 encryption policies, we need to hash the master key to compute a master_key_identifier. Naively, we could just use a truncated SHA-512 or another common cryptographic hash function. However, that would cause the same key material to be used in two different ways: both as input to the hash function, and as input to the AES-ECB-based KDF when deriving the per-inode encryption keys. There *probably* isn't any practical attack on that, but still it would be better "crypto hygiene" to use the key material for one purpose only, e.g. just for a KDF.
It also happens that the AES-based KDF is on our list of things to fix. While it does generate unique derived keys with sufficient entropy, it is nonstandard and has some problems that don't exist in standard KDFs such as HKDF. For example, the AES-based KDF is reversible: given a derived key and nonce, an attacker can easily compute the master key. This was maybe okay for the original threat model of ext4 encryption where the master key and derived keys were considered equally hard to compromise. But now we would like to be more robust against threats such as a derived key being compromised through a timing attack, or a derived key for an in-use file being compromised after the master key has already been wiped from memory via FS_IOC_REMOVE_ENCRYPTION_KEY.
HKDF also has other advantages over the AES-ECB-based KDF such as evenly distributing the entropy from the input key material and being more extensible to deriving other key material that may be needed.
Since we're introducing a new encryption policy version that already includes an on-disk format change, and we also now have a good place to cache an HMAC transform for each master key (struct fscrypt_master_key) so that HKDF can be implemented efficiently, we finally have a chance to switch to HKDF to derive the per-file keys.
In addition, we'll use HKDF to derive the master_key_identifier, avoiding the need for a separate cryptographic hash primitive. This is secure because the output from HKDF is cryptographically isolated, i.e. sending some output in the clear doesn't reveal any other output, in a computational sense. (This is assuming that application-specific info strings aren't repeated between different uses of HKDF, but we'll use context bytes to ensure that.)
Thus, this patch adds an implementation of HKDF to keyinfo.c, using an HMAC transform allocated from the crypto API. Later patches will make use of it.
Note that using HKDF-SHA512 as the key derivation function will introduce a dependency on the security and implementation of SHA-512, whereas before we were using only AES for both key derivation and encryption. However, by using HMAC rather than the hash function directly, HKDF is designed to remain secure even if various classes of attacks, e.g. collision attacks, are found against the underlying unkeyed hash function. Even HMAC-MD5 is still considered secure in practice, despite MD5 itself having been heavily compromised. And meanwhile, the AES-based KDF used the public nonce as the cipher *key*, which is an unusual case which probably hasn't undergone much cryptanalysis. HKDF-SHA512 seems like a safer bet.
We *could* actually avoid introducing a hash primitive by instantiating HKDF-Expand with CMAC-AES256 as the pseudorandom function rather than HMAC-SHA512. That would work; however, the HKDF specification doesn't explicitly allow a non-HMAC pseudorandom function, so it would be less standard. It would also require skipping HKDF-Extract and making the API accept only 32-byte master keys, since otherwise HKDF-Extract using CMAC-AES would produce a pseudorandom key only 16 bytes long which would only be enough for AES-128, not AES-256.
References:
- RFC 5869. "HMAC-based Extract-and-Expand Key Derivation Function
(HKDF)". https://tools.ietf.org/html/rfc5869
- Krawczyk (2010). "Cryptographic Extraction and Key Derivation: The
HKDF Scheme". https://eprint.iacr.org/2010/264.pdf
--- fs/crypto/Kconfig | 2 + fs/crypto/keyinfo.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 181 insertions(+), 1 deletion(-)
fs/crypto/Kconfig +2 -0
@@ -8,6 +8,8 @@ config FS_ENCRYPTION select CRYPTO_CTS select CRYPTO_CTR select CRYPTO_SHA256 + select CRYPTO_SHA512 + select CRYPTO_HMAC select KEYS help Enable encryption of files and directories. This
fs/crypto/keyinfo.c +179 -1
201 lines not on this page · raw message
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 18/25 fscrypt: allow adding and removing keys for v2 encryption policies
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Extend the FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY ioctls to support adding and removing keys for use by v2 encryption policies.
Keys for v2 encryption policies are identified by a 16-byte "identifier", which is a cryptographic hash of the key, instead of by an 8-byte "descriptor". For FS_IOC_ADD_ENCRYPTION_KEY, the kernel calculates the key identifier and copies it to userspace. Userspace is not allowed to choose the key identifier, since the kernel would have to recalculate it anyway to verify that it is correct.
For FS_IOC_REMOVE_ENCRYPTION_KEY, userspace provides the key identifier rather than the key descriptor to identify the key to be removed. For both ioctls, a type field indicates whether the old or new way of specifying keys is being used. A common structure is used, 'struct fscrypt_key_specifier', and it has some extra space just in case we have to introduce a new way to identify keys in the future.
Note that keys for v1 and v2 encryption policies are both stored in ->s_master_keys, but their descriptions will be of different lengths. Therefore, they cannot be mixed up when we search for a key.
For now the ioctls still always require capable(CAP_SYS_ADMIN). We'll be able to relax that soon, but only for v2 policies.
--- fs/crypto/keyinfo.c | 75 ++++++++++++++++++++++++++++++++++++++++---- include/uapi/linux/fscrypt.h | 2 ++ 2 files changed, 71 insertions(+), 6 deletions(-)
fs/crypto/keyinfo.c +69 -6
@@ -32,6 +32,19 @@#define HKDF_HMAC_ALG "hmac(sha512)"#define HKDF_HASHLEN SHA512_DIGEST_SIZE +/* +* The list of contexts in which we use HKDF to derive additional keys from a +* master key. The values in this list are used as the first byte of the +* application-specific info string to guarantee that info strings are never +* repeated between contexts. +* +* Keys derived with different info strings are cryptographically isolated from +* each other --- knowledge of one derived key doesn't reveal any others. +* (This property is particularly important for the derived key used as the +* "key identifier", as that is stored in the clear.) +*/ +#define HKDF_CONTEXT_KEY_IDENTIFIER 1 +/* * HKDF consists of two steps: *
@@ -228,7 +241,12 @@ struct fscrypt_master_key {*/ struct fscrypt_master_key_secret mk_secret; -/* Arbitrary key descriptor which was assigned by userspace */ +/* +* For v1 policy keys: an arbitrary key descriptor which was assigned by +* userspace (->descriptor). +* +* For v2 policy keys: a cryptographic hash of this key (->identifier). +*/ struct fscrypt_key_specifier mk_spec;/*
@@ -252,6 +270,8 @@ static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)switch (spec->type) {case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR :return FSCRYPT_KEY_DESCRIPTOR_SIZE ; +case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER : +return FSCRYPT_KEY_IDENTIFIER_SIZE ; }return 0 ; }
@@ -346,7 +366,7 @@ static struct key *search_fscrypt_keyring(struct key *keyring,#define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \ (sizeof("fscrypt-") - 1 + sizeof(((struct super_block *)0)->s_id) + 1) -#define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_DESCRIPTOR_SIZE + 1) +#define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1) static void format_fs_keyring_description( char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
@@ -550,6 +570,39 @@ int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)if (copy_from_user (secret.raw, uarg->raw, secret.size)) goto out_wipe_secret; +if (arg.key_spec.type =FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER ) { +struct crypto_shash *hmac_tfm; + + hmac_tfm =allocate_hmac_tfm (secret.raw, secret.size); +if (IS_ERR (hmac_tfm)) { + err =PTR_ERR (hmac_tfm); + goto out_wipe_secret; + } + +/* +* Hash the master key to get the key identifier, then return it +* to userspace. Specifically, we derive the key identifier +* from the master key with HKDF-SHA512, using a unique +* application-specific info string. This is preferable to a +* simple truncated SHA512 because it means we only ever use the +* master key for a single cryptographic primitive (HKDF-SHA512) +* rather than two (HKDF-SHA512 and SHA512). It *maybe* would +* be okay, but cryptographically it would be bad practice. +*/ + err =hkdf_expand (hmac_tfm,HKDF_CONTEXT_KEY_IDENTIFIER , +NULL ,0 , arg.key_spec.identifier, +FSCRYPT_KEY_IDENTIFIER_SIZE ); +crypto_free_shash (hmac_tfm); +if (err) + goto out_wipe_secret; + + err = -EFAULT ; +if (copy_to_user (uarg->key_spec.identifier, + arg.key_spec.identifier, +FSCRYPT_KEY_IDENTIFIER_SIZE )) + goto out_wipe_secret; + } + err =add_master_key (sb, &secret, &arg.key_spec); out_wipe_secret:wipe_master_key_secret (&secret);
@@ -872,6 +925,10 @@ static int derive_key_aes(const u8 *master_key,* Search the current task's subscribed keyrings for a "logon" key with * description prefix:descriptor, and if found acquire a read lock on it and * return a pointer to its validated payload in *payload_ret. +* +* This is only used for v1 encryption policies, where keys are identified by +* master_key_descriptor. With newer policy versions, only the filesystem-level +* keyring (->s_master_keys) is supported. */ static struct key * find_and_lock_process_key(const char *prefix,
@@ -977,17 +1034,23 @@ static int find_and_derive_key(const struct inode *inode,memcpy (mk_spec.descriptor, ctx->v1.master_key_descriptor,FSCRYPT_KEY_DESCRIPTOR_SIZE );break ; +case FSCRYPT_CONTEXT_V2 : + mk_spec.type =FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER ; +memcpy (mk_spec.identifier, ctx->v2.master_key_identifier, +FSCRYPT_KEY_IDENTIFIER_SIZE ); +break ;default : -return -EOPNOTSUPP ; +return -EOPNOTSUPP ;/* should have been checked earlier too */ } key =find_master_key (inode->i_sb, &mk_spec);if (IS_ERR (key)) { -if (key !=ERR_PTR (-ENOKEY )) +if (key !=ERR_PTR (-ENOKEY ) || + ctx->v1.version !=FSCRYPT_CONTEXT_V1 )return PTR_ERR (key);/* -* As a legacy fallback, we search the current task's subscribed -* keyrings in addition to ->s_master_keys. +* As a legacy fallback for v1 policies, we search the current +* task's subscribed keyrings in addition to ->s_master_keys. */ return find_and_derive_key_legacy (inode, &ctx->v1, derived_key, derived_keysize);
include/uapi/linux/fscrypt.h +2 -0
@@ -82,10 +82,12 @@ struct fscrypt_get_policy_ex_args {struct fscrypt_key_specifier {__u32 type;#define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1 +#define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2__u32 reserved;union {__u8 max_specifier[32 ];__u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE ]; +__u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE ]; }; };
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 19/25 fscrypt: use HKDF-SHA512 to derive the per-file keys for v2 policies
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The AES-ECB-based method we're using to derive the per-inode encryption keys is nonstandard and has a number of problems, such as being trivially reversible. Fix these problems for v2 encryption policies by deriving the keys using HKDF-SHA512 instead. The inode's nonce prefixed with a context byte is used as the application-specific info string.
Supposedly, one of the reasons that HKDF wasn't used originally was because of performance concerns. However, we actually can derive on the order of 1 million keys per second, so it's likely not a bottleneck in practice. Moreover, although HKDF-SHA512 can require a bit more actual crypto work per key derivation than the old KDF, the real world performance is actually just as good or even better than the old KDF. This is because the old KDF has to allocate and key a new "ecb(aes)" transform for every key derivation (since it's keyed with the nonce rather than the master key), whereas with HKDF we simply use a cached, pre-keyed "hmac(sha512)" transform. And the old KDF often spends more time allocating its crypto transform than doing actual crypto work.
Another benefit to switching to HKDF is that we no longer need to hold the raw master key in memory, but rather only an HMAC transform keyed by a pseudorandom key extracted from the master key. Of course, for the software HMAC implementation there is no security benefit, since compromising the state of the HMAC transform is equivalent to compromising the raw master key. However, there could be a security benefit if used with an HMAC implementation that holds the secret in crypto accelerator hardware rather than in main memory.
--- fs/crypto/fscrypt_private.h | 4 ++-- fs/crypto/keyinfo.c | 58 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 16 deletions(-)
fs/crypto/fscrypt_private.h +2 -2
@@ -49,7 +49,7 @@ struct fscrypt_context_v1 {/* * A unique value used in combination with the master key to derive the -* file's actual encryption key +* file's actual encryption key, using the AES-ECB-based KDF. */ u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE ]; };
@@ -78,7 +78,7 @@ struct fscrypt_context_v2 {/* * A unique value used in combination with the master key to derive the -* file's actual encryption key +* file's actual encryption key, using HKDF-SHA512. */ u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE ]; };
fs/crypto/keyinfo.c +44 -14
114 lines not on this page · raw message
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 20/25 fscrypt: allow unprivileged users to add/remove keys for v2 policies
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Having the FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY ioctls be root-only is sufficient for some users of filesystem encryption, e.g. Android and Chromium OS where all the encryption keys are managed by a privileged process. However, it is not sufficient for general use where non-root users are setting up encrypted directories. If these ioctls were root-only, such users would have to continue to use process-subscribed keyrings and would continue to run into all the problems noted earlier, including visibility problems when processes running under different UIDs need to be able to access the files, and the inability to remove the key, "locking" the directory.
Fortunately, we can indeed make the ioctls unprivileged, but only for v2 encryption policies and only after a few additional changes which this patch implements.
First, to allow any user to add a key with filesystem-level visibility, the keys must be identified using a cryptographic hash so that users cannot add the wrong key for other users' files. We use the key_identifier for this, which is why v2 encryption policies are a requirement.
Second, we charge each key a user adds to their quota for the keyrings service. Thus, a user can't cause a denial of service by adding a very large number of keys.
Third, we have to be careful about when a key is allowed to be removed, given that multiple users may add the same key (although that should *not* normally be the case; it's astronomically unlikely for keys to collide by chance, so it should only happen as a result of explicit sharing or compromise). One might consider only allowing the first user who added a key to remove it, or allowing any user who knows a key to remove it. But neither of those are good enough because we don't want a user on the system who knows another user's key to be able to cause a denial of service where the former user removes the latter user's key at an inopportune time. After all, it *should* be the case that if you have an encrypted directory and you give everyone in the world the key, including malicious users on the same system, it should still be no less secure than *not* using encryption.
The solution is to keep track of which users have added a key and only really remove the key once all users have removed it.
However, it is tolerated that a user will be unable to remove a key, i.e. unable to "lock" their encrypted files, if another user has added the same key. But in a sense, this is actually a good thing because it will avoid providing a false notion of security where a key appears to have been "removed" when actually it's still in memory, available to any attacker who compromises the operating system kernel.
--- fs/crypto/crypto.c | 7 + fs/crypto/fscrypt_private.h | 1 + fs/crypto/keyinfo.c | 341 ++++++++++++++++++++++++++++++++++++++++--- include/uapi/linux/fscrypt.h | 11 +- 4 files changed, 338 insertions(+), 22 deletions(-)
fs/crypto/crypto.c +7 -0
@@ -469,8 +469,14 @@ static int __init fscrypt_init(void)if (err) goto fail_free_info; + err =register_key_type (&key_type_fscrypt_mk_user); +if (err) + goto fail_unregister_fscrypt_mk_type; +return 0 ; +fail_unregister_fscrypt_mk_type: +unregister_key_type (&key_type_fscrypt_mk); fail_free_info:kmem_cache_destroy (fscrypt_info_cachep); fail_free_ctx:
@@ -494,6 +500,7 @@ static void __exit fscrypt_exit(void)kmem_cache_destroy (fscrypt_ctx_cachep);kmem_cache_destroy (fscrypt_info_cachep);unregister_key_type (&key_type_fscrypt_mk); +unregister_key_type (&key_type_fscrypt_mk_user);fscrypt_essiv_cleanup (); }
fs/crypto/fscrypt_private.h +1 -0
@@ -235,6 +235,7 @@ extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx,/* keyinfo.c */ extern struct key_type key_type_fscrypt_mk; +extern struct key_type key_type_fscrypt_mk_user; extern void __exit fscrypt_essiv_cleanup(void);/* policy.c */
fs/crypto/keyinfo.c +322 -19
514 lines not on this page · raw message
include/uapi/linux/fscrypt.h +8 -3
@@ -102,7 +102,10 @@ struct fscrypt_add_key_args {/* Struct passed to FS_IOC_REMOVE_ENCRYPTION_KEY */ struct fscrypt_remove_key_args { -__u64 reserved[3 ]; +__u32 flags; +#define FSCRYPT_REMOVE_KEY_FLAG_ALL_USERS 0x00000001 +__u32 reserved1; +__u64 reserved2[2 ];struct fscrypt_key_specifier key_spec; };
@@ -117,9 +120,11 @@ struct fscrypt_get_key_status_args {#define FSCRYPT_KEY_STATUS_ABSENT 1#define FSCRYPT_KEY_STATUS_PRESENT 2#define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3 +__u32 status_flags; +#define FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF 0x00000001 +__u32 user_count;__u32 reserved2; - -__u64 reserved3[7 ]; +__u64 reserved3[6 ]; };#define FS_IOC_SET_ENCRYPTION_POLICY _IOR( 'f', 19, struct fscrypt_policy)
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 21/25 fscrypt: require that key be added when setting a v2 encryption policy
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
By looking up the master keys in a filesystem-level keyring rather than in the calling processes' key hierarchy, it becomes possible for a user to set an encryption policy which refers to some key they don't actually know, then encrypt their files using that key. Cryptographically this shouldn't actually be a major problem; for one, every file will still be encrypted with a unique derived key, rather than with the master key directly. But to be on the safe side, enforce that a v2 encryption policy can only be set if the user has previously added the key, or has capable(CAP_FOWNER).
We tolerate that this problem will continue to exist for v1 encryption policies, however; there is no way around that.
--- fs/crypto/fscrypt_private.h | 2 ++ fs/crypto/keyinfo.c | 42 ++++++++++++++++++++++++++++++++++++++++++ fs/crypto/policy.c | 6 ++++++ 3 files changed, 50 insertions(+)
fs/crypto/fscrypt_private.h +2 -0
@@ -234,6 +234,8 @@ extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx, gfp_t gfp_flags);/* keyinfo.c */ +extern int fscrypt_verify_key_added (struct super_block *sb, +const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE ]);extern struct key_type key_type_fscrypt_mk;extern struct key_type key_type_fscrypt_mk_user; extern void __exit fscrypt_essiv_cleanup(void);
fs/crypto/keyinfo.c +42 -0
48 lines not on this page · raw message
fs/crypto/policy.c +6 -0
@@ -170,6 +170,7 @@ static int set_encryption_policy(struct inode *inode, const union fscrypt_policy *policy) {union fscrypt_context ctx; +int err;if (!fscrypt_supported_policy (policy))return -EINVAL ;
@@ -190,6 +191,11 @@ static int set_encryption_policy(struct inode *inode,*/ pr_warn_once ("%s (pid %d) is setting less secure v1 encryption policy; recommend upgrading to v2.\n" , current->comm, current->pid); + }else { + err =fscrypt_verify_key_added (inode->i_sb, + policy->v2.master_key_identifier); +if (err) +return err; } return inode->i_sb->s_cop->set_context(inode, &ctx,
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 22/25 ext4 crypto: wire up FS_IOC_GET_ENCRYPTION_POLICY_EX
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
FS_IOC_GET_ENCRYPTION_POLICY_EX allows filesystem encryption users to retrieve the encryption policy for files and directories that use a v2 encryption policy. Unlike the original FS_IOC_GET_ENCRYPTION_POLICY, FS_IOC_GET_ENCRYPTION_POLICY_EX is also extensible to new versions of the policy struct that may be added in the future.
--- fs/ext4/ioctl.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/ext4/ioctl.c +4 -0
@@ -978,6 +978,9 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)case EXT4_IOC_GET_ENCRYPTION_POLICY :return fscrypt_ioctl_get_policy (filp, (void __user *)arg); +case FS_IOC_GET_ENCRYPTION_POLICY_EX : +return fscrypt_ioctl_get_policy_ex (filp, (void __user *)arg); +case FS_IOC_ADD_ENCRYPTION_KEY :if (!ext4_has_feature_encrypt (sb))return -EOPNOTSUPP ;
@@ -1117,6 +1120,7 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)case EXT4_IOC_SET_ENCRYPTION_POLICY :case EXT4_IOC_GET_ENCRYPTION_PWSALT :case EXT4_IOC_GET_ENCRYPTION_POLICY : +case FS_IOC_GET_ENCRYPTION_POLICY_EX :case FS_IOC_ADD_ENCRYPTION_KEY :case FS_IOC_REMOVE_ENCRYPTION_KEY :case FS_IOC_GET_ENCRYPTION_KEY_STATUS :
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 23/25 f2fs crypto: wire up FS_IOC_GET_ENCRYPTION_POLICY_EX
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
FS_IOC_GET_ENCRYPTION_POLICY_EX allows filesystem encryption users to retrieve the encryption policy for files and directories that use a v2 encryption policy. Unlike the original FS_IOC_GET_ENCRYPTION_POLICY, FS_IOC_GET_ENCRYPTION_POLICY_EX is also extensible to new versions of the policy struct that may be added in the future.
--- fs/f2fs/file.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
fs/f2fs/file.c +5 -7
@@ -1887,11 +1887,6 @@ static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)return fscrypt_ioctl_set_policy (filp, (const void __user *)arg); } -static int f2fs_ioc_get_encryption_policy (struct file *filp,unsigned long arg) -{ -return fscrypt_ioctl_get_policy (filp, (void __user *)arg); -} -static int f2fs_ioc_get_encryption_pwsalt (struct file *filp,unsigned long arg) {struct inode *inode =file_inode (filp);
@@ -2647,10 +2642,12 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)return f2fs_ioc_fitrim (filp, arg);case F2FS_IOC_SET_ENCRYPTION_POLICY :return f2fs_ioc_set_encryption_policy (filp, arg); -case F2FS_IOC_GET_ENCRYPTION_POLICY : -return f2fs_ioc_get_encryption_policy (filp, arg);case F2FS_IOC_GET_ENCRYPTION_PWSALT :return f2fs_ioc_get_encryption_pwsalt (filp, arg); +case F2FS_IOC_GET_ENCRYPTION_POLICY : +return fscrypt_ioctl_get_policy (filp, (void __user *)arg); +case FS_IOC_GET_ENCRYPTION_POLICY_EX : +return fscrypt_ioctl_get_policy_ex (filp, (void __user *)arg);case FS_IOC_ADD_ENCRYPTION_KEY :return fscrypt_ioctl_add_key (filp, (void __user *)arg);case FS_IOC_REMOVE_ENCRYPTION_KEY :
@@ -2737,6 +2734,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)case F2FS_IOC_SET_ENCRYPTION_POLICY :case F2FS_IOC_GET_ENCRYPTION_PWSALT :case F2FS_IOC_GET_ENCRYPTION_POLICY : +case FS_IOC_GET_ENCRYPTION_POLICY_EX :case FS_IOC_ADD_ENCRYPTION_KEY :case FS_IOC_REMOVE_ENCRYPTION_KEY :case FS_IOC_GET_ENCRYPTION_KEY_STATUS :
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 24/25 ubifs crypto: wire up FS_IOC_GET_ENCRYPTION_POLICY_EX
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
FS_IOC_GET_ENCRYPTION_POLICY_EX allows filesystem encryption users to retrieve the encryption policy for files and directories that use a v2 encryption policy. Unlike the original FS_IOC_GET_ENCRYPTION_POLICY, FS_IOC_GET_ENCRYPTION_POLICY_EX is also extensible to new versions of the policy struct that may be added in the future.
--- fs/ubifs/ioctl.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/ubifs/ioctl.c +6 -6
@@ -197,13 +197,12 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return -EOPNOTSUPP ;#endif } -case FS_IOC_GET_ENCRYPTION_POLICY : { -#ifdef CONFIG_UBIFS_FS_ENCRYPTION + +case FS_IOC_GET_ENCRYPTION_POLICY :return fscrypt_ioctl_get_policy (file, (void __user *)arg); -#else -return -EOPNOTSUPP ; -#endif - } + +case FS_IOC_GET_ENCRYPTION_POLICY_EX : +return fscrypt_ioctl_get_policy_ex (file, (void __user *)arg);case FS_IOC_ADD_ENCRYPTION_KEY :return fscrypt_ioctl_add_key (file, (void __user *)arg);
@@ -231,6 +230,7 @@ long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)break ;case FS_IOC_SET_ENCRYPTION_POLICY :case FS_IOC_GET_ENCRYPTION_POLICY : +case FS_IOC_GET_ENCRYPTION_POLICY_EX :case FS_IOC_ADD_ENCRYPTION_KEY :case FS_IOC_REMOVE_ENCRYPTION_KEY :case FS_IOC_GET_ENCRYPTION_KEY_STATUS :
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
RFC PATCH 25/25 fscrypt: document the new ioctls and policy version
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Update the fscrypt documentation file to catch up to all the latest changes, including the new ioctls to manage master encryption keys in the filesystem-level keyring, the support for v2 encryption policies, and the new key derivation function.
--- Documentation/filesystems/fscrypt.rst | 565 ++++++++++++++++++++++++++++------ 1 file changed, 472 insertions(+), 93 deletions(-)
Documentation/filesystems/fscrypt.rst +83 -23
@@ -72,6 +72,9 @@ Online attacks fscrypt (and storage encryption in general) can only provide limited protection, if any at all, against online attacks. In detail: +Side-channel attacks +~~~~~~~~~~~~~~~~~~~~ + fscrypt is only resistant to side-channel attacks, such as timing or electromagnetic attacks, to the extent that the underlying Linux Cryptographic API algorithms are. If a vulnerable algorithm is used,
@@ -80,29 +83,86 @@ attacker to mount a side channel attack against the online system. Side channel attacks may also be mounted against applications consuming decrypted data. -After an encryption key has been provided, fscrypt is not designed to -hide the plaintext file contents or filenames from other users on the -same system, regardless of the visibility of the keyring key. -Instead, existing access control mechanisms such as file mode bits, -POSIX ACLs, LSMs, or mount namespaces should be used for this purpose. -Also note that as long as the encryption keys are *anywhere* in -memory, an online attacker can necessarily compromise them by mounting -a physical attack or by exploiting any kernel security vulnerability -which provides an arbitrary memory read primitive. - -While it is ostensibly possible to "evict" keys from the system, -recently accessed encrypted files will remain accessible at least -until the filesystem is unmounted or the VFS caches are dropped, e.g. -using ``echo 2 > /proc/sys/vm/drop_caches``. Even after that, if the -RAM is compromised before being powered off, it will likely still be -possible to recover portions of the plaintext file contents, if not -some of the encryption keys as well. (Since Linux v4.12, all -in-kernel keys related to fscrypt are sanitized before being freed. -However, userspace would need to do its part as well.) - -Currently, fscrypt does not prevent a user from maliciously providing -an incorrect key for another user's existing encrypted files. A -protection against this is planned. +Unauthorized file access +~~~~~~~~~~~~~~~~~~~~~~~~ + +After an encryption key has been added, fscrypt does not hide the +plaintext file contents or filenames from other users on the same +system. Instead, existing access control mechanisms such as file mode +bits, POSIX ACLs, LSMs, or namespaces should be used for this purpose. + +(For the reasoning behind this, understand that while the key is +added, the confidentiality of the data, from the perspective of the +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 compromise +~~~~~~~~~~~~~~~~~ + +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, fscrypt does allow an encryption key to be removed from the +kernel, which may protect it from later compromise. + +In more detail, the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl will wipe a +master encryption key from kernel memory. Moreover, it will try to +evict all cached inodes which had been "unlocked" using the key, +thereby wiping their derived encryption keys and making them once +again appear "locked", i.e. in ciphertext or encrypted form. + +However, FS_IOC_REMOVE_ENCRYPTION_KEY has some limitations: + +- Derived keys for in-use files will *not* be removed or wiped. + Therefore, for maximum effect, userspace should close the relevant + encrypted files and directories before removing a master key, as + well as kill any processes whose working directory is in an affected + encrypted directory. + +- The kernel cannot magically wipe copies of the master key(s) that + userspace might have as well. Therefore, userspace must wipe all + copies of the master key(s) it makes as well. Naturally, the same + also applies to all higher levels in the key hierarchy, e.g. to all + key(s) that are used to wrap or derive the fscrypt master keys. + Userspace should also follow other security precautions such as + mlock()ing memory containing keys to prevent it from being swapped + out. + +- In general, decrypted contents and filenames in the kernel VFS + caches are freed but not wiped. Therefore, portions thereof may be + recoverable from freed memory, even after the corresponding key(s) + were wiped. To partially solve this, you may enable page poisoning + by enabling CONFIG_PAGE_POISONING in your kernel config and adding + page_poison=1 to your kernel command line. However, that has a + performance cost. + +- Secret keys might still exist in CPU registers, in crypto + accelerator hardware (if used by the crypto API to provide any of + the algorithms), or in other places not explicitly considered here. + +Limitations of v1 policies +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The original encryption policy version (which we call "v1") had some +weaknesses with respect to online attacks: + +- There was no verification that the provided master key was correct. + Therefore, malicious users could associate the wrong key with + encrypted files, even files to which they had only read-only access. + +- A compromise of any file's derived encryption key also compromised + the master key it was derived from. + +- Non-root users could not securely remove encryption keys. + +All the above problems are fixed with v2 encryption policies +(:c:type:`fscrypt_policy_v2`). For this reason, it's recommended to +use v2 encryption policies for all new encrypted directories. Key hierarchy ======@@ -167,19 +227,27 @@ master keys or to support rotating master keys. Instead, the master
keys may be wrapped in userspace, e.g. as done by the `fscrypt
<https://github.com/google/fscrypt>`_ tool.
-The current KDF encrypts the master key using the 16-byte nonce as an -AES-128-ECB key. The output is used as the derived key. If the -output is longer than needed, then it is truncated to the needed -length. Truncation is the norm for directories and symlinks, since -those use the CTS-CBC encryption mode which requires a key half as -long as that required by the XTS encryption mode.
-
-Note: this KDF meets the primary security requirement, which is to -produce unique derived keys that preserve the entropy of the master -key, assuming that the master key is already a good pseudorandom key. -However, it is nonstandard and has some problems such as being -reversible, so it is generally considered to be a mistake! It may be -replaced with HKDF or another more standard KDF in the future. +A different KDF is used depending on the encryption policy version:
+
+For v1 encryption policies, the KDF is somewhat ad-hoc: we encrypt the +master key with AES-128-ECB using the file's 16-byte nonce as the AES +key, and the resulting ciphertext is used as the derived key. If the +master key is longer than the derived key, then only the needed prefix +of the ciphertext is used. Truncation is the norm for directories and +symlinks, since those use the CTS-CBC encryption mode which requires a +key half as long as that required by the XTS encryption mode. +
+For v2 encryption policies, the KDF is HKDF-SHA512. HKDF is preferred +to the AES-based KDF because HKDF is standardized and has a number of +desirable properties such as being nonreversible and evenly +distributing the entropy from the master key. To derive a file's +encryption key using HKDF, the master key is used as the "input key +material", a fixed value is used as the "salt", and the file's 16-byte +nonce prefixed with a context byte is used as the +"application-specific information string". (A fixed salt is used +because there is no random salt available on a per-master-key basis, +and the master keys should already be good pseudorandom keys that are +long enough to make dictionary attacks infeasible.)
Encryption modes and usage
=============
@@ -249,21 +317,38 @@ Setting an encryption policy
The FS_IOC_SET_ENCRYPTION_POLICY ioctl sets an encryption policy on an empty directory or verifies that a directory or regular file already has the specified encryption policy. It takes in a pointer to a
-:c:type:struct fscrypt_policy, defined as follows::
-
- #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
+:c:type:struct fscrypt_policy_v1 or a :c:type:struct +fscrypt_policy_v2, defined as follows::
- struct fscrypt_policy {
+ #define FSCRYPT_POLICY_VERSION_LEGACY 0
+ #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
+ struct fscrypt_policy_v1 {
__u8 version;
__u8 contents_encryption_mode;
__u8 filenames_encryption_mode;
__u8 flags;
__u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
};
+ #define fscrypt_policy fscrypt_policy_v1
+
+ #define FSCRYPT_POLICY_VERSION_2 2
+ #define FSCRYPT_KEY_IDENTIFIER_SIZE 16
+ struct fscrypt_policy_v2 {
+ __u8 version;
+ __u8 contents_encryption_mode;
+ __u8 filenames_encryption_mode;
+ __u8 flags;
+ __u8 reserved[4];
+ __u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];+ };
This structure must be initialized as follows:
-- ``version`` must be 0.
+- ``version`` must be FSCRYPT_POLICY_VERSION_LEGACY (0) if the struct + is :c:type:`fscrypt_policy_v1` or FSCRYPT_POLICY_VERSION_2 (2) if + the struct is :c:type:`fscrypt_policy_v2`. (Note: we refer to the + legacy policy version as "v1", though its version number was really + 0.) For new encrypted directories, use v2 policies.
- ``contents_encryption_mode`` and ``filenames_encryption_mode`` must be set to constants from ``<linux/fs.h>`` which identify the
@@ -276,15 +361,25 @@ This structure must be initialized as follows:
identifies the amount of NUL-padding to use when encrypting filenames. If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32 (0x3).
-- ``master_key_descriptor`` specifies how to find the master key in
- the keyring; see `Adding keys`_. It is up to userspace to choose a - unique ``master_key_descriptor`` for each master key. The e4crypt - and fscrypt tools use the first 8 bytes of +- For v2 encryption policies, ``reserved`` must be zeroed. + +- For v1 encryption policies, ``master_key_descriptor`` specifies how + to find the master key in a keyring; see `Adding keys`_. It is up + to userspace to choose a unique ``master_key_descriptor`` for each + master key. The e4crypt and fscrypt tools use the first 8 bytes of
``SHA-512(SHA-512(master_key))``, but this particular scheme is not required. Also, the master key need not be in the keyring yet when FS_IOC_SET_ENCRYPTION_POLICY is executed. However, it must be added before any files can be created in the encrypted directory.
+ For v2 encryption policies, ``master_key_descriptor`` has been + replaced with ``master_key_identifier``, which is longer and cannot + be arbitrarily chosen. Instead, the key must first be added using + FS_IOC_ADD_ENCRYPTION_KEY, as described in `Adding keys`_. Then, + the ``key_spec.identifier`` the kernel returned in the + :c:type:`struct fscrypt_add_key_args` must be used as the + ``master_key_identifier`` in the ``struct fscrypt_policy_v2``.
+
If the file is not yet encrypted, then FS_IOC_SET_ENCRYPTION_POLICY verifies that the file is an empty directory. If so, the specified encryption policy is assigned to the directory, turning it into an
@@ -300,6 +395,15 @@ policy exactly matches the actual one. If they match, then the ioctl
returns 0. Otherwise, it fails with EEXIST. This works on both
regular files and directories, including nonempty directories.
+When a v2 encryption policy is assigned to a directory, it is also +required that either the specified key has been added by the current +user, or the caller has CAP_FOWNER in the initial user namespace. +(This is needed to prevent a user from encrypting their data with +another user's key.) The key must remain added while +FS_IOC_SET_ENCRYPTION_POLICY is executing. However, if the new +encrypted directory does not need to be accessed immediately, then the +key can be removed right away afterwards.
+
Note that the ext4 filesystem does not allow the root directory to be encrypted, even if it is empty. Users who want to encrypt an entire filesystem with one key should consider using dm-crypt instead.
@@ -312,7 +416,9 @@ FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors:
- ``EEXIST``: the file is already encrypted with an encryption policy different from the one specified
- ``EINVAL``: an invalid encryption policy was specified (invalid
- version, mode(s), or flags)
+ version, mode(s), or flags; or reserved bits were set)
+- ``ENOKEY``: a v2 encryption policy was specified, but the key with + the specified ``master_key_identifier`` has not been added
- ``ENOTDIR``: the file is unencrypted and is a regular file, not a directory
- ``ENOTEMPTY``: the file is unencrypted and is a nonempty directory
@@ -331,25 +437,82 @@ FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors:
Getting an encryption policy
----------------------------
-The FS_IOC_GET_ENCRYPTION_POLICY ioctl retrieves the :c:type:struct -fscrypt_policy, if any, for a directory or regular file. See above -for the struct definition. No additional permissions are required -beyond the ability to open the file.
+Two ioctls are available to get a file's encryption policy:
+
+- FS_IOC_GET_ENCRYPTION_POLICY_EX +- FS_IOC_GET_ENCRYPTION_POLICY
+
+The extended (_EX) version of the ioctl is more general and is +recommended to use when possible. However, on older kernels only the +original ioctl is available. Applications should try the extended +version, and if it fails with ENOTTY fall back to the original +version.
+
+Preferred method
+~~~~~~~~~~~~~~~~
+
+The FS_IOC_GET_ENCRYPTION_POLICY_EX ioctl retrieves the encryption +policy, if any, for a directory or regular file. No additional +permissions are required beyond the ability to open the file. It +takes in a pointer to a buffer formatted as a :c:type:struct +fscrypt_get_policy_ex_args, defined as follows::
+
+ struct fscrypt_get_policy_ex_args {
+ __u64 size;
+ union {
+ __u8 version; + struct fscrypt_policy_v1 v1; + struct fscrypt_policy_v2 v2;
+ } policy;
+ };
+
+The caller must initialize ``size`` to the size of the buffer in +bytes, including both the ``size`` field and the space available for +the policy struct. It is recommended to use ``sizeof(struct +fscrypt_get_policy_ex_args)``.
+
+On successful return, ``size`` is set to the actual number of bytes +returned, including both the ``size`` field and the actual size of the +returned policy struct. In addition, the ``version`` field should be +used to determine the actual policy version returned. Note that the +version code for the "v1" policy is actually 0 +(FSCRYPT_POLICY_VERSION_LEGACY).
-FS_IOC_GET_ENCRYPTION_POLICY can fail with the following errors:
+FS_IOC_GET_ENCRYPTION_POLICY_EX can fail with the following errors:
- ``EINVAL``: the file is encrypted, but it uses an unrecognized
- encryption context format
+ encryption policy version; or, an invalid ``size`` was provided
- ``ENODATA``: the file is not encrypted
-- ``ENOTTY``: this type of filesystem does not implement encryption
+- ``ENOTTY``: this type of filesystem does not implement encryption, + or this kernel is too old to support FS_IOC_GET_ENCRYPTION_POLICY_EX + (try FS_IOC_GET_ENCRYPTION_POLICY instead) - ``EOPNOTSUPP``: the kernel was not configured with encryption support for this filesystem +- ``EOVERFLOW``: the file is encrypted and uses a recognized + encryption policy version, but the policy struct does not fit into + the provided buffer
Note: if you only need to know whether a file is encrypted or not, on most filesystems it is also possible to use the FS_IOC_GETFLAGS ioctl and check for FS_ENCRYPT_FL, or to use the statx() system call and check for STATX_ATTR_ENCRYPTED in stx_attributes.
+Legacy method
+~~~~~~~~~~~~~
+
+The FS_IOC_GET_ENCRYPTION_POLICY ioctl can also retrieve the +encryption policy, if any, for a directory or regular file. However, +unlike the extended version (FS_IOC_GET_ENCRYPTION_POLICY_EX), +FS_IOC_GET_ENCRYPTION_POLICY only supports the original policy +version. It takes in a pointer directly to a :c:type:struct +fscrypt_policy_v1 rather than a :c:type:struct +fscrypt_get_policy_ex_args.
+
+The error codes for FS_IOC_GET_ENCRYPTION_POLICY are the same as those +for FS_IOC_GET_ENCRYPTION_POLICY_EX, except that +FS_IOC_GET_ENCRYPTION_POLICY also returns ``EINVAL`` if the file is +encrypted using a newer encryption policy version.
+
Getting the per-filesystem salt
-------------------------------
@@ -365,8 +528,97 @@ generate and manage any needed salt(s) in userspace.
Adding keys
-----------
-To provide a master key, userspace must add it to an appropriate -keyring using the add_key() system call (see:
+Preferred method
+~~~~~~~~~~~~~~~~
+
+The FS_IOC_ADD_ENCRYPTION_KEY ioctl adds a master encryption key to +the filesystem, making all files on the filesystem which were +encrypted using that key appear "unlocked", i.e. in plaintext form. +It takes in a pointer to a :c:type:struct fscrypt_add_key_args, +defined as follows::
+
+ struct fscrypt_add_key_args {
+ __u32 raw_size; + __u32 reserved1; + __u64 reserved2[2]; + struct fscrypt_key_specifier key_spec; + __u8 raw[];
+ };
+
+ struct fscrypt_key_specifier {
+ __u32 type;
+ #define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1
+ #define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2
+ __u32 reserved;
+ union {
+ __u8 max_specifier[32];
+ __u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
+ __u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];+ };
+ };
+
+:c:type:struct fscrypt_add_key_args must be initialized as follows:
+
+- ``raw_size`` must be the size of the ``raw`` key provided, in bytes. +
+- If the key is being added for use by v1 encryption policies, then + ``key_spec.type`` must contain FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR, and + ``key_spec.descriptor`` must contain the descriptor of the key being + added, corresponding to the value in the ``master_key_descriptor`` + field of :c:type:`struct fscrypt_policy_v1`. To add this type of + key, the calling process must have the CAP_SYS_ADMIN capability in + the initial user namespace. + + Alternatively, if the key is being added for use by v2 encryption + policies, then ``key_spec.type`` must contain + FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER, and ``key_spec.identifier`` is an + *output* field which the kernel fills in with a cryptographic hash + of the key. To add this type of key, the calling process does not + need any privileges. However, the number of keys that can be added + is limited by the user's quota for the keyrings service (see + ``Documentation/security/keys/core.rst``). + +- ``raw`` is a variable-length field which must contain the actual + key, ``raw_size`` bytes long.
+
+- All reserved fields must be zeroed.
+
+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 +- ``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 +- ``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
+
+Legacy method
+~~~~~~~~~~~~~
+
+For v1 encryption policies, a master encryption key can also be +provided by adding it to a process-subscribed keyring, e.g. to a +session keyring, or to a user keyring if the user keyring is linked +into the session keyring.
+
+This method is deprecated (and not supported for v2 encryption +policies) for several reasons. First, it cannot be used in
+combination with FS_IOC_REMOVE_ENCRYPTION_KEY (see Removing keys_), +so for removing a key a workaround such as keyctl_unlink() in +combination with ``sync; echo 2 > /proc/sys/vm/drop_caches`` would
+have to be used. Second, it doesn't match the fact that the +locked/unlocked status of encrypted files (i.e. whether they appear to +be in plaintext form or in ciphertext form) is global. This mismatch +has caused much confusion as well as real problems when processes +running under different UIDs, such as a ``sudo`` command, need to +access encrypted files.
+
+Nevertheless, to add a key to one of the process-subscribed keyrings, +the add_key() system call can be used (see:
``Documentation/security/keys/core.rst``). The key type must be "logon"; keys of this type are kept in kernel memory and cannot be
read back by userspace. The key description must be "fscrypt:"
@@ -391,26 +643,143 @@ with a filesystem-specific prefix such as "ext4:". However, the
filesystem-specific prefixes are deprecated and should not be used in new programs.
-There are several different types of keyrings in which encryption keys -may be placed, such as a session keyring, a user session keyring, or a -user keyring. Each key must be placed in a keyring that is "attached" -to all processes that might need to access files encrypted with it, in -the sense that request_key() will find the key. Generally, if only -processes belonging to a specific user need to access a given -encrypted directory and no session keyring has been installed, then -that directory's key should be placed in that user's user session -keyring or user keyring. Otherwise, a session keyring should be -installed if needed, and the key should be linked into that session -keyring, or in a keyring linked into that session keyring. -
-Note: introducing the complex visibility semantics of keyrings here -was arguably a mistake --- especially given that by design, after any -process successfully opens an encrypted file (thereby setting up the -per-file key), possessing the keyring key is not actually required for -any process to read/write the file until its in-memory inode is -evicted. In the future there probably should be a way to provide keys -directly to the filesystem instead, which would make the intended -semantics clearer.
+Removing keys
+-------------
+
+The FS_IOC_REMOVE_ENCRYPTION_KEY ioctl can be used to remove a master +encryption key from the kernel, wiping the corresponding secrets from +memory and causing any files which were "unlocked" with the key to +appear "locked" again. It takes in a pointer to a :c:type:struct +fscrypt_remove_key_args, defined as follows::
+
+ struct fscrypt_remove_key_args {
+ __u32 flags;
+ #define FSCRYPT_REMOVE_KEY_FLAG_ALL_USERS 0x00000001
+ __u32 reserved1; + __u64 reserved2[2]; + struct fscrypt_key_specifier key_spec;
+ };
+
+This structure must be initialized as follows:
+
+- ``flags`` can contain the following flags:
+
+ - ``FSCRYPT_REMOVE_KEY_FLAG_ALL_USERS`` specifies that the key
+ should be removed even if it has also been added by other users. + Specifying this flag requires the CAP_SYS_ADMIN capability in + the initial user namespace.
+
+- The key to remove is specified by ``key_spec``:
+
+ - To remove a key used by v1 encryption policies, set
+ ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill + in ``key_spec.descriptor``. To remove this type of key, the + calling process must have the CAP_SYS_ADMIN capability in the + initial user namespace. + + - To remove a key used by v2 encryption policies, set + ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill + in ``key_spec.identifier``. To remove this type of key, no + privileges are needed. However, users can only remove keys that + they added themselves, subject to privileged override with + FSCRYPT_REMOVE_KEY_FLAG_ALL_USERS.
+
+- All reserved fields must be zeroed.
+
+FS_IOC_REMOVE_ENCRYPTION_KEY can fail with the following errors:
+
+- ``EACCES``: The FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR key specifier type + and/or the FSCRYPT_REMOVE_KEY_FLAG_ALL_USERS flag was specified, but + the caller does not have the CAP_SYS_ADMIN capability in the initial + user namespace +- ``EBUSY``: the master key secret was wiped from memory, but some + files which were unlocked with it are still in use. Such files + could not be locked, nor could their per-file keys be wiped from + memory. The ioctl may be retried later to re-attempt locking the + remaining files. +- ``EINVAL``: invalid flags or key specifier type, or reserved bits + were set +- ``ENOKEY``: the key is not present or has already been removed +- ``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 +- ``EUSERS``: the key cannot be removed because other users have added + it too
+
+Before using this ioctl, please read the Kernel compromise_ section +for a discussion of the security goals and limitations of this ioctl. +
+Getting key status
+------------------
+
+The FS_IOC_GET_ENCRYPTION_KEY_STATUS ioctl retrieves the status of a +master encryption key. It takes in a pointer to a :c:type:struct +fscrypt_get_key_status_args, defined as follows::
+
+ struct fscrypt_get_key_status_args {
+ /* input */ + __u64 reserved1[3]; + struct fscrypt_key_specifier key_spec; + + /* output */ + __u32 status; + #define FSCRYPT_KEY_STATUS_ABSENT 1 + #define FSCRYPT_KEY_STATUS_PRESENT 2 + #define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3 + __u32 status_flags; + #define FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF 0x00000001 + __u32 user_count; + __u32 reserved2; + __u64 reserved3[6];
+ };
+
+The caller must zero ``reserved1``, then fill in ``key_spec``:
+
+ - To get the status of a key for v1 encryption policies, set
+ ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill
+ in ``key_spec.descriptor``.
+
+ - To get the status of a key for v2 encryption policies, set
+ ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill
+ in ``key_spec.identifier``.
+
+On success, 0 is returned and the kernel fills in the output fields:
+
+- ``status`` indicates whether the key is absent, present, or + incompletely removed. Incompletely removed means that the master + secret has been removed, but some files are still in use; i.e., + FS_IOC_REMOVE_ENCRYPTION_KEY returned EBUSY.
+
+- ``status_flags`` can contain the following flags:
+
+ - ``FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF`` indicates that the key
+ has added by the current user. This is only set for keys
+ identified by ``identifier`` rather than by ``descriptor``.
+
+- ``user_count`` specifies the number of users who have added the key. + This is only set for keys identified by ``identifier`` rather than + by ``descriptor``. + +FS_IOC_GET_ENCRYPTION_KEY_STATUS can fail with the following errors: + +- ``EINVAL``: invalid key specifier type, or reserved bits were set +- ``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
+
+Among other use cases, FS_IOC_GET_ENCRYPTION_KEY_STATUS might be +useful for determining whether the key for a given encrypted directory +needs to be added before prompting the user for the passphrase needed +to derive the key.
+
+FS_IOC_GET_ENCRYPTION_KEY_STATUS can only get the status of keys in +the filesystem-level keyring, i.e. the keyring managed by +FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY. It cannot +get the status of a key that has only been added for use by v1 +encryption policies using the legacy mechanism involving +process-subscribed keyrings.
Access semantics
========
@@ -459,7 +828,7 @@ Without the key
Some filesystem operations may be performed on encrypted regular files, directories, and symlinks even before their encryption key has
-been provided:
+been added, or after their encryption key has been removed:
- File metadata may be read, e.g. using stat().
@@ -524,20 +893,20 @@ Encryption context
------------------
An encryption policy is represented on-disk by a :c:type:`struct
-fscrypt_context`. It is up to individual filesystems to decide where -to store it, but normally it would be stored in a hidden extended -attribute. It should *not* be exposed by the xattr-related system -calls such as getxattr() and setxattr() because of the special -semantics of the encryption xattr. (In particular, there would be -much confusion if an encryption policy were to be added to or removed -from anything other than an empty directory.) The struct is defined -as follows::
+fscrypt_context_v1 or a :c:type:struct fscrypt_context_v2`. It is +up to individual filesystems to decide where to store it, but normally +it would be stored in a hidden extended attribute. It should *not* be +exposed by the xattr-related system calls such as getxattr() and +setxattr() because of the special semantics of the encryption xattr. +(In particular, there would be much confusion if an encryption policy +were to be added to or removed from anything other than an empty +directory.) These structs are defined as follows::
- #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
#define FS_KEY_DERIVATION_NONCE_SIZE 16
- struct fscrypt_context {
- u8 format;
+ #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
+ struct fscrypt_context_v1 {
+ u8 version;
u8 contents_encryption_mode;
u8 filenames_encryption_mode;
u8 flags;
@@ -545,12 +914,22 @@ as follows::
u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
};-Note that :c:type:struct fscrypt_context contains the same -information as :c:type:struct fscrypt_policy (see Setting an -encryption policy_), except that :c:type:struct fscrypt_context -also contains a nonce. The nonce is randomly generated by the kernel -and is used to derive the inode's encryption key as described in -Per-file keys_.
+ #define FSCRYPT_KEY_IDENTIFIER_SIZE 16
+ struct fscrypt_context_v2 {
+ u8 version; + u8 contents_encryption_mode; + u8 filenames_encryption_mode; + u8 flags; + u8 reserved[4]; + u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; + u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
+ };
+
+Note that the context structs contain the same information as the +corresponding policy structs (see Setting an encryption policy_), +except that the context structs also contain a nonce. The nonce is +randomly generated by the kernel and is used to derive the inode's +encryption key as described in Per-file keys_.
Data path changes
-----------------
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog