Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3325bdad authored by Eric Biggers's avatar Eric Biggers Committed by Jaegeuk Kim
Browse files

fscrypt: support test_dummy_encryption=v2

v1 encryption policies are deprecated in favor of v2, and some new
features (e.g. encryption+casefolding) are only being added for v2.

Therefore, the "test_dummy_encryption" mount option (which is used for
encryption I/O testing with xfstests) needs to support v2 policies.

To do this, extend its syntax to be "test_dummy_encryption=v1" or
"test_dummy_encryption=v2".  The existing "test_dummy_encryption" (no
argument) also continues to be accepted, to specify the default setting
-- currently v1, but the next patch changes it to v2.

To cleanly support both v1 and v2 while also making it easy to support
specifying other encryption settings in the future (say, accepting
"$contents_mode:$filenames_mode:v2"), make ext4 and f2fs maintain a
pointer to the dummy fscrypt_context rather than using mount flags.

To avoid concurrency issues, don't allow test_dummy_encryption to be set
or changed during a remount.  (The former restriction is new, but
xfstests doesn't run into it, so no one should notice.)

Tested with 'gce-xfstests -c {ext4,f2fs}/encrypt -g auto'.  On ext4,
there are two regressions, both of which are test bugs: ext4/023 and
ext4/028 fail because they set an xattr and expect it to be stored
inline, but the increase in size of the fscrypt_context from
24 to 40 bytes causes this xattr to be spilled into an external block.

Link: https://lore.kernel.org/r/20200512233251.118314-4-ebiggers@kernel.org


Acked-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
Reviewed-by: default avatarTheodore Ts'o <tytso@mit.edu>
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
parent 37bd5129
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -217,8 +217,12 @@ fsync_mode=%s Control the policy of fsync. Currently supports "posix",
                       pass, but the performance will regress. "nobarrier" is
                       based on "posix", but doesn't issue flush command for
                       non-atomic files likewise "nobarrier" mount option.
test_dummy_encryption  Enable dummy encryption, which provides a fake fscrypt
test_dummy_encryption
test_dummy_encryption=%s
                       Enable dummy encryption, which provides a fake fscrypt
                       context. The fake fscrypt context is used by xfstests.
                       The argument may be either "v1" or "v2", in order to
                       select the corresponding fscrypt policy version.
checkpoint=%s[:%u[%]]     Set to "disable" to turn off checkpointing. Set to "enable"
                       to reenable checkpointing. Is enabled by default. While
                       disabled, any unmounting or unexpected shutdowns will cause
+6 −9
Original line number Diff line number Diff line
@@ -395,21 +395,18 @@ int fscrypt_get_encryption_info(struct inode *inode)

	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
	if (res < 0) {
		if (!fscrypt_dummy_context_enabled(inode) ||
		    IS_ENCRYPTED(inode)) {
		const union fscrypt_context *dummy_ctx =
			fscrypt_get_dummy_context(inode->i_sb);

		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
			fscrypt_warn(inode,
				     "Error %d getting encryption context",
				     res);
			return res;
		}
		/* Fake up a context for an unencrypted directory */
		memset(&ctx, 0, sizeof(ctx));
		ctx.version = FSCRYPT_CONTEXT_V1;
		ctx.v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
		ctx.v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
		memset(ctx.v1.master_key_descriptor, 0x42,
		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
		res = sizeof(ctx.v1);
		res = fscrypt_context_size(dummy_ctx);
		memcpy(&ctx, dummy_ctx, res);
	}

	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
+125 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
 */

#include <linux/random.h>
#include <linux/seq_file.h>
#include <linux/string.h>
#include <linux/mount.h>
#include "fscrypt_private.h"
@@ -616,3 +617,127 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
	return preload ? fscrypt_get_encryption_info(child): 0;
}
EXPORT_SYMBOL(fscrypt_inherit_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.
 *	 If no argument was specified, then @arg->from == NULL.
 * @dummy_ctx: the filesystem's current dummy context (input/output, see below)
 *
 * Handle the test_dummy_encryption mount option by creating a dummy encryption
 * context, saving it in @dummy_ctx, and adding the corresponding dummy
 * encryption key to the filesystem.  If the @dummy_ctx is already set, then
 * instead validate that it matches @arg.  Don't support changing it via
 * remount, as that is difficult to do safely.
 *
 * The reason we use an fscrypt_context rather than an fscrypt_policy is because
 * we mustn't generate a new nonce each time we access a dummy-encrypted
 * directory, as that would change the way filenames are encrypted.
 *
 * Return: 0 on success (dummy context set, or the same context is already set);
 *         -EEXIST if a different dummy context is already set;
 *         or another -errno value.
 */
int fscrypt_set_test_dummy_encryption(struct super_block *sb,
				      const substring_t *arg,
				      struct fscrypt_dummy_context *dummy_ctx)
{
	const char *argstr = "v1";
	const char *argstr_to_free = NULL;
	struct fscrypt_key_specifier key_spec = { 0 };
	int version;
	union fscrypt_context *ctx = NULL;
	int err;

	if (arg->from) {
		argstr = argstr_to_free = match_strdup(arg);
		if (!argstr)
			return -ENOMEM;
	}

	if (!strcmp(argstr, "v1")) {
		version = FSCRYPT_CONTEXT_V1;
		key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
		memset(key_spec.u.descriptor, 0x42,
		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
	} else if (!strcmp(argstr, "v2")) {
		version = FSCRYPT_CONTEXT_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 (dummy_ctx->ctx) {
		/*
		 * Note: if we ever make test_dummy_encryption support
		 * specifying other encryption settings, such as the encryption
		 * modes, we'll need to compare those settings here.
		 */
		if (dummy_ctx->ctx->version == version)
			err = 0;
		else
			err = -EEXIST;
		goto out;
	}

	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx) {
		err = -ENOMEM;
		goto out;
	}

	err = fscrypt_add_test_dummy_key(sb, &key_spec);
	if (err)
		goto out;

	ctx->version = version;
	switch (ctx->version) {
	case FSCRYPT_CONTEXT_V1:
		ctx->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
		ctx->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
		memcpy(ctx->v1.master_key_descriptor, key_spec.u.descriptor,
		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
		break;
	case FSCRYPT_CONTEXT_V2:
		ctx->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
		ctx->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
		memcpy(ctx->v2.master_key_identifier, key_spec.u.identifier,
		       FSCRYPT_KEY_IDENTIFIER_SIZE);
		break;
	default:
		WARN_ON(1);
		err = -EINVAL;
		goto out;
	}
	dummy_ctx->ctx = ctx;
	ctx = NULL;
	err = 0;
out:
	kfree(ctx);
	kfree(argstr_to_free);
	return err;
}
EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);

/**
 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
 * @seq: the seq_file to print the option to
 * @sep: the separator character to use
 * @sb: the filesystem whose options are being shown
 *
 * Show the test_dummy_encryption mount option, if it was specified.
 * This is mainly used for /proc/mounts.
 */
void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
					struct super_block *sb)
{
	const union fscrypt_context *ctx = fscrypt_get_dummy_context(sb);

	if (!ctx)
		return;
	seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, ctx->version);
}
EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);
+5 −3
Original line number Diff line number Diff line
@@ -1335,11 +1335,9 @@ struct ext4_super_block {
 */
#define EXT4_MF_MNTDIR_SAMPLED		0x0001
#define EXT4_MF_FS_ABORTED		0x0002	/* Fatal error detected */
#define EXT4_MF_TEST_DUMMY_ENCRYPTION	0x0004

#ifdef CONFIG_FS_ENCRYPTION
#define DUMMY_ENCRYPTION_ENABLED(sbi) (unlikely((sbi)->s_mount_flags & \
						EXT4_MF_TEST_DUMMY_ENCRYPTION))
