mmap.c 14 KB

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