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>
15 static int inode_has_encryption_context(struct inode *inode)
17 if (!inode->i_sb->s_cop->get_context)
19 return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0);
23 * check whether the policy is consistent with the encryption context
26 static int is_encryption_context_consistent_with_policy(struct inode *inode,
27 const struct fscrypt_policy *policy)
29 struct fscrypt_context ctx;
32 if (!inode->i_sb->s_cop->get_context)
35 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
36 if (res != sizeof(ctx))
39 return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
40 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
41 (ctx.flags == policy->flags) &&
42 (ctx.contents_encryption_mode ==
43 policy->contents_encryption_mode) &&
44 (ctx.filenames_encryption_mode ==
45 policy->filenames_encryption_mode));
48 static int create_encryption_context_from_policy(struct inode *inode,
49 const struct fscrypt_policy *policy)
51 struct fscrypt_context ctx;
54 if (!inode->i_sb->s_cop->set_context)
57 if (inode->i_sb->s_cop->prepare_context) {
58 res = inode->i_sb->s_cop->prepare_context(inode);
63 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
64 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
65 FS_KEY_DESCRIPTOR_SIZE);
67 if (!fscrypt_valid_contents_enc_mode(
68 policy->contents_encryption_mode)) {
70 "%s: Invalid contents encryption mode %d\n", __func__,
71 policy->contents_encryption_mode);
75 if (!fscrypt_valid_filenames_enc_mode(
76 policy->filenames_encryption_mode)) {
78 "%s: Invalid filenames encryption mode %d\n", __func__,
79 policy->filenames_encryption_mode);
83 if (policy->flags & ~FS_POLICY_FLAGS_VALID)
86 ctx.contents_encryption_mode = policy->contents_encryption_mode;
87 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
88 ctx.flags = policy->flags;
89 BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
90 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
92 return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
95 int fscrypt_process_policy(struct inode *inode,
96 const struct fscrypt_policy *policy)
98 if (!inode_owner_or_capable(inode))
101 if (policy->version != 0)
104 if (!inode_has_encryption_context(inode)) {
105 if (!S_ISDIR(inode->i_mode))
107 if (!inode->i_sb->s_cop->empty_dir)
109 if (!inode->i_sb->s_cop->empty_dir(inode))
111 return create_encryption_context_from_policy(inode, policy);
114 if (is_encryption_context_consistent_with_policy(inode, policy))
117 printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
121 EXPORT_SYMBOL(fscrypt_process_policy);
123 int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy)
125 struct fscrypt_context ctx;
128 if (!inode->i_sb->s_cop->get_context ||
129 !inode->i_sb->s_cop->is_encrypted(inode))
132 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
133 if (res != sizeof(ctx))
135 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
139 policy->contents_encryption_mode = ctx.contents_encryption_mode;
140 policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
141 policy->flags = ctx.flags;
142 memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
143 FS_KEY_DESCRIPTOR_SIZE);
146 EXPORT_SYMBOL(fscrypt_get_policy);
148 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
150 struct fscrypt_info *parent_ci, *child_ci;
153 if ((parent == NULL) || (child == NULL)) {
154 printk(KERN_ERR "parent %p child %p\n", parent, child);
158 /* no restrictions if the parent directory is not encrypted */
159 if (!parent->i_sb->s_cop->is_encrypted(parent))
161 /* if the child directory is not encrypted, this is always a problem */
162 if (!parent->i_sb->s_cop->is_encrypted(child))
164 res = fscrypt_get_encryption_info(parent);
167 res = fscrypt_get_encryption_info(child);
170 parent_ci = parent->i_crypt_info;
171 child_ci = child->i_crypt_info;
172 if (!parent_ci && !child_ci)
174 if (!parent_ci || !child_ci)
177 return (memcmp(parent_ci->ci_master_key,
178 child_ci->ci_master_key,
179 FS_KEY_DESCRIPTOR_SIZE) == 0 &&
180 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
181 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
182 (parent_ci->ci_flags == child_ci->ci_flags));
184 EXPORT_SYMBOL(fscrypt_has_permitted_context);
187 * fscrypt_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.
190 * @fs_data: private data given by FS.
191 * @preload: preload child i_crypt_info
193 * Return: Zero on success, non-zero otherwise
195 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
196 void *fs_data, bool preload)
198 struct fscrypt_context ctx;
199 struct fscrypt_info *ci;
202 if (!parent->i_sb->s_cop->set_context)
205 res = fscrypt_get_encryption_info(parent);
209 ci = parent->i_crypt_info;
213 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
214 if (fscrypt_dummy_context_enabled(parent)) {
215 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
216 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
218 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
221 ctx.contents_encryption_mode = ci->ci_data_mode;
222 ctx.filenames_encryption_mode = ci->ci_filename_mode;
223 ctx.flags = ci->ci_flags;
224 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
225 FS_KEY_DESCRIPTOR_SIZE);
227 get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
228 res = parent->i_sb->s_cop->set_context(child, &ctx,
229 sizeof(ctx), fs_data);
232 return preload ? fscrypt_get_encryption_info(child): 0;
234 EXPORT_SYMBOL(fscrypt_inherit_context);