fscrypto.h 8.4 KB

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