blob: cf9e9fc02f0afe7348404ca769927d587a63ec64 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Eric Biggers46f47e42017-01-24 10:58:06 -08002/*
3 * fscrypt_supp.h
4 *
Dave Chinner734f0d22017-10-09 12:15:34 -07005 * Do not include this file directly. Use fscrypt.h instead!
Eric Biggers46f47e42017-01-24 10:58:06 -08006 */
Dave Chinner734f0d22017-10-09 12:15:34 -07007#ifndef _LINUX_FSCRYPT_H
8#error "Incorrect include of linux/fscrypt_supp.h!"
9#endif
Eric Biggers46f47e42017-01-24 10:58:06 -080010
11#ifndef _LINUX_FSCRYPT_SUPP_H
12#define _LINUX_FSCRYPT_SUPP_H
13
Eric Biggers46f47e42017-01-24 10:58:06 -080014/* crypto.c */
15extern struct kmem_cache *fscrypt_info_cachep;
16extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
17extern void fscrypt_release_ctx(struct fscrypt_ctx *);
18extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
19 unsigned int, unsigned int,
20 u64, gfp_t);
21extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
22 unsigned int, u64);
23extern void fscrypt_restore_control_page(struct page *);
24
25extern const struct dentry_operations fscrypt_d_ops;
26
27static inline void fscrypt_set_d_op(struct dentry *dentry)
28{
29 d_set_d_op(dentry, &fscrypt_d_ops);
30}
31
32static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
33{
34 spin_lock(&dentry->d_lock);
35 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
36 spin_unlock(&dentry->d_lock);
37}
38
39/* policy.c */
40extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
41extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
42extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
43extern int fscrypt_inherit_context(struct inode *, struct inode *,
44 void *, bool);
45/* keyinfo.c */
46extern int fscrypt_get_encryption_info(struct inode *);
47extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
48
49/* fname.c */
50extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
51 int lookup, struct fscrypt_name *);
Eric Biggers27e47a62017-05-22 18:14:06 -070052
53static inline void fscrypt_free_filename(struct fscrypt_name *fname)
54{
55 kfree(fname->crypto_buf.name);
56}
57
Eric Biggers46f47e42017-01-24 10:58:06 -080058extern u32 fscrypt_fname_encrypted_size(const struct inode *, u32);
59extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
60 struct fscrypt_str *);
61extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
62extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
63 const struct fscrypt_str *, struct fscrypt_str *);
64extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
65 struct fscrypt_str *);
66
Eric Biggers17159422017-04-24 10:00:10 -070067#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
68
69/* Extracts the second-to-last ciphertext block; see explanation below */
70#define FSCRYPT_FNAME_DIGEST(name, len) \
71 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
72 FS_CRYPTO_BLOCK_SIZE))
73
74#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
75
76/**
77 * fscrypt_digested_name - alternate identifier for an on-disk filename
78 *
79 * When userspace lists an encrypted directory without access to the key,
80 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
81 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
82 * full ciphertext (base64-encoded). This is necessary to allow supporting
83 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
84 *
85 * To make it possible for filesystems to still find the correct directory entry
86 * despite not knowing the full on-disk name, we encode any filesystem-specific
87 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
88 * followed by the second-to-last ciphertext block of the filename. Due to the
89 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
90 * depends on the full plaintext. (Note that ciphertext stealing causes the
Eric Biggers6f9d6962017-05-01 11:43:32 -070091 * last two blocks to appear "flipped".) This makes accidental collisions very
92 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
93 * share the same filesystem-specific hashes.
Eric Biggers17159422017-04-24 10:00:10 -070094 *
Eric Biggers6f9d6962017-05-01 11:43:32 -070095 * However, this scheme isn't immune to intentional collisions, which can be
96 * created by anyone able to create arbitrary plaintext filenames and view them
97 * without the key. Making the "digest" be a real cryptographic hash like
98 * SHA-256 over the full ciphertext would prevent this, although it would be
99 * less efficient and harder to implement, especially since the filesystem would
100 * need to calculate it for each directory entry examined during a search.
Eric Biggers17159422017-04-24 10:00:10 -0700101 */
102struct fscrypt_digested_name {
103 u32 hash;
104 u32 minor_hash;
105 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
106};
107
108/**
109 * fscrypt_match_name() - test whether the given name matches a directory entry
110 * @fname: the name being searched for
111 * @de_name: the name from the directory entry
112 * @de_name_len: the length of @de_name in bytes
113 *
114 * Normally @fname->disk_name will be set, and in that case we simply compare
115 * that to the name stored in the directory entry. The only exception is that
116 * if we don't have the key for an encrypted directory and a filename in it is
117 * very long, then we won't have the full disk_name and we'll instead need to
118 * match against the fscrypt_digested_name.
119 *
120 * Return: %true if the name matches, otherwise %false.
121 */
122static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
123 const u8 *de_name, u32 de_name_len)
124{
125 if (unlikely(!fname->disk_name.name)) {
126 const struct fscrypt_digested_name *n =
127 (const void *)fname->crypto_buf.name;
128 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
129 return false;
130 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
131 return false;
132 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
133 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
134 }
135
136 if (de_name_len != fname->disk_name.len)
137 return false;
138 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
139}
140
Eric Biggers46f47e42017-01-24 10:58:06 -0800141/* bio.c */
142extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
143extern void fscrypt_pullback_bio_page(struct page **, bool);
144extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
145 unsigned int);
146
Eric Biggersefcc7ae2017-10-09 12:15:40 -0700147/* hooks.c */
148extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggers0ea87a92017-10-09 12:15:41 -0700149extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir);
Eric Biggers94b26f32017-10-09 12:15:42 -0700150extern int __fscrypt_prepare_rename(struct inode *old_dir,
151 struct dentry *old_dentry,
152 struct inode *new_dir,
153 struct dentry *new_dentry,
154 unsigned int flags);
Eric Biggers32c3cf02017-10-09 12:15:43 -0700155extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
Eric Biggersefcc7ae2017-10-09 12:15:40 -0700156
Eric Biggers46f47e42017-01-24 10:58:06 -0800157#endif /* _LINUX_FSCRYPT_SUPP_H */