Lines Matching +full:hardware +full:- +full:wise

1 // SPDX-License-Identifier: GPL-2.0
3 * Encryption policy functions for per-file encryption support.
23 * fscrypt_policies_equal() - check whether two encryption policies are the same
32 if (policy1->version != policy2->version) in fscrypt_policies_equal()
41 switch (policy->version) { in fscrypt_policy_to_key_spec()
43 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; in fscrypt_policy_to_key_spec()
44 memcpy(key_spec->u.descriptor, policy->v1.master_key_descriptor, in fscrypt_policy_to_key_spec()
48 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; in fscrypt_policy_to_key_spec()
49 memcpy(key_spec->u.identifier, policy->v2.master_key_identifier, in fscrypt_policy_to_key_spec()
54 return -EINVAL; in fscrypt_policy_to_key_spec()
60 if (!sb->s_cop->get_dummy_policy) in fscrypt_get_dummy_policy()
62 return sb->s_cop->get_dummy_policy(sb); in fscrypt_get_dummy_policy()
114 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) { in supported_direct_key_modes()
116 mode->friendly_name); in supported_direct_key_modes()
125 const char *type = (policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) in supported_iv_ino_lblk_policy()
127 struct super_block *sb = inode->i_sb; in supported_iv_ino_lblk_policy()
130 * IV_INO_LBLK_* exist only because of hardware limitations, and in supported_iv_ino_lblk_policy()
131 * currently the only known use case for them involves AES-256-XTS. in supported_iv_ino_lblk_policy()
133 * allow AES-256-XTS here. This can be relaxed later if a use case for in supported_iv_ino_lblk_policy()
136 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) { in supported_iv_ino_lblk_policy()
138 "Can't use %s policy with contents mode other than AES-256-XTS", in supported_iv_ino_lblk_policy()
147 if (!sb->s_cop->has_stable_inodes || in supported_iv_ino_lblk_policy()
148 !sb->s_cop->has_stable_inodes(sb)) { in supported_iv_ino_lblk_policy()
151 type, sb->s_id); in supported_iv_ino_lblk_policy()
162 if (!sb->s_cop->has_32bit_inodes) { in supported_iv_ino_lblk_policy()
165 type, sb->s_id); in supported_iv_ino_lblk_policy()
177 type, sb->s_id); in supported_iv_ino_lblk_policy()
186 if (!fscrypt_valid_enc_modes_v1(policy->contents_encryption_mode, in fscrypt_supported_v1_policy()
187 policy->filenames_encryption_mode)) { in fscrypt_supported_v1_policy()
190 policy->contents_encryption_mode, in fscrypt_supported_v1_policy()
191 policy->filenames_encryption_mode); in fscrypt_supported_v1_policy()
195 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | in fscrypt_supported_v1_policy()
198 policy->flags); in fscrypt_supported_v1_policy()
202 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) && in fscrypt_supported_v1_policy()
203 !supported_direct_key_modes(inode, policy->contents_encryption_mode, in fscrypt_supported_v1_policy()
204 policy->filenames_encryption_mode)) in fscrypt_supported_v1_policy()
222 if (!fscrypt_valid_enc_modes_v2(policy->contents_encryption_mode, in fscrypt_supported_v2_policy()
223 policy->filenames_encryption_mode)) { in fscrypt_supported_v2_policy()
226 policy->contents_encryption_mode, in fscrypt_supported_v2_policy()
227 policy->filenames_encryption_mode); in fscrypt_supported_v2_policy()
231 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | in fscrypt_supported_v2_policy()
236 policy->flags); in fscrypt_supported_v2_policy()
240 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY); in fscrypt_supported_v2_policy()
241 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64); in fscrypt_supported_v2_policy()
242 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32); in fscrypt_supported_v2_policy()
245 policy->flags); in fscrypt_supported_v2_policy()
249 if (policy->log2_data_unit_size) { in fscrypt_supported_v2_policy()
250 if (!inode->i_sb->s_cop->supports_subblock_data_units) { in fscrypt_supported_v2_policy()
255 if (policy->log2_data_unit_size > inode->i_blkbits || in fscrypt_supported_v2_policy()
256 policy->log2_data_unit_size < SECTOR_SHIFT /* 9 */) { in fscrypt_supported_v2_policy()
259 policy->log2_data_unit_size); in fscrypt_supported_v2_policy()
262 if (policy->log2_data_unit_size != inode->i_blkbits && in fscrypt_supported_v2_policy()
263 (policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) { in fscrypt_supported_v2_policy()
269 "Sub-block data units not yet supported with IV_INO_LBLK_32"); in fscrypt_supported_v2_policy()
274 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) && in fscrypt_supported_v2_policy()
275 !supported_direct_key_modes(inode, policy->contents_encryption_mode, in fscrypt_supported_v2_policy()
276 policy->filenames_encryption_mode)) in fscrypt_supported_v2_policy()
279 if ((policy->flags & (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 | in fscrypt_supported_v2_policy()
284 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) { in fscrypt_supported_v2_policy()
293 * fscrypt_supported_policy() - check whether an encryption policy is supported
307 switch (policy_u->version) { in fscrypt_supported_policy()
309 return fscrypt_supported_v1_policy(&policy_u->v1, inode); in fscrypt_supported_policy()
311 return fscrypt_supported_v2_policy(&policy_u->v2, inode); in fscrypt_supported_policy()
317 * fscrypt_new_context() - create a new fscrypt_context
333 switch (policy_u->version) { in fscrypt_new_context()
335 const struct fscrypt_policy_v1 *policy = &policy_u->v1; in fscrypt_new_context()
336 struct fscrypt_context_v1 *ctx = &ctx_u->v1; in fscrypt_new_context()
338 ctx->version = FSCRYPT_CONTEXT_V1; in fscrypt_new_context()
339 ctx->contents_encryption_mode = in fscrypt_new_context()
340 policy->contents_encryption_mode; in fscrypt_new_context()
341 ctx->filenames_encryption_mode = in fscrypt_new_context()
342 policy->filenames_encryption_mode; in fscrypt_new_context()
343 ctx->flags = policy->flags; in fscrypt_new_context()
344 memcpy(ctx->master_key_descriptor, in fscrypt_new_context()
345 policy->master_key_descriptor, in fscrypt_new_context()
346 sizeof(ctx->master_key_descriptor)); in fscrypt_new_context()
347 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); in fscrypt_new_context()
351 const struct fscrypt_policy_v2 *policy = &policy_u->v2; in fscrypt_new_context()
352 struct fscrypt_context_v2 *ctx = &ctx_u->v2; in fscrypt_new_context()
354 ctx->version = FSCRYPT_CONTEXT_V2; in fscrypt_new_context()
355 ctx->contents_encryption_mode = in fscrypt_new_context()
356 policy->contents_encryption_mode; in fscrypt_new_context()
357 ctx->filenames_encryption_mode = in fscrypt_new_context()
358 policy->filenames_encryption_mode; in fscrypt_new_context()
359 ctx->flags = policy->flags; in fscrypt_new_context()
360 ctx->log2_data_unit_size = policy->log2_data_unit_size; in fscrypt_new_context()
361 memcpy(ctx->master_key_identifier, in fscrypt_new_context()
362 policy->master_key_identifier, in fscrypt_new_context()
363 sizeof(ctx->master_key_identifier)); in fscrypt_new_context()
364 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); in fscrypt_new_context()
372 * fscrypt_policy_from_context() - convert an fscrypt_context to
380 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
393 return -EINVAL; in fscrypt_policy_from_context()
395 switch (ctx_u->version) { in fscrypt_policy_from_context()
397 const struct fscrypt_context_v1 *ctx = &ctx_u->v1; in fscrypt_policy_from_context()
398 struct fscrypt_policy_v1 *policy = &policy_u->v1; in fscrypt_policy_from_context()
400 policy->version = FSCRYPT_POLICY_V1; in fscrypt_policy_from_context()
401 policy->contents_encryption_mode = in fscrypt_policy_from_context()
402 ctx->contents_encryption_mode; in fscrypt_policy_from_context()
403 policy->filenames_encryption_mode = in fscrypt_policy_from_context()
404 ctx->filenames_encryption_mode; in fscrypt_policy_from_context()
405 policy->flags = ctx->flags; in fscrypt_policy_from_context()
406 memcpy(policy->master_key_descriptor, in fscrypt_policy_from_context()
407 ctx->master_key_descriptor, in fscrypt_policy_from_context()
408 sizeof(policy->master_key_descriptor)); in fscrypt_policy_from_context()
412 const struct fscrypt_context_v2 *ctx = &ctx_u->v2; in fscrypt_policy_from_context()
413 struct fscrypt_policy_v2 *policy = &policy_u->v2; in fscrypt_policy_from_context()
415 policy->version = FSCRYPT_POLICY_V2; in fscrypt_policy_from_context()
416 policy->contents_encryption_mode = in fscrypt_policy_from_context()
417 ctx->contents_encryption_mode; in fscrypt_policy_from_context()
418 policy->filenames_encryption_mode = in fscrypt_policy_from_context()
419 ctx->filenames_encryption_mode; in fscrypt_policy_from_context()
420 policy->flags = ctx->flags; in fscrypt_policy_from_context()
421 policy->log2_data_unit_size = ctx->log2_data_unit_size; in fscrypt_policy_from_context()
422 memcpy(policy->__reserved, ctx->__reserved, in fscrypt_policy_from_context()
423 sizeof(policy->__reserved)); in fscrypt_policy_from_context()
424 memcpy(policy->master_key_identifier, in fscrypt_policy_from_context()
425 ctx->master_key_identifier, in fscrypt_policy_from_context()
426 sizeof(policy->master_key_identifier)); in fscrypt_policy_from_context()
431 return -EINVAL; in fscrypt_policy_from_context()
444 *policy = ci->ci_policy; in fscrypt_get_policy()
449 return -ENODATA; in fscrypt_get_policy()
451 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); in fscrypt_get_policy()
453 return (ret == -ERANGE) ? -EINVAL : ret; in fscrypt_get_policy()
467 return -EINVAL; in set_encryption_policy()
469 switch (policy->version) { in set_encryption_policy()
475 * same encrypted files (even just read-only access). The new in set_encryption_policy()
477 * an improved key derivation function and allows non-root users in set_encryption_policy()
483 current->comm, current->pid); in set_encryption_policy()
486 err = fscrypt_verify_key_added(inode->i_sb, in set_encryption_policy()
487 policy->v2.master_key_identifier); in set_encryption_policy()
490 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) in set_encryption_policy()
491 …LBLK_32 encryption policy. This should only be used if there are certain hardware limitations.\n", in set_encryption_policy()
492 current->comm, current->pid); in set_encryption_policy()
496 return -EINVAL; in set_encryption_policy()
502 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL); in set_encryption_policy()
515 return -EFAULT; in fscrypt_ioctl_set_policy()
519 return -EINVAL; in fscrypt_ioctl_set_policy()
522 * We should just copy the remaining 'size - 1' bytes here, but a in fscrypt_ioctl_set_policy()
525 * it's a compile-time constant. Thus it would think copy_from_user() in fscrypt_ioctl_set_policy()
526 * is passed compile-time constant ULONG_MAX, causing the compile-time in fscrypt_ioctl_set_policy()
528 * when building an i386 kernel with -Os and branch profiling enabled. in fscrypt_ioctl_set_policy()
534 return -EFAULT; in fscrypt_ioctl_set_policy()
538 return -EACCES; in fscrypt_ioctl_set_policy()
547 if (ret == -ENODATA) { in fscrypt_ioctl_set_policy()
548 if (!S_ISDIR(inode->i_mode)) in fscrypt_ioctl_set_policy()
549 ret = -ENOTDIR; in fscrypt_ioctl_set_policy()
551 ret = -ENOENT; in fscrypt_ioctl_set_policy()
552 else if (!inode->i_sb->s_cop->empty_dir(inode)) in fscrypt_ioctl_set_policy()
553 ret = -ENOTEMPTY; in fscrypt_ioctl_set_policy()
556 } else if (ret == -EINVAL || in fscrypt_ioctl_set_policy()
560 ret = -EEXIST; in fscrypt_ioctl_set_policy()
581 return -EINVAL; in fscrypt_ioctl_get_policy()
584 return -EFAULT; in fscrypt_ioctl_get_policy()
609 return -EFAULT; in fscrypt_ioctl_get_policy_ex()
612 return -EOVERFLOW; in fscrypt_ioctl_get_policy_ex()
616 return -EFAULT; in fscrypt_ioctl_get_policy_ex()
628 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); in fscrypt_ioctl_get_nonce()
632 return -EINVAL; in fscrypt_ioctl_get_nonce()
635 return -EFAULT; in fscrypt_ioctl_get_nonce()
641 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
649 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
653 * same encryption policy. The pre-access check is needed to detect potentially
665 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && in fscrypt_has_permitted_context()
666 !S_ISLNK(child->i_mode)) in fscrypt_has_permitted_context()
684 * Performance-wise this is not a big deal because we already don't in fscrypt_has_permitted_context()
707 if (err1 == -EINVAL && err2 == -EINVAL) in fscrypt_has_permitted_context()
730 return &fscrypt_get_inode_info_raw(dir)->ci_policy; in fscrypt_policy_to_inherit()
733 return fscrypt_get_dummy_policy(dir->i_sb); in fscrypt_policy_to_inherit()
737 * fscrypt_context_for_new_inode() - create an encryption context for a new inode
741 * Given an in-core "prepared" (via fscrypt_prepare_new_inode) inode,
756 return -ENOKEY; in fscrypt_context_for_new_inode()
758 return fscrypt_new_context(ctx, &ci->ci_policy, ci->ci_nonce); in fscrypt_context_for_new_inode()
763 * fscrypt_set_context() - Set the fscrypt context of a new inode
765 * @fs_data: private data given by FS and passed to ->set_context()
768 * filesystem transaction. Everything here must be %GFP_NOFS-safe.
770 * Return: 0 on success, -errno on failure
787 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 && in fscrypt_set_context()
788 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) in fscrypt_set_context()
789 fscrypt_hash_inode_number(ci, ci->ci_master_key); in fscrypt_set_context()
791 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data); in fscrypt_set_context()
796 * fscrypt_parse_test_dummy_encryption() - parse the test_dummy_encryption mount option
799 * result from parsing the option. Zero-initialize this. If a policy is
803 * Return: 0 on success; -EINVAL if the argument is invalid; -EEXIST if the
804 * argument conflicts with one already specified; or -ENOMEM.
813 if (param->type == fs_value_is_string && *param->string) in fscrypt_parse_test_dummy_encryption()
814 arg = param->string; in fscrypt_parse_test_dummy_encryption()
818 return -ENOMEM; in fscrypt_parse_test_dummy_encryption()
821 policy->version = FSCRYPT_POLICY_V1; in fscrypt_parse_test_dummy_encryption()
822 policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; in fscrypt_parse_test_dummy_encryption()
823 policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; in fscrypt_parse_test_dummy_encryption()
824 memset(policy->v1.master_key_descriptor, 0x42, in fscrypt_parse_test_dummy_encryption()
827 policy->version = FSCRYPT_POLICY_V2; in fscrypt_parse_test_dummy_encryption()
828 policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; in fscrypt_parse_test_dummy_encryption()
829 policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; in fscrypt_parse_test_dummy_encryption()
831 policy->v2.master_key_identifier); in fscrypt_parse_test_dummy_encryption()
833 err = -EINVAL; in fscrypt_parse_test_dummy_encryption()
837 if (dummy_policy->policy) { in fscrypt_parse_test_dummy_encryption()
838 if (fscrypt_policies_equal(policy, dummy_policy->policy)) in fscrypt_parse_test_dummy_encryption()
841 err = -EEXIST; in fscrypt_parse_test_dummy_encryption()
844 dummy_policy->policy = policy; in fscrypt_parse_test_dummy_encryption()
854 * fscrypt_dummy_policies_equal() - check whether two dummy policies are equal
863 if (!p1->policy && !p2->policy) in fscrypt_dummy_policies_equal()
865 if (!p1->policy || !p2->policy) in fscrypt_dummy_policies_equal()
867 return fscrypt_policies_equal(p1->policy, p2->policy); in fscrypt_dummy_policies_equal()
872 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
889 vers = policy->version; in fscrypt_show_test_dummy_encryption()