blob: dc36bda5c2e72d92ebb596e08ec7ffaaa1e709d1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050019#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070020#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
22#include <linux/fs.h>
23#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040025#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/personality.h>
27#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050028#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/syscalls.h>
30#include <linux/mount.h>
31#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080032#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070033#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080034#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070035#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040036#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070037#include <linux/posix_acl.h>
Linus Torvalds99d263d2014-09-13 11:30:10 -070038#include <linux/hash.h>
George Spelvin2a18da7a2016-05-23 07:43:58 -040039#include <linux/bitops.h>
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -050040#include <linux/init_task.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080041#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Eric Parise81e3f42009-12-04 15:47:36 -050043#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050044#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* [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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030080 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
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)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800113 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * 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 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400124
Al Virofd2f7cb2015-02-22 20:07:13 -0500125#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400126
David Drysdale51f39a12014-12-12 16:57:29 -0800127struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400128getname_flags(const char __user *filename, int flags, int *empty)
129{
Al Viro94b5d262015-02-22 19:38:03 -0500130 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400131 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500132 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jeff Layton7ac86262012-10-10 15:25:28 -0400134 result = audit_reusename(filename);
135 if (result)
136 return result;
137
Jeff Layton7950e382012-10-10 16:43:13 -0400138 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700139 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500140 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jeff Layton7950e382012-10-10 16:43:13 -0400142 /*
143 * First, try to embed the struct filename inside the names_cache
144 * allocation
145 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500146 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400147 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400148
Al Viro94b5d262015-02-22 19:38:03 -0500149 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400150 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500151 __putname(result);
152 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400153 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700154
Jeff Layton7950e382012-10-10 16:43:13 -0400155 /*
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 */
Al Viro94b5d262015-02-22 19:38:03 -0500161 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500162 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400163 kname = (char *)result;
164
Al Virofd2f7cb2015-02-22 20:07:13 -0500165 /*
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);
Al Viro94b5d262015-02-22 19:38:03 -0500171 if (unlikely(!result)) {
172 __putname(kname);
173 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400174 }
175 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500176 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 }
Jeff Layton7950e382012-10-10 16:43:13 -0400187 }
188
Al Viro94b5d262015-02-22 19:38:03 -0500189 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700190 /* The empty path is special. */
191 if (unlikely(!len)) {
192 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500193 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500194 if (!(flags & LOOKUP_EMPTY)) {
195 putname(result);
196 return ERR_PTR(-ENOENT);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700199
Jeff Layton7950e382012-10-10 16:43:13 -0400200 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800201 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400202 audit_getname(result);
203 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Jeff Layton91a27b22012-10-10 15:25:28 -0400206struct filename *
207getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400208{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700209 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400210}
211
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800212struct filename *
213getname_kernel(const char * filename)
214{
215 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500216 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800217
218 result = __getname();
219 if (unlikely(!result))
220 return ERR_PTR(-ENOMEM);
221
Paul Moore08518542015-01-21 23:59:56 -0500222 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500223 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500224 } else if (len <= PATH_MAX) {
Al Viro30ce4d12018-04-08 11:57:10 -0400225 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500226 struct filename *tmp;
227
Al Viro30ce4d12018-04-08 11:57:10 -0400228 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500229 if (unlikely(!tmp)) {
230 __putname(result);
231 return ERR_PTR(-ENOMEM);
232 }
233 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500234 result = tmp;
235 } else {
236 __putname(result);
237 return ERR_PTR(-ENAMETOOLONG);
238 }
239 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800240 result->uptr = NULL;
241 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500242 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500243 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800244
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800245 return result;
246}
247
Jeff Layton91a27b22012-10-10 15:25:28 -0400248void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Dmitry Kadashev91ef6582021-07-08 13:34:37 +0700250 if (IS_ERR_OR_NULL(name))
251 return;
252
Paul Moore55422d02015-01-22 00:00:23 -0500253 BUG_ON(name->refcnt <= 0);
254
255 if (--name->refcnt > 0)
256 return;
257
Al Virofd2f7cb2015-02-22 20:07:13 -0500258 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500259 __putname(name->name);
260 kfree(name);
261 } else
262 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Christian Brauner47291ba2021-01-21 14:19:24 +0100265/**
266 * check_acl - perform ACL permission checking
267 * @mnt_userns: user namespace of the mount the inode was found from
268 * @inode: inode to check permissions on
269 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
270 *
271 * This function performs the ACL permission checking. Since this function
272 * retrieve POSIX acls it needs to know whether it is called from a blocking or
273 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
274 *
275 * If the inode has been found through an idmapped mount the user namespace of
276 * the vfsmount must be passed through @mnt_userns. This function will then take
277 * care to map the inode according to @mnt_userns before checking permissions.
278 * On non-idmapped mounts or if permission checking is to be performed on the
279 * raw inode simply passs init_user_ns.
280 */
281static int check_acl(struct user_namespace *mnt_userns,
282 struct inode *inode, int mask)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700283{
Linus Torvalds84635d62011-07-25 22:47:03 -0700284#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700285 struct posix_acl *acl;
286
Linus Torvaldse77819e2011-07-22 19:30:19 -0700287 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400288 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
289 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700290 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400291 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100292 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400293 return -ECHILD;
Christian Brauner47291ba2021-01-21 14:19:24 +0100294 return posix_acl_permission(mnt_userns, inode, acl, mask);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700295 }
296
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800297 acl = get_acl(inode, ACL_TYPE_ACCESS);
298 if (IS_ERR(acl))
299 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700300 if (acl) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100301 int error = posix_acl_permission(mnt_userns, inode, acl, mask);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700302 posix_acl_release(acl);
303 return error;
304 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700305#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700306
307 return -EAGAIN;
308}
309
Christian Brauner47291ba2021-01-21 14:19:24 +0100310/**
311 * acl_permission_check - perform basic UNIX permission checking
312 * @mnt_userns: user namespace of the mount the inode was found from
313 * @inode: inode to check permissions on
314 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700315 *
Christian Brauner47291ba2021-01-21 14:19:24 +0100316 * This function performs the basic UNIX permission checking. Since this
317 * function may retrieve POSIX acls it needs to know whether it is called from a
318 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
319 *
320 * If the inode has been found through an idmapped mount the user namespace of
321 * the vfsmount must be passed through @mnt_userns. This function will then take
322 * care to map the inode according to @mnt_userns before checking permissions.
323 * On non-idmapped mounts or if permission checking is to be performed on the
324 * raw inode simply passs init_user_ns.
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700325 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100326static int acl_permission_check(struct user_namespace *mnt_userns,
327 struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700328{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700329 unsigned int mode = inode->i_mode;
Christian Brauner47291ba2021-01-21 14:19:24 +0100330 kuid_t i_uid;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700331
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700332 /* Are we the owner? If so, ACL's don't matter */
Christian Brauner47291ba2021-01-21 14:19:24 +0100333 i_uid = i_uid_into_mnt(mnt_userns, inode);
334 if (likely(uid_eq(current_fsuid(), i_uid))) {
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700335 mask &= 7;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700336 mode >>= 6;
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700337 return (mask & ~mode) ? -EACCES : 0;
338 }
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700339
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700340 /* Do we have ACL's? */
341 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100342 int error = check_acl(mnt_userns, inode, mask);
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700343 if (error != -EAGAIN)
344 return error;
345 }
346
347 /* Only RWX matters for group/other mode bits */
348 mask &= 7;
349
350 /*
351 * Are the group permissions different from
352 * the other permissions in the bits we care
353 * about? Need to check group ownership if so.
354 */
355 if (mask & (mode ^ (mode >> 3))) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100356 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
357 if (in_group_p(kgid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700358 mode >>= 3;
359 }
360
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700361 /* Bits in 'mode' clear that we require? */
362 return (mask & ~mode) ? -EACCES : 0;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100366 * generic_permission - check for access rights on a Posix-like filesystem
Christian Brauner47291ba2021-01-21 14:19:24 +0100367 * @mnt_userns: user namespace of the mount the inode was found from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @inode: inode to check access rights for
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700369 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
370 * %MAY_NOT_BLOCK ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
372 * Used to check for read/write/execute permissions on a file.
373 * We use "fsuid" for this, letting us set arbitrary permissions
374 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100375 * are used for other things.
376 *
377 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
378 * request cannot be satisfied (eg. requires blocking or too much complexity).
379 * It would then be called again in ref-walk mode.
Christian Brauner47291ba2021-01-21 14:19:24 +0100380 *
381 * If the inode has been found through an idmapped mount the user namespace of
382 * the vfsmount must be passed through @mnt_userns. This function will then take
383 * care to map the inode according to @mnt_userns before checking permissions.
384 * On non-idmapped mounts or if permission checking is to be performed on the
385 * raw inode simply passs init_user_ns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100387int generic_permission(struct user_namespace *mnt_userns, struct inode *inode,
388 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700390 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530393 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100395 ret = acl_permission_check(mnt_userns, inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700396 if (ret != -EACCES)
397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Al Virod594e7e2011-06-20 19:55:42 -0400399 if (S_ISDIR(inode->i_mode)) {
400 /* DACs are overridable for directories */
Al Virod594e7e2011-06-20 19:55:42 -0400401 if (!(mask & MAY_WRITE))
Christian Brauner47291ba2021-01-21 14:19:24 +0100402 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700403 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400404 return 0;
Christian Brauner47291ba2021-01-21 14:19:24 +0100405 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100406 CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500408 return -EACCES;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /*
412 * Searching includes executable on directories, else just read.
413 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600414 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400415 if (mask == MAY_READ)
Christian Brauner47291ba2021-01-21 14:19:24 +0100416 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100417 CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500419 /*
420 * Read/write DACs are always overridable.
421 * Executable DACs are overridable when there is
422 * at least one exec bit set.
423 */
424 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Christian Brauner47291ba2021-01-21 14:19:24 +0100425 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100426 CAP_DAC_OVERRIDE))
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return -EACCES;
430}
Al Viro4d359502014-03-14 12:20:17 -0400431EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christian Brauner47291ba2021-01-21 14:19:24 +0100433/**
434 * do_inode_permission - UNIX permission checking
435 * @mnt_userns: user namespace of the mount the inode was found from
436 * @inode: inode to check permissions on
437 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
438 *
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700439 * We _really_ want to just do "generic_permission()" without
440 * even looking at the inode->i_op values. So we keep a cache
441 * flag in inode->i_opflags, that says "this has not special
442 * permission function, use the fast case".
443 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100444static inline int do_inode_permission(struct user_namespace *mnt_userns,
445 struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700446{
447 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
448 if (likely(inode->i_op->permission))
Christian Brauner549c7292021-01-21 14:19:43 +0100449 return inode->i_op->permission(mnt_userns, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700450
451 /* This gets set once for the inode lifetime */
452 spin_lock(&inode->i_lock);
453 inode->i_opflags |= IOP_FASTPERM;
454 spin_unlock(&inode->i_lock);
455 }
Christian Brauner47291ba2021-01-21 14:19:24 +0100456 return generic_permission(mnt_userns, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700457}
458
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200459/**
David Howells0bdaea92012-06-25 12:55:46 +0100460 * sb_permission - Check superblock-level permissions
461 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700462 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100463 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
464 *
465 * Separate out file-system wide checks from inode-specific permission checks.
466 */
467static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
468{
469 if (unlikely(mask & MAY_WRITE)) {
470 umode_t mode = inode->i_mode;
471
472 /* Nobody gets write access to a read-only fs. */
David Howellsbc98a422017-07-17 08:45:34 +0100473 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
David Howells0bdaea92012-06-25 12:55:46 +0100474 return -EROFS;
475 }
476 return 0;
477}
478
479/**
480 * inode_permission - Check for access rights to a given inode
Christian Brauner47291ba2021-01-21 14:19:24 +0100481 * @mnt_userns: User namespace of the mount the inode was found from
482 * @inode: Inode to check permission on
483 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
David Howells0bdaea92012-06-25 12:55:46 +0100484 *
485 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
486 * this, letting us set arbitrary permissions for filesystem access without
487 * changing the "normal" UIDs which are used for other things.
488 *
489 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
490 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100491int inode_permission(struct user_namespace *mnt_userns,
492 struct inode *inode, int mask)
David Howells0bdaea92012-06-25 12:55:46 +0100493{
494 int retval;
495
496 retval = sb_permission(inode->i_sb, inode, mask);
497 if (retval)
498 return retval;
Eric Biggers4bfd0542018-01-16 21:44:24 -0800499
500 if (unlikely(mask & MAY_WRITE)) {
501 /*
502 * Nobody gets write access to an immutable file.
503 */
504 if (IS_IMMUTABLE(inode))
505 return -EPERM;
506
507 /*
508 * Updating mtime will likely cause i_uid and i_gid to be
509 * written back improperly if their true value is unknown
510 * to the vfs.
511 */
Christian Braunerba73d982021-01-21 14:19:31 +0100512 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric Biggers4bfd0542018-01-16 21:44:24 -0800513 return -EACCES;
514 }
515
Christian Brauner47291ba2021-01-21 14:19:24 +0100516 retval = do_inode_permission(mnt_userns, inode, mask);
Eric Biggers4bfd0542018-01-16 21:44:24 -0800517 if (retval)
518 return retval;
519
520 retval = devcgroup_inode_permission(inode, mask);
521 if (retval)
522 return retval;
523
524 return security_inode_permission(inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100525}
Al Viro4d359502014-03-14 12:20:17 -0400526EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100527
528/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800529 * path_get - get a reference to a path
530 * @path: path to get the reference to
531 *
532 * Given a path increment the reference count to the dentry and the vfsmount.
533 */
Al Virodcf787f2013-03-01 23:51:07 -0500534void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800535{
536 mntget(path->mnt);
537 dget(path->dentry);
538}
539EXPORT_SYMBOL(path_get);
540
541/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800542 * path_put - put a reference to a path
543 * @path: path to put the reference to
544 *
545 * Given a path decrement the reference count to the dentry and the vfsmount.
546 */
Al Virodcf787f2013-03-01 23:51:07 -0500547void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Jan Blunck1d957f92008-02-14 19:34:35 -0800549 dput(path->dentry);
550 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Jan Blunck1d957f92008-02-14 19:34:35 -0800552EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Al Viro894bc8c2015-05-02 07:16:16 -0400554#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400555struct nameidata {
556 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400557 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400558 struct path root;
559 struct inode *inode; /* path.dentry.d_inode */
Al Virobcba1e72021-04-01 22:03:41 -0400560 unsigned int flags, state;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +1100561 unsigned seq, m_seq, r_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400562 int last_type;
563 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100564 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400565 struct saved {
566 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500567 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400568 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400569 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400570 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400571 struct filename *name;
572 struct nameidata *saved;
573 unsigned root_seq;
574 int dfd;
Al Viro0f705952020-03-05 11:34:48 -0500575 kuid_t dir_uid;
576 umode_t dir_mode;
Kees Cook3859a272016-10-28 01:22:25 -0700577} __randomize_layout;
Al Viro1f55a6e2014-11-01 19:30:41 -0400578
Al Virobcba1e72021-04-01 22:03:41 -0400579#define ND_ROOT_PRESET 1
580#define ND_ROOT_GRABBED 2
581#define ND_JUMPED 4
582
Al Viro06422962021-04-01 22:28:03 -0400583static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400584{
NeilBrown756daf22015-03-23 13:37:38 +1100585 struct nameidata *old = current->nameidata;
586 p->stack = p->internal;
Al Viro7962c7d2021-04-03 16:49:44 -0400587 p->depth = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -0400588 p->dfd = dfd;
589 p->name = name;
Al Viro7d01ef72021-04-06 12:33:07 -0400590 p->path.mnt = NULL;
591 p->path.dentry = NULL;
NeilBrown756daf22015-03-23 13:37:38 +1100592 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400593 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100594 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400595}
596
Al Viro06422962021-04-01 22:28:03 -0400597static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name,
598 const struct path *root)
599{
600 __set_nameidata(p, dfd, name);
601 p->state = 0;
602 if (unlikely(root)) {
603 p->state = ND_ROOT_PRESET;
604 p->root = *root;
605 }
606}
607
Al Viro9883d182015-05-13 07:28:08 -0400608static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400609{
Al Viro9883d182015-05-13 07:28:08 -0400610 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100611
612 current->nameidata = old;
613 if (old)
614 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500615 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100616 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400617}
618
Al Viro60ef60c2020-03-03 11:43:55 -0500619static bool nd_alloc_stack(struct nameidata *nd)
Al Viro894bc8c2015-05-02 07:16:16 -0400620{
Al Virobc40aee2015-05-09 13:04:24 -0400621 struct saved *p;
622
Al Viro60ef60c2020-03-03 11:43:55 -0500623 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
624 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
625 if (unlikely(!p))
626 return false;
Al Viro894bc8c2015-05-02 07:16:16 -0400627 memcpy(p, nd->internal, sizeof(nd->internal));
628 nd->stack = p;
Al Viro60ef60c2020-03-03 11:43:55 -0500629 return true;
Al Viro894bc8c2015-05-02 07:16:16 -0400630}
631
Eric W. Biederman397d4252015-08-15 20:27:13 -0500632/**
Al Viro6b03f7e2020-02-24 15:53:19 -0500633 * path_connected - Verify that a dentry is below mnt.mnt_root
Eric W. Biederman397d4252015-08-15 20:27:13 -0500634 *
635 * Rename can sometimes move a file or directory outside of a bind
636 * mount, path_connected allows those cases to be detected.
637 */
Al Viro6b03f7e2020-02-24 15:53:19 -0500638static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500639{
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500640 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500641
Christoph Hellwig402dd2c2020-09-24 08:51:28 +0200642 /* Bind mounts can have disconnected paths */
643 if (mnt->mnt_root == sb->s_root)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500644 return true;
645
Al Viro6b03f7e2020-02-24 15:53:19 -0500646 return is_subdir(dentry, mnt->mnt_root);
Eric W. Biederman397d4252015-08-15 20:27:13 -0500647}
648
Al Viro79733872015-05-09 12:55:43 -0400649static void drop_links(struct nameidata *nd)
650{
651 int i = nd->depth;
652 while (i--) {
653 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500654 do_delayed_call(&last->done);
655 clear_delayed_call(&last->done);
Al Viro79733872015-05-09 12:55:43 -0400656 }
657}
658
659static void terminate_walk(struct nameidata *nd)
660{
661 drop_links(nd);
662 if (!(nd->flags & LOOKUP_RCU)) {
663 int i;
664 path_put(&nd->path);
665 for (i = 0; i < nd->depth; i++)
666 path_put(&nd->stack[i].link);
Al Virobcba1e72021-04-01 22:03:41 -0400667 if (nd->state & ND_ROOT_GRABBED) {
Al Viro102b8af2015-05-12 17:35:52 -0400668 path_put(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400669 nd->state &= ~ND_ROOT_GRABBED;
Al Viro102b8af2015-05-12 17:35:52 -0400670 }
Al Viro79733872015-05-09 12:55:43 -0400671 } else {
672 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400673 rcu_read_unlock();
674 }
675 nd->depth = 0;
Al Viro7d01ef72021-04-06 12:33:07 -0400676 nd->path.mnt = NULL;
677 nd->path.dentry = NULL;
Al Viro79733872015-05-09 12:55:43 -0400678}
679
680/* path_put is needed afterwards regardless of success or failure */
Al Viro2aa38472020-02-26 19:19:05 -0500681static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
Al Viro79733872015-05-09 12:55:43 -0400682{
Al Viro2aa38472020-02-26 19:19:05 -0500683 int res = __legitimize_mnt(path->mnt, mseq);
Al Viro79733872015-05-09 12:55:43 -0400684 if (unlikely(res)) {
685 if (res > 0)
686 path->mnt = NULL;
687 path->dentry = NULL;
688 return false;
689 }
690 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
691 path->dentry = NULL;
692 return false;
693 }
694 return !read_seqcount_retry(&path->dentry->d_seq, seq);
695}
696
Al Viro2aa38472020-02-26 19:19:05 -0500697static inline bool legitimize_path(struct nameidata *nd,
698 struct path *path, unsigned seq)
699{
Al Viro5bd73282020-04-05 21:59:55 -0400700 return __legitimize_path(path, seq, nd->m_seq);
Al Viro2aa38472020-02-26 19:19:05 -0500701}
702
Al Viro79733872015-05-09 12:55:43 -0400703static bool legitimize_links(struct nameidata *nd)
704{
705 int i;
Al Viroeacd9aa82021-02-15 12:03:23 -0500706 if (unlikely(nd->flags & LOOKUP_CACHED)) {
707 drop_links(nd);
708 nd->depth = 0;
709 return false;
710 }
Al Viro79733872015-05-09 12:55:43 -0400711 for (i = 0; i < nd->depth; i++) {
712 struct saved *last = nd->stack + i;
713 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
714 drop_links(nd);
715 nd->depth = i + 1;
716 return false;
717 }
718 }
719 return true;
720}
721
Al Viroee594bf2019-07-16 21:05:36 -0400722static bool legitimize_root(struct nameidata *nd)
723{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100724 /*
725 * For scoped-lookups (where nd->root has been zeroed), we need to
726 * restart the whole lookup from scratch -- because set_root() is wrong
727 * for these lookups (nd->dfd is the root, not the filesystem root).
728 */
729 if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
730 return false;
731 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
Al Virobcba1e72021-04-01 22:03:41 -0400732 if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET))
Al Viroee594bf2019-07-16 21:05:36 -0400733 return true;
Al Virobcba1e72021-04-01 22:03:41 -0400734 nd->state |= ND_ROOT_GRABBED;
Al Viroee594bf2019-07-16 21:05:36 -0400735 return legitimize_path(nd, &nd->root, nd->root_seq);
736}
737
Al Viro19660af2011-03-25 10:32:48 -0400738/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100739 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400740 * Documentation/filesystems/path-lookup.txt). In situations when we can't
741 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500742 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400743 * mode. Refcounts are grabbed at the last known good point before rcu-walk
744 * got stuck, so ref-walk may continue from there. If this is not successful
745 * (eg. a seqcount has changed), then failure is returned and it's up to caller
746 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100747 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100748
749/**
Jens Axboee36cffe2020-12-17 09:19:08 -0700750 * try_to_unlazy - try to switch to ref-walk mode.
Al Viro19660af2011-03-25 10:32:48 -0400751 * @nd: nameidata pathwalk data
Jens Axboee36cffe2020-12-17 09:19:08 -0700752 * Returns: true on success, false on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100753 *
Jens Axboee36cffe2020-12-17 09:19:08 -0700754 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
Al Viro4675ac32017-01-09 22:29:15 -0500755 * for ref-walk mode.
756 * Must be called from rcu-walk context.
Jens Axboee36cffe2020-12-17 09:19:08 -0700757 * Nothing should touch nameidata between try_to_unlazy() failure and
Al Viro79733872015-05-09 12:55:43 -0400758 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100759 */
Jens Axboee36cffe2020-12-17 09:19:08 -0700760static bool try_to_unlazy(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100761{
Nick Piggin31e6b012011-01-07 17:49:52 +1100762 struct dentry *parent = nd->path.dentry;
763
764 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700765
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700766 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400767 if (unlikely(!legitimize_links(nd)))
Al Viro4675ac32017-01-09 22:29:15 -0500768 goto out1;
Al Viro84a2bd32019-07-16 21:20:17 -0400769 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
770 goto out;
Al Viroee594bf2019-07-16 21:05:36 -0400771 if (unlikely(!legitimize_root(nd)))
772 goto out;
Al Viro4675ac32017-01-09 22:29:15 -0500773 rcu_read_unlock();
774 BUG_ON(nd->inode != parent->d_inode);
Jens Axboee36cffe2020-12-17 09:19:08 -0700775 return true;
Al Viro4675ac32017-01-09 22:29:15 -0500776
Al Viro84a2bd32019-07-16 21:20:17 -0400777out1:
Al Viro4675ac32017-01-09 22:29:15 -0500778 nd->path.mnt = NULL;
779 nd->path.dentry = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500780out:
781 rcu_read_unlock();
Jens Axboee36cffe2020-12-17 09:19:08 -0700782 return false;
Al Viro4675ac32017-01-09 22:29:15 -0500783}
784
785/**
Al Viroae66db42021-01-04 00:08:41 -0500786 * try_to_unlazy_next - try to switch to ref-walk mode.
Al Viro4675ac32017-01-09 22:29:15 -0500787 * @nd: nameidata pathwalk data
Al Viroae66db42021-01-04 00:08:41 -0500788 * @dentry: next dentry to step into
789 * @seq: seq number to check @dentry against
790 * Returns: true on success, false on failure
Al Viro4675ac32017-01-09 22:29:15 -0500791 *
Al Viroae66db42021-01-04 00:08:41 -0500792 * Similar to to try_to_unlazy(), but here we have the next dentry already
793 * picked by rcu-walk and want to legitimize that in addition to the current
794 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
795 * Nothing should touch nameidata between try_to_unlazy_next() failure and
Al Viro4675ac32017-01-09 22:29:15 -0500796 * terminate_walk().
797 */
Al Viroae66db42021-01-04 00:08:41 -0500798static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq)
Al Viro4675ac32017-01-09 22:29:15 -0500799{
800 BUG_ON(!(nd->flags & LOOKUP_RCU));
801
802 nd->flags &= ~LOOKUP_RCU;
803 if (unlikely(!legitimize_links(nd)))
804 goto out2;
Al Viro79733872015-05-09 12:55:43 -0400805 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
806 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500807 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro79733872015-05-09 12:55:43 -0400808 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400809
Linus Torvalds15570082013-09-02 11:38:06 -0700810 /*
Al Viro4675ac32017-01-09 22:29:15 -0500811 * We need to move both the parent and the dentry from the RCU domain
812 * to be properly refcounted. And the sequence number in the dentry
813 * validates *both* dentry counters, since we checked the sequence
814 * number of the parent after we got the child sequence number. So we
815 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700816 */
Al Viro4675ac32017-01-09 22:29:15 -0500817 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
818 goto out;
Al Viro84a2bd32019-07-16 21:20:17 -0400819 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
820 goto out_dput;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700821 /*
822 * Sequence counts matched. Now make sure that the root is
823 * still valid and get it if required.
824 */
Al Viro84a2bd32019-07-16 21:20:17 -0400825 if (unlikely(!legitimize_root(nd)))
826 goto out_dput;
Al Viro8b61e742013-11-08 12:45:01 -0500827 rcu_read_unlock();
Al Viroae66db42021-01-04 00:08:41 -0500828 return true;
Al Viro19660af2011-03-25 10:32:48 -0400829
Al Viro79733872015-05-09 12:55:43 -0400830out2:
831 nd->path.mnt = NULL;
832out1:
833 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700834out:
Al Viro8b61e742013-11-08 12:45:01 -0500835 rcu_read_unlock();
Al Viroae66db42021-01-04 00:08:41 -0500836 return false;
Al Viro84a2bd32019-07-16 21:20:17 -0400837out_dput:
838 rcu_read_unlock();
839 dput(dentry);
Al Viroae66db42021-01-04 00:08:41 -0500840 return false;
Nick Piggin31e6b012011-01-07 17:49:52 +1100841}
842
Al Viro4ce16ef32012-06-10 16:10:59 -0400843static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100844{
Al Viroa89f8332017-01-09 22:25:28 -0500845 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
846 return dentry->d_op->d_revalidate(dentry, flags);
847 else
848 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100849}
850
Al Viro9f1fafe2011-03-25 11:00:12 -0400851/**
852 * complete_walk - successful completion of path walk
853 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500854 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400855 * If we had been in RCU mode, drop out of it and legitimize nd->path.
856 * Revalidate the final result, unless we'd already done that during
857 * the path walk or the filesystem doesn't ask for it. Return 0 on
858 * success, -error on failure. In case of failure caller does not
859 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500860 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400861static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500862{
Al Viro16c2cd72011-02-22 15:50:10 -0500863 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500864 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500865
Al Viro9f1fafe2011-03-25 11:00:12 -0400866 if (nd->flags & LOOKUP_RCU) {
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100867 /*
868 * We don't want to zero nd->root for scoped-lookups or
869 * externally-managed nd->root.
870 */
Al Virobcba1e72021-04-01 22:03:41 -0400871 if (!(nd->state & ND_ROOT_PRESET))
872 if (!(nd->flags & LOOKUP_IS_SCOPED))
873 nd->root.mnt = NULL;
Jens Axboe6c6ec2b2020-12-17 09:19:09 -0700874 nd->flags &= ~LOOKUP_CACHED;
Jens Axboee36cffe2020-12-17 09:19:08 -0700875 if (!try_to_unlazy(nd))
Al Viro48a066e2013-09-29 22:06:07 -0400876 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400877 }
878
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100879 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
880 /*
881 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
882 * ever step outside the root during lookup" and should already
883 * be guaranteed by the rest of namei, we want to avoid a namei
884 * BUG resulting in userspace being given a path that was not
885 * scoped within the root at some point during the lookup.
886 *
887 * So, do a final sanity-check to make sure that in the
888 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
889 * we won't silently return an fd completely outside of the
890 * requested root to userspace.
891 *
892 * Userspace could move the path outside the root after this
893 * check, but as discussed elsewhere this is not a concern (the
894 * resolved file was inside the root at some point).
895 */
896 if (!path_is_under(&nd->path, &nd->root))
897 return -EXDEV;
898 }
899
Al Virobcba1e72021-04-01 22:03:41 -0400900 if (likely(!(nd->state & ND_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500901 return 0;
902
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500903 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500904 return 0;
905
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500906 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500907 if (status > 0)
908 return 0;
909
Al Viro16c2cd72011-02-22 15:50:10 -0500910 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500911 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500912
Jeff Layton39159de2009-12-07 12:01:50 -0500913 return status;
914}
915
Aleksa Sarai740a1672019-12-07 01:13:29 +1100916static int set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400917{
Al Viro7bd88372014-09-13 21:55:46 -0400918 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100919
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100920 /*
921 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
922 * still have to ensure it doesn't happen because it will cause a breakout
923 * from the dirfd.
924 */
925 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
926 return -ENOTRECOVERABLE;
927
Al Viro9e6697e2015-12-05 20:07:21 -0500928 if (nd->flags & LOOKUP_RCU) {
929 unsigned seq;
930
931 do {
932 seq = read_seqcount_begin(&fs->seq);
933 nd->root = fs->root;
934 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
935 } while (read_seqcount_retry(&fs->seq, seq));
936 } else {
937 get_fs_root(fs, &nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400938 nd->state |= ND_ROOT_GRABBED;
Al Viro9e6697e2015-12-05 20:07:21 -0500939 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100940 return 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100941}
942
Al Viro248fb5b2015-12-05 20:51:58 -0500943static int nd_jump_root(struct nameidata *nd)
944{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100945 if (unlikely(nd->flags & LOOKUP_BENEATH))
946 return -EXDEV;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100947 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
948 /* Absolute path arguments to path_init() are allowed. */
949 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
950 return -EXDEV;
951 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100952 if (!nd->root.mnt) {
953 int error = set_root(nd);
954 if (error)
955 return error;
956 }
Al Viro248fb5b2015-12-05 20:51:58 -0500957 if (nd->flags & LOOKUP_RCU) {
958 struct dentry *d;
959 nd->path = nd->root;
960 d = nd->path.dentry;
961 nd->inode = d->d_inode;
962 nd->seq = nd->root_seq;
963 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
964 return -ECHILD;
965 } else {
966 path_put(&nd->path);
967 nd->path = nd->root;
968 path_get(&nd->path);
969 nd->inode = nd->path.dentry->d_inode;
970 }
Al Virobcba1e72021-04-01 22:03:41 -0400971 nd->state |= ND_JUMPED;
Al Viro248fb5b2015-12-05 20:51:58 -0500972 return 0;
973}
974
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400975/*
Al Viro6b255392015-11-17 10:20:54 -0500976 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400977 * caller must have taken a reference to path beforehand.
978 */
Aleksa Sarai1bc82072019-12-07 01:13:28 +1100979int nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400980{
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100981 int error = -ELOOP;
Al Viro6e77137b2015-05-02 13:37:52 -0400982 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400983
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100984 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
985 goto err;
986
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100987 error = -EXDEV;
988 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
989 if (nd->path.mnt != path->mnt)
990 goto err;
991 }
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100992 /* Not currently safe for scoped-lookups. */
993 if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
994 goto err;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100995
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100996 path_put(&nd->path);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400997 nd->path = *path;
998 nd->inode = nd->path.dentry->d_inode;
Al Virobcba1e72021-04-01 22:03:41 -0400999 nd->state |= ND_JUMPED;
Aleksa Sarai1bc82072019-12-07 01:13:28 +11001000 return 0;
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001001
1002err:
1003 path_put(path);
1004 return error;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001005}
1006
Al Virob9ff4422015-05-02 20:19:23 -04001007static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -04001008{
Al Viro21c30032015-05-03 21:06:24 -04001009 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -05001010 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -04001011 if (!(nd->flags & LOOKUP_RCU))
1012 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -04001013}
1014
Linus Torvalds561ec642012-10-26 10:05:07 -07001015int sysctl_protected_symlinks __read_mostly = 0;
1016int sysctl_protected_hardlinks __read_mostly = 0;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001017int sysctl_protected_fifos __read_mostly;
1018int sysctl_protected_regular __read_mostly;
Kees Cook800179c2012-07-25 17:29:07 -07001019
1020/**
1021 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -07001022 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -07001023 *
1024 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1025 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1026 * in a sticky world-writable directory. This is to protect privileged
1027 * processes from failing races against path names that may change out
1028 * from under them by way of other users creating malicious symlinks.
1029 * It will permit symlinks to be followed only when outside a sticky
1030 * world-writable directory, or when the uid of the symlink and follower
1031 * match, or when the directory owner matches the symlink's owner.
1032 *
1033 * Returns 0 if following the symlink is allowed, -ve on error.
1034 */
Al Viroad6cc4c2020-01-14 14:41:39 -05001035static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001036{
Christian Braunerba73d982021-01-21 14:19:31 +01001037 struct user_namespace *mnt_userns;
1038 kuid_t i_uid;
1039
Kees Cook800179c2012-07-25 17:29:07 -07001040 if (!sysctl_protected_symlinks)
1041 return 0;
1042
Christian Braunerba73d982021-01-21 14:19:31 +01001043 mnt_userns = mnt_user_ns(nd->path.mnt);
1044 i_uid = i_uid_into_mnt(mnt_userns, inode);
Kees Cook800179c2012-07-25 17:29:07 -07001045 /* Allowed if owner and follower match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001046 if (uid_eq(current_cred()->fsuid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001047 return 0;
1048
1049 /* Allowed if parent directory not sticky and world-writable. */
Al Viro0f705952020-03-05 11:34:48 -05001050 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
Kees Cook800179c2012-07-25 17:29:07 -07001051 return 0;
1052
1053 /* Allowed if parent directory and link owner match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001054 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001055 return 0;
1056
Al Viro319565022015-05-07 20:37:40 -04001057 if (nd->flags & LOOKUP_RCU)
1058 return -ECHILD;
1059
Richard Guy Briggsea841ba2018-03-21 04:42:21 -04001060 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Kees Cook245d7362019-10-02 16:41:58 -07001061 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
Kees Cook800179c2012-07-25 17:29:07 -07001062 return -EACCES;
1063}
1064
1065/**
1066 * safe_hardlink_source - Check for safe hardlink conditions
Christian Braunerba73d982021-01-21 14:19:31 +01001067 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001068 * @inode: the source inode to hardlink from
1069 *
1070 * Return false if at least one of the following conditions:
1071 * - inode is not a regular file
1072 * - inode is setuid
1073 * - inode is setgid and group-exec
1074 * - access failure for read and write
1075 *
1076 * Otherwise returns true.
1077 */
Christian Braunerba73d982021-01-21 14:19:31 +01001078static bool safe_hardlink_source(struct user_namespace *mnt_userns,
1079 struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001080{
1081 umode_t mode = inode->i_mode;
1082
1083 /* Special files should not get pinned to the filesystem. */
1084 if (!S_ISREG(mode))
1085 return false;
1086
1087 /* Setuid files should not get pinned to the filesystem. */
1088 if (mode & S_ISUID)
1089 return false;
1090
1091 /* Executable setgid files should not get pinned to the filesystem. */
1092 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1093 return false;
1094
1095 /* Hardlinking to unreadable or unwritable sources is dangerous. */
Christian Braunerba73d982021-01-21 14:19:31 +01001096 if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
Kees Cook800179c2012-07-25 17:29:07 -07001097 return false;
1098
1099 return true;
1100}
1101
1102/**
1103 * may_linkat - Check permissions for creating a hardlink
Christian Braunerba73d982021-01-21 14:19:31 +01001104 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001105 * @link: the source to hardlink from
1106 *
1107 * Block hardlink when all of:
1108 * - sysctl_protected_hardlinks enabled
1109 * - fsuid does not match inode
1110 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001111 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -07001112 *
Christian Braunerba73d982021-01-21 14:19:31 +01001113 * If the inode has been found through an idmapped mount the user namespace of
1114 * the vfsmount must be passed through @mnt_userns. This function will then take
1115 * care to map the inode according to @mnt_userns before checking permissions.
1116 * On non-idmapped mounts or if permission checking is to be performed on the
1117 * raw inode simply passs init_user_ns.
1118 *
Kees Cook800179c2012-07-25 17:29:07 -07001119 * Returns 0 if successful, -ve on error.
1120 */
Christian Braunerba73d982021-01-21 14:19:31 +01001121int may_linkat(struct user_namespace *mnt_userns, struct path *link)
Kees Cook800179c2012-07-25 17:29:07 -07001122{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001123 struct inode *inode = link->dentry->d_inode;
1124
1125 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01001126 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
1127 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001128 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -07001129
1130 if (!sysctl_protected_hardlinks)
1131 return 0;
1132
Kees Cook800179c2012-07-25 17:29:07 -07001133 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1134 * otherwise, it must be a safe source.
1135 */
Christian Braunerba73d982021-01-21 14:19:31 +01001136 if (safe_hardlink_source(mnt_userns, inode) ||
1137 inode_owner_or_capable(mnt_userns, inode))
Kees Cook800179c2012-07-25 17:29:07 -07001138 return 0;
1139
Kees Cook245d7362019-10-02 16:41:58 -07001140 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001141 return -EPERM;
1142}
1143
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001144/**
1145 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1146 * should be allowed, or not, on files that already
1147 * exist.
Christian Braunerba73d982021-01-21 14:19:31 +01001148 * @mnt_userns: user namespace of the mount the inode was found from
Randy Dunlap2111c3c2021-02-15 20:29:28 -08001149 * @nd: nameidata pathwalk data
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001150 * @inode: the inode of the file to open
1151 *
1152 * Block an O_CREAT open of a FIFO (or a regular file) when:
1153 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1154 * - the file already exists
1155 * - we are in a sticky directory
1156 * - we don't own the file
1157 * - the owner of the directory doesn't own the file
1158 * - the directory is world writable
1159 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1160 * the directory doesn't have to be world writable: being group writable will
1161 * be enough.
1162 *
Christian Braunerba73d982021-01-21 14:19:31 +01001163 * If the inode has been found through an idmapped mount the user namespace of
1164 * the vfsmount must be passed through @mnt_userns. This function will then take
1165 * care to map the inode according to @mnt_userns before checking permissions.
1166 * On non-idmapped mounts or if permission checking is to be performed on the
1167 * raw inode simply passs init_user_ns.
1168 *
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001169 * Returns 0 if the open is allowed, -ve on error.
1170 */
Christian Braunerba73d982021-01-21 14:19:31 +01001171static int may_create_in_sticky(struct user_namespace *mnt_userns,
1172 struct nameidata *nd, struct inode *const inode)
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001173{
Christian Braunerba73d982021-01-21 14:19:31 +01001174 umode_t dir_mode = nd->dir_mode;
1175 kuid_t dir_uid = nd->dir_uid;
1176
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001177 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1178 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Virod0cb5012020-01-26 09:29:34 -05001179 likely(!(dir_mode & S_ISVTX)) ||
Christian Braunerba73d982021-01-21 14:19:31 +01001180 uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
1181 uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001182 return 0;
1183
Al Virod0cb5012020-01-26 09:29:34 -05001184 if (likely(dir_mode & 0002) ||
1185 (dir_mode & 0020 &&
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001186 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1187 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
Kees Cook245d7362019-10-02 16:41:58 -07001188 const char *operation = S_ISFIFO(inode->i_mode) ?
1189 "sticky_create_fifo" :
1190 "sticky_create_regular";
1191 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001192 return -EACCES;
1193 }
1194 return 0;
1195}
1196
David Howellsf015f1262012-06-25 12:55:28 +01001197/*
1198 * follow_up - Find the mountpoint of path's vfsmount
1199 *
1200 * Given a path, find the mountpoint of its source file system.
1201 * Replace @path with the path of the mountpoint in the parent mount.
1202 * Up is towards /.
1203 *
1204 * Return 1 if we went up a level and 0 if we were already at the
1205 * root.
1206 */
Al Virobab77eb2009-04-18 03:26:48 -04001207int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Al Viro0714a532011-11-24 22:19:58 -05001209 struct mount *mnt = real_mount(path->mnt);
1210 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001212
Al Viro48a066e2013-09-29 22:06:07 -04001213 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001214 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001215 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001216 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return 0;
1218 }
Al Viro0714a532011-11-24 22:19:58 -05001219 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001220 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001221 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001222 dput(path->dentry);
1223 path->dentry = mountpoint;
1224 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001225 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return 1;
1227}
Al Viro4d359502014-03-14 12:20:17 -04001228EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Al Viro7ef482f2020-02-26 17:50:13 -05001230static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1231 struct path *path, unsigned *seqp)
1232{
1233 while (mnt_has_parent(m)) {
1234 struct dentry *mountpoint = m->mnt_mountpoint;
1235
1236 m = m->mnt_parent;
1237 if (unlikely(root->dentry == mountpoint &&
1238 root->mnt == &m->mnt))
1239 break;
1240 if (mountpoint != m->mnt.mnt_root) {
1241 path->mnt = &m->mnt;
1242 path->dentry = mountpoint;
1243 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1244 return true;
1245 }
1246 }
1247 return false;
1248}
1249
Al Viro2aa38472020-02-26 19:19:05 -05001250static bool choose_mountpoint(struct mount *m, const struct path *root,
1251 struct path *path)
1252{
1253 bool found;
1254
1255 rcu_read_lock();
1256 while (1) {
1257 unsigned seq, mseq = read_seqbegin(&mount_lock);
1258
1259 found = choose_mountpoint_rcu(m, root, path, &seq);
1260 if (unlikely(!found)) {
1261 if (!read_seqretry(&mount_lock, mseq))
1262 break;
1263 } else {
1264 if (likely(__legitimize_path(path, seq, mseq)))
1265 break;
1266 rcu_read_unlock();
1267 path_put(path);
1268 rcu_read_lock();
1269 }
1270 }
1271 rcu_read_unlock();
1272 return found;
1273}
1274
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001275/*
David Howells9875cf82011-01-14 18:45:21 +00001276 * Perform an automount
1277 * - return -EISDIR to tell follow_managed() to stop and return the path we
1278 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001280static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
Nick Piggin31e6b012011-01-07 17:49:52 +11001281{
Al Viro25e195a2020-01-11 11:27:46 -05001282 struct dentry *dentry = path->dentry;
Al Viro463ffb22005-06-06 13:36:05 -07001283
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001284 /* We don't want to mount if someone's just doing a stat -
1285 * unless they're stat'ing a directory and appended a '/' to
1286 * the name.
1287 *
1288 * We do, however, want to mount if someone wants to open or
1289 * create a file of any type under the mountpoint, wants to
1290 * traverse through the mountpoint or wants to open the
1291 * mounted directory. Also, autofs may mark negative dentries
1292 * as being automount points. These will need the attentions
1293 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001294 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001295 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001296 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Al Viro25e195a2020-01-11 11:27:46 -05001297 dentry->d_inode)
Ian Kent5d38f042017-11-29 16:11:26 -08001298 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001299
Al Viro1c9f5e02020-01-16 22:05:18 -05001300 if (count && (*count)++ >= MAXSYMLINKS)
David Howells9875cf82011-01-14 18:45:21 +00001301 return -ELOOP;
1302
Al Viro25e195a2020-01-11 11:27:46 -05001303 return finish_automount(dentry->d_op->d_automount(path), path);
David Howells9875cf82011-01-14 18:45:21 +00001304}
1305
1306/*
Al Viro9deed3e2020-01-17 08:45:08 -05001307 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1308 * dentries are pinned but not locked here, so negative dentry can go
1309 * positive right under us. Use of smp_load_acquire() provides a barrier
1310 * sufficient for ->d_inode and ->d_flags consistency.
David Howells9875cf82011-01-14 18:45:21 +00001311 */
Al Viro9deed3e2020-01-17 08:45:08 -05001312static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1313 int *count, unsigned lookup_flags)
David Howells9875cf82011-01-14 18:45:21 +00001314{
Al Viro9deed3e2020-01-17 08:45:08 -05001315 struct vfsmount *mnt = path->mnt;
David Howells9875cf82011-01-14 18:45:21 +00001316 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001317 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001318
Al Viro9deed3e2020-01-17 08:45:08 -05001319 while (flags & DCACHE_MANAGED_DENTRY) {
David Howellscc53ce52011-01-14 18:45:26 +00001320 /* Allow the filesystem to manage the transit without i_mutex
1321 * being held. */
Al Virod41efb52019-11-04 22:30:52 -05001322 if (flags & DCACHE_MANAGE_TRANSIT) {
Ian Kentfb5f51c2016-11-24 08:03:41 +11001323 ret = path->dentry->d_op->d_manage(path, false);
Al Viro508c8772020-01-14 22:09:57 -05001324 flags = smp_load_acquire(&path->dentry->d_flags);
David Howellscc53ce52011-01-14 18:45:26 +00001325 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001326 break;
David Howellscc53ce52011-01-14 18:45:26 +00001327 }
1328
Al Viro9deed3e2020-01-17 08:45:08 -05001329 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
David Howells9875cf82011-01-14 18:45:21 +00001330 struct vfsmount *mounted = lookup_mnt(path);
Al Viro9deed3e2020-01-17 08:45:08 -05001331 if (mounted) { // ... in our namespace
David Howells9875cf82011-01-14 18:45:21 +00001332 dput(path->dentry);
1333 if (need_mntput)
1334 mntput(path->mnt);
1335 path->mnt = mounted;
1336 path->dentry = dget(mounted->mnt_root);
Al Viro9deed3e2020-01-17 08:45:08 -05001337 // here we know it's positive
1338 flags = path->dentry->d_flags;
David Howells9875cf82011-01-14 18:45:21 +00001339 need_mntput = true;
1340 continue;
1341 }
David Howells9875cf82011-01-14 18:45:21 +00001342 }
1343
Al Viro9deed3e2020-01-17 08:45:08 -05001344 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1345 break;
David Howells9875cf82011-01-14 18:45:21 +00001346
Al Viro9deed3e2020-01-17 08:45:08 -05001347 // uncovered automount point
1348 ret = follow_automount(path, count, lookup_flags);
1349 flags = smp_load_acquire(&path->dentry->d_flags);
1350 if (ret < 0)
1351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Al Viro8aef1882011-06-16 15:10:06 +01001353
Al Viro9deed3e2020-01-17 08:45:08 -05001354 if (ret == -EISDIR)
1355 ret = 0;
1356 // possible if you race with several mount --move
1357 if (need_mntput && path->mnt == mnt)
1358 mntput(path->mnt);
1359 if (!ret && unlikely(d_flags_negative(flags)))
Al Virod41efb52019-11-04 22:30:52 -05001360 ret = -ENOENT;
Al Viro9deed3e2020-01-17 08:45:08 -05001361 *jumped = need_mntput;
Al Viro84027522015-04-22 10:30:08 -04001362 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Al Viro9deed3e2020-01-17 08:45:08 -05001365static inline int traverse_mounts(struct path *path, bool *jumped,
1366 int *count, unsigned lookup_flags)
1367{
1368 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1369
1370 /* fastpath */
1371 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1372 *jumped = false;
1373 if (unlikely(d_flags_negative(flags)))
1374 return -ENOENT;
1375 return 0;
1376 }
1377 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1378}
1379
David Howellscc53ce52011-01-14 18:45:26 +00001380int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct vfsmount *mounted;
1383
Al Viro1c755af2009-04-18 14:06:57 -04001384 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001386 dput(path->dentry);
1387 mntput(path->mnt);
1388 path->mnt = mounted;
1389 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 1;
1391 }
1392 return 0;
1393}
Al Viro4d359502014-03-14 12:20:17 -04001394EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
David Howells9875cf82011-01-14 18:45:21 +00001396/*
Al Viro9deed3e2020-01-17 08:45:08 -05001397 * Follow down to the covering mount currently visible to userspace. At each
1398 * point, the filesystem owning that dentry may be queried as to whether the
1399 * caller is permitted to proceed or not.
1400 */
1401int follow_down(struct path *path)
1402{
1403 struct vfsmount *mnt = path->mnt;
1404 bool jumped;
1405 int ret = traverse_mounts(path, &jumped, NULL, 0);
1406
1407 if (path->mnt != mnt)
1408 mntput(mnt);
1409 return ret;
1410}
1411EXPORT_SYMBOL(follow_down);
1412
1413/*
Al Viro287548e2011-05-27 06:50:06 -04001414 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1415 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001416 */
1417static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001418 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001419{
Al Viroea936ae2020-01-16 09:52:04 -05001420 struct dentry *dentry = path->dentry;
1421 unsigned int flags = dentry->d_flags;
1422
1423 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1424 return true;
1425
1426 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1427 return false;
1428
Ian Kent62a73752011-03-25 01:51:02 +08001429 for (;;) {
Ian Kent62a73752011-03-25 01:51:02 +08001430 /*
1431 * Don't forget we might have a non-mountpoint managed dentry
1432 * that wants to block transit.
1433 */
Al Viroea936ae2020-01-16 09:52:04 -05001434 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1435 int res = dentry->d_op->d_manage(path, true);
1436 if (res)
1437 return res == -EISDIR;
1438 flags = dentry->d_flags;
NeilBrownb8faf032014-08-04 17:06:29 +10001439 }
Ian Kent62a73752011-03-25 01:51:02 +08001440
Al Viroea936ae2020-01-16 09:52:04 -05001441 if (flags & DCACHE_MOUNTED) {
1442 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1443 if (mounted) {
1444 path->mnt = &mounted->mnt;
1445 dentry = path->dentry = mounted->mnt.mnt_root;
Al Virobcba1e72021-04-01 22:03:41 -04001446 nd->state |= ND_JUMPED;
Al Viroea936ae2020-01-16 09:52:04 -05001447 *seqp = read_seqcount_begin(&dentry->d_seq);
1448 *inode = dentry->d_inode;
1449 /*
1450 * We don't need to re-check ->d_seq after this
1451 * ->d_inode read - there will be an RCU delay
1452 * between mount hash removal and ->mnt_root
1453 * becoming unpinned.
1454 */
1455 flags = dentry->d_flags;
1456 continue;
1457 }
1458 if (read_seqretry(&mount_lock, nd->m_seq))
1459 return false;
1460 }
1461 return !(flags & DCACHE_NEED_AUTOMOUNT);
David Howells9875cf82011-01-14 18:45:21 +00001462 }
Al Viro287548e2011-05-27 06:50:06 -04001463}
1464
Al Virodb3c9ad2020-01-09 14:41:00 -05001465static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1466 struct path *path, struct inode **inode,
1467 unsigned int *seqp)
Al Virobd7c4b52020-01-08 20:37:23 -05001468{
Al Viro9deed3e2020-01-17 08:45:08 -05001469 bool jumped;
Al Virodb3c9ad2020-01-09 14:41:00 -05001470 int ret;
Al Virobd7c4b52020-01-08 20:37:23 -05001471
Al Virodb3c9ad2020-01-09 14:41:00 -05001472 path->mnt = nd->path.mnt;
1473 path->dentry = dentry;
Al Viroc1530072020-01-09 14:50:18 -05001474 if (nd->flags & LOOKUP_RCU) {
1475 unsigned int seq = *seqp;
1476 if (unlikely(!*inode))
1477 return -ENOENT;
1478 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viro9deed3e2020-01-17 08:45:08 -05001479 return 0;
Al Viroae66db42021-01-04 00:08:41 -05001480 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viroc1530072020-01-09 14:50:18 -05001481 return -ECHILD;
1482 // *path might've been clobbered by __follow_mount_rcu()
1483 path->mnt = nd->path.mnt;
1484 path->dentry = dentry;
1485 }
Al Viro9deed3e2020-01-17 08:45:08 -05001486 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1487 if (jumped) {
1488 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1489 ret = -EXDEV;
1490 else
Al Virobcba1e72021-04-01 22:03:41 -04001491 nd->state |= ND_JUMPED;
Al Viro9deed3e2020-01-17 08:45:08 -05001492 }
1493 if (unlikely(ret)) {
1494 dput(path->dentry);
1495 if (path->mnt != nd->path.mnt)
1496 mntput(path->mnt);
1497 } else {
Al Virobd7c4b52020-01-08 20:37:23 -05001498 *inode = d_backing_inode(path->dentry);
1499 *seqp = 0; /* out of RCU mode, so the value doesn't matter */
1500 }
1501 return ret;
1502}
1503
David Howells9875cf82011-01-14 18:45:21 +00001504/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001505 * This looks up the name in dcache and possibly revalidates the found dentry.
1506 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001507 */
Al Viroe3c13922016-03-06 14:03:27 -05001508static struct dentry *lookup_dcache(const struct qstr *name,
1509 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001510 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001511{
Al Viroa89f8332017-01-09 22:25:28 -05001512 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001513 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001514 int error = d_revalidate(dentry, flags);
1515 if (unlikely(error <= 0)) {
1516 if (!error)
1517 d_invalidate(dentry);
1518 dput(dentry);
1519 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001520 }
1521 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001522 return dentry;
1523}
1524
1525/*
Al Viroa03ece52018-03-08 11:00:45 -05001526 * Parent directory has inode locked exclusive. This is one
1527 * and only case when ->lookup() gets called on non in-lookup
1528 * dentries - as the matter of fact, this only gets called
1529 * when directory is guaranteed to have no in-lookup children
1530 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001531 */
Al Viroa03ece52018-03-08 11:00:45 -05001532static struct dentry *__lookup_hash(const struct qstr *name,
1533 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001534{
Al Viroa03ece52018-03-08 11:00:45 -05001535 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001536 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001537 struct inode *dir = base->d_inode;
1538
1539 if (dentry)
1540 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001541
1542 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001543 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001544 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001545
1546 dentry = d_alloc(base, name);
1547 if (unlikely(!dentry))
1548 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001549
Al Viro72bd8662012-06-10 17:17:17 -04001550 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001551 if (unlikely(old)) {
1552 dput(dentry);
1553 dentry = old;
1554 }
1555 return dentry;
1556}
1557
Al Viro20e34352020-01-09 14:58:31 -05001558static struct dentry *lookup_fast(struct nameidata *nd,
1559 struct inode **inode,
1560 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Nick Piggin31e6b012011-01-07 17:49:52 +11001562 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001563 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001564
Al Viro3cac2602009-08-13 18:27:43 +04001565 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001566 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001567 * of a false negative due to a concurrent rename, the caller is
1568 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001569 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001570 if (nd->flags & LOOKUP_RCU) {
1571 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001572 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001573 if (unlikely(!dentry)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001574 if (!try_to_unlazy(nd))
Al Viro20e34352020-01-09 14:58:31 -05001575 return ERR_PTR(-ECHILD);
1576 return NULL;
Al Viro5d0f49c2016-03-05 21:32:53 -05001577 }
Al Viro5a18fff2011-03-11 04:44:53 -05001578
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001579 /*
1580 * This sequence count validates that the inode matches
1581 * the dentry name information from lookup.
1582 */
David Howells63afdfc2015-05-06 15:59:00 +01001583 *inode = d_backing_inode(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001584 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Al Viro20e34352020-01-09 14:58:31 -05001585 return ERR_PTR(-ECHILD);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001586
1587 /*
1588 * This sequence count validates that the parent had no
1589 * changes while we did the lookup of the dentry above.
1590 *
1591 * The memory barrier in read_seqcount_begin of child is
1592 * enough, we can use __read_seqcount_retry here.
1593 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001594 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Al Viro20e34352020-01-09 14:58:31 -05001595 return ERR_PTR(-ECHILD);
Al Viro5a18fff2011-03-11 04:44:53 -05001596
Al Viro254cf582015-05-05 09:40:46 -04001597 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001598 status = d_revalidate(dentry, nd->flags);
Al Viroc1530072020-01-09 14:50:18 -05001599 if (likely(status > 0))
Al Viro20e34352020-01-09 14:58:31 -05001600 return dentry;
Al Viroae66db42021-01-04 00:08:41 -05001601 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viro20e34352020-01-09 14:58:31 -05001602 return ERR_PTR(-ECHILD);
Steven Rostedt (VMware)26ddb452020-12-09 17:09:28 -05001603 if (status == -ECHILD)
Al Viro209a7fb2017-01-09 01:35:39 -05001604 /* we'd been told to redo it in non-rcu mode */
1605 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001606 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001607 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001608 if (unlikely(!dentry))
Al Viro20e34352020-01-09 14:58:31 -05001609 return NULL;
Al Viroa89f8332017-01-09 22:25:28 -05001610 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001611 }
Al Viro5a18fff2011-03-11 04:44:53 -05001612 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001613 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001614 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001615 dput(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001616 return ERR_PTR(status);
Al Viro5a18fff2011-03-11 04:44:53 -05001617 }
Al Viro20e34352020-01-09 14:58:31 -05001618 return dentry;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001619}
1620
1621/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001622static struct dentry *__lookup_slow(const struct qstr *name,
1623 struct dentry *dir,
1624 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001625{
Al Viro88d83312018-04-06 16:43:47 -04001626 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001627 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001628 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001629
Al Viro19363862016-04-14 19:33:34 -04001630 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001631 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001632 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001633again:
Al Virod9171b92016-04-15 03:33:13 -04001634 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001635 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001636 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001637 if (unlikely(!d_in_lookup(dentry))) {
Al Viroc64cd6e2020-01-10 17:17:19 -05001638 int error = d_revalidate(dentry, flags);
1639 if (unlikely(error <= 0)) {
1640 if (!error) {
1641 d_invalidate(dentry);
Al Viro949a8522016-03-06 14:20:52 -05001642 dput(dentry);
Al Viroc64cd6e2020-01-10 17:17:19 -05001643 goto again;
Al Viro949a8522016-03-06 14:20:52 -05001644 }
Al Viroc64cd6e2020-01-10 17:17:19 -05001645 dput(dentry);
1646 dentry = ERR_PTR(error);
Al Viro949a8522016-03-06 14:20:52 -05001647 }
Al Viro94bdd652016-04-15 02:42:04 -04001648 } else {
1649 old = inode->i_op->lookup(inode, dentry, flags);
1650 d_lookup_done(dentry);
1651 if (unlikely(old)) {
1652 dput(dentry);
1653 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001654 }
1655 }
Al Viroe3c13922016-03-06 14:03:27 -05001656 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Al Viro88d83312018-04-06 16:43:47 -04001659static struct dentry *lookup_slow(const struct qstr *name,
1660 struct dentry *dir,
1661 unsigned int flags)
1662{
1663 struct inode *inode = dir->d_inode;
1664 struct dentry *res;
1665 inode_lock_shared(inode);
1666 res = __lookup_slow(name, dir, flags);
1667 inode_unlock_shared(inode);
1668 return res;
1669}
1670
Christian Braunerba73d982021-01-21 14:19:31 +01001671static inline int may_lookup(struct user_namespace *mnt_userns,
1672 struct nameidata *nd)
Al Viro52094c82011-02-21 21:34:47 -05001673{
1674 if (nd->flags & LOOKUP_RCU) {
Linus Torvalds7d6beb72021-02-23 13:39:45 -08001675 int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Jens Axboee36cffe2020-12-17 09:19:08 -07001676 if (err != -ECHILD || !try_to_unlazy(nd))
Al Viro52094c82011-02-21 21:34:47 -05001677 return err;
Al Viro52094c82011-02-21 21:34:47 -05001678 }
Christian Braunerba73d982021-01-21 14:19:31 +01001679 return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001680}
1681
Al Viro49055902020-03-03 11:22:34 -05001682static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1683{
Al Viro49055902020-03-03 11:22:34 -05001684 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1685 return -ELOOP;
Al Viro45425762020-03-03 11:25:31 -05001686
1687 if (likely(nd->depth != EMBEDDED_LEVELS))
1688 return 0;
1689 if (likely(nd->stack != nd->internal))
1690 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001691 if (likely(nd_alloc_stack(nd)))
Al Viro49055902020-03-03 11:22:34 -05001692 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001693
1694 if (nd->flags & LOOKUP_RCU) {
1695 // we need to grab link before we do unlazy. And we can't skip
1696 // unlazy even if we fail to grab the link - cleanup needs it
Al Viro49055902020-03-03 11:22:34 -05001697 bool grabbed_link = legitimize_path(nd, link, seq);
Al Viro60ef60c2020-03-03 11:43:55 -05001698
Jens Axboee36cffe2020-12-17 09:19:08 -07001699 if (!try_to_unlazy(nd) != 0 || !grabbed_link)
Al Viro60ef60c2020-03-03 11:43:55 -05001700 return -ECHILD;
1701
1702 if (nd_alloc_stack(nd))
1703 return 0;
Al Viro49055902020-03-03 11:22:34 -05001704 }
Al Viro60ef60c2020-03-03 11:43:55 -05001705 return -ENOMEM;
Al Viro49055902020-03-03 11:22:34 -05001706}
1707
Al Virob1a81972020-01-19 12:48:44 -05001708enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1709
Al Viro06708ad2020-01-14 14:26:57 -05001710static const char *pick_link(struct nameidata *nd, struct path *link,
Al Virob1a81972020-01-19 12:48:44 -05001711 struct inode *inode, unsigned seq, int flags)
Al Virod63ff282015-05-04 18:13:23 -04001712{
Al Viro1cf26652015-05-06 16:01:56 -04001713 struct saved *last;
Al Viroad6cc4c2020-01-14 14:41:39 -05001714 const char *res;
Al Viro49055902020-03-03 11:22:34 -05001715 int error = reserve_stack(nd, link, seq);
Al Viroad6cc4c2020-01-14 14:41:39 -05001716
Al Viro49055902020-03-03 11:22:34 -05001717 if (unlikely(error)) {
Al Viro84f0cd92020-03-03 10:14:30 -05001718 if (!(nd->flags & LOOKUP_RCU))
1719 path_put(link);
Al Viro49055902020-03-03 11:22:34 -05001720 return ERR_PTR(error);
Al Viro626de992015-05-04 18:26:59 -04001721 }
Al Viroab104922015-05-10 11:50:01 -04001722 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001723 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001724 clear_delayed_call(&last->done);
Al Viro0450b2d2015-05-08 13:23:53 -04001725 last->seq = seq;
Al Viroad6cc4c2020-01-14 14:41:39 -05001726
Al Virob1a81972020-01-19 12:48:44 -05001727 if (flags & WALK_TRAILING) {
Al Viroad6cc4c2020-01-14 14:41:39 -05001728 error = may_follow_link(nd, inode);
1729 if (unlikely(error))
1730 return ERR_PTR(error);
1731 }
1732
Mattias Nisslerdab741e02020-08-27 11:09:46 -06001733 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1734 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
Al Viroad6cc4c2020-01-14 14:41:39 -05001735 return ERR_PTR(-ELOOP);
1736
1737 if (!(nd->flags & LOOKUP_RCU)) {
1738 touch_atime(&last->link);
1739 cond_resched();
1740 } else if (atime_needs_update(&last->link, inode)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001741 if (!try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001742 return ERR_PTR(-ECHILD);
1743 touch_atime(&last->link);
1744 }
1745
1746 error = security_inode_follow_link(link->dentry, inode,
1747 nd->flags & LOOKUP_RCU);
1748 if (unlikely(error))
1749 return ERR_PTR(error);
1750
Al Viroad6cc4c2020-01-14 14:41:39 -05001751 res = READ_ONCE(inode->i_link);
1752 if (!res) {
1753 const char * (*get)(struct dentry *, struct inode *,
1754 struct delayed_call *);
1755 get = inode->i_op->get_link;
1756 if (nd->flags & LOOKUP_RCU) {
1757 res = get(NULL, inode, &last->done);
Jens Axboee36cffe2020-12-17 09:19:08 -07001758 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001759 res = get(link->dentry, inode, &last->done);
Al Viroad6cc4c2020-01-14 14:41:39 -05001760 } else {
1761 res = get(link->dentry, inode, &last->done);
1762 }
1763 if (!res)
1764 goto all_done;
1765 if (IS_ERR(res))
1766 return res;
1767 }
1768 if (*res == '/') {
1769 error = nd_jump_root(nd);
1770 if (unlikely(error))
1771 return ERR_PTR(error);
1772 while (unlikely(*++res == '/'))
1773 ;
1774 }
1775 if (*res)
1776 return res;
1777all_done: // pure jump
1778 put_link(nd);
1779 return NULL;
Al Virod63ff282015-05-04 18:13:23 -04001780}
1781
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001782/*
1783 * Do we need to follow links? We _really_ want to be able
1784 * to do this check without having to look at inode->i_op,
1785 * so we keep a cache of "no, this doesn't need follow_link"
1786 * for the common case.
1787 */
Al Virob0417d22020-01-14 13:34:20 -05001788static const char *step_into(struct nameidata *nd, int flags,
Al Virocbae4d12020-01-12 13:40:02 -05001789 struct dentry *dentry, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001790{
Al Virocbae4d12020-01-12 13:40:02 -05001791 struct path path;
1792 int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1793
1794 if (err < 0)
Al Virob0417d22020-01-14 13:34:20 -05001795 return ERR_PTR(err);
Al Virocbae4d12020-01-12 13:40:02 -05001796 if (likely(!d_is_symlink(path.dentry)) ||
Al Viro8c4efe22020-01-19 12:44:18 -05001797 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
Al Viroaca29032020-01-09 15:17:57 -05001798 (flags & WALK_NOFOLLOW)) {
Al Viro8f64fb12016-11-14 01:50:26 -05001799 /* not a symlink or should not follow */
Al Viroc99687a2020-03-03 10:56:17 -05001800 if (!(nd->flags & LOOKUP_RCU)) {
1801 dput(nd->path.dentry);
1802 if (nd->path.mnt != path.mnt)
1803 mntput(nd->path.mnt);
1804 }
1805 nd->path = path;
Al Viro8f64fb12016-11-14 01:50:26 -05001806 nd->inode = inode;
1807 nd->seq = seq;
Al Virob0417d22020-01-14 13:34:20 -05001808 return NULL;
Al Viro8f64fb12016-11-14 01:50:26 -05001809 }
Al Viroa7f77542016-02-27 19:31:01 -05001810 if (nd->flags & LOOKUP_RCU) {
Al Viro84f0cd92020-03-03 10:14:30 -05001811 /* make sure that d_is_symlink above matches inode */
Al Virocbae4d12020-01-12 13:40:02 -05001812 if (read_seqcount_retry(&path.dentry->d_seq, seq))
Al Virob0417d22020-01-14 13:34:20 -05001813 return ERR_PTR(-ECHILD);
Al Viro84f0cd92020-03-03 10:14:30 -05001814 } else {
1815 if (path.mnt == nd->path.mnt)
1816 mntget(path.mnt);
Al Viroa7f77542016-02-27 19:31:01 -05001817 }
Al Virob1a81972020-01-19 12:48:44 -05001818 return pick_link(nd, &path, inode, seq, flags);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001819}
1820
Al Viroc2df1962020-02-26 14:33:30 -05001821static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1822 struct inode **inodep,
1823 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001824{
Al Viro12487f32020-02-26 14:59:56 -05001825 struct dentry *parent, *old;
Al Viro957dd412020-02-26 01:40:04 -05001826
Al Viro12487f32020-02-26 14:59:56 -05001827 if (path_equal(&nd->path, &nd->root))
1828 goto in_root;
1829 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro7ef482f2020-02-26 17:50:13 -05001830 struct path path;
Al Viroefe772d2020-02-28 10:06:37 -05001831 unsigned seq;
Al Viro7ef482f2020-02-26 17:50:13 -05001832 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1833 &nd->root, &path, &seq))
1834 goto in_root;
Al Viroefe772d2020-02-28 10:06:37 -05001835 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1836 return ERR_PTR(-ECHILD);
1837 nd->path = path;
1838 nd->inode = path.dentry->d_inode;
1839 nd->seq = seq;
1840 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1841 return ERR_PTR(-ECHILD);
1842 /* we know that mountpoint was pinned */
Al Viro957dd412020-02-26 01:40:04 -05001843 }
Al Viro12487f32020-02-26 14:59:56 -05001844 old = nd->path.dentry;
1845 parent = old->d_parent;
1846 *inodep = parent->d_inode;
1847 *seqp = read_seqcount_begin(&parent->d_seq);
1848 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1849 return ERR_PTR(-ECHILD);
1850 if (unlikely(!path_connected(nd->path.mnt, parent)))
1851 return ERR_PTR(-ECHILD);
1852 return parent;
1853in_root:
Al Viroefe772d2020-02-28 10:06:37 -05001854 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1855 return ERR_PTR(-ECHILD);
Al Viroc2df1962020-02-26 14:33:30 -05001856 if (unlikely(nd->flags & LOOKUP_BENEATH))
1857 return ERR_PTR(-ECHILD);
1858 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001859}
1860
Al Viroc2df1962020-02-26 14:33:30 -05001861static struct dentry *follow_dotdot(struct nameidata *nd,
1862 struct inode **inodep,
1863 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001864{
Al Viro12487f32020-02-26 14:59:56 -05001865 struct dentry *parent;
1866
1867 if (path_equal(&nd->path, &nd->root))
1868 goto in_root;
1869 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro2aa38472020-02-26 19:19:05 -05001870 struct path path;
1871
1872 if (!choose_mountpoint(real_mount(nd->path.mnt),
1873 &nd->root, &path))
1874 goto in_root;
Al Viro165200d2020-02-28 10:17:52 -05001875 path_put(&nd->path);
1876 nd->path = path;
Al Viro2aa38472020-02-26 19:19:05 -05001877 nd->inode = path.dentry->d_inode;
Al Viro165200d2020-02-28 10:17:52 -05001878 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1879 return ERR_PTR(-EXDEV);
Al Viro957dd412020-02-26 01:40:04 -05001880 }
Al Viro12487f32020-02-26 14:59:56 -05001881 /* rare case of legitimate dget_parent()... */
1882 parent = dget_parent(nd->path.dentry);
1883 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1884 dput(parent);
1885 return ERR_PTR(-ENOENT);
1886 }
1887 *seqp = 0;
1888 *inodep = parent->d_inode;
1889 return parent;
1890
1891in_root:
Al Viroc2df1962020-02-26 14:33:30 -05001892 if (unlikely(nd->flags & LOOKUP_BENEATH))
1893 return ERR_PTR(-EXDEV);
1894 dget(nd->path.dentry);
1895 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001896}
1897
Al Viro7521f222020-02-26 12:22:58 -05001898static const char *handle_dots(struct nameidata *nd, int type)
Al Viro957dd412020-02-26 01:40:04 -05001899{
1900 if (type == LAST_DOTDOT) {
Al Viro7521f222020-02-26 12:22:58 -05001901 const char *error = NULL;
Al Viroc2df1962020-02-26 14:33:30 -05001902 struct dentry *parent;
1903 struct inode *inode;
1904 unsigned seq;
Al Viro957dd412020-02-26 01:40:04 -05001905
1906 if (!nd->root.mnt) {
Al Viro7521f222020-02-26 12:22:58 -05001907 error = ERR_PTR(set_root(nd));
Al Viro957dd412020-02-26 01:40:04 -05001908 if (error)
1909 return error;
1910 }
1911 if (nd->flags & LOOKUP_RCU)
Al Viroc2df1962020-02-26 14:33:30 -05001912 parent = follow_dotdot_rcu(nd, &inode, &seq);
Al Viro957dd412020-02-26 01:40:04 -05001913 else
Al Viroc2df1962020-02-26 14:33:30 -05001914 parent = follow_dotdot(nd, &inode, &seq);
1915 if (IS_ERR(parent))
1916 return ERR_CAST(parent);
1917 if (unlikely(!parent))
1918 error = step_into(nd, WALK_NOFOLLOW,
1919 nd->path.dentry, nd->inode, nd->seq);
1920 else
1921 error = step_into(nd, WALK_NOFOLLOW,
1922 parent, inode, seq);
1923 if (unlikely(error))
Al Viro957dd412020-02-26 01:40:04 -05001924 return error;
1925
1926 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1927 /*
1928 * If there was a racing rename or mount along our
1929 * path, then we can't be sure that ".." hasn't jumped
1930 * above nd->root (and so userspace should retry or use
1931 * some fallback).
1932 */
1933 smp_rmb();
1934 if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001935 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001936 if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001937 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001938 }
1939 }
Al Viro7521f222020-02-26 12:22:58 -05001940 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001941}
1942
Al Viro92d27012020-01-14 13:24:17 -05001943static const char *walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001944{
Al Virodb3c9ad2020-01-09 14:41:00 -05001945 struct dentry *dentry;
Al Viroce57dfc2011-03-13 19:58:58 -04001946 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001947 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001948 /*
1949 * "." and ".." are special - ".." especially so because it has
1950 * to be able to know about the current root directory and
1951 * parent relationships.
1952 */
Al Viro4693a542015-05-04 17:47:11 -04001953 if (unlikely(nd->last_type != LAST_NORM)) {
Al Viro1c4ff1a2016-11-14 01:39:36 -05001954 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001955 put_link(nd);
Al Viro7521f222020-02-26 12:22:58 -05001956 return handle_dots(nd, nd->last_type);
Al Viro4693a542015-05-04 17:47:11 -04001957 }
Al Viro20e34352020-01-09 14:58:31 -05001958 dentry = lookup_fast(nd, &inode, &seq);
1959 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001960 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001961 if (unlikely(!dentry)) {
Al Virodb3c9ad2020-01-09 14:41:00 -05001962 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1963 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001964 return ERR_CAST(dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001965 }
Al Viro56676ec2020-03-10 21:54:54 -04001966 if (!(flags & WALK_MORE) && nd->depth)
1967 put_link(nd);
Al Virob0417d22020-01-14 13:34:20 -05001968 return step_into(nd, flags, dentry, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001969}
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001972 * We can do the critical dentry name comparison and hashing
1973 * operations one word at a time, but we are limited to:
1974 *
1975 * - Architectures with fast unaligned word accesses. We could
1976 * do a "get_unaligned()" if this helps and is sufficiently
1977 * fast.
1978 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001979 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1980 * do not trap on the (extremely unlikely) case of a page
1981 * crossing operation.
1982 *
1983 * - Furthermore, we need an efficient 64-bit compile for the
1984 * 64-bit case in order to generate the "number of bytes in
1985 * the final mask". Again, that could be replaced with a
1986 * efficient population count instruction or similar.
1987 */
1988#ifdef CONFIG_DCACHE_WORD_ACCESS
1989
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001990#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001991
George Spelvin468a9422016-05-26 22:11:51 -04001992#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001993
George Spelvin468a9422016-05-26 22:11:51 -04001994/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1995
1996#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001997/*
1998 * Register pressure in the mixing function is an issue, particularly
1999 * on 32-bit x86, but almost any function requires one state value and
2000 * one temporary. Instead, use a function designed for two state values
2001 * and no temporaries.
2002 *
2003 * This function cannot create a collision in only two iterations, so
2004 * we have two iterations to achieve avalanche. In those two iterations,
2005 * we have six layers of mixing, which is enough to spread one bit's
2006 * influence out to 2^6 = 64 state bits.
2007 *
2008 * Rotate constants are scored by considering either 64 one-bit input
2009 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2010 * probability of that delta causing a change to each of the 128 output
2011 * bits, using a sample of random initial states.
2012 *
2013 * The Shannon entropy of the computed probabilities is then summed
2014 * to produce a score. Ideally, any input change has a 50% chance of
2015 * toggling any given output bit.
2016 *
2017 * Mixing scores (in bits) for (12,45):
2018 * Input delta: 1-bit 2-bit
2019 * 1 round: 713.3 42542.6
2020 * 2 rounds: 2753.7 140389.8
2021 * 3 rounds: 5954.1 233458.2
2022 * 4 rounds: 7862.6 256672.2
2023 * Perfect: 8192 258048
2024 * (64*128) (64*63/2 * 128)
2025 */
2026#define HASH_MIX(x, y, a) \
2027 ( x ^= (a), \
2028 y ^= x, x = rol64(x,12),\
2029 x += y, y = rol64(y,45),\
2030 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002031
George Spelvin0fed3ac2016-05-02 06:31:01 -04002032/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04002033 * Fold two longs into one 32-bit hash value. This must be fast, but
2034 * latency isn't quite as critical, as there is a fair bit of additional
2035 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04002036 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04002037static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002038{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002039 y ^= x * GOLDEN_RATIO_64;
2040 y *= GOLDEN_RATIO_64;
2041 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04002042}
2043
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002044#else /* 32-bit case */
2045
George Spelvin2a18da7a2016-05-23 07:43:58 -04002046/*
2047 * Mixing scores (in bits) for (7,20):
2048 * Input delta: 1-bit 2-bit
2049 * 1 round: 330.3 9201.6
2050 * 2 rounds: 1246.4 25475.4
2051 * 3 rounds: 1907.1 31295.1
2052 * 4 rounds: 2042.3 31718.6
2053 * Perfect: 2048 31744
2054 * (32*64) (32*31/2 * 64)
2055 */
2056#define HASH_MIX(x, y, a) \
2057 ( x ^= (a), \
2058 y ^= x, x = rol32(x, 7),\
2059 x += y, y = rol32(y,20),\
2060 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002061
George Spelvin2a18da7a2016-05-23 07:43:58 -04002062static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002063{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002064 /* Use arch-optimized multiply if one exists */
2065 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04002066}
2067
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002068#endif
2069
George Spelvin2a18da7a2016-05-23 07:43:58 -04002070/*
2071 * Return the hash of a string of known length. This is carfully
2072 * designed to match hash_name(), which is the more critical function.
2073 * In particular, we must end by hashing a final word containing 0..7
2074 * payload bytes, to match the way that hash_name() iterates until it
2075 * finds the delimiter after the name.
2076 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002077unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002078{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002079 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002080
2081 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002082 if (!len)
2083 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002084 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002085 if (len < sizeof(unsigned long))
2086 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04002087 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002088 name += sizeof(unsigned long);
2089 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002090 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04002091 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002092done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04002093 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002094}
2095EXPORT_SYMBOL(full_name_hash);
2096
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002097/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002098u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002099{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002100 unsigned long a = 0, x = 0, y = (unsigned long)salt;
2101 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002102 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2103
Linus Torvalds8387ff22016-06-10 07:51:30 -07002104 len = 0;
2105 goto inside;
2106
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002107 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002108 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002109 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002110inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002111 a = load_unaligned_zeropad(name+len);
2112 } while (!has_zero(a, &adata, &constants));
2113
2114 adata = prep_zero_mask(a, adata, &constants);
2115 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002116 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002117
George Spelvin2a18da7a2016-05-23 07:43:58 -04002118 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002119}
2120EXPORT_SYMBOL(hashlen_string);
2121
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002122/*
2123 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002124 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002125 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002126static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002127{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002128 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2129 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07002130 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002131
Linus Torvalds8387ff22016-06-10 07:51:30 -07002132 len = 0;
2133 goto inside;
2134
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002135 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002136 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002137 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002138inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002139 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002140 b = a ^ REPEAT_BYTE('/');
2141 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002142
Linus Torvalds36126f82012-05-26 10:43:17 -07002143 adata = prep_zero_mask(a, adata, &constants);
2144 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002145 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002146 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002147
George Spelvin2a18da7a2016-05-23 07:43:58 -04002148 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002149}
2150
George Spelvin2a18da7a2016-05-23 07:43:58 -04002151#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002152
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002153/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002154unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002155{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002156 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002157 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002158 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002159 return end_name_hash(hash);
2160}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002161EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002162
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002163/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002164u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002165{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002166 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002167 unsigned long len = 0, c;
2168
2169 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002170 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002171 len++;
2172 hash = partial_name_hash(c, hash);
2173 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002174 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002175 return hashlen_create(end_name_hash(hash), len);
2176}
George Spelvinf2a031b2016-05-29 01:26:41 -04002177EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002178
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002179/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002180 * We know there's a real path component here of at least
2181 * one character.
2182 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002183static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002184{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002185 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002186 unsigned long len = 0, c;
2187
2188 c = (unsigned char)*name;
2189 do {
2190 len++;
2191 hash = partial_name_hash(c, hash);
2192 c = (unsigned char)name[len];
2193 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002194 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002195}
2196
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002197#endif
2198
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002201 * This is the basic name resolution function, turning a pathname into
2202 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002204 * Returns 0 and nd will have valid dentry and mnt on success.
2205 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 */
Al Viro6de88d72009-08-09 01:41:57 +04002207static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Al Virod8d46112020-02-23 22:04:15 -05002209 int depth = 0; // depth <= nd->depth
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002211
Al Virob4c03532020-01-19 11:44:51 -05002212 nd->last_type = LAST_ROOT;
Al Viroc1088372020-03-05 15:48:44 -05002213 nd->flags |= LOOKUP_PARENT;
Al Viro9b5858e2018-07-09 16:33:23 -04002214 if (IS_ERR(name))
2215 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 while (*name=='/')
2217 name++;
Al Viro1a97d892020-09-19 17:55:58 +01002218 if (!*name) {
2219 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
Al Viro9e18f102015-04-18 20:44:34 -04002220 return 0;
Al Viro1a97d892020-09-19 17:55:58 +01002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* At this point we know we have a real path component. */
2224 for(;;) {
Christian Brauner549c7292021-01-21 14:19:43 +01002225 struct user_namespace *mnt_userns;
Al Viro92d27012020-01-14 13:24:17 -05002226 const char *link;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002227 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002228 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Christian Brauner549c7292021-01-21 14:19:43 +01002230 mnt_userns = mnt_user_ns(nd->path.mnt);
2231 err = may_lookup(mnt_userns, nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002232 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Linus Torvalds8387ff22016-06-10 07:51:30 -07002235 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Al Virofe479a52011-02-22 15:10:03 -05002237 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002238 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002239 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002240 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002241 type = LAST_DOTDOT;
Al Virobcba1e72021-04-01 22:03:41 -04002242 nd->state |= ND_JUMPED;
Al Viro16c2cd72011-02-22 15:50:10 -05002243 }
Al Virofe479a52011-02-22 15:10:03 -05002244 break;
2245 case 1:
2246 type = LAST_DOT;
2247 }
Al Viro5a202bc2011-03-08 14:17:44 -05002248 if (likely(type == LAST_NORM)) {
2249 struct dentry *parent = nd->path.dentry;
Al Virobcba1e72021-04-01 22:03:41 -04002250 nd->state &= ~ND_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002251 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002252 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002253 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002254 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002255 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002256 hash_len = this.hash_len;
2257 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002258 }
2259 }
Al Virofe479a52011-02-22 15:10:03 -05002260
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002261 nd->last.hash_len = hash_len;
2262 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002263 nd->last_type = type;
2264
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002265 name += hashlen_len(hash_len);
2266 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002267 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002268 /*
2269 * If it wasn't NUL, we know it was '/'. Skip that
2270 * slash, and continue until no more slashes.
2271 */
2272 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002273 name++;
2274 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002275 if (unlikely(!*name)) {
2276OK:
Al Virod8d46112020-02-23 22:04:15 -05002277 /* pathname or trailing symlink, done */
Al Viroc1088372020-03-05 15:48:44 -05002278 if (!depth) {
Christian Brauner549c7292021-01-21 14:19:43 +01002279 nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode);
Al Viro0f705952020-03-05 11:34:48 -05002280 nd->dir_mode = nd->inode->i_mode;
Al Viroc1088372020-03-05 15:48:44 -05002281 nd->flags &= ~LOOKUP_PARENT;
Al Viro8620c232015-05-04 08:58:35 -04002282 return 0;
Al Viroc1088372020-03-05 15:48:44 -05002283 }
Al Viro8620c232015-05-04 08:58:35 -04002284 /* last component of nested symlink */
Al Virod8d46112020-02-23 22:04:15 -05002285 name = nd->stack[--depth].name;
Al Viro8c4efe22020-01-19 12:44:18 -05002286 link = walk_component(nd, 0);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002287 } else {
2288 /* not the last component */
Al Viro8c4efe22020-01-19 12:44:18 -05002289 link = walk_component(nd, WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002290 }
Al Viro92d27012020-01-14 13:24:17 -05002291 if (unlikely(link)) {
2292 if (IS_ERR(link))
2293 return PTR_ERR(link);
2294 /* a symlink to follow */
Al Virod8d46112020-02-23 22:04:15 -05002295 nd->stack[depth++].name = name;
Al Viro92d27012020-01-14 13:24:17 -05002296 name = link;
2297 continue;
Nick Piggin31e6b012011-01-07 17:49:52 +11002298 }
Al Viro97242f92015-08-01 19:59:28 -04002299 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2300 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07002301 if (!try_to_unlazy(nd))
Al Viro97242f92015-08-01 19:59:28 -04002302 return -ECHILD;
2303 }
Al Viro3595e232015-05-09 16:54:45 -04002304 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Al Viroedc2b1d2018-07-09 16:27:23 -04002309/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002310static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Aleksa Sarai740a1672019-12-07 01:13:29 +11002312 int error;
Al Viroc8a53ee2015-05-12 18:43:07 -04002313 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Jens Axboe6c6ec2b2020-12-17 09:19:09 -07002315 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2316 if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
2317 return ERR_PTR(-EAGAIN);
2318
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002319 if (!*s)
2320 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002321 if (flags & LOOKUP_RCU)
2322 rcu_read_lock();
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002323
Al Virobcba1e72021-04-01 22:03:41 -04002324 nd->flags = flags;
2325 nd->state |= ND_JUMPED;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002326
2327 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2328 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2329 smp_rmb();
2330
Al Virobcba1e72021-04-01 22:03:41 -04002331 if (nd->state & ND_ROOT_PRESET) {
David Howellsb18825a2013-09-12 19:22:53 +01002332 struct dentry *root = nd->root.dentry;
2333 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002334 if (*s && unlikely(!d_can_lookup(root)))
2335 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002336 nd->path = nd->root;
2337 nd->inode = inode;
2338 if (flags & LOOKUP_RCU) {
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002339 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002340 nd->root_seq = nd->seq;
Al Viro5b6ca022011-03-09 23:04:47 -05002341 } else {
2342 path_get(&nd->path);
2343 }
Al Viro368ee9b2015-05-08 17:19:59 -04002344 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002345 }
2346
Al Viro2a737872009-04-07 11:49:53 -04002347 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002349 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2350 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
Aleksa Sarai740a1672019-12-07 01:13:29 +11002351 error = nd_jump_root(nd);
2352 if (unlikely(error))
2353 return ERR_PTR(error);
2354 return s;
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002355 }
2356
2357 /* Relative pathname -- get the starting-point it is relative to. */
2358 if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002359 if (flags & LOOKUP_RCU) {
2360 struct fs_struct *fs = current->fs;
2361 unsigned seq;
2362
Al Viroe41f7d42011-02-22 14:02:58 -05002363 do {
2364 seq = read_seqcount_begin(&fs->seq);
2365 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002366 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002367 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2368 } while (read_seqcount_retry(&fs->seq, seq));
2369 } else {
2370 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002371 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002372 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002373 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002374 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002375 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002376 struct dentry *dentry;
2377
Al Viro2903ff02012-08-28 12:52:22 -04002378 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002379 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002380
Al Viro2903ff02012-08-28 12:52:22 -04002381 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002382
Al Viroedc2b1d2018-07-09 16:27:23 -04002383 if (*s && unlikely(!d_can_lookup(dentry))) {
2384 fdput(f);
2385 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002386 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002387
Al Viro2903ff02012-08-28 12:52:22 -04002388 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002389 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002390 nd->inode = nd->path.dentry->d_inode;
2391 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002392 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002393 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002394 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002395 }
Al Viro34a26b92015-05-11 08:05:05 -04002396 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002398
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002399 /* For scoped-lookups we need to set the root to the dirfd as well. */
2400 if (flags & LOOKUP_IS_SCOPED) {
2401 nd->root = nd->path;
2402 if (flags & LOOKUP_RCU) {
2403 nd->root_seq = nd->seq;
2404 } else {
2405 path_get(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -04002406 nd->state |= ND_ROOT_GRABBED;
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002407 }
2408 }
2409 return s;
Al Viro9b4a9b12009-04-07 11:44:16 -04002410}
2411
Al Viro1ccac622020-01-14 10:13:40 -05002412static inline const char *lookup_last(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002413{
Al Virobd92d7f2011-03-14 19:54:59 -04002414 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2415 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2416
Al Viroc1088372020-03-05 15:48:44 -05002417 return walk_component(nd, WALK_TRAILING);
Al Virobd92d7f2011-03-14 19:54:59 -04002418}
2419
Al Viro4f757f32017-04-15 17:31:22 -04002420static int handle_lookup_down(struct nameidata *nd)
2421{
Al Viroc1530072020-01-09 14:50:18 -05002422 if (!(nd->flags & LOOKUP_RCU))
Al Virodb3c9ad2020-01-09 14:41:00 -05002423 dget(nd->path.dentry);
Al Virob0417d22020-01-14 13:34:20 -05002424 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2425 nd->path.dentry, nd->inode, nd->seq));
Al Viro4f757f32017-04-15 17:31:22 -04002426}
2427
Al Viro9b4a9b12009-04-07 11:44:16 -04002428/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002429static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002430{
Al Viroc8a53ee2015-05-12 18:43:07 -04002431 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002432 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002433
Al Viro9b5858e2018-07-09 16:33:23 -04002434 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002435 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002436 if (unlikely(err < 0))
2437 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002438 }
2439
Al Viro1ccac622020-01-14 10:13:40 -05002440 while (!(err = link_path_walk(s, nd)) &&
2441 (s = lookup_last(nd)) != NULL)
2442 ;
Al Viro4f0ed932021-04-06 19:46:51 -04002443 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2444 err = handle_lookup_down(nd);
Al Virobcba1e72021-04-01 22:03:41 -04002445 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please...
Al Viro4f0ed932021-04-06 19:46:51 -04002446 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002447 if (!err)
2448 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002449
Al Virodeb106c2015-05-08 18:05:21 -04002450 if (!err && nd->flags & LOOKUP_DIRECTORY)
2451 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002452 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002453 if (!err) {
2454 *path = nd->path;
2455 nd->path.mnt = NULL;
2456 nd->path.dentry = NULL;
2457 }
2458 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002459 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002460}
Nick Piggin31e6b012011-01-07 17:49:52 +11002461
David Howells31d921c2018-11-01 23:07:24 +00002462int filename_lookup(int dfd, struct filename *name, unsigned flags,
2463 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002464{
Al Viro894bc8c2015-05-02 07:16:16 -04002465 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002466 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002467 if (IS_ERR(name))
2468 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002469 set_nameidata(&nd, dfd, name, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04002470 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002471 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002472 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002473 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002474 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002475
2476 if (likely(!retval))
Al Viro161aff12020-01-11 22:52:26 -05002477 audit_inode(name, path->dentry,
2478 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
Al Viro9883d182015-05-13 07:28:08 -04002479 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002480 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002481 return retval;
2482}
2483
Al Viro8bcb77f2015-05-08 16:59:20 -04002484/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002485static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002486 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002487{
Al Viroc8a53ee2015-05-12 18:43:07 -04002488 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002489 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002490 if (!err)
2491 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002492 if (!err) {
2493 *parent = nd->path;
2494 nd->path.mnt = NULL;
2495 nd->path.dentry = NULL;
2496 }
2497 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002498 return err;
2499}
2500
Al Viro5c31b6c2015-05-12 17:32:54 -04002501static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002502 unsigned int flags, struct path *parent,
2503 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002504{
2505 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002506 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002507
Al Viro5c31b6c2015-05-12 17:32:54 -04002508 if (IS_ERR(name))
2509 return name;
Al Viro06422962021-04-01 22:28:03 -04002510 set_nameidata(&nd, dfd, name, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04002511 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002512 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002513 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002514 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002515 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002516 if (likely(!retval)) {
2517 *last = nd.last;
2518 *type = nd.last_type;
Al Viroc9b07ea2019-07-14 13:22:27 -04002519 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002520 } else {
2521 putname(name);
2522 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002523 }
Al Viro9883d182015-05-13 07:28:08 -04002524 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002525 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002526}
2527
Al Viro79714f72012-06-15 03:01:42 +04002528/* does lookup, returns the object with parent locked */
2529struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002530{
Al Viro5c31b6c2015-05-12 17:32:54 -04002531 struct filename *filename;
2532 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002533 struct qstr last;
2534 int type;
Paul Moore51689102015-01-22 00:00:03 -05002535
Al Viro5c31b6c2015-05-12 17:32:54 -04002536 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2537 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002538 if (IS_ERR(filename))
2539 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002540 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002541 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002542 putname(filename);
2543 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002544 }
Al Viro59551022016-01-22 15:40:57 -05002545 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002546 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002547 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002548 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002549 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002550 }
Paul Moore51689102015-01-22 00:00:03 -05002551 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002552 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002553}
2554
Al Virod1811462008-08-02 00:49:18 -04002555int kern_path(const char *name, unsigned int flags, struct path *path)
2556{
Al Viroabc9f5b2015-05-12 16:53:42 -04002557 return filename_lookup(AT_FDCWD, getname_kernel(name),
2558 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002559}
Al Viro4d359502014-03-14 12:20:17 -04002560EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002561
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002562/**
2563 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2564 * @dentry: pointer to dentry of the base directory
2565 * @mnt: pointer to vfs mount of the base directory
2566 * @name: pointer to file name
2567 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002568 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002569 */
2570int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2571 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002572 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002573{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002574 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002575 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002576 return filename_lookup(AT_FDCWD, getname_kernel(name),
2577 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002578}
Al Viro4d359502014-03-14 12:20:17 -04002579EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002580
Al Viro3c95f0d2018-04-06 16:32:38 -04002581static int lookup_one_len_common(const char *name, struct dentry *base,
2582 int len, struct qstr *this)
2583{
2584 this->name = name;
2585 this->len = len;
2586 this->hash = full_name_hash(base, name, len);
2587 if (!len)
2588 return -EACCES;
2589
2590 if (unlikely(name[0] == '.')) {
2591 if (len < 2 || (len == 2 && name[1] == '.'))
2592 return -EACCES;
2593 }
2594
2595 while (len--) {
2596 unsigned int c = *(const unsigned char *)name++;
2597 if (c == '/' || c == '\0')
2598 return -EACCES;
2599 }
2600 /*
2601 * See if the low-level filesystem might want
2602 * to use its own hash..
2603 */
2604 if (base->d_flags & DCACHE_OP_HASH) {
2605 int err = base->d_op->d_hash(base, this);
2606 if (err < 0)
2607 return err;
2608 }
2609
Christian Brauner47291ba2021-01-21 14:19:24 +01002610 return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC);
Al Viro3c95f0d2018-04-06 16:32:38 -04002611}
2612
Christoph Hellwigeead1912007-10-16 23:25:38 -07002613/**
David Howells0da0b7f2018-06-15 15:19:22 +01002614 * try_lookup_one_len - filesystem helper to lookup single pathname component
2615 * @name: pathname component to lookup
2616 * @base: base directory to lookup from
2617 * @len: maximum length @len should be interpreted to
2618 *
2619 * Look up a dentry by name in the dcache, returning NULL if it does not
2620 * currently exist. The function does not try to create a dentry.
2621 *
2622 * Note that this routine is purely a helper for filesystem usage and should
2623 * not be called by generic code.
2624 *
2625 * The caller must hold base->i_mutex.
2626 */
2627struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2628{
2629 struct qstr this;
2630 int err;
2631
2632 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2633
2634 err = lookup_one_len_common(name, base, len, &this);
2635 if (err)
2636 return ERR_PTR(err);
2637
2638 return lookup_dcache(&this, base, 0);
2639}
2640EXPORT_SYMBOL(try_lookup_one_len);
2641
2642/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002643 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002644 * @name: pathname component to lookup
2645 * @base: base directory to lookup from
2646 * @len: maximum length @len should be interpreted to
2647 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002648 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002649 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002650 *
2651 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002652 */
James Morris057f6c02007-04-26 00:12:05 -07002653struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2654{
Al Viro8613a202018-04-06 16:45:33 -04002655 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002656 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002657 int err;
James Morris057f6c02007-04-26 00:12:05 -07002658
Al Viro59551022016-01-22 15:40:57 -05002659 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002660
Al Viro3c95f0d2018-04-06 16:32:38 -04002661 err = lookup_one_len_common(name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002662 if (err)
2663 return ERR_PTR(err);
2664
Al Viro8613a202018-04-06 16:45:33 -04002665 dentry = lookup_dcache(&this, base, 0);
2666 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002667}
Al Viro4d359502014-03-14 12:20:17 -04002668EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002669
NeilBrownbbddca82016-01-07 16:08:20 -05002670/**
2671 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2672 * @name: pathname component to lookup
2673 * @base: base directory to lookup from
2674 * @len: maximum length @len should be interpreted to
2675 *
2676 * Note that this routine is purely a helper for filesystem usage and should
2677 * not be called by generic code.
2678 *
2679 * Unlike lookup_one_len, it should be called without the parent
2680 * i_mutex held, and will take the i_mutex itself if necessary.
2681 */
2682struct dentry *lookup_one_len_unlocked(const char *name,
2683 struct dentry *base, int len)
2684{
2685 struct qstr this;
NeilBrownbbddca82016-01-07 16:08:20 -05002686 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002687 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002688
Al Viro3c95f0d2018-04-06 16:32:38 -04002689 err = lookup_one_len_common(name, base, len, &this);
NeilBrownbbddca82016-01-07 16:08:20 -05002690 if (err)
2691 return ERR_PTR(err);
2692
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002693 ret = lookup_dcache(&this, base, 0);
2694 if (!ret)
2695 ret = lookup_slow(&this, base, 0);
2696 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002697}
2698EXPORT_SYMBOL(lookup_one_len_unlocked);
2699
Al Viro6c2d47982019-10-31 01:21:58 -04002700/*
2701 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2702 * on negatives. Returns known positive or ERR_PTR(); that's what
2703 * most of the users want. Note that pinned negative with unlocked parent
2704 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2705 * need to be very careful; pinned positives have ->d_inode stable, so
2706 * this one avoids such problems.
2707 */
2708struct dentry *lookup_positive_unlocked(const char *name,
2709 struct dentry *base, int len)
2710{
2711 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
Al Viro2fa6b1e2019-11-12 16:13:06 -05002712 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
Al Viro6c2d47982019-10-31 01:21:58 -04002713 dput(ret);
2714 ret = ERR_PTR(-ENOENT);
2715 }
2716 return ret;
2717}
2718EXPORT_SYMBOL(lookup_positive_unlocked);
2719
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002720#ifdef CONFIG_UNIX98_PTYS
2721int path_pts(struct path *path)
2722{
2723 /* Find something mounted on "pts" in the same directory as
2724 * the input path.
2725 */
Al Viroa6a7eb72020-03-11 13:05:03 -04002726 struct dentry *parent = dget_parent(path->dentry);
2727 struct dentry *child;
Al Viro19f60282020-02-26 20:09:37 -05002728 struct qstr this = QSTR_INIT("pts", 3);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002729
Al Viroa6a7eb72020-03-11 13:05:03 -04002730 if (unlikely(!path_connected(path->mnt, parent))) {
2731 dput(parent);
Al Viro63b27722020-02-24 16:01:19 -05002732 return -ENOENT;
Al Viroa6a7eb72020-03-11 13:05:03 -04002733 }
Al Viro63b27722020-02-24 16:01:19 -05002734 dput(path->dentry);
2735 path->dentry = parent;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002736 child = d_hash_and_lookup(parent, &this);
2737 if (!child)
2738 return -ENOENT;
2739
2740 path->dentry = child;
2741 dput(parent);
Al Viro19f60282020-02-26 20:09:37 -05002742 follow_down(path);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002743 return 0;
2744}
2745#endif
2746
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002747int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2748 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Al Viroabc9f5b2015-05-12 16:53:42 -04002750 return filename_lookup(dfd, getname_flags(name, flags, empty),
2751 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
Al Virob853a162015-05-13 09:12:02 -04002753EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002754
Christian Braunerba73d982021-01-21 14:19:31 +01002755int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
2756 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002758 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002759
Christian Braunerba73d982021-01-21 14:19:31 +01002760 if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002762 if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002764 return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002766EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768/*
2769 * Check whether we can remove a link victim from directory dir, check
2770 * whether the type of victim is right.
2771 * 1. We can't do it if dir is read-only (done in permission())
2772 * 2. We should have write and exec permissions on dir
2773 * 3. We can't remove anything from append-only dir
2774 * 4. We can't do anything with immutable dir (done in permission())
2775 * 5. If the sticky bit on dir is set we should either
2776 * a. be owner of dir, or
2777 * b. be owner of victim, or
2778 * c. have CAP_FOWNER capability
2779 * 6. If the victim is append-only or immutable we can't do antyhing with
2780 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002781 * 7. If the victim has an unknown uid or gid we can't change the inode.
2782 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2783 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2784 * 10. We can't remove a root or mountpoint.
2785 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 * nfs_async_unlink().
2787 */
Christian Braunerba73d982021-01-21 14:19:31 +01002788static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
2789 struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
David Howells63afdfc2015-05-06 15:59:00 +01002791 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int error;
2793
David Howellsb18825a2013-09-12 19:22:53 +01002794 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002796 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002799
2800 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01002801 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
2802 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002803 return -EOVERFLOW;
2804
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002805 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Christian Braunerba73d982021-01-21 14:19:31 +01002807 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (error)
2809 return error;
2810 if (IS_APPEND(dir))
2811 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002812
Christian Braunerba73d982021-01-21 14:19:31 +01002813 if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
2814 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
2815 HAS_UNMAPPED_ID(mnt_userns, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return -EPERM;
2817 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002818 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return -ENOTDIR;
2820 if (IS_ROOT(victim))
2821 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002822 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return -EISDIR;
2824 if (IS_DEADDIR(dir))
2825 return -ENOENT;
2826 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2827 return -EBUSY;
2828 return 0;
2829}
2830
2831/* Check whether we can create an object with dentry child in directory
2832 * dir.
2833 * 1. We can't do it if child already exists (open has special treatment for
2834 * this case, but since we are inlined it's OK)
2835 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002836 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2837 * 4. We should have write and exec permissions on dir
2838 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 */
Christian Braunerba73d982021-01-21 14:19:31 +01002840static inline int may_create(struct user_namespace *mnt_userns,
2841 struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Jeff Layton14e972b2013-05-08 10:25:58 -04002843 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (child->d_inode)
2845 return -EEXIST;
2846 if (IS_DEADDIR(dir))
2847 return -ENOENT;
Christian Brauner8e538912021-03-20 13:26:23 +01002848 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Eric W. Biederman036d5232016-07-01 12:52:06 -05002849 return -EOVERFLOW;
Christian Brauner8e538912021-03-20 13:26:23 +01002850
Christian Braunerba73d982021-01-21 14:19:31 +01002851 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854/*
2855 * p1 and p2 should be directories on the same fs.
2856 */
2857struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2858{
2859 struct dentry *p;
2860
2861 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002862 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return NULL;
2864 }
2865
Al Virofc640052016-04-10 01:33:30 -04002866 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002868 p = d_ancestor(p2, p1);
2869 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002870 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2871 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002872 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
2874
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002875 p = d_ancestor(p1, p2);
2876 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002877 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2878 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002879 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881
Al Viro59551022016-01-22 15:40:57 -05002882 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2883 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return NULL;
2885}
Al Viro4d359502014-03-14 12:20:17 -04002886EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888void unlock_rename(struct dentry *p1, struct dentry *p2)
2889{
Al Viro59551022016-01-22 15:40:57 -05002890 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002892 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002893 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895}
Al Viro4d359502014-03-14 12:20:17 -04002896EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Christian Brauner6521f892021-01-21 14:19:33 +01002898/**
2899 * vfs_create - create new file
2900 * @mnt_userns: user namespace of the mount the inode was found from
2901 * @dir: inode of @dentry
2902 * @dentry: pointer to dentry of the base directory
2903 * @mode: mode of the new file
2904 * @want_excl: whether the file must not yet exist
2905 *
2906 * Create a new file.
2907 *
2908 * If the inode has been found through an idmapped mount the user namespace of
2909 * the vfsmount must be passed through @mnt_userns. This function will then take
2910 * care to map the inode according to @mnt_userns before checking permissions.
2911 * On non-idmapped mounts or if permission checking is to be performed on the
2912 * raw inode simply passs init_user_ns.
2913 */
2914int vfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2915 struct dentry *dentry, umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Christian Brauner6521f892021-01-21 14:19:33 +01002917 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 if (error)
2919 return error;
2920
Al Viroacfa4382008-12-04 10:06:33 -05002921 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return -EACCES; /* shouldn't it be ENOSYS? */
2923 mode &= S_IALLUGO;
2924 mode |= S_IFREG;
2925 error = security_inode_create(dir, dentry, mode);
2926 if (error)
2927 return error;
Christian Brauner549c7292021-01-21 14:19:43 +01002928 error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002929 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002930 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return error;
2932}
Al Viro4d359502014-03-14 12:20:17 -04002933EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Al Viro8e6c8482017-12-01 17:12:45 -05002935int vfs_mkobj(struct dentry *dentry, umode_t mode,
2936 int (*f)(struct dentry *, umode_t, void *),
2937 void *arg)
2938{
2939 struct inode *dir = dentry->d_parent->d_inode;
Christian Braunerba73d982021-01-21 14:19:31 +01002940 int error = may_create(&init_user_ns, dir, dentry);
Al Viro8e6c8482017-12-01 17:12:45 -05002941 if (error)
2942 return error;
2943
2944 mode &= S_IALLUGO;
2945 mode |= S_IFREG;
2946 error = security_inode_create(dir, dentry, mode);
2947 if (error)
2948 return error;
2949 error = f(dentry, mode, arg);
2950 if (!error)
2951 fsnotify_create(dir, dentry);
2952 return error;
2953}
2954EXPORT_SYMBOL(vfs_mkobj);
2955
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002956bool may_open_dev(const struct path *path)
2957{
2958 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2959 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2960}
2961
Christian Braunerba73d982021-01-21 14:19:31 +01002962static int may_open(struct user_namespace *mnt_userns, const struct path *path,
2963 int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002965 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 struct inode *inode = dentry->d_inode;
2967 int error;
2968
2969 if (!inode)
2970 return -ENOENT;
2971
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002972 switch (inode->i_mode & S_IFMT) {
2973 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002975 case S_IFDIR:
Kees Cookfc4177b2020-08-14 17:30:14 -07002976 if (acc_mode & MAY_WRITE)
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002977 return -EISDIR;
Kees Cookfc4177b2020-08-14 17:30:14 -07002978 if (acc_mode & MAY_EXEC)
2979 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002980 break;
2981 case S_IFBLK:
2982 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002983 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return -EACCES;
Kees Cook633fb6a2020-08-11 18:36:26 -07002985 fallthrough;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002986 case S_IFIFO:
2987 case S_IFSOCK:
Kees Cook633fb6a2020-08-11 18:36:26 -07002988 if (acc_mode & MAY_EXEC)
2989 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002991 break;
Kees Cook0fd338b2020-08-11 18:36:30 -07002992 case S_IFREG:
2993 if ((acc_mode & MAY_EXEC) && path_noexec(path))
2994 return -EACCES;
2995 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002996 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002997
Christian Braunerba73d982021-01-21 14:19:31 +01002998 error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002999 if (error)
3000 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05003001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 /*
3003 * An append-only file must be opened in append mode for writing.
3004 */
3005 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05003006 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05003007 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05003009 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011
3012 /* O_NOATIME can only be set by the owner or superuser */
Christian Braunerba73d982021-01-21 14:19:31 +01003013 if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
Al Viro7715b522009-12-16 03:54:00 -05003014 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04003016 return 0;
Al Viro7715b522009-12-16 03:54:00 -05003017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Christian Brauner549c7292021-01-21 14:19:43 +01003019static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05003020{
Al Virof0bb5aa2016-11-20 20:27:12 -05003021 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05003022 struct inode *inode = path->dentry->d_inode;
3023 int error = get_write_access(inode);
3024 if (error)
3025 return error;
3026 /*
3027 * Refuse to truncate files with mandatory locks held on them.
3028 */
Jeff Laytond7a06982014-03-10 09:54:15 -04003029 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05003030 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09003031 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05003032 if (!error) {
Christian Brauner549c7292021-01-21 14:19:43 +01003033 error = do_truncate(mnt_userns, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05003034 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003035 filp);
Al Viro7715b522009-12-16 03:54:00 -05003036 }
3037 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003038 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Dave Hansend57999e2008-02-15 14:37:27 -08003041static inline int open_to_namei_flags(int flag)
3042{
Al Viro8a5e9292011-06-25 19:15:54 -04003043 if ((flag & O_ACCMODE) == 3)
3044 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003045 return flag;
3046}
3047
Christian Braunerba73d982021-01-21 14:19:31 +01003048static int may_o_create(struct user_namespace *mnt_userns,
3049 const struct path *dir, struct dentry *dentry,
3050 umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003051{
3052 int error = security_path_mknod(dir, dentry, mode, 0);
3053 if (error)
3054 return error;
3055
Christian Brauner8e538912021-03-20 13:26:23 +01003056 if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
Seth Forshee1328c722017-01-26 14:33:46 -06003057 return -EOVERFLOW;
3058
Christian Braunerba73d982021-01-21 14:19:31 +01003059 error = inode_permission(mnt_userns, dir->dentry->d_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01003060 MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003061 if (error)
3062 return error;
3063
3064 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3065}
3066
David Howells1acf0af2012-06-14 16:13:46 +01003067/*
3068 * Attempt to atomically look up, create and open a file from a negative
3069 * dentry.
3070 *
3071 * Returns 0 if successful. The file will have been created and attached to
3072 * @file by the filesystem calling finish_open().
3073 *
Al Viro00a07c12018-07-09 19:30:20 -04003074 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3075 * be set. The caller will need to perform the open themselves. @path will
3076 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003077 *
3078 * Returns an error code otherwise.
3079 */
Al Viro239eb982020-01-09 14:12:40 -05003080static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3081 struct file *file,
Al Viro239eb982020-01-09 14:12:40 -05003082 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003083{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003084 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003085 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003086 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003087
Miklos Szeredid18e9002012-06-05 15:10:17 +02003088 if (nd->flags & LOOKUP_DIRECTORY)
3089 open_flag |= O_DIRECTORY;
3090
Al Viro30d90492012-06-22 12:40:19 +04003091 file->f_path.dentry = DENTRY_NOT_SET;
3092 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003093 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003094 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003095 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003096 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003097 if (file->f_mode & FMODE_OPENED) {
Al Viro6fb968c2020-01-26 09:53:19 -05003098 if (unlikely(dentry != file->f_path.dentry)) {
3099 dput(dentry);
3100 dentry = dget(file->f_path.dentry);
3101 }
Al Viro64e1ac42018-07-09 19:17:52 -04003102 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003103 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003104 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003105 if (file->f_path.dentry) {
3106 dput(dentry);
3107 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003108 }
Al Viro239eb982020-01-09 14:12:40 -05003109 if (unlikely(d_is_negative(dentry)))
Al Viroa01e7182016-06-07 21:53:51 -04003110 error = -ENOENT;
Sage Weil62b2ce92012-08-15 13:30:12 -07003111 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003112 }
Al Viro239eb982020-01-09 14:12:40 -05003113 if (error) {
3114 dput(dentry);
3115 dentry = ERR_PTR(error);
3116 }
3117 return dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003118}
3119
Nick Piggin31e6b012011-01-07 17:49:52 +11003120/*
David Howells1acf0af2012-06-14 16:13:46 +01003121 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003122 *
Al Viro00a07c12018-07-09 19:30:20 -04003123 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003124 *
Al Viro00a07c12018-07-09 19:30:20 -04003125 * Returns 0 on success, that is, if
3126 * the file was successfully atomically created (if necessary) and opened, or
3127 * the file was not completely opened at this time, though lookups and
3128 * creations were performed.
3129 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3130 * In the latter case dentry returned in @path might be negative if O_CREAT
3131 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003132 *
Al Viro00a07c12018-07-09 19:30:20 -04003133 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003134 */
Al Viroda5ebf52020-01-09 14:25:14 -05003135static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3136 const struct open_flags *op,
3137 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003138{
Christian Brauner549c7292021-01-21 14:19:43 +01003139 struct user_namespace *mnt_userns;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003140 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003141 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003142 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003143 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003144 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003145 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003146 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003147
Al Viroce8644f2016-04-26 14:17:56 -04003148 if (unlikely(IS_DEADDIR(dir_inode)))
Al Viroda5ebf52020-01-09 14:25:14 -05003149 return ERR_PTR(-ENOENT);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003150
Al Viro73a09dd2018-06-08 13:22:02 -04003151 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003152 dentry = d_lookup(dir, &nd->last);
3153 for (;;) {
3154 if (!dentry) {
3155 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3156 if (IS_ERR(dentry))
Al Viroda5ebf52020-01-09 14:25:14 -05003157 return dentry;
Al Viro6fbd0712016-04-28 11:50:59 -04003158 }
3159 if (d_in_lookup(dentry))
3160 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003161
Al Viro6fbd0712016-04-28 11:50:59 -04003162 error = d_revalidate(dentry, nd->flags);
3163 if (likely(error > 0))
3164 break;
3165 if (error)
3166 goto out_dput;
3167 d_invalidate(dentry);
3168 dput(dentry);
3169 dentry = NULL;
3170 }
3171 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003172 /* Cached positive dentry: will open in f_op->open */
Al Viroda5ebf52020-01-09 14:25:14 -05003173 return dentry;
Al Viro6c51e512016-03-05 20:09:32 -05003174 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003175
Al Viro1643b432016-04-27 19:14:10 -04003176 /*
3177 * Checking write permission is tricky, bacuse we don't know if we are
3178 * going to actually need it: O_CREAT opens should work as long as the
3179 * file exists. But checking existence breaks atomicity. The trick is
3180 * to check access and if not granted clear O_CREAT from the flags.
3181 *
3182 * Another problem is returing the "right" error value (e.g. for an
3183 * O_EXCL open we want to return EEXIST not EROFS).
3184 */
Al Viro99a4a902020-03-12 14:07:27 -04003185 if (unlikely(!got_write))
3186 open_flag &= ~O_TRUNC;
Christian Brauner549c7292021-01-21 14:19:43 +01003187 mnt_userns = mnt_user_ns(nd->path.mnt);
Al Viro1643b432016-04-27 19:14:10 -04003188 if (open_flag & O_CREAT) {
Al Viro99a4a902020-03-12 14:07:27 -04003189 if (open_flag & O_EXCL)
3190 open_flag &= ~O_TRUNC;
Al Viro1643b432016-04-27 19:14:10 -04003191 if (!IS_POSIXACL(dir->d_inode))
3192 mode &= ~current_umask();
Al Viro99a4a902020-03-12 14:07:27 -04003193 if (likely(got_write))
Christian Brauner549c7292021-01-21 14:19:43 +01003194 create_error = may_o_create(mnt_userns, &nd->path,
Christian Braunerba73d982021-01-21 14:19:31 +01003195 dentry, mode);
Al Viro99a4a902020-03-12 14:07:27 -04003196 else
3197 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003198 }
Al Viro99a4a902020-03-12 14:07:27 -04003199 if (create_error)
3200 open_flag &= ~O_CREAT;
Al Viro1643b432016-04-27 19:14:10 -04003201 if (dir_inode->i_op->atomic_open) {
Al Virod489cf92020-03-11 08:07:53 -04003202 dentry = atomic_open(nd, dentry, file, open_flag, mode);
Al Viroda5ebf52020-01-09 14:25:14 -05003203 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3204 dentry = ERR_PTR(create_error);
3205 return dentry;
Al Viro1643b432016-04-27 19:14:10 -04003206 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003207
Al Viro6fbd0712016-04-28 11:50:59 -04003208 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003209 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3210 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003211 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003212 if (unlikely(res)) {
3213 if (IS_ERR(res)) {
3214 error = PTR_ERR(res);
3215 goto out_dput;
3216 }
3217 dput(dentry);
3218 dentry = res;
3219 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003220 }
3221
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003222 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003223 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003224 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003225 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003226 if (!dir_inode->i_op->create) {
3227 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003228 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003229 }
Christian Brauner549c7292021-01-21 14:19:43 +01003230
3231 error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry,
3232 mode, open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003233 if (error)
3234 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003235 }
Al Viro1643b432016-04-27 19:14:10 -04003236 if (unlikely(create_error) && !dentry->d_inode) {
3237 error = create_error;
3238 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003239 }
Al Viroda5ebf52020-01-09 14:25:14 -05003240 return dentry;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003241
3242out_dput:
3243 dput(dentry);
Al Viroda5ebf52020-01-09 14:25:14 -05003244 return ERR_PTR(error);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003245}
3246
Al Viroc981a482020-01-26 11:06:21 -05003247static const char *open_last_lookups(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003248 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003249{
Al Viroa1e28032009-12-24 02:12:06 -05003250 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003251 int open_flag = op->open_flag;
Al Viro64894cf2012-07-31 00:53:35 +04003252 bool got_write = false;
Al Viro254cf582015-05-05 09:40:46 -04003253 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003254 struct inode *inode;
Al Viroda5ebf52020-01-09 14:25:14 -05003255 struct dentry *dentry;
Al Virob0417d22020-01-14 13:34:20 -05003256 const char *res;
Al Virofb1cc552009-12-24 01:58:28 -05003257
Al Viroc3e380b2011-02-23 13:39:45 -05003258 nd->flags |= op->intent;
3259
Al Virobc77daa2013-06-06 09:12:33 -04003260 if (nd->last_type != LAST_NORM) {
Al Viro56676ec2020-03-10 21:54:54 -04003261 if (nd->depth)
3262 put_link(nd);
Al Viroff326a32020-03-10 10:19:24 -04003263 return handle_dots(nd, nd->last_type);
Al Viro1f36f772009-12-26 10:56:19 -05003264 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003265
Al Viroca344a892011-03-09 00:36:45 -05003266 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003267 if (nd->last.name[nd->last.len])
3268 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3269 /* we _can_ be in RCU mode here */
Al Viro20e34352020-01-09 14:58:31 -05003270 dentry = lookup_fast(nd, &inode, &seq);
3271 if (IS_ERR(dentry))
Al Viro1ccac622020-01-14 10:13:40 -05003272 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05003273 if (likely(dentry))
Miklos Szeredi71574862012-06-05 15:10:14 +02003274 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003275
Al Viro6583fe22016-03-05 18:14:03 -05003276 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003277 } else {
3278 /* create side of things */
Al Viro72287412020-03-10 10:09:26 -04003279 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003280 if (!try_to_unlazy(nd))
3281 return ERR_PTR(-ECHILD);
Al Viro72287412020-03-10 10:09:26 -04003282 }
Al Viroc9b07ea2019-07-14 13:22:27 -04003283 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003284 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003285 if (unlikely(nd->last.name[nd->last.len]))
Al Viro1ccac622020-01-14 10:13:40 -05003286 return ERR_PTR(-EISDIR);
Al Virofe2d35f2011-03-05 22:58:25 -05003287 }
3288
Al Viro9cf843e2016-04-28 19:35:16 -04003289 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003290 got_write = !mnt_want_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003291 /*
3292 * do _not_ fail yet - we might not need that or fail with
3293 * a different error; let lookup_open() decide; we'll be
3294 * dropping this one anyway.
3295 */
3296 }
Al Viro9cf843e2016-04-28 19:35:16 -04003297 if (open_flag & O_CREAT)
3298 inode_lock(dir->d_inode);
3299 else
3300 inode_lock_shared(dir->d_inode);
Al Viroda5ebf52020-01-09 14:25:14 -05003301 dentry = lookup_open(nd, file, op, got_write);
Al Virof7bb9592020-03-05 13:25:20 -05003302 if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3303 fsnotify_create(dir->d_inode, dentry);
Al Viro9cf843e2016-04-28 19:35:16 -04003304 if (open_flag & O_CREAT)
3305 inode_unlock(dir->d_inode);
3306 else
3307 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003308
Al Viroc981a482020-01-26 11:06:21 -05003309 if (got_write)
Al Viro59e96e62020-01-26 10:22:24 -05003310 mnt_drop_write(nd->path.mnt);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003311
Al Viro59e96e62020-01-26 10:22:24 -05003312 if (IS_ERR(dentry))
3313 return ERR_CAST(dentry);
3314
Al Viro973d4b72020-01-26 10:48:16 -05003315 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
Al Viroe73cabf2020-01-09 14:30:08 -05003316 dput(nd->path.dentry);
3317 nd->path.dentry = dentry;
Al Viroc981a482020-01-26 11:06:21 -05003318 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05003319 }
3320
Al Viro20e34352020-01-09 14:58:31 -05003321finish_lookup:
Al Viro56676ec2020-03-10 21:54:54 -04003322 if (nd->depth)
3323 put_link(nd);
Al Viro8c4efe22020-01-19 12:44:18 -05003324 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
Al Viroff326a32020-03-10 10:19:24 -04003325 if (unlikely(res))
Al Virob0417d22020-01-14 13:34:20 -05003326 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viroff326a32020-03-10 10:19:24 -04003327 return res;
Al Viroc981a482020-01-26 11:06:21 -05003328}
3329
3330/*
3331 * Handle the last step of open()
3332 */
Al Viroc5971b82020-03-05 11:41:29 -05003333static int do_open(struct nameidata *nd,
Al Viroc981a482020-01-26 11:06:21 -05003334 struct file *file, const struct open_flags *op)
3335{
Christian Brauner549c7292021-01-21 14:19:43 +01003336 struct user_namespace *mnt_userns;
Al Viroc981a482020-01-26 11:06:21 -05003337 int open_flag = op->open_flag;
3338 bool do_truncate;
3339 int acc_mode;
Al Viroc981a482020-01-26 11:06:21 -05003340 int error;
3341
Al Viroff326a32020-03-10 10:19:24 -04003342 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3343 error = complete_walk(nd);
3344 if (error)
3345 return error;
3346 }
Al Viro973d4b72020-01-26 10:48:16 -05003347 if (!(file->f_mode & FMODE_CREATED))
3348 audit_inode(nd->name, nd->path.dentry, 0);
Christian Brauner549c7292021-01-21 14:19:43 +01003349 mnt_userns = mnt_user_ns(nd->path.mnt);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003350 if (open_flag & O_CREAT) {
Al Virob94e0b32020-03-10 10:13:53 -04003351 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3352 return -EEXIST;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003353 if (d_is_dir(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003354 return -EISDIR;
Christian Brauner549c7292021-01-21 14:19:43 +01003355 error = may_create_in_sticky(mnt_userns, nd,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003356 d_backing_inode(nd->path.dentry));
3357 if (unlikely(error))
Al Viroc5971b82020-03-05 11:41:29 -05003358 return error;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003359 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003360 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003361 return -ENOTDIR;
Al Viro6c0d46c2011-03-09 00:59:59 -05003362
Al Viro8795e7d2020-01-26 10:38:17 -05003363 do_truncate = false;
3364 acc_mode = op->acc_mode;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003365 if (file->f_mode & FMODE_CREATED) {
3366 /* Don't check for write permission, don't truncate */
3367 open_flag &= ~O_TRUNC;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003368 acc_mode = 0;
Al Viro8795e7d2020-01-26 10:38:17 -05003369 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
Al Viro0f9d1a12011-03-09 00:13:14 -05003370 error = mnt_want_write(nd->path.mnt);
3371 if (error)
Al Viroc5971b82020-03-05 11:41:29 -05003372 return error;
Al Viro8795e7d2020-01-26 10:38:17 -05003373 do_truncate = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003374 }
Christian Brauner549c7292021-01-21 14:19:43 +01003375 error = may_open(mnt_userns, &nd->path, acc_mode, open_flag);
Al Viro8795e7d2020-01-26 10:38:17 -05003376 if (!error && !(file->f_mode & FMODE_OPENED))
Al Viro3ad56152020-01-26 10:06:13 -05003377 error = vfs_open(&nd->path, file);
Al Viro8795e7d2020-01-26 10:38:17 -05003378 if (!error)
3379 error = ima_file_check(file, op->acc_mode);
3380 if (!error && do_truncate)
Christian Brauner549c7292021-01-21 14:19:43 +01003381 error = handle_truncate(mnt_userns, file);
Al Viroc80567c2016-02-27 19:17:33 -05003382 if (unlikely(error > 0)) {
3383 WARN_ON(1);
3384 error = -EINVAL;
3385 }
Al Viro8795e7d2020-01-26 10:38:17 -05003386 if (do_truncate)
Al Viro0f9d1a12011-03-09 00:13:14 -05003387 mnt_drop_write(nd->path.mnt);
Al Viroc5971b82020-03-05 11:41:29 -05003388 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003389}
3390
Christian Brauner6521f892021-01-21 14:19:33 +01003391/**
3392 * vfs_tmpfile - create tmpfile
3393 * @mnt_userns: user namespace of the mount the inode was found from
3394 * @dentry: pointer to dentry of the base directory
3395 * @mode: mode of the new tmpfile
Randy Dunlap2111c3c2021-02-15 20:29:28 -08003396 * @open_flag: flags
Christian Brauner6521f892021-01-21 14:19:33 +01003397 *
3398 * Create a temporary file.
3399 *
3400 * If the inode has been found through an idmapped mount the user namespace of
3401 * the vfsmount must be passed through @mnt_userns. This function will then take
3402 * care to map the inode according to @mnt_userns before checking permissions.
3403 * On non-idmapped mounts or if permission checking is to be performed on the
3404 * raw inode simply passs init_user_ns.
3405 */
3406struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns,
3407 struct dentry *dentry, umode_t mode, int open_flag)
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003408{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003409 struct dentry *child = NULL;
3410 struct inode *dir = dentry->d_inode;
3411 struct inode *inode;
3412 int error;
3413
3414 /* we want directory to be writable */
Christian Brauner6521f892021-01-21 14:19:33 +01003415 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003416 if (error)
3417 goto out_err;
3418 error = -EOPNOTSUPP;
3419 if (!dir->i_op->tmpfile)
3420 goto out_err;
3421 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003422 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003423 if (unlikely(!child))
3424 goto out_err;
Christian Brauner549c7292021-01-21 14:19:43 +01003425 error = dir->i_op->tmpfile(mnt_userns, dir, child, mode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003426 if (error)
3427 goto out_err;
3428 error = -ENOENT;
3429 inode = child->d_inode;
3430 if (unlikely(!inode))
3431 goto out_err;
3432 if (!(open_flag & O_EXCL)) {
3433 spin_lock(&inode->i_lock);
3434 inode->i_state |= I_LINKABLE;
3435 spin_unlock(&inode->i_lock);
3436 }
Christian Braunera2d23292021-01-21 14:19:45 +01003437 ima_post_create_tmpfile(mnt_userns, inode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003438 return child;
3439
3440out_err:
3441 dput(child);
3442 return ERR_PTR(error);
3443}
3444EXPORT_SYMBOL(vfs_tmpfile);
3445
Al Viroc8a53ee2015-05-12 18:43:07 -04003446static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003447 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003448 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003449{
Christian Brauner6521f892021-01-21 14:19:33 +01003450 struct user_namespace *mnt_userns;
Al Viro625b6d12015-05-12 16:36:12 -04003451 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003452 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003453 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003454 if (unlikely(error))
3455 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003456 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003457 if (unlikely(error))
3458 goto out;
Christian Brauner6521f892021-01-21 14:19:33 +01003459 mnt_userns = mnt_user_ns(path.mnt);
3460 child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003461 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003462 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003463 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003464 dput(path.dentry);
3465 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003466 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003467 /* Don't check for other permissions, the inode was just created */
Christian Brauner549c7292021-01-21 14:19:43 +01003468 error = may_open(mnt_userns, &path, 0, op->open_flag);
Al Viro1e8f44f2020-03-11 17:22:19 -04003469 if (!error)
3470 error = vfs_open(&path, file);
Al Viro60545d02013-06-07 01:20:27 -04003471out2:
Al Viro625b6d12015-05-12 16:36:12 -04003472 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003473out:
Al Viro625b6d12015-05-12 16:36:12 -04003474 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003475 return error;
3476}
3477
Al Viro6ac08702016-04-26 00:02:50 -04003478static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3479{
3480 struct path path;
3481 int error = path_lookupat(nd, flags, &path);
3482 if (!error) {
3483 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003484 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003485 path_put(&path);
3486 }
3487 return error;
3488}
3489
Al Viroc8a53ee2015-05-12 18:43:07 -04003490static struct file *path_openat(struct nameidata *nd,
3491 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
Al Viro30d90492012-06-22 12:40:19 +04003493 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003494 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003495
Al Viroea73ea72018-07-11 15:00:04 -04003496 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003497 if (IS_ERR(file))
3498 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003499
Al Virobb458c62013-07-13 13:26:37 +04003500 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003501 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003502 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003503 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003504 } else {
3505 const char *s = path_init(nd, flags);
3506 while (!(error = link_path_walk(s, nd)) &&
Al Viroc5971b82020-03-05 11:41:29 -05003507 (s = open_last_lookups(nd, file, op)) != NULL)
Al Viro1ccac622020-01-14 10:13:40 -05003508 ;
Al Viroc5971b82020-03-05 11:41:29 -05003509 if (!error)
3510 error = do_open(nd, file, op);
Al Viro5f336e72018-07-09 16:38:06 -04003511 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003512 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003513 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003514 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003515 return file;
3516 WARN_ON(1);
3517 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003518 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003519 fput(file);
3520 if (error == -EOPENSTALE) {
3521 if (flags & LOOKUP_RCU)
3522 error = -ECHILD;
3523 else
3524 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003525 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003526 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Jeff Layton669abf42012-10-10 16:43:10 -04003529struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003530 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003531{
Al Viro9883d182015-05-13 07:28:08 -04003532 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003533 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003534 struct file *filp;
3535
Al Viro06422962021-04-01 22:28:03 -04003536 set_nameidata(&nd, dfd, pathname, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04003537 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003538 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003539 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003540 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003541 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003542 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003543 return filp;
3544}
3545
Al Viroffb37ca2021-04-01 19:00:57 -04003546struct file *do_file_open_root(const struct path *root,
Al Virof9652e12013-06-11 08:23:01 +04003547 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003548{
Al Viro9883d182015-05-13 07:28:08 -04003549 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003550 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003551 struct filename *filename;
Al Virobcba1e72021-04-01 22:03:41 -04003552 int flags = op->lookup_flags;
Al Viro73d049a2011-03-11 12:08:24 -05003553
Al Viroffb37ca2021-04-01 19:00:57 -04003554 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003555 return ERR_PTR(-ELOOP);
3556
Paul Moore51689102015-01-22 00:00:03 -05003557 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303558 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003559 return ERR_CAST(filename);
3560
Al Viro06422962021-04-01 22:28:03 -04003561 set_nameidata(&nd, -1, filename, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04003562 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003563 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003564 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003565 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003566 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003567 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003568 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003569 return file;
3570}
3571
Al Virofa14a0b2015-01-22 02:16:49 -05003572static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003573 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003575 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003576 struct qstr last;
3577 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003578 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003579 int error;
3580 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3581
3582 /*
3583 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3584 * other flags passed in are ignored!
3585 */
3586 lookup_flags &= LOOKUP_REVAL;
3587
Al Viro5c31b6c2015-05-12 17:32:54 -04003588 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3589 if (IS_ERR(name))
3590 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003592 /*
3593 * Yucky last component or no last component at all?
3594 * (foo/., foo/.., /////)
3595 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003596 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003597 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003598
Jan Karac30dabf2012-06-12 16:20:30 +02003599 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003600 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003601 /*
3602 * Do the final lookup.
3603 */
Al Viro391172c2015-05-09 11:19:16 -04003604 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003605 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003606 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003608 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003609
Al Viroa8104a92012-07-20 02:25:00 +04003610 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003611 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003612 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003613
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003614 /*
3615 * Special case - lookup gave negative, but... we had foo/bar/
3616 * From the vfs_mknod() POV we just have a negative dentry -
3617 * all is fine. Let's be bastards - you had / on the end, you've
3618 * been asking for (non-existent) directory. -ENOENT for you.
3619 */
Al Viro391172c2015-05-09 11:19:16 -04003620 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003621 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003622 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003623 }
Jan Karac30dabf2012-06-12 16:20:30 +02003624 if (unlikely(err2)) {
3625 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003626 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003627 }
Al Viro181c37b2015-05-12 17:21:25 -04003628 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630fail:
Al Viroa8104a92012-07-20 02:25:00 +04003631 dput(dentry);
3632 dentry = ERR_PTR(error);
3633unlock:
Al Viro59551022016-01-22 15:40:57 -05003634 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003635 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003636 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003637out:
Al Viro391172c2015-05-09 11:19:16 -04003638 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003639 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return dentry;
3641}
Al Virofa14a0b2015-01-22 02:16:49 -05003642
3643struct dentry *kern_path_create(int dfd, const char *pathname,
3644 struct path *path, unsigned int lookup_flags)
3645{
Al Viro181c37b2015-05-12 17:21:25 -04003646 return filename_create(dfd, getname_kernel(pathname),
3647 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003648}
Al Virodae6ad82011-06-26 11:50:15 -04003649EXPORT_SYMBOL(kern_path_create);
3650
Al Viro921a1652012-07-20 01:15:31 +04003651void done_path_create(struct path *path, struct dentry *dentry)
3652{
3653 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003654 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003655 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003656 path_put(path);
3657}
3658EXPORT_SYMBOL(done_path_create);
3659
Al Viro520ae682015-05-13 07:00:28 -04003660inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003661 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003662{
Al Viro181c37b2015-05-12 17:21:25 -04003663 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003664}
3665EXPORT_SYMBOL(user_path_create);
3666
Christian Brauner6521f892021-01-21 14:19:33 +01003667/**
3668 * vfs_mknod - create device node or file
3669 * @mnt_userns: user namespace of the mount the inode was found from
3670 * @dir: inode of @dentry
3671 * @dentry: pointer to dentry of the base directory
3672 * @mode: mode of the new device node or file
3673 * @dev: device number of device to create
3674 *
3675 * Create a device node or file.
3676 *
3677 * If the inode has been found through an idmapped mount the user namespace of
3678 * the vfsmount must be passed through @mnt_userns. This function will then take
3679 * care to map the inode according to @mnt_userns before checking permissions.
3680 * On non-idmapped mounts or if permission checking is to be performed on the
3681 * raw inode simply passs init_user_ns.
3682 */
3683int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
3684 struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003686 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
Christian Brauner6521f892021-01-21 14:19:33 +01003687 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 if (error)
3690 return error;
3691
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003692 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3693 !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return -EPERM;
3695
Al Viroacfa4382008-12-04 10:06:33 -05003696 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return -EPERM;
3698
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003699 error = devcgroup_inode_mknod(mode, dev);
3700 if (error)
3701 return error;
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 error = security_inode_mknod(dir, dentry, mode, dev);
3704 if (error)
3705 return error;
3706
Christian Brauner549c7292021-01-21 14:19:43 +01003707 error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003708 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003709 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return error;
3711}
Al Viro4d359502014-03-14 12:20:17 -04003712EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Al Virof69aac02011-07-26 04:31:40 -04003714static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003715{
3716 switch (mode & S_IFMT) {
3717 case S_IFREG:
3718 case S_IFCHR:
3719 case S_IFBLK:
3720 case S_IFIFO:
3721 case S_IFSOCK:
3722 case 0: /* zero mode translates to S_IFREG */
3723 return 0;
3724 case S_IFDIR:
3725 return -EPERM;
3726 default:
3727 return -EINVAL;
3728 }
3729}
3730
Christoph Hellwig5fee64f2020-07-22 11:41:20 +02003731static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003732 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Christian Brauner6521f892021-01-21 14:19:33 +01003734 struct user_namespace *mnt_userns;
Al Viro2ad94ae2008-07-21 09:32:51 -04003735 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003736 struct path path;
3737 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003738 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Al Viro8e4bfca2012-07-20 01:17:26 +04003740 error = may_mknod(mode);
3741 if (error)
3742 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003743retry:
3744 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003745 if (IS_ERR(dentry))
3746 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003747
Al Virodae6ad82011-06-26 11:50:15 -04003748 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003749 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003750 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003751 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003752 goto out;
Christian Brauner6521f892021-01-21 14:19:33 +01003753
3754 mnt_userns = mnt_user_ns(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003755 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 case 0: case S_IFREG:
Christian Brauner6521f892021-01-21 14:19:33 +01003757 error = vfs_create(mnt_userns, path.dentry->d_inode,
3758 dentry, mode, true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003759 if (!error)
Christian Braunera2d23292021-01-21 14:19:45 +01003760 ima_post_path_mknod(mnt_userns, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 break;
3762 case S_IFCHR: case S_IFBLK:
Christian Brauner6521f892021-01-21 14:19:33 +01003763 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3764 dentry, mode, new_decode_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 break;
3766 case S_IFIFO: case S_IFSOCK:
Christian Brauner6521f892021-01-21 14:19:33 +01003767 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3768 dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 }
Al Viroa8104a92012-07-20 02:25:00 +04003771out:
Al Viro921a1652012-07-20 01:15:31 +04003772 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003773 if (retry_estale(error, lookup_flags)) {
3774 lookup_flags |= LOOKUP_REVAL;
3775 goto retry;
3776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 return error;
3778}
3779
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003780SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3781 unsigned int, dev)
3782{
3783 return do_mknodat(dfd, filename, mode, dev);
3784}
3785
Al Viro8208a222011-07-25 17:32:17 -04003786SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003787{
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003788 return do_mknodat(AT_FDCWD, filename, mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003789}
3790
Christian Brauner6521f892021-01-21 14:19:33 +01003791/**
3792 * vfs_mkdir - create directory
3793 * @mnt_userns: user namespace of the mount the inode was found from
3794 * @dir: inode of @dentry
3795 * @dentry: pointer to dentry of the base directory
3796 * @mode: mode of the new directory
3797 *
3798 * Create a directory.
3799 *
3800 * If the inode has been found through an idmapped mount the user namespace of
3801 * the vfsmount must be passed through @mnt_userns. This function will then take
3802 * care to map the inode according to @mnt_userns before checking permissions.
3803 * On non-idmapped mounts or if permission checking is to be performed on the
3804 * raw inode simply passs init_user_ns.
3805 */
3806int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
3807 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
Christian Brauner6521f892021-01-21 14:19:33 +01003809 int error = may_create(mnt_userns, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003810 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
3812 if (error)
3813 return error;
3814
Al Viroacfa4382008-12-04 10:06:33 -05003815 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return -EPERM;
3817
3818 mode &= (S_IRWXUGO|S_ISVTX);
3819 error = security_inode_mkdir(dir, dentry, mode);
3820 if (error)
3821 return error;
3822
Al Viro8de52772012-02-06 12:45:27 -05003823 if (max_links && dir->i_nlink >= max_links)
3824 return -EMLINK;
3825
Christian Brauner549c7292021-01-21 14:19:43 +01003826 error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003827 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003828 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return error;
3830}
Al Viro4d359502014-03-14 12:20:17 -04003831EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Christoph Hellwig83ff98c2020-07-22 11:14:59 +02003833static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Dave Hansen6902d922006-09-30 23:29:01 -07003835 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003836 struct path path;
3837 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003838 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003840retry:
3841 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003842 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003843 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003844
Al Virodae6ad82011-06-26 11:50:15 -04003845 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003846 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003847 error = security_path_mkdir(&path, dentry, mode);
Christian Brauner6521f892021-01-21 14:19:33 +01003848 if (!error) {
3849 struct user_namespace *mnt_userns;
3850 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01003851 error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry,
3852 mode);
Christian Brauner6521f892021-01-21 14:19:33 +01003853 }
Al Viro921a1652012-07-20 01:15:31 +04003854 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003855 if (retry_estale(error, lookup_flags)) {
3856 lookup_flags |= LOOKUP_REVAL;
3857 goto retry;
3858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 return error;
3860}
3861
Dominik Brodowski0101db72018-03-11 11:34:49 +01003862SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3863{
3864 return do_mkdirat(dfd, pathname, mode);
3865}
3866
Al Viroa218d0f2011-11-21 14:59:34 -05003867SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003868{
Dominik Brodowski0101db72018-03-11 11:34:49 +01003869 return do_mkdirat(AT_FDCWD, pathname, mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003870}
3871
Christian Brauner6521f892021-01-21 14:19:33 +01003872/**
3873 * vfs_rmdir - remove directory
3874 * @mnt_userns: user namespace of the mount the inode was found from
3875 * @dir: inode of @dentry
3876 * @dentry: pointer to dentry of the base directory
3877 *
3878 * Remove a directory.
3879 *
3880 * If the inode has been found through an idmapped mount the user namespace of
3881 * the vfsmount must be passed through @mnt_userns. This function will then take
3882 * care to map the inode according to @mnt_userns before checking permissions.
3883 * On non-idmapped mounts or if permission checking is to be performed on the
3884 * raw inode simply passs init_user_ns.
3885 */
3886int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
3887 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
Christian Brauner6521f892021-01-21 14:19:33 +01003889 int error = may_delete(mnt_userns, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
3891 if (error)
3892 return error;
3893
Al Viroacfa4382008-12-04 10:06:33 -05003894 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 return -EPERM;
3896
Al Viro1d2ef592011-09-14 18:55:41 +01003897 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003898 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Sage Weil912dbc12011-05-24 13:06:11 -07003900 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003901 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003902 goto out;
3903
3904 error = security_inode_rmdir(dir, dentry);
3905 if (error)
3906 goto out;
3907
3908 error = dir->i_op->rmdir(dir, dentry);
3909 if (error)
3910 goto out;
3911
Al Viro87677122018-05-27 16:23:51 -04003912 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003913 dentry->d_inode->i_flags |= S_DEAD;
3914 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003915 detach_mounts(dentry);
Amir Goldstein116b9732019-05-26 17:34:02 +03003916 fsnotify_rmdir(dir, dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003917
3918out:
Al Viro59551022016-01-22 15:40:57 -05003919 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003920 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003921 if (!error)
3922 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return error;
3924}
Al Viro4d359502014-03-14 12:20:17 -04003925EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02003927long do_rmdir(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Christian Brauner6521f892021-01-21 14:19:33 +01003929 struct user_namespace *mnt_userns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003932 struct path path;
3933 struct qstr last;
3934 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003935 unsigned int lookup_flags = 0;
3936retry:
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02003937 name = filename_parentat(dfd, name, lookup_flags,
Al Viroc1d4dd22016-06-05 16:38:18 -04003938 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003939 if (IS_ERR(name))
3940 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Al Virof5beed72015-04-30 16:09:11 -04003942 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003943 case LAST_DOTDOT:
3944 error = -ENOTEMPTY;
3945 goto exit1;
3946 case LAST_DOT:
3947 error = -EINVAL;
3948 goto exit1;
3949 case LAST_ROOT:
3950 error = -EBUSY;
3951 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003953
Al Virof5beed72015-04-30 16:09:11 -04003954 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003955 if (error)
3956 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003957
Al Viro59551022016-01-22 15:40:57 -05003958 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003959 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003961 if (IS_ERR(dentry))
3962 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003963 if (!dentry->d_inode) {
3964 error = -ENOENT;
3965 goto exit3;
3966 }
Al Virof5beed72015-04-30 16:09:11 -04003967 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003968 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003969 goto exit3;
Christian Brauner6521f892021-01-21 14:19:33 +01003970 mnt_userns = mnt_user_ns(path.mnt);
3971 error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003972exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003973 dput(dentry);
3974exit2:
Al Viro59551022016-01-22 15:40:57 -05003975 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003976 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977exit1:
Al Virof5beed72015-04-30 16:09:11 -04003978 path_put(&path);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003979 if (retry_estale(error, lookup_flags)) {
3980 lookup_flags |= LOOKUP_REVAL;
3981 goto retry;
3982 }
Al Viro24fb33d2020-08-12 05:15:18 +01003983 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return error;
3985}
3986
Heiko Carstens3cdad422009-01-14 14:14:22 +01003987SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003988{
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02003989 return do_rmdir(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003990}
3991
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003992/**
3993 * vfs_unlink - unlink a filesystem object
Christian Brauner6521f892021-01-21 14:19:33 +01003994 * @mnt_userns: user namespace of the mount the inode was found from
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003995 * @dir: parent directory
3996 * @dentry: victim
3997 * @delegated_inode: returns victim inode, if the inode is delegated.
3998 *
3999 * The caller must hold dir->i_mutex.
4000 *
4001 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4002 * return a reference to the inode in delegated_inode. The caller
4003 * should then break the delegation on that inode and retry. Because
4004 * breaking a delegation may take a long time, the caller should drop
4005 * dir->i_mutex before doing so.
4006 *
4007 * Alternatively, a caller may pass NULL for delegated_inode. This may
4008 * be appropriate for callers that expect the underlying filesystem not
4009 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004010 *
4011 * If the inode has been found through an idmapped mount the user namespace of
4012 * the vfsmount must be passed through @mnt_userns. This function will then take
4013 * care to map the inode according to @mnt_userns before checking permissions.
4014 * On non-idmapped mounts or if permission checking is to be performed on the
4015 * raw inode simply passs init_user_ns.
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004016 */
Christian Brauner6521f892021-01-21 14:19:33 +01004017int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
4018 struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004020 struct inode *target = dentry->d_inode;
Christian Brauner6521f892021-01-21 14:19:33 +01004021 int error = may_delete(mnt_userns, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023 if (error)
4024 return error;
4025
Al Viroacfa4382008-12-04 10:06:33 -05004026 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return -EPERM;
4028
Al Viro59551022016-01-22 15:40:57 -05004029 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004030 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 error = -EBUSY;
4032 else {
4033 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004034 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004035 error = try_break_deleg(target, delegated_inode);
4036 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004039 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004040 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004041 detach_mounts(dentry);
Amir Goldstein116b9732019-05-26 17:34:02 +03004042 fsnotify_unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004043 }
Al Virobec10522010-03-03 14:12:08 -05004044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004046out:
Al Viro59551022016-01-22 15:40:57 -05004047 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4050 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004051 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
Robert Love0eeca282005-07-12 17:06:03 -04004054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return error;
4056}
Al Viro4d359502014-03-14 12:20:17 -04004057EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
4059/*
4060 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004061 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 * writeout happening, and we don't want to prevent access to the directory
4063 * while waiting on the I/O.
4064 */
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004065long do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
Al Viro2ad94ae2008-07-21 09:32:51 -04004067 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004069 struct path path;
4070 struct qstr last;
4071 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004073 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004074 unsigned int lookup_flags = 0;
4075retry:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004076 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004077 if (IS_ERR(name))
4078 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04004079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004081 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004083
Al Virof5beed72015-04-30 16:09:11 -04004084 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004085 if (error)
4086 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004087retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004088 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004089 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 error = PTR_ERR(dentry);
4091 if (!IS_ERR(dentry)) {
Christian Brauner6521f892021-01-21 14:19:33 +01004092 struct user_namespace *mnt_userns;
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004095 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004096 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004098 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004099 goto slashes;
4100 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004101 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004102 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004103 goto exit2;
Christian Brauner6521f892021-01-21 14:19:33 +01004104 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01004105 error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
4106 &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02004107exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 dput(dentry);
4109 }
Al Viro59551022016-01-22 15:40:57 -05004110 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (inode)
4112 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004113 inode = NULL;
4114 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004115 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004116 if (!error)
4117 goto retry_deleg;
4118 }
Al Virof5beed72015-04-30 16:09:11 -04004119 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120exit1:
Al Virof5beed72015-04-30 16:09:11 -04004121 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004122 if (retry_estale(error, lookup_flags)) {
4123 lookup_flags |= LOOKUP_REVAL;
4124 inode = NULL;
4125 goto retry;
4126 }
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004127 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return error;
4129
4130slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004131 if (d_is_negative(dentry))
4132 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004133 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004134 error = -EISDIR;
4135 else
4136 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 goto exit2;
4138}
4139
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004140SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004141{
4142 if ((flag & ~AT_REMOVEDIR) != 0)
4143 return -EINVAL;
4144
4145 if (flag & AT_REMOVEDIR)
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004146 return do_rmdir(dfd, getname(pathname));
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004147 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004148}
4149
Heiko Carstens3480b252009-01-14 14:14:16 +01004150SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004151{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004152 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004153}
4154
Christian Brauner6521f892021-01-21 14:19:33 +01004155/**
4156 * vfs_symlink - create symlink
4157 * @mnt_userns: user namespace of the mount the inode was found from
4158 * @dir: inode of @dentry
4159 * @dentry: pointer to dentry of the base directory
4160 * @oldname: name of the file to link to
4161 *
4162 * Create a symlink.
4163 *
4164 * If the inode has been found through an idmapped mount the user namespace of
4165 * the vfsmount must be passed through @mnt_userns. This function will then take
4166 * care to map the inode according to @mnt_userns before checking permissions.
4167 * On non-idmapped mounts or if permission checking is to be performed on the
4168 * raw inode simply passs init_user_ns.
4169 */
4170int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
4171 struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
Christian Brauner6521f892021-01-21 14:19:33 +01004173 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 if (error)
4176 return error;
4177
Al Viroacfa4382008-12-04 10:06:33 -05004178 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return -EPERM;
4180
4181 error = security_inode_symlink(dir, dentry, oldname);
4182 if (error)
4183 return error;
4184
Christian Brauner549c7292021-01-21 14:19:43 +01004185 error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004186 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004187 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return error;
4189}
Al Viro4d359502014-03-14 12:20:17 -04004190EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Christoph Hellwigcd3acb62020-07-22 11:14:36 +02004192static long do_symlinkat(const char __user *oldname, int newdfd,
Dominik Brodowskib724e842018-03-11 11:34:49 +01004193 const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Al Viro2ad94ae2008-07-21 09:32:51 -04004195 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004196 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004197 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004198 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004199 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004202 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004204retry:
4205 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004206 error = PTR_ERR(dentry);
4207 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004208 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004209
Jeff Layton91a27b22012-10-10 15:25:28 -04004210 error = security_path_symlink(&path, dentry, from->name);
Christian Brauner6521f892021-01-21 14:19:33 +01004211 if (!error) {
4212 struct user_namespace *mnt_userns;
4213
4214 mnt_userns = mnt_user_ns(path.mnt);
4215 error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry,
4216 from->name);
4217 }
Al Viro921a1652012-07-20 01:15:31 +04004218 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004219 if (retry_estale(error, lookup_flags)) {
4220 lookup_flags |= LOOKUP_REVAL;
4221 goto retry;
4222 }
Dave Hansen6902d922006-09-30 23:29:01 -07004223out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 putname(from);
4225 return error;
4226}
4227
Dominik Brodowskib724e842018-03-11 11:34:49 +01004228SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4229 int, newdfd, const char __user *, newname)
4230{
4231 return do_symlinkat(oldname, newdfd, newname);
4232}
4233
Heiko Carstens3480b252009-01-14 14:14:16 +01004234SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004235{
Dominik Brodowskib724e842018-03-11 11:34:49 +01004236 return do_symlinkat(oldname, AT_FDCWD, newname);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004237}
4238
J. Bruce Fields146a8592011-09-20 17:14:31 -04004239/**
4240 * vfs_link - create a new link
4241 * @old_dentry: object to be linked
Christian Brauner6521f892021-01-21 14:19:33 +01004242 * @mnt_userns: the user namespace of the mount
J. Bruce Fields146a8592011-09-20 17:14:31 -04004243 * @dir: new parent
4244 * @new_dentry: where to create the new link
4245 * @delegated_inode: returns inode needing a delegation break
4246 *
4247 * The caller must hold dir->i_mutex
4248 *
4249 * If vfs_link discovers a delegation on the to-be-linked file in need
4250 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4251 * inode in delegated_inode. The caller should then break the delegation
4252 * and retry. Because breaking a delegation may take a long time, the
4253 * caller should drop the i_mutex before doing so.
4254 *
4255 * Alternatively, a caller may pass NULL for delegated_inode. This may
4256 * be appropriate for callers that expect the underlying filesystem not
4257 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004258 *
4259 * If the inode has been found through an idmapped mount the user namespace of
4260 * the vfsmount must be passed through @mnt_userns. This function will then take
4261 * care to map the inode according to @mnt_userns before checking permissions.
4262 * On non-idmapped mounts or if permission checking is to be performed on the
4263 * raw inode simply passs init_user_ns.
J. Bruce Fields146a8592011-09-20 17:14:31 -04004264 */
Christian Brauner6521f892021-01-21 14:19:33 +01004265int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns,
4266 struct inode *dir, struct dentry *new_dentry,
4267 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
4269 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004270 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 int error;
4272
4273 if (!inode)
4274 return -ENOENT;
4275
Christian Brauner6521f892021-01-21 14:19:33 +01004276 error = may_create(mnt_userns, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 if (error)
4278 return error;
4279
4280 if (dir->i_sb != inode->i_sb)
4281 return -EXDEV;
4282
4283 /*
4284 * A link to an append-only or immutable file cannot be created.
4285 */
4286 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4287 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004288 /*
4289 * Updating the link count will likely cause i_uid and i_gid to
4290 * be writen back improperly if their true value is unknown to
4291 * the vfs.
4292 */
Christian Brauner6521f892021-01-21 14:19:33 +01004293 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004294 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004295 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004297 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 return -EPERM;
4299
4300 error = security_inode_link(old_dentry, dir, new_dentry);
4301 if (error)
4302 return error;
4303
Al Viro59551022016-01-22 15:40:57 -05004304 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304305 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004306 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304307 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004308 else if (max_links && inode->i_nlink >= max_links)
4309 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004310 else {
4311 error = try_break_deleg(inode, delegated_inode);
4312 if (!error)
4313 error = dir->i_op->link(old_dentry, dir, new_dentry);
4314 }
Al Virof4e0c302013-06-11 08:34:36 +04004315
4316 if (!error && (inode->i_state & I_LINKABLE)) {
4317 spin_lock(&inode->i_lock);
4318 inode->i_state &= ~I_LINKABLE;
4319 spin_unlock(&inode->i_lock);
4320 }
Al Viro59551022016-01-22 15:40:57 -05004321 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004322 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004323 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 return error;
4325}
Al Viro4d359502014-03-14 12:20:17 -04004326EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328/*
4329 * Hardlinks are often used in delicate situations. We avoid
4330 * security-related surprises by not following symlinks on the
4331 * newname. --KAB
4332 *
4333 * We don't follow them on the oldname either to be compatible
4334 * with linux 2.0, and to avoid hard-linking to directories
4335 * and other special files. --ADM
4336 */
Christoph Hellwig812931d2020-07-22 11:14:19 +02004337static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004338 const char __user *newname, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Christian Brauner6521f892021-01-21 14:19:33 +01004340 struct user_namespace *mnt_userns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004342 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004343 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304344 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304347 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004348 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304349 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004350 * To use null names we require CAP_DAC_READ_SEARCH
4351 * This ensures that not everyone will be able to create
4352 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304353 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004354 if (flags & AT_EMPTY_PATH) {
4355 if (!capable(CAP_DAC_READ_SEARCH))
4356 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304357 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004358 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004359
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304360 if (flags & AT_SYMLINK_FOLLOW)
4361 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004362retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304363 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004365 return error;
4366
Jeff Layton442e31c2012-12-20 16:15:38 -05004367 new_dentry = user_path_create(newdfd, newname, &new_path,
4368 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004370 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004371 goto out;
4372
4373 error = -EXDEV;
4374 if (old_path.mnt != new_path.mnt)
4375 goto out_dput;
Christian Brauner549c7292021-01-21 14:19:43 +01004376 mnt_userns = mnt_user_ns(new_path.mnt);
4377 error = may_linkat(mnt_userns, &old_path);
Kees Cook800179c2012-07-25 17:29:07 -07004378 if (unlikely(error))
4379 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004380 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004381 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004382 goto out_dput;
Christian Brauner6521f892021-01-21 14:19:33 +01004383 error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
4384 new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004385out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004386 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004387 if (delegated_inode) {
4388 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004389 if (!error) {
4390 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004391 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004392 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004393 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004394 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004395 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004396 how |= LOOKUP_REVAL;
4397 goto retry;
4398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399out:
Al Viro2d8f3032008-07-22 09:59:21 -04004400 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
4402 return error;
4403}
4404
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004405SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4406 int, newdfd, const char __user *, newname, int, flags)
4407{
4408 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4409}
4410
Heiko Carstens3480b252009-01-14 14:14:16 +01004411SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004412{
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004413 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004414}
4415
Miklos Szeredibc270272014-04-01 17:08:42 +02004416/**
4417 * vfs_rename - rename a filesystem object
Randy Dunlap2111c3c2021-02-15 20:29:28 -08004418 * @rd: pointer to &struct renamedata info
Miklos Szeredibc270272014-04-01 17:08:42 +02004419 *
4420 * The caller must hold multiple mutexes--see lock_rename()).
4421 *
4422 * If vfs_rename discovers a delegation in need of breaking at either
4423 * the source or destination, it will return -EWOULDBLOCK and return a
4424 * reference to the inode in delegated_inode. The caller should then
4425 * break the delegation and retry. Because breaking a delegation may
4426 * take a long time, the caller should drop all locks before doing
4427 * so.
4428 *
4429 * Alternatively, a caller may pass NULL for delegated_inode. This may
4430 * be appropriate for callers that expect the underlying filesystem not
4431 * to be NFS exported.
4432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 * The worst of all namespace operations - renaming directory. "Perverted"
4434 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4435 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004436 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004437 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 * b) race potential - two innocent renames can create a loop together.
4439 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004440 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004442 * c) we have to lock _four_ objects - parents and victim (if it exists),
4443 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004444 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 * whether the target exists). Solution: try to be smart with locking
4446 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004447 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 * move will be locked. Thus we can rank directories by the tree
4449 * (ancestors first) and rank all non-directories after them.
4450 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004451 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 * HOWEVER, it relies on the assumption that any object with ->lookup()
4453 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4454 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004455 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004456 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004458 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 * locking].
4460 */
Christian Brauner9fe61452021-01-21 14:19:32 +01004461int vfs_rename(struct renamedata *rd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462{
4463 int error;
Christian Brauner9fe61452021-01-21 14:19:32 +01004464 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
4465 struct dentry *old_dentry = rd->old_dentry;
4466 struct dentry *new_dentry = rd->new_dentry;
4467 struct inode **delegated_inode = rd->delegated_inode;
4468 unsigned int flags = rd->flags;
Miklos Szeredibc270272014-04-01 17:08:42 +02004469 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004470 struct inode *source = old_dentry->d_inode;
4471 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004472 bool new_is_dir = false;
4473 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004474 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004476 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004477 return 0;
4478
Christian Brauner6521f892021-01-21 14:19:33 +01004479 error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 if (error)
4481 return error;
4482
Miklos Szeredida1ce062014-04-01 17:08:43 +02004483 if (!target) {
Christian Brauner6521f892021-01-21 14:19:33 +01004484 error = may_create(rd->new_mnt_userns, new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004485 } else {
4486 new_is_dir = d_is_dir(new_dentry);
4487
4488 if (!(flags & RENAME_EXCHANGE))
Christian Brauner6521f892021-01-21 14:19:33 +01004489 error = may_delete(rd->new_mnt_userns, new_dir,
4490 new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004491 else
Christian Brauner6521f892021-01-21 14:19:33 +01004492 error = may_delete(rd->new_mnt_userns, new_dir,
4493 new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 if (error)
4496 return error;
4497
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004498 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 return -EPERM;
4500
Miklos Szeredibc270272014-04-01 17:08:42 +02004501 /*
4502 * If we are going to change the parent - check write permissions,
4503 * we'll need to flip '..'.
4504 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004505 if (new_dir != old_dir) {
4506 if (is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004507 error = inode_permission(rd->old_mnt_userns, source,
Christian Brauner47291ba2021-01-21 14:19:24 +01004508 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004509 if (error)
4510 return error;
4511 }
4512 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004513 error = inode_permission(rd->new_mnt_userns, target,
Christian Brauner47291ba2021-01-21 14:19:24 +01004514 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004515 if (error)
4516 return error;
4517 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004518 }
Robert Love0eeca282005-07-12 17:06:03 -04004519
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004520 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4521 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004522 if (error)
4523 return error;
4524
Al Viro49d31c22017-07-07 14:51:19 -04004525 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004526 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004527 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004528 lock_two_nondirectories(source, target);
4529 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004530 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004531
4532 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004533 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004534 goto out;
4535
Miklos Szeredida1ce062014-04-01 17:08:43 +02004536 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004537 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004538 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004539 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004540 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4541 old_dir->i_nlink >= max_links)
4542 goto out;
4543 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004544 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004545 error = try_break_deleg(source, delegated_inode);
4546 if (error)
4547 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004548 }
4549 if (target && !new_is_dir) {
4550 error = try_break_deleg(target, delegated_inode);
4551 if (error)
4552 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004553 }
Christian Brauner549c7292021-01-21 14:19:43 +01004554 error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry,
4555 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004556 if (error)
4557 goto out;
4558
Miklos Szeredida1ce062014-04-01 17:08:43 +02004559 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004560 if (is_dir) {
4561 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004562 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004563 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004564 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004565 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004566 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004567 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4568 if (!(flags & RENAME_EXCHANGE))
4569 d_move(old_dentry, new_dentry);
4570 else
4571 d_exchange(old_dentry, new_dentry);
4572 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004573out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004574 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004575 unlock_two_nondirectories(source, target);
4576 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004577 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004578 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004579 if (!error) {
Al Virof4ec3a32019-04-26 13:21:24 -04004580 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004581 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4582 if (flags & RENAME_EXCHANGE) {
Al Virof4ec3a32019-04-26 13:21:24 -04004583 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004584 new_is_dir, NULL, new_dentry);
4585 }
4586 }
Al Viro49d31c22017-07-07 14:51:19 -04004587 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 return error;
4590}
Al Viro4d359502014-03-14 12:20:17 -04004591EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Jens Axboee8866632020-09-26 17:20:17 -06004593int do_renameat2(int olddfd, struct filename *from, int newdfd,
4594 struct filename *to, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Christian Brauner9fe61452021-01-21 14:19:32 +01004596 struct renamedata rd;
Al Viro2ad94ae2008-07-21 09:32:51 -04004597 struct dentry *old_dentry, *new_dentry;
4598 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004599 struct path old_path, new_path;
4600 struct qstr old_last, new_last;
4601 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004602 struct inode *delegated_inode = NULL;
Al Virof5beed72015-04-30 16:09:11 -04004603 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004604 bool should_retry = false;
Jens Axboee8866632020-09-26 17:20:17 -06004605 int error = -EINVAL;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004606
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004607 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Jens Axboee8866632020-09-26 17:20:17 -06004608 goto put_both;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004609
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004610 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4611 (flags & RENAME_EXCHANGE))
Jens Axboee8866632020-09-26 17:20:17 -06004612 goto put_both;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004613
Al Virof5beed72015-04-30 16:09:11 -04004614 if (flags & RENAME_EXCHANGE)
4615 target_flags = 0;
4616
Jeff Laytonc6a94282012-12-11 12:10:10 -05004617retry:
Jens Axboee8866632020-09-26 17:20:17 -06004618 from = filename_parentat(olddfd, from, lookup_flags, &old_path,
4619 &old_last, &old_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004620 if (IS_ERR(from)) {
4621 error = PTR_ERR(from);
Jens Axboee8866632020-09-26 17:20:17 -06004622 goto put_new;
Jeff Layton91a27b22012-10-10 15:25:28 -04004623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Jens Axboee8866632020-09-26 17:20:17 -06004625 to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4626 &new_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004627 if (IS_ERR(to)) {
4628 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
4632 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004633 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 goto exit2;
4635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004637 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 goto exit2;
4639
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004640 if (flags & RENAME_NOREPLACE)
4641 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004642 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 goto exit2;
4644
Al Virof5beed72015-04-30 16:09:11 -04004645 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004646 if (error)
4647 goto exit2;
4648
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004649retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004650 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Al Virof5beed72015-04-30 16:09:11 -04004652 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 error = PTR_ERR(old_dentry);
4654 if (IS_ERR(old_dentry))
4655 goto exit3;
4656 /* source must exist */
4657 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004658 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004660 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 error = PTR_ERR(new_dentry);
4662 if (IS_ERR(new_dentry))
4663 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004664 error = -EEXIST;
4665 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4666 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004667 if (flags & RENAME_EXCHANGE) {
4668 error = -ENOENT;
4669 if (d_is_negative(new_dentry))
4670 goto exit5;
4671
4672 if (!d_is_dir(new_dentry)) {
4673 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004674 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004675 goto exit5;
4676 }
4677 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004678 /* unless the source is a directory trailing slashes give -ENOTDIR */
4679 if (!d_is_dir(old_dentry)) {
4680 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004681 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004682 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004683 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004684 goto exit5;
4685 }
4686 /* source should not be ancestor of target */
4687 error = -EINVAL;
4688 if (old_dentry == trap)
4689 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004691 if (!(flags & RENAME_EXCHANGE))
4692 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 if (new_dentry == trap)
4694 goto exit5;
4695
Al Virof5beed72015-04-30 16:09:11 -04004696 error = security_path_rename(&old_path, old_dentry,
4697 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004698 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004699 goto exit5;
Christian Brauner9fe61452021-01-21 14:19:32 +01004700
4701 rd.old_dir = old_path.dentry->d_inode;
4702 rd.old_dentry = old_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004703 rd.old_mnt_userns = mnt_user_ns(old_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004704 rd.new_dir = new_path.dentry->d_inode;
4705 rd.new_dentry = new_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004706 rd.new_mnt_userns = mnt_user_ns(new_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004707 rd.delegated_inode = &delegated_inode;
4708 rd.flags = flags;
4709 error = vfs_rename(&rd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710exit5:
4711 dput(new_dentry);
4712exit4:
4713 dput(old_dentry);
4714exit3:
Al Virof5beed72015-04-30 16:09:11 -04004715 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004716 if (delegated_inode) {
4717 error = break_deleg_wait(&delegated_inode);
4718 if (!error)
4719 goto retry_deleg;
4720 }
Al Virof5beed72015-04-30 16:09:11 -04004721 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004723 if (retry_estale(error, lookup_flags))
4724 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004725 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726exit1:
Al Virof5beed72015-04-30 16:09:11 -04004727 path_put(&old_path);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004728 if (should_retry) {
4729 should_retry = false;
4730 lookup_flags |= LOOKUP_REVAL;
4731 goto retry;
4732 }
Jens Axboee8866632020-09-26 17:20:17 -06004733put_both:
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004734 putname(from);
Jens Axboee8866632020-09-26 17:20:17 -06004735put_new:
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004736 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return error;
4738}
4739
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004740SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4741 int, newdfd, const char __user *, newname, unsigned int, flags)
4742{
Jens Axboee8866632020-09-26 17:20:17 -06004743 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4744 flags);
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004745}
4746
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004747SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4748 int, newdfd, const char __user *, newname)
4749{
Jens Axboee8866632020-09-26 17:20:17 -06004750 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4751 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004752}
4753
Heiko Carstensa26eab22009-01-14 14:14:17 +01004754SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004755{
Jens Axboee8866632020-09-26 17:20:17 -06004756 return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4757 getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004758}
4759
Al Viro5d826c82014-03-14 13:42:45 -04004760int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
Al Viro5d826c82014-03-14 13:42:45 -04004762 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 if (IS_ERR(link))
4764 goto out;
4765
4766 len = strlen(link);
4767 if (len > (unsigned) buflen)
4768 len = buflen;
4769 if (copy_to_user(buffer, link, len))
4770 len = -EFAULT;
4771out:
4772 return len;
4773}
4774
Miklos Szeredid60874c2016-10-04 14:40:45 +02004775/**
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004776 * vfs_readlink - copy symlink body into userspace buffer
4777 * @dentry: dentry on which to get symbolic link
4778 * @buffer: user memory pointer
4779 * @buflen: size of buffer
4780 *
4781 * Does not touch atime. That's up to the caller if necessary
4782 *
4783 * Does not call security hook.
4784 */
4785int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4786{
4787 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04004788 DEFINE_DELAYED_CALL(done);
4789 const char *link;
4790 int res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004791
Miklos Szeredi76fca902016-12-09 16:45:04 +01004792 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4793 if (unlikely(inode->i_op->readlink))
4794 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004795
Miklos Szeredi76fca902016-12-09 16:45:04 +01004796 if (!d_is_symlink(dentry))
4797 return -EINVAL;
4798
4799 spin_lock(&inode->i_lock);
4800 inode->i_opflags |= IOP_DEFAULT_READLINK;
4801 spin_unlock(&inode->i_lock);
4802 }
4803
Eric Biggers4c4f7c12019-04-10 13:21:14 -07004804 link = READ_ONCE(inode->i_link);
Al Virof2df5da2018-07-19 17:35:51 -04004805 if (!link) {
4806 link = inode->i_op->get_link(dentry, inode, &done);
4807 if (IS_ERR(link))
4808 return PTR_ERR(link);
4809 }
4810 res = readlink_copy(buffer, buflen, link);
4811 do_delayed_call(&done);
4812 return res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004813}
4814EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Miklos Szeredid60874c2016-10-04 14:40:45 +02004816/**
4817 * vfs_get_link - get symlink body
4818 * @dentry: dentry on which to get symbolic link
4819 * @done: caller needs to free returned data with this
4820 *
4821 * Calls security hook and i_op->get_link() on the supplied inode.
4822 *
4823 * It does not touch atime. That's up to the caller if necessary.
4824 *
4825 * Does not work on "special" symlinks like /proc/$$/fd/N
4826 */
4827const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4828{
4829 const char *res = ERR_PTR(-EINVAL);
4830 struct inode *inode = d_inode(dentry);
4831
4832 if (d_is_symlink(dentry)) {
4833 res = ERR_PTR(security_inode_readlink(dentry));
4834 if (!res)
4835 res = inode->i_op->get_link(dentry, inode, done);
4836 }
4837 return res;
4838}
4839EXPORT_SYMBOL(vfs_get_link);
4840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004842const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004843 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004845 char *kaddr;
4846 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004847 struct address_space *mapping = inode->i_mapping;
4848
Al Virod3883d42015-11-17 10:41:04 -05004849 if (!dentry) {
4850 page = find_get_page(mapping, 0);
4851 if (!page)
4852 return ERR_PTR(-ECHILD);
4853 if (!PageUptodate(page)) {
4854 put_page(page);
4855 return ERR_PTR(-ECHILD);
4856 }
4857 } else {
4858 page = read_mapping_page(mapping, 0, NULL);
4859 if (IS_ERR(page))
4860 return (char*)page;
4861 }
Al Virofceef392015-12-29 15:58:39 -05004862 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004863 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4864 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004865 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004866 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867}
4868
Al Viro6b255392015-11-17 10:20:54 -05004869EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Al Virofceef392015-12-29 15:58:39 -05004871void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Al Virofceef392015-12-29 15:58:39 -05004873 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
Al Viro4d359502014-03-14 12:20:17 -04004875EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Al Viroaa80dea2015-11-16 18:26:34 -05004877int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4878{
Al Virofceef392015-12-29 15:58:39 -05004879 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004880 int res = readlink_copy(buffer, buflen,
4881 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004882 &done));
4883 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004884 return res;
4885}
4886EXPORT_SYMBOL(page_readlink);
4887
Nick Piggin54566b22009-01-04 12:00:53 -08004888/*
4889 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4890 */
4891int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892{
4893 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004894 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004895 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004896 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07004897 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08004898 if (nofs)
4899 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
NeilBrown7e53cac2006-03-25 03:07:57 -08004901retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004902 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004903 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004905 goto fail;
4906
Al Viro21fc61c2015-11-17 01:07:57 -05004907 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004908
4909 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4910 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 if (err < 0)
4912 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004913 if (err < len-1)
4914 goto retry;
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 mark_inode_dirty(inode);
4917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918fail:
4919 return err;
4920}
Al Viro4d359502014-03-14 12:20:17 -04004921EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004923int page_symlink(struct inode *inode, const char *symname, int len)
4924{
4925 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004926 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004927}
Al Viro4d359502014-03-14 12:20:17 -04004928EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004929
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004930const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05004931 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933EXPORT_SYMBOL(page_symlink_inode_operations);