mmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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_prepare_write
  251. * @file: The eCryptfs file
  252. * @page: The eCryptfs page
  253. * @from: The start byte from which we will write
  254. * @to: The end byte to which we will write
  255. *
  256. * This function must zero any hole we create
  257. *
  258. * Returns zero on success; non-zero otherwise
  259. */
  260. static int ecryptfs_prepare_write(struct file *file, struct page *page,
  261. unsigned from, unsigned to)
  262. {
  263. loff_t prev_page_end_size;
  264. int rc = 0;
  265. if (!PageUptodate(page)) {
  266. struct ecryptfs_crypt_stat *crypt_stat =
  267. &ecryptfs_inode_to_private(
  268. file->f_path.dentry->d_inode)->crypt_stat;
  269. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  270. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  271. rc = ecryptfs_read_lower_page_segment(
  272. page, page->index, 0, PAGE_CACHE_SIZE,
  273. page->mapping->host);
  274. if (rc) {
  275. printk(KERN_ERR "%s: Error attemping to read "
  276. "lower page segment; rc = [%d]\n",
  277. __func__, rc);
  278. ClearPageUptodate(page);
  279. goto out;
  280. } else
  281. SetPageUptodate(page);
  282. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  283. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  284. rc = ecryptfs_copy_up_encrypted_with_header(
  285. page, crypt_stat);
  286. if (rc) {
  287. printk(KERN_ERR "%s: Error attempting "
  288. "to copy the encrypted content "
  289. "from the lower file whilst "
  290. "inserting the metadata from "
  291. "the xattr into the header; rc "
  292. "= [%d]\n", __func__, rc);
  293. ClearPageUptodate(page);
  294. goto out;
  295. }
  296. SetPageUptodate(page);
  297. } else {
  298. rc = ecryptfs_read_lower_page_segment(
  299. page, page->index, 0, PAGE_CACHE_SIZE,
  300. page->mapping->host);
  301. if (rc) {
  302. printk(KERN_ERR "%s: Error reading "
  303. "page; rc = [%d]\n",
  304. __func__, rc);
  305. ClearPageUptodate(page);
  306. goto out;
  307. }
  308. SetPageUptodate(page);
  309. }
  310. } else {
  311. rc = ecryptfs_decrypt_page(page);
  312. if (rc) {
  313. printk(KERN_ERR "%s: Error decrypting page "
  314. "at index [%ld]; rc = [%d]\n",
  315. __func__, page->index, rc);
  316. ClearPageUptodate(page);
  317. goto out;
  318. }
  319. SetPageUptodate(page);
  320. }
  321. }
  322. prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT);
  323. /* If creating a page or more of holes, zero them out via truncate.
  324. * Note, this will increase i_size. */
  325. if (page->index != 0) {
  326. if (prev_page_end_size > i_size_read(page->mapping->host)) {
  327. rc = ecryptfs_truncate(file->f_path.dentry,
  328. prev_page_end_size);
  329. if (rc) {
  330. printk(KERN_ERR "%s: Error on attempt to "
  331. "truncate to (higher) offset [%lld];"
  332. " rc = [%d]\n", __func__,
  333. prev_page_end_size, rc);
  334. goto out;
  335. }
  336. }
  337. }
  338. /* Writing to a new page, and creating a small hole from start
  339. * of page? Zero it out. */
  340. if ((i_size_read(page->mapping->host) == prev_page_end_size)
  341. && (from != 0))
  342. zero_user(page, 0, PAGE_CACHE_SIZE);
  343. out:
  344. return rc;
  345. }
  346. /**
  347. * ecryptfs_write_inode_size_to_header
  348. *
  349. * Writes the lower file size to the first 8 bytes of the header.
  350. *
  351. * Returns zero on success; non-zero on error.
  352. */
  353. static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
  354. {
  355. char *file_size_virt;
  356. int rc;
  357. file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
  358. if (!file_size_virt) {
  359. rc = -ENOMEM;
  360. goto out;
  361. }
  362. put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
  363. rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
  364. sizeof(u64));
  365. kfree(file_size_virt);
  366. if (rc)
  367. printk(KERN_ERR "%s: Error writing file size to header; "
  368. "rc = [%d]\n", __func__, rc);
  369. out:
  370. return rc;
  371. }
  372. struct kmem_cache *ecryptfs_xattr_cache;
  373. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  374. {
  375. ssize_t size;
  376. void *xattr_virt;
  377. struct dentry *lower_dentry =
  378. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  379. struct inode *lower_inode = lower_dentry->d_inode;
  380. int rc;
  381. if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
  382. printk(KERN_WARNING
  383. "No support for setting xattr in lower filesystem\n");
  384. rc = -ENOSYS;
  385. goto out;
  386. }
  387. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  388. if (!xattr_virt) {
  389. printk(KERN_ERR "Out of memory whilst attempting to write "
  390. "inode size to xattr\n");
  391. rc = -ENOMEM;
  392. goto out;
  393. }
  394. mutex_lock(&lower_inode->i_mutex);
  395. size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  396. xattr_virt, PAGE_CACHE_SIZE);
  397. if (size < 0)
  398. size = 8;
  399. put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
  400. rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  401. xattr_virt, size, 0);
  402. mutex_unlock(&lower_inode->i_mutex);
  403. if (rc)
  404. printk(KERN_ERR "Error whilst attempting to write inode size "
  405. "to lower file xattr; rc = [%d]\n", rc);
  406. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  407. out:
  408. return rc;
  409. }
  410. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  411. {
  412. struct ecryptfs_crypt_stat *crypt_stat;
  413. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  414. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  415. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  416. else
  417. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  418. }
  419. /**
  420. * ecryptfs_commit_write
  421. * @file: The eCryptfs file object
  422. * @page: The eCryptfs page
  423. * @from: Ignored (we rotate the page IV on each write)
  424. * @to: Ignored
  425. *
  426. * This is where we encrypt the data and pass the encrypted data to
  427. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  428. * entire underlying packets.
  429. */
  430. static int ecryptfs_commit_write(struct file *file, struct page *page,
  431. unsigned from, unsigned to)
  432. {
  433. loff_t pos;
  434. struct inode *ecryptfs_inode = page->mapping->host;
  435. struct ecryptfs_crypt_stat *crypt_stat =
  436. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  437. int rc;
  438. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  439. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  440. "crypt_stat at memory location [%p]\n", crypt_stat);
  441. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  442. } else
  443. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  444. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  445. "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
  446. to);
  447. /* Fills in zeros if 'to' goes beyond inode size */
  448. rc = fill_zeros_to_end_of_page(page, to);
  449. if (rc) {
  450. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  451. "zeros in page with index = [0x%.16x]\n",
  452. page->index);
  453. goto out;
  454. }
  455. rc = ecryptfs_encrypt_page(page);
  456. if (rc) {
  457. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  458. "index [0x%.16x])\n", page->index);
  459. goto out;
  460. }
  461. pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
  462. if (pos > i_size_read(ecryptfs_inode)) {
  463. i_size_write(ecryptfs_inode, pos);
  464. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  465. "[0x%.16x]\n", i_size_read(ecryptfs_inode));
  466. }
  467. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  468. if (rc)
  469. printk(KERN_ERR "Error writing inode size to metadata; "
  470. "rc = [%d]\n", rc);
  471. out:
  472. return rc;
  473. }
  474. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  475. {
  476. int rc = 0;
  477. struct inode *inode;
  478. struct inode *lower_inode;
  479. inode = (struct inode *)mapping->host;
  480. lower_inode = ecryptfs_inode_to_lower(inode);
  481. if (lower_inode->i_mapping->a_ops->bmap)
  482. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  483. block);
  484. return rc;
  485. }
  486. struct address_space_operations ecryptfs_aops = {
  487. .writepage = ecryptfs_writepage,
  488. .readpage = ecryptfs_readpage,
  489. .prepare_write = ecryptfs_prepare_write,
  490. .commit_write = ecryptfs_commit_write,
  491. .bmap = ecryptfs_bmap,
  492. };