fscrypt_supp.h 7.2 KB

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