fscrypt: remove kernel-internal constants from UAPI header
Subsystems (2)
Eric Biggers
maintainerPATCH 1/1 fscrypt: remove kernel-internal constants from UAPI header
From: Eric Biggers <[email protected]>
There isn't really any valid reason to use __FSCRYPT_MODE_MAX or FSCRYPT_POLICY_FLAGS_VALID in a userspace program. These constants are only meant to be used by the kernel internally, and they are defined in the UAPI header next to the mode numbers and flags only so that kernel developers don't forget to update them when adding new modes or flags.
In https://lkml.kernel.org/r/[email protected]
there was an example of someone wanting to use __FSCRYPT_MODE_MAX in a user program, and it was wrong because the program would have broken if __FSCRYPT_MODE_MAX were ever increased. So having this definition available is harmful. FSCRYPT_POLICY_FLAGS_VALID has the same problem.
So, remove these definitions from the UAPI header. Replace FSCRYPT_POLICY_FLAGS_VALID with just listing the valid flags explicitly in the one kernel function that needs it. Move __FSCRYPT_MODE_MAX to fscrypt_private.h, remove the double underscores (which were only present to discourage use by userspace), and add a BUILD_BUG_ON() and comments to (hopefully) ensure it is kept in sync.
Keep the old name FS_POLICY_FLAGS_VALID, since it's been around for longer and there's a greater chance that removing it would break source compatibility with some program. Indeed, mtd-utils is using it in an #ifdef, and removing it would introduce compiler warnings (about FS_POLICY_FLAGS_PAD_* being redefined) into the mtd-utils build. However, reduce its value to 0x07 so that it only includes the flags with old names (the ones present before Linux 5.4), and try to make it clear that it's now "frozen" and no new flags should be added to it.
--- fs/crypto/fscrypt_private.h | 9 ++++++--- fs/crypto/keyring.c | 2 +- fs/crypto/keysetup.c | 4 +++- fs/crypto/policy.c | 5 ++++- include/uapi/linux/fscrypt.h | 5 ++--- 5 files changed, 16 insertions(+), 9 deletions(-)
fs/crypto/fscrypt_private.h +6 -3
@@ -25,6 +25,9 @@#define FSCRYPT_CONTEXT_V1 1#define FSCRYPT_CONTEXT_V2 2 +/* Keep this in sync with include/uapi/linux/fscrypt.h */ +#define FSCRYPT_MODE_MAX FSCRYPT_MODE_ADIANTUM +struct fscrypt_context_v1 {u8 version;/* FSCRYPT_CONTEXT_V1 */ u8 contents_encryption_mode;
@@ -491,9 +494,9 @@ struct fscrypt_master_key {* Per-mode encryption keys for the various types of encryption policies * that use them. Allocated and derived on-demand. */ -struct fscrypt_prepared_key mk_direct_keys[__FSCRYPT_MODE_MAX +1 ]; -struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[__FSCRYPT_MODE_MAX +1 ]; -struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[__FSCRYPT_MODE_MAX +1 ]; +struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX +1 ]; +struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX +1 ]; +struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX +1 ];/* Hash key for inode numbers. Initialized only when needed. */ siphash_key_t mk_ino_hash_key;
fs/crypto/keyring.c +1 -1
@@ -44,7 +44,7 @@ static void free_master_key(struct fscrypt_master_key *mk)wipe_master_key_secret (&mk->mk_secret); -for (i =0 ; i <= __FSCRYPT_MODE_MAX; i++) { +for (i =0 ; i <=FSCRYPT_MODE_MAX ; i++) {fscrypt_destroy_prepared_key (&mk->mk_direct_keys[i]);fscrypt_destroy_prepared_key (&mk->mk_iv_ino_lblk_64_keys[i]);fscrypt_destroy_prepared_key (&mk->mk_iv_ino_lblk_32_keys[i]);
fs/crypto/keysetup.c +3 -1
@@ -56,6 +56,8 @@ static struct fscrypt_mode * select_encryption_mode(const union fscrypt_policy *policy, const struct inode *inode) { + BUILD_BUG_ON(ARRAY_SIZE(fscrypt_modes) != FSCRYPT_MODE_MAX + 1); +if (S_ISREG (inode->i_mode))return &fscrypt_modes[fscrypt_policy_contents_mode (policy)];
@@ -168,7 +170,7 @@ static int setup_per_mode_enc_key(struct fscrypt_info *ci,unsigned int hkdf_infolen =0 ;int err; -if (WARN_ON (mode_num > __FSCRYPT_MODE_MAX)) +if (WARN_ON (mode_num >FSCRYPT_MODE_MAX ))return -EINVAL ; prep_key = &keys[mode_num];
fs/crypto/policy.c +4 -1
@@ -175,7 +175,10 @@ static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,return false; } -if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID ) { +if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | +FSCRYPT_POLICY_FLAG_DIRECT_KEY | +FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 | +FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 )) {fscrypt_warn (inode,"Unsupported encryption flags (0x%02x)" , policy->flags);return false;
include/uapi/linux/fscrypt.h +2 -3
@@ -20,7 +20,6 @@#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 -#define FSCRYPT_POLICY_FLAGS_VALID 0x1F/* Encryption algorithms */ #define FSCRYPT_MODE_AES_256_XTS 1
@@ -28,7 +27,7 @@#define FSCRYPT_MODE_AES_128_CBC 5#define FSCRYPT_MODE_AES_128_CTS 6#define FSCRYPT_MODE_ADIANTUM 9 -#define __FSCRYPT_MODE_MAX 9 +/* If adding a mode number > 9, update FSCRYPT_MODE_MAX in fscrypt_private.h */ /* * Legacy policy version; ad-hoc KDF and no key verification.
@@ -177,7 +176,7 @@ struct fscrypt_get_key_status_arg {#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 FSCRYPT_POLICY_FLAGS_VALID +#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 */
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
Quoted 30 lines · Eric Biggers
From: Eric Biggers <[email protected]>
There isn't really any valid reason to use
__FSCRYPT_MODE_MAXorFSCRYPT_POLICY_FLAGS_VALIDin a userspace program. These constants are only meant to be used by the kernel internally, and they are defined in the UAPI header next to the mode numbers and flags only so that kernel developers don't forget to update them when adding new modes or flags.In https://lkml.kernel.org/r/[email protected]
there was an example of someone wanting to use__FSCRYPT_MODE_MAXin a user program, and it was wrong because the program would have broken if__FSCRYPT_MODE_MAXwere ever increased. So having this definition available is harmful.FSCRYPT_POLICY_FLAGS_VALIDhas the same problem.So, remove these definitions from the UAPI header. Replace
FSCRYPT_POLICY_FLAGS_VALIDwith just listing the valid flags explicitly in the one kernel function that needs it. Move__FSCRYPT_MODE_MAXtofscrypt_private.h, remove the double underscores (which were only present to discourage use by userspace), and add aBUILD_BUG_ON()and comments to (hopefully) ensure it is kept in sync.Keep the old name
FS_POLICY_FLAGS_VALID, since it's been around for longer and there's a greater chance that removing it would break source compatibility with some program. Indeed, mtd-utils is using it in an #ifdef, and removing it would introduce compiler warnings (about FS_POLICY_FLAGS_PAD_* being redefined) into the mtd-utils build. However, reduce its value to0x07so that it only includes the flags with old names (the ones present before Linux 5.4), and try to make it clear that it's now "frozen" and no new flags should be added to it.
Quoted 3 lines
Fixes 2336d0deb2d4 ("fscrypt: use FSCRYPT_ prefix for uapi constants")Cc <[email protected]> # v5.4+Signed-off-by Eric Biggers <[email protected]>
Applied to fscrypt.git#master for 5.11.
- Eric