policy.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 (!inode_owner_or_capable(inode))
  83. return -EACCES;
  84. if (policy->version != 0)
  85. return -EINVAL;
  86. if (!inode_has_encryption_context(inode)) {
  87. if (!inode->i_sb->s_cop->empty_dir)
  88. return -EOPNOTSUPP;
  89. if (!inode->i_sb->s_cop->empty_dir(inode))
  90. return -ENOTEMPTY;
  91. return create_encryption_context_from_policy(inode, policy);
  92. }
  93. if (is_encryption_context_consistent_with_policy(inode, policy))
  94. return 0;
  95. printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
  96. __func__);
  97. return -EINVAL;
  98. }
  99. EXPORT_SYMBOL(fscrypt_process_policy);
  100. int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy)
  101. {
  102. struct fscrypt_context ctx;
  103. int res;
  104. if (!inode->i_sb->s_cop->get_context ||
  105. !inode->i_sb->s_cop->is_encrypted(inode))
  106. return -ENODATA;
  107. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  108. if (res != sizeof(ctx))
  109. return -ENODATA;
  110. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  111. return -EINVAL;
  112. policy->version = 0;
  113. policy->contents_encryption_mode = ctx.contents_encryption_mode;
  114. policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
  115. policy->flags = ctx.flags;
  116. memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
  117. FS_KEY_DESCRIPTOR_SIZE);
  118. return 0;
  119. }
  120. EXPORT_SYMBOL(fscrypt_get_policy);
  121. int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
  122. {
  123. struct fscrypt_info *parent_ci, *child_ci;
  124. int res;
  125. if ((parent == NULL) || (child == NULL)) {
  126. printk(KERN_ERR "parent %p child %p\n", parent, child);
  127. BUG_ON(1);
  128. }
  129. /* no restrictions if the parent directory is not encrypted */
  130. if (!parent->i_sb->s_cop->is_encrypted(parent))
  131. return 1;
  132. /* if the child directory is not encrypted, this is always a problem */
  133. if (!parent->i_sb->s_cop->is_encrypted(child))
  134. return 0;
  135. res = fscrypt_get_encryption_info(parent);
  136. if (res)
  137. return 0;
  138. res = fscrypt_get_encryption_info(child);
  139. if (res)
  140. return 0;
  141. parent_ci = parent->i_crypt_info;
  142. child_ci = child->i_crypt_info;
  143. if (!parent_ci && !child_ci)
  144. return 1;
  145. if (!parent_ci || !child_ci)
  146. return 0;
  147. return (memcmp(parent_ci->ci_master_key,
  148. child_ci->ci_master_key,
  149. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  150. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  151. (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
  152. (parent_ci->ci_flags == child_ci->ci_flags));
  153. }
  154. EXPORT_SYMBOL(fscrypt_has_permitted_context);
  155. /**
  156. * fscrypt_inherit_context() - Sets a child context from its parent
  157. * @parent: Parent inode from which the context is inherited.
  158. * @child: Child inode that inherits the context from @parent.
  159. * @fs_data: private data given by FS.
  160. * @preload: preload child i_crypt_info
  161. *
  162. * Return: Zero on success, non-zero otherwise
  163. */
  164. int fscrypt_inherit_context(struct inode *parent, struct inode *child,
  165. void *fs_data, bool preload)
  166. {
  167. struct fscrypt_context ctx;
  168. struct fscrypt_info *ci;
  169. int res;
  170. if (!parent->i_sb->s_cop->set_context)
  171. return -EOPNOTSUPP;
  172. res = fscrypt_get_encryption_info(parent);
  173. if (res < 0)
  174. return res;
  175. ci = parent->i_crypt_info;
  176. if (ci == NULL)
  177. return -ENOKEY;
  178. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  179. if (fscrypt_dummy_context_enabled(parent)) {
  180. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  181. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  182. ctx.flags = 0;
  183. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  184. res = 0;
  185. } else {
  186. ctx.contents_encryption_mode = ci->ci_data_mode;
  187. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  188. ctx.flags = ci->ci_flags;
  189. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  190. FS_KEY_DESCRIPTOR_SIZE);
  191. }
  192. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  193. res = parent->i_sb->s_cop->set_context(child, &ctx,
  194. sizeof(ctx), fs_data);
  195. if (res)
  196. return res;
  197. return preload ? fscrypt_get_encryption_info(child): 0;
  198. }
  199. EXPORT_SYMBOL(fscrypt_inherit_context);