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