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