policy.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 (!inode->i_sb->s_cop->empty_dir(inode))
  70. ret = -ENOTEMPTY;
  71. else
  72. ret = create_encryption_context_from_policy(inode,
  73. &policy);
  74. } else if (ret == sizeof(ctx) &&
  75. is_encryption_context_consistent_with_policy(&ctx,
  76. &policy)) {
  77. /* The file already uses the same encryption policy. */
  78. ret = 0;
  79. } else if (ret >= 0 || ret == -ERANGE) {
  80. /* The file already uses a different encryption policy. */
  81. ret = -EEXIST;
  82. }
  83. inode_unlock(inode);
  84. mnt_drop_write_file(filp);
  85. return ret;
  86. }
  87. EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
  88. int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
  89. {
  90. struct inode *inode = file_inode(filp);
  91. struct fscrypt_context ctx;
  92. struct fscrypt_policy policy;
  93. int res;
  94. if (!IS_ENCRYPTED(inode))
  95. return -ENODATA;
  96. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  97. if (res < 0 && res != -ERANGE)
  98. return res;
  99. if (res != sizeof(ctx))
  100. return -EINVAL;
  101. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  102. return -EINVAL;
  103. policy.version = 0;
  104. policy.contents_encryption_mode = ctx.contents_encryption_mode;
  105. policy.filenames_encryption_mode = ctx.filenames_encryption_mode;
  106. policy.flags = ctx.flags;
  107. memcpy(policy.master_key_descriptor, ctx.master_key_descriptor,
  108. FS_KEY_DESCRIPTOR_SIZE);
  109. if (copy_to_user(arg, &policy, sizeof(policy)))
  110. return -EFAULT;
  111. return 0;
  112. }
  113. EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
  114. /**
  115. * fscrypt_has_permitted_context() - is a file's encryption policy permitted
  116. * within its directory?
  117. *
  118. * @parent: inode for parent directory
  119. * @child: inode for file being looked up, opened, or linked into @parent
  120. *
  121. * Filesystems must call this before permitting access to an inode in a
  122. * situation where the parent directory is encrypted (either before allowing
  123. * ->lookup() to succeed, or for a regular file before allowing it to be opened)
  124. * and before any operation that involves linking an inode into an encrypted
  125. * directory, including link, rename, and cross rename. It enforces the
  126. * constraint that within a given encrypted directory tree, all files use the
  127. * same encryption policy. The pre-access check is needed to detect potentially
  128. * malicious offline violations of this constraint, while the link and rename
  129. * checks are needed to prevent online violations of this constraint.
  130. *
  131. * Return: 1 if permitted, 0 if forbidden. If forbidden, the caller must fail
  132. * the filesystem operation with EPERM.
  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, 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 ==
  177. child_ci->ci_filename_mode) &&
  178. (parent_ci->ci_flags == child_ci->ci_flags);
  179. }
  180. res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx));
  181. if (res != sizeof(parent_ctx))
  182. return 0;
  183. res = cops->get_context(child, &child_ctx, sizeof(child_ctx));
  184. if (res != sizeof(child_ctx))
  185. return 0;
  186. return memcmp(parent_ctx.master_key_descriptor,
  187. child_ctx.master_key_descriptor,
  188. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  189. (parent_ctx.contents_encryption_mode ==
  190. child_ctx.contents_encryption_mode) &&
  191. (parent_ctx.filenames_encryption_mode ==
  192. child_ctx.filenames_encryption_mode) &&
  193. (parent_ctx.flags == child_ctx.flags);
  194. }
  195. EXPORT_SYMBOL(fscrypt_has_permitted_context);
  196. /**
  197. * fscrypt_inherit_context() - Sets a child context from its parent
  198. * @parent: Parent inode from which the context is inherited.
  199. * @child: Child inode that inherits the context from @parent.
  200. * @fs_data: private data given by FS.
  201. * @preload: preload child i_crypt_info if true
  202. *
  203. * Return: 0 on success, -errno on failure
  204. */
  205. int fscrypt_inherit_context(struct inode *parent, struct inode *child,
  206. void *fs_data, bool preload)
  207. {
  208. struct fscrypt_context ctx;
  209. struct fscrypt_info *ci;
  210. int res;
  211. res = fscrypt_get_encryption_info(parent);
  212. if (res < 0)
  213. return res;
  214. ci = parent->i_crypt_info;
  215. if (ci == NULL)
  216. return -ENOKEY;
  217. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  218. ctx.contents_encryption_mode = ci->ci_data_mode;
  219. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  220. ctx.flags = ci->ci_flags;
  221. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  222. FS_KEY_DESCRIPTOR_SIZE);
  223. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  224. BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_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);