blob: 2aad2f949b9ea1e3cdf914fafd7a45e2bd311e03 [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>
Eric Biggersa48b7ad2019-08-04 19:35:43 -070019#include <uapi/linux/fscrypt.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080020
21#define FS_CRYPTO_BLOCK_SIZE 16
22
Eric Biggers542060c2018-01-05 10:44:55 -080023struct fscrypt_ctx;
Eric Biggers46f47e42017-01-24 10:58:06 -080024struct fscrypt_info;
25
Eric Biggers46f47e42017-01-24 10:58:06 -080026struct fscrypt_str {
27 unsigned char *name;
28 u32 len;
29};
30
31struct fscrypt_name {
32 const struct qstr *usr_fname;
33 struct fscrypt_str disk_name;
34 u32 hash;
35 u32 minor_hash;
36 struct fscrypt_str crypto_buf;
Eric Biggers716c3702019-03-20 11:39:13 -070037 bool is_ciphertext_name;
Eric Biggers46f47e42017-01-24 10:58:06 -080038};
39
40#define FSTR_INIT(n, l) { .name = n, .len = l }
41#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
42#define fname_name(p) ((p)->disk_name.name)
43#define fname_len(p) ((p)->disk_name.len)
44
Tahsin Erdoganaf652072017-07-06 00:01:59 -040045/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
Eric Biggers73ce50d2019-08-04 19:35:47 -070046#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
Tahsin Erdoganaf652072017-07-06 00:01:59 -040047
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053048#ifdef CONFIG_FS_ENCRYPTION
49/*
50 * fscrypt superblock flags
51 */
52#define FS_CFLG_OWN_PAGES (1U << 1)
53
54/*
55 * crypto operations for filesystems
56 */
57struct fscrypt_operations {
58 unsigned int flags;
59 const char *key_prefix;
60 int (*get_context)(struct inode *, void *, size_t);
61 int (*set_context)(struct inode *, const void *, size_t, void *);
62 bool (*dummy_context)(struct inode *);
63 bool (*empty_dir)(struct inode *);
64 unsigned int max_namelen;
65};
66
Eric Biggersd568ccc2019-05-20 09:29:40 -070067/* Decryption work */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053068struct fscrypt_ctx {
69 union {
70 struct {
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053071 struct bio *bio;
72 struct work_struct work;
Eric Biggersd568ccc2019-05-20 09:29:40 -070073 };
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053074 struct list_head free_list; /* Free list */
75 };
76 u8 flags; /* Flags */
77};
78
79static inline bool fscrypt_has_encryption_key(const struct inode *inode)
80{
Eric Biggers7a8861d2019-04-11 14:32:15 -070081 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
82 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +053083}
84
85static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
86{
87 return inode->i_sb->s_cop->dummy_context &&
88 inode->i_sb->s_cop->dummy_context(inode);
89}
90
Eric Biggersdd038b32019-03-20 11:39:11 -070091/*
92 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
93 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
94 * must be cleared. Note that we don't have to support arbitrary moves of this
95 * flag because fscrypt doesn't allow encrypted aliases to be the source or
96 * target of a rename().
97 */
98static inline void fscrypt_handle_d_move(struct dentry *dentry)
99{
100 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
101}
102
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530103/* crypto.c */
104extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggers95e2ce52019-03-18 10:23:33 -0700105extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530106extern void fscrypt_release_ctx(struct fscrypt_ctx *);
Eric Biggersfd0e18f2019-05-20 09:29:44 -0700107
108extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
109 unsigned int len,
110 unsigned int offs,
111 gfp_t gfp_flags);
Eric Biggersce7b7a22019-05-20 09:29:43 -0700112extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
113 struct page *page, unsigned int len,
114 unsigned int offs, u64 lblk_num,
115 gfp_t gfp_flags);
Eric Biggers87806292019-05-20 09:29:47 -0700116
117extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
118 unsigned int offs);
Eric Biggers45822362019-05-20 09:29:46 -0700119extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
120 struct page *page, unsigned int len,
121 unsigned int offs, u64 lblk_num);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530122
Eric Biggers2edab9e2019-05-20 09:29:39 -0700123static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530124{
Eric Biggers2edab9e2019-05-20 09:29:39 -0700125 return page->mapping == NULL;
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530126}
127
Eric Biggers2edab9e2019-05-20 09:29:39 -0700128static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
129{
130 return (struct page *)page_private(bounce_page);
131}
132
133extern void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530134
135/* policy.c */
136extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
137extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
Eric Biggers73ce50d2019-08-04 19:35:47 -0700138extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530139extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
140extern int fscrypt_inherit_context(struct inode *, struct inode *,
141 void *, bool);
Eric Biggers98462552019-08-04 19:35:46 -0700142/* keyring.c */
143extern void fscrypt_sb_free(struct super_block *sb);
144extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
Eric Biggerscacc84e2019-08-04 19:35:46 -0700145extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
Eric Biggers080389c2019-08-04 19:35:47 -0700146extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
147 void __user *arg);
Eric Biggersdbfc6582019-08-04 19:35:46 -0700148extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
Eric Biggers4932f532019-10-09 14:35:48 -0700149extern int fscrypt_register_key_removal_notifier(struct notifier_block *nb);
150extern int fscrypt_unregister_key_removal_notifier(struct notifier_block *nb);
Eric Biggers98462552019-08-04 19:35:46 -0700151
Eric Biggersc677e572019-08-04 19:35:45 -0700152/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530153extern int fscrypt_get_encryption_info(struct inode *);
154extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700155extern void fscrypt_free_inode(struct inode *);
Eric Biggerscacc84e2019-08-04 19:35:46 -0700156extern int fscrypt_drop_inode(struct inode *inode);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530157
158/* fname.c */
159extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
160 int lookup, struct fscrypt_name *);
161
162static inline void fscrypt_free_filename(struct fscrypt_name *fname)
163{
164 kfree(fname->crypto_buf.name);
165}
166
167extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
168 struct fscrypt_str *);
169extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
170extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
171 const struct fscrypt_str *, struct fscrypt_str *);
172
173#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
174
175/* Extracts the second-to-last ciphertext block; see explanation below */
176#define FSCRYPT_FNAME_DIGEST(name, len) \
177 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
178 FS_CRYPTO_BLOCK_SIZE))
179
180#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
181
182/**
183 * fscrypt_digested_name - alternate identifier for an on-disk filename
184 *
185 * When userspace lists an encrypted directory without access to the key,
186 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
187 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
188 * full ciphertext (base64-encoded). This is necessary to allow supporting
189 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
190 *
191 * To make it possible for filesystems to still find the correct directory entry
192 * despite not knowing the full on-disk name, we encode any filesystem-specific
193 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
194 * followed by the second-to-last ciphertext block of the filename. Due to the
195 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
196 * depends on the full plaintext. (Note that ciphertext stealing causes the
197 * last two blocks to appear "flipped".) This makes accidental collisions very
198 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
199 * share the same filesystem-specific hashes.
200 *
201 * However, this scheme isn't immune to intentional collisions, which can be
202 * created by anyone able to create arbitrary plaintext filenames and view them
203 * without the key. Making the "digest" be a real cryptographic hash like
204 * SHA-256 over the full ciphertext would prevent this, although it would be
205 * less efficient and harder to implement, especially since the filesystem would
206 * need to calculate it for each directory entry examined during a search.
207 */
208struct fscrypt_digested_name {
209 u32 hash;
210 u32 minor_hash;
211 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
212};
213
214/**
215 * fscrypt_match_name() - test whether the given name matches a directory entry
216 * @fname: the name being searched for
217 * @de_name: the name from the directory entry
218 * @de_name_len: the length of @de_name in bytes
219 *
220 * Normally @fname->disk_name will be set, and in that case we simply compare
221 * that to the name stored in the directory entry. The only exception is that
222 * if we don't have the key for an encrypted directory and a filename in it is
223 * very long, then we won't have the full disk_name and we'll instead need to
224 * match against the fscrypt_digested_name.
225 *
226 * Return: %true if the name matches, otherwise %false.
227 */
228static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
229 const u8 *de_name, u32 de_name_len)
230{
231 if (unlikely(!fname->disk_name.name)) {
232 const struct fscrypt_digested_name *n =
233 (const void *)fname->crypto_buf.name;
234 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
235 return false;
236 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
237 return false;
238 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
239 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
240 }
241
242 if (de_name_len != fname->disk_name.len)
243 return false;
244 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
245}
246
247/* bio.c */
248extern void fscrypt_decrypt_bio(struct bio *);
249extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
250 struct bio *bio);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530251extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
252 unsigned int);
253
254/* hooks.c */
255extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers081985b2019-03-20 11:39:10 -0700256extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
257 struct dentry *dentry);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530258extern int __fscrypt_prepare_rename(struct inode *old_dir,
259 struct dentry *old_dentry,
260 struct inode *new_dir,
261 struct dentry *new_dentry,
262 unsigned int flags);
Eric Biggers716c3702019-03-20 11:39:13 -0700263extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
264 struct fscrypt_name *fname);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530265extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
266 unsigned int max_len,
267 struct fscrypt_str *disk_link);
268extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
269 unsigned int len,
270 struct fscrypt_str *disk_link);
271extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
272 unsigned int max_size,
273 struct delayed_call *done);
274#else /* !CONFIG_FS_ENCRYPTION */
275
276static inline bool fscrypt_has_encryption_key(const struct inode *inode)
277{
278 return false;
279}
280
281static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
282{
283 return false;
284}
285
Eric Biggersdd038b32019-03-20 11:39:11 -0700286static inline void fscrypt_handle_d_move(struct dentry *dentry)
287{
288}
289
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530290/* crypto.c */
291static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
292{
293}
294
Eric Biggers95e2ce52019-03-18 10:23:33 -0700295static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530296{
297 return ERR_PTR(-EOPNOTSUPP);
298}
299
300static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
301{
302 return;
303}
304
Eric Biggersfd0e18f2019-05-20 09:29:44 -0700305static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
306 unsigned int len,
307 unsigned int offs,
308 gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530309{
310 return ERR_PTR(-EOPNOTSUPP);
311}
312
Eric Biggersce7b7a22019-05-20 09:29:43 -0700313static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
314 struct page *page,
315 unsigned int len,
316 unsigned int offs, u64 lblk_num,
317 gfp_t gfp_flags)
318{
319 return -EOPNOTSUPP;
320}
321
Eric Biggers87806292019-05-20 09:29:47 -0700322static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
323 unsigned int len,
324 unsigned int offs)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530325{
326 return -EOPNOTSUPP;
327}
328
Eric Biggers45822362019-05-20 09:29:46 -0700329static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
330 struct page *page,
331 unsigned int len,
332 unsigned int offs, u64 lblk_num)
333{
334 return -EOPNOTSUPP;
335}
336
Eric Biggers2edab9e2019-05-20 09:29:39 -0700337static inline bool fscrypt_is_bounce_page(struct page *page)
338{
339 return false;
340}
341
342static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530343{
344 WARN_ON_ONCE(1);
345 return ERR_PTR(-EINVAL);
346}
347
Eric Biggers2edab9e2019-05-20 09:29:39 -0700348static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530349{
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530350}
351
352/* policy.c */
353static inline int fscrypt_ioctl_set_policy(struct file *filp,
354 const void __user *arg)
355{
356 return -EOPNOTSUPP;
357}
358
359static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
360{
361 return -EOPNOTSUPP;
362}
363
Eric Biggers73ce50d2019-08-04 19:35:47 -0700364static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
365 void __user *arg)
366{
367 return -EOPNOTSUPP;
368}
369
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530370static inline int fscrypt_has_permitted_context(struct inode *parent,
371 struct inode *child)
372{
373 return 0;
374}
375
376static inline int fscrypt_inherit_context(struct inode *parent,
377 struct inode *child,
378 void *fs_data, bool preload)
379{
380 return -EOPNOTSUPP;
381}
382
Eric Biggers98462552019-08-04 19:35:46 -0700383/* keyring.c */
384static inline void fscrypt_sb_free(struct super_block *sb)
385{
386}
387
388static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
389{
390 return -EOPNOTSUPP;
391}
392
Eric Biggerscacc84e2019-08-04 19:35:46 -0700393static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
394{
395 return -EOPNOTSUPP;
396}
397
Eric Biggers080389c2019-08-04 19:35:47 -0700398static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
399 void __user *arg)
400{
401 return -EOPNOTSUPP;
402}
403
Eric Biggersdbfc6582019-08-04 19:35:46 -0700404static inline int fscrypt_ioctl_get_key_status(struct file *filp,
405 void __user *arg)
406{
407 return -EOPNOTSUPP;
408}
409
Eric Biggers4932f532019-10-09 14:35:48 -0700410static inline int fscrypt_register_key_removal_notifier(
411 struct notifier_block *nb)
412{
413 return 0;
414}
415
416static inline int fscrypt_unregister_key_removal_notifier(
417 struct notifier_block *nb)
418{
419 return 0;
420}
421
Eric Biggersc677e572019-08-04 19:35:45 -0700422/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530423static inline int fscrypt_get_encryption_info(struct inode *inode)
424{
425 return -EOPNOTSUPP;
426}
427
428static inline void fscrypt_put_encryption_info(struct inode *inode)
429{
430 return;
431}
432
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700433static inline void fscrypt_free_inode(struct inode *inode)
434{
435}
436
Eric Biggerscacc84e2019-08-04 19:35:46 -0700437static inline int fscrypt_drop_inode(struct inode *inode)
438{
439 return 0;
440}
441
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530442 /* fname.c */
443static inline int fscrypt_setup_filename(struct inode *dir,
444 const struct qstr *iname,
445 int lookup, struct fscrypt_name *fname)
446{
447 if (IS_ENCRYPTED(dir))
448 return -EOPNOTSUPP;
449
Eric Biggers716c3702019-03-20 11:39:13 -0700450 memset(fname, 0, sizeof(*fname));
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530451 fname->usr_fname = iname;
452 fname->disk_name.name = (unsigned char *)iname->name;
453 fname->disk_name.len = iname->len;
454 return 0;
455}
456
457static inline void fscrypt_free_filename(struct fscrypt_name *fname)
458{
459 return;
460}
461
462static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
463 u32 max_encrypted_len,
464 struct fscrypt_str *crypto_str)
465{
466 return -EOPNOTSUPP;
467}
468
469static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
470{
471 return;
472}
473
474static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
475 u32 hash, u32 minor_hash,
476 const struct fscrypt_str *iname,
477 struct fscrypt_str *oname)
478{
479 return -EOPNOTSUPP;
480}
481
482static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
483 const u8 *de_name, u32 de_name_len)
484{
485 /* Encryption support disabled; use standard comparison */
486 if (de_name_len != fname->disk_name.len)
487 return false;
488 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
489}
490
491/* bio.c */
492static inline void fscrypt_decrypt_bio(struct bio *bio)
493{
494}
495
496static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
497 struct bio *bio)
498{
499}
500
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530501static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
502 sector_t pblk, unsigned int len)
503{
504 return -EOPNOTSUPP;
505}
506
507/* hooks.c */
508
509static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
510{
511 if (IS_ENCRYPTED(inode))
512 return -EOPNOTSUPP;
513 return 0;
514}
515
Eric Biggers081985b2019-03-20 11:39:10 -0700516static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
517 struct dentry *dentry)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530518{
519 return -EOPNOTSUPP;
520}
521
522static inline int __fscrypt_prepare_rename(struct inode *old_dir,
523 struct dentry *old_dentry,
524 struct inode *new_dir,
525 struct dentry *new_dentry,
526 unsigned int flags)
527{
528 return -EOPNOTSUPP;
529}
530
531static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggers716c3702019-03-20 11:39:13 -0700532 struct dentry *dentry,
533 struct fscrypt_name *fname)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530534{
535 return -EOPNOTSUPP;
536}
537
538static inline int __fscrypt_prepare_symlink(struct inode *dir,
539 unsigned int len,
540 unsigned int max_len,
541 struct fscrypt_str *disk_link)
542{
543 return -EOPNOTSUPP;
544}
545
546
547static inline int __fscrypt_encrypt_symlink(struct inode *inode,
548 const char *target,
549 unsigned int len,
550 struct fscrypt_str *disk_link)
551{
552 return -EOPNOTSUPP;
553}
554
555static inline const char *fscrypt_get_symlink(struct inode *inode,
556 const void *caddr,
557 unsigned int max_size,
558 struct delayed_call *done)
559{
560 return ERR_PTR(-EOPNOTSUPP);
561}
562#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700563
Eric Biggersd293c3e2017-10-09 12:15:39 -0700564/**
565 * fscrypt_require_key - require an inode's encryption key
566 * @inode: the inode we need the key for
567 *
568 * If the inode is encrypted, set up its encryption key if not already done.
569 * Then require that the key be present and return -ENOKEY otherwise.
570 *
571 * No locks are needed, and the key will live as long as the struct inode --- so
572 * it won't go away from under you.
573 *
574 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
575 * if a problem occurred while setting up the encryption key.
576 */
577static inline int fscrypt_require_key(struct inode *inode)
578{
579 if (IS_ENCRYPTED(inode)) {
580 int err = fscrypt_get_encryption_info(inode);
581
582 if (err)
583 return err;
584 if (!fscrypt_has_encryption_key(inode))
585 return -ENOKEY;
586 }
587 return 0;
588}
Dave Chinner734f0d22017-10-09 12:15:34 -0700589
Eric Biggers0ea87a92017-10-09 12:15:41 -0700590/**
591 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
592 * @old_dentry: an existing dentry for the inode being linked
593 * @dir: the target directory
594 * @dentry: negative dentry for the target filename
595 *
596 * A new link can only be added to an encrypted directory if the directory's
597 * encryption key is available --- since otherwise we'd have no way to encrypt
598 * the filename. Therefore, we first set up the directory's encryption key (if
599 * not already done) and return an error if it's unavailable.
600 *
601 * We also verify that the link will not violate the constraint that all files
602 * in an encrypted directory tree use the same encryption policy.
603 *
604 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggers94e10682019-01-22 16:20:21 -0800605 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700606 * another -errno code.
607 */
608static inline int fscrypt_prepare_link(struct dentry *old_dentry,
609 struct inode *dir,
610 struct dentry *dentry)
611{
612 if (IS_ENCRYPTED(dir))
Eric Biggers081985b2019-03-20 11:39:10 -0700613 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700614 return 0;
615}
616
Eric Biggers94b26f32017-10-09 12:15:42 -0700617/**
618 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
619 * @old_dir: source directory
620 * @old_dentry: dentry for source file
621 * @new_dir: target directory
622 * @new_dentry: dentry for target location (may be negative unless exchanging)
623 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
624 *
625 * Prepare for ->rename() where the source and/or target directories may be
626 * encrypted. A new link can only be added to an encrypted directory if the
627 * directory's encryption key is available --- since otherwise we'd have no way
628 * to encrypt the filename. A rename to an existing name, on the other hand,
629 * *is* cryptographically possible without the key. However, we take the more
630 * conservative approach and just forbid all no-key renames.
631 *
632 * We also verify that the rename will not violate the constraint that all files
633 * in an encrypted directory tree use the same encryption policy.
634 *
Eric Biggers94e10682019-01-22 16:20:21 -0800635 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700636 * rename would cause inconsistent encryption policies, or another -errno code.
637 */
638static inline int fscrypt_prepare_rename(struct inode *old_dir,
639 struct dentry *old_dentry,
640 struct inode *new_dir,
641 struct dentry *new_dentry,
642 unsigned int flags)
643{
644 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
645 return __fscrypt_prepare_rename(old_dir, old_dentry,
646 new_dir, new_dentry, flags);
647 return 0;
648}
649
Eric Biggers32c3cf02017-10-09 12:15:43 -0700650/**
651 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
652 * @dir: directory being searched
653 * @dentry: filename being looked up
Eric Biggers716c3702019-03-20 11:39:13 -0700654 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700655 *
Eric Biggers716c3702019-03-20 11:39:13 -0700656 * Prepare for ->lookup() in a directory which may be encrypted by determining
657 * the name that will actually be used to search the directory on-disk. Lookups
658 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700659 * filenames are presented in encrypted form. Therefore, we'll try to set up
660 * the directory's encryption key, but even without it the lookup can continue.
661 *
Eric Biggers37b434e2019-03-20 11:39:09 -0700662 * This also installs a custom ->d_revalidate() method which will invalidate the
663 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700664 *
Eric Biggers716c3702019-03-20 11:39:13 -0700665 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
666 * correctly formed encoded ciphertext name, so a negative dentry should be
667 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700668 */
669static inline int fscrypt_prepare_lookup(struct inode *dir,
670 struct dentry *dentry,
Eric Biggers716c3702019-03-20 11:39:13 -0700671 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700672{
673 if (IS_ENCRYPTED(dir))
Eric Biggers716c3702019-03-20 11:39:13 -0700674 return __fscrypt_prepare_lookup(dir, dentry, fname);
675
676 memset(fname, 0, sizeof(*fname));
677 fname->usr_fname = &dentry->d_name;
678 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
679 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700680 return 0;
681}
682
Eric Biggers815dac32017-10-09 12:15:44 -0700683/**
684 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
685 * @dentry: dentry through which the inode is being changed
686 * @attr: attributes to change
687 *
688 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
689 * most attribute changes are allowed even without the encryption key. However,
690 * without the encryption key we do have to forbid truncates. This is needed
691 * because the size being truncated to may not be a multiple of the filesystem
692 * block size, and in that case we'd have to decrypt the final block, zero the
693 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
694 * filesystem block boundary, but it's simpler to just forbid all truncates ---
695 * and we already forbid all other contents modifications without the key.)
696 *
697 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
698 * if a problem occurred while setting up the encryption key.
699 */
700static inline int fscrypt_prepare_setattr(struct dentry *dentry,
701 struct iattr *attr)
702{
703 if (attr->ia_valid & ATTR_SIZE)
704 return fscrypt_require_key(d_inode(dentry));
705 return 0;
706}
707
Eric Biggers76e81d62018-01-05 10:45:01 -0800708/**
709 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
710 * @dir: directory in which the symlink is being created
711 * @target: plaintext symlink target
712 * @len: length of @target excluding null terminator
713 * @max_len: space the filesystem has available to store the symlink target
714 * @disk_link: (out) the on-disk symlink target being prepared
715 *
716 * This function computes the size the symlink target will require on-disk,
717 * stores it in @disk_link->len, and validates it against @max_len. An
718 * encrypted symlink may be longer than the original.
719 *
720 * Additionally, @disk_link->name is set to @target if the symlink will be
721 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
722 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
723 * on-disk target later. (The reason for the two-step process is that some
724 * filesystems need to know the size of the symlink target before creating the
725 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
726 *
727 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
728 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
729 * occurred while setting up the encryption key.
730 */
731static inline int fscrypt_prepare_symlink(struct inode *dir,
732 const char *target,
733 unsigned int len,
734 unsigned int max_len,
735 struct fscrypt_str *disk_link)
736{
737 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
738 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
739
740 disk_link->name = (unsigned char *)target;
741 disk_link->len = len + 1;
742 if (disk_link->len > max_len)
743 return -ENAMETOOLONG;
744 return 0;
745}
746
747/**
748 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
749 * @inode: symlink inode
750 * @target: plaintext symlink target
751 * @len: length of @target excluding null terminator
752 * @disk_link: (in/out) the on-disk symlink target being prepared
753 *
754 * If the symlink target needs to be encrypted, then this function encrypts it
755 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
756 * previously to compute @disk_link->len. If the filesystem did not allocate a
757 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
758 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
759 *
760 * Return: 0 on success, -errno on failure
761 */
762static inline int fscrypt_encrypt_symlink(struct inode *inode,
763 const char *target,
764 unsigned int len,
765 struct fscrypt_str *disk_link)
766{
767 if (IS_ENCRYPTED(inode))
768 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
769 return 0;
770}
771
Eric Biggers2edab9e2019-05-20 09:29:39 -0700772/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
773static inline void fscrypt_finalize_bounce_page(struct page **pagep)
774{
775 struct page *page = *pagep;
776
777 if (fscrypt_is_bounce_page(page)) {
778 *pagep = fscrypt_pagecache_page(page);
779 fscrypt_free_bounce_page(page);
780 }
781}
782
Dave Chinner734f0d22017-10-09 12:15:34 -0700783#endif /* _LINUX_FSCRYPT_H */