1 ext4 crypto: add encryption policy and password salt support
3 From: Michael Halcrow <mhalcrow@google.com>
5 Change-Id: I550d197184af04ed27e4c3abb759ca188a3f0de0
6 Signed-off-by: Michael Halcrow <mhalcrow@google.com>
7 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
8 Signed-off-by: Ildar Muslukhov <muslukhovi@gmail.com>
11 fs/ext4/Makefile | 1 +
12 fs/ext4/crypto_policy.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13 fs/ext4/ext4.h | 15 +++++++++++++
14 fs/ext4/ext4_crypto.h | 49 +++++++++++++++++++++++++++++++++++++++++
15 fs/ext4/ioctl.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16 5 files changed, 317 insertions(+)
18 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
19 index cd6f50f..3886ee4 100644
20 --- a/fs/ext4/Makefile
21 +++ b/fs/ext4/Makefile
22 @@ -12,3 +12,4 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
24 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
25 ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
26 +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o
27 diff --git a/fs/ext4/crypto_policy.c b/fs/ext4/crypto_policy.c
29 index 0000000..532b69c
31 +++ b/fs/ext4/crypto_policy.c
34 + * linux/fs/ext4/crypto_policy.c
36 + * Copyright (C) 2015, Google, Inc.
38 + * This contains encryption policy functions for ext4
40 + * Written by Michael Halcrow, 2015.
43 +#include <linux/random.h>
44 +#include <linux/string.h>
45 +#include <linux/types.h>
50 +static int ext4_inode_has_encryption_context(struct inode *inode)
52 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
53 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
58 + * check whether the policy is consistent with the encryption context
61 +static int ext4_is_encryption_context_consistent_with_policy(
62 + struct inode *inode, const struct ext4_encryption_policy *policy)
64 + struct ext4_encryption_context ctx;
65 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
66 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
68 + if (res != sizeof(ctx))
70 + return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
71 + EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
72 + (ctx.contents_encryption_mode ==
73 + policy->contents_encryption_mode) &&
74 + (ctx.filenames_encryption_mode ==
75 + policy->filenames_encryption_mode));
78 +static int ext4_create_encryption_context_from_policy(
79 + struct inode *inode, const struct ext4_encryption_policy *policy)
81 + struct ext4_encryption_context ctx;
84 + ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
85 + memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
86 + EXT4_KEY_DESCRIPTOR_SIZE);
87 + ctx.contents_encryption_mode = policy->contents_encryption_mode;
88 + ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
89 + BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
90 + get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
92 + res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
93 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
96 + ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
100 +int ext4_process_policy(const struct ext4_encryption_policy *policy,
101 + struct inode *inode)
103 + if (policy->version != 0)
106 + if (!ext4_inode_has_encryption_context(inode)) {
107 + if (!ext4_empty_dir(inode))
109 + return ext4_create_encryption_context_from_policy(inode,
113 + if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
116 + printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
121 +int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
123 + struct ext4_encryption_context ctx;
125 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
126 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
127 + &ctx, sizeof(ctx));
128 + if (res != sizeof(ctx))
130 + if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
132 + policy->version = 0;
133 + policy->contents_encryption_mode = ctx.contents_encryption_mode;
134 + policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
135 + memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
136 + EXT4_KEY_DESCRIPTOR_SIZE);
140 +int ext4_is_child_context_consistent_with_parent(struct inode *parent,
141 + struct inode *child)
143 + struct ext4_encryption_context parent_ctx, child_ctx;
146 + if ((parent == NULL) || (child == NULL)) {
147 + pr_err("parent %p child %p\n", parent, child);
150 + /* no restrictions if the parent directory is not encrypted */
151 + if (!ext4_encrypted_inode(parent))
153 + res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
154 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
155 + &parent_ctx, sizeof(parent_ctx));
156 + if (res != sizeof(parent_ctx))
158 + /* if the child directory is not encrypted, this is always a problem */
159 + if (!ext4_encrypted_inode(child))
161 + res = ext4_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION,
162 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
163 + &child_ctx, sizeof(child_ctx));
164 + if (res != sizeof(child_ctx))
166 + return (memcmp(parent_ctx.master_key_descriptor,
167 + child_ctx.master_key_descriptor,
168 + EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
169 + (parent_ctx.contents_encryption_mode ==
170 + child_ctx.contents_encryption_mode) &&
171 + (parent_ctx.filenames_encryption_mode ==
172 + child_ctx.filenames_encryption_mode));
176 + * ext4_inherit_context() - Sets a child context from its parent
177 + * @parent: Parent inode from which the context is inherited.
178 + * @child: Child inode that inherits the context from @parent.
180 + * Return: Zero on success, non-zero otherwise
182 +int ext4_inherit_context(struct inode *parent, struct inode *child)
184 + struct ext4_encryption_context ctx;
185 + int res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
186 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
187 + &ctx, sizeof(ctx));
189 + if (res != sizeof(ctx))
192 + get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
193 + res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
194 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
197 + ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
200 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
201 index 41bb58a..e0956b7 100644
204 @@ -589,6 +589,8 @@ enum {
205 #define EXT4_ENCRYPTION_MODE_AES_256_CBC 3
206 #define EXT4_ENCRYPTION_MODE_AES_256_CTS 4
208 +#include "ext4_crypto.h"
213 @@ -610,6 +612,9 @@ enum {
214 #define EXT4_IOC_RESIZE_FS _IOW('f', 16, __u64)
215 #define EXT4_IOC_SWAP_BOOT _IO('f', 17)
216 #define EXT4_IOC_PRECACHE_EXTENTS _IO('f', 18)
217 +#define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy)
218 +#define EXT4_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16])
219 +#define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)
221 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
223 @@ -1999,6 +2004,16 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
224 struct ext4_group_desc *gdp);
225 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
227 +/* crypto_policy.c */
228 +int ext4_is_child_context_consistent_with_parent(struct inode *parent,
229 + struct inode *child);
230 +int ext4_inherit_context(struct inode *parent, struct inode *child);
231 +void ext4_to_hex(char *dst, char *src, size_t src_size);
232 +int ext4_process_policy(const struct ext4_encryption_policy *policy,
233 + struct inode *inode);
234 +int ext4_get_policy(struct inode *inode,
235 + struct ext4_encryption_policy *policy);
238 extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
240 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
242 index 0000000..a69d2ba
244 +++ b/fs/ext4/ext4_crypto.h
247 + * linux/fs/ext4/ext4_crypto.h
249 + * Copyright (C) 2015, Google, Inc.
251 + * This contains encryption header content for ext4
253 + * Written by Michael Halcrow, 2015.
256 +#ifndef _EXT4_CRYPTO_H
257 +#define _EXT4_CRYPTO_H
259 +#include <linux/fs.h>
261 +#define EXT4_KEY_DESCRIPTOR_SIZE 8
263 +/* Policy provided via an ioctl on the topmost directory */
264 +struct ext4_encryption_policy {
266 + char contents_encryption_mode;
267 + char filenames_encryption_mode;
268 + char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
269 +} __attribute__((__packed__));
271 +#define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
272 +#define EXT4_KEY_DERIVATION_NONCE_SIZE 16
275 + * Encryption context for inode
277 + * Protector format:
278 + * 1 byte: Protector format (1 = this version)
279 + * 1 byte: File contents encryption mode
280 + * 1 byte: File names encryption mode
282 + * 8 bytes: Master Key descriptor
283 + * 16 bytes: Encryption Key derivation nonce
285 +struct ext4_encryption_context {
287 + char contents_encryption_mode;
288 + char filenames_encryption_mode;
290 + char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
291 + char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
292 +} __attribute__((__packed__));
294 +#endif /* _EXT4_CRYPTO_H */
295 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
296 index f58a0d1..9d2c2c6 100644
297 --- a/fs/ext4/ioctl.c
298 +++ b/fs/ext4/ioctl.c
300 #include <linux/compat.h>
301 #include <linux/mount.h>
302 #include <linux/file.h>
303 +#include <linux/random.h>
304 #include <asm/uaccess.h>
305 #include "ext4_jbd2.h"
307 @@ -196,6 +197,16 @@ journal_err_out:
311 +static int uuid_is_zero(__u8 u[16])
315 + for (i=0; i < 16; i++)
321 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
323 struct inode *inode = file_inode(filp);
324 @@ -615,7 +626,78 @@ resizefs_out:
326 case EXT4_IOC_PRECACHE_EXTENTS:
327 return ext4_ext_precache(inode);
328 + case EXT4_IOC_SET_ENCRYPTION_POLICY: {
329 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
330 + struct ext4_encryption_policy policy;
333 + if (copy_from_user(&policy,
334 + (struct ext4_encryption_policy __user *)arg,
337 + goto encryption_policy_out;
340 + err = ext4_process_policy(&policy, inode);
341 +encryption_policy_out:
344 + return -EOPNOTSUPP;
347 + case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
349 + struct ext4_sb_info *sbi = EXT4_SB(sb);
352 + if (!ext4_sb_has_crypto(sb))
353 + return -EOPNOTSUPP;
354 + if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
355 + err = mnt_want_write_file(filp);
358 + handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
359 + if (IS_ERR(handle)) {
360 + err = PTR_ERR(handle);
361 + goto pwsalt_err_exit;
363 + err = ext4_journal_get_write_access(handle, sbi->s_sbh);
365 + goto pwsalt_err_journal;
366 + generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
367 + err = ext4_handle_dirty_metadata(handle, NULL,
369 + pwsalt_err_journal:
370 + err2 = ext4_journal_stop(handle);
374 + mnt_drop_write_file(filp);
378 + if (copy_to_user((void *) arg, sbi->s_es->s_encrypt_pw_salt,
383 + case EXT4_IOC_GET_ENCRYPTION_POLICY: {
384 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
385 + struct ext4_encryption_policy policy;
388 + if (!ext4_encrypted_inode(inode))
390 + err = ext4_get_policy(inode, &policy);
393 + if (copy_to_user((void *)arg, &policy, sizeof(policy)))
397 + return -EOPNOTSUPP;
403 @@ -680,6 +762,9 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
405 case EXT4_IOC_RESIZE_FS:
406 case EXT4_IOC_PRECACHE_EXTENTS:
407 + case EXT4_IOC_SET_ENCRYPTION_POLICY:
408 + case EXT4_IOC_GET_ENCRYPTION_PWSALT:
409 + case EXT4_IOC_GET_ENCRYPTION_POLICY: