policy.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include <linux/mount.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_process_policy(struct file *filp,
  81. const struct fscrypt_policy *policy)
  82. {
  83. struct inode *inode = file_inode(filp);
  84. int ret;
  85. if (!inode_owner_or_capable(inode))
  86. return -EACCES;
  87. if (policy->version != 0)
  88. return -EINVAL;
  89. ret = mnt_want_write_file(filp);
  90. if (ret)
  91. return ret;
  92. inode_lock(inode);
  93. if (!inode_has_encryption_context(inode)) {
  94. if (!S_ISDIR(inode->i_mode))
  95. ret = -ENOTDIR;
  96. else if (!inode->i_sb->s_cop->empty_dir)
  97. ret = -EOPNOTSUPP;
  98. else if (!inode->i_sb->s_cop->empty_dir(inode))
  99. ret = -ENOTEMPTY;
  100. else
  101. ret = create_encryption_context_from_policy(inode,
  102. policy);
  103. } else if (!is_encryption_context_consistent_with_policy(inode,
  104. policy)) {
  105. printk(KERN_WARNING
  106. "%s: Policy inconsistent with encryption context\n",
  107. __func__);
  108. ret = -EINVAL;
  109. }
  110. inode_unlock(inode);
  111. mnt_drop_write_file(filp);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL(fscrypt_process_policy);
  115. int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy)
  116. {
  117. struct fscrypt_context ctx;
  118. int res;
  119. if (!inode->i_sb->s_cop->get_context ||
  120. !inode->i_sb->s_cop->is_encrypted(inode))
  121. return -ENODATA;
  122. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  123. if (res != sizeof(ctx))
  124. return -ENODATA;
  125. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  126. return -EINVAL;
  127. policy->version = 0;
  128. policy->contents_encryption_mode = ctx.contents_encryption_mode;
  129. policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
  130. policy->flags = ctx.flags;
  131. memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
  132. FS_KEY_DESCRIPTOR_SIZE);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL(fscrypt_get_policy);
  136. /**
  137. * fscrypt_has_permitted_context() - is a file's encryption policy permitted
  138. * within its directory?
  139. *
  140. * @parent: inode for parent directory
  141. * @child: inode for file being looked up, opened, or linked into @parent
  142. *
  143. * Filesystems must call this before permitting access to an inode in a
  144. * situation where the parent directory is encrypted (either before allowing
  145. * ->lookup() to succeed, or for a regular file before allowing it to be opened)
  146. * and before any operation that involves linking an inode into an encrypted
  147. * directory, including link, rename, and cross rename. It enforces the
  148. * constraint that within a given encrypted directory tree, all files use the
  149. * same encryption policy. The pre-access check is needed to detect potentially
  150. * malicious offline violations of this constraint, while the link and rename
  151. * checks are needed to prevent online violations of this constraint.
  152. *
  153. * Return: 1 if permitted, 0 if forbidden. If forbidden, the caller must fail
  154. * the filesystem operation with EPERM.
  155. */
  156. int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
  157. {
  158. const struct fscrypt_operations *cops = parent->i_sb->s_cop;
  159. const struct fscrypt_info *parent_ci, *child_ci;
  160. struct fscrypt_context parent_ctx, child_ctx;
  161. int res;
  162. /* No restrictions on file types which are never encrypted */
  163. if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
  164. !S_ISLNK(child->i_mode))
  165. return 1;
  166. /* No restrictions if the parent directory is unencrypted */
  167. if (!cops->is_encrypted(parent))
  168. return 1;
  169. /* Encrypted directories must not contain unencrypted files */
  170. if (!cops->is_encrypted(child))
  171. return 0;
  172. /*
  173. * Both parent and child are encrypted, so verify they use the same
  174. * encryption policy. Compare the fscrypt_info structs if the keys are
  175. * available, otherwise retrieve and compare the fscrypt_contexts.
  176. *
  177. * Note that the fscrypt_context retrieval will be required frequently
  178. * when accessing an encrypted directory tree without the key.
  179. * Performance-wise this is not a big deal because we already don't
  180. * really optimize for file access without the key (to the extent that
  181. * such access is even possible), given that any attempted access
  182. * already causes a fscrypt_context retrieval and keyring search.
  183. *
  184. * In any case, if an unexpected error occurs, fall back to "forbidden".
  185. */
  186. res = fscrypt_get_encryption_info(parent);
  187. if (res)
  188. return 0;
  189. res = fscrypt_get_encryption_info(child);
  190. if (res)
  191. return 0;
  192. parent_ci = parent->i_crypt_info;
  193. child_ci = child->i_crypt_info;
  194. if (parent_ci && child_ci) {
  195. return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key,
  196. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  197. (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
  198. (parent_ci->ci_filename_mode ==
  199. child_ci->ci_filename_mode) &&
  200. (parent_ci->ci_flags == child_ci->ci_flags);
  201. }
  202. res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx));
  203. if (res != sizeof(parent_ctx))
  204. return 0;
  205. res = cops->get_context(child, &child_ctx, sizeof(child_ctx));
  206. if (res != sizeof(child_ctx))
  207. return 0;
  208. return memcmp(parent_ctx.master_key_descriptor,
  209. child_ctx.master_key_descriptor,
  210. FS_KEY_DESCRIPTOR_SIZE) == 0 &&
  211. (parent_ctx.contents_encryption_mode ==
  212. child_ctx.contents_encryption_mode) &&
  213. (parent_ctx.filenames_encryption_mode ==
  214. child_ctx.filenames_encryption_mode) &&
  215. (parent_ctx.flags == child_ctx.flags);
  216. }
  217. EXPORT_SYMBOL(fscrypt_has_permitted_context);
  218. /**
  219. * fscrypt_inherit_context() - Sets a child context from its parent
  220. * @parent: Parent inode from which the context is inherited.
  221. * @child: Child inode that inherits the context from @parent.
  222. * @fs_data: private data given by FS.
  223. * @preload: preload child i_crypt_info
  224. *
  225. * Return: Zero on success, non-zero otherwise
  226. */
  227. int fscrypt_inherit_context(struct inode *parent, struct inode *child,
  228. void *fs_data, bool preload)
  229. {
  230. struct fscrypt_context ctx;
  231. struct fscrypt_info *ci;
  232. int res;
  233. if (!parent->i_sb->s_cop->set_context)
  234. return -EOPNOTSUPP;
  235. res = fscrypt_get_encryption_info(parent);
  236. if (res < 0)
  237. return res;
  238. ci = parent->i_crypt_info;
  239. if (ci == NULL)
  240. return -ENOKEY;
  241. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  242. if (fscrypt_dummy_context_enabled(parent)) {
  243. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  244. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  245. ctx.flags = 0;
  246. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  247. res = 0;
  248. } else {
  249. ctx.contents_encryption_mode = ci->ci_data_mode;
  250. ctx.filenames_encryption_mode = ci->ci_filename_mode;
  251. ctx.flags = ci->ci_flags;
  252. memcpy(ctx.master_key_descriptor, ci->ci_master_key,
  253. FS_KEY_DESCRIPTOR_SIZE);
  254. }
  255. get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  256. res = parent->i_sb->s_cop->set_context(child, &ctx,
  257. sizeof(ctx), fs_data);
  258. if (res)
  259. return res;
  260. return preload ? fscrypt_get_encryption_info(child): 0;
  261. }
  262. EXPORT_SYMBOL(fscrypt_inherit_context);