mmap.c 14.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
4
5
6
7
8
9
/**
 * eCryptfs: Linux filesystem encryption layer
 * This is where eCryptfs coordinates the symmetric encryption and
 * decryption of the file data as it passes between the lower
 * encrypted file and the upper decrypted file.
 *
 * Copyright (C) 1997-2003 Erez Zadok
 * Copyright (C) 2001-2003 Stony Brook University
10
 * Copyright (C) 2004-2007 International Business Machines Corp.
11
12
13
14
15
16
17
18
19
 *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
 */

#include <linux/pagemap.h>
#include <linux/writeback.h>
#include <linux/page-flags.h>
#include <linux/mount.h>
#include <linux/file.h>
#include <linux/scatterlist.h>
20
#include <linux/slab.h>
21
#include <linux/xattr.h>
22
#include <asm/unaligned.h>
23
24
25
#include "ecryptfs_kernel.h"

/**
26
 * ecryptfs_get_locked_page
27
28
29
 *
 * Get one page from cache or lower f/s, return error otherwise.
 *
30
 * Returns locked and up-to-date page (if ok), with increased
31
32
 * refcnt.
 */
33
struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
34
{
35
	struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
36
37
38
	if (!IS_ERR(page))
		lock_page(page);
	return page;
39
40
41
42
43
44
45
}

/**
 * ecryptfs_writepage
 * @page: Page that is locked before this call is made
 *
 * Returns zero on success; non-zero otherwise
46
47
48
49
 *
 * This is where we encrypt the data and pass the encrypted data to
 * the lower filesystem.  In OpenPGP-compatible mode, we operate on
 * entire underlying packets.
50
51
52
53
54
 */
static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
{
	int rc;

55
	rc = ecryptfs_encrypt_page(page);
56
57
	if (rc) {
		ecryptfs_printk(KERN_WARNING, "Error encrypting "
58
				"page (upper index [0x%.16lx])\n", page->index);
59
60
61
62
63
		ClearPageUptodate(page);
		goto out;
	}
	SetPageUptodate(page);
out:
64
	unlock_page(page);
65
66
67
	return rc;
}

68
69
70
71
72
73
74
75
76
77
78
79
80
static void strip_xattr_flag(char *page_virt,
			     struct ecryptfs_crypt_stat *crypt_stat)
{
	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
		size_t written;

		crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
		ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
						&written);
		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
	}
}

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 *   Header Extent:
 *     Octets 0-7:        Unencrypted file size (big-endian)
 *     Octets 8-15:       eCryptfs special marker
 *     Octets 16-19:      Flags
 *      Octet 16:         File format version number (between 0 and 255)
 *      Octets 17-18:     Reserved
 *      Octet 19:         Bit 1 (lsb): Reserved
 *                        Bit 2: Encrypted?
 *                        Bits 3-8: Reserved
 *     Octets 20-23:      Header extent size (big-endian)
 *     Octets 24-25:      Number of header extents at front of file
 *                        (big-endian)
 *     Octet  26:         Begin RFC 2440 authentication token packet set
 */
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * ecryptfs_copy_up_encrypted_with_header
 * @page: Sort of a ``virtual'' representation of the encrypted lower
 *        file. The actual lower file does not have the metadata in
 *        the header. This is locked.
 * @crypt_stat: The eCryptfs inode's cryptographic context
 *
 * The ``view'' is the version of the file that userspace winds up
 * seeing, with the header information inserted.
 */
static int
ecryptfs_copy_up_encrypted_with_header(struct page *page,
				       struct ecryptfs_crypt_stat *crypt_stat)
{
	loff_t extent_num_in_page = 0;
112
	loff_t num_extents_per_page = (PAGE_SIZE
113
114
115
116
				       / crypt_stat->extent_size);
	int rc = 0;

