blob: 46bf66cf76ef884ea59af49b506e9202a4bce1ce [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 Biggers7af0ab02019-08-04 19:35:43 -070019#include <uapi/linux/fscrypt.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080020
21#define FS_CRYPTO_BLOCK_SIZE 16
22
Eric Biggers542060c2018-01-05 10:44:55 -080023struct fscrypt_ctx;
Eric Biggers46f47e42017-01-24 10:58:06 -080024struct fscrypt_info;
25
Eric Biggers46f47e42017-01-24 10:58:06 -080026struct fscrypt_str {
27 unsigned char *name;
28 u32 len;
29};
30
31struct fscrypt_name {
32 const struct qstr *usr_fname;
33 struct fscrypt_str disk_name;
34 u32 hash;
35 u32 minor_hash;
36 struct fscrypt_str crypto_buf;
Eric Biggersb01531d2019-03-20 11:39:13 -070037 bool is_ciphertext_name;
Eric Biggers46f47e42017-01-24 10:58:06 -080038};
39
40#define FSTR_INIT(n, l) { .name = n, .len = l }
41#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
42#define fname_name(p) ((p)->disk_name.name)
43#define fname_len(p) ((p)->disk_name.len)
44
Tahsin Erdoganaf652072017-07-06 00:01:59 -040045/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
46#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
47
Chandan Rajendra643fa962018-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
Eric Biggers2a415a02019-05-20 09:29:40 -070067/* Decryption work */
Chandan Rajendra643fa962018-12-12 15:20:12 +053068struct fscrypt_ctx {
69 union {
70 struct {
Chandan Rajendra643fa962018-12-12 15:20:12 +053071 struct bio *bio;
72 struct work_struct work;
Eric Biggers2a415a02019-05-20 09:29:40 -070073 };
Chandan Rajendra643fa962018-12-12 15:20:12 +053074 struct list_head free_list; /* Free list */
75 };
76 u8 flags; /* Flags */
77};
78
79static inline bool fscrypt_has_encryption_key(const struct inode *inode)
80{
Eric Biggerse37a7842019-04-11 14:32:15 -070081 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
82 return READ_ONCE(inode->i_crypt_info) != NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +053083}
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
Eric Biggers0bf3d5c12019-03-20 11:39:11 -070091/*
92 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
93 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
94 * must be cleared. Note that we don't have to support arbitrary moves of this
95 * flag because fscrypt doesn't allow encrypted aliases to be the source or
96 * target of a rename().
97 */
98static inline void fscrypt_handle_d_move(struct dentry *dentry)
99{
100 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
101}
102
Chandan Rajendra643fa962018-12-12 15:20:12 +0530103/* crypto.c */
104extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Eric Biggerscd0265f2019-03-18 10:23:33 -0700105extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530106extern void fscrypt_release_ctx(struct fscrypt_ctx *);
Eric Biggers53bc1d852019-05-20 09:29:44 -0700107
108extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
109 unsigned int len,
110 unsigned int offs,
111 gfp_t gfp_flags);
Eric Biggers03569f22019-05-20 09:29:43 -0700112extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
113 struct page *page, unsigned int len,
114 unsigned int offs, u64 lblk_num,
115 gfp_t gfp_flags);
Eric Biggersaa8bc1a2019-05-20 09:29:47 -0700116
117extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
118 unsigned int offs);
Eric Biggers41adbcb2019-05-20 09:29:46 -0700119extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
120 struct page *page, unsigned int len,
121 unsigned int offs, u64 lblk_num);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530122
Eric Biggersd2d07272019-05-20 09:29:39 -0700123static inline bool fscrypt_is_bounce_page(struct page *page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530124{
Eric Biggersd2d07272019-05-20 09:29:39 -0700125 return page->mapping == NULL;
Chandan Rajendra643fa962018-12-12 15:20:12 +0530126}
127
Eric Biggersd2d07272019-05-20 09:29:39 -0700128static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
129{
130 return (struct page *)page_private(bounce_page);
131}
132
133extern void fscrypt_free_bounce_page(struct page *bounce_page);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530134
135/* policy.c */
136extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
137extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
138extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
139extern int fscrypt_inherit_context(struct inode *, struct inode *,
140 void *, bool);
Eric Biggers22d94f42019-08-04 19:35:46 -0700141/* keyring.c */
142extern void fscrypt_sb_free(struct super_block *sb);
143extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
144
Eric Biggersfeed8252019-08-04 19:35:45 -0700145/* keysetup.c */
Chandan Rajendra643fa962018-12-12 15:20:12 +0530146extern int fscrypt_get_encryption_info(struct inode *);
147extern void fscrypt_put_encryption_info(struct inode *);
Eric Biggers2c58d542019-04-10 13:21:15 -0700148extern void fscrypt_free_inode(struct inode *);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530149
150/* fname.c */
151extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
152 int lookup, struct fscrypt_name *);
153
154static inline void fscrypt_free_filename(struct fscrypt_name *fname)
155{
156 kfree(fname->crypto_buf.name);
157}
158
159extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
160 struct fscrypt_str *);
161extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
162extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
163 const struct fscrypt_str *, struct fscrypt_str *);
164
165#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
166
167/* Extracts the second-to-last ciphertext block; see explanation below */
168#define FSCRYPT_FNAME_DIGEST(name, len) \
169 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
170 FS_CRYPTO_BLOCK_SIZE))
171
172#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
173
174/**
175 * fscrypt_digested_name - alternate identifier for an on-disk filename
176 *
177 * When userspace lists an encrypted directory without access to the key,
178 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
179 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
180 * full ciphertext (base64-encoded). This is necessary to allow supporting
181 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
182 *
183 * To make it possible for filesystems to still find the correct directory entry
184 * despite not knowing the full on-disk name, we encode any filesystem-specific
185 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
186 * followed by the second-to-last ciphertext block of the filename. Due to the
187 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
188 * depends on the full plaintext. (Note that ciphertext stealing causes the
189 * last two blocks to appear "flipped".) This makes accidental collisions very
190 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
191 * share the same filesystem-specific hashes.
192 *
193 * However, this scheme isn't immune to intentional collisions, which can be
194 * created by anyone able to create arbitrary plaintext filenames and view them
195 * without the key. Making the "digest" be a real cryptographic hash like
196 * SHA-256 over the full ciphertext would prevent this, although it would be
197 * less efficient and harder to implement, especially since the filesystem would
198 * need to calculate it for each directory entry examined during a search.
199 */
200struct fscrypt_digested_name {
201 u32 hash;
202 u32 minor_hash;
203 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
204};
205
206/**
207 * fscrypt_match_name() - test whether the given name matches a directory entry
208 * @fname: the name being searched for
209 * @de_name: the name from the directory entry
210 * @de_name_len: the length of @de_name in bytes
211 *
212 * Normally @fname->disk_name will be set, and in that case we simply compare
213 * that to the name stored in the directory entry. The only exception is that
214 * if we don't have the key for an encrypted directory and a filename in it is
215 * very long, then we won't have the full disk_name and we'll instead need to
216 * match against the fscrypt_digested_name.
217 *
218 * Return: %true if the name matches, otherwise %false.
219 */
220static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
221 const u8 *de_name, u32 de_name_len)
222{
223 if (unlikely(!fname->disk_name.name)) {
224 const struct fscrypt_digested_name *n =
225 (const void *)fname->crypto_buf.name;
226 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
227 return false;
228 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
229 return false;
230 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
231 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
232 }
233
234 if (de_name_len != fname->disk_name.len)
235 return false;
236 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
237}
238
239/* bio.c */
240extern void fscrypt_decrypt_bio(struct bio *);
241extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
242 struct bio *bio);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530243extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
244 unsigned int);
245
246/* hooks.c */
247extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers968dd6d2019-03-20 11:39:10 -0700248extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
249 struct dentry *dentry);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530250extern int __fscrypt_prepare_rename(struct inode *old_dir,
251 struct dentry *old_dentry,
252 struct inode *new_dir,
253 struct dentry *new_dentry,
254 unsigned int flags);
Eric Biggersb01531d2019-03-20 11:39:13 -0700255extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
256 struct fscrypt_name *fname);
Chandan Rajendra643fa962018-12-12 15:20:12 +0530257extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
258 unsigned int max_len,
259 struct fscrypt_str *disk_link);
260extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
261 unsigned int len,
262 struct fscrypt_str *disk_link);
263extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
264 unsigned int max_size,
265 struct delayed_call *done);
Sascha Hauereea2c052019-03-26 08:52:31 +0100266static inline void fscrypt_set_ops(struct super_block *sb,
267 const struct fscrypt_operations *s_cop)
268{
269 sb->s_cop = s_cop;
270}
Chandan Rajendra643fa962018-12-12 15:20:12 +0530271#else /* !CONFIG_FS_ENCRYPTION */
272
273static inline bool fscrypt_has_encryption_key(const struct inode *inode)
274{
275 return false;
276}
277
278static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
279{
280 return false;
281}
282
Eric Biggers0bf3d5c12019-03-20 11:39:11 -0700283static inline void fscrypt_handle_d_move(struct dentry *dentry)
284{
285}
286
Chandan Rajendra643fa962018-12-12 15:20:12 +0530287/* crypto.c */
288static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
289{
290}
291
Eric Biggerscd0265f2019-03-18 10:23:33 -0700292static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530293{
294 return ERR_PTR(-EOPNOTSUPP);
295}
296
297static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
298{
299 return;
300}
301
Eric Biggers53bc1d852019-05-20 09:29:44 -0700302static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
303 unsigned int len,
304 unsigned int offs,
305 gfp_t gfp_flags)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530306{
307 return ERR_PTR(-EOPNOTSUPP);
308}
309
Eric Biggers03569f22019-05-20 09:29:43 -0700310static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
311 struct page *page,
312 unsigned int len,
313 unsigned int offs, u64 lblk_num,
314 gfp_t gfp_flags)
315{
316 return -EOPNOTSUPP;
317}
318
Eric Biggersaa8bc1a2019-05-20 09:29:47 -0700319static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
320 unsigned int len,
321 unsigned int offs)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530322{
323 return -EOPNOTSUPP;
324}
325
Eric Biggers41adbcb2019-05-20 09:29:46 -0700326static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
327 struct page *page,
328 unsigned int len,
329 unsigned int offs, u64 lblk_num)
330{
331 return -EOPNOTSUPP;
332}
333
Eric Biggersd2d07272019-05-20 09:29:39 -0700334static inline bool fscrypt_is_bounce_page(struct page *page)
335{
336 return false;
337}
338
339static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530340{
341 WARN_ON_ONCE(1);
342 return ERR_PTR(-EINVAL);
343}
344
Eric Biggersd2d07272019-05-20 09:29:39 -0700345static inline void fscrypt_free_bounce_page(struct page *bounce_page)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530346{
Chandan Rajendra643fa962018-12-12 15:20:12 +0530347}
348
349/* policy.c */
350static inline int fscrypt_ioctl_set_policy(struct file *filp,
351 const void __user *arg)
352{
353 return -EOPNOTSUPP;
354}
355
356static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
357{
358 return -EOPNOTSUPP;
359}
360
361static inline int fscrypt_has_permitted_context(struct inode *parent,
362 struct inode *child)
363{
364 return 0;
365}
366
367static inline int fscrypt_inherit_context(struct inode *parent,
368 struct inode *child,
369 void *fs_data, bool preload)
370{
371 return -EOPNOTSUPP;
372}
373
Eric Biggers22d94f42019-08-04 19:35:46 -0700374/* keyring.c */
375static inline void fscrypt_sb_free(struct super_block *sb)
376{
377}
378
379static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
380{
381 return -EOPNOTSUPP;
382}
383
Eric Biggersfeed8252019-08-04 19:35:45 -0700384/* keysetup.c */
Chandan Rajendra643fa962018-12-12 15:20:12 +0530385static inline int fscrypt_get_encryption_info(struct inode *inode)
386{
387 return -EOPNOTSUPP;
388}
389
390static inline void fscrypt_put_encryption_info(struct inode *inode)
391{
392 return;
393}
394
Eric Biggers2c58d542019-04-10 13:21:15 -0700395static inline void fscrypt_free_inode(struct inode *inode)
396{
397}
398
Chandan Rajendra643fa962018-12-12 15:20:12 +0530399 /* fname.c */
400static inline int fscrypt_setup_filename(struct inode *dir,
401 const struct qstr *iname,
402 int lookup, struct fscrypt_name *fname)
403{
404 if (IS_ENCRYPTED(dir))
405 return -EOPNOTSUPP;
406
Eric Biggersb01531d2019-03-20 11:39:13 -0700407 memset(fname, 0, sizeof(*fname));
Chandan Rajendra643fa962018-12-12 15:20:12 +0530408 fname->usr_fname = iname;
409 fname->disk_name.name = (unsigned char *)iname->name;
410 fname->disk_name.len = iname->len;
411 return 0;
412}
413
414static inline void fscrypt_free_filename(struct fscrypt_name *fname)
415{
416 return;
417}
418
419static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
420 u32 max_encrypted_len,
421 struct fscrypt_str *crypto_str)
422{
423 return -EOPNOTSUPP;
424}
425
426static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
427{
428 return;
429}
430
431static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
432 u32 hash, u32 minor_hash,
433 const struct fscrypt_str *iname,
434 struct fscrypt_str *oname)
435{
436 return -EOPNOTSUPP;
437}
438
439static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
440 const u8 *de_name, u32 de_name_len)
441{
442 /* Encryption support disabled; use standard comparison */
443 if (de_name_len != fname->disk_name.len)
444 return false;
445 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
446}
447
448/* bio.c */
449static inline void fscrypt_decrypt_bio(struct bio *bio)
450{
451}
452
453static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
454 struct bio *bio)
455{
456}
457
Chandan Rajendra643fa962018-12-12 15:20:12 +0530458static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
459 sector_t pblk, unsigned int len)
460{
461 return -EOPNOTSUPP;
462}
463
464/* hooks.c */
465
466static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
467{
468 if (IS_ENCRYPTED(inode))
469 return -EOPNOTSUPP;
470 return 0;
471}
472
Eric Biggers968dd6d2019-03-20 11:39:10 -0700473static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
474 struct dentry *dentry)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530475{
476 return -EOPNOTSUPP;
477}
478
479static inline int __fscrypt_prepare_rename(struct inode *old_dir,
480 struct dentry *old_dentry,
481 struct inode *new_dir,
482 struct dentry *new_dentry,
483 unsigned int flags)
484{
485 return -EOPNOTSUPP;
486}
487
488static inline int __fscrypt_prepare_lookup(struct inode *dir,
Eric Biggersb01531d2019-03-20 11:39:13 -0700489 struct dentry *dentry,
490 struct fscrypt_name *fname)
Chandan Rajendra643fa962018-12-12 15:20:12 +0530491{
492 return -EOPNOTSUPP;
493}
494
495static inline int __fscrypt_prepare_symlink(struct inode *dir,
496 unsigned int len,
497 unsigned int max_len,
498 struct fscrypt_str *disk_link)
499{
500 return -EOPNOTSUPP;
501}
502
503
504static inline int __fscrypt_encrypt_symlink(struct inode *inode,
505 const char *target,
506 unsigned int len,
507 struct fscrypt_str *disk_link)
508{
509 return -EOPNOTSUPP;
510}
511
512static inline const char *fscrypt_get_symlink(struct inode *inode,
513 const void *caddr,
514 unsigned int max_size,
515 struct delayed_call *done)
516{
517 return ERR_PTR(-EOPNOTSUPP);
518}
Sascha Hauereea2c052019-03-26 08:52:31 +0100519
520static inline void fscrypt_set_ops(struct super_block *sb,
521 const struct fscrypt_operations *s_cop)
522{
523}
524
Chandan Rajendra643fa962018-12-12 15:20:12 +0530525#endif /* !CONFIG_FS_ENCRYPTION */
Dave Chinner734f0d22017-10-09 12:15:34 -0700526
Eric Biggersd293c3e2017-10-09 12:15:39 -0700527/**
528 * fscrypt_require_key - require an inode's encryption key
529 * @inode: the inode we need the key for
530 *
531 * If the inode is encrypted, set up its encryption key if not already done.
532 * Then require that the key be present and return -ENOKEY otherwise.
533 *
534 * No locks are needed, and the key will live as long as the struct inode --- so
535 * it won't go away from under you.
536 *
537 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
538 * if a problem occurred while setting up the encryption key.
539 */
540static inline int fscrypt_require_key(struct inode *inode)
541{
542 if (IS_ENCRYPTED(inode)) {
543 int err = fscrypt_get_encryption_info(inode);
544
545 if (err)
546 return err;
547 if (!fscrypt_has_encryption_key(inode))
548 return -ENOKEY;
549 }
550 return 0;
551}
Dave Chinner734f0d22017-10-09 12:15:34 -0700552
Eric Biggers0ea87a92017-10-09 12:15:41 -0700553/**
554 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
555 * @old_dentry: an existing dentry for the inode being linked
556 * @dir: the target directory
557 * @dentry: negative dentry for the target filename
558 *
559 * A new link can only be added to an encrypted directory if the directory's
560 * encryption key is available --- since otherwise we'd have no way to encrypt
561 * the filename. Therefore, we first set up the directory's encryption key (if
562 * not already done) and return an error if it's unavailable.
563 *
564 * We also verify that the link will not violate the constraint that all files
565 * in an encrypted directory tree use the same encryption policy.
566 *
567 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
Eric Biggersf5e55e72019-01-22 16:20:21 -0800568 * -EXDEV if the link would result in an inconsistent encryption policy, or
Eric Biggers0ea87a92017-10-09 12:15:41 -0700569 * another -errno code.
570 */
571static inline int fscrypt_prepare_link(struct dentry *old_dentry,
572 struct inode *dir,
573 struct dentry *dentry)
574{
575 if (IS_ENCRYPTED(dir))
Eric Biggers968dd6d2019-03-20 11:39:10 -0700576 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700577 return 0;
578}
579
Eric Biggers94b26f32017-10-09 12:15:42 -0700580/**
581 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
582 * @old_dir: source directory
583 * @old_dentry: dentry for source file
584 * @new_dir: target directory
585 * @new_dentry: dentry for target location (may be negative unless exchanging)
586 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
587 *
588 * Prepare for ->rename() where the source and/or target directories may be
589 * encrypted. A new link can only be added to an encrypted directory if the
590 * directory's encryption key is available --- since otherwise we'd have no way
591 * to encrypt the filename. A rename to an existing name, on the other hand,
592 * *is* cryptographically possible without the key. However, we take the more
593 * conservative approach and just forbid all no-key renames.
594 *
595 * We also verify that the rename will not violate the constraint that all files
596 * in an encrypted directory tree use the same encryption policy.
597 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800598 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
Eric Biggers94b26f32017-10-09 12:15:42 -0700599 * rename would cause inconsistent encryption policies, or another -errno code.
600 */
601static inline int fscrypt_prepare_rename(struct inode *old_dir,
602 struct dentry *old_dentry,
603 struct inode *new_dir,
604 struct dentry *new_dentry,
605 unsigned int flags)
606{
607 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
608 return __fscrypt_prepare_rename(old_dir, old_dentry,
609 new_dir, new_dentry, flags);
610 return 0;
611}
612
Eric Biggers32c3cf02017-10-09 12:15:43 -0700613/**
614 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
615 * @dir: directory being searched
616 * @dentry: filename being looked up
Eric Biggersb01531d2019-03-20 11:39:13 -0700617 * @fname: (output) the name to use to search the on-disk directory
Eric Biggers32c3cf02017-10-09 12:15:43 -0700618 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700619 * Prepare for ->lookup() in a directory which may be encrypted by determining
620 * the name that will actually be used to search the directory on-disk. Lookups
621 * can be done with or without the directory's encryption key; without the key,
Eric Biggers32c3cf02017-10-09 12:15:43 -0700622 * filenames are presented in encrypted form. Therefore, we'll try to set up
623 * the directory's encryption key, but even without it the lookup can continue.
624 *
Eric Biggers6cc24862019-03-20 11:39:09 -0700625 * This also installs a custom ->d_revalidate() method which will invalidate the
626 * dentry if it was created without the key and the key is later added.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700627 *
Eric Biggersb01531d2019-03-20 11:39:13 -0700628 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
629 * correctly formed encoded ciphertext name, so a negative dentry should be
630 * created; or another -errno code.
Eric Biggers32c3cf02017-10-09 12:15:43 -0700631 */
632static inline int fscrypt_prepare_lookup(struct inode *dir,
633 struct dentry *dentry,
Eric Biggersb01531d2019-03-20 11:39:13 -0700634 struct fscrypt_name *fname)
Eric Biggers32c3cf02017-10-09 12:15:43 -0700635{
636 if (IS_ENCRYPTED(dir))
Eric Biggersb01531d2019-03-20 11:39:13 -0700637 return __fscrypt_prepare_lookup(dir, dentry, fname);
638
639 memset(fname, 0, sizeof(*fname));
640 fname->usr_fname = &dentry->d_name;
641 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
642 fname->disk_name.len = dentry->d_name.len;
Eric Biggers32c3cf02017-10-09 12:15:43 -0700643 return 0;
644}
645
Eric Biggers815dac32017-10-09 12:15:44 -0700646/**
647 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
648 * @dentry: dentry through which the inode is being changed
649 * @attr: attributes to change
650 *
651 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
652 * most attribute changes are allowed even without the encryption key. However,
653 * without the encryption key we do have to forbid truncates. This is needed
654 * because the size being truncated to may not be a multiple of the filesystem
655 * block size, and in that case we'd have to decrypt the final block, zero the
656 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
657 * filesystem block boundary, but it's simpler to just forbid all truncates ---
658 * and we already forbid all other contents modifications without the key.)
659 *
660 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
661 * if a problem occurred while setting up the encryption key.
662 */
663static inline int fscrypt_prepare_setattr(struct dentry *dentry,
664 struct iattr *attr)
665{
666 if (attr->ia_valid & ATTR_SIZE)
667 return fscrypt_require_key(d_inode(dentry));
668 return 0;
669}
670
Eric Biggers76e81d62018-01-05 10:45:01 -0800671/**
672 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
673 * @dir: directory in which the symlink is being created
674 * @target: plaintext symlink target
675 * @len: length of @target excluding null terminator
676 * @max_len: space the filesystem has available to store the symlink target
677 * @disk_link: (out) the on-disk symlink target being prepared
678 *
679 * This function computes the size the symlink target will require on-disk,
680 * stores it in @disk_link->len, and validates it against @max_len. An
681 * encrypted symlink may be longer than the original.
682 *
683 * Additionally, @disk_link->name is set to @target if the symlink will be
684 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
685 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
686 * on-disk target later. (The reason for the two-step process is that some
687 * filesystems need to know the size of the symlink target before creating the
688 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
689 *
690 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
691 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
692 * occurred while setting up the encryption key.
693 */
694static inline int fscrypt_prepare_symlink(struct inode *dir,
695 const char *target,
696 unsigned int len,
697 unsigned int max_len,
698 struct fscrypt_str *disk_link)
699{
700 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
701 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
702
703 disk_link->name = (unsigned char *)target;
704 disk_link->len = len + 1;
705 if (disk_link->len > max_len)
706 return -ENAMETOOLONG;
707 return 0;
708}
709
710/**
711 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
712 * @inode: symlink inode
713 * @target: plaintext symlink target
714 * @len: length of @target excluding null terminator
715 * @disk_link: (in/out) the on-disk symlink target being prepared
716 *
717 * If the symlink target needs to be encrypted, then this function encrypts it
718 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
719 * previously to compute @disk_link->len. If the filesystem did not allocate a
720 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
721 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
722 *
723 * Return: 0 on success, -errno on failure
724 */
725static inline int fscrypt_encrypt_symlink(struct inode *inode,
726 const char *target,
727 unsigned int len,
728 struct fscrypt_str *disk_link)
729{
730 if (IS_ENCRYPTED(inode))
731 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
732 return 0;
733}
734
Eric Biggersd2d07272019-05-20 09:29:39 -0700735/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
736static inline void fscrypt_finalize_bounce_page(struct page **pagep)
737{
738 struct page *page = *pagep;
739
740 if (fscrypt_is_bounce_page(page)) {
741 *pagep = fscrypt_pagecache_page(page);
742 fscrypt_free_bounce_page(page);
743 }
744}
745
Dave Chinner734f0d22017-10-09 12:15:34 -0700746#endif /* _LINUX_FSCRYPT_H */