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