	while (extent_num_in_page < num_extents_per_page) {
Michael Halcrow's avatar
Michael Halcrow committed
117
118
		loff_t view_extent_num = ((((loff_t)page->index)
					   * 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);
122

123
		if (view_extent_num < num_header_extents_at_front) {
124
125
126
			/* This is a header extent */
			char *page_virt;

127
			page_virt = kmap_atomic(page);
128
			memset(page_virt, 0, PAGE_SIZE);
129
130
			/* TODO: Support more than one header extent */
			if (view_extent_num == 0) {
131
132
				size_t written;

133
134
				rc = ecryptfs_read_xattr_region(
					page_virt, page->mapping->host);
135
				strip_xattr_flag(page_virt + 16, crypt_stat);
136
137
138
				ecryptfs_write_header_metadata(page_virt + 20,
							       crypt_stat,
							       &written);
139
			}
140
			kunmap_atomic(page_virt);
141
142
143
			flush_dcache_page(page);
			if (rc) {
				printk(KERN_ERR "%s: Error reading xattr "
144
				       "region; rc = [%d]\n", __func__, rc);
145
146
147
148
149
				goto out;
			}
		} else {
			/* This is an encrypted data extent */
			loff_t lower_offset =
150
				((view_extent_num * crypt_stat->extent_size)
151
				 - crypt_stat->metadata_size);
152
153

			rc = ecryptfs_read_lower_page_segment(
154
155
				page, (lower_offset >> PAGE_SHIFT),
				(lower_offset & ~PAGE_MASK),
156
157
158
159
				crypt_stat->extent_size, page->mapping->host);
			if (rc) {
				printk(KERN_ERR "%s: Error attempting to read "
				       "extent at offset [%lld] in the lower "
160
				       "file; rc = [%d]\n", __func__,
161
162
163
164
165
166
167
168
169
170
				       lower_offset, rc);
				goto out;
			}
		}
		extent_num_in_page++;
	}
out:
	return rc;
}

171
172
/**
 * ecryptfs_readpage
173
174
 * @file: An eCryptfs file
 * @page: Page from eCryptfs inode mapping into which to stick the read data
175
176
177
178
179
180
181
 *
 * Read in a page, decrypting if necessary.
 *
 * Returns zero on success; non-zero on error.
 */
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;
184
185
	int rc = 0;

186
	if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
187
		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
188
						      PAGE_SIZE,
189
						      page->mapping->host);
190
191
	} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
192
193
194
195
196
197
198
			rc = ecryptfs_copy_up_encrypted_with_header(page,
								    crypt_stat);
			if (rc) {
				printk(KERN_ERR "%s: Error attempting to copy "
				       "the encrypted content from the lower "
				       "file whilst inserting the metadata "
				       "from the xattr into the header; rc = "
199
				       "[%d]\n", __func__, rc);
200
				goto out;
201
			}
202

203
		} else {
204
			rc = ecryptfs_read_lower_page_segment(
205
				page, page->index, 0, PAGE_SIZE,
206
				page->mapping->host);
207
208
209
210
211
212
			if (rc) {
				printk(KERN_ERR "Error reading page; rc = "
				       "[%d]\n", rc);
				goto out;
			}
		}
213
	} else {
214
		rc = ecryptfs_decrypt_page(page);
215
216
217
218
219
220
221
		if (rc) {
			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
					"rc = [%d]\n", rc);
			goto out;
		}
	}
out:
222
223
224
225
	if (rc)
		ClearPageUptodate(page);
	else
		SetPageUptodate(page);
226
	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
227
228
229
230
231
			page->index);
	unlock_page(page);
	return rc;
}

232
233
234
/**
 * Called with lower inode mutex held.
 */
235
236
237
238
239
static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
{
	struct inode *inode = page->mapping->host;
	int end_byte_in_page;

240
	if ((i_size_read(inode) / PAGE_SIZE) != page->index)
241
		goto out;
242
	end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
243
244
	if (to > end_byte_in_page)
		end_byte_in_page = to;
245
	zero_user_segment(page, end_byte_in_page, PAGE_SIZE);
246
out:
247
	return 0;
248
249
}

250
/**
251
 * ecryptfs_write_begin
252
 * @file: The eCryptfs file
253
254
255
256
257
258
 * @mapping: The eCryptfs object
 * @pos: The file offset at which to start writing
 * @len: Length of the write
 * @flags: Various flags
 * @pagep: Pointer to return the page
 * @fsdata: Pointer to return fs data (unused)
259
260
261
262
263
 *
 * This function must zero any hole we create
 *
 * Returns zero on success; non-zero otherwise
 */
264
265
266
267
static int ecryptfs_write_begin(struct file *file,
			struct address_space *mapping,
			loff_t pos, unsigned len, unsigned flags,
			struct page **pagep, void **fsdata)
