crypto_policy.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
  49. memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
  50. EXT4_KEY_DESCRIPTOR_SIZE);
  51. if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) {
  52. printk(KERN_WARNING
  53. "%s: Invalid contents encryption mode %d\n", __func__,
  54. policy->contents_encryption_mode);
  55. return -EINVAL;
  56. }
  57. if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
  58. printk(KERN_WARNING
  59. "%s: Invalid filenames encryption mode %d\n", __func__,
  60. policy->filenames_encryption_mode);
  61. return -EINVAL;
  62. }
  63. if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
  64. return -EINVAL;
  65. ctx.contents_encryption_mode = policy->contents_encryption_mode;
  66. ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
  67. ctx.flags = policy->flags;
  68. BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
  69. get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
  70. res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  71. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  72. sizeof(ctx), 0);
  73. if (!res)
  74. ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
  75. return res;
  76. }
  77. int ext4_process_policy(const struct ext4_encryption_policy *policy,
  78. struct inode *inode)
  79. {
  80. if (policy->version != 0)
  81. return -EINVAL;
  82. if (!ext4_inode_has_encryption_context(inode)) {
  83. if (!ext4_empty_dir(inode))
  84. return -ENOTEMPTY;
  85. return ext4_create_encryption_context_from_policy(inode,
  86. policy);
  87. }
  88. if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
  89. return 0;
  90. printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
  91. __func__);
  92. return -EINVAL;
  93. }
  94. int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
  95. {
  96. struct ext4_encryption_context ctx;
  97. int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  98. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  99. &ctx, sizeof(ctx));
  100. if (res != sizeof(ctx))
  101. return -ENOENT;
  102. if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
  103. return -EINVAL;
  104. policy->version = 0;
  105. policy->contents_encryption_mode = ctx.contents_encryption_mode;
  106. policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
  107. policy->flags = ctx.flags;
  108. memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
  109. EXT4_KEY_DESCRIPTOR_SIZE);
  110. return 0;
  111. }
  112. int ext4_is_child_context_consistent_with_parent(struct inode *parent,
  113. struct inode *child)
  114. {
  115. struct ext4_encryption_context parent_ctx, child_ctx;
  116. int res;
  117. if ((parent == NULL) || (child == NULL)) {
  118. pr_err("parent %p child %p\n", parent, child);
  119. BUG_ON(1);
  120. }
  121. /* no restrictions if the parent directory is not encrypted */
  122. if (!ext4_encrypted_inode(parent))
  123. return 1;
  124. res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
  125. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  126. &parent_ctx, sizeof(parent_ctx));
  127. if (res != sizeof(parent_ctx))
  128. return 0;
  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_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION,
  133. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  134. &child_ctx, sizeof(child_ctx));
  135. if (res != sizeof(child_ctx))
  136. return 0;
  137. return (memcmp(parent_ctx.master_key_descriptor,
  138. child_ctx.master_key_descriptor,
  139. EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
  140. (parent_ctx.contents_encryption_mode ==
  141. child_ctx.contents_encryption_mode) &&
  142. (parent_ctx.filenames_encryption_mode ==
  143. child_ctx.filenames_encryption_mode));
  144. }
  145. /**
  146. * ext4_inherit_context() - Sets a child context from its parent
  147. * @parent: Parent inode from which the context is inherited.
  148. * @child: Child inode that inherits the context from @parent.
  149. *
  150. * Return: Zero on success, non-zero otherwise
  151. */
  152. int ext4_inherit_context(struct inode *parent, struct inode *child)
  153. {
  154. struct ext4_encryption_context ctx;
  155. int res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
  156. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  157. &ctx, sizeof(ctx));
  158. if (res != sizeof(ctx)) {
  159. if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
  160. ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
  161. ctx.contents_encryption_mode =
  162. EXT4_ENCRYPTION_MODE_AES_256_XTS;
  163. ctx.filenames_encryption_mode =
  164. EXT4_ENCRYPTION_MODE_AES_256_CTS;
  165. ctx.flags = 0;
  166. memset(ctx.master_key_descriptor, 0x42,
  167. EXT4_KEY_DESCRIPTOR_SIZE);
  168. res = 0;
  169. } else {
  170. goto out;
  171. }
  172. }
  173. get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
  174. res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
  175. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  176. sizeof(ctx), 0);
  177. out:
  178. if (!res)
  179. ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
  180. return res;
  181. }