#define DUMMY_ENCRYPTION_ENABLED(sbi) ((sbi)->s_dummy_enc_ctx.ctx != NULL)
#else
#define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
#endif
@@ -1528,8 +1526,12 @@ struct ext4_sb_info {
	struct ratelimit_state s_warning_ratelimit_state;
	struct ratelimit_state s_msg_ratelimit_state;

	/* Encryption context for '-o test_dummy_encryption' */
	struct fscrypt_dummy_context s_dummy_enc_ctx;

	/* Barrier between changing inodes' journal flags and writepages ops. */
	struct percpu_rw_semaphore s_journal_flag_rwsem;

	struct dax_device *s_daxdev;
};

+54 −14
Original line number Diff line number Diff line
@@ -1011,6 +1011,7 @@ static void ext4_put_super(struct super_block *sb)
		crypto_free_shash(sbi->s_chksum_driver);
	kfree(sbi->s_blockgroup_lock);
	fs_put_dax(sbi->s_daxdev);
	fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
#ifdef CONFIG_UNICODE
	utf8_unload(sbi->s_encoding);
#endif
@@ -1298,9 +1299,10 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
	return res;
}

static bool ext4_dummy_context(struct inode *inode)
static const union fscrypt_context *
ext4_get_dummy_context(struct super_block *sb)
{
	return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
	return EXT4_SB(sb)->s_dummy_enc_ctx.ctx;
}

