mmap.c 14 KB

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