blob: 6febaa5701606e498d3eaa889c7170b83be7bba8 [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 Rajendraad8ceb02018-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 Rajendraad8ceb02018-12-12 15:20:12 +053017#include <linux/mm.h>
18#include <linux/slab.h>
Blagovest Kolenichev94699592020-03-27 03:19:47 -070019#include <uapi/linux/fscrypt.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080020
21#define FS_CRYPTO_BLOCK_SIZE 16
22
23struct 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 Biggers716c3702019-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(). */
Blagovest Kolenichev94699592020-03-27 03:19:47 -070045#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
Tahsin Erdoganaf652072017-07-06 00:01:59 -040046
Chandan Rajendraad8ceb02018-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;
Blagovest Kolenichev94699592020-03-27 03:19:47 -070064 bool (*has_stable_inodes)(struct super_block *sb);
65 void (*get_ino_and_lblk_bits)(struct super_block *sb,
66 int *ino_bits_ret, int *lblk_bits_ret);
67 bool (*inline_crypt_enabled)(struct super_block *sb);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053068};
69
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053070static inline bool fscrypt_has_encryption_key(const struct inode *inode)
71{
Eric Biggers7a8861d2019-04-11 14:32:15 -070072 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
73 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053074}
75
76static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
77{
78 return inode->i_sb->s_cop->dummy_context &&
79 inode->i_sb->s_cop->dummy_context(inode);
80}
81
Eric Biggersdd038b32019-03-20 11:39:11 -070082/*
83 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
84 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
85 * must be cleared. Note that we don't have to support arbitrary moves of this
86 * flag because fscrypt doesn't allow encrypted aliases to be the source or
87 * target of a rename().
88 */
89static inline void fscrypt_handle_d_move(struct dentry *dentry)
90{
91 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
92}
93
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053094/* crypto.c */
95extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggersfd0e18f2019-05-20 09:29:44 -070096
97extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
98 unsigned int len,
99 unsigned int offs,
100 gfp_t gfp_flags);
Eric Biggersce7b7a22019-05-20 09:29:43 -0700101extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
102 struct page *page, unsigned int len,
103 unsigned int offs, u64 lblk_num,
104 gfp_t gfp_flags);
Eric Biggers87806292019-05-20 09:29:47 -0700105
106extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
107 unsigned int offs);
Eric Biggers45822362019-05-20 09:29:46 -0700108extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
109 struct page *page, unsigned int len,
110 unsigned int offs, u64 lblk_num);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530111
Eric Biggers2edab9e2019-05-20 09:29:39 -0700112static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530113{
Eric Biggers2edab9e2019-05-20 09:29:39 -0700114 return page->mapping == NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530115}
116
Eric Biggers2edab9e2019-05-20 09:29:39 -0700117static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
118{
119 return (struct page *)page_private(bounce_page);
120}
121
122extern void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530123
124/* policy.c */
125extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
126extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700127extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530128extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
129extern int fscrypt_inherit_context(struct inode *, struct inode *,
130 void *, bool);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700131/* keyring.c */
132extern void fscrypt_sb_free(struct super_block *sb);
133extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
134extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
135extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
136 void __user *arg);
137extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
138extern int fscrypt_register_key_removal_notifier(struct notifier_block *nb);
139extern int fscrypt_unregister_key_removal_notifier(struct notifier_block *nb);
140
141/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530142extern int fscrypt_get_encryption_info(struct inode *);
143extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700144extern void fscrypt_free_inode(struct inode *);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700145extern int fscrypt_drop_inode(struct inode *inode);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530146
147/* fname.c */
148extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
149 int lookup, struct fscrypt_name *);
150
151static inline void fscrypt_free_filename(struct fscrypt_name *fname)
152{
153 kfree(fname->crypto_buf.name);
154}
155
156extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
157 struct fscrypt_str *);
158extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
159extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
160 const struct fscrypt_str *, struct fscrypt_str *);
161
162#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
163
164/* Extracts the second-to-last ciphertext block; see explanation below */
165#define FSCRYPT_FNAME_DIGEST(name, len) \
166 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
167 FS_CRYPTO_BLOCK_SIZE))
168
169#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
170
171/**
172 * fscrypt_digested_name - alternate identifier for an on-disk filename
173 *
174 * When userspace lists an encrypted directory without access to the key,
175 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
176 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
177 * full ciphertext (base64-encoded). This is necessary to allow supporting
178 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
179 *
180 * To make it possible for filesystems to still find the correct directory entry
181 * despite not knowing the full on-disk name, we encode any filesystem-specific
182 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
183 * followed by the second-to-last ciphertext block of the filename. Due to the
184 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
185 * depends on the full plaintext. (Note that ciphertext stealing causes the
186 * last two blocks to appear "flipped".) This makes accidental collisions very
187 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
188 * share the same filesystem-specific hashes.
189 *
190 * However, this scheme isn't immune to intentional collisions, which can be
191 * created by anyone able to create arbitrary plaintext filenames and view them
192 * without the key. Making the "digest" be a real cryptographic hash like
193 * SHA-256 over the full ciphertext would prevent this, although it would be
194 * less efficient and harder to implement, especially since the filesystem would
195 * need to calculate it for each directory entry examined during a search.
196 */
197struct fscrypt_digested_name {
198 u32 hash;
199 u32 minor_hash;
200 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
201};
202
203/**
204 * fscrypt_match_name() - test whether the given name matches a directory entry
205 * @fname: the name being searched for
206 * @de_name: the name from the directory entry
207 * @de_name_len: the length of @de_name in bytes
208 *
209 * Normally @fname->disk_name will be set, and in that case we simply compare
210 * that to the name stored in the directory entry. The only exception is that
211 * if we don't have the key for an encrypted directory and a filename in it is
212 * very long, then we won't have the full disk_name and we'll instead need to
213 * match against the fscrypt_digested_name.
214 *
215 * Return: %true if the name matches, otherwise %false.
216 */
217static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
218 const u8 *de_name, u32 de_name_len)
219{
220 if (unlikely(!fname->disk_name.name)) {
221 const struct fscrypt_digested_name *n =
222 (const void *)fname->crypto_buf.name;
223 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
224 return false;
225 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
226 return false;
227 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
228 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
229 }
230
231 if (de_name_len != fname->disk_name.len)
232 return false;
233 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
234}
235
236/* bio.c */
237extern void fscrypt_decrypt_bio(struct bio *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530238extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
239 unsigned int);
240
241/* hooks.c */
242extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers081985b2019-03-20 11:39:10 -0700243extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
244 struct dentry *dentry);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530245extern int __fscrypt_prepare_rename(struct inode *old_dir,
246 struct dentry *old_dentry,
247 struct inode *new_dir,
248 struct dentry *new_dentry,
249 unsigned int flags);
Eric Biggers716c3702019-03-20 11:39:13 -0700250extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
251 struct fscrypt_name *fname);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530252extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
253 unsigned int max_len,
254 struct fscrypt_str *disk_link);
255extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
256 unsigned int len,
257 struct fscrypt_str *disk_link);
258extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
259 unsigned int max_size,
260 struct delayed_call *done);
261#else /* !CONFIG_FS_ENCRYPTION */
262
263static inline bool fscrypt_has_encryption_key(const struct inode *inode)
264{
265 return false;
266}
267
268static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
269{
270 return false;
271}
272
Eric Biggersdd038b32019-03-20 11:39:11 -0700273static inline void fscrypt_handle_d_move(struct dentry *dentry)
274{
275}
276
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530277/* crypto.c */
278static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
279{
280}
281
Eric Biggersfd0e18f2019-05-20 09:29:44 -0700282static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
283 unsigned int len,
284 unsigned int offs,
285 gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530286{
287 return ERR_PTR(-EOPNOTSUPP);
288}
289
Eric Biggersce7b7a22019-05-20 09:29:43 -0700290static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
291 struct page *page,
292 unsigned int len,
293 unsigned int offs, u64 lblk_num,
294 gfp_t gfp_flags)
295{
296 return -EOPNOTSUPP;
297}
298
Eric Biggers87806292019-05-20 09:29:47 -0700299static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
300 unsigned int len,
301 unsigned int offs)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530302{
303 return -EOPNOTSUPP;
304}
305
Eric Biggers45822362019-05-20 09:29:46 -0700306static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
307 struct page *page,
308 unsigned int len,
309 unsigned int offs, u64 lblk_num)
310{
311 return -EOPNOTSUPP;
312}
313
Eric Biggers2edab9e2019-05-20 09:29:39 -0700314static inline bool fscrypt_is_bounce_page(struct page *page)
315{
316 return false;
317}
318
319static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530320{
321 WARN_ON_ONCE(1);
322 return ERR_PTR(-EINVAL);
323}
324
Eric Biggers2edab9e2019-05-20 09:29:39 -0700325static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530326{
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530327}
328
329/* policy.c */
330static inline int fscrypt_ioctl_set_policy(struct file *filp,
331 const void __user *arg)
332{
333 return -EOPNOTSUPP;
334}
335
336static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
337{
338 return -EOPNOTSUPP;
339}
340
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700341static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
342 void __user *arg)
343{
344 return -EOPNOTSUPP;
345}
346
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530347static inline int fscrypt_has_permitted_context(struct inode *parent,
348 struct inode *child)
349{
350 return 0;
351}
352
353static inline int fscrypt_inherit_context(struct inode *parent,
354 struct inode *child,
355 void *fs_data, bool preload)
356{
357 return -EOPNOTSUPP;
358}
359
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700360/* keyring.c */
361static inline void fscrypt_sb_free(struct super_block *sb)
362{
363}
364
365static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
366{
367 return -EOPNOTSUPP;
368}
369
370static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
371{
372 return -EOPNOTSUPP;
373}
374
375static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
376 void __user *arg)
377{
378 return -EOPNOTSUPP;
379}
380
381static inline int fscrypt_ioctl_get_key_status(struct file *filp,
382 void __user *arg)
383{
384 return -EOPNOTSUPP;
385}
386
387static inline int fscrypt_register_key_removal_notifier(
388 struct notifier_block *nb)
389{
390 return 0;
391}
392
393static inline int fscrypt_unregister_key_removal_notifier(
394 struct notifier_block *nb)
395{
396 return 0;
397}
398
399/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530400static inline int fscrypt_get_encryption_info(struct inode *inode)
401{
402 return -EOPNOTSUPP;
403}
404
405static inline void fscrypt_put_encryption_info(struct inode *inode)
406{
407 return;
408}
409
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700410static inline void fscrypt_free_inode(struct inode *inode)
411{
412}
413
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700414static inline int fscrypt_drop_inode(struct inode *inode)
415{
416 return 0;
417}
418
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530419 /* fname.c */
420static inline int fscrypt_setup_filename(struct inode *dir,
421 const struct qstr *iname,
422 int lookup, struct fscrypt_name *fname)
423{
424 if (IS_ENCRYPTED(dir))
425 return -EOPNOTSUPP;
426
Eric Biggers716c3702019-03-20 11:39:13 -0700427 memset(fname, 0, sizeof(*fname));
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530428 fname->usr_fname = iname;
429 fname->disk_name.name = (unsigned char *)iname->name;
430 fname->disk_name.len = iname->len;
431 return 0;
432}
433
434static inline void fscrypt_free_filename(struct fscrypt_name *fname)
435{
436 return;
437}
438
439static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
440 u32 max_encrypted_len,
441 struct fscrypt_str *crypto_str)
442{
443 return -EOPNOTSUPP;
444}
445
446static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
447{
448 return;
449}
450
451static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
452 u32 hash, u32 minor_hash,
453 const struct fscrypt_str *iname,
454 struct fscrypt_str *oname)
455{
456 return -EOPNOTSUPP;
457}
458
459static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
460 const u8 *de_name, u32 de_name_len)
461{
462 /* Encryption support disabled; use standard comparison */
463 if (de_name_len != fname->disk_name.len)
464 return false;
465 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
466}
467
468/* bio.c */
469static inline void fscrypt_decrypt_bio(struct bio *bio)
470{
471}
472
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530473static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
474 sector_t pblk, unsigned int len)
475{
476 return -EOPNOTSUPP;
477}
478
479/* hooks.c */
480
481static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
482{
483 if (IS_ENCRYPTED(inode))
484 return -EOPNOTSUPP;
485 return 0;
486}
487
Eric Biggers081985b2019-03-20 11:39:10 -0700488static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
489 struct dentry *dentry)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530490{
491 return -EOPNOTSUPP;
492}
493
494static inline int __fscrypt_prepare_rename(struct inode *old_dir,
495 struct dentry *old_dentry,
496 struct inode *new_dir,
497 struct dentry *new_dentry,
498 unsigned int flags)
499{
500 return -EOPNOTSUPP;
501}
502
503static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggers716c3702019-03-20 11:39:13 -0700504 struct dentry *dentry,
505 struct fscrypt_name *fname)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530506{
507 return -EOPNOTSUPP;
508}
509
510static inline int __fscrypt_prepare_symlink(struct inode *dir,
511 unsigned int len,
512 unsigned int max_len,
513 struct fscrypt_str *disk_link)
514{
515 return -EOPNOTSUPP;
516}
517
518
519static inline int __fscrypt_encrypt_symlink(struct inode *inode,
520 const char *target,
521 unsigned int len,
522 struct fscrypt_str *disk_link)
523{
524 return -EOPNOTSUPP;
525}
526
527static inline const char *fscrypt_get_symlink(struct inode *inode,
528 const void *caddr,
529 unsigned int max_size,
530 struct delayed_call *done)
531{
532 return ERR_PTR(-EOPNOTSUPP);
533}
534#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700535
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700536/* inline_crypt.c */
537#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
538extern bool fscrypt_inode_uses_inline_crypto(const struct inode *inode);
539
540extern bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode);
541
542extern int fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
543 u64 first_lblk, gfp_t gfp_mask);
544
545extern int fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
546 const struct buffer_head *first_bh,
547 gfp_t gfp_mask);
548
549extern bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
550 u64 next_lblk);
551
552extern bool fscrypt_mergeable_bio_bh(struct bio *bio,
553 const struct buffer_head *next_bh);
554
555#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
556static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
557{
558 return false;
559}
560
561static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
562{
563 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
564}
565
566static inline int fscrypt_set_bio_crypt_ctx(struct bio *bio,
567 const struct inode *inode,
568 u64 first_lblk, gfp_t gfp_mask)
569{
570 return 0;
571}
572
573static inline int fscrypt_set_bio_crypt_ctx_bh(
574 struct bio *bio,
575 const struct buffer_head *first_bh,
576 gfp_t gfp_mask)
577{
578 return 0;
579}
580
581static inline bool fscrypt_mergeable_bio(struct bio *bio,
582 const struct inode *inode,
583 u64 next_lblk)
584{
585 return true;
586}
587
588static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
589 const struct buffer_head *next_bh)
590{
591 return true;
592}
593#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
594
Eric Biggersd293c3e2017-10-09 12:15:39 -0700595/**
596 * fscrypt_require_key - require an inode's encryption key
597 * @inode: the inode we need the key for
598 *
599 * If the inode is encrypted, set up its encryption key if not already done.
600 * Then require that the key be present and return -ENOKEY otherwise.
601 *
602 * No locks are needed, and the key will live as long as the struct inode --- so
603 * it won't go away from under you.
604 *
605 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
606 * if a problem occurred while setting up the encryption key.
607 */
608static inline int fscrypt_require_key(struct inode *inode)
609{
610 if (IS_ENCRYPTED(inode)) {
611 int err = fscrypt_get_encryption_info(inode);
612
613 if (err)
614 return err;
615 if (!fscrypt_has_encryption_key(inode))
616 return -ENOKEY;
617 }
618 return 0;
619}
Dave Chinner734f0d22017-10-09 12:15:34 -0700620
Eric Biggers0ea87a92017-10-09 12:15:41 -0700621/**
622 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
623 * @old_dentry: an existing dentry for the inode being linked
624 * @dir: the target directory
625 * @dentry: negative dentry for the target filename
626 *
627 * A new link can only be added to an encrypted directory if the directory's
628 * encryption key is available --- since otherwise we'd have no way to encrypt
629 * the filename. Therefore, we first set up the directory's encryption key (if
630 * not already done) and return an error if it's unavailable.
631 *
632 * We also verify that the link will not violate the constraint that all files
633 * in an encrypted directory tree use the same encryption policy.
634 *
635 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggers94e10682019-01-22 16:20:21 -0800636 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700637 * another -errno code.
638 */
639static inline int fscrypt_prepare_link(struct dentry *old_dentry,
640 struct inode *dir,
641 struct dentry *dentry)
642{
643 if (IS_ENCRYPTED(dir))
Eric Biggers081985b2019-03-20 11:39:10 -0700644 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700645 return 0;
646}
647
Eric Biggers94b26f32017-10-09 12:15:42 -0700648/**
649 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
650 * @old_dir: source directory
651 * @old_dentry: dentry for source file
652 * @new_dir: target directory
653 * @new_dentry: dentry for target location (may be negative unless exchanging)
654 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
655 *
656 * Prepare for ->rename() where the source and/or target directories may be
657 * encrypted. A new link can only be added to an encrypted directory if the
658 * directory's encryption key is available --- since otherwise we'd have no way
659 * to encrypt the filename. A rename to an existing name, on the other hand,
660 * *is* cryptographically possible without the key. However, we take the more
661 * conservative approach and just forbid all no-key renames.
662 *
663 * We also verify that the rename will not violate the constraint that all files
664 * in an encrypted directory tree use the same encryption policy.
665 *
Eric Biggers94e10682019-01-22 16:20:21 -0800666 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700667 * rename would cause inconsistent encryption policies, or another -errno code.
668 */
669static inline int fscrypt_prepare_rename(struct inode *old_dir,
670 struct dentry *old_dentry,
671 struct inode *new_dir,
672 struct dentry *new_dentry,
673 unsigned int flags)
674{
675 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
676 return __fscrypt_prepare_rename(old_dir, old_dentry,
677 new_dir, new_dentry, flags);
678 return 0;
679}
680
Eric Biggers32c3cf02017-10-09 12:15:43 -0700681/**
682 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
683 * @dir: directory being searched
684 * @dentry: filename being looked up
Eric Biggers716c3702019-03-20 11:39:13 -0700685 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700686 *
Eric Biggers716c3702019-03-20 11:39:13 -0700687 * Prepare for ->lookup() in a directory which may be encrypted by determining
688 * the name that will actually be used to search the directory on-disk. Lookups
689 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700690 * filenames are presented in encrypted form. Therefore, we'll try to set up
691 * the directory's encryption key, but even without it the lookup can continue.
692 *
Eric Biggers37b434e2019-03-20 11:39:09 -0700693 * This also installs a custom ->d_revalidate() method which will invalidate the
694 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700695 *
Eric Biggers716c3702019-03-20 11:39:13 -0700696 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
697 * correctly formed encoded ciphertext name, so a negative dentry should be
698 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700699 */
700static inline int fscrypt_prepare_lookup(struct inode *dir,
701 struct dentry *dentry,
Eric Biggers716c3702019-03-20 11:39:13 -0700702 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700703{
704 if (IS_ENCRYPTED(dir))
Eric Biggers716c3702019-03-20 11:39:13 -0700705 return __fscrypt_prepare_lookup(dir, dentry, fname);
706
707 memset(fname, 0, sizeof(*fname));
708 fname->usr_fname = &dentry->d_name;
709 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
710 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700711 return 0;
712}
713
Eric Biggers815dac32017-10-09 12:15:44 -0700714/**
715 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
716 * @dentry: dentry through which the inode is being changed
717 * @attr: attributes to change
718 *
719 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
720 * most attribute changes are allowed even without the encryption key. However,
721 * without the encryption key we do have to forbid truncates. This is needed
722 * because the size being truncated to may not be a multiple of the filesystem
723 * block size, and in that case we'd have to decrypt the final block, zero the
724 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
725 * filesystem block boundary, but it's simpler to just forbid all truncates ---
726 * and we already forbid all other contents modifications without the key.)
727 *
728 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
729 * if a problem occurred while setting up the encryption key.
730 */
731static inline int fscrypt_prepare_setattr(struct dentry *dentry,
732 struct iattr *attr)
733{
734 if (attr->ia_valid & ATTR_SIZE)
735 return fscrypt_require_key(d_inode(dentry));
736 return 0;
737}
738
Eric Biggers76e81d62018-01-05 10:45:01 -0800739/**
740 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
741 * @dir: directory in which the symlink is being created
742 * @target: plaintext symlink target
743 * @len: length of @target excluding null terminator
744 * @max_len: space the filesystem has available to store the symlink target
745 * @disk_link: (out) the on-disk symlink target being prepared
746 *
747 * This function computes the size the symlink target will require on-disk,
748 * stores it in @disk_link->len, and validates it against @max_len. An
749 * encrypted symlink may be longer than the original.
750 *
751 * Additionally, @disk_link->name is set to @target if the symlink will be
752 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
753 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
754 * on-disk target later. (The reason for the two-step process is that some
755 * filesystems need to know the size of the symlink target before creating the
756 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
757 *
758 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
759 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
760 * occurred while setting up the encryption key.
761 */
762static inline int fscrypt_prepare_symlink(struct inode *dir,
763 const char *target,
764 unsigned int len,
765 unsigned int max_len,
766 struct fscrypt_str *disk_link)
767{
768 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
769 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
770
771 disk_link->name = (unsigned char *)target;
772 disk_link->len = len + 1;
773 if (disk_link->len > max_len)
774 return -ENAMETOOLONG;
775 return 0;
776}
777
778/**
779 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
780 * @inode: symlink inode
781 * @target: plaintext symlink target
782 * @len: length of @target excluding null terminator
783 * @disk_link: (in/out) the on-disk symlink target being prepared
784 *
785 * If the symlink target needs to be encrypted, then this function encrypts it
786 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
787 * previously to compute @disk_link->len. If the filesystem did not allocate a
788 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
789 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
790 *
791 * Return: 0 on success, -errno on failure
792 */
793static inline int fscrypt_encrypt_symlink(struct inode *inode,
794 const char *target,
795 unsigned int len,
796 struct fscrypt_str *disk_link)
797{
798 if (IS_ENCRYPTED(inode))
799 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
800 return 0;
801}
802
Eric Biggers2edab9e2019-05-20 09:29:39 -0700803/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
804static inline void fscrypt_finalize_bounce_page(struct page **pagep)
805{
806 struct page *page = *pagep;
807
808 if (fscrypt_is_bounce_page(page)) {
809 *pagep = fscrypt_pagecache_page(page);
810 fscrypt_free_bounce_page(page);
811 }
812}
813
Dave Chinner734f0d22017-10-09 12:15:34 -0700814#endif /* _LINUX_FSCRYPT_H */