blob: 6ac092cc40675b39dc7eb0f78e70f958ef591956 [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);
Blagovest Kolenichev49875672020-04-20 07:29:59 -070068 int (*get_num_devices)(struct super_block *sb);
69 void (*get_devices)(struct super_block *sb,
70 struct request_queue **devs);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053071};
72
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053073static inline bool fscrypt_has_encryption_key(const struct inode *inode)
74{
Eric Biggers7a8861d2019-04-11 14:32:15 -070075 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
76 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053077}
78
79static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
80{
81 return inode->i_sb->s_cop->dummy_context &&
82 inode->i_sb->s_cop->dummy_context(inode);
83}
84
Eric Biggersdd038b32019-03-20 11:39:11 -070085/*
86 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
87 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
88 * must be cleared. Note that we don't have to support arbitrary moves of this
89 * flag because fscrypt doesn't allow encrypted aliases to be the source or
90 * target of a rename().
91 */
92static inline void fscrypt_handle_d_move(struct dentry *dentry)
93{
94 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
95}
96
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053097/* crypto.c */
98extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggersfd0e18f2019-05-20 09:29:44 -070099
100extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
101 unsigned int len,
102 unsigned int offs,
103 gfp_t gfp_flags);
Eric Biggersce7b7a22019-05-20 09:29:43 -0700104extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
105 struct page *page, unsigned int len,
106 unsigned int offs, u64 lblk_num,
107 gfp_t gfp_flags);
Eric Biggers87806292019-05-20 09:29:47 -0700108
109extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
110 unsigned int offs);
Eric Biggers45822362019-05-20 09:29:46 -0700111extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
112 struct page *page, unsigned int len,
113 unsigned int offs, u64 lblk_num);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530114
Eric Biggers2edab9e2019-05-20 09:29:39 -0700115static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530116{
Eric Biggers2edab9e2019-05-20 09:29:39 -0700117 return page->mapping == NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530118}
119
Eric Biggers2edab9e2019-05-20 09:29:39 -0700120static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
121{
122 return (struct page *)page_private(bounce_page);
123}
124
125extern void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530126
127/* policy.c */
128extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
129extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700130extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530131extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
132extern int fscrypt_inherit_context(struct inode *, struct inode *,
133 void *, bool);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700134/* keyring.c */
135extern void fscrypt_sb_free(struct super_block *sb);
136extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
137extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
138extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
139 void __user *arg);
140extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
141extern int fscrypt_register_key_removal_notifier(struct notifier_block *nb);
142extern int fscrypt_unregister_key_removal_notifier(struct notifier_block *nb);
143
144/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530145extern int fscrypt_get_encryption_info(struct inode *);
146extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700147extern void fscrypt_free_inode(struct inode *);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700148extern int fscrypt_drop_inode(struct inode *inode);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530149
150/* fname.c */
151extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
152 int lookup, struct fscrypt_name *);
153
154static inline void fscrypt_free_filename(struct fscrypt_name *fname)
155{
156 kfree(fname->crypto_buf.name);
157}
158
159extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
160 struct fscrypt_str *);
161extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
162extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
163 const struct fscrypt_str *, struct fscrypt_str *);
164
165#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
166
167/* Extracts the second-to-last ciphertext block; see explanation below */
168#define FSCRYPT_FNAME_DIGEST(name, len) \
169 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
170 FS_CRYPTO_BLOCK_SIZE))
171
172#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
173
174/**
175 * fscrypt_digested_name - alternate identifier for an on-disk filename
176 *
177 * When userspace lists an encrypted directory without access to the key,
178 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
179 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
180 * full ciphertext (base64-encoded). This is necessary to allow supporting
181 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
182 *
183 * To make it possible for filesystems to still find the correct directory entry
184 * despite not knowing the full on-disk name, we encode any filesystem-specific
185 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
186 * followed by the second-to-last ciphertext block of the filename. Due to the
187 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
188 * depends on the full plaintext. (Note that ciphertext stealing causes the
189 * last two blocks to appear "flipped".) This makes accidental collisions very
190 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
191 * share the same filesystem-specific hashes.
192 *
193 * However, this scheme isn't immune to intentional collisions, which can be
194 * created by anyone able to create arbitrary plaintext filenames and view them
195 * without the key. Making the "digest" be a real cryptographic hash like
196 * SHA-256 over the full ciphertext would prevent this, although it would be
197 * less efficient and harder to implement, especially since the filesystem would
198 * need to calculate it for each directory entry examined during a search.
199 */
200struct fscrypt_digested_name {
201 u32 hash;
202 u32 minor_hash;
203 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
204};
205
206/**
207 * fscrypt_match_name() - test whether the given name matches a directory entry
208 * @fname: the name being searched for
209 * @de_name: the name from the directory entry
210 * @de_name_len: the length of @de_name in bytes
211 *
212 * Normally @fname->disk_name will be set, and in that case we simply compare
213 * that to the name stored in the directory entry. The only exception is that
214 * if we don't have the key for an encrypted directory and a filename in it is
215 * very long, then we won't have the full disk_name and we'll instead need to
216 * match against the fscrypt_digested_name.
217 *
218 * Return: %true if the name matches, otherwise %false.
219 */
220static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
221 const u8 *de_name, u32 de_name_len)
222{
223 if (unlikely(!fname->disk_name.name)) {
224 const struct fscrypt_digested_name *n =
225 (const void *)fname->crypto_buf.name;
226 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
227 return false;
228 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
229 return false;
230 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
231 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
232 }
233
234 if (de_name_len != fname->disk_name.len)
235 return false;
236 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
237}
238
239/* bio.c */
240extern void fscrypt_decrypt_bio(struct bio *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530241extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
242 unsigned int);
243
244/* hooks.c */
245extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers081985b2019-03-20 11:39:10 -0700246extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
247 struct dentry *dentry);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530248extern int __fscrypt_prepare_rename(struct inode *old_dir,
249 struct dentry *old_dentry,
250 struct inode *new_dir,
251 struct dentry *new_dentry,
252 unsigned int flags);
Eric Biggers716c3702019-03-20 11:39:13 -0700253extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
254 struct fscrypt_name *fname);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530255extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
256 unsigned int max_len,
257 struct fscrypt_str *disk_link);
258extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
259 unsigned int len,
260 struct fscrypt_str *disk_link);
261extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
262 unsigned int max_size,
263 struct delayed_call *done);
264#else /* !CONFIG_FS_ENCRYPTION */
265
266static inline bool fscrypt_has_encryption_key(const struct inode *inode)
267{
268 return false;
269}
270
271static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
272{
273 return false;
274}
275
Eric Biggersdd038b32019-03-20 11:39:11 -0700276static inline void fscrypt_handle_d_move(struct dentry *dentry)
277{
278}
279
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530280/* crypto.c */
281static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
282{
283}
284
Eric Biggersfd0e18f2019-05-20 09:29:44 -0700285static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
286 unsigned int len,
287 unsigned int offs,
288 gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530289{
290 return ERR_PTR(-EOPNOTSUPP);
291}
292
Eric Biggersce7b7a22019-05-20 09:29:43 -0700293static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
294 struct page *page,
295 unsigned int len,
296 unsigned int offs, u64 lblk_num,
297 gfp_t gfp_flags)
298{
299 return -EOPNOTSUPP;
300}
301
Eric Biggers87806292019-05-20 09:29:47 -0700302static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
303 unsigned int len,
304 unsigned int offs)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530305{
306 return -EOPNOTSUPP;
307}
308
Eric Biggers45822362019-05-20 09:29:46 -0700309static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
310 struct page *page,
311 unsigned int len,
312 unsigned int offs, u64 lblk_num)
313{
314 return -EOPNOTSUPP;
315}
316
Eric Biggers2edab9e2019-05-20 09:29:39 -0700317static inline bool fscrypt_is_bounce_page(struct page *page)
318{
319 return false;
320}
321
322static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530323{
324 WARN_ON_ONCE(1);
325 return ERR_PTR(-EINVAL);
326}
327
Eric Biggers2edab9e2019-05-20 09:29:39 -0700328static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530329{
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530330}
331
332/* policy.c */
333static inline int fscrypt_ioctl_set_policy(struct file *filp,
334 const void __user *arg)
335{
336 return -EOPNOTSUPP;
337}
338
339static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
340{
341 return -EOPNOTSUPP;
342}
343
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700344static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
345 void __user *arg)
346{
347 return -EOPNOTSUPP;
348}
349
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530350static inline int fscrypt_has_permitted_context(struct inode *parent,
351 struct inode *child)
352{
353 return 0;
354}
355
356static inline int fscrypt_inherit_context(struct inode *parent,
357 struct inode *child,
358 void *fs_data, bool preload)
359{
360 return -EOPNOTSUPP;
361}
362
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700363/* keyring.c */
364static inline void fscrypt_sb_free(struct super_block *sb)
365{
366}
367
368static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
369{
370 return -EOPNOTSUPP;
371}
372
373static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
374{
375 return -EOPNOTSUPP;
376}
377
378static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
379 void __user *arg)
380{
381 return -EOPNOTSUPP;
382}
383
384static inline int fscrypt_ioctl_get_key_status(struct file *filp,
385 void __user *arg)
386{
387 return -EOPNOTSUPP;
388}
389
390static inline int fscrypt_register_key_removal_notifier(
391 struct notifier_block *nb)
392{
393 return 0;
394}
395
396static inline int fscrypt_unregister_key_removal_notifier(
397 struct notifier_block *nb)
398{
399 return 0;
400}
401
402/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530403static inline int fscrypt_get_encryption_info(struct inode *inode)
404{
405 return -EOPNOTSUPP;
406}
407
408static inline void fscrypt_put_encryption_info(struct inode *inode)
409{
410 return;
411}
412
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700413static inline void fscrypt_free_inode(struct inode *inode)
414{
415}
416
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700417static inline int fscrypt_drop_inode(struct inode *inode)
418{
419 return 0;
420}
421
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530422 /* fname.c */
423static inline int fscrypt_setup_filename(struct inode *dir,
424 const struct qstr *iname,
425 int lookup, struct fscrypt_name *fname)
426{
427 if (IS_ENCRYPTED(dir))
428 return -EOPNOTSUPP;
429
Eric Biggers716c3702019-03-20 11:39:13 -0700430 memset(fname, 0, sizeof(*fname));
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530431 fname->usr_fname = iname;
432 fname->disk_name.name = (unsigned char *)iname->name;
433 fname->disk_name.len = iname->len;
434 return 0;
435}
436
437static inline void fscrypt_free_filename(struct fscrypt_name *fname)
438{
439 return;
440}
441
442static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
443 u32 max_encrypted_len,
444 struct fscrypt_str *crypto_str)
445{
446 return -EOPNOTSUPP;
447}
448
449static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
450{
451 return;
452}
453
454static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
455 u32 hash, u32 minor_hash,
456 const struct fscrypt_str *iname,
457 struct fscrypt_str *oname)
458{
459 return -EOPNOTSUPP;
460}
461
462static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
463 const u8 *de_name, u32 de_name_len)
464{
465 /* Encryption support disabled; use standard comparison */
466 if (de_name_len != fname->disk_name.len)
467 return false;
468 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
469}
470
471/* bio.c */
472static inline void fscrypt_decrypt_bio(struct bio *bio)
473{
474}
475
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530476static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
477 sector_t pblk, unsigned int len)
478{
479 return -EOPNOTSUPP;
480}
481
482/* hooks.c */
483
484static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
485{
486 if (IS_ENCRYPTED(inode))
487 return -EOPNOTSUPP;
488 return 0;
489}
490
Eric Biggers081985b2019-03-20 11:39:10 -0700491static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
492 struct dentry *dentry)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530493{
494 return -EOPNOTSUPP;
495}
496
497static inline int __fscrypt_prepare_rename(struct inode *old_dir,
498 struct dentry *old_dentry,
499 struct inode *new_dir,
500 struct dentry *new_dentry,
501 unsigned int flags)
502{
503 return -EOPNOTSUPP;
504}
505
506static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggers716c3702019-03-20 11:39:13 -0700507 struct dentry *dentry,
508 struct fscrypt_name *fname)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530509{
510 return -EOPNOTSUPP;
511}
512
513static inline int __fscrypt_prepare_symlink(struct inode *dir,
514 unsigned int len,
515 unsigned int max_len,
516 struct fscrypt_str *disk_link)
517{
518 return -EOPNOTSUPP;
519}
520
521
522static inline int __fscrypt_encrypt_symlink(struct inode *inode,
523 const char *target,
524 unsigned int len,
525 struct fscrypt_str *disk_link)
526{
527 return -EOPNOTSUPP;
528}
529
530static inline const char *fscrypt_get_symlink(struct inode *inode,
531 const void *caddr,
532 unsigned int max_size,
533 struct delayed_call *done)
534{
535 return ERR_PTR(-EOPNOTSUPP);
536}
537#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700538
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700539/* inline_crypt.c */
540#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
541extern bool fscrypt_inode_uses_inline_crypto(const struct inode *inode);
542
543extern bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode);
544
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700545extern void fscrypt_set_bio_crypt_ctx(struct bio *bio,
546 const struct inode *inode,
547 u64 first_lblk, gfp_t gfp_mask);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700548
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700549extern void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
550 const struct buffer_head *first_bh,
551 gfp_t gfp_mask);
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700552
553extern bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
554 u64 next_lblk);
555
556extern bool fscrypt_mergeable_bio_bh(struct bio *bio,
557 const struct buffer_head *next_bh);
558
559#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
560static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
561{
562 return false;
563}
564
565static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
566{
567 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
568}
569
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700570static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
571 const struct inode *inode,
572 u64 first_lblk, gfp_t gfp_mask) { }
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700573
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700574static inline void fscrypt_set_bio_crypt_ctx_bh(
575 struct bio *bio,
576 const struct buffer_head *first_bh,
577 gfp_t gfp_mask) { }
Blagovest Kolenichev94699592020-03-27 03:19:47 -0700578
579static inline bool fscrypt_mergeable_bio(struct bio *bio,
580 const struct inode *inode,
581 u64 next_lblk)
582{
583 return true;
584}
585
586static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
587 const struct buffer_head *next_bh)
588{
589 return true;
590}
591#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
592
Blagovest Kolenichev49875672020-04-20 07:29:59 -0700593#if IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
594static inline bool
595fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
596{
597 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
598}
599#else
600static inline bool
601fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
602{
603 return false;
604}
605#endif
606
Eric Biggersd293c3e2017-10-09 12:15:39 -0700607/**
608 * fscrypt_require_key - require an inode's encryption key
609 * @inode: the inode we need the key for
610 *
611 * If the inode is encrypted, set up its encryption key if not already done.
612 * Then require that the key be present and return -ENOKEY otherwise.
613 *
614 * No locks are needed, and the key will live as long as the struct inode --- so
615 * it won't go away from under you.
616 *
617 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
618 * if a problem occurred while setting up the encryption key.
619 */
620static inline int fscrypt_require_key(struct inode *inode)
621{
622 if (IS_ENCRYPTED(inode)) {
623 int err = fscrypt_get_encryption_info(inode);
624
625 if (err)
626 return err;
627 if (!fscrypt_has_encryption_key(inode))
628 return -ENOKEY;
629 }
630 return 0;
631}
Dave Chinner734f0d22017-10-09 12:15:34 -0700632
Eric Biggers0ea87a92017-10-09 12:15:41 -0700633/**
634 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
635 * @old_dentry: an existing dentry for the inode being linked
636 * @dir: the target directory
637 * @dentry: negative dentry for the target filename
638 *
639 * A new link can only be added to an encrypted directory if the directory's
640 * encryption key is available --- since otherwise we'd have no way to encrypt
641 * the filename. Therefore, we first set up the directory's encryption key (if
642 * not already done) and return an error if it's unavailable.
643 *
644 * We also verify that the link will not violate the constraint that all files
645 * in an encrypted directory tree use the same encryption policy.
646 *
647 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggers94e10682019-01-22 16:20:21 -0800648 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700649 * another -errno code.
650 */
651static inline int fscrypt_prepare_link(struct dentry *old_dentry,
652 struct inode *dir,
653 struct dentry *dentry)
654{
655 if (IS_ENCRYPTED(dir))
Eric Biggers081985b2019-03-20 11:39:10 -0700656 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700657 return 0;
658}
659
Eric Biggers94b26f32017-10-09 12:15:42 -0700660/**
661 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
662 * @old_dir: source directory
663 * @old_dentry: dentry for source file
664 * @new_dir: target directory
665 * @new_dentry: dentry for target location (may be negative unless exchanging)
666 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
667 *
668 * Prepare for ->rename() where the source and/or target directories may be
669 * encrypted. A new link can only be added to an encrypted directory if the
670 * directory's encryption key is available --- since otherwise we'd have no way
671 * to encrypt the filename. A rename to an existing name, on the other hand,
672 * *is* cryptographically possible without the key. However, we take the more
673 * conservative approach and just forbid all no-key renames.
674 *
675 * We also verify that the rename will not violate the constraint that all files
676 * in an encrypted directory tree use the same encryption policy.
677 *
Eric Biggers94e10682019-01-22 16:20:21 -0800678 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700679 * rename would cause inconsistent encryption policies, or another -errno code.
680 */
681static inline int fscrypt_prepare_rename(struct inode *old_dir,
682 struct dentry *old_dentry,
683 struct inode *new_dir,
684 struct dentry *new_dentry,
685 unsigned int flags)
686{
687 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
688 return __fscrypt_prepare_rename(old_dir, old_dentry,
689 new_dir, new_dentry, flags);
690 return 0;
691}
692
Eric Biggers32c3cf02017-10-09 12:15:43 -0700693/**
694 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
695 * @dir: directory being searched
696 * @dentry: filename being looked up
Eric Biggers716c3702019-03-20 11:39:13 -0700697 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700698 *
Eric Biggers716c3702019-03-20 11:39:13 -0700699 * Prepare for ->lookup() in a directory which may be encrypted by determining
700 * the name that will actually be used to search the directory on-disk. Lookups
701 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700702 * filenames are presented in encrypted form. Therefore, we'll try to set up
703 * the directory's encryption key, but even without it the lookup can continue.
704 *
Eric Biggers37b434e2019-03-20 11:39:09 -0700705 * This also installs a custom ->d_revalidate() method which will invalidate the
706 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700707 *
Eric Biggers716c3702019-03-20 11:39:13 -0700708 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
709 * correctly formed encoded ciphertext name, so a negative dentry should be
710 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700711 */
712static inline int fscrypt_prepare_lookup(struct inode *dir,
713 struct dentry *dentry,
Eric Biggers716c3702019-03-20 11:39:13 -0700714 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700715{
716 if (IS_ENCRYPTED(dir))
Eric Biggers716c3702019-03-20 11:39:13 -0700717 return __fscrypt_prepare_lookup(dir, dentry, fname);
718
719 memset(fname, 0, sizeof(*fname));
720 fname->usr_fname = &dentry->d_name;
721 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
722 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700723 return 0;
724}
725
Eric Biggers815dac32017-10-09 12:15:44 -0700726/**
727 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
728 * @dentry: dentry through which the inode is being changed
729 * @attr: attributes to change
730 *
731 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
732 * most attribute changes are allowed even without the encryption key. However,
733 * without the encryption key we do have to forbid truncates. This is needed
734 * because the size being truncated to may not be a multiple of the filesystem
735 * block size, and in that case we'd have to decrypt the final block, zero the
736 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
737 * filesystem block boundary, but it's simpler to just forbid all truncates ---
738 * and we already forbid all other contents modifications without the key.)
739 *
740 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
741 * if a problem occurred while setting up the encryption key.
742 */
743static inline int fscrypt_prepare_setattr(struct dentry *dentry,
744 struct iattr *attr)
745{
746 if (attr->ia_valid & ATTR_SIZE)
747 return fscrypt_require_key(d_inode(dentry));
748 return 0;
749}
750
Eric Biggers76e81d62018-01-05 10:45:01 -0800751/**
752 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
753 * @dir: directory in which the symlink is being created
754 * @target: plaintext symlink target
755 * @len: length of @target excluding null terminator
756 * @max_len: space the filesystem has available to store the symlink target
757 * @disk_link: (out) the on-disk symlink target being prepared
758 *
759 * This function computes the size the symlink target will require on-disk,
760 * stores it in @disk_link->len, and validates it against @max_len. An
761 * encrypted symlink may be longer than the original.
762 *
763 * Additionally, @disk_link->name is set to @target if the symlink will be
764 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
765 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
766 * on-disk target later. (The reason for the two-step process is that some
767 * filesystems need to know the size of the symlink target before creating the
768 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
769 *
770 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
771 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
772 * occurred while setting up the encryption key.
773 */
774static inline int fscrypt_prepare_symlink(struct inode *dir,
775 const char *target,
776 unsigned int len,
777 unsigned int max_len,
778 struct fscrypt_str *disk_link)
779{
780 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
781 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
782
783 disk_link->name = (unsigned char *)target;
784 disk_link->len = len + 1;
785 if (disk_link->len > max_len)
786 return -ENAMETOOLONG;
787 return 0;
788}
789
790/**
791 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
792 * @inode: symlink inode
793 * @target: plaintext symlink target
794 * @len: length of @target excluding null terminator
795 * @disk_link: (in/out) the on-disk symlink target being prepared
796 *
797 * If the symlink target needs to be encrypted, then this function encrypts it
798 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
799 * previously to compute @disk_link->len. If the filesystem did not allocate a
800 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
801 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
802 *
803 * Return: 0 on success, -errno on failure
804 */
805static inline int fscrypt_encrypt_symlink(struct inode *inode,
806 const char *target,
807 unsigned int len,
808 struct fscrypt_str *disk_link)
809{
810 if (IS_ENCRYPTED(inode))
811 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
812 return 0;
813}
814
Eric Biggers2edab9e2019-05-20 09:29:39 -0700815/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
816static inline void fscrypt_finalize_bounce_page(struct page **pagep)
817{
818 struct page *page = *pagep;
819
820 if (fscrypt_is_bounce_page(page)) {
821 *pagep = fscrypt_pagecache_page(page);
822 fscrypt_free_bounce_page(page);
823 }
824}
825
Dave Chinner734f0d22017-10-09 12:15:34 -0700826#endif /* _LINUX_FSCRYPT_H */