1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * eCryptfs: Linux filesystem encryption layer
4 * This is where eCryptfs coordinates the symmetric encryption and
5 * decryption of the file data as it passes between the lower
6 * encrypted file and the upper decrypted file.
8 * Copyright (C) 1997-2003 Erez Zadok
9 * Copyright (C) 2001-2003 Stony Brook University
10 * Copyright (C) 2004-2007 International Business Machines Corp.
11 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
14 #include <linux/pagemap.h>
15 #include <linux/writeback.h>
16 #include <linux/page-flags.h>
17 #include <linux/mount.h>
18 #include <linux/file.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <linux/xattr.h>
22 #include <asm/unaligned.h>
23 #include "ecryptfs_kernel.h"
26 * ecryptfs_get_locked_page
28 * Get one page from cache or lower f/s, return error otherwise.
30 * Returns locked and up-to-date page (if ok), with increased
33 struct page
*ecryptfs_get_locked_page(struct inode
*inode
, loff_t index
)
35 struct page
*page
= read_mapping_page(inode
->i_mapping
, index
, NULL
);
43 * @page: Page that is locked before this call is made
45 * Returns zero on success; non-zero otherwise
47 * This is where we encrypt the data and pass the encrypted data to
48 * the lower filesystem. In OpenPGP-compatible mode, we operate on
49 * entire underlying packets.
51 static int ecryptfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
55 rc
= ecryptfs_encrypt_page(page
);
57 ecryptfs_printk(KERN_WARNING
, "Error encrypting "
58 "page (upper index [0x%.16lx])\n", page
->index
);
59 ClearPageUptodate(page
);
62 SetPageUptodate(page
);
68 static void strip_xattr_flag(char *page_virt
,
69 struct ecryptfs_crypt_stat
*crypt_stat
)
71 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
) {
74 crypt_stat
->flags
&= ~ECRYPTFS_METADATA_IN_XATTR
;
75 ecryptfs_write_crypt_stat_flags(page_virt
, crypt_stat
,
77 crypt_stat
->flags
|= ECRYPTFS_METADATA_IN_XATTR
;
83 * Octets 0-7: Unencrypted file size (big-endian)
84 * Octets 8-15: eCryptfs special marker
86 * Octet 16: File format version number (between 0 and 255)
87 * Octets 17-18: Reserved
88 * Octet 19: Bit 1 (lsb): Reserved
91 * Octets 20-23: Header extent size (big-endian)
92 * Octets 24-25: Number of header extents at front of file
94 * Octet 26: Begin RFC 2440 authentication token packet set
98 * ecryptfs_copy_up_encrypted_with_header
99 * @page: Sort of a ``virtual'' representation of the encrypted lower
100 * file. The actual lower file does not have the metadata in
101 * the header. This is locked.
102 * @crypt_stat: The eCryptfs inode's cryptographic context
104 * The ``view'' is the version of the file that userspace winds up
105 * seeing, with the header information inserted.
108 ecryptfs_copy_up_encrypted_with_header(struct page
*page
,
109 struct ecryptfs_crypt_stat
*crypt_stat
)
111 loff_t extent_num_in_page
= 0;
112 loff_t num_extents_per_page
= (PAGE_SIZE
113 / crypt_stat
->extent_size
);
116 while (extent_num_in_page
< num_extents_per_page
) {
117 loff_t view_extent_num
= ((((loff_t
)page
->index
)
118 * num_extents_per_page
)
119 + extent_num_in_page
);
120 size_t num_header_extents_at_front
=
121 (crypt_stat
->metadata_size
/ crypt_stat
->extent_size
);
123 if (view_extent_num
< num_header_extents_at_front
) {
124 /* This is a header extent */
127 page_virt
= kmap_atomic(page
);
128 memset(page_virt
, 0, PAGE_SIZE
);
129 /* TODO: Support more than one header extent */
130 if (view_extent_num
== 0) {
133 rc
= ecryptfs_read_xattr_region(
134 page_virt
, page
->mapping
->host
);
135 strip_xattr_flag(page_virt
+ 16, crypt_stat
);
136 ecryptfs_write_header_metadata(page_virt
+ 20,
140 kunmap_atomic(page_virt
);
141 flush_dcache_page(page
);
143 printk(KERN_ERR
"%s: Error reading xattr "
144 "region; rc = [%d]\n", __func__
, rc
);
148 /* This is an encrypted data extent */
149 loff_t lower_offset
=
150 ((view_extent_num
* crypt_stat
->extent_size
)
151 - crypt_stat
->metadata_size
);
153 rc
= ecryptfs_read_lower_page_segment(
154 page
, (lower_offset
>> PAGE_SHIFT
),
155 (lower_offset
& ~PAGE_MASK
),
156 crypt_stat
->extent_size
, page
->mapping
->host
);
158 printk(KERN_ERR
"%s: Error attempting to read "
159 "extent at offset [%lld] in the lower "
160 "file; rc = [%d]\n", __func__
,
165 extent_num_in_page
++;
173 * @file: An eCryptfs file
174 * @page: Page from eCryptfs inode mapping into which to stick the read data
176 * Read in a page, decrypting if necessary.
178 * Returns zero on success; non-zero on error.
180 static int ecryptfs_readpage(struct file
*file
, struct page
*page
)
182 struct ecryptfs_crypt_stat
*crypt_stat
=
183 &ecryptfs_inode_to_private(page
->mapping
->host
)->crypt_stat
;
186 if (!crypt_stat
|| !(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)) {
187 rc
= ecryptfs_read_lower_page_segment(page
, page
->index
, 0,
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
,
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", __func__
, rc
);
204 rc
= ecryptfs_read_lower_page_segment(
205 page
, page
->index
, 0, PAGE_SIZE
,
206 page
->mapping
->host
);
208 printk(KERN_ERR
"Error reading page; rc = "
214 rc
= ecryptfs_decrypt_page(page
);
216 ecryptfs_printk(KERN_ERR
, "Error decrypting page; "
223 ClearPageUptodate(page
);
225 SetPageUptodate(page
);
226 ecryptfs_printk(KERN_DEBUG
, "Unlocking page with index = [0x%.16lx]\n",
233 * Called with lower inode mutex held.
235 static int fill_zeros_to_end_of_page(struct page
*page
, unsigned int to
)
237 struct inode
*inode
= page
->mapping
->host
;
238 int end_byte_in_page
;
240 if ((i_size_read(inode
) / PAGE_SIZE
) != page
->index
)
242 end_byte_in_page
= i_size_read(inode
) % PAGE_SIZE
;
243 if (to
> end_byte_in_page
)
244 end_byte_in_page
= to
;
245 zero_user_segment(page
, end_byte_in_page
, PAGE_SIZE
);
251 * ecryptfs_write_begin
252 * @file: The eCryptfs file
253 * @mapping: The eCryptfs object
254 * @pos: The file offset at which to start writing
255 * @len: Length of the write
256 * @flags: Various flags
257 * @pagep: Pointer to return the page
258 * @fsdata: Pointer to return fs data (unused)
260 * This function must zero any hole we create
262 * Returns zero on success; non-zero otherwise
264 static int ecryptfs_write_begin(struct file
*file
,
265 struct address_space
*mapping
,
266 loff_t pos
, unsigned len
, unsigned flags
,
267 struct page
**pagep
, void **fsdata
)
269 pgoff_t index
= pos
>> PAGE_SHIFT
;
271 loff_t prev_page_end_size
;
274 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
279 prev_page_end_size
= ((loff_t
)index
<< PAGE_SHIFT
);
280 if (!PageUptodate(page
)) {
281 struct ecryptfs_crypt_stat
*crypt_stat
=
282 &ecryptfs_inode_to_private(mapping
->host
)->crypt_stat
;
284 if (!(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)) {
285 rc
= ecryptfs_read_lower_page_segment(
286 page
, index
, 0, PAGE_SIZE
, mapping
->host
);
288 printk(KERN_ERR
"%s: Error attempting to read "
289 "lower page segment; rc = [%d]\n",
291 ClearPageUptodate(page
);
294 SetPageUptodate(page
);
295 } else if (crypt_stat
->flags
& ECRYPTFS_VIEW_AS_ENCRYPTED
) {
296 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
) {
297 rc
= ecryptfs_copy_up_encrypted_with_header(
300 printk(KERN_ERR
"%s: Error attempting "
301 "to copy the encrypted content "
302 "from the lower file whilst "
303 "inserting the metadata from "
304 "the xattr into the header; rc "
305 "= [%d]\n", __func__
, rc
);
306 ClearPageUptodate(page
);
309 SetPageUptodate(page
);
311 rc
= ecryptfs_read_lower_page_segment(
312 page
, index
, 0, PAGE_SIZE
,
315 printk(KERN_ERR
"%s: Error reading "
318 ClearPageUptodate(page
);
321 SetPageUptodate(page
);
324 if (prev_page_end_size
325 >= i_size_read(page
->mapping
->host
)) {
326 zero_user(page
, 0, PAGE_SIZE
);
327 SetPageUptodate(page
);
328 } else if (len
< PAGE_SIZE
) {
329 rc
= ecryptfs_decrypt_page(page
);
331 printk(KERN_ERR
"%s: Error decrypting "
332 "page at index [%ld]; "
334 __func__
, page
->index
, rc
);
335 ClearPageUptodate(page
);
338 SetPageUptodate(page
);
342 /* If creating a page or more of holes, zero them out via truncate.
343 * Note, this will increase i_size. */
345 if (prev_page_end_size
> i_size_read(page
->mapping
->host
)) {
346 rc
= ecryptfs_truncate(file
->f_path
.dentry
,
349 printk(KERN_ERR
"%s: Error on attempt to "
350 "truncate to (higher) offset [%lld];"
351 " rc = [%d]\n", __func__
,
352 prev_page_end_size
, rc
);
357 /* Writing to a new page, and creating a small hole from start
358 * of page? Zero it out. */
359 if ((i_size_read(mapping
->host
) == prev_page_end_size
)
361 zero_user(page
, 0, PAGE_SIZE
);
372 * ecryptfs_write_inode_size_to_header
374 * Writes the lower file size to the first 8 bytes of the header.
376 * Returns zero on success; non-zero on error.
378 static int ecryptfs_write_inode_size_to_header(struct inode
*ecryptfs_inode
)
380 char *file_size_virt
;
383 file_size_virt
= kmalloc(sizeof(u64
), GFP_KERNEL
);
384 if (!file_size_virt
) {
388 put_unaligned_be64(i_size_read(ecryptfs_inode
), file_size_virt
);
389 rc
= ecryptfs_write_lower(ecryptfs_inode
, file_size_virt
, 0,
391 kfree(file_size_virt
);
393 printk(KERN_ERR
"%s: Error writing file size to header; "
394 "rc = [%d]\n", __func__
, rc
);
401 struct kmem_cache
*ecryptfs_xattr_cache
;
403 static int ecryptfs_write_inode_size_to_xattr(struct inode
*ecryptfs_inode
)
407 struct dentry
*lower_dentry
=
408 ecryptfs_inode_to_private(ecryptfs_inode
)->lower_file
->f_path
.dentry
;
409 struct inode
*lower_inode
= d_inode(lower_dentry
);
412 if (!(lower_inode
->i_opflags
& IOP_XATTR
)) {
414 "No support for setting xattr in lower filesystem\n");
418 xattr_virt
= kmem_cache_alloc(ecryptfs_xattr_cache
, GFP_KERNEL
);
423 inode_lock(lower_inode
);
424 size
= __vfs_getxattr(lower_dentry
, lower_inode
, ECRYPTFS_XATTR_NAME
,
425 xattr_virt
, PAGE_SIZE
);
428 put_unaligned_be64(i_size_read(ecryptfs_inode
), xattr_virt
);
429 rc
= __vfs_setxattr(lower_dentry
, lower_inode
, ECRYPTFS_XATTR_NAME
,
430 xattr_virt
, size
, 0);
431 inode_unlock(lower_inode
);
433 printk(KERN_ERR
"Error whilst attempting to write inode size "
434 "to lower file xattr; rc = [%d]\n", rc
);
435 kmem_cache_free(ecryptfs_xattr_cache
, xattr_virt
);
440 int ecryptfs_write_inode_size_to_metadata(struct inode
*ecryptfs_inode
)
442 struct ecryptfs_crypt_stat
*crypt_stat
;
444 crypt_stat
= &ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
;
445 BUG_ON(!(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
));
446 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
)
447 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode
);
449 return ecryptfs_write_inode_size_to_header(ecryptfs_inode
);
454 * @file: The eCryptfs file object
455 * @mapping: The eCryptfs object
456 * @pos: The file position
457 * @len: The length of the data (unused)
458 * @copied: The amount of data copied
459 * @page: The eCryptfs page
460 * @fsdata: The fsdata (unused)
462 static int ecryptfs_write_end(struct file
*file
,
463 struct address_space
*mapping
,
464 loff_t pos
, unsigned len
, unsigned copied
,
465 struct page
*page
, void *fsdata
)
467 pgoff_t index
= pos
>> PAGE_SHIFT
;
468 unsigned from
= pos
& (PAGE_SIZE
- 1);
469 unsigned to
= from
+ copied
;
470 struct inode
*ecryptfs_inode
= mapping
->host
;
471 struct ecryptfs_crypt_stat
*crypt_stat
=
472 &ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
;
475 ecryptfs_printk(KERN_DEBUG
, "Calling fill_zeros_to_end_of_page"
476 "(page w/ index = [0x%.16lx], to = [%d])\n", index
, to
);
477 if (!(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)) {
478 rc
= ecryptfs_write_lower_page_segment(ecryptfs_inode
, page
, 0,
482 fsstack_copy_inode_size(ecryptfs_inode
,
483 ecryptfs_inode_to_lower(ecryptfs_inode
));
487 if (!PageUptodate(page
)) {
488 if (copied
< PAGE_SIZE
) {
492 SetPageUptodate(page
);
494 /* Fills in zeros if 'to' goes beyond inode size */
495 rc
= fill_zeros_to_end_of_page(page
, to
);
497 ecryptfs_printk(KERN_WARNING
, "Error attempting to fill "
498 "zeros in page with index = [0x%.16lx]\n", index
);
501 rc
= ecryptfs_encrypt_page(page
);
503 ecryptfs_printk(KERN_WARNING
, "Error encrypting page (upper "
504 "index [0x%.16lx])\n", index
);
507 if (pos
+ copied
> i_size_read(ecryptfs_inode
)) {
508 i_size_write(ecryptfs_inode
, pos
+ copied
);
509 ecryptfs_printk(KERN_DEBUG
, "Expanded file size to "
511 (unsigned long long)i_size_read(ecryptfs_inode
));
513 rc
= ecryptfs_write_inode_size_to_metadata(ecryptfs_inode
);
515 printk(KERN_ERR
"Error writing inode size to metadata; "
525 static sector_t
ecryptfs_bmap(struct address_space
*mapping
, sector_t block
)
527 struct inode
*lower_inode
= ecryptfs_inode_to_lower(mapping
->host
);
528 int ret
= bmap(lower_inode
, &block
);
535 const struct address_space_operations ecryptfs_aops
= {
536 .writepage
= ecryptfs_writepage
,
537 .readpage
= ecryptfs_readpage
,
538 .write_begin
= ecryptfs_write_begin
,
539 .write_end
= ecryptfs_write_end
,
540 .bmap
= ecryptfs_bmap
,