blob: f8fe6bf2036df3bd531132877101cb979c1feed0 [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>
Ingo Molnar5b825c32017-02-02 17:54:15 +010012#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020013#include <linux/xattr.h>
Miklos Szeredi5201dc42016-09-01 11:11:59 +020014#include <linux/posix_acl.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020015#include "overlayfs.h"
16
Miklos Szeredie9be9d52014-10-24 00:14:38 +020017int ovl_setattr(struct dentry *dentry, struct iattr *attr)
18{
19 int err;
20 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040021 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022
Miklos Szeredicf9a6782015-12-11 16:30:49 +010023 /*
24 * Check for permissions before trying to copy-up. This is redundant
25 * since it will be rechecked later by ->setattr() on upper dentry. But
26 * without this, copy-up can be triggered by just about anybody.
27 *
28 * We don't initialize inode->size, which just means that
29 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
30 * check for a swapfile (which this won't be anyway).
31 */
Jan Kara31051c82016-05-26 16:55:18 +020032 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010033 if (err)
34 return err;
35
Miklos Szeredie9be9d52014-10-24 00:14:38 +020036 err = ovl_want_write(dentry);
37 if (err)
38 goto out;
39
Miklos Szerediacff81e2015-12-04 19:18:48 +010040 err = ovl_copy_up(dentry);
41 if (!err) {
42 upperdentry = ovl_dentry_upper(dentry);
43
Miklos Szeredib99c2d92016-07-04 16:49:48 +020044 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
45 attr->ia_valid &= ~ATTR_MODE;
46
Al Viro59551022016-01-22 15:40:57 -050047 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040048 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040050 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030051 if (!err)
52 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050053 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020054 }
55 ovl_drop_write(dentry);
56out:
57 return err;
58}
59
David Howellsa528d352017-01-31 16:46:22 +000060static int ovl_getattr(const struct path *path, struct kstat *stat,
61 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020062{
David Howellsa528d352017-01-31 16:46:22 +000063 struct dentry *dentry = path->dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020064 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040065 const struct cred *old_cred;
66 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067
68 ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040069 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +000070 err = vfs_getattr(&realpath, stat, request_mask, flags);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040071 revert_creds(old_cred);
72 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020073}
74
75int ovl_permission(struct inode *inode, int mask)
76{
Miklos Szeredie9be9d52014-10-24 00:14:38 +020077 bool is_upper;
Miklos Szeredi39b681f2016-07-29 12:05:24 +020078 struct inode *realinode = ovl_inode_real(inode, &is_upper);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -040079 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020080 int err;
81
Miklos Szeredie9be9d52014-10-24 00:14:38 +020082 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +020083 if (!realinode) {
84 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +020085 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020086 }
87
Vivek Goyalc0ca3d72016-07-01 16:34:27 -040088 /*
89 * Check overlay inode with the creds of task and underlying inode
90 * with creds of mounter
91 */
92 err = generic_permission(inode, mask);
93 if (err)
94 return err;
95
96 old_cred = ovl_override_creds(inode->i_sb);
Vivek Goyal500cac32016-07-13 11:00:14 -040097 if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -040098 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -040099 /* Make sure mounter can read file for copy up later */
100 mask |= MAY_READ;
101 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200102 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400103 revert_creds(old_cred);
104
105 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200106}
107
Al Viro6b255392015-11-17 10:20:54 -0500108static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500109 struct inode *inode,
110 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200111{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400112 const struct cred *old_cred;
113 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200114
Al Viro6b255392015-11-17 10:20:54 -0500115 if (!dentry)
116 return ERR_PTR(-ECHILD);
117
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400118 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200119 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400120 revert_creds(old_cred);
121 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200122}
123
Miklos Szeredi09562542016-08-08 15:08:49 +0200124bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200125{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200126 return strncmp(name, OVL_XATTR_PREFIX,
127 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200128}
129
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200130int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
131 size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132{
133 int err;
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200134 struct path realpath;
135 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400136 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200137
138 err = ovl_want_write(dentry);
139 if (err)
140 goto out;
141
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200142 if (!value && !OVL_TYPE_UPPER(type)) {
143 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
144 if (err < 0)
145 goto out_drop_write;
146 }
147
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148 err = ovl_copy_up(dentry);
149 if (err)
150 goto out_drop_write;
151
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200152 if (!OVL_TYPE_UPPER(type))
153 ovl_path_upper(dentry, &realpath);
154
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400155 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200156 if (value)
157 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
158 else {
159 WARN_ON(flags != XATTR_REPLACE);
160 err = vfs_removexattr(realpath.dentry, name);
161 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400162 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163
164out_drop_write:
165 ovl_drop_write(dentry);
166out:
167 return err;
168}
169
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200170int ovl_xattr_get(struct dentry *dentry, const char *name,
171 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200172{
Miklos Szeredib5817552016-06-06 16:21:37 +0200173 struct dentry *realdentry = ovl_dentry_real(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400174 ssize_t res;
175 const struct cred *old_cred;
Miklos Szeredi52148462014-11-20 16:40:00 +0100176
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400177 old_cred = ovl_override_creds(dentry->d_sb);
178 res = vfs_getxattr(realdentry, name, value, size);
179 revert_creds(old_cred);
180 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181}
182
183ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
184{
Miklos Szeredib5817552016-06-06 16:21:37 +0200185 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200187 size_t len;
188 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400189 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200190
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400191 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200192 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400193 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200194 if (res <= 0 || size == 0)
195 return res;
196
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200198 for (s = list, len = res; len;) {
199 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200201 /* underlying fs providing us with an broken xattr list? */
202 if (WARN_ON(slen > len))
203 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200204
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200205 len -= slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200206 if (ovl_is_private_xattr(s)) {
207 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200208 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200210 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211 }
212 }
213
214 return res;
215}
216
Vivek Goyal39a25b22016-07-01 16:34:26 -0400217struct posix_acl *ovl_get_acl(struct inode *inode, int type)
218{
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200219 struct inode *realinode = ovl_inode_real(inode, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400220 const struct cred *old_cred;
221 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400222
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200223 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400224 return NULL;
225
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400226 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200227 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400228 revert_creds(old_cred);
229
230 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400231}
232
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200233static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
234 struct dentry *realdentry)
235{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100236 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200237 return false;
238
239 if (special_file(realdentry->d_inode->i_mode))
240 return false;
241
242 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
243 return false;
244
245 return true;
246}
247
Miklos Szeredi2d902672016-06-30 08:53:27 +0200248int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200249{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200250 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200251 struct path realpath;
252 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200253
254 type = ovl_path_real(dentry, &realpath);
David Howells4bacc9c2015-06-18 14:32:31 +0100255 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200257 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200258 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200259 ovl_drop_write(dentry);
260 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200261 }
262
Miklos Szeredi2d902672016-06-30 08:53:27 +0200263 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200264}
265
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200266int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
267{
268 struct dentry *alias;
269 struct path upperpath;
270
271 if (!(flags & S_ATIME))
272 return 0;
273
274 alias = d_find_any_alias(inode);
275 if (!alias)
276 return 0;
277
278 ovl_path_upper(alias, &upperpath);
279 if (upperpath.dentry) {
280 touch_atime(&upperpath);
281 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
282 }
283
284 dput(alias);
285
286 return 0;
287}
288
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200289static const struct inode_operations ovl_file_inode_operations = {
290 .setattr = ovl_setattr,
291 .permission = ovl_permission,
292 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200293 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400294 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200295 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296};
297
298static const struct inode_operations ovl_symlink_inode_operations = {
299 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500300 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200302 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200303 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200304};
305
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100306static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308 inode->i_ino = get_next_ino();
309 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200310 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200311#ifdef CONFIG_FS_POSIX_ACL
312 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
313#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200314
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100315 switch (mode & S_IFMT) {
316 case S_IFREG:
317 inode->i_op = &ovl_file_inode_operations;
318 break;
319
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321 inode->i_op = &ovl_dir_inode_operations;
322 inode->i_fop = &ovl_dir_operations;
323 break;
324
325 case S_IFLNK:
326 inode->i_op = &ovl_symlink_inode_operations;
327 break;
328
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200329 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100331 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200332 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200333 }
334}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200335
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100336struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200337{
338 struct inode *inode;
339
340 inode = new_inode(sb);
341 if (inode)
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100342 ovl_fill_inode(inode, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200343
344 return inode;
345}
346
347static int ovl_inode_test(struct inode *inode, void *data)
348{
349 return ovl_inode_real(inode, NULL) == data;
350}
351
352static int ovl_inode_set(struct inode *inode, void *data)
353{
354 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
355 return 0;
356}
357
358struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
359
360{
361 struct inode *inode;
362
363 inode = iget5_locked(sb, (unsigned long) realinode,
364 ovl_inode_test, ovl_inode_set, realinode);
365 if (inode && inode->i_state & I_NEW) {
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100366 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200367 set_nlink(inode, realinode->i_nlink);
368 unlock_new_inode(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369 }
370
371 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200372}