2 * Encryption policy functions for per-file encryption support.
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility.
7 * Written by Michael Halcrow, 2015.
8 * Modified by Jaegeuk Kim, 2015.
11 #include <linux/random.h>
12 #include <linux/string.h>
13 #include <linux/fscrypto.h>
14 #include <linux/mount.h>
16 static int inode_has_encryption_context(struct inode *inode)
18 if (!inode->i_sb->s_cop->get_context)
20 return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0);
24 * check whether the policy is consistent with the encryption context
27 static int is_encryption_context_consistent_with_policy(struct inode *inode,
28 const struct fscrypt_policy *policy)
30 struct fscrypt_context ctx;
33 if (!inode->i_sb->s_cop->get_context)
36 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
37 if (res != sizeof(ctx))
40 return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
41 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
42 (ctx.flags == policy->flags) &&
43 (ctx.contents_encryption_mode ==
44 policy->contents_encryption_mode) &&
45 (ctx.filenames_encryption_mode ==
46 policy->filenames_encryption_mode));
49 static int create_encryption_context_from_policy(struct inode *inode,
50 const struct fscrypt_policy *policy)
52 struct fscrypt_context ctx;
55 if (!inode->i_sb->s_cop->set_context)
58 if (inode->i_sb->s_cop->prepare_context) {
59 res = inode->i_sb->s_cop->prepare_context(inode);
64 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
65 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
66 FS_KEY_DESCRIPTOR_SIZE);
68 if (!fscrypt_valid_contents_enc_mode(
69 policy->contents_encryption_mode)) {
71 "%s: Invalid contents encryption mode %d\n", __func__,
72 policy->contents_encryption_mode);
76 if (!fscrypt_valid_filenames_enc_mode(
77 policy->filenames_encryption_mode)) {
79 "%s: Invalid filenames encryption mode %d\n", __func__,
80 policy->filenames_encryption_mode);
84 if (policy->flags & ~FS_POLICY_FLAGS_VALID)
87 ctx.contents_encryption_mode = policy->contents_encryption_mode;
88 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
89 ctx.flags = policy->flags;
90 BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
91 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
93 return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
96 int fscrypt_process_policy(struct file *filp,
97 const struct fscrypt_policy *policy)
99 struct inode *inode = file_inode(filp);
102 if (!inode_owner_or_capable(inode))
105 if (policy->version != 0)
108 ret = mnt_want_write_file(filp);
114 if (!inode_has_encryption_context(inode)) {
115 if (!S_ISDIR(inode->i_mode))
117 else if (!inode->i_sb->s_cop->empty_dir)
119 else if (!inode->i_sb->s_cop->empty_dir(inode))
122 ret = create_encryption_context_from_policy(inode,
124 } else if (!is_encryption_context_consistent_with_policy(inode,
127 "%s: Policy inconsistent with encryption context\n",
134 mnt_drop_write_file(filp);
137 EXPORT_SYMBOL(fscrypt_process_policy);
139 int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy)
141 struct fscrypt_context ctx;
144 if (!inode->i_sb->s_cop->get_context ||
145 !inode->i_sb->s_cop->is_encrypted(inode))
148 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
149 if (res != sizeof(ctx))
151 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
155 policy->contents_encryption_mode = ctx.contents_encryption_mode;
156 policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
157 policy->flags = ctx.flags;
158 memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
159 FS_KEY_DESCRIPTOR_SIZE);
162 EXPORT_SYMBOL(fscrypt_get_policy);
164 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
166 struct fscrypt_info *parent_ci, *child_ci;
169 if ((parent == NULL) || (child == NULL)) {
170 printk(KERN_ERR "parent %p child %p\n", parent, child);
174 /* no restrictions if the parent directory is not encrypted */
175 if (!parent->i_sb->s_cop->is_encrypted(parent))
177 /* if the child directory is not encrypted, this is always a problem */
178 if (!parent->i_sb->s_cop->is_encrypted(child))
180 res = fscrypt_get_encryption_info(parent);
183 res = fscrypt_get_encryption_info(child);
186 parent_ci = parent->i_crypt_info;
187 child_ci = child->i_crypt_info;
188 if (!parent_ci && !child_ci)
190 if (!parent_ci || !child_ci)
193 return (memcmp(parent_ci->ci_master_key,
194 child_ci->ci_master_key,
195 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
196 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
197 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
198 (parent_ci->ci_flags == child_ci->ci_flags));
200 EXPORT_SYMBOL(fscrypt_has_permitted_context);
203 * fscrypt_inherit_context() - Sets a child context from its parent
204 * @parent: Parent inode from which the context is inherited.
205 * @child: Child inode that inherits the context from @parent.
206 * @fs_data: private data given by FS.
207 * @preload: preload child i_crypt_info
209 * Return: Zero on success, non-zero otherwise
211 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
212 void *fs_data, bool preload)
214 struct fscrypt_context ctx;
215 struct fscrypt_info *ci;
218 if (!parent->i_sb->s_cop->set_context)
221 res = fscrypt_get_encryption_info(parent);
225 ci = parent->i_crypt_info;
229 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
230 if (fscrypt_dummy_context_enabled(parent)) {
231 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
232 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
234 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
237 ctx.contents_encryption_mode = ci->ci_data_mode;
238 ctx.filenames_encryption_mode = ci->ci_filename_mode;
239 ctx.flags = ci->ci_flags;
240 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
241 FS_KEY_DESCRIPTOR_SIZE);
243 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
244 res = parent->i_sb->s_cop->set_context(child, &ctx,
245 sizeof(ctx), fs_data);
248 return preload ? fscrypt_get_encryption_info(child): 0;
250 EXPORT_SYMBOL(fscrypt_inherit_context);