policy.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Encryption policy functions for per-file encryption support.
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. * Copyright (C) 2015, Motorola Mobility.
  6. *
  7. * Written by Michael Halcrow, 2015.
  8. * Modified by Jaegeuk Kim, 2015.
  9. */
  10. #include <linux/random.h>
  11. #include <linux/string.h>
  12. #include <linux/fscrypto.h>
  13. static int inode_has_encryption_context(struct inode *inode)
  14. {
  15. if (!inode->i_sb->s_cop->get_context)
  16. return 0;
  17. return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0);
  18. }
  19. /*
  20. * check whether the policy is consistent with the encryption context
  21. * for the inode
  22. */
  23. static int is_encryption_context_consistent_with_policy(struct inode *inode,
  24. const struct fscrypt_policy *policy)
  25. {
  26. struct fscrypt_context ctx;
  27. int res;
  28. if (!inode->i_sb->s_cop->get_context)
  29. return 0;
  30. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  31. if (res != sizeof(ctx))
  32. return 0;
  33. return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
  34. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  35. (ctx.flags == policy->flags) &&
  36. (ctx.contents_encryption_mode ==
  37. policy->contents_encryption_mode) &&
  38. (ctx.filenames_encryption_mode ==
  39. policy->filenames_encryption_mode));
  40. }
  41. static int create_encryption_context_from_policy(struct inode *inode,
  42. const struct fscrypt_policy *policy)
  43. {
  44. struct fscrypt_context ctx;
  45. int res;
  46. if (!inode->i_sb->s_cop->set_context)
  47. return -EOPNOTSUPP;
  48. if (inode->i_sb->s_cop->prepare_context) {
  49. res = inode->i_sb->s_cop->prepare_context(inode);
  50. if (res)
  51. return res;
  52. }
  53. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  54. memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
  55. FS_KEY_DESCRIPTOR_SIZE);
  56. if (!fscrypt_valid_contents_enc_mode(
  57. policy->contents_encryption_mode)) {
  58. printk(KERN_WARNING
  59. "%s: Invalid contents encryption mode %d\n", __func__,
  60. policy->contents_encryption_mode);
  61. return -EINVAL;
  62. }
  63. if (!fscrypt_valid_filenames_enc_mode(
  64. policy->filenames_encryption_mode)) {
  65. printk(KERN_WARNING
  66. "%s: Invalid filenames encryption mode %d\n", __func__,
  67. policy->filenames_encryption_mode);
  68. return -EINVAL;
  69. }
  70. if (policy->flags & ~FS_POLICY_FLAGS_VALID)
  71. return -EINVAL;
  72. ctx.contents_encryption_mode = policy->contents_encryption_mode;
  73. ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
  74. ctx.flags = policy->flags;
  75. BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
  76. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  77. return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
  78. }
  79. int fscrypt_process_policy(struct inode *inode,
  80. const struct fscrypt_policy *policy)
  81. {
  82. if (policy->version != 0)
  83. return -EINVAL;
  84. if (!inode_has_encryption_context(inode)) {
  85. if (!inode->i_sb->s_cop->empty_dir)
  86. return -EOPNOTSUPP;
  87. if (!inode->i_sb->s_cop->empty_dir(inode))
  88. return -ENOTEMPTY;
  89. return create_encryption_context_from_policy(inode, policy);
  90. }
  91. if (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. EXPORT_SYMBOL(fscrypt_process_policy);
  98. int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy)
  99. {
  100. struct fscrypt_context ctx;
  101. int res;
  102. if (!inode->i_sb->s_cop->get_context ||
  103. !inode->i_sb->s_cop->is_encrypted(inode))
  104. return -ENODATA;
  105. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  106. if (res != sizeof(ctx))
  107. return -ENODATA;
  108. if (ctx.format != FS_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. FS_KEY_DESCRIPTOR_SIZE);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(fscrypt_get_policy);
  119. int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
  120. {
  121. struct fscrypt_info *parent_ci, *child_ci;
  122. int res;
  123. if ((parent == NULL) || (child == NULL)) {
  124. printk(KERN_ERR "parent %p child %p\n", parent, child);
  125. BUG_ON(1);
  126. }
  127. /* no restrictions if the parent directory is not encrypted */
  128. if (!parent->i_sb->s_cop->is_encrypted(parent))
  129. return 1;
  130. /* if the child directory is not encrypted, this is always a problem */
  131. if (!parent->i_sb->s_cop->is_encrypted(child))
  132. return 0;
  133. res = fscrypt_get_encryption_info(parent);
  134. if (res)
  135. return 0;
  136. res = fscrypt_get_encryption_info(child);
  137. if (res)
  138. return 0;
  139. parent_ci = parent->i_crypt_info;
  140. child_ci = child->i_crypt_info;
  141. if (!parent_ci && !child_ci)
  142. return 1;
  143. if (!parent_ci || !child_ci)
  144. return 0;
  145. return (memcmp(parent_ci->ci_master_key,
  146. child_ci->ci_master_key,
  147. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  148. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  149. (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
  150. (parent_ci->ci_flags == child_ci->ci_flags));
  151. }
  152. EXPORT_SYMBOL(fscrypt_has_permitted_context);
  153. /**
  154. * fscrypt_inherit_context() - Sets a child context from its parent
  155. * @parent: Parent inode from which the context is inherited.
  156. * @child: Child inode that inherits the context from @parent.
  157. * @fs_data: private data given by FS.
  158. * @preload: preload child i_crypt_info
  159. *
  160. * Return: Zero on success, non-zero otherwise
  161. */
  162. int fscrypt_inherit_context(struct inode *parent, struct inode *child,
  163. void *fs_data, bool preload)
  164. {
  165. struct fscrypt_context ctx;
  166. struct fscrypt_info *ci;
  167. int res;
  168. if (!parent->i_sb->s_cop->set_context)
  169. return -EOPNOTSUPP;
  170. res = fscrypt_get_encryption_info(parent);
  171. if (res < 0)
  172. return res;
  173. ci = parent->i_crypt_info;
  174. if (ci == NULL)
  175. return -ENOKEY;
  176. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  177. if (fscrypt_dummy_context_enabled(parent)) {
  178. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  179. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  180. ctx.flags = 0;
  181. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  182. res = 0;
  183. } else {
  184. ctx.contents_encryption_mode = ci->ci_data_mode;
  185. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  186. ctx.flags = ci->ci_flags;
  187. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  188. FS_KEY_DESCRIPTOR_SIZE);
  189. }
  190. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  191. res = parent->i_sb->s_cop->set_context(child, &ctx,
  192. sizeof(ctx), fs_data);
  193. if (res)
  194. return res;
  195. return preload ? fscrypt_get_encryption_info(child): 0;
  196. }
  197. EXPORT_SYMBOL(fscrypt_inherit_context);