hooks.c 15 KB

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