policy.c 8.3 KB

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