xref: /linux/fs/overlayfs/inode.c (revision 293d5b43948309434568f4dcbb36cce4c3c51bd5)
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 	const struct cred *old_cred;
45 
46 	/*
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 
59 	err = ovl_want_write(dentry);
60 	if (err)
61 		goto out;
62 
63 	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 
71 	err = ovl_copy_up(dentry);
72 	if (!err) {
73 		struct inode *winode = NULL;
74 
75 		upperdentry = ovl_dentry_upper(dentry);
76 
77 		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 
84 		if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
85 			attr->ia_valid &= ~ATTR_MODE;
86 
87 		inode_lock(upperdentry->d_inode);
88 		old_cred = ovl_override_creds(dentry->d_sb);
89 		err = notify_change(upperdentry, attr, NULL);
90 		revert_creds(old_cred);
91 		if (!err)
92 			ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
93 		inode_unlock(upperdentry->d_inode);
94 
95 		if (winode)
96 			put_write_access(winode);
97 	}
98 out_drop_write:
99 	ovl_drop_write(dentry);
100 out:
101 	return err;
102 }
103 
104 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
105 			 struct kstat *stat)
106 {
107 	struct path realpath;
108 	const struct cred *old_cred;
109 	int err;
110 
111 	ovl_path_real(dentry, &realpath);
112 	old_cred = ovl_override_creds(dentry->d_sb);
113 	err = vfs_getattr(&realpath, stat);
114 	revert_creds(old_cred);
115 	return err;
116 }
117 
118 int ovl_permission(struct inode *inode, int mask)
119 {
120 	bool is_upper;
121 	struct inode *realinode = ovl_inode_real(inode, &is_upper);
122 	const struct cred *old_cred;
123 	int err;
124 
125 	/* Careful in RCU walk mode */
126 	if (!realinode) {
127 		WARN_ON(!(mask & MAY_NOT_BLOCK));
128 		return -ECHILD;
129 	}
130 
131 	/*
132 	 * Check overlay inode with the creds of task and underlying inode
133 	 * with creds of mounter
134 	 */
135 	err = generic_permission(inode, mask);
136 	if (err)
137 		return err;
138 
139 	old_cred = ovl_override_creds(inode->i_sb);
140 	if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
141 		mask &= ~(MAY_WRITE | MAY_APPEND);
142 		/* Make sure mounter can read file for copy up later */
143 		mask |= MAY_READ;
144 	}
145 	err = inode_permission(realinode, mask);
146 	revert_creds(old_cred);
147 
148 	return err;
149 }
150 
151 static const char *ovl_get_link(struct dentry *dentry,
152 				struct inode *inode,
153 				struct delayed_call *done)
154 {
155 	struct dentry *realdentry;
156 	struct inode *realinode;
157 	const struct cred *old_cred;
158 	const char *p;
159 
160 	if (!dentry)
161 		return ERR_PTR(-ECHILD);
162 
163 	realdentry = ovl_dentry_real(dentry);
164 	realinode = realdentry->d_inode;
165 
166 	if (WARN_ON(!realinode->i_op->get_link))
167 		return ERR_PTR(-EPERM);
168 
169 	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;
173 }
174 
175 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
176 {
177 	struct path realpath;
178 	struct inode *realinode;
179 	const struct cred *old_cred;
180 	int err;
181 
182 	ovl_path_real(dentry, &realpath);
183 	realinode = realpath.dentry->d_inode;
184 
185 	if (!realinode->i_op->readlink)
186 		return -EINVAL;
187 
188 	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;
192 }
193 
194 static bool ovl_is_private_xattr(const char *name)
195 {
196 #define OVL_XATTR_PRE_NAME OVL_XATTR_PREFIX "."
197 	return strncmp(name, OVL_XATTR_PRE_NAME,
198 		       sizeof(OVL_XATTR_PRE_NAME) - 1) == 0;
199 }
200 
201 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
202 		 const char *name, const void *value,
203 		 size_t size, int flags)
204 {
205 	int err;
206 	struct dentry *upperdentry;
207 	const struct cred *old_cred;
208 
209 	err = ovl_want_write(dentry);
210 	if (err)
211 		goto out;
212 
213 	err = ovl_copy_up(dentry);
214 	if (err)
215 		goto out_drop_write;
216 
217 	upperdentry = ovl_dentry_upper(dentry);
218 	old_cred = ovl_override_creds(dentry->d_sb);
219 	err = vfs_setxattr(upperdentry, name, value, size, flags);
220 	revert_creds(old_cred);
221 
222 out_drop_write:
223 	ovl_drop_write(dentry);
224 out:
225 	return err;
226 }
227 
228 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
229 		     const char *name, void *value, size_t size)
230 {
231 	struct dentry *realdentry = ovl_dentry_real(dentry);
232 	ssize_t res;
233 	const struct cred *old_cred;
234 
235 	if (ovl_is_private_xattr(name))
236 		return -ENODATA;
237 
238 	old_cred = ovl_override_creds(dentry->d_sb);
239 	res = vfs_getxattr(realdentry, name, value, size);
240 	revert_creds(old_cred);
241 	return res;
242 }
243 
244 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
245 {
246 	struct dentry *realdentry = ovl_dentry_real(dentry);
247 	ssize_t res;
248 	int off;
249 	const struct cred *old_cred;
250 
251 	old_cred = ovl_override_creds(dentry->d_sb);
252 	res = vfs_listxattr(realdentry, list, size);
253 	revert_creds(old_cred);
254 	if (res <= 0 || size == 0)
255 		return res;
256 
257 	/* filter out private xattrs */
258 	for (off = 0; off < res;) {
259 		char *s = list + off;
260 		size_t slen = strlen(s) + 1;
261 
262 		BUG_ON(off + slen > res);
263 
264 		if (ovl_is_private_xattr(s)) {
265 			res -= slen;
266 			memmove(s, s + slen, res - off);
267 		} else {
268 			off += slen;
269 		}
270 	}
271 
272 	return res;
273 }
274 
275 int ovl_removexattr(struct dentry *dentry, const char *name)
276 {
277 	int err;
278 	struct path realpath;
279 	enum ovl_path_type type = ovl_path_real(dentry, &realpath);
280 	const struct cred *old_cred;
281 
282 	err = ovl_want_write(dentry);
283 	if (err)
284 		goto out;
285 
286 	err = -ENODATA;
287 	if (ovl_is_private_xattr(name))
288 		goto out_drop_write;
289 
290 	if (!OVL_TYPE_UPPER(type)) {
291 		err = vfs_getxattr(realpath.dentry, name, NULL, 0);
292 		if (err < 0)
293 			goto out_drop_write;
294 
295 		err = ovl_copy_up(dentry);
296 		if (err)
297 			goto out_drop_write;
298 
299 		ovl_path_upper(dentry, &realpath);
300 	}
301 
302 	old_cred = ovl_override_creds(dentry->d_sb);
303 	err = vfs_removexattr(realpath.dentry, name);
304 	revert_creds(old_cred);
305 out_drop_write:
306 	ovl_drop_write(dentry);
307 out:
308 	return err;
309 }
310 
311 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
312 {
313 	struct inode *realinode = ovl_inode_real(inode, NULL);
314 	const struct cred *old_cred;
315 	struct posix_acl *acl;
316 
317 	if (!IS_POSIXACL(realinode))
318 		return NULL;
319 
320 	if (!realinode->i_op->get_acl)
321 		return NULL;
322 
323 	old_cred = ovl_override_creds(inode->i_sb);
324 	acl = realinode->i_op->get_acl(realinode, type);
325 	revert_creds(old_cred);
326 
327 	return acl;
328 }
329 
330 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
331 				  struct dentry *realdentry)
332 {
333 	if (OVL_TYPE_UPPER(type))
334 		return false;
335 
336 	if (special_file(realdentry->d_inode->i_mode))
337 		return false;
338 
339 	if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
340 		return false;
341 
342 	return true;
343 }
344 
345 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
346 {
347 	int err = 0;
348 	struct path realpath;
349 	enum ovl_path_type type;
350 
351 	type = ovl_path_real(dentry, &realpath);
352 	if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
353 		err = ovl_want_write(dentry);
354 		if (!err) {
355 			if (file_flags & O_TRUNC)
356 				err = ovl_copy_up_truncate(dentry);
357 			else
358 				err = ovl_copy_up(dentry);
359 			ovl_drop_write(dentry);
360 		}
361 	}
362 
363 	return err;
364 }
365 
366 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
367 {
368 	struct dentry *alias;
369 	struct path upperpath;
370 
371 	if (!(flags & S_ATIME))
372 		return 0;
373 
374 	alias = d_find_any_alias(inode);
375 	if (!alias)
376 		return 0;
377 
378 	ovl_path_upper(alias, &upperpath);
379 	if (upperpath.dentry) {
380 		touch_atime(&upperpath);
381 		inode->i_atime = d_inode(upperpath.dentry)->i_atime;
382 	}
383 
384 	dput(alias);
385 
386 	return 0;
387 }
388 
389 static const struct inode_operations ovl_file_inode_operations = {
390 	.setattr	= ovl_setattr,
391 	.permission	= ovl_permission,
392 	.getattr	= ovl_getattr,
393 	.setxattr	= generic_setxattr,
394 	.getxattr	= ovl_getxattr,
395 	.listxattr	= ovl_listxattr,
396 	.removexattr	= ovl_removexattr,
397 	.get_acl	= ovl_get_acl,
398 	.update_time	= ovl_update_time,
399 };
400 
401 static const struct inode_operations ovl_symlink_inode_operations = {
402 	.setattr	= ovl_setattr,
403 	.get_link	= ovl_get_link,
404 	.readlink	= ovl_readlink,
405 	.getattr	= ovl_getattr,
406 	.setxattr	= generic_setxattr,
407 	.getxattr	= ovl_getxattr,
408 	.listxattr	= ovl_listxattr,
409 	.removexattr	= ovl_removexattr,
410 	.update_time	= ovl_update_time,
411 };
412 
413 static void ovl_fill_inode(struct inode *inode, umode_t mode)
414 {
415 	inode->i_ino = get_next_ino();
416 	inode->i_mode = mode;
417 	inode->i_flags |= S_NOCMTIME;
418 
419 	mode &= S_IFMT;
420 	switch (mode) {
421 	case S_IFDIR:
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 	default:
431 		WARN(1, "illegal file type: %i\n", mode);
432 		/* Fall through */
433 
434 	case S_IFREG:
435 	case S_IFSOCK:
436 	case S_IFBLK:
437 	case S_IFCHR:
438 	case S_IFIFO:
439 		inode->i_op = &ovl_file_inode_operations;
440 		break;
441 	}
442 }
443 
444 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode)
445 {
446 	struct inode *inode;
447 
448 	inode = new_inode(sb);
449 	if (inode)
450 		ovl_fill_inode(inode, mode);
451 
452 	return inode;
453 }
454 
455 static int ovl_inode_test(struct inode *inode, void *data)
456 {
457 	return ovl_inode_real(inode, NULL) == data;
458 }
459 
460 static int ovl_inode_set(struct inode *inode, void *data)
461 {
462 	inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
463 	return 0;
464 }
465 
466 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
467 
468 {
469 	struct inode *inode;
470 
471 	inode = iget5_locked(sb, (unsigned long) realinode,
472 			     ovl_inode_test, ovl_inode_set, realinode);
473 	if (inode && inode->i_state & I_NEW) {
474 		ovl_fill_inode(inode, realinode->i_mode);
475 		set_nlink(inode, realinode->i_nlink);
476 		unlock_new_inode(inode);
477 	}
478 
479 	return inode;
480 }
481