1 ext4 crypto: add encryption policy and password salt support
3 From: Michael Halcrow <mhalcrow@google.com>
5 Signed-off-by: Michael Halcrow <mhalcrow@google.com>
6 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
7 Signed-off-by: Ildar Muslukhov <muslukhovi@gmail.com>
10 fs/ext4/Makefile | 1 +
11 fs/ext4/crypto_policy.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 fs/ext4/ext4.h | 15 +++++++++++++
13 fs/ext4/ext4_crypto.h | 49 +++++++++++++++++++++++++++++++++++++++++
14 fs/ext4/ioctl.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15 5 files changed, 317 insertions(+)
17 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
18 index cd6f50f..3886ee4 100644
19 --- a/fs/ext4/Makefile
20 +++ b/fs/ext4/Makefile
21 @@ -12,3 +12,4 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
23 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
24 ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
25 +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o
26 diff --git a/fs/ext4/crypto_policy.c b/fs/ext4/crypto_policy.c
28 index 0000000..532b69c
30 +++ b/fs/ext4/crypto_policy.c
33 + * linux/fs/ext4/crypto_policy.c
35 + * Copyright (C) 2015, Google, Inc.
37 + * This contains encryption policy functions for ext4
39 + * Written by Michael Halcrow, 2015.
42 +#include <linux/random.h>
43 +#include <linux/string.h>
44 +#include <linux/types.h>
49 +static int ext4_inode_has_encryption_context(struct inode *inode)
51 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
52 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
57 + * check whether the policy is consistent with the encryption context
60 +static int ext4_is_encryption_context_consistent_with_policy(
61 + struct inode *inode, const struct ext4_encryption_policy *policy)
63 + struct ext4_encryption_context ctx;
64 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
65 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
67 + if (res != sizeof(ctx))
69 + return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
70 + EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
71 + (ctx.contents_encryption_mode ==
72 + policy->contents_encryption_mode) &&
73 + (ctx.filenames_encryption_mode ==
74 + policy->filenames_encryption_mode));
77 +static int ext4_create_encryption_context_from_policy(
78 + struct inode *inode, const struct ext4_encryption_policy *policy)
80 + struct ext4_encryption_context ctx;
83 + ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
84 + memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
85 + EXT4_KEY_DESCRIPTOR_SIZE);
86 + ctx.contents_encryption_mode = policy->contents_encryption_mode;
87 + ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
88 + BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
89 + get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
91 + res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
92 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
95 + ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
99 +int ext4_process_policy(const struct ext4_encryption_policy *policy,
100 + struct inode *inode)
102 + if (policy->version != 0)
105 + if (!ext4_inode_has_encryption_context(inode)) {
106 + if (!ext4_empty_dir(inode))
108 + return ext4_create_encryption_context_from_policy(inode,
112 + if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
115 + printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
120 +int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
122 + struct ext4_encryption_context ctx;
124 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
125 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
126 + &ctx, sizeof(ctx));
127 + if (res != sizeof(ctx))
129 + if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
131 + policy->version = 0;
132 + policy->contents_encryption_mode = ctx.contents_encryption_mode;
133 + policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
134 + memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
135 + EXT4_KEY_DESCRIPTOR_SIZE);
139 +int ext4_is_child_context_consistent_with_parent(struct inode *parent,
140 + struct inode *child)
142 + struct ext4_encryption_context parent_ctx, child_ctx;
145 + if ((parent == NULL) || (child == NULL)) {
146 + pr_err("parent %p child %p\n", parent, child);
149 + /* no restrictions if the parent directory is not encrypted */
150 + if (!ext4_encrypted_inode(parent))
152 + res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
153 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
154 + &parent_ctx, sizeof(parent_ctx));
155 + if (res != sizeof(parent_ctx))
157 + /* if the child directory is not encrypted, this is always a problem */
158 + if (!ext4_encrypted_inode(child))
160 + res = ext4_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION,
161 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
162 + &child_ctx, sizeof(child_ctx));
163 + if (res != sizeof(child_ctx))
165 + return (memcmp(parent_ctx.master_key_descriptor,
166 + child_ctx.master_key_descriptor,
167 + EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
168 + (parent_ctx.contents_encryption_mode ==
169 + child_ctx.contents_encryption_mode) &&
170 + (parent_ctx.filenames_encryption_mode ==
171 + child_ctx.filenames_encryption_mode));
175 + * ext4_inherit_context() - Sets a child context from its parent
176 + * @parent: Parent inode from which the context is inherited.
177 + * @child: Child inode that inherits the context from @parent.
179 + * Return: Zero on success, non-zero otherwise
181 +int ext4_inherit_context(struct inode *parent, struct inode *child)
183 + struct ext4_encryption_context ctx;
184 + int res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
185 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
186 + &ctx, sizeof(ctx));
188 + if (res != sizeof(ctx))
191 + get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
192 + res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
193 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
196 + ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
199 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
200 index 41bb58a..e0956b7 100644
203 @@ -589,6 +589,8 @@ enum {
204 #define EXT4_ENCRYPTION_MODE_AES_256_CBC 3
205 #define EXT4_ENCRYPTION_MODE_AES_256_CTS 4
207 +#include "ext4_crypto.h"
212 @@ -610,6 +612,9 @@ enum {
213 #define EXT4_IOC_RESIZE_FS _IOW('f', 16, __u64)
214 #define EXT4_IOC_SWAP_BOOT _IO('f', 17)
215 #define EXT4_IOC_PRECACHE_EXTENTS _IO('f', 18)
216 +#define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy)
217 +#define EXT4_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16])
218 +#define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)
220 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
222 @@ -1999,6 +2004,16 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
223 struct ext4_group_desc *gdp);
224 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
226 +/* crypto_policy.c */
227 +int ext4_is_child_context_consistent_with_parent(struct inode *parent,
228 + struct inode *child);
229 +int ext4_inherit_context(struct inode *parent, struct inode *child);
230 +void ext4_to_hex(char *dst, char *src, size_t src_size);
231 +int ext4_process_policy(const struct ext4_encryption_policy *policy,
232 + struct inode *inode);
233 +int ext4_get_policy(struct inode *inode,
234 + struct ext4_encryption_policy *policy);
237 extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
239 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
241 index 0000000..a69d2ba
243 +++ b/fs/ext4/ext4_crypto.h
246 + * linux/fs/ext4/ext4_crypto.h
248 + * Copyright (C) 2015, Google, Inc.
250 + * This contains encryption header content for ext4
252 + * Written by Michael Halcrow, 2015.
255 +#ifndef _EXT4_CRYPTO_H
256 +#define _EXT4_CRYPTO_H
258 +#include <linux/fs.h>
260 +#define EXT4_KEY_DESCRIPTOR_SIZE 8
262 +/* Policy provided via an ioctl on the topmost directory */
263 +struct ext4_encryption_policy {
265 + char contents_encryption_mode;
266 + char filenames_encryption_mode;
267 + char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
268 +} __attribute__((__packed__));
270 +#define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
271 +#define EXT4_KEY_DERIVATION_NONCE_SIZE 16
274 + * Encryption context for inode
276 + * Protector format:
277 + * 1 byte: Protector format (1 = this version)
278 + * 1 byte: File contents encryption mode
279 + * 1 byte: File names encryption mode
281 + * 8 bytes: Master Key descriptor
282 + * 16 bytes: Encryption Key derivation nonce
284 +struct ext4_encryption_context {
286 + char contents_encryption_mode;
287 + char filenames_encryption_mode;
289 + char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
290 + char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
291 +} __attribute__((__packed__));
293 +#endif /* _EXT4_CRYPTO_H */
294 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
295 index f58a0d1..9d2c2c6 100644
296 --- a/fs/ext4/ioctl.c
297 +++ b/fs/ext4/ioctl.c
299 #include <linux/compat.h>
300 #include <linux/mount.h>
301 #include <linux/file.h>
302 +#include <linux/random.h>
303 #include <asm/uaccess.h>
304 #include "ext4_jbd2.h"
306 @@ -196,6 +197,16 @@ journal_err_out:
310 +static int uuid_is_zero(__u8 u[16])
314 + for (i = 0; i < 16; i++)
320 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
322 struct inode *inode = file_inode(filp);
323 @@ -615,7 +626,78 @@ resizefs_out:
325 case EXT4_IOC_PRECACHE_EXTENTS:
326 return ext4_ext_precache(inode);
327 + case EXT4_IOC_SET_ENCRYPTION_POLICY: {
328 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
329 + struct ext4_encryption_policy policy;
332 + if (copy_from_user(&policy,
333 + (struct ext4_encryption_policy __user *)arg,
336 + goto encryption_policy_out;
339 + err = ext4_process_policy(&policy, inode);
340 +encryption_policy_out:
343 + return -EOPNOTSUPP;
346 + case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
348 + struct ext4_sb_info *sbi = EXT4_SB(sb);
351 + if (!ext4_sb_has_crypto(sb))
352 + return -EOPNOTSUPP;
353 + if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
354 + err = mnt_want_write_file(filp);
357 + handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
358 + if (IS_ERR(handle)) {
359 + err = PTR_ERR(handle);
360 + goto pwsalt_err_exit;
362 + err = ext4_journal_get_write_access(handle, sbi->s_sbh);
364 + goto pwsalt_err_journal;
365 + generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
366 + err = ext4_handle_dirty_metadata(handle, NULL,
368 + pwsalt_err_journal:
369 + err2 = ext4_journal_stop(handle);
373 + mnt_drop_write_file(filp);
377 + if (copy_to_user((void *) arg, sbi->s_es->s_encrypt_pw_salt,
382 + case EXT4_IOC_GET_ENCRYPTION_POLICY: {
383 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
384 + struct ext4_encryption_policy policy;
387 + if (!ext4_encrypted_inode(inode))
389 + err = ext4_get_policy(inode, &policy);
392 + if (copy_to_user((void *)arg, &policy, sizeof(policy)))
396 + return -EOPNOTSUPP;
402 @@ -680,6 +762,9 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
404 case EXT4_IOC_RESIZE_FS:
405 case EXT4_IOC_PRECACHE_EXTENTS:
406 + case EXT4_IOC_SET_ENCRYPTION_POLICY:
407 + case EXT4_IOC_GET_ENCRYPTION_PWSALT:
408 + case EXT4_IOC_GET_ENCRYPTION_POLICY: