policy.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /*
  15. * check whether an encryption policy is consistent with an encryption context
  16. */
  17. static bool is_encryption_context_consistent_with_policy(
  18. const struct fscrypt_context *ctx,
  19. const struct fscrypt_policy *policy)
  20. {
  21. return memcmp(ctx->master_key_descriptor, policy->master_key_descriptor,
  22. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  23. (ctx->flags == policy->flags) &&
  24. (ctx->contents_encryption_mode ==
  25. policy->contents_encryption_mode) &&
  26. (ctx->filenames_encryption_mode ==
  27. policy->filenames_encryption_mode);
  28. }
  29. static int create_encryption_context_from_policy(struct inode *inode,
  30. const struct fscrypt_policy *policy)
  31. {
  32. struct fscrypt_context ctx;
  33. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  34. memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
  35. FS_KEY_DESCRIPTOR_SIZE);
  36. if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
  37. policy->filenames_encryption_mode))
  38. return -EINVAL;
  39. if (policy->flags & ~FS_POLICY_FLAGS_VALID)
  40. return -EINVAL;
  41. ctx.contents_encryption_mode = policy->contents_encryption_mode;
  42. ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
  43. ctx.flags = policy->flags;
  44. BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE);
  45. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  46. return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
  47. }
  48. int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
  49. {
  50. struct fscrypt_policy policy;
  51. struct inode *inode = file_inode(filp);
  52. int ret;
  53. struct fscrypt_context ctx;
  54. if (copy_from_user(&policy, arg, sizeof(policy)))
  55. return -EFAULT;
  56. if (!inode_owner_or_capable(inode))
  57. return -EACCES;
  58. if (policy.version != 0)
  59. return -EINVAL;
  60. ret = mnt_want_write_file(filp);
  61. if (ret)
  62. return ret;
  63. inode_lock(inode);
  64. ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  65. if (ret == -ENODATA) {
  66. if (!S_ISDIR(inode->i_mode))
  67. ret = -ENOTDIR;
  68. else if (IS_DEADDIR(inode))
  69. ret = -ENOENT;
  70. else if (!inode->i_sb->s_cop->empty_dir(inode))
  71. ret = -ENOTEMPTY;
  72. else
  73. ret = create_encryption_context_from_policy(inode,
  74. &policy);
  75. } else if (ret == sizeof(ctx) &&
  76. is_encryption_context_consistent_with_policy(&ctx,
  77. &policy)) {
  78. /* The file already uses the same encryption policy. */
  79. ret = 0;
  80. } else if (ret >= 0 || ret == -ERANGE) {
  81. /* The file already uses a different encryption policy. */
  82. ret = -EEXIST;
  83. }
  84. inode_unlock(inode);
  85. mnt_drop_write_file(filp);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
  89. int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
  90. {
  91. struct inode *inode = file_inode(filp);
  92. struct fscrypt_context ctx;
  93. struct fscrypt_policy policy;
  94. int res;
  95. if (!IS_ENCRYPTED(inode))
  96. return -ENODATA;
  97. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  98. if (res < 0 && res != -ERANGE)
  99. return res;
  100. if (res != sizeof(ctx))
  101. return -EINVAL;
  102. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  103. return -EINVAL;
  104. policy.version = 0;
  105. policy.contents_encryption_mode = ctx.contents_encryption_mode;
  106. policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
  107. policy.flags = ctx.flags;
  108. memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
  109. FS_KEY_DESCRIPTOR_SIZE);
  110. if (copy_to_user(arg, &policy, sizeof(policy)))
  111. return -EFAULT;
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
  115. /**
  116. * fscrypt_has_permitted_context() - is a file's encryption policy permitted
  117. * within its directory?
  118. *
  119. * @parent: inode for parent directory
  120. * @child: inode for file being looked up, opened, or linked into @parent
  121. *
  122. * Filesystems must call this before permitting access to an inode in a
  123. * situation where the parent directory is encrypted (either before allowing
  124. * ->lookup() to succeed, or for a regular file before allowing it to be opened)
  125. * and before any operation that involves linking an inode into an encrypted
  126. * directory, including link, rename, and cross rename. It enforces the
  127. * constraint that within a given encrypted directory tree, all files use the
  128. * same encryption policy. The pre-access check is needed to detect potentially
  129. * malicious offline violations of this constraint, while the link and rename
  130. * checks are needed to prevent online violations of this constraint.
  131. *
  132. * Return: 1 if permitted, 0 if forbidden.
  133. */
  134. int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
  135. {
  136. const struct fscrypt_operations *cops = parent->i_sb->s_cop;
  137. const struct fscrypt_info *parent_ci, *child_ci;
  138. struct fscrypt_context parent_ctx, child_ctx;
  139. int res;
  140. /* No restrictions on file types which are never encrypted */
  141. if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
  142. !S_ISLNK(child->i_mode))
  143. return 1;
  144. /* No restrictions if the parent directory is unencrypted */
  145. if (!IS_ENCRYPTED(parent))
  146. return 1;
  147. /* Encrypted directories must not contain unencrypted files */
  148. if (!IS_ENCRYPTED(child))
  149. return 0;
  150. /*
  151. * Both parent and child are encrypted, so verify they use the same
  152. * encryption policy. Compare the fscrypt_info structs if the keys are
  153. * available, otherwise retrieve and compare the fscrypt_contexts.
  154. *
  155. * Note that the fscrypt_context retrieval will be required frequently
  156. * when accessing an encrypted directory tree without the key.
  157. * Performance-wise this is not a big deal because we already don't
  158. * really optimize for file access without the key (to the extent that
  159. * such access is even possible), given that any attempted access
  160. * already causes a fscrypt_context retrieval and keyring search.
  161. *
  162. * In any case, if an unexpected error occurs, fall back to "forbidden".
  163. */
  164. res = fscrypt_get_encryption_info(parent);
  165. if (res)
  166. return 0;
  167. res = fscrypt_get_encryption_info(child);
  168. if (res)
  169. return 0;
  170. parent_ci = parent->i_crypt_info;
  171. child_ci = child->i_crypt_info;
  172. if (parent_ci && child_ci) {
  173. return memcmp(parent_ci->ci_master_key_descriptor,
  174. child_ci->ci_master_key_descriptor,
  175. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  176. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  177. (parent_ci->ci_filename_mode ==
  178. child_ci->ci_filename_mode) &&
  179. (parent_ci->ci_flags == child_ci->ci_flags);
  180. }
  181. res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx));
  182. if (res != sizeof(parent_ctx))
  183. return 0;
  184. res = cops->get_context(child, &child_ctx, sizeof(child_ctx));
  185. if (res != sizeof(child_ctx))
  186. return 0;
  187. return memcmp(parent_ctx.master_key_descriptor,
  188. child_ctx.master_key_descriptor,
  189. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  190. (parent_ctx.contents_encryption_mode ==
  191. child_ctx.contents_encryption_mode) &&
  192. (parent_ctx.filenames_encryption_mode ==
  193. child_ctx.filenames_encryption_mode) &&
  194. (parent_ctx.flags == child_ctx.flags);
  195. }
  196. EXPORT_SYMBOL(fscrypt_has_permitted_context);
  197. /**
  198. * fscrypt_inherit_context() - Sets a child context from its parent
  199. * @parent: Parent inode from which the context is inherited.
  200. * @child: Child inode that inherits the context from @parent.
  201. * @fs_data: private data given by FS.
  202. * @preload: preload child i_crypt_info if true
  203. *
  204. * Return: 0 on success, -errno on failure
  205. */
  206. int fscrypt_inherit_context(struct inode *parent, struct inode *child,
  207. void *fs_data, bool preload)
  208. {
  209. struct fscrypt_context ctx;
  210. struct fscrypt_info *ci;
  211. int res;
  212. res = fscrypt_get_encryption_info(parent);
  213. if (res < 0)
  214. return res;
  215. ci = parent->i_crypt_info;
  216. if (ci == NULL)
  217. return -ENOKEY;
  218. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  219. ctx.contents_encryption_mode = ci->ci_data_mode;
  220. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  221. ctx.flags = ci->ci_flags;
  222. memcpy(ctx.master_key_descriptor, ci->ci_master_key_descriptor,
  223. FS_KEY_DESCRIPTOR_SIZE);
  224. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  225. res = parent->i_sb->s_cop->set_context(child, &ctx,
  226. sizeof(ctx), fs_data);
  227. if (res)
  228. return res;
  229. return preload ? fscrypt_get_encryption_info(child): 0;
  230. }
  231. EXPORT_SYMBOL(fscrypt_inherit_context);