mmap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * eCryptfs: Linux filesystem encryption layer
  4. * This is where eCryptfs coordinates the symmetric encryption and
  5. * decryption of the file data as it passes between the lower
  6. * encrypted file and the upper decrypted file.
  7. *
  8. * Copyright (C) 1997-2003 Erez Zadok
  9. * Copyright (C) 2001-2003 Stony Brook University
  10. * Copyright (C) 2004-2007 International Business Machines Corp.
  11. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  12. */
  13. #include <linux/pagemap.h>
  14. #include <linux/writeback.h>
  15. #include <linux/page-flags.h>
  16. #include <linux/mount.h>
  17. #include <linux/file.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/slab.h>
  20. #include <linux/xattr.h>
  21. #include <linux/unaligned.h>
  22. #include "ecryptfs_kernel.h"
  23. /*
  24. * ecryptfs_get_locked_page
  25. *
  26. * Get one page from cache or lower f/s, return error otherwise.
  27. *
  28. * Returns locked and up-to-date page (if ok), with increased
  29. * refcnt.
  30. */
  31. struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
  32. {
  33. struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
  34. if (!IS_ERR(page))
  35. lock_page(page);
  36. return page;
  37. }
  38. /**
  39. * ecryptfs_writepage
  40. * @page: Page that is locked before this call is made
  41. * @wbc: Write-back control structure
  42. *
  43. * Returns zero on success; non-zero otherwise
  44. *
  45. * This is where we encrypt the data and pass the encrypted data to
  46. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  47. * entire underlying packets.
  48. */
  49. static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
  50. {
  51. int rc;
  52. rc = ecryptfs_encrypt_page(page);
  53. if (rc) {
  54. ecryptfs_printk(KERN_WARNING, "Error encrypting "
  55. "page (upper index [0x%.16lx])\n", page->index);
  56. ClearPageUptodate(page);
  57. goto out;
  58. }
  59. SetPageUptodate(page);
  60. out:
  61. unlock_page(page);
  62. return rc;
  63. }
  64. static void strip_xattr_flag(char *page_virt,
  65. struct ecryptfs_crypt_stat *crypt_stat)
  66. {
  67. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  68. size_t written;
  69. crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
  70. ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
  71. &written);
  72. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  73. }
  74. }
  75. /*
  76. * Header Extent:
  77. * Octets 0-7: Unencrypted file size (big-endian)
  78. * Octets 8-15: eCryptfs special marker
  79. * Octets 16-19: Flags
  80. * Octet 16: File format version number (between 0 and 255)
  81. * Octets 17-18: Reserved
  82. * Octet 19: Bit 1 (lsb): Reserved
  83. * Bit 2: Encrypted?
  84. * Bits 3-8: Reserved
  85. * Octets 20-23: Header extent size (big-endian)
  86. * Octets 24-25: Number of header extents at front of file
  87. * (big-endian)
  88. * Octet 26: Begin RFC 2440 authentication token packet set
  89. */
  90. /**
  91. * ecryptfs_copy_up_encrypted_with_header
  92. * @page: Sort of a ``virtual'' representation of the encrypted lower
  93. * file. The actual lower file does not have the metadata in
  94. * the header. This is locked.
  95. * @crypt_stat: The eCryptfs inode's cryptographic context
  96. *
  97. * The ``view'' is the version of the file that userspace winds up
  98. * seeing, with the header information inserted.
  99. */
  100. static int
  101. ecryptfs_copy_up_encrypted_with_header(struct page *page,
  102. struct ecryptfs_crypt_stat *crypt_stat)
  103. {
  104. loff_t extent_num_in_page = 0;
  105. loff_t num_extents_per_page = (PAGE_SIZE
  106. / crypt_stat->extent_size);
  107. int rc = 0;
  108. while (extent_num_in_page < num_extents_per_page) {
  109. loff_t view_extent_num = ((((loff_t)page->index)
  110. * num_extents_per_page)
  111. + extent_num_in_page);
  112. size_t num_header_extents_at_front =
  113. (crypt_stat->metadata_size / crypt_stat->extent_size);
  114. if (view_extent_num < num_header_extents_at_front) {
  115. /* This is a header extent */
  116. char *page_virt;
  117. page_virt = kmap_local_page(page);
  118. memset(page_virt, 0, PAGE_SIZE);
  119. /* TODO: Support more than one header extent */
  120. if (view_extent_num == 0) {
  121. size_t written;
  122. rc = ecryptfs_read_xattr_region(
  123. page_virt, page->mapping->host);
  124. strip_xattr_flag(page_virt + 16, crypt_stat);
  125. ecryptfs_write_header_metadata(page_virt + 20,
  126. crypt_stat,
  127. &written);
  128. }
  129. kunmap_local(page_virt);
  130. flush_dcache_page(page);
  131. if (rc) {
  132. printk(KERN_ERR "%s: Error reading xattr "
  133. "region; rc = [%d]\n", __func__, rc);
  134. goto out;
  135. }
  136. } else {
  137. /* This is an encrypted data extent */
  138. loff_t lower_offset =
  139. ((view_extent_num * crypt_stat->extent_size)
  140. - crypt_stat->metadata_size);
  141. rc = ecryptfs_read_lower_page_segment(
  142. page, (lower_offset >> PAGE_SHIFT),
  143. (lower_offset & ~PAGE_MASK),
  144. crypt_stat->extent_size, page->mapping->host);
  145. if (rc) {
  146. printk(KERN_ERR "%s: Error attempting to read "
  147. "extent at offset [%lld] in the lower "
  148. "file; rc = [%d]\n", __func__,
  149. lower_offset, rc);
  150. goto out;
  151. }
  152. }
  153. extent_num_in_page++;
  154. }
  155. out:
  156. return rc;
  157. }
  158. /**
  159. * ecryptfs_read_folio
  160. * @file: An eCryptfs file
  161. * @folio: Folio from eCryptfs inode mapping into which to stick the read data
  162. *
  163. * Read in a folio, decrypting if necessary.
  164. *
  165. * Returns zero on success; non-zero on error.
  166. */
  167. static int ecryptfs_read_folio(struct file *file, struct folio *folio)
  168. {
  169. struct page *page = &folio->page;
  170. struct ecryptfs_crypt_stat *crypt_stat =
  171. &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
  172. int rc = 0;
  173. if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  174. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  175. PAGE_SIZE,
  176. page->mapping->host);
  177. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  178. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  179. rc = ecryptfs_copy_up_encrypted_with_header(page,
  180. crypt_stat);
  181. if (rc) {
  182. printk(KERN_ERR "%s: Error attempting to copy "
  183. "the encrypted content from the lower "
  184. "file whilst inserting the metadata "
  185. "from the xattr into the header; rc = "
  186. "[%d]\n", __func__, rc);
  187. goto out;
  188. }
  189. } else {
  190. rc = ecryptfs_read_lower_page_segment(
  191. page, page->index, 0, PAGE_SIZE,
  192. page->mapping->host);
  193. if (rc) {
  194. printk(KERN_ERR "Error reading page; rc = "
  195. "[%d]\n", rc);
  196. goto out;
  197. }
  198. }
  199. } else {
  200. rc = ecryptfs_decrypt_page(page);
  201. if (rc) {
  202. ecryptfs_printk(KERN_ERR, "Error decrypting page; "
  203. "rc = [%d]\n", rc);
  204. goto out;
  205. }
  206. }
  207. out:
  208. if (rc)
  209. ClearPageUptodate(page);
  210. else
  211. SetPageUptodate(page);
  212. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
  213. page->index);
  214. unlock_page(page);
  215. return rc;
  216. }
  217. /*
  218. * Called with lower inode mutex held.
  219. */
  220. static int fill_zeros_to_end_of_page(struct folio *folio, unsigned int to)
  221. {
  222. struct inode *inode = folio->mapping->host;
  223. int end_byte_in_page;
  224. if ((i_size_read(inode) / PAGE_SIZE) != folio->index)
  225. goto out;
  226. end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
  227. if (to > end_byte_in_page)
  228. end_byte_in_page = to;
  229. folio_zero_segment(folio, end_byte_in_page, PAGE_SIZE);
  230. out:
  231. return 0;
  232. }
  233. /**
  234. * ecryptfs_write_begin
  235. * @file: The eCryptfs file
  236. * @mapping: The eCryptfs object
  237. * @pos: The file offset at which to start writing
  238. * @len: Length of the write
  239. * @foliop: Pointer to return the folio
  240. * @fsdata: Pointer to return fs data (unused)
  241. *
  242. * This function must zero any hole we create
  243. *
  244. * Returns zero on success; non-zero otherwise
  245. */
  246. static int ecryptfs_write_begin(struct file *file,
  247. struct address_space *mapping,
  248. loff_t pos, unsigned len,
  249. struct folio **foliop, void **fsdata)
  250. {
  251. pgoff_t index = pos >> PAGE_SHIFT;
  252. struct folio *folio;
  253. loff_t prev_page_end_size;
  254. int rc = 0;
  255. folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
  256. mapping_gfp_mask(mapping));
  257. if (IS_ERR(folio))
  258. return PTR_ERR(folio);
  259. *foliop = folio;
  260. prev_page_end_size = ((loff_t)index << PAGE_SHIFT);
  261. if (!folio_test_uptodate(folio)) {
  262. struct ecryptfs_crypt_stat *crypt_stat =
  263. &ecryptfs_inode_to_private(mapping->host)->crypt_stat;
  264. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  265. rc = ecryptfs_read_lower_page_segment(
  266. &folio->page, index, 0, PAGE_SIZE, mapping->host);
  267. if (rc) {
  268. printk(KERN_ERR "%s: Error attempting to read "
  269. "lower page segment; rc = [%d]\n",
  270. __func__, rc);
  271. folio_clear_uptodate(folio);
  272. goto out;
  273. } else
  274. folio_mark_uptodate(folio);
  275. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  276. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  277. rc = ecryptfs_copy_up_encrypted_with_header(
  278. &folio->page, crypt_stat);
  279. if (rc) {
  280. printk(KERN_ERR "%s: Error attempting "
  281. "to copy the encrypted content "
  282. "from the lower file whilst "
  283. "inserting the metadata from "
  284. "the xattr into the header; rc "
  285. "= [%d]\n", __func__, rc);
  286. folio_clear_uptodate(folio);
  287. goto out;
  288. }
  289. folio_mark_uptodate(folio);
  290. } else {
  291. rc = ecryptfs_read_lower_page_segment(
  292. &folio->page, index, 0, PAGE_SIZE,
  293. mapping->host);
  294. if (rc) {
  295. printk(KERN_ERR "%s: Error reading "
  296. "page; rc = [%d]\n",
  297. __func__, rc);
  298. folio_clear_uptodate(folio);
  299. goto out;
  300. }
  301. folio_mark_uptodate(folio);
  302. }
  303. } else {
  304. if (prev_page_end_size
  305. >= i_size_read(mapping->host)) {
  306. folio_zero_range(folio, 0, PAGE_SIZE);
  307. folio_mark_uptodate(folio);
  308. } else if (len < PAGE_SIZE) {
  309. rc = ecryptfs_decrypt_page(&folio->page);
  310. if (rc) {
  311. printk(KERN_ERR "%s: Error decrypting "
  312. "page at index [%ld]; "
  313. "rc = [%d]\n",
  314. __func__, folio->index, rc);
  315. folio_clear_uptodate(folio);
  316. goto out;
  317. }
  318. folio_mark_uptodate(folio);
  319. }
  320. }
  321. }
  322. /* If creating a page or more of holes, zero them out via truncate.
  323. * Note, this will increase i_size. */
  324. if (index != 0) {
  325. if (prev_page_end_size > i_size_read(mapping->host)) {
  326. rc = ecryptfs_truncate(file->f_path.dentry,
  327. prev_page_end_size);
  328. if (rc) {
  329. printk(KERN_ERR "%s: Error on attempt to "
  330. "truncate to (higher) offset [%lld];"
  331. " rc = [%d]\n", __func__,
  332. prev_page_end_size, rc);
  333. goto out;
  334. }
  335. }
  336. }
  337. /* Writing to a new page, and creating a small hole from start
  338. * of page? Zero it out. */
  339. if ((i_size_read(mapping->host) == prev_page_end_size)
  340. && (pos != 0))
  341. folio_zero_range(folio, 0, PAGE_SIZE);
  342. out:
  343. if (unlikely(rc)) {
  344. folio_unlock(folio);
  345. folio_put(folio);
  346. }
  347. return rc;
  348. }
  349. /*
  350. * ecryptfs_write_inode_size_to_header
  351. *
  352. * Writes the lower file size to the first 8 bytes of the header.
  353. *
  354. * Returns zero on success; non-zero on error.
  355. */
  356. static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
  357. {
  358. char *file_size_virt;
  359. int rc;
  360. file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
  361. if (!file_size_virt) {
  362. rc = -ENOMEM;
  363. goto out;
  364. }
  365. put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
  366. rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
  367. sizeof(u64));
  368. kfree(file_size_virt);
  369. if (rc < 0)
  370. printk(KERN_ERR "%s: Error writing file size to header; "
  371. "rc = [%d]\n", __func__, rc);
  372. else
  373. rc = 0;
  374. out:
  375. return rc;
  376. }
  377. struct kmem_cache *ecryptfs_xattr_cache;
  378. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  379. {
  380. ssize_t size;
  381. void *xattr_virt;
  382. struct dentry *lower_dentry =
  383. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
  384. struct inode *lower_inode = d_inode(lower_dentry);
  385. int rc;
  386. if (!(lower_inode->i_opflags & IOP_XATTR)) {
  387. printk(KERN_WARNING
  388. "No support for setting xattr in lower filesystem\n");
  389. rc = -ENOSYS;
  390. goto out;
  391. }
  392. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  393. if (!xattr_virt) {
  394. rc = -ENOMEM;
  395. goto out;
  396. }
  397. inode_lock(lower_inode);
  398. size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
  399. xattr_virt, PAGE_SIZE);
  400. if (size < 0)
  401. size = 8;
  402. put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
  403. rc = __vfs_setxattr(&nop_mnt_idmap, lower_dentry, lower_inode,
  404. ECRYPTFS_XATTR_NAME, xattr_virt, size, 0);
  405. inode_unlock(lower_inode);
  406. if (rc)
  407. printk(KERN_ERR "Error whilst attempting to write inode size "
  408. "to lower file xattr; rc = [%d]\n", rc);
  409. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  410. out:
  411. return rc;
  412. }
  413. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  414. {
  415. struct ecryptfs_crypt_stat *crypt_stat;
  416. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  417. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  418. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  419. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  420. else
  421. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  422. }
  423. /**
  424. * ecryptfs_write_end
  425. * @file: The eCryptfs file object
  426. * @mapping: The eCryptfs object
  427. * @pos: The file position
  428. * @len: The length of the data (unused)
  429. * @copied: The amount of data copied
  430. * @folio: The eCryptfs folio
  431. * @fsdata: The fsdata (unused)
  432. */
  433. static int ecryptfs_write_end(struct file *file,
  434. struct address_space *mapping,
  435. loff_t pos, unsigned len, unsigned copied,
  436. struct folio *folio, void *fsdata)
  437. {
  438. pgoff_t index = pos >> PAGE_SHIFT;
  439. unsigned from = pos & (PAGE_SIZE - 1);
  440. unsigned to = from + copied;
  441. struct inode *ecryptfs_inode = mapping->host;
  442. struct ecryptfs_crypt_stat *crypt_stat =
  443. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  444. int rc;
  445. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  446. "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
  447. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  448. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
  449. &folio->page, 0, to);
  450. if (!rc) {
  451. rc = copied;
  452. fsstack_copy_inode_size(ecryptfs_inode,
  453. ecryptfs_inode_to_lower(ecryptfs_inode));
  454. }
  455. goto out;
  456. }
  457. if (!folio_test_uptodate(folio)) {
  458. if (copied < PAGE_SIZE) {
  459. rc = 0;
  460. goto out;
  461. }
  462. folio_mark_uptodate(folio);
  463. }
  464. /* Fills in zeros if 'to' goes beyond inode size */
  465. rc = fill_zeros_to_end_of_page(folio, to);
  466. if (rc) {
  467. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  468. "zeros in page with index = [0x%.16lx]\n", index);
  469. goto out;
  470. }
  471. rc = ecryptfs_encrypt_page(&folio->page);
  472. if (rc) {
  473. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  474. "index [0x%.16lx])\n", index);
  475. goto out;
  476. }
  477. if (pos + copied > i_size_read(ecryptfs_inode)) {
  478. i_size_write(ecryptfs_inode, pos + copied);
  479. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  480. "[0x%.16llx]\n",
  481. (unsigned long long)i_size_read(ecryptfs_inode));
  482. }
  483. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  484. if (rc)
  485. printk(KERN_ERR "Error writing inode size to metadata; "
  486. "rc = [%d]\n", rc);
  487. else
  488. rc = copied;
  489. out:
  490. folio_unlock(folio);
  491. folio_put(folio);
  492. return rc;
  493. }
  494. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  495. {
  496. struct inode *lower_inode = ecryptfs_inode_to_lower(mapping->host);
  497. int ret = bmap(lower_inode, &block);
  498. if (ret)
  499. return 0;
  500. return block;
  501. }
  502. #include <linux/buffer_head.h>
  503. const struct address_space_operations ecryptfs_aops = {
  504. /*
  505. * XXX: This is pretty broken for multiple reasons: ecryptfs does not
  506. * actually use buffer_heads, and ecryptfs will crash without
  507. * CONFIG_BLOCK. But it matches the behavior before the default for
  508. * address_space_operations without the ->dirty_folio method was
  509. * cleaned up, so this is the best we can do without maintainer
  510. * feedback.
  511. */
  512. #ifdef CONFIG_BLOCK
  513. .dirty_folio = block_dirty_folio,
  514. .invalidate_folio = block_invalidate_folio,
  515. #endif
  516. .writepage = ecryptfs_writepage,
  517. .read_folio = ecryptfs_read_folio,
  518. .write_begin = ecryptfs_write_begin,
  519. .write_end = ecryptfs_write_end,
  520. .bmap = ecryptfs_bmap,
  521. };