18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * eCryptfs: Linux filesystem encryption layer
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2007 International Business Machines Corp.
68c2ecf20Sopenharmony_ci *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/fs.h>
108c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
118c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "ecryptfs_kernel.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/**
168c2ecf20Sopenharmony_ci * ecryptfs_write_lower
178c2ecf20Sopenharmony_ci * @ecryptfs_inode: The eCryptfs inode
188c2ecf20Sopenharmony_ci * @data: Data to write
198c2ecf20Sopenharmony_ci * @offset: Byte offset in the lower file to which to write the data
208c2ecf20Sopenharmony_ci * @size: Number of bytes from @data to write at @offset in the lower
218c2ecf20Sopenharmony_ci *        file
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Write data to the lower file.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Returns bytes written on success; less than zero on error
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ciint ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
288c2ecf20Sopenharmony_ci			 loff_t offset, size_t size)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct file *lower_file;
318c2ecf20Sopenharmony_ci	ssize_t rc;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
348c2ecf20Sopenharmony_ci	if (!lower_file)
358c2ecf20Sopenharmony_ci		return -EIO;
368c2ecf20Sopenharmony_ci	rc = kernel_write(lower_file, data, size, &offset);
378c2ecf20Sopenharmony_ci	mark_inode_dirty_sync(ecryptfs_inode);
388c2ecf20Sopenharmony_ci	return rc;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/**
428c2ecf20Sopenharmony_ci * ecryptfs_write_lower_page_segment
438c2ecf20Sopenharmony_ci * @ecryptfs_inode: The eCryptfs inode
448c2ecf20Sopenharmony_ci * @page_for_lower: The page containing the data to be written to the
458c2ecf20Sopenharmony_ci *                  lower file
468c2ecf20Sopenharmony_ci * @offset_in_page: The offset in the @page_for_lower from which to
478c2ecf20Sopenharmony_ci *                  start writing the data
488c2ecf20Sopenharmony_ci * @size: The amount of data from @page_for_lower to write to the
498c2ecf20Sopenharmony_ci *        lower file
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * Determines the byte offset in the file for the given page and
528c2ecf20Sopenharmony_ci * offset within the page, maps the page, and makes the call to write
538c2ecf20Sopenharmony_ci * the contents of @page_for_lower to the lower inode.
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * Returns zero on success; non-zero otherwise
568c2ecf20Sopenharmony_ci */
578c2ecf20Sopenharmony_ciint ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
588c2ecf20Sopenharmony_ci				      struct page *page_for_lower,
598c2ecf20Sopenharmony_ci				      size_t offset_in_page, size_t size)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	char *virt;
628c2ecf20Sopenharmony_ci	loff_t offset;
638c2ecf20Sopenharmony_ci	int rc;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	offset = ((((loff_t)page_for_lower->index) << PAGE_SHIFT)
668c2ecf20Sopenharmony_ci		  + offset_in_page);
678c2ecf20Sopenharmony_ci	virt = kmap(page_for_lower);
688c2ecf20Sopenharmony_ci	rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
698c2ecf20Sopenharmony_ci	if (rc > 0)
708c2ecf20Sopenharmony_ci		rc = 0;
718c2ecf20Sopenharmony_ci	kunmap(page_for_lower);
728c2ecf20Sopenharmony_ci	return rc;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/**
768c2ecf20Sopenharmony_ci * ecryptfs_write
778c2ecf20Sopenharmony_ci * @ecryptfs_inode: The eCryptfs file into which to write
788c2ecf20Sopenharmony_ci * @data: Virtual address where data to write is located
798c2ecf20Sopenharmony_ci * @offset: Offset in the eCryptfs file at which to begin writing the
808c2ecf20Sopenharmony_ci *          data from @data
818c2ecf20Sopenharmony_ci * @size: The number of bytes to write from @data
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * Write an arbitrary amount of data to an arbitrary location in the
848c2ecf20Sopenharmony_ci * eCryptfs inode page cache. This is done on a page-by-page, and then
858c2ecf20Sopenharmony_ci * by an extent-by-extent, basis; individual extents are encrypted and
868c2ecf20Sopenharmony_ci * written to the lower page cache (via VFS writes). This function
878c2ecf20Sopenharmony_ci * takes care of all the address translation to locations in the lower
888c2ecf20Sopenharmony_ci * filesystem; it also handles truncate events, writing out zeros
898c2ecf20Sopenharmony_ci * where necessary.
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * Returns zero on success; non-zero otherwise
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_ciint ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
948c2ecf20Sopenharmony_ci		   size_t size)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct page *ecryptfs_page;
978c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat;
988c2ecf20Sopenharmony_ci	char *ecryptfs_page_virt;
998c2ecf20Sopenharmony_ci	loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
1008c2ecf20Sopenharmony_ci	loff_t data_offset = 0;
1018c2ecf20Sopenharmony_ci	loff_t pos;
1028c2ecf20Sopenharmony_ci	int rc = 0;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1058c2ecf20Sopenharmony_ci	/*
1068c2ecf20Sopenharmony_ci	 * if we are writing beyond current size, then start pos
1078c2ecf20Sopenharmony_ci	 * at the current size - we'll fill in zeros from there.
1088c2ecf20Sopenharmony_ci	 */
1098c2ecf20Sopenharmony_ci	if (offset > ecryptfs_file_size)
1108c2ecf20Sopenharmony_ci		pos = ecryptfs_file_size;
1118c2ecf20Sopenharmony_ci	else
1128c2ecf20Sopenharmony_ci		pos = offset;
1138c2ecf20Sopenharmony_ci	while (pos < (offset + size)) {
1148c2ecf20Sopenharmony_ci		pgoff_t ecryptfs_page_idx = (pos >> PAGE_SHIFT);
1158c2ecf20Sopenharmony_ci		size_t start_offset_in_page = (pos & ~PAGE_MASK);
1168c2ecf20Sopenharmony_ci		size_t num_bytes = (PAGE_SIZE - start_offset_in_page);
1178c2ecf20Sopenharmony_ci		loff_t total_remaining_bytes = ((offset + size) - pos);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		if (fatal_signal_pending(current)) {
1208c2ecf20Sopenharmony_ci			rc = -EINTR;
1218c2ecf20Sopenharmony_ci			break;
1228c2ecf20Sopenharmony_ci		}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci		if (num_bytes > total_remaining_bytes)
1258c2ecf20Sopenharmony_ci			num_bytes = total_remaining_bytes;
1268c2ecf20Sopenharmony_ci		if (pos < offset) {
1278c2ecf20Sopenharmony_ci			/* remaining zeros to write, up to destination offset */
1288c2ecf20Sopenharmony_ci			loff_t total_remaining_zeros = (offset - pos);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci			if (num_bytes > total_remaining_zeros)
1318c2ecf20Sopenharmony_ci				num_bytes = total_remaining_zeros;
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
1348c2ecf20Sopenharmony_ci							 ecryptfs_page_idx);
1358c2ecf20Sopenharmony_ci		if (IS_ERR(ecryptfs_page)) {
1368c2ecf20Sopenharmony_ci			rc = PTR_ERR(ecryptfs_page);
1378c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error getting page at "
1388c2ecf20Sopenharmony_ci			       "index [%ld] from eCryptfs inode "
1398c2ecf20Sopenharmony_ci			       "mapping; rc = [%d]\n", __func__,
1408c2ecf20Sopenharmony_ci			       ecryptfs_page_idx, rc);
1418c2ecf20Sopenharmony_ci			goto out;
1428c2ecf20Sopenharmony_ci		}
1438c2ecf20Sopenharmony_ci		ecryptfs_page_virt = kmap_atomic(ecryptfs_page);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		/*
1468c2ecf20Sopenharmony_ci		 * pos: where we're now writing, offset: where the request was
1478c2ecf20Sopenharmony_ci		 * If current pos is before request, we are filling zeros
1488c2ecf20Sopenharmony_ci		 * If we are at or beyond request, we are writing the *data*
1498c2ecf20Sopenharmony_ci		 * If we're in a fresh page beyond eof, zero it in either case
1508c2ecf20Sopenharmony_ci		 */
1518c2ecf20Sopenharmony_ci		if (pos < offset || !start_offset_in_page) {
1528c2ecf20Sopenharmony_ci			/* We are extending past the previous end of the file.
1538c2ecf20Sopenharmony_ci			 * Fill in zero values to the end of the page */
1548c2ecf20Sopenharmony_ci			memset(((char *)ecryptfs_page_virt
1558c2ecf20Sopenharmony_ci				+ start_offset_in_page), 0,
1568c2ecf20Sopenharmony_ci				PAGE_SIZE - start_offset_in_page);
1578c2ecf20Sopenharmony_ci		}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		/* pos >= offset, we are now writing the data request */
1608c2ecf20Sopenharmony_ci		if (pos >= offset) {
1618c2ecf20Sopenharmony_ci			memcpy(((char *)ecryptfs_page_virt
1628c2ecf20Sopenharmony_ci				+ start_offset_in_page),
1638c2ecf20Sopenharmony_ci			       (data + data_offset), num_bytes);
1648c2ecf20Sopenharmony_ci			data_offset += num_bytes;
1658c2ecf20Sopenharmony_ci		}
1668c2ecf20Sopenharmony_ci		kunmap_atomic(ecryptfs_page_virt);
1678c2ecf20Sopenharmony_ci		flush_dcache_page(ecryptfs_page);
1688c2ecf20Sopenharmony_ci		SetPageUptodate(ecryptfs_page);
1698c2ecf20Sopenharmony_ci		unlock_page(ecryptfs_page);
1708c2ecf20Sopenharmony_ci		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
1718c2ecf20Sopenharmony_ci			rc = ecryptfs_encrypt_page(ecryptfs_page);
1728c2ecf20Sopenharmony_ci		else
1738c2ecf20Sopenharmony_ci			rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
1748c2ecf20Sopenharmony_ci						ecryptfs_page,
1758c2ecf20Sopenharmony_ci						start_offset_in_page,
1768c2ecf20Sopenharmony_ci						data_offset);
1778c2ecf20Sopenharmony_ci		put_page(ecryptfs_page);
1788c2ecf20Sopenharmony_ci		if (rc) {
1798c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error encrypting "
1808c2ecf20Sopenharmony_ci			       "page; rc = [%d]\n", __func__, rc);
1818c2ecf20Sopenharmony_ci			goto out;
1828c2ecf20Sopenharmony_ci		}
1838c2ecf20Sopenharmony_ci		pos += num_bytes;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci	if (pos > ecryptfs_file_size) {
1868c2ecf20Sopenharmony_ci		i_size_write(ecryptfs_inode, pos);
1878c2ecf20Sopenharmony_ci		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
1888c2ecf20Sopenharmony_ci			int rc2;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci			rc2 = ecryptfs_write_inode_size_to_metadata(
1918c2ecf20Sopenharmony_ci								ecryptfs_inode);
1928c2ecf20Sopenharmony_ci			if (rc2) {
1938c2ecf20Sopenharmony_ci				printk(KERN_ERR	"Problem with "
1948c2ecf20Sopenharmony_ci				       "ecryptfs_write_inode_size_to_metadata; "
1958c2ecf20Sopenharmony_ci				       "rc = [%d]\n", rc2);
1968c2ecf20Sopenharmony_ci				if (!rc)
1978c2ecf20Sopenharmony_ci					rc = rc2;
1988c2ecf20Sopenharmony_ci				goto out;
1998c2ecf20Sopenharmony_ci			}
2008c2ecf20Sopenharmony_ci		}
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ciout:
2038c2ecf20Sopenharmony_ci	return rc;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/**
2078c2ecf20Sopenharmony_ci * ecryptfs_read_lower
2088c2ecf20Sopenharmony_ci * @data: The read data is stored here by this function
2098c2ecf20Sopenharmony_ci * @offset: Byte offset in the lower file from which to read the data
2108c2ecf20Sopenharmony_ci * @size: Number of bytes to read from @offset of the lower file and
2118c2ecf20Sopenharmony_ci *        store into @data
2128c2ecf20Sopenharmony_ci * @ecryptfs_inode: The eCryptfs inode
2138c2ecf20Sopenharmony_ci *
2148c2ecf20Sopenharmony_ci * Read @size bytes of data at byte offset @offset from the lower
2158c2ecf20Sopenharmony_ci * inode into memory location @data.
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * Returns bytes read on success; 0 on EOF; less than zero on error
2188c2ecf20Sopenharmony_ci */
2198c2ecf20Sopenharmony_ciint ecryptfs_read_lower(char *data, loff_t offset, size_t size,
2208c2ecf20Sopenharmony_ci			struct inode *ecryptfs_inode)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	struct file *lower_file;
2238c2ecf20Sopenharmony_ci	lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
2248c2ecf20Sopenharmony_ci	if (!lower_file)
2258c2ecf20Sopenharmony_ci		return -EIO;
2268c2ecf20Sopenharmony_ci	return kernel_read(lower_file, data, size, &offset);
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci/**
2308c2ecf20Sopenharmony_ci * ecryptfs_read_lower_page_segment
2318c2ecf20Sopenharmony_ci * @page_for_ecryptfs: The page into which data for eCryptfs will be
2328c2ecf20Sopenharmony_ci *                     written
2338c2ecf20Sopenharmony_ci * @offset_in_page: Offset in @page_for_ecryptfs from which to start
2348c2ecf20Sopenharmony_ci *                  writing
2358c2ecf20Sopenharmony_ci * @size: The number of bytes to write into @page_for_ecryptfs
2368c2ecf20Sopenharmony_ci * @ecryptfs_inode: The eCryptfs inode
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * Determines the byte offset in the file for the given page and
2398c2ecf20Sopenharmony_ci * offset within the page, maps the page, and makes the call to read
2408c2ecf20Sopenharmony_ci * the contents of @page_for_ecryptfs from the lower inode.
2418c2ecf20Sopenharmony_ci *
2428c2ecf20Sopenharmony_ci * Returns zero on success; non-zero otherwise
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_ciint ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
2458c2ecf20Sopenharmony_ci				     pgoff_t page_index,
2468c2ecf20Sopenharmony_ci				     size_t offset_in_page, size_t size,
2478c2ecf20Sopenharmony_ci				     struct inode *ecryptfs_inode)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	char *virt;
2508c2ecf20Sopenharmony_ci	loff_t offset;
2518c2ecf20Sopenharmony_ci	int rc;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	offset = ((((loff_t)page_index) << PAGE_SHIFT) + offset_in_page);
2548c2ecf20Sopenharmony_ci	virt = kmap(page_for_ecryptfs);
2558c2ecf20Sopenharmony_ci	rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
2568c2ecf20Sopenharmony_ci	if (rc > 0)
2578c2ecf20Sopenharmony_ci		rc = 0;
2588c2ecf20Sopenharmony_ci	kunmap(page_for_ecryptfs);
2598c2ecf20Sopenharmony_ci	flush_dcache_page(page_for_ecryptfs);
2608c2ecf20Sopenharmony_ci	return rc;
2618c2ecf20Sopenharmony_ci}
262