blob: 5d777d7a363c1f78fc4dcf08e0bad0880e9be860 [file] [log] [blame]
Jaegeuk Kim8dec0742017-06-22 12:14:40 -07001/*
2 * fscrypt.h: declarations for per-file encryption
3 *
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +05304 * Filesystems that implement per-file encryption must include this header
5 * file.
Jaegeuk Kim8dec0742017-06-22 12:14:40 -07006 *
7 * Copyright (C) 2015, Google, Inc.
8 *
9 * Written by Michael Halcrow, 2015.
10 * Modified by Jaegeuk Kim, 2015.
11 */
12#ifndef _LINUX_FSCRYPT_H
13#define _LINUX_FSCRYPT_H
14
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070015#include <linux/fs.h>
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +053016#include <linux/mm.h>
17#include <linux/slab.h>
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070018
19#define FS_CRYPTO_BLOCK_SIZE 16
20
Jaegeuk Kimd9197652018-01-05 10:44:52 -080021struct fscrypt_ctx;
Neeraj Soni36c65122018-04-18 21:04:46 +053022
Neeraj Soni933c9442018-07-26 17:27:52 +053023/* iv sector for security/pfe/pfk_fscrypt.c and f2fs */
Neeraj Soni36c65122018-04-18 21:04:46 +053024#define PG_DUN(i, p) \
Neeraj Soni933c9442018-07-26 17:27:52 +053025 (((((u64)(i)->i_ino) & 0xffffffff) << 32) | ((p)->index & 0xffffffff))
Neeraj Soni36c65122018-04-18 21:04:46 +053026
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070027struct fscrypt_info;
28
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070029struct fscrypt_str {
30 unsigned char *name;
31 u32 len;
32};
33
34struct fscrypt_name {
35 const struct qstr *usr_fname;
36 struct fscrypt_str disk_name;
37 u32 hash;
38 u32 minor_hash;
39 struct fscrypt_str crypto_buf;
Eric Biggers3a3ac012019-03-20 11:39:13 -070040 bool is_ciphertext_name;
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070041};
42
43#define FSTR_INIT(n, l) { .name = n, .len = l }
44#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
45#define fname_name(p) ((p)->disk_name.name)
46#define fname_len(p) ((p)->disk_name.len)
47
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +053048#ifdef CONFIG_FS_ENCRYPTION
49/*
50 * fscrypt superblock flags
51 */
52#define FS_CFLG_OWN_PAGES (1U << 1)
53
54/*
55 * crypto operations for filesystems
56 */
57struct fscrypt_operations {
58 unsigned int flags;
59 const char *key_prefix;
60 int (*get_context)(struct inode *, void *, size_t);
61 int (*set_context)(struct inode *, const void *, size_t, void *);
62 bool (*dummy_context)(struct inode *);
63 bool (*empty_dir)(struct inode *);
64 unsigned int max_namelen;
65};
66
67struct fscrypt_ctx {
68 union {
69 struct {
70 struct page *bounce_page; /* Ciphertext page */
71 struct page *control_page; /* Original page */
72 } w;
73 struct {
74 struct bio *bio;
75 struct work_struct work;
76 } r;
77 struct list_head free_list; /* Free list */
78 };
79 u8 flags; /* Flags */
80};
81
82static inline bool fscrypt_has_encryption_key(const struct inode *inode)
83{
Eric Biggers70e22212019-04-11 14:32:15 -070084 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
85 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +053086}
87
88static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
89{
90 return inode->i_sb->s_cop->dummy_context &&
91 inode->i_sb->s_cop->dummy_context(inode);
92}
93
Eric Biggers7bc138e2019-03-20 11:39:11 -070094/*
95 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
96 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
97 * must be cleared. Note that we don't have to support arbitrary moves of this
98 * flag because fscrypt doesn't allow encrypted aliases to be the source or
99 * target of a rename().
100 */
101static inline void fscrypt_handle_d_move(struct dentry *dentry)
102{
103 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
104}
105
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530106/* crypto.c */
107extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggers0db29a12019-03-18 10:23:33 -0700108extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530109extern void fscrypt_release_ctx(struct fscrypt_ctx *);
110extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
111 unsigned int, unsigned int,
112 u64, gfp_t);
113extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
114 unsigned int, u64);
115
116static inline struct page *fscrypt_control_page(struct page *page)
117{
118 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
119}
120
121extern void fscrypt_restore_control_page(struct page *);
122
123/* policy.c */
124extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
125extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
126extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
127extern int fscrypt_inherit_context(struct inode *, struct inode *,
128 void *, bool);
129/* keyinfo.c */
130extern int fscrypt_get_encryption_info(struct inode *);
131extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggersac8894a2019-04-10 13:21:15 -0700132extern void fscrypt_free_inode(struct inode *);
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530133
134/* fname.c */
135extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
136 int lookup, struct fscrypt_name *);
137
138static inline void fscrypt_free_filename(struct fscrypt_name *fname)
139{
140 kfree(fname->crypto_buf.name);
141}
142
143extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
144 struct fscrypt_str *);
145extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
146extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
147 const struct fscrypt_str *, struct fscrypt_str *);
148
149#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
150
151/* Extracts the second-to-last ciphertext block; see explanation below */
152#define FSCRYPT_FNAME_DIGEST(name, len) \
153 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
154 FS_CRYPTO_BLOCK_SIZE))
155
156#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
157
158/**
159 * fscrypt_digested_name - alternate identifier for an on-disk filename
160 *
161 * When userspace lists an encrypted directory without access to the key,
162 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
163 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
164 * full ciphertext (base64-encoded). This is necessary to allow supporting
165 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
166 *
167 * To make it possible for filesystems to still find the correct directory entry
168 * despite not knowing the full on-disk name, we encode any filesystem-specific
169 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
170 * followed by the second-to-last ciphertext block of the filename. Due to the
171 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
172 * depends on the full plaintext. (Note that ciphertext stealing causes the
173 * last two blocks to appear "flipped".) This makes accidental collisions very
174 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
175 * share the same filesystem-specific hashes.
176 *
177 * However, this scheme isn't immune to intentional collisions, which can be
178 * created by anyone able to create arbitrary plaintext filenames and view them
179 * without the key. Making the "digest" be a real cryptographic hash like
180 * SHA-256 over the full ciphertext would prevent this, although it would be
181 * less efficient and harder to implement, especially since the filesystem would
182 * need to calculate it for each directory entry examined during a search.
183 */
184struct fscrypt_digested_name {
185 u32 hash;
186 u32 minor_hash;
187 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
188};
189
190/**
191 * fscrypt_match_name() - test whether the given name matches a directory entry
192 * @fname: the name being searched for
193 * @de_name: the name from the directory entry
194 * @de_name_len: the length of @de_name in bytes
195 *
196 * Normally @fname->disk_name will be set, and in that case we simply compare
197 * that to the name stored in the directory entry. The only exception is that
198 * if we don't have the key for an encrypted directory and a filename in it is
199 * very long, then we won't have the full disk_name and we'll instead need to
200 * match against the fscrypt_digested_name.
201 *
202 * Return: %true if the name matches, otherwise %false.
203 */
204static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
205 const u8 *de_name, u32 de_name_len)
206{
207 if (unlikely(!fname->disk_name.name)) {
208 const struct fscrypt_digested_name *n =
209 (const void *)fname->crypto_buf.name;
210 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
211 return false;
212 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
213 return false;
214 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
215 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
216 }
217
218 if (de_name_len != fname->disk_name.len)
219 return false;
220 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
221}
222
223/* bio.c */
224extern void fscrypt_decrypt_bio(struct bio *);
225extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
226 struct bio *bio);
227extern void fscrypt_pullback_bio_page(struct page **, bool);
228extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
229 unsigned int);
230
231/* hooks.c */
232extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggersef5b1542019-03-20 11:39:10 -0700233extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
234 struct dentry *dentry);
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530235extern int __fscrypt_prepare_rename(struct inode *old_dir,
236 struct dentry *old_dentry,
237 struct inode *new_dir,
238 struct dentry *new_dentry,
239 unsigned int flags);
Eric Biggers3a3ac012019-03-20 11:39:13 -0700240extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
241 struct fscrypt_name *fname);
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530242extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
243 unsigned int max_len,
244 struct fscrypt_str *disk_link);
245extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
246 unsigned int len,
247 struct fscrypt_str *disk_link);
248extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
249 unsigned int max_size,
250 struct delayed_call *done);
251#else /* !CONFIG_FS_ENCRYPTION */
252
253static inline bool fscrypt_has_encryption_key(const struct inode *inode)
254{
255 return false;
256}
257
258static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
259{
260 return false;
261}
262
Eric Biggers7bc138e2019-03-20 11:39:11 -0700263static inline void fscrypt_handle_d_move(struct dentry *dentry)
264{
265}
266
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530267/* crypto.c */
268static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
269{
270}
271
Eric Biggers0db29a12019-03-18 10:23:33 -0700272static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530273{
274 return ERR_PTR(-EOPNOTSUPP);
275}
276
277static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
278{
279 return;
280}
281
282static inline struct page *fscrypt_encrypt_page(const struct inode *inode,
283 struct page *page,
284 unsigned int len,
285 unsigned int offs,
286 u64 lblk_num, gfp_t gfp_flags)
287{
288 return ERR_PTR(-EOPNOTSUPP);
289}
290
291static inline int fscrypt_decrypt_page(const struct inode *inode,
292 struct page *page,
293 unsigned int len, unsigned int offs,
294 u64 lblk_num)
295{
296 return -EOPNOTSUPP;
297}
298
299static inline struct page *fscrypt_control_page(struct page *page)
300{
301 WARN_ON_ONCE(1);
302 return ERR_PTR(-EINVAL);
303}
304
305static inline void fscrypt_restore_control_page(struct page *page)
306{
307 return;
308}
309
310/* policy.c */
311static inline int fscrypt_ioctl_set_policy(struct file *filp,
312 const void __user *arg)
313{
314 return -EOPNOTSUPP;
315}
316
317static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
318{
319 return -EOPNOTSUPP;
320}
321
322static inline int fscrypt_has_permitted_context(struct inode *parent,
323 struct inode *child)
324{
325 return 0;
326}
327
328static inline int fscrypt_inherit_context(struct inode *parent,
329 struct inode *child,
330 void *fs_data, bool preload)
331{
332 return -EOPNOTSUPP;
333}
334
335/* keyinfo.c */
336static inline int fscrypt_get_encryption_info(struct inode *inode)
337{
338 return -EOPNOTSUPP;
339}
340
341static inline void fscrypt_put_encryption_info(struct inode *inode)
342{
343 return;
344}
345
Eric Biggersac8894a2019-04-10 13:21:15 -0700346static inline void fscrypt_free_inode(struct inode *inode)
347{
348}
349
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530350 /* fname.c */
351static inline int fscrypt_setup_filename(struct inode *dir,
352 const struct qstr *iname,
353 int lookup, struct fscrypt_name *fname)
354{
355 if (IS_ENCRYPTED(dir))
356 return -EOPNOTSUPP;
357
Eric Biggers3a3ac012019-03-20 11:39:13 -0700358 memset(fname, 0, sizeof(*fname));
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530359 fname->usr_fname = iname;
360 fname->disk_name.name = (unsigned char *)iname->name;
361 fname->disk_name.len = iname->len;
362 return 0;
363}
364
365static inline void fscrypt_free_filename(struct fscrypt_name *fname)
366{
367 return;
368}
369
370static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
371 u32 max_encrypted_len,
372 struct fscrypt_str *crypto_str)
373{
374 return -EOPNOTSUPP;
375}
376
377static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
378{
379 return;
380}
381
382static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
383 u32 hash, u32 minor_hash,
384 const struct fscrypt_str *iname,
385 struct fscrypt_str *oname)
386{
387 return -EOPNOTSUPP;
388}
389
390static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
391 const u8 *de_name, u32 de_name_len)
392{
393 /* Encryption support disabled; use standard comparison */
394 if (de_name_len != fname->disk_name.len)
395 return false;
396 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
397}
398
399/* bio.c */
400static inline void fscrypt_decrypt_bio(struct bio *bio)
401{
402}
403
404static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
405 struct bio *bio)
406{
407}
408
409static inline void fscrypt_pullback_bio_page(struct page **page, bool restore)
410{
411 return;
412}
413
414static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
415 sector_t pblk, unsigned int len)
416{
417 return -EOPNOTSUPP;
418}
419
420/* hooks.c */
421
422static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
423{
424 if (IS_ENCRYPTED(inode))
425 return -EOPNOTSUPP;
426 return 0;
427}
428
Eric Biggersef5b1542019-03-20 11:39:10 -0700429static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
430 struct dentry *dentry)
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530431{
432 return -EOPNOTSUPP;
433}
434
435static inline int __fscrypt_prepare_rename(struct inode *old_dir,
436 struct dentry *old_dentry,
437 struct inode *new_dir,
438 struct dentry *new_dentry,
439 unsigned int flags)
440{
441 return -EOPNOTSUPP;
442}
443
444static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggers3a3ac012019-03-20 11:39:13 -0700445 struct dentry *dentry,
446 struct fscrypt_name *fname)
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530447{
448 return -EOPNOTSUPP;
449}
450
451static inline int __fscrypt_prepare_symlink(struct inode *dir,
452 unsigned int len,
453 unsigned int max_len,
454 struct fscrypt_str *disk_link)
455{
456 return -EOPNOTSUPP;
457}
458
459
460static inline int __fscrypt_encrypt_symlink(struct inode *inode,
461 const char *target,
462 unsigned int len,
463 struct fscrypt_str *disk_link)
464{
465 return -EOPNOTSUPP;
466}
467
468static inline const char *fscrypt_get_symlink(struct inode *inode,
469 const void *caddr,
470 unsigned int max_size,
471 struct delayed_call *done)
472{
473 return ERR_PTR(-EOPNOTSUPP);
474}
475#endif /* !CONFIG_FS_ENCRYPTION */
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700476
477/**
478 * fscrypt_require_key - require an inode's encryption key
479 * @inode: the inode we need the key for
480 *
481 * If the inode is encrypted, set up its encryption key if not already done.
482 * Then require that the key be present and return -ENOKEY otherwise.
483 *
484 * No locks are needed, and the key will live as long as the struct inode --- so
485 * it won't go away from under you.
486 *
487 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
488 * if a problem occurred while setting up the encryption key.
489 */
490static inline int fscrypt_require_key(struct inode *inode)
491{
492 if (IS_ENCRYPTED(inode)) {
493 int err = fscrypt_get_encryption_info(inode);
494
495 if (err)
496 return err;
497 if (!fscrypt_has_encryption_key(inode))
498 return -ENOKEY;
499 }
500 return 0;
501}
502
503/**
504 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
505 * @old_dentry: an existing dentry for the inode being linked
506 * @dir: the target directory
507 * @dentry: negative dentry for the target filename
508 *
509 * A new link can only be added to an encrypted directory if the directory's
510 * encryption key is available --- since otherwise we'd have no way to encrypt
511 * the filename. Therefore, we first set up the directory's encryption key (if
512 * not already done) and return an error if it's unavailable.
513 *
514 * We also verify that the link will not violate the constraint that all files
515 * in an encrypted directory tree use the same encryption policy.
516 *
517 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggers15e483d2019-01-22 16:20:21 -0800518 * -EXDEV if the link would result in an inconsistent encryption policy, or
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700519 * another -errno code.
520 */
521static inline int fscrypt_prepare_link(struct dentry *old_dentry,
522 struct inode *dir,
523 struct dentry *dentry)
524{
525 if (IS_ENCRYPTED(dir))
Eric Biggersef5b1542019-03-20 11:39:10 -0700526 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700527 return 0;
528}
529
530/**
531 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
532 * @old_dir: source directory
533 * @old_dentry: dentry for source file
534 * @new_dir: target directory
535 * @new_dentry: dentry for target location (may be negative unless exchanging)
536 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
537 *
538 * Prepare for ->rename() where the source and/or target directories may be
539 * encrypted. A new link can only be added to an encrypted directory if the
540 * directory's encryption key is available --- since otherwise we'd have no way
541 * to encrypt the filename. A rename to an existing name, on the other hand,
542 * *is* cryptographically possible without the key. However, we take the more
543 * conservative approach and just forbid all no-key renames.
544 *
545 * We also verify that the rename will not violate the constraint that all files
546 * in an encrypted directory tree use the same encryption policy.
547 *
Eric Biggers15e483d2019-01-22 16:20:21 -0800548 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700549 * rename would cause inconsistent encryption policies, or another -errno code.
550 */
551static inline int fscrypt_prepare_rename(struct inode *old_dir,
552 struct dentry *old_dentry,
553 struct inode *new_dir,
554 struct dentry *new_dentry,
555 unsigned int flags)
556{
557 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
558 return __fscrypt_prepare_rename(old_dir, old_dentry,
559 new_dir, new_dentry, flags);
560 return 0;
561}
562
563/**
564 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
565 * @dir: directory being searched
566 * @dentry: filename being looked up
Eric Biggers3a3ac012019-03-20 11:39:13 -0700567 * @fname: (output) the name to use to search the on-disk directory
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700568 *
Eric Biggers3a3ac012019-03-20 11:39:13 -0700569 * Prepare for ->lookup() in a directory which may be encrypted by determining
570 * the name that will actually be used to search the directory on-disk. Lookups
571 * can be done with or without the directory's encryption key; without the key,
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700572 * filenames are presented in encrypted form. Therefore, we'll try to set up
573 * the directory's encryption key, but even without it the lookup can continue.
574 *
Eric Biggers7c6a5692019-03-20 11:39:09 -0700575 * This also installs a custom ->d_revalidate() method which will invalidate the
576 * dentry if it was created without the key and the key is later added.
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700577 *
Eric Biggers3a3ac012019-03-20 11:39:13 -0700578 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
579 * correctly formed encoded ciphertext name, so a negative dentry should be
580 * created; or another -errno code.
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700581 */
582static inline int fscrypt_prepare_lookup(struct inode *dir,
583 struct dentry *dentry,
Eric Biggers3a3ac012019-03-20 11:39:13 -0700584 struct fscrypt_name *fname)
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700585{
586 if (IS_ENCRYPTED(dir))
Eric Biggers3a3ac012019-03-20 11:39:13 -0700587 return __fscrypt_prepare_lookup(dir, dentry, fname);
588
589 memset(fname, 0, sizeof(*fname));
590 fname->usr_fname = &dentry->d_name;
591 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
592 fname->disk_name.len = dentry->d_name.len;
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700593 return 0;
594}
595
596/**
597 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
598 * @dentry: dentry through which the inode is being changed
599 * @attr: attributes to change
600 *
601 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
602 * most attribute changes are allowed even without the encryption key. However,
603 * without the encryption key we do have to forbid truncates. This is needed
604 * because the size being truncated to may not be a multiple of the filesystem
605 * block size, and in that case we'd have to decrypt the final block, zero the
606 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
607 * filesystem block boundary, but it's simpler to just forbid all truncates ---
608 * and we already forbid all other contents modifications without the key.)
609 *
610 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
611 * if a problem occurred while setting up the encryption key.
612 */
613static inline int fscrypt_prepare_setattr(struct dentry *dentry,
614 struct iattr *attr)
615{
616 if (attr->ia_valid & ATTR_SIZE)
617 return fscrypt_require_key(d_inode(dentry));
618 return 0;
619}
620
Jaegeuk Kimd9197652018-01-05 10:44:52 -0800621/**
622 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
623 * @dir: directory in which the symlink is being created
624 * @target: plaintext symlink target
625 * @len: length of @target excluding null terminator
626 * @max_len: space the filesystem has available to store the symlink target
627 * @disk_link: (out) the on-disk symlink target being prepared
628 *
629 * This function computes the size the symlink target will require on-disk,
630 * stores it in @disk_link->len, and validates it against @max_len. An
631 * encrypted symlink may be longer than the original.
632 *
633 * Additionally, @disk_link->name is set to @target if the symlink will be
634 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
635 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
636 * on-disk target later. (The reason for the two-step process is that some
637 * filesystems need to know the size of the symlink target before creating the
638 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
639 *
640 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
641 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
642 * occurred while setting up the encryption key.
643 */
644static inline int fscrypt_prepare_symlink(struct inode *dir,
645 const char *target,
646 unsigned int len,
647 unsigned int max_len,
648 struct fscrypt_str *disk_link)
649{
650 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
651 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
652
653 disk_link->name = (unsigned char *)target;
654 disk_link->len = len + 1;
655 if (disk_link->len > max_len)
656 return -ENAMETOOLONG;
657 return 0;
658}
659
660/**
661 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
662 * @inode: symlink inode
663 * @target: plaintext symlink target
664 * @len: length of @target excluding null terminator
665 * @disk_link: (in/out) the on-disk symlink target being prepared
666 *
667 * If the symlink target needs to be encrypted, then this function encrypts it
668 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
669 * previously to compute @disk_link->len. If the filesystem did not allocate a
670 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
671 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
672 *
673 * Return: 0 on success, -errno on failure
674 */
675static inline int fscrypt_encrypt_symlink(struct inode *inode,
676 const char *target,
677 unsigned int len,
678 struct fscrypt_str *disk_link)
679{
680 if (IS_ENCRYPTED(inode))
681 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
682 return 0;
683}
684
Jaegeuk Kim5190c812018-04-22 16:05:02 -0700685/* fscrypt_ice.c */
686#ifdef CONFIG_PFK
687extern int fscrypt_using_hardware_encryption(const struct inode *inode);
688extern void fscrypt_set_ice_dun(const struct inode *inode,
689 struct bio *bio, u64 dun);
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700690extern bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted,
691 int bi_crypt_skip);
692extern void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip);
Jaegeuk Kim5190c812018-04-22 16:05:02 -0700693#else
694static inline int fscrypt_using_hardware_encryption(const struct inode *inode)
695{
696 return 0;
697}
698
699static inline void fscrypt_set_ice_dun(const struct inode *inode,
700 struct bio *bio, u64 dun)
701{
702 return;
703}
704
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700705static inline void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip)
706{
707}
708
Jaegeuk Kim5190c812018-04-22 16:05:02 -0700709static inline bool fscrypt_mergeable_bio(struct bio *bio,
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700710 u64 dun, bool bio_encrypted, int bi_crypt_skip)
Jaegeuk Kim5190c812018-04-22 16:05:02 -0700711{
712 return true;
713}
714#endif
715
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700716#endif /* _LINUX_FSCRYPT_H */