Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | static 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 | */ |
| 29 | static 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'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 40 | (ctx.flags == |
| 41 | policy->flags) && |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 42 | (ctx.contents_encryption_mode == |
| 43 | policy->contents_encryption_mode) && |
| 44 | (ctx.filenames_encryption_mode == |
| 45 | policy->filenames_encryption_mode)); |
| 46 | } |
| 47 | |
| 48 | static 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'o | f5aed2c | 2015-05-18 13:18:47 -0400 | [diff] [blame] | 54 | res = ext4_convert_inline_data(inode); |
| 55 | if (res) |
| 56 | return res; |
| 57 | |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 58 | ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1; |
| 59 | memcpy(ctx.master_key_descriptor, policy->master_key_descriptor, |
| 60 | EXT4_KEY_DESCRIPTOR_SIZE); |
Michael Halcrow | b30ab0e | 2015-04-12 00:43:56 -0400 | [diff] [blame] | 61 | 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'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 65 | return -EINVAL; |
Michael Halcrow | b30ab0e | 2015-04-12 00:43:56 -0400 | [diff] [blame] | 66 | } |
Michael Halcrow | d5d0e8c | 2015-04-12 00:56:17 -0400 | [diff] [blame] | 67 | 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'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 71 | return -EINVAL; |
Michael Halcrow | d5d0e8c | 2015-04-12 00:56:17 -0400 | [diff] [blame] | 72 | } |
Theodore Ts'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 73 | if (policy->flags & ~EXT4_POLICY_FLAGS_VALID) |
| 74 | return -EINVAL; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 75 | ctx.contents_encryption_mode = policy->contents_encryption_mode; |
| 76 | ctx.filenames_encryption_mode = policy->filenames_encryption_mode; |
Theodore Ts'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 77 | ctx.flags = policy->flags; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 78 | 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 Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 84 | if (!res) |
| 85 | ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); |
| 86 | return res; |
| 87 | } |
| 88 | |
| 89 | int 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)) { |
| 96 | if (!ext4_empty_dir(inode)) |
| 97 | return -ENOTEMPTY; |
| 98 | return ext4_create_encryption_context_from_policy(inode, |
| 99 | policy); |
| 100 | } |
| 101 | |
| 102 | if (ext4_is_encryption_context_consistent_with_policy(inode, policy)) |
| 103 | return 0; |
| 104 | |
| 105 | printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n", |
| 106 | __func__); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | |
| 110 | int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy) |
| 111 | { |
| 112 | struct ext4_encryption_context ctx; |
| 113 | |
| 114 | int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, |
| 115 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, |
| 116 | &ctx, sizeof(ctx)); |
| 117 | if (res != sizeof(ctx)) |
| 118 | return -ENOENT; |
| 119 | if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 120 | return -EINVAL; |
| 121 | policy->version = 0; |
| 122 | policy->contents_encryption_mode = ctx.contents_encryption_mode; |
| 123 | policy->filenames_encryption_mode = ctx.filenames_encryption_mode; |
Theodore Ts'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 124 | policy->flags = ctx.flags; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 125 | memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor, |
| 126 | EXT4_KEY_DESCRIPTOR_SIZE); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int ext4_is_child_context_consistent_with_parent(struct inode *parent, |
| 131 | struct inode *child) |
| 132 | { |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 133 | struct ext4_crypt_info *parent_ci, *child_ci; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 134 | int res; |
| 135 | |
| 136 | if ((parent == NULL) || (child == NULL)) { |
| 137 | pr_err("parent %p child %p\n", parent, child); |
| 138 | BUG_ON(1); |
| 139 | } |
| 140 | /* no restrictions if the parent directory is not encrypted */ |
| 141 | if (!ext4_encrypted_inode(parent)) |
| 142 | return 1; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 143 | /* if the child directory is not encrypted, this is always a problem */ |
| 144 | if (!ext4_encrypted_inode(child)) |
| 145 | return 0; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 146 | res = ext4_get_encryption_info(parent); |
| 147 | if (res) |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 148 | return 0; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 149 | res = ext4_get_encryption_info(child); |
| 150 | if (res) |
| 151 | return 0; |
| 152 | parent_ci = EXT4_I(parent)->i_crypt_info; |
| 153 | child_ci = EXT4_I(child)->i_crypt_info; |
| 154 | if (!parent_ci && !child_ci) |
| 155 | return 1; |
| 156 | if (!parent_ci || !child_ci) |
| 157 | return 0; |
| 158 | |
| 159 | return (memcmp(parent_ci->ci_master_key, |
| 160 | child_ci->ci_master_key, |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 161 | EXT4_KEY_DESCRIPTOR_SIZE) == 0 && |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 162 | (parent_ci->ci_data_mode == child_ci->ci_data_mode) && |
| 163 | (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) && |
| 164 | (parent_ci->ci_flags == child_ci->ci_flags)); |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * ext4_inherit_context() - Sets a child context from its parent |
| 169 | * @parent: Parent inode from which the context is inherited. |
| 170 | * @child: Child inode that inherits the context from @parent. |
| 171 | * |
| 172 | * Return: Zero on success, non-zero otherwise |
| 173 | */ |
| 174 | int ext4_inherit_context(struct inode *parent, struct inode *child) |
| 175 | { |
| 176 | struct ext4_encryption_context ctx; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 177 | struct ext4_crypt_info *ci; |
| 178 | int res; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 179 | |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 180 | res = ext4_get_encryption_info(parent); |
| 181 | if (res < 0) |
| 182 | return res; |
| 183 | ci = EXT4_I(parent)->i_crypt_info; |
| 184 | BUG_ON(ci == NULL); |
| 185 | |
| 186 | ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1; |
| 187 | if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) { |
| 188 | ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS; |
| 189 | ctx.filenames_encryption_mode = |
| 190 | EXT4_ENCRYPTION_MODE_AES_256_CTS; |
| 191 | ctx.flags = 0; |
| 192 | memset(ctx.master_key_descriptor, 0x42, |
| 193 | EXT4_KEY_DESCRIPTOR_SIZE); |
| 194 | res = 0; |
| 195 | } else { |
| 196 | ctx.contents_encryption_mode = ci->ci_data_mode; |
| 197 | ctx.filenames_encryption_mode = ci->ci_filename_mode; |
| 198 | ctx.flags = ci->ci_flags; |
| 199 | memcpy(ctx.master_key_descriptor, ci->ci_master_key, |
| 200 | EXT4_KEY_DESCRIPTOR_SIZE); |
Theodore Ts'o | 6ddb244 | 2015-04-16 01:56:00 -0400 | [diff] [blame] | 201 | } |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 202 | get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE); |
| 203 | res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION, |
| 204 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx, |
| 205 | sizeof(ctx), 0); |
Theodore Ts'o | f5aed2c | 2015-05-18 13:18:47 -0400 | [diff] [blame] | 206 | if (!res) { |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 207 | ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT); |
Theodore Ts'o | f5aed2c | 2015-05-18 13:18:47 -0400 | [diff] [blame] | 208 | ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA); |
Theodore Ts'o | 5555702 | 2015-05-31 13:34:29 -0400 | [diff] [blame^] | 209 | res = ext4_get_encryption_info(child); |
Theodore Ts'o | f5aed2c | 2015-05-18 13:18:47 -0400 | [diff] [blame] | 210 | } |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 211 | return res; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 212 | } |