mmap.c 16 KB

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