268
{
269
	pgoff_t index = pos >> PAGE_SHIFT;
270
	struct page *page;
271
	loff_t prev_page_end_size;
272
	int rc = 0;
273

274
	page = grab_cache_page_write_begin(mapping, index, flags);
275
276
277
278
	if (!page)
		return -ENOMEM;
	*pagep = page;

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;
283

284
		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
285
			rc = ecryptfs_read_lower_page_segment(
286
				page, index, 0, PAGE_SIZE, mapping->host);
287
			if (rc) {
Masanari Iida's avatar
Masanari Iida committed
288
				printk(KERN_ERR "%s: Error attempting to read "
289
				       "lower page segment; rc = [%d]\n",
290
				       __func__, rc);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
				ClearPageUptodate(page);
				goto out;
			} else
				SetPageUptodate(page);
		} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
			if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
				rc = ecryptfs_copy_up_encrypted_with_header(
					page, crypt_stat);
				if (rc) {
					printk(KERN_ERR "%s: Error attempting "
					       "to copy the encrypted content "
					       "from the lower file whilst "
					       "inserting the metadata from "
					       "the xattr into the header; rc "
305
					       "= [%d]\n", __func__, rc);
306
307
308
309
310
311
					ClearPageUptodate(page);
					goto out;
				}
				SetPageUptodate(page);
			} else {
				rc = ecryptfs_read_lower_page_segment(
312
					page, index, 0, PAGE_SIZE,
313
					mapping->host);
314
315
316
				if (rc) {
					printk(KERN_ERR "%s: Error reading "
					       "page; rc = [%d]\n",
317
					       __func__, rc);
318
319
320
321
322
323
					ClearPageUptodate(page);
					goto out;
				}
				SetPageUptodate(page);
			}
		} else {
324
325
			if (prev_page_end_size
			    >= i_size_read(page->mapping->host)) {
326
				zero_user(page, 0, PAGE_SIZE);
327
				SetPageUptodate(page);
328
			} else if (len < PAGE_SIZE) {
329
330
331
332
333
334
335
336
337
				rc = ecryptfs_decrypt_page(page);
				if (rc) {
					printk(KERN_ERR "%s: Error decrypting "
					       "page at index [%ld]; "
					       "rc = [%d]\n",
					       __func__, page->index, rc);
					ClearPageUptodate(page);
					goto out;
				}
338
				SetPageUptodate(page);
339
340
			}
		}
341
	}
342
343
	/* If creating a page or more of holes, zero them out via truncate.
	 * Note, this will increase i_size. */
344
	if (index != 0) {
345
		if (prev_page_end_size > i_size_read(page->mapping->host)) {
346
			rc = ecryptfs_truncate(file->f_path.dentry,
347
					       prev_page_end_size);
348
			if (rc) {
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);
353
354
				goto out;
			}
355
		}
356
	}
357
358
	/* Writing to a new page, and creating a small hole from start
	 * of page?  Zero it out. */
359
360
	if ((i_size_read(mapping->host) == prev_page_end_size)
	    && (pos != 0))
361
		zero_user(page, 0, PAGE_SIZE);
362
out:
363
364
	if (unlikely(rc)) {
		unlock_page(page);
365
		put_page(page);
366
367
		*pagep = NULL;
	}
368
369
370
371
372
373
374
375
376
377
	return rc;
}

/**
 * ecryptfs_write_inode_size_to_header
 *
 * Writes the lower file size to the first 8 bytes of the header.
 *
 * Returns zero on success; non-zero on error.
 */
378
static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
379
{
380
381
	char *file_size_virt;
	int rc;
382

383
384
385
	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
	if (!file_size_virt) {
		rc = -ENOMEM;
386
387
		goto out;
	}
388
	put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
389
390
391
	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
				  sizeof(u64));
	kfree(file_size_virt);
392
	if (rc < 0)
393
		printk(KERN_ERR "%s: Error writing file size to header; "
394
		       "rc = [%d]\n", __func__, rc);
395
396
	else
		rc = 0;
397
398
399
400
out:
	return rc;
}

401
402
403
struct kmem_cache *ecryptfs_xattr_cache;

