]> Git Repo - linux.git/blob - include/linux/fscrypt.h
fscrypt: add inline encryption support
[linux.git] / include / linux / fscrypt.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * fscrypt.h: declarations for per-file encryption
4  *
5  * Filesystems that implement per-file encryption must include this header
6  * file.
7  *
8  * Copyright (C) 2015, Google, Inc.
9  *
10  * Written by Michael Halcrow, 2015.
11  * Modified by Jaegeuk Kim, 2015.
12  */
13 #ifndef _LINUX_FSCRYPT_H
14 #define _LINUX_FSCRYPT_H
15
16 #include <linux/fs.h>
17 #include <linux/mm.h>
18 #include <linux/parser.h>
19 #include <linux/slab.h>
20 #include <uapi/linux/fscrypt.h>
21
22 #define FS_CRYPTO_BLOCK_SIZE            16
23
24 union fscrypt_context;
25 struct fscrypt_info;
26 struct seq_file;
27
28 struct fscrypt_str {
29         unsigned char *name;
30         u32 len;
31 };
32
33 struct fscrypt_name {
34         const struct qstr *usr_fname;
35         struct fscrypt_str disk_name;
36         u32 hash;
37         u32 minor_hash;
38         struct fscrypt_str crypto_buf;
39         bool is_ciphertext_name;
40 };
41
42 #define FSTR_INIT(n, l)         { .name = n, .len = l }
43 #define FSTR_TO_QSTR(f)         QSTR_INIT((f)->name, (f)->len)
44 #define fname_name(p)           ((p)->disk_name.name)
45 #define fname_len(p)            ((p)->disk_name.len)
46
47 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
48 #define FSCRYPT_SET_CONTEXT_MAX_SIZE    40
49
50 #ifdef CONFIG_FS_ENCRYPTION
51 /*
52  * fscrypt superblock flags
53  */
54 #define FS_CFLG_OWN_PAGES (1U << 1)
55
56 /*
57  * crypto operations for filesystems
58  */
59 struct fscrypt_operations {
60         unsigned int flags;
61         const char *key_prefix;
62         int (*get_context)(struct inode *inode, void *ctx, size_t len);
63         int (*set_context)(struct inode *inode, const void *ctx, size_t len,
64                            void *fs_data);
65         const union fscrypt_context *(*get_dummy_context)(
66                 struct super_block *sb);
67         bool (*empty_dir)(struct inode *inode);
68         unsigned int max_namelen;
69         bool (*has_stable_inodes)(struct super_block *sb);
70         void (*get_ino_and_lblk_bits)(struct super_block *sb,
71                                       int *ino_bits_ret, int *lblk_bits_ret);
72         int (*get_num_devices)(struct super_block *sb);
73         void (*get_devices)(struct super_block *sb,
74                             struct request_queue **devs);
75 };
76
77 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
78 {
79         /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
80         return READ_ONCE(inode->i_crypt_info) != NULL;
81 }
82
83 /**
84  * fscrypt_needs_contents_encryption() - check whether an inode needs
85  *                                       contents encryption
86  * @inode: the inode to check
87  *
88  * Return: %true iff the inode is an encrypted regular file and the kernel was
89  * built with fscrypt support.
90  *
91  * If you need to know whether the encrypt bit is set even when the kernel was
92  * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
93  */
94 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
95 {
96         return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
97 }
98
99 static inline const union fscrypt_context *
100 fscrypt_get_dummy_context(struct super_block *sb)
101 {
102         if (!sb->s_cop->get_dummy_context)
103                 return NULL;
104         return sb->s_cop->get_dummy_context(sb);
105 }
106
107 /*
108  * When d_splice_alias() moves a directory's encrypted alias to its decrypted
109  * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
110  * must be cleared.  Note that we don't have to support arbitrary moves of this
111  * flag because fscrypt doesn't allow encrypted aliases to be the source or
112  * target of a rename().
113  */
114 static inline void fscrypt_handle_d_move(struct dentry *dentry)
115 {
116         dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
117 }
118
119 /* crypto.c */
120 void fscrypt_enqueue_decrypt_work(struct work_struct *);
121
122 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
123                                               unsigned int len,
124                                               unsigned int offs,
125                                               gfp_t gfp_flags);
126 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
127                                   unsigned int len, unsigned int offs,
128                                   u64 lblk_num, gfp_t gfp_flags);
129
130 int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
131                                      unsigned int offs);
132 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
133                                   unsigned int len, unsigned int offs,
134                                   u64 lblk_num);
135
136 static inline bool fscrypt_is_bounce_page(struct page *page)
137 {
138         return page->mapping == NULL;
139 }
140
141 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
142 {
143         return (struct page *)page_private(bounce_page);
144 }
145
146 void fscrypt_free_bounce_page(struct page *bounce_page);
147
148 /* policy.c */
149 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
150 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
151 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
152 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
153 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
154 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
155                             void *fs_data, bool preload);
156
157 struct fscrypt_dummy_context {
158         const union fscrypt_context *ctx;
159 };
160
161 int fscrypt_set_test_dummy_encryption(struct super_block *sb,
162                                       const substring_t *arg,
163                                       struct fscrypt_dummy_context *dummy_ctx);
164 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
165                                         struct super_block *sb);
166 static inline void
167 fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx)
168 {
169         kfree(dummy_ctx->ctx);
170         dummy_ctx->ctx = NULL;
171 }
172
173 /* keyring.c */
174 void fscrypt_sb_free(struct super_block *sb);
175 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
176 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
177 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
178 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
179
180 /* keysetup.c */
181 int fscrypt_get_encryption_info(struct inode *inode);
182 void fscrypt_put_encryption_info(struct inode *inode);
183 void fscrypt_free_inode(struct inode *inode);
184 int fscrypt_drop_inode(struct inode *inode);
185
186 /* fname.c */
187 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
188                            int lookup, struct fscrypt_name *fname);
189
190 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
191 {
192         kfree(fname->crypto_buf.name);
193 }
194
195 int fscrypt_fname_alloc_buffer(const struct inode *inode, u32 max_encrypted_len,
196                                struct fscrypt_str *crypto_str);
197 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
198 int fscrypt_fname_disk_to_usr(const struct inode *inode,
199                               u32 hash, u32 minor_hash,
200                               const struct fscrypt_str *iname,
201                               struct fscrypt_str *oname);
202 bool fscrypt_match_name(const struct fscrypt_name *fname,
203                         const u8 *de_name, u32 de_name_len);
204 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
205
206 /* bio.c */
207 void fscrypt_decrypt_bio(struct bio *bio);
208 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
209                           sector_t pblk, unsigned int len);
210
211 /* hooks.c */
212 int fscrypt_file_open(struct inode *inode, struct file *filp);
213 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
214                            struct dentry *dentry);
215 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
216                              struct inode *new_dir, struct dentry *new_dentry,
217                              unsigned int flags);
218 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
219                              struct fscrypt_name *fname);
220 int fscrypt_prepare_setflags(struct inode *inode,
221                              unsigned int oldflags, unsigned int flags);
222 int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
223                               unsigned int max_len,
224                               struct fscrypt_str *disk_link);
225 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
226                               unsigned int len, struct fscrypt_str *disk_link);
227 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
228                                 unsigned int max_size,
229                                 struct delayed_call *done);
230 static inline void fscrypt_set_ops(struct super_block *sb,
231                                    const struct fscrypt_operations *s_cop)
232 {
233         sb->s_cop = s_cop;
234 }
235 #else  /* !CONFIG_FS_ENCRYPTION */
236
237 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
238 {
239         return false;
240 }
241
242 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
243 {
244         return false;
245 }
246
247 static inline const union fscrypt_context *
248 fscrypt_get_dummy_context(struct super_block *sb)
249 {
250         return NULL;
251 }
252
253 static inline void fscrypt_handle_d_move(struct dentry *dentry)
254 {
255 }
256
257 /* crypto.c */
258 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
259 {
260 }
261
262 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
263                                                             unsigned int len,
264                                                             unsigned int offs,
265                                                             gfp_t gfp_flags)
266 {
267         return ERR_PTR(-EOPNOTSUPP);
268 }
269
270 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
271                                                 struct page *page,
272                                                 unsigned int len,
273                                                 unsigned int offs, u64 lblk_num,
274                                                 gfp_t gfp_flags)
275 {
276         return -EOPNOTSUPP;
277 }
278
279 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
280                                                    unsigned int len,
281                                                    unsigned int offs)
282 {
283         return -EOPNOTSUPP;
284 }
285
286 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
287                                                 struct page *page,
288                                                 unsigned int len,
289                                                 unsigned int offs, u64 lblk_num)
290 {
291         return -EOPNOTSUPP;
292 }
293
294 static inline bool fscrypt_is_bounce_page(struct page *page)
295 {
296         return false;
297 }
298
299 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
300 {
301         WARN_ON_ONCE(1);
302         return ERR_PTR(-EINVAL);
303 }
304
305 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
306 {
307 }
308
309 /* policy.c */
310 static inline int fscrypt_ioctl_set_policy(struct file *filp,
311                                            const void __user *arg)
312 {
313         return -EOPNOTSUPP;
314 }
315
316 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
317 {
318         return -EOPNOTSUPP;
319 }
320
321 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
322                                               void __user *arg)
323 {
324         return -EOPNOTSUPP;
325 }
326
327 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
328 {
329         return -EOPNOTSUPP;
330 }
331
332 static inline int fscrypt_has_permitted_context(struct inode *parent,
333                                                 struct inode *child)
334 {
335         return 0;
336 }
337
338 static inline int fscrypt_inherit_context(struct inode *parent,
339                                           struct inode *child,
340                                           void *fs_data, bool preload)
341 {
342         return -EOPNOTSUPP;
343 }
344
345 struct fscrypt_dummy_context {
346 };
347
348 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
349                                                       char sep,
350                                                       struct super_block *sb)
351 {
352 }
353
354 static inline void
355 fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx)
356 {
357 }
358
359 /* keyring.c */
360 static inline void fscrypt_sb_free(struct super_block *sb)
361 {
362 }
363
364 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
365 {
366         return -EOPNOTSUPP;
367 }
368
369 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
370 {
371         return -EOPNOTSUPP;
372 }
373
374 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
375                                                      void __user *arg)
376 {
377         return -EOPNOTSUPP;
378 }
379
380 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
381                                                void __user *arg)
382 {
383         return -EOPNOTSUPP;
384 }
385
386 /* keysetup.c */
387 static inline int fscrypt_get_encryption_info(struct inode *inode)
388 {
389         return -EOPNOTSUPP;
390 }
391
392 static inline void fscrypt_put_encryption_info(struct inode *inode)
393 {
394         return;
395 }
396
397 static inline void fscrypt_free_inode(struct inode *inode)
398 {
399 }
400
401 static inline int fscrypt_drop_inode(struct inode *inode)
402 {
403         return 0;
404 }
405
406  /* fname.c */
407 static inline int fscrypt_setup_filename(struct inode *dir,
408                                          const struct qstr *iname,
409                                          int lookup, struct fscrypt_name *fname)
410 {
411         if (IS_ENCRYPTED(dir))
412                 return -EOPNOTSUPP;
413
414         memset(fname, 0, sizeof(*fname));
415         fname->usr_fname = iname;
416         fname->disk_name.name = (unsigned char *)iname->name;
417         fname->disk_name.len = iname->len;
418         return 0;
419 }
420
421 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
422 {
423         return;
424 }
425
426 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
427                                              u32 max_encrypted_len,
428                                              struct fscrypt_str *crypto_str)
429 {
430         return -EOPNOTSUPP;
431 }
432
433 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
434 {
435         return;
436 }
437
438 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
439                                             u32 hash, u32 minor_hash,
440                                             const struct fscrypt_str *iname,
441                                             struct fscrypt_str *oname)
442 {
443         return -EOPNOTSUPP;
444 }
445
446 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
447                                       const u8 *de_name, u32 de_name_len)
448 {
449         /* Encryption support disabled; use standard comparison */
450         if (de_name_len != fname->disk_name.len)
451                 return false;
452         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
453 }
454
455 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
456                                         const struct qstr *name)
457 {
458         WARN_ON_ONCE(1);
459         return 0;
460 }
461
462 /* bio.c */
463 static inline void fscrypt_decrypt_bio(struct bio *bio)
464 {
465 }
466
467 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
468                                         sector_t pblk, unsigned int len)
469 {
470         return -EOPNOTSUPP;
471 }
472
473 /* hooks.c */
474
475 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
476 {
477         if (IS_ENCRYPTED(inode))
478                 return -EOPNOTSUPP;
479         return 0;
480 }
481
482 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
483                                          struct dentry *dentry)
484 {
485         return -EOPNOTSUPP;
486 }
487
488 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
489                                            struct dentry *old_dentry,
490                                            struct inode *new_dir,
491                                            struct dentry *new_dentry,
492                                            unsigned int flags)
493 {
494         return -EOPNOTSUPP;
495 }
496
497 static inline int __fscrypt_prepare_lookup(struct inode *dir,
498                                            struct dentry *dentry,
499                                            struct fscrypt_name *fname)
500 {
501         return -EOPNOTSUPP;
502 }
503
504 static inline int fscrypt_prepare_setflags(struct inode *inode,
505                                            unsigned int oldflags,
506                                            unsigned int flags)
507 {
508         return 0;
509 }
510
511 static inline int __fscrypt_prepare_symlink(struct inode *dir,
512                                             unsigned int len,
513                                             unsigned int max_len,
514                                             struct fscrypt_str *disk_link)
515 {
516         return -EOPNOTSUPP;
517 }
518
519
520 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
521                                             const char *target,
522                                             unsigned int len,
523                                             struct fscrypt_str *disk_link)
524 {
525         return -EOPNOTSUPP;
526 }
527
528 static inline const char *fscrypt_get_symlink(struct inode *inode,
529                                               const void *caddr,
530                                               unsigned int max_size,
531                                               struct delayed_call *done)
532 {
533         return ERR_PTR(-EOPNOTSUPP);
534 }
535
536 static inline void fscrypt_set_ops(struct super_block *sb,
537                                    const struct fscrypt_operations *s_cop)
538 {
539 }
540
541 #endif  /* !CONFIG_FS_ENCRYPTION */
542
543 /* inline_crypt.c */
544 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
545
546 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
547
548 void fscrypt_set_bio_crypt_ctx(struct bio *bio,
549                                const struct inode *inode, u64 first_lblk,
550                                gfp_t gfp_mask);
551
552 void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
553                                   const struct buffer_head *first_bh,
554                                   gfp_t gfp_mask);
555
556 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
557                            u64 next_lblk);
558
559 bool fscrypt_mergeable_bio_bh(struct bio *bio,
560                               const struct buffer_head *next_bh);
561
562 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
563
564 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
565 {
566         return false;
567 }
568
569 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
570                                              const struct inode *inode,
571                                              u64 first_lblk, gfp_t gfp_mask) { }
572
573 static inline void fscrypt_set_bio_crypt_ctx_bh(
574                                          struct bio *bio,
575                                          const struct buffer_head *first_bh,
576                                          gfp_t gfp_mask) { }
577
578 static inline bool fscrypt_mergeable_bio(struct bio *bio,
579                                          const struct inode *inode,
580                                          u64 next_lblk)
581 {
582         return true;
583 }
584
585 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
586                                             const struct buffer_head *next_bh)
587 {
588         return true;
589 }
590 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
591
592 /**
593  * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
594  *                                      encryption
595  * @inode: an inode. If encrypted, its key must be set up.
596  *
597  * Return: true if the inode requires file contents encryption and if the
598  *         encryption should be done in the block layer via blk-crypto rather
599  *         than in the filesystem layer.
600  */
601 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
602 {
603         return fscrypt_needs_contents_encryption(inode) &&
604                __fscrypt_inode_uses_inline_crypto(inode);
605 }
606
607 /**
608  * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
609  *                                        encryption
610  * @inode: an inode. If encrypted, its key must be set up.
611  *
612  * Return: true if the inode requires file contents encryption and if the
613  *         encryption should be done in the filesystem layer rather than in the
614  *         block layer via blk-crypto.
615  */
616 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
617 {
618         return fscrypt_needs_contents_encryption(inode) &&
619                !__fscrypt_inode_uses_inline_crypto(inode);
620 }
621
622 /**
623  * fscrypt_require_key() - require an inode's encryption key
624  * @inode: the inode we need the key for
625  *
626  * If the inode is encrypted, set up its encryption key if not already done.
627  * Then require that the key be present and return -ENOKEY otherwise.
628  *
629  * No locks are needed, and the key will live as long as the struct inode --- so
630  * it won't go away from under you.
631  *
632  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
633  * if a problem occurred while setting up the encryption key.
634  */
635 static inline int fscrypt_require_key(struct inode *inode)
636 {
637         if (IS_ENCRYPTED(inode)) {
638                 int err = fscrypt_get_encryption_info(inode);
639
640                 if (err)
641                         return err;
642                 if (!fscrypt_has_encryption_key(inode))
643                         return -ENOKEY;
644         }
645         return 0;
646 }
647
648 /**
649  * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
650  *                          directory
651  * @old_dentry: an existing dentry for the inode being linked
652  * @dir: the target directory
653  * @dentry: negative dentry for the target filename
654  *
655  * A new link can only be added to an encrypted directory if the directory's
656  * encryption key is available --- since otherwise we'd have no way to encrypt
657  * the filename.  Therefore, we first set up the directory's encryption key (if
658  * not already done) and return an error if it's unavailable.
659  *
660  * We also verify that the link will not violate the constraint that all files
661  * in an encrypted directory tree use the same encryption policy.
662  *
663  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
664  * -EXDEV if the link would result in an inconsistent encryption policy, or
665  * another -errno code.
666  */
667 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
668                                        struct inode *dir,
669                                        struct dentry *dentry)
670 {
671         if (IS_ENCRYPTED(dir))
672                 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
673         return 0;
674 }
675
676 /**
677  * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
678  *                            directories
679  * @old_dir: source directory
680  * @old_dentry: dentry for source file
681  * @new_dir: target directory
682  * @new_dentry: dentry for target location (may be negative unless exchanging)
683  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
684  *
685  * Prepare for ->rename() where the source and/or target directories may be
686  * encrypted.  A new link can only be added to an encrypted directory if the
687  * directory's encryption key is available --- since otherwise we'd have no way
688  * to encrypt the filename.  A rename to an existing name, on the other hand,
689  * *is* cryptographically possible without the key.  However, we take the more
690  * conservative approach and just forbid all no-key renames.
691  *
692  * We also verify that the rename will not violate the constraint that all files
693  * in an encrypted directory tree use the same encryption policy.
694  *
695  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
696  * rename would cause inconsistent encryption policies, or another -errno code.
697  */
698 static inline int fscrypt_prepare_rename(struct inode *old_dir,
699                                          struct dentry *old_dentry,
700                                          struct inode *new_dir,
701                                          struct dentry *new_dentry,
702                                          unsigned int flags)
703 {
704         if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
705                 return __fscrypt_prepare_rename(old_dir, old_dentry,
706                                                 new_dir, new_dentry, flags);
707         return 0;
708 }
709
710 /**
711  * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
712  *                            directory
713  * @dir: directory being searched
714  * @dentry: filename being looked up
715  * @fname: (output) the name to use to search the on-disk directory
716  *
717  * Prepare for ->lookup() in a directory which may be encrypted by determining
718  * the name that will actually be used to search the directory on-disk.  Lookups
719  * can be done with or without the directory's encryption key; without the key,
720  * filenames are presented in encrypted form.  Therefore, we'll try to set up
721  * the directory's encryption key, but even without it the lookup can continue.
722  *
723  * This also installs a custom ->d_revalidate() method which will invalidate the
724  * dentry if it was created without the key and the key is later added.
725  *
726  * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
727  * correctly formed encoded ciphertext name, so a negative dentry should be
728  * created; or another -errno code.
729  */
730 static inline int fscrypt_prepare_lookup(struct inode *dir,
731                                          struct dentry *dentry,
732                                          struct fscrypt_name *fname)
733 {
734         if (IS_ENCRYPTED(dir))
735                 return __fscrypt_prepare_lookup(dir, dentry, fname);
736
737         memset(fname, 0, sizeof(*fname));
738         fname->usr_fname = &dentry->d_name;
739         fname->disk_name.name = (unsigned char *)dentry->d_name.name;
740         fname->disk_name.len = dentry->d_name.len;
741         return 0;
742 }
743
744 /**
745  * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
746  *                             attributes
747  * @dentry: dentry through which the inode is being changed
748  * @attr: attributes to change
749  *
750  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
751  * most attribute changes are allowed even without the encryption key.  However,
752  * without the encryption key we do have to forbid truncates.  This is needed
753  * because the size being truncated to may not be a multiple of the filesystem
754  * block size, and in that case we'd have to decrypt the final block, zero the
755  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
756  * filesystem block boundary, but it's simpler to just forbid all truncates ---
757  * and we already forbid all other contents modifications without the key.)
758  *
759  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
760  * if a problem occurred while setting up the encryption key.
761  */
762 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
763                                           struct iattr *attr)
764 {
765         if (attr->ia_valid & ATTR_SIZE)
766                 return fscrypt_require_key(d_inode(dentry));
767         return 0;
768 }
769
770 /**
771  * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
772  * @dir: directory in which the symlink is being created
773  * @target: plaintext symlink target
774  * @len: length of @target excluding null terminator
775  * @max_len: space the filesystem has available to store the symlink target
776  * @disk_link: (out) the on-disk symlink target being prepared
777  *
778  * This function computes the size the symlink target will require on-disk,
779  * stores it in @disk_link->len, and validates it against @max_len.  An
780  * encrypted symlink may be longer than the original.
781  *
782  * Additionally, @disk_link->name is set to @target if the symlink will be
783  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
784  * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
785  * on-disk target later.  (The reason for the two-step process is that some
786  * filesystems need to know the size of the symlink target before creating the
787  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
788  *
789  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
790  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
791  * occurred while setting up the encryption key.
792  */
793 static inline int fscrypt_prepare_symlink(struct inode *dir,
794                                           const char *target,
795                                           unsigned int len,
796                                           unsigned int max_len,
797                                           struct fscrypt_str *disk_link)
798 {
799         if (IS_ENCRYPTED(dir) || fscrypt_get_dummy_context(dir->i_sb) != NULL)
800                 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
801
802         disk_link->name = (unsigned char *)target;
803         disk_link->len = len + 1;
804         if (disk_link->len > max_len)
805                 return -ENAMETOOLONG;
806         return 0;
807 }
808
809 /**
810  * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
811  * @inode: symlink inode
812  * @target: plaintext symlink target
813  * @len: length of @target excluding null terminator
814  * @disk_link: (in/out) the on-disk symlink target being prepared
815  *
816  * If the symlink target needs to be encrypted, then this function encrypts it
817  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
818  * previously to compute @disk_link->len.  If the filesystem did not allocate a
819  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
820  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
821  *
822  * Return: 0 on success, -errno on failure
823  */
824 static inline int fscrypt_encrypt_symlink(struct inode *inode,
825                                           const char *target,
826                                           unsigned int len,
827                                           struct fscrypt_str *disk_link)
828 {
829         if (IS_ENCRYPTED(inode))
830                 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
831         return 0;
832 }
833
834 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
835 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
836 {
837         struct page *page = *pagep;
838
839         if (fscrypt_is_bounce_page(page)) {
840                 *pagep = fscrypt_pagecache_page(page);
841                 fscrypt_free_bounce_page(page);
842         }
843 }
844
845 #endif  /* _LINUX_FSCRYPT_H */
This page took 0.078942 seconds and 4 git commands to generate.