1
2
3
4
5
6
7
8#include <linux/key.h>
9
10#include "fscrypt_private.h"
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32int fscrypt_file_open(struct inode *inode, struct file *filp)
33{
34 int err;
35 struct dentry *dir;
36
37 err = fscrypt_require_key(inode);
38 if (err)
39 return err;
40
41 dir = dget_parent(file_dentry(filp));
42 if (IS_ENCRYPTED(d_inode(dir)) &&
43 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
44 fscrypt_warn(inode,
45 "Inconsistent encryption context (parent directory: %lu)",
46 d_inode(dir)->i_ino);
47 err = -EPERM;
48 }
49 dput(dir);
50 return err;
51}
52EXPORT_SYMBOL_GPL(fscrypt_file_open);
53
54int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
55 struct dentry *dentry)
56{
57 if (fscrypt_is_nokey_name(dentry))
58 return -ENOKEY;
59
60
61
62
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 if (fscrypt_is_nokey_name(old_dentry) ||
76 fscrypt_is_nokey_name(new_dentry))
77 return -ENOKEY;
78
79
80
81
82
83 if (old_dir != new_dir) {
84 if (IS_ENCRYPTED(new_dir) &&
85 !fscrypt_has_permitted_context(new_dir,
86 d_inode(old_dentry)))
87 return -EXDEV;
88
89 if ((flags & RENAME_EXCHANGE) &&
90 IS_ENCRYPTED(old_dir) &&
91 !fscrypt_has_permitted_context(old_dir,
92 d_inode(new_dentry)))
93 return -EXDEV;
94 }
95 return 0;
96}
97EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
98
99int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
100 struct fscrypt_name *fname)
101{
102 int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
103
104 if (err && err != -ENOENT)
105 return err;
106
107 if (fname->is_nokey_name) {
108 spin_lock(&dentry->d_lock);
109 dentry->d_flags |= DCACHE_NOKEY_NAME;
110 spin_unlock(&dentry->d_lock);
111 }
112 return err;
113}
114EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
115
116int __fscrypt_prepare_readdir(struct inode *dir)
117{
118 return fscrypt_get_encryption_info(dir, true);
119}
120EXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir);
121
122int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr)
123{
124 if (attr->ia_valid & ATTR_SIZE)
125 return fscrypt_require_key(d_inode(dentry));
126 return 0;
127}
128EXPORT_SYMBOL_GPL(__fscrypt_prepare_setattr);
129
130
131
132
133
134
135
136
137
138
139
140
141int fscrypt_prepare_setflags(struct inode *inode,
142 unsigned int oldflags, unsigned int flags)
143{
144 struct fscrypt_info *ci;
145 struct key *key;
146 struct fscrypt_master_key *mk;
147 int err;
148
149
150
151
152
153
154 if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
155 err = fscrypt_require_key(inode);
156 if (err)
157 return err;
158 ci = inode->i_crypt_info;
159 if (ci->ci_policy.version != FSCRYPT_POLICY_V2)
160 return -EINVAL;
161 key = ci->ci_master_key;
162 mk = key->payload.data[0];
163 down_read(&key->sem);
164 if (is_master_key_secret_present(&mk->mk_secret))
165 err = fscrypt_derive_dirhash_key(ci, mk);
166 else
167 err = -ENOKEY;
168 up_read(&key->sem);
169 return err;
170 }
171 return 0;
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197int fscrypt_prepare_symlink(struct inode *dir, const char *target,
198 unsigned int len, unsigned int max_len,
199 struct fscrypt_str *disk_link)
200{
201 const union fscrypt_policy *policy;
202
203
204
205
206
207
208 policy = fscrypt_policy_to_inherit(dir);
209 if (policy == NULL) {
210
211 disk_link->name = (unsigned char *)target;
212 disk_link->len = len + 1;
213 if (disk_link->len > max_len)
214 return -ENAMETOOLONG;
215 return 0;
216 }
217 if (IS_ERR(policy))
218 return PTR_ERR(policy);
219
220
221
222
223
224
225
226
227
228
229
230
231 if (!fscrypt_fname_encrypted_size(policy, len,
232 max_len - sizeof(struct fscrypt_symlink_data),
233 &disk_link->len))
234 return -ENAMETOOLONG;
235 disk_link->len += sizeof(struct fscrypt_symlink_data);
236
237 disk_link->name = NULL;
238 return 0;
239}
240EXPORT_SYMBOL_GPL(fscrypt_prepare_symlink);
241
242int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
243 unsigned int len, struct fscrypt_str *disk_link)
244{
245 int err;
246 struct qstr iname = QSTR_INIT(target, len);
247 struct fscrypt_symlink_data *sd;
248 unsigned int ciphertext_len;
249
250
251
252
253
254
255 if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
256 return -ENOKEY;
257
258 if (disk_link->name) {
259
260 sd = (struct fscrypt_symlink_data *)disk_link->name;
261 } else {
262 sd = kmalloc(disk_link->len, GFP_NOFS);
263 if (!sd)
264 return -ENOMEM;
265 }
266 ciphertext_len = disk_link->len - sizeof(*sd);
267 sd->len = cpu_to_le16(ciphertext_len);
268
269 err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
270 ciphertext_len);
271 if (err)
272 goto err_free_sd;
273
274
275
276
277
278
279 sd->encrypted_path[ciphertext_len] = '\0';
280
281
282 err = -ENOMEM;
283 inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
284 if (!inode->i_link)
285 goto err_free_sd;
286
287 if (!disk_link->name)
288 disk_link->name = (unsigned char *)sd;
289 return 0;
290
291err_free_sd:
292 if (!disk_link->name)
293 kfree(sd);
294 return err;
295}
296EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
313 unsigned int max_size,
314 struct delayed_call *done)
315{
316 const struct fscrypt_symlink_data *sd;
317 struct fscrypt_str cstr, pstr;
318 bool has_key;
319 int err;
320
321
322 if (WARN_ON(!IS_ENCRYPTED(inode)))
323 return ERR_PTR(-EINVAL);
324
325
326 pstr.name = READ_ONCE(inode->i_link);
327 if (pstr.name)
328 return pstr.name;
329
330
331
332
333
334 err = fscrypt_get_encryption_info(inode, false);
335 if (err)
336 return ERR_PTR(err);
337 has_key = fscrypt_has_encryption_key(inode);
338
339
340
341
342
343
344 if (max_size < sizeof(*sd))
345 return ERR_PTR(-EUCLEAN);
346 sd = caddr;
347 cstr.name = (unsigned char *)sd->encrypted_path;
348 cstr.len = le16_to_cpu(sd->len);
349
350 if (cstr.len == 0)
351 return ERR_PTR(-EUCLEAN);
352
353 if (cstr.len + sizeof(*sd) - 1 > max_size)
354 return ERR_PTR(-EUCLEAN);
355
356 err = fscrypt_fname_alloc_buffer(cstr.len, &pstr);
357 if (err)
358 return ERR_PTR(err);
359
360 err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
361 if (err)
362 goto err_kfree;
363
364 err = -EUCLEAN;
365 if (pstr.name[0] == '\0')
366 goto err_kfree;
367
368 pstr.name[pstr.len] = '\0';
369
370
371
372
373
374
375
376 if (!has_key ||
377 cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
378 set_delayed_call(done, kfree_link, pstr.name);
379
380 return pstr.name;
381
382err_kfree:
383 kfree(pstr.name);
384 return ERR_PTR(err);
385}
386EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat)
409{
410 struct dentry *dentry = path->dentry;
411 struct inode *inode = d_inode(dentry);
412 const char *link;
413 DEFINE_DELAYED_CALL(done);
414
415
416
417
418
419
420 link = READ_ONCE(inode->i_link);
421 if (!link) {
422 link = inode->i_op->get_link(dentry, inode, &done);
423 if (IS_ERR(link))
424 return PTR_ERR(link);
425 }
426 stat->size = strlen(link);
427 do_delayed_call(&done);
428 return 0;
429}
430EXPORT_SYMBOL_GPL(fscrypt_symlink_getattr);
431