blob: 4131117f1c2f73282e8e7c187ac0e906d6f79758 [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(). */
46#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
47
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 *);
138extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
139extern int fscrypt_inherit_context(struct inode *, struct inode *,
140 void *, bool);
Eric Biggersc677e572019-08-04 19:35:45 -0700141/* 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 *);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530145
146/* fname.c */
147extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
148 int lookup, struct fscrypt_name *);
149
150static inline void fscrypt_free_filename(struct fscrypt_name *fname)
151{
152 kfree(fname->crypto_buf.name);
153}
154
155extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
156 struct fscrypt_str *);
157extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
158extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
159 const struct fscrypt_str *, struct fscrypt_str *);
160
161#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
162
163/* Extracts the second-to-last ciphertext block; see explanation below */
164#define FSCRYPT_FNAME_DIGEST(name, len) \
165 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
166 FS_CRYPTO_BLOCK_SIZE))
167
168#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
169
170/**
171 * fscrypt_digested_name - alternate identifier for an on-disk filename
172 *
173 * When userspace lists an encrypted directory without access to the key,
174 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
175 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
176 * full ciphertext (base64-encoded). This is necessary to allow supporting
177 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
178 *
179 * To make it possible for filesystems to still find the correct directory entry
180 * despite not knowing the full on-disk name, we encode any filesystem-specific
181 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
182 * followed by the second-to-last ciphertext block of the filename. Due to the
183 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
184 * depends on the full plaintext. (Note that ciphertext stealing causes the
185 * last two blocks to appear "flipped".) This makes accidental collisions very
186 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
187 * share the same filesystem-specific hashes.
188 *
189 * However, this scheme isn't immune to intentional collisions, which can be
190 * created by anyone able to create arbitrary plaintext filenames and view them
191 * without the key. Making the "digest" be a real cryptographic hash like
192 * SHA-256 over the full ciphertext would prevent this, although it would be
193 * less efficient and harder to implement, especially since the filesystem would
194 * need to calculate it for each directory entry examined during a search.
195 */
196struct fscrypt_digested_name {
197 u32 hash;
198 u32 minor_hash;
199 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
200};
201
202/**
203 * fscrypt_match_name() - test whether the given name matches a directory entry
204 * @fname: the name being searched for
205 * @de_name: the name from the directory entry
206 * @de_name_len: the length of @de_name in bytes
207 *
208 * Normally @fname->disk_name will be set, and in that case we simply compare
209 * that to the name stored in the directory entry. The only exception is that
210 * if we don't have the key for an encrypted directory and a filename in it is
211 * very long, then we won't have the full disk_name and we'll instead need to
212 * match against the fscrypt_digested_name.
213 *
214 * Return: %true if the name matches, otherwise %false.
215 */
216static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
217 const u8 *de_name, u32 de_name_len)
218{
219 if (unlikely(!fname->disk_name.name)) {
220 const struct fscrypt_digested_name *n =
221 (const void *)fname->crypto_buf.name;
222 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
223 return false;
224 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
225 return false;
226 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
227 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
228 }
229
230 if (de_name_len != fname->disk_name.len)
231 return false;
232 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
233}
234
235/* bio.c */
236extern void fscrypt_decrypt_bio(struct bio *);
237extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
238 struct bio *bio);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530239extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
240 unsigned int);
241
242/* hooks.c */
243extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers081985b2019-03-20 11:39:10 -0700244extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
245 struct dentry *dentry);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530246extern int __fscrypt_prepare_rename(struct inode *old_dir,
247 struct dentry *old_dentry,
248 struct inode *new_dir,
249 struct dentry *new_dentry,
250 unsigned int flags);
Eric Biggers716c3702019-03-20 11:39:13 -0700251extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
252 struct fscrypt_name *fname);
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530253extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
254 unsigned int max_len,
255 struct fscrypt_str *disk_link);
256extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
257 unsigned int len,
258 struct fscrypt_str *disk_link);
259extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
260 unsigned int max_size,
261 struct delayed_call *done);
262#else /* !CONFIG_FS_ENCRYPTION */
263
264static inline bool fscrypt_has_encryption_key(const struct inode *inode)
265{
266 return false;
267}
268
269static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
270{
271 return false;
272}
273
Eric Biggersdd038b32019-03-20 11:39:11 -0700274static inline void fscrypt_handle_d_move(struct dentry *dentry)
275{
276}
277
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530278/* crypto.c */
279static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
280{
281}
282
Eric Biggers95e2ce52019-03-18 10:23:33 -0700283static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530284{
285 return ERR_PTR(-EOPNOTSUPP);
286}
287
288static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
289{
290 return;
291}
292
Eric Biggersfd0e18f2019-05-20 09:29:44 -0700293static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
294 unsigned int len,
295 unsigned int offs,
296 gfp_t gfp_flags)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530297{
298 return ERR_PTR(-EOPNOTSUPP);
299}
300
Eric Biggersce7b7a22019-05-20 09:29:43 -0700301static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
302 struct page *page,
303 unsigned int len,
304 unsigned int offs, u64 lblk_num,
305 gfp_t gfp_flags)
306{
307 return -EOPNOTSUPP;
308}
309
Eric Biggers87806292019-05-20 09:29:47 -0700310static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
311 unsigned int len,
312 unsigned int offs)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530313{
314 return -EOPNOTSUPP;
315}
316
Eric Biggers45822362019-05-20 09:29:46 -0700317static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
318 struct page *page,
319 unsigned int len,
320 unsigned int offs, u64 lblk_num)
321{
322 return -EOPNOTSUPP;
323}
324
Eric Biggers2edab9e2019-05-20 09:29:39 -0700325static inline bool fscrypt_is_bounce_page(struct page *page)
326{
327 return false;
328}
329
330static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530331{
332 WARN_ON_ONCE(1);
333 return ERR_PTR(-EINVAL);
334}
335
Eric Biggers2edab9e2019-05-20 09:29:39 -0700336static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530337{
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530338}
339
340/* policy.c */
341static inline int fscrypt_ioctl_set_policy(struct file *filp,
342 const void __user *arg)
343{
344 return -EOPNOTSUPP;
345}
346
347static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
348{
349 return -EOPNOTSUPP;
350}
351
352static inline int fscrypt_has_permitted_context(struct inode *parent,
353 struct inode *child)
354{
355 return 0;
356}
357
358static inline int fscrypt_inherit_context(struct inode *parent,
359 struct inode *child,
360 void *fs_data, bool preload)
361{
362 return -EOPNOTSUPP;
363}
364
Eric Biggersc677e572019-08-04 19:35:45 -0700365/* keysetup.c */
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530366static inline int fscrypt_get_encryption_info(struct inode *inode)
367{
368 return -EOPNOTSUPP;
369}
370
371static inline void fscrypt_put_encryption_info(struct inode *inode)
372{
373 return;
374}
375
Eric Biggers1f1be4a2019-04-10 13:21:15 -0700376static inline void fscrypt_free_inode(struct inode *inode)
377{
378}
379
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530380 /* fname.c */
381static inline int fscrypt_setup_filename(struct inode *dir,
382 const struct qstr *iname,
383 int lookup, struct fscrypt_name *fname)
384{
385 if (IS_ENCRYPTED(dir))
386 return -EOPNOTSUPP;
387
Eric Biggers716c3702019-03-20 11:39:13 -0700388 memset(fname, 0, sizeof(*fname));
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530389 fname->usr_fname = iname;
390 fname->disk_name.name = (unsigned char *)iname->name;
391 fname->disk_name.len = iname->len;
392 return 0;
393}
394
395static inline void fscrypt_free_filename(struct fscrypt_name *fname)
396{
397 return;
398}
399
400static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
401 u32 max_encrypted_len,
402 struct fscrypt_str *crypto_str)
403{
404 return -EOPNOTSUPP;
405}
406
407static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
408{
409 return;
410}
411
412static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
413 u32 hash, u32 minor_hash,
414 const struct fscrypt_str *iname,
415 struct fscrypt_str *oname)
416{
417 return -EOPNOTSUPP;
418}
419
420static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
421 const u8 *de_name, u32 de_name_len)
422{
423 /* Encryption support disabled; use standard comparison */
424 if (de_name_len != fname->disk_name.len)
425 return false;
426 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
427}
428
429/* bio.c */
430static inline void fscrypt_decrypt_bio(struct bio *bio)
431{
432}
433
434static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
435 struct bio *bio)
436{
437}
438
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530439static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
440 sector_t pblk, unsigned int len)
441{
442 return -EOPNOTSUPP;
443}
444
445/* hooks.c */
446
447static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
448{
449 if (IS_ENCRYPTED(inode))
450 return -EOPNOTSUPP;
451 return 0;
452}
453
Eric Biggers081985b2019-03-20 11:39:10 -0700454static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
455 struct dentry *dentry)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530456{
457 return -EOPNOTSUPP;
458}
459
460static inline int __fscrypt_prepare_rename(struct inode *old_dir,
461 struct dentry *old_dentry,
462 struct inode *new_dir,
463 struct dentry *new_dentry,
464 unsigned int flags)
465{
466 return -EOPNOTSUPP;
467}
468
469static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggers716c3702019-03-20 11:39:13 -0700470 struct dentry *dentry,
471 struct fscrypt_name *fname)
Chandan Rajendraad8ceb02018-12-12 15:20:12 +0530472{
473 return -EOPNOTSUPP;
474}
475
476static inline int __fscrypt_prepare_symlink(struct inode *dir,
477 unsigned int len,
478 unsigned int max_len,
479 struct fscrypt_str *disk_link)
480{
481 return -EOPNOTSUPP;
482}
483
484
485static inline int __fscrypt_encrypt_symlink(struct inode *inode,
486 const char *target,
487 unsigned int len,
488 struct fscrypt_str *disk_link)
489{
490 return -EOPNOTSUPP;
491}
492
493static inline const char *fscrypt_get_symlink(struct inode *inode,
494 const void *caddr,
495 unsigned int max_size,
496 struct delayed_call *done)
497{
498 return ERR_PTR(-EOPNOTSUPP);
499}
500#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700501
Eric Biggersd293c3e2017-10-09 12:15:39 -0700502/**
503 * fscrypt_require_key - require an inode's encryption key
504 * @inode: the inode we need the key for
505 *
506 * If the inode is encrypted, set up its encryption key if not already done.
507 * Then require that the key be present and return -ENOKEY otherwise.
508 *
509 * No locks are needed, and the key will live as long as the struct inode --- so
510 * it won't go away from under you.
511 *
512 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
513 * if a problem occurred while setting up the encryption key.
514 */
515static inline int fscrypt_require_key(struct inode *inode)
516{
517 if (IS_ENCRYPTED(inode)) {
518 int err = fscrypt_get_encryption_info(inode);
519
520 if (err)
521 return err;
522 if (!fscrypt_has_encryption_key(inode))
523 return -ENOKEY;
524 }
525 return 0;
526}
Dave Chinner734f0d22017-10-09 12:15:34 -0700527
Eric Biggers0ea87a92017-10-09 12:15:41 -0700528/**
529 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
530 * @old_dentry: an existing dentry for the inode being linked
531 * @dir: the target directory
532 * @dentry: negative dentry for the target filename
533 *
534 * A new link can only be added to an encrypted directory if the directory's
535 * encryption key is available --- since otherwise we'd have no way to encrypt
536 * the filename. Therefore, we first set up the directory's encryption key (if
537 * not already done) and return an error if it's unavailable.
538 *
539 * We also verify that the link will not violate the constraint that all files
540 * in an encrypted directory tree use the same encryption policy.
541 *
542 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggers94e10682019-01-22 16:20:21 -0800543 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700544 * another -errno code.
545 */
546static inline int fscrypt_prepare_link(struct dentry *old_dentry,
547 struct inode *dir,
548 struct dentry *dentry)
549{
550 if (IS_ENCRYPTED(dir))
Eric Biggers081985b2019-03-20 11:39:10 -0700551 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700552 return 0;
553}
554
Eric Biggers94b26f32017-10-09 12:15:42 -0700555/**
556 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
557 * @old_dir: source directory
558 * @old_dentry: dentry for source file
559 * @new_dir: target directory
560 * @new_dentry: dentry for target location (may be negative unless exchanging)
561 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
562 *
563 * Prepare for ->rename() where the source and/or target directories may be
564 * encrypted. A new link can only be added to an encrypted directory if the
565 * directory's encryption key is available --- since otherwise we'd have no way
566 * to encrypt the filename. A rename to an existing name, on the other hand,
567 * *is* cryptographically possible without the key. However, we take the more
568 * conservative approach and just forbid all no-key renames.
569 *
570 * We also verify that the rename will not violate the constraint that all files
571 * in an encrypted directory tree use the same encryption policy.
572 *
Eric Biggers94e10682019-01-22 16:20:21 -0800573 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700574 * rename would cause inconsistent encryption policies, or another -errno code.
575 */
576static inline int fscrypt_prepare_rename(struct inode *old_dir,
577 struct dentry *old_dentry,
578 struct inode *new_dir,
579 struct dentry *new_dentry,
580 unsigned int flags)
581{
582 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
583 return __fscrypt_prepare_rename(old_dir, old_dentry,
584 new_dir, new_dentry, flags);
585 return 0;
586}
587
Eric Biggers32c3cf02017-10-09 12:15:43 -0700588/**
589 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
590 * @dir: directory being searched
591 * @dentry: filename being looked up
Eric Biggers716c3702019-03-20 11:39:13 -0700592 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700593 *
Eric Biggers716c3702019-03-20 11:39:13 -0700594 * Prepare for ->lookup() in a directory which may be encrypted by determining
595 * the name that will actually be used to search the directory on-disk. Lookups
596 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700597 * filenames are presented in encrypted form. Therefore, we'll try to set up
598 * the directory's encryption key, but even without it the lookup can continue.
599 *
Eric Biggers37b434e2019-03-20 11:39:09 -0700600 * This also installs a custom ->d_revalidate() method which will invalidate the
601 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700602 *
Eric Biggers716c3702019-03-20 11:39:13 -0700603 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
604 * correctly formed encoded ciphertext name, so a negative dentry should be
605 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700606 */
607static inline int fscrypt_prepare_lookup(struct inode *dir,
608 struct dentry *dentry,
Eric Biggers716c3702019-03-20 11:39:13 -0700609 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700610{
611 if (IS_ENCRYPTED(dir))
Eric Biggers716c3702019-03-20 11:39:13 -0700612 return __fscrypt_prepare_lookup(dir, dentry, fname);
613
614 memset(fname, 0, sizeof(*fname));
615 fname->usr_fname = &dentry->d_name;
616 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
617 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700618 return 0;
619}
620
Eric Biggers815dac32017-10-09 12:15:44 -0700621/**
622 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
623 * @dentry: dentry through which the inode is being changed
624 * @attr: attributes to change
625 *
626 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
627 * most attribute changes are allowed even without the encryption key. However,
628 * without the encryption key we do have to forbid truncates. This is needed
629 * because the size being truncated to may not be a multiple of the filesystem
630 * block size, and in that case we'd have to decrypt the final block, zero the
631 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
632 * filesystem block boundary, but it's simpler to just forbid all truncates ---
633 * and we already forbid all other contents modifications without the key.)
634 *
635 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
636 * if a problem occurred while setting up the encryption key.
637 */
638static inline int fscrypt_prepare_setattr(struct dentry *dentry,
639 struct iattr *attr)
640{
641 if (attr->ia_valid & ATTR_SIZE)
642 return fscrypt_require_key(d_inode(dentry));
643 return 0;
644}
645
Eric Biggers76e81d62018-01-05 10:45:01 -0800646/**
647 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
648 * @dir: directory in which the symlink is being created
649 * @target: plaintext symlink target
650 * @len: length of @target excluding null terminator
651 * @max_len: space the filesystem has available to store the symlink target
652 * @disk_link: (out) the on-disk symlink target being prepared
653 *
654 * This function computes the size the symlink target will require on-disk,
655 * stores it in @disk_link->len, and validates it against @max_len. An
656 * encrypted symlink may be longer than the original.
657 *
658 * Additionally, @disk_link->name is set to @target if the symlink will be
659 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
660 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
661 * on-disk target later. (The reason for the two-step process is that some
662 * filesystems need to know the size of the symlink target before creating the
663 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
664 *
665 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
666 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
667 * occurred while setting up the encryption key.
668 */
669static inline int fscrypt_prepare_symlink(struct inode *dir,
670 const char *target,
671 unsigned int len,
672 unsigned int max_len,
673 struct fscrypt_str *disk_link)
674{
675 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
676 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
677
678 disk_link->name = (unsigned char *)target;
679 disk_link->len = len + 1;
680 if (disk_link->len > max_len)
681 return -ENAMETOOLONG;
682 return 0;
683}
684
685/**
686 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
687 * @inode: symlink inode
688 * @target: plaintext symlink target
689 * @len: length of @target excluding null terminator
690 * @disk_link: (in/out) the on-disk symlink target being prepared
691 *
692 * If the symlink target needs to be encrypted, then this function encrypts it
693 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
694 * previously to compute @disk_link->len. If the filesystem did not allocate a
695 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
696 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
697 *
698 * Return: 0 on success, -errno on failure
699 */
700static inline int fscrypt_encrypt_symlink(struct inode *inode,
701 const char *target,
702 unsigned int len,
703 struct fscrypt_str *disk_link)
704{
705 if (IS_ENCRYPTED(inode))
706 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
707 return 0;
708}
709
Eric Biggers2edab9e2019-05-20 09:29:39 -0700710/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
711static inline void fscrypt_finalize_bounce_page(struct page **pagep)
712{
713 struct page *page = *pagep;
714
715 if (fscrypt_is_bounce_page(page)) {
716 *pagep = fscrypt_pagecache_page(page);
717 fscrypt_free_bounce_page(page);
718 }
719}
720
Dave Chinner734f0d22017-10-09 12:15:34 -0700721#endif /* _LINUX_FSCRYPT_H */