blob: 861a01713f3f4c16e726c051e52a39673a9f872d [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Michael Halcrow237fead2006-10-04 02:16:22 -07002/**
3 * eCryptfs: Linux filesystem encryption layer
4 *
5 * Copyright (C) 1997-2004 Erez Zadok
6 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08007 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07008 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Michael C. Thompsion <mcthomps@us.ibm.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070010 */
11
12#include <linux/file.h>
13#include <linux/vmalloc.h>
14#include <linux/pagemap.h>
15#include <linux/dcache.h>
16#include <linux/namei.h>
17#include <linux/mount.h>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080018#include <linux/fs_stack.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Roberto Sassu48b512e2010-10-05 18:53:45 +020020#include <linux/xattr.h>
Harvey Harrison0a688ad2008-07-23 21:30:07 -070021#include <asm/unaligned.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070022#include "ecryptfs_kernel.h"
23
Al Virob2648d52021-01-29 17:35:43 -050024static int lock_parent(struct dentry *dentry,
25 struct dentry **lower_dentry,
26 struct inode **lower_dir)
Michael Halcrow237fead2006-10-04 02:16:22 -070027{
Al Virob2648d52021-01-29 17:35:43 -050028 struct dentry *lower_dir_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -070029
Al Virob2648d52021-01-29 17:35:43 -050030 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
31 *lower_dir = d_inode(lower_dir_dentry);
32 *lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -070033
Al Virob2648d52021-01-29 17:35:43 -050034 inode_lock_nested(*lower_dir, I_MUTEX_PARENT);
35 return (*lower_dentry)->d_parent == lower_dir_dentry ? 0 : -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -070036}
37
Tyler Hicksc4f79072011-05-23 21:18:20 -050038static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
39{
Himangi Saraogic4cf3ba2014-06-27 01:11:59 +053040 return ecryptfs_inode_to_lower(inode) == lower_inode;
Tyler Hicksc4f79072011-05-23 21:18:20 -050041}
42
Tyler Hicks5ccf9202011-05-24 02:16:51 -050043static int ecryptfs_inode_set(struct inode *inode, void *opaque)
Tyler Hicksc4f79072011-05-23 21:18:20 -050044{
Tyler Hicks5ccf9202011-05-24 02:16:51 -050045 struct inode *lower_inode = opaque;
46
47 ecryptfs_set_inode_lower(inode, lower_inode);
48 fsstack_copy_attr_all(inode, lower_inode);
49 /* i_size will be overwritten for encrypted regular files */
50 fsstack_copy_inode_size(inode, lower_inode);
51 inode->i_ino = lower_inode->i_ino;
Tyler Hicksc4f79072011-05-23 21:18:20 -050052 inode->i_mapping->a_ops = &ecryptfs_aops;
Tyler Hicks5ccf9202011-05-24 02:16:51 -050053
54 if (S_ISLNK(inode->i_mode))
55 inode->i_op = &ecryptfs_symlink_iops;
56 else if (S_ISDIR(inode->i_mode))
57 inode->i_op = &ecryptfs_dir_iops;
58 else
59 inode->i_op = &ecryptfs_main_iops;
60
61 if (S_ISDIR(inode->i_mode))
62 inode->i_fop = &ecryptfs_dir_fops;
63 else if (special_file(inode->i_mode))
64 init_special_inode(inode, inode->i_mode, inode->i_rdev);
65 else
66 inode->i_fop = &ecryptfs_main_fops;
67
Tyler Hicksc4f79072011-05-23 21:18:20 -050068 return 0;
69}
70
Tyler Hicks5ccf9202011-05-24 02:16:51 -050071static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
72 struct super_block *sb)
73{
74 struct inode *inode;
75
76 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
77 return ERR_PTR(-EXDEV);
78 if (!igrab(lower_inode))
79 return ERR_PTR(-ESTALE);
80 inode = iget5_locked(sb, (unsigned long)lower_inode,
81 ecryptfs_inode_test, ecryptfs_inode_set,
82 lower_inode);
83 if (!inode) {
84 iput(lower_inode);
85 return ERR_PTR(-EACCES);
86 }
87 if (!(inode->i_state & I_NEW))
88 iput(lower_inode);
89
90 return inode;
91}
92
Tyler Hicksc4f79072011-05-23 21:18:20 -050093struct inode *ecryptfs_get_inode(struct inode *lower_inode,
94 struct super_block *sb)
95{
Tyler Hicks5ccf9202011-05-24 02:16:51 -050096 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
Tyler Hicksc4f79072011-05-23 21:18:20 -050097
Tyler Hicks5ccf9202011-05-24 02:16:51 -050098 if (!IS_ERR(inode) && (inode->i_state & I_NEW))
Tyler Hicksc4f79072011-05-23 21:18:20 -050099 unlock_new_inode(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500100
Tyler Hicksc4f79072011-05-23 21:18:20 -0500101 return inode;
Tyler Hicksc4f79072011-05-23 21:18:20 -0500102}
103
Tyler Hicksc4f79072011-05-23 21:18:20 -0500104/**
105 * ecryptfs_interpose
106 * @lower_dentry: Existing dentry in the lower filesystem
107 * @dentry: ecryptfs' dentry
108 * @sb: ecryptfs's super_block
Tyler Hicksc4f79072011-05-23 21:18:20 -0500109 *
110 * Interposes upper and lower dentries.
111 *
112 * Returns zero on success; non-zero otherwise
113 */
114static int ecryptfs_interpose(struct dentry *lower_dentry,
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500115 struct dentry *dentry, struct super_block *sb)
Tyler Hicksc4f79072011-05-23 21:18:20 -0500116{
David Howells2b0143b2015-03-17 22:25:59 +0000117 struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500118
Tyler Hicksc4f79072011-05-23 21:18:20 -0500119 if (IS_ERR(inode))
120 return PTR_ERR(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500121 d_instantiate(dentry, inode);
122
Tyler Hicksc4f79072011-05-23 21:18:20 -0500123 return 0;
124}
125
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500126static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
127 struct inode *inode)
128{
Al Virob2648d52021-01-29 17:35:43 -0500129 struct dentry *lower_dentry;
130 struct inode *lower_dir;
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500131 int rc;
132
Al Virob2648d52021-01-29 17:35:43 -0500133 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
Al Virobcf0d9d2019-11-03 12:07:15 -0500134 dget(lower_dentry); // don't even try to make the lower negative
Al Virob2648d52021-01-29 17:35:43 -0500135 if (!rc) {
136 if (d_unhashed(lower_dentry))
137 rc = -EINVAL;
138 else
139 rc = vfs_unlink(&init_user_ns, lower_dir, lower_dentry,
140 NULL);
141 }
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500142 if (rc) {
143 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
144 goto out_unlock;
145 }
Al Virob2648d52021-01-29 17:35:43 -0500146 fsstack_copy_attr_times(dir, lower_dir);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500147 set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
148 inode->i_ctime = dir->i_ctime;
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500149out_unlock:
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500150 dput(lower_dentry);
Al Virob2648d52021-01-29 17:35:43 -0500151 inode_unlock(lower_dir);
Al Virobcf0d9d2019-11-03 12:07:15 -0500152 if (!rc)
153 d_drop(dentry);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500154 return rc;
155}
156
Michael Halcrow237fead2006-10-04 02:16:22 -0700157/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700158 * ecryptfs_do_create
159 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
160 * @ecryptfs_dentry: New file's dentry in ecryptfs
161 * @mode: The mode of the new file
Michael Halcrow237fead2006-10-04 02:16:22 -0700162 *
163 * Creates the underlying file and the eCryptfs inode which will link to
164 * it. It will also update the eCryptfs directory inode to mimic the
165 * stat of the lower directory inode.
166 *
Tyler Hicksb59db432011-11-21 17:31:02 -0600167 * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
Michael Halcrow237fead2006-10-04 02:16:22 -0700168 */
Tyler Hicksb59db432011-11-21 17:31:02 -0600169static struct inode *
Michael Halcrow237fead2006-10-04 02:16:22 -0700170ecryptfs_do_create(struct inode *directory_inode,
Al Viro175a4eb2011-07-26 03:30:54 -0400171 struct dentry *ecryptfs_dentry, umode_t mode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700172{
173 int rc;
174 struct dentry *lower_dentry;
Al Virob2648d52021-01-29 17:35:43 -0500175 struct inode *lower_dir;
Tyler Hicksb59db432011-11-21 17:31:02 -0600176 struct inode *inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700177
Al Virob2648d52021-01-29 17:35:43 -0500178 rc = lock_parent(ecryptfs_dentry, &lower_dentry, &lower_dir);
179 if (!rc)
180 rc = vfs_create(&init_user_ns, lower_dir,
181 lower_dentry, mode, true);
Michael Halcrow4981e082007-10-16 01:28:09 -0700182 if (rc) {
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800183 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700184 "rc = [%d]\n", __func__, rc);
Tyler Hicksb59db432011-11-21 17:31:02 -0600185 inode = ERR_PTR(rc);
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800186 goto out_lock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700187 }
David Howells2b0143b2015-03-17 22:25:59 +0000188 inode = __ecryptfs_get_inode(d_inode(lower_dentry),
Tyler Hicksb59db432011-11-21 17:31:02 -0600189 directory_inode->i_sb);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500190 if (IS_ERR(inode)) {
Al Virob2648d52021-01-29 17:35:43 -0500191 vfs_unlink(&init_user_ns, lower_dir, lower_dentry, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700192 goto out_lock;
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500193 }
Al Virob2648d52021-01-29 17:35:43 -0500194 fsstack_copy_attr_times(directory_inode, lower_dir);
195 fsstack_copy_inode_size(directory_inode, lower_dir);
Michael Halcrow237fead2006-10-04 02:16:22 -0700196out_lock:
Al Virob2648d52021-01-29 17:35:43 -0500197 inode_unlock(lower_dir);
Tyler Hicksb59db432011-11-21 17:31:02 -0600198 return inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700199}
200
201/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700202 * ecryptfs_initialize_file
203 *
204 * Cause the file to be changed from a basic empty file to an ecryptfs
205 * file with a header and first data page.
206 *
207 * Returns zero on success
208 */
Tyler Hickse3ccaa92012-06-20 23:50:59 -0700209int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
210 struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700211{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700212 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -0600213 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700214 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700215
Tyler Hicksb59db432011-11-21 17:31:02 -0600216 if (S_ISDIR(ecryptfs_inode->i_mode)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700217 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800218 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700219 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700220 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700221 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
Tyler Hicksb59db432011-11-21 17:31:02 -0600222 rc = ecryptfs_new_file_context(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700223 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700224 ecryptfs_printk(KERN_ERR, "Error creating new file "
225 "context; rc = [%d]\n", rc);
226 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700227 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600228 rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100229 if (rc) {
230 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500231 "the lower file for the dentry with name "
David Howells9e78d142013-12-10 15:26:48 +0000232 "[%pd]; rc = [%d]\n", __func__,
233 ecryptfs_dentry, rc);
Roberto Sassu27992892010-11-03 11:11:28 +0100234 goto out;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700235 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600236 rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500237 if (rc)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700238 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
Tyler Hicksb59db432011-11-21 17:31:02 -0600239 ecryptfs_put_lower_file(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700240out:
241 return rc;
242}
243
244/**
245 * ecryptfs_create
246 * @dir: The inode of the directory in which to create the file.
247 * @dentry: The eCryptfs dentry
248 * @mode: The mode of the new file.
Michael Halcrow237fead2006-10-04 02:16:22 -0700249 *
250 * Creates a new file.
251 *
252 * Returns zero on success; non-zero on error condition
253 */
254static int
Christian Brauner549c7292021-01-21 14:19:43 +0100255ecryptfs_create(struct user_namespace *mnt_userns,
256 struct inode *directory_inode, struct dentry *ecryptfs_dentry,
Al Viroebfc3b42012-06-10 18:05:36 -0400257 umode_t mode, bool excl)
Michael Halcrow237fead2006-10-04 02:16:22 -0700258{
Tyler Hicksb59db432011-11-21 17:31:02 -0600259 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700260 int rc;
261
Tyler Hicksb59db432011-11-21 17:31:02 -0600262 ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
263 mode);
Viresh Kumara1c83682015-08-12 15:59:44 +0530264 if (IS_ERR(ecryptfs_inode)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700265 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
266 "lower filesystem\n");
Tyler Hicksb59db432011-11-21 17:31:02 -0600267 rc = PTR_ERR(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700268 goto out;
269 }
270 /* At this point, a file exists on "disk"; we need to make sure
271 * that this on disk file is prepared to be an ecryptfs file */
Tyler Hicksb59db432011-11-21 17:31:02 -0600272 rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
273 if (rc) {
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500274 ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
275 ecryptfs_inode);
Al Viro0e81ba22015-12-05 18:23:48 -0500276 iget_failed(ecryptfs_inode);
Tyler Hicksb59db432011-11-21 17:31:02 -0600277 goto out;
278 }
Al Viro1e2e5472018-05-04 08:23:01 -0400279 d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700280out:
281 return rc;
282}
283
Tyler Hicks778aeb42011-05-24 04:56:23 -0500284static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700285{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500286 struct ecryptfs_crypt_stat *crypt_stat;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500287 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700288
Tyler Hicks778aeb42011-05-24 04:56:23 -0500289 rc = ecryptfs_get_lower_file(dentry, inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100290 if (rc) {
291 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500292 "the lower file for the dentry with name "
David Howells9e78d142013-12-10 15:26:48 +0000293 "[%pd]; rc = [%d]\n", __func__,
294 dentry, rc);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500295 return rc;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700296 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500297
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500298 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500299 /* TODO: lock for crypt_stat comparison */
300 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
Tyler Hicks778aeb42011-05-24 04:56:23 -0500301 ecryptfs_set_default_sizes(crypt_stat);
302
303 rc = ecryptfs_read_and_validate_header_region(inode);
304 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700305 if (rc) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500306 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
307 if (!rc)
308 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
Michael Halcrow237fead2006-10-04 02:16:22 -0700309 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500310
311 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
312 return 0;
313}
314
315/**
316 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
317 */
Al Virob1168a92016-03-28 00:30:35 -0400318static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
319 struct dentry *lower_dentry)
Tyler Hicks778aeb42011-05-24 04:56:23 -0500320{
Al Viro762c6962019-11-03 13:55:43 -0500321 struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
Al Viroe72b9dd2019-11-03 13:45:04 -0500322 struct inode *inode, *lower_inode;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500323 struct ecryptfs_dentry_info *dentry_info;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500324 int rc = 0;
325
Tyler Hicks778aeb42011-05-24 04:56:23 -0500326 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500327 if (!dentry_info) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500328 dput(lower_dentry);
Al Virob1168a92016-03-28 00:30:35 -0400329 return ERR_PTR(-ENOMEM);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500330 }
Al Viro0b1d9012012-07-20 12:09:19 +0400331
Al Virob1168a92016-03-28 00:30:35 -0400332 fsstack_copy_attr_atime(d_inode(dentry->d_parent),
Al Viro762c6962019-11-03 13:55:43 -0500333 d_inode(path->dentry));
Al Viro84d08fa2013-07-05 18:59:33 +0400334 BUG_ON(!d_count(lower_dentry));
Al Viro0b1d9012012-07-20 12:09:19 +0400335
336 ecryptfs_set_dentry_private(dentry, dentry_info);
Al Viro762c6962019-11-03 13:55:43 -0500337 dentry_info->lower_path.mnt = mntget(path->mnt);
Al Viro92dd1232013-09-15 20:50:13 -0400338 dentry_info->lower_path.dentry = lower_dentry;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500339
Al Viroe72b9dd2019-11-03 13:45:04 -0500340 /*
341 * negative dentry can go positive under us here - its parent is not
342 * locked. That's OK and that could happen just as we return from
343 * ecryptfs_lookup() anyway. Just need to be careful and fetch
344 * ->d_inode only once - it's not stable here.
345 */
346 lower_inode = READ_ONCE(lower_dentry->d_inode);
347
348 if (!lower_inode) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500349 /* We want to add because we couldn't find in lower */
350 d_add(dentry, NULL);
Al Virob1168a92016-03-28 00:30:35 -0400351 return NULL;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500352 }
Al Virob1168a92016-03-28 00:30:35 -0400353 inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500354 if (IS_ERR(inode)) {
355 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
356 __func__, PTR_ERR(inode));
Al Virob1168a92016-03-28 00:30:35 -0400357 return ERR_CAST(inode);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500358 }
359 if (S_ISREG(inode->i_mode)) {
360 rc = ecryptfs_i_size_read(dentry, inode);
361 if (rc) {
362 make_bad_inode(inode);
Al Virob1168a92016-03-28 00:30:35 -0400363 return ERR_PTR(rc);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500364 }
365 }
366
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500367 if (inode->i_state & I_NEW)
368 unlock_new_inode(inode);
Al Virob1168a92016-03-28 00:30:35 -0400369 return d_splice_alias(inode, dentry);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800370}
371
372/**
373 * ecryptfs_lookup
374 * @ecryptfs_dir_inode: The eCryptfs directory inode
375 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
Al Viro89076bc2015-05-12 08:29:38 -0400376 * @flags: lookup flags
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800377 *
378 * Find a file on disk. If the file does not exist, then we'll add it to the
379 * dentry cache and continue on to read it from the disk.
380 */
381static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
382 struct dentry *ecryptfs_dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400383 unsigned int flags)
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800384{
385 char *encrypted_and_encoded_name = NULL;
Al Viro88ae4ab2016-03-28 00:43:29 -0400386 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800387 struct dentry *lower_dir_dentry, *lower_dentry;
Al Viro88ae4ab2016-03-28 00:43:29 -0400388 const char *name = ecryptfs_dentry->d_name.name;
389 size_t len = ecryptfs_dentry->d_name.len;
Al Virob1168a92016-03-28 00:30:35 -0400390 struct dentry *res;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800391 int rc = 0;
392
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800393 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
Al Viro88ae4ab2016-03-28 00:43:29 -0400394
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500395 mount_crypt_stat = &ecryptfs_superblock_to_private(
396 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Guenter Roeckab13a922018-01-18 18:40:25 -0800397 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
Al Viro88ae4ab2016-03-28 00:43:29 -0400398 rc = ecryptfs_encrypt_and_encode_filename(
399 &encrypted_and_encoded_name, &len,
400 mount_crypt_stat, name, len);
401 if (rc) {
402 printk(KERN_ERR "%s: Error attempting to encrypt and encode "
403 "filename; rc = [%d]\n", __func__, rc);
404 return ERR_PTR(rc);
405 }
406 name = encrypted_and_encoded_name;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800407 }
Al Viro88ae4ab2016-03-28 00:43:29 -0400408
409 lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800410 if (IS_ERR(lower_dentry)) {
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600411 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
Al Virob1168a92016-03-28 00:30:35 -0400412 "[%ld] on lower_dentry = [%s]\n", __func__,
413 PTR_ERR(lower_dentry),
Al Viro88ae4ab2016-03-28 00:43:29 -0400414 name);
Al Virob1168a92016-03-28 00:30:35 -0400415 res = ERR_CAST(lower_dentry);
Al Viro88ae4ab2016-03-28 00:43:29 -0400416 } else {
417 res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800418 }
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800419 kfree(encrypted_and_encoded_name);
Al Virob1168a92016-03-28 00:30:35 -0400420 return res;
Michael Halcrow237fead2006-10-04 02:16:22 -0700421}
422
423static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
424 struct dentry *new_dentry)
425{
426 struct dentry *lower_old_dentry;
427 struct dentry *lower_new_dentry;
Al Virob2648d52021-01-29 17:35:43 -0500428 struct inode *lower_dir;
Michael Halcrow237fead2006-10-04 02:16:22 -0700429 u64 file_size_save;
430 int rc;
431
David Howells2b0143b2015-03-17 22:25:59 +0000432 file_size_save = i_size_read(d_inode(old_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700433 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
Al Virob2648d52021-01-29 17:35:43 -0500434 rc = lock_parent(new_dentry, &lower_new_dentry, &lower_dir);
435 if (!rc)
436 rc = vfs_link(lower_old_dentry, &init_user_ns, lower_dir,
437 lower_new_dentry, NULL);
David Howells2b0143b2015-03-17 22:25:59 +0000438 if (rc || d_really_is_negative(lower_new_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700439 goto out_lock;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500440 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700441 if (rc)
442 goto out_lock;
Al Virob2648d52021-01-29 17:35:43 -0500443 fsstack_copy_attr_times(dir, lower_dir);
444 fsstack_copy_inode_size(dir, lower_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000445 set_nlink(d_inode(old_dentry),
446 ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
447 i_size_write(d_inode(new_dentry), file_size_save);
Michael Halcrow237fead2006-10-04 02:16:22 -0700448out_lock:
Al Virob2648d52021-01-29 17:35:43 -0500449 inode_unlock(lower_dir);
Michael Halcrow237fead2006-10-04 02:16:22 -0700450 return rc;
451}
452
453static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
454{
David Howells2b0143b2015-03-17 22:25:59 +0000455 return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700456}
457
Christian Brauner549c7292021-01-21 14:19:43 +0100458static int ecryptfs_symlink(struct user_namespace *mnt_userns,
459 struct inode *dir, struct dentry *dentry,
Michael Halcrow237fead2006-10-04 02:16:22 -0700460 const char *symname)
461{
462 int rc;
463 struct dentry *lower_dentry;
Al Virob2648d52021-01-29 17:35:43 -0500464 struct inode *lower_dir;
Michael Halcrow237fead2006-10-04 02:16:22 -0700465 char *encoded_symname;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800466 size_t encoded_symlen;
467 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700468
Al Virob2648d52021-01-29 17:35:43 -0500469 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
470 if (rc)
471 goto out_lock;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800472 mount_crypt_stat = &ecryptfs_superblock_to_private(
473 dir->i_sb)->mount_crypt_stat;
474 rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
475 &encoded_symlen,
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800476 mount_crypt_stat, symname,
477 strlen(symname));
478 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700479 goto out_lock;
Al Virob2648d52021-01-29 17:35:43 -0500480 rc = vfs_symlink(&init_user_ns, lower_dir, lower_dentry,
Miklos Szeredidb2e7472008-06-24 16:50:16 +0200481 encoded_symname);
Michael Halcrow237fead2006-10-04 02:16:22 -0700482 kfree(encoded_symname);
David Howells2b0143b2015-03-17 22:25:59 +0000483 if (rc || d_really_is_negative(lower_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700484 goto out_lock;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500485 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700486 if (rc)
487 goto out_lock;
Al Virob2648d52021-01-29 17:35:43 -0500488 fsstack_copy_attr_times(dir, lower_dir);
489 fsstack_copy_inode_size(dir, lower_dir);
Michael Halcrow237fead2006-10-04 02:16:22 -0700490out_lock:
Al Virob2648d52021-01-29 17:35:43 -0500491 inode_unlock(lower_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000492 if (d_really_is_negative(dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700493 d_drop(dentry);
494 return rc;
495}
496
Christian Brauner549c7292021-01-21 14:19:43 +0100497static int ecryptfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
498 struct dentry *dentry, umode_t mode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700499{
500 int rc;
501 struct dentry *lower_dentry;
Al Virob2648d52021-01-29 17:35:43 -0500502 struct inode *lower_dir;
Michael Halcrow237fead2006-10-04 02:16:22 -0700503
Al Virob2648d52021-01-29 17:35:43 -0500504 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
505 if (!rc)
506 rc = vfs_mkdir(&init_user_ns, lower_dir,
507 lower_dentry, mode);
David Howells2b0143b2015-03-17 22:25:59 +0000508 if (rc || d_really_is_negative(lower_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700509 goto out;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500510 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700511 if (rc)
512 goto out;
Al Virob2648d52021-01-29 17:35:43 -0500513 fsstack_copy_attr_times(dir, lower_dir);
514 fsstack_copy_inode_size(dir, lower_dir);
515 set_nlink(dir, lower_dir->i_nlink);
Michael Halcrow237fead2006-10-04 02:16:22 -0700516out:
Al Virob2648d52021-01-29 17:35:43 -0500517 inode_unlock(lower_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000518 if (d_really_is_negative(dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700519 d_drop(dentry);
520 return rc;
521}
522
523static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
524{
Michael Halcrow237fead2006-10-04 02:16:22 -0700525 struct dentry *lower_dentry;
Al Virob2648d52021-01-29 17:35:43 -0500526 struct inode *lower_dir;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800527 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700528
Al Virob2648d52021-01-29 17:35:43 -0500529 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
Al Virobcf0d9d2019-11-03 12:07:15 -0500530 dget(lower_dentry); // don't even try to make the lower negative
Al Virob2648d52021-01-29 17:35:43 -0500531 if (!rc) {
532 if (d_unhashed(lower_dentry))
533 rc = -EINVAL;
534 else
535 rc = vfs_rmdir(&init_user_ns, lower_dir, lower_dentry);
536 }
Al Virobcf0d9d2019-11-03 12:07:15 -0500537 if (!rc) {
David Howells2b0143b2015-03-17 22:25:59 +0000538 clear_nlink(d_inode(dentry));
Al Virob2648d52021-01-29 17:35:43 -0500539 fsstack_copy_attr_times(dir, lower_dir);
540 set_nlink(dir, lower_dir->i_nlink);
Al Virobcf0d9d2019-11-03 12:07:15 -0500541 }
542 dput(lower_dentry);
Al Virob2648d52021-01-29 17:35:43 -0500543 inode_unlock(lower_dir);
Michael Halcrow237fead2006-10-04 02:16:22 -0700544 if (!rc)
545 d_drop(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700546 return rc;
547}
548
549static int
Christian Brauner549c7292021-01-21 14:19:43 +0100550ecryptfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
551 struct dentry *dentry, umode_t mode, dev_t dev)
Michael Halcrow237fead2006-10-04 02:16:22 -0700552{
553 int rc;
554 struct dentry *lower_dentry;
Al Virob2648d52021-01-29 17:35:43 -0500555 struct inode *lower_dir;
Michael Halcrow237fead2006-10-04 02:16:22 -0700556
Al Virob2648d52021-01-29 17:35:43 -0500557 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
558 if (!rc)
559 rc = vfs_mknod(&init_user_ns, lower_dir,
560 lower_dentry, mode, dev);
David Howells2b0143b2015-03-17 22:25:59 +0000561 if (rc || d_really_is_negative(lower_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700562 goto out;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500563 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700564 if (rc)
565 goto out;
Al Virob2648d52021-01-29 17:35:43 -0500566 fsstack_copy_attr_times(dir, lower_dir);
567 fsstack_copy_inode_size(dir, lower_dir);
Michael Halcrow237fead2006-10-04 02:16:22 -0700568out:
Al Virob2648d52021-01-29 17:35:43 -0500569 inode_unlock(lower_dir);
David Howells2b0143b2015-03-17 22:25:59 +0000570 if (d_really_is_negative(dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700571 d_drop(dentry);
572 return rc;
573}
574
575static int
Christian Brauner549c7292021-01-21 14:19:43 +0100576ecryptfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
577 struct dentry *old_dentry, struct inode *new_dir,
578 struct dentry *new_dentry, unsigned int flags)
Michael Halcrow237fead2006-10-04 02:16:22 -0700579{
580 int rc;
581 struct dentry *lower_old_dentry;
582 struct dentry *lower_new_dentry;
583 struct dentry *lower_old_dir_dentry;
584 struct dentry *lower_new_dir_dentry;
Al Virobcf0d9d2019-11-03 12:07:15 -0500585 struct dentry *trap;
Tyler Hicks8335eaf2012-09-13 12:00:56 -0700586 struct inode *target_inode;
Christian Brauner9fe61452021-01-21 14:19:32 +0100587 struct renamedata rd = {};
Michael Halcrow237fead2006-10-04 02:16:22 -0700588
Miklos Szeredi1cd66c92016-09-27 11:03:58 +0200589 if (flags)
590 return -EINVAL;
591
Al Virobcf0d9d2019-11-03 12:07:15 -0500592 lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
593 lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
594
Michael Halcrow237fead2006-10-04 02:16:22 -0700595 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
596 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500597
David Howells2b0143b2015-03-17 22:25:59 +0000598 target_inode = d_inode(new_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500599
Erez Zadok0d132f72009-12-05 21:17:09 -0500600 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500601 dget(lower_new_dentry);
Al Viro74dd7c92018-10-09 23:32:41 -0400602 rc = -EINVAL;
603 if (lower_old_dentry->d_parent != lower_old_dir_dentry)
Erez Zadok0d132f72009-12-05 21:17:09 -0500604 goto out_lock;
Al Viro74dd7c92018-10-09 23:32:41 -0400605 if (lower_new_dentry->d_parent != lower_new_dir_dentry)
606 goto out_lock;
607 if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
608 goto out_lock;
609 /* source should not be ancestor of target */
610 if (trap == lower_old_dentry)
611 goto out_lock;
Erez Zadok0d132f72009-12-05 21:17:09 -0500612 /* target should not be ancestor of source */
613 if (trap == lower_new_dentry) {
614 rc = -ENOTEMPTY;
615 goto out_lock;
616 }
Christian Brauner9fe61452021-01-21 14:19:32 +0100617
Christian Brauner6521f892021-01-21 14:19:33 +0100618 rd.old_mnt_userns = &init_user_ns;
619 rd.old_dir = d_inode(lower_old_dir_dentry);
620 rd.old_dentry = lower_old_dentry;
621 rd.new_mnt_userns = &init_user_ns;
622 rd.new_dir = d_inode(lower_new_dir_dentry);
623 rd.new_dentry = lower_new_dentry;
Christian Brauner9fe61452021-01-21 14:19:32 +0100624 rc = vfs_rename(&rd);
Michael Halcrow237fead2006-10-04 02:16:22 -0700625 if (rc)
626 goto out_lock;
Tyler Hicks8335eaf2012-09-13 12:00:56 -0700627 if (target_inode)
628 fsstack_copy_attr_all(target_inode,
629 ecryptfs_inode_to_lower(target_inode));
David Howells2b0143b2015-03-17 22:25:59 +0000630 fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700631 if (new_dir != old_dir)
David Howells2b0143b2015-03-17 22:25:59 +0000632 fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700633out_lock:
Michael Halcrow237fead2006-10-04 02:16:22 -0700634 dput(lower_new_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500635 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700636 return rc;
637}
638
Al Virob22e8fe2013-11-29 22:51:47 -0500639static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
Michael Halcrow237fead2006-10-04 02:16:22 -0700640{
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100641 DEFINE_DELAYED_CALL(done);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500642 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100643 const char *link;
Al Virob22e8fe2013-11-29 22:51:47 -0500644 char *buf;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800645 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700646
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100647 link = vfs_get_link(lower_dentry, &done);
648 if (IS_ERR(link))
649 return ERR_CAST(link);
650
Al Virob22e8fe2013-11-29 22:51:47 -0500651 rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100652 link, strlen(link));
653 do_delayed_call(&done);
654 if (rc)
655 return ERR_PTR(rc);
656
657 return buf;
Tyler Hicks3a60a162010-03-22 00:41:35 -0500658}
659
Al Viro6b255392015-11-17 10:20:54 -0500660static const char *ecryptfs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500661 struct inode *inode,
662 struct delayed_call *done)
Michael Halcrow237fead2006-10-04 02:16:22 -0700663{
Al Virob22e8fe2013-11-29 22:51:47 -0500664 size_t len;
Al Viro6b255392015-11-17 10:20:54 -0500665 char *buf;
666
667 if (!dentry)
668 return ERR_PTR(-ECHILD);
669
670 buf = ecryptfs_readlink_lower(dentry, &len);
Al Virob22e8fe2013-11-29 22:51:47 -0500671 if (IS_ERR(buf))
Al Viro680baac2015-05-02 13:32:22 -0400672 return buf;
David Howells2b0143b2015-03-17 22:25:59 +0000673 fsstack_copy_attr_atime(d_inode(dentry),
674 d_inode(ecryptfs_dentry_to_lower(dentry)));
Al Viro408bd622012-05-03 09:34:20 -0400675 buf[len] = '\0';
Al Virofceef392015-12-29 15:58:39 -0500676 set_delayed_call(done, kfree_link, buf);
677 return buf;
Michael Halcrow237fead2006-10-04 02:16:22 -0700678}
679
Michael Halcrow237fead2006-10-04 02:16:22 -0700680/**
681 * upper_size_to_lower_size
682 * @crypt_stat: Crypt_stat associated with file
683 * @upper_size: Size of the upper file
684 *
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800685 * Calculate the required size of the lower file based on the
Michael Halcrow237fead2006-10-04 02:16:22 -0700686 * specified size of the upper file. This calculation is based on the
687 * number of headers in the underlying file and the extent size.
688 *
689 * Returns Calculated size of the lower file.
690 */
691static loff_t
692upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
693 loff_t upper_size)
694{
695 loff_t lower_size;
696
Tyler Hicks157f1072010-02-11 07:10:38 -0600697 lower_size = ecryptfs_lower_header_size(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700698 if (upper_size != 0) {
699 loff_t num_extents;
700
701 num_extents = upper_size >> crypt_stat->extent_shift;
702 if (upper_size & ~crypt_stat->extent_mask)
703 num_extents++;
704 lower_size += (num_extents * crypt_stat->extent_size);
705 }
706 return lower_size;
707}
708
709/**
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500710 * truncate_upper
Michael Halcrow237fead2006-10-04 02:16:22 -0700711 * @dentry: The ecryptfs layer dentry
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500712 * @ia: Address of the ecryptfs inode's attributes
713 * @lower_ia: Address of the lower inode's attributes
Michael Halcrow237fead2006-10-04 02:16:22 -0700714 *
715 * Function to handle truncations modifying the size of the file. Note
716 * that the file sizes are interpolated. When expanding, we are simply
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500717 * writing strings of 0's out. When truncating, we truncate the upper
718 * inode and update the lower_ia according to the page index
719 * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
720 * the caller must use lower_ia in a call to notify_change() to perform
721 * the truncation of the lower inode.
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 *
723 * Returns zero on success; non-zero otherwise
724 */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500725static int truncate_upper(struct dentry *dentry, struct iattr *ia,
726 struct iattr *lower_ia)
Michael Halcrow237fead2006-10-04 02:16:22 -0700727{
728 int rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +0000729 struct inode *inode = d_inode(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700730 struct ecryptfs_crypt_stat *crypt_stat;
731 loff_t i_size = i_size_read(inode);
732 loff_t lower_size_before_truncate;
733 loff_t lower_size_after_truncate;
734
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500735 if (unlikely((ia->ia_size == i_size))) {
736 lower_ia->ia_valid &= ~ATTR_SIZE;
Tyler Hicks332ab162011-04-14 15:35:11 -0500737 return 0;
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500738 }
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500739 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500740 if (rc)
741 return rc;
David Howells2b0143b2015-03-17 22:25:59 +0000742 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700743 /* Switch on growing or shrinking file */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500744 if (ia->ia_size > i_size) {
Michael Halcrow2ed92552007-10-16 01:28:10 -0700745 char zero[] = { 0x00 };
Michael Halcrow240e2df2007-06-27 14:09:44 -0700746
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500747 lower_ia->ia_valid &= ~ATTR_SIZE;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700748 /* Write a single 0 at the last position of the file;
749 * this triggers code that will fill in 0's throughout
750 * the intermediate portion of the previous end of the
751 * file and the new and of the file */
Al Viro48c1e442010-05-21 11:09:58 -0400752 rc = ecryptfs_write(inode, zero,
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500753 (ia->ia_size - 1), 1);
754 } else { /* ia->ia_size < i_size_read(inode) */
755 /* We're chopping off all the pages down to the page
756 * in which ia->ia_size is located. Fill in the end of
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300757 * that page from (ia->ia_size & ~PAGE_MASK) to
758 * PAGE_SIZE with zeros. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300759 size_t num_zeros = (PAGE_SIZE
760 - (ia->ia_size & ~PAGE_MASK));
Michael Halcrow2ed92552007-10-16 01:28:10 -0700761
Tyler Hicks13a791b2009-04-13 15:29:27 -0500762 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200763 truncate_setsize(inode, ia->ia_size);
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500764 lower_ia->ia_size = ia->ia_size;
765 lower_ia->ia_valid |= ATTR_SIZE;
Al Viro48c1e442010-05-21 11:09:58 -0400766 goto out;
Tyler Hicks13a791b2009-04-13 15:29:27 -0500767 }
Michael Halcrow2ed92552007-10-16 01:28:10 -0700768 if (num_zeros) {
769 char *zeros_virt;
770
771 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
772 if (!zeros_virt) {
773 rc = -ENOMEM;
Al Viro48c1e442010-05-21 11:09:58 -0400774 goto out;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700775 }
Al Viro48c1e442010-05-21 11:09:58 -0400776 rc = ecryptfs_write(inode, zeros_virt,
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500777 ia->ia_size, num_zeros);
Michael Halcrow2ed92552007-10-16 01:28:10 -0700778 kfree(zeros_virt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700779 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700780 printk(KERN_ERR "Error attempting to zero out "
781 "the remainder of the end page on "
782 "reducing truncate; rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400783 goto out;
Michael Halcrow240e2df2007-06-27 14:09:44 -0700784 }
785 }
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200786 truncate_setsize(inode, ia->ia_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700787 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800788 if (rc) {
789 printk(KERN_ERR "Problem with "
790 "ecryptfs_write_inode_size_to_metadata; "
791 "rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400792 goto out;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800793 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700794 /* We are reducing the size of the ecryptfs file, and need to
795 * know if we need to reduce the size of the lower file. */
796 lower_size_before_truncate =
797 upper_size_to_lower_size(crypt_stat, i_size);
798 lower_size_after_truncate =
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500799 upper_size_to_lower_size(crypt_stat, ia->ia_size);
800 if (lower_size_after_truncate < lower_size_before_truncate) {
801 lower_ia->ia_size = lower_size_after_truncate;
802 lower_ia->ia_valid |= ATTR_SIZE;
803 } else
804 lower_ia->ia_valid &= ~ATTR_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700805 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700806out:
Tyler Hicks332ab162011-04-14 15:35:11 -0500807 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700808 return rc;
809}
810
Tyler Hicksa261a032012-01-19 20:33:44 -0600811static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
812{
813 struct ecryptfs_crypt_stat *crypt_stat;
814 loff_t lower_oldsize, lower_newsize;
815
816 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
817 lower_oldsize = upper_size_to_lower_size(crypt_stat,
818 i_size_read(inode));
819 lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
820 if (lower_newsize > lower_oldsize) {
821 /*
822 * The eCryptfs inode and the new *lower* size are mixed here
823 * because we may not have the lower i_mutex held and/or it may
824 * not be appropriate to call inode_newsize_ok() with inodes
825 * from other filesystems.
826 */
827 return inode_newsize_ok(inode, lower_newsize);
828 }
829
830 return 0;
831}
832
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500833/**
834 * ecryptfs_truncate
835 * @dentry: The ecryptfs layer dentry
836 * @new_length: The length to expand the file to
837 *
838 * Simple function that handles the truncation of an eCryptfs inode and
839 * its corresponding lower inode.
840 *
841 * Returns zero on success; non-zero otherwise
842 */
843int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
844{
845 struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
846 struct iattr lower_ia = { .ia_valid = 0 };
847 int rc;
848
David Howells2b0143b2015-03-17 22:25:59 +0000849 rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
Tyler Hicksa261a032012-01-19 20:33:44 -0600850 if (rc)
851 return rc;
852
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500853 rc = truncate_upper(dentry, &ia, &lower_ia);
854 if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
855 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
856
Al Viro59551022016-01-22 15:40:57 -0500857 inode_lock(d_inode(lower_dentry));
Christian Brauner2f221d62021-01-21 14:19:26 +0100858 rc = notify_change(&init_user_ns, lower_dentry,
859 &lower_ia, NULL);
Al Viro59551022016-01-22 15:40:57 -0500860 inode_unlock(d_inode(lower_dentry));
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500861 }
862 return rc;
863}
864
Michael Halcrow237fead2006-10-04 02:16:22 -0700865static int
Christian Brauner549c7292021-01-21 14:19:43 +0100866ecryptfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
867 int mask)
Michael Halcrow237fead2006-10-04 02:16:22 -0700868{
Christian Brauner47291ba2021-01-21 14:19:24 +0100869 return inode_permission(&init_user_ns,
870 ecryptfs_inode_to_lower(inode), mask);
Michael Halcrow237fead2006-10-04 02:16:22 -0700871}
872
873/**
874 * ecryptfs_setattr
875 * @dentry: dentry handle to the inode to modify
876 * @ia: Structure with flags of what to change and values
877 *
878 * Updates the metadata of an inode. If the update is to the size
879 * i.e. truncation, then ecryptfs_truncate will handle the size modification
880 * of both the ecryptfs inode and the lower inode.
881 *
882 * All other metadata changes will be passed right to the lower filesystem,
883 * and we will just update our inode to look like the lower.
884 */
Christian Brauner549c7292021-01-21 14:19:43 +0100885static int ecryptfs_setattr(struct user_namespace *mnt_userns,
886 struct dentry *dentry, struct iattr *ia)
Michael Halcrow237fead2006-10-04 02:16:22 -0700887{
888 int rc = 0;
889 struct dentry *lower_dentry;
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500890 struct iattr lower_ia;
Michael Halcrow237fead2006-10-04 02:16:22 -0700891 struct inode *inode;
892 struct inode *lower_inode;
893 struct ecryptfs_crypt_stat *crypt_stat;
894
David Howells2b0143b2015-03-17 22:25:59 +0000895 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
Herbert Xue81f3342016-04-16 15:01:09 +0800896 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
897 rc = ecryptfs_init_crypt_stat(crypt_stat);
898 if (rc)
899 return rc;
900 }
David Howells2b0143b2015-03-17 22:25:59 +0000901 inode = d_inode(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700902 lower_inode = ecryptfs_inode_to_lower(inode);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700903 lower_dentry = ecryptfs_dentry_to_lower(dentry);
904 mutex_lock(&crypt_stat->cs_mutex);
David Howellse36cb0b2015-01-29 12:02:35 +0000905 if (d_is_dir(dentry))
Michael Halcrowe10f2812007-06-27 14:09:44 -0700906 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
David Howellse36cb0b2015-01-29 12:02:35 +0000907 else if (d_is_reg(dentry)
Michael Halcrow64ee4802007-07-19 01:47:54 -0700908 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
909 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700910 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -0700911
Michael Halcrowe10f2812007-06-27 14:09:44 -0700912 mount_crypt_stat = &ecryptfs_superblock_to_private(
913 dentry->d_sb)->mount_crypt_stat;
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500914 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500915 if (rc) {
916 mutex_unlock(&crypt_stat->cs_mutex);
917 goto out;
918 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700919 rc = ecryptfs_read_metadata(dentry);
Tyler Hicks332ab162011-04-14 15:35:11 -0500920 ecryptfs_put_lower_file(inode);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700921 if (rc) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700922 if (!(mount_crypt_stat->flags
923 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
924 rc = -EIO;
Michael Halcrow25bd8172008-02-06 01:38:35 -0800925 printk(KERN_WARNING "Either the lower file "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700926 "is not in a valid eCryptfs format, "
Michael Halcrow25bd8172008-02-06 01:38:35 -0800927 "or the key could not be retrieved. "
928 "Plaintext passthrough mode is not "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700929 "enabled; returning -EIO\n");
Michael Halcrowe10f2812007-06-27 14:09:44 -0700930 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700931 goto out;
932 }
933 rc = 0;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -0500934 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
935 | ECRYPTFS_ENCRYPTED);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700936 }
Michael Halcrowe10f2812007-06-27 14:09:44 -0700937 }
938 mutex_unlock(&crypt_stat->cs_mutex);
Tyler Hicksa261a032012-01-19 20:33:44 -0600939
Christian Brauner2f221d62021-01-21 14:19:26 +0100940 rc = setattr_prepare(&init_user_ns, dentry, ia);
Tyler Hicksa261a032012-01-19 20:33:44 -0600941 if (rc)
942 goto out;
943 if (ia->ia_valid & ATTR_SIZE) {
944 rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
945 if (rc)
946 goto out;
947 }
948
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500949 memcpy(&lower_ia, ia, sizeof(lower_ia));
950 if (ia->ia_valid & ATTR_FILE)
951 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
Michael Halcrow237fead2006-10-04 02:16:22 -0700952 if (ia->ia_valid & ATTR_SIZE) {
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500953 rc = truncate_upper(dentry, ia, &lower_ia);
Michael Halcrow237fead2006-10-04 02:16:22 -0700954 if (rc < 0)
955 goto out;
956 }
Jeff Layton1ac564e2007-10-18 03:05:17 -0700957
958 /*
959 * mode change is for clearing setuid/setgid bits. Allow lower fs
960 * to interpret this in its own way.
961 */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500962 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
963 lower_ia.ia_valid &= ~ATTR_MODE;
Jeff Layton1ac564e2007-10-18 03:05:17 -0700964
Al Viro59551022016-01-22 15:40:57 -0500965 inode_lock(d_inode(lower_dentry));
Christian Brauner2f221d62021-01-21 14:19:26 +0100966 rc = notify_change(&init_user_ns, lower_dentry, &lower_ia, NULL);
Al Viro59551022016-01-22 15:40:57 -0500967 inode_unlock(d_inode(lower_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700968out:
Erez Zadok9afa2fb2009-12-02 19:51:54 -0500969 fsstack_copy_attr_all(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700970 return rc;
971}
972
Christian Brauner549c7292021-01-21 14:19:43 +0100973static int ecryptfs_getattr_link(struct user_namespace *mnt_userns,
974 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +0000975 u32 request_mask, unsigned int flags)
Tyler Hicks3a60a162010-03-22 00:41:35 -0500976{
David Howellsa528d352017-01-31 16:46:22 +0000977 struct dentry *dentry = path->dentry;
Tyler Hicks3a60a162010-03-22 00:41:35 -0500978 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
979 int rc = 0;
980
981 mount_crypt_stat = &ecryptfs_superblock_to_private(
982 dentry->d_sb)->mount_crypt_stat;
Christian Brauner0d56a452021-01-21 14:19:30 +0100983 generic_fillattr(&init_user_ns, d_inode(dentry), stat);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500984 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
985 char *target;
986 size_t targetsiz;
987
Al Virob22e8fe2013-11-29 22:51:47 -0500988 target = ecryptfs_readlink_lower(dentry, &targetsiz);
989 if (!IS_ERR(target)) {
Tyler Hicks3a60a162010-03-22 00:41:35 -0500990 kfree(target);
991 stat->size = targetsiz;
Al Virob22e8fe2013-11-29 22:51:47 -0500992 } else {
993 rc = PTR_ERR(target);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500994 }
995 }
996 return rc;
997}
998
Christian Brauner549c7292021-01-21 14:19:43 +0100999static int ecryptfs_getattr(struct user_namespace *mnt_userns,
1000 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00001001 u32 request_mask, unsigned int flags)
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001002{
David Howellsa528d352017-01-31 16:46:22 +00001003 struct dentry *dentry = path->dentry;
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001004 struct kstat lower_stat;
1005 int rc;
1006
David Howellsa528d352017-01-31 16:46:22 +00001007 rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
1008 request_mask, flags);
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001009 if (!rc) {
David Howells2b0143b2015-03-17 22:25:59 +00001010 fsstack_copy_attr_all(d_inode(dentry),
1011 ecryptfs_inode_to_lower(d_inode(dentry)));
Christian Brauner0d56a452021-01-21 14:19:30 +01001012 generic_fillattr(&init_user_ns, d_inode(dentry), stat);
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001013 stat->blocks = lower_stat.blocks;
1014 }
1015 return rc;
1016}
1017
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001018int
Al Viro3767e252016-05-27 11:06:05 -04001019ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
1020 const char *name, const void *value,
Michael Halcrow237fead2006-10-04 02:16:22 -07001021 size_t size, int flags)
1022{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001023 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -07001024 struct dentry *lower_dentry;
Miklos Szeredi0b964442021-01-19 17:22:03 +01001025 struct inode *lower_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001026
1027 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Miklos Szeredi0b964442021-01-19 17:22:03 +01001028 lower_inode = d_inode(lower_dentry);
1029 if (!(lower_inode->i_opflags & IOP_XATTR)) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001030 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001031 goto out;
1032 }
Miklos Szeredi0b964442021-01-19 17:22:03 +01001033 inode_lock(lower_inode);
Linus Torvalds7d6beb72021-02-23 13:39:45 -08001034 rc = __vfs_setxattr_locked(&init_user_ns, lower_dentry, name, value, size, flags, NULL);
Miklos Szeredi0b964442021-01-19 17:22:03 +01001035 inode_unlock(lower_inode);
Al Viro3767e252016-05-27 11:06:05 -04001036 if (!rc && inode)
Miklos Szeredi0b964442021-01-19 17:22:03 +01001037 fsstack_copy_attr_all(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001038out:
1039 return rc;
1040}
1041
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001042ssize_t
Al Viroce23e642016-04-11 00:48:00 -04001043ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
1044 const char *name, void *value, size_t size)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001045{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001046 int rc;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001047
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001048 if (!(lower_inode->i_opflags & IOP_XATTR)) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001049 rc = -EOPNOTSUPP;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001050 goto out;
1051 }
Al Viroce23e642016-04-11 00:48:00 -04001052 inode_lock(lower_inode);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001053 rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
Al Viroce23e642016-04-11 00:48:00 -04001054 inode_unlock(lower_inode);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001055out:
1056 return rc;
1057}
1058
Adrian Bunk7896b632008-02-06 01:38:32 -08001059static ssize_t
Al Viroce23e642016-04-11 00:48:00 -04001060ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
1061 const char *name, void *value, size_t size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001062{
Al Viroce23e642016-04-11 00:48:00 -04001063 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1064 ecryptfs_inode_to_lower(inode),
1065 name, value, size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001066}
1067
1068static ssize_t
1069ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1070{
1071 int rc = 0;
1072 struct dentry *lower_dentry;
1073
1074 lower_dentry = ecryptfs_dentry_to_lower(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001075 if (!d_inode(lower_dentry)->i_op->listxattr) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001076 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001077 goto out;
1078 }
Al Viro59551022016-01-22 15:40:57 -05001079 inode_lock(d_inode(lower_dentry));
David Howells2b0143b2015-03-17 22:25:59 +00001080 rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
Al Viro59551022016-01-22 15:40:57 -05001081 inode_unlock(d_inode(lower_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -07001082out:
1083 return rc;
1084}
1085
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001086static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
1087 const char *name)
Michael Halcrow237fead2006-10-04 02:16:22 -07001088{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001089 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -07001090 struct dentry *lower_dentry;
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001091 struct inode *lower_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001092
1093 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001094 lower_inode = ecryptfs_inode_to_lower(inode);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001095 if (!(lower_inode->i_opflags & IOP_XATTR)) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001096 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001097 goto out;
1098 }
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001099 inode_lock(lower_inode);
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01001100 rc = __vfs_removexattr(&init_user_ns, lower_dentry, name);
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001101 inode_unlock(lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001102out:
1103 return rc;
1104}
1105
Arjan van de Ven754661f2007-02-12 00:55:38 -08001106const struct inode_operations ecryptfs_symlink_iops = {
Al Viro6b255392015-11-17 10:20:54 -05001107 .get_link = ecryptfs_get_link,
Michael Halcrow237fead2006-10-04 02:16:22 -07001108 .permission = ecryptfs_permission,
1109 .setattr = ecryptfs_setattr,
Tyler Hicks3a60a162010-03-22 00:41:35 -05001110 .getattr = ecryptfs_getattr_link,
Michael Halcrow237fead2006-10-04 02:16:22 -07001111 .listxattr = ecryptfs_listxattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001112};
1113
Arjan van de Ven754661f2007-02-12 00:55:38 -08001114const struct inode_operations ecryptfs_dir_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001115 .create = ecryptfs_create,
1116 .lookup = ecryptfs_lookup,
1117 .link = ecryptfs_link,
1118 .unlink = ecryptfs_unlink,
1119 .symlink = ecryptfs_symlink,
1120 .mkdir = ecryptfs_mkdir,
1121 .rmdir = ecryptfs_rmdir,
1122 .mknod = ecryptfs_mknod,
1123 .rename = ecryptfs_rename,
1124 .permission = ecryptfs_permission,
1125 .setattr = ecryptfs_setattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001126 .listxattr = ecryptfs_listxattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001127};
1128
Arjan van de Ven754661f2007-02-12 00:55:38 -08001129const struct inode_operations ecryptfs_main_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001130 .permission = ecryptfs_permission,
1131 .setattr = ecryptfs_setattr,
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001132 .getattr = ecryptfs_getattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001133 .listxattr = ecryptfs_listxattr,
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001134};
1135
1136static int ecryptfs_xattr_get(const struct xattr_handler *handler,
1137 struct dentry *dentry, struct inode *inode,
1138 const char *name, void *buffer, size_t size)
1139{
1140 return ecryptfs_getxattr(dentry, inode, name, buffer, size);
1141}
1142
1143static int ecryptfs_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01001144 struct user_namespace *mnt_userns,
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001145 struct dentry *dentry, struct inode *inode,
1146 const char *name, const void *value, size_t size,
1147 int flags)
1148{
1149 if (value)
1150 return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
1151 else {
1152 BUG_ON(flags != XATTR_REPLACE);
1153 return ecryptfs_removexattr(dentry, inode, name);
1154 }
1155}
1156
YueHaibingc0360612019-06-14 23:51:17 +08001157static const struct xattr_handler ecryptfs_xattr_handler = {
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001158 .prefix = "", /* match anything */
1159 .get = ecryptfs_xattr_get,
1160 .set = ecryptfs_xattr_set,
1161};
1162
1163const struct xattr_handler *ecryptfs_xattr_handlers[] = {
1164 &ecryptfs_xattr_handler,
1165 NULL
Michael Halcrow237fead2006-10-04 02:16:22 -07001166};