blob: a10e948d24fab3f5f08403efaac6ed351697b2b8 [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
Al Viro0f7ff2d2015-12-06 12:31:07 -050016static int ovl_copy_up_truncate(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020017{
18 int err;
19 struct dentry *parent;
20 struct kstat stat;
21 struct path lowerpath;
Vivek Goyal8eac98b2016-09-06 13:40:32 -040022 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020023
24 parent = dget_parent(dentry);
25 err = ovl_copy_up(parent);
26 if (err)
27 goto out_dput_parent;
28
29 ovl_path_lower(dentry, &lowerpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020030
Vivek Goyal8eac98b2016-09-06 13:40:32 -040031 old_cred = ovl_override_creds(dentry->d_sb);
32 err = vfs_getattr(&lowerpath, &stat);
33 if (!err) {
34 stat.size = 0;
35 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
36 }
37 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020038
39out_dput_parent:
40 dput(parent);
41 return err;
42}
43
44int ovl_setattr(struct dentry *dentry, struct iattr *attr)
45{
46 int err;
47 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040048 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049
Miklos Szeredicf9a6782015-12-11 16:30:49 +010050 /*
51 * Check for permissions before trying to copy-up. This is redundant
52 * since it will be rechecked later by ->setattr() on upper dentry. But
53 * without this, copy-up can be triggered by just about anybody.
54 *
55 * We don't initialize inode->size, which just means that
56 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
57 * check for a swapfile (which this won't be anyway).
58 */
Jan Kara31051c82016-05-26 16:55:18 +020059 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010060 if (err)
61 return err;
62
Miklos Szeredie9be9d52014-10-24 00:14:38 +020063 err = ovl_want_write(dentry);
64 if (err)
65 goto out;
66
Miklos Szerediacff81e2015-12-04 19:18:48 +010067 err = ovl_copy_up(dentry);
68 if (!err) {
69 upperdentry = ovl_dentry_upper(dentry);
70
Miklos Szeredib99c2d92016-07-04 16:49:48 +020071 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
72 attr->ia_valid &= ~ATTR_MODE;
73
Al Viro59551022016-01-22 15:40:57 -050074 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040075 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020076 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040077 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030078 if (!err)
79 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050080 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020081 }
82 ovl_drop_write(dentry);
83out:
84 return err;
85}
86
87static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
88 struct kstat *stat)
89{
90 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040091 const struct cred *old_cred;
92 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020093
94 ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040095 old_cred = ovl_override_creds(dentry->d_sb);
96 err = vfs_getattr(&realpath, stat);
97 revert_creds(old_cred);
98 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020099}
100
101int ovl_permission(struct inode *inode, int mask)
102{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200103 bool is_upper;
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200104 struct inode *realinode = ovl_inode_real(inode, &is_upper);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400105 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200106 int err;
107
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200108 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200109 if (!realinode) {
110 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200111 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200112 }
113
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400114 /*
115 * Check overlay inode with the creds of task and underlying inode
116 * with creds of mounter
117 */
118 err = generic_permission(inode, mask);
119 if (err)
120 return err;
121
122 old_cred = ovl_override_creds(inode->i_sb);
Vivek Goyal500cac32016-07-13 11:00:14 -0400123 if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400124 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400125 /* Make sure mounter can read file for copy up later */
126 mask |= MAY_READ;
127 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200128 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400129 revert_creds(old_cred);
130
131 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132}
133
Al Viro6b255392015-11-17 10:20:54 -0500134static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500135 struct inode *inode,
136 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200137{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400138 const struct cred *old_cred;
139 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140
Al Viro6b255392015-11-17 10:20:54 -0500141 if (!dentry)
142 return ERR_PTR(-ECHILD);
143
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400144 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200145 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400146 revert_creds(old_cred);
147 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148}
149
Miklos Szeredi09562542016-08-08 15:08:49 +0200150bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200151{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200152 return strncmp(name, OVL_XATTR_PREFIX,
153 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154}
155
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200156int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
157 size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158{
159 int err;
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200160 struct path realpath;
161 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400162 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163
164 err = ovl_want_write(dentry);
165 if (err)
166 goto out;
167
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200168 if (!value && !OVL_TYPE_UPPER(type)) {
169 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
170 if (err < 0)
171 goto out_drop_write;
172 }
173
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174 err = ovl_copy_up(dentry);
175 if (err)
176 goto out_drop_write;
177
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200178 if (!OVL_TYPE_UPPER(type))
179 ovl_path_upper(dentry, &realpath);
180
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400181 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200182 if (value)
183 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
184 else {
185 WARN_ON(flags != XATTR_REPLACE);
186 err = vfs_removexattr(realpath.dentry, name);
187 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400188 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200189
190out_drop_write:
191 ovl_drop_write(dentry);
192out:
193 return err;
194}
195
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200196int ovl_xattr_get(struct dentry *dentry, const char *name,
197 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200198{
Miklos Szeredib5817552016-06-06 16:21:37 +0200199 struct dentry *realdentry = ovl_dentry_real(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400200 ssize_t res;
201 const struct cred *old_cred;
Miklos Szeredi52148462014-11-20 16:40:00 +0100202
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400203 old_cred = ovl_override_creds(dentry->d_sb);
204 res = vfs_getxattr(realdentry, name, value, size);
205 revert_creds(old_cred);
206 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207}
208
209ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
210{
Miklos Szeredib5817552016-06-06 16:21:37 +0200211 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200213 size_t len;
214 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400215 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200216
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400217 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200218 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400219 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200220 if (res <= 0 || size == 0)
221 return res;
222
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200224 for (s = list, len = res; len;) {
225 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200226
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200227 /* underlying fs providing us with an broken xattr list? */
228 if (WARN_ON(slen > len))
229 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200230
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200231 len -= slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200232 if (ovl_is_private_xattr(s)) {
233 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200234 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200235 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200236 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200237 }
238 }
239
240 return res;
241}
242
Vivek Goyal39a25b22016-07-01 16:34:26 -0400243struct posix_acl *ovl_get_acl(struct inode *inode, int type)
244{
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200245 struct inode *realinode = ovl_inode_real(inode, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400246 const struct cred *old_cred;
247 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400248
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200249 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400250 return NULL;
251
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400252 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200253 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400254 revert_creds(old_cred);
255
256 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400257}
258
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200259static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
260 struct dentry *realdentry)
261{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100262 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200263 return false;
264
265 if (special_file(realdentry->d_inode->i_mode))
266 return false;
267
268 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
269 return false;
270
271 return true;
272}
273
Miklos Szeredi2d902672016-06-30 08:53:27 +0200274int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200275{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200276 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200277 struct path realpath;
278 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279
280 type = ovl_path_real(dentry, &realpath);
David Howells4bacc9c2015-06-18 14:32:31 +0100281 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200283 if (!err) {
284 if (file_flags & O_TRUNC)
285 err = ovl_copy_up_truncate(dentry);
286 else
287 err = ovl_copy_up(dentry);
288 ovl_drop_write(dentry);
289 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200290 }
291
Miklos Szeredi2d902672016-06-30 08:53:27 +0200292 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200293}
294
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200295int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
296{
297 struct dentry *alias;
298 struct path upperpath;
299
300 if (!(flags & S_ATIME))
301 return 0;
302
303 alias = d_find_any_alias(inode);
304 if (!alias)
305 return 0;
306
307 ovl_path_upper(alias, &upperpath);
308 if (upperpath.dentry) {
309 touch_atime(&upperpath);
310 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
311 }
312
313 dput(alias);
314
315 return 0;
316}
317
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318static const struct inode_operations ovl_file_inode_operations = {
319 .setattr = ovl_setattr,
320 .permission = ovl_permission,
321 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400323 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200324 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325};
326
327static const struct inode_operations ovl_symlink_inode_operations = {
328 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500329 .get_link = ovl_get_link,
Miklos Szeredi78a3fa42016-10-04 14:40:44 +0200330 .readlink = generic_readlink,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200332 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200333 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334};
335
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100336static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 inode->i_ino = get_next_ino();
339 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200340 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200341#ifdef CONFIG_FS_POSIX_ACL
342 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
343#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200344
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100345 switch (mode & S_IFMT) {
346 case S_IFREG:
347 inode->i_op = &ovl_file_inode_operations;
348 break;
349
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351 inode->i_op = &ovl_dir_inode_operations;
352 inode->i_fop = &ovl_dir_operations;
353 break;
354
355 case S_IFLNK:
356 inode->i_op = &ovl_symlink_inode_operations;
357 break;
358
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200359 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100361 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200363 }
364}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200365
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100366struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200367{
368 struct inode *inode;
369
370 inode = new_inode(sb);
371 if (inode)
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100372 ovl_fill_inode(inode, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200373
374 return inode;
375}
376
377static int ovl_inode_test(struct inode *inode, void *data)
378{
379 return ovl_inode_real(inode, NULL) == data;
380}
381
382static int ovl_inode_set(struct inode *inode, void *data)
383{
384 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
385 return 0;
386}
387
388struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
389
390{
391 struct inode *inode;
392
393 inode = iget5_locked(sb, (unsigned long) realinode,
394 ovl_inode_test, ovl_inode_set, realinode);
395 if (inode && inode->i_state & I_NEW) {
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100396 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200397 set_nlink(inode, realinode->i_nlink);
398 unlock_new_inode(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200399 }
400
401 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200402}