blob: d554e86abbe365c82ca3413e3295715295fe7ad5 [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{
112 struct ovl_entry *oe;
113 struct dentry *alias = NULL;
114 struct inode *realinode;
115 struct dentry *realdentry;
116 bool is_upper;
117 int err;
118
119 if (S_ISDIR(inode->i_mode)) {
120 oe = inode->i_private;
121 } else if (mask & MAY_NOT_BLOCK) {
122 return -ECHILD;
123 } else {
124 /*
125 * For non-directories find an alias and get the info
126 * from there.
127 */
128 alias = d_find_any_alias(inode);
129 if (WARN_ON(!alias))
130 return -ENOENT;
131
132 oe = alias->d_fsdata;
133 }
134
135 realdentry = ovl_entry_real(oe, &is_upper);
136
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200137 if (ovl_is_default_permissions(inode)) {
138 struct kstat stat;
139 struct path realpath = { .dentry = realdentry };
140
141 if (mask & MAY_NOT_BLOCK)
142 return -ECHILD;
143
144 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
145
146 err = vfs_getattr(&realpath, &stat);
147 if (err)
Miklos Szeredia4859d72016-06-29 08:26:59 +0200148 goto out_dput;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200149
Miklos Szeredia4859d72016-06-29 08:26:59 +0200150 err = -ESTALE;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200151 if ((stat.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredia4859d72016-06-29 08:26:59 +0200152 goto out_dput;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200153
154 inode->i_mode = stat.mode;
155 inode->i_uid = stat.uid;
156 inode->i_gid = stat.gid;
157
Miklos Szeredia4859d72016-06-29 08:26:59 +0200158 err = generic_permission(inode, mask);
159 goto out_dput;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200160 }
161
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200162 /* Careful in RCU walk mode */
163 realinode = ACCESS_ONCE(realdentry->d_inode);
164 if (!realinode) {
165 WARN_ON(!(mask & MAY_NOT_BLOCK));
166 err = -ENOENT;
167 goto out_dput;
168 }
169
170 if (mask & MAY_WRITE) {
171 umode_t mode = realinode->i_mode;
172
173 /*
174 * Writes will always be redirected to upper layer, so
175 * ignore lower layer being read-only.
176 *
177 * If the overlay itself is read-only then proceed
178 * with the permission check, don't return EROFS.
179 * This will only happen if this is the lower layer of
180 * another overlayfs.
181 *
182 * If upper fs becomes read-only after the overlay was
183 * constructed return EROFS to prevent modification of
184 * upper layer.
185 */
186 err = -EROFS;
187 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
188 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
189 goto out_dput;
190 }
191
192 err = __inode_permission(realinode, mask);
193out_dput:
194 dput(alias);
195 return err;
196}
197
Al Viro6b255392015-11-17 10:20:54 -0500198static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500199 struct inode *inode,
200 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200201{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200202 struct dentry *realdentry;
203 struct inode *realinode;
204
Al Viro6b255392015-11-17 10:20:54 -0500205 if (!dentry)
206 return ERR_PTR(-ECHILD);
207
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200208 realdentry = ovl_dentry_real(dentry);
209 realinode = realdentry->d_inode;
210
Al Viro6b255392015-11-17 10:20:54 -0500211 if (WARN_ON(!realinode->i_op->get_link))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212 return ERR_PTR(-EPERM);
213
Al Virofceef392015-12-29 15:58:39 -0500214 return realinode->i_op->get_link(realdentry, realinode, done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215}
216
217static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
218{
219 struct path realpath;
220 struct inode *realinode;
221
222 ovl_path_real(dentry, &realpath);
223 realinode = realpath.dentry->d_inode;
224
225 if (!realinode->i_op->readlink)
226 return -EINVAL;
227
228 touch_atime(&realpath);
229
230 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
231}
232
233
234static bool ovl_is_private_xattr(const char *name)
235{
hujianyangcead89b2014-11-24 18:25:21 +0800236 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200237}
238
Al Viro3767e252016-05-27 11:06:05 -0400239int ovl_setxattr(struct dentry *dentry, struct inode *inode,
240 const char *name, const void *value,
241 size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242{
243 int err;
244 struct dentry *upperdentry;
245
246 err = ovl_want_write(dentry);
247 if (err)
248 goto out;
249
250 err = -EPERM;
251 if (ovl_is_private_xattr(name))
252 goto out_drop_write;
253
254 err = ovl_copy_up(dentry);
255 if (err)
256 goto out_drop_write;
257
258 upperdentry = ovl_dentry_upper(dentry);
259 err = vfs_setxattr(upperdentry, name, value, size, flags);
260
261out_drop_write:
262 ovl_drop_write(dentry);
263out:
264 return err;
265}
266
Al Viroce23e642016-04-11 00:48:00 -0400267ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
268 const char *name, void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200269{
Miklos Szeredib5817552016-06-06 16:21:37 +0200270 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100271
Miklos Szeredib5817552016-06-06 16:21:37 +0200272 if (ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273 return -ENODATA;
274
Miklos Szeredib5817552016-06-06 16:21:37 +0200275 return vfs_getxattr(realdentry, name, value, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276}
277
278ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
279{
Miklos Szeredib5817552016-06-06 16:21:37 +0200280 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200281 ssize_t res;
282 int off;
283
Miklos Szeredib5817552016-06-06 16:21:37 +0200284 res = vfs_listxattr(realdentry, list, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285 if (res <= 0 || size == 0)
286 return res;
287
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288 /* filter out private xattrs */
289 for (off = 0; off < res;) {
290 char *s = list + off;
291 size_t slen = strlen(s) + 1;
292
293 BUG_ON(off + slen > res);
294
295 if (ovl_is_private_xattr(s)) {
296 res -= slen;
297 memmove(s, s + slen, res - off);
298 } else {
299 off += slen;
300 }
301 }
302
303 return res;
304}
305
306int ovl_removexattr(struct dentry *dentry, const char *name)
307{
308 int err;
309 struct path realpath;
Miklos Szeredi52148462014-11-20 16:40:00 +0100310 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311
312 err = ovl_want_write(dentry);
313 if (err)
314 goto out;
315
Miklos Szeredi52148462014-11-20 16:40:00 +0100316 err = -ENODATA;
Miklos Szeredib5817552016-06-06 16:21:37 +0200317 if (ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318 goto out_drop_write;
319
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100320 if (!OVL_TYPE_UPPER(type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
322 if (err < 0)
323 goto out_drop_write;
324
325 err = ovl_copy_up(dentry);
326 if (err)
327 goto out_drop_write;
328
329 ovl_path_upper(dentry, &realpath);
330 }
331
332 err = vfs_removexattr(realpath.dentry, name);
333out_drop_write:
334 ovl_drop_write(dentry);
335out:
336 return err;
337}
338
339static 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,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200383};
384
385static const struct inode_operations ovl_symlink_inode_operations = {
386 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500387 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388 .readlink = ovl_readlink,
389 .getattr = ovl_getattr,
390 .setxattr = ovl_setxattr,
391 .getxattr = ovl_getxattr,
392 .listxattr = ovl_listxattr,
393 .removexattr = ovl_removexattr,
394};
395
396struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
397 struct ovl_entry *oe)
398{
399 struct inode *inode;
400
401 inode = new_inode(sb);
402 if (!inode)
403 return NULL;
404
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405 inode->i_ino = get_next_ino();
406 inode->i_mode = mode;
407 inode->i_flags |= S_NOATIME | S_NOCMTIME;
408
Vivek Goyal07a2daa2016-07-01 16:34:25 -0400409 mode &= S_IFMT;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200410 switch (mode) {
411 case S_IFDIR:
412 inode->i_private = oe;
413 inode->i_op = &ovl_dir_inode_operations;
414 inode->i_fop = &ovl_dir_operations;
415 break;
416
417 case S_IFLNK:
418 inode->i_op = &ovl_symlink_inode_operations;
419 break;
420
421 case S_IFREG:
422 case S_IFSOCK:
423 case S_IFBLK:
424 case S_IFCHR:
425 case S_IFIFO:
426 inode->i_op = &ovl_file_inode_operations;
427 break;
428
429 default:
430 WARN(1, "illegal file type: %i\n", mode);
431 iput(inode);
432 inode = NULL;
433 }
434
435 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200436}