crypto_policy.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * copied from linux/fs/ext4/crypto_policy.c
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. * Copyright (C) 2015, Motorola Mobility.
  6. *
  7. * This contains encryption policy functions for f2fs with some modifications
  8. * to support f2fs-specific xattr APIs.
  9. *
  10. * Written by Michael Halcrow, 2015.
  11. * Modified by Jaegeuk Kim, 2015.
  12. */
  13. #include <linux/random.h>
  14. #include <linux/string.h>
  15. #include <linux/types.h>
  16. #include <linux/f2fs_fs.h>
  17. #include "f2fs.h"
  18. #include "xattr.h"
  19. static int f2fs_inode_has_encryption_context(struct inode *inode)
  20. {
  21. int res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  22. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0, NULL);
  23. return (res > 0);
  24. }
  25. /*
  26. * check whether the policy is consistent with the encryption context
  27. * for the inode
  28. */
  29. static int f2fs_is_encryption_context_consistent_with_policy(
  30. struct inode *inode, const struct f2fs_encryption_policy *policy)
  31. {
  32. struct f2fs_encryption_context ctx;
  33. int res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  34. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  35. sizeof(ctx), NULL);
  36. if (res != sizeof(ctx))
  37. return 0;
  38. return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
  39. F2FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  40. (ctx.flags == policy->flags) &&
  41. (ctx.contents_encryption_mode ==
  42. policy->contents_encryption_mode) &&
  43. (ctx.filenames_encryption_mode ==
  44. policy->filenames_encryption_mode));
  45. }
  46. static int f2fs_create_encryption_context_from_policy(
  47. struct inode *inode, const struct f2fs_encryption_policy *policy)
  48. {
  49. struct f2fs_encryption_context ctx;
  50. ctx.format = F2FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  51. memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
  52. F2FS_KEY_DESCRIPTOR_SIZE);
  53. if (!f2fs_valid_contents_enc_mode(policy->contents_encryption_mode)) {
  54. printk(KERN_WARNING
  55. "%s: Invalid contents encryption mode %d\n", __func__,
  56. policy->contents_encryption_mode);
  57. return -EINVAL;
  58. }
  59. if (!f2fs_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
  60. printk(KERN_WARNING
  61. "%s: Invalid filenames encryption mode %d\n", __func__,
  62. policy->filenames_encryption_mode);
  63. return -EINVAL;
  64. }
  65. if (policy->flags & ~F2FS_POLICY_FLAGS_VALID)
  66. return -EINVAL;
  67. ctx.contents_encryption_mode = policy->contents_encryption_mode;
  68. ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
  69. ctx.flags = policy->flags;
  70. BUILD_BUG_ON(sizeof(ctx.nonce) != F2FS_KEY_DERIVATION_NONCE_SIZE);
  71. get_random_bytes(ctx.nonce, F2FS_KEY_DERIVATION_NONCE_SIZE);
  72. return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  73. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  74. sizeof(ctx), NULL, XATTR_CREATE);
  75. }
  76. int f2fs_process_policy(const struct f2fs_encryption_policy *policy,
  77. struct inode *inode)
  78. {
  79. if (policy->version != 0)
  80. return -EINVAL;
  81. if (!S_ISDIR(inode->i_mode))
  82. return -EINVAL;
  83. if (!f2fs_inode_has_encryption_context(inode)) {
  84. if (!f2fs_empty_dir(inode))
  85. return -ENOTEMPTY;
  86. return f2fs_create_encryption_context_from_policy(inode,
  87. policy);
  88. }
  89. if (f2fs_is_encryption_context_consistent_with_policy(inode, policy))
  90. return 0;
  91. printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
  92. __func__);
  93. return -EINVAL;
  94. }
  95. int f2fs_get_policy(struct inode *inode, struct f2fs_encryption_policy *policy)
  96. {
  97. struct f2fs_encryption_context ctx;
  98. int res;
  99. if (!f2fs_encrypted_inode(inode))
  100. return -ENODATA;
  101. res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  102. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  103. &ctx, sizeof(ctx), NULL);
  104. if (res != sizeof(ctx))
  105. return -ENODATA;
  106. if (ctx.format != F2FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  107. return -EINVAL;
  108. policy->version = 0;
  109. policy->contents_encryption_mode = ctx.contents_encryption_mode;
  110. policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
  111. policy->flags = ctx.flags;
  112. memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
  113. F2FS_KEY_DESCRIPTOR_SIZE);
  114. return 0;
  115. }
  116. int f2fs_is_child_context_consistent_with_parent(struct inode *parent,
  117. struct inode *child)
  118. {
  119. struct f2fs_crypt_info *parent_ci, *child_ci;
  120. int res;
  121. if ((parent == NULL) || (child == NULL)) {
  122. pr_err("parent %p child %p\n", parent, child);
  123. BUG_ON(1);
  124. }
  125. /* no restrictions if the parent directory is not encrypted */
  126. if (!f2fs_encrypted_inode(parent))
  127. return 1;
  128. /* if the child directory is not encrypted, this is always a problem */
  129. if (!f2fs_encrypted_inode(child))
  130. return 0;
  131. res = f2fs_get_encryption_info(parent);
  132. if (res)
  133. return 0;
  134. res = f2fs_get_encryption_info(child);
  135. if (res)
  136. return 0;
  137. parent_ci = F2FS_I(parent)->i_crypt_info;
  138. child_ci = F2FS_I(child)->i_crypt_info;
  139. if (!parent_ci && !child_ci)
  140. return 1;
  141. if (!parent_ci || !child_ci)
  142. return 0;
  143. return (memcmp(parent_ci->ci_master_key,
  144. child_ci->ci_master_key,
  145. F2FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  146. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  147. (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
  148. (parent_ci->ci_flags == child_ci->ci_flags));
  149. }
  150. /**
  151. * f2fs_inherit_context() - Sets a child context from its parent
  152. * @parent: Parent inode from which the context is inherited.
  153. * @child: Child inode that inherits the context from @parent.
  154. *
  155. * Return: Zero on success, non-zero otherwise
  156. */
  157. int f2fs_inherit_context(struct inode *parent, struct inode *child,
  158. struct page *ipage)
  159. {
  160. struct f2fs_encryption_context ctx;
  161. struct f2fs_crypt_info *ci;
  162. int res;
  163. res = f2fs_get_encryption_info(parent);
  164. if (res < 0)
  165. return res;
  166. ci = F2FS_I(parent)->i_crypt_info;
  167. BUG_ON(ci == NULL);
  168. ctx.format = F2FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  169. ctx.contents_encryption_mode = ci->ci_data_mode;
  170. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  171. ctx.flags = ci->ci_flags;
  172. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  173. F2FS_KEY_DESCRIPTOR_SIZE);
  174. get_random_bytes(ctx.nonce, F2FS_KEY_DERIVATION_NONCE_SIZE);
  175. return f2fs_setxattr(child, F2FS_XATTR_INDEX_ENCRYPTION,
  176. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  177. sizeof(ctx), ipage, XATTR_CREATE);
  178. }