pfk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2015-2018, 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).
  15. *
  16. * This driver is responsible for overall management of various
  17. * Per File Encryption variants that work on top of or as part of different
  18. * file systems.
  19. *
  20. * The driver has the following purpose :
  21. * 1) Define priorities between PFE's if more than one is enabled
  22. * 2) Extract key information from inode
  23. * 3) Load and manage various keys in ICE HW engine
  24. * 4) It should be invoked from various layers in FS/BLOCK/STORAGE DRIVER
  25. * that need to take decision on HW encryption management of the data
  26. * Some examples:
  27. * BLOCK LAYER: when it takes decision on whether 2 chunks can be united
  28. * to one encryption / decryption request sent to the HW
  29. *
  30. * UFS DRIVER: when it need to configure ICE HW with a particular key slot
  31. * to be used for encryption / decryption
  32. *
  33. * PFE variants can differ on particular way of storing the cryptographic info
  34. * inside inode, actions to be taken upon file operations, etc., but the common
  35. * properties are described above
  36. *
  37. */
  38. /* Uncomment the line below to enable debug messages */
  39. /* #define DEBUG 1 */
  40. #define pr_fmt(fmt) "pfk [%s]: " fmt, __func__
  41. #include <linux/module.h>
  42. #include <linux/fs.h>
  43. #include <linux/errno.h>
  44. #include <linux/printk.h>
  45. #include <linux/bio.h>
  46. #include <linux/security.h>
  47. #include <crypto/algapi.h>
  48. #include <crypto/ice.h>
  49. #include <linux/pfk.h>
  50. #include <linux/ecryptfs.h>
  51. #include "pfk_kc.h"
  52. #include "objsec.h"
  53. #include "ecryptfs_kernel.h"
  54. #include "pfk_ice.h"
  55. #include "pfk_fscrypt.h"
  56. #include "pfk_ecryptfs.h"
  57. #include "pfk_internal.h"
  58. static bool pfk_ready;
  59. /* might be replaced by a table when more than one cipher is supported */
  60. #define PFK_SUPPORTED_KEY_SIZE 32
  61. #define PFK_SUPPORTED_SALT_SIZE 32
  62. /* Various PFE types and function tables to support each one of them */
  63. enum pfe_type {ECRYPTFS_PFE, FSCRYPT_PFE, INVALID_PFE};
  64. typedef int (*pfk_parse_inode_type)(const struct bio *bio,
  65. const struct inode *inode,
  66. struct pfk_key_info *key_info,
  67. enum ice_cryto_algo_mode *algo,
  68. bool *is_pfe,
  69. const char *storage_type);
  70. typedef bool (*pfk_allow_merge_bio_type)(const struct bio *bio1,
  71. const struct bio *bio2, const struct inode *inode1,
  72. const struct inode *inode2);
  73. static const pfk_parse_inode_type pfk_parse_inode_ftable[] = {
  74. /* ECRYPTFS_PFE */ &pfk_ecryptfs_parse_inode,
  75. /* FSCRYPT_PFE */ &pfk_fscrypt_parse_inode,
  76. };
  77. static const pfk_allow_merge_bio_type pfk_allow_merge_bio_ftable[] = {
  78. /* ECRYPTFS_PFE */ &pfk_ecryptfs_allow_merge_bio,
  79. /* FSCRYPT_PFE */ &pfk_fscrypt_allow_merge_bio,
  80. };
  81. static void __exit pfk_exit(void)
  82. {
  83. pfk_ready = false;
  84. pfk_fscrypt_deinit();
  85. pfk_kc_deinit();
  86. }
  87. static int __init pfk_init(void)
  88. {
  89. int ret = 0;
  90. ret = pfk_ecryptfs_init();
  91. if (ret != 0)
  92. goto fail;
  93. ret = pfk_fscrypt_init();
  94. if (ret != 0) {
  95. pfk_ecryptfs_deinit();
  96. goto fail;
  97. }
  98. ret = pfk_kc_init();
  99. if (ret != 0) {
  100. pr_err("could init pfk key cache, error %d\n", ret);
  101. pfk_fscrypt_deinit();
  102. pfk_ecryptfs_deinit();
  103. goto fail;
  104. }
  105. pfk_ready = true;
  106. pr_info("Driver initialized successfully\n");
  107. return 0;
  108. fail:
  109. pr_err("Failed to init driver\n");
  110. return -ENODEV;
  111. }
  112. /*
  113. * If more than one type is supported simultaneously, this function will also
  114. * set the priority between them
  115. */
  116. static enum pfe_type pfk_get_pfe_type(const struct inode *inode)
  117. {
  118. if (!inode)
  119. return INVALID_PFE;
  120. if (pfk_is_ecryptfs_type(inode))
  121. return ECRYPTFS_PFE;
  122. if (pfk_is_fscrypt_type(inode))
  123. return FSCRYPT_PFE;
  124. return INVALID_PFE;
  125. }
  126. /**
  127. * pfk_is_ready() - driver is initialized and ready.
  128. *
  129. * Return: true if the driver is ready.
  130. */
  131. static inline bool pfk_is_ready(void)
  132. {
  133. return pfk_ready;
  134. }
  135. /**
  136. * pfk_bio_get_inode() - get the inode from a bio.
  137. * @bio: Pointer to BIO structure.
  138. *
  139. * Walk the bio struct links to get the inode.
  140. * Please note, that in general bio may consist of several pages from
  141. * several files, but in our case we always assume that all pages come
  142. * from the same file, since our logic ensures it. That is why we only
  143. * walk through the first page to look for inode.
  144. *
  145. * Return: pointer to the inode struct if successful, or NULL otherwise.
  146. *
  147. */
  148. static struct inode *pfk_bio_get_inode(const struct bio *bio)
  149. {
  150. struct inode *inode;
  151. if (!bio)
  152. return NULL;
  153. if (!bio_has_data((struct bio *)bio))
  154. return NULL;
  155. if (!bio->bi_io_vec)
  156. return NULL;
  157. if (!bio->bi_io_vec->bv_page)
  158. return NULL;
  159. /* Using direct-io (O_DIRECT) without page cache */
  160. inode = dio_bio_get_inode((struct bio *)bio);
  161. if (inode) {
  162. pr_debug("inode on direct-io, inode = 0x%p.\n", inode);
  163. return inode;
  164. }
  165. if (!page_mapping(bio->bi_io_vec->bv_page))
  166. return NULL;
  167. return page_mapping(bio->bi_io_vec->bv_page)->host;
  168. }
  169. /**
  170. * pfk_key_size_to_key_type() - translate key size to key size enum
  171. * @key_size: key size in bytes
  172. * @key_size_type: pointer to store the output enum (can be null)
  173. *
  174. * return 0 in case of success, error otherwise (i.e not supported key size)
  175. */
  176. int pfk_key_size_to_key_type(size_t key_size,
  177. enum ice_crpto_key_size *key_size_type)
  178. {
  179. /*
  180. * currently only 32 bit key size is supported
  181. * in the future, table with supported key sizes might
  182. * be introduced
  183. */
  184. if (key_size != PFK_SUPPORTED_KEY_SIZE) {
  185. pr_err("not supported key size %zu\n", key_size);
  186. return -EINVAL;
  187. }
  188. if (key_size_type)
  189. *key_size_type = ICE_CRYPTO_KEY_SIZE_256;
  190. return 0;
  191. }
  192. /*
  193. * Retrieves filesystem type from inode's superblock
  194. */
  195. bool pfe_is_inode_filesystem_type(const struct inode *inode,
  196. const char *fs_type)
  197. {
  198. if (!inode || !fs_type)
  199. return false;
  200. if (!inode->i_sb)
  201. return false;
  202. if (!inode->i_sb->s_type)
  203. return false;
  204. return (strcmp(inode->i_sb->s_type->name, fs_type) == 0);
  205. }
  206. /**
  207. * pfk_get_key_for_bio() - get the encryption key to be used for a bio
  208. *
  209. * @bio: pointer to the BIO
  210. * @key_info: pointer to the key information which will be filled in
  211. * @algo_mode: optional pointer to the algorithm identifier which will be set
  212. * @is_pfe: will be set to false if the BIO should be left unencrypted
  213. *
  214. * Return: 0 if a key is being used, otherwise a -errno value
  215. */
  216. static int pfk_get_key_for_bio(const struct bio *bio,
  217. struct pfk_key_info *key_info,
  218. enum ice_cryto_algo_mode *algo_mode,
  219. bool *is_pfe, unsigned int *data_unit)
  220. {
  221. const struct inode *inode;
  222. enum pfe_type which_pfe;
  223. const struct blk_encryption_key *key;
  224. char *s_type = NULL;
  225. inode = pfk_bio_get_inode(bio);
  226. which_pfe = pfk_get_pfe_type(inode);
  227. s_type = (char *)pfk_kc_get_storage_type();
  228. if (data_unit && (bio_dun(bio) ||
  229. !memcmp(s_type, "ufs", strlen("ufs"))))
  230. *data_unit = 1 << ICE_CRYPTO_DATA_UNIT_4_KB;
  231. if (which_pfe != INVALID_PFE) {
  232. /* Encrypted file; override ->bi_crypt_key */
  233. pr_debug("parsing inode %lu with PFE type %d\n",
  234. inode->i_ino, which_pfe);
  235. return (*(pfk_parse_inode_ftable[which_pfe]))
  236. (bio, inode, key_info, algo_mode, is_pfe,
  237. (const char *)s_type);
  238. }
  239. /*
  240. * bio is not for an encrypted file. Use ->bi_crypt_key if it was set.
  241. * Otherwise, don't encrypt/decrypt the bio.
  242. */
  243. #ifdef CONFIG_DM_DEFAULT_KEY
  244. key = bio->bi_crypt_key;
  245. #endif
  246. if (!key) {
  247. *is_pfe = false;
  248. return -EINVAL;
  249. }
  250. /* Note: the "salt" is really just the second half of the XTS key. */
  251. BUILD_BUG_ON(sizeof(key->raw) !=
  252. PFK_SUPPORTED_KEY_SIZE + PFK_SUPPORTED_SALT_SIZE);
  253. key_info->key = &key->raw[0];
  254. key_info->key_size = PFK_SUPPORTED_KEY_SIZE;
  255. key_info->salt = &key->raw[PFK_SUPPORTED_KEY_SIZE];
  256. key_info->salt_size = PFK_SUPPORTED_SALT_SIZE;
  257. if (algo_mode)
  258. *algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
  259. return 0;
  260. }
  261. /**
  262. * pfk_load_key_start() - loads PFE encryption key to the ICE
  263. * Can also be invoked from non
  264. * PFE context, in this case it
  265. * is not relevant and is_pfe
  266. * flag is set to false
  267. *
  268. * @bio: Pointer to the BIO structure
  269. * @ice_setting: Pointer to ice setting structure that will be filled with
  270. * ice configuration values, including the index to which the key was loaded
  271. * @is_pfe: will be false if inode is not relevant to PFE, in such a case
  272. * it should be treated as non PFE by the block layer
  273. *
  274. * Returns the index where the key is stored in encryption hw and additional
  275. * information that will be used later for configuration of the encryption hw.
  276. *
  277. * Must be followed by pfk_load_key_end when key is no longer used by ice
  278. *
  279. */
  280. int pfk_load_key_start(const struct bio *bio,
  281. struct ice_crypto_setting *ice_setting, bool *is_pfe,
  282. bool async)
  283. {
  284. int ret = 0;
  285. struct pfk_key_info key_info = {NULL, NULL, 0, 0};
  286. enum ice_cryto_algo_mode algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
  287. enum ice_crpto_key_size key_size_type = 0;
  288. unsigned int data_unit = 1 << ICE_CRYPTO_DATA_UNIT_512_B;
  289. u32 key_index = 0;
  290. if (!is_pfe) {
  291. pr_err("is_pfe is NULL\n");
  292. return -EINVAL;
  293. }
  294. /*
  295. * only a few errors below can indicate that
  296. * this function was not invoked within PFE context,
  297. * otherwise we will consider it PFE
  298. */
  299. *is_pfe = true;
  300. if (!pfk_is_ready())
  301. return -ENODEV;
  302. if (!ice_setting) {
  303. pr_err("ice setting is NULL\n");
  304. return -EINVAL;
  305. }
  306. ret = pfk_get_key_for_bio(bio, &key_info, &algo_mode, is_pfe,
  307. &data_unit);
  308. if (ret != 0)
  309. return ret;
  310. ret = pfk_key_size_to_key_type(key_info.key_size, &key_size_type);
  311. if (ret != 0)
  312. return ret;
  313. ret = pfk_kc_load_key_start(key_info.key, key_info.key_size,
  314. key_info.salt, key_info.salt_size, &key_index, async,
  315. data_unit);
  316. if (ret) {
  317. if (ret != -EBUSY && ret != -EAGAIN)
  318. pr_err("start: could not load key into pfk key cache, error %d\n",
  319. ret);
  320. return ret;
  321. }
  322. ice_setting->key_size = key_size_type;
  323. ice_setting->algo_mode = algo_mode;
  324. /* hardcoded for now */
  325. ice_setting->key_mode = ICE_CRYPTO_USE_LUT_SW_KEY;
  326. ice_setting->key_index = key_index;
  327. return 0;
  328. }
  329. /**
  330. * pfk_load_key_end() - marks the PFE key as no longer used by ICE
  331. * Can also be invoked from non
  332. * PFE context, in this case it is not
  333. * relevant and is_pfe flag is
  334. * set to false
  335. *
  336. * @bio: Pointer to the BIO structure
  337. * @is_pfe: Pointer to is_pfe flag, which will be true if function was invoked
  338. * from PFE context
  339. */
  340. int pfk_load_key_end(const struct bio *bio, bool *is_pfe)
  341. {
  342. int ret = 0;
  343. struct pfk_key_info key_info = {NULL, NULL, 0, 0};
  344. if (!is_pfe) {
  345. pr_err("is_pfe is NULL\n");
  346. return -EINVAL;
  347. }
  348. /* only a few errors below can indicate that
  349. * this function was not invoked within PFE context,
  350. * otherwise we will consider it PFE
  351. */
  352. *is_pfe = true;
  353. if (!pfk_is_ready())
  354. return -ENODEV;
  355. ret = pfk_get_key_for_bio(bio, &key_info, NULL, is_pfe, NULL);
  356. if (ret != 0)
  357. return ret;
  358. pfk_kc_load_key_end(key_info.key, key_info.key_size,
  359. key_info.salt, key_info.salt_size);
  360. return 0;
  361. }
  362. /**
  363. * pfk_allow_merge_bio() - Check if 2 BIOs can be merged.
  364. * @bio1: Pointer to first BIO structure.
  365. * @bio2: Pointer to second BIO structure.
  366. *
  367. * Prevent merging of BIOs from encrypted and non-encrypted
  368. * files, or files encrypted with different key.
  369. * Also prevent non encrypted and encrypted data from the same file
  370. * to be merged (ecryptfs header if stored inside file should be non
  371. * encrypted)
  372. * This API is called by the file system block layer.
  373. *
  374. * Return: true if the BIOs allowed to be merged, false
  375. * otherwise.
  376. */
  377. bool pfk_allow_merge_bio(const struct bio *bio1, const struct bio *bio2)
  378. {
  379. const struct blk_encryption_key *key1 = NULL;
  380. const struct blk_encryption_key *key2 = NULL;
  381. const struct inode *inode1;
  382. const struct inode *inode2;
  383. enum pfe_type which_pfe1;
  384. enum pfe_type which_pfe2;
  385. #ifdef CONFIG_DM_DEFAULT_KEY
  386. key1 = bio1->bi_crypt_key;
  387. key2 = bio2->bi_crypt_key;
  388. #endif
  389. if (!pfk_is_ready())
  390. return false;
  391. if (!bio1 || !bio2)
  392. return false;
  393. if (bio1 == bio2)
  394. return true;
  395. inode1 = pfk_bio_get_inode(bio1);
  396. inode2 = pfk_bio_get_inode(bio2);
  397. which_pfe1 = pfk_get_pfe_type(inode1);
  398. which_pfe2 = pfk_get_pfe_type(inode2);
  399. /*
  400. * If one bio is for an encrypted file and the other is for a different
  401. * type of encrypted file or for blocks that are not part of an
  402. * encrypted file, do not merge.
  403. */
  404. if (which_pfe1 != which_pfe2)
  405. return false;
  406. if (which_pfe1 != INVALID_PFE) {
  407. /* Both bios are for the same type of encrypted file. */
  408. return (*(pfk_allow_merge_bio_ftable[which_pfe1]))(bio1, bio2,
  409. inode1, inode2);
  410. }
  411. /*
  412. * Neither bio is for an encrypted file. Merge only if the default keys
  413. * are the same (or both are NULL).
  414. */
  415. return key1 == key2 ||
  416. (key1 && key2 &&
  417. !crypto_memneq(key1->raw, key2->raw, sizeof(key1->raw)));
  418. }
  419. /**
  420. * Flush key table on storage core reset. During core reset key configuration
  421. * is lost in ICE. We need to flash the cache, so that the keys will be
  422. * reconfigured again for every subsequent transaction
  423. */
  424. void pfk_clear_on_reset(void)
  425. {
  426. if (!pfk_is_ready())
  427. return;
  428. pfk_kc_clear_on_reset();
  429. }
  430. module_init(pfk_init);
  431. module_exit(pfk_exit);
  432. MODULE_LICENSE("GPL v2");
  433. MODULE_DESCRIPTION("Per-File-Key driver");