policy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/mount.h>
  13. #include "fscrypt_private.h"
  14. static int inode_has_encryption_context(struct inode *inode)
  15. {
  16. if (!inode->i_sb->s_cop->get_context)
  17. return 0;
  18. return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0);
  19. }
  20. /*
  21. * check whether the policy is consistent with the encryption context
  22. * for the inode
  23. */
  24. static int is_encryption_context_consistent_with_policy(struct inode *inode,
  25. const struct fscrypt_policy *policy)
  26. {
  27. struct fscrypt_context ctx;
  28. int res;
  29. if (!inode->i_sb->s_cop->get_context)
  30. return 0;
  31. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  32. if (res != sizeof(ctx))
  33. return 0;
  34. return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
  35. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  36. (ctx.flags == policy->flags) &&
  37. (ctx.contents_encryption_mode ==
  38. policy->contents_encryption_mode) &&
  39. (ctx.filenames_encryption_mode ==
  40. policy->filenames_encryption_mode));
  41. }
  42. static int create_encryption_context_from_policy(struct inode *inode,
  43. const struct fscrypt_policy *policy)
  44. {
  45. struct fscrypt_context ctx;
  46. int res;
  47. if (!inode->i_sb->s_cop->set_context)
  48. return -EOPNOTSUPP;
  49. if (inode->i_sb->s_cop->prepare_context) {
  50. res = inode->i_sb->s_cop->prepare_context(inode);
  51. if (res)
  52. return res;
  53. }
  54. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  55. memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
  56. FS_KEY_DESCRIPTOR_SIZE);
  57. if (!fscrypt_valid_contents_enc_mode(
  58. policy->contents_encryption_mode)) {
  59. printk(KERN_WARNING
  60. "%s: Invalid contents encryption mode %d\n", __func__,
  61. policy->contents_encryption_mode);
  62. return -EINVAL;
  63. }
  64. if (!fscrypt_valid_filenames_enc_mode(
  65. policy->filenames_encryption_mode)) {
  66. printk(KERN_WARNING
  67. "%s: Invalid filenames encryption mode %d\n", __func__,
  68. policy->filenames_encryption_mode);
  69. return -EINVAL;
  70. }
  71. if (policy->flags & ~FS_POLICY_FLAGS_VALID)
  72. return -EINVAL;
  73. ctx.contents_encryption_mode = policy->contents_encryption_mode;
  74. ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
  75. ctx.flags = policy->flags;
  76. BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
  77. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  78. return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
  79. }
  80. int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
  81. {
  82. struct fscrypt_policy policy;
  83. struct inode *inode = file_inode(filp);
  84. int ret;
  85. if (copy_from_user(&policy, arg, sizeof(policy)))
  86. return -EFAULT;
  87. if (!inode_owner_or_capable(inode))
  88. return -EACCES;
  89. if (policy.version != 0)
  90. return -EINVAL;
  91. ret = mnt_want_write_file(filp);
  92. if (ret)
  93. return ret;
  94. inode_lock(inode);
  95. if (!inode_has_encryption_context(inode)) {
  96. if (!S_ISDIR(inode->i_mode))
  97. ret = -EINVAL;
  98. else if (!inode->i_sb->s_cop->empty_dir)
  99. ret = -EOPNOTSUPP;
  100. else if (!inode->i_sb->s_cop->empty_dir(inode))
  101. ret = -ENOTEMPTY;
  102. else
  103. ret = create_encryption_context_from_policy(inode,
  104. &policy);
  105. } else if (!is_encryption_context_consistent_with_policy(inode,
  106. &policy)) {
  107. printk(KERN_WARNING
  108. "%s: Policy inconsistent with encryption context\n",
  109. __func__);
  110. ret = -EINVAL;
  111. }
  112. inode_unlock(inode);
  113. mnt_drop_write_file(filp);
  114. return ret;
  115. }
  116. EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
  117. int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
  118. {
  119. struct inode *inode = file_inode(filp);
  120. struct fscrypt_context ctx;
  121. struct fscrypt_policy policy;
  122. int res;
  123. if (!inode->i_sb->s_cop->get_context ||
  124. !inode->i_sb->s_cop->is_encrypted(inode))
  125. return -ENODATA;
  126. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  127. if (res != sizeof(ctx))
  128. return -ENODATA;
  129. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  130. return -EINVAL;
  131. policy.version = 0;
  132. policy.contents_encryption_mode = ctx.contents_encryption_mode;
  133. policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
  134. policy.flags = ctx.flags;
  135. memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
  136. FS_KEY_DESCRIPTOR_SIZE);
  137. if (copy_to_user(arg, &policy, sizeof(policy)))
  138. return -EFAULT;
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
  142. int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
  143. {
  144. struct fscrypt_info *parent_ci, *child_ci;
  145. int res;
  146. if ((parent == NULL) || (child == NULL)) {
  147. printk(KERN_ERR "parent %p child %p\n", parent, child);
  148. BUG_ON(1);
  149. }
  150. /* No restrictions on file types which are never encrypted */
  151. if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
  152. !S_ISLNK(child->i_mode))
  153. return 1;
  154. /* no restrictions if the parent directory is not encrypted */
  155. if (!parent->i_sb->s_cop->is_encrypted(parent))
  156. return 1;
  157. /* if the child directory is not encrypted, this is always a problem */
  158. if (!parent->i_sb->s_cop->is_encrypted(child))
  159. return 0;
  160. res = fscrypt_get_encryption_info(parent);
  161. if (res)
  162. return 0;
  163. res = fscrypt_get_encryption_info(child);
  164. if (res)
  165. return 0;
  166. parent_ci = parent->i_crypt_info;
  167. child_ci = child->i_crypt_info;
  168. if (!parent_ci && !child_ci)
  169. return 1;
  170. if (!parent_ci || !child_ci)
  171. return 0;
  172. return (memcmp(parent_ci->ci_master_key,
  173. child_ci->ci_master_key,
  174. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  175. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  176. (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
  177. (parent_ci->ci_flags == child_ci->ci_flags));
  178. }
  179. EXPORT_SYMBOL(fscrypt_has_permitted_context);
  180. /**
  181. * fscrypt_inherit_context() - Sets a child context from its parent
  182. * @parent: Parent inode from which the context is inherited.
  183. * @child: Child inode that inherits the context from @parent.
  184. * @fs_data: private data given by FS.
  185. * @preload: preload child i_crypt_info
  186. *
  187. * Return: Zero on success, non-zero otherwise
  188. */
  189. int fscrypt_inherit_context(struct inode *parent, struct inode *child,
  190. void *fs_data, bool preload)
  191. {
  192. struct fscrypt_context ctx;
  193. struct fscrypt_info *ci;
  194. int res;
  195. if (!parent->i_sb->s_cop->set_context)
  196. return -EOPNOTSUPP;
  197. res = fscrypt_get_encryption_info(parent);
  198. if (res < 0)
  199. return res;
  200. ci = parent->i_crypt_info;
  201. if (ci == NULL)
  202. return -ENOKEY;
  203. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  204. if (fscrypt_dummy_context_enabled(parent)) {
  205. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  206. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  207. ctx.flags = 0;
  208. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  209. res = 0;
  210. } else {
  211. ctx.contents_encryption_mode = ci->ci_data_mode;
  212. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  213. ctx.flags = ci->ci_flags;
  214. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  215. FS_KEY_DESCRIPTOR_SIZE);
  216. }
  217. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  218. res = parent->i_sb->s_cop->set_context(child, &ctx,
  219. sizeof(ctx), fs_data);
  220. if (res)
  221. return res;
  222. return preload ? fscrypt_get_encryption_info(child): 0;
  223. }
  224. EXPORT_SYMBOL(fscrypt_inherit_context);