PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ecryptfs/mmap.c

https://github.com/dmitriy103/bravo_kernel-2.6.35
C | 557 lines | 403 code | 34 blank | 120 comment | 63 complexity | e36df198f3ef7ce62bde7233b95feacb 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. rc = ecryptfs_encrypt_page(page);
  62. if (rc) {
  63. ecryptfs_printk(KERN_WARNING, "Error encrypting "
  64. "page (upper index [0x%.16x])\n", page->index);
  65. ClearPageUptodate(page);
  66. goto out;
  67. }
  68. SetPageUptodate(page);
  69. unlock_page(page);
  70. out:
  71. return rc;
  72. }
  73. static void strip_xattr_flag(char *page_virt,
  74. struct ecryptfs_crypt_stat *crypt_stat)
  75. {
  76. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  77. size_t written;
  78. crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
  79. ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
  80. &written);
  81. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  82. }
  83. }
  84. /**
  85. * Header Extent:
  86. * Octets 0-7: Unencrypted file size (big-endian)
  87. * Octets 8-15: eCryptfs special marker
  88. * Octets 16-19: Flags
  89. * Octet 16: File format version number (between 0 and 255)
  90. * Octets 17-18: Reserved
  91. * Octet 19: Bit 1 (lsb): Reserved
  92. * Bit 2: Encrypted?
  93. * Bits 3-8: Reserved
  94. * Octets 20-23: Header extent size (big-endian)
  95. * Octets 24-25: Number of header extents at front of file
  96. * (big-endian)
  97. * Octet 26: Begin RFC 2440 authentication token packet set
  98. */
  99. /**
  100. * ecryptfs_copy_up_encrypted_with_header
  101. * @page: Sort of a ``virtual'' representation of the encrypted lower
  102. * file. The actual lower file does not have the metadata in
  103. * the header. This is locked.
  104. * @crypt_stat: The eCryptfs inode's cryptographic context
  105. *
  106. * The ``view'' is the version of the file that userspace winds up
  107. * seeing, with the header information inserted.
  108. */
  109. static int
  110. ecryptfs_copy_up_encrypted_with_header(struct page *page,
  111. struct ecryptfs_crypt_stat *crypt_stat)
  112. {
  113. loff_t extent_num_in_page = 0;
  114. loff_t num_extents_per_page = (PAGE_CACHE_SIZE
  115. / crypt_stat->extent_size);
  116. int rc = 0;
  117. while (extent_num_in_page < num_extents_per_page) {
  118. loff_t view_extent_num = ((((loff_t)page->index)
  119. * num_extents_per_page)
  120. + extent_num_in_page);
  121. size_t num_header_extents_at_front =
  122. (crypt_stat->metadata_size / crypt_stat->extent_size);
  123. if (view_extent_num < num_header_extents_at_front) {
  124. /* This is a header extent */
  125. char *page_virt;
  126. page_virt = kmap_atomic(page, KM_USER0);
  127. memset(page_virt, 0, PAGE_CACHE_SIZE);
  128. /* TODO: Support more than one header extent */
  129. if (view_extent_num == 0) {
  130. size_t written;
  131. rc = ecryptfs_read_xattr_region(
  132. page_virt, page->mapping->host);
  133. strip_xattr_flag(page_virt + 16, crypt_stat);
  134. ecryptfs_write_header_metadata(page_virt + 20,
  135. crypt_stat,
  136. &written);
  137. }
  138. kunmap_atomic(page_virt, KM_USER0);
  139. flush_dcache_page(page);
  140. if (rc) {
  141. printk(KERN_ERR "%s: Error reading xattr "
  142. "region; rc = [%d]\n", __func__, rc);
  143. goto out;
  144. }
  145. } else {
  146. /* This is an encrypted data extent */
  147. loff_t lower_offset =
  148. ((view_extent_num * crypt_stat->extent_size)
  149. - crypt_stat->metadata_size);
  150. rc = ecryptfs_read_lower_page_segment(
  151. page, (lower_offset >> PAGE_CACHE_SHIFT),
  152. (lower_offset & ~PAGE_CACHE_MASK),
  153. crypt_stat->extent_size, page->mapping->host);
  154. if (rc) {
  155. printk(KERN_ERR "%s: Error attempting to read "
  156. "extent at offset [%lld] in the lower "
  157. "file; rc = [%d]\n", __func__,
  158. lower_offset, rc);
  159. goto out;
  160. }
  161. }
  162. extent_num_in_page++;
  163. }
  164. out:
  165. return rc;
  166. }
  167. /**
  168. * ecryptfs_readpage
  169. * @file: An eCryptfs file
  170. * @page: Page from eCryptfs inode mapping into which to stick the read data
  171. *
  172. * Read in a page, decrypting if necessary.
  173. *
  174. * Returns zero on success; non-zero on error.
  175. */
  176. static int ecryptfs_readpage(struct file *file, struct page *page)
  177. {
  178. struct ecryptfs_crypt_stat *crypt_stat =
  179. &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
  180. int rc = 0;
  181. if (!crypt_stat
  182. || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  183. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  184. ecryptfs_printk(KERN_DEBUG,
  185. "Passing through unencrypted page\n");
  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%.16x]\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. 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. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  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 attemping 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. rc = ecryptfs_decrypt_page(page);
  317. if (rc) {
  318. printk(KERN_ERR "%s: Error decrypting page "
  319. "at index [%ld]; rc = [%d]\n",
  320. __func__, page->index, rc);
  321. ClearPageUptodate(page);
  322. goto out;
  323. }
  324. SetPageUptodate(page);
  325. }
  326. }
  327. prev_page_end_size = ((loff_t)index << PAGE_CACHE_SHIFT);
  328. /* If creating a page or more of holes, zero them out via truncate.
  329. * Note, this will increase i_size. */
  330. if (index != 0) {
  331. if (prev_page_end_size > i_size_read(page->mapping->host)) {
  332. rc = ecryptfs_truncate(file->f_path.dentry,
  333. prev_page_end_size);
  334. if (rc) {
  335. printk(KERN_ERR "%s: Error on attempt to "
  336. "truncate to (higher) offset [%lld];"
  337. " rc = [%d]\n", __func__,
  338. prev_page_end_size, rc);
  339. goto out;
  340. }
  341. }
  342. }
  343. /* Writing to a new page, and creating a small hole from start
  344. * of page? Zero it out. */
  345. if ((i_size_read(mapping->host) == prev_page_end_size)
  346. && (pos != 0))
  347. zero_user(page, 0, PAGE_CACHE_SIZE);
  348. out:
  349. if (unlikely(rc)) {
  350. unlock_page(page);
  351. page_cache_release(page);
  352. *pagep = NULL;
  353. }
  354. return rc;
  355. }
  356. /**
  357. * ecryptfs_write_inode_size_to_header
  358. *
  359. * Writes the lower file size to the first 8 bytes of the header.
  360. *
  361. * Returns zero on success; non-zero on error.
  362. */
  363. static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
  364. {
  365. char *file_size_virt;
  366. int rc;
  367. file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
  368. if (!file_size_virt) {
  369. rc = -ENOMEM;
  370. goto out;
  371. }
  372. put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
  373. rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
  374. sizeof(u64));
  375. kfree(file_size_virt);
  376. if (rc < 0)
  377. printk(KERN_ERR "%s: Error writing file size to header; "
  378. "rc = [%d]\n", __func__, rc);
  379. else
  380. rc = 0;
  381. out:
  382. return rc;
  383. }
  384. struct kmem_cache *ecryptfs_xattr_cache;
  385. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  386. {
  387. ssize_t size;
  388. void *xattr_virt;
  389. struct dentry *lower_dentry =
  390. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  391. struct inode *lower_inode = lower_dentry->d_inode;
  392. int rc;
  393. if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
  394. printk(KERN_WARNING
  395. "No support for setting xattr in lower filesystem\n");
  396. rc = -ENOSYS;
  397. goto out;
  398. }
  399. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  400. if (!xattr_virt) {
  401. printk(KERN_ERR "Out of memory whilst attempting to write "
  402. "inode size to xattr\n");
  403. rc = -ENOMEM;
  404. goto out;
  405. }
  406. mutex_lock(&lower_inode->i_mutex);
  407. size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  408. xattr_virt, PAGE_CACHE_SIZE);
  409. if (size < 0)
  410. size = 8;
  411. put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
  412. rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  413. xattr_virt, size, 0);
  414. mutex_unlock(&lower_inode->i_mutex);
  415. if (rc)
  416. printk(KERN_ERR "Error whilst attempting to write inode size "
  417. "to lower file xattr; rc = [%d]\n", rc);
  418. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  419. out:
  420. return rc;
  421. }
  422. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  423. {
  424. struct ecryptfs_crypt_stat *crypt_stat;
  425. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  426. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  427. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  428. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  429. else
  430. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  431. }
  432. /**
  433. * ecryptfs_write_end
  434. * @file: The eCryptfs file object
  435. * @mapping: The eCryptfs object
  436. * @pos: The file position
  437. * @len: The length of the data (unused)
  438. * @copied: The amount of data copied
  439. * @page: The eCryptfs page
  440. * @fsdata: The fsdata (unused)
  441. *
  442. * This is where we encrypt the data and pass the encrypted data to
  443. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  444. * entire underlying packets.
  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_CACHE_SHIFT;
  452. unsigned from = pos & (PAGE_CACHE_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. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  459. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  460. "crypt_stat at memory location [%p]\n", crypt_stat);
  461. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  462. } else
  463. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  464. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  465. "(page w/ index = [0x%.16x], to = [%d])\n", index, to);
  466. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  467. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
  468. to);
  469. if (!rc) {
  470. rc = copied;
  471. fsstack_copy_inode_size(ecryptfs_inode,
  472. ecryptfs_inode_to_lower(ecryptfs_inode));
  473. }
  474. goto out;
  475. }
  476. /* Fills in zeros if 'to' goes beyond inode size */
  477. rc = fill_zeros_to_end_of_page(page, to);
  478. if (rc) {
  479. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  480. "zeros in page with index = [0x%.16x]\n", index);
  481. goto out;
  482. }
  483. rc = ecryptfs_encrypt_page(page);
  484. if (rc) {
  485. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  486. "index [0x%.16x])\n", index);
  487. goto out;
  488. }
  489. if (pos + copied > i_size_read(ecryptfs_inode)) {
  490. i_size_write(ecryptfs_inode, pos + copied);
  491. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  492. "[0x%.16x]\n", i_size_read(ecryptfs_inode));
  493. }
  494. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  495. if (rc)
  496. printk(KERN_ERR "Error writing inode size to metadata; "
  497. "rc = [%d]\n", rc);
  498. else
  499. rc = copied;
  500. out:
  501. unlock_page(page);
  502. page_cache_release(page);
  503. return rc;
  504. }
  505. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  506. {
  507. int rc = 0;
  508. struct inode *inode;
  509. struct inode *lower_inode;
  510. inode = (struct inode *)mapping->host;
  511. lower_inode = ecryptfs_inode_to_lower(inode);
  512. if (lower_inode->i_mapping->a_ops->bmap)
  513. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  514. block);
  515. return rc;
  516. }
  517. const struct address_space_operations ecryptfs_aops = {
  518. .writepage = ecryptfs_writepage,
  519. .readpage = ecryptfs_readpage,
  520. .write_begin = ecryptfs_write_begin,
  521. .write_end = ecryptfs_write_end,
  522. .bmap = ecryptfs_bmap,
  523. };