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