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