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