1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef _LINUX_FSCRYPT_H
14#define _LINUX_FSCRYPT_H
15
16#include <linux/fs.h>
17#include <linux/mm.h>
18#include <linux/slab.h>
19
20#define FS_CRYPTO_BLOCK_SIZE 16
21
22struct fscrypt_ctx;
23struct fscrypt_info;
24
25struct fscrypt_str {
26 unsigned char *name;
27 u32 len;
28};
29
30struct fscrypt_name {
31 const struct qstr *usr_fname;
32 struct fscrypt_str disk_name;
33 u32 hash;
34 u32 minor_hash;
35 struct fscrypt_str crypto_buf;
36};
37
38#define FSTR_INIT(n, l) { .name = n, .len = l }
39#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
40#define fname_name(p) ((p)->disk_name.name)
41#define fname_len(p) ((p)->disk_name.len)
42
43
44#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
45
46#ifdef CONFIG_FS_ENCRYPTION
47
48
49
50#define FS_CFLG_OWN_PAGES (1U << 1)
51
52
53
54
55struct fscrypt_operations {
56 unsigned int flags;
57 const char *key_prefix;
58 int (*get_context)(struct inode *, void *, size_t);
59 int (*set_context)(struct inode *, const void *, size_t, void *);
60 bool (*dummy_context)(struct inode *);
61 bool (*empty_dir)(struct inode *);
62 unsigned int max_namelen;
63};
64
65struct fscrypt_ctx {
66 union {
67 struct {
68 struct page *bounce_page;
69 struct page *control_page;
70 } w;
71 struct {
72 struct bio *bio;
73 struct work_struct work;
74 } r;
75 struct list_head free_list;
76 };
77 u8 flags;
78};
79
80static inline bool fscrypt_has_encryption_key(const struct inode *inode)
81{
82 return (inode->i_crypt_info != NULL);
83}
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
91
92extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
93extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
94extern void fscrypt_release_ctx(struct fscrypt_ctx *);
95extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
96 unsigned int, unsigned int,
97 u64, gfp_t);
98extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
99 unsigned int, u64);
100
101static inline struct page *fscrypt_control_page(struct page *page)
102{
103 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
104}
105
106extern void fscrypt_restore_control_page(struct page *);
107
108
109extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
110extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
111extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
112extern int fscrypt_inherit_context(struct inode *, struct inode *,
113 void *, bool);
114
115extern int fscrypt_get_encryption_info(struct inode *);
116extern void fscrypt_put_encryption_info(struct inode *);
117
118
119extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
120 int lookup, struct fscrypt_name *);
121
122static inline void fscrypt_free_filename(struct fscrypt_name *fname)
123{
124 kfree(fname->crypto_buf.name);
125}
126
127extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
128 struct fscrypt_str *);
129extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
130extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
131 const struct fscrypt_str *, struct fscrypt_str *);
132
133#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
134
135
136#define FSCRYPT_FNAME_DIGEST(name, len) \
137 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
138 FS_CRYPTO_BLOCK_SIZE))
139
140#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168struct fscrypt_digested_name {
169 u32 hash;
170 u32 minor_hash;
171 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
172};
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
189 const u8 *de_name, u32 de_name_len)
190{
191 if (unlikely(!fname->disk_name.name)) {
192 const struct fscrypt_digested_name *n =
193 (const void *)fname->crypto_buf.name;
194 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
195 return false;
196 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
197 return false;
198 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
199 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
200 }
201
202 if (de_name_len != fname->disk_name.len)
203 return false;
204 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
205}
206
207
208extern void fscrypt_decrypt_bio(struct bio *);
209extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
210 struct bio *bio);
211extern void fscrypt_pullback_bio_page(struct page **, bool);
212extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
213 unsigned int);
214
215
216extern int fscrypt_file_open(struct inode *inode, struct file *filp);
217extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir);
218extern int __fscrypt_prepare_rename(struct inode *old_dir,
219 struct dentry *old_dentry,
220 struct inode *new_dir,
221 struct dentry *new_dentry,
222 unsigned int flags);
223extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
224extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
225 unsigned int max_len,
226 struct fscrypt_str *disk_link);
227extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
228 unsigned int len,
229 struct fscrypt_str *disk_link);
230extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
231 unsigned int max_size,
232 struct delayed_call *done);
233#else
234
235static inline bool fscrypt_has_encryption_key(const struct inode *inode)
236{
237 return false;
238}
239
240static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
241{
242 return false;
243}
244
245
246static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
247{
248}
249
250static inline struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode,
251 gfp_t gfp_flags)
252{
253 return ERR_PTR(-EOPNOTSUPP);
254}
255
256static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
257{
258 return;
259}
260
261static inline struct page *fscrypt_encrypt_page(const struct inode *inode,
262 struct page *page,
263 unsigned int len,
264 unsigned int offs,
265 u64 lblk_num, gfp_t gfp_flags)
266{
267 return ERR_PTR(-EOPNOTSUPP);
268}
269
270static inline int fscrypt_decrypt_page(const struct inode *inode,
271 struct page *page,
272 unsigned int len, unsigned int offs,
273 u64 lblk_num)
274{
275 return -EOPNOTSUPP;
276}
277
278static inline struct page *fscrypt_control_page(struct page *page)
279{
280 WARN_ON_ONCE(1);
281 return ERR_PTR(-EINVAL);
282}
283
284static inline void fscrypt_restore_control_page(struct page *page)
285{
286 return;
287}
288
289
290static inline int fscrypt_ioctl_set_policy(struct file *filp,
291 const void __user *arg)
292{
293 return -EOPNOTSUPP;
294}
295
296static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
297{
298 return -EOPNOTSUPP;
299}
300
301static inline int fscrypt_has_permitted_context(struct inode *parent,
302 struct inode *child)
303{
304 return 0;
305}
306
307static inline int fscrypt_inherit_context(struct inode *parent,
308 struct inode *child,
309 void *fs_data, bool preload)
310{
311 return -EOPNOTSUPP;
312}
313
314
315static inline int fscrypt_get_encryption_info(struct inode *inode)
316{
317 return -EOPNOTSUPP;
318}
319
320static inline void fscrypt_put_encryption_info(struct inode *inode)
321{
322 return;
323}
324
325
326static inline int fscrypt_setup_filename(struct inode *dir,
327 const struct qstr *iname,
328 int lookup, struct fscrypt_name *fname)
329{
330 if (IS_ENCRYPTED(dir))
331 return -EOPNOTSUPP;
332
333 memset(fname, 0, sizeof(struct fscrypt_name));
334 fname->usr_fname = iname;
335 fname->disk_name.name = (unsigned char *)iname->name;
336 fname->disk_name.len = iname->len;
337 return 0;
338}
339
340static inline void fscrypt_free_filename(struct fscrypt_name *fname)
341{
342 return;
343}
344
345static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
346 u32 max_encrypted_len,
347 struct fscrypt_str *crypto_str)
348{
349 return -EOPNOTSUPP;
350}
351
352static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
353{
354 return;
355}
356
357static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
358 u32 hash, u32 minor_hash,
359 const struct fscrypt_str *iname,
360 struct fscrypt_str *oname)
361{
362 return -EOPNOTSUPP;
363}
364
365static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
366 const u8 *de_name, u32 de_name_len)
367{
368
369 if (de_name_len != fname->disk_name.len)
370 return false;
371 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
372}
373
374
375static inline void fscrypt_decrypt_bio(struct bio *bio)
376{
377}
378
379static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
380 struct bio *bio)
381{
382}
383
384static inline void fscrypt_pullback_bio_page(struct page **page, bool restore)
385{
386 return;
387}
388
389static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
390 sector_t pblk, unsigned int len)
391{
392 return -EOPNOTSUPP;
393}
394
395
396
397static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
398{
399 if (IS_ENCRYPTED(inode))
400 return -EOPNOTSUPP;
401 return 0;
402}
403
404static inline int __fscrypt_prepare_link(struct inode *inode,
405 struct inode *dir)
406{
407 return -EOPNOTSUPP;
408}
409
410static inline int __fscrypt_prepare_rename(struct inode *old_dir,
411 struct dentry *old_dentry,
412 struct inode *new_dir,
413 struct dentry *new_dentry,
414 unsigned int flags)
415{
416 return -EOPNOTSUPP;
417}
418
419static inline int __fscrypt_prepare_lookup(struct inode *dir,
420 struct dentry *dentry)
421{
422 return -EOPNOTSUPP;
423}
424
425static inline int __fscrypt_prepare_symlink(struct inode *dir,
426 unsigned int len,
427 unsigned int max_len,
428 struct fscrypt_str *disk_link)
429{
430 return -EOPNOTSUPP;
431}
432
433
434static inline int __fscrypt_encrypt_symlink(struct inode *inode,
435 const char *target,
436 unsigned int len,
437 struct fscrypt_str *disk_link)
438{
439 return -EOPNOTSUPP;
440}
441
442static inline const char *fscrypt_get_symlink(struct inode *inode,
443 const void *caddr,
444 unsigned int max_size,
445 struct delayed_call *done)
446{
447 return ERR_PTR(-EOPNOTSUPP);
448}
449#endif
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464static inline int fscrypt_require_key(struct inode *inode)
465{
466 if (IS_ENCRYPTED(inode)) {
467 int err = fscrypt_get_encryption_info(inode);
468
469 if (err)
470 return err;
471 if (!fscrypt_has_encryption_key(inode))
472 return -ENOKEY;
473 }
474 return 0;
475}
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495static inline int fscrypt_prepare_link(struct dentry *old_dentry,
496 struct inode *dir,
497 struct dentry *dentry)
498{
499 if (IS_ENCRYPTED(dir))
500 return __fscrypt_prepare_link(d_inode(old_dentry), dir);
501 return 0;
502}
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525static inline int fscrypt_prepare_rename(struct inode *old_dir,
526 struct dentry *old_dentry,
527 struct inode *new_dir,
528 struct dentry *new_dentry,
529 unsigned int flags)
530{
531 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
532 return __fscrypt_prepare_rename(old_dir, old_dentry,
533 new_dir, new_dentry, flags);
534 return 0;
535}
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556static inline int fscrypt_prepare_lookup(struct inode *dir,
557 struct dentry *dentry,
558 unsigned int flags)
559{
560 if (IS_ENCRYPTED(dir))
561 return __fscrypt_prepare_lookup(dir, dentry);
562 return 0;
563}
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582static inline int fscrypt_prepare_setattr(struct dentry *dentry,
583 struct iattr *attr)
584{
585 if (attr->ia_valid & ATTR_SIZE)
586 return fscrypt_require_key(d_inode(dentry));
587 return 0;
588}
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613static inline int fscrypt_prepare_symlink(struct inode *dir,
614 const char *target,
615 unsigned int len,
616 unsigned int max_len,
617 struct fscrypt_str *disk_link)
618{
619 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
620 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
621
622 disk_link->name = (unsigned char *)target;
623 disk_link->len = len + 1;
624 if (disk_link->len > max_len)
625 return -ENAMETOOLONG;
626 return 0;
627}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644static inline int fscrypt_encrypt_symlink(struct inode *inode,
645 const char *target,
646 unsigned int len,
647 struct fscrypt_str *disk_link)
648{
649 if (IS_ENCRYPTED(inode))
650 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
651 return 0;
652}
653
654#endif
655