PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ecryptfs/mmap.c

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