blob: 30b1ca66124920a23a2e226e853406045f3bfba3 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * fs/crypto/hooks.c
3 *
4 * Encryption hooks for higher-level filesystem operations.
5 */
6
7#include "fscrypt_private.h"
8
9/**
10 * fscrypt_file_open - prepare to open a possibly-encrypted regular file
11 * @inode: the inode being opened
12 * @filp: the struct file being set up
13 *
14 * Currently, an encrypted regular file can only be opened if its encryption key
15 * is available; access to the raw encrypted contents is not supported.
16 * Therefore, we first set up the inode's encryption key (if not already done)
17 * and return an error if it's unavailable.
18 *
19 * We also verify that if the parent directory (from the path via which the file
20 * is being opened) is encrypted, then the inode being opened uses the same
21 * encryption policy. This is needed as part of the enforcement that all files
22 * in an encrypted directory tree use the same encryption policy, as a
23 * protection against certain types of offline attacks. Note that this check is
24 * needed even when opening an *unencrypted* file, since it's forbidden to have
25 * an unencrypted file in an encrypted directory.
26 *
27 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
28 */
29int fscrypt_file_open(struct inode *inode, struct file *filp)
30{
31 int err;
32 struct dentry *dir;
33
34 err = fscrypt_require_key(inode);
35 if (err)
36 return err;
37
38 dir = dget_parent(file_dentry(filp));
39 if (IS_ENCRYPTED(d_inode(dir)) &&
40 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
41 fscrypt_warn(inode,
42 "Inconsistent encryption context (parent directory: %lu)",
43 d_inode(dir)->i_ino);
44 err = -EPERM;
45 }
46 dput(dir);
47 return err;
48}
49EXPORT_SYMBOL_GPL(fscrypt_file_open);
50
51int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
52 struct dentry *dentry)
53{
54 int err;
55
56 err = fscrypt_require_key(dir);
57 if (err)
58 return err;
59
60 /* ... in case we looked up ciphertext name before key was added */
61 if (dentry->d_flags & DCACHE_ENCRYPTED_NAME)
62 return -ENOKEY;
63
64 if (!fscrypt_has_permitted_context(dir, inode))
65 return -EXDEV;
66
67 return 0;
68}
69EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
70
71int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
72 struct inode *new_dir, struct dentry *new_dentry,
73 unsigned int flags)
74{
75 int err;
76
77 err = fscrypt_require_key(old_dir);
78 if (err)
79 return err;
80
81 err = fscrypt_require_key(new_dir);
82 if (err)
83 return err;
84
85 /* ... in case we looked up ciphertext name(s) before key was added */
86 if ((old_dentry->d_flags | new_dentry->d_flags) &
87 DCACHE_ENCRYPTED_NAME)
88 return -ENOKEY;
89
90 if (old_dir != new_dir) {
91 if (IS_ENCRYPTED(new_dir) &&
92 !fscrypt_has_permitted_context(new_dir,
93 d_inode(old_dentry)))
94 return -EXDEV;
95
96 if ((flags & RENAME_EXCHANGE) &&
97 IS_ENCRYPTED(old_dir) &&
98 !fscrypt_has_permitted_context(old_dir,
99 d_inode(new_dentry)))
100 return -EXDEV;
101 }
102 return 0;
103}
104EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
105
106int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
107 struct fscrypt_name *fname)
108{
109 int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
110
111 if (err && err != -ENOENT)
112 return err;
113
114 if (fname->is_ciphertext_name) {
115 spin_lock(&dentry->d_lock);
116 dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
117 spin_unlock(&dentry->d_lock);
118 d_set_d_op(dentry, &fscrypt_d_ops);
119 }
120 return err;
121}
122EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
123
124int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
125 unsigned int max_len,
126 struct fscrypt_str *disk_link)
127{
128 int err;
129
130 /*
131 * To calculate the size of the encrypted symlink target we need to know
132 * the amount of NUL padding, which is determined by the flags set in
133 * the encryption policy which will be inherited from the directory.
134 * The easiest way to get access to this is to just load the directory's
135 * fscrypt_info, since we'll need it to create the dir_entry anyway.
136 *
137 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
138 */
139 err = fscrypt_get_encryption_info(dir);
140 if (err)
141 return err;
142 if (!fscrypt_has_encryption_key(dir))
143 return -ENOKEY;
144
145 /*
146 * Calculate the size of the encrypted symlink and verify it won't
147 * exceed max_len. Note that for historical reasons, encrypted symlink
148 * targets are prefixed with the ciphertext length, despite this
149 * actually being redundant with i_size. This decreases by 2 bytes the
150 * longest symlink target we can accept.
151 *
152 * We could recover 1 byte by not counting a null terminator, but
153 * counting it (even though it is meaningless for ciphertext) is simpler
154 * for now since filesystems will assume it is there and subtract it.
155 */
156 if (!fscrypt_fname_encrypted_size(dir, len,
157 max_len - sizeof(struct fscrypt_symlink_data),
158 &disk_link->len))
159 return -ENAMETOOLONG;
160 disk_link->len += sizeof(struct fscrypt_symlink_data);
161
162 disk_link->name = NULL;
163 return 0;
164}
165EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
166
167int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
168 unsigned int len, struct fscrypt_str *disk_link)
169{
170 int err;
171 struct qstr iname = QSTR_INIT(target, len);
172 struct fscrypt_symlink_data *sd;
173 unsigned int ciphertext_len;
174
175 err = fscrypt_require_key(inode);
176 if (err)
177 return err;
178
179 if (disk_link->name) {
180 /* filesystem-provided buffer */
181 sd = (struct fscrypt_symlink_data *)disk_link->name;
182 } else {
183 sd = kmalloc(disk_link->len, GFP_NOFS);
184 if (!sd)
185 return -ENOMEM;
186 }
187 ciphertext_len = disk_link->len - sizeof(*sd);
188 sd->len = cpu_to_le16(ciphertext_len);
189
190 err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
191 if (err)
192 goto err_free_sd;
193
194 /*
195 * Null-terminating the ciphertext doesn't make sense, but we still
196 * count the null terminator in the length, so we might as well
197 * initialize it just in case the filesystem writes it out.
198 */
199 sd->encrypted_path[ciphertext_len] = '\0';
200
201 /* Cache the plaintext symlink target for later use by get_link() */
202 err = -ENOMEM;
203 inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
204 if (!inode->i_link)
205 goto err_free_sd;
206
207 if (!disk_link->name)
208 disk_link->name = (unsigned char *)sd;
209 return 0;
210
211err_free_sd:
212 if (!disk_link->name)
213 kfree(sd);
214 return err;
215}
216EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
217
218/**
219 * fscrypt_get_symlink - get the target of an encrypted symlink
220 * @inode: the symlink inode
221 * @caddr: the on-disk contents of the symlink
222 * @max_size: size of @caddr buffer
223 * @done: if successful, will be set up to free the returned target if needed
224 *
225 * If the symlink's encryption key is available, we decrypt its target.
226 * Otherwise, we encode its target for presentation.
227 *
228 * This may sleep, so the filesystem must have dropped out of RCU mode already.
229 *
230 * Return: the presentable symlink target or an ERR_PTR()
231 */
232const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
233 unsigned int max_size,
234 struct delayed_call *done)
235{
236 const struct fscrypt_symlink_data *sd;
237 struct fscrypt_str cstr, pstr;
238 bool has_key;
239 int err;
240
241 /* This is for encrypted symlinks only */
242 if (WARN_ON(!IS_ENCRYPTED(inode)))
243 return ERR_PTR(-EINVAL);
244
245 /* If the decrypted target is already cached, just return it. */
246 pstr.name = READ_ONCE(inode->i_link);
247 if (pstr.name)
248 return pstr.name;
249
250 /*
251 * Try to set up the symlink's encryption key, but we can continue
252 * regardless of whether the key is available or not.
253 */
254 err = fscrypt_get_encryption_info(inode);
255 if (err)
256 return ERR_PTR(err);
257 has_key = fscrypt_has_encryption_key(inode);
258
259 /*
260 * For historical reasons, encrypted symlink targets are prefixed with
261 * the ciphertext length, even though this is redundant with i_size.
262 */
263
264 if (max_size < sizeof(*sd))
265 return ERR_PTR(-EUCLEAN);
266 sd = caddr;
267 cstr.name = (unsigned char *)sd->encrypted_path;
268 cstr.len = le16_to_cpu(sd->len);
269
270 if (cstr.len == 0)
271 return ERR_PTR(-EUCLEAN);
272
273 if (cstr.len + sizeof(*sd) - 1 > max_size)
274 return ERR_PTR(-EUCLEAN);
275
276 err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
277 if (err)
278 return ERR_PTR(err);
279
280 err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
281 if (err)
282 goto err_kfree;
283
284 err = -EUCLEAN;
285 if (pstr.name[0] == '\0')
286 goto err_kfree;
287
288 pstr.name[pstr.len] = '\0';
289
290 /*
291 * Cache decrypted symlink targets in i_link for later use. Don't cache
292 * symlink targets encoded without the key, since those become outdated
293 * once the key is added. This pairs with the READ_ONCE() above and in
294 * the VFS path lookup code.
295 */
296 if (!has_key ||
297 cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
298 set_delayed_call(done, kfree_link, pstr.name);
299
300 return pstr.name;
301
302err_kfree:
303 kfree(pstr.name);
304 return ERR_PTR(err);
305}
306EXPORT_SYMBOL_GPL(fscrypt_get_symlink);