1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun * eCryptfs: Linux filesystem encryption layer
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2007 International Business Machines Corp.
6*4882a593Smuzhiyun * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/fs.h>
10*4882a593Smuzhiyun #include <linux/pagemap.h>
11*4882a593Smuzhiyun #include <linux/sched/signal.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include "ecryptfs_kernel.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun * ecryptfs_write_lower
17*4882a593Smuzhiyun * @ecryptfs_inode: The eCryptfs inode
18*4882a593Smuzhiyun * @data: Data to write
19*4882a593Smuzhiyun * @offset: Byte offset in the lower file to which to write the data
20*4882a593Smuzhiyun * @size: Number of bytes from @data to write at @offset in the lower
21*4882a593Smuzhiyun * file
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Write data to the lower file.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Returns bytes written on success; less than zero on error
26*4882a593Smuzhiyun */
ecryptfs_write_lower(struct inode * ecryptfs_inode,char * data,loff_t offset,size_t size)27*4882a593Smuzhiyun int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
28*4882a593Smuzhiyun loff_t offset, size_t size)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun struct file *lower_file;
31*4882a593Smuzhiyun ssize_t rc;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
34*4882a593Smuzhiyun if (!lower_file)
35*4882a593Smuzhiyun return -EIO;
36*4882a593Smuzhiyun rc = kernel_write(lower_file, data, size, &offset);
37*4882a593Smuzhiyun mark_inode_dirty_sync(ecryptfs_inode);
38*4882a593Smuzhiyun return rc;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun * ecryptfs_write_lower_page_segment
43*4882a593Smuzhiyun * @ecryptfs_inode: The eCryptfs inode
44*4882a593Smuzhiyun * @page_for_lower: The page containing the data to be written to the
45*4882a593Smuzhiyun * lower file
46*4882a593Smuzhiyun * @offset_in_page: The offset in the @page_for_lower from which to
47*4882a593Smuzhiyun * start writing the data
48*4882a593Smuzhiyun * @size: The amount of data from @page_for_lower to write to the
49*4882a593Smuzhiyun * lower file
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * Determines the byte offset in the file for the given page and
52*4882a593Smuzhiyun * offset within the page, maps the page, and makes the call to write
53*4882a593Smuzhiyun * the contents of @page_for_lower to the lower inode.
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * Returns zero on success; non-zero otherwise
56*4882a593Smuzhiyun */
ecryptfs_write_lower_page_segment(struct inode * ecryptfs_inode,struct page * page_for_lower,size_t offset_in_page,size_t size)57*4882a593Smuzhiyun int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
58*4882a593Smuzhiyun struct page *page_for_lower,
59*4882a593Smuzhiyun size_t offset_in_page, size_t size)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun char *virt;
62*4882a593Smuzhiyun loff_t offset;
63*4882a593Smuzhiyun int rc;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun offset = ((((loff_t)page_for_lower->index) << PAGE_SHIFT)
66*4882a593Smuzhiyun + offset_in_page);
67*4882a593Smuzhiyun virt = kmap(page_for_lower);
68*4882a593Smuzhiyun rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
69*4882a593Smuzhiyun if (rc > 0)
70*4882a593Smuzhiyun rc = 0;
71*4882a593Smuzhiyun kunmap(page_for_lower);
72*4882a593Smuzhiyun return rc;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /**
76*4882a593Smuzhiyun * ecryptfs_write
77*4882a593Smuzhiyun * @ecryptfs_inode: The eCryptfs file into which to write
78*4882a593Smuzhiyun * @data: Virtual address where data to write is located
79*4882a593Smuzhiyun * @offset: Offset in the eCryptfs file at which to begin writing the
80*4882a593Smuzhiyun * data from @data
81*4882a593Smuzhiyun * @size: The number of bytes to write from @data
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * Write an arbitrary amount of data to an arbitrary location in the
84*4882a593Smuzhiyun * eCryptfs inode page cache. This is done on a page-by-page, and then
85*4882a593Smuzhiyun * by an extent-by-extent, basis; individual extents are encrypted and
86*4882a593Smuzhiyun * written to the lower page cache (via VFS writes). This function
87*4882a593Smuzhiyun * takes care of all the address translation to locations in the lower
88*4882a593Smuzhiyun * filesystem; it also handles truncate events, writing out zeros
89*4882a593Smuzhiyun * where necessary.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * Returns zero on success; non-zero otherwise
92*4882a593Smuzhiyun */
ecryptfs_write(struct inode * ecryptfs_inode,char * data,loff_t offset,size_t size)93*4882a593Smuzhiyun int ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
94*4882a593Smuzhiyun size_t size)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun struct page *ecryptfs_page;
97*4882a593Smuzhiyun struct ecryptfs_crypt_stat *crypt_stat;
98*4882a593Smuzhiyun char *ecryptfs_page_virt;
99*4882a593Smuzhiyun loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
100*4882a593Smuzhiyun loff_t data_offset = 0;
101*4882a593Smuzhiyun loff_t pos;
102*4882a593Smuzhiyun int rc = 0;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * if we are writing beyond current size, then start pos
107*4882a593Smuzhiyun * at the current size - we'll fill in zeros from there.
108*4882a593Smuzhiyun */
109*4882a593Smuzhiyun if (offset > ecryptfs_file_size)
110*4882a593Smuzhiyun pos = ecryptfs_file_size;
111*4882a593Smuzhiyun else
112*4882a593Smuzhiyun pos = offset;
113*4882a593Smuzhiyun while (pos < (offset + size)) {
114*4882a593Smuzhiyun pgoff_t ecryptfs_page_idx = (pos >> PAGE_SHIFT);
115*4882a593Smuzhiyun size_t start_offset_in_page = (pos & ~PAGE_MASK);
116*4882a593Smuzhiyun size_t num_bytes = (PAGE_SIZE - start_offset_in_page);
117*4882a593Smuzhiyun loff_t total_remaining_bytes = ((offset + size) - pos);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (fatal_signal_pending(current)) {
120*4882a593Smuzhiyun rc = -EINTR;
121*4882a593Smuzhiyun break;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (num_bytes > total_remaining_bytes)
125*4882a593Smuzhiyun num_bytes = total_remaining_bytes;
126*4882a593Smuzhiyun if (pos < offset) {
127*4882a593Smuzhiyun /* remaining zeros to write, up to destination offset */
128*4882a593Smuzhiyun loff_t total_remaining_zeros = (offset - pos);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (num_bytes > total_remaining_zeros)
131*4882a593Smuzhiyun num_bytes = total_remaining_zeros;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
134*4882a593Smuzhiyun ecryptfs_page_idx);
135*4882a593Smuzhiyun if (IS_ERR(ecryptfs_page)) {
136*4882a593Smuzhiyun rc = PTR_ERR(ecryptfs_page);
137*4882a593Smuzhiyun printk(KERN_ERR "%s: Error getting page at "
138*4882a593Smuzhiyun "index [%ld] from eCryptfs inode "
139*4882a593Smuzhiyun "mapping; rc = [%d]\n", __func__,
140*4882a593Smuzhiyun ecryptfs_page_idx, rc);
141*4882a593Smuzhiyun goto out;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun ecryptfs_page_virt = kmap_atomic(ecryptfs_page);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * pos: where we're now writing, offset: where the request was
147*4882a593Smuzhiyun * If current pos is before request, we are filling zeros
148*4882a593Smuzhiyun * If we are at or beyond request, we are writing the *data*
149*4882a593Smuzhiyun * If we're in a fresh page beyond eof, zero it in either case
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun if (pos < offset || !start_offset_in_page) {
152*4882a593Smuzhiyun /* We are extending past the previous end of the file.
153*4882a593Smuzhiyun * Fill in zero values to the end of the page */
154*4882a593Smuzhiyun memset(((char *)ecryptfs_page_virt
155*4882a593Smuzhiyun + start_offset_in_page), 0,
156*4882a593Smuzhiyun PAGE_SIZE - start_offset_in_page);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* pos >= offset, we are now writing the data request */
160*4882a593Smuzhiyun if (pos >= offset) {
161*4882a593Smuzhiyun memcpy(((char *)ecryptfs_page_virt
162*4882a593Smuzhiyun + start_offset_in_page),
163*4882a593Smuzhiyun (data + data_offset), num_bytes);
164*4882a593Smuzhiyun data_offset += num_bytes;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun kunmap_atomic(ecryptfs_page_virt);
167*4882a593Smuzhiyun flush_dcache_page(ecryptfs_page);
168*4882a593Smuzhiyun SetPageUptodate(ecryptfs_page);
169*4882a593Smuzhiyun unlock_page(ecryptfs_page);
170*4882a593Smuzhiyun if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
171*4882a593Smuzhiyun rc = ecryptfs_encrypt_page(ecryptfs_page);
172*4882a593Smuzhiyun else
173*4882a593Smuzhiyun rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
174*4882a593Smuzhiyun ecryptfs_page,
175*4882a593Smuzhiyun start_offset_in_page,
176*4882a593Smuzhiyun data_offset);
177*4882a593Smuzhiyun put_page(ecryptfs_page);
178*4882a593Smuzhiyun if (rc) {
179*4882a593Smuzhiyun printk(KERN_ERR "%s: Error encrypting "
180*4882a593Smuzhiyun "page; rc = [%d]\n", __func__, rc);
181*4882a593Smuzhiyun goto out;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun pos += num_bytes;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun if (pos > ecryptfs_file_size) {
186*4882a593Smuzhiyun i_size_write(ecryptfs_inode, pos);
187*4882a593Smuzhiyun if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
188*4882a593Smuzhiyun int rc2;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun rc2 = ecryptfs_write_inode_size_to_metadata(
191*4882a593Smuzhiyun ecryptfs_inode);
192*4882a593Smuzhiyun if (rc2) {
193*4882a593Smuzhiyun printk(KERN_ERR "Problem with "
194*4882a593Smuzhiyun "ecryptfs_write_inode_size_to_metadata; "
195*4882a593Smuzhiyun "rc = [%d]\n", rc2);
196*4882a593Smuzhiyun if (!rc)
197*4882a593Smuzhiyun rc = rc2;
198*4882a593Smuzhiyun goto out;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun out:
203*4882a593Smuzhiyun return rc;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /**
207*4882a593Smuzhiyun * ecryptfs_read_lower
208*4882a593Smuzhiyun * @data: The read data is stored here by this function
209*4882a593Smuzhiyun * @offset: Byte offset in the lower file from which to read the data
210*4882a593Smuzhiyun * @size: Number of bytes to read from @offset of the lower file and
211*4882a593Smuzhiyun * store into @data
212*4882a593Smuzhiyun * @ecryptfs_inode: The eCryptfs inode
213*4882a593Smuzhiyun *
214*4882a593Smuzhiyun * Read @size bytes of data at byte offset @offset from the lower
215*4882a593Smuzhiyun * inode into memory location @data.
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * Returns bytes read on success; 0 on EOF; less than zero on error
218*4882a593Smuzhiyun */
ecryptfs_read_lower(char * data,loff_t offset,size_t size,struct inode * ecryptfs_inode)219*4882a593Smuzhiyun int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
220*4882a593Smuzhiyun struct inode *ecryptfs_inode)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun struct file *lower_file;
223*4882a593Smuzhiyun lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
224*4882a593Smuzhiyun if (!lower_file)
225*4882a593Smuzhiyun return -EIO;
226*4882a593Smuzhiyun return kernel_read(lower_file, data, size, &offset);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /**
230*4882a593Smuzhiyun * ecryptfs_read_lower_page_segment
231*4882a593Smuzhiyun * @page_for_ecryptfs: The page into which data for eCryptfs will be
232*4882a593Smuzhiyun * written
233*4882a593Smuzhiyun * @offset_in_page: Offset in @page_for_ecryptfs from which to start
234*4882a593Smuzhiyun * writing
235*4882a593Smuzhiyun * @size: The number of bytes to write into @page_for_ecryptfs
236*4882a593Smuzhiyun * @ecryptfs_inode: The eCryptfs inode
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * Determines the byte offset in the file for the given page and
239*4882a593Smuzhiyun * offset within the page, maps the page, and makes the call to read
240*4882a593Smuzhiyun * the contents of @page_for_ecryptfs from the lower inode.
241*4882a593Smuzhiyun *
242*4882a593Smuzhiyun * Returns zero on success; non-zero otherwise
243*4882a593Smuzhiyun */
ecryptfs_read_lower_page_segment(struct page * page_for_ecryptfs,pgoff_t page_index,size_t offset_in_page,size_t size,struct inode * ecryptfs_inode)244*4882a593Smuzhiyun int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
245*4882a593Smuzhiyun pgoff_t page_index,
246*4882a593Smuzhiyun size_t offset_in_page, size_t size,
247*4882a593Smuzhiyun struct inode *ecryptfs_inode)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun char *virt;
250*4882a593Smuzhiyun loff_t offset;
251*4882a593Smuzhiyun int rc;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun offset = ((((loff_t)page_index) << PAGE_SHIFT) + offset_in_page);
254*4882a593Smuzhiyun virt = kmap(page_for_ecryptfs);
255*4882a593Smuzhiyun rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
256*4882a593Smuzhiyun if (rc > 0)
257*4882a593Smuzhiyun rc = 0;
258*4882a593Smuzhiyun kunmap(page_for_ecryptfs);
259*4882a593Smuzhiyun flush_dcache_page(page_for_ecryptfs);
260*4882a593Smuzhiyun return rc;
261*4882a593Smuzhiyun }
262