blob: 1f28d3f463c3b0418492f823809977e3d96aa7eb [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>
Matthew Wilcox (Oracle)2d878172022-02-22 09:43:12 -050025#include <linux/sched/mm.h>
Robert Love0eeca282005-07-12 17:06:03 -040026#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/personality.h>
28#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050029#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/syscalls.h>
31#include <linux/mount.h>
32#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080033#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070034#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080035#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070036#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040037#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070038#include <linux/posix_acl.h>
Linus Torvalds99d263d2014-09-13 11:30:10 -070039#include <linux/hash.h>
George Spelvin2a18da7a2016-05-23 07:43:58 -040040#include <linux/bitops.h>
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -050041#include <linux/init_task.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Eric Parise81e3f42009-12-04 15:47:36 -050044#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050045#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* [Feb-1997 T. Schoebel-Theuer]
48 * Fundamental changes in the pathname lookup mechanisms (namei)
49 * were necessary because of omirr. The reason is that omirr needs
50 * to know the _real_ pathname, not the user-supplied one, in case
51 * of symlinks (and also when transname replacements occur).
52 *
53 * The new code replaces the old recursive symlink resolution with
54 * an iterative one (in case of non-nested symlink chains). It does
55 * this with calls to <fs>_follow_link().
56 * As a side effect, dir_namei(), _namei() and follow_link() are now
57 * replaced with a single function lookup_dentry() that can handle all
58 * the special cases of the former code.
59 *
60 * With the new dcache, the pathname is stored at each inode, at least as
61 * long as the refcount of the inode is positive. As a side effect, the
62 * size of the dcache depends on the inode cache and thus is dynamic.
63 *
64 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
65 * resolution to correspond with current state of the code.
66 *
67 * Note that the symlink resolution is not *completely* iterative.
68 * There is still a significant amount of tail- and mid- recursion in
69 * the algorithm. Also, note that <fs>_readlink() is not used in
70 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
71 * may return different results than <fs>_follow_link(). Many virtual
72 * filesystems (including /proc) exhibit this behavior.
73 */
74
75/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
76 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
77 * and the name already exists in form of a symlink, try to create the new
78 * name indicated by the symlink. The old code always complained that the
79 * name already exists, due to not following the symlink even if its target
80 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030081 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
83 * I don't know which semantics is the right one, since I have no access
84 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
85 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
86 * "old" one. Personally, I think the new semantics is much more logical.
87 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
88 * file does succeed in both HP-UX and SunOs, but not in Solaris
89 * and in the old Linux semantics.
90 */
91
92/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
93 * semantics. See the comments in "open_namei" and "do_link" below.
94 *
95 * [10-Sep-98 Alan Modra] Another symlink change.
96 */
97
98/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
99 * inside the path - always follow.
100 * in the last component in creation/removal/renaming - never follow.
101 * if LOOKUP_FOLLOW passed - follow.
102 * if the pathname has trailing slashes - follow.
103 * otherwise - don't follow.
104 * (applied in that order).
105 *
106 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
107 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
108 * During the 2.4 we need to fix the userland stuff depending on it -
109 * hopefully we will be able to get rid of that wart in 2.5. So far only
110 * XEmacs seems to be relying on it...
111 */
112/*
113 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800114 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * any extra contention...
116 */
117
118/* In order to reduce some races, while at the same time doing additional
119 * checking and hopefully speeding things up, we copy filenames to the
120 * kernel data space before using them..
121 *
122 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
123 * PATH_MAX includes the nul terminator --RR.
124 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400125
Al Virofd2f7cb2015-02-22 20:07:13 -0500126#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400127
David Drysdale51f39a12014-12-12 16:57:29 -0800128struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400129getname_flags(const char __user *filename, int flags, int *empty)
130{
Al Viro94b5d262015-02-22 19:38:03 -0500131 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400132 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500133 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jeff Layton7ac86262012-10-10 15:25:28 -0400135 result = audit_reusename(filename);
136 if (result)
137 return result;
138
Jeff Layton7950e382012-10-10 16:43:13 -0400139 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700140 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500141 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeff Layton7950e382012-10-10 16:43:13 -0400143 /*
144 * First, try to embed the struct filename inside the names_cache
145 * allocation
146 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500147 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400148 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400149
Al Viro94b5d262015-02-22 19:38:03 -0500150 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400151 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500152 __putname(result);
153 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400154 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700155
Jeff Layton7950e382012-10-10 16:43:13 -0400156 /*
157 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
158 * separate struct filename so we can dedicate the entire
159 * names_cache allocation for the pathname, and re-do the copy from
160 * userland.
161 */
Al Viro94b5d262015-02-22 19:38:03 -0500162 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500163 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400164 kname = (char *)result;
165
Al Virofd2f7cb2015-02-22 20:07:13 -0500166 /*
167 * size is chosen that way we to guarantee that
168 * result->iname[0] is within the same object and that
169 * kname can't be equal to result->iname, no matter what.
170 */
171 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500172 if (unlikely(!result)) {
173 __putname(kname);
174 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400175 }
176 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500177 len = strncpy_from_user(kname, filename, PATH_MAX);
178 if (unlikely(len < 0)) {
179 __putname(kname);
180 kfree(result);
181 return ERR_PTR(len);
182 }
183 if (unlikely(len == PATH_MAX)) {
184 __putname(kname);
185 kfree(result);
186 return ERR_PTR(-ENAMETOOLONG);
187 }
Jeff Layton7950e382012-10-10 16:43:13 -0400188 }
189
Al Viro94b5d262015-02-22 19:38:03 -0500190 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700191 /* The empty path is special. */
192 if (unlikely(!len)) {
193 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500194 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500195 if (!(flags & LOOKUP_EMPTY)) {
196 putname(result);
197 return ERR_PTR(-ENOENT);
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700200
Jeff Layton7950e382012-10-10 16:43:13 -0400201 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800202 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400203 audit_getname(result);
204 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Jeff Layton91a27b22012-10-10 15:25:28 -0400207struct filename *
Dmitry Kadashev8228e2c2021-07-08 13:34:42 +0700208getname_uflags(const char __user *filename, int uflags)
209{
210 int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
211
212 return getname_flags(filename, flags, NULL);
213}
214
215struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400216getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400217{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700218 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400219}
220
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800221struct filename *
222getname_kernel(const char * filename)
223{
224 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500225 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800226
227 result = __getname();
228 if (unlikely(!result))
229 return ERR_PTR(-ENOMEM);
230
Paul Moore08518542015-01-21 23:59:56 -0500231 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500232 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500233 } else if (len <= PATH_MAX) {
Al Viro30ce4d12018-04-08 11:57:10 -0400234 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500235 struct filename *tmp;
236
Al Viro30ce4d12018-04-08 11:57:10 -0400237 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500238 if (unlikely(!tmp)) {
239 __putname(result);
240 return ERR_PTR(-ENOMEM);
241 }
242 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500243 result = tmp;
244 } else {
245 __putname(result);
246 return ERR_PTR(-ENAMETOOLONG);
247 }
248 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800249 result->uptr = NULL;
250 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500251 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500252 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800253
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800254 return result;
255}
256
Jeff Layton91a27b22012-10-10 15:25:28 -0400257void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Al Viroea47ab12021-09-07 16:14:05 -0400259 if (IS_ERR(name))
Dmitry Kadashev91ef6582021-07-08 13:34:37 +0700260 return;
261
Paul Moore55422d02015-01-22 00:00:23 -0500262 BUG_ON(name->refcnt <= 0);
263
264 if (--name->refcnt > 0)
265 return;
266
Al Virofd2f7cb2015-02-22 20:07:13 -0500267 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500268 __putname(name->name);
269 kfree(name);
270 } else
271 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Christian Brauner47291ba2021-01-21 14:19:24 +0100274/**
275 * check_acl - perform ACL permission checking
276 * @mnt_userns: user namespace of the mount the inode was found from
277 * @inode: inode to check permissions on
278 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
279 *
280 * This function performs the ACL permission checking. Since this function
281 * retrieve POSIX acls it needs to know whether it is called from a blocking or
282 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
283 *
284 * If the inode has been found through an idmapped mount the user namespace of
285 * the vfsmount must be passed through @mnt_userns. This function will then take
286 * care to map the inode according to @mnt_userns before checking permissions.
287 * On non-idmapped mounts or if permission checking is to be performed on the
288 * raw inode simply passs init_user_ns.
289 */
290static int check_acl(struct user_namespace *mnt_userns,
291 struct inode *inode, int mask)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700292{
Linus Torvalds84635d62011-07-25 22:47:03 -0700293#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700294 struct posix_acl *acl;
295
Linus Torvaldse77819e2011-07-22 19:30:19 -0700296 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400297 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
298 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700299 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400300 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100301 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400302 return -ECHILD;
Christian Brauner47291ba2021-01-21 14:19:24 +0100303 return posix_acl_permission(mnt_userns, inode, acl, mask);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700304 }
305
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800306 acl = get_acl(inode, ACL_TYPE_ACCESS);
307 if (IS_ERR(acl))
308 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700309 if (acl) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100310 int error = posix_acl_permission(mnt_userns, inode, acl, mask);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700311 posix_acl_release(acl);
312 return error;
313 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700314#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700315
316 return -EAGAIN;
317}
318
Christian Brauner47291ba2021-01-21 14:19:24 +0100319/**
320 * acl_permission_check - perform basic UNIX permission checking
321 * @mnt_userns: user namespace of the mount the inode was found from
322 * @inode: inode to check permissions on
323 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700324 *
Christian Brauner47291ba2021-01-21 14:19:24 +0100325 * This function performs the basic UNIX permission checking. Since this
326 * function may retrieve POSIX acls it needs to know whether it is called from a
327 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
328 *
329 * If the inode has been found through an idmapped mount the user namespace of
330 * the vfsmount must be passed through @mnt_userns. This function will then take
331 * care to map the inode according to @mnt_userns before checking permissions.
332 * On non-idmapped mounts or if permission checking is to be performed on the
333 * raw inode simply passs init_user_ns.
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700334 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100335static int acl_permission_check(struct user_namespace *mnt_userns,
336 struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700337{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700338 unsigned int mode = inode->i_mode;
Christian Brauner47291ba2021-01-21 14:19:24 +0100339 kuid_t i_uid;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700340
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700341 /* Are we the owner? If so, ACL's don't matter */
Christian Brauner47291ba2021-01-21 14:19:24 +0100342 i_uid = i_uid_into_mnt(mnt_userns, inode);
343 if (likely(uid_eq(current_fsuid(), i_uid))) {
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700344 mask &= 7;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700345 mode >>= 6;
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700346 return (mask & ~mode) ? -EACCES : 0;
347 }
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700348
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700349 /* Do we have ACL's? */
350 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100351 int error = check_acl(mnt_userns, inode, mask);
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700352 if (error != -EAGAIN)
353 return error;
354 }
355
356 /* Only RWX matters for group/other mode bits */
357 mask &= 7;
358
359 /*
360 * Are the group permissions different from
361 * the other permissions in the bits we care
362 * about? Need to check group ownership if so.
363 */
364 if (mask & (mode ^ (mode >> 3))) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100365 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
366 if (in_group_p(kgid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700367 mode >>= 3;
368 }
369
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700370 /* Bits in 'mode' clear that we require? */
371 return (mask & ~mode) ? -EACCES : 0;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100375 * generic_permission - check for access rights on a Posix-like filesystem
Christian Brauner47291ba2021-01-21 14:19:24 +0100376 * @mnt_userns: user namespace of the mount the inode was found from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * @inode: inode to check access rights for
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700378 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
379 * %MAY_NOT_BLOCK ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
381 * Used to check for read/write/execute permissions on a file.
382 * We use "fsuid" for this, letting us set arbitrary permissions
383 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100384 * are used for other things.
385 *
386 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
387 * request cannot be satisfied (eg. requires blocking or too much complexity).
388 * It would then be called again in ref-walk mode.
Christian Brauner47291ba2021-01-21 14:19:24 +0100389 *
390 * If the inode has been found through an idmapped mount the user namespace of
391 * the vfsmount must be passed through @mnt_userns. This function will then take
392 * care to map the inode according to @mnt_userns before checking permissions.
393 * On non-idmapped mounts or if permission checking is to be performed on the
394 * raw inode simply passs init_user_ns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100396int generic_permission(struct user_namespace *mnt_userns, struct inode *inode,
397 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700399 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530402 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100404 ret = acl_permission_check(mnt_userns, inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700405 if (ret != -EACCES)
406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Al Virod594e7e2011-06-20 19:55:42 -0400408 if (S_ISDIR(inode->i_mode)) {
409 /* DACs are overridable for directories */
Al Virod594e7e2011-06-20 19:55:42 -0400410 if (!(mask & MAY_WRITE))
Christian Brauner47291ba2021-01-21 14:19:24 +0100411 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700412 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400413 return 0;
Christian Brauner47291ba2021-01-21 14:19:24 +0100414 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100415 CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500417 return -EACCES;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /*
421 * Searching includes executable on directories, else just read.
422 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600423 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400424 if (mask == MAY_READ)
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_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500428 /*
429 * Read/write DACs are always overridable.
430 * Executable DACs are overridable when there is
431 * at least one exec bit set.
432 */
433 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Christian Brauner47291ba2021-01-21 14:19:24 +0100434 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100435 CAP_DAC_OVERRIDE))
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 return -EACCES;
439}
Al Viro4d359502014-03-14 12:20:17 -0400440EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Christian Brauner47291ba2021-01-21 14:19:24 +0100442/**
443 * do_inode_permission - UNIX permission checking
444 * @mnt_userns: user namespace of the mount the inode was found from
445 * @inode: inode to check permissions on
446 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
447 *
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700448 * We _really_ want to just do "generic_permission()" without
449 * even looking at the inode->i_op values. So we keep a cache
450 * flag in inode->i_opflags, that says "this has not special
451 * permission function, use the fast case".
452 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100453static inline int do_inode_permission(struct user_namespace *mnt_userns,
454 struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700455{
456 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
457 if (likely(inode->i_op->permission))
Christian Brauner549c7292021-01-21 14:19:43 +0100458 return inode->i_op->permission(mnt_userns, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700459
460 /* This gets set once for the inode lifetime */
461 spin_lock(&inode->i_lock);
462 inode->i_opflags |= IOP_FASTPERM;
463 spin_unlock(&inode->i_lock);
464 }
Christian Brauner47291ba2021-01-21 14:19:24 +0100465 return generic_permission(mnt_userns, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700466}
467
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200468/**
David Howells0bdaea92012-06-25 12:55:46 +0100469 * sb_permission - Check superblock-level permissions
470 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700471 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100472 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
473 *
474 * Separate out file-system wide checks from inode-specific permission checks.
475 */
476static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
477{
478 if (unlikely(mask & MAY_WRITE)) {
479 umode_t mode = inode->i_mode;
480
481 /* Nobody gets write access to a read-only fs. */
David Howellsbc98a422017-07-17 08:45:34 +0100482 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
David Howells0bdaea92012-06-25 12:55:46 +0100483 return -EROFS;
484 }
485 return 0;
486}
487
488/**
489 * inode_permission - Check for access rights to a given inode
Christian Brauner47291ba2021-01-21 14:19:24 +0100490 * @mnt_userns: User namespace of the mount the inode was found from
491 * @inode: Inode to check permission on
492 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
David Howells0bdaea92012-06-25 12:55:46 +0100493 *
494 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
495 * this, letting us set arbitrary permissions for filesystem access without
496 * changing the "normal" UIDs which are used for other things.
497 *
498 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
499 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100500int inode_permission(struct user_namespace *mnt_userns,
501 struct inode *inode, int mask)
David Howells0bdaea92012-06-25 12:55:46 +0100502{
503 int retval;
504
505 retval = sb_permission(inode->i_sb, inode, mask);
506 if (retval)
507 return retval;
Eric Biggers4bfd0542018-01-16 21:44:24 -0800508
509 if (unlikely(mask & MAY_WRITE)) {
510 /*
511 * Nobody gets write access to an immutable file.
512 */
513 if (IS_IMMUTABLE(inode))
514 return -EPERM;
515
516 /*
517 * Updating mtime will likely cause i_uid and i_gid to be
518 * written back improperly if their true value is unknown
519 * to the vfs.
520 */
Christian Braunerba73d982021-01-21 14:19:31 +0100521 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric Biggers4bfd0542018-01-16 21:44:24 -0800522 return -EACCES;
523 }
524
Christian Brauner47291ba2021-01-21 14:19:24 +0100525 retval = do_inode_permission(mnt_userns, inode, mask);
Eric Biggers4bfd0542018-01-16 21:44:24 -0800526 if (retval)
527 return retval;
528
529 retval = devcgroup_inode_permission(inode, mask);
530 if (retval)
531 return retval;
532
533 return security_inode_permission(inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100534}
Al Viro4d359502014-03-14 12:20:17 -0400535EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100536
537/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800538 * path_get - get a reference to a path
539 * @path: path to get the reference to
540 *
541 * Given a path increment the reference count to the dentry and the vfsmount.
542 */
Al Virodcf787f2013-03-01 23:51:07 -0500543void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800544{
545 mntget(path->mnt);
546 dget(path->dentry);
547}
548EXPORT_SYMBOL(path_get);
549
550/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800551 * path_put - put a reference to a path
552 * @path: path to put the reference to
553 *
554 * Given a path decrement the reference count to the dentry and the vfsmount.
555 */
Al Virodcf787f2013-03-01 23:51:07 -0500556void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Jan Blunck1d957f92008-02-14 19:34:35 -0800558 dput(path->dentry);
559 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
Jan Blunck1d957f92008-02-14 19:34:35 -0800561EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Al Viro894bc8c2015-05-02 07:16:16 -0400563#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400564struct nameidata {
565 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400566 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400567 struct path root;
568 struct inode *inode; /* path.dentry.d_inode */
Al Virobcba1e72021-04-01 22:03:41 -0400569 unsigned int flags, state;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +1100570 unsigned seq, m_seq, r_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400571 int last_type;
572 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100573 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400574 struct saved {
575 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500576 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400577 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400578 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400579 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400580 struct filename *name;
581 struct nameidata *saved;
582 unsigned root_seq;
583 int dfd;
Al Viro0f705952020-03-05 11:34:48 -0500584 kuid_t dir_uid;
585 umode_t dir_mode;
Kees Cook3859a272016-10-28 01:22:25 -0700586} __randomize_layout;
Al Viro1f55a6e2014-11-01 19:30:41 -0400587
Al Virobcba1e72021-04-01 22:03:41 -0400588#define ND_ROOT_PRESET 1
589#define ND_ROOT_GRABBED 2
590#define ND_JUMPED 4
591
Al Viro06422962021-04-01 22:28:03 -0400592static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400593{
NeilBrown756daf22015-03-23 13:37:38 +1100594 struct nameidata *old = current->nameidata;
595 p->stack = p->internal;
Al Viro7962c7d2021-04-03 16:49:44 -0400596 p->depth = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -0400597 p->dfd = dfd;
598 p->name = name;
Al Viro7d01ef752021-04-06 12:33:07 -0400599 p->path.mnt = NULL;
600 p->path.dentry = NULL;
NeilBrown756daf22015-03-23 13:37:38 +1100601 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400602 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100603 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400604}
605
Al Viro06422962021-04-01 22:28:03 -0400606static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name,
607 const struct path *root)
608{
609 __set_nameidata(p, dfd, name);
610 p->state = 0;
611 if (unlikely(root)) {
612 p->state = ND_ROOT_PRESET;
613 p->root = *root;
614 }
615}
616
Al Viro9883d182015-05-13 07:28:08 -0400617static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400618{
Al Viro9883d182015-05-13 07:28:08 -0400619 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100620
621 current->nameidata = old;
622 if (old)
623 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500624 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100625 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400626}
627
Al Viro60ef60c2020-03-03 11:43:55 -0500628static bool nd_alloc_stack(struct nameidata *nd)
Al Viro894bc8c2015-05-02 07:16:16 -0400629{
Al Virobc40aee2015-05-09 13:04:24 -0400630 struct saved *p;
631
Al Viro60ef60c2020-03-03 11:43:55 -0500632 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
633 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
634 if (unlikely(!p))
635 return false;
Al Viro894bc8c2015-05-02 07:16:16 -0400636 memcpy(p, nd->internal, sizeof(nd->internal));
637 nd->stack = p;
Al Viro60ef60c2020-03-03 11:43:55 -0500638 return true;
Al Viro894bc8c2015-05-02 07:16:16 -0400639}
640
Eric W. Biederman397d4252015-08-15 20:27:13 -0500641/**
Al Viro6b03f7e2020-02-24 15:53:19 -0500642 * path_connected - Verify that a dentry is below mnt.mnt_root
Eric W. Biederman397d4252015-08-15 20:27:13 -0500643 *
644 * Rename can sometimes move a file or directory outside of a bind
645 * mount, path_connected allows those cases to be detected.
646 */
Al Viro6b03f7e2020-02-24 15:53:19 -0500647static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500648{
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500649 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500650
Christoph Hellwig402dd2c2020-09-24 08:51:28 +0200651 /* Bind mounts can have disconnected paths */
652 if (mnt->mnt_root == sb->s_root)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500653 return true;
654
Al Viro6b03f7e2020-02-24 15:53:19 -0500655 return is_subdir(dentry, mnt->mnt_root);
Eric W. Biederman397d4252015-08-15 20:27:13 -0500656}
657
Al Viro7973387a2015-05-09 12:55:43 -0400658static void drop_links(struct nameidata *nd)
659{
660 int i = nd->depth;
661 while (i--) {
662 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500663 do_delayed_call(&last->done);
664 clear_delayed_call(&last->done);
Al Viro7973387a2015-05-09 12:55:43 -0400665 }
666}
667
668static void terminate_walk(struct nameidata *nd)
669{
670 drop_links(nd);
671 if (!(nd->flags & LOOKUP_RCU)) {
672 int i;
673 path_put(&nd->path);
674 for (i = 0; i < nd->depth; i++)
675 path_put(&nd->stack[i].link);
Al Virobcba1e72021-04-01 22:03:41 -0400676 if (nd->state & ND_ROOT_GRABBED) {
Al Viro102b8af2015-05-12 17:35:52 -0400677 path_put(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400678 nd->state &= ~ND_ROOT_GRABBED;
Al Viro102b8af2015-05-12 17:35:52 -0400679 }
Al Viro7973387a2015-05-09 12:55:43 -0400680 } else {
681 nd->flags &= ~LOOKUP_RCU;
Al Viro7973387a2015-05-09 12:55:43 -0400682 rcu_read_unlock();
683 }
684 nd->depth = 0;
Al Viro7d01ef752021-04-06 12:33:07 -0400685 nd->path.mnt = NULL;
686 nd->path.dentry = NULL;
Al Viro7973387a2015-05-09 12:55:43 -0400687}
688
689/* path_put is needed afterwards regardless of success or failure */
Al Viro2aa38472020-02-26 19:19:05 -0500690static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
Al Viro7973387a2015-05-09 12:55:43 -0400691{
Al Viro2aa38472020-02-26 19:19:05 -0500692 int res = __legitimize_mnt(path->mnt, mseq);
Al Viro7973387a2015-05-09 12:55:43 -0400693 if (unlikely(res)) {
694 if (res > 0)
695 path->mnt = NULL;
696 path->dentry = NULL;
697 return false;
698 }
699 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
700 path->dentry = NULL;
701 return false;
702 }
703 return !read_seqcount_retry(&path->dentry->d_seq, seq);
704}
705
Al Viro2aa38472020-02-26 19:19:05 -0500706static inline bool legitimize_path(struct nameidata *nd,
707 struct path *path, unsigned seq)
708{
Al Viro5bd73282020-04-05 21:59:55 -0400709 return __legitimize_path(path, seq, nd->m_seq);
Al Viro2aa38472020-02-26 19:19:05 -0500710}
711
Al Viro7973387a2015-05-09 12:55:43 -0400712static bool legitimize_links(struct nameidata *nd)
713{
714 int i;
Al Viroeacd9aa82021-02-15 12:03:23 -0500715 if (unlikely(nd->flags & LOOKUP_CACHED)) {
716 drop_links(nd);
717 nd->depth = 0;
718 return false;
719 }
Al Viro7973387a2015-05-09 12:55:43 -0400720 for (i = 0; i < nd->depth; i++) {
721 struct saved *last = nd->stack + i;
722 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
723 drop_links(nd);
724 nd->depth = i + 1;
725 return false;
726 }
727 }
728 return true;
729}
730
Al Viroee594bf2019-07-16 21:05:36 -0400731static bool legitimize_root(struct nameidata *nd)
732{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100733 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
Al Virobcba1e72021-04-01 22:03:41 -0400734 if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET))
Al Viroee594bf2019-07-16 21:05:36 -0400735 return true;
Al Virobcba1e72021-04-01 22:03:41 -0400736 nd->state |= ND_ROOT_GRABBED;
Al Viroee594bf2019-07-16 21:05:36 -0400737 return legitimize_path(nd, &nd->root, nd->root_seq);
738}
739
Al Viro19660af2011-03-25 10:32:48 -0400740/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100741 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400742 * Documentation/filesystems/path-lookup.txt). In situations when we can't
743 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500744 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400745 * mode. Refcounts are grabbed at the last known good point before rcu-walk
746 * got stuck, so ref-walk may continue from there. If this is not successful
747 * (eg. a seqcount has changed), then failure is returned and it's up to caller
748 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100749 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100750
751/**
Jens Axboee36cffe2020-12-17 09:19:08 -0700752 * try_to_unlazy - try to switch to ref-walk mode.
Al Viro19660af2011-03-25 10:32:48 -0400753 * @nd: nameidata pathwalk data
Jens Axboee36cffe2020-12-17 09:19:08 -0700754 * Returns: true on success, false on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100755 *
Jens Axboee36cffe2020-12-17 09:19:08 -0700756 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
Al Viro4675ac32017-01-09 22:29:15 -0500757 * for ref-walk mode.
758 * Must be called from rcu-walk context.
Jens Axboee36cffe2020-12-17 09:19:08 -0700759 * Nothing should touch nameidata between try_to_unlazy() failure and
Al Viro7973387a2015-05-09 12:55:43 -0400760 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100761 */
Jens Axboee36cffe2020-12-17 09:19:08 -0700762static bool try_to_unlazy(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100763{
Nick Piggin31e6b012011-01-07 17:49:52 +1100764 struct dentry *parent = nd->path.dentry;
765
766 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700767
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700768 nd->flags &= ~LOOKUP_RCU;
Al Viro7973387a2015-05-09 12:55:43 -0400769 if (unlikely(!legitimize_links(nd)))
Al Viro4675ac32017-01-09 22:29:15 -0500770 goto out1;
Al Viro84a2bd32019-07-16 21:20:17 -0400771 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
772 goto out;
Al Viroee594bf2019-07-16 21:05:36 -0400773 if (unlikely(!legitimize_root(nd)))
774 goto out;
Al Viro4675ac32017-01-09 22:29:15 -0500775 rcu_read_unlock();
776 BUG_ON(nd->inode != parent->d_inode);
Jens Axboee36cffe2020-12-17 09:19:08 -0700777 return true;
Al Viro4675ac32017-01-09 22:29:15 -0500778
Al Viro84a2bd32019-07-16 21:20:17 -0400779out1:
Al Viro4675ac32017-01-09 22:29:15 -0500780 nd->path.mnt = NULL;
781 nd->path.dentry = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500782out:
783 rcu_read_unlock();
Jens Axboee36cffe2020-12-17 09:19:08 -0700784 return false;
Al Viro4675ac32017-01-09 22:29:15 -0500785}
786
787/**
Al Viroae66db42021-01-04 00:08:41 -0500788 * try_to_unlazy_next - try to switch to ref-walk mode.
Al Viro4675ac32017-01-09 22:29:15 -0500789 * @nd: nameidata pathwalk data
Al Viroae66db42021-01-04 00:08:41 -0500790 * @dentry: next dentry to step into
791 * @seq: seq number to check @dentry against
792 * Returns: true on success, false on failure
Al Viro4675ac32017-01-09 22:29:15 -0500793 *
Tom Rix30476f72022-01-25 05:13:40 -0800794 * Similar to try_to_unlazy(), but here we have the next dentry already
Al Viroae66db42021-01-04 00:08:41 -0500795 * picked by rcu-walk and want to legitimize that in addition to the current
796 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
797 * Nothing should touch nameidata between try_to_unlazy_next() failure and
Al Viro4675ac32017-01-09 22:29:15 -0500798 * terminate_walk().
799 */
Al Viroae66db42021-01-04 00:08:41 -0500800static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq)
Al Viro4675ac32017-01-09 22:29:15 -0500801{
802 BUG_ON(!(nd->flags & LOOKUP_RCU));
803
804 nd->flags &= ~LOOKUP_RCU;
805 if (unlikely(!legitimize_links(nd)))
806 goto out2;
Al Viro7973387a2015-05-09 12:55:43 -0400807 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
808 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500809 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro7973387a2015-05-09 12:55:43 -0400810 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400811
Linus Torvalds15570082013-09-02 11:38:06 -0700812 /*
Al Viro4675ac32017-01-09 22:29:15 -0500813 * We need to move both the parent and the dentry from the RCU domain
814 * to be properly refcounted. And the sequence number in the dentry
815 * validates *both* dentry counters, since we checked the sequence
816 * number of the parent after we got the child sequence number. So we
817 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700818 */
Al Viro4675ac32017-01-09 22:29:15 -0500819 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
820 goto out;
Al Viro84a2bd32019-07-16 21:20:17 -0400821 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
822 goto out_dput;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700823 /*
824 * Sequence counts matched. Now make sure that the root is
825 * still valid and get it if required.
826 */
Al Viro84a2bd32019-07-16 21:20:17 -0400827 if (unlikely(!legitimize_root(nd)))
828 goto out_dput;
Al Viro8b61e742013-11-08 12:45:01 -0500829 rcu_read_unlock();
Al Viroae66db42021-01-04 00:08:41 -0500830 return true;
Al Viro19660af2011-03-25 10:32:48 -0400831
Al Viro7973387a2015-05-09 12:55:43 -0400832out2:
833 nd->path.mnt = NULL;
834out1:
835 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700836out:
Al Viro8b61e742013-11-08 12:45:01 -0500837 rcu_read_unlock();
Al Viroae66db42021-01-04 00:08:41 -0500838 return false;
Al Viro84a2bd32019-07-16 21:20:17 -0400839out_dput:
840 rcu_read_unlock();
841 dput(dentry);
Al Viroae66db42021-01-04 00:08:41 -0500842 return false;
Nick Piggin31e6b012011-01-07 17:49:52 +1100843}
844
Al Viro4ce16ef32012-06-10 16:10:59 -0400845static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100846{
Al Viroa89f8332017-01-09 22:25:28 -0500847 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
848 return dentry->d_op->d_revalidate(dentry, flags);
849 else
850 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100851}
852
Al Viro9f1fafe2011-03-25 11:00:12 -0400853/**
854 * complete_walk - successful completion of path walk
855 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500856 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400857 * If we had been in RCU mode, drop out of it and legitimize nd->path.
858 * Revalidate the final result, unless we'd already done that during
859 * the path walk or the filesystem doesn't ask for it. Return 0 on
860 * success, -error on failure. In case of failure caller does not
861 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500862 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400863static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500864{
Al Viro16c2cd72011-02-22 15:50:10 -0500865 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500866 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500867
Al Viro9f1fafe2011-03-25 11:00:12 -0400868 if (nd->flags & LOOKUP_RCU) {
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100869 /*
870 * We don't want to zero nd->root for scoped-lookups or
871 * externally-managed nd->root.
872 */
Al Virobcba1e72021-04-01 22:03:41 -0400873 if (!(nd->state & ND_ROOT_PRESET))
874 if (!(nd->flags & LOOKUP_IS_SCOPED))
875 nd->root.mnt = NULL;
Jens Axboe6c6ec2b2020-12-17 09:19:09 -0700876 nd->flags &= ~LOOKUP_CACHED;
Jens Axboee36cffe2020-12-17 09:19:08 -0700877 if (!try_to_unlazy(nd))
Al Viro48a066e2013-09-29 22:06:07 -0400878 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400879 }
880
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100881 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
882 /*
883 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
884 * ever step outside the root during lookup" and should already
885 * be guaranteed by the rest of namei, we want to avoid a namei
886 * BUG resulting in userspace being given a path that was not
887 * scoped within the root at some point during the lookup.
888 *
889 * So, do a final sanity-check to make sure that in the
890 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
891 * we won't silently return an fd completely outside of the
892 * requested root to userspace.
893 *
894 * Userspace could move the path outside the root after this
895 * check, but as discussed elsewhere this is not a concern (the
896 * resolved file was inside the root at some point).
897 */
898 if (!path_is_under(&nd->path, &nd->root))
899 return -EXDEV;
900 }
901
Al Virobcba1e72021-04-01 22:03:41 -0400902 if (likely(!(nd->state & ND_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500903 return 0;
904
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500905 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500906 return 0;
907
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500908 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500909 if (status > 0)
910 return 0;
911
Al Viro16c2cd72011-02-22 15:50:10 -0500912 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500913 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500914
Jeff Layton39159de2009-12-07 12:01:50 -0500915 return status;
916}
917
Aleksa Sarai740a1672019-12-07 01:13:29 +1100918static int set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400919{
Al Viro7bd88372014-09-13 21:55:46 -0400920 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100921
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100922 /*
923 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
924 * still have to ensure it doesn't happen because it will cause a breakout
925 * from the dirfd.
926 */
927 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
928 return -ENOTRECOVERABLE;
929
Al Viro9e6697e2015-12-05 20:07:21 -0500930 if (nd->flags & LOOKUP_RCU) {
931 unsigned seq;
932
933 do {
934 seq = read_seqcount_begin(&fs->seq);
935 nd->root = fs->root;
936 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
937 } while (read_seqcount_retry(&fs->seq, seq));
938 } else {
939 get_fs_root(fs, &nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400940 nd->state |= ND_ROOT_GRABBED;
Al Viro9e6697e2015-12-05 20:07:21 -0500941 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100942 return 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100943}
944
Al Viro248fb5b2015-12-05 20:51:58 -0500945static int nd_jump_root(struct nameidata *nd)
946{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100947 if (unlikely(nd->flags & LOOKUP_BENEATH))
948 return -EXDEV;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100949 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
950 /* Absolute path arguments to path_init() are allowed. */
951 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
952 return -EXDEV;
953 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100954 if (!nd->root.mnt) {
955 int error = set_root(nd);
956 if (error)
957 return error;
958 }
Al Viro248fb5b2015-12-05 20:51:58 -0500959 if (nd->flags & LOOKUP_RCU) {
960 struct dentry *d;
961 nd->path = nd->root;
962 d = nd->path.dentry;
963 nd->inode = d->d_inode;
964 nd->seq = nd->root_seq;
965 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
966 return -ECHILD;
967 } else {
968 path_put(&nd->path);
969 nd->path = nd->root;
970 path_get(&nd->path);
971 nd->inode = nd->path.dentry->d_inode;
972 }
Al Virobcba1e72021-04-01 22:03:41 -0400973 nd->state |= ND_JUMPED;
Al Viro248fb5b2015-12-05 20:51:58 -0500974 return 0;
975}
976
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400977/*
Al Viro6b255392015-11-17 10:20:54 -0500978 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400979 * caller must have taken a reference to path beforehand.
980 */
Aleksa Sarai1bc82072019-12-07 01:13:28 +1100981int nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400982{
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100983 int error = -ELOOP;
Al Viro6e77137b2015-05-02 13:37:52 -0400984 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400985
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100986 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
987 goto err;
988
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100989 error = -EXDEV;
990 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
991 if (nd->path.mnt != path->mnt)
992 goto err;
993 }
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100994 /* Not currently safe for scoped-lookups. */
995 if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
996 goto err;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100997
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100998 path_put(&nd->path);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400999 nd->path = *path;
1000 nd->inode = nd->path.dentry->d_inode;
Al Virobcba1e72021-04-01 22:03:41 -04001001 nd->state |= ND_JUMPED;
Aleksa Sarai1bc82072019-12-07 01:13:28 +11001002 return 0;
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001003
1004err:
1005 path_put(path);
1006 return error;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001007}
1008
Al Virob9ff4422015-05-02 20:19:23 -04001009static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -04001010{
Al Viro21c30032015-05-03 21:06:24 -04001011 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -05001012 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -04001013 if (!(nd->flags & LOOKUP_RCU))
1014 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -04001015}
1016
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001017static int sysctl_protected_symlinks __read_mostly;
1018static int sysctl_protected_hardlinks __read_mostly;
1019static int sysctl_protected_fifos __read_mostly;
1020static int sysctl_protected_regular __read_mostly;
1021
1022#ifdef CONFIG_SYSCTL
1023static struct ctl_table namei_sysctls[] = {
1024 {
1025 .procname = "protected_symlinks",
1026 .data = &sysctl_protected_symlinks,
1027 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001028 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001029 .proc_handler = proc_dointvec_minmax,
1030 .extra1 = SYSCTL_ZERO,
1031 .extra2 = SYSCTL_ONE,
1032 },
1033 {
1034 .procname = "protected_hardlinks",
1035 .data = &sysctl_protected_hardlinks,
1036 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001037 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001038 .proc_handler = proc_dointvec_minmax,
1039 .extra1 = SYSCTL_ZERO,
1040 .extra2 = SYSCTL_ONE,
1041 },
1042 {
1043 .procname = "protected_fifos",
1044 .data = &sysctl_protected_fifos,
1045 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001046 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001047 .proc_handler = proc_dointvec_minmax,
1048 .extra1 = SYSCTL_ZERO,
1049 .extra2 = SYSCTL_TWO,
1050 },
1051 {
1052 .procname = "protected_regular",
1053 .data = &sysctl_protected_regular,
1054 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001055 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001056 .proc_handler = proc_dointvec_minmax,
1057 .extra1 = SYSCTL_ZERO,
1058 .extra2 = SYSCTL_TWO,
1059 },
1060 { }
1061};
1062
1063static int __init init_fs_namei_sysctls(void)
1064{
1065 register_sysctl_init("fs", namei_sysctls);
1066 return 0;
1067}
1068fs_initcall(init_fs_namei_sysctls);
1069
1070#endif /* CONFIG_SYSCTL */
Kees Cook800179c2012-07-25 17:29:07 -07001071
1072/**
1073 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -07001074 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -07001075 *
1076 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1077 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1078 * in a sticky world-writable directory. This is to protect privileged
1079 * processes from failing races against path names that may change out
1080 * from under them by way of other users creating malicious symlinks.
1081 * It will permit symlinks to be followed only when outside a sticky
1082 * world-writable directory, or when the uid of the symlink and follower
1083 * match, or when the directory owner matches the symlink's owner.
1084 *
1085 * Returns 0 if following the symlink is allowed, -ve on error.
1086 */
Al Viroad6cc4c2020-01-14 14:41:39 -05001087static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001088{
Christian Braunerba73d982021-01-21 14:19:31 +01001089 struct user_namespace *mnt_userns;
1090 kuid_t i_uid;
1091
Kees Cook800179c2012-07-25 17:29:07 -07001092 if (!sysctl_protected_symlinks)
1093 return 0;
1094
Christian Braunerba73d982021-01-21 14:19:31 +01001095 mnt_userns = mnt_user_ns(nd->path.mnt);
1096 i_uid = i_uid_into_mnt(mnt_userns, inode);
Kees Cook800179c2012-07-25 17:29:07 -07001097 /* Allowed if owner and follower match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001098 if (uid_eq(current_cred()->fsuid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001099 return 0;
1100
1101 /* Allowed if parent directory not sticky and world-writable. */
Al Viro0f705952020-03-05 11:34:48 -05001102 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
Kees Cook800179c2012-07-25 17:29:07 -07001103 return 0;
1104
1105 /* Allowed if parent directory and link owner match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001106 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001107 return 0;
1108
Al Viro319565022015-05-07 20:37:40 -04001109 if (nd->flags & LOOKUP_RCU)
1110 return -ECHILD;
1111
Richard Guy Briggsea841ba2018-03-21 04:42:21 -04001112 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Kees Cook245d7362019-10-02 16:41:58 -07001113 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
Kees Cook800179c2012-07-25 17:29:07 -07001114 return -EACCES;
1115}
1116
1117/**
1118 * safe_hardlink_source - Check for safe hardlink conditions
Christian Braunerba73d982021-01-21 14:19:31 +01001119 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001120 * @inode: the source inode to hardlink from
1121 *
1122 * Return false if at least one of the following conditions:
1123 * - inode is not a regular file
1124 * - inode is setuid
1125 * - inode is setgid and group-exec
1126 * - access failure for read and write
1127 *
1128 * Otherwise returns true.
1129 */
Christian Braunerba73d982021-01-21 14:19:31 +01001130static bool safe_hardlink_source(struct user_namespace *mnt_userns,
1131 struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001132{
1133 umode_t mode = inode->i_mode;
1134
1135 /* Special files should not get pinned to the filesystem. */
1136 if (!S_ISREG(mode))
1137 return false;
1138
1139 /* Setuid files should not get pinned to the filesystem. */
1140 if (mode & S_ISUID)
1141 return false;
1142
1143 /* Executable setgid files should not get pinned to the filesystem. */
1144 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1145 return false;
1146
1147 /* Hardlinking to unreadable or unwritable sources is dangerous. */
Christian Braunerba73d982021-01-21 14:19:31 +01001148 if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
Kees Cook800179c2012-07-25 17:29:07 -07001149 return false;
1150
1151 return true;
1152}
1153
1154/**
1155 * may_linkat - Check permissions for creating a hardlink
Christian Braunerba73d982021-01-21 14:19:31 +01001156 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001157 * @link: the source to hardlink from
1158 *
1159 * Block hardlink when all of:
1160 * - sysctl_protected_hardlinks enabled
1161 * - fsuid does not match inode
1162 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001163 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -07001164 *
Christian Braunerba73d982021-01-21 14:19:31 +01001165 * If the inode has been found through an idmapped mount the user namespace of
1166 * the vfsmount must be passed through @mnt_userns. This function will then take
1167 * care to map the inode according to @mnt_userns before checking permissions.
1168 * On non-idmapped mounts or if permission checking is to be performed on the
1169 * raw inode simply passs init_user_ns.
1170 *
Kees Cook800179c2012-07-25 17:29:07 -07001171 * Returns 0 if successful, -ve on error.
1172 */
Christian Braunerba73d982021-01-21 14:19:31 +01001173int may_linkat(struct user_namespace *mnt_userns, struct path *link)
Kees Cook800179c2012-07-25 17:29:07 -07001174{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001175 struct inode *inode = link->dentry->d_inode;
1176
1177 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01001178 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
1179 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001180 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -07001181
1182 if (!sysctl_protected_hardlinks)
1183 return 0;
1184
Kees Cook800179c2012-07-25 17:29:07 -07001185 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1186 * otherwise, it must be a safe source.
1187 */
Christian Braunerba73d982021-01-21 14:19:31 +01001188 if (safe_hardlink_source(mnt_userns, inode) ||
1189 inode_owner_or_capable(mnt_userns, inode))
Kees Cook800179c2012-07-25 17:29:07 -07001190 return 0;
1191
Kees Cook245d7362019-10-02 16:41:58 -07001192 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001193 return -EPERM;
1194}
1195
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001196/**
1197 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1198 * should be allowed, or not, on files that already
1199 * exist.
Christian Braunerba73d982021-01-21 14:19:31 +01001200 * @mnt_userns: user namespace of the mount the inode was found from
Randy Dunlap2111c3c2021-02-15 20:29:28 -08001201 * @nd: nameidata pathwalk data
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001202 * @inode: the inode of the file to open
1203 *
1204 * Block an O_CREAT open of a FIFO (or a regular file) when:
1205 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1206 * - the file already exists
1207 * - we are in a sticky directory
1208 * - we don't own the file
1209 * - the owner of the directory doesn't own the file
1210 * - the directory is world writable
1211 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1212 * the directory doesn't have to be world writable: being group writable will
1213 * be enough.
1214 *
Christian Braunerba73d982021-01-21 14:19:31 +01001215 * If the inode has been found through an idmapped mount the user namespace of
1216 * the vfsmount must be passed through @mnt_userns. This function will then take
1217 * care to map the inode according to @mnt_userns before checking permissions.
1218 * On non-idmapped mounts or if permission checking is to be performed on the
1219 * raw inode simply passs init_user_ns.
1220 *
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001221 * Returns 0 if the open is allowed, -ve on error.
1222 */
Christian Braunerba73d982021-01-21 14:19:31 +01001223static int may_create_in_sticky(struct user_namespace *mnt_userns,
1224 struct nameidata *nd, struct inode *const inode)
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001225{
Christian Braunerba73d982021-01-21 14:19:31 +01001226 umode_t dir_mode = nd->dir_mode;
1227 kuid_t dir_uid = nd->dir_uid;
1228
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001229 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1230 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Virod0cb5012020-01-26 09:29:34 -05001231 likely(!(dir_mode & S_ISVTX)) ||
Christian Braunerba73d982021-01-21 14:19:31 +01001232 uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
1233 uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001234 return 0;
1235
Al Virod0cb5012020-01-26 09:29:34 -05001236 if (likely(dir_mode & 0002) ||
1237 (dir_mode & 0020 &&
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001238 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1239 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
Kees Cook245d7362019-10-02 16:41:58 -07001240 const char *operation = S_ISFIFO(inode->i_mode) ?
1241 "sticky_create_fifo" :
1242 "sticky_create_regular";
1243 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001244 return -EACCES;
1245 }
1246 return 0;
1247}
1248
David Howellsf015f1262012-06-25 12:55:28 +01001249/*
1250 * follow_up - Find the mountpoint of path's vfsmount
1251 *
1252 * Given a path, find the mountpoint of its source file system.
1253 * Replace @path with the path of the mountpoint in the parent mount.
1254 * Up is towards /.
1255 *
1256 * Return 1 if we went up a level and 0 if we were already at the
1257 * root.
1258 */
Al Virobab77eb2009-04-18 03:26:48 -04001259int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Al Viro0714a532011-11-24 22:19:58 -05001261 struct mount *mnt = real_mount(path->mnt);
1262 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001264
Al Viro48a066e2013-09-29 22:06:07 -04001265 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001266 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001267 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001268 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270 }
Al Viro0714a532011-11-24 22:19:58 -05001271 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001272 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001273 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001274 dput(path->dentry);
1275 path->dentry = mountpoint;
1276 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001277 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return 1;
1279}
Al Viro4d359502014-03-14 12:20:17 -04001280EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Al Viro7ef482f2020-02-26 17:50:13 -05001282static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1283 struct path *path, unsigned *seqp)
1284{
1285 while (mnt_has_parent(m)) {
1286 struct dentry *mountpoint = m->mnt_mountpoint;
1287
1288 m = m->mnt_parent;
1289 if (unlikely(root->dentry == mountpoint &&
1290 root->mnt == &m->mnt))
1291 break;
1292 if (mountpoint != m->mnt.mnt_root) {
1293 path->mnt = &m->mnt;
1294 path->dentry = mountpoint;
1295 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1296 return true;
1297 }
1298 }
1299 return false;
1300}
1301
Al Viro2aa38472020-02-26 19:19:05 -05001302static bool choose_mountpoint(struct mount *m, const struct path *root,
1303 struct path *path)
1304{
1305 bool found;
1306
1307 rcu_read_lock();
1308 while (1) {
1309 unsigned seq, mseq = read_seqbegin(&mount_lock);
1310
1311 found = choose_mountpoint_rcu(m, root, path, &seq);
1312 if (unlikely(!found)) {
1313 if (!read_seqretry(&mount_lock, mseq))
1314 break;
1315 } else {
1316 if (likely(__legitimize_path(path, seq, mseq)))
1317 break;
1318 rcu_read_unlock();
1319 path_put(path);
1320 rcu_read_lock();
1321 }
1322 }
1323 rcu_read_unlock();
1324 return found;
1325}
1326
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001327/*
David Howells9875cf82011-01-14 18:45:21 +00001328 * Perform an automount
1329 * - return -EISDIR to tell follow_managed() to stop and return the path we
1330 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001332static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
Nick Piggin31e6b012011-01-07 17:49:52 +11001333{
Al Viro25e195a2020-01-11 11:27:46 -05001334 struct dentry *dentry = path->dentry;
Al Viro463ffb22005-06-06 13:36:05 -07001335
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001336 /* We don't want to mount if someone's just doing a stat -
1337 * unless they're stat'ing a directory and appended a '/' to
1338 * the name.
1339 *
1340 * We do, however, want to mount if someone wants to open or
1341 * create a file of any type under the mountpoint, wants to
1342 * traverse through the mountpoint or wants to open the
1343 * mounted directory. Also, autofs may mark negative dentries
1344 * as being automount points. These will need the attentions
1345 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001346 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001347 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001348 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Al Viro25e195a2020-01-11 11:27:46 -05001349 dentry->d_inode)
Ian Kent5d38f042017-11-29 16:11:26 -08001350 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001351
Al Viro1c9f5e02020-01-16 22:05:18 -05001352 if (count && (*count)++ >= MAXSYMLINKS)
David Howells9875cf82011-01-14 18:45:21 +00001353 return -ELOOP;
1354
Al Viro25e195a2020-01-11 11:27:46 -05001355 return finish_automount(dentry->d_op->d_automount(path), path);
David Howells9875cf82011-01-14 18:45:21 +00001356}
1357
1358/*
Al Viro9deed3e2020-01-17 08:45:08 -05001359 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1360 * dentries are pinned but not locked here, so negative dentry can go
1361 * positive right under us. Use of smp_load_acquire() provides a barrier
1362 * sufficient for ->d_inode and ->d_flags consistency.
David Howells9875cf82011-01-14 18:45:21 +00001363 */
Al Viro9deed3e2020-01-17 08:45:08 -05001364static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1365 int *count, unsigned lookup_flags)
David Howells9875cf82011-01-14 18:45:21 +00001366{
Al Viro9deed3e2020-01-17 08:45:08 -05001367 struct vfsmount *mnt = path->mnt;
David Howells9875cf82011-01-14 18:45:21 +00001368 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001369 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001370
Al Viro9deed3e2020-01-17 08:45:08 -05001371 while (flags & DCACHE_MANAGED_DENTRY) {
David Howellscc53ce52011-01-14 18:45:26 +00001372 /* Allow the filesystem to manage the transit without i_mutex
1373 * being held. */
Al Virod41efb52019-11-04 22:30:52 -05001374 if (flags & DCACHE_MANAGE_TRANSIT) {
Ian Kentfb5f51c2016-11-24 08:03:41 +11001375 ret = path->dentry->d_op->d_manage(path, false);
Al Viro508c8772020-01-14 22:09:57 -05001376 flags = smp_load_acquire(&path->dentry->d_flags);
David Howellscc53ce52011-01-14 18:45:26 +00001377 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001378 break;
David Howellscc53ce52011-01-14 18:45:26 +00001379 }
1380
Al Viro9deed3e2020-01-17 08:45:08 -05001381 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
David Howells9875cf82011-01-14 18:45:21 +00001382 struct vfsmount *mounted = lookup_mnt(path);
Al Viro9deed3e2020-01-17 08:45:08 -05001383 if (mounted) { // ... in our namespace
David Howells9875cf82011-01-14 18:45:21 +00001384 dput(path->dentry);
1385 if (need_mntput)
1386 mntput(path->mnt);
1387 path->mnt = mounted;
1388 path->dentry = dget(mounted->mnt_root);
Al Viro9deed3e2020-01-17 08:45:08 -05001389 // here we know it's positive
1390 flags = path->dentry->d_flags;
David Howells9875cf82011-01-14 18:45:21 +00001391 need_mntput = true;
1392 continue;
1393 }
David Howells9875cf82011-01-14 18:45:21 +00001394 }
1395
Al Viro9deed3e2020-01-17 08:45:08 -05001396 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1397 break;
David Howells9875cf82011-01-14 18:45:21 +00001398
Al Viro9deed3e2020-01-17 08:45:08 -05001399 // uncovered automount point
1400 ret = follow_automount(path, count, lookup_flags);
1401 flags = smp_load_acquire(&path->dentry->d_flags);
1402 if (ret < 0)
1403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Al Viro8aef1882011-06-16 15:10:06 +01001405
Al Viro9deed3e2020-01-17 08:45:08 -05001406 if (ret == -EISDIR)
1407 ret = 0;
1408 // possible if you race with several mount --move
1409 if (need_mntput && path->mnt == mnt)
1410 mntput(path->mnt);
1411 if (!ret && unlikely(d_flags_negative(flags)))
Al Virod41efb52019-11-04 22:30:52 -05001412 ret = -ENOENT;
Al Viro9deed3e2020-01-17 08:45:08 -05001413 *jumped = need_mntput;
Al Viro8402752e2015-04-22 10:30:08 -04001414 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Al Viro9deed3e2020-01-17 08:45:08 -05001417static inline int traverse_mounts(struct path *path, bool *jumped,
1418 int *count, unsigned lookup_flags)
1419{
1420 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1421
1422 /* fastpath */
1423 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1424 *jumped = false;
1425 if (unlikely(d_flags_negative(flags)))
1426 return -ENOENT;
1427 return 0;
1428 }
1429 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1430}
1431
David Howellscc53ce52011-01-14 18:45:26 +00001432int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct vfsmount *mounted;
1435
Al Viro1c755af2009-04-18 14:06:57 -04001436 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001438 dput(path->dentry);
1439 mntput(path->mnt);
1440 path->mnt = mounted;
1441 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return 1;
1443 }
1444 return 0;
1445}
Al Viro4d359502014-03-14 12:20:17 -04001446EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
David Howells9875cf82011-01-14 18:45:21 +00001448/*
Al Viro9deed3e2020-01-17 08:45:08 -05001449 * Follow down to the covering mount currently visible to userspace. At each
1450 * point, the filesystem owning that dentry may be queried as to whether the
1451 * caller is permitted to proceed or not.
1452 */
1453int follow_down(struct path *path)
1454{
1455 struct vfsmount *mnt = path->mnt;
1456 bool jumped;
1457 int ret = traverse_mounts(path, &jumped, NULL, 0);
1458
1459 if (path->mnt != mnt)
1460 mntput(mnt);
1461 return ret;
1462}
1463EXPORT_SYMBOL(follow_down);
1464
1465/*
Al Viro287548e2011-05-27 06:50:06 -04001466 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1467 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001468 */
1469static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001470 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001471{
Al Viroea936ae2020-01-16 09:52:04 -05001472 struct dentry *dentry = path->dentry;
1473 unsigned int flags = dentry->d_flags;
1474
1475 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1476 return true;
1477
1478 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1479 return false;
1480
Ian Kent62a73752011-03-25 01:51:02 +08001481 for (;;) {
Ian Kent62a73752011-03-25 01:51:02 +08001482 /*
1483 * Don't forget we might have a non-mountpoint managed dentry
1484 * that wants to block transit.
1485 */
Al Viroea936ae2020-01-16 09:52:04 -05001486 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1487 int res = dentry->d_op->d_manage(path, true);
1488 if (res)
1489 return res == -EISDIR;
1490 flags = dentry->d_flags;
NeilBrownb8faf032014-08-04 17:06:29 +10001491 }
Ian Kent62a73752011-03-25 01:51:02 +08001492
Al Viroea936ae2020-01-16 09:52:04 -05001493 if (flags & DCACHE_MOUNTED) {
1494 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1495 if (mounted) {
1496 path->mnt = &mounted->mnt;
1497 dentry = path->dentry = mounted->mnt.mnt_root;
Al Virobcba1e72021-04-01 22:03:41 -04001498 nd->state |= ND_JUMPED;
Al Viroea936ae2020-01-16 09:52:04 -05001499 *seqp = read_seqcount_begin(&dentry->d_seq);
1500 *inode = dentry->d_inode;
1501 /*
1502 * We don't need to re-check ->d_seq after this
1503 * ->d_inode read - there will be an RCU delay
1504 * between mount hash removal and ->mnt_root
1505 * becoming unpinned.
1506 */
1507 flags = dentry->d_flags;
1508 continue;
1509 }
1510 if (read_seqretry(&mount_lock, nd->m_seq))
1511 return false;
1512 }
1513 return !(flags & DCACHE_NEED_AUTOMOUNT);
David Howells9875cf82011-01-14 18:45:21 +00001514 }
Al Viro287548e2011-05-27 06:50:06 -04001515}
1516
Al Virodb3c9ad2020-01-09 14:41:00 -05001517static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1518 struct path *path, struct inode **inode,
1519 unsigned int *seqp)
Al Virobd7c4b52020-01-08 20:37:23 -05001520{
Al Viro9deed3e2020-01-17 08:45:08 -05001521 bool jumped;
Al Virodb3c9ad2020-01-09 14:41:00 -05001522 int ret;
Al Virobd7c4b52020-01-08 20:37:23 -05001523
Al Virodb3c9ad2020-01-09 14:41:00 -05001524 path->mnt = nd->path.mnt;
1525 path->dentry = dentry;
Al Viroc1530072020-01-09 14:50:18 -05001526 if (nd->flags & LOOKUP_RCU) {
1527 unsigned int seq = *seqp;
1528 if (unlikely(!*inode))
1529 return -ENOENT;
1530 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viro9deed3e2020-01-17 08:45:08 -05001531 return 0;
Al Viroae66db42021-01-04 00:08:41 -05001532 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viroc1530072020-01-09 14:50:18 -05001533 return -ECHILD;
1534 // *path might've been clobbered by __follow_mount_rcu()
1535 path->mnt = nd->path.mnt;
1536 path->dentry = dentry;
1537 }
Al Viro9deed3e2020-01-17 08:45:08 -05001538 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1539 if (jumped) {
1540 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1541 ret = -EXDEV;
1542 else
Al Virobcba1e72021-04-01 22:03:41 -04001543 nd->state |= ND_JUMPED;
Al Viro9deed3e2020-01-17 08:45:08 -05001544 }
1545 if (unlikely(ret)) {
1546 dput(path->dentry);
1547 if (path->mnt != nd->path.mnt)
1548 mntput(path->mnt);
1549 } else {
Al Virobd7c4b52020-01-08 20:37:23 -05001550 *inode = d_backing_inode(path->dentry);
1551 *seqp = 0; /* out of RCU mode, so the value doesn't matter */
1552 }
1553 return ret;
1554}
1555
David Howells9875cf82011-01-14 18:45:21 +00001556/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001557 * This looks up the name in dcache and possibly revalidates the found dentry.
1558 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001559 */
Al Viroe3c13922016-03-06 14:03:27 -05001560static struct dentry *lookup_dcache(const struct qstr *name,
1561 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001562 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001563{
Al Viroa89f8332017-01-09 22:25:28 -05001564 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001565 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001566 int error = d_revalidate(dentry, flags);
1567 if (unlikely(error <= 0)) {
1568 if (!error)
1569 d_invalidate(dentry);
1570 dput(dentry);
1571 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001572 }
1573 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001574 return dentry;
1575}
1576
1577/*
Al Viroa03ece52018-03-08 11:00:45 -05001578 * Parent directory has inode locked exclusive. This is one
1579 * and only case when ->lookup() gets called on non in-lookup
1580 * dentries - as the matter of fact, this only gets called
1581 * when directory is guaranteed to have no in-lookup children
1582 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001583 */
Al Viroa03ece52018-03-08 11:00:45 -05001584static struct dentry *__lookup_hash(const struct qstr *name,
1585 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001586{
Al Viroa03ece52018-03-08 11:00:45 -05001587 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001588 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001589 struct inode *dir = base->d_inode;
1590
1591 if (dentry)
1592 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001593
1594 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001595 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001596 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001597
1598 dentry = d_alloc(base, name);
1599 if (unlikely(!dentry))
1600 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001601
Al Viro72bd8662012-06-10 17:17:17 -04001602 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001603 if (unlikely(old)) {
1604 dput(dentry);
1605 dentry = old;
1606 }
1607 return dentry;
1608}
1609
Al Viro20e34352020-01-09 14:58:31 -05001610static struct dentry *lookup_fast(struct nameidata *nd,
1611 struct inode **inode,
1612 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Nick Piggin31e6b012011-01-07 17:49:52 +11001614 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001615 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001616
Al Viro3cac2602009-08-13 18:27:43 +04001617 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001618 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001619 * of a false negative due to a concurrent rename, the caller is
1620 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001621 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001622 if (nd->flags & LOOKUP_RCU) {
1623 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001624 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001625 if (unlikely(!dentry)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001626 if (!try_to_unlazy(nd))
Al Viro20e34352020-01-09 14:58:31 -05001627 return ERR_PTR(-ECHILD);
1628 return NULL;
Al Viro5d0f49c2016-03-05 21:32:53 -05001629 }
Al Viro5a18fff2011-03-11 04:44:53 -05001630
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001631 /*
1632 * This sequence count validates that the inode matches
1633 * the dentry name information from lookup.
1634 */
David Howells63afdfc2015-05-06 15:59:00 +01001635 *inode = d_backing_inode(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001636 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Al Viro20e34352020-01-09 14:58:31 -05001637 return ERR_PTR(-ECHILD);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001638
1639 /*
1640 * This sequence count validates that the parent had no
1641 * changes while we did the lookup of the dentry above.
1642 *
1643 * The memory barrier in read_seqcount_begin of child is
1644 * enough, we can use __read_seqcount_retry here.
1645 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001646 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Al Viro20e34352020-01-09 14:58:31 -05001647 return ERR_PTR(-ECHILD);
Al Viro5a18fff2011-03-11 04:44:53 -05001648
Al Viro254cf582015-05-05 09:40:46 -04001649 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001650 status = d_revalidate(dentry, nd->flags);
Al Viroc1530072020-01-09 14:50:18 -05001651 if (likely(status > 0))
Al Viro20e34352020-01-09 14:58:31 -05001652 return dentry;
Al Viroae66db42021-01-04 00:08:41 -05001653 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viro20e34352020-01-09 14:58:31 -05001654 return ERR_PTR(-ECHILD);
Steven Rostedt (VMware)26ddb452020-12-09 17:09:28 -05001655 if (status == -ECHILD)
Al Viro209a7fb2017-01-09 01:35:39 -05001656 /* we'd been told to redo it in non-rcu mode */
1657 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001658 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001659 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001660 if (unlikely(!dentry))
Al Viro20e34352020-01-09 14:58:31 -05001661 return NULL;
Al Viroa89f8332017-01-09 22:25:28 -05001662 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001663 }
Al Viro5a18fff2011-03-11 04:44:53 -05001664 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001665 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001666 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001667 dput(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001668 return ERR_PTR(status);
Al Viro5a18fff2011-03-11 04:44:53 -05001669 }
Al Viro20e34352020-01-09 14:58:31 -05001670 return dentry;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001671}
1672
1673/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001674static struct dentry *__lookup_slow(const struct qstr *name,
1675 struct dentry *dir,
1676 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001677{
Al Viro88d83312018-04-06 16:43:47 -04001678 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001679 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001680 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001681
Al Viro19363862016-04-14 19:33:34 -04001682 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001683 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001684 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001685again:
Al Virod9171b92016-04-15 03:33:13 -04001686 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001687 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001688 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001689 if (unlikely(!d_in_lookup(dentry))) {
Al Viroc64cd6e2020-01-10 17:17:19 -05001690 int error = d_revalidate(dentry, flags);
1691 if (unlikely(error <= 0)) {
1692 if (!error) {
1693 d_invalidate(dentry);
Al Viro949a8522016-03-06 14:20:52 -05001694 dput(dentry);
Al Viroc64cd6e2020-01-10 17:17:19 -05001695 goto again;
Al Viro949a8522016-03-06 14:20:52 -05001696 }
Al Viroc64cd6e2020-01-10 17:17:19 -05001697 dput(dentry);
1698 dentry = ERR_PTR(error);
Al Viro949a8522016-03-06 14:20:52 -05001699 }
Al Viro94bdd652016-04-15 02:42:04 -04001700 } else {
1701 old = inode->i_op->lookup(inode, dentry, flags);
1702 d_lookup_done(dentry);
1703 if (unlikely(old)) {
1704 dput(dentry);
1705 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001706 }
1707 }
Al Viroe3c13922016-03-06 14:03:27 -05001708 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Al Viro88d83312018-04-06 16:43:47 -04001711static struct dentry *lookup_slow(const struct qstr *name,
1712 struct dentry *dir,
1713 unsigned int flags)
1714{
1715 struct inode *inode = dir->d_inode;
1716 struct dentry *res;
1717 inode_lock_shared(inode);
1718 res = __lookup_slow(name, dir, flags);
1719 inode_unlock_shared(inode);
1720 return res;
1721}
1722
Christian Braunerba73d982021-01-21 14:19:31 +01001723static inline int may_lookup(struct user_namespace *mnt_userns,
1724 struct nameidata *nd)
Al Viro52094c82011-02-21 21:34:47 -05001725{
1726 if (nd->flags & LOOKUP_RCU) {
Linus Torvalds7d6beb72021-02-23 13:39:45 -08001727 int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Jens Axboee36cffe2020-12-17 09:19:08 -07001728 if (err != -ECHILD || !try_to_unlazy(nd))
Al Viro52094c82011-02-21 21:34:47 -05001729 return err;
Al Viro52094c82011-02-21 21:34:47 -05001730 }
Christian Braunerba73d982021-01-21 14:19:31 +01001731 return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001732}
1733
Al Viro49055902020-03-03 11:22:34 -05001734static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1735{
Al Viro49055902020-03-03 11:22:34 -05001736 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1737 return -ELOOP;
Al Viro45425762020-03-03 11:25:31 -05001738
1739 if (likely(nd->depth != EMBEDDED_LEVELS))
1740 return 0;
1741 if (likely(nd->stack != nd->internal))
1742 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001743 if (likely(nd_alloc_stack(nd)))
Al Viro49055902020-03-03 11:22:34 -05001744 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001745
1746 if (nd->flags & LOOKUP_RCU) {
1747 // we need to grab link before we do unlazy. And we can't skip
1748 // unlazy even if we fail to grab the link - cleanup needs it
Al Viro49055902020-03-03 11:22:34 -05001749 bool grabbed_link = legitimize_path(nd, link, seq);
Al Viro60ef60c2020-03-03 11:43:55 -05001750
Al Viroe5ca0242022-01-07 12:20:37 -05001751 if (!try_to_unlazy(nd) || !grabbed_link)
Al Viro60ef60c2020-03-03 11:43:55 -05001752 return -ECHILD;
1753
1754 if (nd_alloc_stack(nd))
1755 return 0;
Al Viro49055902020-03-03 11:22:34 -05001756 }
Al Viro60ef60c2020-03-03 11:43:55 -05001757 return -ENOMEM;
Al Viro49055902020-03-03 11:22:34 -05001758}
1759
Al Virob1a81972020-01-19 12:48:44 -05001760enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1761
Al Viro06708ad2020-01-14 14:26:57 -05001762static const char *pick_link(struct nameidata *nd, struct path *link,
Al Virob1a81972020-01-19 12:48:44 -05001763 struct inode *inode, unsigned seq, int flags)
Al Virod63ff282015-05-04 18:13:23 -04001764{
Al Viro1cf26652015-05-06 16:01:56 -04001765 struct saved *last;
Al Viroad6cc4c2020-01-14 14:41:39 -05001766 const char *res;
Al Viro49055902020-03-03 11:22:34 -05001767 int error = reserve_stack(nd, link, seq);
Al Viroad6cc4c2020-01-14 14:41:39 -05001768
Al Viro49055902020-03-03 11:22:34 -05001769 if (unlikely(error)) {
Al Viro84f0cd92020-03-03 10:14:30 -05001770 if (!(nd->flags & LOOKUP_RCU))
1771 path_put(link);
Al Viro49055902020-03-03 11:22:34 -05001772 return ERR_PTR(error);
Al Viro626de992015-05-04 18:26:59 -04001773 }
Al Viroab104922015-05-10 11:50:01 -04001774 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001775 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001776 clear_delayed_call(&last->done);
Al Viro0450b2d2015-05-08 13:23:53 -04001777 last->seq = seq;
Al Viroad6cc4c2020-01-14 14:41:39 -05001778
Al Virob1a81972020-01-19 12:48:44 -05001779 if (flags & WALK_TRAILING) {
Al Viroad6cc4c2020-01-14 14:41:39 -05001780 error = may_follow_link(nd, inode);
1781 if (unlikely(error))
1782 return ERR_PTR(error);
1783 }
1784
Mattias Nisslerdab741e02020-08-27 11:09:46 -06001785 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1786 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
Al Viroad6cc4c2020-01-14 14:41:39 -05001787 return ERR_PTR(-ELOOP);
1788
1789 if (!(nd->flags & LOOKUP_RCU)) {
1790 touch_atime(&last->link);
1791 cond_resched();
1792 } else if (atime_needs_update(&last->link, inode)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001793 if (!try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001794 return ERR_PTR(-ECHILD);
1795 touch_atime(&last->link);
1796 }
1797
1798 error = security_inode_follow_link(link->dentry, inode,
1799 nd->flags & LOOKUP_RCU);
1800 if (unlikely(error))
1801 return ERR_PTR(error);
1802
Al Viroad6cc4c2020-01-14 14:41:39 -05001803 res = READ_ONCE(inode->i_link);
1804 if (!res) {
1805 const char * (*get)(struct dentry *, struct inode *,
1806 struct delayed_call *);
1807 get = inode->i_op->get_link;
1808 if (nd->flags & LOOKUP_RCU) {
1809 res = get(NULL, inode, &last->done);
Jens Axboee36cffe2020-12-17 09:19:08 -07001810 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001811 res = get(link->dentry, inode, &last->done);
Al Viroad6cc4c2020-01-14 14:41:39 -05001812 } else {
1813 res = get(link->dentry, inode, &last->done);
1814 }
1815 if (!res)
1816 goto all_done;
1817 if (IS_ERR(res))
1818 return res;
1819 }
1820 if (*res == '/') {
1821 error = nd_jump_root(nd);
1822 if (unlikely(error))
1823 return ERR_PTR(error);
1824 while (unlikely(*++res == '/'))
1825 ;
1826 }
1827 if (*res)
1828 return res;
1829all_done: // pure jump
1830 put_link(nd);
1831 return NULL;
Al Virod63ff282015-05-04 18:13:23 -04001832}
1833
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001834/*
1835 * Do we need to follow links? We _really_ want to be able
1836 * to do this check without having to look at inode->i_op,
1837 * so we keep a cache of "no, this doesn't need follow_link"
1838 * for the common case.
1839 */
Al Virob0417d22020-01-14 13:34:20 -05001840static const char *step_into(struct nameidata *nd, int flags,
Al Virocbae4d12020-01-12 13:40:02 -05001841 struct dentry *dentry, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001842{
Al Virocbae4d12020-01-12 13:40:02 -05001843 struct path path;
1844 int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1845
1846 if (err < 0)
Al Virob0417d22020-01-14 13:34:20 -05001847 return ERR_PTR(err);
Al Virocbae4d12020-01-12 13:40:02 -05001848 if (likely(!d_is_symlink(path.dentry)) ||
Al Viro8c4efe22020-01-19 12:44:18 -05001849 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
Al Viroaca29032020-01-09 15:17:57 -05001850 (flags & WALK_NOFOLLOW)) {
Al Viro8f64fb12016-11-14 01:50:26 -05001851 /* not a symlink or should not follow */
Al Viroc99687a2020-03-03 10:56:17 -05001852 if (!(nd->flags & LOOKUP_RCU)) {
1853 dput(nd->path.dentry);
1854 if (nd->path.mnt != path.mnt)
1855 mntput(nd->path.mnt);
1856 }
1857 nd->path = path;
Al Viro8f64fb12016-11-14 01:50:26 -05001858 nd->inode = inode;
1859 nd->seq = seq;
Al Virob0417d22020-01-14 13:34:20 -05001860 return NULL;
Al Viro8f64fb12016-11-14 01:50:26 -05001861 }
Al Viroa7f77542016-02-27 19:31:01 -05001862 if (nd->flags & LOOKUP_RCU) {
Al Viro84f0cd92020-03-03 10:14:30 -05001863 /* make sure that d_is_symlink above matches inode */
Al Virocbae4d12020-01-12 13:40:02 -05001864 if (read_seqcount_retry(&path.dentry->d_seq, seq))
Al Virob0417d22020-01-14 13:34:20 -05001865 return ERR_PTR(-ECHILD);
Al Viro84f0cd92020-03-03 10:14:30 -05001866 } else {
1867 if (path.mnt == nd->path.mnt)
1868 mntget(path.mnt);
Al Viroa7f77542016-02-27 19:31:01 -05001869 }
Al Virob1a81972020-01-19 12:48:44 -05001870 return pick_link(nd, &path, inode, seq, flags);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001871}
1872
Al Viroc2df1962020-02-26 14:33:30 -05001873static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1874 struct inode **inodep,
1875 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001876{
Al Viro12487f32020-02-26 14:59:56 -05001877 struct dentry *parent, *old;
Al Viro957dd412020-02-26 01:40:04 -05001878
Al Viro12487f32020-02-26 14:59:56 -05001879 if (path_equal(&nd->path, &nd->root))
1880 goto in_root;
1881 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro7ef482f2020-02-26 17:50:13 -05001882 struct path path;
Al Viroefe772d2020-02-28 10:06:37 -05001883 unsigned seq;
Al Viro7ef482f2020-02-26 17:50:13 -05001884 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1885 &nd->root, &path, &seq))
1886 goto in_root;
Al Viroefe772d2020-02-28 10:06:37 -05001887 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1888 return ERR_PTR(-ECHILD);
1889 nd->path = path;
1890 nd->inode = path.dentry->d_inode;
1891 nd->seq = seq;
1892 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1893 return ERR_PTR(-ECHILD);
1894 /* we know that mountpoint was pinned */
Al Viro957dd412020-02-26 01:40:04 -05001895 }
Al Viro12487f32020-02-26 14:59:56 -05001896 old = nd->path.dentry;
1897 parent = old->d_parent;
1898 *inodep = parent->d_inode;
1899 *seqp = read_seqcount_begin(&parent->d_seq);
1900 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1901 return ERR_PTR(-ECHILD);
1902 if (unlikely(!path_connected(nd->path.mnt, parent)))
1903 return ERR_PTR(-ECHILD);
1904 return parent;
1905in_root:
Al Viroefe772d2020-02-28 10:06:37 -05001906 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1907 return ERR_PTR(-ECHILD);
Al Viroc2df1962020-02-26 14:33:30 -05001908 if (unlikely(nd->flags & LOOKUP_BENEATH))
1909 return ERR_PTR(-ECHILD);
1910 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001911}
1912
Al Viroc2df1962020-02-26 14:33:30 -05001913static struct dentry *follow_dotdot(struct nameidata *nd,
1914 struct inode **inodep,
1915 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001916{
Al Viro12487f32020-02-26 14:59:56 -05001917 struct dentry *parent;
1918
1919 if (path_equal(&nd->path, &nd->root))
1920 goto in_root;
1921 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro2aa38472020-02-26 19:19:05 -05001922 struct path path;
1923
1924 if (!choose_mountpoint(real_mount(nd->path.mnt),
1925 &nd->root, &path))
1926 goto in_root;
Al Viro165200d2020-02-28 10:17:52 -05001927 path_put(&nd->path);
1928 nd->path = path;
Al Viro2aa38472020-02-26 19:19:05 -05001929 nd->inode = path.dentry->d_inode;
Al Viro165200d2020-02-28 10:17:52 -05001930 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1931 return ERR_PTR(-EXDEV);
Al Viro957dd412020-02-26 01:40:04 -05001932 }
Al Viro12487f32020-02-26 14:59:56 -05001933 /* rare case of legitimate dget_parent()... */
1934 parent = dget_parent(nd->path.dentry);
1935 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1936 dput(parent);
1937 return ERR_PTR(-ENOENT);
1938 }
1939 *seqp = 0;
1940 *inodep = parent->d_inode;
1941 return parent;
1942
1943in_root:
Al Viroc2df1962020-02-26 14:33:30 -05001944 if (unlikely(nd->flags & LOOKUP_BENEATH))
1945 return ERR_PTR(-EXDEV);
1946 dget(nd->path.dentry);
1947 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001948}
1949
Al Viro7521f222020-02-26 12:22:58 -05001950static const char *handle_dots(struct nameidata *nd, int type)
Al Viro957dd412020-02-26 01:40:04 -05001951{
1952 if (type == LAST_DOTDOT) {
Al Viro7521f222020-02-26 12:22:58 -05001953 const char *error = NULL;
Al Viroc2df1962020-02-26 14:33:30 -05001954 struct dentry *parent;
1955 struct inode *inode;
1956 unsigned seq;
Al Viro957dd412020-02-26 01:40:04 -05001957
1958 if (!nd->root.mnt) {
Al Viro7521f222020-02-26 12:22:58 -05001959 error = ERR_PTR(set_root(nd));
Al Viro957dd412020-02-26 01:40:04 -05001960 if (error)
1961 return error;
1962 }
1963 if (nd->flags & LOOKUP_RCU)
Al Viroc2df1962020-02-26 14:33:30 -05001964 parent = follow_dotdot_rcu(nd, &inode, &seq);
Al Viro957dd412020-02-26 01:40:04 -05001965 else
Al Viroc2df1962020-02-26 14:33:30 -05001966 parent = follow_dotdot(nd, &inode, &seq);
1967 if (IS_ERR(parent))
1968 return ERR_CAST(parent);
1969 if (unlikely(!parent))
1970 error = step_into(nd, WALK_NOFOLLOW,
1971 nd->path.dentry, nd->inode, nd->seq);
1972 else
1973 error = step_into(nd, WALK_NOFOLLOW,
1974 parent, inode, seq);
1975 if (unlikely(error))
Al Viro957dd412020-02-26 01:40:04 -05001976 return error;
1977
1978 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1979 /*
1980 * If there was a racing rename or mount along our
1981 * path, then we can't be sure that ".." hasn't jumped
1982 * above nd->root (and so userspace should retry or use
1983 * some fallback).
1984 */
1985 smp_rmb();
1986 if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001987 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001988 if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001989 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001990 }
1991 }
Al Viro7521f222020-02-26 12:22:58 -05001992 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001993}
1994
Al Viro92d27012020-01-14 13:24:17 -05001995static const char *walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001996{
Al Virodb3c9ad2020-01-09 14:41:00 -05001997 struct dentry *dentry;
Al Viroce57dfc2011-03-13 19:58:58 -04001998 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001999 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04002000 /*
2001 * "." and ".." are special - ".." especially so because it has
2002 * to be able to know about the current root directory and
2003 * parent relationships.
2004 */
Al Viro4693a542015-05-04 17:47:11 -04002005 if (unlikely(nd->last_type != LAST_NORM)) {
Al Viro1c4ff1a2016-11-14 01:39:36 -05002006 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04002007 put_link(nd);
Al Viro7521f222020-02-26 12:22:58 -05002008 return handle_dots(nd, nd->last_type);
Al Viro4693a542015-05-04 17:47:11 -04002009 }
Al Viro20e34352020-01-09 14:58:31 -05002010 dentry = lookup_fast(nd, &inode, &seq);
2011 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05002012 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05002013 if (unlikely(!dentry)) {
Al Virodb3c9ad2020-01-09 14:41:00 -05002014 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
2015 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05002016 return ERR_CAST(dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04002017 }
Al Viro56676ec2020-03-10 21:54:54 -04002018 if (!(flags & WALK_MORE) && nd->depth)
2019 put_link(nd);
Al Virob0417d22020-01-14 13:34:20 -05002020 return step_into(nd, flags, dentry, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04002021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002024 * We can do the critical dentry name comparison and hashing
2025 * operations one word at a time, but we are limited to:
2026 *
2027 * - Architectures with fast unaligned word accesses. We could
2028 * do a "get_unaligned()" if this helps and is sufficiently
2029 * fast.
2030 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002031 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
2032 * do not trap on the (extremely unlikely) case of a page
2033 * crossing operation.
2034 *
2035 * - Furthermore, we need an efficient 64-bit compile for the
2036 * 64-bit case in order to generate the "number of bytes in
2037 * the final mask". Again, that could be replaced with a
2038 * efficient population count instruction or similar.
2039 */
2040#ifdef CONFIG_DCACHE_WORD_ACCESS
2041
Linus Torvaldsf68e5562012-04-06 13:54:56 -07002042#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002043
George Spelvin468a9422016-05-26 22:11:51 -04002044#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002045
George Spelvin468a9422016-05-26 22:11:51 -04002046/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
2047
2048#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04002049/*
2050 * Register pressure in the mixing function is an issue, particularly
2051 * on 32-bit x86, but almost any function requires one state value and
2052 * one temporary. Instead, use a function designed for two state values
2053 * and no temporaries.
2054 *
2055 * This function cannot create a collision in only two iterations, so
2056 * we have two iterations to achieve avalanche. In those two iterations,
2057 * we have six layers of mixing, which is enough to spread one bit's
2058 * influence out to 2^6 = 64 state bits.
2059 *
2060 * Rotate constants are scored by considering either 64 one-bit input
2061 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2062 * probability of that delta causing a change to each of the 128 output
2063 * bits, using a sample of random initial states.
2064 *
2065 * The Shannon entropy of the computed probabilities is then summed
2066 * to produce a score. Ideally, any input change has a 50% chance of
2067 * toggling any given output bit.
2068 *
2069 * Mixing scores (in bits) for (12,45):
2070 * Input delta: 1-bit 2-bit
2071 * 1 round: 713.3 42542.6
2072 * 2 rounds: 2753.7 140389.8
2073 * 3 rounds: 5954.1 233458.2
2074 * 4 rounds: 7862.6 256672.2
2075 * Perfect: 8192 258048
2076 * (64*128) (64*63/2 * 128)
2077 */
2078#define HASH_MIX(x, y, a) \
2079 ( x ^= (a), \
2080 y ^= x, x = rol64(x,12),\
2081 x += y, y = rol64(y,45),\
2082 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002083
George Spelvin0fed3ac2016-05-02 06:31:01 -04002084/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04002085 * Fold two longs into one 32-bit hash value. This must be fast, but
2086 * latency isn't quite as critical, as there is a fair bit of additional
2087 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04002088 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04002089static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002090{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002091 y ^= x * GOLDEN_RATIO_64;
2092 y *= GOLDEN_RATIO_64;
2093 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04002094}
2095
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002096#else /* 32-bit case */
2097
George Spelvin2a18da7a2016-05-23 07:43:58 -04002098/*
2099 * Mixing scores (in bits) for (7,20):
2100 * Input delta: 1-bit 2-bit
2101 * 1 round: 330.3 9201.6
2102 * 2 rounds: 1246.4 25475.4
2103 * 3 rounds: 1907.1 31295.1
2104 * 4 rounds: 2042.3 31718.6
2105 * Perfect: 2048 31744
2106 * (32*64) (32*31/2 * 64)
2107 */
2108#define HASH_MIX(x, y, a) \
2109 ( x ^= (a), \
2110 y ^= x, x = rol32(x, 7),\
2111 x += y, y = rol32(y,20),\
2112 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002113
George Spelvin2a18da7a2016-05-23 07:43:58 -04002114static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002115{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002116 /* Use arch-optimized multiply if one exists */
2117 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04002118}
2119
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002120#endif
2121
George Spelvin2a18da7a2016-05-23 07:43:58 -04002122/*
2123 * Return the hash of a string of known length. This is carfully
2124 * designed to match hash_name(), which is the more critical function.
2125 * In particular, we must end by hashing a final word containing 0..7
2126 * payload bytes, to match the way that hash_name() iterates until it
2127 * finds the delimiter after the name.
2128 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002129unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002130{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002131 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002132
2133 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002134 if (!len)
2135 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002136 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002137 if (len < sizeof(unsigned long))
2138 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04002139 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002140 name += sizeof(unsigned long);
2141 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002142 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04002143 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002144done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04002145 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002146}
2147EXPORT_SYMBOL(full_name_hash);
2148
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002149/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002150u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002151{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002152 unsigned long a = 0, x = 0, y = (unsigned long)salt;
2153 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002154 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2155
Linus Torvalds8387ff22016-06-10 07:51:30 -07002156 len = 0;
2157 goto inside;
2158
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002159 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002160 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002161 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002162inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002163 a = load_unaligned_zeropad(name+len);
2164 } while (!has_zero(a, &adata, &constants));
2165
2166 adata = prep_zero_mask(a, adata, &constants);
2167 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002168 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002169
George Spelvin2a18da7a2016-05-23 07:43:58 -04002170 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002171}
2172EXPORT_SYMBOL(hashlen_string);
2173
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002174/*
2175 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002176 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002177 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002178static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002179{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002180 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2181 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07002182 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002183
Linus Torvalds8387ff22016-06-10 07:51:30 -07002184 len = 0;
2185 goto inside;
2186
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002187 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002188 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002189 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002190inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002191 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002192 b = a ^ REPEAT_BYTE('/');
2193 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002194
Linus Torvalds36126f82012-05-26 10:43:17 -07002195 adata = prep_zero_mask(a, adata, &constants);
2196 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002197 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002198 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002199
George Spelvin2a18da7a2016-05-23 07:43:58 -04002200 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002201}
2202
George Spelvin2a18da7a2016-05-23 07:43:58 -04002203#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002204
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002205/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002206unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002207{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002208 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002209 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002210 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002211 return end_name_hash(hash);
2212}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002213EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002214
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002215/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002216u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002217{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002218 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002219 unsigned long len = 0, c;
2220
2221 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002222 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002223 len++;
2224 hash = partial_name_hash(c, hash);
2225 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002226 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002227 return hashlen_create(end_name_hash(hash), len);
2228}
George Spelvinf2a031b2016-05-29 01:26:41 -04002229EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002230
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002231/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002232 * We know there's a real path component here of at least
2233 * one character.
2234 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002235static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002236{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002237 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002238 unsigned long len = 0, c;
2239
2240 c = (unsigned char)*name;
2241 do {
2242 len++;
2243 hash = partial_name_hash(c, hash);
2244 c = (unsigned char)name[len];
2245 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002246 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002247}
2248
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002249#endif
2250
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002251/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002253 * This is the basic name resolution function, turning a pathname into
2254 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002256 * Returns 0 and nd will have valid dentry and mnt on success.
2257 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 */
Al Viro6de88d72009-08-09 01:41:57 +04002259static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Al Virod8d46112020-02-23 22:04:15 -05002261 int depth = 0; // depth <= nd->depth
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002263
Al Virob4c03532020-01-19 11:44:51 -05002264 nd->last_type = LAST_ROOT;
Al Viroc108837e2020-03-05 15:48:44 -05002265 nd->flags |= LOOKUP_PARENT;
Al Viro9b5858e2018-07-09 16:33:23 -04002266 if (IS_ERR(name))
2267 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 while (*name=='/')
2269 name++;
Al Viro1a97d892020-09-19 17:55:58 +01002270 if (!*name) {
2271 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
Al Viro9e18f102015-04-18 20:44:34 -04002272 return 0;
Al Viro1a97d892020-09-19 17:55:58 +01002273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* At this point we know we have a real path component. */
2276 for(;;) {
Christian Brauner549c7292021-01-21 14:19:43 +01002277 struct user_namespace *mnt_userns;
Al Viro92d27012020-01-14 13:24:17 -05002278 const char *link;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002279 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002280 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Christian Brauner549c7292021-01-21 14:19:43 +01002282 mnt_userns = mnt_user_ns(nd->path.mnt);
2283 err = may_lookup(mnt_userns, nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002284 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002285 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Linus Torvalds8387ff22016-06-10 07:51:30 -07002287 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Al Virofe479a52011-02-22 15:10:03 -05002289 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002290 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002291 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002292 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002293 type = LAST_DOTDOT;
Al Virobcba1e72021-04-01 22:03:41 -04002294 nd->state |= ND_JUMPED;
Al Viro16c2cd72011-02-22 15:50:10 -05002295 }
Al Virofe479a52011-02-22 15:10:03 -05002296 break;
2297 case 1:
2298 type = LAST_DOT;
2299 }
Al Viro5a202bc2011-03-08 14:17:44 -05002300 if (likely(type == LAST_NORM)) {
2301 struct dentry *parent = nd->path.dentry;
Al Virobcba1e72021-04-01 22:03:41 -04002302 nd->state &= ~ND_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002303 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002304 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002305 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002306 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002307 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002308 hash_len = this.hash_len;
2309 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002310 }
2311 }
Al Virofe479a52011-02-22 15:10:03 -05002312
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002313 nd->last.hash_len = hash_len;
2314 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002315 nd->last_type = type;
2316
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002317 name += hashlen_len(hash_len);
2318 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002319 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002320 /*
2321 * If it wasn't NUL, we know it was '/'. Skip that
2322 * slash, and continue until no more slashes.
2323 */
2324 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002325 name++;
2326 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002327 if (unlikely(!*name)) {
2328OK:
Al Virod8d46112020-02-23 22:04:15 -05002329 /* pathname or trailing symlink, done */
Al Viroc108837e2020-03-05 15:48:44 -05002330 if (!depth) {
Christian Brauner549c7292021-01-21 14:19:43 +01002331 nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode);
Al Viro0f705952020-03-05 11:34:48 -05002332 nd->dir_mode = nd->inode->i_mode;
Al Viroc108837e2020-03-05 15:48:44 -05002333 nd->flags &= ~LOOKUP_PARENT;
Al Viro8620c232015-05-04 08:58:35 -04002334 return 0;
Al Viroc108837e2020-03-05 15:48:44 -05002335 }
Al Viro8620c232015-05-04 08:58:35 -04002336 /* last component of nested symlink */
Al Virod8d46112020-02-23 22:04:15 -05002337 name = nd->stack[--depth].name;
Al Viro8c4efe22020-01-19 12:44:18 -05002338 link = walk_component(nd, 0);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002339 } else {
2340 /* not the last component */
Al Viro8c4efe22020-01-19 12:44:18 -05002341 link = walk_component(nd, WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002342 }
Al Viro92d27012020-01-14 13:24:17 -05002343 if (unlikely(link)) {
2344 if (IS_ERR(link))
2345 return PTR_ERR(link);
2346 /* a symlink to follow */
Al Virod8d46112020-02-23 22:04:15 -05002347 nd->stack[depth++].name = name;
Al Viro92d27012020-01-14 13:24:17 -05002348 name = link;
2349 continue;
Nick Piggin31e6b012011-01-07 17:49:52 +11002350 }
Al Viro97242f92015-08-01 19:59:28 -04002351 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2352 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07002353 if (!try_to_unlazy(nd))
Al Viro97242f92015-08-01 19:59:28 -04002354 return -ECHILD;
2355 }
Al Viro3595e232015-05-09 16:54:45 -04002356 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Al Viroedc2b1d2018-07-09 16:27:23 -04002361/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002362static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Aleksa Sarai740a1672019-12-07 01:13:29 +11002364 int error;
Al Viroc8a53ee2015-05-12 18:43:07 -04002365 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Jens Axboe6c6ec2b2020-12-17 09:19:09 -07002367 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2368 if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
2369 return ERR_PTR(-EAGAIN);
2370
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002371 if (!*s)
2372 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002373 if (flags & LOOKUP_RCU)
2374 rcu_read_lock();
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002375
Al Virobcba1e72021-04-01 22:03:41 -04002376 nd->flags = flags;
2377 nd->state |= ND_JUMPED;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002378
2379 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2380 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2381 smp_rmb();
2382
Al Virobcba1e72021-04-01 22:03:41 -04002383 if (nd->state & ND_ROOT_PRESET) {
David Howellsb18825a2013-09-12 19:22:53 +01002384 struct dentry *root = nd->root.dentry;
2385 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002386 if (*s && unlikely(!d_can_lookup(root)))
2387 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002388 nd->path = nd->root;
2389 nd->inode = inode;
2390 if (flags & LOOKUP_RCU) {
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002391 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002392 nd->root_seq = nd->seq;
Al Viro5b6ca022011-03-09 23:04:47 -05002393 } else {
2394 path_get(&nd->path);
2395 }
Al Viro368ee9b2015-05-08 17:19:59 -04002396 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002397 }
2398
Al Viro2a737872009-04-07 11:49:53 -04002399 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002401 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2402 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
Aleksa Sarai740a1672019-12-07 01:13:29 +11002403 error = nd_jump_root(nd);
2404 if (unlikely(error))
2405 return ERR_PTR(error);
2406 return s;
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002407 }
2408
2409 /* Relative pathname -- get the starting-point it is relative to. */
2410 if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002411 if (flags & LOOKUP_RCU) {
2412 struct fs_struct *fs = current->fs;
2413 unsigned seq;
2414
Al Viroe41f7d42011-02-22 14:02:58 -05002415 do {
2416 seq = read_seqcount_begin(&fs->seq);
2417 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002418 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002419 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2420 } while (read_seqcount_retry(&fs->seq, seq));
2421 } else {
2422 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002423 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002424 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002425 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002426 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002427 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002428 struct dentry *dentry;
2429
Al Viro2903ff02012-08-28 12:52:22 -04002430 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002431 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002432
Al Viro2903ff02012-08-28 12:52:22 -04002433 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002434
Al Viroedc2b1d2018-07-09 16:27:23 -04002435 if (*s && unlikely(!d_can_lookup(dentry))) {
2436 fdput(f);
2437 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002438 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002439
Al Viro2903ff02012-08-28 12:52:22 -04002440 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002441 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002442 nd->inode = nd->path.dentry->d_inode;
2443 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002444 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002445 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002446 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002447 }
Al Viro34a26b92015-05-11 08:05:05 -04002448 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002450
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002451 /* For scoped-lookups we need to set the root to the dirfd as well. */
2452 if (flags & LOOKUP_IS_SCOPED) {
2453 nd->root = nd->path;
2454 if (flags & LOOKUP_RCU) {
2455 nd->root_seq = nd->seq;
2456 } else {
2457 path_get(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -04002458 nd->state |= ND_ROOT_GRABBED;
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002459 }
2460 }
2461 return s;
Al Viro9b4a9b12009-04-07 11:44:16 -04002462}
2463
Al Viro1ccac622020-01-14 10:13:40 -05002464static inline const char *lookup_last(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002465{
Al Virobd92d7f2011-03-14 19:54:59 -04002466 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2467 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2468
Al Viroc108837e2020-03-05 15:48:44 -05002469 return walk_component(nd, WALK_TRAILING);
Al Virobd92d7f2011-03-14 19:54:59 -04002470}
2471
Al Viro4f757f32017-04-15 17:31:22 -04002472static int handle_lookup_down(struct nameidata *nd)
2473{
Al Viroc1530072020-01-09 14:50:18 -05002474 if (!(nd->flags & LOOKUP_RCU))
Al Virodb3c9ad2020-01-09 14:41:00 -05002475 dget(nd->path.dentry);
Al Virob0417d22020-01-14 13:34:20 -05002476 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2477 nd->path.dentry, nd->inode, nd->seq));
Al Viro4f757f32017-04-15 17:31:22 -04002478}
2479
Al Viro9b4a9b12009-04-07 11:44:16 -04002480/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002481static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002482{
Al Viroc8a53ee2015-05-12 18:43:07 -04002483 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002484 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002485
Al Viro9b5858e2018-07-09 16:33:23 -04002486 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002487 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002488 if (unlikely(err < 0))
2489 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002490 }
2491
Al Viro1ccac622020-01-14 10:13:40 -05002492 while (!(err = link_path_walk(s, nd)) &&
2493 (s = lookup_last(nd)) != NULL)
2494 ;
Al Viro4f0ed932021-04-06 19:46:51 -04002495 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2496 err = handle_lookup_down(nd);
Al Virobcba1e72021-04-01 22:03:41 -04002497 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please...
Al Viro4f0ed932021-04-06 19:46:51 -04002498 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002499 if (!err)
2500 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002501
Al Virodeb106c2015-05-08 18:05:21 -04002502 if (!err && nd->flags & LOOKUP_DIRECTORY)
2503 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002504 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002505 if (!err) {
2506 *path = nd->path;
2507 nd->path.mnt = NULL;
2508 nd->path.dentry = NULL;
2509 }
2510 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002511 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002512}
Nick Piggin31e6b012011-01-07 17:49:52 +11002513
Stephen Brennan794ebce2021-09-01 10:51:42 -07002514int filename_lookup(int dfd, struct filename *name, unsigned flags,
David Howells31d921c2018-11-01 23:07:24 +00002515 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002516{
Al Viro894bc8c2015-05-02 07:16:16 -04002517 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002518 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002519 if (IS_ERR(name))
2520 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002521 set_nameidata(&nd, dfd, name, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04002522 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002523 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002524 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002525 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002526 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002527
2528 if (likely(!retval))
Al Viro161aff12020-01-11 22:52:26 -05002529 audit_inode(name, path->dentry,
2530 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
Al Viro9883d182015-05-13 07:28:08 -04002531 restore_nameidata();
Dmitry Kadashev020250f2021-07-08 13:34:43 +07002532 return retval;
2533}
2534
Al Viro8bcb77f2015-05-08 16:59:20 -04002535/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002536static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002537 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002538{
Al Viroc8a53ee2015-05-12 18:43:07 -04002539 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002540 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002541 if (!err)
2542 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002543 if (!err) {
2544 *parent = nd->path;
2545 nd->path.mnt = NULL;
2546 nd->path.dentry = NULL;
2547 }
2548 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002549 return err;
2550}
2551
Stephen Brennan0766ec82021-09-01 10:51:41 -07002552/* Note: this does not consume "name" */
Al Viroc5f563f2021-09-07 15:57:42 -04002553static int filename_parentat(int dfd, struct filename *name,
Stephen Brennan0766ec82021-09-01 10:51:41 -07002554 unsigned int flags, struct path *parent,
2555 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002556{
2557 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002558 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002559
Al Viro5c31b6c2015-05-12 17:32:54 -04002560 if (IS_ERR(name))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002561 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002562 set_nameidata(&nd, dfd, name, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04002563 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002564 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002565 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002566 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002567 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002568 if (likely(!retval)) {
2569 *last = nd.last;
2570 *type = nd.last_type;
Al Viroc9b07ea2019-07-14 13:22:27 -04002571 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002572 }
Al Viro9883d182015-05-13 07:28:08 -04002573 restore_nameidata();
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002574 return retval;
2575}
2576
Al Viro79714f72012-06-15 03:01:42 +04002577/* does lookup, returns the object with parent locked */
Stephen Brennan0766ec82021-09-01 10:51:41 -07002578static struct dentry *__kern_path_locked(struct filename *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002579{
Al Viro5c31b6c2015-05-12 17:32:54 -04002580 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002581 struct qstr last;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002582 int type, error;
Paul Moore51689102015-01-22 00:00:03 -05002583
Al Viroc5f563f2021-09-07 15:57:42 -04002584 error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002585 if (error)
2586 return ERR_PTR(error);
Al Viro5c31b6c2015-05-12 17:32:54 -04002587 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002588 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002589 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002590 }
Al Viro59551022016-01-22 15:40:57 -05002591 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002592 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002593 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002594 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002595 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002596 }
Al Viro79714f72012-06-15 03:01:42 +04002597 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002598}
2599
Stephen Brennan0766ec82021-09-01 10:51:41 -07002600struct dentry *kern_path_locked(const char *name, struct path *path)
2601{
2602 struct filename *filename = getname_kernel(name);
2603 struct dentry *res = __kern_path_locked(filename, path);
2604
2605 putname(filename);
2606 return res;
2607}
2608
Al Virod1811462008-08-02 00:49:18 -04002609int kern_path(const char *name, unsigned int flags, struct path *path)
2610{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002611 struct filename *filename = getname_kernel(name);
2612 int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL);
2613
2614 putname(filename);
2615 return ret;
2616
Al Virod1811462008-08-02 00:49:18 -04002617}
Al Viro4d359502014-03-14 12:20:17 -04002618EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002619
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002620/**
2621 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2622 * @dentry: pointer to dentry of the base directory
2623 * @mnt: pointer to vfs mount of the base directory
2624 * @name: pointer to file name
2625 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002626 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002627 */
2628int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2629 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002630 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002631{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002632 struct filename *filename;
Al Viro9ad1aaa2015-05-12 16:44:39 -04002633 struct path root = {.mnt = mnt, .dentry = dentry};
Stephen Brennan794ebce2021-09-01 10:51:42 -07002634 int ret;
2635
2636 filename = getname_kernel(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002637 /* the first argument of filename_lookup() is ignored with root */
Stephen Brennan794ebce2021-09-01 10:51:42 -07002638 ret = filename_lookup(AT_FDCWD, filename, flags, path, &root);
2639 putname(filename);
2640 return ret;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002641}
Al Viro4d359502014-03-14 12:20:17 -04002642EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002643
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002644static int lookup_one_common(struct user_namespace *mnt_userns,
2645 const char *name, struct dentry *base, int len,
2646 struct qstr *this)
Al Viro3c95f0d2018-04-06 16:32:38 -04002647{
2648 this->name = name;
2649 this->len = len;
2650 this->hash = full_name_hash(base, name, len);
2651 if (!len)
2652 return -EACCES;
2653
2654 if (unlikely(name[0] == '.')) {
2655 if (len < 2 || (len == 2 && name[1] == '.'))
2656 return -EACCES;
2657 }
2658
2659 while (len--) {
2660 unsigned int c = *(const unsigned char *)name++;
2661 if (c == '/' || c == '\0')
2662 return -EACCES;
2663 }
2664 /*
2665 * See if the low-level filesystem might want
2666 * to use its own hash..
2667 */
2668 if (base->d_flags & DCACHE_OP_HASH) {
2669 int err = base->d_op->d_hash(base, this);
2670 if (err < 0)
2671 return err;
2672 }
2673
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002674 return inode_permission(mnt_userns, base->d_inode, MAY_EXEC);
Al Viro3c95f0d2018-04-06 16:32:38 -04002675}
2676
Christoph Hellwigeead1912007-10-16 23:25:38 -07002677/**
David Howells0da0b7f2018-06-15 15:19:22 +01002678 * try_lookup_one_len - filesystem helper to lookup single pathname component
2679 * @name: pathname component to lookup
2680 * @base: base directory to lookup from
2681 * @len: maximum length @len should be interpreted to
2682 *
2683 * Look up a dentry by name in the dcache, returning NULL if it does not
2684 * currently exist. The function does not try to create a dentry.
2685 *
2686 * Note that this routine is purely a helper for filesystem usage and should
2687 * not be called by generic code.
2688 *
2689 * The caller must hold base->i_mutex.
2690 */
2691struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2692{
2693 struct qstr this;
2694 int err;
2695
2696 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2697
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002698 err = lookup_one_common(&init_user_ns, name, base, len, &this);
David Howells0da0b7f2018-06-15 15:19:22 +01002699 if (err)
2700 return ERR_PTR(err);
2701
2702 return lookup_dcache(&this, base, 0);
2703}
2704EXPORT_SYMBOL(try_lookup_one_len);
2705
2706/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002707 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002708 * @name: pathname component to lookup
2709 * @base: base directory to lookup from
2710 * @len: maximum length @len should be interpreted to
2711 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002712 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002713 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002714 *
2715 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002716 */
James Morris057f6c02007-04-26 00:12:05 -07002717struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2718{
Al Viro8613a202018-04-06 16:45:33 -04002719 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002720 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002721 int err;
James Morris057f6c02007-04-26 00:12:05 -07002722
Al Viro59551022016-01-22 15:40:57 -05002723 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002724
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002725 err = lookup_one_common(&init_user_ns, name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002726 if (err)
2727 return ERR_PTR(err);
2728
Al Viro8613a202018-04-06 16:45:33 -04002729 dentry = lookup_dcache(&this, base, 0);
2730 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002731}
Al Viro4d359502014-03-14 12:20:17 -04002732EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002733
NeilBrownbbddca82016-01-07 16:08:20 -05002734/**
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002735 * lookup_one - filesystem helper to lookup single pathname component
2736 * @mnt_userns: user namespace of the mount the lookup is performed from
2737 * @name: pathname component to lookup
2738 * @base: base directory to lookup from
2739 * @len: maximum length @len should be interpreted to
2740 *
2741 * Note that this routine is purely a helper for filesystem usage and should
2742 * not be called by generic code.
2743 *
2744 * The caller must hold base->i_mutex.
2745 */
2746struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name,
2747 struct dentry *base, int len)
2748{
2749 struct dentry *dentry;
2750 struct qstr this;
2751 int err;
2752
2753 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2754
2755 err = lookup_one_common(mnt_userns, name, base, len, &this);
2756 if (err)
2757 return ERR_PTR(err);
2758
2759 dentry = lookup_dcache(&this, base, 0);
2760 return dentry ? dentry : __lookup_slow(&this, base, 0);
2761}
2762EXPORT_SYMBOL(lookup_one);
2763
2764/**
Christian Brauner00675017e2022-04-04 12:51:40 +02002765 * lookup_one_unlocked - filesystem helper to lookup single pathname component
2766 * @mnt_userns: idmapping of the mount the lookup is performed from
2767 * @name: pathname component to lookup
2768 * @base: base directory to lookup from
2769 * @len: maximum length @len should be interpreted to
2770 *
2771 * Note that this routine is purely a helper for filesystem usage and should
2772 * not be called by generic code.
2773 *
2774 * Unlike lookup_one_len, it should be called without the parent
2775 * i_mutex held, and will take the i_mutex itself if necessary.
2776 */
2777struct dentry *lookup_one_unlocked(struct user_namespace *mnt_userns,
2778 const char *name, struct dentry *base,
2779 int len)
2780{
2781 struct qstr this;
2782 int err;
2783 struct dentry *ret;
2784
2785 err = lookup_one_common(mnt_userns, name, base, len, &this);
2786 if (err)
2787 return ERR_PTR(err);
2788
2789 ret = lookup_dcache(&this, base, 0);
2790 if (!ret)
2791 ret = lookup_slow(&this, base, 0);
2792 return ret;
2793}
2794EXPORT_SYMBOL(lookup_one_unlocked);
2795
2796/**
2797 * lookup_one_positive_unlocked - filesystem helper to lookup single
2798 * pathname component
2799 * @mnt_userns: idmapping of the mount the lookup is performed from
2800 * @name: pathname component to lookup
2801 * @base: base directory to lookup from
2802 * @len: maximum length @len should be interpreted to
2803 *
2804 * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
2805 * known positive or ERR_PTR(). This is what most of the users want.
2806 *
2807 * Note that pinned negative with unlocked parent _can_ become positive at any
2808 * time, so callers of lookup_one_unlocked() need to be very careful; pinned
2809 * positives have >d_inode stable, so this one avoids such problems.
2810 *
2811 * Note that this routine is purely a helper for filesystem usage and should
2812 * not be called by generic code.
2813 *
2814 * The helper should be called without i_mutex held.
2815 */
2816struct dentry *lookup_one_positive_unlocked(struct user_namespace *mnt_userns,
2817 const char *name,
2818 struct dentry *base, int len)
2819{
2820 struct dentry *ret = lookup_one_unlocked(mnt_userns, name, base, len);
2821
2822 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
2823 dput(ret);
2824 ret = ERR_PTR(-ENOENT);
2825 }
2826 return ret;
2827}
2828EXPORT_SYMBOL(lookup_one_positive_unlocked);
2829
2830/**
NeilBrownbbddca82016-01-07 16:08:20 -05002831 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2832 * @name: pathname component to lookup
2833 * @base: base directory to lookup from
2834 * @len: maximum length @len should be interpreted to
2835 *
2836 * Note that this routine is purely a helper for filesystem usage and should
2837 * not be called by generic code.
2838 *
2839 * Unlike lookup_one_len, it should be called without the parent
2840 * i_mutex held, and will take the i_mutex itself if necessary.
2841 */
2842struct dentry *lookup_one_len_unlocked(const char *name,
2843 struct dentry *base, int len)
2844{
Christian Brauner00675017e2022-04-04 12:51:40 +02002845 return lookup_one_unlocked(&init_user_ns, name, base, len);
NeilBrownbbddca82016-01-07 16:08:20 -05002846}
2847EXPORT_SYMBOL(lookup_one_len_unlocked);
2848
Al Viro6c2d47982019-10-31 01:21:58 -04002849/*
2850 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2851 * on negatives. Returns known positive or ERR_PTR(); that's what
2852 * most of the users want. Note that pinned negative with unlocked parent
2853 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2854 * need to be very careful; pinned positives have ->d_inode stable, so
2855 * this one avoids such problems.
2856 */
2857struct dentry *lookup_positive_unlocked(const char *name,
2858 struct dentry *base, int len)
2859{
Christian Brauner00675017e2022-04-04 12:51:40 +02002860 return lookup_one_positive_unlocked(&init_user_ns, name, base, len);
Al Viro6c2d47982019-10-31 01:21:58 -04002861}
2862EXPORT_SYMBOL(lookup_positive_unlocked);
2863
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002864#ifdef CONFIG_UNIX98_PTYS
2865int path_pts(struct path *path)
2866{
2867 /* Find something mounted on "pts" in the same directory as
2868 * the input path.
2869 */
Al Viroa6a7eb72020-03-11 13:05:03 -04002870 struct dentry *parent = dget_parent(path->dentry);
2871 struct dentry *child;
Al Viro19f60282020-02-26 20:09:37 -05002872 struct qstr this = QSTR_INIT("pts", 3);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002873
Al Viroa6a7eb72020-03-11 13:05:03 -04002874 if (unlikely(!path_connected(path->mnt, parent))) {
2875 dput(parent);
Al Viro63b27722020-02-24 16:01:19 -05002876 return -ENOENT;
Al Viroa6a7eb72020-03-11 13:05:03 -04002877 }
Al Viro63b27722020-02-24 16:01:19 -05002878 dput(path->dentry);
2879 path->dentry = parent;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002880 child = d_hash_and_lookup(parent, &this);
2881 if (!child)
2882 return -ENOENT;
2883
2884 path->dentry = child;
2885 dput(parent);
Al Viro19f60282020-02-26 20:09:37 -05002886 follow_down(path);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002887 return 0;
2888}
2889#endif
2890
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002891int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2892 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002894 struct filename *filename = getname_flags(name, flags, empty);
2895 int ret = filename_lookup(dfd, filename, flags, path, NULL);
2896
2897 putname(filename);
2898 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
Al Virob853a162015-05-13 09:12:02 -04002900EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002901
Christian Braunerba73d982021-01-21 14:19:31 +01002902int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
2903 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002905 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002906
Christian Braunerba73d982021-01-21 14:19:31 +01002907 if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002909 if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002911 return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002913EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915/*
2916 * Check whether we can remove a link victim from directory dir, check
2917 * whether the type of victim is right.
2918 * 1. We can't do it if dir is read-only (done in permission())
2919 * 2. We should have write and exec permissions on dir
2920 * 3. We can't remove anything from append-only dir
2921 * 4. We can't do anything with immutable dir (done in permission())
2922 * 5. If the sticky bit on dir is set we should either
2923 * a. be owner of dir, or
2924 * b. be owner of victim, or
2925 * c. have CAP_FOWNER capability
2926 * 6. If the victim is append-only or immutable we can't do antyhing with
2927 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002928 * 7. If the victim has an unknown uid or gid we can't change the inode.
2929 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2930 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2931 * 10. We can't remove a root or mountpoint.
2932 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 * nfs_async_unlink().
2934 */
Christian Braunerba73d982021-01-21 14:19:31 +01002935static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
2936 struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
David Howells63afdfc2015-05-06 15:59:00 +01002938 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 int error;
2940
David Howellsb18825a2013-09-12 19:22:53 +01002941 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002943 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002946
2947 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01002948 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
2949 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002950 return -EOVERFLOW;
2951
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002952 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Christian Braunerba73d982021-01-21 14:19:31 +01002954 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (error)
2956 return error;
2957 if (IS_APPEND(dir))
2958 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002959
Christian Braunerba73d982021-01-21 14:19:31 +01002960 if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
2961 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
2962 HAS_UNMAPPED_ID(mnt_userns, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return -EPERM;
2964 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002965 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return -ENOTDIR;
2967 if (IS_ROOT(victim))
2968 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002969 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return -EISDIR;
2971 if (IS_DEADDIR(dir))
2972 return -ENOENT;
2973 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2974 return -EBUSY;
2975 return 0;
2976}
2977
2978/* Check whether we can create an object with dentry child in directory
2979 * dir.
2980 * 1. We can't do it if child already exists (open has special treatment for
2981 * this case, but since we are inlined it's OK)
2982 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002983 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2984 * 4. We should have write and exec permissions on dir
2985 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 */
Christian Braunerba73d982021-01-21 14:19:31 +01002987static inline int may_create(struct user_namespace *mnt_userns,
2988 struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Jeff Layton14e972b2013-05-08 10:25:58 -04002990 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (child->d_inode)
2992 return -EEXIST;
2993 if (IS_DEADDIR(dir))
2994 return -ENOENT;
Christian Brauner8e538912021-03-20 13:26:23 +01002995 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Eric W. Biederman036d5232016-07-01 12:52:06 -05002996 return -EOVERFLOW;
Christian Brauner8e538912021-03-20 13:26:23 +01002997
Christian Braunerba73d982021-01-21 14:19:31 +01002998 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001/*
3002 * p1 and p2 should be directories on the same fs.
3003 */
3004struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
3005{
3006 struct dentry *p;
3007
3008 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05003009 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return NULL;
3011 }
3012
Al Virofc640052016-04-10 01:33:30 -04003013 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003015 p = d_ancestor(p2, p1);
3016 if (p) {
Al Viro59551022016-01-22 15:40:57 -05003017 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
3018 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003019 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003022 p = d_ancestor(p1, p2);
3023 if (p) {
Al Viro59551022016-01-22 15:40:57 -05003024 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
3025 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003026 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028
Al Viro59551022016-01-22 15:40:57 -05003029 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
3030 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return NULL;
3032}
Al Viro4d359502014-03-14 12:20:17 -04003033EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035void unlock_rename(struct dentry *p1, struct dentry *p2)
3036{
Al Viro59551022016-01-22 15:40:57 -05003037 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05003039 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04003040 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042}
Al Viro4d359502014-03-14 12:20:17 -04003043EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Christian Brauner6521f892021-01-21 14:19:33 +01003045/**
3046 * vfs_create - create new file
3047 * @mnt_userns: user namespace of the mount the inode was found from
3048 * @dir: inode of @dentry
3049 * @dentry: pointer to dentry of the base directory
3050 * @mode: mode of the new file
3051 * @want_excl: whether the file must not yet exist
3052 *
3053 * Create a new file.
3054 *
3055 * If the inode has been found through an idmapped mount the user namespace of
3056 * the vfsmount must be passed through @mnt_userns. This function will then take
3057 * care to map the inode according to @mnt_userns before checking permissions.
3058 * On non-idmapped mounts or if permission checking is to be performed on the
3059 * raw inode simply passs init_user_ns.
3060 */
3061int vfs_create(struct user_namespace *mnt_userns, struct inode *dir,
3062 struct dentry *dentry, umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Christian Brauner6521f892021-01-21 14:19:33 +01003064 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (error)
3066 return error;
3067
Al Viroacfa4382008-12-04 10:06:33 -05003068 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return -EACCES; /* shouldn't it be ENOSYS? */
3070 mode &= S_IALLUGO;
3071 mode |= S_IFREG;
3072 error = security_inode_create(dir, dentry, mode);
3073 if (error)
3074 return error;
Christian Brauner549c7292021-01-21 14:19:43 +01003075 error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003076 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003077 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return error;
3079}
Al Viro4d359502014-03-14 12:20:17 -04003080EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Al Viro8e6c8482017-12-01 17:12:45 -05003082int vfs_mkobj(struct dentry *dentry, umode_t mode,
3083 int (*f)(struct dentry *, umode_t, void *),
3084 void *arg)
3085{
3086 struct inode *dir = dentry->d_parent->d_inode;
Christian Braunerba73d982021-01-21 14:19:31 +01003087 int error = may_create(&init_user_ns, dir, dentry);
Al Viro8e6c8482017-12-01 17:12:45 -05003088 if (error)
3089 return error;
3090
3091 mode &= S_IALLUGO;
3092 mode |= S_IFREG;
3093 error = security_inode_create(dir, dentry, mode);
3094 if (error)
3095 return error;
3096 error = f(dentry, mode, arg);
3097 if (!error)
3098 fsnotify_create(dir, dentry);
3099 return error;
3100}
3101EXPORT_SYMBOL(vfs_mkobj);
3102
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003103bool may_open_dev(const struct path *path)
3104{
3105 return !(path->mnt->mnt_flags & MNT_NODEV) &&
3106 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
3107}
3108
Christian Braunerba73d982021-01-21 14:19:31 +01003109static int may_open(struct user_namespace *mnt_userns, const struct path *path,
3110 int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02003112 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 struct inode *inode = dentry->d_inode;
3114 int error;
3115
3116 if (!inode)
3117 return -ENOENT;
3118
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003119 switch (inode->i_mode & S_IFMT) {
3120 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003122 case S_IFDIR:
Kees Cookfc4177b2020-08-14 17:30:14 -07003123 if (acc_mode & MAY_WRITE)
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003124 return -EISDIR;
Kees Cookfc4177b2020-08-14 17:30:14 -07003125 if (acc_mode & MAY_EXEC)
3126 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003127 break;
3128 case S_IFBLK:
3129 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003130 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return -EACCES;
Kees Cook633fb6a2020-08-11 18:36:26 -07003132 fallthrough;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003133 case S_IFIFO:
3134 case S_IFSOCK:
Kees Cook633fb6a2020-08-11 18:36:26 -07003135 if (acc_mode & MAY_EXEC)
3136 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003138 break;
Kees Cook0fd338b2020-08-11 18:36:30 -07003139 case S_IFREG:
3140 if ((acc_mode & MAY_EXEC) && path_noexec(path))
3141 return -EACCES;
3142 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08003143 }
Dave Hansenb41572e2007-10-16 23:31:14 -07003144
Christian Braunerba73d982021-01-21 14:19:31 +01003145 error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07003146 if (error)
3147 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05003148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /*
3150 * An append-only file must be opened in append mode for writing.
3151 */
3152 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05003153 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05003154 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05003156 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158
3159 /* O_NOATIME can only be set by the owner or superuser */
Christian Braunerba73d982021-01-21 14:19:31 +01003160 if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
Al Viro7715b522009-12-16 03:54:00 -05003161 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04003163 return 0;
Al Viro7715b522009-12-16 03:54:00 -05003164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Christian Brauner549c7292021-01-21 14:19:43 +01003166static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05003167{
Al Virof0bb5aa2016-11-20 20:27:12 -05003168 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05003169 struct inode *inode = path->dentry->d_inode;
3170 int error = get_write_access(inode);
3171 if (error)
3172 return error;
Jeff Layton482e0002021-10-26 11:56:45 -04003173
Jeff Laytonf7e33bd2021-08-19 14:56:38 -04003174 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05003175 if (!error) {
Christian Brauner549c7292021-01-21 14:19:43 +01003176 error = do_truncate(mnt_userns, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05003177 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003178 filp);
Al Viro7715b522009-12-16 03:54:00 -05003179 }
3180 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003181 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
Dave Hansend57999e2008-02-15 14:37:27 -08003184static inline int open_to_namei_flags(int flag)
3185{
Al Viro8a5e9292011-06-25 19:15:54 -04003186 if ((flag & O_ACCMODE) == 3)
3187 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003188 return flag;
3189}
3190
Christian Braunerba73d982021-01-21 14:19:31 +01003191static int may_o_create(struct user_namespace *mnt_userns,
3192 const struct path *dir, struct dentry *dentry,
3193 umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003194{
3195 int error = security_path_mknod(dir, dentry, mode, 0);
3196 if (error)
3197 return error;
3198
Christian Brauner8e538912021-03-20 13:26:23 +01003199 if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
Seth Forshee1328c722017-01-26 14:33:46 -06003200 return -EOVERFLOW;
3201
Christian Braunerba73d982021-01-21 14:19:31 +01003202 error = inode_permission(mnt_userns, dir->dentry->d_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01003203 MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003204 if (error)
3205 return error;
3206
3207 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3208}
3209
David Howells1acf0af2012-06-14 16:13:46 +01003210/*
3211 * Attempt to atomically look up, create and open a file from a negative
3212 * dentry.
3213 *
3214 * Returns 0 if successful. The file will have been created and attached to
3215 * @file by the filesystem calling finish_open().
3216 *
Al Viro00a07c12018-07-09 19:30:20 -04003217 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3218 * be set. The caller will need to perform the open themselves. @path will
3219 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003220 *
3221 * Returns an error code otherwise.
3222 */
Al Viro239eb982020-01-09 14:12:40 -05003223static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3224 struct file *file,
Al Viro239eb982020-01-09 14:12:40 -05003225 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003226{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003227 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003228 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003229 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003230
Miklos Szeredid18e9002012-06-05 15:10:17 +02003231 if (nd->flags & LOOKUP_DIRECTORY)
3232 open_flag |= O_DIRECTORY;
3233
Al Viro30d904942012-06-22 12:40:19 +04003234 file->f_path.dentry = DENTRY_NOT_SET;
3235 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003236 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003237 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003238 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003239 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003240 if (file->f_mode & FMODE_OPENED) {
Al Viro6fb968c2020-01-26 09:53:19 -05003241 if (unlikely(dentry != file->f_path.dentry)) {
3242 dput(dentry);
3243 dentry = dget(file->f_path.dentry);
3244 }
Al Viro64e1ac42018-07-09 19:17:52 -04003245 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003246 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003247 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003248 if (file->f_path.dentry) {
3249 dput(dentry);
3250 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003251 }
Al Viro239eb982020-01-09 14:12:40 -05003252 if (unlikely(d_is_negative(dentry)))
Al Viroa01e7182016-06-07 21:53:51 -04003253 error = -ENOENT;
Sage Weil62b2ce92012-08-15 13:30:12 -07003254 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003255 }
Al Viro239eb982020-01-09 14:12:40 -05003256 if (error) {
3257 dput(dentry);
3258 dentry = ERR_PTR(error);
3259 }
3260 return dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003261}
3262
Nick Piggin31e6b012011-01-07 17:49:52 +11003263/*
David Howells1acf0af2012-06-14 16:13:46 +01003264 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003265 *
Al Viro00a07c12018-07-09 19:30:20 -04003266 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003267 *
Al Viro00a07c12018-07-09 19:30:20 -04003268 * Returns 0 on success, that is, if
3269 * the file was successfully atomically created (if necessary) and opened, or
3270 * the file was not completely opened at this time, though lookups and
3271 * creations were performed.
3272 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3273 * In the latter case dentry returned in @path might be negative if O_CREAT
3274 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003275 *
Al Viro00a07c12018-07-09 19:30:20 -04003276 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003277 */
Al Viroda5ebf52020-01-09 14:25:14 -05003278static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3279 const struct open_flags *op,
3280 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003281{
Christian Brauner549c7292021-01-21 14:19:43 +01003282 struct user_namespace *mnt_userns;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003283 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003284 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003285 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003286 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003287 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003288 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003289 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003290
Al Viroce8644f2016-04-26 14:17:56 -04003291 if (unlikely(IS_DEADDIR(dir_inode)))
Al Viroda5ebf52020-01-09 14:25:14 -05003292 return ERR_PTR(-ENOENT);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003293
Al Viro73a09dd2018-06-08 13:22:02 -04003294 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003295 dentry = d_lookup(dir, &nd->last);
3296 for (;;) {
3297 if (!dentry) {
3298 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3299 if (IS_ERR(dentry))
Al Viroda5ebf52020-01-09 14:25:14 -05003300 return dentry;
Al Viro6fbd0712016-04-28 11:50:59 -04003301 }
3302 if (d_in_lookup(dentry))
3303 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003304
Al Viro6fbd0712016-04-28 11:50:59 -04003305 error = d_revalidate(dentry, nd->flags);
3306 if (likely(error > 0))
3307 break;
3308 if (error)
3309 goto out_dput;
3310 d_invalidate(dentry);
3311 dput(dentry);
3312 dentry = NULL;
3313 }
3314 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003315 /* Cached positive dentry: will open in f_op->open */
Al Viroda5ebf52020-01-09 14:25:14 -05003316 return dentry;
Al Viro6c51e512016-03-05 20:09:32 -05003317 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003318
Al Viro1643b432016-04-27 19:14:10 -04003319 /*
3320 * Checking write permission is tricky, bacuse we don't know if we are
3321 * going to actually need it: O_CREAT opens should work as long as the
3322 * file exists. But checking existence breaks atomicity. The trick is
3323 * to check access and if not granted clear O_CREAT from the flags.
3324 *
3325 * Another problem is returing the "right" error value (e.g. for an
3326 * O_EXCL open we want to return EEXIST not EROFS).
3327 */
Al Viro99a4a902020-03-12 14:07:27 -04003328 if (unlikely(!got_write))
3329 open_flag &= ~O_TRUNC;
Christian Brauner549c7292021-01-21 14:19:43 +01003330 mnt_userns = mnt_user_ns(nd->path.mnt);
Al Viro1643b432016-04-27 19:14:10 -04003331 if (open_flag & O_CREAT) {
Al Viro99a4a902020-03-12 14:07:27 -04003332 if (open_flag & O_EXCL)
3333 open_flag &= ~O_TRUNC;
Al Viro1643b432016-04-27 19:14:10 -04003334 if (!IS_POSIXACL(dir->d_inode))
3335 mode &= ~current_umask();
Al Viro99a4a902020-03-12 14:07:27 -04003336 if (likely(got_write))
Christian Brauner549c7292021-01-21 14:19:43 +01003337 create_error = may_o_create(mnt_userns, &nd->path,
Christian Braunerba73d982021-01-21 14:19:31 +01003338 dentry, mode);
Al Viro99a4a902020-03-12 14:07:27 -04003339 else
3340 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003341 }
Al Viro99a4a902020-03-12 14:07:27 -04003342 if (create_error)
3343 open_flag &= ~O_CREAT;
Al Viro1643b432016-04-27 19:14:10 -04003344 if (dir_inode->i_op->atomic_open) {
Al Virod489cf92020-03-11 08:07:53 -04003345 dentry = atomic_open(nd, dentry, file, open_flag, mode);
Al Viroda5ebf52020-01-09 14:25:14 -05003346 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3347 dentry = ERR_PTR(create_error);
3348 return dentry;
Al Viro1643b432016-04-27 19:14:10 -04003349 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003350
Al Viro6fbd0712016-04-28 11:50:59 -04003351 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003352 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3353 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003354 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003355 if (unlikely(res)) {
3356 if (IS_ERR(res)) {
3357 error = PTR_ERR(res);
3358 goto out_dput;
3359 }
3360 dput(dentry);
3361 dentry = res;
3362 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003363 }
3364
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003365 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003366 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003367 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003368 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003369 if (!dir_inode->i_op->create) {
3370 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003371 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003372 }
Christian Brauner549c7292021-01-21 14:19:43 +01003373
3374 error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry,
3375 mode, open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003376 if (error)
3377 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003378 }
Al Viro1643b432016-04-27 19:14:10 -04003379 if (unlikely(create_error) && !dentry->d_inode) {
3380 error = create_error;
3381 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003382 }
Al Viroda5ebf52020-01-09 14:25:14 -05003383 return dentry;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003384
3385out_dput:
3386 dput(dentry);
Al Viroda5ebf52020-01-09 14:25:14 -05003387 return ERR_PTR(error);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003388}
3389
Al Viroc981a482020-01-26 11:06:21 -05003390static const char *open_last_lookups(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003391 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003392{
Al Viroa1e28032009-12-24 02:12:06 -05003393 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003394 int open_flag = op->open_flag;
Al Viro64894cf2012-07-31 00:53:35 +04003395 bool got_write = false;
Al Viro254cf582015-05-05 09:40:46 -04003396 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003397 struct inode *inode;
Al Viroda5ebf52020-01-09 14:25:14 -05003398 struct dentry *dentry;
Al Virob0417d22020-01-14 13:34:20 -05003399 const char *res;
Al Virofb1cc552009-12-24 01:58:28 -05003400
Al Viroc3e380b2011-02-23 13:39:45 -05003401 nd->flags |= op->intent;
3402
Al Virobc77daa2013-06-06 09:12:33 -04003403 if (nd->last_type != LAST_NORM) {
Al Viro56676ec2020-03-10 21:54:54 -04003404 if (nd->depth)
3405 put_link(nd);
Al Viroff326a32020-03-10 10:19:24 -04003406 return handle_dots(nd, nd->last_type);
Al Viro1f36f772009-12-26 10:56:19 -05003407 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003408
Al Viroca344a892011-03-09 00:36:45 -05003409 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003410 if (nd->last.name[nd->last.len])
3411 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3412 /* we _can_ be in RCU mode here */
Al Viro20e34352020-01-09 14:58:31 -05003413 dentry = lookup_fast(nd, &inode, &seq);
3414 if (IS_ERR(dentry))
Al Viro1ccac622020-01-14 10:13:40 -05003415 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05003416 if (likely(dentry))
Miklos Szeredi71574862012-06-05 15:10:14 +02003417 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003418
Al Viro6583fe22016-03-05 18:14:03 -05003419 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003420 } else {
3421 /* create side of things */
Al Viro72287412020-03-10 10:09:26 -04003422 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003423 if (!try_to_unlazy(nd))
3424 return ERR_PTR(-ECHILD);
Al Viro72287412020-03-10 10:09:26 -04003425 }
Al Viroc9b07ea2019-07-14 13:22:27 -04003426 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003427 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003428 if (unlikely(nd->last.name[nd->last.len]))
Al Viro1ccac622020-01-14 10:13:40 -05003429 return ERR_PTR(-EISDIR);
Al Virofe2d35f2011-03-05 22:58:25 -05003430 }
3431
Al Viro9cf843e2016-04-28 19:35:16 -04003432 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003433 got_write = !mnt_want_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003434 /*
3435 * do _not_ fail yet - we might not need that or fail with
3436 * a different error; let lookup_open() decide; we'll be
3437 * dropping this one anyway.
3438 */
3439 }
Al Viro9cf843e2016-04-28 19:35:16 -04003440 if (open_flag & O_CREAT)
3441 inode_lock(dir->d_inode);
3442 else
3443 inode_lock_shared(dir->d_inode);
Al Viroda5ebf52020-01-09 14:25:14 -05003444 dentry = lookup_open(nd, file, op, got_write);
Al Virof7bb9592020-03-05 13:25:20 -05003445 if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3446 fsnotify_create(dir->d_inode, dentry);
Al Viro9cf843e2016-04-28 19:35:16 -04003447 if (open_flag & O_CREAT)
3448 inode_unlock(dir->d_inode);
3449 else
3450 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003451
Al Viroc981a482020-01-26 11:06:21 -05003452 if (got_write)
Al Viro59e96e62020-01-26 10:22:24 -05003453 mnt_drop_write(nd->path.mnt);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003454
Al Viro59e96e62020-01-26 10:22:24 -05003455 if (IS_ERR(dentry))
3456 return ERR_CAST(dentry);
3457
Al Viro973d4b72020-01-26 10:48:16 -05003458 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
Al Viroe73cabf2020-01-09 14:30:08 -05003459 dput(nd->path.dentry);
3460 nd->path.dentry = dentry;
Al Viroc981a482020-01-26 11:06:21 -05003461 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05003462 }
3463
Al Viro20e34352020-01-09 14:58:31 -05003464finish_lookup:
Al Viro56676ec2020-03-10 21:54:54 -04003465 if (nd->depth)
3466 put_link(nd);
Al Viro8c4efe22020-01-19 12:44:18 -05003467 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
Al Viroff326a32020-03-10 10:19:24 -04003468 if (unlikely(res))
Al Virob0417d22020-01-14 13:34:20 -05003469 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viroff326a32020-03-10 10:19:24 -04003470 return res;
Al Viroc981a482020-01-26 11:06:21 -05003471}
3472
3473/*
3474 * Handle the last step of open()
3475 */
Al Viroc5971b82020-03-05 11:41:29 -05003476static int do_open(struct nameidata *nd,
Al Viroc981a482020-01-26 11:06:21 -05003477 struct file *file, const struct open_flags *op)
3478{
Christian Brauner549c7292021-01-21 14:19:43 +01003479 struct user_namespace *mnt_userns;
Al Viroc981a482020-01-26 11:06:21 -05003480 int open_flag = op->open_flag;
3481 bool do_truncate;
3482 int acc_mode;
Al Viroc981a482020-01-26 11:06:21 -05003483 int error;
3484
Al Viroff326a32020-03-10 10:19:24 -04003485 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3486 error = complete_walk(nd);
3487 if (error)
3488 return error;
3489 }
Al Viro973d4b72020-01-26 10:48:16 -05003490 if (!(file->f_mode & FMODE_CREATED))
3491 audit_inode(nd->name, nd->path.dentry, 0);
Christian Brauner549c7292021-01-21 14:19:43 +01003492 mnt_userns = mnt_user_ns(nd->path.mnt);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003493 if (open_flag & O_CREAT) {
Al Virob94e0b32020-03-10 10:13:53 -04003494 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3495 return -EEXIST;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003496 if (d_is_dir(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003497 return -EISDIR;
Christian Brauner549c7292021-01-21 14:19:43 +01003498 error = may_create_in_sticky(mnt_userns, nd,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003499 d_backing_inode(nd->path.dentry));
3500 if (unlikely(error))
Al Viroc5971b82020-03-05 11:41:29 -05003501 return error;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003502 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003503 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003504 return -ENOTDIR;
Al Viro6c0d46c2011-03-09 00:59:59 -05003505
Al Viro8795e7d2020-01-26 10:38:17 -05003506 do_truncate = false;
3507 acc_mode = op->acc_mode;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003508 if (file->f_mode & FMODE_CREATED) {
3509 /* Don't check for write permission, don't truncate */
3510 open_flag &= ~O_TRUNC;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003511 acc_mode = 0;
Al Viro8795e7d2020-01-26 10:38:17 -05003512 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
Al Viro0f9d1a12011-03-09 00:13:14 -05003513 error = mnt_want_write(nd->path.mnt);
3514 if (error)
Al Viroc5971b82020-03-05 11:41:29 -05003515 return error;
Al Viro8795e7d2020-01-26 10:38:17 -05003516 do_truncate = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003517 }
Christian Brauner549c7292021-01-21 14:19:43 +01003518 error = may_open(mnt_userns, &nd->path, acc_mode, open_flag);
Al Viro8795e7d2020-01-26 10:38:17 -05003519 if (!error && !(file->f_mode & FMODE_OPENED))
Al Viro3ad56152020-01-26 10:06:13 -05003520 error = vfs_open(&nd->path, file);
Al Viro8795e7d2020-01-26 10:38:17 -05003521 if (!error)
3522 error = ima_file_check(file, op->acc_mode);
3523 if (!error && do_truncate)
Christian Brauner549c7292021-01-21 14:19:43 +01003524 error = handle_truncate(mnt_userns, file);
Al Viroc80567c2016-02-27 19:17:33 -05003525 if (unlikely(error > 0)) {
3526 WARN_ON(1);
3527 error = -EINVAL;
3528 }
Al Viro8795e7d2020-01-26 10:38:17 -05003529 if (do_truncate)
Al Viro0f9d1a12011-03-09 00:13:14 -05003530 mnt_drop_write(nd->path.mnt);
Al Viroc5971b82020-03-05 11:41:29 -05003531 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003532}
3533
Christian Brauner6521f892021-01-21 14:19:33 +01003534/**
3535 * vfs_tmpfile - create tmpfile
3536 * @mnt_userns: user namespace of the mount the inode was found from
3537 * @dentry: pointer to dentry of the base directory
3538 * @mode: mode of the new tmpfile
Randy Dunlap2111c3c2021-02-15 20:29:28 -08003539 * @open_flag: flags
Christian Brauner6521f892021-01-21 14:19:33 +01003540 *
3541 * Create a temporary file.
3542 *
3543 * If the inode has been found through an idmapped mount the user namespace of
3544 * the vfsmount must be passed through @mnt_userns. This function will then take
3545 * care to map the inode according to @mnt_userns before checking permissions.
3546 * On non-idmapped mounts or if permission checking is to be performed on the
3547 * raw inode simply passs init_user_ns.
3548 */
3549struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns,
3550 struct dentry *dentry, umode_t mode, int open_flag)
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003551{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003552 struct dentry *child = NULL;
3553 struct inode *dir = dentry->d_inode;
3554 struct inode *inode;
3555 int error;
3556
3557 /* we want directory to be writable */
Christian Brauner6521f892021-01-21 14:19:33 +01003558 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003559 if (error)
3560 goto out_err;
3561 error = -EOPNOTSUPP;
3562 if (!dir->i_op->tmpfile)
3563 goto out_err;
3564 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003565 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003566 if (unlikely(!child))
3567 goto out_err;
Christian Brauner549c7292021-01-21 14:19:43 +01003568 error = dir->i_op->tmpfile(mnt_userns, dir, child, mode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003569 if (error)
3570 goto out_err;
3571 error = -ENOENT;
3572 inode = child->d_inode;
3573 if (unlikely(!inode))
3574 goto out_err;
3575 if (!(open_flag & O_EXCL)) {
3576 spin_lock(&inode->i_lock);
3577 inode->i_state |= I_LINKABLE;
3578 spin_unlock(&inode->i_lock);
3579 }
Christian Braunera2d23292021-01-21 14:19:45 +01003580 ima_post_create_tmpfile(mnt_userns, inode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003581 return child;
3582
3583out_err:
3584 dput(child);
3585 return ERR_PTR(error);
3586}
3587EXPORT_SYMBOL(vfs_tmpfile);
3588
Al Viroc8a53ee2015-05-12 18:43:07 -04003589static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003590 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003591 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003592{
Christian Brauner6521f892021-01-21 14:19:33 +01003593 struct user_namespace *mnt_userns;
Al Viro625b6d12015-05-12 16:36:12 -04003594 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003595 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003596 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003597 if (unlikely(error))
3598 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003599 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003600 if (unlikely(error))
3601 goto out;
Christian Brauner6521f892021-01-21 14:19:33 +01003602 mnt_userns = mnt_user_ns(path.mnt);
3603 child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003604 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003605 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003606 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003607 dput(path.dentry);
3608 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003609 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003610 /* Don't check for other permissions, the inode was just created */
Christian Brauner549c7292021-01-21 14:19:43 +01003611 error = may_open(mnt_userns, &path, 0, op->open_flag);
Al Viro1e8f44f2020-03-11 17:22:19 -04003612 if (!error)
3613 error = vfs_open(&path, file);
Al Viro60545d02013-06-07 01:20:27 -04003614out2:
Al Viro625b6d12015-05-12 16:36:12 -04003615 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003616out:
Al Viro625b6d12015-05-12 16:36:12 -04003617 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003618 return error;
3619}
3620
Al Viro6ac08702016-04-26 00:02:50 -04003621static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3622{
3623 struct path path;
3624 int error = path_lookupat(nd, flags, &path);
3625 if (!error) {
3626 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003627 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003628 path_put(&path);
3629 }
3630 return error;
3631}
3632
Al Viroc8a53ee2015-05-12 18:43:07 -04003633static struct file *path_openat(struct nameidata *nd,
3634 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
Al Viro30d904942012-06-22 12:40:19 +04003636 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003637 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003638
Al Viroea73ea72018-07-11 15:00:04 -04003639 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003640 if (IS_ERR(file))
3641 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003642
Al Virobb458c62013-07-13 13:26:37 +04003643 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003644 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003645 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003646 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003647 } else {
3648 const char *s = path_init(nd, flags);
3649 while (!(error = link_path_walk(s, nd)) &&
Al Viroc5971b82020-03-05 11:41:29 -05003650 (s = open_last_lookups(nd, file, op)) != NULL)
Al Viro1ccac622020-01-14 10:13:40 -05003651 ;
Al Viroc5971b82020-03-05 11:41:29 -05003652 if (!error)
3653 error = do_open(nd, file, op);
Al Viro5f336e72018-07-09 16:38:06 -04003654 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003655 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003656 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003657 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003658 return file;
3659 WARN_ON(1);
3660 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003661 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003662 fput(file);
3663 if (error == -EOPENSTALE) {
3664 if (flags & LOOKUP_RCU)
3665 error = -ECHILD;
3666 else
3667 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003668 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003669 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Jeff Layton669abf42012-10-10 16:43:10 -04003672struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003673 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003674{
Al Viro9883d182015-05-13 07:28:08 -04003675 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003676 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003677 struct file *filp;
3678
Al Viro06422962021-04-01 22:28:03 -04003679 set_nameidata(&nd, dfd, pathname, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04003680 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003681 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003682 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003683 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003684 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003685 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003686 return filp;
3687}
3688
Al Viroffb37ca2021-04-01 19:00:57 -04003689struct file *do_file_open_root(const struct path *root,
Al Virof9652e12013-06-11 08:23:01 +04003690 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003691{
Al Viro9883d182015-05-13 07:28:08 -04003692 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003693 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003694 struct filename *filename;
Al Virobcba1e72021-04-01 22:03:41 -04003695 int flags = op->lookup_flags;
Al Viro73d049a2011-03-11 12:08:24 -05003696
Al Viroffb37ca2021-04-01 19:00:57 -04003697 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003698 return ERR_PTR(-ELOOP);
3699
Paul Moore51689102015-01-22 00:00:03 -05003700 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303701 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003702 return ERR_CAST(filename);
3703
Al Viro06422962021-04-01 22:28:03 -04003704 set_nameidata(&nd, -1, filename, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04003705 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003706 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003707 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003708 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003709 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003710 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003711 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003712 return file;
3713}
3714
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003715static struct dentry *filename_create(int dfd, struct filename *name,
3716 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003718 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003719 struct qstr last;
NeilBrownb3d46502022-04-14 13:57:35 +10003720 bool want_dir = lookup_flags & LOOKUP_DIRECTORY;
3721 unsigned int reval_flag = lookup_flags & LOOKUP_REVAL;
3722 unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro391172c2015-05-09 11:19:16 -04003723 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003724 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003725 int error;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003726
NeilBrownb3d46502022-04-14 13:57:35 +10003727 error = filename_parentat(dfd, name, reval_flag, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07003728 if (error)
3729 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003731 /*
3732 * Yucky last component or no last component at all?
3733 * (foo/., foo/.., /////)
3734 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003735 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003736 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003737
Jan Karac30dabf2012-06-12 16:20:30 +02003738 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003739 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003740 /*
NeilBrownb3d46502022-04-14 13:57:35 +10003741 * Do the final lookup. Suppress 'create' if there is a trailing
3742 * '/', and a directory wasn't requested.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003743 */
NeilBrownb3d46502022-04-14 13:57:35 +10003744 if (last.name[last.len] && !want_dir)
3745 create_flags = 0;
Al Viro59551022016-01-22 15:40:57 -05003746 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
NeilBrownb3d46502022-04-14 13:57:35 +10003747 dentry = __lookup_hash(&last, path->dentry, reval_flag | create_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003749 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003750
Al Viroa8104a92012-07-20 02:25:00 +04003751 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003752 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003753 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003754
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003755 /*
3756 * Special case - lookup gave negative, but... we had foo/bar/
3757 * From the vfs_mknod() POV we just have a negative dentry -
3758 * all is fine. Let's be bastards - you had / on the end, you've
3759 * been asking for (non-existent) directory. -ENOENT for you.
3760 */
NeilBrownb3d46502022-04-14 13:57:35 +10003761 if (unlikely(!create_flags)) {
Al Viroa8104a92012-07-20 02:25:00 +04003762 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003763 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003764 }
Jan Karac30dabf2012-06-12 16:20:30 +02003765 if (unlikely(err2)) {
3766 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003767 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770fail:
Al Viroa8104a92012-07-20 02:25:00 +04003771 dput(dentry);
3772 dentry = ERR_PTR(error);
3773unlock:
Al Viro59551022016-01-22 15:40:57 -05003774 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003775 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003776 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003777out:
Al Viro391172c2015-05-09 11:19:16 -04003778 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 return dentry;
3780}
Al Virofa14a0b2015-01-22 02:16:49 -05003781
3782struct dentry *kern_path_create(int dfd, const char *pathname,
3783 struct path *path, unsigned int lookup_flags)
3784{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003785 struct filename *filename = getname_kernel(pathname);
3786 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3787
3788 putname(filename);
3789 return res;
Al Virofa14a0b2015-01-22 02:16:49 -05003790}
Al Virodae6ad82011-06-26 11:50:15 -04003791EXPORT_SYMBOL(kern_path_create);
3792
Al Viro921a1652012-07-20 01:15:31 +04003793void done_path_create(struct path *path, struct dentry *dentry)
3794{
3795 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003796 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003797 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003798 path_put(path);
3799}
3800EXPORT_SYMBOL(done_path_create);
3801
Al Viro520ae682015-05-13 07:00:28 -04003802inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003803 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003804{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003805 struct filename *filename = getname(pathname);
3806 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3807
3808 putname(filename);
3809 return res;
Al Virodae6ad82011-06-26 11:50:15 -04003810}
3811EXPORT_SYMBOL(user_path_create);
3812
Christian Brauner6521f892021-01-21 14:19:33 +01003813/**
3814 * vfs_mknod - create device node or file
3815 * @mnt_userns: user namespace of the mount the inode was found from
3816 * @dir: inode of @dentry
3817 * @dentry: pointer to dentry of the base directory
3818 * @mode: mode of the new device node or file
3819 * @dev: device number of device to create
3820 *
3821 * Create a device node or file.
3822 *
3823 * If the inode has been found through an idmapped mount the user namespace of
3824 * the vfsmount must be passed through @mnt_userns. This function will then take
3825 * care to map the inode according to @mnt_userns before checking permissions.
3826 * On non-idmapped mounts or if permission checking is to be performed on the
3827 * raw inode simply passs init_user_ns.
3828 */
3829int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
3830 struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003832 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
Christian Brauner6521f892021-01-21 14:19:33 +01003833 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 if (error)
3836 return error;
3837
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003838 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3839 !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 return -EPERM;
3841
Al Viroacfa4382008-12-04 10:06:33 -05003842 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return -EPERM;
3844
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003845 error = devcgroup_inode_mknod(mode, dev);
3846 if (error)
3847 return error;
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 error = security_inode_mknod(dir, dentry, mode, dev);
3850 if (error)
3851 return error;
3852
Christian Brauner549c7292021-01-21 14:19:43 +01003853 error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003854 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003855 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return error;
3857}
Al Viro4d359502014-03-14 12:20:17 -04003858EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Al Virof69aac02011-07-26 04:31:40 -04003860static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003861{
3862 switch (mode & S_IFMT) {
3863 case S_IFREG:
3864 case S_IFCHR:
3865 case S_IFBLK:
3866 case S_IFIFO:
3867 case S_IFSOCK:
3868 case 0: /* zero mode translates to S_IFREG */
3869 return 0;
3870 case S_IFDIR:
3871 return -EPERM;
3872 default:
3873 return -EINVAL;
3874 }
3875}
3876
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003877static int do_mknodat(int dfd, struct filename *name, umode_t mode,
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003878 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Christian Brauner6521f892021-01-21 14:19:33 +01003880 struct user_namespace *mnt_userns;
Al Viro2ad94ae2008-07-21 09:32:51 -04003881 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003882 struct path path;
3883 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003884 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Al Viro8e4bfca2012-07-20 01:17:26 +04003886 error = may_mknod(mode);
3887 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003888 goto out1;
Jeff Layton972567f2012-12-20 16:00:10 -05003889retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003890 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev77972512021-07-08 13:34:40 +07003891 error = PTR_ERR(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003892 if (IS_ERR(dentry))
Dmitry Kadashev77972512021-07-08 13:34:40 +07003893 goto out1;
Al Viro2ad94ae2008-07-21 09:32:51 -04003894
Al Virodae6ad82011-06-26 11:50:15 -04003895 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003896 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003897 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003898 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003899 goto out2;
Christian Brauner6521f892021-01-21 14:19:33 +01003900
3901 mnt_userns = mnt_user_ns(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003902 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 case 0: case S_IFREG:
Christian Brauner6521f892021-01-21 14:19:33 +01003904 error = vfs_create(mnt_userns, path.dentry->d_inode,
3905 dentry, mode, true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003906 if (!error)
Christian Braunera2d23292021-01-21 14:19:45 +01003907 ima_post_path_mknod(mnt_userns, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 break;
3909 case S_IFCHR: case S_IFBLK:
Christian Brauner6521f892021-01-21 14:19:33 +01003910 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3911 dentry, mode, new_decode_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 break;
3913 case S_IFIFO: case S_IFSOCK:
Christian Brauner6521f892021-01-21 14:19:33 +01003914 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3915 dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003918out2:
Al Viro921a1652012-07-20 01:15:31 +04003919 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003920 if (retry_estale(error, lookup_flags)) {
3921 lookup_flags |= LOOKUP_REVAL;
3922 goto retry;
3923 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003924out1:
3925 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 return error;
3927}
3928
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003929SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3930 unsigned int, dev)
3931{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003932 return do_mknodat(dfd, getname(filename), mode, dev);
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003933}
3934
Al Viro8208a222011-07-25 17:32:17 -04003935SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003936{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003937 return do_mknodat(AT_FDCWD, getname(filename), mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003938}
3939
Christian Brauner6521f892021-01-21 14:19:33 +01003940/**
3941 * vfs_mkdir - create directory
3942 * @mnt_userns: user namespace of the mount the inode was found from
3943 * @dir: inode of @dentry
3944 * @dentry: pointer to dentry of the base directory
3945 * @mode: mode of the new directory
3946 *
3947 * Create a directory.
3948 *
3949 * If the inode has been found through an idmapped mount the user namespace of
3950 * the vfsmount must be passed through @mnt_userns. This function will then take
3951 * care to map the inode according to @mnt_userns before checking permissions.
3952 * On non-idmapped mounts or if permission checking is to be performed on the
3953 * raw inode simply passs init_user_ns.
3954 */
3955int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
3956 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
Christian Brauner6521f892021-01-21 14:19:33 +01003958 int error = may_create(mnt_userns, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003959 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
3961 if (error)
3962 return error;
3963
Al Viroacfa4382008-12-04 10:06:33 -05003964 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return -EPERM;
3966
3967 mode &= (S_IRWXUGO|S_ISVTX);
3968 error = security_inode_mkdir(dir, dentry, mode);
3969 if (error)
3970 return error;
3971
Al Viro8de52772012-02-06 12:45:27 -05003972 if (max_links && dir->i_nlink >= max_links)
3973 return -EMLINK;
3974
Christian Brauner549c7292021-01-21 14:19:43 +01003975 error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003976 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003977 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return error;
3979}
Al Viro4d359502014-03-14 12:20:17 -04003980EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003982int do_mkdirat(int dfd, struct filename *name, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
Dave Hansen6902d922006-09-30 23:29:01 -07003984 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003985 struct path path;
3986 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003987 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003989retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003990 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003991 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003992 if (IS_ERR(dentry))
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003993 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003994
Al Virodae6ad82011-06-26 11:50:15 -04003995 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003996 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003997 error = security_path_mkdir(&path, dentry, mode);
Christian Brauner6521f892021-01-21 14:19:33 +01003998 if (!error) {
3999 struct user_namespace *mnt_userns;
4000 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01004001 error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry,
4002 mode);
Christian Brauner6521f892021-01-21 14:19:33 +01004003 }
Al Viro921a1652012-07-20 01:15:31 +04004004 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05004005 if (retry_estale(error, lookup_flags)) {
4006 lookup_flags |= LOOKUP_REVAL;
4007 goto retry;
4008 }
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004009out_putname:
4010 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 return error;
4012}
4013
Dominik Brodowski0101db72018-03-11 11:34:49 +01004014SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
4015{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004016 return do_mkdirat(dfd, getname(pathname), mode);
Dominik Brodowski0101db72018-03-11 11:34:49 +01004017}
4018
Al Viroa218d0f2011-11-21 14:59:34 -05004019SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004020{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004021 return do_mkdirat(AT_FDCWD, getname(pathname), mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004022}
4023
Christian Brauner6521f892021-01-21 14:19:33 +01004024/**
4025 * vfs_rmdir - remove directory
4026 * @mnt_userns: user namespace of the mount the inode was found from
4027 * @dir: inode of @dentry
4028 * @dentry: pointer to dentry of the base directory
4029 *
4030 * Remove a directory.
4031 *
4032 * If the inode has been found through an idmapped mount the user namespace of
4033 * the vfsmount must be passed through @mnt_userns. This function will then take
4034 * care to map the inode according to @mnt_userns before checking permissions.
4035 * On non-idmapped mounts or if permission checking is to be performed on the
4036 * raw inode simply passs init_user_ns.
4037 */
4038int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
4039 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
Christian Brauner6521f892021-01-21 14:19:33 +01004041 int error = may_delete(mnt_userns, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 if (error)
4044 return error;
4045
Al Viroacfa4382008-12-04 10:06:33 -05004046 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return -EPERM;
4048
Al Viro1d2ef592011-09-14 18:55:41 +01004049 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05004050 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Sage Weil912dbc12011-05-24 13:06:11 -07004052 error = -EBUSY;
David Howells1bd9c4e2021-11-18 08:58:08 +00004053 if (is_local_mountpoint(dentry) ||
4054 (dentry->d_inode->i_flags & S_KERNEL_FILE))
Sage Weil912dbc12011-05-24 13:06:11 -07004055 goto out;
4056
4057 error = security_inode_rmdir(dir, dentry);
4058 if (error)
4059 goto out;
4060
4061 error = dir->i_op->rmdir(dir, dentry);
4062 if (error)
4063 goto out;
4064
Al Viro87677122018-05-27 16:23:51 -04004065 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004066 dentry->d_inode->i_flags |= S_DEAD;
4067 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004068 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004069
4070out:
Al Viro59551022016-01-22 15:40:57 -05004071 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01004072 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004073 if (!error)
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004074 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return error;
4076}
Al Viro4d359502014-03-14 12:20:17 -04004077EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004079int do_rmdir(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
Christian Brauner6521f892021-01-21 14:19:33 +01004081 struct user_namespace *mnt_userns;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004082 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004084 struct path path;
4085 struct qstr last;
4086 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05004087 unsigned int lookup_flags = 0;
4088retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004089 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004090 if (error)
4091 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Al Virof5beed72015-04-30 16:09:11 -04004093 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004094 case LAST_DOTDOT:
4095 error = -ENOTEMPTY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004096 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004097 case LAST_DOT:
4098 error = -EINVAL;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004099 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004100 case LAST_ROOT:
4101 error = -EBUSY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004102 goto exit2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004104
Al Virof5beed72015-04-30 16:09:11 -04004105 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004106 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004107 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004108
Al Viro59551022016-01-22 15:40:57 -05004109 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004110 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004112 if (IS_ERR(dentry))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004113 goto exit3;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004114 if (!dentry->d_inode) {
4115 error = -ENOENT;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004116 goto exit4;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004117 }
Al Virof5beed72015-04-30 16:09:11 -04004118 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004119 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004120 goto exit4;
Christian Brauner6521f892021-01-21 14:19:33 +01004121 mnt_userns = mnt_user_ns(path.mnt);
4122 error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004123exit4:
Dave Hansen6902d922006-09-30 23:29:01 -07004124 dput(dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004125exit3:
Al Viro59551022016-01-22 15:40:57 -05004126 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04004127 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004128exit2:
Al Virof5beed72015-04-30 16:09:11 -04004129 path_put(&path);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05004130 if (retry_estale(error, lookup_flags)) {
4131 lookup_flags |= LOOKUP_REVAL;
4132 goto retry;
4133 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004134exit1:
Al Viro24fb33d2020-08-12 05:15:18 +01004135 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return error;
4137}
4138
Heiko Carstens3cdad422009-01-14 14:14:22 +01004139SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004140{
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004141 return do_rmdir(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004142}
4143
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004144/**
4145 * vfs_unlink - unlink a filesystem object
Christian Brauner6521f892021-01-21 14:19:33 +01004146 * @mnt_userns: user namespace of the mount the inode was found from
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004147 * @dir: parent directory
4148 * @dentry: victim
4149 * @delegated_inode: returns victim inode, if the inode is delegated.
4150 *
4151 * The caller must hold dir->i_mutex.
4152 *
4153 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4154 * return a reference to the inode in delegated_inode. The caller
4155 * should then break the delegation on that inode and retry. Because
4156 * breaking a delegation may take a long time, the caller should drop
4157 * dir->i_mutex before doing so.
4158 *
4159 * Alternatively, a caller may pass NULL for delegated_inode. This may
4160 * be appropriate for callers that expect the underlying filesystem not
4161 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004162 *
4163 * If the inode has been found through an idmapped mount the user namespace of
4164 * the vfsmount must be passed through @mnt_userns. This function will then take
4165 * care to map the inode according to @mnt_userns before checking permissions.
4166 * On non-idmapped mounts or if permission checking is to be performed on the
4167 * raw inode simply passs init_user_ns.
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004168 */
Christian Brauner6521f892021-01-21 14:19:33 +01004169int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
4170 struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004172 struct inode *target = dentry->d_inode;
Christian Brauner6521f892021-01-21 14:19:33 +01004173 int error = may_delete(mnt_userns, dir, dentry, 0);
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->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return -EPERM;
4180
Al Viro59551022016-01-22 15:40:57 -05004181 inode_lock(target);
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004182 if (IS_SWAPFILE(target))
4183 error = -EPERM;
4184 else if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 error = -EBUSY;
4186 else {
4187 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004188 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004189 error = try_break_deleg(target, delegated_inode);
4190 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004193 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004194 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004195 detach_mounts(dentry);
4196 }
Al Virobec10522010-03-03 14:12:08 -05004197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004199out:
Al Viro59551022016-01-22 15:40:57 -05004200 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
4202 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004203 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
4204 fsnotify_unlink(dir, dentry);
4205 } else if (!error) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004206 fsnotify_link_count(target);
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004207 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
Robert Love0eeca282005-07-12 17:06:03 -04004209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 return error;
4211}
Al Viro4d359502014-03-14 12:20:17 -04004212EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
4214/*
4215 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004216 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 * writeout happening, and we don't want to prevent access to the directory
4218 * while waiting on the I/O.
4219 */
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004220int do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
Al Viro2ad94ae2008-07-21 09:32:51 -04004222 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004224 struct path path;
4225 struct qstr last;
4226 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004228 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004229 unsigned int lookup_flags = 0;
4230retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004231 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004232 if (error)
4233 goto exit1;
Al Viro2ad94ae2008-07-21 09:32:51 -04004234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004236 if (type != LAST_NORM)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004237 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004238
Al Virof5beed72015-04-30 16:09:11 -04004239 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004240 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004241 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004242retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004243 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004244 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 error = PTR_ERR(dentry);
4246 if (!IS_ERR(dentry)) {
Christian Brauner6521f892021-01-21 14:19:33 +01004247 struct user_namespace *mnt_userns;
4248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004250 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004251 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004253 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004254 goto slashes;
4255 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004256 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004257 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004258 goto exit3;
Christian Brauner6521f892021-01-21 14:19:33 +01004259 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01004260 error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
4261 &delegated_inode);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004262exit3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 dput(dentry);
4264 }
Al Viro59551022016-01-22 15:40:57 -05004265 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 if (inode)
4267 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004268 inode = NULL;
4269 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004270 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004271 if (!error)
4272 goto retry_deleg;
4273 }
Al Virof5beed72015-04-30 16:09:11 -04004274 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004275exit2:
Al Virof5beed72015-04-30 16:09:11 -04004276 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004277 if (retry_estale(error, lookup_flags)) {
4278 lookup_flags |= LOOKUP_REVAL;
4279 inode = NULL;
4280 goto retry;
4281 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004282exit1:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004283 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 return error;
4285
4286slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004287 if (d_is_negative(dentry))
4288 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004289 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004290 error = -EISDIR;
4291 else
4292 error = -ENOTDIR;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004293 goto exit3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004296SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004297{
4298 if ((flag & ~AT_REMOVEDIR) != 0)
4299 return -EINVAL;
4300
4301 if (flag & AT_REMOVEDIR)
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004302 return do_rmdir(dfd, getname(pathname));
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004303 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004304}
4305
Heiko Carstens3480b252009-01-14 14:14:16 +01004306SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004307{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004308 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004309}
4310
Christian Brauner6521f892021-01-21 14:19:33 +01004311/**
4312 * vfs_symlink - create symlink
4313 * @mnt_userns: user namespace of the mount the inode was found from
4314 * @dir: inode of @dentry
4315 * @dentry: pointer to dentry of the base directory
4316 * @oldname: name of the file to link to
4317 *
4318 * Create a symlink.
4319 *
4320 * If the inode has been found through an idmapped mount the user namespace of
4321 * the vfsmount must be passed through @mnt_userns. This function will then take
4322 * care to map the inode according to @mnt_userns before checking permissions.
4323 * On non-idmapped mounts or if permission checking is to be performed on the
4324 * raw inode simply passs init_user_ns.
4325 */
4326int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
4327 struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328{
Christian Brauner6521f892021-01-21 14:19:33 +01004329 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 if (error)
4332 return error;
4333
Al Viroacfa4382008-12-04 10:06:33 -05004334 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return -EPERM;
4336
4337 error = security_inode_symlink(dir, dentry, oldname);
4338 if (error)
4339 return error;
4340
Christian Brauner549c7292021-01-21 14:19:43 +01004341 error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004342 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004343 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return error;
4345}
Al Viro4d359502014-03-14 12:20:17 -04004346EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07004348int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
Al Viro2ad94ae2008-07-21 09:32:51 -04004350 int error;
Dave Hansen6902d922006-09-30 23:29:01 -07004351 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004352 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004353 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004355 if (IS_ERR(from)) {
4356 error = PTR_ERR(from);
4357 goto out_putnames;
4358 }
Jeff Laytonf46d3562012-12-11 12:10:08 -05004359retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004360 dentry = filename_create(newdfd, to, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004361 error = PTR_ERR(dentry);
4362 if (IS_ERR(dentry))
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004363 goto out_putnames;
Dave Hansen6902d922006-09-30 23:29:01 -07004364
Jeff Layton91a27b22012-10-10 15:25:28 -04004365 error = security_path_symlink(&path, dentry, from->name);
Christian Brauner6521f892021-01-21 14:19:33 +01004366 if (!error) {
4367 struct user_namespace *mnt_userns;
4368
4369 mnt_userns = mnt_user_ns(path.mnt);
4370 error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry,
4371 from->name);
4372 }
Al Viro921a1652012-07-20 01:15:31 +04004373 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004374 if (retry_estale(error, lookup_flags)) {
4375 lookup_flags |= LOOKUP_REVAL;
4376 goto retry;
4377 }
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004378out_putnames:
4379 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 putname(from);
4381 return error;
4382}
4383
Dominik Brodowskib724e842018-03-11 11:34:49 +01004384SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4385 int, newdfd, const char __user *, newname)
4386{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004387 return do_symlinkat(getname(oldname), newdfd, getname(newname));
Dominik Brodowskib724e842018-03-11 11:34:49 +01004388}
4389
Heiko Carstens3480b252009-01-14 14:14:16 +01004390SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004391{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004392 return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004393}
4394
J. Bruce Fields146a8592011-09-20 17:14:31 -04004395/**
4396 * vfs_link - create a new link
4397 * @old_dentry: object to be linked
Christian Brauner6521f892021-01-21 14:19:33 +01004398 * @mnt_userns: the user namespace of the mount
J. Bruce Fields146a8592011-09-20 17:14:31 -04004399 * @dir: new parent
4400 * @new_dentry: where to create the new link
4401 * @delegated_inode: returns inode needing a delegation break
4402 *
4403 * The caller must hold dir->i_mutex
4404 *
4405 * If vfs_link discovers a delegation on the to-be-linked file in need
4406 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4407 * inode in delegated_inode. The caller should then break the delegation
4408 * and retry. Because breaking a delegation may take a long time, the
4409 * caller should drop the i_mutex before doing so.
4410 *
4411 * Alternatively, a caller may pass NULL for delegated_inode. This may
4412 * be appropriate for callers that expect the underlying filesystem not
4413 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004414 *
4415 * If the inode has been found through an idmapped mount the user namespace of
4416 * the vfsmount must be passed through @mnt_userns. This function will then take
4417 * care to map the inode according to @mnt_userns before checking permissions.
4418 * On non-idmapped mounts or if permission checking is to be performed on the
4419 * raw inode simply passs init_user_ns.
J. Bruce Fields146a8592011-09-20 17:14:31 -04004420 */
Christian Brauner6521f892021-01-21 14:19:33 +01004421int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns,
4422 struct inode *dir, struct dentry *new_dentry,
4423 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
4425 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004426 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 int error;
4428
4429 if (!inode)
4430 return -ENOENT;
4431
Christian Brauner6521f892021-01-21 14:19:33 +01004432 error = may_create(mnt_userns, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 if (error)
4434 return error;
4435
4436 if (dir->i_sb != inode->i_sb)
4437 return -EXDEV;
4438
4439 /*
4440 * A link to an append-only or immutable file cannot be created.
4441 */
4442 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4443 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004444 /*
4445 * Updating the link count will likely cause i_uid and i_gid to
4446 * be writen back improperly if their true value is unknown to
4447 * the vfs.
4448 */
Christian Brauner6521f892021-01-21 14:19:33 +01004449 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004450 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004451 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004453 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return -EPERM;
4455
4456 error = security_inode_link(old_dentry, dir, new_dentry);
4457 if (error)
4458 return error;
4459
Al Viro59551022016-01-22 15:40:57 -05004460 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304461 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004462 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304463 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004464 else if (max_links && inode->i_nlink >= max_links)
4465 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004466 else {
4467 error = try_break_deleg(inode, delegated_inode);
4468 if (!error)
4469 error = dir->i_op->link(old_dentry, dir, new_dentry);
4470 }
Al Virof4e0c302013-06-11 08:34:36 +04004471
4472 if (!error && (inode->i_state & I_LINKABLE)) {
4473 spin_lock(&inode->i_lock);
4474 inode->i_state &= ~I_LINKABLE;
4475 spin_unlock(&inode->i_lock);
4476 }
Al Viro59551022016-01-22 15:40:57 -05004477 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004478 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004479 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 return error;
4481}
Al Viro4d359502014-03-14 12:20:17 -04004482EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484/*
4485 * Hardlinks are often used in delicate situations. We avoid
4486 * security-related surprises by not following symlinks on the
4487 * newname. --KAB
4488 *
4489 * We don't follow them on the oldname either to be compatible
4490 * with linux 2.0, and to avoid hard-linking to directories
4491 * and other special files. --ADM
4492 */
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07004493int do_linkat(int olddfd, struct filename *old, int newdfd,
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004494 struct filename *new, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495{
Christian Brauner6521f892021-01-21 14:19:33 +01004496 struct user_namespace *mnt_userns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004498 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004499 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304500 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004503 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
4504 error = -EINVAL;
4505 goto out_putnames;
4506 }
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304507 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004508 * To use null names we require CAP_DAC_READ_SEARCH
4509 * This ensures that not everyone will be able to create
4510 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304511 */
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004512 if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
4513 error = -ENOENT;
4514 goto out_putnames;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004515 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004516
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304517 if (flags & AT_SYMLINK_FOLLOW)
4518 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004519retry:
Stephen Brennan794ebce2021-09-01 10:51:42 -07004520 error = filename_lookup(olddfd, old, how, &old_path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 if (error)
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004522 goto out_putnames;
Al Viro2ad94ae2008-07-21 09:32:51 -04004523
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004524 new_dentry = filename_create(newdfd, new, &new_path,
Jeff Layton442e31c2012-12-20 16:15:38 -05004525 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004527 if (IS_ERR(new_dentry))
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004528 goto out_putpath;
Al Virodae6ad82011-06-26 11:50:15 -04004529
4530 error = -EXDEV;
4531 if (old_path.mnt != new_path.mnt)
4532 goto out_dput;
Christian Brauner549c7292021-01-21 14:19:43 +01004533 mnt_userns = mnt_user_ns(new_path.mnt);
4534 error = may_linkat(mnt_userns, &old_path);
Kees Cook800179c2012-07-25 17:29:07 -07004535 if (unlikely(error))
4536 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004537 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004538 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004539 goto out_dput;
Christian Brauner6521f892021-01-21 14:19:33 +01004540 error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
4541 new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004542out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004543 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004544 if (delegated_inode) {
4545 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004546 if (!error) {
4547 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004548 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004549 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004550 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004551 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004552 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004553 how |= LOOKUP_REVAL;
4554 goto retry;
4555 }
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004556out_putpath:
Al Viro2d8f3032008-07-22 09:59:21 -04004557 path_put(&old_path);
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004558out_putnames:
4559 putname(old);
4560 putname(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 return error;
4563}
4564
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004565SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4566 int, newdfd, const char __user *, newname, int, flags)
4567{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004568 return do_linkat(olddfd, getname_uflags(oldname, flags),
4569 newdfd, getname(newname), flags);
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004570}
4571
Heiko Carstens3480b252009-01-14 14:14:16 +01004572SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004573{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004574 return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004575}
4576
Miklos Szeredibc270272014-04-01 17:08:42 +02004577/**
4578 * vfs_rename - rename a filesystem object
Randy Dunlap2111c3c2021-02-15 20:29:28 -08004579 * @rd: pointer to &struct renamedata info
Miklos Szeredibc270272014-04-01 17:08:42 +02004580 *
4581 * The caller must hold multiple mutexes--see lock_rename()).
4582 *
4583 * If vfs_rename discovers a delegation in need of breaking at either
4584 * the source or destination, it will return -EWOULDBLOCK and return a
4585 * reference to the inode in delegated_inode. The caller should then
4586 * break the delegation and retry. Because breaking a delegation may
4587 * take a long time, the caller should drop all locks before doing
4588 * so.
4589 *
4590 * Alternatively, a caller may pass NULL for delegated_inode. This may
4591 * be appropriate for callers that expect the underlying filesystem not
4592 * to be NFS exported.
4593 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 * The worst of all namespace operations - renaming directory. "Perverted"
4595 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4596 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004597 *
J. Bruce Fieldsd03b29a22014-02-17 16:52:33 -05004598 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 * b) race potential - two innocent renames can create a loop together.
4600 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004601 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004603 * c) we have to lock _four_ objects - parents and victim (if it exists),
4604 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004605 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 * whether the target exists). Solution: try to be smart with locking
4607 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004608 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 * move will be locked. Thus we can rank directories by the tree
4610 * (ancestors first) and rank all non-directories after them.
4611 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004612 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 * HOWEVER, it relies on the assumption that any object with ->lookup()
4614 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4615 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004616 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004617 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004619 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 * locking].
4621 */
Christian Brauner9fe61452021-01-21 14:19:32 +01004622int vfs_rename(struct renamedata *rd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
4624 int error;
Christian Brauner9fe61452021-01-21 14:19:32 +01004625 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
4626 struct dentry *old_dentry = rd->old_dentry;
4627 struct dentry *new_dentry = rd->new_dentry;
4628 struct inode **delegated_inode = rd->delegated_inode;
4629 unsigned int flags = rd->flags;
Miklos Szeredibc270272014-04-01 17:08:42 +02004630 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004631 struct inode *source = old_dentry->d_inode;
4632 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004633 bool new_is_dir = false;
4634 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004635 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004637 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004638 return 0;
4639
Christian Brauner6521f892021-01-21 14:19:33 +01004640 error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 if (error)
4642 return error;
4643
Miklos Szeredida1ce062014-04-01 17:08:43 +02004644 if (!target) {
Christian Brauner6521f892021-01-21 14:19:33 +01004645 error = may_create(rd->new_mnt_userns, new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004646 } else {
4647 new_is_dir = d_is_dir(new_dentry);
4648
4649 if (!(flags & RENAME_EXCHANGE))
Christian Brauner6521f892021-01-21 14:19:33 +01004650 error = may_delete(rd->new_mnt_userns, new_dir,
4651 new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004652 else
Christian Brauner6521f892021-01-21 14:19:33 +01004653 error = may_delete(rd->new_mnt_userns, new_dir,
4654 new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 if (error)
4657 return error;
4658
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004659 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 return -EPERM;
4661
Miklos Szeredibc270272014-04-01 17:08:42 +02004662 /*
4663 * If we are going to change the parent - check write permissions,
4664 * we'll need to flip '..'.
4665 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004666 if (new_dir != old_dir) {
4667 if (is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004668 error = inode_permission(rd->old_mnt_userns, source,
Christian Brauner47291ba2021-01-21 14:19:24 +01004669 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004670 if (error)
4671 return error;
4672 }
4673 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004674 error = inode_permission(rd->new_mnt_userns, target,
Christian Brauner47291ba2021-01-21 14:19:24 +01004675 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004676 if (error)
4677 return error;
4678 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004679 }
Robert Love0eeca282005-07-12 17:06:03 -04004680
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004681 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4682 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004683 if (error)
4684 return error;
4685
Al Viro49d31c22017-07-07 14:51:19 -04004686 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004687 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004688 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004689 lock_two_nondirectories(source, target);
4690 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004691 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004692
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004693 error = -EPERM;
4694 if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target)))
4695 goto out;
4696
Miklos Szeredibc270272014-04-01 17:08:42 +02004697 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004698 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004699 goto out;
4700
Miklos Szeredida1ce062014-04-01 17:08:43 +02004701 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004702 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004703 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004704 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004705 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4706 old_dir->i_nlink >= max_links)
4707 goto out;
4708 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004709 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004710 error = try_break_deleg(source, delegated_inode);
4711 if (error)
4712 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004713 }
4714 if (target && !new_is_dir) {
4715 error = try_break_deleg(target, delegated_inode);
4716 if (error)
4717 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004718 }
Christian Brauner549c7292021-01-21 14:19:43 +01004719 error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry,
4720 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004721 if (error)
4722 goto out;
4723
Miklos Szeredida1ce062014-04-01 17:08:43 +02004724 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004725 if (is_dir) {
4726 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004727 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004728 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004729 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004730 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004731 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004732 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4733 if (!(flags & RENAME_EXCHANGE))
4734 d_move(old_dentry, new_dentry);
4735 else
4736 d_exchange(old_dentry, new_dentry);
4737 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004738out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004739 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004740 unlock_two_nondirectories(source, target);
4741 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004742 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004743 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004744 if (!error) {
Al Virof4ec3a32019-04-26 13:21:24 -04004745 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004746 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4747 if (flags & RENAME_EXCHANGE) {
Al Virof4ec3a32019-04-26 13:21:24 -04004748 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004749 new_is_dir, NULL, new_dentry);
4750 }
4751 }
Al Viro49d31c22017-07-07 14:51:19 -04004752 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 return error;
4755}
Al Viro4d359502014-03-14 12:20:17 -04004756EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Jens Axboee8866632020-09-26 17:20:17 -06004758int do_renameat2(int olddfd, struct filename *from, int newdfd,
4759 struct filename *to, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
Christian Brauner9fe61452021-01-21 14:19:32 +01004761 struct renamedata rd;
Al Viro2ad94ae2008-07-21 09:32:51 -04004762 struct dentry *old_dentry, *new_dentry;
4763 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004764 struct path old_path, new_path;
4765 struct qstr old_last, new_last;
4766 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004767 struct inode *delegated_inode = NULL;
Al Virof5beed72015-04-30 16:09:11 -04004768 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004769 bool should_retry = false;
Jens Axboee8866632020-09-26 17:20:17 -06004770 int error = -EINVAL;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004771
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004772 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004773 goto put_names;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004774
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004775 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4776 (flags & RENAME_EXCHANGE))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004777 goto put_names;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004778
Al Virof5beed72015-04-30 16:09:11 -04004779 if (flags & RENAME_EXCHANGE)
4780 target_flags = 0;
4781
Jeff Laytonc6a94282012-12-11 12:10:10 -05004782retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004783 error = filename_parentat(olddfd, from, lookup_flags, &old_path,
4784 &old_last, &old_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004785 if (error)
4786 goto put_names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Al Viroc5f563f2021-09-07 15:57:42 -04004788 error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4789 &new_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004790 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 goto exit1;
4792
4793 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004794 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 goto exit2;
4796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004798 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 goto exit2;
4800
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004801 if (flags & RENAME_NOREPLACE)
4802 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004803 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 goto exit2;
4805
Al Virof5beed72015-04-30 16:09:11 -04004806 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004807 if (error)
4808 goto exit2;
4809
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004810retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004811 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Al Virof5beed72015-04-30 16:09:11 -04004813 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 error = PTR_ERR(old_dentry);
4815 if (IS_ERR(old_dentry))
4816 goto exit3;
4817 /* source must exist */
4818 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004819 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004821 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 error = PTR_ERR(new_dentry);
4823 if (IS_ERR(new_dentry))
4824 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004825 error = -EEXIST;
4826 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4827 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004828 if (flags & RENAME_EXCHANGE) {
4829 error = -ENOENT;
4830 if (d_is_negative(new_dentry))
4831 goto exit5;
4832
4833 if (!d_is_dir(new_dentry)) {
4834 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004835 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004836 goto exit5;
4837 }
4838 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004839 /* unless the source is a directory trailing slashes give -ENOTDIR */
4840 if (!d_is_dir(old_dentry)) {
4841 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004842 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004843 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004844 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004845 goto exit5;
4846 }
4847 /* source should not be ancestor of target */
4848 error = -EINVAL;
4849 if (old_dentry == trap)
4850 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004852 if (!(flags & RENAME_EXCHANGE))
4853 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 if (new_dentry == trap)
4855 goto exit5;
4856
Al Virof5beed72015-04-30 16:09:11 -04004857 error = security_path_rename(&old_path, old_dentry,
4858 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004859 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004860 goto exit5;
Christian Brauner9fe61452021-01-21 14:19:32 +01004861
4862 rd.old_dir = old_path.dentry->d_inode;
4863 rd.old_dentry = old_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004864 rd.old_mnt_userns = mnt_user_ns(old_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004865 rd.new_dir = new_path.dentry->d_inode;
4866 rd.new_dentry = new_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004867 rd.new_mnt_userns = mnt_user_ns(new_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004868 rd.delegated_inode = &delegated_inode;
4869 rd.flags = flags;
4870 error = vfs_rename(&rd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871exit5:
4872 dput(new_dentry);
4873exit4:
4874 dput(old_dentry);
4875exit3:
Al Virof5beed72015-04-30 16:09:11 -04004876 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004877 if (delegated_inode) {
4878 error = break_deleg_wait(&delegated_inode);
4879 if (!error)
4880 goto retry_deleg;
4881 }
Al Virof5beed72015-04-30 16:09:11 -04004882 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004884 if (retry_estale(error, lookup_flags))
4885 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004886 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887exit1:
Al Virof5beed72015-04-30 16:09:11 -04004888 path_put(&old_path);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004889 if (should_retry) {
4890 should_retry = false;
4891 lookup_flags |= LOOKUP_REVAL;
4892 goto retry;
4893 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004894put_names:
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004895 putname(from);
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004896 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 return error;
4898}
4899
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004900SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4901 int, newdfd, const char __user *, newname, unsigned int, flags)
4902{
Jens Axboee8866632020-09-26 17:20:17 -06004903 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4904 flags);
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004905}
4906
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004907SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4908 int, newdfd, const char __user *, newname)
4909{
Jens Axboee8866632020-09-26 17:20:17 -06004910 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4911 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004912}
4913
Heiko Carstensa26eab22009-01-14 14:14:17 +01004914SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004915{
Jens Axboee8866632020-09-26 17:20:17 -06004916 return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4917 getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004918}
4919
Al Viro5d826c82014-03-14 13:42:45 -04004920int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
Al Viro5d826c82014-03-14 13:42:45 -04004922 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (IS_ERR(link))
4924 goto out;
4925
4926 len = strlen(link);
4927 if (len > (unsigned) buflen)
4928 len = buflen;
4929 if (copy_to_user(buffer, link, len))
4930 len = -EFAULT;
4931out:
4932 return len;
4933}
4934
Miklos Szeredid60874c2016-10-04 14:40:45 +02004935/**
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004936 * vfs_readlink - copy symlink body into userspace buffer
4937 * @dentry: dentry on which to get symbolic link
4938 * @buffer: user memory pointer
4939 * @buflen: size of buffer
4940 *
4941 * Does not touch atime. That's up to the caller if necessary
4942 *
4943 * Does not call security hook.
4944 */
4945int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4946{
4947 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04004948 DEFINE_DELAYED_CALL(done);
4949 const char *link;
4950 int res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004951
Miklos Szeredi76fca902016-12-09 16:45:04 +01004952 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4953 if (unlikely(inode->i_op->readlink))
4954 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004955
Miklos Szeredi76fca902016-12-09 16:45:04 +01004956 if (!d_is_symlink(dentry))
4957 return -EINVAL;
4958
4959 spin_lock(&inode->i_lock);
4960 inode->i_opflags |= IOP_DEFAULT_READLINK;
4961 spin_unlock(&inode->i_lock);
4962 }
4963
Eric Biggers4c4f7c12019-04-10 13:21:14 -07004964 link = READ_ONCE(inode->i_link);
Al Virof2df5da2018-07-19 17:35:51 -04004965 if (!link) {
4966 link = inode->i_op->get_link(dentry, inode, &done);
4967 if (IS_ERR(link))
4968 return PTR_ERR(link);
4969 }
4970 res = readlink_copy(buffer, buflen, link);
4971 do_delayed_call(&done);
4972 return res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004973}
4974EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Miklos Szeredid60874c2016-10-04 14:40:45 +02004976/**
4977 * vfs_get_link - get symlink body
4978 * @dentry: dentry on which to get symbolic link
4979 * @done: caller needs to free returned data with this
4980 *
4981 * Calls security hook and i_op->get_link() on the supplied inode.
4982 *
4983 * It does not touch atime. That's up to the caller if necessary.
4984 *
4985 * Does not work on "special" symlinks like /proc/$$/fd/N
4986 */
4987const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4988{
4989 const char *res = ERR_PTR(-EINVAL);
4990 struct inode *inode = d_inode(dentry);
4991
4992 if (d_is_symlink(dentry)) {
4993 res = ERR_PTR(security_inode_readlink(dentry));
4994 if (!res)
4995 res = inode->i_op->get_link(dentry, inode, done);
4996 }
4997 return res;
4998}
4999EXPORT_SYMBOL(vfs_get_link);
5000
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05005002const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05005003 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
Duane Griffinebd09ab2008-12-19 20:47:12 +00005005 char *kaddr;
5006 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05005007 struct address_space *mapping = inode->i_mapping;
5008
Al Virod3883d42015-11-17 10:41:04 -05005009 if (!dentry) {
5010 page = find_get_page(mapping, 0);
5011 if (!page)
5012 return ERR_PTR(-ECHILD);
5013 if (!PageUptodate(page)) {
5014 put_page(page);
5015 return ERR_PTR(-ECHILD);
5016 }
5017 } else {
5018 page = read_mapping_page(mapping, 0, NULL);
5019 if (IS_ERR(page))
5020 return (char*)page;
5021 }
Al Virofceef392015-12-29 15:58:39 -05005022 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05005023 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
5024 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05005025 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00005026 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027}
5028
Al Viro6b255392015-11-17 10:20:54 -05005029EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Al Virofceef392015-12-29 15:58:39 -05005031void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032{
Al Virofceef392015-12-29 15:58:39 -05005033 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034}
Al Viro4d359502014-03-14 12:20:17 -04005035EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Al Viroaa80dea2015-11-16 18:26:34 -05005037int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
5038{
Al Virofceef392015-12-29 15:58:39 -05005039 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05005040 int res = readlink_copy(buffer, buflen,
5041 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05005042 &done));
5043 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05005044 return res;
5045}
5046EXPORT_SYMBOL(page_readlink);
5047
Matthew Wilcox (Oracle)56f57462022-02-22 09:40:54 -05005048int page_symlink(struct inode *inode, const char *symname, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
5050 struct address_space *mapping = inode->i_mapping;
Matthew Wilcox (Oracle)27a77912022-03-03 13:35:20 -05005051 const struct address_space_operations *aops = mapping->a_ops;
Matthew Wilcox (Oracle)56f57462022-02-22 09:40:54 -05005052 bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005053 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07005054 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08005055 int err;
Matthew Wilcox (Oracle)2d878172022-02-22 09:43:12 -05005056 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
NeilBrown7e53cac2006-03-25 03:07:57 -08005058retry:
Matthew Wilcox (Oracle)2d878172022-02-22 09:43:12 -05005059 if (nofs)
5060 flags = memalloc_nofs_save();
Matthew Wilcox (Oracle)27a77912022-03-03 13:35:20 -05005061 err = aops->write_begin(NULL, mapping, 0, len-1, &page, &fsdata);
Matthew Wilcox (Oracle)2d878172022-02-22 09:43:12 -05005062 if (nofs)
5063 memalloc_nofs_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07005065 goto fail;
5066
Al Viro21fc61c2015-11-17 01:07:57 -05005067 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07005068
Matthew Wilcox (Oracle)27a77912022-03-03 13:35:20 -05005069 err = aops->write_end(NULL, mapping, 0, len-1, len-1,
Nick Pigginafddba42007-10-16 01:25:01 -07005070 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (err < 0)
5072 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07005073 if (err < len-1)
5074 goto retry;
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 mark_inode_dirty(inode);
5077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078fail:
5079 return err;
5080}
Al Viro4d359502014-03-14 12:20:17 -04005081EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005082
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005083const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05005084 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086EXPORT_SYMBOL(page_symlink_inode_operations);