blob: d016fa384d607ee81eae1a42a03353b3a11f2737 [file] [log] [blame]
Linus Torvalds32190f02017-11-14 11:35:15 -08001/* SPDX-License-Identifier: GPL-2.0 */
Eric Biggers46f47e42017-01-24 10:58:06 -08002/*
Dave Chinner734f0d22017-10-09 12:15:34 -07003 * fscrypt.h: declarations for per-file encryption
4 *
Chandan Rajendra643fa962018-12-12 15:20:12 +05305 * Filesystems that implement per-file encryption must include this header
6 * file.
Eric Biggers46f47e42017-01-24 10:58:06 -08007 *
8 * Copyright (C) 2015, Google, Inc.
9 *
10 * Written by Michael Halcrow, 2015.
11 * Modified by Jaegeuk Kim, 2015.
12 */
Dave Chinner734f0d22017-10-09 12:15:34 -070013#ifndef _LINUX_FSCRYPT_H
14#define _LINUX_FSCRYPT_H
Eric Biggers46f47e42017-01-24 10:58:06 -080015
Eric Biggers46f47e42017-01-24 10:58:06 -080016#include <linux/fs.h>
Chandan Rajendra643fa962018-12-12 15:20:12 +053017#include <linux/mm.h>
18#include <linux/slab.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080019
20#define FS_CRYPTO_BLOCK_SIZE 16
21
Eric Biggers542060c2018-01-05 10:44:55 -080022struct fscrypt_ctx;
Eric Biggers46f47e42017-01-24 10:58:06 -080023struct fscrypt_info;
24
Eric Biggers46f47e42017-01-24 10:58:06 -080025struct fscrypt_str {
26 unsigned char *name;
27 u32 len;
28};
29
30struct fscrypt_name {
31 const struct qstr *usr_fname;
32 struct fscrypt_str disk_name;
33 u32 hash;
34 u32 minor_hash;
35 struct fscrypt_str crypto_buf;
Eric Biggersb01531d2019-03-20 11:39:13 -070036 bool is_ciphertext_name;
Eric Biggers46f47e42017-01-24 10:58:06 -080037};
38
39#define FSTR_INIT(n, l) { .name = n, .len = l }
40#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
41#define fname_name(p) ((p)->disk_name.name)
42#define fname_len(p) ((p)->disk_name.len)
43
Tahsin Erdoganaf652072017-07-06 00:01:59 -040044/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
45#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
46
Chandan Rajendra643fa962018-12-12 15:20:12 +053047#ifdef CONFIG_FS_ENCRYPTION
48/*
49 * fscrypt superblock flags
50 */
51#define FS_CFLG_OWN_PAGES (1U << 1)
52
53/*
54 * crypto operations for filesystems
55 */
56struct fscrypt_operations {
57 unsigned int flags;
58 const char *key_prefix;
59 int (*get_context)(struct inode *, void *, size_t);
60 int (*set_context)(struct inode *, const void *, size_t, void *);
61 bool (*dummy_context)(struct inode *);
62 bool (*empty_dir)(struct inode *);
63 unsigned int max_namelen;
64};
65
66struct fscrypt_ctx {
67 union {
68 struct {
69 struct page *bounce_page; /* Ciphertext page */
70 struct page *control_page; /* Original page */
71 } w;
72 struct {
73 struct bio *bio;
74 struct work_struct work;
75 } r;
76 struct list_head free_list; /* Free list */
77 };
78 u8 flags; /* Flags */
79};
80
81static inline bool fscrypt_has_encryption_key(const struct inode *inode)
82{
Eric Biggerse37a7842019-04-11 14:32:15 -070083 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
84 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +053085}
86
87static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
88{
89 return inode->i_sb->s_cop->dummy_context &&
90 inode->i_sb->s_cop->dummy_context(inode);
91}
92
Eric Biggers0bf3d5c12019-03-20 11:39:11 -070093/*
94 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
95 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
96 * must be cleared. Note that we don't have to support arbitrary moves of this
97 * flag because fscrypt doesn't allow encrypted aliases to be the source or
98 * target of a rename().
99 */
100static inline void fscrypt_handle_d_move(struct dentry *dentry)
101{
102 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
103}
104
Chandan Rajendra643fa962018-12-12 15:20:12 +0530105/* crypto.c */
106extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggerscd0265f2019-03-18 10:23:33 -0700107extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530108extern void fscrypt_release_ctx(struct fscrypt_ctx *);
109extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
110 unsigned int, unsigned int,
111 u64, gfp_t);
112extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
113 unsigned int, u64);
114
Eric Biggersd2d07272019-05-20 09:29:39 -0700115static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530116{
Eric Biggersd2d07272019-05-20 09:29:39 -0700117 return page->mapping == NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530118}
119
Eric Biggersd2d07272019-05-20 09:29:39 -0700120static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
121{
122 return (struct page *)page_private(bounce_page);
123}
124
125extern void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530126
127/* policy.c */
128extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
129extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
130extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
131extern int fscrypt_inherit_context(struct inode *, struct inode *,
132 void *, bool);
133/* keyinfo.c */
134extern int fscrypt_get_encryption_info(struct inode *);
135extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggers2c58d542019-04-10 13:21:15 -0700136extern void fscrypt_free_inode(struct inode *);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530137
138/* fname.c */
139extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
140 int lookup, struct fscrypt_name *);
141
142static inline void fscrypt_free_filename(struct fscrypt_name *fname)
143{
144 kfree(fname->crypto_buf.name);
145}
146
147extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
148 struct fscrypt_str *);
149extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
150extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
151 const struct fscrypt_str *, struct fscrypt_str *);
152
153#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
154
155/* Extracts the second-to-last ciphertext block; see explanation below */
156#define FSCRYPT_FNAME_DIGEST(name, len) \
157 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
158 FS_CRYPTO_BLOCK_SIZE))
159
160#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
161
162/**
163 * fscrypt_digested_name - alternate identifier for an on-disk filename
164 *
165 * When userspace lists an encrypted directory without access to the key,
166 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
167 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
168 * full ciphertext (base64-encoded). This is necessary to allow supporting
169 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
170 *
171 * To make it possible for filesystems to still find the correct directory entry
172 * despite not knowing the full on-disk name, we encode any filesystem-specific
173 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
174 * followed by the second-to-last ciphertext block of the filename. Due to the
175 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
176 * depends on the full plaintext. (Note that ciphertext stealing causes the
177 * last two blocks to appear "flipped".) This makes accidental collisions very
178 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
179 * share the same filesystem-specific hashes.
180 *
181 * However, this scheme isn't immune to intentional collisions, which can be
182 * created by anyone able to create arbitrary plaintext filenames and view them
183 * without the key. Making the "digest" be a real cryptographic hash like
184 * SHA-256 over the full ciphertext would prevent this, although it would be
185 * less efficient and harder to implement, especially since the filesystem would
186 * need to calculate it for each directory entry examined during a search.
187 */
188struct fscrypt_digested_name {
189 u32 hash;
190 u32 minor_hash;
191 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
192};
193
194/**
195 * fscrypt_match_name() - test whether the given name matches a directory entry
196 * @fname: the name being searched for
197 * @de_name: the name from the directory entry
198 * @de_name_len: the length of @de_name in bytes
199 *
200 * Normally @fname->disk_name will be set, and in that case we simply compare
201 * that to the name stored in the directory entry. The only exception is that
202 * if we don't have the key for an encrypted directory and a filename in it is
203 * very long, then we won't have the full disk_name and we'll instead need to
204 * match against the fscrypt_digested_name.
205 *
206 * Return: %true if the name matches, otherwise %false.
207 */
208static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
209 const u8 *de_name, u32 de_name_len)
210{
211 if (unlikely(!fname->disk_name.name)) {
212 const struct fscrypt_digested_name *n =
213 (const void *)fname->crypto_buf.name;
214 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
215 return false;
216 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
217 return false;
218 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
219 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
220 }
221
222 if (de_name_len != fname->disk_name.len)
223 return false;
224 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
225}
226
227/* bio.c */
228extern void fscrypt_decrypt_bio(struct bio *);
229extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
230 struct bio *bio);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530231extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
232 unsigned int);
233
234/* hooks.c */
235extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers968dd6d2019-03-20 11:39:10 -0700236extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
237 struct dentry *dentry);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530238extern int __fscrypt_prepare_rename(struct inode *old_dir,
239 struct dentry *old_dentry,
240 struct inode *new_dir,
241 struct dentry *new_dentry,
242 unsigned int flags);
Eric Biggersb01531d2019-03-20 11:39:13 -0700243extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
244 struct fscrypt_name *fname);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530245extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
246 unsigned int max_len,
247 struct fscrypt_str *disk_link);
248extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
249 unsigned int len,
250 struct fscrypt_str *disk_link);
251extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
252 unsigned int max_size,
253 struct delayed_call *done);
Sascha Hauereea2c052019-03-26 08:52:31 +0100254static inline void fscrypt_set_ops(struct super_block *sb,
255 const struct fscrypt_operations *s_cop)
256{
257 sb->s_cop = s_cop;
258}
Chandan Rajendra643fa962018-12-12 15:20:12 +0530259#else /* !CONFIG_FS_ENCRYPTION */
260
261static inline bool fscrypt_has_encryption_key(const struct inode *inode)
262{
263 return false;
264}
265
266static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
267{
268 return false;
269}
270
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700271static inline void fscrypt_handle_d_move(struct dentry *dentry)
272{
273}
274
Chandan Rajendra643fa962018-12-12 15:20:12 +0530275/* crypto.c */
276static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
277{
278}
279
Eric Biggerscd0265f2019-03-18 10:23:33 -0700280static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530281{
282 return ERR_PTR(-EOPNOTSUPP);
283}
284
285static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
286{
287 return;
288}
289
290static inline struct page *fscrypt_encrypt_page(const struct inode *inode,
291 struct page *page,
292 unsigned int len,
293 unsigned int offs,
294 u64 lblk_num, gfp_t gfp_flags)
295{
296 return ERR_PTR(-EOPNOTSUPP);
297}
298
299static inline int fscrypt_decrypt_page(const struct inode *inode,
300 struct page *page,
301 unsigned int len, unsigned int offs,
302 u64 lblk_num)
303{
304 return -EOPNOTSUPP;
305}
306
Eric Biggersd2d07272019-05-20 09:29:39 -0700307static inline bool fscrypt_is_bounce_page(struct page *page)
308{
309 return false;
310}
311
312static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530313{
314 WARN_ON_ONCE(1);
315 return ERR_PTR(-EINVAL);
316}
317
Eric Biggersd2d07272019-05-20 09:29:39 -0700318static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530319{
Chandan Rajendra643fa962018-12-12 15:20:12 +0530320}
321
322/* policy.c */
323static inline int fscrypt_ioctl_set_policy(struct file *filp,
324 const void __user *arg)
325{
326 return -EOPNOTSUPP;
327}
328
329static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
330{
331 return -EOPNOTSUPP;
332}
333
334static inline int fscrypt_has_permitted_context(struct inode *parent,
335 struct inode *child)
336{
337 return 0;
338}
339
340static inline int fscrypt_inherit_context(struct inode *parent,
341 struct inode *child,
342 void *fs_data, bool preload)
343{
344 return -EOPNOTSUPP;
345}
346
347/* keyinfo.c */
348static inline int fscrypt_get_encryption_info(struct inode *inode)
349{
350 return -EOPNOTSUPP;
351}
352
353static inline void fscrypt_put_encryption_info(struct inode *inode)
354{
355 return;
356}
357
Eric Biggers2c58d542019-04-10 13:21:15 -0700358static inline void fscrypt_free_inode(struct inode *inode)
359{
360}
361
Chandan Rajendra643fa962018-12-12 15:20:12 +0530362 /* fname.c */
363static inline int fscrypt_setup_filename(struct inode *dir,
364 const struct qstr *iname,
365 int lookup, struct fscrypt_name *fname)
366{
367 if (IS_ENCRYPTED(dir))
368 return -EOPNOTSUPP;
369
Eric Biggersb01531d2019-03-20 11:39:13 -0700370 memset(fname, 0, sizeof(*fname));
Chandan Rajendra643fa962018-12-12 15:20:12 +0530371 fname->usr_fname = iname;
372 fname->disk_name.name = (unsigned char *)iname->name;
373 fname->disk_name.len = iname->len;
374 return 0;
375}
376
377static inline void fscrypt_free_filename(struct fscrypt_name *fname)
378{
379 return;
380}
381
382static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
383 u32 max_encrypted_len,
384 struct fscrypt_str *crypto_str)
385{
386 return -EOPNOTSUPP;
387}
388
389static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
390{
391 return;
392}
393
394static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
395 u32 hash, u32 minor_hash,
396 const struct fscrypt_str *iname,
397 struct fscrypt_str *oname)
398{
399 return -EOPNOTSUPP;
400}
401
402static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
403 const u8 *de_name, u32 de_name_len)
404{
405 /* Encryption support disabled; use standard comparison */
406 if (de_name_len != fname->disk_name.len)
407 return false;
408 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
409}
410
411/* bio.c */
412static inline void fscrypt_decrypt_bio(struct bio *bio)
413{
414}
415
416static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
417 struct bio *bio)
418{
419}
420
Chandan Rajendra643fa962018-12-12 15:20:12 +0530421static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
422 sector_t pblk, unsigned int len)
423{
424 return -EOPNOTSUPP;
425}
426
427/* hooks.c */
428
429static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
430{
431 if (IS_ENCRYPTED(inode))
432 return -EOPNOTSUPP;
433 return 0;
434}
435
Eric Biggers968dd6d2019-03-20 11:39:10 -0700436static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
437 struct dentry *dentry)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530438{
439 return -EOPNOTSUPP;
440}
441
442static inline int __fscrypt_prepare_rename(struct inode *old_dir,
443 struct dentry *old_dentry,
444 struct inode *new_dir,
445 struct dentry *new_dentry,
446 unsigned int flags)
447{
448 return -EOPNOTSUPP;
449}
450
451static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggersb01531d2019-03-20 11:39:13 -0700452 struct dentry *dentry,
453 struct fscrypt_name *fname)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530454{
455 return -EOPNOTSUPP;
456}
457
458static inline int __fscrypt_prepare_symlink(struct inode *dir,
459 unsigned int len,
460 unsigned int max_len,
461 struct fscrypt_str *disk_link)
462{
463 return -EOPNOTSUPP;
464}
465
466
467static inline int __fscrypt_encrypt_symlink(struct inode *inode,
468 const char *target,
469 unsigned int len,
470 struct fscrypt_str *disk_link)
471{
472 return -EOPNOTSUPP;
473}
474
475static inline const char *fscrypt_get_symlink(struct inode *inode,
476 const void *caddr,
477 unsigned int max_size,
478 struct delayed_call *done)
479{
480 return ERR_PTR(-EOPNOTSUPP);
481}
Sascha Hauereea2c052019-03-26 08:52:31 +0100482
483static inline void fscrypt_set_ops(struct super_block *sb,
484 const struct fscrypt_operations *s_cop)
485{
486}
487
Chandan Rajendra643fa962018-12-12 15:20:12 +0530488#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700489
Eric Biggersd293c3e2017-10-09 12:15:39 -0700490/**
491 * fscrypt_require_key - require an inode's encryption key
492 * @inode: the inode we need the key for
493 *
494 * If the inode is encrypted, set up its encryption key if not already done.
495 * Then require that the key be present and return -ENOKEY otherwise.
496 *
497 * No locks are needed, and the key will live as long as the struct inode --- so
498 * it won't go away from under you.
499 *
500 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
501 * if a problem occurred while setting up the encryption key.
502 */
503static inline int fscrypt_require_key(struct inode *inode)
504{
505 if (IS_ENCRYPTED(inode)) {
506 int err = fscrypt_get_encryption_info(inode);
507
508 if (err)
509 return err;
510 if (!fscrypt_has_encryption_key(inode))
511 return -ENOKEY;
512 }
513 return 0;
514}
Dave Chinner734f0d22017-10-09 12:15:34 -0700515
Eric Biggers0ea87a92017-10-09 12:15:41 -0700516/**
517 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
518 * @old_dentry: an existing dentry for the inode being linked
519 * @dir: the target directory
520 * @dentry: negative dentry for the target filename
521 *
522 * A new link can only be added to an encrypted directory if the directory's
523 * encryption key is available --- since otherwise we'd have no way to encrypt
524 * the filename. Therefore, we first set up the directory's encryption key (if
525 * not already done) and return an error if it's unavailable.
526 *
527 * We also verify that the link will not violate the constraint that all files
528 * in an encrypted directory tree use the same encryption policy.
529 *
530 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800531 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700532 * another -errno code.
533 */
534static inline int fscrypt_prepare_link(struct dentry *old_dentry,
535 struct inode *dir,
536 struct dentry *dentry)
537{
538 if (IS_ENCRYPTED(dir))
Eric Biggers968dd6d2019-03-20 11:39:10 -0700539 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700540 return 0;
541}
542
Eric Biggers94b26f32017-10-09 12:15:42 -0700543/**
544 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
545 * @old_dir: source directory
546 * @old_dentry: dentry for source file
547 * @new_dir: target directory
548 * @new_dentry: dentry for target location (may be negative unless exchanging)
549 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
550 *
551 * Prepare for ->rename() where the source and/or target directories may be
552 * encrypted. A new link can only be added to an encrypted directory if the
553 * directory's encryption key is available --- since otherwise we'd have no way
554 * to encrypt the filename. A rename to an existing name, on the other hand,
555 * *is* cryptographically possible without the key. However, we take the more
556 * conservative approach and just forbid all no-key renames.
557 *
558 * We also verify that the rename will not violate the constraint that all files
559 * in an encrypted directory tree use the same encryption policy.
560 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800561 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700562 * rename would cause inconsistent encryption policies, or another -errno code.
563 */
564static inline int fscrypt_prepare_rename(struct inode *old_dir,
565 struct dentry *old_dentry,
566 struct inode *new_dir,
567 struct dentry *new_dentry,
568 unsigned int flags)
569{
570 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
571 return __fscrypt_prepare_rename(old_dir, old_dentry,
572 new_dir, new_dentry, flags);
573 return 0;
574}
575
Eric Biggers32c3cf02017-10-09 12:15:43 -0700576/**
577 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
578 * @dir: directory being searched
579 * @dentry: filename being looked up
Eric Biggersb01531d2019-03-20 11:39:13 -0700580 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700581 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700582 * Prepare for ->lookup() in a directory which may be encrypted by determining
583 * the name that will actually be used to search the directory on-disk. Lookups
584 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700585 * filenames are presented in encrypted form. Therefore, we'll try to set up
586 * the directory's encryption key, but even without it the lookup can continue.
587 *
Eric Biggers6cc24862019-03-20 11:39:09 -0700588 * This also installs a custom ->d_revalidate() method which will invalidate the
589 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700590 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700591 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
592 * correctly formed encoded ciphertext name, so a negative dentry should be
593 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700594 */
595static inline int fscrypt_prepare_lookup(struct inode *dir,
596 struct dentry *dentry,
Eric Biggersb01531d2019-03-20 11:39:13 -0700597 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700598{
599 if (IS_ENCRYPTED(dir))
Eric Biggersb01531d2019-03-20 11:39:13 -0700600 return __fscrypt_prepare_lookup(dir, dentry, fname);
601
602 memset(fname, 0, sizeof(*fname));
603 fname->usr_fname = &dentry->d_name;
604 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
605 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700606 return 0;
607}
608
Eric Biggers815dac32017-10-09 12:15:44 -0700609/**
610 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
611 * @dentry: dentry through which the inode is being changed
612 * @attr: attributes to change
613 *
614 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
615 * most attribute changes are allowed even without the encryption key. However,
616 * without the encryption key we do have to forbid truncates. This is needed
617 * because the size being truncated to may not be a multiple of the filesystem
618 * block size, and in that case we'd have to decrypt the final block, zero the
619 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
620 * filesystem block boundary, but it's simpler to just forbid all truncates ---
621 * and we already forbid all other contents modifications without the key.)
622 *
623 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
624 * if a problem occurred while setting up the encryption key.
625 */
626static inline int fscrypt_prepare_setattr(struct dentry *dentry,
627 struct iattr *attr)
628{
629 if (attr->ia_valid & ATTR_SIZE)
630 return fscrypt_require_key(d_inode(dentry));
631 return 0;
632}
633
Eric Biggers76e81d62018-01-05 10:45:01 -0800634/**
635 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
636 * @dir: directory in which the symlink is being created
637 * @target: plaintext symlink target
638 * @len: length of @target excluding null terminator
639 * @max_len: space the filesystem has available to store the symlink target
640 * @disk_link: (out) the on-disk symlink target being prepared
641 *
642 * This function computes the size the symlink target will require on-disk,
643 * stores it in @disk_link->len, and validates it against @max_len. An
644 * encrypted symlink may be longer than the original.
645 *
646 * Additionally, @disk_link->name is set to @target if the symlink will be
647 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
648 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
649 * on-disk target later. (The reason for the two-step process is that some
650 * filesystems need to know the size of the symlink target before creating the
651 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
652 *
653 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
654 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
655 * occurred while setting up the encryption key.
656 */
657static inline int fscrypt_prepare_symlink(struct inode *dir,
658 const char *target,
659 unsigned int len,
660 unsigned int max_len,
661 struct fscrypt_str *disk_link)
662{
663 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
664 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
665
666 disk_link->name = (unsigned char *)target;
667 disk_link->len = len + 1;
668 if (disk_link->len > max_len)
669 return -ENAMETOOLONG;
670 return 0;
671}
672
673/**
674 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
675 * @inode: symlink inode
676 * @target: plaintext symlink target
677 * @len: length of @target excluding null terminator
678 * @disk_link: (in/out) the on-disk symlink target being prepared
679 *
680 * If the symlink target needs to be encrypted, then this function encrypts it
681 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
682 * previously to compute @disk_link->len. If the filesystem did not allocate a
683 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
684 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
685 *
686 * Return: 0 on success, -errno on failure
687 */
688static inline int fscrypt_encrypt_symlink(struct inode *inode,
689 const char *target,
690 unsigned int len,
691 struct fscrypt_str *disk_link)
692{
693 if (IS_ENCRYPTED(inode))
694 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
695 return 0;
696}
697
Eric Biggersd2d07272019-05-20 09:29:39 -0700698/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
699static inline void fscrypt_finalize_bounce_page(struct page **pagep)
700{
701 struct page *page = *pagep;
702
703 if (fscrypt_is_bounce_page(page)) {
704 *pagep = fscrypt_pagecache_page(page);
705 fscrypt_free_bounce_page(page);
706 }
707}
708
Dave Chinner734f0d22017-10-09 12:15:34 -0700709#endif /* _LINUX_FSCRYPT_H */