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