pfk_fscrypt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. /*
  14. * Per-File-Key (PFK) - FSCRYPT
  15. *
  16. * This driver is used for working with F2FS crypt extension
  17. *
  18. * The key information is stored in node by F2FS when file is first opened
  19. * and will be later accessed by Block Device Driver to actually load the key
  20. * to encryption hw.
  21. *
  22. * PFK exposes API's for loading and removing keys from encryption hw
  23. * and also API to determine whether 2 adjacent blocks can be agregated by
  24. * Block Layer in one request to encryption hw.
  25. *
  26. */
  27. /* Uncomment the line below to enable debug messages */
  28. #define DEBUG 1
  29. #define pr_fmt(fmt) "pfk_fscrypt [%s]: " fmt, __func__
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/errno.h>
  33. #include <linux/printk.h>
  34. #include "fscrypt_ice.h"
  35. #include "pfk_fscrypt.h"
  36. static bool pfk_fscrypt_ready;
  37. /*
  38. * pfk_fscrypt_deinit() - Deinit function, should be invoked by upper PFK layer
  39. */
  40. void pfk_fscrypt_deinit(void)
  41. {
  42. pfk_fscrypt_ready = false;
  43. }
  44. /*
  45. * pfk_fscrypt_init() - Init function, should be invoked by upper PFK layer
  46. */
  47. int __init pfk_fscrypt_init(void)
  48. {
  49. pfk_fscrypt_ready = true;
  50. pr_info("PFK FSCRYPT inited successfully\n");
  51. return 0;
  52. }
  53. /**
  54. * pfk_fscrypt_is_ready() - driver is initialized and ready.
  55. *
  56. * Return: true if the driver is ready.
  57. */
  58. static inline bool pfk_fscrypt_is_ready(void)
  59. {
  60. return pfk_fscrypt_ready;
  61. }
  62. /**
  63. * pfk_is_f2fs_type() - return true if inode belongs to ICE F2FS PFE
  64. * @inode: inode pointer
  65. */
  66. bool pfk_is_fscrypt_type(const struct inode *inode)
  67. {
  68. if (!pfe_is_inode_filesystem_type(inode, "f2fs"))
  69. return false;
  70. return fscrypt_should_be_processed_by_ice(inode);
  71. }
  72. /**
  73. * pfk_fscrypt_parse_cipher() - parse cipher from inode to enum
  74. * @inode: inode
  75. * @algo: pointer to store the output enum (can be null)
  76. *
  77. * return 0 in case of success, error otherwise (i.e not supported cipher)
  78. */
  79. static int pfk_fscrypt_parse_cipher(const struct inode *inode,
  80. enum ice_cryto_algo_mode *algo)
  81. {
  82. /*
  83. * currently only AES XTS algo is supported
  84. * in the future, table with supported ciphers might
  85. * be introduced
  86. */
  87. if (!inode)
  88. return -EINVAL;
  89. if (!fscrypt_is_aes_xts_cipher(inode)) {
  90. pr_err("f2fs alghoritm is not supported by pfk\n");
  91. return -EINVAL;
  92. }
  93. if (algo)
  94. *algo = ICE_CRYPTO_ALGO_MODE_AES_XTS;
  95. return 0;
  96. }
  97. int pfk_fscrypt_parse_inode(const struct bio *bio,
  98. const struct inode *inode,
  99. struct pfk_key_info *key_info,
  100. enum ice_cryto_algo_mode *algo,
  101. bool *is_pfe,
  102. const char *storage_type)
  103. {
  104. int ret = 0;
  105. if (!is_pfe)
  106. return -EINVAL;
  107. /*
  108. * only a few errors below can indicate that
  109. * this function was not invoked within PFE context,
  110. * otherwise we will consider it PFE
  111. */
  112. *is_pfe = true;
  113. if (!pfk_fscrypt_is_ready())
  114. return -ENODEV;
  115. if (!inode)
  116. return -EINVAL;
  117. if (!key_info)
  118. return -EINVAL;
  119. key_info->key = fscrypt_get_ice_encryption_key(inode);
  120. if (!key_info->key) {
  121. pr_err("could not parse key from f2fs\n");
  122. return -EINVAL;
  123. }
  124. key_info->key_size = fscrypt_get_ice_encryption_key_size(inode);
  125. if (!key_info->key_size) {
  126. pr_err("could not parse key size from f2fs\n");
  127. return -EINVAL;
  128. }
  129. key_info->salt = fscrypt_get_ice_encryption_salt(inode);
  130. if (!key_info->salt) {
  131. pr_err("could not parse salt from f2fs\n");
  132. return -EINVAL;
  133. }
  134. key_info->salt_size = fscrypt_get_ice_encryption_salt_size(inode);
  135. if (!key_info->salt_size) {
  136. pr_err("could not parse salt size from f2fs\n");
  137. return -EINVAL;
  138. }
  139. ret = pfk_fscrypt_parse_cipher(inode, algo);
  140. if (ret != 0) {
  141. pr_err("not supported cipher\n");
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. bool pfk_fscrypt_allow_merge_bio(const struct bio *bio1,
  147. const struct bio *bio2, const struct inode *inode1,
  148. const struct inode *inode2)
  149. {
  150. bool mergeable;
  151. /* if there is no fscrypt pfk, don't disallow merging blocks */
  152. if (!pfk_fscrypt_is_ready())
  153. return true;
  154. if (!inode1 || !inode2)
  155. return false;
  156. mergeable = fscrypt_is_ice_encryption_info_equal(inode1, inode2);
  157. if (!mergeable)
  158. return false;
  159. /* ICE allows only consecutive iv_key stream. */
  160. if (!bio_dun(bio1) && !bio_dun(bio2))
  161. return true;
  162. else if(!bio_dun(bio1) || !bio_dun(bio2))
  163. return false;
  164. return bio_end_dun(bio1) == bio_dun(bio2);
  165. }