blob: bd525f7573a49ce4f238848596627095adf14cbf [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Eric Biggersefcc7ae2017-10-09 12:15:40 -07002/*
3 * fs/crypto/hooks.c
4 *
5 * Encryption hooks for higher-level filesystem operations.
6 */
7
8#include <linux/ratelimit.h>
9#include "fscrypt_private.h"
10
11/**
12 * fscrypt_file_open - prepare to open a possibly-encrypted regular file
13 * @inode: the inode being opened
14 * @filp: the struct file being set up
15 *
16 * Currently, an encrypted regular file can only be opened if its encryption key
17 * is available; access to the raw encrypted contents is not supported.
18 * Therefore, we first set up the inode's encryption key (if not already done)
19 * and return an error if it's unavailable.
20 *
21 * We also verify that if the parent directory (from the path via which the file
22 * is being opened) is encrypted, then the inode being opened uses the same
23 * encryption policy. This is needed as part of the enforcement that all files
24 * in an encrypted directory tree use the same encryption policy, as a
25 * protection against certain types of offline attacks. Note that this check is
26 * needed even when opening an *unencrypted* file, since it's forbidden to have
27 * an unencrypted file in an encrypted directory.
28 *
29 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
30 */
31int fscrypt_file_open(struct inode *inode, struct file *filp)
32{
33 int err;
34 struct dentry *dir;
35
36 err = fscrypt_require_key(inode);
37 if (err)
38 return err;
39
40 dir = dget_parent(file_dentry(filp));
41 if (IS_ENCRYPTED(d_inode(dir)) &&
42 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
Eric Biggers544d08f2018-04-30 15:51:47 -070043 fscrypt_warn(inode->i_sb,
44 "inconsistent encryption contexts: %lu/%lu",
45 d_inode(dir)->i_ino, inode->i_ino);
Eric Biggersefcc7ae2017-10-09 12:15:40 -070046 err = -EPERM;
47 }
48 dput(dir);
49 return err;
50}
51EXPORT_SYMBOL_GPL(fscrypt_file_open);
Eric Biggers0ea87a92017-10-09 12:15:41 -070052
Eric Biggers968dd6d2019-03-20 11:39:10 -070053int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
54 struct dentry *dentry)
Eric Biggers0ea87a92017-10-09 12:15:41 -070055{
56 int err;
57
58 err = fscrypt_require_key(dir);
59 if (err)
60 return err;
61
Eric Biggers968dd6d2019-03-20 11:39:10 -070062 /* ... in case we looked up ciphertext name before key was added */
63 if (dentry->d_flags & DCACHE_ENCRYPTED_NAME)
64 return -ENOKEY;
65
Eric Biggers0ea87a92017-10-09 12:15:41 -070066 if (!fscrypt_has_permitted_context(dir, inode))
Eric Biggersf5e55e72019-01-22 16:20:21 -080067 return -EXDEV;
Eric Biggers0ea87a92017-10-09 12:15:41 -070068
69 return 0;
70}
71EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
Eric Biggers94b26f32017-10-09 12:15:42 -070072
73int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
74 struct inode *new_dir, struct dentry *new_dentry,
75 unsigned int flags)
76{
77 int err;
78
79 err = fscrypt_require_key(old_dir);
80 if (err)
81 return err;
82
83 err = fscrypt_require_key(new_dir);
84 if (err)
85 return err;
86
Eric Biggers968dd6d2019-03-20 11:39:10 -070087 /* ... in case we looked up ciphertext name(s) before key was added */
88 if ((old_dentry->d_flags | new_dentry->d_flags) &
89 DCACHE_ENCRYPTED_NAME)
90 return -ENOKEY;
91
Eric Biggers94b26f32017-10-09 12:15:42 -070092 if (old_dir != new_dir) {
93 if (IS_ENCRYPTED(new_dir) &&
94 !fscrypt_has_permitted_context(new_dir,
95 d_inode(old_dentry)))
Eric Biggersf5e55e72019-01-22 16:20:21 -080096 return -EXDEV;
Eric Biggers94b26f32017-10-09 12:15:42 -070097
98 if ((flags & RENAME_EXCHANGE) &&
99 IS_ENCRYPTED(old_dir) &&
100 !fscrypt_has_permitted_context(old_dir,
101 d_inode(new_dentry)))
Eric Biggersf5e55e72019-01-22 16:20:21 -0800102 return -EXDEV;
Eric Biggers94b26f32017-10-09 12:15:42 -0700103 }
104 return 0;
105}
106EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
Eric Biggers32c3cf02017-10-09 12:15:43 -0700107
Eric Biggersb01531d2019-03-20 11:39:13 -0700108int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
109 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700110{
Eric Biggersb01531d2019-03-20 11:39:13 -0700111 int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
Eric Biggers32c3cf02017-10-09 12:15:43 -0700112
Eric Biggersb01531d2019-03-20 11:39:13 -0700113 if (err && err != -ENOENT)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700114 return err;
115
Eric Biggersb01531d2019-03-20 11:39:13 -0700116 if (fname->is_ciphertext_name) {
Eric Biggers32c3cf02017-10-09 12:15:43 -0700117 spin_lock(&dentry->d_lock);
Eric Biggers6cc24862019-03-20 11:39:09 -0700118 dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700119 spin_unlock(&dentry->d_lock);
Eric Biggersd456a332019-03-20 11:39:12 -0700120 d_set_d_op(dentry, &fscrypt_d_ops);
Eric Biggers32c3cf02017-10-09 12:15:43 -0700121 }
Eric Biggersb01531d2019-03-20 11:39:13 -0700122 return err;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700123}
124EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
Eric Biggers76e81d62018-01-05 10:45:01 -0800125
126int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
127 unsigned int max_len,
128 struct fscrypt_str *disk_link)
129{
130 int err;
131
132 /*
133 * To calculate the size of the encrypted symlink target we need to know
134 * the amount of NUL padding, which is determined by the flags set in
135 * the encryption policy which will be inherited from the directory.
136 * The easiest way to get access to this is to just load the directory's
137 * fscrypt_info, since we'll need it to create the dir_entry anyway.
138 *
139 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
140 */
141 err = fscrypt_get_encryption_info(dir);
142 if (err)
143 return err;
144 if (!fscrypt_has_encryption_key(dir))
145 return -ENOKEY;
146
147 /*
148 * Calculate the size of the encrypted symlink and verify it won't
149 * exceed max_len. Note that for historical reasons, encrypted symlink
150 * targets are prefixed with the ciphertext length, despite this
151 * actually being redundant with i_size. This decreases by 2 bytes the
152 * longest symlink target we can accept.
153 *
154 * We could recover 1 byte by not counting a null terminator, but
155 * counting it (even though it is meaningless for ciphertext) is simpler
156 * for now since filesystems will assume it is there and subtract it.
157 */
Eric Biggersb9db0b42018-01-11 23:30:08 -0500158 if (!fscrypt_fname_encrypted_size(dir, len,
159 max_len - sizeof(struct fscrypt_symlink_data),
160 &disk_link->len))
Eric Biggers76e81d62018-01-05 10:45:01 -0800161 return -ENAMETOOLONG;
Eric Biggersb9db0b42018-01-11 23:30:08 -0500162 disk_link->len += sizeof(struct fscrypt_symlink_data);
163
Eric Biggers76e81d62018-01-05 10:45:01 -0800164 disk_link->name = NULL;
165 return 0;
166}
167EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
168
169int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
170 unsigned int len, struct fscrypt_str *disk_link)
171{
172 int err;
Eric Biggers0b1dfa42018-01-19 13:45:24 -0800173 struct qstr iname = QSTR_INIT(target, len);
Eric Biggers76e81d62018-01-05 10:45:01 -0800174 struct fscrypt_symlink_data *sd;
175 unsigned int ciphertext_len;
Eric Biggers76e81d62018-01-05 10:45:01 -0800176
177 err = fscrypt_require_key(inode);
178 if (err)
179 return err;
180
181 if (disk_link->name) {
182 /* filesystem-provided buffer */
183 sd = (struct fscrypt_symlink_data *)disk_link->name;
184 } else {
185 sd = kmalloc(disk_link->len, GFP_NOFS);
186 if (!sd)
187 return -ENOMEM;
188 }
189 ciphertext_len = disk_link->len - sizeof(*sd);
190 sd->len = cpu_to_le16(ciphertext_len);
191
Eric Biggers50c961d2018-01-11 23:30:08 -0500192 err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
Eric Biggers2c58d542019-04-10 13:21:15 -0700193 if (err)
194 goto err_free_sd;
195
Eric Biggers76e81d62018-01-05 10:45:01 -0800196 /*
197 * Null-terminating the ciphertext doesn't make sense, but we still
198 * count the null terminator in the length, so we might as well
199 * initialize it just in case the filesystem writes it out.
200 */
201 sd->encrypted_path[ciphertext_len] = '\0';
202
Eric Biggers2c58d542019-04-10 13:21:15 -0700203 /* Cache the plaintext symlink target for later use by get_link() */
204 err = -ENOMEM;
205 inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
206 if (!inode->i_link)
207 goto err_free_sd;
208
Eric Biggers76e81d62018-01-05 10:45:01 -0800209 if (!disk_link->name)
210 disk_link->name = (unsigned char *)sd;
211 return 0;
Eric Biggers2c58d542019-04-10 13:21:15 -0700212
213err_free_sd:
214 if (!disk_link->name)
215 kfree(sd);
216 return err;
Eric Biggers76e81d62018-01-05 10:45:01 -0800217}
218EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
Eric Biggers3b0d8832018-01-05 10:45:02 -0800219
220/**
221 * fscrypt_get_symlink - get the target of an encrypted symlink
222 * @inode: the symlink inode
223 * @caddr: the on-disk contents of the symlink
224 * @max_size: size of @caddr buffer
Eric Biggers2c58d542019-04-10 13:21:15 -0700225 * @done: if successful, will be set up to free the returned target if needed
Eric Biggers3b0d8832018-01-05 10:45:02 -0800226 *
227 * If the symlink's encryption key is available, we decrypt its target.
228 * Otherwise, we encode its target for presentation.
229 *
230 * This may sleep, so the filesystem must have dropped out of RCU mode already.
231 *
232 * Return: the presentable symlink target or an ERR_PTR()
233 */
234const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
235 unsigned int max_size,
236 struct delayed_call *done)
237{
238 const struct fscrypt_symlink_data *sd;
239 struct fscrypt_str cstr, pstr;
Eric Biggers2c58d542019-04-10 13:21:15 -0700240 bool has_key;
Eric Biggers3b0d8832018-01-05 10:45:02 -0800241 int err;
242
243 /* This is for encrypted symlinks only */
244 if (WARN_ON(!IS_ENCRYPTED(inode)))
245 return ERR_PTR(-EINVAL);
246
Eric Biggers2c58d542019-04-10 13:21:15 -0700247 /* If the decrypted target is already cached, just return it. */
248 pstr.name = READ_ONCE(inode->i_link);
249 if (pstr.name)
250 return pstr.name;
251
Eric Biggers3b0d8832018-01-05 10:45:02 -0800252 /*
253 * Try to set up the symlink's encryption key, but we can continue
254 * regardless of whether the key is available or not.
255 */
256 err = fscrypt_get_encryption_info(inode);
257 if (err)
258 return ERR_PTR(err);
Eric Biggers2c58d542019-04-10 13:21:15 -0700259 has_key = fscrypt_has_encryption_key(inode);
Eric Biggers3b0d8832018-01-05 10:45:02 -0800260
261 /*
262 * For historical reasons, encrypted symlink targets are prefixed with
263 * the ciphertext length, even though this is redundant with i_size.
264 */
265
266 if (max_size < sizeof(*sd))
267 return ERR_PTR(-EUCLEAN);
268 sd = caddr;
269 cstr.name = (unsigned char *)sd->encrypted_path;
270 cstr.len = le16_to_cpu(sd->len);
271
272 if (cstr.len == 0)
273 return ERR_PTR(-EUCLEAN);
274
275 if (cstr.len + sizeof(*sd) - 1 > max_size)
276 return ERR_PTR(-EUCLEAN);
277
278 err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
279 if (err)
280 return ERR_PTR(err);
281
282 err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
283 if (err)
284 goto err_kfree;
285
286 err = -EUCLEAN;
287 if (pstr.name[0] == '\0')
288 goto err_kfree;
289
290 pstr.name[pstr.len] = '\0';
Eric Biggers2c58d542019-04-10 13:21:15 -0700291
292 /*
293 * Cache decrypted symlink targets in i_link for later use. Don't cache
294 * symlink targets encoded without the key, since those become outdated
295 * once the key is added. This pairs with the READ_ONCE() above and in
296 * the VFS path lookup code.
297 */
298 if (!has_key ||
299 cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
300 set_delayed_call(done, kfree_link, pstr.name);
301
Eric Biggers3b0d8832018-01-05 10:45:02 -0800302 return pstr.name;
303
304err_kfree:
305 kfree(pstr.name);
306 return ERR_PTR(err);
307}
308EXPORT_SYMBOL_GPL(fscrypt_get_symlink);