blob: 7d85e64ea62f3880311cd32b81ca96b596b89f5a [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lee Jones688a9f72021-03-30 17:44:56 +01002/*
Michael Halcrow237fead2006-10-04 02:16:22 -07003 * 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
Michael Halcrowdd2a3b72007-02-12 00:53:46 -080010 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -070011 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070012 */
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>
Michael Halcrow237fead2006-10-04 02:16:22 -070019#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020021#include <linux/xattr.h>
Harvey Harrison0a688ad2008-07-23 21:30:07 -070022#include <asm/unaligned.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070023#include "ecryptfs_kernel.h"
24
Lee Jones688a9f72021-03-30 17:44:56 +010025/*
Michael Halcrow16a72c42007-10-16 01:28:14 -070026 * ecryptfs_get_locked_page
Michael Halcrow237fead2006-10-04 02:16:22 -070027 *
28 * Get one page from cache or lower f/s, return error otherwise.
29 *
Michael Halcrow16a72c42007-10-16 01:28:14 -070030 * Returns locked and up-to-date page (if ok), with increased
Michael Halcrow237fead2006-10-04 02:16:22 -070031 * refcnt.
32 */
Al Viro02bd9792010-05-21 11:02:14 -040033struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
Michael Halcrow237fead2006-10-04 02:16:22 -070034{
Al Viro02bd9792010-05-21 11:02:14 -040035 struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
Michael Halcrow16a72c42007-10-16 01:28:14 -070036 if (!IS_ERR(page))
37 lock_page(page);
38 return page;
Michael Halcrow237fead2006-10-04 02:16:22 -070039}
40
Michael Halcrow237fead2006-10-04 02:16:22 -070041/**
Michael Halcrow237fead2006-10-04 02:16:22 -070042 * ecryptfs_writepage
43 * @page: Page that is locked before this call is made
Lee Jones688a9f72021-03-30 17:44:56 +010044 * @wbc: Write-back control structure
Michael Halcrow237fead2006-10-04 02:16:22 -070045 *
46 * Returns zero on success; non-zero otherwise
Li Wang1589cb12012-01-25 15:40:31 +080047 *
48 * This is where we encrypt the data and pass the encrypted data to
49 * the lower filesystem. In OpenPGP-compatible mode, we operate on
50 * entire underlying packets.
Michael Halcrow237fead2006-10-04 02:16:22 -070051 */
52static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
53{
Michael Halcrow237fead2006-10-04 02:16:22 -070054 int rc;
55
Michael Halcrow0216f7f2007-10-16 01:28:08 -070056 rc = ecryptfs_encrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -070057 if (rc) {
58 ecryptfs_printk(KERN_WARNING, "Error encrypting "
Joe Perches888d57b2010-11-10 15:46:16 -080059 "page (upper index [0x%.16lx])\n", page->index);
Michael Halcrow237fead2006-10-04 02:16:22 -070060 ClearPageUptodate(page);
61 goto out;
62 }
63 SetPageUptodate(page);
Michael Halcrow237fead2006-10-04 02:16:22 -070064out:
Thieu Le57db4e82011-03-08 16:26:03 -080065 unlock_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -070066 return rc;
67}
68
Tyler Hicksf4e60e62010-02-11 00:02:32 -060069static void strip_xattr_flag(char *page_virt,
70 struct ecryptfs_crypt_stat *crypt_stat)
71{
72 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
73 size_t written;
74
75 crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
76 ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
77 &written);
78 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
79 }
80}
81
Lee Jones688a9f72021-03-30 17:44:56 +010082/*
Michael Halcrowe77a56d2007-02-12 00:53:47 -080083 * Header Extent:
84 * Octets 0-7: Unencrypted file size (big-endian)
85 * Octets 8-15: eCryptfs special marker
86 * Octets 16-19: Flags
87 * Octet 16: File format version number (between 0 and 255)
88 * Octets 17-18: Reserved
89 * Octet 19: Bit 1 (lsb): Reserved
90 * Bit 2: Encrypted?
91 * Bits 3-8: Reserved
92 * Octets 20-23: Header extent size (big-endian)
93 * Octets 24-25: Number of header extents at front of file
94 * (big-endian)
95 * Octet 26: Begin RFC 2440 authentication token packet set
96 */
Michael Halcrow237fead2006-10-04 02:16:22 -070097
98/**
Michael Halcrowbf12be12007-10-16 01:28:11 -070099 * ecryptfs_copy_up_encrypted_with_header
100 * @page: Sort of a ``virtual'' representation of the encrypted lower
101 * file. The actual lower file does not have the metadata in
102 * the header. This is locked.
103 * @crypt_stat: The eCryptfs inode's cryptographic context
104 *
105 * The ``view'' is the version of the file that userspace winds up
106 * seeing, with the header information inserted.
107 */
108static int
109ecryptfs_copy_up_encrypted_with_header(struct page *page,
110 struct ecryptfs_crypt_stat *crypt_stat)
111{
112 loff_t extent_num_in_page = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300113 loff_t num_extents_per_page = (PAGE_SIZE
Michael Halcrowbf12be12007-10-16 01:28:11 -0700114 / crypt_stat->extent_size);
115 int rc = 0;
116
117 while (extent_num_in_page < num_extents_per_page) {
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700118 loff_t view_extent_num = ((((loff_t)page->index)
119 * num_extents_per_page)
Michael Halcrowbf12be12007-10-16 01:28:11 -0700120 + extent_num_in_page);
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800121 size_t num_header_extents_at_front =
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600122 (crypt_stat->metadata_size / crypt_stat->extent_size);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700123
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800124 if (view_extent_num < num_header_extents_at_front) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700125 /* This is a header extent */
126 char *page_virt;
127
Cong Wang465c9342012-02-10 13:39:50 +0800128 page_virt = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300129 memset(page_virt, 0, PAGE_SIZE);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700130 /* TODO: Support more than one header extent */
131 if (view_extent_num == 0) {
Tyler Hicks157f1072010-02-11 07:10:38 -0600132 size_t written;
133
Michael Halcrowbf12be12007-10-16 01:28:11 -0700134 rc = ecryptfs_read_xattr_region(
135 page_virt, page->mapping->host);
Tyler Hicksf4e60e62010-02-11 00:02:32 -0600136 strip_xattr_flag(page_virt + 16, crypt_stat);
Tyler Hicks157f1072010-02-11 07:10:38 -0600137 ecryptfs_write_header_metadata(page_virt + 20,
138 crypt_stat,
139 &written);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700140 }
Cong Wang465c9342012-02-10 13:39:50 +0800141 kunmap_atomic(page_virt);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700142 flush_dcache_page(page);
143 if (rc) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700144 printk(KERN_ERR "%s: Error reading xattr "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700145 "region; rc = [%d]\n", __func__, rc);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700146 goto out;
147 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700148 } else {
149 /* This is an encrypted data extent */
150 loff_t lower_offset =
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800151 ((view_extent_num * crypt_stat->extent_size)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600152 - crypt_stat->metadata_size);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700153
154 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300155 page, (lower_offset >> PAGE_SHIFT),
156 (lower_offset & ~PAGE_MASK),
Michael Halcrowbf12be12007-10-16 01:28:11 -0700157 crypt_stat->extent_size, page->mapping->host);
158 if (rc) {
159 printk(KERN_ERR "%s: Error attempting to read "
160 "extent at offset [%lld] in the lower "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700161 "file; rc = [%d]\n", __func__,
Michael Halcrowbf12be12007-10-16 01:28:11 -0700162 lower_offset, rc);
163 goto out;
164 }
165 }
166 extent_num_in_page++;
167 }
168out:
169 return rc;
170}
171
172/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700173 * ecryptfs_readpage
Michael Halcrowbf12be12007-10-16 01:28:11 -0700174 * @file: An eCryptfs file
175 * @page: Page from eCryptfs inode mapping into which to stick the read data
Michael Halcrow237fead2006-10-04 02:16:22 -0700176 *
177 * Read in a page, decrypting if necessary.
178 *
179 * Returns zero on success; non-zero on error.
180 */
181static int ecryptfs_readpage(struct file *file, struct page *page)
182{
Michael Halcrowbf12be12007-10-16 01:28:11 -0700183 struct ecryptfs_crypt_stat *crypt_stat =
Al Virobef5bc22010-05-21 10:56:12 -0400184 &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700185 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700186
Tyler Hicksfed88592011-02-23 00:54:20 -0600187 if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700188 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300189 PAGE_SIZE,
Michael Halcrowbf12be12007-10-16 01:28:11 -0700190 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800191 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
192 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700193 rc = ecryptfs_copy_up_encrypted_with_header(page,
194 crypt_stat);
195 if (rc) {
196 printk(KERN_ERR "%s: Error attempting to copy "
197 "the encrypted content from the lower "
198 "file whilst inserting the metadata "
199 "from the xattr into the header; rc = "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700200 "[%d]\n", __func__, rc);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700201 goto out;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800202 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700203
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800204 } else {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700205 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300206 page, page->index, 0, PAGE_SIZE,
Michael Halcrowbf12be12007-10-16 01:28:11 -0700207 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800208 if (rc) {
209 printk(KERN_ERR "Error reading page; rc = "
210 "[%d]\n", rc);
211 goto out;
212 }
213 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700214 } else {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700215 rc = ecryptfs_decrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700216 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700217 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
218 "rc = [%d]\n", rc);
219 goto out;
220 }
221 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700222out:
Michael Halcrow16a72c42007-10-16 01:28:14 -0700223 if (rc)
224 ClearPageUptodate(page);
225 else
226 SetPageUptodate(page);
Joe Perches888d57b2010-11-10 15:46:16 -0800227 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700228 page->index);
229 unlock_page(page);
230 return rc;
231}
232
Lee Jones688a9f72021-03-30 17:44:56 +0100233/*
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800234 * Called with lower inode mutex held.
235 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700236static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
237{
238 struct inode *inode = page->mapping->host;
239 int end_byte_in_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700240
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 if ((i_size_read(inode) / PAGE_SIZE) != page->index)
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800242 goto out;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300243 end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800244 if (to > end_byte_in_page)
245 end_byte_in_page = to;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 zero_user_segment(page, end_byte_in_page, PAGE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700247out:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800248 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700249}
250
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800251/**
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700252 * ecryptfs_write_begin
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800253 * @file: The eCryptfs file
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700254 * @mapping: The eCryptfs object
255 * @pos: The file offset at which to start writing
256 * @len: Length of the write
257 * @flags: Various flags
258 * @pagep: Pointer to return the page
259 * @fsdata: Pointer to return fs data (unused)
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800260 *
261 * This function must zero any hole we create
262 *
263 * Returns zero on success; non-zero otherwise
264 */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700265static int ecryptfs_write_begin(struct file *file,
266 struct address_space *mapping,
267 loff_t pos, unsigned len, unsigned flags,
268 struct page **pagep, void **fsdata)
Michael Halcrow53a27312007-05-23 13:58:15 -0700269{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300270 pgoff_t index = pos >> PAGE_SHIFT;
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700271 struct page *page;
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800272 loff_t prev_page_end_size;
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800273 int rc = 0;
Michael Halcrow53a27312007-05-23 13:58:15 -0700274
Nick Piggin54566b22009-01-04 12:00:53 -0800275 page = grab_cache_page_write_begin(mapping, index, flags);
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700276 if (!page)
277 return -ENOMEM;
278 *pagep = page;
279
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300280 prev_page_end_size = ((loff_t)index << PAGE_SHIFT);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700281 if (!PageUptodate(page)) {
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800282 struct ecryptfs_crypt_stat *crypt_stat =
Al Virobef5bc22010-05-21 10:56:12 -0400283 &ecryptfs_inode_to_private(mapping->host)->crypt_stat;
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800284
Tyler Hicksfed88592011-02-23 00:54:20 -0600285 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800286 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300287 page, index, 0, PAGE_SIZE, mapping->host);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800288 if (rc) {
Masanari Iida971bd8f2015-05-20 23:54:02 +0900289 printk(KERN_ERR "%s: Error attempting to read "
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800290 "lower page segment; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700291 __func__, rc);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800292 ClearPageUptodate(page);
293 goto out;
294 } else
295 SetPageUptodate(page);
296 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
297 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
298 rc = ecryptfs_copy_up_encrypted_with_header(
299 page, crypt_stat);
300 if (rc) {
301 printk(KERN_ERR "%s: Error attempting "
302 "to copy the encrypted content "
303 "from the lower file whilst "
304 "inserting the metadata from "
305 "the xattr into the header; rc "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700306 "= [%d]\n", __func__, rc);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800307 ClearPageUptodate(page);
308 goto out;
309 }
310 SetPageUptodate(page);
311 } else {
312 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300313 page, index, 0, PAGE_SIZE,
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700314 mapping->host);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800315 if (rc) {
316 printk(KERN_ERR "%s: Error reading "
317 "page; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700318 __func__, rc);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800319 ClearPageUptodate(page);
320 goto out;
321 }
322 SetPageUptodate(page);
323 }
324 } else {
Frank Swiderski24562482010-11-15 10:43:22 -0800325 if (prev_page_end_size
326 >= i_size_read(page->mapping->host)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 zero_user(page, 0, PAGE_SIZE);
Li Wange4bc6522012-10-30 19:52:40 +0800328 SetPageUptodate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300329 } else if (len < PAGE_SIZE) {
Frank Swiderski24562482010-11-15 10:43:22 -0800330 rc = ecryptfs_decrypt_page(page);
331 if (rc) {
332 printk(KERN_ERR "%s: Error decrypting "
333 "page at index [%ld]; "
334 "rc = [%d]\n",
335 __func__, page->index, rc);
336 ClearPageUptodate(page);
337 goto out;
338 }
Li Wange4bc6522012-10-30 19:52:40 +0800339 SetPageUptodate(page);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800340 }
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800341 }
Michael Halcrow16a72c42007-10-16 01:28:14 -0700342 }
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800343 /* If creating a page or more of holes, zero them out via truncate.
344 * Note, this will increase i_size. */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700345 if (index != 0) {
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800346 if (prev_page_end_size > i_size_read(page->mapping->host)) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700347 rc = ecryptfs_truncate(file->f_path.dentry,
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800348 prev_page_end_size);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700349 if (rc) {
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800350 printk(KERN_ERR "%s: Error on attempt to "
Michael Halcrow240e2df2007-06-27 14:09:44 -0700351 "truncate to (higher) offset [%lld];"
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700352 " rc = [%d]\n", __func__,
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800353 prev_page_end_size, rc);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700354 goto out;
355 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700356 }
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800357 }
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800358 /* Writing to a new page, and creating a small hole from start
359 * of page? Zero it out. */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700360 if ((i_size_read(mapping->host) == prev_page_end_size)
361 && (pos != 0))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300362 zero_user(page, 0, PAGE_SIZE);
Michael Halcrow53a27312007-05-23 13:58:15 -0700363out:
Tyler Hicks50f198a2011-03-09 11:49:13 -0600364 if (unlikely(rc)) {
365 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300366 put_page(page);
Tyler Hicks50f198a2011-03-09 11:49:13 -0600367 *pagep = NULL;
368 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700369 return rc;
370}
371
Lee Jones688a9f72021-03-30 17:44:56 +0100372/*
Michael Halcrow237fead2006-10-04 02:16:22 -0700373 * ecryptfs_write_inode_size_to_header
374 *
375 * Writes the lower file size to the first 8 bytes of the header.
376 *
377 * Returns zero on success; non-zero on error.
378 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700379static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700380{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700381 char *file_size_virt;
382 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700383
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700384 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
385 if (!file_size_virt) {
386 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700387 goto out;
388 }
Harvey Harrison0a688ad2008-07-23 21:30:07 -0700389 put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700390 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
391 sizeof(u64));
392 kfree(file_size_virt);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500393 if (rc < 0)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700394 printk(KERN_ERR "%s: Error writing file size to header; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700395 "rc = [%d]\n", __func__, rc);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500396 else
397 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700398out:
399 return rc;
400}
401
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700402struct kmem_cache *ecryptfs_xattr_cache;
403
404static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800405{
406 ssize_t size;
407 void *xattr_virt;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700408 struct dentry *lower_dentry =
Al Virob5830432014-10-31 01:22:04 -0400409 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000410 struct inode *lower_inode = d_inode(lower_dentry);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800411 int rc;
412
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200413 if (!(lower_inode->i_opflags & IOP_XATTR)) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700414 printk(KERN_WARNING
415 "No support for setting xattr in lower filesystem\n");
416 rc = -ENOSYS;
417 goto out;
418 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800419 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
420 if (!xattr_virt) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800421 rc = -ENOMEM;
422 goto out;
423 }
Al Viro59551022016-01-22 15:40:57 -0500424 inode_lock(lower_inode);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200425 size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
426 xattr_virt, PAGE_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800427 if (size < 0)
428 size = 8;
Harvey Harrison0a688ad2008-07-23 21:30:07 -0700429 put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100430 rc = __vfs_setxattr(&init_user_ns, lower_dentry, lower_inode,
431 ECRYPTFS_XATTR_NAME, xattr_virt, size, 0);
Al Viro59551022016-01-22 15:40:57 -0500432 inode_unlock(lower_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800433 if (rc)
434 printk(KERN_ERR "Error whilst attempting to write inode size "
435 "to lower file xattr; rc = [%d]\n", rc);
436 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
437out:
438 return rc;
439}
440
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700441int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800442{
443 struct ecryptfs_crypt_stat *crypt_stat;
444
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700445 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Tyler Hicks13a791b2009-04-13 15:29:27 -0500446 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800447 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700448 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800449 else
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700450 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800451}
452
Michael Halcrow237fead2006-10-04 02:16:22 -0700453/**
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700454 * ecryptfs_write_end
Michael Halcrow237fead2006-10-04 02:16:22 -0700455 * @file: The eCryptfs file object
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700456 * @mapping: The eCryptfs object
457 * @pos: The file position
458 * @len: The length of the data (unused)
459 * @copied: The amount of data copied
Michael Halcrow237fead2006-10-04 02:16:22 -0700460 * @page: The eCryptfs page
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700461 * @fsdata: The fsdata (unused)
Michael Halcrow237fead2006-10-04 02:16:22 -0700462 */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700463static int ecryptfs_write_end(struct file *file,
464 struct address_space *mapping,
465 loff_t pos, unsigned len, unsigned copied,
466 struct page *page, void *fsdata)
Michael Halcrow237fead2006-10-04 02:16:22 -0700467{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300468 pgoff_t index = pos >> PAGE_SHIFT;
469 unsigned from = pos & (PAGE_SIZE - 1);
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700470 unsigned to = from + copied;
471 struct inode *ecryptfs_inode = mapping->host;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700472 struct ecryptfs_crypt_stat *crypt_stat =
Al Virobef5bc22010-05-21 10:56:12 -0400473 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700474 int rc;
475
Michael Halcrow237fead2006-10-04 02:16:22 -0700476 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
Joe Perches888d57b2010-11-10 15:46:16 -0800477 "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500478 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
479 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
480 to);
481 if (!rc) {
482 rc = copied;
483 fsstack_copy_inode_size(ecryptfs_inode,
484 ecryptfs_inode_to_lower(ecryptfs_inode));
485 }
486 goto out;
487 }
Li Wange4bc6522012-10-30 19:52:40 +0800488 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300489 if (copied < PAGE_SIZE) {
Li Wange4bc6522012-10-30 19:52:40 +0800490 rc = 0;
491 goto out;
492 }
493 SetPageUptodate(page);
494 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700495 /* Fills in zeros if 'to' goes beyond inode size */
Michael Halcrow237fead2006-10-04 02:16:22 -0700496 rc = fill_zeros_to_end_of_page(page, to);
497 if (rc) {
498 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
Joe Perches888d57b2010-11-10 15:46:16 -0800499 "zeros in page with index = [0x%.16lx]\n", index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700500 goto out;
501 }
Tyler Hicks821f7492012-07-03 16:50:57 -0700502 rc = ecryptfs_encrypt_page(page);
503 if (rc) {
504 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
505 "index [0x%.16lx])\n", index);
506 goto out;
507 }
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700508 if (pos + copied > i_size_read(ecryptfs_inode)) {
509 i_size_write(ecryptfs_inode, pos + copied);
Michael Halcrow237fead2006-10-04 02:16:22 -0700510 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
Joe Perches888d57b2010-11-10 15:46:16 -0800511 "[0x%.16llx]\n",
512 (unsigned long long)i_size_read(ecryptfs_inode));
Michael Halcrow237fead2006-10-04 02:16:22 -0700513 }
Tyler Hicks821f7492012-07-03 16:50:57 -0700514 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
515 if (rc)
516 printk(KERN_ERR "Error writing inode size to metadata; "
517 "rc = [%d]\n", rc);
518 else
519 rc = copied;
Michael Halcrow237fead2006-10-04 02:16:22 -0700520out:
Tyler Hicks821f7492012-07-03 16:50:57 -0700521 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300522 put_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700523 return rc;
524}
525
Michael Halcrow237fead2006-10-04 02:16:22 -0700526static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
527{
Carlos Maiolino569d2052020-01-09 14:30:43 +0100528 struct inode *lower_inode = ecryptfs_inode_to_lower(mapping->host);
529 int ret = bmap(lower_inode, &block);
Michael Halcrow237fead2006-10-04 02:16:22 -0700530
Carlos Maiolino569d2052020-01-09 14:30:43 +0100531 if (ret)
532 return 0;
533 return block;
Michael Halcrow237fead2006-10-04 02:16:22 -0700534}
535
Christoph Hellwig0af57372021-06-28 19:36:12 -0700536#include <linux/buffer_head.h>
537
Alexey Dobriyan7f094102009-09-21 17:01:10 -0700538const struct address_space_operations ecryptfs_aops = {
Christoph Hellwig0af57372021-06-28 19:36:12 -0700539 /*
540 * XXX: This is pretty broken for multiple reasons: ecryptfs does not
541 * actually use buffer_heads, and ecryptfs will crash without
542 * CONFIG_BLOCK. But it matches the behavior before the default for
543 * address_space_operations without the ->set_page_dirty method was
544 * cleaned up, so this is the best we can do without maintainer
545 * feedback.
546 */
547#ifdef CONFIG_BLOCK
548 .set_page_dirty = __set_page_dirty_buffers,
549#endif
Michael Halcrow237fead2006-10-04 02:16:22 -0700550 .writepage = ecryptfs_writepage,
551 .readpage = ecryptfs_readpage,
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700552 .write_begin = ecryptfs_write_begin,
553 .write_end = ecryptfs_write_end,
Michael Halcrow237fead2006-10-04 02:16:22 -0700554 .bmap = ecryptfs_bmap,
Michael Halcrow237fead2006-10-04 02:16:22 -0700555};