• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  linux/fs/namei.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6 
7 /*
8  * Some corrections by tytso.
9  */
10 
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12  * lookup logic.
13  */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15  */
16 
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/slab.h>
20 #include <linux/fs.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <linux/posix_acl.h>
36 #include <asm/uaccess.h>
37 
38 #include "internal.h"
39 #include "mount.h"
40 
41 /* [Feb-1997 T. Schoebel-Theuer]
42  * Fundamental changes in the pathname lookup mechanisms (namei)
43  * were necessary because of omirr.  The reason is that omirr needs
44  * to know the _real_ pathname, not the user-supplied one, in case
45  * of symlinks (and also when transname replacements occur).
46  *
47  * The new code replaces the old recursive symlink resolution with
48  * an iterative one (in case of non-nested symlink chains).  It does
49  * this with calls to <fs>_follow_link().
50  * As a side effect, dir_namei(), _namei() and follow_link() are now
51  * replaced with a single function lookup_dentry() that can handle all
52  * the special cases of the former code.
53  *
54  * With the new dcache, the pathname is stored at each inode, at least as
55  * long as the refcount of the inode is positive.  As a side effect, the
56  * size of the dcache depends on the inode cache and thus is dynamic.
57  *
58  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
59  * resolution to correspond with current state of the code.
60  *
61  * Note that the symlink resolution is not *completely* iterative.
62  * There is still a significant amount of tail- and mid- recursion in
63  * the algorithm.  Also, note that <fs>_readlink() is not used in
64  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
65  * may return different results than <fs>_follow_link().  Many virtual
66  * filesystems (including /proc) exhibit this behavior.
67  */
68 
69 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
70  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
71  * and the name already exists in form of a symlink, try to create the new
72  * name indicated by the symlink. The old code always complained that the
73  * name already exists, due to not following the symlink even if its target
74  * is nonexistent.  The new semantics affects also mknod() and link() when
75  * the name is a symlink pointing to a non-existent name.
76  *
77  * I don't know which semantics is the right one, since I have no access
78  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
79  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
80  * "old" one. Personally, I think the new semantics is much more logical.
81  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
82  * file does succeed in both HP-UX and SunOs, but not in Solaris
83  * and in the old Linux semantics.
84  */
85 
86 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
87  * semantics.  See the comments in "open_namei" and "do_link" below.
88  *
89  * [10-Sep-98 Alan Modra] Another symlink change.
90  */
91 
92 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
93  *	inside the path - always follow.
94  *	in the last component in creation/removal/renaming - never follow.
95  *	if LOOKUP_FOLLOW passed - follow.
96  *	if the pathname has trailing slashes - follow.
97  *	otherwise - don't follow.
98  * (applied in that order).
99  *
100  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
101  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
102  * During the 2.4 we need to fix the userland stuff depending on it -
103  * hopefully we will be able to get rid of that wart in 2.5. So far only
104  * XEmacs seems to be relying on it...
105  */
106 /*
107  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
108  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
109  * any extra contention...
110  */
111 
112 /* In order to reduce some races, while at the same time doing additional
113  * checking and hopefully speeding things up, we copy filenames to the
114  * kernel data space before using them..
115  *
116  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117  * PATH_MAX includes the nul terminator --RR.
118  */
do_getname(const char __user * filename,char * page)119 static int do_getname(const char __user *filename, char *page)
120 {
121 	int retval;
122 	unsigned long len = PATH_MAX;
123 
124 	if (!segment_eq(get_fs(), KERNEL_DS)) {
125 		if ((unsigned long) filename >= TASK_SIZE)
126 			return -EFAULT;
127 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
128 			len = TASK_SIZE - (unsigned long) filename;
129 	}
130 
131 	retval = strncpy_from_user(page, filename, len);
132 	if (retval > 0) {
133 		if (retval < len)
134 			return 0;
135 		return -ENAMETOOLONG;
136 	} else if (!retval)
137 		retval = -ENOENT;
138 	return retval;
139 }
140 
getname_flags(const char __user * filename,int flags,int * empty)141 static char *getname_flags(const char __user *filename, int flags, int *empty)
142 {
143 	char *result = __getname();
144 	int retval;
145 
146 	if (!result)
147 		return ERR_PTR(-ENOMEM);
148 
149 	retval = do_getname(filename, result);
150 	if (retval < 0) {
151 		if (retval == -ENOENT && empty)
152 			*empty = 1;
153 		if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
154 			__putname(result);
155 			return ERR_PTR(retval);
156 		}
157 	}
158 	audit_getname(result);
159 	return result;
160 }
161 
getname(const char __user * filename)162 char *getname(const char __user * filename)
163 {
164 	return getname_flags(filename, 0, NULL);
165 }
166 
167 #ifdef CONFIG_AUDITSYSCALL
putname(const char * name)168 void putname(const char *name)
169 {
170 	if (unlikely(!audit_dummy_context()))
171 		audit_putname(name);
172 	else
173 		__putname(name);
174 }
175 EXPORT_SYMBOL(putname);
176 #endif
177 
check_acl(struct inode * inode,int mask)178 static int check_acl(struct inode *inode, int mask)
179 {
180 #ifdef CONFIG_FS_POSIX_ACL
181 	struct posix_acl *acl;
182 
183 	if (mask & MAY_NOT_BLOCK) {
184 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
185 	        if (!acl)
186 	                return -EAGAIN;
187 		/* no ->get_acl() calls in RCU mode... */
188 		if (acl == ACL_NOT_CACHED)
189 			return -ECHILD;
190 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
191 	}
192 
193 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
194 
195 	/*
196 	 * A filesystem can force a ACL callback by just never filling the
197 	 * ACL cache. But normally you'd fill the cache either at inode
198 	 * instantiation time, or on the first ->get_acl call.
199 	 *
200 	 * If the filesystem doesn't have a get_acl() function at all, we'll
201 	 * just create the negative cache entry.
202 	 */
203 	if (acl == ACL_NOT_CACHED) {
204 	        if (inode->i_op->get_acl) {
205 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
206 			if (IS_ERR(acl))
207 				return PTR_ERR(acl);
208 		} else {
209 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
210 		        return -EAGAIN;
211 		}
212 	}
213 
214 	if (acl) {
215 	        int error = posix_acl_permission(inode, acl, mask);
216 	        posix_acl_release(acl);
217 	        return error;
218 	}
219 #endif
220 
221 	return -EAGAIN;
222 }
223 
224 /*
225  * This does the basic permission checking
226  */
acl_permission_check(struct inode * inode,int mask)227 static int acl_permission_check(struct inode *inode, int mask)
228 {
229 	unsigned int mode = inode->i_mode;
230 
231 	if (current_user_ns() != inode_userns(inode))
232 		goto other_perms;
233 
234 	if (likely(current_fsuid() == inode->i_uid))
235 		mode >>= 6;
236 	else {
237 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
238 			int error = check_acl(inode, mask);
239 			if (error != -EAGAIN)
240 				return error;
241 		}
242 
243 		if (in_group_p(inode->i_gid))
244 			mode >>= 3;
245 	}
246 
247 other_perms:
248 	/*
249 	 * If the DACs are ok we don't need any capability check.
250 	 */
251 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
252 		return 0;
253 	return -EACCES;
254 }
255 
256 /**
257  * generic_permission -  check for access rights on a Posix-like filesystem
258  * @inode:	inode to check access rights for
259  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
260  *
261  * Used to check for read/write/execute permissions on a file.
262  * We use "fsuid" for this, letting us set arbitrary permissions
263  * for filesystem access without changing the "normal" uids which
264  * are used for other things.
265  *
266  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
267  * request cannot be satisfied (eg. requires blocking or too much complexity).
268  * It would then be called again in ref-walk mode.
269  */
generic_permission(struct inode * inode,int mask)270 int generic_permission(struct inode *inode, int mask)
271 {
272 	int ret;
273 
274 	/*
275 	 * Do the basic permission checks.
276 	 */
277 	ret = acl_permission_check(inode, mask);
278 	if (ret != -EACCES)
279 		return ret;
280 
281 	if (S_ISDIR(inode->i_mode)) {
282 		/* DACs are overridable for directories */
283 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
284 			return 0;
285 		if (!(mask & MAY_WRITE))
286 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
287 				return 0;
288 		return -EACCES;
289 	}
290 	/*
291 	 * Read/write DACs are always overridable.
292 	 * Executable DACs are overridable when there is
293 	 * at least one exec bit set.
294 	 */
295 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
296 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
297 			return 0;
298 
299 	/*
300 	 * Searching includes executable on directories, else just read.
301 	 */
302 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
303 	if (mask == MAY_READ)
304 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
305 			return 0;
306 
307 	return -EACCES;
308 }
309 
310 /*
311  * We _really_ want to just do "generic_permission()" without
312  * even looking at the inode->i_op values. So we keep a cache
313  * flag in inode->i_opflags, that says "this has not special
314  * permission function, use the fast case".
315  */
do_inode_permission(struct inode * inode,int mask)316 static inline int do_inode_permission(struct inode *inode, int mask)
317 {
318 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
319 		if (likely(inode->i_op->permission))
320 			return inode->i_op->permission(inode, mask);
321 
322 		/* This gets set once for the inode lifetime */
323 		spin_lock(&inode->i_lock);
324 		inode->i_opflags |= IOP_FASTPERM;
325 		spin_unlock(&inode->i_lock);
326 	}
327 	return generic_permission(inode, mask);
328 }
329 
330 /**
331  * inode_permission  -  check for access rights to a given inode
332  * @inode:	inode to check permission on
333  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
334  *
335  * Used to check for read/write/execute permissions on an inode.
336  * We use "fsuid" for this, letting us set arbitrary permissions
337  * for filesystem access without changing the "normal" uids which
338  * are used for other things.
339  *
340  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
341  */
inode_permission(struct inode * inode,int mask)342 int inode_permission(struct inode *inode, int mask)
343 {
344 	int retval;
345 
346 	if (unlikely(mask & MAY_WRITE)) {
347 		umode_t mode = inode->i_mode;
348 
349 		/*
350 		 * Nobody gets write access to a read-only fs.
351 		 */
352 		if (IS_RDONLY(inode) &&
353 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
354 			return -EROFS;
355 
356 		/*
357 		 * Nobody gets write access to an immutable file.
358 		 */
359 		if (IS_IMMUTABLE(inode))
360 			return -EACCES;
361 	}
362 
363 	retval = do_inode_permission(inode, mask);
364 	if (retval)
365 		return retval;
366 
367 	retval = devcgroup_inode_permission(inode, mask);
368 	if (retval)
369 		return retval;
370 
371 	return security_inode_permission(inode, mask);
372 }
373 
374 /**
375  * path_get - get a reference to a path
376  * @path: path to get the reference to
377  *
378  * Given a path increment the reference count to the dentry and the vfsmount.
379  */
path_get(struct path * path)380 void path_get(struct path *path)
381 {
382 	mntget(path->mnt);
383 	dget(path->dentry);
384 }
385 EXPORT_SYMBOL(path_get);
386 
387 /**
388  * path_put - put a reference to a path
389  * @path: path to put the reference to
390  *
391  * Given a path decrement the reference count to the dentry and the vfsmount.
392  */
path_put(struct path * path)393 void path_put(struct path *path)
394 {
395 	dput(path->dentry);
396 	mntput(path->mnt);
397 }
398 EXPORT_SYMBOL(path_put);
399 
400 /*
401  * Path walking has 2 modes, rcu-walk and ref-walk (see
402  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
403  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
404  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
405  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
406  * got stuck, so ref-walk may continue from there. If this is not successful
407  * (eg. a seqcount has changed), then failure is returned and it's up to caller
408  * to restart the path walk from the beginning in ref-walk mode.
409  */
410 
411 /**
412  * unlazy_walk - try to switch to ref-walk mode.
413  * @nd: nameidata pathwalk data
414  * @dentry: child of nd->path.dentry or NULL
415  * Returns: 0 on success, -ECHILD on failure
416  *
417  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
418  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
419  * @nd or NULL.  Must be called from rcu-walk context.
420  */
unlazy_walk(struct nameidata * nd,struct dentry * dentry)421 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
422 {
423 	struct fs_struct *fs = current->fs;
424 	struct dentry *parent = nd->path.dentry;
425 	int want_root = 0;
426 
427 	BUG_ON(!(nd->flags & LOOKUP_RCU));
428 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
429 		want_root = 1;
430 		spin_lock(&fs->lock);
431 		if (nd->root.mnt != fs->root.mnt ||
432 				nd->root.dentry != fs->root.dentry)
433 			goto err_root;
434 	}
435 	spin_lock(&parent->d_lock);
436 	if (!dentry) {
437 		if (!__d_rcu_to_refcount(parent, nd->seq))
438 			goto err_parent;
439 		BUG_ON(nd->inode != parent->d_inode);
440 	} else {
441 		if (dentry->d_parent != parent)
442 			goto err_parent;
443 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
444 		if (!__d_rcu_to_refcount(dentry, nd->seq))
445 			goto err_child;
446 		/*
447 		 * If the sequence check on the child dentry passed, then
448 		 * the child has not been removed from its parent. This
449 		 * means the parent dentry must be valid and able to take
450 		 * a reference at this point.
451 		 */
452 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
453 		BUG_ON(!parent->d_count);
454 		parent->d_count++;
455 		spin_unlock(&dentry->d_lock);
456 	}
457 	spin_unlock(&parent->d_lock);
458 	if (want_root) {
459 		path_get(&nd->root);
460 		spin_unlock(&fs->lock);
461 	}
462 	mntget(nd->path.mnt);
463 
464 	rcu_read_unlock();
465 	br_read_unlock(&vfsmount_lock);
466 	nd->flags &= ~LOOKUP_RCU;
467 	return 0;
468 
469 err_child:
470 	spin_unlock(&dentry->d_lock);
471 err_parent:
472 	spin_unlock(&parent->d_lock);
473 err_root:
474 	if (want_root)
475 		spin_unlock(&fs->lock);
476 	return -ECHILD;
477 }
478 
479 /**
480  * release_open_intent - free up open intent resources
481  * @nd: pointer to nameidata
482  */
release_open_intent(struct nameidata * nd)483 void release_open_intent(struct nameidata *nd)
484 {
485 	struct file *file = nd->intent.open.file;
486 
487 	if (file && !IS_ERR(file)) {
488 		if (file->f_path.dentry == NULL)
489 			put_filp(file);
490 		else
491 			fput(file);
492 	}
493 }
494 
d_revalidate(struct dentry * dentry,struct nameidata * nd)495 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
496 {
497 	return dentry->d_op->d_revalidate(dentry, nd);
498 }
499 
500 /**
501  * complete_walk - successful completion of path walk
502  * @nd:  pointer nameidata
503  *
504  * If we had been in RCU mode, drop out of it and legitimize nd->path.
505  * Revalidate the final result, unless we'd already done that during
506  * the path walk or the filesystem doesn't ask for it.  Return 0 on
507  * success, -error on failure.  In case of failure caller does not
508  * need to drop nd->path.
509  */
complete_walk(struct nameidata * nd)510 static int complete_walk(struct nameidata *nd)
511 {
512 	struct dentry *dentry = nd->path.dentry;
513 	int status;
514 
515 	if (nd->flags & LOOKUP_RCU) {
516 		nd->flags &= ~LOOKUP_RCU;
517 		if (!(nd->flags & LOOKUP_ROOT))
518 			nd->root.mnt = NULL;
519 		spin_lock(&dentry->d_lock);
520 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
521 			spin_unlock(&dentry->d_lock);
522 			rcu_read_unlock();
523 			br_read_unlock(&vfsmount_lock);
524 			return -ECHILD;
525 		}
526 		BUG_ON(nd->inode != dentry->d_inode);
527 		spin_unlock(&dentry->d_lock);
528 		mntget(nd->path.mnt);
529 		rcu_read_unlock();
530 		br_read_unlock(&vfsmount_lock);
531 	}
532 
533 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
534 		return 0;
535 
536 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
537 		return 0;
538 
539 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
540 		return 0;
541 
542 	/* Note: we do not d_invalidate() */
543 	status = d_revalidate(dentry, nd);
544 	if (status > 0)
545 		return 0;
546 
547 	if (!status)
548 		status = -ESTALE;
549 
550 	path_put(&nd->path);
551 	return status;
552 }
553 
set_root(struct nameidata * nd)554 static __always_inline void set_root(struct nameidata *nd)
555 {
556 	if (!nd->root.mnt)
557 		get_fs_root(current->fs, &nd->root);
558 }
559 
560 static int link_path_walk(const char *, struct nameidata *);
561 
set_root_rcu(struct nameidata * nd)562 static __always_inline void set_root_rcu(struct nameidata *nd)
563 {
564 	if (!nd->root.mnt) {
565 		struct fs_struct *fs = current->fs;
566 		unsigned seq;
567 
568 		do {
569 			seq = read_seqcount_begin(&fs->seq);
570 			nd->root = fs->root;
571 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
572 		} while (read_seqcount_retry(&fs->seq, seq));
573 	}
574 }
575 
__vfs_follow_link(struct nameidata * nd,const char * link)576 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
577 {
578 	int ret;
579 
580 	if (IS_ERR(link))
581 		goto fail;
582 
583 	if (*link == '/') {
584 		set_root(nd);
585 		path_put(&nd->path);
586 		nd->path = nd->root;
587 		path_get(&nd->root);
588 		nd->flags |= LOOKUP_JUMPED;
589 	}
590 	nd->inode = nd->path.dentry->d_inode;
591 
592 	ret = link_path_walk(link, nd);
593 	return ret;
594 fail:
595 	path_put(&nd->path);
596 	return PTR_ERR(link);
597 }
598 
path_put_conditional(struct path * path,struct nameidata * nd)599 static void path_put_conditional(struct path *path, struct nameidata *nd)
600 {
601 	dput(path->dentry);
602 	if (path->mnt != nd->path.mnt)
603 		mntput(path->mnt);
604 }
605 
path_to_nameidata(const struct path * path,struct nameidata * nd)606 static inline void path_to_nameidata(const struct path *path,
607 					struct nameidata *nd)
608 {
609 	if (!(nd->flags & LOOKUP_RCU)) {
610 		dput(nd->path.dentry);
611 		if (nd->path.mnt != path->mnt)
612 			mntput(nd->path.mnt);
613 	}
614 	nd->path.mnt = path->mnt;
615 	nd->path.dentry = path->dentry;
616 }
617 
put_link(struct nameidata * nd,struct path * link,void * cookie)618 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
619 {
620 	struct inode *inode = link->dentry->d_inode;
621 	if (!IS_ERR(cookie) && inode->i_op->put_link)
622 		inode->i_op->put_link(link->dentry, nd, cookie);
623 	path_put(link);
624 }
625 
626 static __always_inline int
follow_link(struct path * link,struct nameidata * nd,void ** p)627 follow_link(struct path *link, struct nameidata *nd, void **p)
628 {
629 	int error;
630 	struct dentry *dentry = link->dentry;
631 
632 	BUG_ON(nd->flags & LOOKUP_RCU);
633 
634 	if (link->mnt == nd->path.mnt)
635 		mntget(link->mnt);
636 
637 	if (unlikely(current->total_link_count >= 40)) {
638 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
639 		path_put(&nd->path);
640 		return -ELOOP;
641 	}
642 	cond_resched();
643 	current->total_link_count++;
644 
645 	touch_atime(link);
646 	nd_set_link(nd, NULL);
647 
648 	error = security_inode_follow_link(link->dentry, nd);
649 	if (error) {
650 		*p = ERR_PTR(error); /* no ->put_link(), please */
651 		path_put(&nd->path);
652 		return error;
653 	}
654 
655 	nd->last_type = LAST_BIND;
656 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
657 	error = PTR_ERR(*p);
658 	if (!IS_ERR(*p)) {
659 		char *s = nd_get_link(nd);
660 		error = 0;
661 		if (s)
662 			error = __vfs_follow_link(nd, s);
663 		else if (nd->last_type == LAST_BIND) {
664 			nd->flags |= LOOKUP_JUMPED;
665 			nd->inode = nd->path.dentry->d_inode;
666 			if (nd->inode->i_op->follow_link) {
667 				/* stepped on a _really_ weird one */
668 				path_put(&nd->path);
669 				error = -ELOOP;
670 			}
671 		}
672 	}
673 	return error;
674 }
675 
follow_up_rcu(struct path * path)676 static int follow_up_rcu(struct path *path)
677 {
678 	struct mount *mnt = real_mount(path->mnt);
679 	struct mount *parent;
680 	struct dentry *mountpoint;
681 
682 	parent = mnt->mnt_parent;
683 	if (&parent->mnt == path->mnt)
684 		return 0;
685 	mountpoint = mnt->mnt_mountpoint;
686 	path->dentry = mountpoint;
687 	path->mnt = &parent->mnt;
688 	return 1;
689 }
690 
691 /*
692  * follow_up - Find the mountpoint of path's vfsmount
693  *
694  * Given a path, find the mountpoint of its source file system.
695  * Replace @path with the path of the mountpoint in the parent mount.
696  * Up is towards /.
697  *
698  * Return 1 if we went up a level and 0 if we were already at the
699  * root.
700  */
follow_up(struct path * path)701 int follow_up(struct path *path)
702 {
703 	struct mount *mnt = real_mount(path->mnt);
704 	struct mount *parent;
705 	struct dentry *mountpoint;
706 
707 	br_read_lock(&vfsmount_lock);
708 	parent = mnt->mnt_parent;
709 	if (&parent->mnt == path->mnt) {
710 		br_read_unlock(&vfsmount_lock);
711 		return 0;
712 	}
713 	mntget(&parent->mnt);
714 	mountpoint = dget(mnt->mnt_mountpoint);
715 	br_read_unlock(&vfsmount_lock);
716 	dput(path->dentry);
717 	path->dentry = mountpoint;
718 	mntput(path->mnt);
719 	path->mnt = &parent->mnt;
720 	return 1;
721 }
722 
723 /*
724  * Perform an automount
725  * - return -EISDIR to tell follow_managed() to stop and return the path we
726  *   were called with.
727  */
follow_automount(struct path * path,unsigned flags,bool * need_mntput)728 static int follow_automount(struct path *path, unsigned flags,
729 			    bool *need_mntput)
730 {
731 	struct vfsmount *mnt;
732 	int err;
733 
734 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
735 		return -EREMOTE;
736 
737 	/* We don't want to mount if someone's just doing a stat -
738 	 * unless they're stat'ing a directory and appended a '/' to
739 	 * the name.
740 	 *
741 	 * We do, however, want to mount if someone wants to open or
742 	 * create a file of any type under the mountpoint, wants to
743 	 * traverse through the mountpoint or wants to open the
744 	 * mounted directory.  Also, autofs may mark negative dentries
745 	 * as being automount points.  These will need the attentions
746 	 * of the daemon to instantiate them before they can be used.
747 	 */
748 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
749 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
750 	    path->dentry->d_inode)
751 		return -EISDIR;
752 
753 	current->total_link_count++;
754 	if (current->total_link_count >= 40)
755 		return -ELOOP;
756 
757 	mnt = path->dentry->d_op->d_automount(path);
758 	if (IS_ERR(mnt)) {
759 		/*
760 		 * The filesystem is allowed to return -EISDIR here to indicate
761 		 * it doesn't want to automount.  For instance, autofs would do
762 		 * this so that its userspace daemon can mount on this dentry.
763 		 *
764 		 * However, we can only permit this if it's a terminal point in
765 		 * the path being looked up; if it wasn't then the remainder of
766 		 * the path is inaccessible and we should say so.
767 		 */
768 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
769 			return -EREMOTE;
770 		return PTR_ERR(mnt);
771 	}
772 
773 	if (!mnt) /* mount collision */
774 		return 0;
775 
776 	if (!*need_mntput) {
777 		/* lock_mount() may release path->mnt on error */
778 		mntget(path->mnt);
779 		*need_mntput = true;
780 	}
781 	err = finish_automount(mnt, path);
782 
783 	switch (err) {
784 	case -EBUSY:
785 		/* Someone else made a mount here whilst we were busy */
786 		return 0;
787 	case 0:
788 		path_put(path);
789 		path->mnt = mnt;
790 		path->dentry = dget(mnt->mnt_root);
791 		return 0;
792 	default:
793 		return err;
794 	}
795 
796 }
797 
798 /*
799  * Handle a dentry that is managed in some way.
800  * - Flagged for transit management (autofs)
801  * - Flagged as mountpoint
802  * - Flagged as automount point
803  *
804  * This may only be called in refwalk mode.
805  *
806  * Serialization is taken care of in namespace.c
807  */
follow_managed(struct path * path,unsigned flags)808 static int follow_managed(struct path *path, unsigned flags)
809 {
810 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
811 	unsigned managed;
812 	bool need_mntput = false;
813 	int ret = 0;
814 
815 	/* Given that we're not holding a lock here, we retain the value in a
816 	 * local variable for each dentry as we look at it so that we don't see
817 	 * the components of that value change under us */
818 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
819 	       managed &= DCACHE_MANAGED_DENTRY,
820 	       unlikely(managed != 0)) {
821 		/* Allow the filesystem to manage the transit without i_mutex
822 		 * being held. */
823 		if (managed & DCACHE_MANAGE_TRANSIT) {
824 			BUG_ON(!path->dentry->d_op);
825 			BUG_ON(!path->dentry->d_op->d_manage);
826 			ret = path->dentry->d_op->d_manage(path->dentry, false);
827 			if (ret < 0)
828 				break;
829 		}
830 
831 		/* Transit to a mounted filesystem. */
832 		if (managed & DCACHE_MOUNTED) {
833 			struct vfsmount *mounted = lookup_mnt(path);
834 			if (mounted) {
835 				dput(path->dentry);
836 				if (need_mntput)
837 					mntput(path->mnt);
838 				path->mnt = mounted;
839 				path->dentry = dget(mounted->mnt_root);
840 				need_mntput = true;
841 				continue;
842 			}
843 
844 			/* Something is mounted on this dentry in another
845 			 * namespace and/or whatever was mounted there in this
846 			 * namespace got unmounted before we managed to get the
847 			 * vfsmount_lock */
848 		}
849 
850 		/* Handle an automount point */
851 		if (managed & DCACHE_NEED_AUTOMOUNT) {
852 			ret = follow_automount(path, flags, &need_mntput);
853 			if (ret < 0)
854 				break;
855 			continue;
856 		}
857 
858 		/* We didn't change the current path point */
859 		break;
860 	}
861 
862 	if (need_mntput && path->mnt == mnt)
863 		mntput(path->mnt);
864 	if (ret == -EISDIR)
865 		ret = 0;
866 	return ret < 0 ? ret : need_mntput;
867 }
868 
follow_down_one(struct path * path)869 int follow_down_one(struct path *path)
870 {
871 	struct vfsmount *mounted;
872 
873 	mounted = lookup_mnt(path);
874 	if (mounted) {
875 		dput(path->dentry);
876 		mntput(path->mnt);
877 		path->mnt = mounted;
878 		path->dentry = dget(mounted->mnt_root);
879 		return 1;
880 	}
881 	return 0;
882 }
883 
managed_dentry_might_block(struct dentry * dentry)884 static inline bool managed_dentry_might_block(struct dentry *dentry)
885 {
886 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
887 		dentry->d_op->d_manage(dentry, true) < 0);
888 }
889 
890 /*
891  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
892  * we meet a managed dentry that would need blocking.
893  */
__follow_mount_rcu(struct nameidata * nd,struct path * path,struct inode ** inode)894 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
895 			       struct inode **inode)
896 {
897 	for (;;) {
898 		struct mount *mounted;
899 		/*
900 		 * Don't forget we might have a non-mountpoint managed dentry
901 		 * that wants to block transit.
902 		 */
903 		if (unlikely(managed_dentry_might_block(path->dentry)))
904 			return false;
905 
906 		if (!d_mountpoint(path->dentry))
907 			break;
908 
909 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
910 		if (!mounted)
911 			break;
912 		path->mnt = &mounted->mnt;
913 		path->dentry = mounted->mnt.mnt_root;
914 		nd->flags |= LOOKUP_JUMPED;
915 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
916 		/*
917 		 * Update the inode too. We don't need to re-check the
918 		 * dentry sequence number here after this d_inode read,
919 		 * because a mount-point is always pinned.
920 		 */
921 		*inode = path->dentry->d_inode;
922 	}
923 	return true;
924 }
925 
follow_mount_rcu(struct nameidata * nd)926 static void follow_mount_rcu(struct nameidata *nd)
927 {
928 	while (d_mountpoint(nd->path.dentry)) {
929 		struct mount *mounted;
930 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
931 		if (!mounted)
932 			break;
933 		nd->path.mnt = &mounted->mnt;
934 		nd->path.dentry = mounted->mnt.mnt_root;
935 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
936 	}
937 }
938 
follow_dotdot_rcu(struct nameidata * nd)939 static int follow_dotdot_rcu(struct nameidata *nd)
940 {
941 	set_root_rcu(nd);
942 
943 	while (1) {
944 		if (nd->path.dentry == nd->root.dentry &&
945 		    nd->path.mnt == nd->root.mnt) {
946 			break;
947 		}
948 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
949 			struct dentry *old = nd->path.dentry;
950 			struct dentry *parent = old->d_parent;
951 			unsigned seq;
952 
953 			seq = read_seqcount_begin(&parent->d_seq);
954 			if (read_seqcount_retry(&old->d_seq, nd->seq))
955 				goto failed;
956 			nd->path.dentry = parent;
957 			nd->seq = seq;
958 			break;
959 		}
960 		if (!follow_up_rcu(&nd->path))
961 			break;
962 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
963 	}
964 	follow_mount_rcu(nd);
965 	nd->inode = nd->path.dentry->d_inode;
966 	return 0;
967 
968 failed:
969 	nd->flags &= ~LOOKUP_RCU;
970 	if (!(nd->flags & LOOKUP_ROOT))
971 		nd->root.mnt = NULL;
972 	rcu_read_unlock();
973 	br_read_unlock(&vfsmount_lock);
974 	return -ECHILD;
975 }
976 
977 /*
978  * Follow down to the covering mount currently visible to userspace.  At each
979  * point, the filesystem owning that dentry may be queried as to whether the
980  * caller is permitted to proceed or not.
981  */
follow_down(struct path * path)982 int follow_down(struct path *path)
983 {
984 	unsigned managed;
985 	int ret;
986 
987 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
988 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
989 		/* Allow the filesystem to manage the transit without i_mutex
990 		 * being held.
991 		 *
992 		 * We indicate to the filesystem if someone is trying to mount
993 		 * something here.  This gives autofs the chance to deny anyone
994 		 * other than its daemon the right to mount on its
995 		 * superstructure.
996 		 *
997 		 * The filesystem may sleep at this point.
998 		 */
999 		if (managed & DCACHE_MANAGE_TRANSIT) {
1000 			BUG_ON(!path->dentry->d_op);
1001 			BUG_ON(!path->dentry->d_op->d_manage);
1002 			ret = path->dentry->d_op->d_manage(
1003 				path->dentry, false);
1004 			if (ret < 0)
1005 				return ret == -EISDIR ? 0 : ret;
1006 		}
1007 
1008 		/* Transit to a mounted filesystem. */
1009 		if (managed & DCACHE_MOUNTED) {
1010 			struct vfsmount *mounted = lookup_mnt(path);
1011 			if (!mounted)
1012 				break;
1013 			dput(path->dentry);
1014 			mntput(path->mnt);
1015 			path->mnt = mounted;
1016 			path->dentry = dget(mounted->mnt_root);
1017 			continue;
1018 		}
1019 
1020 		/* Don't handle automount points here */
1021 		break;
1022 	}
1023 	return 0;
1024 }
1025 
1026 /*
1027  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1028  */
follow_mount(struct path * path)1029 static void follow_mount(struct path *path)
1030 {
1031 	while (d_mountpoint(path->dentry)) {
1032 		struct vfsmount *mounted = lookup_mnt(path);
1033 		if (!mounted)
1034 			break;
1035 		dput(path->dentry);
1036 		mntput(path->mnt);
1037 		path->mnt = mounted;
1038 		path->dentry = dget(mounted->mnt_root);
1039 	}
1040 }
1041 
follow_dotdot(struct nameidata * nd)1042 static void follow_dotdot(struct nameidata *nd)
1043 {
1044 	set_root(nd);
1045 
1046 	while(1) {
1047 		struct dentry *old = nd->path.dentry;
1048 
1049 		if (nd->path.dentry == nd->root.dentry &&
1050 		    nd->path.mnt == nd->root.mnt) {
1051 			break;
1052 		}
1053 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
1054 			/* rare case of legitimate dget_parent()... */
1055 			nd->path.dentry = dget_parent(nd->path.dentry);
1056 			dput(old);
1057 			break;
1058 		}
1059 		if (!follow_up(&nd->path))
1060 			break;
1061 	}
1062 	follow_mount(&nd->path);
1063 	nd->inode = nd->path.dentry->d_inode;
1064 }
1065 
1066 /*
1067  * This looks up the name in dcache, possibly revalidates the old dentry and
1068  * allocates a new one if not found or not valid.  In the need_lookup argument
1069  * returns whether i_op->lookup is necessary.
1070  *
1071  * dir->d_inode->i_mutex must be held
1072  */
lookup_dcache(struct qstr * name,struct dentry * dir,struct nameidata * nd,bool * need_lookup)1073 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1074 				    struct nameidata *nd, bool *need_lookup)
1075 {
1076 	struct dentry *dentry;
1077 	int error;
1078 
1079 	*need_lookup = false;
1080 	dentry = d_lookup(dir, name);
1081 	if (dentry) {
1082 		if (d_need_lookup(dentry)) {
1083 			*need_lookup = true;
1084 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1085 			error = d_revalidate(dentry, nd);
1086 			if (unlikely(error <= 0)) {
1087 				if (error < 0) {
1088 					dput(dentry);
1089 					return ERR_PTR(error);
1090 				} else if (!d_invalidate(dentry)) {
1091 					dput(dentry);
1092 					dentry = NULL;
1093 				}
1094 			}
1095 		}
1096 	}
1097 
1098 	if (!dentry) {
1099 		dentry = d_alloc(dir, name);
1100 		if (unlikely(!dentry))
1101 			return ERR_PTR(-ENOMEM);
1102 
1103 		*need_lookup = true;
1104 	}
1105 	return dentry;
1106 }
1107 
1108 /*
1109  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1110  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1111  *
1112  * dir->d_inode->i_mutex must be held
1113  */
lookup_real(struct inode * dir,struct dentry * dentry,struct nameidata * nd)1114 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1115 				  struct nameidata *nd)
1116 {
1117 	struct dentry *old;
1118 
1119 	/* Don't create child dentry for a dead directory. */
1120 	if (unlikely(IS_DEADDIR(dir))) {
1121 		dput(dentry);
1122 		return ERR_PTR(-ENOENT);
1123 	}
1124 
1125 	old = dir->i_op->lookup(dir, dentry, nd);
1126 	if (unlikely(old)) {
1127 		dput(dentry);
1128 		dentry = old;
1129 	}
1130 	return dentry;
1131 }
1132 
__lookup_hash(struct qstr * name,struct dentry * base,struct nameidata * nd)1133 static struct dentry *__lookup_hash(struct qstr *name,
1134 		struct dentry *base, struct nameidata *nd)
1135 {
1136 	bool need_lookup;
1137 	struct dentry *dentry;
1138 
1139 	dentry = lookup_dcache(name, base, nd, &need_lookup);
1140 	if (!need_lookup)
1141 		return dentry;
1142 
1143 	return lookup_real(base->d_inode, dentry, nd);
1144 }
1145 
1146 /*
1147  *  It's more convoluted than I'd like it to be, but... it's still fairly
1148  *  small and for now I'd prefer to have fast path as straight as possible.
1149  *  It _is_ time-critical.
1150  */
do_lookup(struct nameidata * nd,struct qstr * name,struct path * path,struct inode ** inode)1151 static int do_lookup(struct nameidata *nd, struct qstr *name,
1152 			struct path *path, struct inode **inode)
1153 {
1154 	struct vfsmount *mnt = nd->path.mnt;
1155 	struct dentry *dentry, *parent = nd->path.dentry;
1156 	int need_reval = 1;
1157 	int status = 1;
1158 	int err;
1159 
1160 	/*
1161 	 * Rename seqlock is not required here because in the off chance
1162 	 * of a false negative due to a concurrent rename, we're going to
1163 	 * do the non-racy lookup, below.
1164 	 */
1165 	if (nd->flags & LOOKUP_RCU) {
1166 		unsigned seq;
1167 		*inode = nd->inode;
1168 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
1169 		if (!dentry)
1170 			goto unlazy;
1171 
1172 		/* Memory barrier in read_seqcount_begin of child is enough */
1173 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1174 			return -ECHILD;
1175 		nd->seq = seq;
1176 
1177 		if (unlikely(d_need_lookup(dentry)))
1178 			goto unlazy;
1179 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1180 			status = d_revalidate(dentry, nd);
1181 			if (unlikely(status <= 0)) {
1182 				if (status != -ECHILD)
1183 					need_reval = 0;
1184 				goto unlazy;
1185 			}
1186 		}
1187 		path->mnt = mnt;
1188 		path->dentry = dentry;
1189 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1190 			goto unlazy;
1191 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1192 			goto unlazy;
1193 		return 0;
1194 unlazy:
1195 		if (unlazy_walk(nd, dentry))
1196 			return -ECHILD;
1197 	} else {
1198 		dentry = __d_lookup(parent, name);
1199 	}
1200 
1201 	if (unlikely(!dentry))
1202 		goto need_lookup;
1203 
1204 	if (unlikely(d_need_lookup(dentry))) {
1205 		dput(dentry);
1206 		goto need_lookup;
1207 	}
1208 
1209 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1210 		status = d_revalidate(dentry, nd);
1211 	if (unlikely(status <= 0)) {
1212 		if (status < 0) {
1213 			dput(dentry);
1214 			return status;
1215 		}
1216 		if (!d_invalidate(dentry)) {
1217 			dput(dentry);
1218 			goto need_lookup;
1219 		}
1220 	}
1221 done:
1222 	path->mnt = mnt;
1223 	path->dentry = dentry;
1224 	err = follow_managed(path, nd->flags);
1225 	if (unlikely(err < 0)) {
1226 		path_put_conditional(path, nd);
1227 		return err;
1228 	}
1229 	if (err)
1230 		nd->flags |= LOOKUP_JUMPED;
1231 	*inode = path->dentry->d_inode;
1232 	return 0;
1233 
1234 need_lookup:
1235 	BUG_ON(nd->inode != parent->d_inode);
1236 
1237 	mutex_lock(&parent->d_inode->i_mutex);
1238 	dentry = __lookup_hash(name, parent, nd);
1239 	mutex_unlock(&parent->d_inode->i_mutex);
1240 	if (IS_ERR(dentry))
1241 		return PTR_ERR(dentry);
1242 	goto done;
1243 }
1244 
may_lookup(struct nameidata * nd)1245 static inline int may_lookup(struct nameidata *nd)
1246 {
1247 	if (nd->flags & LOOKUP_RCU) {
1248 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1249 		if (err != -ECHILD)
1250 			return err;
1251 		if (unlazy_walk(nd, NULL))
1252 			return -ECHILD;
1253 	}
1254 	return inode_permission(nd->inode, MAY_EXEC);
1255 }
1256 
handle_dots(struct nameidata * nd,int type)1257 static inline int handle_dots(struct nameidata *nd, int type)
1258 {
1259 	if (type == LAST_DOTDOT) {
1260 		if (nd->flags & LOOKUP_RCU) {
1261 			if (follow_dotdot_rcu(nd))
1262 				return -ECHILD;
1263 		} else
1264 			follow_dotdot(nd);
1265 	}
1266 	return 0;
1267 }
1268 
terminate_walk(struct nameidata * nd)1269 static void terminate_walk(struct nameidata *nd)
1270 {
1271 	if (!(nd->flags & LOOKUP_RCU)) {
1272 		path_put(&nd->path);
1273 	} else {
1274 		nd->flags &= ~LOOKUP_RCU;
1275 		if (!(nd->flags & LOOKUP_ROOT))
1276 			nd->root.mnt = NULL;
1277 		rcu_read_unlock();
1278 		br_read_unlock(&vfsmount_lock);
1279 	}
1280 }
1281 
1282 /*
1283  * Do we need to follow links? We _really_ want to be able
1284  * to do this check without having to look at inode->i_op,
1285  * so we keep a cache of "no, this doesn't need follow_link"
1286  * for the common case.
1287  */
should_follow_link(struct inode * inode,int follow)1288 static inline int should_follow_link(struct inode *inode, int follow)
1289 {
1290 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1291 		if (likely(inode->i_op->follow_link))
1292 			return follow;
1293 
1294 		/* This gets set once for the inode lifetime */
1295 		spin_lock(&inode->i_lock);
1296 		inode->i_opflags |= IOP_NOFOLLOW;
1297 		spin_unlock(&inode->i_lock);
1298 	}
1299 	return 0;
1300 }
1301 
walk_component(struct nameidata * nd,struct path * path,struct qstr * name,int type,int follow)1302 static inline int walk_component(struct nameidata *nd, struct path *path,
1303 		struct qstr *name, int type, int follow)
1304 {
1305 	struct inode *inode;
1306 	int err;
1307 	/*
1308 	 * "." and ".." are special - ".." especially so because it has
1309 	 * to be able to know about the current root directory and
1310 	 * parent relationships.
1311 	 */
1312 	if (unlikely(type != LAST_NORM))
1313 		return handle_dots(nd, type);
1314 	err = do_lookup(nd, name, path, &inode);
1315 	if (unlikely(err)) {
1316 		terminate_walk(nd);
1317 		return err;
1318 	}
1319 	if (!inode) {
1320 		path_to_nameidata(path, nd);
1321 		terminate_walk(nd);
1322 		return -ENOENT;
1323 	}
1324 	if (should_follow_link(inode, follow)) {
1325 		if (nd->flags & LOOKUP_RCU) {
1326 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1327 				terminate_walk(nd);
1328 				return -ECHILD;
1329 			}
1330 		}
1331 		BUG_ON(inode != path->dentry->d_inode);
1332 		return 1;
1333 	}
1334 	path_to_nameidata(path, nd);
1335 	nd->inode = inode;
1336 	return 0;
1337 }
1338 
1339 /*
1340  * This limits recursive symlink follows to 8, while
1341  * limiting consecutive symlinks to 40.
1342  *
1343  * Without that kind of total limit, nasty chains of consecutive
1344  * symlinks can cause almost arbitrarily long lookups.
1345  */
nested_symlink(struct path * path,struct nameidata * nd)1346 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1347 {
1348 	int res;
1349 
1350 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1351 		path_put_conditional(path, nd);
1352 		path_put(&nd->path);
1353 		return -ELOOP;
1354 	}
1355 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1356 
1357 	nd->depth++;
1358 	current->link_count++;
1359 
1360 	do {
1361 		struct path link = *path;
1362 		void *cookie;
1363 
1364 		res = follow_link(&link, nd, &cookie);
1365 		if (!res)
1366 			res = walk_component(nd, path, &nd->last,
1367 					     nd->last_type, LOOKUP_FOLLOW);
1368 		put_link(nd, &link, cookie);
1369 	} while (res > 0);
1370 
1371 	current->link_count--;
1372 	nd->depth--;
1373 	return res;
1374 }
1375 
1376 /*
1377  * We really don't want to look at inode->i_op->lookup
1378  * when we don't have to. So we keep a cache bit in
1379  * the inode ->i_opflags field that says "yes, we can
1380  * do lookup on this inode".
1381  */
can_lookup(struct inode * inode)1382 static inline int can_lookup(struct inode *inode)
1383 {
1384 	if (likely(inode->i_opflags & IOP_LOOKUP))
1385 		return 1;
1386 	if (likely(!inode->i_op->lookup))
1387 		return 0;
1388 
1389 	/* We do this once for the lifetime of the inode */
1390 	spin_lock(&inode->i_lock);
1391 	inode->i_opflags |= IOP_LOOKUP;
1392 	spin_unlock(&inode->i_lock);
1393 	return 1;
1394 }
1395 
1396 /*
1397  * We can do the critical dentry name comparison and hashing
1398  * operations one word at a time, but we are limited to:
1399  *
1400  * - Architectures with fast unaligned word accesses. We could
1401  *   do a "get_unaligned()" if this helps and is sufficiently
1402  *   fast.
1403  *
1404  * - Little-endian machines (so that we can generate the mask
1405  *   of low bytes efficiently). Again, we *could* do a byte
1406  *   swapping load on big-endian architectures if that is not
1407  *   expensive enough to make the optimization worthless.
1408  *
1409  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1410  *   do not trap on the (extremely unlikely) case of a page
1411  *   crossing operation.
1412  *
1413  * - Furthermore, we need an efficient 64-bit compile for the
1414  *   64-bit case in order to generate the "number of bytes in
1415  *   the final mask". Again, that could be replaced with a
1416  *   efficient population count instruction or similar.
1417  */
1418 #ifdef CONFIG_DCACHE_WORD_ACCESS
1419 
1420 #include <asm/word-at-a-time.h>
1421 
1422 #ifdef CONFIG_64BIT
1423 
fold_hash(unsigned long hash)1424 static inline unsigned int fold_hash(unsigned long hash)
1425 {
1426 	hash += hash >> (8*sizeof(int));
1427 	return hash;
1428 }
1429 
1430 #else	/* 32-bit case */
1431 
1432 #define fold_hash(x) (x)
1433 
1434 #endif
1435 
full_name_hash(const unsigned char * name,unsigned int len)1436 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1437 {
1438 	unsigned long a, mask;
1439 	unsigned long hash = 0;
1440 
1441 	for (;;) {
1442 		a = load_unaligned_zeropad(name);
1443 		if (len < sizeof(unsigned long))
1444 			break;
1445 		hash += a;
1446 		hash *= 9;
1447 		name += sizeof(unsigned long);
1448 		len -= sizeof(unsigned long);
1449 		if (!len)
1450 			goto done;
1451 	}
1452 	mask = ~(~0ul << len*8);
1453 	hash += mask & a;
1454 done:
1455 	return fold_hash(hash);
1456 }
1457 EXPORT_SYMBOL(full_name_hash);
1458 
1459 /*
1460  * Calculate the length and hash of the path component, and
1461  * return the length of the component;
1462  */
hash_name(const char * name,unsigned int * hashp)1463 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1464 {
1465 	unsigned long a, mask, hash, len;
1466 
1467 	hash = a = 0;
1468 	len = -sizeof(unsigned long);
1469 	do {
1470 		hash = (hash + a) * 9;
1471 		len += sizeof(unsigned long);
1472 		a = load_unaligned_zeropad(name+len);
1473 		/* Do we have any NUL or '/' bytes in this word? */
1474 		mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
1475 	} while (!mask);
1476 
1477 	/* The mask *below* the first high bit set */
1478 	mask = (mask - 1) & ~mask;
1479 	mask >>= 7;
1480 	hash += a & mask;
1481 	*hashp = fold_hash(hash);
1482 
1483 	return len + count_masked_bytes(mask);
1484 }
1485 
1486 #else
1487 
full_name_hash(const unsigned char * name,unsigned int len)1488 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1489 {
1490 	unsigned long hash = init_name_hash();
1491 	while (len--)
1492 		hash = partial_name_hash(*name++, hash);
1493 	return end_name_hash(hash);
1494 }
1495 EXPORT_SYMBOL(full_name_hash);
1496 
1497 /*
1498  * We know there's a real path component here of at least
1499  * one character.
1500  */
hash_name(const char * name,unsigned int * hashp)1501 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1502 {
1503 	unsigned long hash = init_name_hash();
1504 	unsigned long len = 0, c;
1505 
1506 	c = (unsigned char)*name;
1507 	do {
1508 		len++;
1509 		hash = partial_name_hash(c, hash);
1510 		c = (unsigned char)name[len];
1511 	} while (c && c != '/');
1512 	*hashp = end_name_hash(hash);
1513 	return len;
1514 }
1515 
1516 #endif
1517 
1518 /*
1519  * Name resolution.
1520  * This is the basic name resolution function, turning a pathname into
1521  * the final dentry. We expect 'base' to be positive and a directory.
1522  *
1523  * Returns 0 and nd will have valid dentry and mnt on success.
1524  * Returns error and drops reference to input namei data on failure.
1525  */
link_path_walk(const char * name,struct nameidata * nd)1526 static int link_path_walk(const char *name, struct nameidata *nd)
1527 {
1528 	struct path next;
1529 	int err;
1530 
1531 	while (*name=='/')
1532 		name++;
1533 	if (!*name)
1534 		return 0;
1535 
1536 	/* At this point we know we have a real path component. */
1537 	for(;;) {
1538 		struct qstr this;
1539 		long len;
1540 		int type;
1541 
1542 		err = may_lookup(nd);
1543  		if (err)
1544 			break;
1545 
1546 		len = hash_name(name, &this.hash);
1547 		this.name = name;
1548 		this.len = len;
1549 
1550 		type = LAST_NORM;
1551 		if (name[0] == '.') switch (len) {
1552 			case 2:
1553 				if (name[1] == '.') {
1554 					type = LAST_DOTDOT;
1555 					nd->flags |= LOOKUP_JUMPED;
1556 				}
1557 				break;
1558 			case 1:
1559 				type = LAST_DOT;
1560 		}
1561 		if (likely(type == LAST_NORM)) {
1562 			struct dentry *parent = nd->path.dentry;
1563 			nd->flags &= ~LOOKUP_JUMPED;
1564 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1565 				err = parent->d_op->d_hash(parent, nd->inode,
1566 							   &this);
1567 				if (err < 0)
1568 					break;
1569 			}
1570 		}
1571 
1572 		if (!name[len])
1573 			goto last_component;
1574 		/*
1575 		 * If it wasn't NUL, we know it was '/'. Skip that
1576 		 * slash, and continue until no more slashes.
1577 		 */
1578 		do {
1579 			len++;
1580 		} while (unlikely(name[len] == '/'));
1581 		if (!name[len])
1582 			goto last_component;
1583 		name += len;
1584 
1585 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1586 		if (err < 0)
1587 			return err;
1588 
1589 		if (err) {
1590 			err = nested_symlink(&next, nd);
1591 			if (err)
1592 				return err;
1593 		}
1594 		if (can_lookup(nd->inode))
1595 			continue;
1596 		err = -ENOTDIR;
1597 		break;
1598 		/* here ends the main loop */
1599 
1600 last_component:
1601 		nd->last = this;
1602 		nd->last_type = type;
1603 		return 0;
1604 	}
1605 	terminate_walk(nd);
1606 	return err;
1607 }
1608 
path_init(int dfd,const char * name,unsigned int flags,struct nameidata * nd,struct file ** fp)1609 static int path_init(int dfd, const char *name, unsigned int flags,
1610 		     struct nameidata *nd, struct file **fp)
1611 {
1612 	int retval = 0;
1613 	int fput_needed;
1614 	struct file *file;
1615 
1616 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
1617 	nd->flags = flags | LOOKUP_JUMPED;
1618 	nd->depth = 0;
1619 	if (flags & LOOKUP_ROOT) {
1620 		struct inode *inode = nd->root.dentry->d_inode;
1621 		if (*name) {
1622 			if (!inode->i_op->lookup)
1623 				return -ENOTDIR;
1624 			retval = inode_permission(inode, MAY_EXEC);
1625 			if (retval)
1626 				return retval;
1627 		}
1628 		nd->path = nd->root;
1629 		nd->inode = inode;
1630 		if (flags & LOOKUP_RCU) {
1631 			br_read_lock(&vfsmount_lock);
1632 			rcu_read_lock();
1633 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1634 		} else {
1635 			path_get(&nd->path);
1636 		}
1637 		return 0;
1638 	}
1639 
1640 	nd->root.mnt = NULL;
1641 
1642 	if (*name=='/') {
1643 		if (flags & LOOKUP_RCU) {
1644 			br_read_lock(&vfsmount_lock);
1645 			rcu_read_lock();
1646 			set_root_rcu(nd);
1647 		} else {
1648 			set_root(nd);
1649 			path_get(&nd->root);
1650 		}
1651 		nd->path = nd->root;
1652 	} else if (dfd == AT_FDCWD) {
1653 		if (flags & LOOKUP_RCU) {
1654 			struct fs_struct *fs = current->fs;
1655 			unsigned seq;
1656 
1657 			br_read_lock(&vfsmount_lock);
1658 			rcu_read_lock();
1659 
1660 			do {
1661 				seq = read_seqcount_begin(&fs->seq);
1662 				nd->path = fs->pwd;
1663 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1664 			} while (read_seqcount_retry(&fs->seq, seq));
1665 		} else {
1666 			get_fs_pwd(current->fs, &nd->path);
1667 		}
1668 	} else {
1669 		struct dentry *dentry;
1670 
1671 		file = fget_raw_light(dfd, &fput_needed);
1672 		retval = -EBADF;
1673 		if (!file)
1674 			goto out_fail;
1675 
1676 		dentry = file->f_path.dentry;
1677 
1678 		if (*name) {
1679 			retval = -ENOTDIR;
1680 			if (!S_ISDIR(dentry->d_inode->i_mode))
1681 				goto fput_fail;
1682 
1683 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
1684 			if (retval)
1685 				goto fput_fail;
1686 		}
1687 
1688 		nd->path = file->f_path;
1689 		if (flags & LOOKUP_RCU) {
1690 			if (fput_needed)
1691 				*fp = file;
1692 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1693 			br_read_lock(&vfsmount_lock);
1694 			rcu_read_lock();
1695 		} else {
1696 			path_get(&file->f_path);
1697 			fput_light(file, fput_needed);
1698 		}
1699 	}
1700 
1701 	nd->inode = nd->path.dentry->d_inode;
1702 	return 0;
1703 
1704 fput_fail:
1705 	fput_light(file, fput_needed);
1706 out_fail:
1707 	return retval;
1708 }
1709 
lookup_last(struct nameidata * nd,struct path * path)1710 static inline int lookup_last(struct nameidata *nd, struct path *path)
1711 {
1712 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1713 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1714 
1715 	nd->flags &= ~LOOKUP_PARENT;
1716 	return walk_component(nd, path, &nd->last, nd->last_type,
1717 					nd->flags & LOOKUP_FOLLOW);
1718 }
1719 
1720 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
path_lookupat(int dfd,const char * name,unsigned int flags,struct nameidata * nd)1721 static int path_lookupat(int dfd, const char *name,
1722 				unsigned int flags, struct nameidata *nd)
1723 {
1724 	struct file *base = NULL;
1725 	struct path path;
1726 	int err;
1727 
1728 	/*
1729 	 * Path walking is largely split up into 2 different synchronisation
1730 	 * schemes, rcu-walk and ref-walk (explained in
1731 	 * Documentation/filesystems/path-lookup.txt). These share much of the
1732 	 * path walk code, but some things particularly setup, cleanup, and
1733 	 * following mounts are sufficiently divergent that functions are
1734 	 * duplicated. Typically there is a function foo(), and its RCU
1735 	 * analogue, foo_rcu().
1736 	 *
1737 	 * -ECHILD is the error number of choice (just to avoid clashes) that
1738 	 * is returned if some aspect of an rcu-walk fails. Such an error must
1739 	 * be handled by restarting a traditional ref-walk (which will always
1740 	 * be able to complete).
1741 	 */
1742 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1743 
1744 	if (unlikely(err))
1745 		return err;
1746 
1747 	current->total_link_count = 0;
1748 	err = link_path_walk(name, nd);
1749 
1750 	if (!err && !(flags & LOOKUP_PARENT)) {
1751 		err = lookup_last(nd, &path);
1752 		while (err > 0) {
1753 			void *cookie;
1754 			struct path link = path;
1755 			nd->flags |= LOOKUP_PARENT;
1756 			err = follow_link(&link, nd, &cookie);
1757 			if (!err)
1758 				err = lookup_last(nd, &path);
1759 			put_link(nd, &link, cookie);
1760 		}
1761 	}
1762 
1763 	if (!err)
1764 		err = complete_walk(nd);
1765 
1766 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1767 		if (!nd->inode->i_op->lookup) {
1768 			path_put(&nd->path);
1769 			err = -ENOTDIR;
1770 		}
1771 	}
1772 
1773 	if (base)
1774 		fput(base);
1775 
1776 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1777 		path_put(&nd->root);
1778 		nd->root.mnt = NULL;
1779 	}
1780 	return err;
1781 }
1782 
do_path_lookup(int dfd,const char * name,unsigned int flags,struct nameidata * nd)1783 static int do_path_lookup(int dfd, const char *name,
1784 				unsigned int flags, struct nameidata *nd)
1785 {
1786 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1787 	if (unlikely(retval == -ECHILD))
1788 		retval = path_lookupat(dfd, name, flags, nd);
1789 	if (unlikely(retval == -ESTALE))
1790 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1791 
1792 	if (likely(!retval)) {
1793 		if (unlikely(!audit_dummy_context())) {
1794 			if (nd->path.dentry && nd->inode)
1795 				audit_inode(name, nd->path.dentry);
1796 		}
1797 	}
1798 	return retval;
1799 }
1800 
kern_path_parent(const char * name,struct nameidata * nd)1801 int kern_path_parent(const char *name, struct nameidata *nd)
1802 {
1803 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1804 }
1805 
kern_path(const char * name,unsigned int flags,struct path * path)1806 int kern_path(const char *name, unsigned int flags, struct path *path)
1807 {
1808 	struct nameidata nd;
1809 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1810 	if (!res)
1811 		*path = nd.path;
1812 	return res;
1813 }
1814 
1815 /**
1816  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1817  * @dentry:  pointer to dentry of the base directory
1818  * @mnt: pointer to vfs mount of the base directory
1819  * @name: pointer to file name
1820  * @flags: lookup flags
1821  * @path: pointer to struct path to fill
1822  */
vfs_path_lookup(struct dentry * dentry,struct vfsmount * mnt,const char * name,unsigned int flags,struct path * path)1823 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1824 		    const char *name, unsigned int flags,
1825 		    struct path *path)
1826 {
1827 	struct nameidata nd;
1828 	int err;
1829 	nd.root.dentry = dentry;
1830 	nd.root.mnt = mnt;
1831 	BUG_ON(flags & LOOKUP_PARENT);
1832 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1833 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1834 	if (!err)
1835 		*path = nd.path;
1836 	return err;
1837 }
1838 
1839 /*
1840  * Restricted form of lookup. Doesn't follow links, single-component only,
1841  * needs parent already locked. Doesn't follow mounts.
1842  * SMP-safe.
1843  */
lookup_hash(struct nameidata * nd)1844 static struct dentry *lookup_hash(struct nameidata *nd)
1845 {
1846 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
1847 }
1848 
1849 /**
1850  * lookup_one_len - filesystem helper to lookup single pathname component
1851  * @name:	pathname component to lookup
1852  * @base:	base directory to lookup from
1853  * @len:	maximum length @len should be interpreted to
1854  *
1855  * Note that this routine is purely a helper for filesystem usage and should
1856  * not be called by generic code.  Also note that by using this function the
1857  * nameidata argument is passed to the filesystem methods and a filesystem
1858  * using this helper needs to be prepared for that.
1859  */
lookup_one_len(const char * name,struct dentry * base,int len)1860 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1861 {
1862 	struct qstr this;
1863 	unsigned int c;
1864 	int err;
1865 
1866 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1867 
1868 	this.name = name;
1869 	this.len = len;
1870 	this.hash = full_name_hash(name, len);
1871 	if (!len)
1872 		return ERR_PTR(-EACCES);
1873 
1874 	while (len--) {
1875 		c = *(const unsigned char *)name++;
1876 		if (c == '/' || c == '\0')
1877 			return ERR_PTR(-EACCES);
1878 	}
1879 	/*
1880 	 * See if the low-level filesystem might want
1881 	 * to use its own hash..
1882 	 */
1883 	if (base->d_flags & DCACHE_OP_HASH) {
1884 		int err = base->d_op->d_hash(base, base->d_inode, &this);
1885 		if (err < 0)
1886 			return ERR_PTR(err);
1887 	}
1888 
1889 	err = inode_permission(base->d_inode, MAY_EXEC);
1890 	if (err)
1891 		return ERR_PTR(err);
1892 
1893 	return __lookup_hash(&this, base, NULL);
1894 }
1895 
user_path_at_empty(int dfd,const char __user * name,unsigned flags,struct path * path,int * empty)1896 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1897 		 struct path *path, int *empty)
1898 {
1899 	struct nameidata nd;
1900 	char *tmp = getname_flags(name, flags, empty);
1901 	int err = PTR_ERR(tmp);
1902 	if (!IS_ERR(tmp)) {
1903 
1904 		BUG_ON(flags & LOOKUP_PARENT);
1905 
1906 		err = do_path_lookup(dfd, tmp, flags, &nd);
1907 		putname(tmp);
1908 		if (!err)
1909 			*path = nd.path;
1910 	}
1911 	return err;
1912 }
1913 
user_path_at(int dfd,const char __user * name,unsigned flags,struct path * path)1914 int user_path_at(int dfd, const char __user *name, unsigned flags,
1915 		 struct path *path)
1916 {
1917 	return user_path_at_empty(dfd, name, flags, path, NULL);
1918 }
1919 
user_path_parent(int dfd,const char __user * path,struct nameidata * nd,char ** name)1920 static int user_path_parent(int dfd, const char __user *path,
1921 			struct nameidata *nd, char **name)
1922 {
1923 	char *s = getname(path);
1924 	int error;
1925 
1926 	if (IS_ERR(s))
1927 		return PTR_ERR(s);
1928 
1929 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1930 	if (error)
1931 		putname(s);
1932 	else
1933 		*name = s;
1934 
1935 	return error;
1936 }
1937 
1938 /*
1939  * It's inline, so penalty for filesystems that don't use sticky bit is
1940  * minimal.
1941  */
check_sticky(struct inode * dir,struct inode * inode)1942 static inline int check_sticky(struct inode *dir, struct inode *inode)
1943 {
1944 	uid_t fsuid = current_fsuid();
1945 
1946 	if (!(dir->i_mode & S_ISVTX))
1947 		return 0;
1948 	if (current_user_ns() != inode_userns(inode))
1949 		goto other_userns;
1950 	if (inode->i_uid == fsuid)
1951 		return 0;
1952 	if (dir->i_uid == fsuid)
1953 		return 0;
1954 
1955 other_userns:
1956 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
1957 }
1958 
1959 /*
1960  *	Check whether we can remove a link victim from directory dir, check
1961  *  whether the type of victim is right.
1962  *  1. We can't do it if dir is read-only (done in permission())
1963  *  2. We should have write and exec permissions on dir
1964  *  3. We can't remove anything from append-only dir
1965  *  4. We can't do anything with immutable dir (done in permission())
1966  *  5. If the sticky bit on dir is set we should either
1967  *	a. be owner of dir, or
1968  *	b. be owner of victim, or
1969  *	c. have CAP_FOWNER capability
1970  *  6. If the victim is append-only or immutable we can't do antyhing with
1971  *     links pointing to it.
1972  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1973  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1974  *  9. We can't remove a root or mountpoint.
1975  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1976  *     nfs_async_unlink().
1977  */
may_delete(struct inode * dir,struct dentry * victim,int isdir)1978 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1979 {
1980 	int error;
1981 
1982 	if (!victim->d_inode)
1983 		return -ENOENT;
1984 
1985 	BUG_ON(victim->d_parent->d_inode != dir);
1986 	audit_inode_child(victim, dir);
1987 
1988 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1989 	if (error)
1990 		return error;
1991 	if (IS_APPEND(dir))
1992 		return -EPERM;
1993 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1994 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1995 		return -EPERM;
1996 	if (isdir) {
1997 		if (!S_ISDIR(victim->d_inode->i_mode))
1998 			return -ENOTDIR;
1999 		if (IS_ROOT(victim))
2000 			return -EBUSY;
2001 	} else if (S_ISDIR(victim->d_inode->i_mode))
2002 		return -EISDIR;
2003 	if (IS_DEADDIR(dir))
2004 		return -ENOENT;
2005 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2006 		return -EBUSY;
2007 	return 0;
2008 }
2009 
2010 /*	Check whether we can create an object with dentry child in directory
2011  *  dir.
2012  *  1. We can't do it if child already exists (open has special treatment for
2013  *     this case, but since we are inlined it's OK)
2014  *  2. We can't do it if dir is read-only (done in permission())
2015  *  3. We should have write and exec permissions on dir
2016  *  4. We can't do it if dir is immutable (done in permission())
2017  */
may_create(struct inode * dir,struct dentry * child)2018 static inline int may_create(struct inode *dir, struct dentry *child)
2019 {
2020 	if (child->d_inode)
2021 		return -EEXIST;
2022 	if (IS_DEADDIR(dir))
2023 		return -ENOENT;
2024 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2025 }
2026 
2027 /*
2028  * p1 and p2 should be directories on the same fs.
2029  */
lock_rename(struct dentry * p1,struct dentry * p2)2030 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2031 {
2032 	struct dentry *p;
2033 
2034 	if (p1 == p2) {
2035 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2036 		return NULL;
2037 	}
2038 
2039 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2040 
2041 	p = d_ancestor(p2, p1);
2042 	if (p) {
2043 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2044 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2045 		return p;
2046 	}
2047 
2048 	p = d_ancestor(p1, p2);
2049 	if (p) {
2050 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2051 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2052 		return p;
2053 	}
2054 
2055 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2056 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2057 	return NULL;
2058 }
2059 
unlock_rename(struct dentry * p1,struct dentry * p2)2060 void unlock_rename(struct dentry *p1, struct dentry *p2)
2061 {
2062 	mutex_unlock(&p1->d_inode->i_mutex);
2063 	if (p1 != p2) {
2064 		mutex_unlock(&p2->d_inode->i_mutex);
2065 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2066 	}
2067 }
2068 
vfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,struct nameidata * nd)2069 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2070 		struct nameidata *nd)
2071 {
2072 	int error = may_create(dir, dentry);
2073 
2074 	if (error)
2075 		return error;
2076 
2077 	if (!dir->i_op->create)
2078 		return -EACCES;	/* shouldn't it be ENOSYS? */
2079 	mode &= S_IALLUGO;
2080 	mode |= S_IFREG;
2081 	error = security_inode_create(dir, dentry, mode);
2082 	if (error)
2083 		return error;
2084 	error = dir->i_op->create(dir, dentry, mode, nd);
2085 	if (!error)
2086 		fsnotify_create(dir, dentry);
2087 	return error;
2088 }
2089 
may_open(struct path * path,int acc_mode,int flag)2090 static int may_open(struct path *path, int acc_mode, int flag)
2091 {
2092 	struct dentry *dentry = path->dentry;
2093 	struct inode *inode = dentry->d_inode;
2094 	int error;
2095 
2096 	/* O_PATH? */
2097 	if (!acc_mode)
2098 		return 0;
2099 
2100 	if (!inode)
2101 		return -ENOENT;
2102 
2103 	switch (inode->i_mode & S_IFMT) {
2104 	case S_IFLNK:
2105 		return -ELOOP;
2106 	case S_IFDIR:
2107 		if (acc_mode & MAY_WRITE)
2108 			return -EISDIR;
2109 		break;
2110 	case S_IFBLK:
2111 	case S_IFCHR:
2112 		if (path->mnt->mnt_flags & MNT_NODEV)
2113 			return -EACCES;
2114 		/*FALLTHRU*/
2115 	case S_IFIFO:
2116 	case S_IFSOCK:
2117 		flag &= ~O_TRUNC;
2118 		break;
2119 	}
2120 
2121 	error = inode_permission(inode, acc_mode);
2122 	if (error)
2123 		return error;
2124 
2125 	/*
2126 	 * An append-only file must be opened in append mode for writing.
2127 	 */
2128 	if (IS_APPEND(inode)) {
2129 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2130 			return -EPERM;
2131 		if (flag & O_TRUNC)
2132 			return -EPERM;
2133 	}
2134 
2135 	/* O_NOATIME can only be set by the owner or superuser */
2136 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2137 		return -EPERM;
2138 
2139 	return 0;
2140 }
2141 
handle_truncate(struct file * filp)2142 static int handle_truncate(struct file *filp)
2143 {
2144 	struct path *path = &filp->f_path;
2145 	struct inode *inode = path->dentry->d_inode;
2146 	int error = get_write_access(inode);
2147 	if (error)
2148 		return error;
2149 	/*
2150 	 * Refuse to truncate files with mandatory locks held on them.
2151 	 */
2152 	error = locks_verify_locked(inode);
2153 	if (!error)
2154 		error = security_path_truncate(path);
2155 	if (!error) {
2156 		error = do_truncate(path->dentry, 0,
2157 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2158 				    filp);
2159 	}
2160 	put_write_access(inode);
2161 	return error;
2162 }
2163 
open_to_namei_flags(int flag)2164 static inline int open_to_namei_flags(int flag)
2165 {
2166 	if ((flag & O_ACCMODE) == 3)
2167 		flag--;
2168 	return flag;
2169 }
2170 
2171 /*
2172  * Handle the last step of open()
2173  */
do_last(struct nameidata * nd,struct path * path,const struct open_flags * op,const char * pathname)2174 static struct file *do_last(struct nameidata *nd, struct path *path,
2175 			    const struct open_flags *op, const char *pathname)
2176 {
2177 	struct dentry *dir = nd->path.dentry;
2178 	struct dentry *dentry;
2179 	int open_flag = op->open_flag;
2180 	int will_truncate = open_flag & O_TRUNC;
2181 	int want_write = 0;
2182 	int acc_mode = op->acc_mode;
2183 	struct file *filp;
2184 	int error;
2185 
2186 	nd->flags &= ~LOOKUP_PARENT;
2187 	nd->flags |= op->intent;
2188 
2189 	switch (nd->last_type) {
2190 	case LAST_DOTDOT:
2191 	case LAST_DOT:
2192 		error = handle_dots(nd, nd->last_type);
2193 		if (error)
2194 			return ERR_PTR(error);
2195 		/* fallthrough */
2196 	case LAST_ROOT:
2197 		error = complete_walk(nd);
2198 		if (error)
2199 			return ERR_PTR(error);
2200 		audit_inode(pathname, nd->path.dentry);
2201 		if (open_flag & O_CREAT) {
2202 			error = -EISDIR;
2203 			goto exit;
2204 		}
2205 		goto ok;
2206 	case LAST_BIND:
2207 		error = complete_walk(nd);
2208 		if (error)
2209 			return ERR_PTR(error);
2210 		audit_inode(pathname, dir);
2211 		goto ok;
2212 	}
2213 
2214 	if (!(open_flag & O_CREAT)) {
2215 		int symlink_ok = 0;
2216 		if (nd->last.name[nd->last.len])
2217 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2218 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2219 			symlink_ok = 1;
2220 		/* we _can_ be in RCU mode here */
2221 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2222 					!symlink_ok);
2223 		if (error < 0)
2224 			return ERR_PTR(error);
2225 		if (error) /* symlink */
2226 			return NULL;
2227 		/* sayonara */
2228 		error = complete_walk(nd);
2229 		if (error)
2230 			return ERR_PTR(error);
2231 
2232 		error = -ENOTDIR;
2233 		if (nd->flags & LOOKUP_DIRECTORY) {
2234 			if (!nd->inode->i_op->lookup)
2235 				goto exit;
2236 		}
2237 		audit_inode(pathname, nd->path.dentry);
2238 		goto ok;
2239 	}
2240 
2241 	/* create side of things */
2242 	/*
2243 	 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2244 	 * cleared when we got to the last component we are about to look up
2245 	 */
2246 	error = complete_walk(nd);
2247 	if (error)
2248 		return ERR_PTR(error);
2249 
2250 	audit_inode(pathname, dir);
2251 	error = -EISDIR;
2252 	/* trailing slashes? */
2253 	if (nd->last.name[nd->last.len])
2254 		goto exit;
2255 
2256 	mutex_lock(&dir->d_inode->i_mutex);
2257 
2258 	dentry = lookup_hash(nd);
2259 	error = PTR_ERR(dentry);
2260 	if (IS_ERR(dentry)) {
2261 		mutex_unlock(&dir->d_inode->i_mutex);
2262 		goto exit;
2263 	}
2264 
2265 	path->dentry = dentry;
2266 	path->mnt = nd->path.mnt;
2267 
2268 	/* Negative dentry, just create the file */
2269 	if (!dentry->d_inode) {
2270 		umode_t mode = op->mode;
2271 		if (!IS_POSIXACL(dir->d_inode))
2272 			mode &= ~current_umask();
2273 		/*
2274 		 * This write is needed to ensure that a
2275 		 * rw->ro transition does not occur between
2276 		 * the time when the file is created and when
2277 		 * a permanent write count is taken through
2278 		 * the 'struct file' in nameidata_to_filp().
2279 		 */
2280 		error = mnt_want_write(nd->path.mnt);
2281 		if (error)
2282 			goto exit_mutex_unlock;
2283 		want_write = 1;
2284 		/* Don't check for write permission, don't truncate */
2285 		open_flag &= ~O_TRUNC;
2286 		will_truncate = 0;
2287 		acc_mode = MAY_OPEN;
2288 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2289 		if (error)
2290 			goto exit_mutex_unlock;
2291 		error = vfs_create(dir->d_inode, dentry, mode, nd);
2292 		if (error)
2293 			goto exit_mutex_unlock;
2294 		mutex_unlock(&dir->d_inode->i_mutex);
2295 		dput(nd->path.dentry);
2296 		nd->path.dentry = dentry;
2297 		goto common;
2298 	}
2299 
2300 	/*
2301 	 * It already exists.
2302 	 */
2303 	mutex_unlock(&dir->d_inode->i_mutex);
2304 	audit_inode(pathname, path->dentry);
2305 
2306 	error = -EEXIST;
2307 	if (open_flag & O_EXCL)
2308 		goto exit_dput;
2309 
2310 	error = follow_managed(path, nd->flags);
2311 	if (error < 0)
2312 		goto exit_dput;
2313 
2314 	if (error)
2315 		nd->flags |= LOOKUP_JUMPED;
2316 
2317 	error = -ENOENT;
2318 	if (!path->dentry->d_inode)
2319 		goto exit_dput;
2320 
2321 	if (path->dentry->d_inode->i_op->follow_link)
2322 		return NULL;
2323 
2324 	path_to_nameidata(path, nd);
2325 	nd->inode = path->dentry->d_inode;
2326 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2327 	error = complete_walk(nd);
2328 	if (error)
2329 		return ERR_PTR(error);
2330 	error = -EISDIR;
2331 	if (S_ISDIR(nd->inode->i_mode))
2332 		goto exit;
2333 ok:
2334 	if (!S_ISREG(nd->inode->i_mode))
2335 		will_truncate = 0;
2336 
2337 	if (will_truncate) {
2338 		error = mnt_want_write(nd->path.mnt);
2339 		if (error)
2340 			goto exit;
2341 		want_write = 1;
2342 	}
2343 common:
2344 	error = may_open(&nd->path, acc_mode, open_flag);
2345 	if (error)
2346 		goto exit;
2347 	filp = nameidata_to_filp(nd);
2348 	if (!IS_ERR(filp)) {
2349 		error = ima_file_check(filp, op->acc_mode);
2350 		if (error) {
2351 			fput(filp);
2352 			filp = ERR_PTR(error);
2353 		}
2354 	}
2355 	if (!IS_ERR(filp)) {
2356 		if (will_truncate) {
2357 			error = handle_truncate(filp);
2358 			if (error) {
2359 				fput(filp);
2360 				filp = ERR_PTR(error);
2361 			}
2362 		}
2363 	}
2364 out:
2365 	if (want_write)
2366 		mnt_drop_write(nd->path.mnt);
2367 	path_put(&nd->path);
2368 	return filp;
2369 
2370 exit_mutex_unlock:
2371 	mutex_unlock(&dir->d_inode->i_mutex);
2372 exit_dput:
2373 	path_put_conditional(path, nd);
2374 exit:
2375 	filp = ERR_PTR(error);
2376 	goto out;
2377 }
2378 
path_openat(int dfd,const char * pathname,struct nameidata * nd,const struct open_flags * op,int flags)2379 static struct file *path_openat(int dfd, const char *pathname,
2380 		struct nameidata *nd, const struct open_flags *op, int flags)
2381 {
2382 	struct file *base = NULL;
2383 	struct file *filp;
2384 	struct path path;
2385 	int error;
2386 
2387 	filp = get_empty_filp();
2388 	if (!filp)
2389 		return ERR_PTR(-ENFILE);
2390 
2391 	filp->f_flags = op->open_flag;
2392 	nd->intent.open.file = filp;
2393 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2394 	nd->intent.open.create_mode = op->mode;
2395 
2396 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2397 	if (unlikely(error))
2398 		goto out_filp;
2399 
2400 	current->total_link_count = 0;
2401 	error = link_path_walk(pathname, nd);
2402 	if (unlikely(error))
2403 		goto out_filp;
2404 
2405 	filp = do_last(nd, &path, op, pathname);
2406 	while (unlikely(!filp)) { /* trailing symlink */
2407 		struct path link = path;
2408 		void *cookie;
2409 		if (!(nd->flags & LOOKUP_FOLLOW)) {
2410 			path_put_conditional(&path, nd);
2411 			path_put(&nd->path);
2412 			filp = ERR_PTR(-ELOOP);
2413 			break;
2414 		}
2415 		nd->flags |= LOOKUP_PARENT;
2416 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2417 		error = follow_link(&link, nd, &cookie);
2418 		if (unlikely(error))
2419 			filp = ERR_PTR(error);
2420 		else
2421 			filp = do_last(nd, &path, op, pathname);
2422 		put_link(nd, &link, cookie);
2423 	}
2424 out:
2425 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2426 		path_put(&nd->root);
2427 	if (base)
2428 		fput(base);
2429 	release_open_intent(nd);
2430 	return filp;
2431 
2432 out_filp:
2433 	filp = ERR_PTR(error);
2434 	goto out;
2435 }
2436 
do_filp_open(int dfd,const char * pathname,const struct open_flags * op,int flags)2437 struct file *do_filp_open(int dfd, const char *pathname,
2438 		const struct open_flags *op, int flags)
2439 {
2440 	struct nameidata nd;
2441 	struct file *filp;
2442 
2443 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2444 	if (unlikely(filp == ERR_PTR(-ECHILD)))
2445 		filp = path_openat(dfd, pathname, &nd, op, flags);
2446 	if (unlikely(filp == ERR_PTR(-ESTALE)))
2447 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2448 	return filp;
2449 }
2450 
do_file_open_root(struct dentry * dentry,struct vfsmount * mnt,const char * name,const struct open_flags * op,int flags)2451 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2452 		const char *name, const struct open_flags *op, int flags)
2453 {
2454 	struct nameidata nd;
2455 	struct file *file;
2456 
2457 	nd.root.mnt = mnt;
2458 	nd.root.dentry = dentry;
2459 
2460 	flags |= LOOKUP_ROOT;
2461 
2462 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2463 		return ERR_PTR(-ELOOP);
2464 
2465 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2466 	if (unlikely(file == ERR_PTR(-ECHILD)))
2467 		file = path_openat(-1, name, &nd, op, flags);
2468 	if (unlikely(file == ERR_PTR(-ESTALE)))
2469 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2470 	return file;
2471 }
2472 
kern_path_create(int dfd,const char * pathname,struct path * path,int is_dir)2473 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2474 {
2475 	struct dentry *dentry = ERR_PTR(-EEXIST);
2476 	struct nameidata nd;
2477 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2478 	if (error)
2479 		return ERR_PTR(error);
2480 
2481 	/*
2482 	 * Yucky last component or no last component at all?
2483 	 * (foo/., foo/.., /////)
2484 	 */
2485 	if (nd.last_type != LAST_NORM)
2486 		goto out;
2487 	nd.flags &= ~LOOKUP_PARENT;
2488 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2489 	nd.intent.open.flags = O_EXCL;
2490 
2491 	/*
2492 	 * Do the final lookup.
2493 	 */
2494 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2495 	dentry = lookup_hash(&nd);
2496 	if (IS_ERR(dentry))
2497 		goto fail;
2498 
2499 	if (dentry->d_inode)
2500 		goto eexist;
2501 	/*
2502 	 * Special case - lookup gave negative, but... we had foo/bar/
2503 	 * From the vfs_mknod() POV we just have a negative dentry -
2504 	 * all is fine. Let's be bastards - you had / on the end, you've
2505 	 * been asking for (non-existent) directory. -ENOENT for you.
2506 	 */
2507 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
2508 		dput(dentry);
2509 		dentry = ERR_PTR(-ENOENT);
2510 		goto fail;
2511 	}
2512 	*path = nd.path;
2513 	return dentry;
2514 eexist:
2515 	dput(dentry);
2516 	dentry = ERR_PTR(-EEXIST);
2517 fail:
2518 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2519 out:
2520 	path_put(&nd.path);
2521 	return dentry;
2522 }
2523 EXPORT_SYMBOL(kern_path_create);
2524 
user_path_create(int dfd,const char __user * pathname,struct path * path,int is_dir)2525 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2526 {
2527 	char *tmp = getname(pathname);
2528 	struct dentry *res;
2529 	if (IS_ERR(tmp))
2530 		return ERR_CAST(tmp);
2531 	res = kern_path_create(dfd, tmp, path, is_dir);
2532 	putname(tmp);
2533 	return res;
2534 }
2535 EXPORT_SYMBOL(user_path_create);
2536 
vfs_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)2537 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2538 {
2539 	int error = may_create(dir, dentry);
2540 
2541 	if (error)
2542 		return error;
2543 
2544 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2545 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
2546 		return -EPERM;
2547 
2548 	if (!dir->i_op->mknod)
2549 		return -EPERM;
2550 
2551 	error = devcgroup_inode_mknod(mode, dev);
2552 	if (error)
2553 		return error;
2554 
2555 	error = security_inode_mknod(dir, dentry, mode, dev);
2556 	if (error)
2557 		return error;
2558 
2559 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2560 	if (!error)
2561 		fsnotify_create(dir, dentry);
2562 	return error;
2563 }
2564 
may_mknod(umode_t mode)2565 static int may_mknod(umode_t mode)
2566 {
2567 	switch (mode & S_IFMT) {
2568 	case S_IFREG:
2569 	case S_IFCHR:
2570 	case S_IFBLK:
2571 	case S_IFIFO:
2572 	case S_IFSOCK:
2573 	case 0: /* zero mode translates to S_IFREG */
2574 		return 0;
2575 	case S_IFDIR:
2576 		return -EPERM;
2577 	default:
2578 		return -EINVAL;
2579 	}
2580 }
2581 
SYSCALL_DEFINE4(mknodat,int,dfd,const char __user *,filename,umode_t,mode,unsigned,dev)2582 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2583 		unsigned, dev)
2584 {
2585 	struct dentry *dentry;
2586 	struct path path;
2587 	int error;
2588 
2589 	if (S_ISDIR(mode))
2590 		return -EPERM;
2591 
2592 	dentry = user_path_create(dfd, filename, &path, 0);
2593 	if (IS_ERR(dentry))
2594 		return PTR_ERR(dentry);
2595 
2596 	if (!IS_POSIXACL(path.dentry->d_inode))
2597 		mode &= ~current_umask();
2598 	error = may_mknod(mode);
2599 	if (error)
2600 		goto out_dput;
2601 	error = mnt_want_write(path.mnt);
2602 	if (error)
2603 		goto out_dput;
2604 	error = security_path_mknod(&path, dentry, mode, dev);
2605 	if (error)
2606 		goto out_drop_write;
2607 	switch (mode & S_IFMT) {
2608 		case 0: case S_IFREG:
2609 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
2610 			break;
2611 		case S_IFCHR: case S_IFBLK:
2612 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
2613 					new_decode_dev(dev));
2614 			break;
2615 		case S_IFIFO: case S_IFSOCK:
2616 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
2617 			break;
2618 	}
2619 out_drop_write:
2620 	mnt_drop_write(path.mnt);
2621 out_dput:
2622 	dput(dentry);
2623 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2624 	path_put(&path);
2625 
2626 	return error;
2627 }
2628 
SYSCALL_DEFINE3(mknod,const char __user *,filename,umode_t,mode,unsigned,dev)2629 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
2630 {
2631 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
2632 }
2633 
vfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)2634 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2635 {
2636 	int error = may_create(dir, dentry);
2637 	unsigned max_links = dir->i_sb->s_max_links;
2638 
2639 	if (error)
2640 		return error;
2641 
2642 	if (!dir->i_op->mkdir)
2643 		return -EPERM;
2644 
2645 	mode &= (S_IRWXUGO|S_ISVTX);
2646 	error = security_inode_mkdir(dir, dentry, mode);
2647 	if (error)
2648 		return error;
2649 
2650 	if (max_links && dir->i_nlink >= max_links)
2651 		return -EMLINK;
2652 
2653 	error = dir->i_op->mkdir(dir, dentry, mode);
2654 	if (!error)
2655 		fsnotify_mkdir(dir, dentry);
2656 	return error;
2657 }
2658 
SYSCALL_DEFINE3(mkdirat,int,dfd,const char __user *,pathname,umode_t,mode)2659 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
2660 {
2661 	struct dentry *dentry;
2662 	struct path path;
2663 	int error;
2664 
2665 	dentry = user_path_create(dfd, pathname, &path, 1);
2666 	if (IS_ERR(dentry))
2667 		return PTR_ERR(dentry);
2668 
2669 	if (!IS_POSIXACL(path.dentry->d_inode))
2670 		mode &= ~current_umask();
2671 	error = mnt_want_write(path.mnt);
2672 	if (error)
2673 		goto out_dput;
2674 	error = security_path_mkdir(&path, dentry, mode);
2675 	if (error)
2676 		goto out_drop_write;
2677 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2678 out_drop_write:
2679 	mnt_drop_write(path.mnt);
2680 out_dput:
2681 	dput(dentry);
2682 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2683 	path_put(&path);
2684 	return error;
2685 }
2686 
SYSCALL_DEFINE2(mkdir,const char __user *,pathname,umode_t,mode)2687 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
2688 {
2689 	return sys_mkdirat(AT_FDCWD, pathname, mode);
2690 }
2691 
2692 /*
2693  * The dentry_unhash() helper will try to drop the dentry early: we
2694  * should have a usage count of 1 if we're the only user of this
2695  * dentry, and if that is true (possibly after pruning the dcache),
2696  * then we drop the dentry now.
2697  *
2698  * A low-level filesystem can, if it choses, legally
2699  * do a
2700  *
2701  *	if (!d_unhashed(dentry))
2702  *		return -EBUSY;
2703  *
2704  * if it cannot handle the case of removing a directory
2705  * that is still in use by something else..
2706  */
dentry_unhash(struct dentry * dentry)2707 void dentry_unhash(struct dentry *dentry)
2708 {
2709 	shrink_dcache_parent(dentry);
2710 	spin_lock(&dentry->d_lock);
2711 	if (dentry->d_count == 1)
2712 		__d_drop(dentry);
2713 	spin_unlock(&dentry->d_lock);
2714 }
2715 
vfs_rmdir(struct inode * dir,struct dentry * dentry)2716 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2717 {
2718 	int error = may_delete(dir, dentry, 1);
2719 
2720 	if (error)
2721 		return error;
2722 
2723 	if (!dir->i_op->rmdir)
2724 		return -EPERM;
2725 
2726 	dget(dentry);
2727 	mutex_lock(&dentry->d_inode->i_mutex);
2728 
2729 	error = -EBUSY;
2730 	if (d_mountpoint(dentry))
2731 		goto out;
2732 
2733 	error = security_inode_rmdir(dir, dentry);
2734 	if (error)
2735 		goto out;
2736 
2737 	shrink_dcache_parent(dentry);
2738 	error = dir->i_op->rmdir(dir, dentry);
2739 	if (error)
2740 		goto out;
2741 
2742 	dentry->d_inode->i_flags |= S_DEAD;
2743 	dont_mount(dentry);
2744 
2745 out:
2746 	mutex_unlock(&dentry->d_inode->i_mutex);
2747 	dput(dentry);
2748 	if (!error)
2749 		d_delete(dentry);
2750 	return error;
2751 }
2752 
do_rmdir(int dfd,const char __user * pathname)2753 static long do_rmdir(int dfd, const char __user *pathname)
2754 {
2755 	int error = 0;
2756 	char * name;
2757 	struct dentry *dentry;
2758 	struct nameidata nd;
2759 
2760 	error = user_path_parent(dfd, pathname, &nd, &name);
2761 	if (error)
2762 		return error;
2763 
2764 	switch(nd.last_type) {
2765 	case LAST_DOTDOT:
2766 		error = -ENOTEMPTY;
2767 		goto exit1;
2768 	case LAST_DOT:
2769 		error = -EINVAL;
2770 		goto exit1;
2771 	case LAST_ROOT:
2772 		error = -EBUSY;
2773 		goto exit1;
2774 	}
2775 
2776 	nd.flags &= ~LOOKUP_PARENT;
2777 
2778 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2779 	dentry = lookup_hash(&nd);
2780 	error = PTR_ERR(dentry);
2781 	if (IS_ERR(dentry))
2782 		goto exit2;
2783 	if (!dentry->d_inode) {
2784 		error = -ENOENT;
2785 		goto exit3;
2786 	}
2787 	error = mnt_want_write(nd.path.mnt);
2788 	if (error)
2789 		goto exit3;
2790 	error = security_path_rmdir(&nd.path, dentry);
2791 	if (error)
2792 		goto exit4;
2793 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2794 exit4:
2795 	mnt_drop_write(nd.path.mnt);
2796 exit3:
2797 	dput(dentry);
2798 exit2:
2799 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2800 exit1:
2801 	path_put(&nd.path);
2802 	putname(name);
2803 	return error;
2804 }
2805 
SYSCALL_DEFINE1(rmdir,const char __user *,pathname)2806 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2807 {
2808 	return do_rmdir(AT_FDCWD, pathname);
2809 }
2810 
vfs_unlink(struct inode * dir,struct dentry * dentry)2811 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2812 {
2813 	int error = may_delete(dir, dentry, 0);
2814 
2815 	if (error)
2816 		return error;
2817 
2818 	if (!dir->i_op->unlink)
2819 		return -EPERM;
2820 
2821 	mutex_lock(&dentry->d_inode->i_mutex);
2822 	if (d_mountpoint(dentry))
2823 		error = -EBUSY;
2824 	else {
2825 		error = security_inode_unlink(dir, dentry);
2826 		if (!error) {
2827 			error = dir->i_op->unlink(dir, dentry);
2828 			if (!error)
2829 				dont_mount(dentry);
2830 		}
2831 	}
2832 	mutex_unlock(&dentry->d_inode->i_mutex);
2833 
2834 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
2835 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2836 		fsnotify_link_count(dentry->d_inode);
2837 		d_delete(dentry);
2838 	}
2839 
2840 	return error;
2841 }
2842 
2843 /*
2844  * Make sure that the actual truncation of the file will occur outside its
2845  * directory's i_mutex.  Truncate can take a long time if there is a lot of
2846  * writeout happening, and we don't want to prevent access to the directory
2847  * while waiting on the I/O.
2848  */
do_unlinkat(int dfd,const char __user * pathname)2849 static long do_unlinkat(int dfd, const char __user *pathname)
2850 {
2851 	int error;
2852 	char *name;
2853 	struct dentry *dentry;
2854 	struct nameidata nd;
2855 	struct inode *inode = NULL;
2856 
2857 	error = user_path_parent(dfd, pathname, &nd, &name);
2858 	if (error)
2859 		return error;
2860 
2861 	error = -EISDIR;
2862 	if (nd.last_type != LAST_NORM)
2863 		goto exit1;
2864 
2865 	nd.flags &= ~LOOKUP_PARENT;
2866 
2867 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2868 	dentry = lookup_hash(&nd);
2869 	error = PTR_ERR(dentry);
2870 	if (!IS_ERR(dentry)) {
2871 		/* Why not before? Because we want correct error value */
2872 		if (nd.last.name[nd.last.len])
2873 			goto slashes;
2874 		inode = dentry->d_inode;
2875 		if (!inode)
2876 			goto slashes;
2877 		ihold(inode);
2878 		error = mnt_want_write(nd.path.mnt);
2879 		if (error)
2880 			goto exit2;
2881 		error = security_path_unlink(&nd.path, dentry);
2882 		if (error)
2883 			goto exit3;
2884 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2885 exit3:
2886 		mnt_drop_write(nd.path.mnt);
2887 	exit2:
2888 		dput(dentry);
2889 	}
2890 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2891 	if (inode)
2892 		iput(inode);	/* truncate the inode here */
2893 exit1:
2894 	path_put(&nd.path);
2895 	putname(name);
2896 	return error;
2897 
2898 slashes:
2899 	error = !dentry->d_inode ? -ENOENT :
2900 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2901 	goto exit2;
2902 }
2903 
SYSCALL_DEFINE3(unlinkat,int,dfd,const char __user *,pathname,int,flag)2904 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2905 {
2906 	if ((flag & ~AT_REMOVEDIR) != 0)
2907 		return -EINVAL;
2908 
2909 	if (flag & AT_REMOVEDIR)
2910 		return do_rmdir(dfd, pathname);
2911 
2912 	return do_unlinkat(dfd, pathname);
2913 }
2914 
SYSCALL_DEFINE1(unlink,const char __user *,pathname)2915 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2916 {
2917 	return do_unlinkat(AT_FDCWD, pathname);
2918 }
2919 
vfs_symlink(struct inode * dir,struct dentry * dentry,const char * oldname)2920 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2921 {
2922 	int error = may_create(dir, dentry);
2923 
2924 	if (error)
2925 		return error;
2926 
2927 	if (!dir->i_op->symlink)
2928 		return -EPERM;
2929 
2930 	error = security_inode_symlink(dir, dentry, oldname);
2931 	if (error)
2932 		return error;
2933 
2934 	error = dir->i_op->symlink(dir, dentry, oldname);
2935 	if (!error)
2936 		fsnotify_create(dir, dentry);
2937 	return error;
2938 }
2939 
SYSCALL_DEFINE3(symlinkat,const char __user *,oldname,int,newdfd,const char __user *,newname)2940 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2941 		int, newdfd, const char __user *, newname)
2942 {
2943 	int error;
2944 	char *from;
2945 	struct dentry *dentry;
2946 	struct path path;
2947 
2948 	from = getname(oldname);
2949 	if (IS_ERR(from))
2950 		return PTR_ERR(from);
2951 
2952 	dentry = user_path_create(newdfd, newname, &path, 0);
2953 	error = PTR_ERR(dentry);
2954 	if (IS_ERR(dentry))
2955 		goto out_putname;
2956 
2957 	error = mnt_want_write(path.mnt);
2958 	if (error)
2959 		goto out_dput;
2960 	error = security_path_symlink(&path, dentry, from);
2961 	if (error)
2962 		goto out_drop_write;
2963 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2964 out_drop_write:
2965 	mnt_drop_write(path.mnt);
2966 out_dput:
2967 	dput(dentry);
2968 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2969 	path_put(&path);
2970 out_putname:
2971 	putname(from);
2972 	return error;
2973 }
2974 
SYSCALL_DEFINE2(symlink,const char __user *,oldname,const char __user *,newname)2975 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2976 {
2977 	return sys_symlinkat(oldname, AT_FDCWD, newname);
2978 }
2979 
vfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)2980 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2981 {
2982 	struct inode *inode = old_dentry->d_inode;
2983 	unsigned max_links = dir->i_sb->s_max_links;
2984 	int error;
2985 
2986 	if (!inode)
2987 		return -ENOENT;
2988 
2989 	error = may_create(dir, new_dentry);
2990 	if (error)
2991 		return error;
2992 
2993 	if (dir->i_sb != inode->i_sb)
2994 		return -EXDEV;
2995 
2996 	/*
2997 	 * A link to an append-only or immutable file cannot be created.
2998 	 */
2999 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3000 		return -EPERM;
3001 	if (!dir->i_op->link)
3002 		return -EPERM;
3003 	if (S_ISDIR(inode->i_mode))
3004 		return -EPERM;
3005 
3006 	error = security_inode_link(old_dentry, dir, new_dentry);
3007 	if (error)
3008 		return error;
3009 
3010 	mutex_lock(&inode->i_mutex);
3011 	/* Make sure we don't allow creating hardlink to an unlinked file */
3012 	if (inode->i_nlink == 0)
3013 		error =  -ENOENT;
3014 	else if (max_links && inode->i_nlink >= max_links)
3015 		error = -EMLINK;
3016 	else
3017 		error = dir->i_op->link(old_dentry, dir, new_dentry);
3018 	mutex_unlock(&inode->i_mutex);
3019 	if (!error)
3020 		fsnotify_link(dir, inode, new_dentry);
3021 	return error;
3022 }
3023 
3024 /*
3025  * Hardlinks are often used in delicate situations.  We avoid
3026  * security-related surprises by not following symlinks on the
3027  * newname.  --KAB
3028  *
3029  * We don't follow them on the oldname either to be compatible
3030  * with linux 2.0, and to avoid hard-linking to directories
3031  * and other special files.  --ADM
3032  */
SYSCALL_DEFINE5(linkat,int,olddfd,const char __user *,oldname,int,newdfd,const char __user *,newname,int,flags)3033 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3034 		int, newdfd, const char __user *, newname, int, flags)
3035 {
3036 	struct dentry *new_dentry;
3037 	struct path old_path, new_path;
3038 	int how = 0;
3039 	int error;
3040 
3041 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3042 		return -EINVAL;
3043 	/*
3044 	 * To use null names we require CAP_DAC_READ_SEARCH
3045 	 * This ensures that not everyone will be able to create
3046 	 * handlink using the passed filedescriptor.
3047 	 */
3048 	if (flags & AT_EMPTY_PATH) {
3049 		if (!capable(CAP_DAC_READ_SEARCH))
3050 			return -ENOENT;
3051 		how = LOOKUP_EMPTY;
3052 	}
3053 
3054 	if (flags & AT_SYMLINK_FOLLOW)
3055 		how |= LOOKUP_FOLLOW;
3056 
3057 	error = user_path_at(olddfd, oldname, how, &old_path);
3058 	if (error)
3059 		return error;
3060 
3061 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
3062 	error = PTR_ERR(new_dentry);
3063 	if (IS_ERR(new_dentry))
3064 		goto out;
3065 
3066 	error = -EXDEV;
3067 	if (old_path.mnt != new_path.mnt)
3068 		goto out_dput;
3069 	error = mnt_want_write(new_path.mnt);
3070 	if (error)
3071 		goto out_dput;
3072 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3073 	if (error)
3074 		goto out_drop_write;
3075 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3076 out_drop_write:
3077 	mnt_drop_write(new_path.mnt);
3078 out_dput:
3079 	dput(new_dentry);
3080 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3081 	path_put(&new_path);
3082 out:
3083 	path_put(&old_path);
3084 
3085 	return error;
3086 }
3087 
SYSCALL_DEFINE2(link,const char __user *,oldname,const char __user *,newname)3088 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3089 {
3090 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3091 }
3092 
3093 /*
3094  * The worst of all namespace operations - renaming directory. "Perverted"
3095  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3096  * Problems:
3097  *	a) we can get into loop creation. Check is done in is_subdir().
3098  *	b) race potential - two innocent renames can create a loop together.
3099  *	   That's where 4.4 screws up. Current fix: serialization on
3100  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3101  *	   story.
3102  *	c) we have to lock _three_ objects - parents and victim (if it exists).
3103  *	   And that - after we got ->i_mutex on parents (until then we don't know
3104  *	   whether the target exists).  Solution: try to be smart with locking
3105  *	   order for inodes.  We rely on the fact that tree topology may change
3106  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
3107  *	   move will be locked.  Thus we can rank directories by the tree
3108  *	   (ancestors first) and rank all non-directories after them.
3109  *	   That works since everybody except rename does "lock parent, lookup,
3110  *	   lock child" and rename is under ->s_vfs_rename_mutex.
3111  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
3112  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
3113  *	   we'd better make sure that there's no link(2) for them.
3114  *	d) conversion from fhandle to dentry may come in the wrong moment - when
3115  *	   we are removing the target. Solution: we will have to grab ->i_mutex
3116  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3117  *	   ->i_mutex on parents, which works but leads to some truly excessive
3118  *	   locking].
3119  */
vfs_rename_dir(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)3120 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3121 			  struct inode *new_dir, struct dentry *new_dentry)
3122 {
3123 	int error = 0;
3124 	struct inode *target = new_dentry->d_inode;
3125 	unsigned max_links = new_dir->i_sb->s_max_links;
3126 
3127 	/*
3128 	 * If we are going to change the parent - check write permissions,
3129 	 * we'll need to flip '..'.
3130 	 */
3131 	if (new_dir != old_dir) {
3132 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3133 		if (error)
3134 			return error;
3135 	}
3136 
3137 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3138 	if (error)
3139 		return error;
3140 
3141 	dget(new_dentry);
3142 	if (target)
3143 		mutex_lock(&target->i_mutex);
3144 
3145 	error = -EBUSY;
3146 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3147 		goto out;
3148 
3149 	error = -EMLINK;
3150 	if (max_links && !target && new_dir != old_dir &&
3151 	    new_dir->i_nlink >= max_links)
3152 		goto out;
3153 
3154 	if (target)
3155 		shrink_dcache_parent(new_dentry);
3156 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3157 	if (error)
3158 		goto out;
3159 
3160 	if (target) {
3161 		target->i_flags |= S_DEAD;
3162 		dont_mount(new_dentry);
3163 	}
3164 out:
3165 	if (target)
3166 		mutex_unlock(&target->i_mutex);
3167 	dput(new_dentry);
3168 	if (!error)
3169 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3170 			d_move(old_dentry,new_dentry);
3171 	return error;
3172 }
3173 
vfs_rename_other(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)3174 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3175 			    struct inode *new_dir, struct dentry *new_dentry)
3176 {
3177 	struct inode *target = new_dentry->d_inode;
3178 	int error;
3179 
3180 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3181 	if (error)
3182 		return error;
3183 
3184 	dget(new_dentry);
3185 	if (target)
3186 		mutex_lock(&target->i_mutex);
3187 
3188 	error = -EBUSY;
3189 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3190 		goto out;
3191 
3192 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3193 	if (error)
3194 		goto out;
3195 
3196 	if (target)
3197 		dont_mount(new_dentry);
3198 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3199 		d_move(old_dentry, new_dentry);
3200 out:
3201 	if (target)
3202 		mutex_unlock(&target->i_mutex);
3203 	dput(new_dentry);
3204 	return error;
3205 }
3206 
vfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)3207 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3208 	       struct inode *new_dir, struct dentry *new_dentry)
3209 {
3210 	int error;
3211 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3212 	const unsigned char *old_name;
3213 
3214 	if (old_dentry->d_inode == new_dentry->d_inode)
3215  		return 0;
3216 
3217 	error = may_delete(old_dir, old_dentry, is_dir);
3218 	if (error)
3219 		return error;
3220 
3221 	if (!new_dentry->d_inode)
3222 		error = may_create(new_dir, new_dentry);
3223 	else
3224 		error = may_delete(new_dir, new_dentry, is_dir);
3225 	if (error)
3226 		return error;
3227 
3228 	if (!old_dir->i_op->rename)
3229 		return -EPERM;
3230 
3231 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3232 
3233 	if (is_dir)
3234 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3235 	else
3236 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3237 	if (!error)
3238 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
3239 			      new_dentry->d_inode, old_dentry);
3240 	fsnotify_oldname_free(old_name);
3241 
3242 	return error;
3243 }
3244 
SYSCALL_DEFINE4(renameat,int,olddfd,const char __user *,oldname,int,newdfd,const char __user *,newname)3245 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3246 		int, newdfd, const char __user *, newname)
3247 {
3248 	struct dentry *old_dir, *new_dir;
3249 	struct dentry *old_dentry, *new_dentry;
3250 	struct dentry *trap;
3251 	struct nameidata oldnd, newnd;
3252 	char *from;
3253 	char *to;
3254 	int error;
3255 
3256 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
3257 	if (error)
3258 		goto exit;
3259 
3260 	error = user_path_parent(newdfd, newname, &newnd, &to);
3261 	if (error)
3262 		goto exit1;
3263 
3264 	error = -EXDEV;
3265 	if (oldnd.path.mnt != newnd.path.mnt)
3266 		goto exit2;
3267 
3268 	old_dir = oldnd.path.dentry;
3269 	error = -EBUSY;
3270 	if (oldnd.last_type != LAST_NORM)
3271 		goto exit2;
3272 
3273 	new_dir = newnd.path.dentry;
3274 	if (newnd.last_type != LAST_NORM)
3275 		goto exit2;
3276 
3277 	oldnd.flags &= ~LOOKUP_PARENT;
3278 	newnd.flags &= ~LOOKUP_PARENT;
3279 	newnd.flags |= LOOKUP_RENAME_TARGET;
3280 
3281 	trap = lock_rename(new_dir, old_dir);
3282 
3283 	old_dentry = lookup_hash(&oldnd);
3284 	error = PTR_ERR(old_dentry);
3285 	if (IS_ERR(old_dentry))
3286 		goto exit3;
3287 	/* source must exist */
3288 	error = -ENOENT;
3289 	if (!old_dentry->d_inode)
3290 		goto exit4;
3291 	/* unless the source is a directory trailing slashes give -ENOTDIR */
3292 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3293 		error = -ENOTDIR;
3294 		if (oldnd.last.name[oldnd.last.len])
3295 			goto exit4;
3296 		if (newnd.last.name[newnd.last.len])
3297 			goto exit4;
3298 	}
3299 	/* source should not be ancestor of target */
3300 	error = -EINVAL;
3301 	if (old_dentry == trap)
3302 		goto exit4;
3303 	new_dentry = lookup_hash(&newnd);
3304 	error = PTR_ERR(new_dentry);
3305 	if (IS_ERR(new_dentry))
3306 		goto exit4;
3307 	/* target should not be an ancestor of source */
3308 	error = -ENOTEMPTY;
3309 	if (new_dentry == trap)
3310 		goto exit5;
3311 
3312 	error = mnt_want_write(oldnd.path.mnt);
3313 	if (error)
3314 		goto exit5;
3315 	error = security_path_rename(&oldnd.path, old_dentry,
3316 				     &newnd.path, new_dentry);
3317 	if (error)
3318 		goto exit6;
3319 	error = vfs_rename(old_dir->d_inode, old_dentry,
3320 				   new_dir->d_inode, new_dentry);
3321 exit6:
3322 	mnt_drop_write(oldnd.path.mnt);
3323 exit5:
3324 	dput(new_dentry);
3325 exit4:
3326 	dput(old_dentry);
3327 exit3:
3328 	unlock_rename(new_dir, old_dir);
3329 exit2:
3330 	path_put(&newnd.path);
3331 	putname(to);
3332 exit1:
3333 	path_put(&oldnd.path);
3334 	putname(from);
3335 exit:
3336 	return error;
3337 }
3338 
SYSCALL_DEFINE2(rename,const char __user *,oldname,const char __user *,newname)3339 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3340 {
3341 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3342 }
3343 
vfs_readlink(struct dentry * dentry,char __user * buffer,int buflen,const char * link)3344 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3345 {
3346 	int len;
3347 
3348 	len = PTR_ERR(link);
3349 	if (IS_ERR(link))
3350 		goto out;
3351 
3352 	len = strlen(link);
3353 	if (len > (unsigned) buflen)
3354 		len = buflen;
3355 	if (copy_to_user(buffer, link, len))
3356 		len = -EFAULT;
3357 out:
3358 	return len;
3359 }
3360 
3361 /*
3362  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
3363  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
3364  * using) it for any given inode is up to filesystem.
3365  */
generic_readlink(struct dentry * dentry,char __user * buffer,int buflen)3366 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3367 {
3368 	struct nameidata nd;
3369 	void *cookie;
3370 	int res;
3371 
3372 	nd.depth = 0;
3373 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3374 	if (IS_ERR(cookie))
3375 		return PTR_ERR(cookie);
3376 
3377 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3378 	if (dentry->d_inode->i_op->put_link)
3379 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3380 	return res;
3381 }
3382 
vfs_follow_link(struct nameidata * nd,const char * link)3383 int vfs_follow_link(struct nameidata *nd, const char *link)
3384 {
3385 	return __vfs_follow_link(nd, link);
3386 }
3387 
3388 /* get the link contents into pagecache */
page_getlink(struct dentry * dentry,struct page ** ppage)3389 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3390 {
3391 	char *kaddr;
3392 	struct page *page;
3393 	struct address_space *mapping = dentry->d_inode->i_mapping;
3394 	page = read_mapping_page(mapping, 0, NULL);
3395 	if (IS_ERR(page))
3396 		return (char*)page;
3397 	*ppage = page;
3398 	kaddr = kmap(page);
3399 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3400 	return kaddr;
3401 }
3402 
page_readlink(struct dentry * dentry,char __user * buffer,int buflen)3403 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3404 {
3405 	struct page *page = NULL;
3406 	char *s = page_getlink(dentry, &page);
3407 	int res = vfs_readlink(dentry,buffer,buflen,s);
3408 	if (page) {
3409 		kunmap(page);
3410 		page_cache_release(page);
3411 	}
3412 	return res;
3413 }
3414 
page_follow_link_light(struct dentry * dentry,struct nameidata * nd)3415 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3416 {
3417 	struct page *page = NULL;
3418 	nd_set_link(nd, page_getlink(dentry, &page));
3419 	return page;
3420 }
3421 
page_put_link(struct dentry * dentry,struct nameidata * nd,void * cookie)3422 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3423 {
3424 	struct page *page = cookie;
3425 
3426 	if (page) {
3427 		kunmap(page);
3428 		page_cache_release(page);
3429 	}
3430 }
3431 
3432 /*
3433  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3434  */
__page_symlink(struct inode * inode,const char * symname,int len,int nofs)3435 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3436 {
3437 	struct address_space *mapping = inode->i_mapping;
3438 	struct page *page;
3439 	void *fsdata;
3440 	int err;
3441 	char *kaddr;
3442 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3443 	if (nofs)
3444 		flags |= AOP_FLAG_NOFS;
3445 
3446 retry:
3447 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
3448 				flags, &page, &fsdata);
3449 	if (err)
3450 		goto fail;
3451 
3452 	kaddr = kmap_atomic(page);
3453 	memcpy(kaddr, symname, len-1);
3454 	kunmap_atomic(kaddr);
3455 
3456 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3457 							page, fsdata);
3458 	if (err < 0)
3459 		goto fail;
3460 	if (err < len-1)
3461 		goto retry;
3462 
3463 	mark_inode_dirty(inode);
3464 	return 0;
3465 fail:
3466 	return err;
3467 }
3468 
page_symlink(struct inode * inode,const char * symname,int len)3469 int page_symlink(struct inode *inode, const char *symname, int len)
3470 {
3471 	return __page_symlink(inode, symname, len,
3472 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3473 }
3474 
3475 const struct inode_operations page_symlink_inode_operations = {
3476 	.readlink	= generic_readlink,
3477 	.follow_link	= page_follow_link_light,
3478 	.put_link	= page_put_link,
3479 };
3480 
3481 EXPORT_SYMBOL(user_path_at);
3482 EXPORT_SYMBOL(follow_down_one);
3483 EXPORT_SYMBOL(follow_down);
3484 EXPORT_SYMBOL(follow_up);
3485 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3486 EXPORT_SYMBOL(getname);
3487 EXPORT_SYMBOL(lock_rename);
3488 EXPORT_SYMBOL(lookup_one_len);
3489 EXPORT_SYMBOL(page_follow_link_light);
3490 EXPORT_SYMBOL(page_put_link);
3491 EXPORT_SYMBOL(page_readlink);
3492 EXPORT_SYMBOL(__page_symlink);
3493 EXPORT_SYMBOL(page_symlink);
3494 EXPORT_SYMBOL(page_symlink_inode_operations);
3495 EXPORT_SYMBOL(kern_path);
3496 EXPORT_SYMBOL(vfs_path_lookup);
3497 EXPORT_SYMBOL(inode_permission);
3498 EXPORT_SYMBOL(unlock_rename);
3499 EXPORT_SYMBOL(vfs_create);
3500 EXPORT_SYMBOL(vfs_follow_link);
3501 EXPORT_SYMBOL(vfs_link);
3502 EXPORT_SYMBOL(vfs_mkdir);
3503 EXPORT_SYMBOL(vfs_mknod);
3504 EXPORT_SYMBOL(generic_permission);
3505 EXPORT_SYMBOL(vfs_readlink);
3506 EXPORT_SYMBOL(vfs_rename);
3507 EXPORT_SYMBOL(vfs_rmdir);
3508 EXPORT_SYMBOL(vfs_symlink);
3509 EXPORT_SYMBOL(vfs_unlink);
3510 EXPORT_SYMBOL(dentry_unhash);
3511 EXPORT_SYMBOL(generic_readlink);
3512