static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
404
405
406
{
	ssize_t size;
	void *xattr_virt;
407
	struct dentry *lower_dentry =
Al Viro's avatar
Al Viro committed
408
		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
409
	struct inode *lower_inode = d_inode(lower_dentry);
410
411
	int rc;

412
	if (!(lower_inode->i_opflags & IOP_XATTR)) {
413
414
415
416
417
		printk(KERN_WARNING
		       "No support for setting xattr in lower filesystem\n");
		rc = -ENOSYS;
		goto out;
	}
418
419
420
421
422
	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
	if (!xattr_virt) {
		rc = -ENOMEM;
		goto out;
	}
Al Viro's avatar
Al Viro committed
423
	inode_lock(lower_inode);
424
425
	size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
			      xattr_virt, PAGE_SIZE);
426
427
	if (size < 0)
		size = 8;
428
	put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
429
430
	rc = __vfs_setxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
			    xattr_virt, size, 0);
Al Viro's avatar
Al Viro committed
431
	inode_unlock(lower_inode);
432
433
434
435
436
437
438
439
	if (rc)
		printk(KERN_ERR "Error whilst attempting to write inode size "
		       "to lower file xattr; rc = [%d]\n", rc);
	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
out:
	return rc;
}

440
int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
441
442
443
{
	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);
448
	else
449
		return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
450
451
}

452
/**
453
 * ecryptfs_write_end
454
 * @file: The eCryptfs file object
455
456
457
458
 * @mapping: The eCryptfs object
 * @pos: The file position
 * @len: The length of the data (unused)
 * @copied: The amount of data copied
459
 * @page: The eCryptfs page
460
 * @fsdata: The fsdata (unused)
461
 */
462
463
464
465
static int ecryptfs_write_end(struct file *file,
			struct address_space *mapping,
			loff_t pos, unsigned len, unsigned copied,
			struct page *page, void *fsdata)
466
{
467
468
	pgoff_t index = pos >> PAGE_SHIFT;
	unsigned from = pos & (PAGE_SIZE - 1);
469
470
	unsigned to = from + copied;
	struct inode *ecryptfs_inode = mapping->host;
471
	struct ecryptfs_crypt_stat *crypt_stat =
472
		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
473
474
475
	int rc;

	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
476
			"(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
477
478
479
480
481
482
483
484
485
486
	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
		rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
						       to);
		if (!rc) {
			rc = copied;
			fsstack_copy_inode_size(ecryptfs_inode,
				ecryptfs_inode_to_lower(ecryptfs_inode));
		}
		goto out;
	}
487
	if (!PageUptodate(page)) {
488
		if (copied < PAGE_SIZE) {
489
490
491
492
493
			rc = 0;
			goto out;
		}
		SetPageUptodate(page);
	}
494
	/* Fills in zeros if 'to' goes beyond inode size */
495
496
497
	rc = fill_zeros_to_end_of_page(page, to);
	if (rc) {
		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
498
			"zeros in page with index = [0x%.16lx]\n", index);
499
500
		goto out;
	}
501
502
503
504
505
506
	rc = ecryptfs_encrypt_page(page);
	if (rc) {
		ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
				"index [0x%.16lx])\n", index);
		goto out;
	}
507
508
	if (pos + copied > i_size_read(ecryptfs_inode)) {
		i_size_write(ecryptfs_inode, pos + copied);
509
		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
510
511
			"[0x%.16llx]\n",
			(unsigned long long)i_size_read(ecryptfs_inode));
512
	}
513
514
515
516
517
518
	rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
	if (rc)
		printk(KERN_ERR "Error writing inode size to metadata; "
		       "rc = [%d]\n", rc);
	else
		rc = copied;
519
out:
520
	unlock_page(page);
521
	put_page(page);
522
523
524
525
526
	return rc;
}

static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
{
527
528
529
530
531
532
	struct inode *lower_inode = ecryptfs_inode_to_lower(mapping->host);
	int ret = bmap(lower_inode, &block);

	if (ret)
		return 0;
	return block;
533
534
}

535
const struct address_space_operations ecryptfs_aops = {
536
537
	.writepage = ecryptfs_writepage,
	.readpage = ecryptfs_readpage,
538
539
	.write_begin = ecryptfs_write_begin,
	.write_end = ecryptfs_write_end,
540
541
	.bmap = ecryptfs_bmap,
};