hooks.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * fs/crypto/hooks.c
  3. *
  4. * Encryption hooks for higher-level filesystem operations.
  5. */
  6. #include <linux/ratelimit.h>
  7. #include "fscrypt_private.h"
  8. /**
  9. * fscrypt_file_open - prepare to open a possibly-encrypted regular file
  10. * @inode: the inode being opened
  11. * @filp: the struct file being set up
  12. *
  13. * Currently, an encrypted regular file can only be opened if its encryption key
  14. * is available; access to the raw encrypted contents is not supported.
  15. * Therefore, we first set up the inode's encryption key (if not already done)
  16. * and return an error if it's unavailable.
  17. *
  18. * We also verify that if the parent directory (from the path via which the file
  19. * is being opened) is encrypted, then the inode being opened uses the same
  20. * encryption policy. This is needed as part of the enforcement that all files
  21. * in an encrypted directory tree use the same encryption policy, as a
  22. * protection against certain types of offline attacks. Note that this check is
  23. * needed even when opening an *unencrypted* file, since it's forbidden to have
  24. * an unencrypted file in an encrypted directory.
  25. *
  26. * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
  27. */
  28. int fscrypt_file_open(struct inode *inode, struct file *filp)
  29. {
  30. int err;
  31. struct dentry *dir;
  32. err = fscrypt_require_key(inode);
  33. if (err)
  34. return err;
  35. dir = dget_parent(file_dentry(filp));
  36. if (IS_ENCRYPTED(d_inode(dir)) &&
  37. !fscrypt_has_permitted_context(d_inode(dir), inode)) {
  38. fscrypt_warn(inode->i_sb,
  39. "inconsistent encryption contexts: %lu/%lu",
  40. d_inode(dir)->i_ino, inode->i_ino);
  41. err = -EPERM;
  42. }
  43. dput(dir);
  44. return err;
  45. }
  46. EXPORT_SYMBOL_GPL(fscrypt_file_open);
  47. int __fscrypt_prepare_link(struct inode *inode, struct inode *dir)
  48. {
  49. int err;
  50. err = fscrypt_require_key(dir);
  51. if (err)
  52. return err;
  53. if (!fscrypt_has_permitted_context(dir, inode))
  54. return -EPERM;
  55. return 0;
  56. }
  57. EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
  58. int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
  59. struct inode *new_dir, struct dentry *new_dentry,
  60. unsigned int flags)
  61. {
  62. int err;
  63. err = fscrypt_require_key(old_dir);
  64. if (err)
  65. return err;
  66. err = fscrypt_require_key(new_dir);
  67. if (err)
  68. return err;
  69. if (old_dir != new_dir) {
  70. if (IS_ENCRYPTED(new_dir) &&
  71. !fscrypt_has_permitted_context(new_dir,
  72. d_inode(old_dentry)))
  73. return -EPERM;
  74. if ((flags & RENAME_EXCHANGE) &&
  75. IS_ENCRYPTED(old_dir) &&
  76. !fscrypt_has_permitted_context(old_dir,
  77. d_inode(new_dentry)))
  78. return -EPERM;
  79. }
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
  83. int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry)
  84. {
  85. int err = fscrypt_get_encryption_info(dir);
  86. if (err)
  87. return err;
  88. if (fscrypt_has_encryption_key(dir)) {
  89. spin_lock(&dentry->d_lock);
  90. dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
  91. spin_unlock(&dentry->d_lock);
  92. }
  93. d_set_d_op(dentry, &fscrypt_d_ops);
  94. return 0;
  95. }
  96. EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
  97. int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
  98. unsigned int max_len,
  99. struct fscrypt_str *disk_link)
  100. {
  101. int err;
  102. /*
  103. * To calculate the size of the encrypted symlink target we need to know
  104. * the amount of NUL padding, which is determined by the flags set in
  105. * the encryption policy which will be inherited from the directory.
  106. * The easiest way to get access to this is to just load the directory's
  107. * fscrypt_info, since we'll need it to create the dir_entry anyway.
  108. *
  109. * Note: in test_dummy_encryption mode, @dir may be unencrypted.
  110. */
  111. err = fscrypt_get_encryption_info(dir);
  112. if (err)
  113. return err;
  114. if (!fscrypt_has_encryption_key(dir))
  115. return -ENOKEY;
  116. /*
  117. * Calculate the size of the encrypted symlink and verify it won't
  118. * exceed max_len. Note that for historical reasons, encrypted symlink
  119. * targets are prefixed with the ciphertext length, despite this
  120. * actually being redundant with i_size. This decreases by 2 bytes the
  121. * longest symlink target we can accept.
  122. *
  123. * We could recover 1 byte by not counting a null terminator, but
  124. * counting it (even though it is meaningless for ciphertext) is simpler
  125. * for now since filesystems will assume it is there and subtract it.
  126. */
  127. if (!fscrypt_fname_encrypted_size(dir, len,
  128. max_len - sizeof(struct fscrypt_symlink_data),
  129. &disk_link->len))
  130. return -ENAMETOOLONG;
  131. disk_link->len += sizeof(struct fscrypt_symlink_data);
  132. disk_link->name = NULL;
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
  136. int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
  137. unsigned int len, struct fscrypt_str *disk_link)
  138. {
  139. int err;
  140. struct qstr iname = QSTR_INIT(target, len);
  141. struct fscrypt_symlink_data *sd;
  142. unsigned int ciphertext_len;
  143. err = fscrypt_require_key(inode);
  144. if (err)
  145. return err;
  146. if (disk_link->name) {
  147. /* filesystem-provided buffer */
  148. sd = (struct fscrypt_symlink_data *)disk_link->name;
  149. } else {
  150. sd = kmalloc(disk_link->len, GFP_NOFS);
  151. if (!sd)
  152. return -ENOMEM;
  153. }
  154. ciphertext_len = disk_link->len - sizeof(*sd);
  155. sd->len = cpu_to_le16(ciphertext_len);
  156. err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
  157. if (err) {
  158. if (!disk_link->name)
  159. kfree(sd);
  160. return err;
  161. }
  162. /*
  163. * Null-terminating the ciphertext doesn't make sense, but we still
  164. * count the null terminator in the length, so we might as well
  165. * initialize it just in case the filesystem writes it out.
  166. */
  167. sd->encrypted_path[ciphertext_len] = '\0';
  168. if (!disk_link->name)
  169. disk_link->name = (unsigned char *)sd;
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
  173. /**
  174. * fscrypt_get_symlink - get the target of an encrypted symlink
  175. * @inode: the symlink inode
  176. * @caddr: the on-disk contents of the symlink
  177. * @max_size: size of @caddr buffer
  178. * @done: if successful, will be set up to free the returned target
  179. *
  180. * If the symlink's encryption key is available, we decrypt its target.
  181. * Otherwise, we encode its target for presentation.
  182. *
  183. * This may sleep, so the filesystem must have dropped out of RCU mode already.
  184. *
  185. * Return: the presentable symlink target or an ERR_PTR()
  186. */
  187. const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
  188. unsigned int max_size,
  189. struct delayed_call *done)
  190. {
  191. const struct fscrypt_symlink_data *sd;
  192. struct fscrypt_str cstr, pstr;
  193. int err;
  194. /* This is for encrypted symlinks only */
  195. if (WARN_ON(!IS_ENCRYPTED(inode)))
  196. return ERR_PTR(-EINVAL);
  197. /*
  198. * Try to set up the symlink's encryption key, but we can continue
  199. * regardless of whether the key is available or not.
  200. */
  201. err = fscrypt_get_encryption_info(inode);
  202. if (err)
  203. return ERR_PTR(err);
  204. /*
  205. * For historical reasons, encrypted symlink targets are prefixed with
  206. * the ciphertext length, even though this is redundant with i_size.
  207. */
  208. if (max_size < sizeof(*sd))
  209. return ERR_PTR(-EUCLEAN);
  210. sd = caddr;
  211. cstr.name = (unsigned char *)sd->encrypted_path;
  212. cstr.len = le16_to_cpu(sd->len);
  213. if (cstr.len == 0)
  214. return ERR_PTR(-EUCLEAN);
  215. if (cstr.len + sizeof(*sd) - 1 > max_size)
  216. return ERR_PTR(-EUCLEAN);
  217. err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
  218. if (err)
  219. return ERR_PTR(err);
  220. err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
  221. if (err)
  222. goto err_kfree;
  223. err = -EUCLEAN;
  224. if (pstr.name[0] == '\0')
  225. goto err_kfree;
  226. pstr.name[pstr.len] = '\0';
  227. set_delayed_call(done, kfree_link, pstr.name);
  228. return pstr.name;
  229. err_kfree:
  230. kfree(pstr.name);
  231. return ERR_PTR(err);
  232. }
  233. EXPORT_SYMBOL_GPL(fscrypt_get_symlink);