fscrypt: add new helper functions for test_dummy_encryption

Unfortunately the design of fscrypt_set_test_dummy_encryption() doesn't
work properly for the new mount API, as it combines too many steps into
one function:

  - Parse the argument to test_dummy_encryption
  - Check the setting against the filesystem instance
  - Apply the setting to the filesystem instance

The new mount API has split these into separate steps.  ext4 partially
worked around this by duplicating some of the logic, but it still had
some bugs.  To address this, add some new helper functions that split up
the steps of fscrypt_set_test_dummy_encryption():

  - fscrypt_parse_test_dummy_encryption()
  - fscrypt_dummy_policies_equal()
  - fscrypt_add_test_dummy_key()

While we're add it, also add a function fscrypt_is_dummy_policy_set()
which will be useful to avoid some #ifdef's.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Link: https://lore.kernel.org/r/20220501050857.538984-5-ebiggers@kernel.org
This commit is contained in:
Eric Biggers 2022-04-30 22:08:54 -07:00
parent bfb9700bdf
commit 218d921b58
4 changed files with 152 additions and 69 deletions

View File

@ -545,8 +545,8 @@ struct key *
fscrypt_find_master_key(struct super_block *sb,
const struct fscrypt_key_specifier *mk_spec);
int fscrypt_add_test_dummy_key(struct super_block *sb,
struct fscrypt_key_specifier *key_spec);
int fscrypt_get_test_dummy_key_identifier(
u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
int fscrypt_verify_key_added(struct super_block *sb,
const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);

View File

@ -688,29 +688,69 @@ out_wipe_secret:
}
EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
/*
* Add the key for '-o test_dummy_encryption' to the filesystem keyring.
*
* Use a per-boot random key to prevent people from misusing this option.
*/
int fscrypt_add_test_dummy_key(struct super_block *sb,
struct fscrypt_key_specifier *key_spec)
static void
fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret *secret)
{
static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
struct fscrypt_master_key_secret secret;
int err;
get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
memset(&secret, 0, sizeof(secret));
secret.size = FSCRYPT_MAX_KEY_SIZE;
memcpy(secret.raw, test_key, FSCRYPT_MAX_KEY_SIZE);
memset(secret, 0, sizeof(*secret));
secret->size = FSCRYPT_MAX_KEY_SIZE;
memcpy(secret->raw, test_key, FSCRYPT_MAX_KEY_SIZE);
}
err = add_master_key(sb, &secret, key_spec);
int fscrypt_get_test_dummy_key_identifier(
u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
{
struct fscrypt_master_key_secret secret;
int err;
fscrypt_get_test_dummy_secret(&secret);
err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
if (err)
goto out;
err = fscrypt_hkdf_expand(&secret.hkdf, HKDF_CONTEXT_KEY_IDENTIFIER,
NULL, 0, key_identifier,
FSCRYPT_KEY_IDENTIFIER_SIZE);
out:
wipe_master_key_secret(&secret);
return err;
}
/**
* fscrypt_add_test_dummy_key() - add the test dummy encryption key
* @sb: the filesystem instance to add the key to
* @dummy_policy: the encryption policy for test_dummy_encryption
*
* If needed, add the key for the test_dummy_encryption mount option to the
* filesystem. To prevent misuse of this mount option, a per-boot random key is
* used instead of a hardcoded one. This makes it so that any encrypted files
* created using this option won't be accessible after a reboot.
*
* Return: 0 on success, -errno on failure
*/
int fscrypt_add_test_dummy_key(struct super_block *sb,
const struct fscrypt_dummy_policy *dummy_policy)
{
const union fscrypt_policy *policy = dummy_policy->policy;
struct fscrypt_key_specifier key_spec;
struct fscrypt_master_key_secret secret;
int err;
if (!policy)
return 0;
err = fscrypt_policy_to_key_spec(policy, &key_spec);
if (err)
return err;
fscrypt_get_test_dummy_secret(&secret);
err = add_master_key(sb, &secret, &key_spec);
wipe_master_key_secret(&secret);
return err;
}
EXPORT_SYMBOL_GPL(fscrypt_add_test_dummy_key);
/*
* Verify that the current user has added a master key with the given identifier
* (returns -ENOKEY if not). This is needed to prevent a user from encrypting

View File

@ -10,6 +10,7 @@
* Modified by Eric Biggers, 2019 for v2 policy support.
*/
#include <linux/fs_context.h>
#include <linux/random.h>
#include <linux/seq_file.h>
#include <linux/string.h>
@ -724,73 +725,45 @@ int fscrypt_set_context(struct inode *inode, void *fs_data)
EXPORT_SYMBOL_GPL(fscrypt_set_context);
/**
* fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
* @sb: the filesystem on which test_dummy_encryption is being specified
* @arg: the argument to the test_dummy_encryption option. May be NULL.
* @dummy_policy: the filesystem's current dummy policy (input/output, see
* below)
* fscrypt_parse_test_dummy_encryption() - parse the test_dummy_encryption mount option
* @param: the mount option
* @dummy_policy: (input/output) the place to write the dummy policy that will
* result from parsing the option. Zero-initialize this. If a policy is
* already set here (due to test_dummy_encryption being given multiple
* times), then this function will verify that the policies are the same.
*
* Handle the test_dummy_encryption mount option by creating a dummy encryption
* policy, saving it in @dummy_policy, and adding the corresponding dummy
* encryption key to the filesystem. If the @dummy_policy is already set, then
* instead validate that it matches @arg. Don't support changing it via
* remount, as that is difficult to do safely.
*
* Return: 0 on success (dummy policy set, or the same policy is already set);
* -EEXIST if a different dummy policy is already set;
* or another -errno value.
* Return: 0 on success; -EINVAL if the argument is invalid; -EEXIST if the
* argument conflicts with one already specified; or -ENOMEM.
*/
int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
struct fscrypt_dummy_policy *dummy_policy)
{
struct fscrypt_key_specifier key_spec = { 0 };
int version;
union fscrypt_policy *policy = NULL;
const char *arg = "v2";
union fscrypt_policy *policy;
int err;
if (!arg)
arg = "v2";
if (!strcmp(arg, "v1")) {
version = FSCRYPT_POLICY_V1;
key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
memset(key_spec.u.descriptor, 0x42,
FSCRYPT_KEY_DESCRIPTOR_SIZE);
} else if (!strcmp(arg, "v2")) {
version = FSCRYPT_POLICY_V2;
key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
/* key_spec.u.identifier gets filled in when adding the key */
} else {
err = -EINVAL;
goto out;
}
if (param->type == fs_value_is_string && *param->string)
arg = param->string;
policy = kzalloc(sizeof(*policy), GFP_KERNEL);
if (!policy) {
err = -ENOMEM;
goto out;
}
if (!policy)
return -ENOMEM;
err = fscrypt_add_test_dummy_key(sb, &key_spec);
if (err)
goto out;
policy->version = version;
switch (policy->version) {
case FSCRYPT_POLICY_V1:
if (!strcmp(arg, "v1")) {
policy->version = FSCRYPT_POLICY_V1;
policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor,
memset(policy->v1.master_key_descriptor, 0x42,
FSCRYPT_KEY_DESCRIPTOR_SIZE);
break;
case FSCRYPT_POLICY_V2:
} else if (!strcmp(arg, "v2")) {
policy->version = FSCRYPT_POLICY_V2;
policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
memcpy(policy->v2.master_key_identifier, key_spec.u.identifier,
FSCRYPT_KEY_IDENTIFIER_SIZE);
break;
default:
WARN_ON(1);
err = fscrypt_get_test_dummy_key_identifier(
policy->v2.master_key_identifier);
if (err)
goto out;
} else {
err = -EINVAL;
goto out;
}
@ -809,6 +782,37 @@ out:
kfree(policy);
return err;
}
EXPORT_SYMBOL_GPL(fscrypt_parse_test_dummy_encryption);
/**
* fscrypt_dummy_policies_equal() - check whether two dummy policies are equal
* @p1: the first test dummy policy (may be unset)
* @p2: the second test dummy policy (may be unset)
*
* Return: %true if the dummy policies are both set and equal, or both unset.
*/
bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
const struct fscrypt_dummy_policy *p2)
{
if (!p1->policy && !p2->policy)
return true;
if (!p1->policy || !p2->policy)
return false;
return fscrypt_policies_equal(p1->policy, p2->policy);
}
EXPORT_SYMBOL_GPL(fscrypt_dummy_policies_equal);
/* Deprecated, do not use */
int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
struct fscrypt_dummy_policy *dummy_policy)
{
struct fs_parameter param = {
.type = fs_value_is_string,
.string = arg ? (char *)arg : "",
};
return fscrypt_parse_test_dummy_encryption(&param, dummy_policy) ?:
fscrypt_add_test_dummy_key(sb, dummy_policy);
}
EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
/**

View File

@ -32,6 +32,7 @@
union fscrypt_policy;
struct fscrypt_info;
struct fs_parameter;
struct seq_file;
struct fscrypt_str {
@ -289,10 +290,19 @@ struct fscrypt_dummy_policy {
const union fscrypt_policy *policy;
};
int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
struct fscrypt_dummy_policy *dummy_policy);
bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
const struct fscrypt_dummy_policy *p2);
int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
struct fscrypt_dummy_policy *dummy_policy);
void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
struct super_block *sb);
static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
{
return dummy_policy->policy != NULL;
}
static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
{
@ -303,6 +313,8 @@ fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
/* keyring.c */
void fscrypt_sb_free(struct super_block *sb);
int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
int fscrypt_add_test_dummy_key(struct super_block *sb,
const struct fscrypt_dummy_policy *dummy_policy);
int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
@ -477,12 +489,32 @@ static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
struct fscrypt_dummy_policy {
};
static inline int
fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
struct fscrypt_dummy_policy *dummy_policy)
{
return -EINVAL;
}
static inline bool
fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
const struct fscrypt_dummy_policy *p2)
{
return true;
}
static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
char sep,
struct super_block *sb)
{
}
static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
{
return false;
}
static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
{
@ -498,6 +530,13 @@ static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
return -EOPNOTSUPP;
}
static inline int
fscrypt_add_test_dummy_key(struct super_block *sb,
const struct fscrypt_dummy_policy *dummy_policy)
{
return 0;
}
static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
{
return -EOPNOTSUPP;