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 * @page: Page that is locked before this call is made
63 * Returns zero on success; non-zero otherwise
65 static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
69 rc = ecryptfs_encrypt_page(page);
71 ecryptfs_printk(KERN_WARNING, "Error encrypting "
72 "page (upper index [0x%.16x])\n", page->index);
73 ClearPageUptodate(page);
76 SetPageUptodate(page);
84 * Octets 0-7: Unencrypted file size (big-endian)
85 * Octets 8-15: eCryptfs special marker
87 * Octet 16: File format version number (between 0 and 255)
88 * Octets 17-18: Reserved
89 * Octet 19: Bit 1 (lsb): Reserved
92 * Octets 20-23: Header extent size (big-endian)
93 * Octets 24-25: Number of header extents at front of file
95 * Octet 26: Begin RFC 2440 authentication token packet set
97 static void set_header_info(char *page_virt,
98 struct ecryptfs_crypt_stat *crypt_stat)
101 int save_num_header_extents_at_front =
102 crypt_stat->num_header_extents_at_front;
104 crypt_stat->num_header_extents_at_front = 1;
105 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
106 crypt_stat->num_header_extents_at_front =
107 save_num_header_extents_at_front;
111 * ecryptfs_copy_up_encrypted_with_header
112 * @page: Sort of a ``virtual'' representation of the encrypted lower
113 * file. The actual lower file does not have the metadata in
114 * the header. This is locked.
115 * @crypt_stat: The eCryptfs inode's cryptographic context
117 * The ``view'' is the version of the file that userspace winds up
118 * seeing, with the header information inserted.
121 ecryptfs_copy_up_encrypted_with_header(struct page *page,
122 struct ecryptfs_crypt_stat *crypt_stat)
124 loff_t extent_num_in_page = 0;
125 loff_t num_extents_per_page = (PAGE_CACHE_SIZE
126 / crypt_stat->extent_size);
129 while (extent_num_in_page < num_extents_per_page) {
130 loff_t view_extent_num = ((((loff_t)page->index)
131 * num_extents_per_page)
132 + extent_num_in_page);
134 if (view_extent_num < crypt_stat->num_header_extents_at_front) {
135 /* This is a header extent */
138 page_virt = kmap_atomic(page, KM_USER0);
139 memset(page_virt, 0, PAGE_CACHE_SIZE);
140 /* TODO: Support more than one header extent */
141 if (view_extent_num == 0) {
142 rc = ecryptfs_read_xattr_region(
143 page_virt, page->mapping->host);
144 set_header_info(page_virt, crypt_stat);
146 kunmap_atomic(page_virt, KM_USER0);
147 flush_dcache_page(page);
149 ClearPageUptodate(page);
150 printk(KERN_ERR "%s: Error reading xattr "
151 "region; rc = [%d]\n", __FUNCTION__, rc);
154 SetPageUptodate(page);
156 /* This is an encrypted data extent */
157 loff_t lower_offset =
159 crypt_stat->num_header_extents_at_front)
160 * crypt_stat->extent_size);
162 rc = ecryptfs_read_lower_page_segment(
163 page, (lower_offset >> PAGE_CACHE_SHIFT),
164 (lower_offset & ~PAGE_CACHE_MASK),
165 crypt_stat->extent_size, page->mapping->host);
167 printk(KERN_ERR "%s: Error attempting to read "
168 "extent at offset [%lld] in the lower "
169 "file; rc = [%d]\n", __FUNCTION__,
174 extent_num_in_page++;
182 * @file: An eCryptfs file
183 * @page: Page from eCryptfs inode mapping into which to stick the read data
185 * Read in a page, decrypting if necessary.
187 * Returns zero on success; non-zero on error.
189 static int ecryptfs_readpage(struct file *file, struct page *page)
191 struct ecryptfs_crypt_stat *crypt_stat =
192 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
196 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
197 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
198 ecryptfs_printk(KERN_DEBUG,
199 "Passing through unencrypted page\n");
200 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
202 page->mapping->host);
203 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
204 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
205 rc = ecryptfs_copy_up_encrypted_with_header(page,
208 printk(KERN_ERR "%s: Error attempting to copy "
209 "the encrypted content from the lower "
210 "file whilst inserting the metadata "
211 "from the xattr into the header; rc = "
212 "[%d]\n", __FUNCTION__, rc);
217 rc = ecryptfs_read_lower_page_segment(
218 page, page->index, 0, PAGE_CACHE_SIZE,
219 page->mapping->host);
221 printk(KERN_ERR "Error reading page; rc = "
227 rc = ecryptfs_decrypt_page(page);
229 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
235 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
242 * Called with lower inode mutex held.
244 static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
246 struct inode *inode = page->mapping->host;
247 int end_byte_in_page;
249 if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
251 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
252 if (to > end_byte_in_page)
253 end_byte_in_page = to;
254 zero_user_page(page, end_byte_in_page,
255 PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
260 static int ecryptfs_prepare_write(struct file *file, struct page *page,
261 unsigned from, unsigned to)
265 if (from == 0 && to == PAGE_CACHE_SIZE)
266 goto out; /* If we are writing a full page, it will be
268 if (!PageUptodate(page))
269 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
271 page->mapping->host);
272 if (page->index != 0) {
273 loff_t end_of_prev_pg_pos =
274 (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
276 if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
277 rc = ecryptfs_truncate(file->f_path.dentry,
280 printk(KERN_ERR "Error on attempt to "
281 "truncate to (higher) offset [%lld];"
282 " rc = [%d]\n", end_of_prev_pg_pos, rc);
286 if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host))
287 zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
294 * ecryptfs_write_inode_size_to_header
296 * Writes the lower file size to the first 8 bytes of the header.
298 * Returns zero on success; non-zero on error.
300 static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
303 char *file_size_virt;
306 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
307 if (!file_size_virt) {
311 file_size = (u64)i_size_read(ecryptfs_inode);
312 file_size = cpu_to_be64(file_size);
313 memcpy(file_size_virt, &file_size, sizeof(u64));
314 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
316 kfree(file_size_virt);
318 printk(KERN_ERR "%s: Error writing file size to header; "
319 "rc = [%d]\n", __FUNCTION__, rc);
324 struct kmem_cache *ecryptfs_xattr_cache;
326 static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
330 struct dentry *lower_dentry =
331 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
332 struct inode *lower_inode = lower_dentry->d_inode;
336 if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
338 "No support for setting xattr in lower filesystem\n");
342 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
344 printk(KERN_ERR "Out of memory whilst attempting to write "
345 "inode size to xattr\n");
349 mutex_lock(&lower_inode->i_mutex);
350 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
351 xattr_virt, PAGE_CACHE_SIZE);
354 file_size = (u64)i_size_read(ecryptfs_inode);
355 file_size = cpu_to_be64(file_size);
356 memcpy(xattr_virt, &file_size, sizeof(u64));
357 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
358 xattr_virt, size, 0);
359 mutex_unlock(&lower_inode->i_mutex);
361 printk(KERN_ERR "Error whilst attempting to write inode size "
362 "to lower file xattr; rc = [%d]\n", rc);
363 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
368 int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
370 struct ecryptfs_crypt_stat *crypt_stat;
372 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
373 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
374 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
376 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
380 * ecryptfs_commit_write
381 * @file: The eCryptfs file object
382 * @page: The eCryptfs page
383 * @from: Ignored (we rotate the page IV on each write)
386 * This is where we encrypt the data and pass the encrypted data to
387 * the lower filesystem. In OpenPGP-compatible mode, we operate on
388 * entire underlying packets.
390 static int ecryptfs_commit_write(struct file *file, struct page *page,
391 unsigned from, unsigned to)
394 struct inode *ecryptfs_inode = page->mapping->host;
395 struct ecryptfs_crypt_stat *crypt_stat =
396 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
399 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
400 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
401 "crypt_stat at memory location [%p]\n", crypt_stat);
402 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
404 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
405 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
406 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
408 /* Fills in zeros if 'to' goes beyond inode size */
409 rc = fill_zeros_to_end_of_page(page, to);
411 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
412 "zeros in page with index = [0x%.16x]\n",
416 rc = ecryptfs_encrypt_page(page);
418 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
419 "index [0x%.16x])\n", page->index);
422 pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
423 if (pos > i_size_read(ecryptfs_inode)) {
424 i_size_write(ecryptfs_inode, pos);
425 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
426 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
428 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
430 printk(KERN_ERR "Error writing inode size to metadata; "
436 static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
440 struct inode *lower_inode;
442 inode = (struct inode *)mapping->host;
443 lower_inode = ecryptfs_inode_to_lower(inode);
444 if (lower_inode->i_mapping->a_ops->bmap)
445 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
450 struct address_space_operations ecryptfs_aops = {
451 .writepage = ecryptfs_writepage,
452 .readpage = ecryptfs_readpage,
453 .prepare_write = ecryptfs_prepare_write,
454 .commit_write = ecryptfs_commit_write,
455 .bmap = ecryptfs_bmap,