hooks.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fs/crypto/hooks.c
  4. *
  5. * Encryption hooks for higher-level filesystem operations.
  6. */
  7. #include <linux/export.h>
  8. #include "fscrypt_private.h"
  9. /**
  10. * fscrypt_file_open() - prepare to open a possibly-encrypted regular file
  11. * @inode: the inode being opened
  12. * @filp: the struct file being set up
  13. *
  14. * Currently, an encrypted regular file can only be opened if its encryption key
  15. * is available; access to the raw encrypted contents is not supported.
  16. * Therefore, we first set up the inode's encryption key (if not already done)
  17. * and return an error if it's unavailable.
  18. *
  19. * We also verify that if the parent directory (from the path via which the file
  20. * is being opened) is encrypted, then the inode being opened uses the same
  21. * encryption policy. This is needed as part of the enforcement that all files
  22. * in an encrypted directory tree use the same encryption policy, as a
  23. * protection against certain types of offline attacks. Note that this check is
  24. * needed even when opening an *unencrypted* file, since it's forbidden to have
  25. * an unencrypted file in an encrypted directory.
  26. *
  27. * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
  28. */
  29. int fscrypt_file_open(struct inode *inode, struct file *filp)
  30. {
  31. int err;
  32. struct dentry *dentry, *dentry_parent;
  33. struct inode *inode_parent;
  34. err = fscrypt_require_key(inode);
  35. if (err)
  36. return err;
  37. dentry = file_dentry(filp);
  38. /*
  39. * Getting a reference to the parent dentry is needed for the actual
  40. * encryption policy comparison, but it's expensive on multi-core
  41. * systems. Since this function runs on unencrypted files too, start
  42. * with a lightweight RCU-mode check for the parent directory being
  43. * unencrypted (in which case it's fine for the child to be either
  44. * unencrypted, or encrypted with any policy). Only continue on to the
  45. * full policy check if the parent directory is actually encrypted.
  46. */
  47. rcu_read_lock();
  48. dentry_parent = READ_ONCE(dentry->d_parent);
  49. inode_parent = d_inode_rcu(dentry_parent);
  50. if (inode_parent != NULL && !IS_ENCRYPTED(inode_parent)) {
  51. rcu_read_unlock();
  52. return 0;
  53. }
  54. rcu_read_unlock();
  55. dentry_parent = dget_parent(dentry);
  56. if (!fscrypt_has_permitted_context(d_inode(dentry_parent), inode)) {
  57. fscrypt_warn(inode,
  58. "Inconsistent encryption context (parent directory: %lu)",
  59. d_inode(dentry_parent)->i_ino);
  60. err = -EPERM;
  61. }
  62. dput(dentry_parent);
  63. return err;
  64. }
  65. EXPORT_SYMBOL_GPL(fscrypt_file_open);
  66. int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
  67. struct dentry *dentry)
  68. {
  69. if (fscrypt_is_nokey_name(dentry))
  70. return -ENOKEY;
  71. /*
  72. * We don't need to separately check that the directory inode's key is
  73. * available, as it's implied by the dentry not being a no-key name.
  74. */
  75. if (!fscrypt_has_permitted_context(dir, inode))
  76. return -EXDEV;
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
  80. int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
  81. struct inode *new_dir, struct dentry *new_dentry,
  82. unsigned int flags)
  83. {
  84. if (fscrypt_is_nokey_name(old_dentry) ||
  85. fscrypt_is_nokey_name(new_dentry))
  86. return -ENOKEY;
  87. /*
  88. * We don't need to separately check that the directory inodes' keys are
  89. * available, as it's implied by the dentries not being no-key names.
  90. */
  91. if (old_dir != new_dir) {
  92. if (IS_ENCRYPTED(new_dir) &&
  93. !fscrypt_has_permitted_context(new_dir,
  94. d_inode(old_dentry)))
  95. return -EXDEV;
  96. if ((flags & RENAME_EXCHANGE) &&
  97. IS_ENCRYPTED(old_dir) &&
  98. !fscrypt_has_permitted_context(old_dir,
  99. d_inode(new_dentry)))
  100. return -EXDEV;
  101. }
  102. return 0;
  103. }
  104. EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
  105. int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
  106. struct fscrypt_name *fname)
  107. {
  108. int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
  109. if (err && err != -ENOENT)
  110. return err;
  111. fscrypt_prepare_dentry(dentry, fname->is_nokey_name);
  112. return err;
  113. }
  114. EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
  115. /**
  116. * fscrypt_prepare_lookup_partial() - prepare lookup without filename setup
  117. * @dir: the encrypted directory being searched
  118. * @dentry: the dentry being looked up in @dir
  119. *
  120. * This function should be used by the ->lookup and ->atomic_open methods of
  121. * filesystems that handle filename encryption and no-key name encoding
  122. * themselves and thus can't use fscrypt_prepare_lookup(). Like
  123. * fscrypt_prepare_lookup(), this will try to set up the directory's encryption
  124. * key and will set DCACHE_NOKEY_NAME on the dentry if the key is unavailable.
  125. * However, this function doesn't set up a struct fscrypt_name for the filename.
  126. *
  127. * Return: 0 on success; -errno on error. Note that the encryption key being
  128. * unavailable is not considered an error. It is also not an error if
  129. * the encryption policy is unsupported by this kernel; that is treated
  130. * like the key being unavailable, so that files can still be deleted.
  131. */
  132. int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry)
  133. {
  134. int err = fscrypt_get_encryption_info(dir, true);
  135. bool is_nokey_name = (!err && !fscrypt_has_encryption_key(dir));
  136. fscrypt_prepare_dentry(dentry, is_nokey_name);
  137. return err;
  138. }
  139. EXPORT_SYMBOL_GPL(fscrypt_prepare_lookup_partial);
  140. int __fscrypt_prepare_readdir(struct inode *dir)
  141. {
  142. return fscrypt_get_encryption_info(dir, true);
  143. }
  144. EXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir);
  145. int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr)
  146. {
  147. if (attr->ia_valid & ATTR_SIZE)
  148. return fscrypt_require_key(d_inode(dentry));
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(__fscrypt_prepare_setattr);
  152. /**
  153. * fscrypt_prepare_setflags() - prepare to change flags with FS_IOC_SETFLAGS
  154. * @inode: the inode on which flags are being changed
  155. * @oldflags: the old flags
  156. * @flags: the new flags
  157. *
  158. * The caller should be holding i_rwsem for write.
  159. *
  160. * Return: 0 on success; -errno if the flags change isn't allowed or if
  161. * another error occurs.
  162. */
  163. int fscrypt_prepare_setflags(struct inode *inode,
  164. unsigned int oldflags, unsigned int flags)
  165. {
  166. struct fscrypt_inode_info *ci;
  167. struct fscrypt_master_key *mk;
  168. int err;
  169. /*
  170. * When the CASEFOLD flag is set on an encrypted directory, we must
  171. * derive the secret key needed for the dirhash. This is only possible
  172. * if the directory uses a v2 encryption policy.
  173. */
  174. if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
  175. err = fscrypt_require_key(inode);
  176. if (err)
  177. return err;
  178. ci = fscrypt_get_inode_info_raw(inode);
  179. if (ci->ci_policy.version != FSCRYPT_POLICY_V2)
  180. return -EINVAL;
  181. mk = ci->ci_master_key;
  182. down_read(&mk->mk_sem);
  183. if (mk->mk_present)
  184. fscrypt_derive_dirhash_key(ci, mk);
  185. else
  186. err = -ENOKEY;
  187. up_read(&mk->mk_sem);
  188. return err;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
  194. * @dir: directory in which the symlink is being created
  195. * @target: plaintext symlink target
  196. * @len: length of @target excluding null terminator
  197. * @max_len: space the filesystem has available to store the symlink target
  198. * @disk_link: (out) the on-disk symlink target being prepared
  199. *
  200. * This function computes the size the symlink target will require on-disk,
  201. * stores it in @disk_link->len, and validates it against @max_len. An
  202. * encrypted symlink may be longer than the original.
  203. *
  204. * Additionally, @disk_link->name is set to @target if the symlink will be
  205. * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
  206. * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
  207. * on-disk target later. (The reason for the two-step process is that some
  208. * filesystems need to know the size of the symlink target before creating the
  209. * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
  210. *
  211. * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
  212. * -ENOKEY if the encryption key is missing, or another -errno code if a problem
  213. * occurred while setting up the encryption key.
  214. */
  215. int fscrypt_prepare_symlink(struct inode *dir, const char *target,
  216. unsigned int len, unsigned int max_len,
  217. struct fscrypt_str *disk_link)
  218. {
  219. const union fscrypt_policy *policy;
  220. /*
  221. * To calculate the size of the encrypted symlink target we need to know
  222. * the amount of NUL padding, which is determined by the flags set in
  223. * the encryption policy which will be inherited from the directory.
  224. */
  225. policy = fscrypt_policy_to_inherit(dir);
  226. if (policy == NULL) {
  227. /* Not encrypted */
  228. disk_link->name = (unsigned char *)target;
  229. disk_link->len = len + 1;
  230. if (disk_link->len > max_len)
  231. return -ENAMETOOLONG;
  232. return 0;
  233. }
  234. if (IS_ERR(policy))
  235. return PTR_ERR(policy);
  236. /*
  237. * Calculate the size of the encrypted symlink and verify it won't
  238. * exceed max_len. Note that for historical reasons, encrypted symlink
  239. * targets are prefixed with the ciphertext length, despite this
  240. * actually being redundant with i_size. This decreases by 2 bytes the
  241. * longest symlink target we can accept.
  242. *
  243. * We could recover 1 byte by not counting a null terminator, but
  244. * counting it (even though it is meaningless for ciphertext) is simpler
  245. * for now since filesystems will assume it is there and subtract it.
  246. */
  247. if (!__fscrypt_fname_encrypted_size(policy, len,
  248. max_len - sizeof(struct fscrypt_symlink_data) - 1,
  249. &disk_link->len))
  250. return -ENAMETOOLONG;
  251. disk_link->len += sizeof(struct fscrypt_symlink_data) + 1;
  252. disk_link->name = NULL;
  253. return 0;
  254. }
  255. EXPORT_SYMBOL_GPL(fscrypt_prepare_symlink);
  256. int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
  257. unsigned int len, struct fscrypt_str *disk_link)
  258. {
  259. int err;
  260. struct qstr iname = QSTR_INIT(target, len);
  261. struct fscrypt_symlink_data *sd;
  262. unsigned int ciphertext_len;
  263. /*
  264. * fscrypt_prepare_new_inode() should have already set up the new
  265. * symlink inode's encryption key. We don't wait until now to do it,
  266. * since we may be in a filesystem transaction now.
  267. */
  268. if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
  269. return -ENOKEY;
  270. if (disk_link->name) {
  271. /* filesystem-provided buffer */
  272. sd = (struct fscrypt_symlink_data *)disk_link->name;
  273. } else {
  274. sd = kmalloc(disk_link->len, GFP_NOFS);
  275. if (!sd)
  276. return -ENOMEM;
  277. }
  278. ciphertext_len = disk_link->len - sizeof(*sd) - 1;
  279. sd->len = cpu_to_le16(ciphertext_len);
  280. err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
  281. ciphertext_len);
  282. if (err)
  283. goto err_free_sd;
  284. /*
  285. * Null-terminating the ciphertext doesn't make sense, but we still
  286. * count the null terminator in the length, so we might as well
  287. * initialize it just in case the filesystem writes it out.
  288. */
  289. sd->encrypted_path[ciphertext_len] = '\0';
  290. /* Cache the plaintext symlink target for later use by get_link() */
  291. err = -ENOMEM;
  292. inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
  293. if (!inode->i_link)
  294. goto err_free_sd;
  295. if (!disk_link->name)
  296. disk_link->name = (unsigned char *)sd;
  297. return 0;
  298. err_free_sd:
  299. if (!disk_link->name)
  300. kfree(sd);
  301. return err;
  302. }
  303. EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
  304. /**
  305. * fscrypt_get_symlink() - get the target of an encrypted symlink
  306. * @inode: the symlink inode
  307. * @caddr: the on-disk contents of the symlink
  308. * @max_size: size of @caddr buffer
  309. * @done: if successful, will be set up to free the returned target if needed
  310. *
  311. * If the symlink's encryption key is available, we decrypt its target.
  312. * Otherwise, we encode its target for presentation.
  313. *
  314. * This may sleep, so the filesystem must have dropped out of RCU mode already.
  315. *
  316. * Return: the presentable symlink target or an ERR_PTR()
  317. */
  318. const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
  319. unsigned int max_size,
  320. struct delayed_call *done)
  321. {
  322. const struct fscrypt_symlink_data *sd;
  323. struct fscrypt_str cstr, pstr;
  324. bool has_key;
  325. int err;
  326. /* This is for encrypted symlinks only */
  327. if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
  328. return ERR_PTR(-EINVAL);
  329. /* If the decrypted target is already cached, just return it. */
  330. pstr.name = READ_ONCE(inode->i_link);
  331. if (pstr.name)
  332. return pstr.name;
  333. /*
  334. * Try to set up the symlink's encryption key, but we can continue
  335. * regardless of whether the key is available or not.
  336. */
  337. err = fscrypt_get_encryption_info(inode, false);
  338. if (err)
  339. return ERR_PTR(err);
  340. has_key = fscrypt_has_encryption_key(inode);
  341. /*
  342. * For historical reasons, encrypted symlink targets are prefixed with
  343. * the ciphertext length, even though this is redundant with i_size.
  344. */
  345. if (max_size < sizeof(*sd) + 1)
  346. return ERR_PTR(-EUCLEAN);
  347. sd = caddr;
  348. cstr.name = (unsigned char *)sd->encrypted_path;
  349. cstr.len = le16_to_cpu(sd->len);
  350. if (cstr.len == 0)
  351. return ERR_PTR(-EUCLEAN);
  352. if (cstr.len + sizeof(*sd) > max_size)
  353. return ERR_PTR(-EUCLEAN);
  354. err = fscrypt_fname_alloc_buffer(cstr.len, &pstr);
  355. if (err)
  356. return ERR_PTR(err);
  357. err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
  358. if (err)
  359. goto err_kfree;
  360. err = -EUCLEAN;
  361. if (pstr.name[0] == '\0')
  362. goto err_kfree;
  363. pstr.name[pstr.len] = '\0';
  364. /*
  365. * Cache decrypted symlink targets in i_link for later use. Don't cache
  366. * symlink targets encoded without the key, since those become outdated
  367. * once the key is added. This pairs with the READ_ONCE() above and in
  368. * the VFS path lookup code.
  369. */
  370. if (!has_key ||
  371. cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
  372. set_delayed_call(done, kfree_link, pstr.name);
  373. return pstr.name;
  374. err_kfree:
  375. kfree(pstr.name);
  376. return ERR_PTR(err);
  377. }
  378. EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
  379. /**
  380. * fscrypt_symlink_getattr() - set the correct st_size for encrypted symlinks
  381. * @path: the path for the encrypted symlink being queried
  382. * @stat: the struct being filled with the symlink's attributes
  383. *
  384. * Override st_size of encrypted symlinks to be the length of the decrypted
  385. * symlink target (or the no-key encoded symlink target, if the key is
  386. * unavailable) rather than the length of the encrypted symlink target. This is
  387. * necessary for st_size to match the symlink target that userspace actually
  388. * sees. POSIX requires this, and some userspace programs depend on it.
  389. *
  390. * This requires reading the symlink target from disk if needed, setting up the
  391. * inode's encryption key if possible, and then decrypting or encoding the
  392. * symlink target. This makes lstat() more heavyweight than is normally the
  393. * case. However, decrypted symlink targets will be cached in ->i_link, so
  394. * usually the symlink won't have to be read and decrypted again later if/when
  395. * it is actually followed, readlink() is called, or lstat() is called again.
  396. *
  397. * Return: 0 on success, -errno on failure
  398. */
  399. int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat)
  400. {
  401. struct dentry *dentry = path->dentry;
  402. struct inode *inode = d_inode(dentry);
  403. const char *link;
  404. DEFINE_DELAYED_CALL(done);
  405. /*
  406. * To get the symlink target that userspace will see (whether it's the
  407. * decrypted target or the no-key encoded target), we can just get it in
  408. * the same way the VFS does during path resolution and readlink().
  409. */
  410. link = READ_ONCE(inode->i_link);
  411. if (!link) {
  412. link = inode->i_op->get_link(dentry, inode, &done);
  413. if (IS_ERR(link))
  414. return PTR_ERR(link);
  415. }
  416. stat->size = strlen(link);
  417. do_delayed_call(&done);
  418. return 0;
  419. }
  420. EXPORT_SYMBOL_GPL(fscrypt_symlink_getattr);