Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Lee Jones | a62187e | 2021-03-30 17:44:50 +0100 | [diff] [blame] | 2 | /* |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 3 | * eCryptfs: Linux filesystem encryption layer |
| 4 | * |
| 5 | * Copyright (C) 1997-2004 Erez Zadok |
| 6 | * Copyright (C) 2001-2004 Stony Brook University |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 7 | * Copyright (C) 2004-2007 International Business Machines Corp. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 8 | * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com> |
| 9 | * Michael C. Thompson <mcthomps@us.ibm.com> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/file.h> |
| 13 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 15 | #include <linux/mount.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/security.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 18 | #include <linux/compat.h> |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 19 | #include <linux/fs_stack.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 20 | #include "ecryptfs_kernel.h" |
| 21 | |
Lee Jones | a62187e | 2021-03-30 17:44:50 +0100 | [diff] [blame] | 22 | /* |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 23 | * ecryptfs_read_update_atime |
| 24 | * |
| 25 | * generic_file_read updates the atime of upper layer inode. But, it |
| 26 | * doesn't give us a chance to update the atime of the lower layer |
| 27 | * inode. This function is a wrapper to generic_file_read. It |
| 28 | * updates the atime of the lower level inode if generic_file_read |
| 29 | * returns without any errors. This is to be used only for file reads. |
| 30 | * The function to be used for directory reads is ecryptfs_read. |
| 31 | */ |
| 32 | static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb, |
Al Viro | 0279782 | 2014-04-02 14:40:38 -0400 | [diff] [blame] | 33 | struct iov_iter *to) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 34 | { |
Edward Shishkin | 38a708d | 2010-10-30 00:11:50 +0200 | [diff] [blame] | 35 | ssize_t rc; |
Matthew Wilcox | cc18ec3 | 2013-06-15 07:55:59 -0400 | [diff] [blame] | 36 | struct path *path; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 37 | struct file *file = iocb->ki_filp; |
| 38 | |
Al Viro | 0279782 | 2014-04-02 14:40:38 -0400 | [diff] [blame] | 39 | rc = generic_file_read_iter(iocb, to); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 40 | if (rc >= 0) { |
Matthew Wilcox | cc18ec3 | 2013-06-15 07:55:59 -0400 | [diff] [blame] | 41 | path = ecryptfs_dentry_to_lower_path(file->f_path.dentry); |
| 42 | touch_atime(path); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 43 | } |
| 44 | return rc; |
| 45 | } |
| 46 | |
| 47 | struct ecryptfs_getdents_callback { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 48 | struct dir_context ctx; |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 49 | struct dir_context *caller; |
Al Viro | 0747fdb | 2013-06-16 20:05:38 +0400 | [diff] [blame] | 50 | struct super_block *sb; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 51 | int filldir_called; |
| 52 | int entries_written; |
| 53 | }; |
| 54 | |
Michael Halcrow | 7d6c704 | 2008-10-15 22:02:49 -0700 | [diff] [blame] | 55 | /* Inspired by generic filldir in fs/readdir.c */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 56 | static int |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 57 | ecryptfs_filldir(struct dir_context *ctx, const char *lower_name, |
| 58 | int lower_namelen, loff_t offset, u64 ino, unsigned int d_type) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 59 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 60 | struct ecryptfs_getdents_callback *buf = |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 61 | container_of(ctx, struct ecryptfs_getdents_callback, ctx); |
Michael Halcrow | a8f1286 | 2009-01-06 14:42:03 -0800 | [diff] [blame] | 62 | size_t name_size; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 63 | char *name; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 64 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 65 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 66 | buf->filldir_called++; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 67 | rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size, |
Al Viro | 0747fdb | 2013-06-16 20:05:38 +0400 | [diff] [blame] | 68 | buf->sb, lower_name, |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 69 | lower_namelen); |
| 70 | if (rc) { |
Tyler Hicks | e86281e | 2018-03-28 23:41:52 +0000 | [diff] [blame] | 71 | if (rc != -EINVAL) { |
| 72 | ecryptfs_printk(KERN_DEBUG, |
| 73 | "%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n", |
| 74 | __func__, lower_name, rc); |
| 75 | return rc; |
| 76 | } |
| 77 | |
| 78 | /* Mask -EINVAL errors as these are most likely due a plaintext |
| 79 | * filename present in the lower filesystem despite filename |
| 80 | * encryption being enabled. One unavoidable example would be |
| 81 | * the "lost+found" dentry in the root directory of an Ext4 |
| 82 | * filesystem. |
| 83 | */ |
| 84 | return 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 85 | } |
Tyler Hicks | e86281e | 2018-03-28 23:41:52 +0000 | [diff] [blame] | 86 | |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 87 | buf->caller->pos = buf->ctx.pos; |
| 88 | rc = !dir_emit(buf->caller, name, name_size, ino, d_type); |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 89 | kfree(name); |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 90 | if (!rc) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 91 | buf->entries_written++; |
Tyler Hicks | e86281e | 2018-03-28 23:41:52 +0000 | [diff] [blame] | 92 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * ecryptfs_readdir |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 98 | * @file: The eCryptfs directory file |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 99 | * @ctx: The actor to feed the entries to |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 100 | */ |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 101 | static int ecryptfs_readdir(struct file *file, struct dir_context *ctx) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 102 | { |
| 103 | int rc; |
| 104 | struct file *lower_file; |
Al Viro | 0747fdb | 2013-06-16 20:05:38 +0400 | [diff] [blame] | 105 | struct inode *inode = file_inode(file); |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 106 | struct ecryptfs_getdents_callback buf = { |
| 107 | .ctx.actor = ecryptfs_filldir, |
| 108 | .caller = ctx, |
Al Viro | 0747fdb | 2013-06-16 20:05:38 +0400 | [diff] [blame] | 109 | .sb = inode->i_sb, |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 110 | }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 111 | lower_file = ecryptfs_file_to_lower(file); |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 112 | rc = iterate_dir(lower_file, &buf.ctx); |
Al Viro | 2de5f05 | 2013-05-22 21:23:40 -0400 | [diff] [blame] | 113 | ctx->pos = buf.ctx.pos; |
Michael Halcrow | 7d6c704 | 2008-10-15 22:02:49 -0700 | [diff] [blame] | 114 | if (rc < 0) |
| 115 | goto out; |
| 116 | if (buf.filldir_called && !buf.entries_written) |
| 117 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 118 | if (rc >= 0) |
Michael Halcrow | 7d6c704 | 2008-10-15 22:02:49 -0700 | [diff] [blame] | 119 | fsstack_copy_attr_atime(inode, |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 120 | file_inode(lower_file)); |
Michael Halcrow | 7d6c704 | 2008-10-15 22:02:49 -0700 | [diff] [blame] | 121 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 122 | return rc; |
| 123 | } |
| 124 | |
| 125 | struct kmem_cache *ecryptfs_file_info_cache; |
| 126 | |
Tyler Hicks | e3ccaa9 | 2012-06-20 23:50:59 -0700 | [diff] [blame] | 127 | static int read_or_initialize_metadata(struct dentry *dentry) |
| 128 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 129 | struct inode *inode = d_inode(dentry); |
Tyler Hicks | e3ccaa9 | 2012-06-20 23:50:59 -0700 | [diff] [blame] | 130 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat; |
| 131 | struct ecryptfs_crypt_stat *crypt_stat; |
| 132 | int rc; |
| 133 | |
| 134 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; |
| 135 | mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 136 | inode->i_sb)->mount_crypt_stat; |
| 137 | mutex_lock(&crypt_stat->cs_mutex); |
| 138 | |
| 139 | if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED && |
| 140 | crypt_stat->flags & ECRYPTFS_KEY_VALID) { |
| 141 | rc = 0; |
| 142 | goto out; |
| 143 | } |
| 144 | |
| 145 | rc = ecryptfs_read_metadata(dentry); |
| 146 | if (!rc) |
| 147 | goto out; |
| 148 | |
| 149 | if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) { |
| 150 | crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED |
| 151 | | ECRYPTFS_ENCRYPTED); |
| 152 | rc = 0; |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) && |
| 157 | !i_size_read(ecryptfs_inode_to_lower(inode))) { |
| 158 | rc = ecryptfs_initialize_file(dentry, inode); |
| 159 | if (!rc) |
| 160 | goto out; |
| 161 | } |
| 162 | |
| 163 | rc = -EIO; |
| 164 | out: |
| 165 | mutex_unlock(&crypt_stat->cs_mutex); |
| 166 | return rc; |
| 167 | } |
| 168 | |
Jeff Mahoney | f0fe970 | 2016-07-05 17:32:30 -0400 | [diff] [blame] | 169 | static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma) |
| 170 | { |
| 171 | struct file *lower_file = ecryptfs_file_to_lower(file); |
| 172 | /* |
| 173 | * Don't allow mmap on top of file systems that don't support it |
| 174 | * natively. If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs |
| 175 | * allows recursive mounting, this will need to be extended. |
| 176 | */ |
| 177 | if (!lower_file->f_op->mmap) |
| 178 | return -ENODEV; |
| 179 | return generic_file_mmap(file, vma); |
| 180 | } |
| 181 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 182 | /** |
| 183 | * ecryptfs_open |
Chris J Arges | 40f0fd3 | 2016-06-09 15:31:29 -0500 | [diff] [blame] | 184 | * @inode: inode specifying file to open |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 185 | * @file: Structure to return filled in |
| 186 | * |
| 187 | * Opens the file specified by inode. |
| 188 | * |
| 189 | * Returns zero on success; non-zero otherwise |
| 190 | */ |
| 191 | static int ecryptfs_open(struct inode *inode, struct file *file) |
| 192 | { |
| 193 | int rc = 0; |
| 194 | struct ecryptfs_crypt_stat *crypt_stat = NULL; |
Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 195 | struct dentry *ecryptfs_dentry = file->f_path.dentry; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 196 | /* Private value of ecryptfs_dentry allocated in |
| 197 | * ecryptfs_lookup() */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 198 | struct ecryptfs_file_info *file_info; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 199 | |
| 200 | /* Released in ecryptfs_release or end of function if failure */ |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 201 | file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 202 | ecryptfs_set_file_private(file, file_info); |
| 203 | if (!file_info) { |
| 204 | ecryptfs_printk(KERN_ERR, |
| 205 | "Error attempting to allocate memory\n"); |
| 206 | rc = -ENOMEM; |
| 207 | goto out; |
| 208 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 209 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 210 | mutex_lock(&crypt_stat->cs_mutex); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 211 | if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 212 | ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n"); |
| 213 | /* Policy code enabled in future release */ |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 214 | crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED |
| 215 | | ECRYPTFS_ENCRYPTED); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 216 | } |
| 217 | mutex_unlock(&crypt_stat->cs_mutex); |
Tyler Hicks | 3b06b3e | 2011-05-24 03:49:02 -0500 | [diff] [blame] | 218 | rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode); |
Roberto Sassu | 2799289 | 2010-11-03 11:11:28 +0100 | [diff] [blame] | 219 | if (rc) { |
| 220 | printk(KERN_ERR "%s: Error attempting to initialize " |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 221 | "the lower file for the dentry with name " |
David Howells | 9e78d14 | 2013-12-10 15:26:48 +0000 | [diff] [blame] | 222 | "[%pd]; rc = [%d]\n", __func__, |
| 223 | ecryptfs_dentry, rc); |
Roberto Sassu | 2799289 | 2010-11-03 11:11:28 +0100 | [diff] [blame] | 224 | goto out_free; |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 225 | } |
Roberto Sassu | 0abe116 | 2010-11-03 11:11:34 +0100 | [diff] [blame] | 226 | if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE) |
| 227 | == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) { |
Erez Zadok | e27759d | 2009-12-03 13:35:27 -0500 | [diff] [blame] | 228 | rc = -EPERM; |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 229 | printk(KERN_WARNING "%s: Lower file is RO; eCryptfs " |
Erez Zadok | e27759d | 2009-12-03 13:35:27 -0500 | [diff] [blame] | 230 | "file must hence be opened RO\n", __func__); |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 231 | goto out_put; |
Erez Zadok | e27759d | 2009-12-03 13:35:27 -0500 | [diff] [blame] | 232 | } |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 233 | ecryptfs_set_file_lower( |
| 234 | file, ecryptfs_inode_to_private(inode)->lower_file); |
Tyler Hicks | e3ccaa9 | 2012-06-20 23:50:59 -0700 | [diff] [blame] | 235 | rc = read_or_initialize_metadata(ecryptfs_dentry); |
| 236 | if (rc) |
| 237 | goto out_put; |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 238 | ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = " |
| 239 | "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino, |
| 240 | (unsigned long long)i_size_read(inode)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 241 | goto out; |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 242 | out_put: |
| 243 | ecryptfs_put_lower_file(inode); |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 244 | out_free: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 245 | kmem_cache_free(ecryptfs_file_info_cache, |
| 246 | ecryptfs_file_to_private(file)); |
| 247 | out: |
| 248 | return rc; |
| 249 | } |
| 250 | |
Al Viro | 6a480a7 | 2016-05-04 14:04:13 -0400 | [diff] [blame] | 251 | /** |
| 252 | * ecryptfs_dir_open |
Chris J Arges | 40f0fd3 | 2016-06-09 15:31:29 -0500 | [diff] [blame] | 253 | * @inode: inode specifying file to open |
Al Viro | 6a480a7 | 2016-05-04 14:04:13 -0400 | [diff] [blame] | 254 | * @file: Structure to return filled in |
| 255 | * |
| 256 | * Opens the file specified by inode. |
| 257 | * |
| 258 | * Returns zero on success; non-zero otherwise |
| 259 | */ |
| 260 | static int ecryptfs_dir_open(struct inode *inode, struct file *file) |
| 261 | { |
| 262 | struct dentry *ecryptfs_dentry = file->f_path.dentry; |
| 263 | /* Private value of ecryptfs_dentry allocated in |
| 264 | * ecryptfs_lookup() */ |
| 265 | struct ecryptfs_file_info *file_info; |
| 266 | struct file *lower_file; |
| 267 | |
| 268 | /* Released in ecryptfs_release or end of function if failure */ |
| 269 | file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL); |
| 270 | ecryptfs_set_file_private(file, file_info); |
| 271 | if (unlikely(!file_info)) { |
| 272 | ecryptfs_printk(KERN_ERR, |
| 273 | "Error attempting to allocate memory\n"); |
| 274 | return -ENOMEM; |
| 275 | } |
| 276 | lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry), |
| 277 | file->f_flags, current_cred()); |
| 278 | if (IS_ERR(lower_file)) { |
| 279 | printk(KERN_ERR "%s: Error attempting to initialize " |
| 280 | "the lower file for the dentry with name " |
| 281 | "[%pd]; rc = [%ld]\n", __func__, |
| 282 | ecryptfs_dentry, PTR_ERR(lower_file)); |
| 283 | kmem_cache_free(ecryptfs_file_info_cache, file_info); |
| 284 | return PTR_ERR(lower_file); |
| 285 | } |
| 286 | ecryptfs_set_file_lower(file, lower_file); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 290 | static int ecryptfs_flush(struct file *file, fl_owner_t td) |
| 291 | { |
Tyler Hicks | 64e6651 | 2012-09-12 18:38:00 -0700 | [diff] [blame] | 292 | struct file *lower_file = ecryptfs_file_to_lower(file); |
| 293 | |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 294 | if (lower_file->f_op->flush) { |
Tyler Hicks | 64e6651 | 2012-09-12 18:38:00 -0700 | [diff] [blame] | 295 | filemap_write_and_wait(file->f_mapping); |
| 296 | return lower_file->f_op->flush(lower_file, td); |
| 297 | } |
| 298 | |
| 299 | return 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static int ecryptfs_release(struct inode *inode, struct file *file) |
| 303 | { |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 304 | ecryptfs_put_lower_file(inode); |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 305 | kmem_cache_free(ecryptfs_file_info_cache, |
| 306 | ecryptfs_file_to_private(file)); |
| 307 | return 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Al Viro | 6a480a7 | 2016-05-04 14:04:13 -0400 | [diff] [blame] | 310 | static int ecryptfs_dir_release(struct inode *inode, struct file *file) |
| 311 | { |
| 312 | fput(ecryptfs_file_to_lower(file)); |
| 313 | kmem_cache_free(ecryptfs_file_info_cache, |
| 314 | ecryptfs_file_to_private(file)); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence) |
| 319 | { |
| 320 | return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence); |
| 321 | } |
| 322 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 323 | static int |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 324 | ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 325 | { |
Tyler Hicks | bc5abcf | 2013-06-04 10:24:56 -0700 | [diff] [blame] | 326 | int rc; |
| 327 | |
Jeff Layton | 6d4b512 | 2017-08-02 10:14:21 -0400 | [diff] [blame] | 328 | rc = file_write_and_wait(file); |
Tyler Hicks | bc5abcf | 2013-06-04 10:24:56 -0700 | [diff] [blame] | 329 | if (rc) |
| 330 | return rc; |
| 331 | |
Tyler Hicks | 821f749 | 2012-07-03 16:50:57 -0700 | [diff] [blame] | 332 | return vfs_fsync(ecryptfs_file_to_lower(file), datasync); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int ecryptfs_fasync(int fd, struct file *file, int flag) |
| 336 | { |
| 337 | int rc = 0; |
| 338 | struct file *lower_file = NULL; |
| 339 | |
| 340 | lower_file = ecryptfs_file_to_lower(file); |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 341 | if (lower_file->f_op->fasync) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 342 | rc = lower_file->f_op->fasync(fd, lower_file, flag); |
| 343 | return rc; |
| 344 | } |
| 345 | |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 346 | static long |
| 347 | ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 348 | { |
Tyler Hicks | 2000f5b | 2013-11-13 10:09:42 -0800 | [diff] [blame] | 349 | struct file *lower_file = ecryptfs_file_to_lower(file); |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 350 | long rc = -ENOTTY; |
| 351 | |
Tyler Hicks | 6d65261 | 2015-02-24 19:28:10 -0600 | [diff] [blame] | 352 | if (!lower_file->f_op->unlocked_ioctl) |
| 353 | return rc; |
| 354 | |
| 355 | switch (cmd) { |
| 356 | case FITRIM: |
| 357 | case FS_IOC_GETFLAGS: |
| 358 | case FS_IOC_SETFLAGS: |
| 359 | case FS_IOC_GETVERSION: |
| 360 | case FS_IOC_SETVERSION: |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 361 | rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg); |
Tyler Hicks | 6d65261 | 2015-02-24 19:28:10 -0600 | [diff] [blame] | 362 | fsstack_copy_attr_all(file_inode(file), file_inode(lower_file)); |
| 363 | |
| 364 | return rc; |
| 365 | default: |
| 366 | return rc; |
| 367 | } |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | #ifdef CONFIG_COMPAT |
| 371 | static long |
| 372 | ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 373 | { |
Tyler Hicks | 2000f5b | 2013-11-13 10:09:42 -0800 | [diff] [blame] | 374 | struct file *lower_file = ecryptfs_file_to_lower(file); |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 375 | long rc = -ENOIOCTLCMD; |
| 376 | |
Tyler Hicks | 6d65261 | 2015-02-24 19:28:10 -0600 | [diff] [blame] | 377 | if (!lower_file->f_op->compat_ioctl) |
| 378 | return rc; |
| 379 | |
| 380 | switch (cmd) { |
Arnd Bergmann | 314999d | 2019-06-03 13:51:58 +0200 | [diff] [blame] | 381 | case FITRIM: |
Tyler Hicks | 6d65261 | 2015-02-24 19:28:10 -0600 | [diff] [blame] | 382 | case FS_IOC32_GETFLAGS: |
| 383 | case FS_IOC32_SETFLAGS: |
| 384 | case FS_IOC32_GETVERSION: |
| 385 | case FS_IOC32_SETVERSION: |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 386 | rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg); |
Tyler Hicks | 6d65261 | 2015-02-24 19:28:10 -0600 | [diff] [blame] | 387 | fsstack_copy_attr_all(file_inode(file), file_inode(lower_file)); |
| 388 | |
| 389 | return rc; |
| 390 | default: |
| 391 | return rc; |
| 392 | } |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 393 | } |
| 394 | #endif |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 395 | |
| 396 | const struct file_operations ecryptfs_dir_fops = { |
Al Viro | 51a16a9 | 2016-05-04 16:21:20 -0400 | [diff] [blame] | 397 | .iterate_shared = ecryptfs_readdir, |
Andy Whitcroft | 323ef68 | 2011-02-16 04:49:59 +0000 | [diff] [blame] | 398 | .read = generic_read_dir, |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 399 | .unlocked_ioctl = ecryptfs_unlocked_ioctl, |
| 400 | #ifdef CONFIG_COMPAT |
| 401 | .compat_ioctl = ecryptfs_compat_ioctl, |
| 402 | #endif |
Al Viro | 6a480a7 | 2016-05-04 14:04:13 -0400 | [diff] [blame] | 403 | .open = ecryptfs_dir_open, |
| 404 | .release = ecryptfs_dir_release, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 405 | .fsync = ecryptfs_fsync, |
Al Viro | 6a480a7 | 2016-05-04 14:04:13 -0400 | [diff] [blame] | 406 | .llseek = ecryptfs_dir_llseek, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | const struct file_operations ecryptfs_main_fops = { |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 410 | .llseek = generic_file_llseek, |
Al Viro | 0279782 | 2014-04-02 14:40:38 -0400 | [diff] [blame] | 411 | .read_iter = ecryptfs_read_update_atime, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 412 | .write_iter = generic_file_write_iter, |
Tyler Hicks | c43f7b8 | 2009-11-03 11:45:11 -0600 | [diff] [blame] | 413 | .unlocked_ioctl = ecryptfs_unlocked_ioctl, |
| 414 | #ifdef CONFIG_COMPAT |
| 415 | .compat_ioctl = ecryptfs_compat_ioctl, |
| 416 | #endif |
Jeff Mahoney | f0fe970 | 2016-07-05 17:32:30 -0400 | [diff] [blame] | 417 | .mmap = ecryptfs_mmap, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 418 | .open = ecryptfs_open, |
| 419 | .flush = ecryptfs_flush, |
| 420 | .release = ecryptfs_release, |
| 421 | .fsync = ecryptfs_fsync, |
| 422 | .fasync = ecryptfs_fasync, |
Michael Halcrow | e9f6a99 | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 423 | .splice_read = generic_file_splice_read, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 424 | }; |