blob: f84492ff505d9063d679aa8c897b4fbb191a2f14 [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>
13#include "overlayfs.h"
14
Al Viro0f7ff2d2015-12-06 12:31:07 -050015static int ovl_copy_up_truncate(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016{
17 int err;
18 struct dentry *parent;
19 struct kstat stat;
20 struct path lowerpath;
21
22 parent = dget_parent(dentry);
23 err = ovl_copy_up(parent);
24 if (err)
25 goto out_dput_parent;
26
27 ovl_path_lower(dentry, &lowerpath);
28 err = vfs_getattr(&lowerpath, &stat);
29 if (err)
30 goto out_dput_parent;
31
Al Viro0f7ff2d2015-12-06 12:31:07 -050032 stat.size = 0;
33 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020034
35out_dput_parent:
36 dput(parent);
37 return err;
38}
39
40int ovl_setattr(struct dentry *dentry, struct iattr *attr)
41{
42 int err;
43 struct dentry *upperdentry;
44
Miklos Szeredicf9a6782015-12-11 16:30:49 +010045 /*
46 * Check for permissions before trying to copy-up. This is redundant
47 * since it will be rechecked later by ->setattr() on upper dentry. But
48 * without this, copy-up can be triggered by just about anybody.
49 *
50 * We don't initialize inode->size, which just means that
51 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
52 * check for a swapfile (which this won't be anyway).
53 */
54 err = inode_change_ok(dentry->d_inode, attr);
55 if (err)
56 return err;
57
Miklos Szeredie9be9d52014-10-24 00:14:38 +020058 err = ovl_want_write(dentry);
59 if (err)
60 goto out;
61
Miklos Szeredi03bea602016-06-29 16:03:55 +020062 if (attr->ia_valid & ATTR_SIZE) {
63 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
64
65 err = -ETXTBSY;
66 if (atomic_read(&realinode->i_writecount) < 0)
67 goto out_drop_write;
68 }
69
Miklos Szerediacff81e2015-12-04 19:18:48 +010070 err = ovl_copy_up(dentry);
71 if (!err) {
Miklos Szeredi03bea602016-06-29 16:03:55 +020072 struct inode *winode = NULL;
73
Miklos Szerediacff81e2015-12-04 19:18:48 +010074 upperdentry = ovl_dentry_upper(dentry);
75
Miklos Szeredi03bea602016-06-29 16:03:55 +020076 if (attr->ia_valid & ATTR_SIZE) {
77 winode = d_inode(upperdentry);
78 err = get_write_access(winode);
79 if (err)
80 goto out_drop_write;
81 }
82
Miklos Szeredib99c2d92016-07-04 16:49:48 +020083 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
84 attr->ia_valid &= ~ATTR_MODE;
85
Al Viro59551022016-01-22 15:40:57 -050086 inode_lock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020087 err = notify_change(upperdentry, attr, NULL);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030088 if (!err)
89 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050090 inode_unlock(upperdentry->d_inode);
Miklos Szeredi03bea602016-06-29 16:03:55 +020091
92 if (winode)
93 put_write_access(winode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020094 }
Miklos Szeredi03bea602016-06-29 16:03:55 +020095out_drop_write:
Miklos Szeredie9be9d52014-10-24 00:14:38 +020096 ovl_drop_write(dentry);
97out:
98 return err;
99}
100
101static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
102 struct kstat *stat)
103{
104 struct path realpath;
105
106 ovl_path_real(dentry, &realpath);
107 return vfs_getattr(&realpath, stat);
108}
109
110int ovl_permission(struct inode *inode, int mask)
111{
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200112 struct ovl_entry *oe = inode->i_private;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200113 bool is_upper;
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200114 struct dentry *realdentry = ovl_entry_real(oe, &is_upper);
115 struct inode *realinode;
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400116 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200117 int err;
118
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200119 if (ovl_is_default_permissions(inode)) {
120 struct kstat stat;
121 struct path realpath = { .dentry = realdentry };
122
123 if (mask & MAY_NOT_BLOCK)
124 return -ECHILD;
125
126 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
127
128 err = vfs_getattr(&realpath, &stat);
129 if (err)
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200130 return err;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200131
132 if ((stat.mode ^ inode->i_mode) & S_IFMT)
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200133 return -ESTALE;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200134
135 inode->i_mode = stat.mode;
136 inode->i_uid = stat.uid;
137 inode->i_gid = stat.gid;
138
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200139 return generic_permission(inode, mask);
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200140 }
141
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142 /* Careful in RCU walk mode */
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200143 realinode = d_inode_rcu(realdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200144 if (!realinode) {
145 WARN_ON(!(mask & MAY_NOT_BLOCK));
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200146 return -ENOENT;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147 }
148
149 if (mask & MAY_WRITE) {
150 umode_t mode = realinode->i_mode;
151
152 /*
153 * Writes will always be redirected to upper layer, so
154 * ignore lower layer being read-only.
155 *
156 * If the overlay itself is read-only then proceed
157 * with the permission check, don't return EROFS.
158 * This will only happen if this is the lower layer of
159 * another overlayfs.
160 *
161 * If upper fs becomes read-only after the overlay was
162 * constructed return EROFS to prevent modification of
163 * upper layer.
164 */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200165 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
166 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200167 return -EROFS;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200168 }
169
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400170 /*
171 * Check overlay inode with the creds of task and underlying inode
172 * with creds of mounter
173 */
174 err = generic_permission(inode, mask);
175 if (err)
176 return err;
177
178 old_cred = ovl_override_creds(inode->i_sb);
179 err = __inode_permission(realinode, mask);
180 revert_creds(old_cred);
181
182 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200183}
184
Al Viro6b255392015-11-17 10:20:54 -0500185static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500186 struct inode *inode,
187 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200188{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200189 struct dentry *realdentry;
190 struct inode *realinode;
191
Al Viro6b255392015-11-17 10:20:54 -0500192 if (!dentry)
193 return ERR_PTR(-ECHILD);
194
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200195 realdentry = ovl_dentry_real(dentry);
196 realinode = realdentry->d_inode;
197
Al Viro6b255392015-11-17 10:20:54 -0500198 if (WARN_ON(!realinode->i_op->get_link))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199 return ERR_PTR(-EPERM);
200
Al Virofceef392015-12-29 15:58:39 -0500201 return realinode->i_op->get_link(realdentry, realinode, done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200202}
203
204static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
205{
206 struct path realpath;
207 struct inode *realinode;
208
209 ovl_path_real(dentry, &realpath);
210 realinode = realpath.dentry->d_inode;
211
212 if (!realinode->i_op->readlink)
213 return -EINVAL;
214
215 touch_atime(&realpath);
216
217 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
218}
219
220
221static bool ovl_is_private_xattr(const char *name)
222{
hujianyangcead89b2014-11-24 18:25:21 +0800223 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200224}
225
Al Viro3767e252016-05-27 11:06:05 -0400226int ovl_setxattr(struct dentry *dentry, struct inode *inode,
227 const char *name, const void *value,
228 size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200229{
230 int err;
231 struct dentry *upperdentry;
232
233 err = ovl_want_write(dentry);
234 if (err)
235 goto out;
236
237 err = -EPERM;
238 if (ovl_is_private_xattr(name))
239 goto out_drop_write;
240
241 err = ovl_copy_up(dentry);
242 if (err)
243 goto out_drop_write;
244
245 upperdentry = ovl_dentry_upper(dentry);
246 err = vfs_setxattr(upperdentry, name, value, size, flags);
247
248out_drop_write:
249 ovl_drop_write(dentry);
250out:
251 return err;
252}
253
Al Viroce23e642016-04-11 00:48:00 -0400254ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
255 const char *name, void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256{
Miklos Szeredib5817552016-06-06 16:21:37 +0200257 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100258
Miklos Szeredib5817552016-06-06 16:21:37 +0200259 if (ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200260 return -ENODATA;
261
Miklos Szeredib5817552016-06-06 16:21:37 +0200262 return vfs_getxattr(realdentry, name, value, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200263}
264
265ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
266{
Miklos Szeredib5817552016-06-06 16:21:37 +0200267 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268 ssize_t res;
269 int off;
270
Miklos Szeredib5817552016-06-06 16:21:37 +0200271 res = vfs_listxattr(realdentry, list, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272 if (res <= 0 || size == 0)
273 return res;
274
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200275 /* filter out private xattrs */
276 for (off = 0; off < res;) {
277 char *s = list + off;
278 size_t slen = strlen(s) + 1;
279
280 BUG_ON(off + slen > res);
281
282 if (ovl_is_private_xattr(s)) {
283 res -= slen;
284 memmove(s, s + slen, res - off);
285 } else {
286 off += slen;
287 }
288 }
289
290 return res;
291}
292
293int ovl_removexattr(struct dentry *dentry, const char *name)
294{
295 int err;
296 struct path realpath;
Miklos Szeredi52148462014-11-20 16:40:00 +0100297 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200298
299 err = ovl_want_write(dentry);
300 if (err)
301 goto out;
302
Miklos Szeredi52148462014-11-20 16:40:00 +0100303 err = -ENODATA;
Miklos Szeredib5817552016-06-06 16:21:37 +0200304 if (ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 goto out_drop_write;
306
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100307 if (!OVL_TYPE_UPPER(type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
309 if (err < 0)
310 goto out_drop_write;
311
312 err = ovl_copy_up(dentry);
313 if (err)
314 goto out_drop_write;
315
316 ovl_path_upper(dentry, &realpath);
317 }
318
319 err = vfs_removexattr(realpath.dentry, name);
320out_drop_write:
321 ovl_drop_write(dentry);
322out:
323 return err;
324}
325
Vivek Goyal39a25b22016-07-01 16:34:26 -0400326struct posix_acl *ovl_get_acl(struct inode *inode, int type)
327{
328 struct inode *realinode = ovl_inode_real(inode);
329
Vivek Goyal39a25b22016-07-01 16:34:26 -0400330 if (!IS_POSIXACL(realinode))
331 return NULL;
332
333 if (!realinode->i_op->get_acl)
334 return NULL;
335
336 return realinode->i_op->get_acl(realinode, type);
337}
338
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
340 struct dentry *realdentry)
341{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100342 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 return false;
344
345 if (special_file(realdentry->d_inode->i_mode))
346 return false;
347
348 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
349 return false;
350
351 return true;
352}
353
Miklos Szeredi2d902672016-06-30 08:53:27 +0200354int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200356 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200357 struct path realpath;
358 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200359
360 type = ovl_path_real(dentry, &realpath);
David Howells4bacc9c2015-06-18 14:32:31 +0100361 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200363 if (!err) {
364 if (file_flags & O_TRUNC)
365 err = ovl_copy_up_truncate(dentry);
366 else
367 err = ovl_copy_up(dentry);
368 ovl_drop_write(dentry);
369 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200370 }
371
Miklos Szeredi2d902672016-06-30 08:53:27 +0200372 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200373}
374
375static const struct inode_operations ovl_file_inode_operations = {
376 .setattr = ovl_setattr,
377 .permission = ovl_permission,
378 .getattr = ovl_getattr,
379 .setxattr = ovl_setxattr,
380 .getxattr = ovl_getxattr,
381 .listxattr = ovl_listxattr,
382 .removexattr = ovl_removexattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400383 .get_acl = ovl_get_acl,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384};
385
386static const struct inode_operations ovl_symlink_inode_operations = {
387 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500388 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389 .readlink = ovl_readlink,
390 .getattr = ovl_getattr,
391 .setxattr = ovl_setxattr,
392 .getxattr = ovl_getxattr,
393 .listxattr = ovl_listxattr,
394 .removexattr = ovl_removexattr,
395};
396
397struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
398 struct ovl_entry *oe)
399{
400 struct inode *inode;
401
402 inode = new_inode(sb);
403 if (!inode)
404 return NULL;
405
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406 inode->i_ino = get_next_ino();
407 inode->i_mode = mode;
408 inode->i_flags |= S_NOATIME | S_NOCMTIME;
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200409 inode->i_private = oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200410
Vivek Goyal07a2daa2016-07-01 16:34:25 -0400411 mode &= S_IFMT;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200412 switch (mode) {
413 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200414 inode->i_op = &ovl_dir_inode_operations;
415 inode->i_fop = &ovl_dir_operations;
416 break;
417
418 case S_IFLNK:
419 inode->i_op = &ovl_symlink_inode_operations;
420 break;
421
422 case S_IFREG:
423 case S_IFSOCK:
424 case S_IFBLK:
425 case S_IFCHR:
426 case S_IFIFO:
427 inode->i_op = &ovl_file_inode_operations;
428 break;
429
430 default:
431 WARN(1, "illegal file type: %i\n", mode);
432 iput(inode);
433 inode = NULL;
434 }
435
436 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200437}