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) 1997-2004 Erez Zadok
0006  * Copyright (C) 2001-2004 Stony Brook University
0007  * Copyright (C) 2004-2007 International Business Machines Corp.
0008  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
0009  *          Michael C. Thompson <mcthomps@us.ibm.com>
0010  */
0011 
0012 #include <linux/file.h>
0013 #include <linux/poll.h>
0014 #include <linux/slab.h>
0015 #include <linux/mount.h>
0016 #include <linux/pagemap.h>
0017 #include <linux/security.h>
0018 #include <linux/compat.h>
0019 #include <linux/fs_stack.h>
0020 #include "ecryptfs_kernel.h"
0021 
0022 /*
0023  * ecryptfs_read_update_atime
0024  *
0025  * generic_file_read updates the atime of upper layer inode.  But, it
0026  * doesn't give us a chance to update the atime of the lower layer
0027  * inode.  This function is a wrapper to generic_file_read.  It
0028  * updates the atime of the lower level inode if generic_file_read
0029  * returns without any errors. This is to be used only for file reads.
0030  * The function to be used for directory reads is ecryptfs_read.
0031  */
0032 static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
0033                 struct iov_iter *to)
0034 {
0035     ssize_t rc;
0036     struct path *path;
0037     struct file *file = iocb->ki_filp;
0038 
0039     rc = generic_file_read_iter(iocb, to);
0040     if (rc >= 0) {
0041         path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
0042         touch_atime(path);
0043     }
0044     return rc;
0045 }
0046 
0047 struct ecryptfs_getdents_callback {
0048     struct dir_context ctx;
0049     struct dir_context *caller;
0050     struct super_block *sb;
0051     int filldir_called;
0052     int entries_written;
0053 };
0054 
0055 /* Inspired by generic filldir in fs/readdir.c */
0056 static int
0057 ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
0058          int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
0059 {
0060     struct ecryptfs_getdents_callback *buf =
0061         container_of(ctx, struct ecryptfs_getdents_callback, ctx);
0062     size_t name_size;
0063     char *name;
0064     int rc;
0065 
0066     buf->filldir_called++;
0067     rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
0068                           buf->sb, lower_name,
0069                           lower_namelen);
0070     if (rc) {
0071         if (rc != -EINVAL) {
0072             ecryptfs_printk(KERN_DEBUG,
0073                     "%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
0074                     __func__, lower_name, rc);
0075             return rc;
0076         }
0077 
0078         /* Mask -EINVAL errors as these are most likely due a plaintext
0079          * filename present in the lower filesystem despite filename
0080          * encryption being enabled. One unavoidable example would be
0081          * the "lost+found" dentry in the root directory of an Ext4
0082          * filesystem.
0083          */
0084         return 0;
0085     }
0086 
0087     buf->caller->pos = buf->ctx.pos;
0088     rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
0089     kfree(name);
0090     if (!rc)
0091         buf->entries_written++;
0092 
0093     return rc;
0094 }
0095 
0096 /**
0097  * ecryptfs_readdir
0098  * @file: The eCryptfs directory file
0099  * @ctx: The actor to feed the entries to
0100  */
0101 static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
0102 {
0103     int rc;
0104     struct file *lower_file;
0105     struct inode *inode = file_inode(file);
0106     struct ecryptfs_getdents_callback buf = {
0107         .ctx.actor = ecryptfs_filldir,
0108         .caller = ctx,
0109         .sb = inode->i_sb,
0110     };
0111     lower_file = ecryptfs_file_to_lower(file);
0112     rc = iterate_dir(lower_file, &buf.ctx);
0113     ctx->pos = buf.ctx.pos;
0114     if (rc < 0)
0115         goto out;
0116     if (buf.filldir_called && !buf.entries_written)
0117         goto out;
0118     if (rc >= 0)
0119         fsstack_copy_attr_atime(inode,
0120                     file_inode(lower_file));
0121 out:
0122     return rc;
0123 }
0124 
0125 struct kmem_cache *ecryptfs_file_info_cache;
0126 
0127 static int read_or_initialize_metadata(struct dentry *dentry)
0128 {
0129     struct inode *inode = d_inode(dentry);
0130     struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
0131     struct ecryptfs_crypt_stat *crypt_stat;
0132     int rc;
0133 
0134     crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
0135     mount_crypt_stat = &ecryptfs_superblock_to_private(
0136                         inode->i_sb)->mount_crypt_stat;
0137     mutex_lock(&crypt_stat->cs_mutex);
0138 
0139     if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
0140         crypt_stat->flags & ECRYPTFS_KEY_VALID) {
0141         rc = 0;
0142         goto out;
0143     }
0144 
0145     rc = ecryptfs_read_metadata(dentry);
0146     if (!rc)
0147         goto out;
0148 
0149     if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
0150         crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
0151                        | ECRYPTFS_ENCRYPTED);
0152         rc = 0;
0153         goto out;
0154     }
0155 
0156     if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
0157         !i_size_read(ecryptfs_inode_to_lower(inode))) {
0158         rc = ecryptfs_initialize_file(dentry, inode);
0159         if (!rc)
0160             goto out;
0161     }
0162 
0163     rc = -EIO;
0164 out:
0165     mutex_unlock(&crypt_stat->cs_mutex);
0166     return rc;
0167 }
0168 
0169 static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
0170 {
0171     struct file *lower_file = ecryptfs_file_to_lower(file);
0172     /*
0173      * Don't allow mmap on top of file systems that don't support it
0174      * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
0175      * allows recursive mounting, this will need to be extended.
0176      */
0177     if (!lower_file->f_op->mmap)
0178         return -ENODEV;
0179     return generic_file_mmap(file, vma);
0180 }
0181 
0182 /**
0183  * ecryptfs_open
0184  * @inode: inode specifying file to open
0185  * @file: Structure to return filled in
0186  *
0187  * Opens the file specified by inode.
0188  *
0189  * Returns zero on success; non-zero otherwise
0190  */
0191 static int ecryptfs_open(struct inode *inode, struct file *file)
0192 {
0193     int rc = 0;
0194     struct ecryptfs_crypt_stat *crypt_stat = NULL;
0195     struct dentry *ecryptfs_dentry = file->f_path.dentry;
0196     /* Private value of ecryptfs_dentry allocated in
0197      * ecryptfs_lookup() */
0198     struct ecryptfs_file_info *file_info;
0199 
0200     /* Released in ecryptfs_release or end of function if failure */
0201     file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
0202     ecryptfs_set_file_private(file, file_info);
0203     if (!file_info) {
0204         ecryptfs_printk(KERN_ERR,
0205                 "Error attempting to allocate memory\n");
0206         rc = -ENOMEM;
0207         goto out;
0208     }
0209     crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
0210     mutex_lock(&crypt_stat->cs_mutex);
0211     if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
0212         ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
0213         /* Policy code enabled in future release */
0214         crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
0215                       | ECRYPTFS_ENCRYPTED);
0216     }
0217     mutex_unlock(&crypt_stat->cs_mutex);
0218     rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
0219     if (rc) {
0220         printk(KERN_ERR "%s: Error attempting to initialize "
0221             "the lower file for the dentry with name "
0222             "[%pd]; rc = [%d]\n", __func__,
0223             ecryptfs_dentry, rc);
0224         goto out_free;
0225     }
0226     if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
0227         == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
0228         rc = -EPERM;
0229         printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
0230                "file must hence be opened RO\n", __func__);
0231         goto out_put;
0232     }
0233     ecryptfs_set_file_lower(
0234         file, ecryptfs_inode_to_private(inode)->lower_file);
0235     rc = read_or_initialize_metadata(ecryptfs_dentry);
0236     if (rc)
0237         goto out_put;
0238     ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
0239             "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
0240             (unsigned long long)i_size_read(inode));
0241     goto out;
0242 out_put:
0243     ecryptfs_put_lower_file(inode);
0244 out_free:
0245     kmem_cache_free(ecryptfs_file_info_cache,
0246             ecryptfs_file_to_private(file));
0247 out:
0248     return rc;
0249 }
0250 
0251 /**
0252  * ecryptfs_dir_open
0253  * @inode: inode specifying file to open
0254  * @file: Structure to return filled in
0255  *
0256  * Opens the file specified by inode.
0257  *
0258  * Returns zero on success; non-zero otherwise
0259  */
0260 static int ecryptfs_dir_open(struct inode *inode, struct file *file)
0261 {
0262     struct dentry *ecryptfs_dentry = file->f_path.dentry;
0263     /* Private value of ecryptfs_dentry allocated in
0264      * ecryptfs_lookup() */
0265     struct ecryptfs_file_info *file_info;
0266     struct file *lower_file;
0267 
0268     /* Released in ecryptfs_release or end of function if failure */
0269     file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
0270     ecryptfs_set_file_private(file, file_info);
0271     if (unlikely(!file_info)) {
0272         ecryptfs_printk(KERN_ERR,
0273                 "Error attempting to allocate memory\n");
0274         return -ENOMEM;
0275     }
0276     lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
0277                  file->f_flags, current_cred());
0278     if (IS_ERR(lower_file)) {
0279         printk(KERN_ERR "%s: Error attempting to initialize "
0280             "the lower file for the dentry with name "
0281             "[%pd]; rc = [%ld]\n", __func__,
0282             ecryptfs_dentry, PTR_ERR(lower_file));
0283         kmem_cache_free(ecryptfs_file_info_cache, file_info);
0284         return PTR_ERR(lower_file);
0285     }
0286     ecryptfs_set_file_lower(file, lower_file);
0287     return 0;
0288 }
0289 
0290 static int ecryptfs_flush(struct file *file, fl_owner_t td)
0291 {
0292     struct file *lower_file = ecryptfs_file_to_lower(file);
0293 
0294     if (lower_file->f_op->flush) {
0295         filemap_write_and_wait(file->f_mapping);
0296         return lower_file->f_op->flush(lower_file, td);
0297     }
0298 
0299     return 0;
0300 }
0301 
0302 static int ecryptfs_release(struct inode *inode, struct file *file)
0303 {
0304     ecryptfs_put_lower_file(inode);
0305     kmem_cache_free(ecryptfs_file_info_cache,
0306             ecryptfs_file_to_private(file));
0307     return 0;
0308 }
0309 
0310 static int ecryptfs_dir_release(struct inode *inode, struct file *file)
0311 {
0312     fput(ecryptfs_file_to_lower(file));
0313     kmem_cache_free(ecryptfs_file_info_cache,
0314             ecryptfs_file_to_private(file));
0315     return 0;
0316 }
0317 
0318 static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
0319 {
0320     return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
0321 }
0322 
0323 static int
0324 ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
0325 {
0326     int rc;
0327 
0328     rc = file_write_and_wait(file);
0329     if (rc)
0330         return rc;
0331 
0332     return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
0333 }
0334 
0335 static int ecryptfs_fasync(int fd, struct file *file, int flag)
0336 {
0337     int rc = 0;
0338     struct file *lower_file = NULL;
0339 
0340     lower_file = ecryptfs_file_to_lower(file);
0341     if (lower_file->f_op->fasync)
0342         rc = lower_file->f_op->fasync(fd, lower_file, flag);
0343     return rc;
0344 }
0345 
0346 static long
0347 ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0348 {
0349     struct file *lower_file = ecryptfs_file_to_lower(file);
0350     long rc = -ENOTTY;
0351 
0352     if (!lower_file->f_op->unlocked_ioctl)
0353         return rc;
0354 
0355     switch (cmd) {
0356     case FITRIM:
0357     case FS_IOC_GETFLAGS:
0358     case FS_IOC_SETFLAGS:
0359     case FS_IOC_GETVERSION:
0360     case FS_IOC_SETVERSION:
0361         rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
0362         fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
0363 
0364         return rc;
0365     default:
0366         return rc;
0367     }
0368 }
0369 
0370 #ifdef CONFIG_COMPAT
0371 static long
0372 ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0373 {
0374     struct file *lower_file = ecryptfs_file_to_lower(file);
0375     long rc = -ENOIOCTLCMD;
0376 
0377     if (!lower_file->f_op->compat_ioctl)
0378         return rc;
0379 
0380     switch (cmd) {
0381     case FITRIM:
0382     case FS_IOC32_GETFLAGS:
0383     case FS_IOC32_SETFLAGS:
0384     case FS_IOC32_GETVERSION:
0385     case FS_IOC32_SETVERSION:
0386         rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
0387         fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
0388 
0389         return rc;
0390     default:
0391         return rc;
0392     }
0393 }
0394 #endif
0395 
0396 const struct file_operations ecryptfs_dir_fops = {
0397     .iterate_shared = ecryptfs_readdir,
0398     .read = generic_read_dir,
0399     .unlocked_ioctl = ecryptfs_unlocked_ioctl,
0400 #ifdef CONFIG_COMPAT
0401     .compat_ioctl = ecryptfs_compat_ioctl,
0402 #endif
0403     .open = ecryptfs_dir_open,
0404     .release = ecryptfs_dir_release,
0405     .fsync = ecryptfs_fsync,
0406     .llseek = ecryptfs_dir_llseek,
0407 };
0408 
0409 const struct file_operations ecryptfs_main_fops = {
0410     .llseek = generic_file_llseek,
0411     .read_iter = ecryptfs_read_update_atime,
0412     .write_iter = generic_file_write_iter,
0413     .unlocked_ioctl = ecryptfs_unlocked_ioctl,
0414 #ifdef CONFIG_COMPAT
0415     .compat_ioctl = ecryptfs_compat_ioctl,
0416 #endif
0417     .mmap = ecryptfs_mmap,
0418     .open = ecryptfs_open,
0419     .flush = ecryptfs_flush,
0420     .release = ecryptfs_release,
0421     .fsync = ecryptfs_fsync,
0422     .fasync = ecryptfs_fasync,
0423     .splice_read = generic_file_splice_read,
0424 };