blob: 370d3aa0a9cf95918d5ac0ec74bb832e6470303f [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
54 ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
55 memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
56 EXT4_KEY_DESCRIPTOR_SIZE);
Michael Halcrowb30ab0e2015-04-12 00:43:56 -040057 if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) {
58 printk(KERN_WARNING
59 "%s: Invalid contents encryption mode %d\n", __func__,
60 policy->contents_encryption_mode);
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040061 return -EINVAL;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -040062 }
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040063 if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
64 printk(KERN_WARNING
65 "%s: Invalid filenames encryption mode %d\n", __func__,
66 policy->filenames_encryption_mode);
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040067 return -EINVAL;
Michael Halcrowd5d0e8c2015-04-12 00:56:17 -040068 }
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040069 if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
70 return -EINVAL;
Michael Halcrow9bd82122015-04-11 07:48:01 -040071 ctx.contents_encryption_mode = policy->contents_encryption_mode;
72 ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -040073 ctx.flags = policy->flags;
Michael Halcrow9bd82122015-04-11 07:48:01 -040074 BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
75 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
76
77 res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
78 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
79 sizeof(ctx), 0);
Michael Halcrow9bd82122015-04-11 07:48:01 -040080 if (!res)
81 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
82 return res;
83}
84
85int ext4_process_policy(const struct ext4_encryption_policy *policy,
86 struct inode *inode)
87{
88 if (policy->version != 0)
89 return -EINVAL;
90
91 if (!ext4_inode_has_encryption_context(inode)) {
92 if (!ext4_empty_dir(inode))
93 return -ENOTEMPTY;
94 return ext4_create_encryption_context_from_policy(inode,
95 policy);
96 }
97
98 if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
99 return 0;
100
101 printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
102 __func__);
103 return -EINVAL;
104}
105
106int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
107{
108 struct ext4_encryption_context ctx;
109
110 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
111 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
112 &ctx, sizeof(ctx));
113 if (res != sizeof(ctx))
114 return -ENOENT;
115 if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
116 return -EINVAL;
117 policy->version = 0;
118 policy->contents_encryption_mode = ctx.contents_encryption_mode;
119 policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
Theodore Ts'oa44cd7a2015-05-01 16:56:50 -0400120 policy->flags = ctx.flags;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400121 memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
122 EXT4_KEY_DESCRIPTOR_SIZE);
123 return 0;
124}
125
126int ext4_is_child_context_consistent_with_parent(struct inode *parent,
127 struct inode *child)
128{
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400129 struct ext4_crypt_info *parent_ci, *child_ci;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400130 int res;
131
132 if ((parent == NULL) || (child == NULL)) {
133 pr_err("parent %p child %p\n", parent, child);
134 BUG_ON(1);
135 }
136 /* no restrictions if the parent directory is not encrypted */
137 if (!ext4_encrypted_inode(parent))
138 return 1;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400139 /* if the child directory is not encrypted, this is always a problem */
140 if (!ext4_encrypted_inode(child))
141 return 0;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400142 res = ext4_get_encryption_info(parent);
143 if (res)
Michael Halcrow9bd82122015-04-11 07:48:01 -0400144 return 0;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400145 res = ext4_get_encryption_info(child);
146 if (res)
147 return 0;
148 parent_ci = EXT4_I(parent)->i_crypt_info;
149 child_ci = EXT4_I(child)->i_crypt_info;
150 if (!parent_ci && !child_ci)
151 return 1;
152 if (!parent_ci || !child_ci)
153 return 0;
154
155 return (memcmp(parent_ci->ci_master_key,
156 child_ci->ci_master_key,
Michael Halcrow9bd82122015-04-11 07:48:01 -0400157 EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400158 (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
159 (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
160 (parent_ci->ci_flags == child_ci->ci_flags));
Michael Halcrow9bd82122015-04-11 07:48:01 -0400161}
162
163/**
164 * ext4_inherit_context() - Sets a child context from its parent
165 * @parent: Parent inode from which the context is inherited.
166 * @child: Child inode that inherits the context from @parent.
167 *
168 * Return: Zero on success, non-zero otherwise
169 */
170int ext4_inherit_context(struct inode *parent, struct inode *child)
171{
172 struct ext4_encryption_context ctx;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400173 struct ext4_crypt_info *ci;
174 int res;
Michael Halcrow9bd82122015-04-11 07:48:01 -0400175
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400176 res = ext4_get_encryption_info(parent);
177 if (res < 0)
178 return res;
179 ci = EXT4_I(parent)->i_crypt_info;
180 BUG_ON(ci == NULL);
181
182 ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
183 if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
184 ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
185 ctx.filenames_encryption_mode =
186 EXT4_ENCRYPTION_MODE_AES_256_CTS;
187 ctx.flags = 0;
188 memset(ctx.master_key_descriptor, 0x42,
189 EXT4_KEY_DESCRIPTOR_SIZE);
190 res = 0;
191 } else {
192 ctx.contents_encryption_mode = ci->ci_data_mode;
193 ctx.filenames_encryption_mode = ci->ci_filename_mode;
194 ctx.flags = ci->ci_flags;
195 memcpy(ctx.master_key_descriptor, ci->ci_master_key,
196 EXT4_KEY_DESCRIPTOR_SIZE);
Theodore Ts'o6ddb2442015-04-16 01:56:00 -0400197 }
Michael Halcrow9bd82122015-04-11 07:48:01 -0400198 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
199 res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
200 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
201 sizeof(ctx), 0);
Michael Halcrow9bd82122015-04-11 07:48:01 -0400202 if (!res)
203 ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
204 return res;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400205
Michael Halcrow9bd82122015-04-11 07:48:01 -0400206}