blob: 1ed79eeb24389b8ace1cdc2a52e666a2c40efc71 [file] [log] [blame]
Hyojun Kim63da4202017-10-06 17:10:08 -07001/*
2 * fscrypt_supp.h
3 *
Jaegeuk Kim8dec0742017-06-22 12:14:40 -07004 * Do not include this file directly. Use fscrypt.h instead!
Hyojun Kim63da4202017-10-06 17:10:08 -07005 */
Jaegeuk Kim8dec0742017-06-22 12:14:40 -07006#ifndef _LINUX_FSCRYPT_H
7#error "Incorrect include of linux/fscrypt_supp.h!"
8#endif
Hyojun Kim63da4202017-10-06 17:10:08 -07009
10#ifndef _LINUX_FSCRYPT_SUPP_H
11#define _LINUX_FSCRYPT_SUPP_H
12
Jaegeuk Kimd9197652018-01-05 10:44:52 -080013#include <linux/mm.h>
14#include <linux/slab.h>
15
16/*
17 * fscrypt superblock flags
18 */
19#define FS_CFLG_OWN_PAGES (1U << 1)
20
21/*
22 * crypto operations for filesystems
23 */
24struct fscrypt_operations {
25 unsigned int flags;
26 const char *key_prefix;
27 int (*get_context)(struct inode *, void *, size_t);
28 int (*set_context)(struct inode *, const void *, size_t, void *);
29 bool (*dummy_context)(struct inode *);
30 bool (*empty_dir)(struct inode *);
31 unsigned (*max_namelen)(struct inode *);
32};
33
34struct fscrypt_ctx {
35 union {
36 struct {
37 struct page *bounce_page; /* Ciphertext page */
38 struct page *control_page; /* Original page */
39 } w;
40 struct {
41 struct bio *bio;
42 struct work_struct work;
43 } r;
44 struct list_head free_list; /* Free list */
45 };
46 u8 flags; /* Flags */
47};
48
49static inline bool fscrypt_has_encryption_key(const struct inode *inode)
50{
51 return (inode->i_crypt_info != NULL);
52}
53
54static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
55{
56 return inode->i_sb->s_cop->dummy_context &&
57 inode->i_sb->s_cop->dummy_context(inode);
58}
59
Hyojun Kim63da4202017-10-06 17:10:08 -070060/* crypto.c */
Eric Biggers823fd562018-04-18 11:09:47 -070061extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Hyojun Kim63da4202017-10-06 17:10:08 -070062extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
63extern void fscrypt_release_ctx(struct fscrypt_ctx *);
64extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
65 unsigned int, unsigned int,
66 u64, gfp_t);
67extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
68 unsigned int, u64);
Jaegeuk Kimd9197652018-01-05 10:44:52 -080069
70static inline struct page *fscrypt_control_page(struct page *page)
71{
72 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
73}
74
Hyojun Kim63da4202017-10-06 17:10:08 -070075extern void fscrypt_restore_control_page(struct page *);
76
77extern const struct dentry_operations fscrypt_d_ops;
78
79static inline void fscrypt_set_d_op(struct dentry *dentry)
80{
81 d_set_d_op(dentry, &fscrypt_d_ops);
82}
83
84static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
85{
86 spin_lock(&dentry->d_lock);
87 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
88 spin_unlock(&dentry->d_lock);
89}
90
91/* policy.c */
92extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
93extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
94extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
95extern int fscrypt_inherit_context(struct inode *, struct inode *,
96 void *, bool);
97/* keyinfo.c */
98extern int fscrypt_get_encryption_info(struct inode *);
Jaegeuk Kimd9197652018-01-05 10:44:52 -080099extern void fscrypt_put_encryption_info(struct inode *);
Hyojun Kim63da4202017-10-06 17:10:08 -0700100
101/* fname.c */
102extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
103 int lookup, struct fscrypt_name *);
104
105static inline void fscrypt_free_filename(struct fscrypt_name *fname)
106{
107 kfree(fname->crypto_buf.name);
108}
109
Hyojun Kim63da4202017-10-06 17:10:08 -0700110extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
111 struct fscrypt_str *);
112extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
113extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
114 const struct fscrypt_str *, struct fscrypt_str *);
Hyojun Kim63da4202017-10-06 17:10:08 -0700115
116#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
117
118/* Extracts the second-to-last ciphertext block; see explanation below */
119#define FSCRYPT_FNAME_DIGEST(name, len) \
120 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
121 FS_CRYPTO_BLOCK_SIZE))
122
123#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
124
125/**
126 * fscrypt_digested_name - alternate identifier for an on-disk filename
127 *
128 * When userspace lists an encrypted directory without access to the key,
129 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
130 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
131 * full ciphertext (base64-encoded). This is necessary to allow supporting
132 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
133 *
134 * To make it possible for filesystems to still find the correct directory entry
135 * despite not knowing the full on-disk name, we encode any filesystem-specific
136 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
137 * followed by the second-to-last ciphertext block of the filename. Due to the
138 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
139 * depends on the full plaintext. (Note that ciphertext stealing causes the
140 * last two blocks to appear "flipped".) This makes accidental collisions very
141 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
142 * share the same filesystem-specific hashes.
143 *
144 * However, this scheme isn't immune to intentional collisions, which can be
145 * created by anyone able to create arbitrary plaintext filenames and view them
146 * without the key. Making the "digest" be a real cryptographic hash like
147 * SHA-256 over the full ciphertext would prevent this, although it would be
148 * less efficient and harder to implement, especially since the filesystem would
149 * need to calculate it for each directory entry examined during a search.
150 */
151struct fscrypt_digested_name {
152 u32 hash;
153 u32 minor_hash;
154 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
155};
156
157/**
158 * fscrypt_match_name() - test whether the given name matches a directory entry
159 * @fname: the name being searched for
160 * @de_name: the name from the directory entry
161 * @de_name_len: the length of @de_name in bytes
162 *
163 * Normally @fname->disk_name will be set, and in that case we simply compare
164 * that to the name stored in the directory entry. The only exception is that
165 * if we don't have the key for an encrypted directory and a filename in it is
166 * very long, then we won't have the full disk_name and we'll instead need to
167 * match against the fscrypt_digested_name.
168 *
169 * Return: %true if the name matches, otherwise %false.
170 */
171static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
172 const u8 *de_name, u32 de_name_len)
173{
174 if (unlikely(!fname->disk_name.name)) {
175 const struct fscrypt_digested_name *n =
176 (const void *)fname->crypto_buf.name;
177 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
178 return false;
179 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
180 return false;
181 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
182 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
183 }
184
185 if (de_name_len != fname->disk_name.len)
186 return false;
187 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
188}
189
190/* bio.c */
Eric Biggers823fd562018-04-18 11:09:47 -0700191extern void fscrypt_decrypt_bio(struct bio *);
192extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
193 struct bio *bio);
Hyojun Kim63da4202017-10-06 17:10:08 -0700194extern void fscrypt_pullback_bio_page(struct page **, bool);
195extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
196 unsigned int);
197
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700198/* hooks.c */
199extern int fscrypt_file_open(struct inode *inode, struct file *filp);
200extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir);
201extern int __fscrypt_prepare_rename(struct inode *old_dir,
202 struct dentry *old_dentry,
203 struct inode *new_dir,
204 struct dentry *new_dentry,
205 unsigned int flags);
206extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
Jaegeuk Kimd9197652018-01-05 10:44:52 -0800207extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
208 unsigned int max_len,
209 struct fscrypt_str *disk_link);
210extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
211 unsigned int len,
212 struct fscrypt_str *disk_link);
213extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
214 unsigned int max_size,
215 struct delayed_call *done);
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700216
Hyojun Kim63da4202017-10-06 17:10:08 -0700217#endif /* _LINUX_FSCRYPT_SUPP_H */