2 * linux/fs/ext4/crypto_policy.c
4 * Copyright (C) 2015, Google, Inc.
6 * This contains encryption policy functions for ext4
8 * Written by Michael Halcrow, 2015.
11 #include <linux/random.h>
12 #include <linux/string.h>
13 #include <linux/types.h>
15 #include "ext4_jbd2.h"
19 static int ext4_inode_has_encryption_context(struct inode *inode)
21 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
22 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
27 * check whether the policy is consistent with the encryption context
30 static int ext4_is_encryption_context_consistent_with_policy(
31 struct inode *inode, const struct ext4_encryption_policy *policy)
33 struct ext4_encryption_context ctx;
34 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
35 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
37 if (res != sizeof(ctx))
39 return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
40 EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
43 (ctx.contents_encryption_mode ==
44 policy->contents_encryption_mode) &&
45 (ctx.filenames_encryption_mode ==
46 policy->filenames_encryption_mode));
49 static int ext4_create_encryption_context_from_policy(
50 struct inode *inode, const struct ext4_encryption_policy *policy)
52 struct ext4_encryption_context ctx;
56 res = ext4_convert_inline_data(inode);
60 ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
61 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
62 EXT4_KEY_DESCRIPTOR_SIZE);
63 if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) {
65 "%s: Invalid contents encryption mode %d\n", __func__,
66 policy->contents_encryption_mode);
69 if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
71 "%s: Invalid filenames encryption mode %d\n", __func__,
72 policy->filenames_encryption_mode);
75 if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
77 ctx.contents_encryption_mode = policy->contents_encryption_mode;
78 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
79 ctx.flags = policy->flags;
80 BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
81 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
83 handle = ext4_journal_start(inode, EXT4_HT_MISC,
84 ext4_jbd2_credits_xattr(inode));
86 return PTR_ERR(handle);
87 res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
88 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
91 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
92 res = ext4_mark_inode_dirty(handle, inode);
94 EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
96 res2 = ext4_journal_stop(handle);
102 int ext4_process_policy(const struct ext4_encryption_policy *policy,
105 if (!inode_owner_or_capable(inode))
108 if (policy->version != 0)
111 if (!ext4_inode_has_encryption_context(inode)) {
112 if (!S_ISDIR(inode->i_mode))
114 if (!ext4_empty_dir(inode))
116 return ext4_create_encryption_context_from_policy(inode,
120 if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
123 printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
128 int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
130 struct ext4_encryption_context ctx;
132 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
133 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
135 if (res != sizeof(ctx))
137 if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
140 policy->contents_encryption_mode = ctx.contents_encryption_mode;
141 policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
142 policy->flags = ctx.flags;
143 memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
144 EXT4_KEY_DESCRIPTOR_SIZE);
148 int ext4_is_child_context_consistent_with_parent(struct inode *parent,
151 struct ext4_crypt_info *parent_ci, *child_ci;
154 if ((parent == NULL) || (child == NULL)) {
155 pr_err("parent %p child %p\n", parent, child);
156 WARN_ON(1); /* Should never happen */
159 /* no restrictions if the parent directory is not encrypted */
160 if (!ext4_encrypted_inode(parent))
162 /* if the child directory is not encrypted, this is always a problem */
163 if (!ext4_encrypted_inode(child))
165 res = ext4_get_encryption_info(parent);
168 res = ext4_get_encryption_info(child);
171 parent_ci = EXT4_I(parent)->i_crypt_info;
172 child_ci = EXT4_I(child)->i_crypt_info;
173 if (!parent_ci && !child_ci)
175 if (!parent_ci || !child_ci)
178 return (memcmp(parent_ci->ci_master_key,
179 child_ci->ci_master_key,
180 EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
181 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
182 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
183 (parent_ci->ci_flags == child_ci->ci_flags));
187 * ext4_inherit_context() - Sets a child context from its parent
188 * @parent: Parent inode from which the context is inherited.
189 * @child: Child inode that inherits the context from @parent.
191 * Return: Zero on success, non-zero otherwise
193 int ext4_inherit_context(struct inode *parent, struct inode *child)
195 struct ext4_encryption_context ctx;
196 struct ext4_crypt_info *ci;
199 res = ext4_get_encryption_info(parent);
202 ci = EXT4_I(parent)->i_crypt_info;
206 ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
207 if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
208 ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
209 ctx.filenames_encryption_mode =
210 EXT4_ENCRYPTION_MODE_AES_256_CTS;
212 memset(ctx.master_key_descriptor, 0x42,
213 EXT4_KEY_DESCRIPTOR_SIZE);
216 ctx.contents_encryption_mode = ci->ci_data_mode;
217 ctx.filenames_encryption_mode = ci->ci_filename_mode;
218 ctx.flags = ci->ci_flags;
219 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
220 EXT4_KEY_DESCRIPTOR_SIZE);
222 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
223 res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
224 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
227 ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
228 ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA);
229 res = ext4_get_encryption_info(child);