static bool ext4_has_stable_inodes(struct super_block *sb)
@@ -1319,7 +1321,7 @@ static const struct fscrypt_operations ext4_cryptops = {
	.key_prefix		= "ext4:",
	.get_context		= ext4_get_context,
	.set_context		= ext4_set_context,
	.dummy_context		= ext4_dummy_context,
	.get_dummy_context	= ext4_get_dummy_context,
	.empty_dir		= ext4_empty_dir,
	.max_namelen		= EXT4_NAME_LEN,
	.has_stable_inodes	= ext4_has_stable_inodes,
@@ -1512,6 +1514,7 @@ static const match_table_t tokens = {
	{Opt_init_itable, "init_itable"},
	{Opt_noinit_itable, "noinit_itable"},
	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
	{Opt_test_dummy_encryption, "test_dummy_encryption"},
	{Opt_nombcache, "nombcache"},
	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
@@ -1723,7 +1726,7 @@ static const struct mount_opts {
	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
	{Opt_test_dummy_encryption, 0, MOPT_STRING},
	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
	{Opt_err, 0, 0}
};
@@ -1758,6 +1761,48 @@ static int ext4_sb_read_encoding(const struct ext4_super_block *es,
}
#endif

static int ext4_set_test_dummy_encryption(struct super_block *sb,
					  const char *opt,
					  const substring_t *arg,
					  bool is_remount)
{
#ifdef CONFIG_FS_ENCRYPTION
	struct ext4_sb_info *sbi = EXT4_SB(sb);
	int err;

	/*
	 * This mount option is just for testing, and it's not worthwhile to
	 * implement the extra complexity (e.g. RCU protection) that would be
	 * needed to allow it to be set or changed during remount.  We do allow
	 * it to be specified during remount, but only if there is no change.
	 */
	if (is_remount && !sbi->s_dummy_enc_ctx.ctx) {
		ext4_msg(sb, KERN_WARNING,
			 "Can't set test_dummy_encryption on remount");
		return -1;
	}
	err = fscrypt_set_test_dummy_encryption(sb, arg, &sbi->s_dummy_enc_ctx);
	if (err) {
		if (err == -EEXIST)
			ext4_msg(sb, KERN_WARNING,
				 "Can't change test_dummy_encryption on remount");
		else if (err == -EINVAL)
			ext4_msg(sb, KERN_WARNING,
				 "Value of option \"%s\" is unrecognized", opt);
		else
			ext4_msg(sb, KERN_WARNING,
				 "Error processing option \"%s\" [%d]",
				 opt, err);
		return -1;
	}
	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
#else
	ext4_msg(sb, KERN_WARNING,
		 "Test dummy encryption mount option ignored");
#endif
	return 1;
}

static int handle_mount_opt(struct super_block *sb, char *opt, int token,
			    substring_t *args, unsigned long *journal_devnum,
			    unsigned int *journal_ioprio, int is_remount)
@@ -1940,14 +1985,8 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
		*journal_ioprio =
			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
	} else if (token == Opt_test_dummy_encryption) {
#ifdef CONFIG_FS_ENCRYPTION
		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
		ext4_msg(sb, KERN_WARNING,
			 "Test dummy encryption mode enabled");
#else
		ext4_msg(sb, KERN_WARNING,
			 "Test dummy encryption mount option ignored");
#endif
		return ext4_set_test_dummy_encryption(sb, opt, &args[0],
						      is_remount);
	} else if (m->flags & MOPT_DATAJ) {
		if (is_remount) {
			if (!sbi->s_journal)
@@ -2200,8 +2239,8 @@ static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
	if (test_opt(sb, DATA_ERR_ABORT))
		SEQ_OPTS_PUTS("data_err=abort");
	if (DUMMY_ENCRYPTION_ENABLED(sbi))
		SEQ_OPTS_PUTS("test_dummy_encryption");

	fscrypt_show_test_dummy_encryption(seq, sep, sb);

	ext4_show_quota_options(seq, sb);
	return 0;
@@ -4651,6 +4690,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
	for (i = 0; i < EXT4_MAXQUOTAS; i++)
		kfree(sbi->s_qf_names[i]);
#endif
	fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
	ext4_blkdev_remove(sbi);
	brelse(bh);
out_fail:
Loading