blob: 855f743c226e6cb11c79dc2d85a2f21087767e56 [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{
Eric Biggerse37a7842019-04-11 14:32:15 -070082 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
83 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +053084}
85
86static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
87{
88 return inode->i_sb->s_cop->dummy_context &&
89 inode->i_sb->s_cop->dummy_context(inode);
90}
91
92/* crypto.c */
93extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggerscd0265f2019-03-18 10:23:33 -070094extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendra643fa962018-12-12 15:20:12 +053095extern void fscrypt_release_ctx(struct fscrypt_ctx *);
96extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
97 unsigned int, unsigned int,
98 u64, gfp_t);
99extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
100 unsigned int, u64);
101
102static inline struct page *fscrypt_control_page(struct page *page)
103{
104 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
105}
106
107extern void fscrypt_restore_control_page(struct page *);
108
109/* policy.c */
110extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
111extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
112extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
113extern int fscrypt_inherit_context(struct inode *, struct inode *,
114 void *, bool);
115/* keyinfo.c */
116extern int fscrypt_get_encryption_info(struct inode *);
117extern void fscrypt_put_encryption_info(struct inode *);
118
119/* fname.c */
120extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
121 int lookup, struct fscrypt_name *);
122
123static inline void fscrypt_free_filename(struct fscrypt_name *fname)
124{
125 kfree(fname->crypto_buf.name);
126}
127
128extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
129 struct fscrypt_str *);
130extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
131extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
132 const struct fscrypt_str *, struct fscrypt_str *);
133
134#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
135
136/* Extracts the second-to-last ciphertext block; see explanation below */
137#define FSCRYPT_FNAME_DIGEST(name, len) \
138 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
139 FS_CRYPTO_BLOCK_SIZE))
140
141#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
142
143/**
144 * fscrypt_digested_name - alternate identifier for an on-disk filename
145 *
146 * When userspace lists an encrypted directory without access to the key,
147 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
148 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
149 * full ciphertext (base64-encoded). This is necessary to allow supporting
150 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
151 *
152 * To make it possible for filesystems to still find the correct directory entry
153 * despite not knowing the full on-disk name, we encode any filesystem-specific
154 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
155 * followed by the second-to-last ciphertext block of the filename. Due to the
156 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
157 * depends on the full plaintext. (Note that ciphertext stealing causes the
158 * last two blocks to appear "flipped".) This makes accidental collisions very
159 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
160 * share the same filesystem-specific hashes.
161 *
162 * However, this scheme isn't immune to intentional collisions, which can be
163 * created by anyone able to create arbitrary plaintext filenames and view them
164 * without the key. Making the "digest" be a real cryptographic hash like
165 * SHA-256 over the full ciphertext would prevent this, although it would be
166 * less efficient and harder to implement, especially since the filesystem would
167 * need to calculate it for each directory entry examined during a search.
168 */
169struct fscrypt_digested_name {
170 u32 hash;
171 u32 minor_hash;
172 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
173};
174
175/**
176 * fscrypt_match_name() - test whether the given name matches a directory entry
177 * @fname: the name being searched for
178 * @de_name: the name from the directory entry
179 * @de_name_len: the length of @de_name in bytes
180 *
181 * Normally @fname->disk_name will be set, and in that case we simply compare
182 * that to the name stored in the directory entry. The only exception is that
183 * if we don't have the key for an encrypted directory and a filename in it is
184 * very long, then we won't have the full disk_name and we'll instead need to
185 * match against the fscrypt_digested_name.
186 *
187 * Return: %true if the name matches, otherwise %false.
188 */
189static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
190 const u8 *de_name, u32 de_name_len)
191{
192 if (unlikely(!fname->disk_name.name)) {
193 const struct fscrypt_digested_name *n =
194 (const void *)fname->crypto_buf.name;
195 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
196 return false;
197 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
198 return false;
199 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
200 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
201 }
202
203 if (de_name_len != fname->disk_name.len)
204 return false;
205 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
206}
207
208/* bio.c */
209extern void fscrypt_decrypt_bio(struct bio *);
210extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
211 struct bio *bio);
212extern void fscrypt_pullback_bio_page(struct page **, bool);
213extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
214 unsigned int);
215
216/* hooks.c */
217extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers968dd6d2019-03-20 11:39:10 -0700218extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
219 struct dentry *dentry);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530220extern int __fscrypt_prepare_rename(struct inode *old_dir,
221 struct dentry *old_dentry,
222 struct inode *new_dir,
223 struct dentry *new_dentry,
224 unsigned int flags);
225extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
226extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
227 unsigned int max_len,
228 struct fscrypt_str *disk_link);
229extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
230 unsigned int len,
231 struct fscrypt_str *disk_link);
232extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
233 unsigned int max_size,
234 struct delayed_call *done);
235#else /* !CONFIG_FS_ENCRYPTION */
236
237static inline bool fscrypt_has_encryption_key(const struct inode *inode)
238{
239 return false;
240}
241
242static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
243{
244 return false;
245}
246
247/* crypto.c */
248static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
249{
250}
251
Eric Biggerscd0265f2019-03-18 10:23:33 -0700252static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530253{
254 return ERR_PTR(-EOPNOTSUPP);
255}
256
257static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
258{
259 return;
260}
261
262static inline struct page *fscrypt_encrypt_page(const struct inode *inode,
263 struct page *page,
264 unsigned int len,
265 unsigned int offs,
266 u64 lblk_num, gfp_t gfp_flags)
267{
268 return ERR_PTR(-EOPNOTSUPP);
269}
270
271static inline int fscrypt_decrypt_page(const struct inode *inode,
272 struct page *page,
273 unsigned int len, unsigned int offs,
274 u64 lblk_num)
275{
276 return -EOPNOTSUPP;
277}
278
279static inline struct page *fscrypt_control_page(struct page *page)
280{
281 WARN_ON_ONCE(1);
282 return ERR_PTR(-EINVAL);
283}
284
285static inline void fscrypt_restore_control_page(struct page *page)
286{
287 return;
288}
289
290/* policy.c */
291static inline int fscrypt_ioctl_set_policy(struct file *filp,
292 const void __user *arg)
293{
294 return -EOPNOTSUPP;
295}
296
297static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
298{
299 return -EOPNOTSUPP;
300}
301
302static inline int fscrypt_has_permitted_context(struct inode *parent,
303 struct inode *child)
304{
305 return 0;
306}
307
308static inline int fscrypt_inherit_context(struct inode *parent,
309 struct inode *child,
310 void *fs_data, bool preload)
311{
312 return -EOPNOTSUPP;
313}
314
315/* keyinfo.c */
316static inline int fscrypt_get_encryption_info(struct inode *inode)
317{
318 return -EOPNOTSUPP;
319}
320
321static inline void fscrypt_put_encryption_info(struct inode *inode)
322{
323 return;
324}
325
326 /* fname.c */
327static inline int fscrypt_setup_filename(struct inode *dir,
328 const struct qstr *iname,
329 int lookup, struct fscrypt_name *fname)
330{
331 if (IS_ENCRYPTED(dir))
332 return -EOPNOTSUPP;
333
334 memset(fname, 0, sizeof(struct fscrypt_name));
335 fname->usr_fname = iname;
336 fname->disk_name.name = (unsigned char *)iname->name;
337 fname->disk_name.len = iname->len;
338 return 0;
339}
340
341static inline void fscrypt_free_filename(struct fscrypt_name *fname)
342{
343 return;
344}
345
346static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
347 u32 max_encrypted_len,
348 struct fscrypt_str *crypto_str)
349{
350 return -EOPNOTSUPP;
351}
352
353static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
354{
355 return;
356}
357
358static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
359 u32 hash, u32 minor_hash,
360 const struct fscrypt_str *iname,
361 struct fscrypt_str *oname)
362{
363 return -EOPNOTSUPP;
364}
365
366static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
367 const u8 *de_name, u32 de_name_len)
368{
369 /* Encryption support disabled; use standard comparison */
370 if (de_name_len != fname->disk_name.len)
371 return false;
372 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
373}
374
375/* bio.c */
376static inline void fscrypt_decrypt_bio(struct bio *bio)
377{
378}
379
380static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
381 struct bio *bio)
382{
383}
384
385static inline void fscrypt_pullback_bio_page(struct page **page, bool restore)
386{
387 return;
388}
389
390static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
391 sector_t pblk, unsigned int len)
392{
393 return -EOPNOTSUPP;
394}
395
396/* hooks.c */
397
398static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
399{
400 if (IS_ENCRYPTED(inode))
401 return -EOPNOTSUPP;
402 return 0;
403}
404
Eric Biggers968dd6d2019-03-20 11:39:10 -0700405static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
406 struct dentry *dentry)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530407{
408 return -EOPNOTSUPP;
409}
410
411static inline int __fscrypt_prepare_rename(struct inode *old_dir,
412 struct dentry *old_dentry,
413 struct inode *new_dir,
414 struct dentry *new_dentry,
415 unsigned int flags)
416{
417 return -EOPNOTSUPP;
418}
419
420static inline int __fscrypt_prepare_lookup(struct inode *dir,
421 struct dentry *dentry)
422{
423 return -EOPNOTSUPP;
424}
425
426static inline int __fscrypt_prepare_symlink(struct inode *dir,
427 unsigned int len,
428 unsigned int max_len,
429 struct fscrypt_str *disk_link)
430{
431 return -EOPNOTSUPP;
432}
433
434
435static inline int __fscrypt_encrypt_symlink(struct inode *inode,
436 const char *target,
437 unsigned int len,
438 struct fscrypt_str *disk_link)
439{
440 return -EOPNOTSUPP;
441}
442
443static inline const char *fscrypt_get_symlink(struct inode *inode,
444 const void *caddr,
445 unsigned int max_size,
446 struct delayed_call *done)
447{
448 return ERR_PTR(-EOPNOTSUPP);
449}
450#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700451
Eric Biggersd293c3e2017-10-09 12:15:39 -0700452/**
453 * fscrypt_require_key - require an inode's encryption key
454 * @inode: the inode we need the key for
455 *
456 * If the inode is encrypted, set up its encryption key if not already done.
457 * Then require that the key be present and return -ENOKEY otherwise.
458 *
459 * No locks are needed, and the key will live as long as the struct inode --- so
460 * it won't go away from under you.
461 *
462 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
463 * if a problem occurred while setting up the encryption key.
464 */
465static inline int fscrypt_require_key(struct inode *inode)
466{
467 if (IS_ENCRYPTED(inode)) {
468 int err = fscrypt_get_encryption_info(inode);
469
470 if (err)
471 return err;
472 if (!fscrypt_has_encryption_key(inode))
473 return -ENOKEY;
474 }
475 return 0;
476}
Dave Chinner734f0d22017-10-09 12:15:34 -0700477
Eric Biggers0ea87a92017-10-09 12:15:41 -0700478/**
479 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
480 * @old_dentry: an existing dentry for the inode being linked
481 * @dir: the target directory
482 * @dentry: negative dentry for the target filename
483 *
484 * A new link can only be added to an encrypted directory if the directory's
485 * encryption key is available --- since otherwise we'd have no way to encrypt
486 * the filename. Therefore, we first set up the directory's encryption key (if
487 * not already done) and return an error if it's unavailable.
488 *
489 * We also verify that the link will not violate the constraint that all files
490 * in an encrypted directory tree use the same encryption policy.
491 *
492 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800493 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700494 * another -errno code.
495 */
496static inline int fscrypt_prepare_link(struct dentry *old_dentry,
497 struct inode *dir,
498 struct dentry *dentry)
499{
500 if (IS_ENCRYPTED(dir))
Eric Biggers968dd6d2019-03-20 11:39:10 -0700501 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700502 return 0;
503}
504
Eric Biggers94b26f32017-10-09 12:15:42 -0700505/**
506 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
507 * @old_dir: source directory
508 * @old_dentry: dentry for source file
509 * @new_dir: target directory
510 * @new_dentry: dentry for target location (may be negative unless exchanging)
511 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
512 *
513 * Prepare for ->rename() where the source and/or target directories may be
514 * encrypted. A new link can only be added to an encrypted directory if the
515 * directory's encryption key is available --- since otherwise we'd have no way
516 * to encrypt the filename. A rename to an existing name, on the other hand,
517 * *is* cryptographically possible without the key. However, we take the more
518 * conservative approach and just forbid all no-key renames.
519 *
520 * We also verify that the rename will not violate the constraint that all files
521 * in an encrypted directory tree use the same encryption policy.
522 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800523 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700524 * rename would cause inconsistent encryption policies, or another -errno code.
525 */
526static inline int fscrypt_prepare_rename(struct inode *old_dir,
527 struct dentry *old_dentry,
528 struct inode *new_dir,
529 struct dentry *new_dentry,
530 unsigned int flags)
531{
532 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
533 return __fscrypt_prepare_rename(old_dir, old_dentry,
534 new_dir, new_dentry, flags);
535 return 0;
536}
537
Eric Biggers32c3cf02017-10-09 12:15:43 -0700538/**
539 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
540 * @dir: directory being searched
541 * @dentry: filename being looked up
542 * @flags: lookup flags
543 *
544 * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be
545 * done with or without the directory's encryption key; without the key,
546 * filenames are presented in encrypted form. Therefore, we'll try to set up
547 * the directory's encryption key, but even without it the lookup can continue.
548 *
Eric Biggers6cc24862019-03-20 11:39:09 -0700549 * This also installs a custom ->d_revalidate() method which will invalidate the
550 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700551 *
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 */