mmap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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(file->f_path.dentry->d_inode)->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(
  282. file->f_path.dentry->d_inode)->crypt_stat;
  283. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  284. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  285. rc = ecryptfs_read_lower_page_segment(
  286. page, index, 0, PAGE_CACHE_SIZE, mapping->host);
  287. if (rc) {
  288. printk(KERN_ERR "%s: Error attemping to read "
  289. "lower page segment; rc = [%d]\n",
  290. __func__, rc);
  291. ClearPageUptodate(page);
  292. goto out;
  293. } else
  294. SetPageUptodate(page);
  295. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  296. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  297. rc = ecryptfs_copy_up_encrypted_with_header(
  298. page, crypt_stat);
  299. if (rc) {
  300. printk(KERN_ERR "%s: Error attempting "
  301. "to copy the encrypted content "
  302. "from the lower file whilst "
  303. "inserting the metadata from "
  304. "the xattr into the header; rc "
  305. "= [%d]\n", __func__, rc);
  306. ClearPageUptodate(page);
  307. goto out;
  308. }
  309. SetPageUptodate(page);
  310. } else {
  311. rc = ecryptfs_read_lower_page_segment(
  312. page, index, 0, PAGE_CACHE_SIZE,
  313. mapping->host);
  314. if (rc) {
  315. printk(KERN_ERR "%s: Error reading "
  316. "page; rc = [%d]\n",
  317. __func__, rc);
  318. ClearPageUptodate(page);
  319. goto out;
  320. }
  321. SetPageUptodate(page);
  322. }
  323. } else {
  324. rc = ecryptfs_decrypt_page(page);
  325. if (rc) {
  326. printk(KERN_ERR "%s: Error decrypting page "
  327. "at index [%ld]; rc = [%d]\n",
  328. __func__, page->index, rc);
  329. ClearPageUptodate(page);
  330. goto out;
  331. }
  332. SetPageUptodate(page);
  333. }
  334. }
  335. prev_page_end_size = ((loff_t)index << PAGE_CACHE_SHIFT);
  336. /* If creating a page or more of holes, zero them out via truncate.
  337. * Note, this will increase i_size. */
  338. if (index != 0) {
  339. if (prev_page_end_size > i_size_read(page->mapping->host)) {
  340. rc = ecryptfs_truncate(file->f_path.dentry,
  341. prev_page_end_size);
  342. if (rc) {
  343. printk(KERN_ERR "%s: Error on attempt to "
  344. "truncate to (higher) offset [%lld];"
  345. " rc = [%d]\n", __func__,
  346. prev_page_end_size, rc);
  347. goto out;
  348. }
  349. }
  350. }
  351. /* Writing to a new page, and creating a small hole from start
  352. * of page? Zero it out. */
  353. if ((i_size_read(mapping->host) == prev_page_end_size)
  354. && (pos != 0))
  355. zero_user(page, 0, PAGE_CACHE_SIZE);
  356. out:
  357. return rc;
  358. }
  359. /**
  360. * ecryptfs_write_inode_size_to_header
  361. *
  362. * Writes the lower file size to the first 8 bytes of the header.
  363. *
  364. * Returns zero on success; non-zero on error.
  365. */
  366. static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
  367. {
  368. char *file_size_virt;
  369. int rc;
  370. file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
  371. if (!file_size_virt) {
  372. rc = -ENOMEM;
  373. goto out;
  374. }
  375. put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
  376. rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
  377. sizeof(u64));
  378. kfree(file_size_virt);
  379. if (rc < 0)
  380. printk(KERN_ERR "%s: Error writing file size to header; "
  381. "rc = [%d]\n", __func__, rc);
  382. else
  383. rc = 0;
  384. out:
  385. return rc;
  386. }
  387. struct kmem_cache *ecryptfs_xattr_cache;
  388. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  389. {
  390. ssize_t size;
  391. void *xattr_virt;
  392. struct dentry *lower_dentry =
  393. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  394. struct inode *lower_inode = lower_dentry->d_inode;
  395. int rc;
  396. if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
  397. printk(KERN_WARNING
  398. "No support for setting xattr in lower filesystem\n");
  399. rc = -ENOSYS;
  400. goto out;
  401. }
  402. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  403. if (!xattr_virt) {
  404. printk(KERN_ERR "Out of memory whilst attempting to write "
  405. "inode size to xattr\n");
  406. rc = -ENOMEM;
  407. goto out;
  408. }
  409. mutex_lock(&lower_inode->i_mutex);
  410. size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  411. xattr_virt, PAGE_CACHE_SIZE);
  412. if (size < 0)
  413. size = 8;
  414. put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
  415. rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  416. xattr_virt, size, 0);
  417. mutex_unlock(&lower_inode->i_mutex);
  418. if (rc)
  419. printk(KERN_ERR "Error whilst attempting to write inode size "
  420. "to lower file xattr; rc = [%d]\n", rc);
  421. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  422. out:
  423. return rc;
  424. }
  425. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  426. {
  427. struct ecryptfs_crypt_stat *crypt_stat;
  428. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  429. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  430. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  431. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  432. else
  433. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  434. }
  435. /**
  436. * ecryptfs_write_end
  437. * @file: The eCryptfs file object
  438. * @mapping: The eCryptfs object
  439. * @pos: The file position
  440. * @len: The length of the data (unused)
  441. * @copied: The amount of data copied
  442. * @page: The eCryptfs page
  443. * @fsdata: The fsdata (unused)
  444. *
  445. * This is where we encrypt the data and pass the encrypted data to
  446. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  447. * entire underlying packets.
  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_CACHE_SHIFT;
  455. unsigned from = pos & (PAGE_CACHE_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(file->f_path.dentry->d_inode)->crypt_stat;
  460. int rc;
  461. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  462. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  463. "crypt_stat at memory location [%p]\n", crypt_stat);
  464. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  465. } else
  466. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  467. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  468. "(page w/ index = [0x%.16x], to = [%d])\n", index, to);
  469. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  470. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
  471. to);
  472. if (!rc) {
  473. rc = copied;
  474. fsstack_copy_inode_size(ecryptfs_inode,
  475. ecryptfs_inode_to_lower(ecryptfs_inode));
  476. }
  477. goto out;
  478. }
  479. /* Fills in zeros if 'to' goes beyond inode size */
  480. rc = fill_zeros_to_end_of_page(page, to);
  481. if (rc) {
  482. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  483. "zeros in page with index = [0x%.16x]\n", index);
  484. goto out;
  485. }
  486. rc = ecryptfs_encrypt_page(page);
  487. if (rc) {
  488. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  489. "index [0x%.16x])\n", index);
  490. goto out;
  491. }
  492. if (pos + copied > i_size_read(ecryptfs_inode)) {
  493. i_size_write(ecryptfs_inode, pos + copied);
  494. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  495. "[0x%.16x]\n", i_size_read(ecryptfs_inode));
  496. }
  497. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  498. if (rc)
  499. printk(KERN_ERR "Error writing inode size to metadata; "
  500. "rc = [%d]\n", rc);
  501. else
  502. rc = copied;
  503. out:
  504. unlock_page(page);
  505. page_cache_release(page);
  506. return rc;
  507. }
  508. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  509. {
  510. int rc = 0;
  511. struct inode *inode;
  512. struct inode *lower_inode;
  513. inode = (struct inode *)mapping->host;
  514. lower_inode = ecryptfs_inode_to_lower(inode);
  515. if (lower_inode->i_mapping->a_ops->bmap)
  516. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  517. block);
  518. return rc;
  519. }
  520. const struct address_space_operations ecryptfs_aops = {
  521. .writepage = ecryptfs_writepage,
  522. .readpage = ecryptfs_readpage,
  523. .write_begin = ecryptfs_write_begin,
  524. .write_end = ecryptfs_write_end,
  525. .bmap = ecryptfs_bmap,
  526. };