xref: /openbmc/linux/fs/overlayfs/inode.c (revision 174cd4b1)
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/cred.h>
13 #include <linux/xattr.h>
14 #include <linux/posix_acl.h>
15 #include "overlayfs.h"
16 
17 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
18 {
19 	int err;
20 	struct dentry *upperdentry;
21 	const struct cred *old_cred;
22 
23 	/*
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 	 */
32 	err = setattr_prepare(dentry, attr);
33 	if (err)
34 		return err;
35 
36 	err = ovl_want_write(dentry);
37 	if (err)
38 		goto out;
39 
40 	err = ovl_copy_up(dentry);
41 	if (!err) {
42 		upperdentry = ovl_dentry_upper(dentry);
43 
44 		if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
45 			attr->ia_valid &= ~ATTR_MODE;
46 
47 		inode_lock(upperdentry->d_inode);
48 		old_cred = ovl_override_creds(dentry->d_sb);
49 		err = notify_change(upperdentry, attr, NULL);
50 		revert_creds(old_cred);
51 		if (!err)
52 			ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
53 		inode_unlock(upperdentry->d_inode);
54 	}
55 	ovl_drop_write(dentry);
56 out:
57 	return err;
58 }
59 
60 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
61 			 struct kstat *stat)
62 {
63 	struct path realpath;
64 	const struct cred *old_cred;
65 	int err;
66 
67 	ovl_path_real(dentry, &realpath);
68 	old_cred = ovl_override_creds(dentry->d_sb);
69 	err = vfs_getattr(&realpath, stat);
70 	revert_creds(old_cred);
71 	return err;
72 }
73 
74 int ovl_permission(struct inode *inode, int mask)
75 {
76 	bool is_upper;
77 	struct inode *realinode = ovl_inode_real(inode, &is_upper);
78 	const struct cred *old_cred;
79 	int err;
80 
81 	/* Careful in RCU walk mode */
82 	if (!realinode) {
83 		WARN_ON(!(mask & MAY_NOT_BLOCK));
84 		return -ECHILD;
85 	}
86 
87 	/*
88 	 * Check overlay inode with the creds of task and underlying inode
89 	 * with creds of mounter
90 	 */
91 	err = generic_permission(inode, mask);
92 	if (err)
93 		return err;
94 
95 	old_cred = ovl_override_creds(inode->i_sb);
96 	if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
97 		mask &= ~(MAY_WRITE | MAY_APPEND);
98 		/* Make sure mounter can read file for copy up later */
99 		mask |= MAY_READ;
100 	}
101 	err = inode_permission(realinode, mask);
102 	revert_creds(old_cred);
103 
104 	return err;
105 }
106 
107 static const char *ovl_get_link(struct dentry *dentry,
108 				struct inode *inode,
109 				struct delayed_call *done)
110 {
111 	const struct cred *old_cred;
112 	const char *p;
113 
114 	if (!dentry)
115 		return ERR_PTR(-ECHILD);
116 
117 	old_cred = ovl_override_creds(dentry->d_sb);
118 	p = vfs_get_link(ovl_dentry_real(dentry), done);
119 	revert_creds(old_cred);
120 	return p;
121 }
122 
123 bool ovl_is_private_xattr(const char *name)
124 {
125 	return strncmp(name, OVL_XATTR_PREFIX,
126 		       sizeof(OVL_XATTR_PREFIX) - 1) == 0;
127 }
128 
129 int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
130 		  size_t size, int flags)
131 {
132 	int err;
133 	struct path realpath;
134 	enum ovl_path_type type = ovl_path_real(dentry, &realpath);
135 	const struct cred *old_cred;
136 
137 	err = ovl_want_write(dentry);
138 	if (err)
139 		goto out;
140 
141 	if (!value && !OVL_TYPE_UPPER(type)) {
142 		err = vfs_getxattr(realpath.dentry, name, NULL, 0);
143 		if (err < 0)
144 			goto out_drop_write;
145 	}
146 
147 	err = ovl_copy_up(dentry);
148 	if (err)
149 		goto out_drop_write;
150 
151 	if (!OVL_TYPE_UPPER(type))
152 		ovl_path_upper(dentry, &realpath);
153 
154 	old_cred = ovl_override_creds(dentry->d_sb);
155 	if (value)
156 		err = vfs_setxattr(realpath.dentry, name, value, size, flags);
157 	else {
158 		WARN_ON(flags != XATTR_REPLACE);
159 		err = vfs_removexattr(realpath.dentry, name);
160 	}
161 	revert_creds(old_cred);
162 
163 out_drop_write:
164 	ovl_drop_write(dentry);
165 out:
166 	return err;
167 }
168 
169 int ovl_xattr_get(struct dentry *dentry, const char *name,
170 		  void *value, size_t size)
171 {
172 	struct dentry *realdentry = ovl_dentry_real(dentry);
173 	ssize_t res;
174 	const struct cred *old_cred;
175 
176 	old_cred = ovl_override_creds(dentry->d_sb);
177 	res = vfs_getxattr(realdentry, name, value, size);
178 	revert_creds(old_cred);
179 	return res;
180 }
181 
182 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
183 {
184 	struct dentry *realdentry = ovl_dentry_real(dentry);
185 	ssize_t res;
186 	size_t len;
187 	char *s;
188 	const struct cred *old_cred;
189 
190 	old_cred = ovl_override_creds(dentry->d_sb);
191 	res = vfs_listxattr(realdentry, list, size);
192 	revert_creds(old_cred);
193 	if (res <= 0 || size == 0)
194 		return res;
195 
196 	/* filter out private xattrs */
197 	for (s = list, len = res; len;) {
198 		size_t slen = strnlen(s, len) + 1;
199 
200 		/* underlying fs providing us with an broken xattr list? */
201 		if (WARN_ON(slen > len))
202 			return -EIO;
203 
204 		len -= slen;
205 		if (ovl_is_private_xattr(s)) {
206 			res -= slen;
207 			memmove(s, s + slen, len);
208 		} else {
209 			s += slen;
210 		}
211 	}
212 
213 	return res;
214 }
215 
216 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
217 {
218 	struct inode *realinode = ovl_inode_real(inode, NULL);
219 	const struct cred *old_cred;
220 	struct posix_acl *acl;
221 
222 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
223 		return NULL;
224 
225 	old_cred = ovl_override_creds(inode->i_sb);
226 	acl = get_acl(realinode, type);
227 	revert_creds(old_cred);
228 
229 	return acl;
230 }
231 
232 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
233 				  struct dentry *realdentry)
234 {
235 	if (OVL_TYPE_UPPER(type))
236 		return false;
237 
238 	if (special_file(realdentry->d_inode->i_mode))
239 		return false;
240 
241 	if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
242 		return false;
243 
244 	return true;
245 }
246 
247 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
248 {
249 	int err = 0;
250 	struct path realpath;
251 	enum ovl_path_type type;
252 
253 	type = ovl_path_real(dentry, &realpath);
254 	if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
255 		err = ovl_want_write(dentry);
256 		if (!err) {
257 			err = ovl_copy_up_flags(dentry, file_flags);
258 			ovl_drop_write(dentry);
259 		}
260 	}
261 
262 	return err;
263 }
264 
265 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
266 {
267 	struct dentry *alias;
268 	struct path upperpath;
269 
270 	if (!(flags & S_ATIME))
271 		return 0;
272 
273 	alias = d_find_any_alias(inode);
274 	if (!alias)
275 		return 0;
276 
277 	ovl_path_upper(alias, &upperpath);
278 	if (upperpath.dentry) {
279 		touch_atime(&upperpath);
280 		inode->i_atime = d_inode(upperpath.dentry)->i_atime;
281 	}
282 
283 	dput(alias);
284 
285 	return 0;
286 }
287 
288 static const struct inode_operations ovl_file_inode_operations = {
289 	.setattr	= ovl_setattr,
290 	.permission	= ovl_permission,
291 	.getattr	= ovl_getattr,
292 	.listxattr	= ovl_listxattr,
293 	.get_acl	= ovl_get_acl,
294 	.update_time	= ovl_update_time,
295 };
296 
297 static const struct inode_operations ovl_symlink_inode_operations = {
298 	.setattr	= ovl_setattr,
299 	.get_link	= ovl_get_link,
300 	.getattr	= ovl_getattr,
301 	.listxattr	= ovl_listxattr,
302 	.update_time	= ovl_update_time,
303 };
304 
305 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
306 {
307 	inode->i_ino = get_next_ino();
308 	inode->i_mode = mode;
309 	inode->i_flags |= S_NOCMTIME;
310 #ifdef CONFIG_FS_POSIX_ACL
311 	inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
312 #endif
313 
314 	switch (mode & S_IFMT) {
315 	case S_IFREG:
316 		inode->i_op = &ovl_file_inode_operations;
317 		break;
318 
319 	case S_IFDIR:
320 		inode->i_op = &ovl_dir_inode_operations;
321 		inode->i_fop = &ovl_dir_operations;
322 		break;
323 
324 	case S_IFLNK:
325 		inode->i_op = &ovl_symlink_inode_operations;
326 		break;
327 
328 	default:
329 		inode->i_op = &ovl_file_inode_operations;
330 		init_special_inode(inode, mode, rdev);
331 		break;
332 	}
333 }
334 
335 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
336 {
337 	struct inode *inode;
338 
339 	inode = new_inode(sb);
340 	if (inode)
341 		ovl_fill_inode(inode, mode, rdev);
342 
343 	return inode;
344 }
345 
346 static int ovl_inode_test(struct inode *inode, void *data)
347 {
348 	return ovl_inode_real(inode, NULL) == data;
349 }
350 
351 static int ovl_inode_set(struct inode *inode, void *data)
352 {
353 	inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
354 	return 0;
355 }
356 
357 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
358 
359 {
360 	struct inode *inode;
361 
362 	inode = iget5_locked(sb, (unsigned long) realinode,
363 			     ovl_inode_test, ovl_inode_set, realinode);
364 	if (inode && inode->i_state & I_NEW) {
365 		ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
366 		set_nlink(inode, realinode->i_nlink);
367 		unlock_new_inode(inode);
368 	}
369 
370 	return inode;
371 }
372