Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Encryption policy functions for per-file encryption support. |
| 3 | * |
| 4 | * Copyright (C) 2015, Google, Inc. |
| 5 | * Copyright (C) 2015, Motorola Mobility. |
| 6 | * |
| 7 | * Written by Michael Halcrow, 2015. |
| 8 | * Modified by Jaegeuk Kim, 2015. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/random.h> |
| 12 | #include <linux/string.h> |
| 13 | #include <linux/fscrypto.h> |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 14 | #include <linux/mount.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 15 | |
| 16 | static int inode_has_encryption_context(struct inode *inode) |
| 17 | { |
| 18 | if (!inode->i_sb->s_cop->get_context) |
| 19 | return 0; |
| 20 | return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0); |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * check whether the policy is consistent with the encryption context |
| 25 | * for the inode |
| 26 | */ |
| 27 | static int is_encryption_context_consistent_with_policy(struct inode *inode, |
| 28 | const struct fscrypt_policy *policy) |
| 29 | { |
| 30 | struct fscrypt_context ctx; |
| 31 | int res; |
| 32 | |
| 33 | if (!inode->i_sb->s_cop->get_context) |
| 34 | return 0; |
| 35 | |
| 36 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 37 | if (res != sizeof(ctx)) |
| 38 | return 0; |
| 39 | |
| 40 | return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor, |
| 41 | FS_KEY_DESCRIPTOR_SIZE) == 0 && |
| 42 | (ctx.flags == policy->flags) && |
| 43 | (ctx.contents_encryption_mode == |
| 44 | policy->contents_encryption_mode) && |
| 45 | (ctx.filenames_encryption_mode == |
| 46 | policy->filenames_encryption_mode)); |
| 47 | } |
| 48 | |
| 49 | static int create_encryption_context_from_policy(struct inode *inode, |
| 50 | const struct fscrypt_policy *policy) |
| 51 | { |
| 52 | struct fscrypt_context ctx; |
| 53 | int res; |
| 54 | |
| 55 | if (!inode->i_sb->s_cop->set_context) |
| 56 | return -EOPNOTSUPP; |
| 57 | |
| 58 | if (inode->i_sb->s_cop->prepare_context) { |
| 59 | res = inode->i_sb->s_cop->prepare_context(inode); |
| 60 | if (res) |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
| 65 | memcpy(ctx.master_key_descriptor, policy->master_key_descriptor, |
| 66 | FS_KEY_DESCRIPTOR_SIZE); |
| 67 | |
| 68 | if (!fscrypt_valid_contents_enc_mode( |
| 69 | policy->contents_encryption_mode)) { |
| 70 | printk(KERN_WARNING |
| 71 | "%s: Invalid contents encryption mode %d\n", __func__, |
| 72 | policy->contents_encryption_mode); |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
| 76 | if (!fscrypt_valid_filenames_enc_mode( |
| 77 | policy->filenames_encryption_mode)) { |
| 78 | printk(KERN_WARNING |
| 79 | "%s: Invalid filenames encryption mode %d\n", __func__, |
| 80 | policy->filenames_encryption_mode); |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | |
| 84 | if (policy->flags & ~FS_POLICY_FLAGS_VALID) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | ctx.contents_encryption_mode = policy->contents_encryption_mode; |
| 88 | ctx.filenames_encryption_mode = policy->filenames_encryption_mode; |
| 89 | ctx.flags = policy->flags; |
| 90 | BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE); |
| 91 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
| 92 | |
| 93 | return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL); |
| 94 | } |
| 95 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 96 | int fscrypt_process_policy(struct file *filp, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 97 | const struct fscrypt_policy *policy) |
| 98 | { |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 99 | struct inode *inode = file_inode(filp); |
| 100 | int ret; |
| 101 | |
Eric Biggers | 163ae1c | 2016-09-08 10:57:08 -0700 | [diff] [blame] | 102 | if (!inode_owner_or_capable(inode)) |
| 103 | return -EACCES; |
| 104 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 105 | if (policy->version != 0) |
| 106 | return -EINVAL; |
| 107 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 108 | ret = mnt_want_write_file(filp); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 112 | inode_lock(inode); |
| 113 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 114 | if (!inode_has_encryption_context(inode)) { |
Eric Biggers | 002ced4 | 2016-09-08 11:36:39 -0700 | [diff] [blame] | 115 | if (!S_ISDIR(inode->i_mode)) |
Eric Biggers | d3d5e69 | 2016-12-05 11:12:45 -0800 | [diff] [blame] | 116 | ret = -ENOTDIR; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 117 | else if (!inode->i_sb->s_cop->empty_dir) |
| 118 | ret = -EOPNOTSUPP; |
| 119 | else if (!inode->i_sb->s_cop->empty_dir(inode)) |
| 120 | ret = -ENOTEMPTY; |
| 121 | else |
| 122 | ret = create_encryption_context_from_policy(inode, |
| 123 | policy); |
| 124 | } else if (!is_encryption_context_consistent_with_policy(inode, |
| 125 | policy)) { |
| 126 | printk(KERN_WARNING |
| 127 | "%s: Policy inconsistent with encryption context\n", |
| 128 | __func__); |
| 129 | ret = -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 132 | inode_unlock(inode); |
| 133 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 134 | mnt_drop_write_file(filp); |
| 135 | return ret; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL(fscrypt_process_policy); |
| 138 | |
| 139 | int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy) |
| 140 | { |
| 141 | struct fscrypt_context ctx; |
| 142 | int res; |
| 143 | |
| 144 | if (!inode->i_sb->s_cop->get_context || |
| 145 | !inode->i_sb->s_cop->is_encrypted(inode)) |
| 146 | return -ENODATA; |
| 147 | |
| 148 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 149 | if (res != sizeof(ctx)) |
| 150 | return -ENODATA; |
| 151 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 152 | return -EINVAL; |
| 153 | |
| 154 | policy->version = 0; |
| 155 | policy->contents_encryption_mode = ctx.contents_encryption_mode; |
| 156 | policy->filenames_encryption_mode = ctx.filenames_encryption_mode; |
| 157 | policy->flags = ctx.flags; |
| 158 | memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor, |
| 159 | FS_KEY_DESCRIPTOR_SIZE); |
| 160 | return 0; |
| 161 | } |
| 162 | EXPORT_SYMBOL(fscrypt_get_policy); |
| 163 | |
Eric Biggers | 64a599a | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 164 | /** |
| 165 | * fscrypt_has_permitted_context() - is a file's encryption policy permitted |
| 166 | * within its directory? |
| 167 | * |
| 168 | * @parent: inode for parent directory |
| 169 | * @child: inode for file being looked up, opened, or linked into @parent |
| 170 | * |
| 171 | * Filesystems must call this before permitting access to an inode in a |
| 172 | * situation where the parent directory is encrypted (either before allowing |
| 173 | * ->lookup() to succeed, or for a regular file before allowing it to be opened) |
| 174 | * and before any operation that involves linking an inode into an encrypted |
| 175 | * directory, including link, rename, and cross rename. It enforces the |
| 176 | * constraint that within a given encrypted directory tree, all files use the |
| 177 | * same encryption policy. The pre-access check is needed to detect potentially |
| 178 | * malicious offline violations of this constraint, while the link and rename |
| 179 | * checks are needed to prevent online violations of this constraint. |
| 180 | * |
| 181 | * Return: 1 if permitted, 0 if forbidden. If forbidden, the caller must fail |
| 182 | * the filesystem operation with EPERM. |
| 183 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 184 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) |
| 185 | { |
Eric Biggers | 64a599a | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 186 | const struct fscrypt_operations *cops = parent->i_sb->s_cop; |
| 187 | const struct fscrypt_info *parent_ci, *child_ci; |
| 188 | struct fscrypt_context parent_ctx, child_ctx; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 189 | int res; |
| 190 | |
Eric Biggers | d259b68 | 2016-12-19 14:20:13 -0800 | [diff] [blame] | 191 | /* No restrictions on file types which are never encrypted */ |
| 192 | if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && |
| 193 | !S_ISLNK(child->i_mode)) |
| 194 | return 1; |
| 195 | |
Eric Biggers | 64a599a | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 196 | /* No restrictions if the parent directory is unencrypted */ |
| 197 | if (!cops->is_encrypted(parent)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 198 | return 1; |
Eric Biggers | 64a599a | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 199 | |
| 200 | /* Encrypted directories must not contain unencrypted files */ |
| 201 | if (!cops->is_encrypted(child)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 202 | return 0; |
Eric Biggers | 64a599a | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Both parent and child are encrypted, so verify they use the same |
| 206 | * encryption policy. Compare the fscrypt_info structs if the keys are |
| 207 | * available, otherwise retrieve and compare the fscrypt_contexts. |
| 208 | * |
| 209 | * Note that the fscrypt_context retrieval will be required frequently |
| 210 | * when accessing an encrypted directory tree without the key. |
| 211 | * Performance-wise this is not a big deal because we already don't |
| 212 | * really optimize for file access without the key (to the extent that |
| 213 | * such access is even possible), given that any attempted access |
| 214 | * already causes a fscrypt_context retrieval and keyring search. |
| 215 | * |
| 216 | * In any case, if an unexpected error occurs, fall back to "forbidden". |
| 217 | */ |
| 218 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 219 | res = fscrypt_get_encryption_info(parent); |
| 220 | if (res) |
| 221 | return 0; |
| 222 | res = fscrypt_get_encryption_info(child); |
| 223 | if (res) |
| 224 | return 0; |
| 225 | parent_ci = parent->i_crypt_info; |
| 226 | child_ci = child->i_crypt_info; |
Eric Biggers | 64a599a | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 227 | |
| 228 | if (parent_ci && child_ci) { |
| 229 | return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key, |
| 230 | FS_KEY_DESCRIPTOR_SIZE) == 0 && |
| 231 | (parent_ci->ci_data_mode == child_ci->ci_data_mode) && |
| 232 | (parent_ci->ci_filename_mode == |
| 233 | child_ci->ci_filename_mode) && |
| 234 | (parent_ci->ci_flags == child_ci->ci_flags); |
| 235 | } |
| 236 | |
| 237 | res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx)); |
| 238 | if (res != sizeof(parent_ctx)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 239 | return 0; |
| 240 | |
Eric Biggers | 64a599a | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 241 | res = cops->get_context(child, &child_ctx, sizeof(child_ctx)); |
| 242 | if (res != sizeof(child_ctx)) |
| 243 | return 0; |
| 244 | |
| 245 | return memcmp(parent_ctx.master_key_descriptor, |
| 246 | child_ctx.master_key_descriptor, |
| 247 | FS_KEY_DESCRIPTOR_SIZE) == 0 && |
| 248 | (parent_ctx.contents_encryption_mode == |
| 249 | child_ctx.contents_encryption_mode) && |
| 250 | (parent_ctx.filenames_encryption_mode == |
| 251 | child_ctx.filenames_encryption_mode) && |
| 252 | (parent_ctx.flags == child_ctx.flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 253 | } |
| 254 | EXPORT_SYMBOL(fscrypt_has_permitted_context); |
| 255 | |
| 256 | /** |
| 257 | * fscrypt_inherit_context() - Sets a child context from its parent |
| 258 | * @parent: Parent inode from which the context is inherited. |
| 259 | * @child: Child inode that inherits the context from @parent. |
| 260 | * @fs_data: private data given by FS. |
| 261 | * @preload: preload child i_crypt_info |
| 262 | * |
| 263 | * Return: Zero on success, non-zero otherwise |
| 264 | */ |
| 265 | int fscrypt_inherit_context(struct inode *parent, struct inode *child, |
| 266 | void *fs_data, bool preload) |
| 267 | { |
| 268 | struct fscrypt_context ctx; |
| 269 | struct fscrypt_info *ci; |
| 270 | int res; |
| 271 | |
| 272 | if (!parent->i_sb->s_cop->set_context) |
| 273 | return -EOPNOTSUPP; |
| 274 | |
| 275 | res = fscrypt_get_encryption_info(parent); |
| 276 | if (res < 0) |
| 277 | return res; |
| 278 | |
| 279 | ci = parent->i_crypt_info; |
| 280 | if (ci == NULL) |
| 281 | return -ENOKEY; |
| 282 | |
| 283 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
| 284 | if (fscrypt_dummy_context_enabled(parent)) { |
| 285 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 286 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
| 287 | ctx.flags = 0; |
| 288 | memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE); |
| 289 | res = 0; |
| 290 | } else { |
| 291 | ctx.contents_encryption_mode = ci->ci_data_mode; |
| 292 | ctx.filenames_encryption_mode = ci->ci_filename_mode; |
| 293 | ctx.flags = ci->ci_flags; |
| 294 | memcpy(ctx.master_key_descriptor, ci->ci_master_key, |
| 295 | FS_KEY_DESCRIPTOR_SIZE); |
| 296 | } |
| 297 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
| 298 | res = parent->i_sb->s_cop->set_context(child, &ctx, |
| 299 | sizeof(ctx), fs_data); |
| 300 | if (res) |
| 301 | return res; |
| 302 | return preload ? fscrypt_get_encryption_info(child): 0; |
| 303 | } |
| 304 | EXPORT_SYMBOL(fscrypt_inherit_context); |