mmap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 <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 file *file, loff_t index)
  45. {
  46. struct dentry *dentry;
  47. struct inode *inode;
  48. struct address_space *mapping;
  49. struct page *page;
  50. dentry = file->f_path.dentry;
  51. inode = dentry->d_inode;
  52. mapping = inode->i_mapping;
  53. page = read_mapping_page(mapping, index, (void *)file);
  54. if (!IS_ERR(page))
  55. lock_page(page);
  56. return page;
  57. }
  58. /**
  59. * ecryptfs_writepage
  60. * @page: Page that is locked before this call is made
  61. *
  62. * Returns zero on success; non-zero otherwise
  63. */
  64. static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
  65. {
  66. int rc;
  67. rc = ecryptfs_encrypt_page(page);
  68. if (rc) {
  69. ecryptfs_printk(KERN_WARNING, "Error encrypting "
  70. "page (upper index [0x%.16x])\n", page->index);
  71. ClearPageUptodate(page);
  72. goto out;
  73. }
  74. SetPageUptodate(page);
  75. unlock_page(page);
  76. out:
  77. return rc;
  78. }
  79. /**
  80. * Header Extent:
  81. * Octets 0-7: Unencrypted file size (big-endian)
  82. * Octets 8-15: eCryptfs special marker
  83. * Octets 16-19: Flags
  84. * Octet 16: File format version number (between 0 and 255)
  85. * Octets 17-18: Reserved
  86. * Octet 19: Bit 1 (lsb): Reserved
  87. * Bit 2: Encrypted?
  88. * Bits 3-8: Reserved
  89. * Octets 20-23: Header extent size (big-endian)
  90. * Octets 24-25: Number of header extents at front of file
  91. * (big-endian)
  92. * Octet 26: Begin RFC 2440 authentication token packet set
  93. */
  94. static void set_header_info(char *page_virt,
  95. struct ecryptfs_crypt_stat *crypt_stat)
  96. {
  97. size_t written;
  98. size_t save_num_header_bytes_at_front =
  99. crypt_stat->num_header_bytes_at_front;
  100. crypt_stat->num_header_bytes_at_front =
  101. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  102. ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
  103. crypt_stat->num_header_bytes_at_front =
  104. save_num_header_bytes_at_front;
  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->num_header_bytes_at_front
  130. / crypt_stat->extent_size);
  131. if (view_extent_num < num_header_extents_at_front) {
  132. /* This is a header extent */
  133. char *page_virt;
  134. page_virt = kmap_atomic(page, KM_USER0);
  135. memset(page_virt, 0, PAGE_CACHE_SIZE);
  136. /* TODO: Support more than one header extent */
  137. if (view_extent_num == 0) {
  138. rc = ecryptfs_read_xattr_region(
  139. page_virt, page->mapping->host);
  140. set_header_info(page_virt, crypt_stat);
  141. }
  142. kunmap_atomic(page_virt, KM_USER0);
  143. flush_dcache_page(page);
  144. if (rc) {
  145. printk(KERN_ERR "%s: Error reading xattr "
  146. "region; rc = [%d]\n", __func__, rc);
  147. goto out;
  148. }
  149. } else {
  150. /* This is an encrypted data extent */
  151. loff_t lower_offset =
  152. ((view_extent_num * crypt_stat->extent_size)
  153. - crypt_stat->num_header_bytes_at_front);
  154. rc = ecryptfs_read_lower_page_segment(
  155. page, (lower_offset >> PAGE_CACHE_SHIFT),
  156. (lower_offset & ~PAGE_CACHE_MASK),
  157. crypt_stat->extent_size, page->mapping->host);
  158. if (rc) {
  159. printk(KERN_ERR "%s: Error attempting to read "
  160. "extent at offset [%lld] in the lower "
  161. "file; rc = [%d]\n", __func__,
  162. lower_offset, rc);
  163. goto out;
  164. }
  165. }
  166. extent_num_in_page++;
  167. }
  168. out:
  169. return rc;
  170. }
  171. /**
  172. * ecryptfs_readpage
  173. * @file: An eCryptfs file
  174. * @page: Page from eCryptfs inode mapping into which to stick the read data
  175. *
  176. * Read in a page, decrypting if necessary.
  177. *
  178. * Returns zero on success; non-zero on error.
  179. */
  180. static int ecryptfs_readpage(struct file *file, struct page *page)
  181. {
  182. struct ecryptfs_crypt_stat *crypt_stat =
  183. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  184. int rc = 0;
  185. if (!crypt_stat
  186. || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  187. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  188. ecryptfs_printk(KERN_DEBUG,
  189. "Passing through unencrypted page\n");
  190. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  191. PAGE_CACHE_SIZE,
  192. page->mapping->host);
  193. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  194. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  195. rc = ecryptfs_copy_up_encrypted_with_header(page,
  196. crypt_stat);
  197. if (rc) {
  198. printk(KERN_ERR "%s: Error attempting to copy "
  199. "the encrypted content from the lower "
  200. "file whilst inserting the metadata "
  201. "from the xattr into the header; rc = "
  202. "[%d]\n", __func__, rc);
  203. goto out;
  204. }
  205. } else {
  206. rc = ecryptfs_read_lower_page_segment(
  207. page, page->index, 0, PAGE_CACHE_SIZE,
  208. page->mapping->host);
  209. if (rc) {
  210. printk(KERN_ERR "Error reading page; rc = "
  211. "[%d]\n", rc);
  212. goto out;
  213. }
  214. }
  215. } else {
  216. rc = ecryptfs_decrypt_page(page);
  217. if (rc) {
  218. ecryptfs_printk(KERN_ERR, "Error decrypting page; "
  219. "rc = [%d]\n", rc);
  220. goto out;
  221. }
  222. }
  223. out:
  224. if (rc)
  225. ClearPageUptodate(page);
  226. else
  227. SetPageUptodate(page);
  228. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  229. page->index);
  230. unlock_page(page);
  231. return rc;
  232. }
  233. /**
  234. * Called with lower inode mutex held.
  235. */
  236. static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
  237. {
  238. struct inode *inode = page->mapping->host;
  239. int end_byte_in_page;
  240. if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
  241. goto out;
  242. end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
  243. if (to > end_byte_in_page)
  244. end_byte_in_page = to;
  245. zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
  246. out:
  247. return 0;
  248. }
  249. /**
  250. * ecryptfs_write_begin
  251. * @file: The eCryptfs file
  252. * @mapping: The eCryptfs object
  253. * @pos: The file offset at which to start writing
  254. * @len: Length of the write
  255. * @flags: Various flags
  256. * @pagep: Pointer to return the page
  257. * @fsdata: Pointer to return fs data (unused)
  258. *
  259. * This function must zero any hole we create
  260. *
  261. * Returns zero on success; non-zero otherwise
  262. */
  263. static int ecryptfs_write_begin(struct file *file,
  264. struct address_space *mapping,
  265. loff_t pos, unsigned len, unsigned flags,
  266. struct page **pagep, void **fsdata)
  267. {
  268. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  269. struct page *page;
  270. loff_t prev_page_end_size;
  271. int rc = 0;
  272. page = grab_cache_page_write_begin(mapping, index, flags);
  273. if (!page)
  274. return -ENOMEM;
  275. *pagep = page;
  276. if (!PageUptodate(page)) {
  277. struct ecryptfs_crypt_stat *crypt_stat =
  278. &ecryptfs_inode_to_private(
  279. file->f_path.dentry->d_inode)->crypt_stat;
  280. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  281. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  282. rc = ecryptfs_read_lower_page_segment(
  283. page, index, 0, PAGE_CACHE_SIZE, mapping->host);
  284. if (rc) {
  285. printk(KERN_ERR "%s: Error attemping to read "
  286. "lower page segment; rc = [%d]\n",
  287. __func__, rc);
  288. ClearPageUptodate(page);
  289. goto out;
  290. } else
  291. SetPageUptodate(page);
  292. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  293. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  294. rc = ecryptfs_copy_up_encrypted_with_header(
  295. page, crypt_stat);
  296. if (rc) {
  297. printk(KERN_ERR "%s: Error attempting "
  298. "to copy the encrypted content "
  299. "from the lower file whilst "
  300. "inserting the metadata from "
  301. "the xattr into the header; rc "
  302. "= [%d]\n", __func__, rc);
  303. ClearPageUptodate(page);
  304. goto out;
  305. }
  306. SetPageUptodate(page);
  307. } else {
  308. rc = ecryptfs_read_lower_page_segment(
  309. page, index, 0, PAGE_CACHE_SIZE,
  310. mapping->host);
  311. if (rc) {
  312. printk(KERN_ERR "%s: Error reading "
  313. "page; rc = [%d]\n",
  314. __func__, rc);
  315. ClearPageUptodate(page);
  316. goto out;
  317. }
  318. SetPageUptodate(page);
  319. }
  320. } else {
  321. rc = ecryptfs_decrypt_page(page);
  322. if (rc) {
  323. printk(KERN_ERR "%s: Error decrypting page "
  324. "at index [%ld]; rc = [%d]\n",
  325. __func__, page->index, rc);
  326. ClearPageUptodate(page);
  327. goto out;
  328. }
  329. SetPageUptodate(page);
  330. }
  331. }
  332. prev_page_end_size = ((loff_t)index << PAGE_CACHE_SHIFT);
  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_CACHE_SIZE);
  353. out:
  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)
  377. printk(KERN_ERR "%s: Error writing file size to header; "
  378. "rc = [%d]\n", __func__, rc);
  379. out:
  380. return rc;
  381. }
  382. struct kmem_cache *ecryptfs_xattr_cache;
  383. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  384. {
  385. ssize_t size;
  386. void *xattr_virt;
  387. struct dentry *lower_dentry =
  388. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  389. struct inode *lower_inode = lower_dentry->d_inode;
  390. int rc;
  391. if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
  392. printk(KERN_WARNING
  393. "No support for setting xattr in lower filesystem\n");
  394. rc = -ENOSYS;
  395. goto out;
  396. }
  397. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  398. if (!xattr_virt) {
  399. printk(KERN_ERR "Out of memory whilst attempting to write "
  400. "inode size to xattr\n");
  401. rc = -ENOMEM;
  402. goto out;
  403. }
  404. mutex_lock(&lower_inode->i_mutex);
  405. size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  406. xattr_virt, PAGE_CACHE_SIZE);
  407. if (size < 0)
  408. size = 8;
  409. put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
  410. rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  411. xattr_virt, size, 0);
  412. mutex_unlock(&lower_inode->i_mutex);
  413. if (rc)
  414. printk(KERN_ERR "Error whilst attempting to write inode size "
  415. "to lower file xattr; rc = [%d]\n", rc);
  416. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  417. out:
  418. return rc;
  419. }
  420. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  421. {
  422. struct ecryptfs_crypt_stat *crypt_stat;
  423. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  424. BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
  425. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  426. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  427. else
  428. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  429. }
  430. /**
  431. * ecryptfs_write_end
  432. * @file: The eCryptfs file object
  433. * @mapping: The eCryptfs object
  434. * @pos: The file position
  435. * @len: The length of the data (unused)
  436. * @copied: The amount of data copied
  437. * @page: The eCryptfs page
  438. * @fsdata: The fsdata (unused)
  439. *
  440. * This is where we encrypt the data and pass the encrypted data to
  441. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  442. * entire underlying packets.
  443. */
  444. static int ecryptfs_write_end(struct file *file,
  445. struct address_space *mapping,
  446. loff_t pos, unsigned len, unsigned copied,
  447. struct page *page, void *fsdata)
  448. {
  449. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  450. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  451. unsigned to = from + copied;
  452. struct inode *ecryptfs_inode = mapping->host;
  453. struct ecryptfs_crypt_stat *crypt_stat =
  454. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  455. int rc;
  456. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  457. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  458. "crypt_stat at memory location [%p]\n", crypt_stat);
  459. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  460. } else
  461. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  462. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  463. "(page w/ index = [0x%.16x], to = [%d])\n", index, to);
  464. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  465. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
  466. to);
  467. if (!rc) {
  468. rc = copied;
  469. fsstack_copy_inode_size(ecryptfs_inode,
  470. ecryptfs_inode_to_lower(ecryptfs_inode));
  471. }
  472. goto out;
  473. }
  474. /* Fills in zeros if 'to' goes beyond inode size */
  475. rc = fill_zeros_to_end_of_page(page, to);
  476. if (rc) {
  477. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  478. "zeros in page with index = [0x%.16x]\n", index);
  479. goto out;
  480. }
  481. rc = ecryptfs_encrypt_page(page);
  482. if (rc) {
  483. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  484. "index [0x%.16x])\n", index);
  485. goto out;
  486. }
  487. if (pos + copied > i_size_read(ecryptfs_inode)) {
  488. i_size_write(ecryptfs_inode, pos + copied);
  489. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  490. "[0x%.16x]\n", i_size_read(ecryptfs_inode));
  491. }
  492. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  493. if (rc)
  494. printk(KERN_ERR "Error writing inode size to metadata; "
  495. "rc = [%d]\n", rc);
  496. else
  497. rc = copied;
  498. out:
  499. unlock_page(page);
  500. page_cache_release(page);
  501. return rc;
  502. }
  503. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  504. {
  505. int rc = 0;
  506. struct inode *inode;
  507. struct inode *lower_inode;
  508. inode = (struct inode *)mapping->host;
  509. lower_inode = ecryptfs_inode_to_lower(inode);
  510. if (lower_inode->i_mapping->a_ops->bmap)
  511. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  512. block);
  513. return rc;
  514. }
  515. struct address_space_operations ecryptfs_aops = {
  516. .writepage = ecryptfs_writepage,
  517. .readpage = ecryptfs_readpage,
  518. .write_begin = ecryptfs_write_begin,
  519. .write_end = ecryptfs_write_end,
  520. .bmap = ecryptfs_bmap,
  521. };