blob: c7e16bd16a6c233a7f6319ecb8efbd5661bae7fb [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 Rajendra643fa962018-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 Rajendra643fa962018-12-12 15:20:12 +053017#include <linux/mm.h>
18#include <linux/slab.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080019
20#define FS_CRYPTO_BLOCK_SIZE 16
21
Eric Biggers542060c2018-01-05 10:44:55 -080022struct fscrypt_ctx;
Eric Biggers46f47e42017-01-24 10:58:06 -080023struct 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 Biggersb01531d2019-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(). */
45#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
46
Chandan Rajendra643fa962018-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;
64};
65
Eric Biggers2a415a02019-05-20 09:29:40 -070066/* Decryption work */
Chandan Rajendra643fa962018-12-12 15:20:12 +053067struct fscrypt_ctx {
68 union {
69 struct {
Chandan Rajendra643fa962018-12-12 15:20:12 +053070 struct bio *bio;
71 struct work_struct work;
Eric Biggers2a415a02019-05-20 09:29:40 -070072 };
Chandan Rajendra643fa962018-12-12 15:20:12 +053073 struct list_head free_list; /* Free list */
74 };
75 u8 flags; /* Flags */
76};
77
78static inline bool fscrypt_has_encryption_key(const struct inode *inode)
79{
Eric Biggerse37a7842019-04-11 14:32:15 -070080 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
81 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +053082}
83
84static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
85{
86 return inode->i_sb->s_cop->dummy_context &&
87 inode->i_sb->s_cop->dummy_context(inode);
88}
89
Eric Biggers0bf3d5c12019-03-20 11:39:11 -070090/*
91 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
92 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
93 * must be cleared. Note that we don't have to support arbitrary moves of this
94 * flag because fscrypt doesn't allow encrypted aliases to be the source or
95 * target of a rename().
96 */
97static inline void fscrypt_handle_d_move(struct dentry *dentry)
98{
99 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
100}
101
Chandan Rajendra643fa962018-12-12 15:20:12 +0530102/* crypto.c */
103extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggerscd0265f2019-03-18 10:23:33 -0700104extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530105extern void fscrypt_release_ctx(struct fscrypt_ctx *);
Eric Biggers53bc1d852019-05-20 09:29:44 -0700106
107extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
108 unsigned int len,
109 unsigned int offs,
110 gfp_t gfp_flags);
Eric Biggers03569f22019-05-20 09:29:43 -0700111extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
112 struct page *page, unsigned int len,
113 unsigned int offs, u64 lblk_num,
114 gfp_t gfp_flags);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530115extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
116 unsigned int, u64);
117
Eric Biggersd2d07272019-05-20 09:29:39 -0700118static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530119{
Eric Biggersd2d07272019-05-20 09:29:39 -0700120 return page->mapping == NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530121}
122
Eric Biggersd2d07272019-05-20 09:29:39 -0700123static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
124{
125 return (struct page *)page_private(bounce_page);
126}
127
128extern void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530129
130/* policy.c */
131extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
132extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
133extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
134extern int fscrypt_inherit_context(struct inode *, struct inode *,
135 void *, bool);
136/* keyinfo.c */
137extern int fscrypt_get_encryption_info(struct inode *);
138extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggers2c58d542019-04-10 13:21:15 -0700139extern void fscrypt_free_inode(struct inode *);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530140
141/* fname.c */
142extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
143 int lookup, struct fscrypt_name *);
144
145static inline void fscrypt_free_filename(struct fscrypt_name *fname)
146{
147 kfree(fname->crypto_buf.name);
148}
149
150extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
151 struct fscrypt_str *);
152extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
153extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
154 const struct fscrypt_str *, struct fscrypt_str *);
155
156#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
157
158/* Extracts the second-to-last ciphertext block; see explanation below */
159#define FSCRYPT_FNAME_DIGEST(name, len) \
160 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
161 FS_CRYPTO_BLOCK_SIZE))
162
163#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
164
165/**
166 * fscrypt_digested_name - alternate identifier for an on-disk filename
167 *
168 * When userspace lists an encrypted directory without access to the key,
169 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
170 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
171 * full ciphertext (base64-encoded). This is necessary to allow supporting
172 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
173 *
174 * To make it possible for filesystems to still find the correct directory entry
175 * despite not knowing the full on-disk name, we encode any filesystem-specific
176 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
177 * followed by the second-to-last ciphertext block of the filename. Due to the
178 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
179 * depends on the full plaintext. (Note that ciphertext stealing causes the
180 * last two blocks to appear "flipped".) This makes accidental collisions very
181 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
182 * share the same filesystem-specific hashes.
183 *
184 * However, this scheme isn't immune to intentional collisions, which can be
185 * created by anyone able to create arbitrary plaintext filenames and view them
186 * without the key. Making the "digest" be a real cryptographic hash like
187 * SHA-256 over the full ciphertext would prevent this, although it would be
188 * less efficient and harder to implement, especially since the filesystem would
189 * need to calculate it for each directory entry examined during a search.
190 */
191struct fscrypt_digested_name {
192 u32 hash;
193 u32 minor_hash;
194 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
195};
196
197/**
198 * fscrypt_match_name() - test whether the given name matches a directory entry
199 * @fname: the name being searched for
200 * @de_name: the name from the directory entry
201 * @de_name_len: the length of @de_name in bytes
202 *
203 * Normally @fname->disk_name will be set, and in that case we simply compare
204 * that to the name stored in the directory entry. The only exception is that
205 * if we don't have the key for an encrypted directory and a filename in it is
206 * very long, then we won't have the full disk_name and we'll instead need to
207 * match against the fscrypt_digested_name.
208 *
209 * Return: %true if the name matches, otherwise %false.
210 */
211static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
212 const u8 *de_name, u32 de_name_len)
213{
214 if (unlikely(!fname->disk_name.name)) {
215 const struct fscrypt_digested_name *n =
216 (const void *)fname->crypto_buf.name;
217 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
218 return false;
219 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
220 return false;
221 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
222 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
223 }
224
225 if (de_name_len != fname->disk_name.len)
226 return false;
227 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
228}
229
230/* bio.c */
231extern void fscrypt_decrypt_bio(struct bio *);
232extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
233 struct bio *bio);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530234extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
235 unsigned int);
236
237/* hooks.c */
238extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers968dd6d2019-03-20 11:39:10 -0700239extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
240 struct dentry *dentry);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530241extern int __fscrypt_prepare_rename(struct inode *old_dir,
242 struct dentry *old_dentry,
243 struct inode *new_dir,
244 struct dentry *new_dentry,
245 unsigned int flags);
Eric Biggersb01531d2019-03-20 11:39:13 -0700246extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
247 struct fscrypt_name *fname);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530248extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
249 unsigned int max_len,
250 struct fscrypt_str *disk_link);
251extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
252 unsigned int len,
253 struct fscrypt_str *disk_link);
254extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
255 unsigned int max_size,
256 struct delayed_call *done);
Sascha Hauereea2c052019-03-26 08:52:31 +0100257static inline void fscrypt_set_ops(struct super_block *sb,
258 const struct fscrypt_operations *s_cop)
259{
260 sb->s_cop = s_cop;
261}
Chandan Rajendra643fa962018-12-12 15:20:12 +0530262#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 Biggers0bf3d5c12019-03-20 11:39:11 -0700274static inline void fscrypt_handle_d_move(struct dentry *dentry)
275{
276}
277
Chandan Rajendra643fa962018-12-12 15:20:12 +0530278/* crypto.c */
279static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
280{
281}
282
Eric Biggerscd0265f2019-03-18 10:23:33 -0700283static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendra643fa962018-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 Biggers53bc1d852019-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 Rajendra643fa962018-12-12 15:20:12 +0530297{
298 return ERR_PTR(-EOPNOTSUPP);
299}
300
Eric Biggers03569f22019-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
Chandan Rajendra643fa962018-12-12 15:20:12 +0530310static inline int fscrypt_decrypt_page(const struct inode *inode,
311 struct page *page,
312 unsigned int len, unsigned int offs,
313 u64 lblk_num)
314{
315 return -EOPNOTSUPP;
316}
317
Eric Biggersd2d07272019-05-20 09:29:39 -0700318static inline bool fscrypt_is_bounce_page(struct page *page)
319{
320 return false;
321}
322
323static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530324{
325 WARN_ON_ONCE(1);
326 return ERR_PTR(-EINVAL);
327}
328
Eric Biggersd2d07272019-05-20 09:29:39 -0700329static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530330{
Chandan Rajendra643fa962018-12-12 15:20:12 +0530331}
332
333/* policy.c */
334static inline int fscrypt_ioctl_set_policy(struct file *filp,
335 const void __user *arg)
336{
337 return -EOPNOTSUPP;
338}
339
340static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
341{
342 return -EOPNOTSUPP;
343}
344
345static inline int fscrypt_has_permitted_context(struct inode *parent,
346 struct inode *child)
347{
348 return 0;
349}
350
351static inline int fscrypt_inherit_context(struct inode *parent,
352 struct inode *child,
353 void *fs_data, bool preload)
354{
355 return -EOPNOTSUPP;
356}
357
358/* keyinfo.c */
359static inline int fscrypt_get_encryption_info(struct inode *inode)
360{
361 return -EOPNOTSUPP;
362}
363
364static inline void fscrypt_put_encryption_info(struct inode *inode)
365{
366 return;
367}
368
Eric Biggers2c58d542019-04-10 13:21:15 -0700369static inline void fscrypt_free_inode(struct inode *inode)
370{
371}
372
Chandan Rajendra643fa962018-12-12 15:20:12 +0530373 /* fname.c */
374static inline int fscrypt_setup_filename(struct inode *dir,
375 const struct qstr *iname,
376 int lookup, struct fscrypt_name *fname)
377{
378 if (IS_ENCRYPTED(dir))
379 return -EOPNOTSUPP;
380
Eric Biggersb01531d2019-03-20 11:39:13 -0700381 memset(fname, 0, sizeof(*fname));
Chandan Rajendra643fa962018-12-12 15:20:12 +0530382 fname->usr_fname = iname;
383 fname->disk_name.name = (unsigned char *)iname->name;
384 fname->disk_name.len = iname->len;
385 return 0;
386}
387
388static inline void fscrypt_free_filename(struct fscrypt_name *fname)
389{
390 return;
391}
392
393static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
394 u32 max_encrypted_len,
395 struct fscrypt_str *crypto_str)
396{
397 return -EOPNOTSUPP;
398}
399
400static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
401{
402 return;
403}
404
405static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
406 u32 hash, u32 minor_hash,
407 const struct fscrypt_str *iname,
408 struct fscrypt_str *oname)
409{
410 return -EOPNOTSUPP;
411}
412
413static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
414 const u8 *de_name, u32 de_name_len)
415{
416 /* Encryption support disabled; use standard comparison */
417 if (de_name_len != fname->disk_name.len)
418 return false;
419 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
420}
421
422/* bio.c */
423static inline void fscrypt_decrypt_bio(struct bio *bio)
424{
425}
426
427static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
428 struct bio *bio)
429{
430}
431
Chandan Rajendra643fa962018-12-12 15:20:12 +0530432static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
433 sector_t pblk, unsigned int len)
434{
435 return -EOPNOTSUPP;
436}
437
438/* hooks.c */
439
440static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
441{
442 if (IS_ENCRYPTED(inode))
443 return -EOPNOTSUPP;
444 return 0;
445}
446
Eric Biggers968dd6d2019-03-20 11:39:10 -0700447static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
448 struct dentry *dentry)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530449{
450 return -EOPNOTSUPP;
451}
452
453static inline int __fscrypt_prepare_rename(struct inode *old_dir,
454 struct dentry *old_dentry,
455 struct inode *new_dir,
456 struct dentry *new_dentry,
457 unsigned int flags)
458{
459 return -EOPNOTSUPP;
460}
461
462static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggersb01531d2019-03-20 11:39:13 -0700463 struct dentry *dentry,
464 struct fscrypt_name *fname)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530465{
466 return -EOPNOTSUPP;
467}
468
469static inline int __fscrypt_prepare_symlink(struct inode *dir,
470 unsigned int len,
471 unsigned int max_len,
472 struct fscrypt_str *disk_link)
473{
474 return -EOPNOTSUPP;
475}
476
477
478static inline int __fscrypt_encrypt_symlink(struct inode *inode,
479 const char *target,
480 unsigned int len,
481 struct fscrypt_str *disk_link)
482{
483 return -EOPNOTSUPP;
484}
485
486static inline const char *fscrypt_get_symlink(struct inode *inode,
487 const void *caddr,
488 unsigned int max_size,
489 struct delayed_call *done)
490{
491 return ERR_PTR(-EOPNOTSUPP);
492}
Sascha Hauereea2c052019-03-26 08:52:31 +0100493
494static inline void fscrypt_set_ops(struct super_block *sb,
495 const struct fscrypt_operations *s_cop)
496{
497}
498
Chandan Rajendra643fa962018-12-12 15:20:12 +0530499#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700500
Eric Biggersd293c3e2017-10-09 12:15:39 -0700501/**
502 * fscrypt_require_key - require an inode's encryption key
503 * @inode: the inode we need the key for
504 *
505 * If the inode is encrypted, set up its encryption key if not already done.
506 * Then require that the key be present and return -ENOKEY otherwise.
507 *
508 * No locks are needed, and the key will live as long as the struct inode --- so
509 * it won't go away from under you.
510 *
511 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
512 * if a problem occurred while setting up the encryption key.
513 */
514static inline int fscrypt_require_key(struct inode *inode)
515{
516 if (IS_ENCRYPTED(inode)) {
517 int err = fscrypt_get_encryption_info(inode);
518
519 if (err)
520 return err;
521 if (!fscrypt_has_encryption_key(inode))
522 return -ENOKEY;
523 }
524 return 0;
525}
Dave Chinner734f0d22017-10-09 12:15:34 -0700526
Eric Biggers0ea87a92017-10-09 12:15:41 -0700527/**
528 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
529 * @old_dentry: an existing dentry for the inode being linked
530 * @dir: the target directory
531 * @dentry: negative dentry for the target filename
532 *
533 * A new link can only be added to an encrypted directory if the directory's
534 * encryption key is available --- since otherwise we'd have no way to encrypt
535 * the filename. Therefore, we first set up the directory's encryption key (if
536 * not already done) and return an error if it's unavailable.
537 *
538 * We also verify that the link will not violate the constraint that all files
539 * in an encrypted directory tree use the same encryption policy.
540 *
541 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800542 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700543 * another -errno code.
544 */
545static inline int fscrypt_prepare_link(struct dentry *old_dentry,
546 struct inode *dir,
547 struct dentry *dentry)
548{
549 if (IS_ENCRYPTED(dir))
Eric Biggers968dd6d2019-03-20 11:39:10 -0700550 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700551 return 0;
552}
553
Eric Biggers94b26f32017-10-09 12:15:42 -0700554/**
555 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
556 * @old_dir: source directory
557 * @old_dentry: dentry for source file
558 * @new_dir: target directory
559 * @new_dentry: dentry for target location (may be negative unless exchanging)
560 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
561 *
562 * Prepare for ->rename() where the source and/or target directories may be
563 * encrypted. A new link can only be added to an encrypted directory if the
564 * directory's encryption key is available --- since otherwise we'd have no way
565 * to encrypt the filename. A rename to an existing name, on the other hand,
566 * *is* cryptographically possible without the key. However, we take the more
567 * conservative approach and just forbid all no-key renames.
568 *
569 * We also verify that the rename will not violate the constraint that all files
570 * in an encrypted directory tree use the same encryption policy.
571 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800572 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700573 * rename would cause inconsistent encryption policies, or another -errno code.
574 */
575static inline int fscrypt_prepare_rename(struct inode *old_dir,
576 struct dentry *old_dentry,
577 struct inode *new_dir,
578 struct dentry *new_dentry,
579 unsigned int flags)
580{
581 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
582 return __fscrypt_prepare_rename(old_dir, old_dentry,
583 new_dir, new_dentry, flags);
584 return 0;
585}
586
Eric Biggers32c3cf02017-10-09 12:15:43 -0700587/**
588 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
589 * @dir: directory being searched
590 * @dentry: filename being looked up
Eric Biggersb01531d2019-03-20 11:39:13 -0700591 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700592 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700593 * Prepare for ->lookup() in a directory which may be encrypted by determining
594 * the name that will actually be used to search the directory on-disk. Lookups
595 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700596 * filenames are presented in encrypted form. Therefore, we'll try to set up
597 * the directory's encryption key, but even without it the lookup can continue.
598 *
Eric Biggers6cc24862019-03-20 11:39:09 -0700599 * This also installs a custom ->d_revalidate() method which will invalidate the
600 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700601 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700602 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
603 * correctly formed encoded ciphertext name, so a negative dentry should be
604 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700605 */
606static inline int fscrypt_prepare_lookup(struct inode *dir,
607 struct dentry *dentry,
Eric Biggersb01531d2019-03-20 11:39:13 -0700608 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700609{
610 if (IS_ENCRYPTED(dir))
Eric Biggersb01531d2019-03-20 11:39:13 -0700611 return __fscrypt_prepare_lookup(dir, dentry, fname);
612
613 memset(fname, 0, sizeof(*fname));
614 fname->usr_fname = &dentry->d_name;
615 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
616 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700617 return 0;
618}
619
Eric Biggers815dac32017-10-09 12:15:44 -0700620/**
621 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
622 * @dentry: dentry through which the inode is being changed
623 * @attr: attributes to change
624 *
625 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
626 * most attribute changes are allowed even without the encryption key. However,
627 * without the encryption key we do have to forbid truncates. This is needed
628 * because the size being truncated to may not be a multiple of the filesystem
629 * block size, and in that case we'd have to decrypt the final block, zero the
630 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
631 * filesystem block boundary, but it's simpler to just forbid all truncates ---
632 * and we already forbid all other contents modifications without the key.)
633 *
634 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
635 * if a problem occurred while setting up the encryption key.
636 */
637static inline int fscrypt_prepare_setattr(struct dentry *dentry,
638 struct iattr *attr)
639{
640 if (attr->ia_valid & ATTR_SIZE)
641 return fscrypt_require_key(d_inode(dentry));
642 return 0;
643}
644
Eric Biggers76e81d62018-01-05 10:45:01 -0800645/**
646 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
647 * @dir: directory in which the symlink is being created
648 * @target: plaintext symlink target
649 * @len: length of @target excluding null terminator
650 * @max_len: space the filesystem has available to store the symlink target
651 * @disk_link: (out) the on-disk symlink target being prepared
652 *
653 * This function computes the size the symlink target will require on-disk,
654 * stores it in @disk_link->len, and validates it against @max_len. An
655 * encrypted symlink may be longer than the original.
656 *
657 * Additionally, @disk_link->name is set to @target if the symlink will be
658 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
659 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
660 * on-disk target later. (The reason for the two-step process is that some
661 * filesystems need to know the size of the symlink target before creating the
662 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
663 *
664 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
665 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
666 * occurred while setting up the encryption key.
667 */
668static inline int fscrypt_prepare_symlink(struct inode *dir,
669 const char *target,
670 unsigned int len,
671 unsigned int max_len,
672 struct fscrypt_str *disk_link)
673{
674 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
675 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
676
677 disk_link->name = (unsigned char *)target;
678 disk_link->len = len + 1;
679 if (disk_link->len > max_len)
680 return -ENAMETOOLONG;
681 return 0;
682}
683
684/**
685 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
686 * @inode: symlink inode
687 * @target: plaintext symlink target
688 * @len: length of @target excluding null terminator
689 * @disk_link: (in/out) the on-disk symlink target being prepared
690 *
691 * If the symlink target needs to be encrypted, then this function encrypts it
692 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
693 * previously to compute @disk_link->len. If the filesystem did not allocate a
694 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
695 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
696 *
697 * Return: 0 on success, -errno on failure
698 */
699static inline int fscrypt_encrypt_symlink(struct inode *inode,
700 const char *target,
701 unsigned int len,
702 struct fscrypt_str *disk_link)
703{
704 if (IS_ENCRYPTED(inode))
705 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
706 return 0;
707}
708
Eric Biggersd2d07272019-05-20 09:29:39 -0700709/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
710static inline void fscrypt_finalize_bounce_page(struct page **pagep)
711{
712 struct page *page = *pagep;
713
714 if (fscrypt_is_bounce_page(page)) {
715 *pagep = fscrypt_pagecache_page(page);
716 fscrypt_free_bounce_page(page);
717 }
718}
719
Dave Chinner734f0d22017-10-09 12:15:34 -0700720#endif /* _LINUX_FSCRYPT_H */