blob: a1d434d0dea8f89686e236e18d566500cc7c0185 [file] [log] [blame]
Michael Halcrow9bd82122015-04-11 07:48:01 -04001/*
2 * linux/fs/ext4/crypto_policy.c
3 *
4 * Copyright (C) 2015, Google, Inc.
5 *
6 * This contains encryption policy functions for ext4
7 *
8 * Written by Michael Halcrow, 2015.
9 */
10
11#include <linux/random.h>
12#include <linux/string.h>
13#include <linux/types.h>
14
15#include "ext4.h"
16#include "xattr.h"
17
18static int ext4_inode_has_encryption_context(struct inode *inode)
19{
20 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
21 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
22 return (res > 0);
23}
24
25/*
26 * check whether the policy is consistent with the encryption context
27 * for the inode
28 */
29static int ext4_is_encryption_context_consistent_with_policy(
30 struct inode *inode, const struct ext4_encryption_policy *policy)
31{
32 struct ext4_encryption_context ctx;
33 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
34 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
35 sizeof(ctx));
36 if (res != sizeof(ctx))
37 return 0;
38 return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
39 EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040040 (ctx.flags ==
41 policy->flags) &&
Michael Halcrow9bd82122015-04-11 07:48:01 -040042 (ctx.contents_encryption_mode ==
43 policy->contents_encryption_mode) &&
44 (ctx.filenames_encryption_mode ==
45 policy->filenames_encryption_mode));
46}
47
48static int ext4_create_encryption_context_from_policy(
49 struct inode *inode, const struct ext4_encryption_policy *policy)
50{
51 struct ext4_encryption_context ctx;
52 int res = 0;
53
Theodore Ts'of5aed2c2015-05-18 13:18:47 -040054 res = ext4_convert_inline_data(inode);
55 if (res)
56 return res;
57
Michael Halcrow9bd82122015-04-11 07:48:01 -040058 ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
59 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
60 EXT4_KEY_DESCRIPTOR_SIZE);
Michael Halcrowb30ab0e2015-04-12 00:43:56 -040061 if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) {
62 printk(KERN_WARNING
63 "%s: Invalid contents encryption mode %d\n", __func__,
64 policy->contents_encryption_mode);
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040065 return -EINVAL;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -040066 }
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040067 if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
68 printk(KERN_WARNING
69 "%s: Invalid filenames encryption mode %d\n", __func__,
70 policy->filenames_encryption_mode);
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040071 return -EINVAL;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040072 }
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040073 if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
74 return -EINVAL;
Michael Halcrow9bd82122015-04-11 07:48:01 -040075 ctx.contents_encryption_mode = policy->contents_encryption_mode;
76 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040077 ctx.flags = policy->flags;
Michael Halcrow9bd82122015-04-11 07:48:01 -040078 BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
79 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
80
81 res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
82 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
83 sizeof(ctx), 0);
Michael Halcrow9bd82122015-04-11 07:48:01 -040084 if (!res)
85 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
86 return res;
87}
88
89int ext4_process_policy(const struct ext4_encryption_policy *policy,
90 struct inode *inode)
91{
92 if (policy->version != 0)
93 return -EINVAL;
94
95 if (!ext4_inode_has_encryption_context(inode)) {
Theodore Ts'od87f6d72015-05-31 13:35:14 -040096 if (!S_ISDIR(inode->i_mode))
97 return -EINVAL;
Michael Halcrow9bd82122015-04-11 07:48:01 -040098 if (!ext4_empty_dir(inode))
99 return -ENOTEMPTY;
100 return ext4_create_encryption_context_from_policy(inode,
101 policy);
102 }
103
104 if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
105 return 0;
106
107 printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
108 __func__);
109 return -EINVAL;
110}
111
112int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
113{
114 struct ext4_encryption_context ctx;
115
116 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
117 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
118 &ctx, sizeof(ctx));
119 if (res != sizeof(ctx))
120 return -ENOENT;
121 if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
122 return -EINVAL;
123 policy->version = 0;
124 policy->contents_encryption_mode = ctx.contents_encryption_mode;
125 policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -0400126 policy->flags = ctx.flags;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400127 memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
128 EXT4_KEY_DESCRIPTOR_SIZE);
129 return 0;
130}
131
132int ext4_is_child_context_consistent_with_parent(struct inode *parent,
133 struct inode *child)
134{
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400135 struct ext4_crypt_info *parent_ci, *child_ci;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400136 int res;
137
138 if ((parent == NULL) || (child == NULL)) {
139 pr_err("parent %p child %p\n", parent, child);
140 BUG_ON(1);
141 }
142 /* no restrictions if the parent directory is not encrypted */
143 if (!ext4_encrypted_inode(parent))
144 return 1;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400145 /* if the child directory is not encrypted, this is always a problem */
146 if (!ext4_encrypted_inode(child))
147 return 0;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400148 res = ext4_get_encryption_info(parent);
149 if (res)
Michael Halcrow9bd82122015-04-11 07:48:01 -0400150 return 0;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400151 res = ext4_get_encryption_info(child);
152 if (res)
153 return 0;
154 parent_ci = EXT4_I(parent)->i_crypt_info;
155 child_ci = EXT4_I(child)->i_crypt_info;
156 if (!parent_ci && !child_ci)
157 return 1;
158 if (!parent_ci || !child_ci)
159 return 0;
160
161 return (memcmp(parent_ci->ci_master_key,
162 child_ci->ci_master_key,
Michael Halcrow9bd82122015-04-11 07:48:01 -0400163 EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400164 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
165 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
166 (parent_ci->ci_flags == child_ci->ci_flags));
Michael Halcrow9bd82122015-04-11 07:48:01 -0400167}
168
169/**
170 * ext4_inherit_context() - Sets a child context from its parent
171 * @parent: Parent inode from which the context is inherited.
172 * @child: Child inode that inherits the context from @parent.
173 *
174 * Return: Zero on success, non-zero otherwise
175 */
176int ext4_inherit_context(struct inode *parent, struct inode *child)
177{
178 struct ext4_encryption_context ctx;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400179 struct ext4_crypt_info *ci;
180 int res;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400181
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400182 res = ext4_get_encryption_info(parent);
183 if (res < 0)
184 return res;
185 ci = EXT4_I(parent)->i_crypt_info;
186 BUG_ON(ci == NULL);
187
188 ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
189 if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
190 ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
191 ctx.filenames_encryption_mode =
192 EXT4_ENCRYPTION_MODE_AES_256_CTS;
193 ctx.flags = 0;
194 memset(ctx.master_key_descriptor, 0x42,
195 EXT4_KEY_DESCRIPTOR_SIZE);
196 res = 0;
197 } else {
198 ctx.contents_encryption_mode = ci->ci_data_mode;
199 ctx.filenames_encryption_mode = ci->ci_filename_mode;
200 ctx.flags = ci->ci_flags;
201 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
202 EXT4_KEY_DESCRIPTOR_SIZE);
Theodore Ts'o6ddb2442015-04-16 01:56:00 -0400203 }
Michael Halcrow9bd82122015-04-11 07:48:01 -0400204 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
205 res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
206 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
207 sizeof(ctx), 0);
Theodore Ts'of5aed2c2015-05-18 13:18:47 -0400208 if (!res) {
Michael Halcrow9bd82122015-04-11 07:48:01 -0400209 ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
Theodore Ts'of5aed2c2015-05-18 13:18:47 -0400210 ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA);
Theodore Ts'o55557022015-05-31 13:34:29 -0400211 res = ext4_get_encryption_info(child);
Theodore Ts'of5aed2c2015-05-18 13:18:47 -0400212 }
Michael Halcrow9bd82122015-04-11 07:48:01 -0400213 return res;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400214}