treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156
[linux-block.git] / fs / ecryptfs / mmap.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
237fead6
MH
2/**
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.
7 *
8 * Copyright (C) 1997-2003 Erez Zadok
9 * Copyright (C) 2001-2003 Stony Brook University
dd2a3b7a 10 * Copyright (C) 2004-2007 International Business Machines Corp.
237fead6 11 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
237fead6
MH
12 */
13
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>
237fead6 19#include <linux/scatterlist.h>
5a0e3ad6 20#include <linux/slab.h>
5d6c3191 21#include <linux/xattr.h>
0a688ad7 22#include <asm/unaligned.h>
237fead6
MH
23#include "ecryptfs_kernel.h"
24
237fead6 25/**
16a72c45 26 * ecryptfs_get_locked_page
237fead6
MH
27 *
28 * Get one page from cache or lower f/s, return error otherwise.
29 *
16a72c45 30 * Returns locked and up-to-date page (if ok), with increased
237fead6
MH
31 * refcnt.
32 */
02bd9799 33struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
237fead6 34{
02bd9799 35 struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
16a72c45
MH
36 if (!IS_ERR(page))
37 lock_page(page);
38 return page;
237fead6
MH
39}
40
237fead6
MH
41/**
42 * ecryptfs_writepage
43 * @page: Page that is locked before this call is made
44 *
45 * Returns zero on success; non-zero otherwise
1589cb1a
LW
46 *
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.
237fead6
MH
50 */
51static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
52{
237fead6
MH
53 int rc;
54
0216f7f7 55 rc = ecryptfs_encrypt_page(page);
237fead6
MH
56 if (rc) {
57 ecryptfs_printk(KERN_WARNING, "Error encrypting "
888d57bb 58 "page (upper index [0x%.16lx])\n", page->index);
237fead6
MH
59 ClearPageUptodate(page);
60 goto out;
61 }
62 SetPageUptodate(page);
237fead6 63out:
57db4e8d 64 unlock_page(page);
237fead6
MH
65 return rc;
66}
67
f4e60e6b
TH
68static void strip_xattr_flag(char *page_virt,
69 struct ecryptfs_crypt_stat *crypt_stat)
70{
71 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
72 size_t written;
73
74 crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
75 ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
76 &written);
77 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
78 }
79}
80
e77a56dd
MH
81/**
82 * Header Extent:
83 * Octets 0-7: Unencrypted file size (big-endian)
84 * Octets 8-15: eCryptfs special marker
85 * Octets 16-19: Flags
86 * Octet 16: File format version number (between 0 and 255)
87 * Octets 17-18: Reserved
88 * Octet 19: Bit 1 (lsb): Reserved
89 * Bit 2: Encrypted?
90 * Bits 3-8: Reserved
91 * Octets 20-23: Header extent size (big-endian)
92 * Octets 24-25: Number of header extents at front of file
93 * (big-endian)
94 * Octet 26: Begin RFC 2440 authentication token packet set
95 */
237fead6 96
bf12be1c
MH
97/**
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
103 *
104 * The ``view'' is the version of the file that userspace winds up
105 * seeing, with the header information inserted.
106 */
107static int
108ecryptfs_copy_up_encrypted_with_header(struct page *page,
109 struct ecryptfs_crypt_stat *crypt_stat)
110{
111 loff_t extent_num_in_page = 0;
09cbfeaf 112 loff_t num_extents_per_page = (PAGE_SIZE
bf12be1c
MH
113 / crypt_stat->extent_size);
114 int rc = 0;
115
116 while (extent_num_in_page < num_extents_per_page) {
d6a13c17
MH
117 loff_t view_extent_num = ((((loff_t)page->index)
118 * num_extents_per_page)
bf12be1c 119 + extent_num_in_page);
cc11beff 120 size_t num_header_extents_at_front =
fa3ef1cb 121 (crypt_stat->metadata_size / crypt_stat->extent_size);
bf12be1c 122
cc11beff 123 if (view_extent_num < num_header_extents_at_front) {
bf12be1c
MH
124 /* This is a header extent */
125 char *page_virt;
126
465c9343 127 page_virt = kmap_atomic(page);
09cbfeaf 128 memset(page_virt, 0, PAGE_SIZE);
bf12be1c
MH
129 /* TODO: Support more than one header extent */
130 if (view_extent_num == 0) {
157f1071
TH
131 size_t written;
132
bf12be1c
MH
133 rc = ecryptfs_read_xattr_region(
134 page_virt, page->mapping->host);
f4e60e6b 135 strip_xattr_flag(page_virt + 16, crypt_stat);
157f1071
TH
136 ecryptfs_write_header_metadata(page_virt + 20,
137 crypt_stat,
138 &written);
bf12be1c 139 }
465c9343 140 kunmap_atomic(page_virt);
bf12be1c
MH
141 flush_dcache_page(page);
142 if (rc) {
bf12be1c 143 printk(KERN_ERR "%s: Error reading xattr "
18d1dbf1 144 "region; rc = [%d]\n", __func__, rc);
bf12be1c
MH
145 goto out;
146 }
bf12be1c
MH
147 } else {
148 /* This is an encrypted data extent */
149 loff_t lower_offset =
cc11beff 150 ((view_extent_num * crypt_stat->extent_size)
fa3ef1cb 151 - crypt_stat->metadata_size);
bf12be1c
MH
152
153 rc = ecryptfs_read_lower_page_segment(
09cbfeaf
KS
154 page, (lower_offset >> PAGE_SHIFT),
155 (lower_offset & ~PAGE_MASK),
bf12be1c
MH
156 crypt_stat->extent_size, page->mapping->host);
157 if (rc) {
158 printk(KERN_ERR "%s: Error attempting to read "
159 "extent at offset [%lld] in the lower "
18d1dbf1 160 "file; rc = [%d]\n", __func__,
bf12be1c
MH
161 lower_offset, rc);
162 goto out;
163 }
164 }
165 extent_num_in_page++;
166 }
167out:
168 return rc;
169}
170
237fead6
MH
171/**
172 * ecryptfs_readpage
bf12be1c
MH
173 * @file: An eCryptfs file
174 * @page: Page from eCryptfs inode mapping into which to stick the read data
237fead6
MH
175 *
176 * Read in a page, decrypting if necessary.
177 *
178 * Returns zero on success; non-zero on error.
179 */
180static int ecryptfs_readpage(struct file *file, struct page *page)
181{
bf12be1c 182 struct ecryptfs_crypt_stat *crypt_stat =
bef5bc24 183 &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
237fead6 184 int rc = 0;
237fead6 185
fed8859b 186 if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
bf12be1c 187 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
09cbfeaf 188 PAGE_SIZE,
bf12be1c 189 page->mapping->host);
e77a56dd
MH
190 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
191 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
bf12be1c
MH
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 = "
18d1dbf1 199 "[%d]\n", __func__, rc);
bf12be1c 200 goto out;
e77a56dd 201 }
bf12be1c 202
e77a56dd 203 } else {
bf12be1c 204 rc = ecryptfs_read_lower_page_segment(
09cbfeaf 205 page, page->index, 0, PAGE_SIZE,
bf12be1c 206 page->mapping->host);
e77a56dd
MH
207 if (rc) {
208 printk(KERN_ERR "Error reading page; rc = "
209 "[%d]\n", rc);
210 goto out;
211 }
212 }
237fead6 213 } else {
0216f7f7 214 rc = ecryptfs_decrypt_page(page);
237fead6 215 if (rc) {
237fead6
MH
216 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
217 "rc = [%d]\n", rc);
218 goto out;
219 }
220 }
237fead6 221out:
16a72c45
MH
222 if (rc)
223 ClearPageUptodate(page);
224 else
225 SetPageUptodate(page);
888d57bb 226 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
237fead6
MH
227 page->index);
228 unlock_page(page);
229 return rc;
230}
231
dd2a3b7a
MH
232/**
233 * Called with lower inode mutex held.
234 */
237fead6
MH
235static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
236{
237 struct inode *inode = page->mapping->host;
238 int end_byte_in_page;
237fead6 239
09cbfeaf 240 if ((i_size_read(inode) / PAGE_SIZE) != page->index)
9d8b8ce5 241 goto out;
09cbfeaf 242 end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
9d8b8ce5
MH
243 if (to > end_byte_in_page)
244 end_byte_in_page = to;
09cbfeaf 245 zero_user_segment(page, end_byte_in_page, PAGE_SIZE);
237fead6 246out:
9d8b8ce5 247 return 0;
237fead6
MH
248}
249
e4465fda 250/**
807b7ebe 251 * ecryptfs_write_begin
e4465fda 252 * @file: The eCryptfs file
807b7ebe
BP
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)
e4465fda
MH
259 *
260 * This function must zero any hole we create
261 *
262 * Returns zero on success; non-zero otherwise
263 */
807b7ebe
BP
264static 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)
237fead6 268{
09cbfeaf 269 pgoff_t index = pos >> PAGE_SHIFT;
807b7ebe 270 struct page *page;
7a3f595c 271 loff_t prev_page_end_size;
e4465fda 272 int rc = 0;
237fead6 273
54566b2c 274 page = grab_cache_page_write_begin(mapping, index, flags);
807b7ebe
BP
275 if (!page)
276 return -ENOMEM;
277 *pagep = page;
278
09cbfeaf 279 prev_page_end_size = ((loff_t)index << PAGE_SHIFT);
16a72c45 280 if (!PageUptodate(page)) {
e4465fda 281 struct ecryptfs_crypt_stat *crypt_stat =
bef5bc24 282 &ecryptfs_inode_to_private(mapping->host)->crypt_stat;
e4465fda 283
fed8859b 284 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
e4465fda 285 rc = ecryptfs_read_lower_page_segment(
09cbfeaf 286 page, index, 0, PAGE_SIZE, mapping->host);
e4465fda 287 if (rc) {
971bd8fa 288 printk(KERN_ERR "%s: Error attempting to read "
e4465fda 289 "lower page segment; rc = [%d]\n",
18d1dbf1 290 __func__, rc);
e4465fda
MH
291 ClearPageUptodate(page);
292 goto out;
293 } else
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(
298 page, crypt_stat);
299 if (rc) {
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 "
18d1dbf1 305 "= [%d]\n", __func__, rc);
e4465fda
MH
306 ClearPageUptodate(page);
307 goto out;
308 }
309 SetPageUptodate(page);
310 } else {
311 rc = ecryptfs_read_lower_page_segment(
09cbfeaf 312 page, index, 0, PAGE_SIZE,
807b7ebe 313 mapping->host);
e4465fda
MH
314 if (rc) {
315 printk(KERN_ERR "%s: Error reading "
316 "page; rc = [%d]\n",
18d1dbf1 317 __func__, rc);
e4465fda
MH
318 ClearPageUptodate(page);
319 goto out;
320 }
321 SetPageUptodate(page);
322 }
323 } else {
24562486
FS
324 if (prev_page_end_size
325 >= i_size_read(page->mapping->host)) {
09cbfeaf 326 zero_user(page, 0, PAGE_SIZE);
e4bc6522 327 SetPageUptodate(page);
09cbfeaf 328 } else if (len < PAGE_SIZE) {
24562486
FS
329 rc = ecryptfs_decrypt_page(page);
330 if (rc) {
331 printk(KERN_ERR "%s: Error decrypting "
332 "page at index [%ld]; "
333 "rc = [%d]\n",
334 __func__, page->index, rc);
335 ClearPageUptodate(page);
336 goto out;
337 }
e4bc6522 338 SetPageUptodate(page);
e4465fda 339 }
e4465fda 340 }
16a72c45 341 }
e4465fda
MH
342 /* If creating a page or more of holes, zero them out via truncate.
343 * Note, this will increase i_size. */
807b7ebe 344 if (index != 0) {
7a3f595c 345 if (prev_page_end_size > i_size_read(page->mapping->host)) {
240e2df5 346 rc = ecryptfs_truncate(file->f_path.dentry,
7a3f595c 347 prev_page_end_size);
240e2df5 348 if (rc) {
e4465fda 349 printk(KERN_ERR "%s: Error on attempt to "
240e2df5 350 "truncate to (higher) offset [%lld];"
18d1dbf1 351 " rc = [%d]\n", __func__,
e4465fda 352 prev_page_end_size, rc);
240e2df5
MH
353 goto out;
354 }
53a2731f 355 }
7a3f595c 356 }
e4465fda
MH
357 /* Writing to a new page, and creating a small hole from start
358 * of page? Zero it out. */
807b7ebe
BP
359 if ((i_size_read(mapping->host) == prev_page_end_size)
360 && (pos != 0))
09cbfeaf 361 zero_user(page, 0, PAGE_SIZE);
237fead6 362out:
50f198ae
TH
363 if (unlikely(rc)) {
364 unlock_page(page);
09cbfeaf 365 put_page(page);
50f198ae
TH
366 *pagep = NULL;
367 }
237fead6
MH
368 return rc;
369}
370
237fead6
MH
371/**
372 * ecryptfs_write_inode_size_to_header
373 *
374 * Writes the lower file size to the first 8 bytes of the header.
375 *
376 * Returns zero on success; non-zero on error.
377 */
0216f7f7 378static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
237fead6 379{
0216f7f7
MH
380 char *file_size_virt;
381 int rc;
237fead6 382
0216f7f7
MH
383 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
384 if (!file_size_virt) {
385 rc = -ENOMEM;
ae73fc09
MH
386 goto out;
387 }
0a688ad7 388 put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
0216f7f7
MH
389 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
390 sizeof(u64));
391 kfree(file_size_virt);
96a7b9c2 392 if (rc < 0)
0216f7f7 393 printk(KERN_ERR "%s: Error writing file size to header; "
18d1dbf1 394 "rc = [%d]\n", __func__, rc);
96a7b9c2
TH
395 else
396 rc = 0;
237fead6
MH
397out:
398 return rc;
399}
400
0216f7f7
MH
401struct kmem_cache *ecryptfs_xattr_cache;
402
403static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
dd2a3b7a
MH
404{
405 ssize_t size;
406 void *xattr_virt;
0216f7f7 407 struct dentry *lower_dentry =
b583043e 408 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
2b0143b5 409 struct inode *lower_inode = d_inode(lower_dentry);
dd2a3b7a
MH
410 int rc;
411
5d6c3191 412 if (!(lower_inode->i_opflags & IOP_XATTR)) {
0216f7f7
MH
413 printk(KERN_WARNING
414 "No support for setting xattr in lower filesystem\n");
415 rc = -ENOSYS;
416 goto out;
417 }
dd2a3b7a
MH
418 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
419 if (!xattr_virt) {
dd2a3b7a
MH
420 rc = -ENOMEM;
421 goto out;
422 }
5955102c 423 inode_lock(lower_inode);
5d6c3191
AG
424 size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
425 xattr_virt, PAGE_SIZE);
dd2a3b7a
MH
426 if (size < 0)
427 size = 8;
0a688ad7 428 put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
5d6c3191
AG
429 rc = __vfs_setxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
430 xattr_virt, size, 0);
5955102c 431 inode_unlock(lower_inode);
dd2a3b7a
MH
432 if (rc)
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);
436out:
437 return rc;
438}
439
0216f7f7 440int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
dd2a3b7a
MH
441{
442 struct ecryptfs_crypt_stat *crypt_stat;
443
0216f7f7 444 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
13a791b4 445 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
dd2a3b7a 446 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
0216f7f7 447 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
dd2a3b7a 448 else
0216f7f7 449 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
dd2a3b7a
MH
450}
451
237fead6 452/**
807b7ebe 453 * ecryptfs_write_end
237fead6 454 * @file: The eCryptfs file object
807b7ebe
BP
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
237fead6 459 * @page: The eCryptfs page
807b7ebe 460 * @fsdata: The fsdata (unused)
237fead6 461 */
807b7ebe
BP
462static 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)
237fead6 466{
09cbfeaf
KS
467 pgoff_t index = pos >> PAGE_SHIFT;
468 unsigned from = pos & (PAGE_SIZE - 1);
807b7ebe
BP
469 unsigned to = from + copied;
470 struct inode *ecryptfs_inode = mapping->host;
bf12be1c 471 struct ecryptfs_crypt_stat *crypt_stat =
bef5bc24 472 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
237fead6
MH
473 int rc;
474
237fead6 475 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
888d57bb 476 "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
13a791b4
TH
477 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
478 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
479 to);
480 if (!rc) {
481 rc = copied;
482 fsstack_copy_inode_size(ecryptfs_inode,
483 ecryptfs_inode_to_lower(ecryptfs_inode));
484 }
485 goto out;
486 }
e4bc6522 487 if (!PageUptodate(page)) {
09cbfeaf 488 if (copied < PAGE_SIZE) {
e4bc6522
LW
489 rc = 0;
490 goto out;
491 }
492 SetPageUptodate(page);
493 }
bf12be1c 494 /* Fills in zeros if 'to' goes beyond inode size */
237fead6
MH
495 rc = fill_zeros_to_end_of_page(page, to);
496 if (rc) {
497 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
888d57bb 498 "zeros in page with index = [0x%.16lx]\n", index);
237fead6
MH
499 goto out;
500 }
821f7494
TH
501 rc = ecryptfs_encrypt_page(page);
502 if (rc) {
503 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
504 "index [0x%.16lx])\n", index);
505 goto out;
506 }
807b7ebe
BP
507 if (pos + copied > i_size_read(ecryptfs_inode)) {
508 i_size_write(ecryptfs_inode, pos + copied);
237fead6 509 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
888d57bb
JP
510 "[0x%.16llx]\n",
511 (unsigned long long)i_size_read(ecryptfs_inode));
237fead6 512 }
821f7494
TH
513 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
514 if (rc)
515 printk(KERN_ERR "Error writing inode size to metadata; "
516 "rc = [%d]\n", rc);
517 else
518 rc = copied;
237fead6 519out:
821f7494 520 unlock_page(page);
09cbfeaf 521 put_page(page);
237fead6
MH
522 return rc;
523}
524
237fead6
MH
525static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
526{
527 int rc = 0;
528 struct inode *inode;
529 struct inode *lower_inode;
530
531 inode = (struct inode *)mapping->host;
532 lower_inode = ecryptfs_inode_to_lower(inode);
533 if (lower_inode->i_mapping->a_ops->bmap)
534 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
535 block);
536 return rc;
537}
538
7f09410b 539const struct address_space_operations ecryptfs_aops = {
237fead6
MH
540 .writepage = ecryptfs_writepage,
541 .readpage = ecryptfs_readpage,
807b7ebe
BP
542 .write_begin = ecryptfs_write_begin,
543 .write_end = ecryptfs_write_end,
237fead6 544 .bmap = ecryptfs_bmap,
237fead6 545};