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
;
40 * ecryptfs_get_locked_page
42 * Get one page from cache or lower f/s, return error otherwise.
44 * Returns locked and up-to-date page (if ok), with increased
47 struct page
*ecryptfs_get_locked_page(struct file
*file
, loff_t index
)
49 struct dentry
*dentry
;
51 struct address_space
*mapping
;
54 dentry
= file
->f_path
.dentry
;
55 inode
= dentry
->d_inode
;
56 mapping
= inode
->i_mapping
;
57 page
= read_mapping_page(mapping
, index
, (void *)file
);
65 * @page: Page that is locked before this call is made
67 * Returns zero on success; non-zero otherwise
69 static int ecryptfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
73 rc
= ecryptfs_encrypt_page(page
);
75 ecryptfs_printk(KERN_WARNING
, "Error encrypting "
76 "page (upper index [0x%.16x])\n", page
->index
);
77 ClearPageUptodate(page
);
80 SetPageUptodate(page
);
88 * Octets 0-7: Unencrypted file size (big-endian)
89 * Octets 8-15: eCryptfs special marker
91 * Octet 16: File format version number (between 0 and 255)
92 * Octets 17-18: Reserved
93 * Octet 19: Bit 1 (lsb): Reserved
96 * Octets 20-23: Header extent size (big-endian)
97 * Octets 24-25: Number of header extents at front of file
99 * Octet 26: Begin RFC 2440 authentication token packet set
101 static void set_header_info(char *page_virt
,
102 struct ecryptfs_crypt_stat
*crypt_stat
)
105 int save_num_header_extents_at_front
=
106 crypt_stat
->num_header_extents_at_front
;
108 crypt_stat
->num_header_extents_at_front
= 1;
109 ecryptfs_write_header_metadata(page_virt
+ 20, crypt_stat
, &written
);
110 crypt_stat
->num_header_extents_at_front
=
111 save_num_header_extents_at_front
;
115 * ecryptfs_copy_up_encrypted_with_header
116 * @page: Sort of a ``virtual'' representation of the encrypted lower
117 * file. The actual lower file does not have the metadata in
118 * the header. This is locked.
119 * @crypt_stat: The eCryptfs inode's cryptographic context
121 * The ``view'' is the version of the file that userspace winds up
122 * seeing, with the header information inserted.
125 ecryptfs_copy_up_encrypted_with_header(struct page
*page
,
126 struct ecryptfs_crypt_stat
*crypt_stat
)
128 loff_t extent_num_in_page
= 0;
129 loff_t num_extents_per_page
= (PAGE_CACHE_SIZE
130 / crypt_stat
->extent_size
);
133 while (extent_num_in_page
< num_extents_per_page
) {
134 loff_t view_extent_num
= ((((loff_t
)page
->index
)
135 * num_extents_per_page
)
136 + extent_num_in_page
);
138 if (view_extent_num
< crypt_stat
->num_header_extents_at_front
) {
139 /* This is a header extent */
142 page_virt
= kmap_atomic(page
, KM_USER0
);
143 memset(page_virt
, 0, PAGE_CACHE_SIZE
);
144 /* TODO: Support more than one header extent */
145 if (view_extent_num
== 0) {
146 rc
= ecryptfs_read_xattr_region(
147 page_virt
, page
->mapping
->host
);
148 set_header_info(page_virt
, crypt_stat
);
150 kunmap_atomic(page_virt
, KM_USER0
);
151 flush_dcache_page(page
);
153 printk(KERN_ERR
"%s: Error reading xattr "
154 "region; rc = [%d]\n", __FUNCTION__
, rc
);
158 /* This is an encrypted data extent */
159 loff_t lower_offset
=
161 crypt_stat
->num_header_extents_at_front
)
162 * crypt_stat
->extent_size
);
164 rc
= ecryptfs_read_lower_page_segment(
165 page
, (lower_offset
>> PAGE_CACHE_SHIFT
),
166 (lower_offset
& ~PAGE_CACHE_MASK
),
167 crypt_stat
->extent_size
, page
->mapping
->host
);
169 printk(KERN_ERR
"%s: Error attempting to read "
170 "extent at offset [%lld] in the lower "
171 "file; rc = [%d]\n", __FUNCTION__
,
176 extent_num_in_page
++;
184 * @file: An eCryptfs file
185 * @page: Page from eCryptfs inode mapping into which to stick the read data
187 * Read in a page, decrypting if necessary.
189 * Returns zero on success; non-zero on error.
191 static int ecryptfs_readpage(struct file
*file
, struct page
*page
)
193 struct ecryptfs_crypt_stat
*crypt_stat
=
194 &ecryptfs_inode_to_private(file
->f_path
.dentry
->d_inode
)->crypt_stat
;
198 || !(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)
199 || (crypt_stat
->flags
& ECRYPTFS_NEW_FILE
)) {
200 ecryptfs_printk(KERN_DEBUG
,
201 "Passing through unencrypted page\n");
202 rc
= ecryptfs_read_lower_page_segment(page
, page
->index
, 0,
204 page
->mapping
->host
);
205 } else if (crypt_stat
->flags
& ECRYPTFS_VIEW_AS_ENCRYPTED
) {
206 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
) {
207 rc
= ecryptfs_copy_up_encrypted_with_header(page
,
210 printk(KERN_ERR
"%s: Error attempting to copy "
211 "the encrypted content from the lower "
212 "file whilst inserting the metadata "
213 "from the xattr into the header; rc = "
214 "[%d]\n", __FUNCTION__
, rc
);
219 rc
= ecryptfs_read_lower_page_segment(
220 page
, page
->index
, 0, PAGE_CACHE_SIZE
,
221 page
->mapping
->host
);
223 printk(KERN_ERR
"Error reading page; rc = "
229 rc
= ecryptfs_decrypt_page(page
);
231 ecryptfs_printk(KERN_ERR
, "Error decrypting page; "
238 ClearPageUptodate(page
);
240 SetPageUptodate(page
);
241 ecryptfs_printk(KERN_DEBUG
, "Unlocking page with index = [0x%.16x]\n",
248 * Called with lower inode mutex held.
250 static int fill_zeros_to_end_of_page(struct page
*page
, unsigned int to
)
252 struct inode
*inode
= page
->mapping
->host
;
253 int end_byte_in_page
;
255 if ((i_size_read(inode
) / PAGE_CACHE_SIZE
) != page
->index
)
257 end_byte_in_page
= i_size_read(inode
) % PAGE_CACHE_SIZE
;
258 if (to
> end_byte_in_page
)
259 end_byte_in_page
= to
;
260 zero_user_page(page
, end_byte_in_page
,
261 PAGE_CACHE_SIZE
- end_byte_in_page
, KM_USER0
);
266 static int ecryptfs_prepare_write(struct file
*file
, struct page
*page
,
267 unsigned from
, unsigned to
)
271 if (from
== 0 && to
== PAGE_CACHE_SIZE
)
272 goto out
; /* If we are writing a full page, it will be
274 if (!PageUptodate(page
)) {
275 rc
= ecryptfs_read_lower_page_segment(page
, page
->index
, 0,
277 page
->mapping
->host
);
279 printk(KERN_ERR
"%s: Error attemping to read lower "
280 "page segment; rc = [%d]\n", __FUNCTION__
, rc
);
281 ClearPageUptodate(page
);
284 SetPageUptodate(page
);
286 if (page
->index
!= 0) {
287 loff_t end_of_prev_pg_pos
=
288 (((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) - 1);
290 if (end_of_prev_pg_pos
> i_size_read(page
->mapping
->host
)) {
291 rc
= ecryptfs_truncate(file
->f_path
.dentry
,
294 printk(KERN_ERR
"Error on attempt to "
295 "truncate to (higher) offset [%lld];"
296 " rc = [%d]\n", end_of_prev_pg_pos
, rc
);
300 if (end_of_prev_pg_pos
+ 1 > i_size_read(page
->mapping
->host
))
301 zero_user_page(page
, 0, PAGE_CACHE_SIZE
, KM_USER0
);
308 * ecryptfs_write_inode_size_to_header
310 * Writes the lower file size to the first 8 bytes of the header.
312 * Returns zero on success; non-zero on error.
314 static int ecryptfs_write_inode_size_to_header(struct inode
*ecryptfs_inode
)
317 char *file_size_virt
;
320 file_size_virt
= kmalloc(sizeof(u64
), GFP_KERNEL
);
321 if (!file_size_virt
) {
325 file_size
= (u64
)i_size_read(ecryptfs_inode
);
326 file_size
= cpu_to_be64(file_size
);
327 memcpy(file_size_virt
, &file_size
, sizeof(u64
));
328 rc
= ecryptfs_write_lower(ecryptfs_inode
, file_size_virt
, 0,
330 kfree(file_size_virt
);
332 printk(KERN_ERR
"%s: Error writing file size to header; "
333 "rc = [%d]\n", __FUNCTION__
, rc
);
338 struct kmem_cache
*ecryptfs_xattr_cache
;
340 static int ecryptfs_write_inode_size_to_xattr(struct inode
*ecryptfs_inode
)
344 struct dentry
*lower_dentry
=
345 ecryptfs_inode_to_private(ecryptfs_inode
)->lower_file
->f_dentry
;
346 struct inode
*lower_inode
= lower_dentry
->d_inode
;
350 if (!lower_inode
->i_op
->getxattr
|| !lower_inode
->i_op
->setxattr
) {
352 "No support for setting xattr in lower filesystem\n");
356 xattr_virt
= kmem_cache_alloc(ecryptfs_xattr_cache
, GFP_KERNEL
);
358 printk(KERN_ERR
"Out of memory whilst attempting to write "
359 "inode size to xattr\n");
363 mutex_lock(&lower_inode
->i_mutex
);
364 size
= lower_inode
->i_op
->getxattr(lower_dentry
, ECRYPTFS_XATTR_NAME
,
365 xattr_virt
, PAGE_CACHE_SIZE
);
368 file_size
= (u64
)i_size_read(ecryptfs_inode
);
369 file_size
= cpu_to_be64(file_size
);
370 memcpy(xattr_virt
, &file_size
, sizeof(u64
));
371 rc
= lower_inode
->i_op
->setxattr(lower_dentry
, ECRYPTFS_XATTR_NAME
,
372 xattr_virt
, size
, 0);
373 mutex_unlock(&lower_inode
->i_mutex
);
375 printk(KERN_ERR
"Error whilst attempting to write inode size "
376 "to lower file xattr; rc = [%d]\n", rc
);
377 kmem_cache_free(ecryptfs_xattr_cache
, xattr_virt
);
382 int ecryptfs_write_inode_size_to_metadata(struct inode
*ecryptfs_inode
)
384 struct ecryptfs_crypt_stat
*crypt_stat
;
386 crypt_stat
= &ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
;
387 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
)
388 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode
);
390 return ecryptfs_write_inode_size_to_header(ecryptfs_inode
);
394 * ecryptfs_commit_write
395 * @file: The eCryptfs file object
396 * @page: The eCryptfs page
397 * @from: Ignored (we rotate the page IV on each write)
400 * This is where we encrypt the data and pass the encrypted data to
401 * the lower filesystem. In OpenPGP-compatible mode, we operate on
402 * entire underlying packets.
404 static int ecryptfs_commit_write(struct file
*file
, struct page
*page
,
405 unsigned from
, unsigned to
)
408 struct inode
*ecryptfs_inode
= page
->mapping
->host
;
409 struct ecryptfs_crypt_stat
*crypt_stat
=
410 &ecryptfs_inode_to_private(file
->f_path
.dentry
->d_inode
)->crypt_stat
;
413 if (crypt_stat
->flags
& ECRYPTFS_NEW_FILE
) {
414 ecryptfs_printk(KERN_DEBUG
, "ECRYPTFS_NEW_FILE flag set in "
415 "crypt_stat at memory location [%p]\n", crypt_stat
);
416 crypt_stat
->flags
&= ~(ECRYPTFS_NEW_FILE
);
418 ecryptfs_printk(KERN_DEBUG
, "Not a new file\n");
419 ecryptfs_printk(KERN_DEBUG
, "Calling fill_zeros_to_end_of_page"
420 "(page w/ index = [0x%.16x], to = [%d])\n", page
->index
,
422 /* Fills in zeros if 'to' goes beyond inode size */
423 rc
= fill_zeros_to_end_of_page(page
, to
);
425 ecryptfs_printk(KERN_WARNING
, "Error attempting to fill "
426 "zeros in page with index = [0x%.16x]\n",
430 rc
= ecryptfs_encrypt_page(page
);
432 ecryptfs_printk(KERN_WARNING
, "Error encrypting page (upper "
433 "index [0x%.16x])\n", page
->index
);
436 pos
= (((loff_t
)page
->index
) << PAGE_CACHE_SHIFT
) + to
;
437 if (pos
> i_size_read(ecryptfs_inode
)) {
438 i_size_write(ecryptfs_inode
, pos
);
439 ecryptfs_printk(KERN_DEBUG
, "Expanded file size to "
440 "[0x%.16x]\n", i_size_read(ecryptfs_inode
));
442 rc
= ecryptfs_write_inode_size_to_metadata(ecryptfs_inode
);
444 printk(KERN_ERR
"Error writing inode size to metadata; "
450 static sector_t
ecryptfs_bmap(struct address_space
*mapping
, sector_t block
)
454 struct inode
*lower_inode
;
456 inode
= (struct inode
*)mapping
->host
;
457 lower_inode
= ecryptfs_inode_to_lower(inode
);
458 if (lower_inode
->i_mapping
->a_ops
->bmap
)
459 rc
= lower_inode
->i_mapping
->a_ops
->bmap(lower_inode
->i_mapping
,
464 struct address_space_operations ecryptfs_aops
= {
465 .writepage
= ecryptfs_writepage
,
466 .readpage
= ecryptfs_readpage
,
467 .prepare_write
= ecryptfs_prepare_write
,
468 .commit_write
= ecryptfs_commit_write
,
469 .bmap
= ecryptfs_bmap
,