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