xref: /linux/fs/overlayfs/inode.c (revision cc3ae7b0af27118994c1e491382b253be3b762bf)
1 /*
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 
15 static int ovl_copy_up_truncate(struct dentry *dentry)
16 {
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 
32 	stat.size = 0;
33 	err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
34 
35 out_dput_parent:
36 	dput(parent);
37 	return err;
38 }
39 
40 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
41 {
42 	int err;
43 	struct dentry *upperdentry;
44 
45 	/*
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 
58 	err = ovl_want_write(dentry);
59 	if (err)
60 		goto out;
61 
62 	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 
70 	err = ovl_copy_up(dentry);
71 	if (!err) {
72 		struct inode *winode = NULL;
73 
74 		upperdentry = ovl_dentry_upper(dentry);
75 
76 		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 
83 		inode_lock(upperdentry->d_inode);
84 		err = notify_change(upperdentry, attr, NULL);
85 		if (!err)
86 			ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
87 		inode_unlock(upperdentry->d_inode);
88 
89 		if (winode)
90 			put_write_access(winode);
91 	}
92 out_drop_write:
93 	ovl_drop_write(dentry);
94 out:
95 	return err;
96 }
97 
98 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
99 			 struct kstat *stat)
100 {
101 	struct path realpath;
102 
103 	ovl_path_real(dentry, &realpath);
104 	return vfs_getattr(&realpath, stat);
105 }
106 
107 int ovl_permission(struct inode *inode, int mask)
108 {
109 	struct ovl_entry *oe;
110 	struct dentry *alias = NULL;
111 	struct inode *realinode;
112 	struct dentry *realdentry;
113 	bool is_upper;
114 	int err;
115 
116 	if (S_ISDIR(inode->i_mode)) {
117 		oe = inode->i_private;
118 	} else if (mask & MAY_NOT_BLOCK) {
119 		return -ECHILD;
120 	} else {
121 		/*
122 		 * For non-directories find an alias and get the info
123 		 * from there.
124 		 */
125 		alias = d_find_any_alias(inode);
126 		if (WARN_ON(!alias))
127 			return -ENOENT;
128 
129 		oe = alias->d_fsdata;
130 	}
131 
132 	realdentry = ovl_entry_real(oe, &is_upper);
133 
134 	if (ovl_is_default_permissions(inode)) {
135 		struct kstat stat;
136 		struct path realpath = { .dentry = realdentry };
137 
138 		if (mask & MAY_NOT_BLOCK)
139 			return -ECHILD;
140 
141 		realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
142 
143 		err = vfs_getattr(&realpath, &stat);
144 		if (err)
145 			goto out_dput;
146 
147 		err = -ESTALE;
148 		if ((stat.mode ^ inode->i_mode) & S_IFMT)
149 			goto out_dput;
150 
151 		inode->i_mode = stat.mode;
152 		inode->i_uid = stat.uid;
153 		inode->i_gid = stat.gid;
154 
155 		err = generic_permission(inode, mask);
156 		goto out_dput;
157 	}
158 
159 	/* Careful in RCU walk mode */
160 	realinode = ACCESS_ONCE(realdentry->d_inode);
161 	if (!realinode) {
162 		WARN_ON(!(mask & MAY_NOT_BLOCK));
163 		err = -ENOENT;
164 		goto out_dput;
165 	}
166 
167 	if (mask & MAY_WRITE) {
168 		umode_t mode = realinode->i_mode;
169 
170 		/*
171 		 * Writes will always be redirected to upper layer, so
172 		 * ignore lower layer being read-only.
173 		 *
174 		 * If the overlay itself is read-only then proceed
175 		 * with the permission check, don't return EROFS.
176 		 * This will only happen if this is the lower layer of
177 		 * another overlayfs.
178 		 *
179 		 * If upper fs becomes read-only after the overlay was
180 		 * constructed return EROFS to prevent modification of
181 		 * upper layer.
182 		 */
183 		err = -EROFS;
184 		if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
185 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
186 			goto out_dput;
187 	}
188 
189 	err = __inode_permission(realinode, mask);
190 out_dput:
191 	dput(alias);
192 	return err;
193 }
194 
195 static const char *ovl_get_link(struct dentry *dentry,
196 				struct inode *inode,
197 				struct delayed_call *done)
198 {
199 	struct dentry *realdentry;
200 	struct inode *realinode;
201 
202 	if (!dentry)
203 		return ERR_PTR(-ECHILD);
204 
205 	realdentry = ovl_dentry_real(dentry);
206 	realinode = realdentry->d_inode;
207 
208 	if (WARN_ON(!realinode->i_op->get_link))
209 		return ERR_PTR(-EPERM);
210 
211 	return realinode->i_op->get_link(realdentry, realinode, done);
212 }
213 
214 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
215 {
216 	struct path realpath;
217 	struct inode *realinode;
218 
219 	ovl_path_real(dentry, &realpath);
220 	realinode = realpath.dentry->d_inode;
221 
222 	if (!realinode->i_op->readlink)
223 		return -EINVAL;
224 
225 	touch_atime(&realpath);
226 
227 	return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
228 }
229 
230 
231 static bool ovl_is_private_xattr(const char *name)
232 {
233 	return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
234 }
235 
236 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
237 		 const char *name, const void *value,
238 		 size_t size, int flags)
239 {
240 	int err;
241 	struct dentry *upperdentry;
242 
243 	err = ovl_want_write(dentry);
244 	if (err)
245 		goto out;
246 
247 	err = -EPERM;
248 	if (ovl_is_private_xattr(name))
249 		goto out_drop_write;
250 
251 	err = ovl_copy_up(dentry);
252 	if (err)
253 		goto out_drop_write;
254 
255 	upperdentry = ovl_dentry_upper(dentry);
256 	err = vfs_setxattr(upperdentry, name, value, size, flags);
257 
258 out_drop_write:
259 	ovl_drop_write(dentry);
260 out:
261 	return err;
262 }
263 
264 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
265 		     const char *name, void *value, size_t size)
266 {
267 	struct dentry *realdentry = ovl_dentry_real(dentry);
268 
269 	if (ovl_is_private_xattr(name))
270 		return -ENODATA;
271 
272 	return vfs_getxattr(realdentry, name, value, size);
273 }
274 
275 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
276 {
277 	struct dentry *realdentry = ovl_dentry_real(dentry);
278 	ssize_t res;
279 	int off;
280 
281 	res = vfs_listxattr(realdentry, list, size);
282 	if (res <= 0 || size == 0)
283 		return res;
284 
285 	/* filter out private xattrs */
286 	for (off = 0; off < res;) {
287 		char *s = list + off;
288 		size_t slen = strlen(s) + 1;
289 
290 		BUG_ON(off + slen > res);
291 
292 		if (ovl_is_private_xattr(s)) {
293 			res -= slen;
294 			memmove(s, s + slen, res - off);
295 		} else {
296 			off += slen;
297 		}
298 	}
299 
300 	return res;
301 }
302 
303 int ovl_removexattr(struct dentry *dentry, const char *name)
304 {
305 	int err;
306 	struct path realpath;
307 	enum ovl_path_type type = ovl_path_real(dentry, &realpath);
308 
309 	err = ovl_want_write(dentry);
310 	if (err)
311 		goto out;
312 
313 	err = -ENODATA;
314 	if (ovl_is_private_xattr(name))
315 		goto out_drop_write;
316 
317 	if (!OVL_TYPE_UPPER(type)) {
318 		err = vfs_getxattr(realpath.dentry, name, NULL, 0);
319 		if (err < 0)
320 			goto out_drop_write;
321 
322 		err = ovl_copy_up(dentry);
323 		if (err)
324 			goto out_drop_write;
325 
326 		ovl_path_upper(dentry, &realpath);
327 	}
328 
329 	err = vfs_removexattr(realpath.dentry, name);
330 out_drop_write:
331 	ovl_drop_write(dentry);
332 out:
333 	return err;
334 }
335 
336 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
337 				  struct dentry *realdentry)
338 {
339 	if (OVL_TYPE_UPPER(type))
340 		return false;
341 
342 	if (special_file(realdentry->d_inode->i_mode))
343 		return false;
344 
345 	if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
346 		return false;
347 
348 	return true;
349 }
350 
351 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
352 {
353 	int err;
354 	struct path realpath;
355 	enum ovl_path_type type;
356 
357 	if (d_is_dir(dentry))
358 		return d_backing_inode(dentry);
359 
360 	type = ovl_path_real(dentry, &realpath);
361 	if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
362 		err = ovl_want_write(dentry);
363 		if (err)
364 			return ERR_PTR(err);
365 
366 		if (file_flags & O_TRUNC)
367 			err = ovl_copy_up_truncate(dentry);
368 		else
369 			err = ovl_copy_up(dentry);
370 		ovl_drop_write(dentry);
371 		if (err)
372 			return ERR_PTR(err);
373 
374 		ovl_path_upper(dentry, &realpath);
375 	}
376 
377 	if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
378 		return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
379 
380 	return d_backing_inode(realpath.dentry);
381 }
382 
383 static const struct inode_operations ovl_file_inode_operations = {
384 	.setattr	= ovl_setattr,
385 	.permission	= ovl_permission,
386 	.getattr	= ovl_getattr,
387 	.setxattr	= ovl_setxattr,
388 	.getxattr	= ovl_getxattr,
389 	.listxattr	= ovl_listxattr,
390 	.removexattr	= ovl_removexattr,
391 };
392 
393 static const struct inode_operations ovl_symlink_inode_operations = {
394 	.setattr	= ovl_setattr,
395 	.get_link	= ovl_get_link,
396 	.readlink	= ovl_readlink,
397 	.getattr	= ovl_getattr,
398 	.setxattr	= ovl_setxattr,
399 	.getxattr	= ovl_getxattr,
400 	.listxattr	= ovl_listxattr,
401 	.removexattr	= ovl_removexattr,
402 };
403 
404 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
405 			    struct ovl_entry *oe)
406 {
407 	struct inode *inode;
408 
409 	inode = new_inode(sb);
410 	if (!inode)
411 		return NULL;
412 
413 	mode &= S_IFMT;
414 
415 	inode->i_ino = get_next_ino();
416 	inode->i_mode = mode;
417 	inode->i_flags |= S_NOATIME | S_NOCMTIME;
418 
419 	switch (mode) {
420 	case S_IFDIR:
421 		inode->i_private = oe;
422 		inode->i_op = &ovl_dir_inode_operations;
423 		inode->i_fop = &ovl_dir_operations;
424 		break;
425 
426 	case S_IFLNK:
427 		inode->i_op = &ovl_symlink_inode_operations;
428 		break;
429 
430 	case S_IFREG:
431 	case S_IFSOCK:
432 	case S_IFBLK:
433 	case S_IFCHR:
434 	case S_IFIFO:
435 		inode->i_op = &ovl_file_inode_operations;
436 		break;
437 
438 	default:
439 		WARN(1, "illegal file type: %i\n", mode);
440 		iput(inode);
441 		inode = NULL;
442 	}
443 
444 	return inode;
445 }
446