blob: 041db9c6621ca5f462d36f1278db1802e78337a7 [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;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040044 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020045
Miklos Szeredicf9a6782015-12-11 16:30:49 +010046 /*
47 * Check for permissions before trying to copy-up. This is redundant
48 * since it will be rechecked later by ->setattr() on upper dentry. But
49 * without this, copy-up can be triggered by just about anybody.
50 *
51 * We don't initialize inode->size, which just means that
52 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
53 * check for a swapfile (which this won't be anyway).
54 */
55 err = inode_change_ok(dentry->d_inode, attr);
56 if (err)
57 return err;
58
Miklos Szeredie9be9d52014-10-24 00:14:38 +020059 err = ovl_want_write(dentry);
60 if (err)
61 goto out;
62
Miklos Szeredi03bea602016-06-29 16:03:55 +020063 if (attr->ia_valid & ATTR_SIZE) {
64 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
65
66 err = -ETXTBSY;
67 if (atomic_read(&realinode->i_writecount) < 0)
68 goto out_drop_write;
69 }
70
Miklos Szerediacff81e2015-12-04 19:18:48 +010071 err = ovl_copy_up(dentry);
72 if (!err) {
Miklos Szeredi03bea602016-06-29 16:03:55 +020073 struct inode *winode = NULL;
74
Miklos Szerediacff81e2015-12-04 19:18:48 +010075 upperdentry = ovl_dentry_upper(dentry);
76
Miklos Szeredi03bea602016-06-29 16:03:55 +020077 if (attr->ia_valid & ATTR_SIZE) {
78 winode = d_inode(upperdentry);
79 err = get_write_access(winode);
80 if (err)
81 goto out_drop_write;
82 }
83
Miklos Szeredib99c2d92016-07-04 16:49:48 +020084 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
85 attr->ia_valid &= ~ATTR_MODE;
86
Al Viro59551022016-01-22 15:40:57 -050087 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040088 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020089 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040090 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030091 if (!err)
92 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050093 inode_unlock(upperdentry->d_inode);
Miklos Szeredi03bea602016-06-29 16:03:55 +020094
95 if (winode)
96 put_write_access(winode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020097 }
Miklos Szeredi03bea602016-06-29 16:03:55 +020098out_drop_write:
Miklos Szeredie9be9d52014-10-24 00:14:38 +020099 ovl_drop_write(dentry);
100out:
101 return err;
102}
103
104static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
105 struct kstat *stat)
106{
107 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400108 const struct cred *old_cred;
109 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110
111 ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400112 old_cred = ovl_override_creds(dentry->d_sb);
113 err = vfs_getattr(&realpath, stat);
114 revert_creds(old_cred);
115 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200116}
117
118int ovl_permission(struct inode *inode, int mask)
119{
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200120 struct ovl_entry *oe = inode->i_private;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200121 bool is_upper;
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200122 struct dentry *realdentry = ovl_entry_real(oe, &is_upper);
123 struct inode *realinode;
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400124 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200125 int err;
126
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200127 /* Careful in RCU walk mode */
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200128 realinode = d_inode_rcu(realdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200129 if (!realinode) {
130 WARN_ON(!(mask & MAY_NOT_BLOCK));
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200131 return -ENOENT;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132 }
133
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400134 /*
135 * Check overlay inode with the creds of task and underlying inode
136 * with creds of mounter
137 */
138 err = generic_permission(inode, mask);
139 if (err)
140 return err;
141
142 old_cred = ovl_override_creds(inode->i_sb);
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400143 if (!is_upper)
144 mask &= ~(MAY_WRITE | MAY_APPEND);
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200145 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400146 revert_creds(old_cred);
147
148 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149}
150
Al Viro6b255392015-11-17 10:20:54 -0500151static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500152 struct inode *inode,
153 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155 struct dentry *realdentry;
156 struct inode *realinode;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400157 const struct cred *old_cred;
158 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200159
Al Viro6b255392015-11-17 10:20:54 -0500160 if (!dentry)
161 return ERR_PTR(-ECHILD);
162
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163 realdentry = ovl_dentry_real(dentry);
164 realinode = realdentry->d_inode;
165
Al Viro6b255392015-11-17 10:20:54 -0500166 if (WARN_ON(!realinode->i_op->get_link))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200167 return ERR_PTR(-EPERM);
168
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400169 old_cred = ovl_override_creds(dentry->d_sb);
170 p = realinode->i_op->get_link(realdentry, realinode, done);
171 revert_creds(old_cred);
172 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200173}
174
175static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
176{
177 struct path realpath;
178 struct inode *realinode;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400179 const struct cred *old_cred;
180 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181
182 ovl_path_real(dentry, &realpath);
183 realinode = realpath.dentry->d_inode;
184
185 if (!realinode->i_op->readlink)
186 return -EINVAL;
187
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400188 old_cred = ovl_override_creds(dentry->d_sb);
189 err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
190 revert_creds(old_cred);
191 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200192}
193
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200194static bool ovl_is_private_xattr(const char *name)
195{
hujianyangcead89b2014-11-24 18:25:21 +0800196 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197}
198
Al Viro3767e252016-05-27 11:06:05 -0400199int ovl_setxattr(struct dentry *dentry, struct inode *inode,
200 const char *name, const void *value,
201 size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200202{
203 int err;
204 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400205 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200206
207 err = ovl_want_write(dentry);
208 if (err)
209 goto out;
210
211 err = -EPERM;
212 if (ovl_is_private_xattr(name))
213 goto out_drop_write;
214
215 err = ovl_copy_up(dentry);
216 if (err)
217 goto out_drop_write;
218
219 upperdentry = ovl_dentry_upper(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400220 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200221 err = vfs_setxattr(upperdentry, name, value, size, flags);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400222 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223
224out_drop_write:
225 ovl_drop_write(dentry);
226out:
227 return err;
228}
229
Al Viroce23e642016-04-11 00:48:00 -0400230ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
231 const char *name, void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200232{
Miklos Szeredib5817552016-06-06 16:21:37 +0200233 struct dentry *realdentry = ovl_dentry_real(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400234 ssize_t res;
235 const struct cred *old_cred;
Miklos Szeredi52148462014-11-20 16:40:00 +0100236
Miklos Szeredib5817552016-06-06 16:21:37 +0200237 if (ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200238 return -ENODATA;
239
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400240 old_cred = ovl_override_creds(dentry->d_sb);
241 res = vfs_getxattr(realdentry, name, value, size);
242 revert_creds(old_cred);
243 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200244}
245
246ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
247{
Miklos Szeredib5817552016-06-06 16:21:37 +0200248 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200249 ssize_t res;
250 int off;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400251 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200252
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400253 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200254 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400255 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256 if (res <= 0 || size == 0)
257 return res;
258
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200259 /* filter out private xattrs */
260 for (off = 0; off < res;) {
261 char *s = list + off;
262 size_t slen = strlen(s) + 1;
263
264 BUG_ON(off + slen > res);
265
266 if (ovl_is_private_xattr(s)) {
267 res -= slen;
268 memmove(s, s + slen, res - off);
269 } else {
270 off += slen;
271 }
272 }
273
274 return res;
275}
276
277int ovl_removexattr(struct dentry *dentry, const char *name)
278{
279 int err;
280 struct path realpath;
Miklos Szeredi52148462014-11-20 16:40:00 +0100281 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400282 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200283
284 err = ovl_want_write(dentry);
285 if (err)
286 goto out;
287
Miklos Szeredi52148462014-11-20 16:40:00 +0100288 err = -ENODATA;
Miklos Szeredib5817552016-06-06 16:21:37 +0200289 if (ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200290 goto out_drop_write;
291
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100292 if (!OVL_TYPE_UPPER(type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200293 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
294 if (err < 0)
295 goto out_drop_write;
296
297 err = ovl_copy_up(dentry);
298 if (err)
299 goto out_drop_write;
300
301 ovl_path_upper(dentry, &realpath);
302 }
303
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400304 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 err = vfs_removexattr(realpath.dentry, name);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400306 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307out_drop_write:
308 ovl_drop_write(dentry);
309out:
310 return err;
311}
312
Vivek Goyal39a25b22016-07-01 16:34:26 -0400313struct posix_acl *ovl_get_acl(struct inode *inode, int type)
314{
315 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400316 const struct cred *old_cred;
317 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400318
Vivek Goyal39a25b22016-07-01 16:34:26 -0400319 if (!IS_POSIXACL(realinode))
320 return NULL;
321
322 if (!realinode->i_op->get_acl)
323 return NULL;
324
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400325 old_cred = ovl_override_creds(inode->i_sb);
326 acl = realinode->i_op->get_acl(realinode, type);
327 revert_creds(old_cred);
328
329 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400330}
331
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200332static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
333 struct dentry *realdentry)
334{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100335 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200336 return false;
337
338 if (special_file(realdentry->d_inode->i_mode))
339 return false;
340
341 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
342 return false;
343
344 return true;
345}
346
Miklos Szeredi2d902672016-06-30 08:53:27 +0200347int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200349 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350 struct path realpath;
351 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352
353 type = ovl_path_real(dentry, &realpath);
David Howells4bacc9c2015-06-18 14:32:31 +0100354 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200356 if (!err) {
357 if (file_flags & O_TRUNC)
358 err = ovl_copy_up_truncate(dentry);
359 else
360 err = ovl_copy_up(dentry);
361 ovl_drop_write(dentry);
362 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200363 }
364
Miklos Szeredi2d902672016-06-30 08:53:27 +0200365 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200366}
367
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200368int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
369{
370 struct dentry *alias;
371 struct path upperpath;
372
373 if (!(flags & S_ATIME))
374 return 0;
375
376 alias = d_find_any_alias(inode);
377 if (!alias)
378 return 0;
379
380 ovl_path_upper(alias, &upperpath);
381 if (upperpath.dentry) {
382 touch_atime(&upperpath);
383 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
384 }
385
386 dput(alias);
387
388 return 0;
389}
390
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391static const struct inode_operations ovl_file_inode_operations = {
392 .setattr = ovl_setattr,
393 .permission = ovl_permission,
394 .getattr = ovl_getattr,
395 .setxattr = ovl_setxattr,
396 .getxattr = ovl_getxattr,
397 .listxattr = ovl_listxattr,
398 .removexattr = ovl_removexattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400399 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200400 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200401};
402
403static const struct inode_operations ovl_symlink_inode_operations = {
404 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500405 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406 .readlink = ovl_readlink,
407 .getattr = ovl_getattr,
408 .setxattr = ovl_setxattr,
409 .getxattr = ovl_getxattr,
410 .listxattr = ovl_listxattr,
411 .removexattr = ovl_removexattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200412 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413};
414
415struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
416 struct ovl_entry *oe)
417{
418 struct inode *inode;
419
420 inode = new_inode(sb);
421 if (!inode)
422 return NULL;
423
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424 inode->i_ino = get_next_ino();
425 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200426 inode->i_flags |= S_NOCMTIME;
Andreas Gruenbacher58ed4e72016-05-26 02:01:47 +0200427 inode->i_private = oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428
Vivek Goyal07a2daa2016-07-01 16:34:25 -0400429 mode &= S_IFMT;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430 switch (mode) {
431 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432 inode->i_op = &ovl_dir_inode_operations;
433 inode->i_fop = &ovl_dir_operations;
434 break;
435
436 case S_IFLNK:
437 inode->i_op = &ovl_symlink_inode_operations;
438 break;
439
440 case S_IFREG:
441 case S_IFSOCK:
442 case S_IFBLK:
443 case S_IFCHR:
444 case S_IFIFO:
445 inode->i_op = &ovl_file_inode_operations;
446 break;
447
448 default:
449 WARN(1, "illegal file type: %i\n", mode);
450 iput(inode);
451 inode = NULL;
452 }
453
454 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200455}