Sync up with encryption patches
[ext4-patch-queue.git] / encryption-policy-and-context
blobf4aaf6e2f0cb1b6b6dfb4ff2760e2e9e3decec1c
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>
10 ---
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
28 new file mode 100644
29 index 0000000..532b69c
30 --- /dev/null
31 +++ b/fs/ext4/crypto_policy.c
32 @@ -0,0 +1,167 @@
33 +/*
34 + * linux/fs/ext4/crypto_policy.c
35 + *
36 + * Copyright (C) 2015, Google, Inc.
37 + *
38 + * This contains encryption policy functions for ext4
39 + *
40 + * Written by Michael Halcrow, 2015.
41 + */
43 +#include <linux/random.h>
44 +#include <linux/string.h>
45 +#include <linux/types.h>
47 +#include "ext4.h"
48 +#include "xattr.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);
54 +       return (res > 0);
57 +/*
58 + * check whether the policy is consistent with the encryption context
59 + * for the inode
60 + */
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,
67 +                                sizeof(ctx));
68 +       if (res != sizeof(ctx))
69 +               return 0;
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;
82 +       int res = 0;
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,
94 +                            sizeof(ctx), 0);
95 +       if (!res)
96 +               ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
97 +       return res;
100 +int ext4_process_policy(const struct ext4_encryption_policy *policy,
101 +                       struct inode *inode)
103 +       if (policy->version != 0)
104 +               return -EINVAL;
106 +       if (!ext4_inode_has_encryption_context(inode)) {
107 +               if (!ext4_empty_dir(inode))
108 +                       return -ENOTEMPTY;
109 +               return ext4_create_encryption_context_from_policy(inode,
110 +                                                                 policy);
111 +       }
113 +       if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
114 +               return 0;
116 +       printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
117 +              __func__);
118 +       return -EINVAL;
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))
129 +               return -ENOENT;
130 +       if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
131 +               return -EINVAL;
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);
137 +       return 0;
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;
144 +       int res;
146 +       if ((parent == NULL) || (child == NULL)) {
147 +               pr_err("parent %p child %p\n", parent, child);
148 +               BUG_ON(1);
149 +       }
150 +       /* no restrictions if the parent directory is not encrypted */
151 +       if (!ext4_encrypted_inode(parent))
152 +               return 1;
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))
157 +               return 0;
158 +       /* if the child directory is not encrypted, this is always a problem */
159 +       if (!ext4_encrypted_inode(child))
160 +               return 0;
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))
165 +               return 0;
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));
175 +/**
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.
179 + *
180 + * Return: Zero on success, non-zero otherwise
181 + */
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))
190 +               return -ENOENT;
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,
195 +                            sizeof(ctx), 0);
196 +       if (!res)
197 +               ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
198 +       return res;
200 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
201 index 41bb58a..e0956b7 100644
202 --- a/fs/ext4/ext4.h
203 +++ b/fs/ext4/ext4.h
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"
210  /*
211   * ioctl commands
212   */
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)
222  /*
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);
237  /* dir.c */
238  extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
239                                   struct file *,
240 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
241 new file mode 100644
242 index 0000000..a69d2ba
243 --- /dev/null
244 +++ b/fs/ext4/ext4_crypto.h
245 @@ -0,0 +1,49 @@
247 + * linux/fs/ext4/ext4_crypto.h
248 + *
249 + * Copyright (C) 2015, Google, Inc.
250 + *
251 + * This contains encryption header content for ext4
252 + *
253 + * Written by Michael Halcrow, 2015.
254 + */
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 {
265 +       char version;
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
274 +/**
275 + * Encryption context for inode
276 + *
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
281 + *  1 byte: Reserved
282 + *  8 bytes: Master Key descriptor
283 + *  16 bytes: Encryption Key derivation nonce
284 + */
285 +struct ext4_encryption_context {
286 +       char format;
287 +       char contents_encryption_mode;
288 +       char filenames_encryption_mode;
289 +       char reserved;
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
299 @@ -14,6 +14,7 @@
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"
306  #include "ext4.h"
307 @@ -196,6 +197,16 @@ journal_err_out:
308         return err;
311 +static int uuid_is_zero(__u8 u[16])
313 +       int     i;
315 +       for (i=0; i < 16; i++)
316 +               if (u[i])
317 +                       return 0;
318 +       return 1;
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:
325         }
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;
331 +               int err = 0;
333 +               if (copy_from_user(&policy,
334 +                                  (struct ext4_encryption_policy __user *)arg,
335 +                                  sizeof(policy))) {
336 +                       err = -EFAULT;
337 +                       goto encryption_policy_out;
338 +               }
340 +               err = ext4_process_policy(&policy, inode);
341 +encryption_policy_out:
342 +               return err;
343 +#else
344 +               return -EOPNOTSUPP;
345 +#endif
346 +       }
347 +       case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
348 +               int err, err2;
349 +               struct ext4_sb_info *sbi = EXT4_SB(sb);
350 +               handle_t *handle;
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);
356 +                       if (err)
357 +                               return err;
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;
362 +                       }
363 +                       err = ext4_journal_get_write_access(handle, sbi->s_sbh);
364 +                       if (err)
365 +                               goto pwsalt_err_journal;
366 +                       generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
367 +                       err = ext4_handle_dirty_metadata(handle, NULL,
368 +                                                        sbi->s_sbh);
369 +               pwsalt_err_journal:
370 +                       err2 = ext4_journal_stop(handle);
371 +                       if (err2 && !err)
372 +                               err = err2;
373 +               pwsalt_err_exit:
374 +                       mnt_drop_write_file(filp);
375 +                       if (err)
376 +                               return err;
377 +               }
378 +               if (copy_to_user((void *) arg, sbi->s_es->s_encrypt_pw_salt,
379 +                                16))
380 +                       return -EFAULT;
381 +               return 0;
382 +       }
383 +       case EXT4_IOC_GET_ENCRYPTION_POLICY: {
384 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
385 +               struct ext4_encryption_policy policy;
386 +               int err = 0;
388 +               if (!ext4_encrypted_inode(inode))
389 +                       return -ENOENT;
390 +               err = ext4_get_policy(inode, &policy);
391 +               if (err)
392 +                       return err;
393 +               if (copy_to_user((void *)arg, &policy, sizeof(policy)))
394 +                       return -EFAULT;
395 +               return 0;
396 +#else
397 +               return -EOPNOTSUPP;
398 +#endif
399 +       }
400         default:
401                 return -ENOTTY;
402         }
403 @@ -680,6 +762,9 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
404         case FITRIM:
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:
410                 break;
411         default:
412                 return -ENOIOCTLCMD;