crypto_policy.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 (!inode_owner_or_capable(inode))
  97. return -EACCES;
  98. if (policy->version != 0)
  99. return -EINVAL;
  100. if (!ext4_inode_has_encryption_context(inode)) {
  101. if (!S_ISDIR(inode->i_mode))
  102. return -EINVAL;
  103. if (!ext4_empty_dir(inode))
  104. return -ENOTEMPTY;
  105. return ext4_create_encryption_context_from_policy(inode,
  106. policy);
  107. }
  108. if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
  109. return 0;
  110. printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
  111. __func__);
  112. return -EINVAL;
  113. }
  114. int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
  115. {
  116. struct ext4_encryption_context ctx;
  117. int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  118. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  119. &ctx, sizeof(ctx));
  120. if (res != sizeof(ctx))
  121. return -ENOENT;
  122. if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
  123. return -EINVAL;
  124. policy->version = 0;
  125. policy->contents_encryption_mode = ctx.contents_encryption_mode;
  126. policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
  127. policy->flags = ctx.flags;
  128. memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
  129. EXT4_KEY_DESCRIPTOR_SIZE);
  130. return 0;
  131. }
  132. int ext4_is_child_context_consistent_with_parent(struct inode *parent,
  133. struct inode *child)
  134. {
  135. const struct ext4_crypt_info *parent_ci, *child_ci;
  136. struct ext4_encryption_context parent_ctx, child_ctx;
  137. int res;
  138. /* No restrictions on file types which are never encrypted */
  139. if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
  140. !S_ISLNK(child->i_mode))
  141. return 1;
  142. /* No restrictions if the parent directory is unencrypted */
  143. if (!ext4_encrypted_inode(parent))
  144. return 1;
  145. /* Encrypted directories must not contain unencrypted files */
  146. if (!ext4_encrypted_inode(child))
  147. return 0;
  148. /*
  149. * Both parent and child are encrypted, so verify they use the same
  150. * encryption policy. Compare the fscrypt_info structs if the keys are
  151. * available, otherwise retrieve and compare the fscrypt_contexts.
  152. *
  153. * Note that the fscrypt_context retrieval will be required frequently
  154. * when accessing an encrypted directory tree without the key.
  155. * Performance-wise this is not a big deal because we already don't
  156. * really optimize for file access without the key (to the extent that
  157. * such access is even possible), given that any attempted access
  158. * already causes a fscrypt_context retrieval and keyring search.
  159. *
  160. * In any case, if an unexpected error occurs, fall back to "forbidden".
  161. */
  162. res = ext4_get_encryption_info(parent);
  163. if (res)
  164. return 0;
  165. res = ext4_get_encryption_info(child);
  166. if (res)
  167. return 0;
  168. parent_ci = EXT4_I(parent)->i_crypt_info;
  169. child_ci = EXT4_I(child)->i_crypt_info;
  170. if (parent_ci && child_ci) {
  171. return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key,
  172. EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
  173. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  174. (parent_ci->ci_filename_mode ==
  175. child_ci->ci_filename_mode) &&
  176. (parent_ci->ci_flags == child_ci->ci_flags);
  177. }
  178. res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
  179. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  180. &parent_ctx, sizeof(parent_ctx));
  181. if (res != sizeof(parent_ctx))
  182. return 0;
  183. res = ext4_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION,
  184. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  185. &child_ctx, sizeof(child_ctx));
  186. if (res != sizeof(child_ctx))
  187. return 0;
  188. return memcmp(parent_ctx.master_key_descriptor,
  189. child_ctx.master_key_descriptor,
  190. EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
  191. (parent_ctx.contents_encryption_mode ==
  192. child_ctx.contents_encryption_mode) &&
  193. (parent_ctx.filenames_encryption_mode ==
  194. child_ctx.filenames_encryption_mode) &&
  195. (parent_ctx.flags == child_ctx.flags);
  196. }
  197. /**
  198. * ext4_inherit_context() - Sets a child context from its parent
  199. * @parent: Parent inode from which the context is inherited.
  200. * @child: Child inode that inherits the context from @parent.
  201. *
  202. * Return: Zero on success, non-zero otherwise
  203. */
  204. int ext4_inherit_context(struct inode *parent, struct inode *child)
  205. {
  206. struct ext4_encryption_context ctx;
  207. struct ext4_crypt_info *ci;
  208. int res;
  209. res = ext4_get_encryption_info(parent);
  210. if (res < 0)
  211. return res;
  212. ci = EXT4_I(parent)->i_crypt_info;
  213. if (ci == NULL)
  214. return -ENOKEY;
  215. ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
  216. if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
  217. ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
  218. ctx.filenames_encryption_mode =
  219. EXT4_ENCRYPTION_MODE_AES_256_CTS;
  220. ctx.flags = 0;
  221. memset(ctx.master_key_descriptor, 0x42,
  222. EXT4_KEY_DESCRIPTOR_SIZE);
  223. res = 0;
  224. } else {
  225. ctx.contents_encryption_mode = ci->ci_data_mode;
  226. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  227. ctx.flags = ci->ci_flags;
  228. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  229. EXT4_KEY_DESCRIPTOR_SIZE);
  230. }
  231. get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
  232. res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
  233. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  234. sizeof(ctx), 0);
  235. if (!res) {
  236. ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
  237. ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA);
  238. res = ext4_get_encryption_info(child);
  239. }
  240. return res;
  241. }