Clean up whitespace and fixed a bug in the context inheritance error path
[ext4-patch-queue.git] / encryption-policy-and-context
bloba7e0afae51d0d8e551d677761acb22c99d1b2a76
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>
9 ---
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
27 new file mode 100644
28 index 0000000..532b69c
29 --- /dev/null
30 +++ b/fs/ext4/crypto_policy.c
31 @@ -0,0 +1,167 @@
32 +/*
33 + * linux/fs/ext4/crypto_policy.c
34 + *
35 + * Copyright (C) 2015, Google, Inc.
36 + *
37 + * This contains encryption policy functions for ext4
38 + *
39 + * Written by Michael Halcrow, 2015.
40 + */
42 +#include <linux/random.h>
43 +#include <linux/string.h>
44 +#include <linux/types.h>
46 +#include "ext4.h"
47 +#include "xattr.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);
53 +       return (res > 0);
56 +/*
57 + * check whether the policy is consistent with the encryption context
58 + * for the inode
59 + */
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,
66 +                                sizeof(ctx));
67 +       if (res != sizeof(ctx))
68 +               return 0;
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;
81 +       int res = 0;
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,
93 +                            sizeof(ctx), 0);
94 +       if (!res)
95 +               ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
96 +       return res;
99 +int ext4_process_policy(const struct ext4_encryption_policy *policy,
100 +                       struct inode *inode)
102 +       if (policy->version != 0)
103 +               return -EINVAL;
105 +       if (!ext4_inode_has_encryption_context(inode)) {
106 +               if (!ext4_empty_dir(inode))
107 +                       return -ENOTEMPTY;
108 +               return ext4_create_encryption_context_from_policy(inode,
109 +                                                                 policy);
110 +       }
112 +       if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
113 +               return 0;
115 +       printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
116 +              __func__);
117 +       return -EINVAL;
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))
128 +               return -ENOENT;
129 +       if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
130 +               return -EINVAL;
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);
136 +       return 0;
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;
143 +       int res;
145 +       if ((parent == NULL) || (child == NULL)) {
146 +               pr_err("parent %p child %p\n", parent, child);
147 +               BUG_ON(1);
148 +       }
149 +       /* no restrictions if the parent directory is not encrypted */
150 +       if (!ext4_encrypted_inode(parent))
151 +               return 1;
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))
156 +               return 0;
157 +       /* if the child directory is not encrypted, this is always a problem */
158 +       if (!ext4_encrypted_inode(child))
159 +               return 0;
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))
164 +               return 0;
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));
174 +/**
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.
178 + *
179 + * Return: Zero on success, non-zero otherwise
180 + */
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))
189 +               return -ENOENT;
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,
194 +                            sizeof(ctx), 0);
195 +       if (!res)
196 +               ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
197 +       return res;
199 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
200 index 41bb58a..e0956b7 100644
201 --- a/fs/ext4/ext4.h
202 +++ b/fs/ext4/ext4.h
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"
209  /*
210   * ioctl commands
211   */
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)
221  /*
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);
236  /* dir.c */
237  extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
238                                   struct file *,
239 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
240 new file mode 100644
241 index 0000000..a69d2ba
242 --- /dev/null
243 +++ b/fs/ext4/ext4_crypto.h
244 @@ -0,0 +1,49 @@
246 + * linux/fs/ext4/ext4_crypto.h
247 + *
248 + * Copyright (C) 2015, Google, Inc.
249 + *
250 + * This contains encryption header content for ext4
251 + *
252 + * Written by Michael Halcrow, 2015.
253 + */
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 {
264 +       char version;
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
273 +/**
274 + * Encryption context for inode
275 + *
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
280 + *  1 byte: Reserved
281 + *  8 bytes: Master Key descriptor
282 + *  16 bytes: Encryption Key derivation nonce
283 + */
284 +struct ext4_encryption_context {
285 +       char format;
286 +       char contents_encryption_mode;
287 +       char filenames_encryption_mode;
288 +       char reserved;
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
298 @@ -14,6 +14,7 @@
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"
305  #include "ext4.h"
306 @@ -196,6 +197,16 @@ journal_err_out:
307         return err;
310 +static int uuid_is_zero(__u8 u[16])
312 +       int     i;
314 +       for (i = 0; i < 16; i++)
315 +               if (u[i])
316 +                       return 0;
317 +       return 1;
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:
324         }
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;
330 +               int err = 0;
332 +               if (copy_from_user(&policy,
333 +                                  (struct ext4_encryption_policy __user *)arg,
334 +                                  sizeof(policy))) {
335 +                       err = -EFAULT;
336 +                       goto encryption_policy_out;
337 +               }
339 +               err = ext4_process_policy(&policy, inode);
340 +encryption_policy_out:
341 +               return err;
342 +#else
343 +               return -EOPNOTSUPP;
344 +#endif
345 +       }
346 +       case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
347 +               int err, err2;
348 +               struct ext4_sb_info *sbi = EXT4_SB(sb);
349 +               handle_t *handle;
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);
355 +                       if (err)
356 +                               return err;
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;
361 +                       }
362 +                       err = ext4_journal_get_write_access(handle, sbi->s_sbh);
363 +                       if (err)
364 +                               goto pwsalt_err_journal;
365 +                       generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
366 +                       err = ext4_handle_dirty_metadata(handle, NULL,
367 +                                                        sbi->s_sbh);
368 +               pwsalt_err_journal:
369 +                       err2 = ext4_journal_stop(handle);
370 +                       if (err2 && !err)
371 +                               err = err2;
372 +               pwsalt_err_exit:
373 +                       mnt_drop_write_file(filp);
374 +                       if (err)
375 +                               return err;
376 +               }
377 +               if (copy_to_user((void *) arg, sbi->s_es->s_encrypt_pw_salt,
378 +                                16))
379 +                       return -EFAULT;
380 +               return 0;
381 +       }
382 +       case EXT4_IOC_GET_ENCRYPTION_POLICY: {
383 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
384 +               struct ext4_encryption_policy policy;
385 +               int err = 0;
387 +               if (!ext4_encrypted_inode(inode))
388 +                       return -ENOENT;
389 +               err = ext4_get_policy(inode, &policy);
390 +               if (err)
391 +                       return err;
392 +               if (copy_to_user((void *)arg, &policy, sizeof(policy)))
393 +                       return -EFAULT;
394 +               return 0;
395 +#else
396 +               return -EOPNOTSUPP;
397 +#endif
398 +       }
399         default:
400                 return -ENOTTY;
401         }
402 @@ -680,6 +762,9 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
403         case FITRIM:
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:
409                 break;
410         default:
411                 return -ENOIOCTLCMD;