PageRenderTime 865ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/fs/ecryptfs/mmap.c

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