mirror of
https://github.com/torvalds/linux
synced 2024-09-06 18:01:05 +00:00
![Eric Biggers](/assets/img/avatar_default.png)
Until now, fscrypt has always used the filesystem block size as the granularity of file contents encryption. Two scenarios have come up where a sub-block granularity of contents encryption would be useful: 1. Inline crypto hardware that only supports a crypto data unit size that is less than the filesystem block size. 2. Support for direct I/O at a granularity less than the filesystem block size, for example at the block device's logical block size in order to match the traditional direct I/O alignment requirement. (1) first came up with older eMMC inline crypto hardware that only supports a crypto data unit size of 512 bytes. That specific case ultimately went away because all systems with that hardware continued using out of tree code and never actually upgraded to the upstream inline crypto framework. But, now it's coming back in a new way: some current UFS controllers only support a data unit size of 4096 bytes, and there is a proposal to increase the filesystem block size to 16K. (2) was discussed as a "nice to have" feature, though not essential, when support for direct I/O on encrypted files was being upstreamed. Still, the fact that this feature has come up several times does suggest it would be wise to have available. Therefore, this patch implements it by using one of the reserved bytes in fscrypt_policy_v2 to allow users to select a sub-block data unit size. Supported data unit sizes are powers of 2 between 512 and the filesystem block size, inclusively. Support is implemented for both the FS-layer and inline crypto cases. This patch focuses on the basic support for sub-block data units. Some things are out of scope for this patch but may be addressed later: - Supporting sub-block data units in combination with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64, in most cases. Unfortunately this combination usually causes data unit indices to exceed 32 bits, and thus fscrypt_supported_policy() correctly disallows it. The users who potentially need this combination are using f2fs. To support it, f2fs would need to provide an option to slightly reduce its max file size. - Supporting sub-block data units in combination with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32. This has the same problem described above, but also it will need special code to make DUN wraparound still happen on a FS block boundary. - Supporting use case (2) mentioned above. The encrypted direct I/O code will need to stop requiring and assuming FS block alignment. This won't be hard, but it belongs in a separate patch. - Supporting this feature on filesystems other than ext4 and f2fs. (Filesystems declare support for it via their fscrypt_operations.) On UBIFS, sub-block data units don't make sense because UBIFS encrypts variable-length blocks as a result of compression. CephFS could support it, but a bit more work would be needed to make the fscrypt_*_block_inplace functions play nicely with sub-block data units. I don't think there's a use case for this on CephFS anyway. Link: https://lore.kernel.org/r/20230925055451.59499-6-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com>
198 lines
6.5 KiB
C
198 lines
6.5 KiB
C
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
|
|
/*
|
|
* fscrypt user API
|
|
*
|
|
* These ioctls can be used on filesystems that support fscrypt. See the
|
|
* "User API" section of Documentation/filesystems/fscrypt.rst.
|
|
*/
|
|
#ifndef _UAPI_LINUX_FSCRYPT_H
|
|
#define _UAPI_LINUX_FSCRYPT_H
|
|
|
|
#include <linux/ioctl.h>
|
|
#include <linux/types.h>
|
|
|
|
/* 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_FLAG_DIRECT_KEY 0x04
|
|
#define FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 0x08
|
|
#define FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 0x10
|
|
|
|
/* Encryption algorithms */
|
|
#define FSCRYPT_MODE_AES_256_XTS 1
|
|
#define FSCRYPT_MODE_AES_256_CTS 4
|
|
#define FSCRYPT_MODE_AES_128_CBC 5
|
|
#define FSCRYPT_MODE_AES_128_CTS 6
|
|
#define FSCRYPT_MODE_SM4_XTS 7
|
|
#define FSCRYPT_MODE_SM4_CTS 8
|
|
#define FSCRYPT_MODE_ADIANTUM 9
|
|
#define FSCRYPT_MODE_AES_256_HCTR2 10
|
|
/* If adding a mode number > 10, update FSCRYPT_MODE_MAX in fscrypt_private.h */
|
|
|
|
/*
|
|
* Legacy policy version; ad-hoc KDF and no key verification.
|
|
* For new encrypted directories, use fscrypt_policy_v2 instead.
|
|
*
|
|
* Careful: the .version field for this is actually 0, not 1.
|
|
*/
|
|
#define FSCRYPT_POLICY_V1 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];
|
|
};
|
|
|
|
/*
|
|
* Process-subscribed "logon" key description prefix and payload format.
|
|
* Deprecated; prefer FS_IOC_ADD_ENCRYPTION_KEY instead.
|
|
*/
|
|
#define FSCRYPT_KEY_DESC_PREFIX "fscrypt:"
|
|
#define FSCRYPT_KEY_DESC_PREFIX_SIZE 8
|
|
#define FSCRYPT_MAX_KEY_SIZE 64
|
|
struct fscrypt_key {
|
|
__u32 mode;
|
|
__u8 raw[FSCRYPT_MAX_KEY_SIZE];
|
|
__u32 size;
|
|
};
|
|
|
|
/*
|
|
* New policy version with HKDF and key verification (recommended).
|
|
*/
|
|
#define FSCRYPT_POLICY_V2 2
|
|
#define FSCRYPT_KEY_IDENTIFIER_SIZE 16
|
|
struct fscrypt_policy_v2 {
|
|
__u8 version;
|
|
__u8 contents_encryption_mode;
|
|
__u8 filenames_encryption_mode;
|
|
__u8 flags;
|
|
__u8 log2_data_unit_size;
|
|
__u8 __reserved[3];
|
|
__u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
|
|
};
|
|
|
|
/* Struct passed to FS_IOC_GET_ENCRYPTION_POLICY_EX */
|
|
struct fscrypt_get_policy_ex_arg {
|
|
__u64 policy_size; /* input/output */
|
|
union {
|
|
__u8 version;
|
|
struct fscrypt_policy_v1 v1;
|
|
struct fscrypt_policy_v2 v2;
|
|
} policy; /* output */
|
|
};
|
|
|
|
/*
|
|
* v1 policy keys are specified by an arbitrary 8-byte key "descriptor",
|
|
* matching fscrypt_policy_v1::master_key_descriptor.
|
|
*/
|
|
#define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1
|
|
|
|
/*
|
|
* v2 policy keys are specified by a 16-byte key "identifier" which the kernel
|
|
* calculates as a cryptographic hash of the key itself,
|
|
* matching fscrypt_policy_v2::master_key_identifier.
|
|
*/
|
|
#define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2
|
|
|
|
/*
|
|
* Specifies a key, either for v1 or v2 policies. This doesn't contain the
|
|
* actual key itself; this is just the "name" of the key.
|
|
*/
|
|
struct fscrypt_key_specifier {
|
|
__u32 type; /* one of FSCRYPT_KEY_SPEC_TYPE_* */
|
|
__u32 __reserved;
|
|
union {
|
|
__u8 __reserved[32]; /* reserve some extra space */
|
|
__u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
|
|
__u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
|
|
} u;
|
|
};
|
|
|
|
/*
|
|
* Payload of Linux keyring key of type "fscrypt-provisioning", referenced by
|
|
* fscrypt_add_key_arg::key_id as an alternative to fscrypt_add_key_arg::raw.
|
|
*/
|
|
struct fscrypt_provisioning_key_payload {
|
|
__u32 type;
|
|
__u32 __reserved;
|
|
__u8 raw[];
|
|
};
|
|
|
|
/* Struct passed to FS_IOC_ADD_ENCRYPTION_KEY */
|
|
struct fscrypt_add_key_arg {
|
|
struct fscrypt_key_specifier key_spec;
|
|
__u32 raw_size;
|
|
__u32 key_id;
|
|
__u32 __reserved[8];
|
|
__u8 raw[];
|
|
};
|
|
|
|
/* Struct passed to FS_IOC_REMOVE_ENCRYPTION_KEY */
|
|
struct fscrypt_remove_key_arg {
|
|
struct fscrypt_key_specifier key_spec;
|
|
#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY 0x00000001
|
|
#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS 0x00000002
|
|
__u32 removal_status_flags; /* output */
|
|
__u32 __reserved[5];
|
|
};
|
|
|
|
/* Struct passed to FS_IOC_GET_ENCRYPTION_KEY_STATUS */
|
|
struct fscrypt_get_key_status_arg {
|
|
/* input */
|
|
struct fscrypt_key_specifier key_spec;
|
|
__u32 __reserved[6];
|
|
|
|
/* output */
|
|
#define FSCRYPT_KEY_STATUS_ABSENT 1
|
|
#define FSCRYPT_KEY_STATUS_PRESENT 2
|
|
#define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3
|
|
__u32 status;
|
|
#define FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF 0x00000001
|
|
__u32 status_flags;
|
|
__u32 user_count;
|
|
__u32 __out_reserved[13];
|
|
};
|
|
|
|
#define FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct fscrypt_policy_v1)
|
|
#define FS_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16])
|
|
#define FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct fscrypt_policy_v1)
|
|
#define FS_IOC_GET_ENCRYPTION_POLICY_EX _IOWR('f', 22, __u8[9]) /* size + version */
|
|
#define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 23, struct fscrypt_add_key_arg)
|
|
#define FS_IOC_REMOVE_ENCRYPTION_KEY _IOWR('f', 24, struct fscrypt_remove_key_arg)
|
|
#define FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS _IOWR('f', 25, struct fscrypt_remove_key_arg)
|
|
#define FS_IOC_GET_ENCRYPTION_KEY_STATUS _IOWR('f', 26, struct fscrypt_get_key_status_arg)
|
|
#define FS_IOC_GET_ENCRYPTION_NONCE _IOR('f', 27, __u8[16])
|
|
|
|
/**********************************************************************/
|
|
|
|
/* old names; don't add anything new here! */
|
|
#ifndef __KERNEL__
|
|
#define fscrypt_policy fscrypt_policy_v1
|
|
#define FS_KEY_DESCRIPTOR_SIZE FSCRYPT_KEY_DESCRIPTOR_SIZE
|
|
#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_FLAG_DIRECT_KEY FSCRYPT_POLICY_FLAG_DIRECT_KEY
|
|
#define FS_POLICY_FLAGS_VALID 0x07 /* contains old flags only */
|
|
#define FS_ENCRYPTION_MODE_INVALID 0 /* never used */
|
|
#define FS_ENCRYPTION_MODE_AES_256_XTS FSCRYPT_MODE_AES_256_XTS
|
|
#define FS_ENCRYPTION_MODE_AES_256_GCM 2 /* never used */
|
|
#define FS_ENCRYPTION_MODE_AES_256_CBC 3 /* never used */
|
|
#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_ENCRYPTION_MODE_ADIANTUM FSCRYPT_MODE_ADIANTUM
|
|
#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 /* !__KERNEL__ */
|
|
|
|
#endif /* _UAPI_LINUX_FSCRYPT_H */
|