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> |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 13 | #include <linux/mount.h> |
Theodore Ts'o | cc4e0df | 2016-11-26 22:05:18 -0500 | [diff] [blame] | 14 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 15 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 16 | /* |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 17 | * check whether an encryption policy is consistent with an encryption context |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 18 | */ |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 19 | static bool is_encryption_context_consistent_with_policy( |
| 20 | const struct fscrypt_context *ctx, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 21 | const struct fscrypt_policy *policy) |
| 22 | { |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 23 | return memcmp(ctx->master_key_descriptor, policy->master_key_descriptor, |
| 24 | FS_KEY_DESCRIPTOR_SIZE) == 0 && |
| 25 | (ctx->flags == policy->flags) && |
| 26 | (ctx->contents_encryption_mode == |
| 27 | policy->contents_encryption_mode) && |
| 28 | (ctx->filenames_encryption_mode == |
| 29 | policy->filenames_encryption_mode); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static int create_encryption_context_from_policy(struct inode *inode, |
| 33 | const struct fscrypt_policy *policy) |
| 34 | { |
| 35 | struct fscrypt_context ctx; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 36 | |
| 37 | if (!inode->i_sb->s_cop->set_context) |
| 38 | return -EOPNOTSUPP; |
| 39 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 40 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
| 41 | memcpy(ctx.master_key_descriptor, policy->master_key_descriptor, |
| 42 | FS_KEY_DESCRIPTOR_SIZE); |
| 43 | |
| 44 | if (!fscrypt_valid_contents_enc_mode( |
Eric Biggers | 868e1bc | 2016-12-05 11:12:47 -0800 | [diff] [blame] | 45 | policy->contents_encryption_mode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 46 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 47 | |
| 48 | if (!fscrypt_valid_filenames_enc_mode( |
Eric Biggers | 868e1bc | 2016-12-05 11:12:47 -0800 | [diff] [blame] | 49 | policy->filenames_encryption_mode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 50 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 51 | |
| 52 | if (policy->flags & ~FS_POLICY_FLAGS_VALID) |
| 53 | return -EINVAL; |
| 54 | |
| 55 | ctx.contents_encryption_mode = policy->contents_encryption_mode; |
| 56 | ctx.filenames_encryption_mode = policy->filenames_encryption_mode; |
| 57 | ctx.flags = policy->flags; |
| 58 | BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE); |
| 59 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
| 60 | |
| 61 | return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL); |
| 62 | } |
| 63 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 64 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 65 | { |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 66 | struct fscrypt_policy policy; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 67 | struct inode *inode = file_inode(filp); |
| 68 | int ret; |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 69 | struct fscrypt_context ctx; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 70 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 71 | if (copy_from_user(&policy, arg, sizeof(policy))) |
| 72 | return -EFAULT; |
| 73 | |
Eric Biggers | 163ae1c | 2016-09-08 10:57:08 -0700 | [diff] [blame] | 74 | if (!inode_owner_or_capable(inode)) |
| 75 | return -EACCES; |
| 76 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 77 | if (policy.version != 0) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 78 | return -EINVAL; |
| 79 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 80 | ret = mnt_want_write_file(filp); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 84 | inode_lock(inode); |
| 85 | |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 86 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 87 | if (ret == -ENODATA) { |
Eric Biggers | 002ced4 | 2016-09-08 11:36:39 -0700 | [diff] [blame] | 88 | if (!S_ISDIR(inode->i_mode)) |
Eric Biggers | dffd0cf | 2016-12-05 11:12:45 -0800 | [diff] [blame] | 89 | ret = -ENOTDIR; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 90 | else if (!inode->i_sb->s_cop->empty_dir) |
| 91 | ret = -EOPNOTSUPP; |
| 92 | else if (!inode->i_sb->s_cop->empty_dir(inode)) |
| 93 | ret = -ENOTEMPTY; |
| 94 | else |
| 95 | ret = create_encryption_context_from_policy(inode, |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 96 | &policy); |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 97 | } else if (ret == sizeof(ctx) && |
| 98 | is_encryption_context_consistent_with_policy(&ctx, |
| 99 | &policy)) { |
| 100 | /* The file already uses the same encryption policy. */ |
| 101 | ret = 0; |
| 102 | } else if (ret >= 0 || ret == -ERANGE) { |
| 103 | /* The file already uses a different encryption policy. */ |
Eric Biggers | 8488cd9 | 2016-12-05 11:12:46 -0800 | [diff] [blame] | 104 | ret = -EEXIST; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 107 | inode_unlock(inode); |
| 108 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 109 | mnt_drop_write_file(filp); |
| 110 | return ret; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 111 | } |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 112 | EXPORT_SYMBOL(fscrypt_ioctl_set_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 113 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 114 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 115 | { |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 116 | struct inode *inode = file_inode(filp); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 117 | struct fscrypt_context ctx; |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 118 | struct fscrypt_policy policy; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 119 | int res; |
| 120 | |
| 121 | if (!inode->i_sb->s_cop->get_context || |
| 122 | !inode->i_sb->s_cop->is_encrypted(inode)) |
| 123 | return -ENODATA; |
| 124 | |
| 125 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 126 | if (res < 0 && res != -ERANGE) |
| 127 | return res; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 128 | if (res != sizeof(ctx)) |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 129 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 130 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 131 | return -EINVAL; |
| 132 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 133 | policy.version = 0; |
| 134 | policy.contents_encryption_mode = ctx.contents_encryption_mode; |
| 135 | policy.filenames_encryption_mode = ctx.filenames_encryption_mode; |
| 136 | policy.flags = ctx.flags; |
| 137 | memcpy(policy.master_key_descriptor, ctx.master_key_descriptor, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 138 | FS_KEY_DESCRIPTOR_SIZE); |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 139 | |
| 140 | if (copy_to_user(arg, &policy, sizeof(policy))) |
| 141 | return -EFAULT; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 144 | EXPORT_SYMBOL(fscrypt_ioctl_get_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 145 | |
| 146 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) |
| 147 | { |
| 148 | struct fscrypt_info *parent_ci, *child_ci; |
| 149 | int res; |
| 150 | |
| 151 | if ((parent == NULL) || (child == NULL)) { |
| 152 | printk(KERN_ERR "parent %p child %p\n", parent, child); |
| 153 | BUG_ON(1); |
| 154 | } |
| 155 | |
Eric Biggers | 42d97eb | 2016-12-19 14:20:13 -0800 | [diff] [blame] | 156 | /* No restrictions on file types which are never encrypted */ |
| 157 | if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && |
| 158 | !S_ISLNK(child->i_mode)) |
| 159 | return 1; |
| 160 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 161 | /* no restrictions if the parent directory is not encrypted */ |
| 162 | if (!parent->i_sb->s_cop->is_encrypted(parent)) |
| 163 | return 1; |
| 164 | /* if the child directory is not encrypted, this is always a problem */ |
| 165 | if (!parent->i_sb->s_cop->is_encrypted(child)) |
| 166 | return 0; |
| 167 | res = fscrypt_get_encryption_info(parent); |
| 168 | if (res) |
| 169 | return 0; |
| 170 | res = fscrypt_get_encryption_info(child); |
| 171 | if (res) |
| 172 | return 0; |
| 173 | parent_ci = parent->i_crypt_info; |
| 174 | child_ci = child->i_crypt_info; |
| 175 | if (!parent_ci && !child_ci) |
| 176 | return 1; |
| 177 | if (!parent_ci || !child_ci) |
| 178 | return 0; |
| 179 | |
| 180 | return (memcmp(parent_ci->ci_master_key, |
| 181 | child_ci->ci_master_key, |
| 182 | FS_KEY_DESCRIPTOR_SIZE) == 0 && |
| 183 | (parent_ci->ci_data_mode == child_ci->ci_data_mode) && |
| 184 | (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) && |
| 185 | (parent_ci->ci_flags == child_ci->ci_flags)); |
| 186 | } |
| 187 | EXPORT_SYMBOL(fscrypt_has_permitted_context); |
| 188 | |
| 189 | /** |
| 190 | * fscrypt_inherit_context() - Sets a child context from its parent |
| 191 | * @parent: Parent inode from which the context is inherited. |
| 192 | * @child: Child inode that inherits the context from @parent. |
| 193 | * @fs_data: private data given by FS. |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 194 | * @preload: preload child i_crypt_info if true |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 195 | * |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 196 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 197 | */ |
| 198 | int fscrypt_inherit_context(struct inode *parent, struct inode *child, |
| 199 | void *fs_data, bool preload) |
| 200 | { |
| 201 | struct fscrypt_context ctx; |
| 202 | struct fscrypt_info *ci; |
| 203 | int res; |
| 204 | |
| 205 | if (!parent->i_sb->s_cop->set_context) |
| 206 | return -EOPNOTSUPP; |
| 207 | |
| 208 | res = fscrypt_get_encryption_info(parent); |
| 209 | if (res < 0) |
| 210 | return res; |
| 211 | |
| 212 | ci = parent->i_crypt_info; |
| 213 | if (ci == NULL) |
| 214 | return -ENOKEY; |
| 215 | |
| 216 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 217 | ctx.contents_encryption_mode = ci->ci_data_mode; |
| 218 | ctx.filenames_encryption_mode = ci->ci_filename_mode; |
| 219 | ctx.flags = ci->ci_flags; |
| 220 | memcpy(ctx.master_key_descriptor, ci->ci_master_key, |
| 221 | FS_KEY_DESCRIPTOR_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 222 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
| 223 | res = parent->i_sb->s_cop->set_context(child, &ctx, |
| 224 | sizeof(ctx), fs_data); |
| 225 | if (res) |
| 226 | return res; |
| 227 | return preload ? fscrypt_get_encryption_info(child): 0; |
| 228 | } |
| 229 | EXPORT_SYMBOL(fscrypt_inherit_context); |