mmap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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/scatterlist.h>
  33. #include <linux/slab.h>
  34. #include <linux/xattr.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_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_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_SHIFT),
  156. (lower_offset & ~PAGE_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_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_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_SIZE) != page->index)
  237. goto out;
  238. end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
  239. if (to > end_byte_in_page)
  240. end_byte_in_page = to;
  241. zero_user_segment(page, end_byte_in_page, PAGE_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_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_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_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_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_SIZE);
  319. SetPageUptodate(page);
  320. } else if (len < PAGE_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_SIZE);
  354. out:
  355. if (unlikely(rc)) {
  356. unlock_page(page);
  357. put_page(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_opflags & IOP_XATTR)) {
  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. rc = -ENOMEM;
  408. goto out;
  409. }
  410. inode_lock(lower_inode);
  411. size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
  412. xattr_virt, PAGE_SIZE);
  413. if (size < 0)
  414. size = 8;
  415. put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
  416. rc = __vfs_setxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
  417. xattr_virt, size, 0);
  418. inode_unlock(lower_inode);
  419. if (rc)
  420. printk(KERN_ERR "Error whilst attempting to write inode size "
  421. "to lower file xattr; rc = [%d]\n", rc);
  422. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  423. out:
  424. return rc;
  425. }
  426. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  427. {
  428. struct ecryptfs_crypt_stat *crypt_stat;
  429. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  430. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  431. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  432. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  433. else
  434. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  435. }
  436. /**
  437. * ecryptfs_write_end
  438. * @file: The eCryptfs file object
  439. * @mapping: The eCryptfs object
  440. * @pos: The file position
  441. * @len: The length of the data (unused)
  442. * @copied: The amount of data copied
  443. * @page: The eCryptfs page
  444. * @fsdata: The fsdata (unused)
  445. */
  446. static int ecryptfs_write_end(struct file *file,
  447. struct address_space *mapping,
  448. loff_t pos, unsigned len, unsigned copied,
  449. struct page *page, void *fsdata)
  450. {
  451. pgoff_t index = pos >> PAGE_SHIFT;
  452. unsigned from = pos & (PAGE_SIZE - 1);
  453. unsigned to = from + copied;
  454. struct inode *ecryptfs_inode = mapping->host;
  455. struct ecryptfs_crypt_stat *crypt_stat =
  456. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  457. int rc;
  458. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  459. "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
  460. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  461. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
  462. to);
  463. if (!rc) {
  464. rc = copied;
  465. fsstack_copy_inode_size(ecryptfs_inode,
  466. ecryptfs_inode_to_lower(ecryptfs_inode));
  467. }
  468. goto out;
  469. }
  470. if (!PageUptodate(page)) {
  471. if (copied < PAGE_SIZE) {
  472. rc = 0;
  473. goto out;
  474. }
  475. SetPageUptodate(page);
  476. }
  477. /* Fills in zeros if 'to' goes beyond inode size */
  478. rc = fill_zeros_to_end_of_page(page, to);
  479. if (rc) {
  480. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  481. "zeros in page with index = [0x%.16lx]\n", index);
  482. goto out;
  483. }
  484. rc = ecryptfs_encrypt_page(page);
  485. if (rc) {
  486. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  487. "index [0x%.16lx])\n", index);
  488. goto out;
  489. }
  490. if (pos + copied > i_size_read(ecryptfs_inode)) {
  491. i_size_write(ecryptfs_inode, pos + copied);
  492. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  493. "[0x%.16llx]\n",
  494. (unsigned long long)i_size_read(ecryptfs_inode));
  495. }
  496. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  497. if (rc)
  498. printk(KERN_ERR "Error writing inode size to metadata; "
  499. "rc = [%d]\n", rc);
  500. else
  501. rc = copied;
  502. out:
  503. unlock_page(page);
  504. put_page(page);
  505. return rc;
  506. }
  507. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  508. {
  509. int rc = 0;
  510. struct inode *inode;
  511. struct inode *lower_inode;
  512. inode = (struct inode *)mapping->host;
  513. lower_inode = ecryptfs_inode_to_lower(inode);
  514. if (lower_inode->i_mapping->a_ops->bmap)
  515. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  516. block);
  517. return rc;
  518. }
  519. const struct address_space_operations ecryptfs_aops = {
  520. .writepage = ecryptfs_writepage,
  521. .readpage = ecryptfs_readpage,
  522. .write_begin = ecryptfs_write_begin,
  523. .write_end = ecryptfs_write_end,
  524. .bmap = ecryptfs_bmap,
  525. };