blob: 6cf8a34523ffc0505a9873fab13dca541264af99 [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 *);
Eric Biggerscd0265f2019-03-18 10:23:33 -070093extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendra643fa962018-12-12 15:20:12 +053094extern 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);
233#else /* !CONFIG_FS_ENCRYPTION */
234
235static inline bool fscrypt_has_encryption_key(const struct inode *inode)
236{
237 return false;
238}
239
240static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
241{
242 return false;
243}
244
245/* crypto.c */
246static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
247{
248}
249
Eric Biggerscd0265f2019-03-18 10:23:33 -0700250static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530251{
252 return ERR_PTR(-EOPNOTSUPP);
253}
254
255static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
256{
257 return;
258}
259
260static inline struct page *fscrypt_encrypt_page(const struct inode *inode,
261 struct page *page,
262 unsigned int len,
263 unsigned int offs,
264 u64 lblk_num, gfp_t gfp_flags)
265{
266 return ERR_PTR(-EOPNOTSUPP);
267}
268
269static inline int fscrypt_decrypt_page(const struct inode *inode,
270 struct page *page,
271 unsigned int len, unsigned int offs,
272 u64 lblk_num)
273{
274 return -EOPNOTSUPP;
275}
276
277static inline struct page *fscrypt_control_page(struct page *page)
278{
279 WARN_ON_ONCE(1);
280 return ERR_PTR(-EINVAL);
281}
282
283static inline void fscrypt_restore_control_page(struct page *page)
284{
285 return;
286}
287
288/* policy.c */
289static inline int fscrypt_ioctl_set_policy(struct file *filp,
290 const void __user *arg)
291{
292 return -EOPNOTSUPP;
293}
294
295static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
296{
297 return -EOPNOTSUPP;
298}
299
300static inline int fscrypt_has_permitted_context(struct inode *parent,
301 struct inode *child)
302{
303 return 0;
304}
305
306static inline int fscrypt_inherit_context(struct inode *parent,
307 struct inode *child,
308 void *fs_data, bool preload)
309{
310 return -EOPNOTSUPP;
311}
312
313/* keyinfo.c */
314static inline int fscrypt_get_encryption_info(struct inode *inode)
315{
316 return -EOPNOTSUPP;
317}
318
319static inline void fscrypt_put_encryption_info(struct inode *inode)
320{
321 return;
322}
323
324 /* fname.c */
325static inline int fscrypt_setup_filename(struct inode *dir,
326 const struct qstr *iname,
327 int lookup, struct fscrypt_name *fname)
328{
329 if (IS_ENCRYPTED(dir))
330 return -EOPNOTSUPP;
331
332 memset(fname, 0, sizeof(struct fscrypt_name));
333 fname->usr_fname = iname;
334 fname->disk_name.name = (unsigned char *)iname->name;
335 fname->disk_name.len = iname->len;
336 return 0;
337}
338
339static inline void fscrypt_free_filename(struct fscrypt_name *fname)
340{
341 return;
342}
343
344static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
345 u32 max_encrypted_len,
346 struct fscrypt_str *crypto_str)
347{
348 return -EOPNOTSUPP;
349}
350
351static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
352{
353 return;
354}
355
356static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
357 u32 hash, u32 minor_hash,
358 const struct fscrypt_str *iname,
359 struct fscrypt_str *oname)
360{
361 return -EOPNOTSUPP;
362}
363
364static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
365 const u8 *de_name, u32 de_name_len)
366{
367 /* Encryption support disabled; use standard comparison */
368 if (de_name_len != fname->disk_name.len)
369 return false;
370 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
371}
372
373/* bio.c */
374static inline void fscrypt_decrypt_bio(struct bio *bio)
375{
376}
377
378static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
379 struct bio *bio)
380{
381}
382
383static inline void fscrypt_pullback_bio_page(struct page **page, bool restore)
384{
385 return;
386}
387
388static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
389 sector_t pblk, unsigned int len)
390{
391 return -EOPNOTSUPP;
392}
393
394/* hooks.c */
395
396static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
397{
398 if (IS_ENCRYPTED(inode))
399 return -EOPNOTSUPP;
400 return 0;
401}
402
403static inline int __fscrypt_prepare_link(struct inode *inode,
404 struct inode *dir)
405{
406 return -EOPNOTSUPP;
407}
408
409static inline int __fscrypt_prepare_rename(struct inode *old_dir,
410 struct dentry *old_dentry,
411 struct inode *new_dir,
412 struct dentry *new_dentry,
413 unsigned int flags)
414{
415 return -EOPNOTSUPP;
416}
417
418static inline int __fscrypt_prepare_lookup(struct inode *dir,
419 struct dentry *dentry)
420{
421 return -EOPNOTSUPP;
422}
423
424static inline int __fscrypt_prepare_symlink(struct inode *dir,
425 unsigned int len,
426 unsigned int max_len,
427 struct fscrypt_str *disk_link)
428{
429 return -EOPNOTSUPP;
430}
431
432
433static inline int __fscrypt_encrypt_symlink(struct inode *inode,
434 const char *target,
435 unsigned int len,
436 struct fscrypt_str *disk_link)
437{
438 return -EOPNOTSUPP;
439}
440
441static inline const char *fscrypt_get_symlink(struct inode *inode,
442 const void *caddr,
443 unsigned int max_size,
444 struct delayed_call *done)
445{
446 return ERR_PTR(-EOPNOTSUPP);
447}
448#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700449
Eric Biggersd293c3e2017-10-09 12:15:39 -0700450/**
451 * fscrypt_require_key - require an inode's encryption key
452 * @inode: the inode we need the key for
453 *
454 * If the inode is encrypted, set up its encryption key if not already done.
455 * Then require that the key be present and return -ENOKEY otherwise.
456 *
457 * No locks are needed, and the key will live as long as the struct inode --- so
458 * it won't go away from under you.
459 *
460 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
461 * if a problem occurred while setting up the encryption key.
462 */
463static inline int fscrypt_require_key(struct inode *inode)
464{
465 if (IS_ENCRYPTED(inode)) {
466 int err = fscrypt_get_encryption_info(inode);
467
468 if (err)
469 return err;
470 if (!fscrypt_has_encryption_key(inode))
471 return -ENOKEY;
472 }
473 return 0;
474}
Dave Chinner734f0d22017-10-09 12:15:34 -0700475
Eric Biggers0ea87a92017-10-09 12:15:41 -0700476/**
477 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
478 * @old_dentry: an existing dentry for the inode being linked
479 * @dir: the target directory
480 * @dentry: negative dentry for the target filename
481 *
482 * A new link can only be added to an encrypted directory if the directory's
483 * encryption key is available --- since otherwise we'd have no way to encrypt
484 * the filename. Therefore, we first set up the directory's encryption key (if
485 * not already done) and return an error if it's unavailable.
486 *
487 * We also verify that the link will not violate the constraint that all files
488 * in an encrypted directory tree use the same encryption policy.
489 *
490 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800491 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700492 * another -errno code.
493 */
494static inline int fscrypt_prepare_link(struct dentry *old_dentry,
495 struct inode *dir,
496 struct dentry *dentry)
497{
498 if (IS_ENCRYPTED(dir))
499 return __fscrypt_prepare_link(d_inode(old_dentry), dir);
500 return 0;
501}
502
Eric Biggers94b26f32017-10-09 12:15:42 -0700503/**
504 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
505 * @old_dir: source directory
506 * @old_dentry: dentry for source file
507 * @new_dir: target directory
508 * @new_dentry: dentry for target location (may be negative unless exchanging)
509 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
510 *
511 * Prepare for ->rename() where the source and/or target directories may be
512 * encrypted. A new link can only be added to an encrypted directory if the
513 * directory's encryption key is available --- since otherwise we'd have no way
514 * to encrypt the filename. A rename to an existing name, on the other hand,
515 * *is* cryptographically possible without the key. However, we take the more
516 * conservative approach and just forbid all no-key renames.
517 *
518 * We also verify that the rename will not violate the constraint that all files
519 * in an encrypted directory tree use the same encryption policy.
520 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800521 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700522 * rename would cause inconsistent encryption policies, or another -errno code.
523 */
524static inline int fscrypt_prepare_rename(struct inode *old_dir,
525 struct dentry *old_dentry,
526 struct inode *new_dir,
527 struct dentry *new_dentry,
528 unsigned int flags)
529{
530 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
531 return __fscrypt_prepare_rename(old_dir, old_dentry,
532 new_dir, new_dentry, flags);
533 return 0;
534}
535
Eric Biggers32c3cf02017-10-09 12:15:43 -0700536/**
537 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
538 * @dir: directory being searched
539 * @dentry: filename being looked up
540 * @flags: lookup flags
541 *
542 * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be
543 * done with or without the directory's encryption key; without the key,
544 * filenames are presented in encrypted form. Therefore, we'll try to set up
545 * the directory's encryption key, but even without it the lookup can continue.
546 *
547 * To allow invalidating stale dentries if the directory's encryption key is
548 * added later, we also install a custom ->d_revalidate() method and use the
549 * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a
550 * plaintext name (flag set) or a ciphertext name (flag cleared).
551 *
552 * Return: 0 on success, -errno if a problem occurred while setting up the
553 * encryption key
554 */
555static inline int fscrypt_prepare_lookup(struct inode *dir,
556 struct dentry *dentry,
557 unsigned int flags)
558{
559 if (IS_ENCRYPTED(dir))
560 return __fscrypt_prepare_lookup(dir, dentry);
561 return 0;
562}
563
Eric Biggers815dac32017-10-09 12:15:44 -0700564/**
565 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
566 * @dentry: dentry through which the inode is being changed
567 * @attr: attributes to change
568 *
569 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
570 * most attribute changes are allowed even without the encryption key. However,
571 * without the encryption key we do have to forbid truncates. This is needed
572 * because the size being truncated to may not be a multiple of the filesystem
573 * block size, and in that case we'd have to decrypt the final block, zero the
574 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
575 * filesystem block boundary, but it's simpler to just forbid all truncates ---
576 * and we already forbid all other contents modifications without the key.)
577 *
578 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
579 * if a problem occurred while setting up the encryption key.
580 */
581static inline int fscrypt_prepare_setattr(struct dentry *dentry,
582 struct iattr *attr)
583{
584 if (attr->ia_valid & ATTR_SIZE)
585 return fscrypt_require_key(d_inode(dentry));
586 return 0;
587}
588
Eric Biggers76e81d62018-01-05 10:45:01 -0800589/**
590 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
591 * @dir: directory in which the symlink is being created
592 * @target: plaintext symlink target
593 * @len: length of @target excluding null terminator
594 * @max_len: space the filesystem has available to store the symlink target
595 * @disk_link: (out) the on-disk symlink target being prepared
596 *
597 * This function computes the size the symlink target will require on-disk,
598 * stores it in @disk_link->len, and validates it against @max_len. An
599 * encrypted symlink may be longer than the original.
600 *
601 * Additionally, @disk_link->name is set to @target if the symlink will be
602 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
603 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
604 * on-disk target later. (The reason for the two-step process is that some
605 * filesystems need to know the size of the symlink target before creating the
606 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
607 *
608 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
609 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
610 * occurred while setting up the encryption key.
611 */
612static inline int fscrypt_prepare_symlink(struct inode *dir,
613 const char *target,
614 unsigned int len,
615 unsigned int max_len,
616 struct fscrypt_str *disk_link)
617{
618 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
619 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
620
621 disk_link->name = (unsigned char *)target;
622 disk_link->len = len + 1;
623 if (disk_link->len > max_len)
624 return -ENAMETOOLONG;
625 return 0;
626}
627
628/**
629 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
630 * @inode: symlink inode
631 * @target: plaintext symlink target
632 * @len: length of @target excluding null terminator
633 * @disk_link: (in/out) the on-disk symlink target being prepared
634 *
635 * If the symlink target needs to be encrypted, then this function encrypts it
636 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
637 * previously to compute @disk_link->len. If the filesystem did not allocate a
638 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
639 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
640 *
641 * Return: 0 on success, -errno on failure
642 */
643static inline int fscrypt_encrypt_symlink(struct inode *inode,
644 const char *target,
645 unsigned int len,
646 struct fscrypt_str *disk_link)
647{
648 if (IS_ENCRYPTED(inode))
649 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
650 return 0;
651}
652
Dave Chinner734f0d22017-10-09 12:15:34 -0700653#endif /* _LINUX_FSCRYPT_H */