hooks.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. struct dentry *dentry)
  49. {
  50. int err;
  51. err = fscrypt_require_key(dir);
  52. if (err)
  53. return err;
  54. /* ... in case we looked up no-key name before key was added */
  55. if (fscrypt_is_nokey_name(dentry))
  56. return -ENOKEY;
  57. if (!fscrypt_has_permitted_context(dir, inode))
  58. return -EXDEV;
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
  62. int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
  63. struct inode *new_dir, struct dentry *new_dentry,
  64. unsigned int flags)
  65. {
  66. int err;
  67. err = fscrypt_require_key(old_dir);
  68. if (err)
  69. return err;
  70. err = fscrypt_require_key(new_dir);
  71. if (err)
  72. return err;
  73. /* ... in case we looked up no-key name(s) before key was added */
  74. if (fscrypt_is_nokey_name(old_dentry) ||
  75. fscrypt_is_nokey_name(new_dentry))
  76. return -ENOKEY;
  77. if (old_dir != new_dir) {
  78. if (IS_ENCRYPTED(new_dir) &&
  79. !fscrypt_has_permitted_context(new_dir,
  80. d_inode(old_dentry)))
  81. return -EXDEV;
  82. if ((flags & RENAME_EXCHANGE) &&
  83. IS_ENCRYPTED(old_dir) &&
  84. !fscrypt_has_permitted_context(old_dir,
  85. d_inode(new_dentry)))
  86. return -EXDEV;
  87. }
  88. return 0;
  89. }
  90. EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
  91. int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
  92. struct fscrypt_name *fname)
  93. {
  94. int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
  95. if (err && err != -ENOENT)
  96. return err;
  97. if (fname->is_ciphertext_name) {
  98. spin_lock(&dentry->d_lock);
  99. dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
  100. spin_unlock(&dentry->d_lock);
  101. d_set_d_op(dentry, &fscrypt_d_ops);
  102. }
  103. return err;
  104. }
  105. EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
  106. int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
  107. unsigned int max_len,
  108. struct fscrypt_str *disk_link)
  109. {
  110. int err;
  111. /*
  112. * To calculate the size of the encrypted symlink target we need to know
  113. * the amount of NUL padding, which is determined by the flags set in
  114. * the encryption policy which will be inherited from the directory.
  115. * The easiest way to get access to this is to just load the directory's
  116. * fscrypt_info, since we'll need it to create the dir_entry anyway.
  117. *
  118. * Note: in test_dummy_encryption mode, @dir may be unencrypted.
  119. */
  120. err = fscrypt_get_encryption_info(dir);
  121. if (err)
  122. return err;
  123. if (!fscrypt_has_encryption_key(dir))
  124. return -ENOKEY;
  125. /*
  126. * Calculate the size of the encrypted symlink and verify it won't
  127. * exceed max_len. Note that for historical reasons, encrypted symlink
  128. * targets are prefixed with the ciphertext length, despite this
  129. * actually being redundant with i_size. This decreases by 2 bytes the
  130. * longest symlink target we can accept.
  131. *
  132. * We could recover 1 byte by not counting a null terminator, but
  133. * counting it (even though it is meaningless for ciphertext) is simpler
  134. * for now since filesystems will assume it is there and subtract it.
  135. */
  136. if (!fscrypt_fname_encrypted_size(dir, len,
  137. max_len - sizeof(struct fscrypt_symlink_data),
  138. &disk_link->len))
  139. return -ENAMETOOLONG;
  140. disk_link->len += sizeof(struct fscrypt_symlink_data);
  141. disk_link->name = NULL;
  142. return 0;
  143. }
  144. EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
  145. int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
  146. unsigned int len, struct fscrypt_str *disk_link)
  147. {
  148. int err;
  149. struct qstr iname = QSTR_INIT(target, len);
  150. struct fscrypt_symlink_data *sd;
  151. unsigned int ciphertext_len;
  152. err = fscrypt_require_key(inode);
  153. if (err)
  154. return err;
  155. if (disk_link->name) {
  156. /* filesystem-provided buffer */
  157. sd = (struct fscrypt_symlink_data *)disk_link->name;
  158. } else {
  159. sd = kmalloc(disk_link->len, GFP_NOFS);
  160. if (!sd)
  161. return -ENOMEM;
  162. }
  163. ciphertext_len = disk_link->len - sizeof(*sd);
  164. sd->len = cpu_to_le16(ciphertext_len);
  165. err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
  166. if (err) {
  167. if (!disk_link->name)
  168. kfree(sd);
  169. return err;
  170. }
  171. /*
  172. * Null-terminating the ciphertext doesn't make sense, but we still
  173. * count the null terminator in the length, so we might as well
  174. * initialize it just in case the filesystem writes it out.
  175. */
  176. sd->encrypted_path[ciphertext_len] = '\0';
  177. if (!disk_link->name)
  178. disk_link->name = (unsigned char *)sd;
  179. return 0;
  180. }
  181. EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
  182. /**
  183. * fscrypt_get_symlink - get the target of an encrypted symlink
  184. * @inode: the symlink inode
  185. * @caddr: the on-disk contents of the symlink
  186. * @max_size: size of @caddr buffer
  187. * @done: if successful, will be set up to free the returned target
  188. *
  189. * If the symlink's encryption key is available, we decrypt its target.
  190. * Otherwise, we encode its target for presentation.
  191. *
  192. * This may sleep, so the filesystem must have dropped out of RCU mode already.
  193. *
  194. * Return: the presentable symlink target or an ERR_PTR()
  195. */
  196. const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
  197. unsigned int max_size,
  198. struct delayed_call *done)
  199. {
  200. const struct fscrypt_symlink_data *sd;
  201. struct fscrypt_str cstr, pstr;
  202. int err;
  203. /* This is for encrypted symlinks only */
  204. if (WARN_ON(!IS_ENCRYPTED(inode)))
  205. return ERR_PTR(-EINVAL);
  206. /*
  207. * Try to set up the symlink's encryption key, but we can continue
  208. * regardless of whether the key is available or not.
  209. */
  210. err = fscrypt_get_encryption_info(inode);
  211. if (err)
  212. return ERR_PTR(err);
  213. /*
  214. * For historical reasons, encrypted symlink targets are prefixed with
  215. * the ciphertext length, even though this is redundant with i_size.
  216. */
  217. if (max_size < sizeof(*sd))
  218. return ERR_PTR(-EUCLEAN);
  219. sd = caddr;
  220. cstr.name = (unsigned char *)sd->encrypted_path;
  221. cstr.len = le16_to_cpu(sd->len);
  222. if (cstr.len == 0)
  223. return ERR_PTR(-EUCLEAN);
  224. if (cstr.len + sizeof(*sd) - 1 > max_size)
  225. return ERR_PTR(-EUCLEAN);
  226. err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
  227. if (err)
  228. return ERR_PTR(err);
  229. err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
  230. if (err)
  231. goto err_kfree;
  232. err = -EUCLEAN;
  233. if (pstr.name[0] == '\0')
  234. goto err_kfree;
  235. pstr.name[pstr.len] = '\0';
  236. set_delayed_call(done, kfree_link, pstr.name);
  237. return pstr.name;
  238. err_kfree:
  239. kfree(pstr.name);
  240. return ERR_PTR(err);
  241. }
  242. EXPORT_SYMBOL_GPL(fscrypt_get_symlink);