// SPDX-License-Identifier: GPL-2.0 /* * Encryption policy functions for per-file encryption support. * * Copyright (C) 2015, Google, Inc. * Copyright (C) 2015, Motorola Mobility. * * Originally written by Michael Halcrow, 2015. * Modified by Jaegeuk Kim, 2015. * Modified by Eric Biggers, 2019 for v2 policy support.
*/
/** * fscrypt_policies_equal() - check whether two encryption policies are the same * @policy1: the first policy * @policy2: the second policy * * Return: %true if equal, else %false
*/ bool fscrypt_policies_equal(constunion fscrypt_policy *policy1, constunion fscrypt_policy *policy2)
{ if (policy1->version != policy2->version) returnfalse;
/* * Return %true if the given combination of encryption modes is supported for v1 * (and later) encryption policies. * * Do *not* add anything new here, since v1 encryption policies are deprecated. * New combinations of modes should go in fscrypt_valid_enc_modes_v2() only.
*/ staticbool fscrypt_valid_enc_modes_v1(u32 contents_mode, u32 filenames_mode)
{ if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
filenames_mode == FSCRYPT_MODE_AES_256_CTS) returntrue;
if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
filenames_mode == FSCRYPT_MODE_AES_128_CTS) returntrue;
if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
filenames_mode == FSCRYPT_MODE_ADIANTUM) returntrue;
if (contents_mode != filenames_mode) {
fscrypt_warn(inode, "Direct key flag not allowed with different contents and filenames modes"); returnfalse;
}
mode = &fscrypt_modes[contents_mode];
if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
fscrypt_warn(inode, "Direct key flag not allowed with %s",
mode->friendly_name); returnfalse;
} returntrue;
}
/* * IV_INO_LBLK_* exist only because of hardware limitations, and * currently the only known use case for them involves AES-256-XTS. * That's also all we test currently. For these reasons, for now only * allow AES-256-XTS here. This can be relaxed later if a use case for * IV_INO_LBLK_* with other encryption modes arises.
*/ if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
fscrypt_warn(inode, "Can't use %s policy with contents mode other than AES-256-XTS",
type); returnfalse;
}
/* * It's unsafe to include inode numbers in the IVs if the filesystem can * potentially renumber inodes, e.g. via filesystem shrinking.
*/ if (!sb->s_cop->has_stable_inodes ||
!sb->s_cop->has_stable_inodes(sb)) {
fscrypt_warn(inode, "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
type, sb->s_id); returnfalse;
}
/* * IV_INO_LBLK_64 and IV_INO_LBLK_32 both require that inode numbers fit * in 32 bits. In principle, IV_INO_LBLK_32 could support longer inode * numbers because it hashes the inode number; however, currently the * inode number is gotten from inode::i_ino which is 'unsigned long'. * So for now the implementation limit is 32 bits.
*/ if (!sb->s_cop->has_32bit_inodes) {
fscrypt_warn(inode, "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
type, sb->s_id); returnfalse;
}
/* * IV_INO_LBLK_64 and IV_INO_LBLK_32 both require that file data unit * indices fit in 32 bits.
*/ if (fscrypt_max_file_dun_bits(sb,
fscrypt_policy_v2_du_bits(policy, inode)) > 32) {
fscrypt_warn(inode, "Can't use %s policy on filesystem '%s' because its maximum file size is too large",
type, sb->s_id); returnfalse;
} returntrue;
}
if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
!supported_direct_key_modes(inode, policy->contents_encryption_mode,
policy->filenames_encryption_mode)) returnfalse;
if (IS_CASEFOLDED(inode)) { /* With v1, there's no way to derive dirhash keys. */
fscrypt_warn(inode, "v1 policies can't be used on casefolded directories"); returnfalse;
}
if (policy->log2_data_unit_size) { if (!inode->i_sb->s_cop->supports_subblock_data_units) {
fscrypt_warn(inode, "Filesystem does not support configuring crypto data unit size"); returnfalse;
} if (policy->log2_data_unit_size > inode->i_blkbits ||
policy->log2_data_unit_size < SECTOR_SHIFT /* 9 */) {
fscrypt_warn(inode, "Unsupported log2_data_unit_size in encryption policy: %d",
policy->log2_data_unit_size); returnfalse;
} if (policy->log2_data_unit_size != inode->i_blkbits &&
(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) { /* * Not safe to enable yet, as we need to ensure that DUN * wraparound can only occur on a FS block boundary.
*/
fscrypt_warn(inode, "Sub-block data units not yet supported with IV_INO_LBLK_32"); returnfalse;
}
}
if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
!supported_direct_key_modes(inode, policy->contents_encryption_mode,
policy->filenames_encryption_mode)) returnfalse;
if ((policy->flags & (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) &&
!supported_iv_ino_lblk_policy(policy, inode)) returnfalse;
if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
fscrypt_warn(inode, "Reserved bits set in encryption policy"); returnfalse;
}
returntrue;
}
/** * fscrypt_supported_policy() - check whether an encryption policy is supported * @policy_u: the encryption policy * @inode: the inode on which the policy will be used * * Given an encryption policy, check whether all its encryption modes and other * settings are supported by this kernel on the given inode. (But we don't * currently don't check for crypto API support here, so attempting to use an * algorithm not configured into the crypto API will still fail later.) * * Return: %true if supported, else %false
*/ bool fscrypt_supported_policy(constunion fscrypt_policy *policy_u, conststruct inode *inode)
{ switch (policy_u->version) { case FSCRYPT_POLICY_V1: return fscrypt_supported_v1_policy(&policy_u->v1, inode); case FSCRYPT_POLICY_V2: return fscrypt_supported_v2_policy(&policy_u->v2, inode);
} returnfalse;
}
/** * fscrypt_new_context() - create a new fscrypt_context * @ctx_u: output context * @policy_u: input policy * @nonce: nonce to use * * Create an fscrypt_context for an inode that is being assigned the given * encryption policy. @nonce must be a new random nonce. * * Return: the size of the new context in bytes.
*/ staticint fscrypt_new_context(union fscrypt_context *ctx_u, constunion fscrypt_policy *policy_u, const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
{
memset(ctx_u, 0, sizeof(*ctx_u));
/** * fscrypt_policy_from_context() - convert an fscrypt_context to * an fscrypt_policy * @policy_u: output policy * @ctx_u: input context * @ctx_size: size of input context in bytes * * Given an fscrypt_context, build the corresponding fscrypt_policy. * * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized * version number or size. * * This does *not* validate the settings within the policy itself, e.g. the * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
*/ int fscrypt_policy_from_context(union fscrypt_policy *policy_u, constunion fscrypt_context *ctx_u, int ctx_size)
{
memset(policy_u, 0, sizeof(*policy_u));
if (!fscrypt_context_is_valid(ctx_u, ctx_size)) return -EINVAL;
staticint set_encryption_policy(struct inode *inode, constunion fscrypt_policy *policy)
{
u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; union fscrypt_context ctx; int ctxsize; int err;
if (!fscrypt_supported_policy(policy, inode)) return -EINVAL;
switch (policy->version) { case FSCRYPT_POLICY_V1: /* * The original encryption policy version provided no way of * verifying that the correct master key was supplied, which was * insecure in scenarios where multiple users have access to the * same encrypted files (even just read-only access). The new * encryption policy version fixes this and also implies use of * an improved key derivation function and allows non-root users * to securely remove keys. So as long as compatibility with * old kernels isn't required, it is recommended to use the new * policy version for all new encrypted directories.
*/
pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
current->comm, current->pid); break; case FSCRYPT_POLICY_V2:
err = fscrypt_verify_key_added(inode->i_sb,
policy->v2.master_key_identifier); if (err) return err; if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy. This should only be used if there are certain hardware limitations.\n",
current->comm, current->pid); break; default:
WARN_ON_ONCE(1); return -EINVAL;
}
int fscrypt_ioctl_set_policy(struct file *filp, constvoid __user *arg)
{ union fscrypt_policy policy; union fscrypt_policy existing_policy; struct inode *inode = file_inode(filp);
u8 version; int size; int ret;
if (get_user(policy.version, (const u8 __user *)arg)) return -EFAULT;
size = fscrypt_policy_size(&policy); if (size <= 0) return -EINVAL;
/* * We should just copy the remaining 'size - 1' bytes here, but a * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to * think that size can be 0 here (despite the check above!) *and* that * it's a compile-time constant. Thus it would think copy_from_user() * is passed compile-time constant ULONG_MAX, causing the compile-time * buffer overflow check to fail, breaking the build. This only occurred * when building an i386 kernel with -Os and branch profiling enabled. * * Work around it by just copying the first byte again...
*/
version = policy.version; if (copy_from_user(&policy, arg, size)) return -EFAULT;
policy.version = version;
if (!inode_owner_or_capable(&nop_mnt_idmap, inode)) return -EACCES;
ret = mnt_want_write_file(filp); if (ret) return ret;
inode_lock(inode);
ret = fscrypt_get_policy(inode, &existing_policy); if (ret == -ENODATA) { if (!S_ISDIR(inode->i_mode))
ret = -ENOTDIR; elseif (IS_DEADDIR(inode))
ret = -ENOENT; elseif (!inode->i_sb->s_cop->empty_dir(inode))
ret = -ENOTEMPTY; else
ret = set_encryption_policy(inode, &policy);
} elseif (ret == -EINVAL ||
(ret == 0 && !fscrypt_policies_equal(&policy,
&existing_policy))) { /* The file already uses a different encryption policy. */
ret = -EEXIST;
}
/* Original ioctl version; can only get the original policy version */ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
{ union fscrypt_policy policy; int err;
err = fscrypt_get_policy(file_inode(filp), &policy); if (err) return err;
if (policy.version != FSCRYPT_POLICY_V1) return -EINVAL;
if (copy_to_user(arg, &policy, sizeof(policy.v1))) return -EFAULT; return 0;
}
EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
/* Extended ioctl version; can get policies of any version */ int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
{ struct fscrypt_get_policy_ex_arg arg; union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
size_t policy_size; int err;
/* arg is policy_size, then policy */
BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
offsetof(typeof(arg), policy));
BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
/* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */ int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
{ struct inode *inode = file_inode(filp); union fscrypt_context ctx; int ret;
ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); if (ret < 0) return ret; if (!fscrypt_context_is_valid(&ctx, ret)) return -EINVAL; if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
FSCRYPT_FILE_NONCE_SIZE)) return -EFAULT; return 0;
}
EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
/** * fscrypt_has_permitted_context() - is a file's encryption policy permitted * within its directory? * * @parent: inode for parent directory * @child: inode for file being looked up, opened, or linked into @parent * * Filesystems must call this before permitting access to an inode in a * situation where the parent directory is encrypted (either before allowing * ->lookup() to succeed, or for a regular file before allowing it to be opened) * and before any operation that involves linking an inode into an encrypted * directory, including link, rename, and cross rename. It enforces the * constraint that within a given encrypted directory tree, all files use the * same encryption policy. The pre-access check is needed to detect potentially * malicious offline violations of this constraint, while the link and rename * checks are needed to prevent online violations of this constraint. * * Return: 1 if permitted, 0 if forbidden.
*/ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
{ union fscrypt_policy parent_policy, child_policy; int err, err1, err2;
/* No restrictions on file types which are never encrypted */ if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
!S_ISLNK(child->i_mode)) return 1;
/* No restrictions if the parent directory is unencrypted */ if (!IS_ENCRYPTED(parent)) return 1;
/* Encrypted directories must not contain unencrypted files */ if (!IS_ENCRYPTED(child)) return 0;
/* * Both parent and child are encrypted, so verify they use the same * encryption policy. Compare the cached policies if the keys are * available, otherwise retrieve and compare the fscrypt_contexts. * * Note that the fscrypt_context retrieval will be required frequently * when accessing an encrypted directory tree without the key. * Performance-wise this is not a big deal because we already don't * really optimize for file access without the key (to the extent that * such access is even possible), given that any attempted access * already causes a fscrypt_context retrieval and keyring search. * * In any case, if an unexpected error occurs, fall back to "forbidden".
*/
err = fscrypt_get_encryption_info(parent, true); if (err) return 0;
err = fscrypt_get_encryption_info(child, true); if (err) return 0;
/* * Allow the case where the parent and child both have an unrecognized * encryption policy, so that files with an unrecognized encryption * policy can be deleted.
*/ if (err1 == -EINVAL && err2 == -EINVAL) return 1;
/* * Return the encryption policy that new files in the directory will inherit, or * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also * ensure that its key is set up, so that the new filename can be encrypted.
*/ constunion fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
{ int err;
if (IS_ENCRYPTED(dir)) {
err = fscrypt_require_key(dir); if (err) return ERR_PTR(err); return &dir->i_crypt_info->ci_policy;
}
return fscrypt_get_dummy_policy(dir->i_sb);
}
/** * fscrypt_context_for_new_inode() - create an encryption context for a new inode * @ctx: where context should be written * @inode: inode from which to fetch policy and nonce * * Given an in-core "prepared" (via fscrypt_prepare_new_inode) inode, * generate a new context and write it to ctx. ctx _must_ be at least * FSCRYPT_SET_CONTEXT_MAX_SIZE bytes. * * Return: size of the resulting context or a negative error code.
*/ int fscrypt_context_for_new_inode(void *ctx, struct inode *inode)
{ struct fscrypt_inode_info *ci = inode->i_crypt_info;
/** * fscrypt_set_context() - Set the fscrypt context of a new inode * @inode: a new inode * @fs_data: private data given by FS and passed to ->set_context() * * This should be called after fscrypt_prepare_new_inode(), generally during a * filesystem transaction. Everything here must be %GFP_NOFS-safe. * * Return: 0 on success, -errno on failure
*/ int fscrypt_set_context(struct inode *inode, void *fs_data)
{ struct fscrypt_inode_info *ci = inode->i_crypt_info; union fscrypt_context ctx; int ctxsize;
ctxsize = fscrypt_context_for_new_inode(&ctx, inode); if (ctxsize < 0) return ctxsize;
/* * This may be the first time the inode number is available, so do any * delayed key setup that requires the inode number.
*/ if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
(ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
fscrypt_hash_inode_number(ci, ci->ci_master_key);
/** * 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. * * Return: 0 on success; -EINVAL if the argument is invalid; -EEXIST if the * argument conflicts with one already specified; or -ENOMEM.
*/ int fscrypt_parse_test_dummy_encryption(conststruct fs_parameter *param, struct fscrypt_dummy_policy *dummy_policy)
{ constchar *arg = "v2"; union fscrypt_policy *policy; int err;
if (param->type == fs_value_is_string && *param->string)
arg = param->string;
policy = kzalloc(sizeof(*policy), GFP_KERNEL); if (!policy) return -ENOMEM;
/** * 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(conststruct fscrypt_dummy_policy *p1, conststruct fscrypt_dummy_policy *p2)
{ if (!p1->policy && !p2->policy) returntrue; if (!p1->policy || !p2->policy) returnfalse; return fscrypt_policies_equal(p1->policy, p2->policy);
}
EXPORT_SYMBOL_GPL(fscrypt_dummy_policies_equal);
/** * 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)
{ constunion fscrypt_policy *policy = fscrypt_get_dummy_policy(sb); int vers;
if (!policy) return;
vers = policy->version; if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
vers = 1;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.