]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/fscrypto.h
fscrypt: Never allocate fscrypt_ctx on in-place encryption
[mirror_ubuntu-artful-kernel.git] / include / linux / fscrypto.h
CommitLineData
0b81d077
JK
1/*
2 * General per-file encryption definition
3 *
4 * Copyright (C) 2015, Google, Inc.
5 *
6 * Written by Michael Halcrow, 2015.
7 * Modified by Jaegeuk Kim, 2015.
8 */
9
10#ifndef _LINUX_FSCRYPTO_H
11#define _LINUX_FSCRYPTO_H
12
13#include <linux/key.h>
14#include <linux/fs.h>
15#include <linux/mm.h>
16#include <linux/bio.h>
17#include <linux/dcache.h>
d407574e 18#include <crypto/skcipher.h>
0b81d077
JK
19#include <uapi/linux/fs.h>
20
cc4e0df0 21#define FS_CRYPTO_BLOCK_SIZE 16
0b81d077 22
cc4e0df0
TT
23struct fscrypt_info;
24struct fscrypt_ctx;
0b81d077
JK
25
26struct fscrypt_ctx {
27 union {
28 struct {
29 struct page *bounce_page; /* Ciphertext page */
30 struct page *control_page; /* Original page */
31 } w;
32 struct {
33 struct bio *bio;
34 struct work_struct work;
35 } r;
36 struct list_head free_list; /* Free list */
37 };
38 u8 flags; /* Flags */
39 u8 mode; /* Encryption mode for tfm */
40};
41
0b81d077
JK
42/**
43 * For encrypted symlinks, the ciphertext length is stored at the beginning
44 * of the string in little-endian format.
45 */
46struct fscrypt_symlink_data {
47 __le16 len;
48 char encrypted_path[1];
49} __packed;
50
51/**
52 * This function is used to calculate the disk space required to
53 * store a filename of length l in encrypted symlink format.
54 */
55static inline u32 fscrypt_symlink_data_len(u32 l)
56{
57 if (l < FS_CRYPTO_BLOCK_SIZE)
58 l = FS_CRYPTO_BLOCK_SIZE;
59 return (l + sizeof(struct fscrypt_symlink_data) - 1);
60}
61
62struct fscrypt_str {
63 unsigned char *name;
64 u32 len;
65};
66
67struct fscrypt_name {
68 const struct qstr *usr_fname;
69 struct fscrypt_str disk_name;
70 u32 hash;
71 u32 minor_hash;
72 struct fscrypt_str crypto_buf;
73};
74
75#define FSTR_INIT(n, l) { .name = n, .len = l }
76#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
77#define fname_name(p) ((p)->disk_name.name)
78#define fname_len(p) ((p)->disk_name.len)
79
1c7dcf69
DG
80/*
81 * fscrypt superblock flags
82 */
83#define FS_CFLG_INPLACE_ENCRYPTION (1U << 1)
84
0b81d077
JK
85/*
86 * crypto opertions for filesystems
87 */
88struct fscrypt_operations {
1c7dcf69 89 unsigned int flags;
0b81d077 90 int (*get_context)(struct inode *, void *, size_t);
b5a7aef1 91 int (*key_prefix)(struct inode *, u8 **);
0b81d077
JK
92 int (*prepare_context)(struct inode *);
93 int (*set_context)(struct inode *, const void *, size_t, void *);
94 int (*dummy_context)(struct inode *);
95 bool (*is_encrypted)(struct inode *);
96 bool (*empty_dir)(struct inode *);
97 unsigned (*max_namelen)(struct inode *);
98};
99
100static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
101{
102 if (inode->i_sb->s_cop->dummy_context &&
103 inode->i_sb->s_cop->dummy_context(inode))
104 return true;
105 return false;
106}
107
108static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
109{
110 return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
111}
112
113static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
114{
115 return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
116}
117
0b81d077
JK
118static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
119{
120 if (str->len == 1 && str->name[0] == '.')
121 return true;
122
123 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
124 return true;
125
126 return false;
127}
128
129static inline struct page *fscrypt_control_page(struct page *page)
130{
131#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
132 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
133#else
134 WARN_ON_ONCE(1);
135 return ERR_PTR(-EINVAL);
136#endif
137}
138
0b93e1b9 139static inline int fscrypt_has_encryption_key(const struct inode *inode)
0b81d077
JK
140{
141#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
142 return (inode->i_crypt_info != NULL);
143#else
144 return 0;
145#endif
146}
147
148static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
149{
150#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
151 spin_lock(&dentry->d_lock);
152 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
153 spin_unlock(&dentry->d_lock);
154#endif
155}
156
157#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
158extern const struct dentry_operations fscrypt_d_ops;
159#endif
160
161static inline void fscrypt_set_d_op(struct dentry *dentry)
162{
163#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
164 d_set_d_op(dentry, &fscrypt_d_ops);
165#endif
166}
167
168#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
169/* crypto.c */
170extern struct kmem_cache *fscrypt_info_cachep;
0b81d077 171
0b93e1b9 172extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
0b81d077 173extern void fscrypt_release_ctx(struct fscrypt_ctx *);
0b93e1b9 174extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
7821d4dd 175 unsigned int, unsigned int,
9c4bb8a3 176 pgoff_t, gfp_t);
0b93e1b9 177extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
9c4bb8a3 178 unsigned int, pgoff_t);
0b81d077
JK
179extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
180extern void fscrypt_pullback_bio_page(struct page **, bool);
181extern void fscrypt_restore_control_page(struct page *);
0b93e1b9 182extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
0b81d077
JK
183 unsigned int);
184/* policy.c */
db717d8e
EB
185extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
186extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
0b81d077
JK
187extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
188extern int fscrypt_inherit_context(struct inode *, struct inode *,
189 void *, bool);
190/* keyinfo.c */
0b81d077
JK
191extern int fscrypt_get_encryption_info(struct inode *);
192extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
193
194/* fname.c */
195extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
196 int lookup, struct fscrypt_name *);
197extern void fscrypt_free_filename(struct fscrypt_name *);
0b93e1b9
DG
198extern u32 fscrypt_fname_encrypted_size(const struct inode *, u32);
199extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
0b81d077
JK
200 struct fscrypt_str *);
201extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
202extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
203 const struct fscrypt_str *, struct fscrypt_str *);
204extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
205 struct fscrypt_str *);
206#endif
207
208/* crypto.c */
0b93e1b9 209static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(const struct inode *i,
b32e4482 210 gfp_t f)
0b81d077
JK
211{
212 return ERR_PTR(-EOPNOTSUPP);
213}
214
215static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
216{
217 return;
218}
219
0b93e1b9 220static inline struct page *fscrypt_notsupp_encrypt_page(const struct inode *i,
7821d4dd
DG
221 struct page *p,
222 unsigned int len,
223 unsigned int offs,
9c4bb8a3 224 pgoff_t index, gfp_t f)
0b81d077
JK
225{
226 return ERR_PTR(-EOPNOTSUPP);
227}
228
0b93e1b9 229static inline int fscrypt_notsupp_decrypt_page(const struct inode *i, struct page *p,
9c4bb8a3
DG
230 unsigned int len, unsigned int offs,
231 pgoff_t index)
0b81d077
JK
232{
233 return -EOPNOTSUPP;
234}
235
236static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
237 struct bio *b)
238{
239 return;
240}
241
242static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
243{
244 return;
245}
246
247static inline void fscrypt_notsupp_restore_control_page(struct page *p)
248{
249 return;
250}
251
0b93e1b9 252static inline int fscrypt_notsupp_zeroout_range(const struct inode *i, pgoff_t p,
0b81d077
JK
253 sector_t s, unsigned int f)
254{
255 return -EOPNOTSUPP;
256}
257
258/* policy.c */
db717d8e
EB
259static inline int fscrypt_notsupp_ioctl_set_policy(struct file *f,
260 const void __user *arg)
0b81d077
JK
261{
262 return -EOPNOTSUPP;
263}
264
db717d8e
EB
265static inline int fscrypt_notsupp_ioctl_get_policy(struct file *f,
266 void __user *arg)
0b81d077
JK
267{
268 return -EOPNOTSUPP;
269}
270
271static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
272 struct inode *i)
273{
274 return 0;
275}
276
277static inline int fscrypt_notsupp_inherit_context(struct inode *p,
278 struct inode *i, void *v, bool b)
279{
280 return -EOPNOTSUPP;
281}
282
283/* keyinfo.c */
284static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
285{
286 return -EOPNOTSUPP;
287}
288
289static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
290 struct fscrypt_info *f)
291{
292 return;
293}
294
295 /* fname.c */
296static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
297 const struct qstr *iname,
298 int lookup, struct fscrypt_name *fname)
299{
300 if (dir->i_sb->s_cop->is_encrypted(dir))
301 return -EOPNOTSUPP;
302
303 memset(fname, 0, sizeof(struct fscrypt_name));
304 fname->usr_fname = iname;
305 fname->disk_name.name = (unsigned char *)iname->name;
306 fname->disk_name.len = iname->len;
307 return 0;
308}
309
310static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
311{
312 return;
313}
314
315static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
316{
317 /* never happens */
318 WARN_ON(1);
319 return 0;
320}
321
322static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
323 u32 ilen, struct fscrypt_str *crypto_str)
324{
325 return -EOPNOTSUPP;
326}
327
328static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
329{
330 return;
331}
332
333static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode,
334 u32 hash, u32 minor_hash,
335 const struct fscrypt_str *iname,
336 struct fscrypt_str *oname)
337{
338 return -EOPNOTSUPP;
339}
340
341static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
342 const struct qstr *iname,
343 struct fscrypt_str *oname)
344{
345 return -EOPNOTSUPP;
346}
347#endif /* _LINUX_FSCRYPTO_H */