blob: d4bb54f7b6b42abaa417f30aeca3f6c7e069ec00 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
11#include <linux/slab.h>
12#include <linux/xattr.h>
Miklos Szeredi5201dc42016-09-01 11:11:59 +020013#include <linux/posix_acl.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020014#include "overlayfs.h"
15
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016int ovl_setattr(struct dentry *dentry, struct iattr *attr)
17{
18 int err;
19 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040020 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021
Miklos Szeredicf9a6782015-12-11 16:30:49 +010022 /*
23 * Check for permissions before trying to copy-up. This is redundant
24 * since it will be rechecked later by ->setattr() on upper dentry. But
25 * without this, copy-up can be triggered by just about anybody.
26 *
27 * We don't initialize inode->size, which just means that
28 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
29 * check for a swapfile (which this won't be anyway).
30 */
Jan Kara31051c82016-05-26 16:55:18 +020031 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010032 if (err)
33 return err;
34
Miklos Szeredie9be9d52014-10-24 00:14:38 +020035 err = ovl_want_write(dentry);
36 if (err)
37 goto out;
38
Miklos Szerediacff81e2015-12-04 19:18:48 +010039 err = ovl_copy_up(dentry);
40 if (!err) {
41 upperdentry = ovl_dentry_upper(dentry);
42
Miklos Szeredib99c2d92016-07-04 16:49:48 +020043 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
44 attr->ia_valid &= ~ATTR_MODE;
45
Al Viro59551022016-01-22 15:40:57 -050046 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040047 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020048 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040049 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030050 if (!err)
51 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050052 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020053 }
54 ovl_drop_write(dentry);
55out:
56 return err;
57}
58
David Howellsa528d352017-01-31 16:46:22 +000059static int ovl_getattr(const struct path *path, struct kstat *stat,
60 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020061{
David Howellsa528d352017-01-31 16:46:22 +000062 struct dentry *dentry = path->dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020063 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040064 const struct cred *old_cred;
65 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020066
67 ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040068 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +000069 err = vfs_getattr(&realpath, stat, request_mask, flags);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040070 revert_creds(old_cred);
71 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020072}
73
74int ovl_permission(struct inode *inode, int mask)
75{
Miklos Szeredie9be9d52014-10-24 00:14:38 +020076 bool is_upper;
Miklos Szeredi39b681f2016-07-29 12:05:24 +020077 struct inode *realinode = ovl_inode_real(inode, &is_upper);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -040078 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020079 int err;
80
Miklos Szeredie9be9d52014-10-24 00:14:38 +020081 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +020082 if (!realinode) {
83 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +020084 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020085 }
86
Vivek Goyalc0ca3d72016-07-01 16:34:27 -040087 /*
88 * Check overlay inode with the creds of task and underlying inode
89 * with creds of mounter
90 */
91 err = generic_permission(inode, mask);
92 if (err)
93 return err;
94
95 old_cred = ovl_override_creds(inode->i_sb);
Vivek Goyal500cac32016-07-13 11:00:14 -040096 if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -040097 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -040098 /* Make sure mounter can read file for copy up later */
99 mask |= MAY_READ;
100 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200101 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400102 revert_creds(old_cred);
103
104 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200105}
106
Al Viro6b255392015-11-17 10:20:54 -0500107static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500108 struct inode *inode,
109 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400111 const struct cred *old_cred;
112 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200113
Al Viro6b255392015-11-17 10:20:54 -0500114 if (!dentry)
115 return ERR_PTR(-ECHILD);
116
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400117 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200118 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400119 revert_creds(old_cred);
120 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200121}
122
Miklos Szeredi09562542016-08-08 15:08:49 +0200123bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200124{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200125 return strncmp(name, OVL_XATTR_PREFIX,
126 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200127}
128
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200129int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
130 size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200131{
132 int err;
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200133 struct path realpath;
134 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400135 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200136
137 err = ovl_want_write(dentry);
138 if (err)
139 goto out;
140
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200141 if (!value && !OVL_TYPE_UPPER(type)) {
142 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
143 if (err < 0)
144 goto out_drop_write;
145 }
146
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147 err = ovl_copy_up(dentry);
148 if (err)
149 goto out_drop_write;
150
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200151 if (!OVL_TYPE_UPPER(type))
152 ovl_path_upper(dentry, &realpath);
153
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400154 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200155 if (value)
156 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
157 else {
158 WARN_ON(flags != XATTR_REPLACE);
159 err = vfs_removexattr(realpath.dentry, name);
160 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400161 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200162
163out_drop_write:
164 ovl_drop_write(dentry);
165out:
166 return err;
167}
168
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200169int ovl_xattr_get(struct dentry *dentry, const char *name,
170 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200171{
Miklos Szeredib5817552016-06-06 16:21:37 +0200172 struct dentry *realdentry = ovl_dentry_real(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400173 ssize_t res;
174 const struct cred *old_cred;
Miklos Szeredi52148462014-11-20 16:40:00 +0100175
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400176 old_cred = ovl_override_creds(dentry->d_sb);
177 res = vfs_getxattr(realdentry, name, value, size);
178 revert_creds(old_cred);
179 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200180}
181
182ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
183{
Miklos Szeredib5817552016-06-06 16:21:37 +0200184 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200185 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200186 size_t len;
187 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400188 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200189
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400190 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200191 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400192 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193 if (res <= 0 || size == 0)
194 return res;
195
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200197 for (s = list, len = res; len;) {
198 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200200 /* underlying fs providing us with an broken xattr list? */
201 if (WARN_ON(slen > len))
202 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200204 len -= slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205 if (ovl_is_private_xattr(s)) {
206 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200207 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200208 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200209 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210 }
211 }
212
213 return res;
214}
215
Vivek Goyal39a25b22016-07-01 16:34:26 -0400216struct posix_acl *ovl_get_acl(struct inode *inode, int type)
217{
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200218 struct inode *realinode = ovl_inode_real(inode, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400219 const struct cred *old_cred;
220 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400221
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200222 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400223 return NULL;
224
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400225 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200226 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400227 revert_creds(old_cred);
228
229 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400230}
231
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200232static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
233 struct dentry *realdentry)
234{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100235 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236 return false;
237
238 if (special_file(realdentry->d_inode->i_mode))
239 return false;
240
241 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
242 return false;
243
244 return true;
245}
246
Miklos Szeredi2d902672016-06-30 08:53:27 +0200247int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200248{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200249 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200250 struct path realpath;
251 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200252
253 type = ovl_path_real(dentry, &realpath);
David Howells4bacc9c2015-06-18 14:32:31 +0100254 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200255 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200256 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200257 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200258 ovl_drop_write(dentry);
259 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200260 }
261
Miklos Szeredi2d902672016-06-30 08:53:27 +0200262 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200263}
264
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200265int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
266{
267 struct dentry *alias;
268 struct path upperpath;
269
270 if (!(flags & S_ATIME))
271 return 0;
272
273 alias = d_find_any_alias(inode);
274 if (!alias)
275 return 0;
276
277 ovl_path_upper(alias, &upperpath);
278 if (upperpath.dentry) {
279 touch_atime(&upperpath);
280 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
281 }
282
283 dput(alias);
284
285 return 0;
286}
287
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288static const struct inode_operations ovl_file_inode_operations = {
289 .setattr = ovl_setattr,
290 .permission = ovl_permission,
291 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400293 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200294 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200295};
296
297static const struct inode_operations ovl_symlink_inode_operations = {
298 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500299 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200302 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200303};
304
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100305static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200306{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307 inode->i_ino = get_next_ino();
308 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200309 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200310#ifdef CONFIG_FS_POSIX_ACL
311 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
312#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200313
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100314 switch (mode & S_IFMT) {
315 case S_IFREG:
316 inode->i_op = &ovl_file_inode_operations;
317 break;
318
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200319 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 inode->i_op = &ovl_dir_inode_operations;
321 inode->i_fop = &ovl_dir_operations;
322 break;
323
324 case S_IFLNK:
325 inode->i_op = &ovl_symlink_inode_operations;
326 break;
327
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200328 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200329 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100330 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200332 }
333}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100335struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200336{
337 struct inode *inode;
338
339 inode = new_inode(sb);
340 if (inode)
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100341 ovl_fill_inode(inode, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200342
343 return inode;
344}
345
346static int ovl_inode_test(struct inode *inode, void *data)
347{
348 return ovl_inode_real(inode, NULL) == data;
349}
350
351static int ovl_inode_set(struct inode *inode, void *data)
352{
353 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
354 return 0;
355}
356
357struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
358
359{
360 struct inode *inode;
361
362 inode = iget5_locked(sb, (unsigned long) realinode,
363 ovl_inode_test, ovl_inode_set, realinode);
364 if (inode && inode->i_state & I_NEW) {
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100365 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200366 set_nlink(inode, realinode->i_nlink);
367 unlock_new_inode(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200368 }
369
370 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371}