Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * eCryptfs: Linux filesystem encryption layer
0004  *
0005  * Copyright (C) 2007 International Business Machines Corp.
0006  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
0007  */
0008 
0009 #include <linux/fs.h>
0010 #include <linux/pagemap.h>
0011 #include <linux/sched/signal.h>
0012 
0013 #include "ecryptfs_kernel.h"
0014 
0015 /**
0016  * ecryptfs_write_lower
0017  * @ecryptfs_inode: The eCryptfs inode
0018  * @data: Data to write
0019  * @offset: Byte offset in the lower file to which to write the data
0020  * @size: Number of bytes from @data to write at @offset in the lower
0021  *        file
0022  *
0023  * Write data to the lower file.
0024  *
0025  * Returns bytes written on success; less than zero on error
0026  */
0027 int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
0028              loff_t offset, size_t size)
0029 {
0030     struct file *lower_file;
0031     ssize_t rc;
0032 
0033     lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
0034     if (!lower_file)
0035         return -EIO;
0036     rc = kernel_write(lower_file, data, size, &offset);
0037     mark_inode_dirty_sync(ecryptfs_inode);
0038     return rc;
0039 }
0040 
0041 /**
0042  * ecryptfs_write_lower_page_segment
0043  * @ecryptfs_inode: The eCryptfs inode
0044  * @page_for_lower: The page containing the data to be written to the
0045  *                  lower file
0046  * @offset_in_page: The offset in the @page_for_lower from which to
0047  *                  start writing the data
0048  * @size: The amount of data from @page_for_lower to write to the
0049  *        lower file
0050  *
0051  * Determines the byte offset in the file for the given page and
0052  * offset within the page, maps the page, and makes the call to write
0053  * the contents of @page_for_lower to the lower inode.
0054  *
0055  * Returns zero on success; non-zero otherwise
0056  */
0057 int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
0058                       struct page *page_for_lower,
0059                       size_t offset_in_page, size_t size)
0060 {
0061     char *virt;
0062     loff_t offset;
0063     int rc;
0064 
0065     offset = ((((loff_t)page_for_lower->index) << PAGE_SHIFT)
0066           + offset_in_page);
0067     virt = kmap(page_for_lower);
0068     rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
0069     if (rc > 0)
0070         rc = 0;
0071     kunmap(page_for_lower);
0072     return rc;
0073 }
0074 
0075 /**
0076  * ecryptfs_write
0077  * @ecryptfs_inode: The eCryptfs file into which to write
0078  * @data: Virtual address where data to write is located
0079  * @offset: Offset in the eCryptfs file at which to begin writing the
0080  *          data from @data
0081  * @size: The number of bytes to write from @data
0082  *
0083  * Write an arbitrary amount of data to an arbitrary location in the
0084  * eCryptfs inode page cache. This is done on a page-by-page, and then
0085  * by an extent-by-extent, basis; individual extents are encrypted and
0086  * written to the lower page cache (via VFS writes). This function
0087  * takes care of all the address translation to locations in the lower
0088  * filesystem; it also handles truncate events, writing out zeros
0089  * where necessary.
0090  *
0091  * Returns zero on success; non-zero otherwise
0092  */
0093 int ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
0094            size_t size)
0095 {
0096     struct page *ecryptfs_page;
0097     struct ecryptfs_crypt_stat *crypt_stat;
0098     char *ecryptfs_page_virt;
0099     loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
0100     loff_t data_offset = 0;
0101     loff_t pos;
0102     int rc = 0;
0103 
0104     crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
0105     /*
0106      * if we are writing beyond current size, then start pos
0107      * at the current size - we'll fill in zeros from there.
0108      */
0109     if (offset > ecryptfs_file_size)
0110         pos = ecryptfs_file_size;
0111     else
0112         pos = offset;
0113     while (pos < (offset + size)) {
0114         pgoff_t ecryptfs_page_idx = (pos >> PAGE_SHIFT);
0115         size_t start_offset_in_page = (pos & ~PAGE_MASK);
0116         size_t num_bytes = (PAGE_SIZE - start_offset_in_page);
0117         loff_t total_remaining_bytes = ((offset + size) - pos);
0118 
0119         if (fatal_signal_pending(current)) {
0120             rc = -EINTR;
0121             break;
0122         }
0123 
0124         if (num_bytes > total_remaining_bytes)
0125             num_bytes = total_remaining_bytes;
0126         if (pos < offset) {
0127             /* remaining zeros to write, up to destination offset */
0128             loff_t total_remaining_zeros = (offset - pos);
0129 
0130             if (num_bytes > total_remaining_zeros)
0131                 num_bytes = total_remaining_zeros;
0132         }
0133         ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
0134                              ecryptfs_page_idx);
0135         if (IS_ERR(ecryptfs_page)) {
0136             rc = PTR_ERR(ecryptfs_page);
0137             printk(KERN_ERR "%s: Error getting page at "
0138                    "index [%ld] from eCryptfs inode "
0139                    "mapping; rc = [%d]\n", __func__,
0140                    ecryptfs_page_idx, rc);
0141             goto out;
0142         }
0143         ecryptfs_page_virt = kmap_atomic(ecryptfs_page);
0144 
0145         /*
0146          * pos: where we're now writing, offset: where the request was
0147          * If current pos is before request, we are filling zeros
0148          * If we are at or beyond request, we are writing the *data*
0149          * If we're in a fresh page beyond eof, zero it in either case
0150          */
0151         if (pos < offset || !start_offset_in_page) {
0152             /* We are extending past the previous end of the file.
0153              * Fill in zero values to the end of the page */
0154             memset(((char *)ecryptfs_page_virt
0155                 + start_offset_in_page), 0,
0156                 PAGE_SIZE - start_offset_in_page);
0157         }
0158 
0159         /* pos >= offset, we are now writing the data request */
0160         if (pos >= offset) {
0161             memcpy(((char *)ecryptfs_page_virt
0162                 + start_offset_in_page),
0163                    (data + data_offset), num_bytes);
0164             data_offset += num_bytes;
0165         }
0166         kunmap_atomic(ecryptfs_page_virt);
0167         flush_dcache_page(ecryptfs_page);
0168         SetPageUptodate(ecryptfs_page);
0169         unlock_page(ecryptfs_page);
0170         if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
0171             rc = ecryptfs_encrypt_page(ecryptfs_page);
0172         else
0173             rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
0174                         ecryptfs_page,
0175                         start_offset_in_page,
0176                         data_offset);
0177         put_page(ecryptfs_page);
0178         if (rc) {
0179             printk(KERN_ERR "%s: Error encrypting "
0180                    "page; rc = [%d]\n", __func__, rc);
0181             goto out;
0182         }
0183         pos += num_bytes;
0184     }
0185     if (pos > ecryptfs_file_size) {
0186         i_size_write(ecryptfs_inode, pos);
0187         if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
0188             int rc2;
0189 
0190             rc2 = ecryptfs_write_inode_size_to_metadata(
0191                                 ecryptfs_inode);
0192             if (rc2) {
0193                 printk(KERN_ERR "Problem with "
0194                        "ecryptfs_write_inode_size_to_metadata; "
0195                        "rc = [%d]\n", rc2);
0196                 if (!rc)
0197                     rc = rc2;
0198                 goto out;
0199             }
0200         }
0201     }
0202 out:
0203     return rc;
0204 }
0205 
0206 /**
0207  * ecryptfs_read_lower
0208  * @data: The read data is stored here by this function
0209  * @offset: Byte offset in the lower file from which to read the data
0210  * @size: Number of bytes to read from @offset of the lower file and
0211  *        store into @data
0212  * @ecryptfs_inode: The eCryptfs inode
0213  *
0214  * Read @size bytes of data at byte offset @offset from the lower
0215  * inode into memory location @data.
0216  *
0217  * Returns bytes read on success; 0 on EOF; less than zero on error
0218  */
0219 int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
0220             struct inode *ecryptfs_inode)
0221 {
0222     struct file *lower_file;
0223     lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
0224     if (!lower_file)
0225         return -EIO;
0226     return kernel_read(lower_file, data, size, &offset);
0227 }
0228 
0229 /**
0230  * ecryptfs_read_lower_page_segment
0231  * @page_for_ecryptfs: The page into which data for eCryptfs will be
0232  *                     written
0233  * @page_index: Page index in @page_for_ecryptfs from which to start
0234  *      writing
0235  * @offset_in_page: Offset in @page_for_ecryptfs from which to start
0236  *                  writing
0237  * @size: The number of bytes to write into @page_for_ecryptfs
0238  * @ecryptfs_inode: The eCryptfs inode
0239  *
0240  * Determines the byte offset in the file for the given page and
0241  * offset within the page, maps the page, and makes the call to read
0242  * the contents of @page_for_ecryptfs from the lower inode.
0243  *
0244  * Returns zero on success; non-zero otherwise
0245  */
0246 int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
0247                      pgoff_t page_index,
0248                      size_t offset_in_page, size_t size,
0249                      struct inode *ecryptfs_inode)
0250 {
0251     char *virt;
0252     loff_t offset;
0253     int rc;
0254 
0255     offset = ((((loff_t)page_index) << PAGE_SHIFT) + offset_in_page);
0256     virt = kmap(page_for_ecryptfs);
0257     rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
0258     if (rc > 0)
0259         rc = 0;
0260     kunmap(page_for_ecryptfs);
0261     flush_dcache_page(page_for_ecryptfs);
0262     return rc;
0263 }