crypto_policy.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * linux/fs/ext4/crypto_policy.c
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains encryption policy functions for ext4
  7. *
  8. * Written by Michael Halcrow, 2015.
  9. */
  10. #include <linux/random.h>
  11. #include <linux/string.h>
  12. #include <linux/types.h>
  13. #include "ext4_jbd2.h"
  14. #include "ext4.h"
  15. #include "xattr.h"
  16. static int ext4_inode_has_encryption_context(struct inode *inode)
  17. {
  18. int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  19. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
  20. return (res > 0);
  21. }
  22. /*
  23. * check whether the policy is consistent with the encryption context
  24. * for the inode
  25. */
  26. static int ext4_is_encryption_context_consistent_with_policy(
  27. struct inode *inode, const struct ext4_encryption_policy *policy)
  28. {
  29. struct ext4_encryption_context ctx;
  30. int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  31. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  32. sizeof(ctx));
  33. if (res != sizeof(ctx))
  34. return 0;
  35. return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
  36. EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
  37. (ctx.flags ==
  38. policy->flags) &&
  39. (ctx.contents_encryption_mode ==
  40. policy->contents_encryption_mode) &&
  41. (ctx.filenames_encryption_mode ==
  42. policy->filenames_encryption_mode));
  43. }
  44. static int ext4_create_encryption_context_from_policy(
  45. struct inode *inode, const struct ext4_encryption_policy *policy)
  46. {
  47. struct ext4_encryption_context ctx;
  48. handle_t *handle;
  49. int res, res2;
  50. res = ext4_convert_inline_data(inode);
  51. if (res)
  52. return res;
  53. ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
  54. memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
  55. EXT4_KEY_DESCRIPTOR_SIZE);
  56. if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) {
  57. printk(KERN_WARNING
  58. "%s: Invalid contents encryption mode %d\n", __func__,
  59. policy->contents_encryption_mode);
  60. return -EINVAL;
  61. }
  62. if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
  63. printk(KERN_WARNING
  64. "%s: Invalid filenames encryption mode %d\n", __func__,
  65. policy->filenames_encryption_mode);
  66. return -EINVAL;
  67. }
  68. if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
  69. return -EINVAL;
  70. ctx.contents_encryption_mode = policy->contents_encryption_mode;
  71. ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
  72. ctx.flags = policy->flags;
  73. BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
  74. get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
  75. handle = ext4_journal_start(inode, EXT4_HT_MISC,
  76. ext4_jbd2_credits_xattr(inode));
  77. if (IS_ERR(handle))
  78. return PTR_ERR(handle);
  79. res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  80. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  81. sizeof(ctx), 0);
  82. if (!res) {
  83. ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
  84. res = ext4_mark_inode_dirty(handle, inode);
  85. if (res)
  86. EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
  87. }
  88. res2 = ext4_journal_stop(handle);
  89. if (!res)
  90. res = res2;
  91. return res;
  92. }
  93. int ext4_process_policy(const struct ext4_encryption_policy *policy,
  94. struct inode *inode)
  95. {
  96. if (policy->version != 0)
  97. return -EINVAL;
  98. if (!ext4_inode_has_encryption_context(inode)) {
  99. if (!S_ISDIR(inode->i_mode))
  100. return -EINVAL;
  101. if (!ext4_empty_dir(inode))
  102. return -ENOTEMPTY;
  103. return ext4_create_encryption_context_from_policy(inode,
  104. policy);
  105. }
  106. if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
  107. return 0;
  108. printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
  109. __func__);
  110. return -EINVAL;
  111. }
  112. int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
  113. {
  114. struct ext4_encryption_context ctx;
  115. int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  116. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  117. &ctx, sizeof(ctx));
  118. if (res != sizeof(ctx))
  119. return -ENOENT;
  120. if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
  121. return -EINVAL;
  122. policy->version = 0;
  123. policy->contents_encryption_mode = ctx.contents_encryption_mode;
  124. policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
  125. policy->flags = ctx.flags;
  126. memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
  127. EXT4_KEY_DESCRIPTOR_SIZE);
  128. return 0;
  129. }
  130. int ext4_is_child_context_consistent_with_parent(struct inode *parent,
  131. struct inode *child)
  132. {
  133. struct ext4_crypt_info *parent_ci, *child_ci;
  134. int res;
  135. if ((parent == NULL) || (child == NULL)) {
  136. pr_err("parent %p child %p\n", parent, child);
  137. BUG_ON(1);
  138. }
  139. /* no restrictions if the parent directory is not encrypted */
  140. if (!ext4_encrypted_inode(parent))
  141. return 1;
  142. /* if the child directory is not encrypted, this is always a problem */
  143. if (!ext4_encrypted_inode(child))
  144. return 0;
  145. res = ext4_get_encryption_info(parent);
  146. if (res)
  147. return 0;
  148. res = ext4_get_encryption_info(child);
  149. if (res)
  150. return 0;
  151. parent_ci = EXT4_I(parent)->i_crypt_info;
  152. child_ci = EXT4_I(child)->i_crypt_info;
  153. if (!parent_ci && !child_ci)
  154. return 1;
  155. if (!parent_ci || !child_ci)
  156. return 0;
  157. return (memcmp(parent_ci->ci_master_key,
  158. child_ci->ci_master_key,
  159. EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
  160. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  161. (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
  162. (parent_ci->ci_flags == child_ci->ci_flags));
  163. }
  164. /**
  165. * ext4_inherit_context() - Sets a child context from its parent
  166. * @parent: Parent inode from which the context is inherited.
  167. * @child: Child inode that inherits the context from @parent.
  168. *
  169. * Return: Zero on success, non-zero otherwise
  170. */
  171. int ext4_inherit_context(struct inode *parent, struct inode *child)
  172. {
  173. struct ext4_encryption_context ctx;
  174. struct ext4_crypt_info *ci;
  175. int res;
  176. res = ext4_get_encryption_info(parent);
  177. if (res < 0)
  178. return res;
  179. ci = EXT4_I(parent)->i_crypt_info;
  180. if (ci == NULL)
  181. return -ENOKEY;
  182. ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
  183. if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
  184. ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
  185. ctx.filenames_encryption_mode =
  186. EXT4_ENCRYPTION_MODE_AES_256_CTS;
  187. ctx.flags = 0;
  188. memset(ctx.master_key_descriptor, 0x42,
  189. EXT4_KEY_DESCRIPTOR_SIZE);
  190. res = 0;
  191. } else {
  192. ctx.contents_encryption_mode = ci->ci_data_mode;
  193. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  194. ctx.flags = ci->ci_flags;
  195. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  196. EXT4_KEY_DESCRIPTOR_SIZE);
  197. }
  198. get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
  199. res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
  200. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  201. sizeof(ctx), 0);
  202. if (!res) {
  203. ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
  204. ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA);
  205. res = ext4_get_encryption_info(child);
  206. }
  207. return res;
  208. }