blob: abe7081b6b22de38b9915e210bf2d10692486a15 [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
66struct fscrypt_ctx {
67 union {
68 struct {
69 struct page *bounce_page; /* Ciphertext page */
70 struct page *control_page; /* Original page */
71 } w;
72 struct {
73 struct bio *bio;
74 struct work_struct work;
75 } r;
76 struct list_head free_list; /* Free list */
77 };
78 u8 flags; /* Flags */
79};
80
81static inline bool fscrypt_has_encryption_key(const struct inode *inode)
82{
Eric Biggerse37a7842019-04-11 14:32:15 -070083 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
84 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +053085}
86
87static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
88{
89 return inode->i_sb->s_cop->dummy_context &&
90 inode->i_sb->s_cop->dummy_context(inode);
91}
92
Eric Biggers0bf3d5c12019-03-20 11:39:11 -070093/*
94 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
95 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
96 * must be cleared. Note that we don't have to support arbitrary moves of this
97 * flag because fscrypt doesn't allow encrypted aliases to be the source or
98 * target of a rename().
99 */
100static inline void fscrypt_handle_d_move(struct dentry *dentry)
101{
102 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
103}
104
Chandan Rajendra643fa962018-12-12 15:20:12 +0530105/* crypto.c */
106extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggerscd0265f2019-03-18 10:23:33 -0700107extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530108extern void fscrypt_release_ctx(struct fscrypt_ctx *);
109extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
110 unsigned int, unsigned int,
111 u64, gfp_t);
112extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
113 unsigned int, u64);
114
115static inline struct page *fscrypt_control_page(struct page *page)
116{
117 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
118}
119
120extern void fscrypt_restore_control_page(struct page *);
121
122/* policy.c */
123extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
124extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
125extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
126extern int fscrypt_inherit_context(struct inode *, struct inode *,
127 void *, bool);
128/* keyinfo.c */
129extern int fscrypt_get_encryption_info(struct inode *);
130extern void fscrypt_put_encryption_info(struct inode *);
131
132/* fname.c */
133extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
134 int lookup, struct fscrypt_name *);
135
136static inline void fscrypt_free_filename(struct fscrypt_name *fname)
137{
138 kfree(fname->crypto_buf.name);
139}
140
141extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
142 struct fscrypt_str *);
143extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
144extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
145 const struct fscrypt_str *, struct fscrypt_str *);
146
147#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
148
149/* Extracts the second-to-last ciphertext block; see explanation below */
150#define FSCRYPT_FNAME_DIGEST(name, len) \
151 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
152 FS_CRYPTO_BLOCK_SIZE))
153
154#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
155
156/**
157 * fscrypt_digested_name - alternate identifier for an on-disk filename
158 *
159 * When userspace lists an encrypted directory without access to the key,
160 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
161 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
162 * full ciphertext (base64-encoded). This is necessary to allow supporting
163 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
164 *
165 * To make it possible for filesystems to still find the correct directory entry
166 * despite not knowing the full on-disk name, we encode any filesystem-specific
167 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
168 * followed by the second-to-last ciphertext block of the filename. Due to the
169 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
170 * depends on the full plaintext. (Note that ciphertext stealing causes the
171 * last two blocks to appear "flipped".) This makes accidental collisions very
172 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
173 * share the same filesystem-specific hashes.
174 *
175 * However, this scheme isn't immune to intentional collisions, which can be
176 * created by anyone able to create arbitrary plaintext filenames and view them
177 * without the key. Making the "digest" be a real cryptographic hash like
178 * SHA-256 over the full ciphertext would prevent this, although it would be
179 * less efficient and harder to implement, especially since the filesystem would
180 * need to calculate it for each directory entry examined during a search.
181 */
182struct fscrypt_digested_name {
183 u32 hash;
184 u32 minor_hash;
185 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
186};
187
188/**
189 * fscrypt_match_name() - test whether the given name matches a directory entry
190 * @fname: the name being searched for
191 * @de_name: the name from the directory entry
192 * @de_name_len: the length of @de_name in bytes
193 *
194 * Normally @fname->disk_name will be set, and in that case we simply compare
195 * that to the name stored in the directory entry. The only exception is that
196 * if we don't have the key for an encrypted directory and a filename in it is
197 * very long, then we won't have the full disk_name and we'll instead need to
198 * match against the fscrypt_digested_name.
199 *
200 * Return: %true if the name matches, otherwise %false.
201 */
202static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
203 const u8 *de_name, u32 de_name_len)
204{
205 if (unlikely(!fname->disk_name.name)) {
206 const struct fscrypt_digested_name *n =
207 (const void *)fname->crypto_buf.name;
208 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
209 return false;
210 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
211 return false;
212 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
213 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
214 }
215
216 if (de_name_len != fname->disk_name.len)
217 return false;
218 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
219}
220
221/* bio.c */
222extern void fscrypt_decrypt_bio(struct bio *);
223extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
224 struct bio *bio);
225extern void fscrypt_pullback_bio_page(struct page **, bool);
226extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
227 unsigned int);
228
229/* hooks.c */
230extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers968dd6d2019-03-20 11:39:10 -0700231extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
232 struct dentry *dentry);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530233extern int __fscrypt_prepare_rename(struct inode *old_dir,
234 struct dentry *old_dentry,
235 struct inode *new_dir,
236 struct dentry *new_dentry,
237 unsigned int flags);
Eric Biggersb01531d2019-03-20 11:39:13 -0700238extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
239 struct fscrypt_name *fname);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530240extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
241 unsigned int max_len,
242 struct fscrypt_str *disk_link);
243extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
244 unsigned int len,
245 struct fscrypt_str *disk_link);
246extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
247 unsigned int max_size,
248 struct delayed_call *done);
249#else /* !CONFIG_FS_ENCRYPTION */
250
251static inline bool fscrypt_has_encryption_key(const struct inode *inode)
252{
253 return false;
254}
255
256static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
257{
258 return false;
259}
260
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700261static inline void fscrypt_handle_d_move(struct dentry *dentry)
262{
263}
264
Chandan Rajendra643fa962018-12-12 15:20:12 +0530265/* crypto.c */
266static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
267{
268}
269
Eric Biggerscd0265f2019-03-18 10:23:33 -0700270static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530271{
272 return ERR_PTR(-EOPNOTSUPP);
273}
274
275static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
276{
277 return;
278}
279
280static inline struct page *fscrypt_encrypt_page(const struct inode *inode,
281 struct page *page,
282 unsigned int len,
283 unsigned int offs,
284 u64 lblk_num, gfp_t gfp_flags)
285{
286 return ERR_PTR(-EOPNOTSUPP);
287}
288
289static inline int fscrypt_decrypt_page(const struct inode *inode,
290 struct page *page,
291 unsigned int len, unsigned int offs,
292 u64 lblk_num)
293{
294 return -EOPNOTSUPP;
295}
296
297static inline struct page *fscrypt_control_page(struct page *page)
298{
299 WARN_ON_ONCE(1);
300 return ERR_PTR(-EINVAL);
301}
302
303static inline void fscrypt_restore_control_page(struct page *page)
304{
305 return;
306}
307
308/* policy.c */
309static inline int fscrypt_ioctl_set_policy(struct file *filp,
310 const void __user *arg)
311{
312 return -EOPNOTSUPP;
313}
314
315static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
316{
317 return -EOPNOTSUPP;
318}
319
320static inline int fscrypt_has_permitted_context(struct inode *parent,
321 struct inode *child)
322{
323 return 0;
324}
325
326static inline int fscrypt_inherit_context(struct inode *parent,
327 struct inode *child,
328 void *fs_data, bool preload)
329{
330 return -EOPNOTSUPP;
331}
332
333/* keyinfo.c */
334static inline int fscrypt_get_encryption_info(struct inode *inode)
335{
336 return -EOPNOTSUPP;
337}
338
339static inline void fscrypt_put_encryption_info(struct inode *inode)
340{
341 return;
342}
343
344 /* fname.c */
345static inline int fscrypt_setup_filename(struct inode *dir,
346 const struct qstr *iname,
347 int lookup, struct fscrypt_name *fname)
348{
349 if (IS_ENCRYPTED(dir))
350 return -EOPNOTSUPP;
351
Eric Biggersb01531d2019-03-20 11:39:13 -0700352 memset(fname, 0, sizeof(*fname));
Chandan Rajendra643fa962018-12-12 15:20:12 +0530353 fname->usr_fname = iname;
354 fname->disk_name.name = (unsigned char *)iname->name;
355 fname->disk_name.len = iname->len;
356 return 0;
357}
358
359static inline void fscrypt_free_filename(struct fscrypt_name *fname)
360{
361 return;
362}
363
364static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
365 u32 max_encrypted_len,
366 struct fscrypt_str *crypto_str)
367{
368 return -EOPNOTSUPP;
369}
370
371static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
372{
373 return;
374}
375
376static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
377 u32 hash, u32 minor_hash,
378 const struct fscrypt_str *iname,
379 struct fscrypt_str *oname)
380{
381 return -EOPNOTSUPP;
382}
383
384static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
385 const u8 *de_name, u32 de_name_len)
386{
387 /* Encryption support disabled; use standard comparison */
388 if (de_name_len != fname->disk_name.len)
389 return false;
390 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
391}
392
393/* bio.c */
394static inline void fscrypt_decrypt_bio(struct bio *bio)
395{
396}
397
398static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
399 struct bio *bio)
400{
401}
402
403static inline void fscrypt_pullback_bio_page(struct page **page, bool restore)
404{
405 return;
406}
407
408static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
409 sector_t pblk, unsigned int len)
410{
411 return -EOPNOTSUPP;
412}
413
414/* hooks.c */
415
416static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
417{
418 if (IS_ENCRYPTED(inode))
419 return -EOPNOTSUPP;
420 return 0;
421}
422
Eric Biggers968dd6d2019-03-20 11:39:10 -0700423static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
424 struct dentry *dentry)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530425{
426 return -EOPNOTSUPP;
427}
428
429static inline int __fscrypt_prepare_rename(struct inode *old_dir,
430 struct dentry *old_dentry,
431 struct inode *new_dir,
432 struct dentry *new_dentry,
433 unsigned int flags)
434{
435 return -EOPNOTSUPP;
436}
437
438static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggersb01531d2019-03-20 11:39:13 -0700439 struct dentry *dentry,
440 struct fscrypt_name *fname)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530441{
442 return -EOPNOTSUPP;
443}
444
445static inline int __fscrypt_prepare_symlink(struct inode *dir,
446 unsigned int len,
447 unsigned int max_len,
448 struct fscrypt_str *disk_link)
449{
450 return -EOPNOTSUPP;
451}
452
453
454static inline int __fscrypt_encrypt_symlink(struct inode *inode,
455 const char *target,
456 unsigned int len,
457 struct fscrypt_str *disk_link)
458{
459 return -EOPNOTSUPP;
460}
461
462static inline const char *fscrypt_get_symlink(struct inode *inode,
463 const void *caddr,
464 unsigned int max_size,
465 struct delayed_call *done)
466{
467 return ERR_PTR(-EOPNOTSUPP);
468}
469#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700470
Eric Biggersd293c3e2017-10-09 12:15:39 -0700471/**
472 * fscrypt_require_key - require an inode's encryption key
473 * @inode: the inode we need the key for
474 *
475 * If the inode is encrypted, set up its encryption key if not already done.
476 * Then require that the key be present and return -ENOKEY otherwise.
477 *
478 * No locks are needed, and the key will live as long as the struct inode --- so
479 * it won't go away from under you.
480 *
481 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
482 * if a problem occurred while setting up the encryption key.
483 */
484static inline int fscrypt_require_key(struct inode *inode)
485{
486 if (IS_ENCRYPTED(inode)) {
487 int err = fscrypt_get_encryption_info(inode);
488
489 if (err)
490 return err;
491 if (!fscrypt_has_encryption_key(inode))
492 return -ENOKEY;
493 }
494 return 0;
495}
Dave Chinner734f0d22017-10-09 12:15:34 -0700496
Eric Biggers0ea87a92017-10-09 12:15:41 -0700497/**
498 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
499 * @old_dentry: an existing dentry for the inode being linked
500 * @dir: the target directory
501 * @dentry: negative dentry for the target filename
502 *
503 * A new link can only be added to an encrypted directory if the directory's
504 * encryption key is available --- since otherwise we'd have no way to encrypt
505 * the filename. Therefore, we first set up the directory's encryption key (if
506 * not already done) and return an error if it's unavailable.
507 *
508 * We also verify that the link will not violate the constraint that all files
509 * in an encrypted directory tree use the same encryption policy.
510 *
511 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800512 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700513 * another -errno code.
514 */
515static inline int fscrypt_prepare_link(struct dentry *old_dentry,
516 struct inode *dir,
517 struct dentry *dentry)
518{
519 if (IS_ENCRYPTED(dir))
Eric Biggers968dd6d2019-03-20 11:39:10 -0700520 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700521 return 0;
522}
523
Eric Biggers94b26f32017-10-09 12:15:42 -0700524/**
525 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
526 * @old_dir: source directory
527 * @old_dentry: dentry for source file
528 * @new_dir: target directory
529 * @new_dentry: dentry for target location (may be negative unless exchanging)
530 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
531 *
532 * Prepare for ->rename() where the source and/or target directories may be
533 * encrypted. A new link can only be added to an encrypted directory if the
534 * directory's encryption key is available --- since otherwise we'd have no way
535 * to encrypt the filename. A rename to an existing name, on the other hand,
536 * *is* cryptographically possible without the key. However, we take the more
537 * conservative approach and just forbid all no-key renames.
538 *
539 * We also verify that the rename will not violate the constraint that all files
540 * in an encrypted directory tree use the same encryption policy.
541 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800542 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700543 * rename would cause inconsistent encryption policies, or another -errno code.
544 */
545static inline int fscrypt_prepare_rename(struct inode *old_dir,
546 struct dentry *old_dentry,
547 struct inode *new_dir,
548 struct dentry *new_dentry,
549 unsigned int flags)
550{
551 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
552 return __fscrypt_prepare_rename(old_dir, old_dentry,
553 new_dir, new_dentry, flags);
554 return 0;
555}
556
Eric Biggers32c3cf02017-10-09 12:15:43 -0700557/**
558 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
559 * @dir: directory being searched
560 * @dentry: filename being looked up
Eric Biggersb01531d2019-03-20 11:39:13 -0700561 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700562 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700563 * Prepare for ->lookup() in a directory which may be encrypted by determining
564 * the name that will actually be used to search the directory on-disk. Lookups
565 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700566 * filenames are presented in encrypted form. Therefore, we'll try to set up
567 * the directory's encryption key, but even without it the lookup can continue.
568 *
Eric Biggers6cc24862019-03-20 11:39:09 -0700569 * This also installs a custom ->d_revalidate() method which will invalidate the
570 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700571 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700572 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
573 * correctly formed encoded ciphertext name, so a negative dentry should be
574 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700575 */
576static inline int fscrypt_prepare_lookup(struct inode *dir,
577 struct dentry *dentry,
Eric Biggersb01531d2019-03-20 11:39:13 -0700578 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700579{
580 if (IS_ENCRYPTED(dir))
Eric Biggersb01531d2019-03-20 11:39:13 -0700581 return __fscrypt_prepare_lookup(dir, dentry, fname);
582
583 memset(fname, 0, sizeof(*fname));
584 fname->usr_fname = &dentry->d_name;
585 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
586 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700587 return 0;
588}
589
Eric Biggers815dac32017-10-09 12:15:44 -0700590/**
591 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
592 * @dentry: dentry through which the inode is being changed
593 * @attr: attributes to change
594 *
595 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
596 * most attribute changes are allowed even without the encryption key. However,
597 * without the encryption key we do have to forbid truncates. This is needed
598 * because the size being truncated to may not be a multiple of the filesystem
599 * block size, and in that case we'd have to decrypt the final block, zero the
600 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
601 * filesystem block boundary, but it's simpler to just forbid all truncates ---
602 * and we already forbid all other contents modifications without the key.)
603 *
604 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
605 * if a problem occurred while setting up the encryption key.
606 */
607static inline int fscrypt_prepare_setattr(struct dentry *dentry,
608 struct iattr *attr)
609{
610 if (attr->ia_valid & ATTR_SIZE)
611 return fscrypt_require_key(d_inode(dentry));
612 return 0;
613}
614
Eric Biggers76e81d62018-01-05 10:45:01 -0800615/**
616 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
617 * @dir: directory in which the symlink is being created
618 * @target: plaintext symlink target
619 * @len: length of @target excluding null terminator
620 * @max_len: space the filesystem has available to store the symlink target
621 * @disk_link: (out) the on-disk symlink target being prepared
622 *
623 * This function computes the size the symlink target will require on-disk,
624 * stores it in @disk_link->len, and validates it against @max_len. An
625 * encrypted symlink may be longer than the original.
626 *
627 * Additionally, @disk_link->name is set to @target if the symlink will be
628 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
629 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
630 * on-disk target later. (The reason for the two-step process is that some
631 * filesystems need to know the size of the symlink target before creating the
632 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
633 *
634 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
635 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
636 * occurred while setting up the encryption key.
637 */
638static inline int fscrypt_prepare_symlink(struct inode *dir,
639 const char *target,
640 unsigned int len,
641 unsigned int max_len,
642 struct fscrypt_str *disk_link)
643{
644 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
645 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
646
647 disk_link->name = (unsigned char *)target;
648 disk_link->len = len + 1;
649 if (disk_link->len > max_len)
650 return -ENAMETOOLONG;
651 return 0;
652}
653
654/**
655 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
656 * @inode: symlink inode
657 * @target: plaintext symlink target
658 * @len: length of @target excluding null terminator
659 * @disk_link: (in/out) the on-disk symlink target being prepared
660 *
661 * If the symlink target needs to be encrypted, then this function encrypts it
662 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
663 * previously to compute @disk_link->len. If the filesystem did not allocate a
664 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
665 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
666 *
667 * Return: 0 on success, -errno on failure
668 */
669static inline int fscrypt_encrypt_symlink(struct inode *inode,
670 const char *target,
671 unsigned int len,
672 struct fscrypt_str *disk_link)
673{
674 if (IS_ENCRYPTED(inode))
675 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
676 return 0;
677}
678
Dave Chinner734f0d22017-10-09 12:15:34 -0700679#endif /* _LINUX_FSCRYPT_H */