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.
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>
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.
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.
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
28 #include <linux/pagemap.h>
29 #include <linux/writeback.h>
30 #include <linux/page-flags.h>
31 #include <linux/mount.h>
32 #include <linux/file.h>
33 #include <linux/crypto.h>
34 #include <linux/scatterlist.h>
35 #include "ecryptfs_kernel.h"
37 struct kmem_cache *ecryptfs_lower_page_cache;
42 * Get one page from cache or lower f/s, return error otherwise.
44 * Returns unlocked and up-to-date page (if ok), with increased
47 struct page *ecryptfs_get1page(struct file *file, loff_t index)
49 struct dentry *dentry;
51 struct address_space *mapping;
53 dentry = file->f_path.dentry;
54 inode = dentry->d_inode;
55 mapping = inode->i_mapping;
56 return read_mapping_page(mapping, index, (void *)file);
61 * @file: The ecryptfs file
62 * @new_length: The new length of the data in the underlying file;
63 * everything between the prior end of the file and the
64 * new end of the file will be filled with zero's.
65 * new_length must be greater than current length
67 * Function for handling lseek-ing past the end of the file.
69 * This function does not support shrinking, only growing a file.
71 * Returns zero on success; non-zero otherwise.
73 int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
76 struct dentry *dentry = file->f_path.dentry;
77 struct inode *inode = dentry->d_inode;
78 pgoff_t old_end_page_index = 0;
79 pgoff_t index = old_end_page_index;
80 int old_end_pos_in_page = -1;
81 pgoff_t new_end_page_index;
82 int new_end_pos_in_page;
83 loff_t cur_length = i_size_read(inode);
85 if (cur_length != 0) {
86 index = old_end_page_index =
87 ((cur_length - 1) >> PAGE_CACHE_SHIFT);
88 old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
90 new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
91 new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
92 ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
93 "old_end_pos_in_page = [%d]; "
94 "new_end_page_index = [0x%.16x]; "
95 "new_end_pos_in_page = [%d]\n",
96 old_end_page_index, old_end_pos_in_page,
97 new_end_page_index, new_end_pos_in_page);
98 if (old_end_page_index == new_end_page_index) {
99 /* Start and end are in the same page; we just need to
100 * set a portion of the existing page to zero's */
101 rc = ecryptfs_write_zeros(file, index,
102 (old_end_pos_in_page + 1),
104 - old_end_pos_in_page));
106 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
109 "old_end_pos_in_page=[d], "
110 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
112 ")=[d]) returned [%d]\n", file, index,
115 (PAGE_CACHE_SIZE - new_end_pos_in_page),
119 /* Fill the remainder of the previous last page with zeros */
120 rc = ecryptfs_write_zeros(file, index, (old_end_pos_in_page + 1),
121 ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
123 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file=[%p], "
124 "index=[0x%.16x], old_end_pos_in_page=[d], "
125 "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
126 "returned [%d]\n", file, index,
128 (PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
132 while (index < new_end_page_index) {
133 /* Fill all intermediate pages with zeros */
134 rc = ecryptfs_write_zeros(file, index, 0, PAGE_CACHE_SIZE);
136 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
139 "old_end_pos_in_page=[d], "
140 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
142 ")=[d]) returned [%d]\n", file, index,
145 (PAGE_CACHE_SIZE - new_end_pos_in_page),
151 /* Fill the portion at the beginning of the last new page with
153 rc = ecryptfs_write_zeros(file, index, 0, (new_end_pos_in_page + 1));
155 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file="
156 "[%p], index=[0x%.16x], 0, "
157 "new_end_pos_in_page=[%d]"
158 "returned [%d]\n", file, index,
159 new_end_pos_in_page, rc);
168 * @page: Page that is locked before this call is made
170 * Returns zero on success; non-zero otherwise
172 static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
176 rc = ecryptfs_encrypt_page(page);
178 ecryptfs_printk(KERN_WARNING, "Error encrypting "
179 "page (upper index [0x%.16x])\n", page->index);
180 ClearPageUptodate(page);
183 SetPageUptodate(page);
190 * Reads the data from the lower file file at index lower_page_index
191 * and copies that data into page.
193 * @param page Page to fill
194 * @param lower_page_index Index of the page in the lower file to get
196 int ecryptfs_do_readpage(struct file *file, struct page *page,
197 pgoff_t lower_page_index)
200 struct dentry *dentry;
201 struct file *lower_file;
202 struct dentry *lower_dentry;
204 struct inode *lower_inode;
206 struct page *lower_page = NULL;
207 char *lower_page_data;
208 const struct address_space_operations *lower_a_ops;
210 dentry = file->f_path.dentry;
211 lower_file = ecryptfs_file_to_lower(file);
212 lower_dentry = ecryptfs_dentry_to_lower(dentry);
213 inode = dentry->d_inode;
214 lower_inode = ecryptfs_inode_to_lower(inode);
215 lower_a_ops = lower_inode->i_mapping->a_ops;
216 lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
217 (filler_t *)lower_a_ops->readpage,
219 if (IS_ERR(lower_page)) {
220 rc = PTR_ERR(lower_page);
222 ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
225 page_data = kmap_atomic(page, KM_USER0);
226 lower_page_data = kmap_atomic(lower_page, KM_USER1);
227 memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
228 kunmap_atomic(lower_page_data, KM_USER1);
229 kunmap_atomic(page_data, KM_USER0);
230 flush_dcache_page(page);
233 if (likely(lower_page))
234 page_cache_release(lower_page);
236 SetPageUptodate(page);
238 ClearPageUptodate(page);
243 * Octets 0-7: Unencrypted file size (big-endian)
244 * Octets 8-15: eCryptfs special marker
245 * Octets 16-19: Flags
246 * Octet 16: File format version number (between 0 and 255)
247 * Octets 17-18: Reserved
248 * Octet 19: Bit 1 (lsb): Reserved
251 * Octets 20-23: Header extent size (big-endian)
252 * Octets 24-25: Number of header extents at front of file
254 * Octet 26: Begin RFC 2440 authentication token packet set
256 static void set_header_info(char *page_virt,
257 struct ecryptfs_crypt_stat *crypt_stat)
260 int save_num_header_extents_at_front =
261 crypt_stat->num_header_extents_at_front;
263 crypt_stat->num_header_extents_at_front = 1;
264 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
265 crypt_stat->num_header_extents_at_front =
266 save_num_header_extents_at_front;
270 * ecryptfs_copy_up_encrypted_with_header
271 * @page: Sort of a ``virtual'' representation of the encrypted lower
272 * file. The actual lower file does not have the metadata in
273 * the header. This is locked.
274 * @crypt_stat: The eCryptfs inode's cryptographic context
276 * The ``view'' is the version of the file that userspace winds up
277 * seeing, with the header information inserted.
280 ecryptfs_copy_up_encrypted_with_header(struct page *page,
281 struct ecryptfs_crypt_stat *crypt_stat)
283 loff_t extent_num_in_page = 0;
284 loff_t num_extents_per_page = (PAGE_CACHE_SIZE
285 / crypt_stat->extent_size);
288 while (extent_num_in_page < num_extents_per_page) {
289 loff_t view_extent_num = ((page->index * num_extents_per_page)
290 + extent_num_in_page);
292 if (view_extent_num < crypt_stat->num_header_extents_at_front) {
293 /* This is a header extent */
296 page_virt = kmap_atomic(page, KM_USER0);
297 memset(page_virt, 0, PAGE_CACHE_SIZE);
298 /* TODO: Support more than one header extent */
299 if (view_extent_num == 0) {
300 rc = ecryptfs_read_xattr_region(
301 page_virt, page->mapping->host);
302 set_header_info(page_virt, crypt_stat);
304 kunmap_atomic(page_virt, KM_USER0);
305 flush_dcache_page(page);
307 ClearPageUptodate(page);
308 printk(KERN_ERR "%s: Error reading xattr "
309 "region; rc = [%d]\n", __FUNCTION__, rc);
312 SetPageUptodate(page);
314 /* This is an encrypted data extent */
315 loff_t lower_offset =
317 crypt_stat->num_header_extents_at_front)
318 * crypt_stat->extent_size);
320 rc = ecryptfs_read_lower_page_segment(
321 page, (lower_offset >> PAGE_CACHE_SHIFT),
322 (lower_offset & ~PAGE_CACHE_MASK),
323 crypt_stat->extent_size, page->mapping->host);
325 printk(KERN_ERR "%s: Error attempting to read "
326 "extent at offset [%lld] in the lower "
327 "file; rc = [%d]\n", __FUNCTION__,
332 extent_num_in_page++;
340 * @file: An eCryptfs file
341 * @page: Page from eCryptfs inode mapping into which to stick the read data
343 * Read in a page, decrypting if necessary.
345 * Returns zero on success; non-zero on error.
347 static int ecryptfs_readpage(struct file *file, struct page *page)
349 struct ecryptfs_crypt_stat *crypt_stat =
350 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
354 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
355 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
356 ecryptfs_printk(KERN_DEBUG,
357 "Passing through unencrypted page\n");
358 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
360 page->mapping->host);
361 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
362 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
363 rc = ecryptfs_copy_up_encrypted_with_header(page,
366 printk(KERN_ERR "%s: Error attempting to copy "
367 "the encrypted content from the lower "
368 "file whilst inserting the metadata "
369 "from the xattr into the header; rc = "
370 "[%d]\n", __FUNCTION__, rc);
375 rc = ecryptfs_read_lower_page_segment(
376 page, page->index, 0, PAGE_CACHE_SIZE,
377 page->mapping->host);
379 printk(KERN_ERR "Error reading page; rc = "
385 rc = ecryptfs_decrypt_page(page);
387 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
393 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
400 * Called with lower inode mutex held.
402 static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
404 struct inode *inode = page->mapping->host;
405 int end_byte_in_page;
407 if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
409 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
410 if (to > end_byte_in_page)
411 end_byte_in_page = to;
412 zero_user_page(page, end_byte_in_page,
413 PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
419 * eCryptfs does not currently support holes. When writing after a
420 * seek past the end of the file, eCryptfs fills in 0's through to the
421 * current location. The code to fill in the 0's to all the
422 * intermediate pages calls ecryptfs_prepare_write_no_truncate().
425 ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page,
426 unsigned from, unsigned to)
430 if (from == 0 && to == PAGE_CACHE_SIZE)
431 goto out; /* If we are writing a full page, it will be
433 if (!PageUptodate(page))
434 rc = ecryptfs_do_readpage(file, page, page->index);
439 static int ecryptfs_prepare_write(struct file *file, struct page *page,
440 unsigned from, unsigned to)
444 if (from == 0 && to == PAGE_CACHE_SIZE)
445 goto out; /* If we are writing a full page, it will be
447 if (!PageUptodate(page))
448 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
450 page->mapping->host);
451 if (page->index != 0) {
452 loff_t end_of_prev_pg_pos =
453 (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
455 if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
456 rc = ecryptfs_truncate(file->f_path.dentry,
459 printk(KERN_ERR "Error on attempt to "
460 "truncate to (higher) offset [%lld];"
461 " rc = [%d]\n", end_of_prev_pg_pos, rc);
465 if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host))
466 zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
472 int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
473 struct inode *lower_inode,
474 struct writeback_control *wbc)
478 rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
480 ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
484 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
485 page_cache_release(lower_page);
490 static void ecryptfs_release_lower_page(struct page *lower_page)
492 unlock_page(lower_page);
493 page_cache_release(lower_page);
497 * ecryptfs_write_inode_size_to_header
499 * Writes the lower file size to the first 8 bytes of the header.
501 * Returns zero on success; non-zero on error.
503 static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
506 char *file_size_virt;
509 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
510 if (!file_size_virt) {
514 file_size = (u64)i_size_read(ecryptfs_inode);
515 file_size = cpu_to_be64(file_size);
516 memcpy(file_size_virt, &file_size, sizeof(u64));
517 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
519 kfree(file_size_virt);
521 printk(KERN_ERR "%s: Error writing file size to header; "
522 "rc = [%d]\n", __FUNCTION__, rc);
527 struct kmem_cache *ecryptfs_xattr_cache;
529 static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
533 struct dentry *lower_dentry =
534 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
535 struct inode *lower_inode = lower_dentry->d_inode;
539 if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
541 "No support for setting xattr in lower filesystem\n");
545 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
547 printk(KERN_ERR "Out of memory whilst attempting to write "
548 "inode size to xattr\n");
552 mutex_lock(&lower_inode->i_mutex);
553 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
554 xattr_virt, PAGE_CACHE_SIZE);
557 file_size = (u64)i_size_read(ecryptfs_inode);
558 file_size = cpu_to_be64(file_size);
559 memcpy(xattr_virt, &file_size, sizeof(u64));
560 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
561 xattr_virt, size, 0);
562 mutex_unlock(&lower_inode->i_mutex);
564 printk(KERN_ERR "Error whilst attempting to write inode size "
565 "to lower file xattr; rc = [%d]\n", rc);
566 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
571 int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
573 struct ecryptfs_crypt_stat *crypt_stat;
575 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
576 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
577 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
579 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
582 int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
583 struct file *lower_file,
584 unsigned long lower_page_index, int byte_offset,
589 *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
590 if (!(*lower_page)) {
592 ecryptfs_printk(KERN_ERR, "Error attempting to grab "
593 "lower page with index [0x%.16x]\n",
597 rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
602 ecryptfs_printk(KERN_ERR, "prepare_write for "
603 "lower_page_index = [0x%.16x] failed; rc = "
604 "[%d]\n", lower_page_index, rc);
605 ecryptfs_release_lower_page(*lower_page);
606 (*lower_page) = NULL;
613 * ecryptfs_commit_lower_page
615 * Returns zero on success; non-zero on error
618 ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
619 struct file *lower_file, int byte_offset,
624 rc = lower_inode->i_mapping->a_ops->commit_write(
625 lower_file, lower_page, byte_offset, region_size);
627 ecryptfs_printk(KERN_ERR,
628 "Error committing write; rc = [%d]\n", rc);
631 ecryptfs_release_lower_page(lower_page);
636 * ecryptfs_copy_page_to_lower
638 * Used for plaintext pass-through; no page index interpolation
641 int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
642 struct file *lower_file)
645 struct page *lower_page;
647 rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
648 page->index, 0, PAGE_CACHE_SIZE);
650 ecryptfs_printk(KERN_ERR, "Error attempting to get page "
651 "at index [0x%.16x]\n", page->index);
655 memcpy((char *)page_address(lower_page), page_address(page),
657 rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
660 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
661 "at index [0x%.16x]\n", page->index);
667 * ecryptfs_commit_write
668 * @file: The eCryptfs file object
669 * @page: The eCryptfs page
670 * @from: Ignored (we rotate the page IV on each write)
673 * This is where we encrypt the data and pass the encrypted data to
674 * the lower filesystem. In OpenPGP-compatible mode, we operate on
675 * entire underlying packets.
677 static int ecryptfs_commit_write(struct file *file, struct page *page,
678 unsigned from, unsigned to)
681 struct inode *ecryptfs_inode = page->mapping->host;
682 struct ecryptfs_crypt_stat *crypt_stat =
683 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
686 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
687 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
688 "crypt_stat at memory location [%p]\n", crypt_stat);
689 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
691 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
692 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
693 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
695 /* Fills in zeros if 'to' goes beyond inode size */
696 rc = fill_zeros_to_end_of_page(page, to);
698 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
699 "zeros in page with index = [0x%.16x]\n",
703 rc = ecryptfs_encrypt_page(page);
705 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
706 "index [0x%.16x])\n", page->index);
709 pos = (page->index << PAGE_CACHE_SHIFT) + to;
710 if (pos > i_size_read(ecryptfs_inode)) {
711 i_size_write(ecryptfs_inode, pos);
712 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
713 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
715 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
717 printk(KERN_ERR "Error writing inode size to metadata; "
724 * ecryptfs_write_zeros
725 * @file: The ecryptfs file
726 * @index: The index in which we are writing
727 * @start: The position after the last block of data
728 * @num_zeros: The number of zeros to write
730 * Write a specified number of zero's to a page.
732 * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
735 ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
738 struct page *tmp_page;
740 tmp_page = ecryptfs_get1page(file, index);
741 if (IS_ERR(tmp_page)) {
742 ecryptfs_printk(KERN_ERR, "Error getting page at index "
743 "[0x%.16x]\n", index);
744 rc = PTR_ERR(tmp_page);
747 rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start,
748 (start + num_zeros));
750 ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
751 "to page at index [0x%.16x]\n",
753 page_cache_release(tmp_page);
756 zero_user_page(tmp_page, start, num_zeros, KM_USER0);
757 rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
759 ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
760 "to remainder of page at index [0x%.16x]\n",
762 page_cache_release(tmp_page);
766 page_cache_release(tmp_page);
771 static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
775 struct inode *lower_inode;
777 inode = (struct inode *)mapping->host;
778 lower_inode = ecryptfs_inode_to_lower(inode);
779 if (lower_inode->i_mapping->a_ops->bmap)
780 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
785 struct address_space_operations ecryptfs_aops = {
786 .writepage = ecryptfs_writepage,
787 .readpage = ecryptfs_readpage,
788 .prepare_write = ecryptfs_prepare_write,
789 .commit_write = ecryptfs_commit_write,
790 .bmap = ecryptfs_bmap,