crypto_policy.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 (!inode_owner_or_capable(inode))
  80. return -EACCES;
  81. if (policy->version != 0)
  82. return -EINVAL;
  83. if (!S_ISDIR(inode->i_mode))
  84. return -EINVAL;
  85. if (!f2fs_inode_has_encryption_context(inode)) {
  86. if (!f2fs_empty_dir(inode))
  87. return -ENOTEMPTY;
  88. return f2fs_create_encryption_context_from_policy(inode,
  89. policy);
  90. }
  91. if (f2fs_is_encryption_context_consistent_with_policy(inode, policy))
  92. return 0;
  93. printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
  94. __func__);
  95. return -EINVAL;
  96. }
  97. int f2fs_get_policy(struct inode *inode, struct f2fs_encryption_policy *policy)
  98. {
  99. struct f2fs_encryption_context ctx;
  100. int res;
  101. if (!f2fs_encrypted_inode(inode))
  102. return -ENODATA;
  103. res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  104. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  105. &ctx, sizeof(ctx), NULL);
  106. if (res != sizeof(ctx))
  107. return -ENODATA;
  108. if (ctx.format != F2FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  109. return -EINVAL;
  110. policy->version = 0;
  111. policy->contents_encryption_mode = ctx.contents_encryption_mode;
  112. policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
  113. policy->flags = ctx.flags;
  114. memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
  115. F2FS_KEY_DESCRIPTOR_SIZE);
  116. return 0;
  117. }
  118. int f2fs_is_child_context_consistent_with_parent(struct inode *parent,
  119. struct inode *child)
  120. {
  121. const struct f2fs_crypt_info *parent_ci, *child_ci;
  122. struct f2fs_encryption_context parent_ctx, child_ctx;
  123. int res;
  124. /* No restrictions on file types which are never encrypted */
  125. if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
  126. !S_ISLNK(child->i_mode))
  127. return 1;
  128. /* No restrictions if the parent directory is unencrypted */
  129. if (!f2fs_encrypted_inode(parent))
  130. return 1;
  131. /* Encrypted directories must not contain unencrypted files */
  132. if (!f2fs_encrypted_inode(child))
  133. return 0;
  134. /*
  135. * Both parent and child are encrypted, so verify they use the same
  136. * encryption policy. Compare the fscrypt_info structs if the keys are
  137. * available, otherwise retrieve and compare the fscrypt_contexts.
  138. *
  139. * Note that the fscrypt_context retrieval will be required frequently
  140. * when accessing an encrypted directory tree without the key.
  141. * Performance-wise this is not a big deal because we already don't
  142. * really optimize for file access without the key (to the extent that
  143. * such access is even possible), given that any attempted access
  144. * already causes a fscrypt_context retrieval and keyring search.
  145. *
  146. * In any case, if an unexpected error occurs, fall back to "forbidden".
  147. */
  148. res = f2fs_get_encryption_info(parent);
  149. if (res)
  150. return 0;
  151. res = f2fs_get_encryption_info(child);
  152. if (res)
  153. return 0;
  154. parent_ci = F2FS_I(parent)->i_crypt_info;
  155. child_ci = F2FS_I(child)->i_crypt_info;
  156. if (parent_ci && child_ci) {
  157. return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key,
  158. F2FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  159. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  160. (parent_ci->ci_filename_mode ==
  161. child_ci->ci_filename_mode) &&
  162. (parent_ci->ci_flags == child_ci->ci_flags);
  163. }
  164. res = f2fs_getxattr(parent, F2FS_XATTR_INDEX_ENCRYPTION,
  165. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  166. &parent_ctx, sizeof(parent_ctx), NULL);
  167. if (res != sizeof(parent_ctx))
  168. return 0;
  169. res = f2fs_getxattr(child, F2FS_XATTR_INDEX_ENCRYPTION,
  170. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  171. &child_ctx, sizeof(child_ctx), NULL);
  172. if (res != sizeof(child_ctx))
  173. return 0;
  174. return memcmp(parent_ctx.master_key_descriptor,
  175. child_ctx.master_key_descriptor,
  176. F2FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  177. (parent_ctx.contents_encryption_mode ==
  178. child_ctx.contents_encryption_mode) &&
  179. (parent_ctx.filenames_encryption_mode ==
  180. child_ctx.filenames_encryption_mode) &&
  181. (parent_ctx.flags == child_ctx.flags);
  182. }
  183. /**
  184. * f2fs_inherit_context() - Sets a child context from its parent
  185. * @parent: Parent inode from which the context is inherited.
  186. * @child: Child inode that inherits the context from @parent.
  187. *
  188. * Return: Zero on success, non-zero otherwise
  189. */
  190. int f2fs_inherit_context(struct inode *parent, struct inode *child,
  191. struct page *ipage)
  192. {
  193. struct f2fs_encryption_context ctx;
  194. struct f2fs_crypt_info *ci;
  195. int res;
  196. res = f2fs_get_encryption_info(parent);
  197. if (res < 0)
  198. return res;
  199. ci = F2FS_I(parent)->i_crypt_info;
  200. BUG_ON(ci == NULL);
  201. ctx.format = F2FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  202. ctx.contents_encryption_mode = ci->ci_data_mode;
  203. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  204. ctx.flags = ci->ci_flags;
  205. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  206. F2FS_KEY_DESCRIPTOR_SIZE);
  207. get_random_bytes(ctx.nonce, F2FS_KEY_DERIVATION_NONCE_SIZE);
  208. return f2fs_setxattr(child, F2FS_XATTR_INDEX_ENCRYPTION,
  209. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
  210. sizeof(ctx), ipage, XATTR_CREATE);
  211. }