mmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "ecryptfs_kernel.h"
  35. struct kmem_cache *ecryptfs_lower_page_cache;
  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. int save_num_header_extents_at_front =
  99. crypt_stat->num_header_extents_at_front;
  100. crypt_stat->num_header_extents_at_front = 1;
  101. ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
  102. crypt_stat->num_header_extents_at_front =
  103. save_num_header_extents_at_front;
  104. }
  105. /**
  106. * ecryptfs_copy_up_encrypted_with_header
  107. * @page: Sort of a ``virtual'' representation of the encrypted lower
  108. * file. The actual lower file does not have the metadata in
  109. * the header. This is locked.
  110. * @crypt_stat: The eCryptfs inode's cryptographic context
  111. *
  112. * The ``view'' is the version of the file that userspace winds up
  113. * seeing, with the header information inserted.
  114. */
  115. static int
  116. ecryptfs_copy_up_encrypted_with_header(struct page *page,
  117. struct ecryptfs_crypt_stat *crypt_stat)
  118. {
  119. loff_t extent_num_in_page = 0;
  120. loff_t num_extents_per_page = (PAGE_CACHE_SIZE
  121. / crypt_stat->extent_size);
  122. int rc = 0;
  123. while (extent_num_in_page < num_extents_per_page) {
  124. loff_t view_extent_num = ((((loff_t)page->index)
  125. * num_extents_per_page)
  126. + extent_num_in_page);
  127. if (view_extent_num < crypt_stat->num_header_extents_at_front) {
  128. /* This is a header extent */
  129. char *page_virt;
  130. page_virt = kmap_atomic(page, KM_USER0);
  131. memset(page_virt, 0, PAGE_CACHE_SIZE);
  132. /* TODO: Support more than one header extent */
  133. if (view_extent_num == 0) {
  134. rc = ecryptfs_read_xattr_region(
  135. page_virt, page->mapping->host);
  136. set_header_info(page_virt, crypt_stat);
  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", __FUNCTION__, rc);
  143. goto out;
  144. }
  145. } else {
  146. /* This is an encrypted data extent */
  147. loff_t lower_offset =
  148. ((view_extent_num -
  149. crypt_stat->num_header_extents_at_front)
  150. * crypt_stat->extent_size);
  151. rc = ecryptfs_read_lower_page_segment(
  152. page, (lower_offset >> PAGE_CACHE_SHIFT),
  153. (lower_offset & ~PAGE_CACHE_MASK),
  154. crypt_stat->extent_size, page->mapping->host);
  155. if (rc) {
  156. printk(KERN_ERR "%s: Error attempting to read "
  157. "extent at offset [%lld] in the lower "
  158. "file; rc = [%d]\n", __FUNCTION__,
  159. lower_offset, rc);
  160. goto out;
  161. }
  162. }
  163. extent_num_in_page++;
  164. }
  165. out:
  166. return rc;
  167. }
  168. /**
  169. * ecryptfs_readpage
  170. * @file: An eCryptfs file
  171. * @page: Page from eCryptfs inode mapping into which to stick the read data
  172. *
  173. * Read in a page, decrypting if necessary.
  174. *
  175. * Returns zero on success; non-zero on error.
  176. */
  177. static int ecryptfs_readpage(struct file *file, struct page *page)
  178. {
  179. struct ecryptfs_crypt_stat *crypt_stat =
  180. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  181. int rc = 0;
  182. if (!crypt_stat
  183. || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  184. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  185. ecryptfs_printk(KERN_DEBUG,
  186. "Passing through unencrypted page\n");
  187. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  188. PAGE_CACHE_SIZE,
  189. page->mapping->host);
  190. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  191. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  192. rc = ecryptfs_copy_up_encrypted_with_header(page,
  193. crypt_stat);
  194. if (rc) {
  195. printk(KERN_ERR "%s: Error attempting to copy "
  196. "the encrypted content from the lower "
  197. "file whilst inserting the metadata "
  198. "from the xattr into the header; rc = "
  199. "[%d]\n", __FUNCTION__, rc);
  200. goto out;
  201. }
  202. } else {
  203. rc = ecryptfs_read_lower_page_segment(
  204. page, page->index, 0, PAGE_CACHE_SIZE,
  205. page->mapping->host);
  206. if (rc) {
  207. printk(KERN_ERR "Error reading page; rc = "
  208. "[%d]\n", rc);
  209. goto out;
  210. }
  211. }
  212. } else {
  213. rc = ecryptfs_decrypt_page(page);
  214. if (rc) {
  215. ecryptfs_printk(KERN_ERR, "Error decrypting page; "
  216. "rc = [%d]\n", rc);
  217. goto out;
  218. }
  219. }
  220. out:
  221. if (rc)
  222. ClearPageUptodate(page);
  223. else
  224. SetPageUptodate(page);
  225. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  226. page->index);
  227. unlock_page(page);
  228. return rc;
  229. }
  230. /**
  231. * Called with lower inode mutex held.
  232. */
  233. static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
  234. {
  235. struct inode *inode = page->mapping->host;
  236. int end_byte_in_page;
  237. if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
  238. goto out;
  239. end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
  240. if (to > end_byte_in_page)
  241. end_byte_in_page = to;
  242. zero_user_page(page, end_byte_in_page,
  243. PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
  244. out:
  245. return 0;
  246. }
  247. /* This function must zero any hole we create */
  248. static int ecryptfs_prepare_write(struct file *file, struct page *page,
  249. unsigned from, unsigned to)
  250. {
  251. int rc = 0;
  252. loff_t prev_page_end_size;
  253. if (!PageUptodate(page)) {
  254. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  255. PAGE_CACHE_SIZE,
  256. page->mapping->host);
  257. if (rc) {
  258. printk(KERN_ERR "%s: Error attemping to read lower "
  259. "page segment; rc = [%d]\n", __FUNCTION__, rc);
  260. ClearPageUptodate(page);
  261. goto out;
  262. } else
  263. SetPageUptodate(page);
  264. }
  265. prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT);
  266. /*
  267. * If creating a page or more of holes, zero them out via truncate.
  268. * Note, this will increase i_size.
  269. */
  270. if (page->index != 0) {
  271. if (prev_page_end_size > i_size_read(page->mapping->host)) {
  272. rc = ecryptfs_truncate(file->f_path.dentry,
  273. prev_page_end_size);
  274. if (rc) {
  275. printk(KERN_ERR "Error on attempt to "
  276. "truncate to (higher) offset [%lld];"
  277. " rc = [%d]\n", prev_page_end_size, rc);
  278. goto out;
  279. }
  280. }
  281. }
  282. /*
  283. * Writing to a new page, and creating a small hole from start of page?
  284. * Zero it out.
  285. */
  286. if ((i_size_read(page->mapping->host) == prev_page_end_size) &&
  287. (from != 0)) {
  288. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  289. }
  290. out:
  291. return rc;
  292. }
  293. /**
  294. * ecryptfs_write_inode_size_to_header
  295. *
  296. * Writes the lower file size to the first 8 bytes of the header.
  297. *
  298. * Returns zero on success; non-zero on error.
  299. */
  300. static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
  301. {
  302. u64 file_size;
  303. char *file_size_virt;
  304. int rc;
  305. file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
  306. if (!file_size_virt) {
  307. rc = -ENOMEM;
  308. goto out;
  309. }
  310. file_size = (u64)i_size_read(ecryptfs_inode);
  311. file_size = cpu_to_be64(file_size);
  312. memcpy(file_size_virt, &file_size, sizeof(u64));
  313. rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
  314. sizeof(u64));
  315. kfree(file_size_virt);
  316. if (rc)
  317. printk(KERN_ERR "%s: Error writing file size to header; "
  318. "rc = [%d]\n", __FUNCTION__, rc);
  319. out:
  320. return rc;
  321. }
  322. struct kmem_cache *ecryptfs_xattr_cache;
  323. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  324. {
  325. ssize_t size;
  326. void *xattr_virt;
  327. struct dentry *lower_dentry =
  328. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  329. struct inode *lower_inode = lower_dentry->d_inode;
  330. u64 file_size;
  331. int rc;
  332. if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
  333. printk(KERN_WARNING
  334. "No support for setting xattr in lower filesystem\n");
  335. rc = -ENOSYS;
  336. goto out;
  337. }
  338. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  339. if (!xattr_virt) {
  340. printk(KERN_ERR "Out of memory whilst attempting to write "
  341. "inode size to xattr\n");
  342. rc = -ENOMEM;
  343. goto out;
  344. }
  345. mutex_lock(&lower_inode->i_mutex);
  346. size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  347. xattr_virt, PAGE_CACHE_SIZE);
  348. if (size < 0)
  349. size = 8;
  350. file_size = (u64)i_size_read(ecryptfs_inode);
  351. file_size = cpu_to_be64(file_size);
  352. memcpy(xattr_virt, &file_size, sizeof(u64));
  353. rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  354. xattr_virt, size, 0);
  355. mutex_unlock(&lower_inode->i_mutex);
  356. if (rc)
  357. printk(KERN_ERR "Error whilst attempting to write inode size "
  358. "to lower file xattr; rc = [%d]\n", rc);
  359. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  360. out:
  361. return rc;
  362. }
  363. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  364. {
  365. struct ecryptfs_crypt_stat *crypt_stat;
  366. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  367. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  368. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  369. else
  370. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  371. }
  372. /**
  373. * ecryptfs_commit_write
  374. * @file: The eCryptfs file object
  375. * @page: The eCryptfs page
  376. * @from: Ignored (we rotate the page IV on each write)
  377. * @to: Ignored
  378. *
  379. * This is where we encrypt the data and pass the encrypted data to
  380. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  381. * entire underlying packets.
  382. */
  383. static int ecryptfs_commit_write(struct file *file, struct page *page,
  384. unsigned from, unsigned to)
  385. {
  386. loff_t pos;
  387. struct inode *ecryptfs_inode = page->mapping->host;
  388. struct ecryptfs_crypt_stat *crypt_stat =
  389. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  390. int rc;
  391. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  392. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  393. "crypt_stat at memory location [%p]\n", crypt_stat);
  394. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  395. } else
  396. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  397. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  398. "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
  399. to);
  400. /* Fills in zeros if 'to' goes beyond inode size */
  401. rc = fill_zeros_to_end_of_page(page, to);
  402. if (rc) {
  403. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  404. "zeros in page with index = [0x%.16x]\n",
  405. page->index);
  406. goto out;
  407. }
  408. rc = ecryptfs_encrypt_page(page);
  409. if (rc) {
  410. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  411. "index [0x%.16x])\n", page->index);
  412. goto out;
  413. }
  414. pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
  415. if (pos > i_size_read(ecryptfs_inode)) {
  416. i_size_write(ecryptfs_inode, pos);
  417. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  418. "[0x%.16x]\n", i_size_read(ecryptfs_inode));
  419. }
  420. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  421. if (rc)
  422. printk(KERN_ERR "Error writing inode size to metadata; "
  423. "rc = [%d]\n", rc);
  424. out:
  425. return rc;
  426. }
  427. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  428. {
  429. int rc = 0;
  430. struct inode *inode;
  431. struct inode *lower_inode;
  432. inode = (struct inode *)mapping->host;
  433. lower_inode = ecryptfs_inode_to_lower(inode);
  434. if (lower_inode->i_mapping->a_ops->bmap)
  435. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  436. block);
  437. return rc;
  438. }
  439. struct address_space_operations ecryptfs_aops = {
  440. .writepage = ecryptfs_writepage,
  441. .readpage = ecryptfs_readpage,
  442. .prepare_write = ecryptfs_prepare_write,
  443. .commit_write = ecryptfs_commit_write,
  444. .bmap = ecryptfs_bmap,
  445. };