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