blob: 578c2110df0223fbdb952e3e002de5feaa2d1446 [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;
Al Viro03fa86e2022-07-04 18:12:39 -0400570 unsigned seq, next_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
Al Viro6e180322022-07-06 12:40:31 -0400668static void leave_rcu(struct nameidata *nd)
669{
670 nd->flags &= ~LOOKUP_RCU;
Al Viro03fa86e2022-07-04 18:12:39 -0400671 nd->seq = nd->next_seq = 0;
Al Viro6e180322022-07-06 12:40:31 -0400672 rcu_read_unlock();
673}
674
Al Viro7973387a2015-05-09 12:55:43 -0400675static void terminate_walk(struct nameidata *nd)
676{
677 drop_links(nd);
678 if (!(nd->flags & LOOKUP_RCU)) {
679 int i;
680 path_put(&nd->path);
681 for (i = 0; i < nd->depth; i++)
682 path_put(&nd->stack[i].link);
Al Virobcba1e72021-04-01 22:03:41 -0400683 if (nd->state & ND_ROOT_GRABBED) {
Al Viro102b8af2015-05-12 17:35:52 -0400684 path_put(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400685 nd->state &= ~ND_ROOT_GRABBED;
Al Viro102b8af2015-05-12 17:35:52 -0400686 }
Al Viro7973387a2015-05-09 12:55:43 -0400687 } else {
Al Viro6e180322022-07-06 12:40:31 -0400688 leave_rcu(nd);
Al Viro7973387a2015-05-09 12:55:43 -0400689 }
690 nd->depth = 0;
Al Viro7d01ef752021-04-06 12:33:07 -0400691 nd->path.mnt = NULL;
692 nd->path.dentry = NULL;
Al Viro7973387a2015-05-09 12:55:43 -0400693}
694
695/* path_put is needed afterwards regardless of success or failure */
Al Viro2aa38472020-02-26 19:19:05 -0500696static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
Al Viro7973387a2015-05-09 12:55:43 -0400697{
Al Viro2aa38472020-02-26 19:19:05 -0500698 int res = __legitimize_mnt(path->mnt, mseq);
Al Viro7973387a2015-05-09 12:55:43 -0400699 if (unlikely(res)) {
700 if (res > 0)
701 path->mnt = NULL;
702 path->dentry = NULL;
703 return false;
704 }
705 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
706 path->dentry = NULL;
707 return false;
708 }
709 return !read_seqcount_retry(&path->dentry->d_seq, seq);
710}
711
Al Viro2aa38472020-02-26 19:19:05 -0500712static inline bool legitimize_path(struct nameidata *nd,
713 struct path *path, unsigned seq)
714{
Al Viro5bd73282020-04-05 21:59:55 -0400715 return __legitimize_path(path, seq, nd->m_seq);
Al Viro2aa38472020-02-26 19:19:05 -0500716}
717
Al Viro7973387a2015-05-09 12:55:43 -0400718static bool legitimize_links(struct nameidata *nd)
719{
720 int i;
Al Viroeacd9aa82021-02-15 12:03:23 -0500721 if (unlikely(nd->flags & LOOKUP_CACHED)) {
722 drop_links(nd);
723 nd->depth = 0;
724 return false;
725 }
Al Viro7973387a2015-05-09 12:55:43 -0400726 for (i = 0; i < nd->depth; i++) {
727 struct saved *last = nd->stack + i;
728 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
729 drop_links(nd);
730 nd->depth = i + 1;
731 return false;
732 }
733 }
734 return true;
735}
736
Al Viroee594bf2019-07-16 21:05:36 -0400737static bool legitimize_root(struct nameidata *nd)
738{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100739 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
Al Virobcba1e72021-04-01 22:03:41 -0400740 if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET))
Al Viroee594bf2019-07-16 21:05:36 -0400741 return true;
Al Virobcba1e72021-04-01 22:03:41 -0400742 nd->state |= ND_ROOT_GRABBED;
Al Viroee594bf2019-07-16 21:05:36 -0400743 return legitimize_path(nd, &nd->root, nd->root_seq);
744}
745
Al Viro19660af2011-03-25 10:32:48 -0400746/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100747 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400748 * Documentation/filesystems/path-lookup.txt). In situations when we can't
749 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500750 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400751 * mode. Refcounts are grabbed at the last known good point before rcu-walk
752 * got stuck, so ref-walk may continue from there. If this is not successful
753 * (eg. a seqcount has changed), then failure is returned and it's up to caller
754 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100755 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100756
757/**
Jens Axboee36cffe2020-12-17 09:19:08 -0700758 * try_to_unlazy - try to switch to ref-walk mode.
Al Viro19660af2011-03-25 10:32:48 -0400759 * @nd: nameidata pathwalk data
Jens Axboee36cffe2020-12-17 09:19:08 -0700760 * Returns: true on success, false on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100761 *
Jens Axboee36cffe2020-12-17 09:19:08 -0700762 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
Al Viro4675ac32017-01-09 22:29:15 -0500763 * for ref-walk mode.
764 * Must be called from rcu-walk context.
Jens Axboee36cffe2020-12-17 09:19:08 -0700765 * Nothing should touch nameidata between try_to_unlazy() failure and
Al Viro7973387a2015-05-09 12:55:43 -0400766 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100767 */
Jens Axboee36cffe2020-12-17 09:19:08 -0700768static bool try_to_unlazy(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100769{
Nick Piggin31e6b012011-01-07 17:49:52 +1100770 struct dentry *parent = nd->path.dentry;
771
772 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700773
Al Viro7973387a2015-05-09 12:55:43 -0400774 if (unlikely(!legitimize_links(nd)))
Al Viro4675ac32017-01-09 22:29:15 -0500775 goto out1;
Al Viro84a2bd32019-07-16 21:20:17 -0400776 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
777 goto out;
Al Viroee594bf2019-07-16 21:05:36 -0400778 if (unlikely(!legitimize_root(nd)))
779 goto out;
Al Viro6e180322022-07-06 12:40:31 -0400780 leave_rcu(nd);
Al Viro4675ac32017-01-09 22:29:15 -0500781 BUG_ON(nd->inode != parent->d_inode);
Jens Axboee36cffe2020-12-17 09:19:08 -0700782 return true;
Al Viro4675ac32017-01-09 22:29:15 -0500783
Al Viro84a2bd32019-07-16 21:20:17 -0400784out1:
Al Viro4675ac32017-01-09 22:29:15 -0500785 nd->path.mnt = NULL;
786 nd->path.dentry = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500787out:
Al Viro6e180322022-07-06 12:40:31 -0400788 leave_rcu(nd);
Jens Axboee36cffe2020-12-17 09:19:08 -0700789 return false;
Al Viro4675ac32017-01-09 22:29:15 -0500790}
791
792/**
Al Viroae66db42021-01-04 00:08:41 -0500793 * try_to_unlazy_next - try to switch to ref-walk mode.
Al Viro4675ac32017-01-09 22:29:15 -0500794 * @nd: nameidata pathwalk data
Al Viroae66db42021-01-04 00:08:41 -0500795 * @dentry: next dentry to step into
Al Viroae66db42021-01-04 00:08:41 -0500796 * Returns: true on success, false on failure
Al Viro4675ac32017-01-09 22:29:15 -0500797 *
Tom Rix30476f72022-01-25 05:13:40 -0800798 * Similar to try_to_unlazy(), but here we have the next dentry already
Al Viroae66db42021-01-04 00:08:41 -0500799 * picked by rcu-walk and want to legitimize that in addition to the current
800 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
801 * Nothing should touch nameidata between try_to_unlazy_next() failure and
Al Viro4675ac32017-01-09 22:29:15 -0500802 * terminate_walk().
803 */
Al Viro03fa86e2022-07-04 18:12:39 -0400804static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry)
Al Viro4675ac32017-01-09 22:29:15 -0500805{
Al Viro7e4745a2022-07-05 12:22:46 -0400806 int res;
Al Viro4675ac32017-01-09 22:29:15 -0500807 BUG_ON(!(nd->flags & LOOKUP_RCU));
808
Al Viro4675ac32017-01-09 22:29:15 -0500809 if (unlikely(!legitimize_links(nd)))
810 goto out2;
Al Viro7e4745a2022-07-05 12:22:46 -0400811 res = __legitimize_mnt(nd->path.mnt, nd->m_seq);
812 if (unlikely(res)) {
813 if (res > 0)
814 goto out2;
815 goto out1;
816 }
Al Viro4675ac32017-01-09 22:29:15 -0500817 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro7973387a2015-05-09 12:55:43 -0400818 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400819
Linus Torvalds15570082013-09-02 11:38:06 -0700820 /*
Al Viro4675ac32017-01-09 22:29:15 -0500821 * We need to move both the parent and the dentry from the RCU domain
822 * to be properly refcounted. And the sequence number in the dentry
823 * validates *both* dentry counters, since we checked the sequence
824 * number of the parent after we got the child sequence number. So we
825 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700826 */
Al Viro4675ac32017-01-09 22:29:15 -0500827 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
828 goto out;
Al Viro03fa86e2022-07-04 18:12:39 -0400829 if (read_seqcount_retry(&dentry->d_seq, nd->next_seq))
Al Viro84a2bd32019-07-16 21:20:17 -0400830 goto out_dput;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700831 /*
832 * Sequence counts matched. Now make sure that the root is
833 * still valid and get it if required.
834 */
Al Viro84a2bd32019-07-16 21:20:17 -0400835 if (unlikely(!legitimize_root(nd)))
836 goto out_dput;
Al Viro6e180322022-07-06 12:40:31 -0400837 leave_rcu(nd);
Al Viroae66db42021-01-04 00:08:41 -0500838 return true;
Al Viro19660af2011-03-25 10:32:48 -0400839
Al Viro7973387a2015-05-09 12:55:43 -0400840out2:
841 nd->path.mnt = NULL;
842out1:
843 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700844out:
Al Viro6e180322022-07-06 12:40:31 -0400845 leave_rcu(nd);
Al Viroae66db42021-01-04 00:08:41 -0500846 return false;
Al Viro84a2bd32019-07-16 21:20:17 -0400847out_dput:
Al Viro6e180322022-07-06 12:40:31 -0400848 leave_rcu(nd);
Al Viro84a2bd32019-07-16 21:20:17 -0400849 dput(dentry);
Al Viroae66db42021-01-04 00:08:41 -0500850 return false;
Nick Piggin31e6b012011-01-07 17:49:52 +1100851}
852
Al Viro4ce16ef32012-06-10 16:10:59 -0400853static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100854{
Al Viroa89f8332017-01-09 22:25:28 -0500855 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
856 return dentry->d_op->d_revalidate(dentry, flags);
857 else
858 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100859}
860
Al Viro9f1fafe2011-03-25 11:00:12 -0400861/**
862 * complete_walk - successful completion of path walk
863 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500864 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400865 * If we had been in RCU mode, drop out of it and legitimize nd->path.
866 * Revalidate the final result, unless we'd already done that during
867 * the path walk or the filesystem doesn't ask for it. Return 0 on
868 * success, -error on failure. In case of failure caller does not
869 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500870 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400871static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500872{
Al Viro16c2cd72011-02-22 15:50:10 -0500873 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500874 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500875
Al Viro9f1fafe2011-03-25 11:00:12 -0400876 if (nd->flags & LOOKUP_RCU) {
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100877 /*
878 * We don't want to zero nd->root for scoped-lookups or
879 * externally-managed nd->root.
880 */
Al Virobcba1e72021-04-01 22:03:41 -0400881 if (!(nd->state & ND_ROOT_PRESET))
882 if (!(nd->flags & LOOKUP_IS_SCOPED))
883 nd->root.mnt = NULL;
Jens Axboe6c6ec2b2020-12-17 09:19:09 -0700884 nd->flags &= ~LOOKUP_CACHED;
Jens Axboee36cffe2020-12-17 09:19:08 -0700885 if (!try_to_unlazy(nd))
Al Viro48a066e2013-09-29 22:06:07 -0400886 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400887 }
888
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100889 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
890 /*
891 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
892 * ever step outside the root during lookup" and should already
893 * be guaranteed by the rest of namei, we want to avoid a namei
894 * BUG resulting in userspace being given a path that was not
895 * scoped within the root at some point during the lookup.
896 *
897 * So, do a final sanity-check to make sure that in the
898 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
899 * we won't silently return an fd completely outside of the
900 * requested root to userspace.
901 *
902 * Userspace could move the path outside the root after this
903 * check, but as discussed elsewhere this is not a concern (the
904 * resolved file was inside the root at some point).
905 */
906 if (!path_is_under(&nd->path, &nd->root))
907 return -EXDEV;
908 }
909
Al Virobcba1e72021-04-01 22:03:41 -0400910 if (likely(!(nd->state & ND_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500911 return 0;
912
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500913 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500914 return 0;
915
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500916 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500917 if (status > 0)
918 return 0;
919
Al Viro16c2cd72011-02-22 15:50:10 -0500920 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500921 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500922
Jeff Layton39159de2009-12-07 12:01:50 -0500923 return status;
924}
925
Aleksa Sarai740a1672019-12-07 01:13:29 +1100926static int set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400927{
Al Viro7bd88372014-09-13 21:55:46 -0400928 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100929
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100930 /*
931 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
932 * still have to ensure it doesn't happen because it will cause a breakout
933 * from the dirfd.
934 */
935 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
936 return -ENOTRECOVERABLE;
937
Al Viro9e6697e2015-12-05 20:07:21 -0500938 if (nd->flags & LOOKUP_RCU) {
939 unsigned seq;
940
941 do {
942 seq = read_seqcount_begin(&fs->seq);
943 nd->root = fs->root;
944 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
945 } while (read_seqcount_retry(&fs->seq, seq));
946 } else {
947 get_fs_root(fs, &nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400948 nd->state |= ND_ROOT_GRABBED;
Al Viro9e6697e2015-12-05 20:07:21 -0500949 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100950 return 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100951}
952
Al Viro248fb5b2015-12-05 20:51:58 -0500953static int nd_jump_root(struct nameidata *nd)
954{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100955 if (unlikely(nd->flags & LOOKUP_BENEATH))
956 return -EXDEV;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100957 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
958 /* Absolute path arguments to path_init() are allowed. */
959 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
960 return -EXDEV;
961 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100962 if (!nd->root.mnt) {
963 int error = set_root(nd);
964 if (error)
965 return error;
966 }
Al Viro248fb5b2015-12-05 20:51:58 -0500967 if (nd->flags & LOOKUP_RCU) {
968 struct dentry *d;
969 nd->path = nd->root;
970 d = nd->path.dentry;
971 nd->inode = d->d_inode;
972 nd->seq = nd->root_seq;
Al Viro82ef0692022-07-05 11:23:58 -0400973 if (read_seqcount_retry(&d->d_seq, nd->seq))
Al Viro248fb5b2015-12-05 20:51:58 -0500974 return -ECHILD;
975 } else {
976 path_put(&nd->path);
977 nd->path = nd->root;
978 path_get(&nd->path);
979 nd->inode = nd->path.dentry->d_inode;
980 }
Al Virobcba1e72021-04-01 22:03:41 -0400981 nd->state |= ND_JUMPED;
Al Viro248fb5b2015-12-05 20:51:58 -0500982 return 0;
983}
984
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400985/*
Al Viro6b255392015-11-17 10:20:54 -0500986 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400987 * caller must have taken a reference to path beforehand.
988 */
Al Viroea4af4a2022-08-04 13:19:18 -0400989int nd_jump_link(const struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400990{
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100991 int error = -ELOOP;
Al Viro6e77137b2015-05-02 13:37:52 -0400992 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400993
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100994 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
995 goto err;
996
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100997 error = -EXDEV;
998 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
999 if (nd->path.mnt != path->mnt)
1000 goto err;
1001 }
Aleksa Saraiadb21d22019-12-07 01:13:33 +11001002 /* Not currently safe for scoped-lookups. */
1003 if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
1004 goto err;
Aleksa Sarai72ba2922019-12-07 01:13:32 +11001005
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001006 path_put(&nd->path);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001007 nd->path = *path;
1008 nd->inode = nd->path.dentry->d_inode;
Al Virobcba1e72021-04-01 22:03:41 -04001009 nd->state |= ND_JUMPED;
Aleksa Sarai1bc82072019-12-07 01:13:28 +11001010 return 0;
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001011
1012err:
1013 path_put(path);
1014 return error;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001015}
1016
Al Virob9ff4422015-05-02 20:19:23 -04001017static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -04001018{
Al Viro21c30032015-05-03 21:06:24 -04001019 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -05001020 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -04001021 if (!(nd->flags & LOOKUP_RCU))
1022 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -04001023}
1024
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001025static int sysctl_protected_symlinks __read_mostly;
1026static int sysctl_protected_hardlinks __read_mostly;
1027static int sysctl_protected_fifos __read_mostly;
1028static int sysctl_protected_regular __read_mostly;
1029
1030#ifdef CONFIG_SYSCTL
1031static struct ctl_table namei_sysctls[] = {
1032 {
1033 .procname = "protected_symlinks",
1034 .data = &sysctl_protected_symlinks,
1035 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001036 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001037 .proc_handler = proc_dointvec_minmax,
1038 .extra1 = SYSCTL_ZERO,
1039 .extra2 = SYSCTL_ONE,
1040 },
1041 {
1042 .procname = "protected_hardlinks",
1043 .data = &sysctl_protected_hardlinks,
1044 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001045 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001046 .proc_handler = proc_dointvec_minmax,
1047 .extra1 = SYSCTL_ZERO,
1048 .extra2 = SYSCTL_ONE,
1049 },
1050 {
1051 .procname = "protected_fifos",
1052 .data = &sysctl_protected_fifos,
1053 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001054 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001055 .proc_handler = proc_dointvec_minmax,
1056 .extra1 = SYSCTL_ZERO,
1057 .extra2 = SYSCTL_TWO,
1058 },
1059 {
1060 .procname = "protected_regular",
1061 .data = &sysctl_protected_regular,
1062 .maxlen = sizeof(int),
Julius Hemanth Pittic7031c12022-05-13 16:58:15 -07001063 .mode = 0644,
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001064 .proc_handler = proc_dointvec_minmax,
1065 .extra1 = SYSCTL_ZERO,
1066 .extra2 = SYSCTL_TWO,
1067 },
1068 { }
1069};
1070
1071static int __init init_fs_namei_sysctls(void)
1072{
1073 register_sysctl_init("fs", namei_sysctls);
1074 return 0;
1075}
1076fs_initcall(init_fs_namei_sysctls);
1077
1078#endif /* CONFIG_SYSCTL */
Kees Cook800179c2012-07-25 17:29:07 -07001079
1080/**
1081 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -07001082 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -07001083 *
1084 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1085 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1086 * in a sticky world-writable directory. This is to protect privileged
1087 * processes from failing races against path names that may change out
1088 * from under them by way of other users creating malicious symlinks.
1089 * It will permit symlinks to be followed only when outside a sticky
1090 * world-writable directory, or when the uid of the symlink and follower
1091 * match, or when the directory owner matches the symlink's owner.
1092 *
1093 * Returns 0 if following the symlink is allowed, -ve on error.
1094 */
Al Viroad6cc4c2020-01-14 14:41:39 -05001095static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001096{
Christian Braunerba73d982021-01-21 14:19:31 +01001097 struct user_namespace *mnt_userns;
1098 kuid_t i_uid;
1099
Kees Cook800179c2012-07-25 17:29:07 -07001100 if (!sysctl_protected_symlinks)
1101 return 0;
1102
Christian Braunerba73d982021-01-21 14:19:31 +01001103 mnt_userns = mnt_user_ns(nd->path.mnt);
1104 i_uid = i_uid_into_mnt(mnt_userns, inode);
Kees Cook800179c2012-07-25 17:29:07 -07001105 /* Allowed if owner and follower match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001106 if (uid_eq(current_cred()->fsuid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001107 return 0;
1108
1109 /* Allowed if parent directory not sticky and world-writable. */
Al Viro0f705952020-03-05 11:34:48 -05001110 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
Kees Cook800179c2012-07-25 17:29:07 -07001111 return 0;
1112
1113 /* Allowed if parent directory and link owner match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001114 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001115 return 0;
1116
Al Viro319565022015-05-07 20:37:40 -04001117 if (nd->flags & LOOKUP_RCU)
1118 return -ECHILD;
1119
Richard Guy Briggsea841ba2018-03-21 04:42:21 -04001120 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Kees Cook245d7362019-10-02 16:41:58 -07001121 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
Kees Cook800179c2012-07-25 17:29:07 -07001122 return -EACCES;
1123}
1124
1125/**
1126 * safe_hardlink_source - Check for safe hardlink conditions
Christian Braunerba73d982021-01-21 14:19:31 +01001127 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001128 * @inode: the source inode to hardlink from
1129 *
1130 * Return false if at least one of the following conditions:
1131 * - inode is not a regular file
1132 * - inode is setuid
1133 * - inode is setgid and group-exec
1134 * - access failure for read and write
1135 *
1136 * Otherwise returns true.
1137 */
Christian Braunerba73d982021-01-21 14:19:31 +01001138static bool safe_hardlink_source(struct user_namespace *mnt_userns,
1139 struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001140{
1141 umode_t mode = inode->i_mode;
1142
1143 /* Special files should not get pinned to the filesystem. */
1144 if (!S_ISREG(mode))
1145 return false;
1146
1147 /* Setuid files should not get pinned to the filesystem. */
1148 if (mode & S_ISUID)
1149 return false;
1150
1151 /* Executable setgid files should not get pinned to the filesystem. */
1152 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1153 return false;
1154
1155 /* Hardlinking to unreadable or unwritable sources is dangerous. */
Christian Braunerba73d982021-01-21 14:19:31 +01001156 if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
Kees Cook800179c2012-07-25 17:29:07 -07001157 return false;
1158
1159 return true;
1160}
1161
1162/**
1163 * may_linkat - Check permissions for creating a hardlink
Christian Braunerba73d982021-01-21 14:19:31 +01001164 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001165 * @link: the source to hardlink from
1166 *
1167 * Block hardlink when all of:
1168 * - sysctl_protected_hardlinks enabled
1169 * - fsuid does not match inode
1170 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001171 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -07001172 *
Christian Braunerba73d982021-01-21 14:19:31 +01001173 * If the inode has been found through an idmapped mount the user namespace of
1174 * the vfsmount must be passed through @mnt_userns. This function will then take
1175 * care to map the inode according to @mnt_userns before checking permissions.
1176 * On non-idmapped mounts or if permission checking is to be performed on the
1177 * raw inode simply passs init_user_ns.
1178 *
Kees Cook800179c2012-07-25 17:29:07 -07001179 * Returns 0 if successful, -ve on error.
1180 */
Al Viro89966822022-08-04 12:53:46 -04001181int may_linkat(struct user_namespace *mnt_userns, const struct path *link)
Kees Cook800179c2012-07-25 17:29:07 -07001182{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001183 struct inode *inode = link->dentry->d_inode;
1184
1185 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01001186 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
1187 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001188 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -07001189
1190 if (!sysctl_protected_hardlinks)
1191 return 0;
1192
Kees Cook800179c2012-07-25 17:29:07 -07001193 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1194 * otherwise, it must be a safe source.
1195 */
Christian Braunerba73d982021-01-21 14:19:31 +01001196 if (safe_hardlink_source(mnt_userns, inode) ||
1197 inode_owner_or_capable(mnt_userns, inode))
Kees Cook800179c2012-07-25 17:29:07 -07001198 return 0;
1199
Kees Cook245d7362019-10-02 16:41:58 -07001200 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001201 return -EPERM;
1202}
1203
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001204/**
1205 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1206 * should be allowed, or not, on files that already
1207 * exist.
Christian Braunerba73d982021-01-21 14:19:31 +01001208 * @mnt_userns: user namespace of the mount the inode was found from
Randy Dunlap2111c3c2021-02-15 20:29:28 -08001209 * @nd: nameidata pathwalk data
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001210 * @inode: the inode of the file to open
1211 *
1212 * Block an O_CREAT open of a FIFO (or a regular file) when:
1213 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1214 * - the file already exists
1215 * - we are in a sticky directory
1216 * - we don't own the file
1217 * - the owner of the directory doesn't own the file
1218 * - the directory is world writable
1219 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1220 * the directory doesn't have to be world writable: being group writable will
1221 * be enough.
1222 *
Christian Braunerba73d982021-01-21 14:19:31 +01001223 * If the inode has been found through an idmapped mount the user namespace of
1224 * the vfsmount must be passed through @mnt_userns. This function will then take
1225 * care to map the inode according to @mnt_userns before checking permissions.
1226 * On non-idmapped mounts or if permission checking is to be performed on the
1227 * raw inode simply passs init_user_ns.
1228 *
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001229 * Returns 0 if the open is allowed, -ve on error.
1230 */
Christian Braunerba73d982021-01-21 14:19:31 +01001231static int may_create_in_sticky(struct user_namespace *mnt_userns,
1232 struct nameidata *nd, struct inode *const inode)
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001233{
Christian Braunerba73d982021-01-21 14:19:31 +01001234 umode_t dir_mode = nd->dir_mode;
1235 kuid_t dir_uid = nd->dir_uid;
1236
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001237 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1238 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Virod0cb5012020-01-26 09:29:34 -05001239 likely(!(dir_mode & S_ISVTX)) ||
Christian Braunerba73d982021-01-21 14:19:31 +01001240 uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
1241 uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001242 return 0;
1243
Al Virod0cb5012020-01-26 09:29:34 -05001244 if (likely(dir_mode & 0002) ||
1245 (dir_mode & 0020 &&
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001246 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1247 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
Kees Cook245d7362019-10-02 16:41:58 -07001248 const char *operation = S_ISFIFO(inode->i_mode) ?
1249 "sticky_create_fifo" :
1250 "sticky_create_regular";
1251 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001252 return -EACCES;
1253 }
1254 return 0;
1255}
1256
David Howellsf015f1262012-06-25 12:55:28 +01001257/*
1258 * follow_up - Find the mountpoint of path's vfsmount
1259 *
1260 * Given a path, find the mountpoint of its source file system.
1261 * Replace @path with the path of the mountpoint in the parent mount.
1262 * Up is towards /.
1263 *
1264 * Return 1 if we went up a level and 0 if we were already at the
1265 * root.
1266 */
Al Virobab77eb2009-04-18 03:26:48 -04001267int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Al Viro0714a532011-11-24 22:19:58 -05001269 struct mount *mnt = real_mount(path->mnt);
1270 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001272
Al Viro48a066e2013-09-29 22:06:07 -04001273 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001274 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001275 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001276 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278 }
Al Viro0714a532011-11-24 22:19:58 -05001279 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001280 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001281 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001282 dput(path->dentry);
1283 path->dentry = mountpoint;
1284 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001285 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 1;
1287}
Al Viro4d359502014-03-14 12:20:17 -04001288EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Al Viro7ef482f2020-02-26 17:50:13 -05001290static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1291 struct path *path, unsigned *seqp)
1292{
1293 while (mnt_has_parent(m)) {
1294 struct dentry *mountpoint = m->mnt_mountpoint;
1295
1296 m = m->mnt_parent;
1297 if (unlikely(root->dentry == mountpoint &&
1298 root->mnt == &m->mnt))
1299 break;
1300 if (mountpoint != m->mnt.mnt_root) {
1301 path->mnt = &m->mnt;
1302 path->dentry = mountpoint;
1303 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1304 return true;
1305 }
1306 }
1307 return false;
1308}
1309
Al Viro2aa38472020-02-26 19:19:05 -05001310static bool choose_mountpoint(struct mount *m, const struct path *root,
1311 struct path *path)
1312{
1313 bool found;
1314
1315 rcu_read_lock();
1316 while (1) {
1317 unsigned seq, mseq = read_seqbegin(&mount_lock);
1318
1319 found = choose_mountpoint_rcu(m, root, path, &seq);
1320 if (unlikely(!found)) {
1321 if (!read_seqretry(&mount_lock, mseq))
1322 break;
1323 } else {
1324 if (likely(__legitimize_path(path, seq, mseq)))
1325 break;
1326 rcu_read_unlock();
1327 path_put(path);
1328 rcu_read_lock();
1329 }
1330 }
1331 rcu_read_unlock();
1332 return found;
1333}
1334
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001335/*
David Howells9875cf82011-01-14 18:45:21 +00001336 * Perform an automount
1337 * - return -EISDIR to tell follow_managed() to stop and return the path we
1338 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001340static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
Nick Piggin31e6b012011-01-07 17:49:52 +11001341{
Al Viro25e195a2020-01-11 11:27:46 -05001342 struct dentry *dentry = path->dentry;
Al Viro463ffb22005-06-06 13:36:05 -07001343
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001344 /* We don't want to mount if someone's just doing a stat -
1345 * unless they're stat'ing a directory and appended a '/' to
1346 * the name.
1347 *
1348 * We do, however, want to mount if someone wants to open or
1349 * create a file of any type under the mountpoint, wants to
1350 * traverse through the mountpoint or wants to open the
1351 * mounted directory. Also, autofs may mark negative dentries
1352 * as being automount points. These will need the attentions
1353 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001354 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001355 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001356 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Al Viro25e195a2020-01-11 11:27:46 -05001357 dentry->d_inode)
Ian Kent5d38f042017-11-29 16:11:26 -08001358 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001359
Al Viro1c9f5e02020-01-16 22:05:18 -05001360 if (count && (*count)++ >= MAXSYMLINKS)
David Howells9875cf82011-01-14 18:45:21 +00001361 return -ELOOP;
1362
Al Viro25e195a2020-01-11 11:27:46 -05001363 return finish_automount(dentry->d_op->d_automount(path), path);
David Howells9875cf82011-01-14 18:45:21 +00001364}
1365
1366/*
Al Viro9deed3e2020-01-17 08:45:08 -05001367 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1368 * dentries are pinned but not locked here, so negative dentry can go
1369 * positive right under us. Use of smp_load_acquire() provides a barrier
1370 * sufficient for ->d_inode and ->d_flags consistency.
David Howells9875cf82011-01-14 18:45:21 +00001371 */
Al Viro9deed3e2020-01-17 08:45:08 -05001372static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1373 int *count, unsigned lookup_flags)
David Howells9875cf82011-01-14 18:45:21 +00001374{
Al Viro9deed3e2020-01-17 08:45:08 -05001375 struct vfsmount *mnt = path->mnt;
David Howells9875cf82011-01-14 18:45:21 +00001376 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001377 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001378
Al Viro9deed3e2020-01-17 08:45:08 -05001379 while (flags & DCACHE_MANAGED_DENTRY) {
David Howellscc53ce52011-01-14 18:45:26 +00001380 /* Allow the filesystem to manage the transit without i_mutex
1381 * being held. */
Al Virod41efb52019-11-04 22:30:52 -05001382 if (flags & DCACHE_MANAGE_TRANSIT) {
Ian Kentfb5f51c2016-11-24 08:03:41 +11001383 ret = path->dentry->d_op->d_manage(path, false);
Al Viro508c8772020-01-14 22:09:57 -05001384 flags = smp_load_acquire(&path->dentry->d_flags);
David Howellscc53ce52011-01-14 18:45:26 +00001385 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001386 break;
David Howellscc53ce52011-01-14 18:45:26 +00001387 }
1388
Al Viro9deed3e2020-01-17 08:45:08 -05001389 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
David Howells9875cf82011-01-14 18:45:21 +00001390 struct vfsmount *mounted = lookup_mnt(path);
Al Viro9deed3e2020-01-17 08:45:08 -05001391 if (mounted) { // ... in our namespace
David Howells9875cf82011-01-14 18:45:21 +00001392 dput(path->dentry);
1393 if (need_mntput)
1394 mntput(path->mnt);
1395 path->mnt = mounted;
1396 path->dentry = dget(mounted->mnt_root);
Al Viro9deed3e2020-01-17 08:45:08 -05001397 // here we know it's positive
1398 flags = path->dentry->d_flags;
David Howells9875cf82011-01-14 18:45:21 +00001399 need_mntput = true;
1400 continue;
1401 }
David Howells9875cf82011-01-14 18:45:21 +00001402 }
1403
Al Viro9deed3e2020-01-17 08:45:08 -05001404 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1405 break;
David Howells9875cf82011-01-14 18:45:21 +00001406
Al Viro9deed3e2020-01-17 08:45:08 -05001407 // uncovered automount point
1408 ret = follow_automount(path, count, lookup_flags);
1409 flags = smp_load_acquire(&path->dentry->d_flags);
1410 if (ret < 0)
1411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Al Viro8aef1882011-06-16 15:10:06 +01001413
Al Viro9deed3e2020-01-17 08:45:08 -05001414 if (ret == -EISDIR)
1415 ret = 0;
1416 // possible if you race with several mount --move
1417 if (need_mntput && path->mnt == mnt)
1418 mntput(path->mnt);
1419 if (!ret && unlikely(d_flags_negative(flags)))
Al Virod41efb52019-11-04 22:30:52 -05001420 ret = -ENOENT;
Al Viro9deed3e2020-01-17 08:45:08 -05001421 *jumped = need_mntput;
Al Viro8402752e2015-04-22 10:30:08 -04001422 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Al Viro9deed3e2020-01-17 08:45:08 -05001425static inline int traverse_mounts(struct path *path, bool *jumped,
1426 int *count, unsigned lookup_flags)
1427{
1428 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1429
1430 /* fastpath */
1431 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1432 *jumped = false;
1433 if (unlikely(d_flags_negative(flags)))
1434 return -ENOENT;
1435 return 0;
1436 }
1437 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1438}
1439
David Howellscc53ce52011-01-14 18:45:26 +00001440int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct vfsmount *mounted;
1443
Al Viro1c755af2009-04-18 14:06:57 -04001444 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001446 dput(path->dentry);
1447 mntput(path->mnt);
1448 path->mnt = mounted;
1449 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 1;
1451 }
1452 return 0;
1453}
Al Viro4d359502014-03-14 12:20:17 -04001454EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
David Howells9875cf82011-01-14 18:45:21 +00001456/*
Al Viro9deed3e2020-01-17 08:45:08 -05001457 * Follow down to the covering mount currently visible to userspace. At each
1458 * point, the filesystem owning that dentry may be queried as to whether the
1459 * caller is permitted to proceed or not.
1460 */
1461int follow_down(struct path *path)
1462{
1463 struct vfsmount *mnt = path->mnt;
1464 bool jumped;
1465 int ret = traverse_mounts(path, &jumped, NULL, 0);
1466
1467 if (path->mnt != mnt)
1468 mntput(mnt);
1469 return ret;
1470}
1471EXPORT_SYMBOL(follow_down);
1472
1473/*
Al Viro287548e2011-05-27 06:50:06 -04001474 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1475 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001476 */
Al Viro3bd8bc82022-07-03 22:35:56 -04001477static bool __follow_mount_rcu(struct nameidata *nd, struct path *path)
David Howells9875cf82011-01-14 18:45:21 +00001478{
Al Viroea936ae2020-01-16 09:52:04 -05001479 struct dentry *dentry = path->dentry;
1480 unsigned int flags = dentry->d_flags;
1481
1482 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1483 return true;
1484
1485 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1486 return false;
1487
Ian Kent62a73752011-03-25 01:51:02 +08001488 for (;;) {
Ian Kent62a73752011-03-25 01:51:02 +08001489 /*
1490 * Don't forget we might have a non-mountpoint managed dentry
1491 * that wants to block transit.
1492 */
Al Viroea936ae2020-01-16 09:52:04 -05001493 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1494 int res = dentry->d_op->d_manage(path, true);
1495 if (res)
1496 return res == -EISDIR;
1497 flags = dentry->d_flags;
NeilBrownb8faf032014-08-04 17:06:29 +10001498 }
Ian Kent62a73752011-03-25 01:51:02 +08001499
Al Viroea936ae2020-01-16 09:52:04 -05001500 if (flags & DCACHE_MOUNTED) {
1501 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1502 if (mounted) {
1503 path->mnt = &mounted->mnt;
1504 dentry = path->dentry = mounted->mnt.mnt_root;
Al Virobcba1e72021-04-01 22:03:41 -04001505 nd->state |= ND_JUMPED;
Al Viro03fa86e2022-07-04 18:12:39 -04001506 nd->next_seq = read_seqcount_begin(&dentry->d_seq);
Al Viroea936ae2020-01-16 09:52:04 -05001507 flags = dentry->d_flags;
Al Viro03fa86e2022-07-04 18:12:39 -04001508 // makes sure that non-RCU pathwalk could reach
1509 // this state.
Al Viro20aac6c2022-07-04 17:26:29 -04001510 if (read_seqretry(&mount_lock, nd->m_seq))
1511 return false;
Al Viroea936ae2020-01-16 09:52:04 -05001512 continue;
1513 }
1514 if (read_seqretry(&mount_lock, nd->m_seq))
1515 return false;
1516 }
1517 return !(flags & DCACHE_NEED_AUTOMOUNT);
David Howells9875cf82011-01-14 18:45:21 +00001518 }
Al Viro287548e2011-05-27 06:50:06 -04001519}
1520
Al Virodb3c9ad2020-01-09 14:41:00 -05001521static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
Al Viro3bd8bc82022-07-03 22:35:56 -04001522 struct path *path)
Al Virobd7c4b52020-01-08 20:37:23 -05001523{
Al Viro9deed3e2020-01-17 08:45:08 -05001524 bool jumped;
Al Virodb3c9ad2020-01-09 14:41:00 -05001525 int ret;
Al Virobd7c4b52020-01-08 20:37:23 -05001526
Al Virodb3c9ad2020-01-09 14:41:00 -05001527 path->mnt = nd->path.mnt;
1528 path->dentry = dentry;
Al Viroc1530072020-01-09 14:50:18 -05001529 if (nd->flags & LOOKUP_RCU) {
Al Viro03fa86e2022-07-04 18:12:39 -04001530 unsigned int seq = nd->next_seq;
Al Viro3bd8bc82022-07-03 22:35:56 -04001531 if (likely(__follow_mount_rcu(nd, path)))
Al Viro9deed3e2020-01-17 08:45:08 -05001532 return 0;
Al Viro03fa86e2022-07-04 18:12:39 -04001533 // *path and nd->next_seq might've been clobbered
Al Viroc1530072020-01-09 14:50:18 -05001534 path->mnt = nd->path.mnt;
1535 path->dentry = dentry;
Al Viro03fa86e2022-07-04 18:12:39 -04001536 nd->next_seq = seq;
1537 if (!try_to_unlazy_next(nd, dentry))
1538 return -ECHILD;
Al Viroc1530072020-01-09 14:50:18 -05001539 }
Al Viro9deed3e2020-01-17 08:45:08 -05001540 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1541 if (jumped) {
1542 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1543 ret = -EXDEV;
1544 else
Al Virobcba1e72021-04-01 22:03:41 -04001545 nd->state |= ND_JUMPED;
Al Viro9deed3e2020-01-17 08:45:08 -05001546 }
1547 if (unlikely(ret)) {
1548 dput(path->dentry);
1549 if (path->mnt != nd->path.mnt)
1550 mntput(path->mnt);
Al Virobd7c4b52020-01-08 20:37:23 -05001551 }
1552 return ret;
1553}
1554
David Howells9875cf82011-01-14 18:45:21 +00001555/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001556 * This looks up the name in dcache and possibly revalidates the found dentry.
1557 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001558 */
Al Viroe3c13922016-03-06 14:03:27 -05001559static struct dentry *lookup_dcache(const struct qstr *name,
1560 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001561 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001562{
Al Viroa89f8332017-01-09 22:25:28 -05001563 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001564 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001565 int error = d_revalidate(dentry, flags);
1566 if (unlikely(error <= 0)) {
1567 if (!error)
1568 d_invalidate(dentry);
1569 dput(dentry);
1570 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001571 }
1572 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001573 return dentry;
1574}
1575
1576/*
Al Viroa03ece52018-03-08 11:00:45 -05001577 * Parent directory has inode locked exclusive. This is one
1578 * and only case when ->lookup() gets called on non in-lookup
1579 * dentries - as the matter of fact, this only gets called
1580 * when directory is guaranteed to have no in-lookup children
1581 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001582 */
Al Viroa03ece52018-03-08 11:00:45 -05001583static struct dentry *__lookup_hash(const struct qstr *name,
1584 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001585{
Al Viroa03ece52018-03-08 11:00:45 -05001586 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001587 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001588 struct inode *dir = base->d_inode;
1589
1590 if (dentry)
1591 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001592
1593 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001594 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001595 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001596
1597 dentry = d_alloc(base, name);
1598 if (unlikely(!dentry))
1599 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001600
Al Viro72bd8662012-06-10 17:17:17 -04001601 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001602 if (unlikely(old)) {
1603 dput(dentry);
1604 dentry = old;
1605 }
1606 return dentry;
1607}
1608
Al Viro4cb64022022-07-03 22:20:20 -04001609static struct dentry *lookup_fast(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Nick Piggin31e6b012011-01-07 17:49:52 +11001611 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001612 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001613
Al Viro3cac2602009-08-13 18:27:43 +04001614 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001615 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001616 * of a false negative due to a concurrent rename, the caller is
1617 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001618 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001619 if (nd->flags & LOOKUP_RCU) {
Al Viro03fa86e2022-07-04 18:12:39 -04001620 dentry = __d_lookup_rcu(parent, &nd->last, &nd->next_seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001621 if (unlikely(!dentry)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001622 if (!try_to_unlazy(nd))
Al Viro20e34352020-01-09 14:58:31 -05001623 return ERR_PTR(-ECHILD);
1624 return NULL;
Al Viro5d0f49c2016-03-05 21:32:53 -05001625 }
Al Viro5a18fff2011-03-11 04:44:53 -05001626
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001627 /*
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001628 * This sequence count validates that the parent had no
1629 * changes while we did the lookup of the dentry above.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001630 */
Al Viro4cb64022022-07-03 22:20:20 -04001631 if (read_seqcount_retry(&parent->d_seq, nd->seq))
Al Viro20e34352020-01-09 14:58:31 -05001632 return ERR_PTR(-ECHILD);
Al Viro5a18fff2011-03-11 04:44:53 -05001633
Al Viroa89f8332017-01-09 22:25:28 -05001634 status = d_revalidate(dentry, nd->flags);
Al Viroc1530072020-01-09 14:50:18 -05001635 if (likely(status > 0))
Al Viro20e34352020-01-09 14:58:31 -05001636 return dentry;
Al Viro03fa86e2022-07-04 18:12:39 -04001637 if (!try_to_unlazy_next(nd, dentry))
Al Viro20e34352020-01-09 14:58:31 -05001638 return ERR_PTR(-ECHILD);
Steven Rostedt (VMware)26ddb452020-12-09 17:09:28 -05001639 if (status == -ECHILD)
Al Viro209a7fb2017-01-09 01:35:39 -05001640 /* we'd been told to redo it in non-rcu mode */
1641 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001642 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001643 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001644 if (unlikely(!dentry))
Al Viro20e34352020-01-09 14:58:31 -05001645 return NULL;
Al Viroa89f8332017-01-09 22:25:28 -05001646 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001647 }
Al Viro5a18fff2011-03-11 04:44:53 -05001648 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001649 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001650 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001651 dput(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001652 return ERR_PTR(status);
Al Viro5a18fff2011-03-11 04:44:53 -05001653 }
Al Viro20e34352020-01-09 14:58:31 -05001654 return dentry;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001655}
1656
1657/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001658static struct dentry *__lookup_slow(const struct qstr *name,
1659 struct dentry *dir,
1660 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001661{
Al Viro88d83312018-04-06 16:43:47 -04001662 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001663 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001664 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001665
Al Viro19363862016-04-14 19:33:34 -04001666 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001667 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001668 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001669again:
Al Virod9171b92016-04-15 03:33:13 -04001670 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001671 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001672 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001673 if (unlikely(!d_in_lookup(dentry))) {
Al Viroc64cd6e2020-01-10 17:17:19 -05001674 int error = d_revalidate(dentry, flags);
1675 if (unlikely(error <= 0)) {
1676 if (!error) {
1677 d_invalidate(dentry);
Al Viro949a8522016-03-06 14:20:52 -05001678 dput(dentry);
Al Viroc64cd6e2020-01-10 17:17:19 -05001679 goto again;
Al Viro949a8522016-03-06 14:20:52 -05001680 }
Al Viroc64cd6e2020-01-10 17:17:19 -05001681 dput(dentry);
1682 dentry = ERR_PTR(error);
Al Viro949a8522016-03-06 14:20:52 -05001683 }
Al Viro94bdd652016-04-15 02:42:04 -04001684 } else {
1685 old = inode->i_op->lookup(inode, dentry, flags);
1686 d_lookup_done(dentry);
1687 if (unlikely(old)) {
1688 dput(dentry);
1689 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001690 }
1691 }
Al Viroe3c13922016-03-06 14:03:27 -05001692 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Al Viro88d83312018-04-06 16:43:47 -04001695static struct dentry *lookup_slow(const struct qstr *name,
1696 struct dentry *dir,
1697 unsigned int flags)
1698{
1699 struct inode *inode = dir->d_inode;
1700 struct dentry *res;
1701 inode_lock_shared(inode);
1702 res = __lookup_slow(name, dir, flags);
1703 inode_unlock_shared(inode);
1704 return res;
1705}
1706
Christian Braunerba73d982021-01-21 14:19:31 +01001707static inline int may_lookup(struct user_namespace *mnt_userns,
1708 struct nameidata *nd)
Al Viro52094c82011-02-21 21:34:47 -05001709{
1710 if (nd->flags & LOOKUP_RCU) {
Linus Torvalds7d6beb72021-02-23 13:39:45 -08001711 int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Jens Axboee36cffe2020-12-17 09:19:08 -07001712 if (err != -ECHILD || !try_to_unlazy(nd))
Al Viro52094c82011-02-21 21:34:47 -05001713 return err;
Al Viro52094c82011-02-21 21:34:47 -05001714 }
Christian Braunerba73d982021-01-21 14:19:31 +01001715 return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001716}
1717
Al Viro03fa86e2022-07-04 18:12:39 -04001718static int reserve_stack(struct nameidata *nd, struct path *link)
Al Viro49055902020-03-03 11:22:34 -05001719{
Al Viro49055902020-03-03 11:22:34 -05001720 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1721 return -ELOOP;
Al Viro45425762020-03-03 11:25:31 -05001722
1723 if (likely(nd->depth != EMBEDDED_LEVELS))
1724 return 0;
1725 if (likely(nd->stack != nd->internal))
1726 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001727 if (likely(nd_alloc_stack(nd)))
Al Viro49055902020-03-03 11:22:34 -05001728 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001729
1730 if (nd->flags & LOOKUP_RCU) {
1731 // we need to grab link before we do unlazy. And we can't skip
1732 // unlazy even if we fail to grab the link - cleanup needs it
Al Viro03fa86e2022-07-04 18:12:39 -04001733 bool grabbed_link = legitimize_path(nd, link, nd->next_seq);
Al Viro60ef60c2020-03-03 11:43:55 -05001734
Al Viroe5ca0242022-01-07 12:20:37 -05001735 if (!try_to_unlazy(nd) || !grabbed_link)
Al Viro60ef60c2020-03-03 11:43:55 -05001736 return -ECHILD;
1737
1738 if (nd_alloc_stack(nd))
1739 return 0;
Al Viro49055902020-03-03 11:22:34 -05001740 }
Al Viro60ef60c2020-03-03 11:43:55 -05001741 return -ENOMEM;
Al Viro49055902020-03-03 11:22:34 -05001742}
1743
Al Virob1a81972020-01-19 12:48:44 -05001744enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1745
Al Viro06708ad2020-01-14 14:26:57 -05001746static const char *pick_link(struct nameidata *nd, struct path *link,
Al Viro03fa86e2022-07-04 18:12:39 -04001747 struct inode *inode, int flags)
Al Virod63ff282015-05-04 18:13:23 -04001748{
Al Viro1cf26652015-05-06 16:01:56 -04001749 struct saved *last;
Al Viroad6cc4c2020-01-14 14:41:39 -05001750 const char *res;
Al Viro03fa86e2022-07-04 18:12:39 -04001751 int error = reserve_stack(nd, link);
Al Viroad6cc4c2020-01-14 14:41:39 -05001752
Al Viro49055902020-03-03 11:22:34 -05001753 if (unlikely(error)) {
Al Viro84f0cd92020-03-03 10:14:30 -05001754 if (!(nd->flags & LOOKUP_RCU))
1755 path_put(link);
Al Viro49055902020-03-03 11:22:34 -05001756 return ERR_PTR(error);
Al Viro626de992015-05-04 18:26:59 -04001757 }
Al Viroab104922015-05-10 11:50:01 -04001758 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001759 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001760 clear_delayed_call(&last->done);
Al Viro03fa86e2022-07-04 18:12:39 -04001761 last->seq = nd->next_seq;
Al Viroad6cc4c2020-01-14 14:41:39 -05001762
Al Virob1a81972020-01-19 12:48:44 -05001763 if (flags & WALK_TRAILING) {
Al Viroad6cc4c2020-01-14 14:41:39 -05001764 error = may_follow_link(nd, inode);
1765 if (unlikely(error))
1766 return ERR_PTR(error);
1767 }
1768
Mattias Nisslerdab741e02020-08-27 11:09:46 -06001769 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1770 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
Al Viroad6cc4c2020-01-14 14:41:39 -05001771 return ERR_PTR(-ELOOP);
1772
1773 if (!(nd->flags & LOOKUP_RCU)) {
1774 touch_atime(&last->link);
1775 cond_resched();
1776 } else if (atime_needs_update(&last->link, inode)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001777 if (!try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001778 return ERR_PTR(-ECHILD);
1779 touch_atime(&last->link);
1780 }
1781
1782 error = security_inode_follow_link(link->dentry, inode,
1783 nd->flags & LOOKUP_RCU);
1784 if (unlikely(error))
1785 return ERR_PTR(error);
1786
Al Viroad6cc4c2020-01-14 14:41:39 -05001787 res = READ_ONCE(inode->i_link);
1788 if (!res) {
1789 const char * (*get)(struct dentry *, struct inode *,
1790 struct delayed_call *);
1791 get = inode->i_op->get_link;
1792 if (nd->flags & LOOKUP_RCU) {
1793 res = get(NULL, inode, &last->done);
Jens Axboee36cffe2020-12-17 09:19:08 -07001794 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001795 res = get(link->dentry, inode, &last->done);
Al Viroad6cc4c2020-01-14 14:41:39 -05001796 } else {
1797 res = get(link->dentry, inode, &last->done);
1798 }
1799 if (!res)
1800 goto all_done;
1801 if (IS_ERR(res))
1802 return res;
1803 }
1804 if (*res == '/') {
1805 error = nd_jump_root(nd);
1806 if (unlikely(error))
1807 return ERR_PTR(error);
1808 while (unlikely(*++res == '/'))
1809 ;
1810 }
1811 if (*res)
1812 return res;
1813all_done: // pure jump
1814 put_link(nd);
1815 return NULL;
Al Virod63ff282015-05-04 18:13:23 -04001816}
1817
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001818/*
1819 * Do we need to follow links? We _really_ want to be able
1820 * to do this check without having to look at inode->i_op,
1821 * so we keep a cache of "no, this doesn't need follow_link"
1822 * for the common case.
Al Viro03fa86e2022-07-04 18:12:39 -04001823 *
1824 * NOTE: dentry must be what nd->next_seq had been sampled from.
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001825 */
Al Virob0417d22020-01-14 13:34:20 -05001826static const char *step_into(struct nameidata *nd, int flags,
Al Viroa4f5b5212022-07-03 22:07:32 -04001827 struct dentry *dentry)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001828{
Al Virocbae4d12020-01-12 13:40:02 -05001829 struct path path;
Al Viroa4f5b5212022-07-03 22:07:32 -04001830 struct inode *inode;
Al Viro3bd8bc82022-07-03 22:35:56 -04001831 int err = handle_mounts(nd, dentry, &path);
Al Virocbae4d12020-01-12 13:40:02 -05001832
1833 if (err < 0)
Al Virob0417d22020-01-14 13:34:20 -05001834 return ERR_PTR(err);
Al Viro3bd8bc82022-07-03 22:35:56 -04001835 inode = path.dentry->d_inode;
Al Virocbae4d12020-01-12 13:40:02 -05001836 if (likely(!d_is_symlink(path.dentry)) ||
Al Viro8c4efe22020-01-19 12:44:18 -05001837 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
Al Viroaca29032020-01-09 15:17:57 -05001838 (flags & WALK_NOFOLLOW)) {
Al Viro8f64fb12016-11-14 01:50:26 -05001839 /* not a symlink or should not follow */
Al Viro3bd8bc82022-07-03 22:35:56 -04001840 if (nd->flags & LOOKUP_RCU) {
1841 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq))
1842 return ERR_PTR(-ECHILD);
1843 if (unlikely(!inode))
1844 return ERR_PTR(-ENOENT);
1845 } else {
Al Viroc99687a2020-03-03 10:56:17 -05001846 dput(nd->path.dentry);
1847 if (nd->path.mnt != path.mnt)
1848 mntput(nd->path.mnt);
1849 }
1850 nd->path = path;
Al Viro8f64fb12016-11-14 01:50:26 -05001851 nd->inode = inode;
Al Viro03fa86e2022-07-04 18:12:39 -04001852 nd->seq = nd->next_seq;
Al Virob0417d22020-01-14 13:34:20 -05001853 return NULL;
Al Viro8f64fb12016-11-14 01:50:26 -05001854 }
Al Viroa7f77542016-02-27 19:31:01 -05001855 if (nd->flags & LOOKUP_RCU) {
Al Viro84f0cd92020-03-03 10:14:30 -05001856 /* make sure that d_is_symlink above matches inode */
Al Viro03fa86e2022-07-04 18:12:39 -04001857 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq))
Al Virob0417d22020-01-14 13:34:20 -05001858 return ERR_PTR(-ECHILD);
Al Viro84f0cd92020-03-03 10:14:30 -05001859 } else {
1860 if (path.mnt == nd->path.mnt)
1861 mntget(path.mnt);
Al Viroa7f77542016-02-27 19:31:01 -05001862 }
Al Viro03fa86e2022-07-04 18:12:39 -04001863 return pick_link(nd, &path, inode, flags);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001864}
1865
Al Virob16c0012022-07-03 22:18:11 -04001866static struct dentry *follow_dotdot_rcu(struct nameidata *nd)
Al Viro957dd412020-02-26 01:40:04 -05001867{
Al Viro12487f32020-02-26 14:59:56 -05001868 struct dentry *parent, *old;
Al Viro957dd412020-02-26 01:40:04 -05001869
Al Viro12487f32020-02-26 14:59:56 -05001870 if (path_equal(&nd->path, &nd->root))
1871 goto in_root;
1872 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro7ef482f2020-02-26 17:50:13 -05001873 struct path path;
Al Viroefe772d2020-02-28 10:06:37 -05001874 unsigned seq;
Al Viro7ef482f2020-02-26 17:50:13 -05001875 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1876 &nd->root, &path, &seq))
1877 goto in_root;
Al Viroefe772d2020-02-28 10:06:37 -05001878 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1879 return ERR_PTR(-ECHILD);
1880 nd->path = path;
1881 nd->inode = path.dentry->d_inode;
1882 nd->seq = seq;
Al Viro03fa86e2022-07-04 18:12:39 -04001883 // makes sure that non-RCU pathwalk could reach this state
Al Viro82ef0692022-07-05 11:23:58 -04001884 if (read_seqretry(&mount_lock, nd->m_seq))
Al Viroefe772d2020-02-28 10:06:37 -05001885 return ERR_PTR(-ECHILD);
1886 /* we know that mountpoint was pinned */
Al Viro957dd412020-02-26 01:40:04 -05001887 }
Al Viro12487f32020-02-26 14:59:56 -05001888 old = nd->path.dentry;
1889 parent = old->d_parent;
Al Viro03fa86e2022-07-04 18:12:39 -04001890 nd->next_seq = read_seqcount_begin(&parent->d_seq);
1891 // makes sure that non-RCU pathwalk could reach this state
Al Viro82ef0692022-07-05 11:23:58 -04001892 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viro12487f32020-02-26 14:59:56 -05001893 return ERR_PTR(-ECHILD);
1894 if (unlikely(!path_connected(nd->path.mnt, parent)))
1895 return ERR_PTR(-ECHILD);
1896 return parent;
1897in_root:
Al Viro82ef0692022-07-05 11:23:58 -04001898 if (read_seqretry(&mount_lock, nd->m_seq))
Al Viroefe772d2020-02-28 10:06:37 -05001899 return ERR_PTR(-ECHILD);
Al Viroc2df1962020-02-26 14:33:30 -05001900 if (unlikely(nd->flags & LOOKUP_BENEATH))
1901 return ERR_PTR(-ECHILD);
Al Viro03fa86e2022-07-04 18:12:39 -04001902 nd->next_seq = nd->seq;
Al Viro51c65462022-07-04 11:20:51 -04001903 return nd->path.dentry;
Al Viro957dd412020-02-26 01:40:04 -05001904}
1905
Al Virob16c0012022-07-03 22:18:11 -04001906static struct dentry *follow_dotdot(struct nameidata *nd)
Al Viro957dd412020-02-26 01:40:04 -05001907{
Al Viro12487f32020-02-26 14:59:56 -05001908 struct dentry *parent;
1909
1910 if (path_equal(&nd->path, &nd->root))
1911 goto in_root;
1912 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro2aa38472020-02-26 19:19:05 -05001913 struct path path;
1914
1915 if (!choose_mountpoint(real_mount(nd->path.mnt),
1916 &nd->root, &path))
1917 goto in_root;
Al Viro165200d2020-02-28 10:17:52 -05001918 path_put(&nd->path);
1919 nd->path = path;
Al Viro2aa38472020-02-26 19:19:05 -05001920 nd->inode = path.dentry->d_inode;
Al Viro165200d2020-02-28 10:17:52 -05001921 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1922 return ERR_PTR(-EXDEV);
Al Viro957dd412020-02-26 01:40:04 -05001923 }
Al Viro12487f32020-02-26 14:59:56 -05001924 /* rare case of legitimate dget_parent()... */
1925 parent = dget_parent(nd->path.dentry);
1926 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1927 dput(parent);
1928 return ERR_PTR(-ENOENT);
1929 }
Al Viro12487f32020-02-26 14:59:56 -05001930 return parent;
1931
1932in_root:
Al Viroc2df1962020-02-26 14:33:30 -05001933 if (unlikely(nd->flags & LOOKUP_BENEATH))
1934 return ERR_PTR(-EXDEV);
Al Viro51c65462022-07-04 11:20:51 -04001935 return dget(nd->path.dentry);
Al Viro957dd412020-02-26 01:40:04 -05001936}
1937
Al Viro7521f222020-02-26 12:22:58 -05001938static const char *handle_dots(struct nameidata *nd, int type)
Al Viro957dd412020-02-26 01:40:04 -05001939{
1940 if (type == LAST_DOTDOT) {
Al Viro7521f222020-02-26 12:22:58 -05001941 const char *error = NULL;
Al Viroc2df1962020-02-26 14:33:30 -05001942 struct dentry *parent;
Al Viro957dd412020-02-26 01:40:04 -05001943
1944 if (!nd->root.mnt) {
Al Viro7521f222020-02-26 12:22:58 -05001945 error = ERR_PTR(set_root(nd));
Al Viro957dd412020-02-26 01:40:04 -05001946 if (error)
1947 return error;
1948 }
1949 if (nd->flags & LOOKUP_RCU)
Al Virob16c0012022-07-03 22:18:11 -04001950 parent = follow_dotdot_rcu(nd);
Al Viro957dd412020-02-26 01:40:04 -05001951 else
Al Virob16c0012022-07-03 22:18:11 -04001952 parent = follow_dotdot(nd);
Al Viroc2df1962020-02-26 14:33:30 -05001953 if (IS_ERR(parent))
1954 return ERR_CAST(parent);
Al Viroa4f5b5212022-07-03 22:07:32 -04001955 error = step_into(nd, WALK_NOFOLLOW, parent);
Al Viroc2df1962020-02-26 14:33:30 -05001956 if (unlikely(error))
Al Viro957dd412020-02-26 01:40:04 -05001957 return error;
1958
1959 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1960 /*
1961 * If there was a racing rename or mount along our
1962 * path, then we can't be sure that ".." hasn't jumped
1963 * above nd->root (and so userspace should retry or use
1964 * some fallback).
1965 */
1966 smp_rmb();
Al Viro82ef0692022-07-05 11:23:58 -04001967 if (__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))
Al Viro7521f222020-02-26 12:22:58 -05001968 return ERR_PTR(-EAGAIN);
Al Viro82ef0692022-07-05 11:23:58 -04001969 if (__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))
Al Viro7521f222020-02-26 12:22:58 -05001970 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001971 }
1972 }
Al Viro7521f222020-02-26 12:22:58 -05001973 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001974}
1975
Al Viro92d27012020-01-14 13:24:17 -05001976static const char *walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001977{
Al Virodb3c9ad2020-01-09 14:41:00 -05001978 struct dentry *dentry;
Al Viroce57dfc2011-03-13 19:58:58 -04001979 /*
1980 * "." and ".." are special - ".." especially so because it has
1981 * to be able to know about the current root directory and
1982 * parent relationships.
1983 */
Al Viro4693a542015-05-04 17:47:11 -04001984 if (unlikely(nd->last_type != LAST_NORM)) {
Al Viro1c4ff1a2016-11-14 01:39:36 -05001985 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001986 put_link(nd);
Al Viro7521f222020-02-26 12:22:58 -05001987 return handle_dots(nd, nd->last_type);
Al Viro4693a542015-05-04 17:47:11 -04001988 }
Al Viro4cb64022022-07-03 22:20:20 -04001989 dentry = lookup_fast(nd);
Al Viro20e34352020-01-09 14:58:31 -05001990 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001991 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001992 if (unlikely(!dentry)) {
Al Virodb3c9ad2020-01-09 14:41:00 -05001993 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1994 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001995 return ERR_CAST(dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001996 }
Al Viro56676ec2020-03-10 21:54:54 -04001997 if (!(flags & WALK_MORE) && nd->depth)
1998 put_link(nd);
Al Viroa4f5b5212022-07-03 22:07:32 -04001999 return step_into(nd, flags, dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04002000}
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002003 * We can do the critical dentry name comparison and hashing
2004 * operations one word at a time, but we are limited to:
2005 *
2006 * - Architectures with fast unaligned word accesses. We could
2007 * do a "get_unaligned()" if this helps and is sufficiently
2008 * fast.
2009 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002010 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
2011 * do not trap on the (extremely unlikely) case of a page
2012 * crossing operation.
2013 *
2014 * - Furthermore, we need an efficient 64-bit compile for the
2015 * 64-bit case in order to generate the "number of bytes in
2016 * the final mask". Again, that could be replaced with a
2017 * efficient population count instruction or similar.
2018 */
2019#ifdef CONFIG_DCACHE_WORD_ACCESS
2020
Linus Torvaldsf68e5562012-04-06 13:54:56 -07002021#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002022
George Spelvin468a9422016-05-26 22:11:51 -04002023#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002024
George Spelvin468a9422016-05-26 22:11:51 -04002025/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
2026
2027#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04002028/*
2029 * Register pressure in the mixing function is an issue, particularly
2030 * on 32-bit x86, but almost any function requires one state value and
2031 * one temporary. Instead, use a function designed for two state values
2032 * and no temporaries.
2033 *
2034 * This function cannot create a collision in only two iterations, so
2035 * we have two iterations to achieve avalanche. In those two iterations,
2036 * we have six layers of mixing, which is enough to spread one bit's
2037 * influence out to 2^6 = 64 state bits.
2038 *
2039 * Rotate constants are scored by considering either 64 one-bit input
2040 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2041 * probability of that delta causing a change to each of the 128 output
2042 * bits, using a sample of random initial states.
2043 *
2044 * The Shannon entropy of the computed probabilities is then summed
2045 * to produce a score. Ideally, any input change has a 50% chance of
2046 * toggling any given output bit.
2047 *
2048 * Mixing scores (in bits) for (12,45):
2049 * Input delta: 1-bit 2-bit
2050 * 1 round: 713.3 42542.6
2051 * 2 rounds: 2753.7 140389.8
2052 * 3 rounds: 5954.1 233458.2
2053 * 4 rounds: 7862.6 256672.2
2054 * Perfect: 8192 258048
2055 * (64*128) (64*63/2 * 128)
2056 */
2057#define HASH_MIX(x, y, a) \
2058 ( x ^= (a), \
2059 y ^= x, x = rol64(x,12),\
2060 x += y, y = rol64(y,45),\
2061 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002062
George Spelvin0fed3ac2016-05-02 06:31:01 -04002063/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04002064 * Fold two longs into one 32-bit hash value. This must be fast, but
2065 * latency isn't quite as critical, as there is a fair bit of additional
2066 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04002067 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04002068static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002069{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002070 y ^= x * GOLDEN_RATIO_64;
2071 y *= GOLDEN_RATIO_64;
2072 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04002073}
2074
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002075#else /* 32-bit case */
2076
George Spelvin2a18da7a2016-05-23 07:43:58 -04002077/*
2078 * Mixing scores (in bits) for (7,20):
2079 * Input delta: 1-bit 2-bit
2080 * 1 round: 330.3 9201.6
2081 * 2 rounds: 1246.4 25475.4
2082 * 3 rounds: 1907.1 31295.1
2083 * 4 rounds: 2042.3 31718.6
2084 * Perfect: 2048 31744
2085 * (32*64) (32*31/2 * 64)
2086 */
2087#define HASH_MIX(x, y, a) \
2088 ( x ^= (a), \
2089 y ^= x, x = rol32(x, 7),\
2090 x += y, y = rol32(y,20),\
2091 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002092
George Spelvin2a18da7a2016-05-23 07:43:58 -04002093static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002094{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002095 /* Use arch-optimized multiply if one exists */
2096 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04002097}
2098
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002099#endif
2100
George Spelvin2a18da7a2016-05-23 07:43:58 -04002101/*
2102 * Return the hash of a string of known length. This is carfully
2103 * designed to match hash_name(), which is the more critical function.
2104 * In particular, we must end by hashing a final word containing 0..7
2105 * payload bytes, to match the way that hash_name() iterates until it
2106 * finds the delimiter after the name.
2107 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002108unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002109{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002110 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002111
2112 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002113 if (!len)
2114 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002115 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002116 if (len < sizeof(unsigned long))
2117 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04002118 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002119 name += sizeof(unsigned long);
2120 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002121 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04002122 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002123done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04002124 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002125}
2126EXPORT_SYMBOL(full_name_hash);
2127
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002128/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002129u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002130{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002131 unsigned long a = 0, x = 0, y = (unsigned long)salt;
2132 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002133 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2134
Linus Torvalds8387ff22016-06-10 07:51:30 -07002135 len = 0;
2136 goto inside;
2137
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002138 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002139 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002140 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002141inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002142 a = load_unaligned_zeropad(name+len);
2143 } while (!has_zero(a, &adata, &constants));
2144
2145 adata = prep_zero_mask(a, adata, &constants);
2146 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002147 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002148
George Spelvin2a18da7a2016-05-23 07:43:58 -04002149 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002150}
2151EXPORT_SYMBOL(hashlen_string);
2152
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002153/*
2154 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002155 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002156 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002157static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002158{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002159 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2160 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07002161 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002162
Linus Torvalds8387ff22016-06-10 07:51:30 -07002163 len = 0;
2164 goto inside;
2165
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002166 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002167 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002168 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002169inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002170 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002171 b = a ^ REPEAT_BYTE('/');
2172 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002173
Linus Torvalds36126f82012-05-26 10:43:17 -07002174 adata = prep_zero_mask(a, adata, &constants);
2175 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002176 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002177 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002178
George Spelvin2a18da7a2016-05-23 07:43:58 -04002179 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002180}
2181
George Spelvin2a18da7a2016-05-23 07:43:58 -04002182#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002183
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002184/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002185unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002186{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002187 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002188 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002189 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002190 return end_name_hash(hash);
2191}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002192EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002193
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002194/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002195u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002196{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002197 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002198 unsigned long len = 0, c;
2199
2200 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002201 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002202 len++;
2203 hash = partial_name_hash(c, hash);
2204 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002205 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002206 return hashlen_create(end_name_hash(hash), len);
2207}
George Spelvinf2a031b2016-05-29 01:26:41 -04002208EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002209
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002210/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002211 * We know there's a real path component here of at least
2212 * one character.
2213 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002214static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002215{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002216 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002217 unsigned long len = 0, c;
2218
2219 c = (unsigned char)*name;
2220 do {
2221 len++;
2222 hash = partial_name_hash(c, hash);
2223 c = (unsigned char)name[len];
2224 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002225 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002226}
2227
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002228#endif
2229
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002232 * This is the basic name resolution function, turning a pathname into
2233 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002235 * Returns 0 and nd will have valid dentry and mnt on success.
2236 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 */
Al Viro6de88d72009-08-09 01:41:57 +04002238static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Al Virod8d46112020-02-23 22:04:15 -05002240 int depth = 0; // depth <= nd->depth
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002242
Al Virob4c03532020-01-19 11:44:51 -05002243 nd->last_type = LAST_ROOT;
Al Viroc108837e2020-03-05 15:48:44 -05002244 nd->flags |= LOOKUP_PARENT;
Al Viro9b5858e2018-07-09 16:33:23 -04002245 if (IS_ERR(name))
2246 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 while (*name=='/')
2248 name++;
Al Viro1a97d892020-09-19 17:55:58 +01002249 if (!*name) {
2250 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
Al Viro9e18f102015-04-18 20:44:34 -04002251 return 0;
Al Viro1a97d892020-09-19 17:55:58 +01002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /* At this point we know we have a real path component. */
2255 for(;;) {
Christian Brauner549c7292021-01-21 14:19:43 +01002256 struct user_namespace *mnt_userns;
Al Viro92d27012020-01-14 13:24:17 -05002257 const char *link;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002258 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002259 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Christian Brauner549c7292021-01-21 14:19:43 +01002261 mnt_userns = mnt_user_ns(nd->path.mnt);
2262 err = may_lookup(mnt_userns, nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002263 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Linus Torvalds8387ff22016-06-10 07:51:30 -07002266 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Al Virofe479a52011-02-22 15:10:03 -05002268 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002269 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002270 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002271 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002272 type = LAST_DOTDOT;
Al Virobcba1e72021-04-01 22:03:41 -04002273 nd->state |= ND_JUMPED;
Al Viro16c2cd72011-02-22 15:50:10 -05002274 }
Al Virofe479a52011-02-22 15:10:03 -05002275 break;
2276 case 1:
2277 type = LAST_DOT;
2278 }
Al Viro5a202bc2011-03-08 14:17:44 -05002279 if (likely(type == LAST_NORM)) {
2280 struct dentry *parent = nd->path.dentry;
Al Virobcba1e72021-04-01 22:03:41 -04002281 nd->state &= ~ND_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002282 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002283 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002284 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002285 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002286 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002287 hash_len = this.hash_len;
2288 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002289 }
2290 }
Al Virofe479a52011-02-22 15:10:03 -05002291
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002292 nd->last.hash_len = hash_len;
2293 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002294 nd->last_type = type;
2295
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002296 name += hashlen_len(hash_len);
2297 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002298 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002299 /*
2300 * If it wasn't NUL, we know it was '/'. Skip that
2301 * slash, and continue until no more slashes.
2302 */
2303 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002304 name++;
2305 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002306 if (unlikely(!*name)) {
2307OK:
Al Virod8d46112020-02-23 22:04:15 -05002308 /* pathname or trailing symlink, done */
Al Viroc108837e2020-03-05 15:48:44 -05002309 if (!depth) {
Christian Brauner549c7292021-01-21 14:19:43 +01002310 nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode);
Al Viro0f705952020-03-05 11:34:48 -05002311 nd->dir_mode = nd->inode->i_mode;
Al Viroc108837e2020-03-05 15:48:44 -05002312 nd->flags &= ~LOOKUP_PARENT;
Al Viro8620c232015-05-04 08:58:35 -04002313 return 0;
Al Viroc108837e2020-03-05 15:48:44 -05002314 }
Al Viro8620c232015-05-04 08:58:35 -04002315 /* last component of nested symlink */
Al Virod8d46112020-02-23 22:04:15 -05002316 name = nd->stack[--depth].name;
Al Viro8c4efe22020-01-19 12:44:18 -05002317 link = walk_component(nd, 0);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002318 } else {
2319 /* not the last component */
Al Viro8c4efe22020-01-19 12:44:18 -05002320 link = walk_component(nd, WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002321 }
Al Viro92d27012020-01-14 13:24:17 -05002322 if (unlikely(link)) {
2323 if (IS_ERR(link))
2324 return PTR_ERR(link);
2325 /* a symlink to follow */
Al Virod8d46112020-02-23 22:04:15 -05002326 nd->stack[depth++].name = name;
Al Viro92d27012020-01-14 13:24:17 -05002327 name = link;
2328 continue;
Nick Piggin31e6b012011-01-07 17:49:52 +11002329 }
Al Viro97242f92015-08-01 19:59:28 -04002330 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2331 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07002332 if (!try_to_unlazy(nd))
Al Viro97242f92015-08-01 19:59:28 -04002333 return -ECHILD;
2334 }
Al Viro3595e232015-05-09 16:54:45 -04002335 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
Al Viroedc2b1d2018-07-09 16:27:23 -04002340/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002341static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Aleksa Sarai740a1672019-12-07 01:13:29 +11002343 int error;
Al Viroc8a53ee2015-05-12 18:43:07 -04002344 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Jens Axboe6c6ec2b2020-12-17 09:19:09 -07002346 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2347 if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
2348 return ERR_PTR(-EAGAIN);
2349
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002350 if (!*s)
2351 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002352 if (flags & LOOKUP_RCU)
2353 rcu_read_lock();
Al Viro03fa86e2022-07-04 18:12:39 -04002354 else
2355 nd->seq = nd->next_seq = 0;
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002356
Al Virobcba1e72021-04-01 22:03:41 -04002357 nd->flags = flags;
2358 nd->state |= ND_JUMPED;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002359
2360 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2361 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2362 smp_rmb();
2363
Al Virobcba1e72021-04-01 22:03:41 -04002364 if (nd->state & ND_ROOT_PRESET) {
David Howellsb18825a2013-09-12 19:22:53 +01002365 struct dentry *root = nd->root.dentry;
2366 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002367 if (*s && unlikely(!d_can_lookup(root)))
2368 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002369 nd->path = nd->root;
2370 nd->inode = inode;
2371 if (flags & LOOKUP_RCU) {
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002372 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002373 nd->root_seq = nd->seq;
Al Viro5b6ca022011-03-09 23:04:47 -05002374 } else {
2375 path_get(&nd->path);
2376 }
Al Viro368ee9b2015-05-08 17:19:59 -04002377 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002378 }
2379
Al Viro2a737872009-04-07 11:49:53 -04002380 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002382 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2383 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
Aleksa Sarai740a1672019-12-07 01:13:29 +11002384 error = nd_jump_root(nd);
2385 if (unlikely(error))
2386 return ERR_PTR(error);
2387 return s;
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002388 }
2389
2390 /* Relative pathname -- get the starting-point it is relative to. */
2391 if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002392 if (flags & LOOKUP_RCU) {
2393 struct fs_struct *fs = current->fs;
2394 unsigned seq;
2395
Al Viroe41f7d42011-02-22 14:02:58 -05002396 do {
2397 seq = read_seqcount_begin(&fs->seq);
2398 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002399 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002400 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2401 } while (read_seqcount_retry(&fs->seq, seq));
2402 } else {
2403 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002404 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002405 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002406 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002407 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002408 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002409 struct dentry *dentry;
2410
Al Viro2903ff02012-08-28 12:52:22 -04002411 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002412 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002413
Al Viro2903ff02012-08-28 12:52:22 -04002414 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002415
Al Viroedc2b1d2018-07-09 16:27:23 -04002416 if (*s && unlikely(!d_can_lookup(dentry))) {
2417 fdput(f);
2418 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002419 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002420
Al Viro2903ff02012-08-28 12:52:22 -04002421 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002422 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002423 nd->inode = nd->path.dentry->d_inode;
2424 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002425 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002426 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002427 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002428 }
Al Viro34a26b92015-05-11 08:05:05 -04002429 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002431
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002432 /* For scoped-lookups we need to set the root to the dirfd as well. */
2433 if (flags & LOOKUP_IS_SCOPED) {
2434 nd->root = nd->path;
2435 if (flags & LOOKUP_RCU) {
2436 nd->root_seq = nd->seq;
2437 } else {
2438 path_get(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -04002439 nd->state |= ND_ROOT_GRABBED;
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002440 }
2441 }
2442 return s;
Al Viro9b4a9b12009-04-07 11:44:16 -04002443}
2444
Al Viro1ccac622020-01-14 10:13:40 -05002445static inline const char *lookup_last(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002446{
Al Virobd92d7f2011-03-14 19:54:59 -04002447 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2448 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2449
Al Viroc108837e2020-03-05 15:48:44 -05002450 return walk_component(nd, WALK_TRAILING);
Al Virobd92d7f2011-03-14 19:54:59 -04002451}
2452
Al Viro4f757f32017-04-15 17:31:22 -04002453static int handle_lookup_down(struct nameidata *nd)
2454{
Al Viroc1530072020-01-09 14:50:18 -05002455 if (!(nd->flags & LOOKUP_RCU))
Al Virodb3c9ad2020-01-09 14:41:00 -05002456 dget(nd->path.dentry);
Al Viro03fa86e2022-07-04 18:12:39 -04002457 nd->next_seq = nd->seq;
Al Viroa4f5b5212022-07-03 22:07:32 -04002458 return PTR_ERR(step_into(nd, WALK_NOFOLLOW, nd->path.dentry));
Al Viro4f757f32017-04-15 17:31:22 -04002459}
2460
Al Viro9b4a9b12009-04-07 11:44:16 -04002461/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002462static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002463{
Al Viroc8a53ee2015-05-12 18:43:07 -04002464 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002465 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002466
Al Viro9b5858e2018-07-09 16:33:23 -04002467 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002468 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002469 if (unlikely(err < 0))
2470 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002471 }
2472
Al Viro1ccac622020-01-14 10:13:40 -05002473 while (!(err = link_path_walk(s, nd)) &&
2474 (s = lookup_last(nd)) != NULL)
2475 ;
Al Viro4f0ed932021-04-06 19:46:51 -04002476 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2477 err = handle_lookup_down(nd);
Al Virobcba1e72021-04-01 22:03:41 -04002478 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please...
Al Viro4f0ed932021-04-06 19:46:51 -04002479 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002480 if (!err)
2481 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002482
Al Virodeb106c2015-05-08 18:05:21 -04002483 if (!err && nd->flags & LOOKUP_DIRECTORY)
2484 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002485 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002486 if (!err) {
2487 *path = nd->path;
2488 nd->path.mnt = NULL;
2489 nd->path.dentry = NULL;
2490 }
2491 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002492 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002493}
Nick Piggin31e6b012011-01-07 17:49:52 +11002494
Stephen Brennan794ebce2021-09-01 10:51:42 -07002495int filename_lookup(int dfd, struct filename *name, unsigned flags,
David Howells31d921c2018-11-01 23:07:24 +00002496 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002497{
Al Viro894bc8c2015-05-02 07:16:16 -04002498 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002499 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002500 if (IS_ERR(name))
2501 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002502 set_nameidata(&nd, dfd, name, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04002503 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002504 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002505 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002506 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002507 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002508
2509 if (likely(!retval))
Al Viro161aff12020-01-11 22:52:26 -05002510 audit_inode(name, path->dentry,
2511 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
Al Viro9883d182015-05-13 07:28:08 -04002512 restore_nameidata();
Dmitry Kadashev020250f2021-07-08 13:34:43 +07002513 return retval;
2514}
2515
Al Viro8bcb77f2015-05-08 16:59:20 -04002516/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002517static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002518 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002519{
Al Viroc8a53ee2015-05-12 18:43:07 -04002520 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002521 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002522 if (!err)
2523 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002524 if (!err) {
2525 *parent = nd->path;
2526 nd->path.mnt = NULL;
2527 nd->path.dentry = NULL;
2528 }
2529 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002530 return err;
2531}
2532
Stephen Brennan0766ec82021-09-01 10:51:41 -07002533/* Note: this does not consume "name" */
Al Viroc5f563f2021-09-07 15:57:42 -04002534static int filename_parentat(int dfd, struct filename *name,
Stephen Brennan0766ec82021-09-01 10:51:41 -07002535 unsigned int flags, struct path *parent,
2536 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002537{
2538 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002539 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002540
Al Viro5c31b6c2015-05-12 17:32:54 -04002541 if (IS_ERR(name))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002542 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002543 set_nameidata(&nd, dfd, name, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04002544 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002545 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002546 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002547 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002548 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002549 if (likely(!retval)) {
2550 *last = nd.last;
2551 *type = nd.last_type;
Al Viroc9b07ea2019-07-14 13:22:27 -04002552 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002553 }
Al Viro9883d182015-05-13 07:28:08 -04002554 restore_nameidata();
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002555 return retval;
2556}
2557
Al Viro79714f72012-06-15 03:01:42 +04002558/* does lookup, returns the object with parent locked */
Stephen Brennan0766ec82021-09-01 10:51:41 -07002559static struct dentry *__kern_path_locked(struct filename *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002560{
Al Viro5c31b6c2015-05-12 17:32:54 -04002561 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002562 struct qstr last;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002563 int type, error;
Paul Moore51689102015-01-22 00:00:03 -05002564
Al Viroc5f563f2021-09-07 15:57:42 -04002565 error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002566 if (error)
2567 return ERR_PTR(error);
Al Viro5c31b6c2015-05-12 17:32:54 -04002568 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002569 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002570 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002571 }
Al Viro59551022016-01-22 15:40:57 -05002572 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002573 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002574 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002575 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002576 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002577 }
Al Viro79714f72012-06-15 03:01:42 +04002578 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002579}
2580
Stephen Brennan0766ec82021-09-01 10:51:41 -07002581struct dentry *kern_path_locked(const char *name, struct path *path)
2582{
2583 struct filename *filename = getname_kernel(name);
2584 struct dentry *res = __kern_path_locked(filename, path);
2585
2586 putname(filename);
2587 return res;
2588}
2589
Al Virod1811462008-08-02 00:49:18 -04002590int kern_path(const char *name, unsigned int flags, struct path *path)
2591{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002592 struct filename *filename = getname_kernel(name);
2593 int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL);
2594
2595 putname(filename);
2596 return ret;
2597
Al Virod1811462008-08-02 00:49:18 -04002598}
Al Viro4d359502014-03-14 12:20:17 -04002599EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002600
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002601/**
2602 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2603 * @dentry: pointer to dentry of the base directory
2604 * @mnt: pointer to vfs mount of the base directory
2605 * @name: pointer to file name
2606 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002607 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002608 */
2609int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2610 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002611 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002612{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002613 struct filename *filename;
Al Viro9ad1aaa2015-05-12 16:44:39 -04002614 struct path root = {.mnt = mnt, .dentry = dentry};
Stephen Brennan794ebce2021-09-01 10:51:42 -07002615 int ret;
2616
2617 filename = getname_kernel(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002618 /* the first argument of filename_lookup() is ignored with root */
Stephen Brennan794ebce2021-09-01 10:51:42 -07002619 ret = filename_lookup(AT_FDCWD, filename, flags, path, &root);
2620 putname(filename);
2621 return ret;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002622}
Al Viro4d359502014-03-14 12:20:17 -04002623EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002624
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002625static int lookup_one_common(struct user_namespace *mnt_userns,
2626 const char *name, struct dentry *base, int len,
2627 struct qstr *this)
Al Viro3c95f0d2018-04-06 16:32:38 -04002628{
2629 this->name = name;
2630 this->len = len;
2631 this->hash = full_name_hash(base, name, len);
2632 if (!len)
2633 return -EACCES;
2634
2635 if (unlikely(name[0] == '.')) {
2636 if (len < 2 || (len == 2 && name[1] == '.'))
2637 return -EACCES;
2638 }
2639
2640 while (len--) {
2641 unsigned int c = *(const unsigned char *)name++;
2642 if (c == '/' || c == '\0')
2643 return -EACCES;
2644 }
2645 /*
2646 * See if the low-level filesystem might want
2647 * to use its own hash..
2648 */
2649 if (base->d_flags & DCACHE_OP_HASH) {
2650 int err = base->d_op->d_hash(base, this);
2651 if (err < 0)
2652 return err;
2653 }
2654
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002655 return inode_permission(mnt_userns, base->d_inode, MAY_EXEC);
Al Viro3c95f0d2018-04-06 16:32:38 -04002656}
2657
Christoph Hellwigeead1912007-10-16 23:25:38 -07002658/**
David Howells0da0b7f2018-06-15 15:19:22 +01002659 * try_lookup_one_len - filesystem helper to lookup single pathname component
2660 * @name: pathname component to lookup
2661 * @base: base directory to lookup from
2662 * @len: maximum length @len should be interpreted to
2663 *
2664 * Look up a dentry by name in the dcache, returning NULL if it does not
2665 * currently exist. The function does not try to create a dentry.
2666 *
2667 * Note that this routine is purely a helper for filesystem usage and should
2668 * not be called by generic code.
2669 *
2670 * The caller must hold base->i_mutex.
2671 */
2672struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2673{
2674 struct qstr this;
2675 int err;
2676
2677 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2678
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002679 err = lookup_one_common(&init_user_ns, name, base, len, &this);
David Howells0da0b7f2018-06-15 15:19:22 +01002680 if (err)
2681 return ERR_PTR(err);
2682
2683 return lookup_dcache(&this, base, 0);
2684}
2685EXPORT_SYMBOL(try_lookup_one_len);
2686
2687/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002688 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002689 * @name: pathname component to lookup
2690 * @base: base directory to lookup from
2691 * @len: maximum length @len should be interpreted to
2692 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002693 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002694 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002695 *
2696 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002697 */
James Morris057f6c02007-04-26 00:12:05 -07002698struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2699{
Al Viro8613a202018-04-06 16:45:33 -04002700 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002701 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002702 int err;
James Morris057f6c02007-04-26 00:12:05 -07002703
Al Viro59551022016-01-22 15:40:57 -05002704 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002705
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002706 err = lookup_one_common(&init_user_ns, name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002707 if (err)
2708 return ERR_PTR(err);
2709
Al Viro8613a202018-04-06 16:45:33 -04002710 dentry = lookup_dcache(&this, base, 0);
2711 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002712}
Al Viro4d359502014-03-14 12:20:17 -04002713EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002714
NeilBrownbbddca82016-01-07 16:08:20 -05002715/**
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002716 * lookup_one - filesystem helper to lookup single pathname component
2717 * @mnt_userns: user namespace of the mount the lookup is performed from
2718 * @name: pathname component to lookup
2719 * @base: base directory to lookup from
2720 * @len: maximum length @len should be interpreted to
2721 *
2722 * Note that this routine is purely a helper for filesystem usage and should
2723 * not be called by generic code.
2724 *
2725 * The caller must hold base->i_mutex.
2726 */
2727struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name,
2728 struct dentry *base, int len)
2729{
2730 struct dentry *dentry;
2731 struct qstr this;
2732 int err;
2733
2734 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2735
2736 err = lookup_one_common(mnt_userns, name, base, len, &this);
2737 if (err)
2738 return ERR_PTR(err);
2739
2740 dentry = lookup_dcache(&this, base, 0);
2741 return dentry ? dentry : __lookup_slow(&this, base, 0);
2742}
2743EXPORT_SYMBOL(lookup_one);
2744
2745/**
Christian Brauner00675017e2022-04-04 12:51:40 +02002746 * lookup_one_unlocked - filesystem helper to lookup single pathname component
2747 * @mnt_userns: idmapping of the mount the lookup is performed from
2748 * @name: pathname component to lookup
2749 * @base: base directory to lookup from
2750 * @len: maximum length @len should be interpreted to
2751 *
2752 * Note that this routine is purely a helper for filesystem usage and should
2753 * not be called by generic code.
2754 *
2755 * Unlike lookup_one_len, it should be called without the parent
2756 * i_mutex held, and will take the i_mutex itself if necessary.
2757 */
2758struct dentry *lookup_one_unlocked(struct user_namespace *mnt_userns,
2759 const char *name, struct dentry *base,
2760 int len)
2761{
2762 struct qstr this;
2763 int err;
2764 struct dentry *ret;
2765
2766 err = lookup_one_common(mnt_userns, name, base, len, &this);
2767 if (err)
2768 return ERR_PTR(err);
2769
2770 ret = lookup_dcache(&this, base, 0);
2771 if (!ret)
2772 ret = lookup_slow(&this, base, 0);
2773 return ret;
2774}
2775EXPORT_SYMBOL(lookup_one_unlocked);
2776
2777/**
2778 * lookup_one_positive_unlocked - filesystem helper to lookup single
2779 * pathname component
2780 * @mnt_userns: idmapping of the mount the lookup is performed from
2781 * @name: pathname component to lookup
2782 * @base: base directory to lookup from
2783 * @len: maximum length @len should be interpreted to
2784 *
2785 * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
2786 * known positive or ERR_PTR(). This is what most of the users want.
2787 *
2788 * Note that pinned negative with unlocked parent _can_ become positive at any
2789 * time, so callers of lookup_one_unlocked() need to be very careful; pinned
2790 * positives have >d_inode stable, so this one avoids such problems.
2791 *
2792 * Note that this routine is purely a helper for filesystem usage and should
2793 * not be called by generic code.
2794 *
2795 * The helper should be called without i_mutex held.
2796 */
2797struct dentry *lookup_one_positive_unlocked(struct user_namespace *mnt_userns,
2798 const char *name,
2799 struct dentry *base, int len)
2800{
2801 struct dentry *ret = lookup_one_unlocked(mnt_userns, name, base, len);
2802
2803 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
2804 dput(ret);
2805 ret = ERR_PTR(-ENOENT);
2806 }
2807 return ret;
2808}
2809EXPORT_SYMBOL(lookup_one_positive_unlocked);
2810
2811/**
NeilBrownbbddca82016-01-07 16:08:20 -05002812 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2813 * @name: pathname component to lookup
2814 * @base: base directory to lookup from
2815 * @len: maximum length @len should be interpreted to
2816 *
2817 * Note that this routine is purely a helper for filesystem usage and should
2818 * not be called by generic code.
2819 *
2820 * Unlike lookup_one_len, it should be called without the parent
2821 * i_mutex held, and will take the i_mutex itself if necessary.
2822 */
2823struct dentry *lookup_one_len_unlocked(const char *name,
2824 struct dentry *base, int len)
2825{
Christian Brauner00675017e2022-04-04 12:51:40 +02002826 return lookup_one_unlocked(&init_user_ns, name, base, len);
NeilBrownbbddca82016-01-07 16:08:20 -05002827}
2828EXPORT_SYMBOL(lookup_one_len_unlocked);
2829
Al Viro6c2d47982019-10-31 01:21:58 -04002830/*
2831 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2832 * on negatives. Returns known positive or ERR_PTR(); that's what
2833 * most of the users want. Note that pinned negative with unlocked parent
2834 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2835 * need to be very careful; pinned positives have ->d_inode stable, so
2836 * this one avoids such problems.
2837 */
2838struct dentry *lookup_positive_unlocked(const char *name,
2839 struct dentry *base, int len)
2840{
Christian Brauner00675017e2022-04-04 12:51:40 +02002841 return lookup_one_positive_unlocked(&init_user_ns, name, base, len);
Al Viro6c2d47982019-10-31 01:21:58 -04002842}
2843EXPORT_SYMBOL(lookup_positive_unlocked);
2844
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002845#ifdef CONFIG_UNIX98_PTYS
2846int path_pts(struct path *path)
2847{
2848 /* Find something mounted on "pts" in the same directory as
2849 * the input path.
2850 */
Al Viroa6a7eb72020-03-11 13:05:03 -04002851 struct dentry *parent = dget_parent(path->dentry);
2852 struct dentry *child;
Al Viro19f60282020-02-26 20:09:37 -05002853 struct qstr this = QSTR_INIT("pts", 3);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002854
Al Viroa6a7eb72020-03-11 13:05:03 -04002855 if (unlikely(!path_connected(path->mnt, parent))) {
2856 dput(parent);
Al Viro63b27722020-02-24 16:01:19 -05002857 return -ENOENT;
Al Viroa6a7eb72020-03-11 13:05:03 -04002858 }
Al Viro63b27722020-02-24 16:01:19 -05002859 dput(path->dentry);
2860 path->dentry = parent;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002861 child = d_hash_and_lookup(parent, &this);
2862 if (!child)
2863 return -ENOENT;
2864
2865 path->dentry = child;
2866 dput(parent);
Al Viro19f60282020-02-26 20:09:37 -05002867 follow_down(path);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002868 return 0;
2869}
2870#endif
2871
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002872int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2873 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002875 struct filename *filename = getname_flags(name, flags, empty);
2876 int ret = filename_lookup(dfd, filename, flags, path, NULL);
2877
2878 putname(filename);
2879 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
Al Virob853a162015-05-13 09:12:02 -04002881EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002882
Christian Braunerba73d982021-01-21 14:19:31 +01002883int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
2884 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002886 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002887
Christian Braunerba73d982021-01-21 14:19:31 +01002888 if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002890 if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002892 return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002894EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896/*
2897 * Check whether we can remove a link victim from directory dir, check
2898 * whether the type of victim is right.
2899 * 1. We can't do it if dir is read-only (done in permission())
2900 * 2. We should have write and exec permissions on dir
2901 * 3. We can't remove anything from append-only dir
2902 * 4. We can't do anything with immutable dir (done in permission())
2903 * 5. If the sticky bit on dir is set we should either
2904 * a. be owner of dir, or
2905 * b. be owner of victim, or
2906 * c. have CAP_FOWNER capability
2907 * 6. If the victim is append-only or immutable we can't do antyhing with
2908 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002909 * 7. If the victim has an unknown uid or gid we can't change the inode.
2910 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2911 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2912 * 10. We can't remove a root or mountpoint.
2913 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 * nfs_async_unlink().
2915 */
Christian Braunerba73d982021-01-21 14:19:31 +01002916static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
2917 struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
David Howells63afdfc2015-05-06 15:59:00 +01002919 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 int error;
2921
David Howellsb18825a2013-09-12 19:22:53 +01002922 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002924 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002927
2928 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01002929 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
2930 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002931 return -EOVERFLOW;
2932
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002933 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Christian Braunerba73d982021-01-21 14:19:31 +01002935 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (error)
2937 return error;
2938 if (IS_APPEND(dir))
2939 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002940
Christian Braunerba73d982021-01-21 14:19:31 +01002941 if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
2942 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
2943 HAS_UNMAPPED_ID(mnt_userns, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return -EPERM;
2945 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002946 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return -ENOTDIR;
2948 if (IS_ROOT(victim))
2949 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002950 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return -EISDIR;
2952 if (IS_DEADDIR(dir))
2953 return -ENOENT;
2954 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2955 return -EBUSY;
2956 return 0;
2957}
2958
2959/* Check whether we can create an object with dentry child in directory
2960 * dir.
2961 * 1. We can't do it if child already exists (open has special treatment for
2962 * this case, but since we are inlined it's OK)
2963 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002964 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2965 * 4. We should have write and exec permissions on dir
2966 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 */
Christian Braunerba73d982021-01-21 14:19:31 +01002968static inline int may_create(struct user_namespace *mnt_userns,
2969 struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Jeff Layton14e972b2013-05-08 10:25:58 -04002971 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (child->d_inode)
2973 return -EEXIST;
2974 if (IS_DEADDIR(dir))
2975 return -ENOENT;
Christian Brauner8e538912021-03-20 13:26:23 +01002976 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Eric W. Biederman036d5232016-07-01 12:52:06 -05002977 return -EOVERFLOW;
Christian Brauner8e538912021-03-20 13:26:23 +01002978
Christian Braunerba73d982021-01-21 14:19:31 +01002979 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982/*
2983 * p1 and p2 should be directories on the same fs.
2984 */
2985struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2986{
2987 struct dentry *p;
2988
2989 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002990 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return NULL;
2992 }
2993
Al Virofc640052016-04-10 01:33:30 -04002994 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002996 p = d_ancestor(p2, p1);
2997 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002998 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2999 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003000 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003003 p = d_ancestor(p1, p2);
3004 if (p) {
Al Viro59551022016-01-22 15:40:57 -05003005 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
3006 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003007 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
3009
Al Viro59551022016-01-22 15:40:57 -05003010 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
3011 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return NULL;
3013}
Al Viro4d359502014-03-14 12:20:17 -04003014EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016void unlock_rename(struct dentry *p1, struct dentry *p2)
3017{
Al Viro59551022016-01-22 15:40:57 -05003018 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05003020 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04003021 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
3023}
Al Viro4d359502014-03-14 12:20:17 -04003024EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Christian Brauner6521f892021-01-21 14:19:33 +01003026/**
Yang Xu1639a492022-07-14 14:11:27 +08003027 * mode_strip_umask - handle vfs umask stripping
3028 * @dir: parent directory of the new inode
3029 * @mode: mode of the new inode to be created in @dir
3030 *
3031 * Umask stripping depends on whether or not the filesystem supports POSIX
3032 * ACLs. If the filesystem doesn't support it umask stripping is done directly
3033 * in here. If the filesystem does support POSIX ACLs umask stripping is
3034 * deferred until the filesystem calls posix_acl_create().
3035 *
3036 * Returns: mode
3037 */
3038static inline umode_t mode_strip_umask(const struct inode *dir, umode_t mode)
3039{
3040 if (!IS_POSIXACL(dir))
3041 mode &= ~current_umask();
3042 return mode;
3043}
3044
3045/**
3046 * vfs_prepare_mode - prepare the mode to be used for a new inode
3047 * @mnt_userns: user namespace of the mount the inode was found from
3048 * @dir: parent directory of the new inode
3049 * @mode: mode of the new inode
3050 * @mask_perms: allowed permission by the vfs
3051 * @type: type of file to be created
3052 *
3053 * This helper consolidates and enforces vfs restrictions on the @mode of a new
3054 * object to be created.
3055 *
3056 * Umask stripping depends on whether the filesystem supports POSIX ACLs (see
3057 * the kernel documentation for mode_strip_umask()). Moving umask stripping
3058 * after setgid stripping allows the same ordering for both non-POSIX ACL and
3059 * POSIX ACL supporting filesystems.
3060 *
3061 * Note that it's currently valid for @type to be 0 if a directory is created.
3062 * Filesystems raise that flag individually and we need to check whether each
3063 * filesystem can deal with receiving S_IFDIR from the vfs before we enforce a
3064 * non-zero type.
3065 *
3066 * Returns: mode to be passed to the filesystem
3067 */
3068static inline umode_t vfs_prepare_mode(struct user_namespace *mnt_userns,
3069 const struct inode *dir, umode_t mode,
3070 umode_t mask_perms, umode_t type)
3071{
3072 mode = mode_strip_sgid(mnt_userns, dir, mode);
3073 mode = mode_strip_umask(dir, mode);
3074
3075 /*
3076 * Apply the vfs mandated allowed permission mask and set the type of
3077 * file to be created before we call into the filesystem.
3078 */
3079 mode &= (mask_perms & ~S_IFMT);
3080 mode |= (type & S_IFMT);
3081
3082 return mode;
3083}
3084
3085/**
Christian Brauner6521f892021-01-21 14:19:33 +01003086 * vfs_create - create new file
3087 * @mnt_userns: user namespace of the mount the inode was found from
3088 * @dir: inode of @dentry
3089 * @dentry: pointer to dentry of the base directory
3090 * @mode: mode of the new file
3091 * @want_excl: whether the file must not yet exist
3092 *
3093 * Create a new file.
3094 *
3095 * If the inode has been found through an idmapped mount the user namespace of
3096 * the vfsmount must be passed through @mnt_userns. This function will then take
3097 * care to map the inode according to @mnt_userns before checking permissions.
3098 * On non-idmapped mounts or if permission checking is to be performed on the
3099 * raw inode simply passs init_user_ns.
3100 */
3101int vfs_create(struct user_namespace *mnt_userns, struct inode *dir,
3102 struct dentry *dentry, umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Christian Brauner6521f892021-01-21 14:19:33 +01003104 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (error)
3106 return error;
3107
Al Viroacfa4382008-12-04 10:06:33 -05003108 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return -EACCES; /* shouldn't it be ENOSYS? */
Yang Xu1639a492022-07-14 14:11:27 +08003110
3111 mode = vfs_prepare_mode(mnt_userns, dir, mode, S_IALLUGO, S_IFREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 error = security_inode_create(dir, dentry, mode);
3113 if (error)
3114 return error;
Christian Brauner549c7292021-01-21 14:19:43 +01003115 error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003116 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003117 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return error;
3119}
Al Viro4d359502014-03-14 12:20:17 -04003120EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Al Viro8e6c8482017-12-01 17:12:45 -05003122int vfs_mkobj(struct dentry *dentry, umode_t mode,
3123 int (*f)(struct dentry *, umode_t, void *),
3124 void *arg)
3125{
3126 struct inode *dir = dentry->d_parent->d_inode;
Christian Braunerba73d982021-01-21 14:19:31 +01003127 int error = may_create(&init_user_ns, dir, dentry);
Al Viro8e6c8482017-12-01 17:12:45 -05003128 if (error)
3129 return error;
3130
3131 mode &= S_IALLUGO;
3132 mode |= S_IFREG;
3133 error = security_inode_create(dir, dentry, mode);
3134 if (error)
3135 return error;
3136 error = f(dentry, mode, arg);
3137 if (!error)
3138 fsnotify_create(dir, dentry);
3139 return error;
3140}
3141EXPORT_SYMBOL(vfs_mkobj);
3142
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003143bool may_open_dev(const struct path *path)
3144{
3145 return !(path->mnt->mnt_flags & MNT_NODEV) &&
3146 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
3147}
3148
Christian Braunerba73d982021-01-21 14:19:31 +01003149static int may_open(struct user_namespace *mnt_userns, const struct path *path,
3150 int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02003152 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 struct inode *inode = dentry->d_inode;
3154 int error;
3155
3156 if (!inode)
3157 return -ENOENT;
3158
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003159 switch (inode->i_mode & S_IFMT) {
3160 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003162 case S_IFDIR:
Kees Cookfc4177b2020-08-14 17:30:14 -07003163 if (acc_mode & MAY_WRITE)
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003164 return -EISDIR;
Kees Cookfc4177b2020-08-14 17:30:14 -07003165 if (acc_mode & MAY_EXEC)
3166 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003167 break;
3168 case S_IFBLK:
3169 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003170 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return -EACCES;
Kees Cook633fb6a2020-08-11 18:36:26 -07003172 fallthrough;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003173 case S_IFIFO:
3174 case S_IFSOCK:
Kees Cook633fb6a2020-08-11 18:36:26 -07003175 if (acc_mode & MAY_EXEC)
3176 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003178 break;
Kees Cook0fd338b2020-08-11 18:36:30 -07003179 case S_IFREG:
3180 if ((acc_mode & MAY_EXEC) && path_noexec(path))
3181 return -EACCES;
3182 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08003183 }
Dave Hansenb41572e2007-10-16 23:31:14 -07003184
Christian Braunerba73d982021-01-21 14:19:31 +01003185 error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07003186 if (error)
3187 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 /*
3190 * An append-only file must be opened in append mode for writing.
3191 */
3192 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05003193 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05003194 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05003196 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
3198
3199 /* O_NOATIME can only be set by the owner or superuser */
Christian Braunerba73d982021-01-21 14:19:31 +01003200 if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
Al Viro7715b522009-12-16 03:54:00 -05003201 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04003203 return 0;
Al Viro7715b522009-12-16 03:54:00 -05003204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Christian Brauner549c7292021-01-21 14:19:43 +01003206static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05003207{
Al Virof0bb5aa2016-11-20 20:27:12 -05003208 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05003209 struct inode *inode = path->dentry->d_inode;
3210 int error = get_write_access(inode);
3211 if (error)
3212 return error;
Jeff Layton482e0002021-10-26 11:56:45 -04003213
Jeff Laytonf7e33bd2021-08-19 14:56:38 -04003214 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05003215 if (!error) {
Christian Brauner549c7292021-01-21 14:19:43 +01003216 error = do_truncate(mnt_userns, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05003217 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003218 filp);
Al Viro7715b522009-12-16 03:54:00 -05003219 }
3220 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003221 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Dave Hansend57999e2008-02-15 14:37:27 -08003224static inline int open_to_namei_flags(int flag)
3225{
Al Viro8a5e9292011-06-25 19:15:54 -04003226 if ((flag & O_ACCMODE) == 3)
3227 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003228 return flag;
3229}
3230
Christian Braunerba73d982021-01-21 14:19:31 +01003231static int may_o_create(struct user_namespace *mnt_userns,
3232 const struct path *dir, struct dentry *dentry,
3233 umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003234{
3235 int error = security_path_mknod(dir, dentry, mode, 0);
3236 if (error)
3237 return error;
3238
Christian Brauner8e538912021-03-20 13:26:23 +01003239 if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
Seth Forshee1328c722017-01-26 14:33:46 -06003240 return -EOVERFLOW;
3241
Christian Braunerba73d982021-01-21 14:19:31 +01003242 error = inode_permission(mnt_userns, dir->dentry->d_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01003243 MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003244 if (error)
3245 return error;
3246
3247 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3248}
3249
David Howells1acf0af2012-06-14 16:13:46 +01003250/*
3251 * Attempt to atomically look up, create and open a file from a negative
3252 * dentry.
3253 *
3254 * Returns 0 if successful. The file will have been created and attached to
3255 * @file by the filesystem calling finish_open().
3256 *
Al Viro00a07c12018-07-09 19:30:20 -04003257 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3258 * be set. The caller will need to perform the open themselves. @path will
3259 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003260 *
3261 * Returns an error code otherwise.
3262 */
Al Viro239eb982020-01-09 14:12:40 -05003263static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3264 struct file *file,
Al Viro239eb982020-01-09 14:12:40 -05003265 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003266{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003267 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003268 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003269 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003270
Miklos Szeredid18e9002012-06-05 15:10:17 +02003271 if (nd->flags & LOOKUP_DIRECTORY)
3272 open_flag |= O_DIRECTORY;
3273
Al Viro30d904942012-06-22 12:40:19 +04003274 file->f_path.dentry = DENTRY_NOT_SET;
3275 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003276 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003277 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003278 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003279 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003280 if (file->f_mode & FMODE_OPENED) {
Al Viro6fb968c2020-01-26 09:53:19 -05003281 if (unlikely(dentry != file->f_path.dentry)) {
3282 dput(dentry);
3283 dentry = dget(file->f_path.dentry);
3284 }
Al Viro64e1ac42018-07-09 19:17:52 -04003285 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003286 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003287 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003288 if (file->f_path.dentry) {
3289 dput(dentry);
3290 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003291 }
Al Viro239eb982020-01-09 14:12:40 -05003292 if (unlikely(d_is_negative(dentry)))
Al Viroa01e7182016-06-07 21:53:51 -04003293 error = -ENOENT;
Sage Weil62b2ce92012-08-15 13:30:12 -07003294 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003295 }
Al Viro239eb982020-01-09 14:12:40 -05003296 if (error) {
3297 dput(dentry);
3298 dentry = ERR_PTR(error);
3299 }
3300 return dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003301}
3302
Nick Piggin31e6b012011-01-07 17:49:52 +11003303/*
David Howells1acf0af2012-06-14 16:13:46 +01003304 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003305 *
Al Viro00a07c12018-07-09 19:30:20 -04003306 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003307 *
Al Viro00a07c12018-07-09 19:30:20 -04003308 * Returns 0 on success, that is, if
3309 * the file was successfully atomically created (if necessary) and opened, or
3310 * the file was not completely opened at this time, though lookups and
3311 * creations were performed.
3312 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3313 * In the latter case dentry returned in @path might be negative if O_CREAT
3314 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003315 *
Al Viro00a07c12018-07-09 19:30:20 -04003316 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003317 */
Al Viroda5ebf52020-01-09 14:25:14 -05003318static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3319 const struct open_flags *op,
3320 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003321{
Christian Brauner549c7292021-01-21 14:19:43 +01003322 struct user_namespace *mnt_userns;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003323 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003324 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003325 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003326 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003327 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003328 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003329 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003330
Al Viroce8644f2016-04-26 14:17:56 -04003331 if (unlikely(IS_DEADDIR(dir_inode)))
Al Viroda5ebf52020-01-09 14:25:14 -05003332 return ERR_PTR(-ENOENT);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003333
Al Viro73a09dd2018-06-08 13:22:02 -04003334 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003335 dentry = d_lookup(dir, &nd->last);
3336 for (;;) {
3337 if (!dentry) {
3338 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3339 if (IS_ERR(dentry))
Al Viroda5ebf52020-01-09 14:25:14 -05003340 return dentry;
Al Viro6fbd0712016-04-28 11:50:59 -04003341 }
3342 if (d_in_lookup(dentry))
3343 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003344
Al Viro6fbd0712016-04-28 11:50:59 -04003345 error = d_revalidate(dentry, nd->flags);
3346 if (likely(error > 0))
3347 break;
3348 if (error)
3349 goto out_dput;
3350 d_invalidate(dentry);
3351 dput(dentry);
3352 dentry = NULL;
3353 }
3354 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003355 /* Cached positive dentry: will open in f_op->open */
Al Viroda5ebf52020-01-09 14:25:14 -05003356 return dentry;
Al Viro6c51e512016-03-05 20:09:32 -05003357 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003358
Al Viro1643b432016-04-27 19:14:10 -04003359 /*
3360 * Checking write permission is tricky, bacuse we don't know if we are
3361 * going to actually need it: O_CREAT opens should work as long as the
3362 * file exists. But checking existence breaks atomicity. The trick is
3363 * to check access and if not granted clear O_CREAT from the flags.
3364 *
3365 * Another problem is returing the "right" error value (e.g. for an
3366 * O_EXCL open we want to return EEXIST not EROFS).
3367 */
Al Viro99a4a902020-03-12 14:07:27 -04003368 if (unlikely(!got_write))
3369 open_flag &= ~O_TRUNC;
Christian Brauner549c7292021-01-21 14:19:43 +01003370 mnt_userns = mnt_user_ns(nd->path.mnt);
Al Viro1643b432016-04-27 19:14:10 -04003371 if (open_flag & O_CREAT) {
Al Viro99a4a902020-03-12 14:07:27 -04003372 if (open_flag & O_EXCL)
3373 open_flag &= ~O_TRUNC;
Yang Xu1639a492022-07-14 14:11:27 +08003374 mode = vfs_prepare_mode(mnt_userns, dir->d_inode, mode, mode, mode);
Al Viro99a4a902020-03-12 14:07:27 -04003375 if (likely(got_write))
Christian Brauner549c7292021-01-21 14:19:43 +01003376 create_error = may_o_create(mnt_userns, &nd->path,
Christian Braunerba73d982021-01-21 14:19:31 +01003377 dentry, mode);
Al Viro99a4a902020-03-12 14:07:27 -04003378 else
3379 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003380 }
Al Viro99a4a902020-03-12 14:07:27 -04003381 if (create_error)
3382 open_flag &= ~O_CREAT;
Al Viro1643b432016-04-27 19:14:10 -04003383 if (dir_inode->i_op->atomic_open) {
Al Virod489cf92020-03-11 08:07:53 -04003384 dentry = atomic_open(nd, dentry, file, open_flag, mode);
Al Viroda5ebf52020-01-09 14:25:14 -05003385 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3386 dentry = ERR_PTR(create_error);
3387 return dentry;
Al Viro1643b432016-04-27 19:14:10 -04003388 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003389
Al Viro6fbd0712016-04-28 11:50:59 -04003390 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003391 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3392 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003393 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003394 if (unlikely(res)) {
3395 if (IS_ERR(res)) {
3396 error = PTR_ERR(res);
3397 goto out_dput;
3398 }
3399 dput(dentry);
3400 dentry = res;
3401 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003402 }
3403
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003404 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003405 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003406 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003407 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003408 if (!dir_inode->i_op->create) {
3409 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003410 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003411 }
Christian Brauner549c7292021-01-21 14:19:43 +01003412
3413 error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry,
3414 mode, open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003415 if (error)
3416 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003417 }
Al Viro1643b432016-04-27 19:14:10 -04003418 if (unlikely(create_error) && !dentry->d_inode) {
3419 error = create_error;
3420 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003421 }
Al Viroda5ebf52020-01-09 14:25:14 -05003422 return dentry;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003423
3424out_dput:
3425 dput(dentry);
Al Viroda5ebf52020-01-09 14:25:14 -05003426 return ERR_PTR(error);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003427}
3428
Al Viroc981a482020-01-26 11:06:21 -05003429static const char *open_last_lookups(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003430 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003431{
Al Viroa1e28032009-12-24 02:12:06 -05003432 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003433 int open_flag = op->open_flag;
Al Viro64894cf2012-07-31 00:53:35 +04003434 bool got_write = false;
Al Viroda5ebf52020-01-09 14:25:14 -05003435 struct dentry *dentry;
Al Virob0417d22020-01-14 13:34:20 -05003436 const char *res;
Al Virofb1cc552009-12-24 01:58:28 -05003437
Al Viroc3e380b2011-02-23 13:39:45 -05003438 nd->flags |= op->intent;
3439
Al Virobc77daa2013-06-06 09:12:33 -04003440 if (nd->last_type != LAST_NORM) {
Al Viro56676ec2020-03-10 21:54:54 -04003441 if (nd->depth)
3442 put_link(nd);
Al Viroff326a32020-03-10 10:19:24 -04003443 return handle_dots(nd, nd->last_type);
Al Viro1f36f772009-12-26 10:56:19 -05003444 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003445
Al Viroca344a892011-03-09 00:36:45 -05003446 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003447 if (nd->last.name[nd->last.len])
3448 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3449 /* we _can_ be in RCU mode here */
Al Viro4cb64022022-07-03 22:20:20 -04003450 dentry = lookup_fast(nd);
Al Viro20e34352020-01-09 14:58:31 -05003451 if (IS_ERR(dentry))
Al Viro1ccac622020-01-14 10:13:40 -05003452 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05003453 if (likely(dentry))
Miklos Szeredi71574862012-06-05 15:10:14 +02003454 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003455
Al Viro6583fe22016-03-05 18:14:03 -05003456 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003457 } else {
3458 /* create side of things */
Al Viro72287412020-03-10 10:09:26 -04003459 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003460 if (!try_to_unlazy(nd))
3461 return ERR_PTR(-ECHILD);
Al Viro72287412020-03-10 10:09:26 -04003462 }
Al Viroc9b07ea2019-07-14 13:22:27 -04003463 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003464 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003465 if (unlikely(nd->last.name[nd->last.len]))
Al Viro1ccac622020-01-14 10:13:40 -05003466 return ERR_PTR(-EISDIR);
Al Virofe2d35f2011-03-05 22:58:25 -05003467 }
3468
Al Viro9cf843e2016-04-28 19:35:16 -04003469 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003470 got_write = !mnt_want_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003471 /*
3472 * do _not_ fail yet - we might not need that or fail with
3473 * a different error; let lookup_open() decide; we'll be
3474 * dropping this one anyway.
3475 */
3476 }
Al Viro9cf843e2016-04-28 19:35:16 -04003477 if (open_flag & O_CREAT)
3478 inode_lock(dir->d_inode);
3479 else
3480 inode_lock_shared(dir->d_inode);
Al Viroda5ebf52020-01-09 14:25:14 -05003481 dentry = lookup_open(nd, file, op, got_write);
Al Virof7bb9592020-03-05 13:25:20 -05003482 if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3483 fsnotify_create(dir->d_inode, dentry);
Al Viro9cf843e2016-04-28 19:35:16 -04003484 if (open_flag & O_CREAT)
3485 inode_unlock(dir->d_inode);
3486 else
3487 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003488
Al Viroc981a482020-01-26 11:06:21 -05003489 if (got_write)
Al Viro59e96e62020-01-26 10:22:24 -05003490 mnt_drop_write(nd->path.mnt);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003491
Al Viro59e96e62020-01-26 10:22:24 -05003492 if (IS_ERR(dentry))
3493 return ERR_CAST(dentry);
3494
Al Viro973d4b72020-01-26 10:48:16 -05003495 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
Al Viroe73cabf2020-01-09 14:30:08 -05003496 dput(nd->path.dentry);
3497 nd->path.dentry = dentry;
Al Viroc981a482020-01-26 11:06:21 -05003498 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05003499 }
3500
Al Viro20e34352020-01-09 14:58:31 -05003501finish_lookup:
Al Viro56676ec2020-03-10 21:54:54 -04003502 if (nd->depth)
3503 put_link(nd);
Al Viroa4f5b5212022-07-03 22:07:32 -04003504 res = step_into(nd, WALK_TRAILING, dentry);
Al Viroff326a32020-03-10 10:19:24 -04003505 if (unlikely(res))
Al Virob0417d22020-01-14 13:34:20 -05003506 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viroff326a32020-03-10 10:19:24 -04003507 return res;
Al Viroc981a482020-01-26 11:06:21 -05003508}
3509
3510/*
3511 * Handle the last step of open()
3512 */
Al Viroc5971b82020-03-05 11:41:29 -05003513static int do_open(struct nameidata *nd,
Al Viroc981a482020-01-26 11:06:21 -05003514 struct file *file, const struct open_flags *op)
3515{
Christian Brauner549c7292021-01-21 14:19:43 +01003516 struct user_namespace *mnt_userns;
Al Viroc981a482020-01-26 11:06:21 -05003517 int open_flag = op->open_flag;
3518 bool do_truncate;
3519 int acc_mode;
Al Viroc981a482020-01-26 11:06:21 -05003520 int error;
3521
Al Viroff326a32020-03-10 10:19:24 -04003522 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3523 error = complete_walk(nd);
3524 if (error)
3525 return error;
3526 }
Al Viro973d4b72020-01-26 10:48:16 -05003527 if (!(file->f_mode & FMODE_CREATED))
3528 audit_inode(nd->name, nd->path.dentry, 0);
Christian Brauner549c7292021-01-21 14:19:43 +01003529 mnt_userns = mnt_user_ns(nd->path.mnt);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003530 if (open_flag & O_CREAT) {
Al Virob94e0b32020-03-10 10:13:53 -04003531 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3532 return -EEXIST;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003533 if (d_is_dir(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003534 return -EISDIR;
Christian Brauner549c7292021-01-21 14:19:43 +01003535 error = may_create_in_sticky(mnt_userns, nd,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003536 d_backing_inode(nd->path.dentry));
3537 if (unlikely(error))
Al Viroc5971b82020-03-05 11:41:29 -05003538 return error;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003539 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003540 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003541 return -ENOTDIR;
Al Viro6c0d46c2011-03-09 00:59:59 -05003542
Al Viro8795e7d2020-01-26 10:38:17 -05003543 do_truncate = false;
3544 acc_mode = op->acc_mode;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003545 if (file->f_mode & FMODE_CREATED) {
3546 /* Don't check for write permission, don't truncate */
3547 open_flag &= ~O_TRUNC;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003548 acc_mode = 0;
Al Viro8795e7d2020-01-26 10:38:17 -05003549 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
Al Viro0f9d1a12011-03-09 00:13:14 -05003550 error = mnt_want_write(nd->path.mnt);
3551 if (error)
Al Viroc5971b82020-03-05 11:41:29 -05003552 return error;
Al Viro8795e7d2020-01-26 10:38:17 -05003553 do_truncate = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003554 }
Christian Brauner549c7292021-01-21 14:19:43 +01003555 error = may_open(mnt_userns, &nd->path, acc_mode, open_flag);
Al Viro8795e7d2020-01-26 10:38:17 -05003556 if (!error && !(file->f_mode & FMODE_OPENED))
Al Viro3ad56152020-01-26 10:06:13 -05003557 error = vfs_open(&nd->path, file);
Al Viro8795e7d2020-01-26 10:38:17 -05003558 if (!error)
3559 error = ima_file_check(file, op->acc_mode);
3560 if (!error && do_truncate)
Christian Brauner549c7292021-01-21 14:19:43 +01003561 error = handle_truncate(mnt_userns, file);
Al Viroc80567c2016-02-27 19:17:33 -05003562 if (unlikely(error > 0)) {
3563 WARN_ON(1);
3564 error = -EINVAL;
3565 }
Al Viro8795e7d2020-01-26 10:38:17 -05003566 if (do_truncate)
Al Viro0f9d1a12011-03-09 00:13:14 -05003567 mnt_drop_write(nd->path.mnt);
Al Viroc5971b82020-03-05 11:41:29 -05003568 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003569}
3570
Christian Brauner6521f892021-01-21 14:19:33 +01003571/**
3572 * vfs_tmpfile - create tmpfile
3573 * @mnt_userns: user namespace of the mount the inode was found from
3574 * @dentry: pointer to dentry of the base directory
3575 * @mode: mode of the new tmpfile
Randy Dunlap2111c3c2021-02-15 20:29:28 -08003576 * @open_flag: flags
Christian Brauner6521f892021-01-21 14:19:33 +01003577 *
3578 * Create a temporary file.
3579 *
3580 * If the inode has been found through an idmapped mount the user namespace of
3581 * the vfsmount must be passed through @mnt_userns. This function will then take
3582 * care to map the inode according to @mnt_userns before checking permissions.
3583 * On non-idmapped mounts or if permission checking is to be performed on the
3584 * raw inode simply passs init_user_ns.
3585 */
Miklos Szeredi9751b332022-09-24 07:00:00 +02003586static int vfs_tmpfile(struct user_namespace *mnt_userns,
3587 const struct path *parentpath,
3588 struct file *file, umode_t mode)
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003589{
Miklos Szeredi9751b332022-09-24 07:00:00 +02003590 struct dentry *child;
3591 struct inode *dir = d_inode(parentpath->dentry);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003592 struct inode *inode;
3593 int error;
3594
3595 /* we want directory to be writable */
Christian Brauner6521f892021-01-21 14:19:33 +01003596 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003597 if (error)
Miklos Szeredi9751b332022-09-24 07:00:00 +02003598 return error;
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003599 if (!dir->i_op->tmpfile)
Miklos Szeredi9751b332022-09-24 07:00:00 +02003600 return -EOPNOTSUPP;
3601 child = d_alloc(parentpath->dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003602 if (unlikely(!child))
Miklos Szeredi9751b332022-09-24 07:00:00 +02003603 return -ENOMEM;
3604 file->f_path.mnt = parentpath->mnt;
3605 file->f_path.dentry = child;
Yang Xu1639a492022-07-14 14:11:27 +08003606 mode = vfs_prepare_mode(mnt_userns, dir, mode, mode, mode);
Miklos Szeredi863f1442022-09-24 07:00:00 +02003607 error = dir->i_op->tmpfile(mnt_userns, dir, file, mode);
Miklos Szeredi9751b332022-09-24 07:00:00 +02003608 dput(child);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003609 if (error)
Miklos Szeredi9751b332022-09-24 07:00:00 +02003610 return error;
3611 /* Don't check for other permissions, the inode was just created */
3612 error = may_open(mnt_userns, &file->f_path, 0, file->f_flags);
3613 if (error)
3614 return error;
3615 inode = file_inode(file);
3616 if (!(file->f_flags & O_EXCL)) {
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003617 spin_lock(&inode->i_lock);
3618 inode->i_state |= I_LINKABLE;
3619 spin_unlock(&inode->i_lock);
3620 }
Christian Braunera2d23292021-01-21 14:19:45 +01003621 ima_post_create_tmpfile(mnt_userns, inode);
Miklos Szeredi9751b332022-09-24 07:00:00 +02003622 return 0;
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003623}
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003624
Miklos Szeredi22873de2022-09-24 06:59:59 +02003625/**
3626 * vfs_tmpfile_open - open a tmpfile for kernel internal use
3627 * @mnt_userns: user namespace of the mount the inode was found from
3628 * @parentpath: path of the base directory
3629 * @mode: mode of the new tmpfile
3630 * @open_flag: flags
3631 * @cred: credentials for open
3632 *
3633 * Create and open a temporary file. The file is not accounted in nr_files,
3634 * hence this is only for kernel internal use, and must not be installed into
3635 * file tables or such.
3636 */
3637struct file *vfs_tmpfile_open(struct user_namespace *mnt_userns,
3638 const struct path *parentpath,
3639 umode_t mode, int open_flag, const struct cred *cred)
3640{
3641 struct file *file;
3642 int error;
Miklos Szeredi22873de2022-09-24 06:59:59 +02003643
Miklos Szeredi9751b332022-09-24 07:00:00 +02003644 file = alloc_empty_file_noaccount(open_flag, cred);
3645 if (!IS_ERR(file)) {
3646 error = vfs_tmpfile(mnt_userns, parentpath, file, mode);
3647 if (error) {
3648 fput(file);
3649 file = ERR_PTR(error);
3650 }
3651 }
Miklos Szeredi22873de2022-09-24 06:59:59 +02003652 return file;
3653}
3654EXPORT_SYMBOL(vfs_tmpfile_open);
Al Viro648fa862009-12-24 01:26:48 -05003655
Al Viroc8a53ee2015-05-12 18:43:07 -04003656static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003657 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003658 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003659{
Christian Brauner6521f892021-01-21 14:19:33 +01003660 struct user_namespace *mnt_userns;
Al Viro625b6d12015-05-12 16:36:12 -04003661 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003662 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Miklos Szeredi9751b332022-09-24 07:00:00 +02003663
Al Viro60545d02013-06-07 01:20:27 -04003664 if (unlikely(error))
3665 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003666 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003667 if (unlikely(error))
3668 goto out;
Christian Brauner6521f892021-01-21 14:19:33 +01003669 mnt_userns = mnt_user_ns(path.mnt);
Miklos Szeredi9751b332022-09-24 07:00:00 +02003670 error = vfs_tmpfile(mnt_userns, &path, file, op->mode);
3671 if (error)
Al Viro60545d02013-06-07 01:20:27 -04003672 goto out2;
Miklos Szeredi9751b332022-09-24 07:00:00 +02003673 audit_inode(nd->name, file->f_path.dentry, 0);
Al Viro60545d02013-06-07 01:20:27 -04003674out2:
Al Viro625b6d12015-05-12 16:36:12 -04003675 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003676out:
Al Viro625b6d12015-05-12 16:36:12 -04003677 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003678 return error;
3679}
3680
Al Viro6ac08702016-04-26 00:02:50 -04003681static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3682{
3683 struct path path;
3684 int error = path_lookupat(nd, flags, &path);
3685 if (!error) {
3686 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003687 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003688 path_put(&path);
3689 }
3690 return error;
3691}
3692
Al Viroc8a53ee2015-05-12 18:43:07 -04003693static struct file *path_openat(struct nameidata *nd,
3694 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
Al Viro30d904942012-06-22 12:40:19 +04003696 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003697 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003698
Al Viroea73ea72018-07-11 15:00:04 -04003699 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003700 if (IS_ERR(file))
3701 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003702
Al Virobb458c62013-07-13 13:26:37 +04003703 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003704 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003705 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003706 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003707 } else {
3708 const char *s = path_init(nd, flags);
3709 while (!(error = link_path_walk(s, nd)) &&
Al Viroc5971b82020-03-05 11:41:29 -05003710 (s = open_last_lookups(nd, file, op)) != NULL)
Al Viro1ccac622020-01-14 10:13:40 -05003711 ;
Al Viroc5971b82020-03-05 11:41:29 -05003712 if (!error)
3713 error = do_open(nd, file, op);
Al Viro5f336e72018-07-09 16:38:06 -04003714 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003715 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003716 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003717 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003718 return file;
3719 WARN_ON(1);
3720 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003721 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003722 fput(file);
3723 if (error == -EOPENSTALE) {
3724 if (flags & LOOKUP_RCU)
3725 error = -ECHILD;
3726 else
3727 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003728 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003729 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730}
3731
Jeff Layton669abf42012-10-10 16:43:10 -04003732struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003733 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003734{
Al Viro9883d182015-05-13 07:28:08 -04003735 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003736 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003737 struct file *filp;
3738
Al Viro06422962021-04-01 22:28:03 -04003739 set_nameidata(&nd, dfd, pathname, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04003740 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003741 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003742 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003743 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003744 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003745 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003746 return filp;
3747}
3748
Al Viroffb37ca2021-04-01 19:00:57 -04003749struct file *do_file_open_root(const struct path *root,
Al Virof9652e12013-06-11 08:23:01 +04003750 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003751{
Al Viro9883d182015-05-13 07:28:08 -04003752 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003753 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003754 struct filename *filename;
Al Virobcba1e72021-04-01 22:03:41 -04003755 int flags = op->lookup_flags;
Al Viro73d049a2011-03-11 12:08:24 -05003756
Al Viroffb37ca2021-04-01 19:00:57 -04003757 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003758 return ERR_PTR(-ELOOP);
3759
Paul Moore51689102015-01-22 00:00:03 -05003760 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303761 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003762 return ERR_CAST(filename);
3763
Al Viro06422962021-04-01 22:28:03 -04003764 set_nameidata(&nd, -1, filename, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04003765 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003766 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003767 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003768 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003769 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003770 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003771 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003772 return file;
3773}
3774
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003775static struct dentry *filename_create(int dfd, struct filename *name,
3776 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003778 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003779 struct qstr last;
NeilBrownb3d46502022-04-14 13:57:35 +10003780 bool want_dir = lookup_flags & LOOKUP_DIRECTORY;
3781 unsigned int reval_flag = lookup_flags & LOOKUP_REVAL;
3782 unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro391172c2015-05-09 11:19:16 -04003783 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003784 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003785 int error;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003786
NeilBrownb3d46502022-04-14 13:57:35 +10003787 error = filename_parentat(dfd, name, reval_flag, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07003788 if (error)
3789 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003791 /*
3792 * Yucky last component or no last component at all?
3793 * (foo/., foo/.., /////)
3794 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003795 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003796 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003797
Jan Karac30dabf2012-06-12 16:20:30 +02003798 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003799 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003800 /*
NeilBrownb3d46502022-04-14 13:57:35 +10003801 * Do the final lookup. Suppress 'create' if there is a trailing
3802 * '/', and a directory wasn't requested.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003803 */
NeilBrownb3d46502022-04-14 13:57:35 +10003804 if (last.name[last.len] && !want_dir)
3805 create_flags = 0;
Al Viro59551022016-01-22 15:40:57 -05003806 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
NeilBrownb3d46502022-04-14 13:57:35 +10003807 dentry = __lookup_hash(&last, path->dentry, reval_flag | create_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003809 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003810
Al Viroa8104a92012-07-20 02:25:00 +04003811 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003812 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003813 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003814
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003815 /*
3816 * Special case - lookup gave negative, but... we had foo/bar/
3817 * From the vfs_mknod() POV we just have a negative dentry -
3818 * all is fine. Let's be bastards - you had / on the end, you've
3819 * been asking for (non-existent) directory. -ENOENT for you.
3820 */
NeilBrownb3d46502022-04-14 13:57:35 +10003821 if (unlikely(!create_flags)) {
Al Viroa8104a92012-07-20 02:25:00 +04003822 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003823 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003824 }
Jan Karac30dabf2012-06-12 16:20:30 +02003825 if (unlikely(err2)) {
3826 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003827 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830fail:
Al Viroa8104a92012-07-20 02:25:00 +04003831 dput(dentry);
3832 dentry = ERR_PTR(error);
3833unlock:
Al Viro59551022016-01-22 15:40:57 -05003834 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003835 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003836 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003837out:
Al Viro391172c2015-05-09 11:19:16 -04003838 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 return dentry;
3840}
Al Virofa14a0b2015-01-22 02:16:49 -05003841
3842struct dentry *kern_path_create(int dfd, const char *pathname,
3843 struct path *path, unsigned int lookup_flags)
3844{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003845 struct filename *filename = getname_kernel(pathname);
3846 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3847
3848 putname(filename);
3849 return res;
Al Virofa14a0b2015-01-22 02:16:49 -05003850}
Al Virodae6ad82011-06-26 11:50:15 -04003851EXPORT_SYMBOL(kern_path_create);
3852
Al Viro921a1652012-07-20 01:15:31 +04003853void done_path_create(struct path *path, struct dentry *dentry)
3854{
3855 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003856 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003857 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003858 path_put(path);
3859}
3860EXPORT_SYMBOL(done_path_create);
3861
Al Viro520ae682015-05-13 07:00:28 -04003862inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003863 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003864{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003865 struct filename *filename = getname(pathname);
3866 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3867
3868 putname(filename);
3869 return res;
Al Virodae6ad82011-06-26 11:50:15 -04003870}
3871EXPORT_SYMBOL(user_path_create);
3872
Christian Brauner6521f892021-01-21 14:19:33 +01003873/**
3874 * vfs_mknod - create device node or file
3875 * @mnt_userns: user namespace of the mount the inode was found from
3876 * @dir: inode of @dentry
3877 * @dentry: pointer to dentry of the base directory
3878 * @mode: mode of the new device node or file
3879 * @dev: device number of device to create
3880 *
3881 * Create a device node or file.
3882 *
3883 * If the inode has been found through an idmapped mount the user namespace of
3884 * the vfsmount must be passed through @mnt_userns. This function will then take
3885 * care to map the inode according to @mnt_userns before checking permissions.
3886 * On non-idmapped mounts or if permission checking is to be performed on the
3887 * raw inode simply passs init_user_ns.
3888 */
3889int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
3890 struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003892 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
Christian Brauner6521f892021-01-21 14:19:33 +01003893 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
3895 if (error)
3896 return error;
3897
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003898 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3899 !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return -EPERM;
3901
Al Viroacfa4382008-12-04 10:06:33 -05003902 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return -EPERM;
3904
Yang Xu1639a492022-07-14 14:11:27 +08003905 mode = vfs_prepare_mode(mnt_userns, dir, mode, mode, mode);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003906 error = devcgroup_inode_mknod(mode, dev);
3907 if (error)
3908 return error;
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 error = security_inode_mknod(dir, dentry, mode, dev);
3911 if (error)
3912 return error;
3913
Christian Brauner549c7292021-01-21 14:19:43 +01003914 error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003915 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003916 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return error;
3918}
Al Viro4d359502014-03-14 12:20:17 -04003919EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Al Virof69aac02011-07-26 04:31:40 -04003921static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003922{
3923 switch (mode & S_IFMT) {
3924 case S_IFREG:
3925 case S_IFCHR:
3926 case S_IFBLK:
3927 case S_IFIFO:
3928 case S_IFSOCK:
3929 case 0: /* zero mode translates to S_IFREG */
3930 return 0;
3931 case S_IFDIR:
3932 return -EPERM;
3933 default:
3934 return -EINVAL;
3935 }
3936}
3937
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003938static int do_mknodat(int dfd, struct filename *name, umode_t mode,
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003939 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Christian Brauner6521f892021-01-21 14:19:33 +01003941 struct user_namespace *mnt_userns;
Al Viro2ad94ae2008-07-21 09:32:51 -04003942 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003943 struct path path;
3944 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003945 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Al Viro8e4bfca2012-07-20 01:17:26 +04003947 error = may_mknod(mode);
3948 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003949 goto out1;
Jeff Layton972567f2012-12-20 16:00:10 -05003950retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003951 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev77972512021-07-08 13:34:40 +07003952 error = PTR_ERR(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003953 if (IS_ERR(dentry))
Dmitry Kadashev77972512021-07-08 13:34:40 +07003954 goto out1;
Al Viro2ad94ae2008-07-21 09:32:51 -04003955
Yang Xu1639a492022-07-14 14:11:27 +08003956 error = security_path_mknod(&path, dentry,
3957 mode_strip_umask(path.dentry->d_inode, mode), dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003958 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003959 goto out2;
Christian Brauner6521f892021-01-21 14:19:33 +01003960
3961 mnt_userns = mnt_user_ns(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003962 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 case 0: case S_IFREG:
Christian Brauner6521f892021-01-21 14:19:33 +01003964 error = vfs_create(mnt_userns, path.dentry->d_inode,
3965 dentry, mode, true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003966 if (!error)
Christian Braunera2d23292021-01-21 14:19:45 +01003967 ima_post_path_mknod(mnt_userns, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 break;
3969 case S_IFCHR: case S_IFBLK:
Christian Brauner6521f892021-01-21 14:19:33 +01003970 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3971 dentry, mode, new_decode_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 break;
3973 case S_IFIFO: case S_IFSOCK:
Christian Brauner6521f892021-01-21 14:19:33 +01003974 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3975 dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003978out2:
Al Viro921a1652012-07-20 01:15:31 +04003979 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003980 if (retry_estale(error, lookup_flags)) {
3981 lookup_flags |= LOOKUP_REVAL;
3982 goto retry;
3983 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003984out1:
3985 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return error;
3987}
3988
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003989SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3990 unsigned int, dev)
3991{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003992 return do_mknodat(dfd, getname(filename), mode, dev);
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003993}
3994
Al Viro8208a222011-07-25 17:32:17 -04003995SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003996{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003997 return do_mknodat(AT_FDCWD, getname(filename), mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003998}
3999
Christian Brauner6521f892021-01-21 14:19:33 +01004000/**
4001 * vfs_mkdir - create directory
4002 * @mnt_userns: user namespace of the mount the inode was found from
4003 * @dir: inode of @dentry
4004 * @dentry: pointer to dentry of the base directory
4005 * @mode: mode of the new directory
4006 *
4007 * Create a directory.
4008 *
4009 * If the inode has been found through an idmapped mount the user namespace of
4010 * the vfsmount must be passed through @mnt_userns. This function will then take
4011 * care to map the inode according to @mnt_userns before checking permissions.
4012 * On non-idmapped mounts or if permission checking is to be performed on the
4013 * raw inode simply passs init_user_ns.
4014 */
4015int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
4016 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Christian Brauner6521f892021-01-21 14:19:33 +01004018 int error = may_create(mnt_userns, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05004019 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 if (error)
4022 return error;
4023
Al Viroacfa4382008-12-04 10:06:33 -05004024 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 return -EPERM;
4026
Yang Xu1639a492022-07-14 14:11:27 +08004027 mode = vfs_prepare_mode(mnt_userns, dir, mode, S_IRWXUGO | S_ISVTX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 error = security_inode_mkdir(dir, dentry, mode);
4029 if (error)
4030 return error;
4031
Al Viro8de52772012-02-06 12:45:27 -05004032 if (max_links && dir->i_nlink >= max_links)
4033 return -EMLINK;
4034
Christian Brauner549c7292021-01-21 14:19:43 +01004035 error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004036 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004037 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return error;
4039}
Al Viro4d359502014-03-14 12:20:17 -04004040EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004042int do_mkdirat(int dfd, struct filename *name, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
Dave Hansen6902d922006-09-30 23:29:01 -07004044 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004045 struct path path;
4046 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05004047 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Jeff Laytonb76d8b82012-12-20 16:04:09 -05004049retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004050 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004051 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004052 if (IS_ERR(dentry))
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004053 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004054
Yang Xu1639a492022-07-14 14:11:27 +08004055 error = security_path_mkdir(&path, dentry,
4056 mode_strip_umask(path.dentry->d_inode, mode));
Christian Brauner6521f892021-01-21 14:19:33 +01004057 if (!error) {
4058 struct user_namespace *mnt_userns;
4059 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01004060 error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry,
4061 mode);
Christian Brauner6521f892021-01-21 14:19:33 +01004062 }
Al Viro921a1652012-07-20 01:15:31 +04004063 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05004064 if (retry_estale(error, lookup_flags)) {
4065 lookup_flags |= LOOKUP_REVAL;
4066 goto retry;
4067 }
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004068out_putname:
4069 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 return error;
4071}
4072
Dominik Brodowski0101db72018-03-11 11:34:49 +01004073SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
4074{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004075 return do_mkdirat(dfd, getname(pathname), mode);
Dominik Brodowski0101db72018-03-11 11:34:49 +01004076}
4077
Al Viroa218d0f2011-11-21 14:59:34 -05004078SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004079{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07004080 return do_mkdirat(AT_FDCWD, getname(pathname), mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004081}
4082
Christian Brauner6521f892021-01-21 14:19:33 +01004083/**
4084 * vfs_rmdir - remove directory
4085 * @mnt_userns: user namespace of the mount the inode was found from
4086 * @dir: inode of @dentry
4087 * @dentry: pointer to dentry of the base directory
4088 *
4089 * Remove a directory.
4090 *
4091 * If the inode has been found through an idmapped mount the user namespace of
4092 * the vfsmount must be passed through @mnt_userns. This function will then take
4093 * care to map the inode according to @mnt_userns before checking permissions.
4094 * On non-idmapped mounts or if permission checking is to be performed on the
4095 * raw inode simply passs init_user_ns.
4096 */
4097int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
4098 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Christian Brauner6521f892021-01-21 14:19:33 +01004100 int error = may_delete(mnt_userns, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 if (error)
4103 return error;
4104
Al Viroacfa4382008-12-04 10:06:33 -05004105 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return -EPERM;
4107
Al Viro1d2ef592011-09-14 18:55:41 +01004108 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05004109 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Sage Weil912dbc12011-05-24 13:06:11 -07004111 error = -EBUSY;
David Howells1bd9c4e2021-11-18 08:58:08 +00004112 if (is_local_mountpoint(dentry) ||
4113 (dentry->d_inode->i_flags & S_KERNEL_FILE))
Sage Weil912dbc12011-05-24 13:06:11 -07004114 goto out;
4115
4116 error = security_inode_rmdir(dir, dentry);
4117 if (error)
4118 goto out;
4119
4120 error = dir->i_op->rmdir(dir, dentry);
4121 if (error)
4122 goto out;
4123
Al Viro87677122018-05-27 16:23:51 -04004124 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004125 dentry->d_inode->i_flags |= S_DEAD;
4126 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004127 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004128
4129out:
Al Viro59551022016-01-22 15:40:57 -05004130 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01004131 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004132 if (!error)
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004133 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 return error;
4135}
Al Viro4d359502014-03-14 12:20:17 -04004136EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004138int do_rmdir(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Christian Brauner6521f892021-01-21 14:19:33 +01004140 struct user_namespace *mnt_userns;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004141 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004143 struct path path;
4144 struct qstr last;
4145 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05004146 unsigned int lookup_flags = 0;
4147retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004148 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004149 if (error)
4150 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Al Virof5beed72015-04-30 16:09:11 -04004152 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004153 case LAST_DOTDOT:
4154 error = -ENOTEMPTY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004155 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004156 case LAST_DOT:
4157 error = -EINVAL;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004158 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004159 case LAST_ROOT:
4160 error = -EBUSY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004161 goto exit2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004163
Al Virof5beed72015-04-30 16:09:11 -04004164 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004165 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004166 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004167
Al Viro59551022016-01-22 15:40:57 -05004168 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004169 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004171 if (IS_ERR(dentry))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004172 goto exit3;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004173 if (!dentry->d_inode) {
4174 error = -ENOENT;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004175 goto exit4;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004176 }
Al Virof5beed72015-04-30 16:09:11 -04004177 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004178 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004179 goto exit4;
Christian Brauner6521f892021-01-21 14:19:33 +01004180 mnt_userns = mnt_user_ns(path.mnt);
4181 error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004182exit4:
Dave Hansen6902d922006-09-30 23:29:01 -07004183 dput(dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004184exit3:
Al Viro59551022016-01-22 15:40:57 -05004185 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04004186 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004187exit2:
Al Virof5beed72015-04-30 16:09:11 -04004188 path_put(&path);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05004189 if (retry_estale(error, lookup_flags)) {
4190 lookup_flags |= LOOKUP_REVAL;
4191 goto retry;
4192 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004193exit1:
Al Viro24fb33d2020-08-12 05:15:18 +01004194 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return error;
4196}
4197
Heiko Carstens3cdad422009-01-14 14:14:22 +01004198SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004199{
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004200 return do_rmdir(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004201}
4202
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004203/**
4204 * vfs_unlink - unlink a filesystem object
Christian Brauner6521f892021-01-21 14:19:33 +01004205 * @mnt_userns: user namespace of the mount the inode was found from
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004206 * @dir: parent directory
4207 * @dentry: victim
4208 * @delegated_inode: returns victim inode, if the inode is delegated.
4209 *
4210 * The caller must hold dir->i_mutex.
4211 *
4212 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4213 * return a reference to the inode in delegated_inode. The caller
4214 * should then break the delegation on that inode and retry. Because
4215 * breaking a delegation may take a long time, the caller should drop
4216 * dir->i_mutex before doing so.
4217 *
4218 * Alternatively, a caller may pass NULL for delegated_inode. This may
4219 * be appropriate for callers that expect the underlying filesystem not
4220 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004221 *
4222 * If the inode has been found through an idmapped mount the user namespace of
4223 * the vfsmount must be passed through @mnt_userns. This function will then take
4224 * care to map the inode according to @mnt_userns before checking permissions.
4225 * On non-idmapped mounts or if permission checking is to be performed on the
4226 * raw inode simply passs init_user_ns.
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004227 */
Christian Brauner6521f892021-01-21 14:19:33 +01004228int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
4229 struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004231 struct inode *target = dentry->d_inode;
Christian Brauner6521f892021-01-21 14:19:33 +01004232 int error = may_delete(mnt_userns, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
4234 if (error)
4235 return error;
4236
Al Viroacfa4382008-12-04 10:06:33 -05004237 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return -EPERM;
4239
Al Viro59551022016-01-22 15:40:57 -05004240 inode_lock(target);
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004241 if (IS_SWAPFILE(target))
4242 error = -EPERM;
4243 else if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 error = -EBUSY;
4245 else {
4246 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004247 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004248 error = try_break_deleg(target, delegated_inode);
4249 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004250 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004252 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004253 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004254 detach_mounts(dentry);
4255 }
Al Virobec10522010-03-03 14:12:08 -05004256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004258out:
Al Viro59551022016-01-22 15:40:57 -05004259 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004262 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
4263 fsnotify_unlink(dir, dentry);
4264 } else if (!error) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004265 fsnotify_link_count(target);
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004266 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 }
Robert Love0eeca282005-07-12 17:06:03 -04004268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return error;
4270}
Al Viro4d359502014-03-14 12:20:17 -04004271EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273/*
4274 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004275 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 * writeout happening, and we don't want to prevent access to the directory
4277 * while waiting on the I/O.
4278 */
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004279int do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Al Viro2ad94ae2008-07-21 09:32:51 -04004281 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004283 struct path path;
4284 struct qstr last;
4285 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004287 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004288 unsigned int lookup_flags = 0;
4289retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004290 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004291 if (error)
4292 goto exit1;
Al Viro2ad94ae2008-07-21 09:32:51 -04004293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004295 if (type != LAST_NORM)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004296 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004297
Al Virof5beed72015-04-30 16:09:11 -04004298 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004299 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004300 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004301retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004302 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004303 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 error = PTR_ERR(dentry);
4305 if (!IS_ERR(dentry)) {
Christian Brauner6521f892021-01-21 14:19:33 +01004306 struct user_namespace *mnt_userns;
4307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004309 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004310 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004312 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004313 goto slashes;
4314 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004315 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004316 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004317 goto exit3;
Christian Brauner6521f892021-01-21 14:19:33 +01004318 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01004319 error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
4320 &delegated_inode);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004321exit3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 dput(dentry);
4323 }
Al Viro59551022016-01-22 15:40:57 -05004324 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 if (inode)
4326 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004327 inode = NULL;
4328 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004329 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004330 if (!error)
4331 goto retry_deleg;
4332 }
Al Virof5beed72015-04-30 16:09:11 -04004333 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004334exit2:
Al Virof5beed72015-04-30 16:09:11 -04004335 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004336 if (retry_estale(error, lookup_flags)) {
4337 lookup_flags |= LOOKUP_REVAL;
4338 inode = NULL;
4339 goto retry;
4340 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004341exit1:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004342 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return error;
4344
4345slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004346 if (d_is_negative(dentry))
4347 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004348 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004349 error = -EISDIR;
4350 else
4351 error = -ENOTDIR;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004352 goto exit3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353}
4354
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004355SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004356{
4357 if ((flag & ~AT_REMOVEDIR) != 0)
4358 return -EINVAL;
4359
4360 if (flag & AT_REMOVEDIR)
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004361 return do_rmdir(dfd, getname(pathname));
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004362 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004363}
4364
Heiko Carstens3480b252009-01-14 14:14:16 +01004365SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004366{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004367 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004368}
4369
Christian Brauner6521f892021-01-21 14:19:33 +01004370/**
4371 * vfs_symlink - create symlink
4372 * @mnt_userns: user namespace of the mount the inode was found from
4373 * @dir: inode of @dentry
4374 * @dentry: pointer to dentry of the base directory
4375 * @oldname: name of the file to link to
4376 *
4377 * Create a symlink.
4378 *
4379 * If the inode has been found through an idmapped mount the user namespace of
4380 * the vfsmount must be passed through @mnt_userns. This function will then take
4381 * care to map the inode according to @mnt_userns before checking permissions.
4382 * On non-idmapped mounts or if permission checking is to be performed on the
4383 * raw inode simply passs init_user_ns.
4384 */
4385int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
4386 struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
Christian Brauner6521f892021-01-21 14:19:33 +01004388 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 if (error)
4391 return error;
4392
Al Viroacfa4382008-12-04 10:06:33 -05004393 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 return -EPERM;
4395
4396 error = security_inode_symlink(dir, dentry, oldname);
4397 if (error)
4398 return error;
4399
Christian Brauner549c7292021-01-21 14:19:43 +01004400 error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004401 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004402 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return error;
4404}
Al Viro4d359502014-03-14 12:20:17 -04004405EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07004407int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Al Viro2ad94ae2008-07-21 09:32:51 -04004409 int error;
Dave Hansen6902d922006-09-30 23:29:01 -07004410 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004411 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004412 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004414 if (IS_ERR(from)) {
4415 error = PTR_ERR(from);
4416 goto out_putnames;
4417 }
Jeff Laytonf46d3562012-12-11 12:10:08 -05004418retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004419 dentry = filename_create(newdfd, to, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004420 error = PTR_ERR(dentry);
4421 if (IS_ERR(dentry))
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004422 goto out_putnames;
Dave Hansen6902d922006-09-30 23:29:01 -07004423
Jeff Layton91a27b22012-10-10 15:25:28 -04004424 error = security_path_symlink(&path, dentry, from->name);
Christian Brauner6521f892021-01-21 14:19:33 +01004425 if (!error) {
4426 struct user_namespace *mnt_userns;
4427
4428 mnt_userns = mnt_user_ns(path.mnt);
4429 error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry,
4430 from->name);
4431 }
Al Viro921a1652012-07-20 01:15:31 +04004432 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004433 if (retry_estale(error, lookup_flags)) {
4434 lookup_flags |= LOOKUP_REVAL;
4435 goto retry;
4436 }
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004437out_putnames:
4438 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 putname(from);
4440 return error;
4441}
4442
Dominik Brodowskib724e842018-03-11 11:34:49 +01004443SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4444 int, newdfd, const char __user *, newname)
4445{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004446 return do_symlinkat(getname(oldname), newdfd, getname(newname));
Dominik Brodowskib724e842018-03-11 11:34:49 +01004447}
4448
Heiko Carstens3480b252009-01-14 14:14:16 +01004449SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004450{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004451 return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004452}
4453
J. Bruce Fields146a8592011-09-20 17:14:31 -04004454/**
4455 * vfs_link - create a new link
4456 * @old_dentry: object to be linked
Christian Brauner6521f892021-01-21 14:19:33 +01004457 * @mnt_userns: the user namespace of the mount
J. Bruce Fields146a8592011-09-20 17:14:31 -04004458 * @dir: new parent
4459 * @new_dentry: where to create the new link
4460 * @delegated_inode: returns inode needing a delegation break
4461 *
4462 * The caller must hold dir->i_mutex
4463 *
4464 * If vfs_link discovers a delegation on the to-be-linked file in need
4465 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4466 * inode in delegated_inode. The caller should then break the delegation
4467 * and retry. Because breaking a delegation may take a long time, the
4468 * caller should drop the i_mutex before doing so.
4469 *
4470 * Alternatively, a caller may pass NULL for delegated_inode. This may
4471 * be appropriate for callers that expect the underlying filesystem not
4472 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004473 *
4474 * If the inode has been found through an idmapped mount the user namespace of
4475 * the vfsmount must be passed through @mnt_userns. This function will then take
4476 * care to map the inode according to @mnt_userns before checking permissions.
4477 * On non-idmapped mounts or if permission checking is to be performed on the
4478 * raw inode simply passs init_user_ns.
J. Bruce Fields146a8592011-09-20 17:14:31 -04004479 */
Christian Brauner6521f892021-01-21 14:19:33 +01004480int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns,
4481 struct inode *dir, struct dentry *new_dentry,
4482 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
4484 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004485 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 int error;
4487
4488 if (!inode)
4489 return -ENOENT;
4490
Christian Brauner6521f892021-01-21 14:19:33 +01004491 error = may_create(mnt_userns, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 if (error)
4493 return error;
4494
4495 if (dir->i_sb != inode->i_sb)
4496 return -EXDEV;
4497
4498 /*
4499 * A link to an append-only or immutable file cannot be created.
4500 */
4501 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4502 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004503 /*
4504 * Updating the link count will likely cause i_uid and i_gid to
4505 * be writen back improperly if their true value is unknown to
4506 * the vfs.
4507 */
Christian Brauner6521f892021-01-21 14:19:33 +01004508 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004509 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004510 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004512 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return -EPERM;
4514
4515 error = security_inode_link(old_dentry, dir, new_dentry);
4516 if (error)
4517 return error;
4518
Al Viro59551022016-01-22 15:40:57 -05004519 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304520 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004521 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304522 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004523 else if (max_links && inode->i_nlink >= max_links)
4524 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004525 else {
4526 error = try_break_deleg(inode, delegated_inode);
4527 if (!error)
4528 error = dir->i_op->link(old_dentry, dir, new_dentry);
4529 }
Al Virof4e0c302013-06-11 08:34:36 +04004530
4531 if (!error && (inode->i_state & I_LINKABLE)) {
4532 spin_lock(&inode->i_lock);
4533 inode->i_state &= ~I_LINKABLE;
4534 spin_unlock(&inode->i_lock);
4535 }
Al Viro59551022016-01-22 15:40:57 -05004536 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004537 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004538 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return error;
4540}
Al Viro4d359502014-03-14 12:20:17 -04004541EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
4543/*
4544 * Hardlinks are often used in delicate situations. We avoid
4545 * security-related surprises by not following symlinks on the
4546 * newname. --KAB
4547 *
4548 * We don't follow them on the oldname either to be compatible
4549 * with linux 2.0, and to avoid hard-linking to directories
4550 * and other special files. --ADM
4551 */
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07004552int do_linkat(int olddfd, struct filename *old, int newdfd,
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004553 struct filename *new, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
Christian Brauner6521f892021-01-21 14:19:33 +01004555 struct user_namespace *mnt_userns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004557 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004558 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304559 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004562 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
4563 error = -EINVAL;
4564 goto out_putnames;
4565 }
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304566 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004567 * To use null names we require CAP_DAC_READ_SEARCH
4568 * This ensures that not everyone will be able to create
4569 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304570 */
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004571 if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
4572 error = -ENOENT;
4573 goto out_putnames;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004574 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004575
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304576 if (flags & AT_SYMLINK_FOLLOW)
4577 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004578retry:
Stephen Brennan794ebce2021-09-01 10:51:42 -07004579 error = filename_lookup(olddfd, old, how, &old_path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 if (error)
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004581 goto out_putnames;
Al Viro2ad94ae2008-07-21 09:32:51 -04004582
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004583 new_dentry = filename_create(newdfd, new, &new_path,
Jeff Layton442e31c2012-12-20 16:15:38 -05004584 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004586 if (IS_ERR(new_dentry))
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004587 goto out_putpath;
Al Virodae6ad82011-06-26 11:50:15 -04004588
4589 error = -EXDEV;
4590 if (old_path.mnt != new_path.mnt)
4591 goto out_dput;
Christian Brauner549c7292021-01-21 14:19:43 +01004592 mnt_userns = mnt_user_ns(new_path.mnt);
4593 error = may_linkat(mnt_userns, &old_path);
Kees Cook800179c2012-07-25 17:29:07 -07004594 if (unlikely(error))
4595 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004596 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004597 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004598 goto out_dput;
Christian Brauner6521f892021-01-21 14:19:33 +01004599 error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
4600 new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004601out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004602 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004603 if (delegated_inode) {
4604 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004605 if (!error) {
4606 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004607 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004608 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004609 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004610 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004611 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004612 how |= LOOKUP_REVAL;
4613 goto retry;
4614 }
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004615out_putpath:
Al Viro2d8f3032008-07-22 09:59:21 -04004616 path_put(&old_path);
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004617out_putnames:
4618 putname(old);
4619 putname(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 return error;
4622}
4623
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004624SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4625 int, newdfd, const char __user *, newname, int, flags)
4626{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004627 return do_linkat(olddfd, getname_uflags(oldname, flags),
4628 newdfd, getname(newname), flags);
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004629}
4630
Heiko Carstens3480b252009-01-14 14:14:16 +01004631SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004632{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004633 return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004634}
4635
Miklos Szeredibc270272014-04-01 17:08:42 +02004636/**
4637 * vfs_rename - rename a filesystem object
Randy Dunlap2111c3c2021-02-15 20:29:28 -08004638 * @rd: pointer to &struct renamedata info
Miklos Szeredibc270272014-04-01 17:08:42 +02004639 *
4640 * The caller must hold multiple mutexes--see lock_rename()).
4641 *
4642 * If vfs_rename discovers a delegation in need of breaking at either
4643 * the source or destination, it will return -EWOULDBLOCK and return a
4644 * reference to the inode in delegated_inode. The caller should then
4645 * break the delegation and retry. Because breaking a delegation may
4646 * take a long time, the caller should drop all locks before doing
4647 * so.
4648 *
4649 * Alternatively, a caller may pass NULL for delegated_inode. This may
4650 * be appropriate for callers that expect the underlying filesystem not
4651 * to be NFS exported.
4652 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 * The worst of all namespace operations - renaming directory. "Perverted"
4654 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4655 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004656 *
J. Bruce Fieldsd03b29a22014-02-17 16:52:33 -05004657 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 * b) race potential - two innocent renames can create a loop together.
4659 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004660 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004662 * c) we have to lock _four_ objects - parents and victim (if it exists),
4663 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004664 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 * whether the target exists). Solution: try to be smart with locking
4666 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004667 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 * move will be locked. Thus we can rank directories by the tree
4669 * (ancestors first) and rank all non-directories after them.
4670 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004671 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 * HOWEVER, it relies on the assumption that any object with ->lookup()
4673 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4674 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004675 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004676 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004678 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 * locking].
4680 */
Christian Brauner9fe61452021-01-21 14:19:32 +01004681int vfs_rename(struct renamedata *rd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
4683 int error;
Christian Brauner9fe61452021-01-21 14:19:32 +01004684 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
4685 struct dentry *old_dentry = rd->old_dentry;
4686 struct dentry *new_dentry = rd->new_dentry;
4687 struct inode **delegated_inode = rd->delegated_inode;
4688 unsigned int flags = rd->flags;
Miklos Szeredibc270272014-04-01 17:08:42 +02004689 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004690 struct inode *source = old_dentry->d_inode;
4691 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004692 bool new_is_dir = false;
4693 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004694 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004696 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004697 return 0;
4698
Christian Brauner6521f892021-01-21 14:19:33 +01004699 error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 if (error)
4701 return error;
4702
Miklos Szeredida1ce062014-04-01 17:08:43 +02004703 if (!target) {
Christian Brauner6521f892021-01-21 14:19:33 +01004704 error = may_create(rd->new_mnt_userns, new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004705 } else {
4706 new_is_dir = d_is_dir(new_dentry);
4707
4708 if (!(flags & RENAME_EXCHANGE))
Christian Brauner6521f892021-01-21 14:19:33 +01004709 error = may_delete(rd->new_mnt_userns, new_dir,
4710 new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004711 else
Christian Brauner6521f892021-01-21 14:19:33 +01004712 error = may_delete(rd->new_mnt_userns, new_dir,
4713 new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 if (error)
4716 return error;
4717
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004718 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 return -EPERM;
4720
Miklos Szeredibc270272014-04-01 17:08:42 +02004721 /*
4722 * If we are going to change the parent - check write permissions,
4723 * we'll need to flip '..'.
4724 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004725 if (new_dir != old_dir) {
4726 if (is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004727 error = inode_permission(rd->old_mnt_userns, source,
Christian Brauner47291ba2021-01-21 14:19:24 +01004728 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004729 if (error)
4730 return error;
4731 }
4732 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004733 error = inode_permission(rd->new_mnt_userns, target,
Christian Brauner47291ba2021-01-21 14:19:24 +01004734 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004735 if (error)
4736 return error;
4737 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004738 }
Robert Love0eeca282005-07-12 17:06:03 -04004739
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004740 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4741 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004742 if (error)
4743 return error;
4744
Al Viro49d31c22017-07-07 14:51:19 -04004745 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004746 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004747 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004748 lock_two_nondirectories(source, target);
4749 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004750 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004751
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004752 error = -EPERM;
4753 if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target)))
4754 goto out;
4755
Miklos Szeredibc270272014-04-01 17:08:42 +02004756 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004757 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004758 goto out;
4759
Miklos Szeredida1ce062014-04-01 17:08:43 +02004760 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004761 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004762 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004763 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004764 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4765 old_dir->i_nlink >= max_links)
4766 goto out;
4767 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004768 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004769 error = try_break_deleg(source, delegated_inode);
4770 if (error)
4771 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004772 }
4773 if (target && !new_is_dir) {
4774 error = try_break_deleg(target, delegated_inode);
4775 if (error)
4776 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004777 }
Christian Brauner549c7292021-01-21 14:19:43 +01004778 error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry,
4779 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004780 if (error)
4781 goto out;
4782
Miklos Szeredida1ce062014-04-01 17:08:43 +02004783 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004784 if (is_dir) {
4785 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004786 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004787 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004788 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004789 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004790 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004791 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4792 if (!(flags & RENAME_EXCHANGE))
4793 d_move(old_dentry, new_dentry);
4794 else
4795 d_exchange(old_dentry, new_dentry);
4796 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004797out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004798 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004799 unlock_two_nondirectories(source, target);
4800 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004801 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004802 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004803 if (!error) {
Al Virof4ec3a32019-04-26 13:21:24 -04004804 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004805 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4806 if (flags & RENAME_EXCHANGE) {
Al Virof4ec3a32019-04-26 13:21:24 -04004807 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004808 new_is_dir, NULL, new_dentry);
4809 }
4810 }
Al Viro49d31c22017-07-07 14:51:19 -04004811 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return error;
4814}
Al Viro4d359502014-03-14 12:20:17 -04004815EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Jens Axboee8866632020-09-26 17:20:17 -06004817int do_renameat2(int olddfd, struct filename *from, int newdfd,
4818 struct filename *to, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819{
Christian Brauner9fe61452021-01-21 14:19:32 +01004820 struct renamedata rd;
Al Viro2ad94ae2008-07-21 09:32:51 -04004821 struct dentry *old_dentry, *new_dentry;
4822 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004823 struct path old_path, new_path;
4824 struct qstr old_last, new_last;
4825 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004826 struct inode *delegated_inode = NULL;
Al Virof5beed72015-04-30 16:09:11 -04004827 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004828 bool should_retry = false;
Jens Axboee8866632020-09-26 17:20:17 -06004829 int error = -EINVAL;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004830
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004831 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004832 goto put_names;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004833
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004834 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4835 (flags & RENAME_EXCHANGE))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004836 goto put_names;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004837
Al Virof5beed72015-04-30 16:09:11 -04004838 if (flags & RENAME_EXCHANGE)
4839 target_flags = 0;
4840
Jeff Laytonc6a94282012-12-11 12:10:10 -05004841retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004842 error = filename_parentat(olddfd, from, lookup_flags, &old_path,
4843 &old_last, &old_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004844 if (error)
4845 goto put_names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Al Viroc5f563f2021-09-07 15:57:42 -04004847 error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4848 &new_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004849 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 goto exit1;
4851
4852 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004853 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 goto exit2;
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004857 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 goto exit2;
4859
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004860 if (flags & RENAME_NOREPLACE)
4861 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004862 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 goto exit2;
4864
Al Virof5beed72015-04-30 16:09:11 -04004865 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004866 if (error)
4867 goto exit2;
4868
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004869retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004870 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Al Virof5beed72015-04-30 16:09:11 -04004872 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 error = PTR_ERR(old_dentry);
4874 if (IS_ERR(old_dentry))
4875 goto exit3;
4876 /* source must exist */
4877 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004878 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004880 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 error = PTR_ERR(new_dentry);
4882 if (IS_ERR(new_dentry))
4883 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004884 error = -EEXIST;
4885 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4886 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004887 if (flags & RENAME_EXCHANGE) {
4888 error = -ENOENT;
4889 if (d_is_negative(new_dentry))
4890 goto exit5;
4891
4892 if (!d_is_dir(new_dentry)) {
4893 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004894 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004895 goto exit5;
4896 }
4897 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004898 /* unless the source is a directory trailing slashes give -ENOTDIR */
4899 if (!d_is_dir(old_dentry)) {
4900 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004901 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004902 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004903 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004904 goto exit5;
4905 }
4906 /* source should not be ancestor of target */
4907 error = -EINVAL;
4908 if (old_dentry == trap)
4909 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004911 if (!(flags & RENAME_EXCHANGE))
4912 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 if (new_dentry == trap)
4914 goto exit5;
4915
Al Virof5beed72015-04-30 16:09:11 -04004916 error = security_path_rename(&old_path, old_dentry,
4917 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004918 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004919 goto exit5;
Christian Brauner9fe61452021-01-21 14:19:32 +01004920
4921 rd.old_dir = old_path.dentry->d_inode;
4922 rd.old_dentry = old_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004923 rd.old_mnt_userns = mnt_user_ns(old_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004924 rd.new_dir = new_path.dentry->d_inode;
4925 rd.new_dentry = new_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004926 rd.new_mnt_userns = mnt_user_ns(new_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004927 rd.delegated_inode = &delegated_inode;
4928 rd.flags = flags;
4929 error = vfs_rename(&rd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930exit5:
4931 dput(new_dentry);
4932exit4:
4933 dput(old_dentry);
4934exit3:
Al Virof5beed72015-04-30 16:09:11 -04004935 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004936 if (delegated_inode) {
4937 error = break_deleg_wait(&delegated_inode);
4938 if (!error)
4939 goto retry_deleg;
4940 }
Al Virof5beed72015-04-30 16:09:11 -04004941 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004943 if (retry_estale(error, lookup_flags))
4944 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004945 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946exit1:
Al Virof5beed72015-04-30 16:09:11 -04004947 path_put(&old_path);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004948 if (should_retry) {
4949 should_retry = false;
4950 lookup_flags |= LOOKUP_REVAL;
4951 goto retry;
4952 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004953put_names:
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004954 putname(from);
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004955 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 return error;
4957}
4958
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004959SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4960 int, newdfd, const char __user *, newname, unsigned int, flags)
4961{
Jens Axboee8866632020-09-26 17:20:17 -06004962 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4963 flags);
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004964}
4965
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004966SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4967 int, newdfd, const char __user *, newname)
4968{
Jens Axboee8866632020-09-26 17:20:17 -06004969 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4970 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004971}
4972
Heiko Carstensa26eab22009-01-14 14:14:17 +01004973SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004974{
Jens Axboee8866632020-09-26 17:20:17 -06004975 return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4976 getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004977}
4978
Al Viro5d826c82014-03-14 13:42:45 -04004979int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
Al Viro5d826c82014-03-14 13:42:45 -04004981 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 if (IS_ERR(link))
4983 goto out;
4984
4985 len = strlen(link);
4986 if (len > (unsigned) buflen)
4987 len = buflen;
4988 if (copy_to_user(buffer, link, len))
4989 len = -EFAULT;
4990out:
4991 return len;
4992}
4993
Miklos Szeredid60874c2016-10-04 14:40:45 +02004994/**
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004995 * vfs_readlink - copy symlink body into userspace buffer
4996 * @dentry: dentry on which to get symbolic link
4997 * @buffer: user memory pointer
4998 * @buflen: size of buffer
4999 *
5000 * Does not touch atime. That's up to the caller if necessary
5001 *
5002 * Does not call security hook.
5003 */
5004int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
5005{
5006 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04005007 DEFINE_DELAYED_CALL(done);
5008 const char *link;
5009 int res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01005010
Miklos Szeredi76fca902016-12-09 16:45:04 +01005011 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
5012 if (unlikely(inode->i_op->readlink))
5013 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01005014
Miklos Szeredi76fca902016-12-09 16:45:04 +01005015 if (!d_is_symlink(dentry))
5016 return -EINVAL;
5017
5018 spin_lock(&inode->i_lock);
5019 inode->i_opflags |= IOP_DEFAULT_READLINK;
5020 spin_unlock(&inode->i_lock);
5021 }
5022
Eric Biggers4c4f7c12019-04-10 13:21:14 -07005023 link = READ_ONCE(inode->i_link);
Al Virof2df5da2018-07-19 17:35:51 -04005024 if (!link) {
5025 link = inode->i_op->get_link(dentry, inode, &done);
5026 if (IS_ERR(link))
5027 return PTR_ERR(link);
5028 }
5029 res = readlink_copy(buffer, buflen, link);
5030 do_delayed_call(&done);
5031 return res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01005032}
5033EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Miklos Szeredid60874c2016-10-04 14:40:45 +02005035/**
5036 * vfs_get_link - get symlink body
5037 * @dentry: dentry on which to get symbolic link
5038 * @done: caller needs to free returned data with this
5039 *
5040 * Calls security hook and i_op->get_link() on the supplied inode.
5041 *
5042 * It does not touch atime. That's up to the caller if necessary.
5043 *
5044 * Does not work on "special" symlinks like /proc/$$/fd/N
5045 */
5046const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
5047{
5048 const char *res = ERR_PTR(-EINVAL);
5049 struct inode *inode = d_inode(dentry);
5050
5051 if (d_is_symlink(dentry)) {
5052 res = ERR_PTR(security_inode_readlink(dentry));
5053 if (!res)
5054 res = inode->i_op->get_link(dentry, inode, done);
5055 }
5056 return res;
5057}
5058EXPORT_SYMBOL(vfs_get_link);
5059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05005061const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05005062 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
Duane Griffinebd09ab2008-12-19 20:47:12 +00005064 char *kaddr;
5065 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05005066 struct address_space *mapping = inode->i_mapping;
5067
Al Virod3883d42015-11-17 10:41:04 -05005068 if (!dentry) {
5069 page = find_get_page(mapping, 0);
5070 if (!page)
5071 return ERR_PTR(-ECHILD);
5072 if (!PageUptodate(page)) {
5073 put_page(page);
5074 return ERR_PTR(-ECHILD);
5075 }
5076 } else {
5077 page = read_mapping_page(mapping, 0, NULL);
5078 if (IS_ERR(page))
5079 return (char*)page;
5080 }
Al Virofceef392015-12-29 15:58:39 -05005081 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05005082 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
5083 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05005084 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00005085 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086}
5087
Al Viro6b255392015-11-17 10:20:54 -05005088EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Al Virofceef392015-12-29 15:58:39 -05005090void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091{
Al Virofceef392015-12-29 15:58:39 -05005092 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093}
Al Viro4d359502014-03-14 12:20:17 -04005094EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Al Viroaa80dea2015-11-16 18:26:34 -05005096int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
5097{
Al Virofceef392015-12-29 15:58:39 -05005098 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05005099 int res = readlink_copy(buffer, buflen,
5100 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05005101 &done));
5102 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05005103 return res;
5104}
5105EXPORT_SYMBOL(page_readlink);
5106
Matthew Wilcox (Oracle)56f57462022-02-22 09:40:54 -05005107int page_symlink(struct inode *inode, const char *symname, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
5109 struct address_space *mapping = inode->i_mapping;
Matthew Wilcox (Oracle)27a77912022-03-03 13:35:20 -05005110 const struct address_space_operations *aops = mapping->a_ops;
Matthew Wilcox (Oracle)56f57462022-02-22 09:40:54 -05005111 bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005112 struct page *page;
Alexander Potapenko1468c6f2022-09-15 17:04:16 +02005113 void *fsdata = NULL;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08005114 int err;
Matthew Wilcox (Oracle)2d878172022-02-22 09:43:12 -05005115 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
NeilBrown7e53cac2006-03-25 03:07:57 -08005117retry:
Matthew Wilcox (Oracle)2d878172022-02-22 09:43:12 -05005118 if (nofs)
5119 flags = memalloc_nofs_save();
Matthew Wilcox (Oracle)27a77912022-03-03 13:35:20 -05005120 err = aops->write_begin(NULL, mapping, 0, len-1, &page, &fsdata);
Matthew Wilcox (Oracle)2d878172022-02-22 09:43:12 -05005121 if (nofs)
5122 memalloc_nofs_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07005124 goto fail;
5125
Al Viro21fc61c2015-11-17 01:07:57 -05005126 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07005127
Matthew Wilcox (Oracle)27a77912022-03-03 13:35:20 -05005128 err = aops->write_end(NULL, mapping, 0, len-1, len-1,
Nick Pigginafddba42007-10-16 01:25:01 -07005129 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 if (err < 0)
5131 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07005132 if (err < len-1)
5133 goto retry;
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 mark_inode_dirty(inode);
5136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137fail:
5138 return err;
5139}
Al Viro4d359502014-03-14 12:20:17 -04005140EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005141
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005142const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05005143 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145EXPORT_SYMBOL(page_symlink_inode_operations);