crypto_policy.c 6.3 KB

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