Whamcloud - gitweb
LU-16974 utils: make bandwidth options consistent
[fs/lustre-release.git] / libcfs / include / libcfs / crypto / llcrypt.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /*
4  * llcrypt.h: declarations for per-file encryption
5  *
6  * Filesystems that implement per-file encryption must include this header
7  * file.
8  *
9  * Copyright (C) 2015, Google, Inc.
10  *
11  * Written by Michael Halcrow, 2015.
12  * Modified by Jaegeuk Kim, 2015.
13  */
14
15 /*
16  * Linux commit 219d54332a09
17  * tags/v5.4
18  */
19
20 #ifndef _LINUX_LLCRYPT_H
21 #define _LINUX_LLCRYPT_H
22
23 #ifndef DCACHE_ENCRYPTED_NAME
24 #define DCACHE_ENCRYPTED_NAME 0x02000000
25 #endif
26
27 #include <linux/fs.h>
28 #include <linux/mm.h>
29 #include <linux/slab.h>
30 #include <lustre_disk.h>
31 #include <uapi/linux/llcrypt.h>
32
33 #define LL_CRYPTO_BLOCK_SIZE            16
34
35 /* Extracts the second-to-last ciphertext block; see explanation below */
36 #define LLCRYPT_FNAME_DIGEST(name, len)                         \
37         ((name) + round_down((len) - LL_CRYPTO_BLOCK_SIZE - 1,  \
38                              LL_CRYPTO_BLOCK_SIZE))
39
40 struct llcrypt_ctx;
41 struct llcrypt_info;
42
43 struct llcrypt_str {
44         unsigned char *name;
45         u32 len;
46 };
47
48 struct llcrypt_name {
49         const struct qstr *usr_fname;
50         struct llcrypt_str disk_name;
51         u32 hash;
52         u32 minor_hash;
53         struct llcrypt_str crypto_buf;
54         bool is_ciphertext_name;
55 };
56
57 #define LLTR_INIT(n, l)         { .name = n, .len = l }
58 #define LLTR_TO_QSTR(f)         QSTR_INIT((f)->name, (f)->len)
59 #define lname_name(p)           ((p)->disk_name.name)
60 #define lname_len(p)            ((p)->disk_name.len)
61
62 /* Maximum value for the third parameter of llcrypt_operations.set_context(). */
63 #define LLCRYPT_SET_CONTEXT_MAX_SIZE    40
64 #define LLCRYPT_DIGESTED_CHAR_OLD       '_'
65 #define LLCRYPT_DIGESTED_CHAR           '+'
66
67 #ifdef CONFIG_LL_ENCRYPTION
68 /*
69  * llcrypt superblock flags
70  */
71 #define LL_CFLG_OWN_PAGES (1U << 1)
72
73 /*
74  * crypto operations for filesystems
75  */
76 struct llcrypt_operations {
77         unsigned int flags;
78         const char *key_prefix;
79         int (*get_context)(struct inode *, void *, size_t);
80         int (*set_context)(struct inode *, const void *, size_t, void *);
81         bool (*dummy_context)(struct inode *);
82         bool (*empty_dir)(struct inode *);
83         unsigned int max_namelen;
84 };
85
86 /* Decryption work */
87 struct llcrypt_ctx {
88         union {
89                 struct {
90                         struct bio *bio;
91                         struct work_struct work;
92                 };
93                 struct list_head free_list;     /* Free list */
94         };
95         u8 flags;                               /* Flags */
96 };
97
98 extern bool llcrypt_has_encryption_key(const struct inode *inode);
99
100 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
101 {
102         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
103
104         if (unlikely(!lsi))
105                 return false;
106
107         return lsi->lsi_cop->dummy_context &&
108                 lsi->lsi_cop->dummy_context(inode);
109 }
110
111 /*
112  * When d_splice_alias() moves a directory's encrypted alias to its decrypted
113  * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
114  * must be cleared.  Note that we don't have to support arbitrary moves of this
115  * flag because llcrypt doesn't allow encrypted aliases to be the source or
116  * target of a rename().
117  */
118 static inline void llcrypt_handle_d_move(struct dentry *dentry)
119 {
120         dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
121 }
122
123 /* crypto.c */
124 extern int __init llcrypt_init(void);
125 extern void __exit llcrypt_exit(void);
126 extern void llcrypt_enqueue_decrypt_work(struct work_struct *);
127 extern struct llcrypt_ctx *llcrypt_get_ctx(gfp_t);
128 extern void llcrypt_release_ctx(struct llcrypt_ctx *);
129
130 extern struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
131                                                      unsigned int len,
132                                                      unsigned int offs,
133                                                      gfp_t gfp_flags);
134 extern int llcrypt_encrypt_block(const struct inode *inode, struct page *src,
135                          struct page *dst, unsigned int len,
136                          unsigned int offs, u64 lblk_num, gfp_t gfp_flags);
137
138 extern int llcrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
139                                             unsigned int offs);
140
141 extern int llcrypt_decrypt_block(const struct inode *inode, struct page *src,
142                          struct page *dst, unsigned int len,
143                          unsigned int offs, u64 lblk_num, gfp_t gfp_flags);
144
145 static inline int llcrypt_decrypt_block_inplace(const struct inode *inode,
146                                                 struct page *page,
147                                                 unsigned int len,
148                                                 unsigned int offs,
149                                                 u64 lblk_num)
150 {
151         return llcrypt_decrypt_block(inode, page, page, len, offs, lblk_num,
152                                      GFP_NOFS);
153 }
154
155 static inline bool llcrypt_is_bounce_page(struct page *page)
156 {
157         return page->mapping == NULL;
158 }
159
160 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
161 {
162         return (struct page *)page_private(bounce_page);
163 }
164
165 extern void llcrypt_free_bounce_page(struct page *bounce_page);
166
167 /* policy.c */
168 extern int llcrypt_ioctl_set_policy(struct file *, const void __user *);
169 extern int llcrypt_ioctl_get_policy(struct file *, void __user *);
170 extern int llcrypt_ioctl_get_policy_ex(struct file *, void __user *);
171 extern int llcrypt_has_permitted_context(struct inode *, struct inode *);
172 extern int llcrypt_inherit_context(struct inode *, struct inode *,
173                                         void *, bool);
174 extern bool llcrypt_policy_has_filename_enc(struct inode *inode);
175 /* keyring.c */
176 extern void llcrypt_sb_free(struct lustre_sb_info *lsi);
177 extern int llcrypt_ioctl_add_key(struct file *filp, void __user *arg);
178 extern int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg);
179 extern int llcrypt_ioctl_remove_key_all_users(struct file *filp,
180                                               void __user *arg);
181 extern int llcrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
182
183 /* keysetup.c */
184 extern int llcrypt_get_encryption_info(struct inode *);
185 extern void llcrypt_put_encryption_info(struct inode *);
186 extern void llcrypt_free_inode(struct inode *);
187 extern int llcrypt_drop_inode(struct inode *inode);
188
189 /* fname.c */
190 extern int llcrypt_setup_filename(struct inode *, const struct qstr *,
191                                 int lookup, struct llcrypt_name *);
192
193 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
194 {
195         kfree(fname->crypto_buf.name);
196 }
197
198 extern int llcrypt_fname_alloc_buffer(const struct inode *, u32,
199                                 struct llcrypt_str *);
200 extern void llcrypt_fname_free_buffer(struct llcrypt_str *);
201 extern int llcrypt_fname_disk_to_usr(struct inode *, u32, u32,
202                         const struct llcrypt_str *, struct llcrypt_str *);
203
204 #define LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE       32
205 #define LLCRYPT_FNAME_DIGEST_SIZE       LL_CRYPTO_BLOCK_SIZE
206
207 /**
208  * llcrypt_digested_name - alternate identifier for an on-disk filename
209  *
210  * When userspace lists an encrypted directory without access to the key,
211  * filenames whose ciphertext is longer than LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE
212  * bytes are shown in this abbreviated form (base64-encoded) rather than as the
213  * full ciphertext (base64-encoded).  This is necessary to allow supporting
214  * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
215  *
216  * To make it possible for filesystems to still find the correct directory entry
217  * despite not knowing the full on-disk name, we encode any filesystem-specific
218  * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
219  * followed by the second-to-last ciphertext block of the filename.  Due to the
220  * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
221  * depends on the full plaintext.  (Note that ciphertext stealing causes the
222  * last two blocks to appear "flipped".)  This makes accidental collisions very
223  * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
224  * share the same filesystem-specific hashes.
225  *
226  * However, this scheme isn't immune to intentional collisions, which can be
227  * created by anyone able to create arbitrary plaintext filenames and view them
228  * without the key.  Making the "digest" be a real cryptographic hash like
229  * SHA-256 over the full ciphertext would prevent this, although it would be
230  * less efficient and harder to implement, especially since the filesystem would
231  * need to calculate it for each directory entry examined during a search.
232  */
233 struct llcrypt_digested_name {
234         u32 hash;
235         u32 minor_hash;
236         u8 digest[LLCRYPT_FNAME_DIGEST_SIZE];
237 };
238
239 /**
240  * llcrypt_match_name() - test whether the given name matches a directory entry
241  * @fname: the name being searched for
242  * @de_name: the name from the directory entry
243  * @de_name_len: the length of @de_name in bytes
244  *
245  * Normally @fname->disk_name will be set, and in that case we simply compare
246  * that to the name stored in the directory entry.  The only exception is that
247  * if we don't have the key for an encrypted directory and a filename in it is
248  * very long, then we won't have the full disk_name and we'll instead need to
249  * match against the llcrypt_digested_name.
250  *
251  * Return: %true if the name matches, otherwise %false.
252  */
253 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
254                                       const u8 *de_name, u32 de_name_len)
255 {
256         if (unlikely(!fname->disk_name.name)) {
257                 const struct llcrypt_digested_name *n =
258                         (const void *)fname->crypto_buf.name;
259                 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
260                         return false;
261                 if (de_name_len <= LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
262                         return false;
263                 return !memcmp(LLCRYPT_FNAME_DIGEST(de_name, de_name_len),
264                                n->digest, LLCRYPT_FNAME_DIGEST_SIZE);
265         }
266
267         if (de_name_len != fname->disk_name.len)
268                 return false;
269         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
270 }
271
272 /* hooks.c */
273 extern int llcrypt_file_open(struct inode *inode, struct file *filp);
274 extern int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
275                                   struct dentry *dentry);
276 extern int __llcrypt_prepare_rename(struct inode *old_dir,
277                                     struct dentry *old_dentry,
278                                     struct inode *new_dir,
279                                     struct dentry *new_dentry,
280                                     unsigned int flags);
281 extern int __llcrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
282                                     struct llcrypt_name *fname);
283 extern int __llcrypt_prepare_symlink(struct inode *dir, unsigned int len,
284                                      unsigned int max_len,
285                                      struct llcrypt_str *disk_link);
286 extern int __llcrypt_encrypt_symlink(struct inode *inode, const char *target,
287                                      unsigned int len,
288                                      struct llcrypt_str *disk_link);
289 extern const char *llcrypt_get_symlink(struct inode *inode, const void *caddr,
290                                        unsigned int max_size,
291                                        struct delayed_call *done);
292 static inline void llcrypt_set_ops(struct super_block *sb,
293                                    const struct llcrypt_operations *lsi_cop)
294 {
295         struct lustre_sb_info *lsi = s2lsi(sb);
296
297         if (lsi)
298                 lsi->lsi_cop = lsi_cop;
299 }
300 #else  /* !CONFIG_LL_ENCRYPTION */
301
302 struct llcrypt_operations;
303 #define llcrypt_init()         0
304 #define llcrypt_exit()         {}
305
306 #undef IS_ENCRYPTED
307 #define IS_ENCRYPTED(x)        0
308
309 static inline bool llcrypt_has_encryption_key(const struct inode *inode)
310 {
311         return false;
312 }
313
314 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
315 {
316         return false;
317 }
318
319 static inline void llcrypt_handle_d_move(struct dentry *dentry)
320 {
321 }
322
323 /* crypto.c */
324 static inline void llcrypt_enqueue_decrypt_work(struct work_struct *work)
325 {
326 }
327
328 static inline struct llcrypt_ctx *llcrypt_get_ctx(gfp_t gfp_flags)
329 {
330         return ERR_PTR(-EOPNOTSUPP);
331 }
332
333 static inline void llcrypt_release_ctx(struct llcrypt_ctx *ctx)
334 {
335         return;
336 }
337
338 static inline struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
339                                                             unsigned int len,
340                                                             unsigned int offs,
341                                                             gfp_t gfp_flags)
342 {
343         return ERR_PTR(-EOPNOTSUPP);
344 }
345
346 static inline int llcrypt_encrypt_block(const struct inode *inode,
347                                         struct page *src, struct page *dst,
348                                         unsigned int len, unsigned int offs,
349                                         u64 lblk_num, gfp_t gfp_flags)
350 {
351         return -EOPNOTSUPP;
352 }
353
354 static inline int llcrypt_decrypt_pagecache_blocks(struct page *page,
355                                                    unsigned int len,
356                                                    unsigned int offs)
357 {
358         return -EOPNOTSUPP;
359 }
360
361 static inline int llcrypt_decrypt_block(const struct inode *inode,
362                                         struct page *src, struct page *dst,
363                                         unsigned int len, unsigned int offs,
364                                         u64 lblk_num, gfp_t gfp_flags)
365 {
366         return -EOPNOTSUPP;
367 }
368
369 static inline int llcrypt_decrypt_block_inplace(const struct inode *inode,
370                                                 struct page *page,
371                                                 unsigned int len,
372                                                 unsigned int offs, u64 lblk_num)
373 {
374         return -EOPNOTSUPP;
375 }
376
377 static inline bool llcrypt_is_bounce_page(struct page *page)
378 {
379         return false;
380 }
381
382 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
383 {
384         WARN_ON_ONCE(1);
385         return ERR_PTR(-EINVAL);
386 }
387
388 static inline void llcrypt_free_bounce_page(struct page *bounce_page)
389 {
390 }
391
392 /* policy.c */
393 static inline int llcrypt_ioctl_set_policy(struct file *filp,
394                                            const void __user *arg)
395 {
396         return -EOPNOTSUPP;
397 }
398
399 static inline int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
400 {
401         return -EOPNOTSUPP;
402 }
403
404 static inline int llcrypt_ioctl_get_policy_ex(struct file *filp,
405                                               void __user *arg)
406 {
407         return -EOPNOTSUPP;
408 }
409
410 static inline int llcrypt_has_permitted_context(struct inode *parent,
411                                                 struct inode *child)
412 {
413         return 0;
414 }
415
416 static inline int llcrypt_inherit_context(struct inode *parent,
417                                           struct inode *child,
418                                           void *fs_data, bool preload)
419 {
420         return -EOPNOTSUPP;
421 }
422 static inline bool llcrypt_policy_has_filename_enc(struct inode *inode)
423 {
424         return false;
425 }
426
427 /* keyring.c */
428 static inline void llcrypt_sb_free(struct lustre_sb_info *lsi)
429 {
430 }
431
432 static inline int llcrypt_ioctl_add_key(struct file *filp, void __user *arg)
433 {
434         return -EOPNOTSUPP;
435 }
436
437 static inline int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg)
438 {
439         return -EOPNOTSUPP;
440 }
441
442 static inline int llcrypt_ioctl_remove_key_all_users(struct file *filp,
443                                                      void __user *arg)
444 {
445         return -EOPNOTSUPP;
446 }
447
448 static inline int llcrypt_ioctl_get_key_status(struct file *filp,
449                                                void __user *arg)
450 {
451         return -EOPNOTSUPP;
452 }
453
454 /* keysetup.c */
455 static inline int llcrypt_get_encryption_info(struct inode *inode)
456 {
457         return -EOPNOTSUPP;
458 }
459
460 static inline void llcrypt_put_encryption_info(struct inode *inode)
461 {
462         return;
463 }
464
465 static inline void llcrypt_free_inode(struct inode *inode)
466 {
467 }
468
469 static inline int llcrypt_drop_inode(struct inode *inode)
470 {
471         return 0;
472 }
473
474  /* fname.c */
475 static inline int llcrypt_setup_filename(struct inode *dir,
476                                          const struct qstr *iname,
477                                          int lookup, struct llcrypt_name *fname)
478 {
479         if (IS_ENCRYPTED(dir))
480                 return -EOPNOTSUPP;
481
482         memset(fname, 0, sizeof(*fname));
483         fname->usr_fname = iname;
484         fname->disk_name.name = (unsigned char *)iname->name;
485         fname->disk_name.len = iname->len;
486         return 0;
487 }
488
489 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
490 {
491         return;
492 }
493
494 static inline int llcrypt_fname_alloc_buffer(const struct inode *inode,
495                                              u32 max_encrypted_len,
496                                              struct llcrypt_str *crypto_str)
497 {
498         return -EOPNOTSUPP;
499 }
500
501 static inline void llcrypt_fname_free_buffer(struct llcrypt_str *crypto_str)
502 {
503         return;
504 }
505
506 static inline int llcrypt_fname_disk_to_usr(struct inode *inode,
507                                             u32 hash, u32 minor_hash,
508                                             const struct llcrypt_str *iname,
509                                             struct llcrypt_str *oname)
510 {
511         return -EOPNOTSUPP;
512 }
513
514 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
515                                       const u8 *de_name, u32 de_name_len)
516 {
517         /* Encryption support disabled; use standard comparison */
518         if (de_name_len != fname->disk_name.len)
519                 return false;
520         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
521 }
522
523 /* hooks.c */
524
525 static inline int llcrypt_file_open(struct inode *inode, struct file *filp)
526 {
527         if (IS_ENCRYPTED(inode))
528                 return -EOPNOTSUPP;
529         return 0;
530 }
531
532 static inline int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
533                                          struct dentry *dentry)
534 {
535         return -EOPNOTSUPP;
536 }
537
538 static inline int __llcrypt_prepare_rename(struct inode *old_dir,
539                                            struct dentry *old_dentry,
540                                            struct inode *new_dir,
541                                            struct dentry *new_dentry,
542                                            unsigned int flags)
543 {
544         return -EOPNOTSUPP;
545 }
546
547 static inline int __llcrypt_prepare_lookup(struct inode *dir,
548                                            struct dentry *dentry,
549                                            struct llcrypt_name *fname)
550 {
551         return -EOPNOTSUPP;
552 }
553
554 static inline int __llcrypt_prepare_symlink(struct inode *dir,
555                                             unsigned int len,
556                                             unsigned int max_len,
557                                             struct llcrypt_str *disk_link)
558 {
559         return -EOPNOTSUPP;
560 }
561
562
563 static inline int __llcrypt_encrypt_symlink(struct inode *inode,
564                                             const char *target,
565                                             unsigned int len,
566                                             struct llcrypt_str *disk_link)
567 {
568         return -EOPNOTSUPP;
569 }
570
571 #define llcrypt_get_symlink(inode, caddr, max_size, done)   ERR_PTR(-EOPNOTSUPP)
572
573 static inline void llcrypt_set_ops(struct super_block *sb,
574                                    const struct llcrypt_operations *lsi_cop)
575 {
576 }
577
578 #endif  /* !CONFIG_LL_ENCRYPTION */
579
580 /**
581  * llcrypt_require_key - require an inode's encryption key
582  * @inode: the inode we need the key for
583  *
584  * If the inode is encrypted, set up its encryption key if not already done.
585  * Then require that the key be present and return -ENOKEY otherwise.
586  *
587  * No locks are needed, and the key will live as long as the struct inode --- so
588  * it won't go away from under you.
589  *
590  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
591  * if a problem occurred while setting up the encryption key.
592  */
593 static inline int llcrypt_require_key(struct inode *inode)
594 {
595         if (IS_ENCRYPTED(inode)) {
596                 int err = llcrypt_get_encryption_info(inode);
597
598                 if (err)
599                         return err;
600                 if (!llcrypt_has_encryption_key(inode))
601                         return -ENOKEY;
602         }
603         return 0;
604 }
605
606 /**
607  * llcrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
608  * @old_dentry: an existing dentry for the inode being linked
609  * @dir: the target directory
610  * @dentry: negative dentry for the target filename
611  *
612  * A new link can only be added to an encrypted directory if the directory's
613  * encryption key is available --- since otherwise we'd have no way to encrypt
614  * the filename.  Therefore, we first set up the directory's encryption key (if
615  * not already done) and return an error if it's unavailable.
616  *
617  * We also verify that the link will not violate the constraint that all files
618  * in an encrypted directory tree use the same encryption policy.
619  *
620  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
621  * -EXDEV if the link would result in an inconsistent encryption policy, or
622  * another -errno code.
623  */
624 static inline int llcrypt_prepare_link(struct dentry *old_dentry,
625                                        struct inode *dir,
626                                        struct dentry *dentry)
627 {
628         if (IS_ENCRYPTED(dir))
629                 return __llcrypt_prepare_link(d_inode(old_dentry), dir, dentry);
630         return 0;
631 }
632
633 /**
634  * llcrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
635  * @old_dir: source directory
636  * @old_dentry: dentry for source file
637  * @new_dir: target directory
638  * @new_dentry: dentry for target location (may be negative unless exchanging)
639  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
640  *
641  * Prepare for ->rename() where the source and/or target directories may be
642  * encrypted.  A new link can only be added to an encrypted directory if the
643  * directory's encryption key is available --- since otherwise we'd have no way
644  * to encrypt the filename.  A rename to an existing name, on the other hand,
645  * *is* cryptographically possible without the key.  However, we take the more
646  * conservative approach and just forbid all no-key renames.
647  *
648  * We also verify that the rename will not violate the constraint that all files
649  * in an encrypted directory tree use the same encryption policy.
650  *
651  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
652  * rename would cause inconsistent encryption policies, or another -errno code.
653  */
654 static inline int llcrypt_prepare_rename(struct inode *old_dir,
655                                          struct dentry *old_dentry,
656                                          struct inode *new_dir,
657                                          struct dentry *new_dentry,
658                                          unsigned int flags)
659 {
660         if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
661                 return __llcrypt_prepare_rename(old_dir, old_dentry,
662                                                 new_dir, new_dentry, flags);
663         return 0;
664 }
665
666 /**
667  * llcrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
668  * @dir: directory being searched
669  * @dentry: filename being looked up
670  * @fname: (output) the name to use to search the on-disk directory
671  *
672  * Prepare for ->lookup() in a directory which may be encrypted by determining
673  * the name that will actually be used to search the directory on-disk.  Lookups
674  * can be done with or without the directory's encryption key; without the key,
675  * filenames are presented in encrypted form.  Therefore, we'll try to set up
676  * the directory's encryption key, but even without it the lookup can continue.
677  *
678  * This also installs a custom ->d_revalidate() method which will invalidate the
679  * dentry if it was created without the key and the key is later added.
680  *
681  * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
682  * correctly formed encoded ciphertext name, so a negative dentry should be
683  * created; or another -errno code.
684  */
685 static inline int llcrypt_prepare_lookup(struct inode *dir,
686                                          struct dentry *dentry,
687                                          struct llcrypt_name *fname)
688 {
689         if (IS_ENCRYPTED(dir))
690                 return __llcrypt_prepare_lookup(dir, dentry, fname);
691
692         memset(fname, 0, sizeof(*fname));
693         fname->usr_fname = &dentry->d_name;
694         fname->disk_name.name = (unsigned char *)dentry->d_name.name;
695         fname->disk_name.len = dentry->d_name.len;
696         return 0;
697 }
698
699 /**
700  * llcrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
701  * @dentry: dentry through which the inode is being changed
702  * @attr: attributes to change
703  *
704  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
705  * most attribute changes are allowed even without the encryption key.  However,
706  * without the encryption key we do have to forbid truncates.  This is needed
707  * because the size being truncated to may not be a multiple of the filesystem
708  * block size, and in that case we'd have to decrypt the final block, zero the
709  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
710  * filesystem block boundary, but it's simpler to just forbid all truncates ---
711  * and we already forbid all other contents modifications without the key.)
712  *
713  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
714  * if a problem occurred while setting up the encryption key.
715  */
716 static inline int llcrypt_prepare_setattr(struct dentry *dentry,
717                                           struct iattr *attr)
718 {
719         if (attr->ia_valid & ATTR_SIZE)
720                 return llcrypt_require_key(d_inode(dentry));
721         return 0;
722 }
723
724 /**
725  * llcrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
726  * @dir: directory in which the symlink is being created
727  * @target: plaintext symlink target
728  * @len: length of @target excluding null terminator
729  * @max_len: space the filesystem has available to store the symlink target
730  * @disk_link: (out) the on-disk symlink target being prepared
731  *
732  * This function computes the size the symlink target will require on-disk,
733  * stores it in @disk_link->len, and validates it against @max_len.  An
734  * encrypted symlink may be longer than the original.
735  *
736  * Additionally, @disk_link->name is set to @target if the symlink will be
737  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
738  * symlinks, the filesystem must call llcrypt_encrypt_symlink() to create the
739  * on-disk target later.  (The reason for the two-step process is that some
740  * filesystems need to know the size of the symlink target before creating the
741  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
742  *
743  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
744  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
745  * occurred while setting up the encryption key.
746  */
747 static inline int llcrypt_prepare_symlink(struct inode *dir,
748                                           const char *target,
749                                           unsigned int len,
750                                           unsigned int max_len,
751                                           struct llcrypt_str *disk_link)
752 {
753         if ((IS_ENCRYPTED(dir) || llcrypt_dummy_context_enabled(dir)) &&
754             llcrypt_policy_has_filename_enc(dir))
755                 return __llcrypt_prepare_symlink(dir, len, max_len, disk_link);
756
757         disk_link->name = (unsigned char *)target;
758         disk_link->len = len + 1;
759         if (disk_link->len > max_len)
760                 return -ENAMETOOLONG;
761         return 0;
762 }
763
764 /**
765  * llcrypt_encrypt_symlink - encrypt the symlink target if needed
766  * @inode: symlink inode
767  * @target: plaintext symlink target
768  * @len: length of @target excluding null terminator
769  * @disk_link: (in/out) the on-disk symlink target being prepared
770  *
771  * If the symlink target needs to be encrypted, then this function encrypts it
772  * into @disk_link->name.  llcrypt_prepare_symlink() must have been called
773  * previously to compute @disk_link->len.  If the filesystem did not allocate a
774  * buffer for @disk_link->name after calling llcrypt_prepare_link(), then one
775  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
776  *
777  * Return: 0 on success, -errno on failure
778  */
779 static inline int llcrypt_encrypt_symlink(struct inode *inode,
780                                           const char *target,
781                                           unsigned int len,
782                                           struct llcrypt_str *disk_link)
783 {
784         if (IS_ENCRYPTED(inode))
785                 return __llcrypt_encrypt_symlink(inode, target, len, disk_link);
786         return 0;
787 }
788
789 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
790 static inline void llcrypt_finalize_bounce_page(struct page **pagep)
791 {
792         struct page *page = *pagep;
793
794         if (llcrypt_is_bounce_page(page)) {
795                 *pagep = llcrypt_pagecache_page(page);
796                 llcrypt_free_bounce_page(page);
797         }
798 }
799
800 #endif  /* _LINUX_LLCRYPT_H */