blob: d81f04f8d81880b84d34598948126a63e33319f2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/namei.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/*
9 * Some corrections by tytso.
10 */
11
12/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
13 * lookup logic.
14 */
15/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
16 */
17
18#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050019#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070020#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
22#include <linux/fs.h>
23#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040025#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/personality.h>
27#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050028#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/syscalls.h>
30#include <linux/mount.h>
31#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080032#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070033#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080034#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070035#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040036#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070037#include <linux/posix_acl.h>
Linus Torvalds99d263d2014-09-13 11:30:10 -070038#include <linux/hash.h>
George Spelvin2a18da7a2016-05-23 07:43:58 -040039#include <linux/bitops.h>
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -050040#include <linux/init_task.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080041#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Eric Parise81e3f42009-12-04 15:47:36 -050043#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050044#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* [Feb-1997 T. Schoebel-Theuer]
47 * Fundamental changes in the pathname lookup mechanisms (namei)
48 * were necessary because of omirr. The reason is that omirr needs
49 * to know the _real_ pathname, not the user-supplied one, in case
50 * of symlinks (and also when transname replacements occur).
51 *
52 * The new code replaces the old recursive symlink resolution with
53 * an iterative one (in case of non-nested symlink chains). It does
54 * this with calls to <fs>_follow_link().
55 * As a side effect, dir_namei(), _namei() and follow_link() are now
56 * replaced with a single function lookup_dentry() that can handle all
57 * the special cases of the former code.
58 *
59 * With the new dcache, the pathname is stored at each inode, at least as
60 * long as the refcount of the inode is positive. As a side effect, the
61 * size of the dcache depends on the inode cache and thus is dynamic.
62 *
63 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
64 * resolution to correspond with current state of the code.
65 *
66 * Note that the symlink resolution is not *completely* iterative.
67 * There is still a significant amount of tail- and mid- recursion in
68 * the algorithm. Also, note that <fs>_readlink() is not used in
69 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
70 * may return different results than <fs>_follow_link(). Many virtual
71 * filesystems (including /proc) exhibit this behavior.
72 */
73
74/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
75 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
76 * and the name already exists in form of a symlink, try to create the new
77 * name indicated by the symlink. The old code always complained that the
78 * name already exists, due to not following the symlink even if its target
79 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030080 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
82 * I don't know which semantics is the right one, since I have no access
83 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
84 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
85 * "old" one. Personally, I think the new semantics is much more logical.
86 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
87 * file does succeed in both HP-UX and SunOs, but not in Solaris
88 * and in the old Linux semantics.
89 */
90
91/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
92 * semantics. See the comments in "open_namei" and "do_link" below.
93 *
94 * [10-Sep-98 Alan Modra] Another symlink change.
95 */
96
97/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
98 * inside the path - always follow.
99 * in the last component in creation/removal/renaming - never follow.
100 * if LOOKUP_FOLLOW passed - follow.
101 * if the pathname has trailing slashes - follow.
102 * otherwise - don't follow.
103 * (applied in that order).
104 *
105 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
106 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
107 * During the 2.4 we need to fix the userland stuff depending on it -
108 * hopefully we will be able to get rid of that wart in 2.5. So far only
109 * XEmacs seems to be relying on it...
110 */
111/*
112 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800113 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * any extra contention...
115 */
116
117/* In order to reduce some races, while at the same time doing additional
118 * checking and hopefully speeding things up, we copy filenames to the
119 * kernel data space before using them..
120 *
121 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
122 * PATH_MAX includes the nul terminator --RR.
123 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400124
Al Virofd2f7cb2015-02-22 20:07:13 -0500125#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400126
David Drysdale51f39a12014-12-12 16:57:29 -0800127struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400128getname_flags(const char __user *filename, int flags, int *empty)
129{
Al Viro94b5d262015-02-22 19:38:03 -0500130 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400131 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500132 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jeff Layton7ac86262012-10-10 15:25:28 -0400134 result = audit_reusename(filename);
135 if (result)
136 return result;
137
Jeff Layton7950e382012-10-10 16:43:13 -0400138 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700139 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500140 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jeff Layton7950e382012-10-10 16:43:13 -0400142 /*
143 * First, try to embed the struct filename inside the names_cache
144 * allocation
145 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500146 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400147 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400148
Al Viro94b5d262015-02-22 19:38:03 -0500149 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400150 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500151 __putname(result);
152 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400153 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700154
Jeff Layton7950e382012-10-10 16:43:13 -0400155 /*
156 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
157 * separate struct filename so we can dedicate the entire
158 * names_cache allocation for the pathname, and re-do the copy from
159 * userland.
160 */
Al Viro94b5d262015-02-22 19:38:03 -0500161 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500162 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400163 kname = (char *)result;
164
Al Virofd2f7cb2015-02-22 20:07:13 -0500165 /*
166 * size is chosen that way we to guarantee that
167 * result->iname[0] is within the same object and that
168 * kname can't be equal to result->iname, no matter what.
169 */
170 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500171 if (unlikely(!result)) {
172 __putname(kname);
173 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400174 }
175 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500176 len = strncpy_from_user(kname, filename, PATH_MAX);
177 if (unlikely(len < 0)) {
178 __putname(kname);
179 kfree(result);
180 return ERR_PTR(len);
181 }
182 if (unlikely(len == PATH_MAX)) {
183 __putname(kname);
184 kfree(result);
185 return ERR_PTR(-ENAMETOOLONG);
186 }
Jeff Layton7950e382012-10-10 16:43:13 -0400187 }
188
Al Viro94b5d262015-02-22 19:38:03 -0500189 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700190 /* The empty path is special. */
191 if (unlikely(!len)) {
192 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500193 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500194 if (!(flags & LOOKUP_EMPTY)) {
195 putname(result);
196 return ERR_PTR(-ENOENT);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700199
Jeff Layton7950e382012-10-10 16:43:13 -0400200 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800201 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400202 audit_getname(result);
203 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Jeff Layton91a27b22012-10-10 15:25:28 -0400206struct filename *
Dmitry Kadashev8228e2c2021-07-08 13:34:42 +0700207getname_uflags(const char __user *filename, int uflags)
208{
209 int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
210
211 return getname_flags(filename, flags, NULL);
212}
213
214struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400215getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400216{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700217 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400218}
219
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800220struct filename *
221getname_kernel(const char * filename)
222{
223 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500224 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800225
226 result = __getname();
227 if (unlikely(!result))
228 return ERR_PTR(-ENOMEM);
229
Paul Moore08518542015-01-21 23:59:56 -0500230 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500231 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500232 } else if (len <= PATH_MAX) {
Al Viro30ce4d12018-04-08 11:57:10 -0400233 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500234 struct filename *tmp;
235
Al Viro30ce4d12018-04-08 11:57:10 -0400236 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500237 if (unlikely(!tmp)) {
238 __putname(result);
239 return ERR_PTR(-ENOMEM);
240 }
241 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500242 result = tmp;
243 } else {
244 __putname(result);
245 return ERR_PTR(-ENAMETOOLONG);
246 }
247 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800248 result->uptr = NULL;
249 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500250 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500251 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800252
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800253 return result;
254}
255
Jeff Layton91a27b22012-10-10 15:25:28 -0400256void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Al Viroea47ab12021-09-07 16:14:05 -0400258 if (IS_ERR(name))
Dmitry Kadashev91ef6582021-07-08 13:34:37 +0700259 return;
260
Paul Moore55422d02015-01-22 00:00:23 -0500261 BUG_ON(name->refcnt <= 0);
262
263 if (--name->refcnt > 0)
264 return;
265
Al Virofd2f7cb2015-02-22 20:07:13 -0500266 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500267 __putname(name->name);
268 kfree(name);
269 } else
270 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Christian Brauner47291ba2021-01-21 14:19:24 +0100273/**
274 * check_acl - perform ACL permission checking
275 * @mnt_userns: user namespace of the mount the inode was found from
276 * @inode: inode to check permissions on
277 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
278 *
279 * This function performs the ACL permission checking. Since this function
280 * retrieve POSIX acls it needs to know whether it is called from a blocking or
281 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
282 *
283 * If the inode has been found through an idmapped mount the user namespace of
284 * the vfsmount must be passed through @mnt_userns. This function will then take
285 * care to map the inode according to @mnt_userns before checking permissions.
286 * On non-idmapped mounts or if permission checking is to be performed on the
287 * raw inode simply passs init_user_ns.
288 */
289static int check_acl(struct user_namespace *mnt_userns,
290 struct inode *inode, int mask)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700291{
Linus Torvalds84635d62011-07-25 22:47:03 -0700292#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700293 struct posix_acl *acl;
294
Linus Torvaldse77819e2011-07-22 19:30:19 -0700295 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400296 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
297 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700298 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400299 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100300 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400301 return -ECHILD;
Christian Brauner47291ba2021-01-21 14:19:24 +0100302 return posix_acl_permission(mnt_userns, inode, acl, mask);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700303 }
304
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800305 acl = get_acl(inode, ACL_TYPE_ACCESS);
306 if (IS_ERR(acl))
307 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700308 if (acl) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100309 int error = posix_acl_permission(mnt_userns, inode, acl, mask);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700310 posix_acl_release(acl);
311 return error;
312 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700313#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700314
315 return -EAGAIN;
316}
317
Christian Brauner47291ba2021-01-21 14:19:24 +0100318/**
319 * acl_permission_check - perform basic UNIX permission checking
320 * @mnt_userns: user namespace of the mount the inode was found from
321 * @inode: inode to check permissions on
322 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700323 *
Christian Brauner47291ba2021-01-21 14:19:24 +0100324 * This function performs the basic UNIX permission checking. Since this
325 * function may retrieve POSIX acls it needs to know whether it is called from a
326 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
327 *
328 * If the inode has been found through an idmapped mount the user namespace of
329 * the vfsmount must be passed through @mnt_userns. This function will then take
330 * care to map the inode according to @mnt_userns before checking permissions.
331 * On non-idmapped mounts or if permission checking is to be performed on the
332 * raw inode simply passs init_user_ns.
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700333 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100334static int acl_permission_check(struct user_namespace *mnt_userns,
335 struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700336{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700337 unsigned int mode = inode->i_mode;
Christian Brauner47291ba2021-01-21 14:19:24 +0100338 kuid_t i_uid;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700339
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700340 /* Are we the owner? If so, ACL's don't matter */
Christian Brauner47291ba2021-01-21 14:19:24 +0100341 i_uid = i_uid_into_mnt(mnt_userns, inode);
342 if (likely(uid_eq(current_fsuid(), i_uid))) {
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700343 mask &= 7;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700344 mode >>= 6;
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700345 return (mask & ~mode) ? -EACCES : 0;
346 }
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700347
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700348 /* Do we have ACL's? */
349 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100350 int error = check_acl(mnt_userns, inode, mask);
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700351 if (error != -EAGAIN)
352 return error;
353 }
354
355 /* Only RWX matters for group/other mode bits */
356 mask &= 7;
357
358 /*
359 * Are the group permissions different from
360 * the other permissions in the bits we care
361 * about? Need to check group ownership if so.
362 */
363 if (mask & (mode ^ (mode >> 3))) {
Christian Brauner47291ba2021-01-21 14:19:24 +0100364 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
365 if (in_group_p(kgid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700366 mode >>= 3;
367 }
368
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700369 /* Bits in 'mode' clear that we require? */
370 return (mask & ~mode) ? -EACCES : 0;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100374 * generic_permission - check for access rights on a Posix-like filesystem
Christian Brauner47291ba2021-01-21 14:19:24 +0100375 * @mnt_userns: user namespace of the mount the inode was found from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * @inode: inode to check access rights for
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700377 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
378 * %MAY_NOT_BLOCK ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 *
380 * Used to check for read/write/execute permissions on a file.
381 * We use "fsuid" for this, letting us set arbitrary permissions
382 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100383 * are used for other things.
384 *
385 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
386 * request cannot be satisfied (eg. requires blocking or too much complexity).
387 * It would then be called again in ref-walk mode.
Christian Brauner47291ba2021-01-21 14:19:24 +0100388 *
389 * If the inode has been found through an idmapped mount the user namespace of
390 * the vfsmount must be passed through @mnt_userns. This function will then take
391 * care to map the inode according to @mnt_userns before checking permissions.
392 * On non-idmapped mounts or if permission checking is to be performed on the
393 * raw inode simply passs init_user_ns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100395int generic_permission(struct user_namespace *mnt_userns, struct inode *inode,
396 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700398 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530401 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100403 ret = acl_permission_check(mnt_userns, inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700404 if (ret != -EACCES)
405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Al Virod594e7e2011-06-20 19:55:42 -0400407 if (S_ISDIR(inode->i_mode)) {
408 /* DACs are overridable for directories */
Al Virod594e7e2011-06-20 19:55:42 -0400409 if (!(mask & MAY_WRITE))
Christian Brauner47291ba2021-01-21 14:19:24 +0100410 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700411 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400412 return 0;
Christian Brauner47291ba2021-01-21 14:19:24 +0100413 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100414 CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500416 return -EACCES;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /*
420 * Searching includes executable on directories, else just read.
421 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600422 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400423 if (mask == MAY_READ)
Christian Brauner47291ba2021-01-21 14:19:24 +0100424 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100425 CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500427 /*
428 * Read/write DACs are always overridable.
429 * Executable DACs are overridable when there is
430 * at least one exec bit set.
431 */
432 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Christian Brauner47291ba2021-01-21 14:19:24 +0100433 if (capable_wrt_inode_uidgid(mnt_userns, inode,
Christian Brauner0558c1b2021-01-21 14:19:23 +0100434 CAP_DAC_OVERRIDE))
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return -EACCES;
438}
Al Viro4d359502014-03-14 12:20:17 -0400439EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Christian Brauner47291ba2021-01-21 14:19:24 +0100441/**
442 * do_inode_permission - UNIX permission checking
443 * @mnt_userns: user namespace of the mount the inode was found from
444 * @inode: inode to check permissions on
445 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
446 *
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700447 * We _really_ want to just do "generic_permission()" without
448 * even looking at the inode->i_op values. So we keep a cache
449 * flag in inode->i_opflags, that says "this has not special
450 * permission function, use the fast case".
451 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100452static inline int do_inode_permission(struct user_namespace *mnt_userns,
453 struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700454{
455 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
456 if (likely(inode->i_op->permission))
Christian Brauner549c7292021-01-21 14:19:43 +0100457 return inode->i_op->permission(mnt_userns, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700458
459 /* This gets set once for the inode lifetime */
460 spin_lock(&inode->i_lock);
461 inode->i_opflags |= IOP_FASTPERM;
462 spin_unlock(&inode->i_lock);
463 }
Christian Brauner47291ba2021-01-21 14:19:24 +0100464 return generic_permission(mnt_userns, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700465}
466
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200467/**
David Howells0bdaea92012-06-25 12:55:46 +0100468 * sb_permission - Check superblock-level permissions
469 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700470 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100471 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
472 *
473 * Separate out file-system wide checks from inode-specific permission checks.
474 */
475static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
476{
477 if (unlikely(mask & MAY_WRITE)) {
478 umode_t mode = inode->i_mode;
479
480 /* Nobody gets write access to a read-only fs. */
David Howellsbc98a422017-07-17 08:45:34 +0100481 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
David Howells0bdaea92012-06-25 12:55:46 +0100482 return -EROFS;
483 }
484 return 0;
485}
486
487/**
488 * inode_permission - Check for access rights to a given inode
Christian Brauner47291ba2021-01-21 14:19:24 +0100489 * @mnt_userns: User namespace of the mount the inode was found from
490 * @inode: Inode to check permission on
491 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
David Howells0bdaea92012-06-25 12:55:46 +0100492 *
493 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
494 * this, letting us set arbitrary permissions for filesystem access without
495 * changing the "normal" UIDs which are used for other things.
496 *
497 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
498 */
Christian Brauner47291ba2021-01-21 14:19:24 +0100499int inode_permission(struct user_namespace *mnt_userns,
500 struct inode *inode, int mask)
David Howells0bdaea92012-06-25 12:55:46 +0100501{
502 int retval;
503
504 retval = sb_permission(inode->i_sb, inode, mask);
505 if (retval)
506 return retval;
Eric Biggers4bfd0542018-01-16 21:44:24 -0800507
508 if (unlikely(mask & MAY_WRITE)) {
509 /*
510 * Nobody gets write access to an immutable file.
511 */
512 if (IS_IMMUTABLE(inode))
513 return -EPERM;
514
515 /*
516 * Updating mtime will likely cause i_uid and i_gid to be
517 * written back improperly if their true value is unknown
518 * to the vfs.
519 */
Christian Braunerba73d982021-01-21 14:19:31 +0100520 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric Biggers4bfd0542018-01-16 21:44:24 -0800521 return -EACCES;
522 }
523
Christian Brauner47291ba2021-01-21 14:19:24 +0100524 retval = do_inode_permission(mnt_userns, inode, mask);
Eric Biggers4bfd0542018-01-16 21:44:24 -0800525 if (retval)
526 return retval;
527
528 retval = devcgroup_inode_permission(inode, mask);
529 if (retval)
530 return retval;
531
532 return security_inode_permission(inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100533}
Al Viro4d359502014-03-14 12:20:17 -0400534EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100535
536/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800537 * path_get - get a reference to a path
538 * @path: path to get the reference to
539 *
540 * Given a path increment the reference count to the dentry and the vfsmount.
541 */
Al Virodcf787f2013-03-01 23:51:07 -0500542void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800543{
544 mntget(path->mnt);
545 dget(path->dentry);
546}
547EXPORT_SYMBOL(path_get);
548
549/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800550 * path_put - put a reference to a path
551 * @path: path to put the reference to
552 *
553 * Given a path decrement the reference count to the dentry and the vfsmount.
554 */
Al Virodcf787f2013-03-01 23:51:07 -0500555void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Jan Blunck1d957f92008-02-14 19:34:35 -0800557 dput(path->dentry);
558 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Jan Blunck1d957f92008-02-14 19:34:35 -0800560EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Al Viro894bc8c2015-05-02 07:16:16 -0400562#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400563struct nameidata {
564 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400565 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400566 struct path root;
567 struct inode *inode; /* path.dentry.d_inode */
Al Virobcba1e72021-04-01 22:03:41 -0400568 unsigned int flags, state;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +1100569 unsigned seq, m_seq, r_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400570 int last_type;
571 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100572 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400573 struct saved {
574 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500575 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400576 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400577 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400578 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400579 struct filename *name;
580 struct nameidata *saved;
581 unsigned root_seq;
582 int dfd;
Al Viro0f705952020-03-05 11:34:48 -0500583 kuid_t dir_uid;
584 umode_t dir_mode;
Kees Cook3859a272016-10-28 01:22:25 -0700585} __randomize_layout;
Al Viro1f55a6e2014-11-01 19:30:41 -0400586
Al Virobcba1e72021-04-01 22:03:41 -0400587#define ND_ROOT_PRESET 1
588#define ND_ROOT_GRABBED 2
589#define ND_JUMPED 4
590
Al Viro06422962021-04-01 22:28:03 -0400591static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400592{
NeilBrown756daf22015-03-23 13:37:38 +1100593 struct nameidata *old = current->nameidata;
594 p->stack = p->internal;
Al Viro7962c7d2021-04-03 16:49:44 -0400595 p->depth = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -0400596 p->dfd = dfd;
597 p->name = name;
Al Viro7d01ef72021-04-06 12:33:07 -0400598 p->path.mnt = NULL;
599 p->path.dentry = NULL;
NeilBrown756daf22015-03-23 13:37:38 +1100600 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400601 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100602 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400603}
604
Al Viro06422962021-04-01 22:28:03 -0400605static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name,
606 const struct path *root)
607{
608 __set_nameidata(p, dfd, name);
609 p->state = 0;
610 if (unlikely(root)) {
611 p->state = ND_ROOT_PRESET;
612 p->root = *root;
613 }
614}
615
Al Viro9883d182015-05-13 07:28:08 -0400616static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400617{
Al Viro9883d182015-05-13 07:28:08 -0400618 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100619
620 current->nameidata = old;
621 if (old)
622 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500623 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100624 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400625}
626
Al Viro60ef60c2020-03-03 11:43:55 -0500627static bool nd_alloc_stack(struct nameidata *nd)
Al Viro894bc8c2015-05-02 07:16:16 -0400628{
Al Virobc40aee2015-05-09 13:04:24 -0400629 struct saved *p;
630
Al Viro60ef60c2020-03-03 11:43:55 -0500631 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
632 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
633 if (unlikely(!p))
634 return false;
Al Viro894bc8c2015-05-02 07:16:16 -0400635 memcpy(p, nd->internal, sizeof(nd->internal));
636 nd->stack = p;
Al Viro60ef60c2020-03-03 11:43:55 -0500637 return true;
Al Viro894bc8c2015-05-02 07:16:16 -0400638}
639
Eric W. Biederman397d4252015-08-15 20:27:13 -0500640/**
Al Viro6b03f7e2020-02-24 15:53:19 -0500641 * path_connected - Verify that a dentry is below mnt.mnt_root
Eric W. Biederman397d4252015-08-15 20:27:13 -0500642 *
643 * Rename can sometimes move a file or directory outside of a bind
644 * mount, path_connected allows those cases to be detected.
645 */
Al Viro6b03f7e2020-02-24 15:53:19 -0500646static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500647{
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500648 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500649
Christoph Hellwig402dd2c2020-09-24 08:51:28 +0200650 /* Bind mounts can have disconnected paths */
651 if (mnt->mnt_root == sb->s_root)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500652 return true;
653
Al Viro6b03f7e2020-02-24 15:53:19 -0500654 return is_subdir(dentry, mnt->mnt_root);
Eric W. Biederman397d4252015-08-15 20:27:13 -0500655}
656
Al Viro79733872015-05-09 12:55:43 -0400657static void drop_links(struct nameidata *nd)
658{
659 int i = nd->depth;
660 while (i--) {
661 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500662 do_delayed_call(&last->done);
663 clear_delayed_call(&last->done);
Al Viro79733872015-05-09 12:55:43 -0400664 }
665}
666
667static void terminate_walk(struct nameidata *nd)
668{
669 drop_links(nd);
670 if (!(nd->flags & LOOKUP_RCU)) {
671 int i;
672 path_put(&nd->path);
673 for (i = 0; i < nd->depth; i++)
674 path_put(&nd->stack[i].link);
Al Virobcba1e72021-04-01 22:03:41 -0400675 if (nd->state & ND_ROOT_GRABBED) {
Al Viro102b8af2015-05-12 17:35:52 -0400676 path_put(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400677 nd->state &= ~ND_ROOT_GRABBED;
Al Viro102b8af2015-05-12 17:35:52 -0400678 }
Al Viro79733872015-05-09 12:55:43 -0400679 } else {
680 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400681 rcu_read_unlock();
682 }
683 nd->depth = 0;
Al Viro7d01ef72021-04-06 12:33:07 -0400684 nd->path.mnt = NULL;
685 nd->path.dentry = NULL;
Al Viro79733872015-05-09 12:55:43 -0400686}
687
688/* path_put is needed afterwards regardless of success or failure */
Al Viro2aa38472020-02-26 19:19:05 -0500689static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
Al Viro79733872015-05-09 12:55:43 -0400690{
Al Viro2aa38472020-02-26 19:19:05 -0500691 int res = __legitimize_mnt(path->mnt, mseq);
Al Viro79733872015-05-09 12:55:43 -0400692 if (unlikely(res)) {
693 if (res > 0)
694 path->mnt = NULL;
695 path->dentry = NULL;
696 return false;
697 }
698 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
699 path->dentry = NULL;
700 return false;
701 }
702 return !read_seqcount_retry(&path->dentry->d_seq, seq);
703}
704
Al Viro2aa38472020-02-26 19:19:05 -0500705static inline bool legitimize_path(struct nameidata *nd,
706 struct path *path, unsigned seq)
707{
Al Viro5bd73282020-04-05 21:59:55 -0400708 return __legitimize_path(path, seq, nd->m_seq);
Al Viro2aa38472020-02-26 19:19:05 -0500709}
710
Al Viro79733872015-05-09 12:55:43 -0400711static bool legitimize_links(struct nameidata *nd)
712{
713 int i;
Al Viroeacd9aa82021-02-15 12:03:23 -0500714 if (unlikely(nd->flags & LOOKUP_CACHED)) {
715 drop_links(nd);
716 nd->depth = 0;
717 return false;
718 }
Al Viro79733872015-05-09 12:55:43 -0400719 for (i = 0; i < nd->depth; i++) {
720 struct saved *last = nd->stack + i;
721 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
722 drop_links(nd);
723 nd->depth = i + 1;
724 return false;
725 }
726 }
727 return true;
728}
729
Al Viroee594bf2019-07-16 21:05:36 -0400730static bool legitimize_root(struct nameidata *nd)
731{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100732 /*
733 * For scoped-lookups (where nd->root has been zeroed), we need to
734 * restart the whole lookup from scratch -- because set_root() is wrong
735 * for these lookups (nd->dfd is the root, not the filesystem root).
736 */
737 if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
738 return false;
739 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
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 Viro79733872015-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
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700774 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400775 if (unlikely(!legitimize_links(nd)))
Al Viro4675ac32017-01-09 22:29:15 -0500776 goto out1;
Al Viro84a2bd32019-07-16 21:20:17 -0400777 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
778 goto out;
Al Viroee594bf2019-07-16 21:05:36 -0400779 if (unlikely(!legitimize_root(nd)))
780 goto out;
Al Viro4675ac32017-01-09 22:29:15 -0500781 rcu_read_unlock();
782 BUG_ON(nd->inode != parent->d_inode);
Jens Axboee36cffe2020-12-17 09:19:08 -0700783 return true;
Al Viro4675ac32017-01-09 22:29:15 -0500784
Al Viro84a2bd32019-07-16 21:20:17 -0400785out1:
Al Viro4675ac32017-01-09 22:29:15 -0500786 nd->path.mnt = NULL;
787 nd->path.dentry = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500788out:
789 rcu_read_unlock();
Jens Axboee36cffe2020-12-17 09:19:08 -0700790 return false;
Al Viro4675ac32017-01-09 22:29:15 -0500791}
792
793/**
Al Viroae66db42021-01-04 00:08:41 -0500794 * try_to_unlazy_next - try to switch to ref-walk mode.
Al Viro4675ac32017-01-09 22:29:15 -0500795 * @nd: nameidata pathwalk data
Al Viroae66db42021-01-04 00:08:41 -0500796 * @dentry: next dentry to step into
797 * @seq: seq number to check @dentry against
798 * Returns: true on success, false on failure
Al Viro4675ac32017-01-09 22:29:15 -0500799 *
Al Viroae66db42021-01-04 00:08:41 -0500800 * Similar to to try_to_unlazy(), but here we have the next dentry already
801 * picked by rcu-walk and want to legitimize that in addition to the current
802 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
803 * Nothing should touch nameidata between try_to_unlazy_next() failure and
Al Viro4675ac32017-01-09 22:29:15 -0500804 * terminate_walk().
805 */
Al Viroae66db42021-01-04 00:08:41 -0500806static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq)
Al Viro4675ac32017-01-09 22:29:15 -0500807{
808 BUG_ON(!(nd->flags & LOOKUP_RCU));
809
810 nd->flags &= ~LOOKUP_RCU;
811 if (unlikely(!legitimize_links(nd)))
812 goto out2;
Al Viro79733872015-05-09 12:55:43 -0400813 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
814 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500815 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro79733872015-05-09 12:55:43 -0400816 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400817
Linus Torvalds15570082013-09-02 11:38:06 -0700818 /*
Al Viro4675ac32017-01-09 22:29:15 -0500819 * We need to move both the parent and the dentry from the RCU domain
820 * to be properly refcounted. And the sequence number in the dentry
821 * validates *both* dentry counters, since we checked the sequence
822 * number of the parent after we got the child sequence number. So we
823 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700824 */
Al Viro4675ac32017-01-09 22:29:15 -0500825 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
826 goto out;
Al Viro84a2bd32019-07-16 21:20:17 -0400827 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
828 goto out_dput;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700829 /*
830 * Sequence counts matched. Now make sure that the root is
831 * still valid and get it if required.
832 */
Al Viro84a2bd32019-07-16 21:20:17 -0400833 if (unlikely(!legitimize_root(nd)))
834 goto out_dput;
Al Viro8b61e742013-11-08 12:45:01 -0500835 rcu_read_unlock();
Al Viroae66db42021-01-04 00:08:41 -0500836 return true;
Al Viro19660af2011-03-25 10:32:48 -0400837
Al Viro79733872015-05-09 12:55:43 -0400838out2:
839 nd->path.mnt = NULL;
840out1:
841 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700842out:
Al Viro8b61e742013-11-08 12:45:01 -0500843 rcu_read_unlock();
Al Viroae66db42021-01-04 00:08:41 -0500844 return false;
Al Viro84a2bd32019-07-16 21:20:17 -0400845out_dput:
846 rcu_read_unlock();
847 dput(dentry);
Al Viroae66db42021-01-04 00:08:41 -0500848 return false;
Nick Piggin31e6b012011-01-07 17:49:52 +1100849}
850
Al Viro4ce16ef32012-06-10 16:10:59 -0400851static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100852{
Al Viroa89f8332017-01-09 22:25:28 -0500853 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
854 return dentry->d_op->d_revalidate(dentry, flags);
855 else
856 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100857}
858
Al Viro9f1fafe2011-03-25 11:00:12 -0400859/**
860 * complete_walk - successful completion of path walk
861 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500862 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400863 * If we had been in RCU mode, drop out of it and legitimize nd->path.
864 * Revalidate the final result, unless we'd already done that during
865 * the path walk or the filesystem doesn't ask for it. Return 0 on
866 * success, -error on failure. In case of failure caller does not
867 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500868 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400869static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500870{
Al Viro16c2cd72011-02-22 15:50:10 -0500871 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500872 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500873
Al Viro9f1fafe2011-03-25 11:00:12 -0400874 if (nd->flags & LOOKUP_RCU) {
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100875 /*
876 * We don't want to zero nd->root for scoped-lookups or
877 * externally-managed nd->root.
878 */
Al Virobcba1e72021-04-01 22:03:41 -0400879 if (!(nd->state & ND_ROOT_PRESET))
880 if (!(nd->flags & LOOKUP_IS_SCOPED))
881 nd->root.mnt = NULL;
Jens Axboe6c6ec2b2020-12-17 09:19:09 -0700882 nd->flags &= ~LOOKUP_CACHED;
Jens Axboee36cffe2020-12-17 09:19:08 -0700883 if (!try_to_unlazy(nd))
Al Viro48a066e2013-09-29 22:06:07 -0400884 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400885 }
886
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100887 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
888 /*
889 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
890 * ever step outside the root during lookup" and should already
891 * be guaranteed by the rest of namei, we want to avoid a namei
892 * BUG resulting in userspace being given a path that was not
893 * scoped within the root at some point during the lookup.
894 *
895 * So, do a final sanity-check to make sure that in the
896 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
897 * we won't silently return an fd completely outside of the
898 * requested root to userspace.
899 *
900 * Userspace could move the path outside the root after this
901 * check, but as discussed elsewhere this is not a concern (the
902 * resolved file was inside the root at some point).
903 */
904 if (!path_is_under(&nd->path, &nd->root))
905 return -EXDEV;
906 }
907
Al Virobcba1e72021-04-01 22:03:41 -0400908 if (likely(!(nd->state & ND_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500909 return 0;
910
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500911 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500912 return 0;
913
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500914 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500915 if (status > 0)
916 return 0;
917
Al Viro16c2cd72011-02-22 15:50:10 -0500918 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500919 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500920
Jeff Layton39159de2009-12-07 12:01:50 -0500921 return status;
922}
923
Aleksa Sarai740a1672019-12-07 01:13:29 +1100924static int set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400925{
Al Viro7bd88372014-09-13 21:55:46 -0400926 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100927
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100928 /*
929 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
930 * still have to ensure it doesn't happen because it will cause a breakout
931 * from the dirfd.
932 */
933 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
934 return -ENOTRECOVERABLE;
935
Al Viro9e6697e2015-12-05 20:07:21 -0500936 if (nd->flags & LOOKUP_RCU) {
937 unsigned seq;
938
939 do {
940 seq = read_seqcount_begin(&fs->seq);
941 nd->root = fs->root;
942 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
943 } while (read_seqcount_retry(&fs->seq, seq));
944 } else {
945 get_fs_root(fs, &nd->root);
Al Virobcba1e72021-04-01 22:03:41 -0400946 nd->state |= ND_ROOT_GRABBED;
Al Viro9e6697e2015-12-05 20:07:21 -0500947 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100948 return 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100949}
950
Al Viro248fb5b2015-12-05 20:51:58 -0500951static int nd_jump_root(struct nameidata *nd)
952{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100953 if (unlikely(nd->flags & LOOKUP_BENEATH))
954 return -EXDEV;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100955 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
956 /* Absolute path arguments to path_init() are allowed. */
957 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
958 return -EXDEV;
959 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100960 if (!nd->root.mnt) {
961 int error = set_root(nd);
962 if (error)
963 return error;
964 }
Al Viro248fb5b2015-12-05 20:51:58 -0500965 if (nd->flags & LOOKUP_RCU) {
966 struct dentry *d;
967 nd->path = nd->root;
968 d = nd->path.dentry;
969 nd->inode = d->d_inode;
970 nd->seq = nd->root_seq;
971 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
972 return -ECHILD;
973 } else {
974 path_put(&nd->path);
975 nd->path = nd->root;
976 path_get(&nd->path);
977 nd->inode = nd->path.dentry->d_inode;
978 }
Al Virobcba1e72021-04-01 22:03:41 -0400979 nd->state |= ND_JUMPED;
Al Viro248fb5b2015-12-05 20:51:58 -0500980 return 0;
981}
982
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400983/*
Al Viro6b255392015-11-17 10:20:54 -0500984 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400985 * caller must have taken a reference to path beforehand.
986 */
Aleksa Sarai1bc82072019-12-07 01:13:28 +1100987int nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400988{
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100989 int error = -ELOOP;
Al Viro6e77137b2015-05-02 13:37:52 -0400990 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400991
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100992 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
993 goto err;
994
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100995 error = -EXDEV;
996 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
997 if (nd->path.mnt != path->mnt)
998 goto err;
999 }
Aleksa Saraiadb21d22019-12-07 01:13:33 +11001000 /* Not currently safe for scoped-lookups. */
1001 if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
1002 goto err;
Aleksa Sarai72ba2922019-12-07 01:13:32 +11001003
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001004 path_put(&nd->path);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001005 nd->path = *path;
1006 nd->inode = nd->path.dentry->d_inode;
Al Virobcba1e72021-04-01 22:03:41 -04001007 nd->state |= ND_JUMPED;
Aleksa Sarai1bc82072019-12-07 01:13:28 +11001008 return 0;
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001009
1010err:
1011 path_put(path);
1012 return error;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001013}
1014
Al Virob9ff4422015-05-02 20:19:23 -04001015static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -04001016{
Al Viro21c30032015-05-03 21:06:24 -04001017 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -05001018 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -04001019 if (!(nd->flags & LOOKUP_RCU))
1020 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -04001021}
1022
Linus Torvalds561ec642012-10-26 10:05:07 -07001023int sysctl_protected_symlinks __read_mostly = 0;
1024int sysctl_protected_hardlinks __read_mostly = 0;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001025int sysctl_protected_fifos __read_mostly;
1026int sysctl_protected_regular __read_mostly;
Kees Cook800179c2012-07-25 17:29:07 -07001027
1028/**
1029 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -07001030 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -07001031 *
1032 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1033 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1034 * in a sticky world-writable directory. This is to protect privileged
1035 * processes from failing races against path names that may change out
1036 * from under them by way of other users creating malicious symlinks.
1037 * It will permit symlinks to be followed only when outside a sticky
1038 * world-writable directory, or when the uid of the symlink and follower
1039 * match, or when the directory owner matches the symlink's owner.
1040 *
1041 * Returns 0 if following the symlink is allowed, -ve on error.
1042 */
Al Viroad6cc4c2020-01-14 14:41:39 -05001043static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001044{
Christian Braunerba73d982021-01-21 14:19:31 +01001045 struct user_namespace *mnt_userns;
1046 kuid_t i_uid;
1047
Kees Cook800179c2012-07-25 17:29:07 -07001048 if (!sysctl_protected_symlinks)
1049 return 0;
1050
Christian Braunerba73d982021-01-21 14:19:31 +01001051 mnt_userns = mnt_user_ns(nd->path.mnt);
1052 i_uid = i_uid_into_mnt(mnt_userns, inode);
Kees Cook800179c2012-07-25 17:29:07 -07001053 /* Allowed if owner and follower match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001054 if (uid_eq(current_cred()->fsuid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001055 return 0;
1056
1057 /* Allowed if parent directory not sticky and world-writable. */
Al Viro0f705952020-03-05 11:34:48 -05001058 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
Kees Cook800179c2012-07-25 17:29:07 -07001059 return 0;
1060
1061 /* Allowed if parent directory and link owner match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001062 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001063 return 0;
1064
Al Viro319565022015-05-07 20:37:40 -04001065 if (nd->flags & LOOKUP_RCU)
1066 return -ECHILD;
1067
Richard Guy Briggsea841ba2018-03-21 04:42:21 -04001068 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Kees Cook245d7362019-10-02 16:41:58 -07001069 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
Kees Cook800179c2012-07-25 17:29:07 -07001070 return -EACCES;
1071}
1072
1073/**
1074 * safe_hardlink_source - Check for safe hardlink conditions
Christian Braunerba73d982021-01-21 14:19:31 +01001075 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001076 * @inode: the source inode to hardlink from
1077 *
1078 * Return false if at least one of the following conditions:
1079 * - inode is not a regular file
1080 * - inode is setuid
1081 * - inode is setgid and group-exec
1082 * - access failure for read and write
1083 *
1084 * Otherwise returns true.
1085 */
Christian Braunerba73d982021-01-21 14:19:31 +01001086static bool safe_hardlink_source(struct user_namespace *mnt_userns,
1087 struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001088{
1089 umode_t mode = inode->i_mode;
1090
1091 /* Special files should not get pinned to the filesystem. */
1092 if (!S_ISREG(mode))
1093 return false;
1094
1095 /* Setuid files should not get pinned to the filesystem. */
1096 if (mode & S_ISUID)
1097 return false;
1098
1099 /* Executable setgid files should not get pinned to the filesystem. */
1100 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1101 return false;
1102
1103 /* Hardlinking to unreadable or unwritable sources is dangerous. */
Christian Braunerba73d982021-01-21 14:19:31 +01001104 if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
Kees Cook800179c2012-07-25 17:29:07 -07001105 return false;
1106
1107 return true;
1108}
1109
1110/**
1111 * may_linkat - Check permissions for creating a hardlink
Christian Braunerba73d982021-01-21 14:19:31 +01001112 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001113 * @link: the source to hardlink from
1114 *
1115 * Block hardlink when all of:
1116 * - sysctl_protected_hardlinks enabled
1117 * - fsuid does not match inode
1118 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001119 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -07001120 *
Christian Braunerba73d982021-01-21 14:19:31 +01001121 * If the inode has been found through an idmapped mount the user namespace of
1122 * the vfsmount must be passed through @mnt_userns. This function will then take
1123 * care to map the inode according to @mnt_userns before checking permissions.
1124 * On non-idmapped mounts or if permission checking is to be performed on the
1125 * raw inode simply passs init_user_ns.
1126 *
Kees Cook800179c2012-07-25 17:29:07 -07001127 * Returns 0 if successful, -ve on error.
1128 */
Christian Braunerba73d982021-01-21 14:19:31 +01001129int may_linkat(struct user_namespace *mnt_userns, struct path *link)
Kees Cook800179c2012-07-25 17:29:07 -07001130{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001131 struct inode *inode = link->dentry->d_inode;
1132
1133 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01001134 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
1135 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001136 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -07001137
1138 if (!sysctl_protected_hardlinks)
1139 return 0;
1140
Kees Cook800179c2012-07-25 17:29:07 -07001141 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1142 * otherwise, it must be a safe source.
1143 */
Christian Braunerba73d982021-01-21 14:19:31 +01001144 if (safe_hardlink_source(mnt_userns, inode) ||
1145 inode_owner_or_capable(mnt_userns, inode))
Kees Cook800179c2012-07-25 17:29:07 -07001146 return 0;
1147
Kees Cook245d7362019-10-02 16:41:58 -07001148 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001149 return -EPERM;
1150}
1151
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001152/**
1153 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1154 * should be allowed, or not, on files that already
1155 * exist.
Christian Braunerba73d982021-01-21 14:19:31 +01001156 * @mnt_userns: user namespace of the mount the inode was found from
Randy Dunlap2111c3c2021-02-15 20:29:28 -08001157 * @nd: nameidata pathwalk data
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001158 * @inode: the inode of the file to open
1159 *
1160 * Block an O_CREAT open of a FIFO (or a regular file) when:
1161 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1162 * - the file already exists
1163 * - we are in a sticky directory
1164 * - we don't own the file
1165 * - the owner of the directory doesn't own the file
1166 * - the directory is world writable
1167 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1168 * the directory doesn't have to be world writable: being group writable will
1169 * be enough.
1170 *
Christian Braunerba73d982021-01-21 14:19:31 +01001171 * If the inode has been found through an idmapped mount the user namespace of
1172 * the vfsmount must be passed through @mnt_userns. This function will then take
1173 * care to map the inode according to @mnt_userns before checking permissions.
1174 * On non-idmapped mounts or if permission checking is to be performed on the
1175 * raw inode simply passs init_user_ns.
1176 *
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001177 * Returns 0 if the open is allowed, -ve on error.
1178 */
Christian Braunerba73d982021-01-21 14:19:31 +01001179static int may_create_in_sticky(struct user_namespace *mnt_userns,
1180 struct nameidata *nd, struct inode *const inode)
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001181{
Christian Braunerba73d982021-01-21 14:19:31 +01001182 umode_t dir_mode = nd->dir_mode;
1183 kuid_t dir_uid = nd->dir_uid;
1184
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001185 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1186 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Virod0cb5012020-01-26 09:29:34 -05001187 likely(!(dir_mode & S_ISVTX)) ||
Christian Braunerba73d982021-01-21 14:19:31 +01001188 uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
1189 uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001190 return 0;
1191
Al Virod0cb5012020-01-26 09:29:34 -05001192 if (likely(dir_mode & 0002) ||
1193 (dir_mode & 0020 &&
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001194 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1195 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
Kees Cook245d7362019-10-02 16:41:58 -07001196 const char *operation = S_ISFIFO(inode->i_mode) ?
1197 "sticky_create_fifo" :
1198 "sticky_create_regular";
1199 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001200 return -EACCES;
1201 }
1202 return 0;
1203}
1204
David Howellsf015f1262012-06-25 12:55:28 +01001205/*
1206 * follow_up - Find the mountpoint of path's vfsmount
1207 *
1208 * Given a path, find the mountpoint of its source file system.
1209 * Replace @path with the path of the mountpoint in the parent mount.
1210 * Up is towards /.
1211 *
1212 * Return 1 if we went up a level and 0 if we were already at the
1213 * root.
1214 */
Al Virobab77eb2009-04-18 03:26:48 -04001215int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Al Viro0714a532011-11-24 22:19:58 -05001217 struct mount *mnt = real_mount(path->mnt);
1218 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001220
Al Viro48a066e2013-09-29 22:06:07 -04001221 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001222 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001223 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001224 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return 0;
1226 }
Al Viro0714a532011-11-24 22:19:58 -05001227 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001228 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001229 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001230 dput(path->dentry);
1231 path->dentry = mountpoint;
1232 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001233 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return 1;
1235}
Al Viro4d359502014-03-14 12:20:17 -04001236EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Al Viro7ef482f2020-02-26 17:50:13 -05001238static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1239 struct path *path, unsigned *seqp)
1240{
1241 while (mnt_has_parent(m)) {
1242 struct dentry *mountpoint = m->mnt_mountpoint;
1243
1244 m = m->mnt_parent;
1245 if (unlikely(root->dentry == mountpoint &&
1246 root->mnt == &m->mnt))
1247 break;
1248 if (mountpoint != m->mnt.mnt_root) {
1249 path->mnt = &m->mnt;
1250 path->dentry = mountpoint;
1251 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1252 return true;
1253 }
1254 }
1255 return false;
1256}
1257
Al Viro2aa38472020-02-26 19:19:05 -05001258static bool choose_mountpoint(struct mount *m, const struct path *root,
1259 struct path *path)
1260{
1261 bool found;
1262
1263 rcu_read_lock();
1264 while (1) {
1265 unsigned seq, mseq = read_seqbegin(&mount_lock);
1266
1267 found = choose_mountpoint_rcu(m, root, path, &seq);
1268 if (unlikely(!found)) {
1269 if (!read_seqretry(&mount_lock, mseq))
1270 break;
1271 } else {
1272 if (likely(__legitimize_path(path, seq, mseq)))
1273 break;
1274 rcu_read_unlock();
1275 path_put(path);
1276 rcu_read_lock();
1277 }
1278 }
1279 rcu_read_unlock();
1280 return found;
1281}
1282
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001283/*
David Howells9875cf82011-01-14 18:45:21 +00001284 * Perform an automount
1285 * - return -EISDIR to tell follow_managed() to stop and return the path we
1286 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001288static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
Nick Piggin31e6b012011-01-07 17:49:52 +11001289{
Al Viro25e195a2020-01-11 11:27:46 -05001290 struct dentry *dentry = path->dentry;
Al Viro463ffb22005-06-06 13:36:05 -07001291
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001292 /* We don't want to mount if someone's just doing a stat -
1293 * unless they're stat'ing a directory and appended a '/' to
1294 * the name.
1295 *
1296 * We do, however, want to mount if someone wants to open or
1297 * create a file of any type under the mountpoint, wants to
1298 * traverse through the mountpoint or wants to open the
1299 * mounted directory. Also, autofs may mark negative dentries
1300 * as being automount points. These will need the attentions
1301 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001302 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001303 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001304 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Al Viro25e195a2020-01-11 11:27:46 -05001305 dentry->d_inode)
Ian Kent5d38f042017-11-29 16:11:26 -08001306 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001307
Al Viro1c9f5e02020-01-16 22:05:18 -05001308 if (count && (*count)++ >= MAXSYMLINKS)
David Howells9875cf82011-01-14 18:45:21 +00001309 return -ELOOP;
1310
Al Viro25e195a2020-01-11 11:27:46 -05001311 return finish_automount(dentry->d_op->d_automount(path), path);
David Howells9875cf82011-01-14 18:45:21 +00001312}
1313
1314/*
Al Viro9deed3e2020-01-17 08:45:08 -05001315 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1316 * dentries are pinned but not locked here, so negative dentry can go
1317 * positive right under us. Use of smp_load_acquire() provides a barrier
1318 * sufficient for ->d_inode and ->d_flags consistency.
David Howells9875cf82011-01-14 18:45:21 +00001319 */
Al Viro9deed3e2020-01-17 08:45:08 -05001320static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1321 int *count, unsigned lookup_flags)
David Howells9875cf82011-01-14 18:45:21 +00001322{
Al Viro9deed3e2020-01-17 08:45:08 -05001323 struct vfsmount *mnt = path->mnt;
David Howells9875cf82011-01-14 18:45:21 +00001324 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001325 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001326
Al Viro9deed3e2020-01-17 08:45:08 -05001327 while (flags & DCACHE_MANAGED_DENTRY) {
David Howellscc53ce52011-01-14 18:45:26 +00001328 /* Allow the filesystem to manage the transit without i_mutex
1329 * being held. */
Al Virod41efb52019-11-04 22:30:52 -05001330 if (flags & DCACHE_MANAGE_TRANSIT) {
Ian Kentfb5f51c2016-11-24 08:03:41 +11001331 ret = path->dentry->d_op->d_manage(path, false);
Al Viro508c8772020-01-14 22:09:57 -05001332 flags = smp_load_acquire(&path->dentry->d_flags);
David Howellscc53ce52011-01-14 18:45:26 +00001333 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001334 break;
David Howellscc53ce52011-01-14 18:45:26 +00001335 }
1336
Al Viro9deed3e2020-01-17 08:45:08 -05001337 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
David Howells9875cf82011-01-14 18:45:21 +00001338 struct vfsmount *mounted = lookup_mnt(path);
Al Viro9deed3e2020-01-17 08:45:08 -05001339 if (mounted) { // ... in our namespace
David Howells9875cf82011-01-14 18:45:21 +00001340 dput(path->dentry);
1341 if (need_mntput)
1342 mntput(path->mnt);
1343 path->mnt = mounted;
1344 path->dentry = dget(mounted->mnt_root);
Al Viro9deed3e2020-01-17 08:45:08 -05001345 // here we know it's positive
1346 flags = path->dentry->d_flags;
David Howells9875cf82011-01-14 18:45:21 +00001347 need_mntput = true;
1348 continue;
1349 }
David Howells9875cf82011-01-14 18:45:21 +00001350 }
1351
Al Viro9deed3e2020-01-17 08:45:08 -05001352 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1353 break;
David Howells9875cf82011-01-14 18:45:21 +00001354
Al Viro9deed3e2020-01-17 08:45:08 -05001355 // uncovered automount point
1356 ret = follow_automount(path, count, lookup_flags);
1357 flags = smp_load_acquire(&path->dentry->d_flags);
1358 if (ret < 0)
1359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Al Viro8aef1882011-06-16 15:10:06 +01001361
Al Viro9deed3e2020-01-17 08:45:08 -05001362 if (ret == -EISDIR)
1363 ret = 0;
1364 // possible if you race with several mount --move
1365 if (need_mntput && path->mnt == mnt)
1366 mntput(path->mnt);
1367 if (!ret && unlikely(d_flags_negative(flags)))
Al Virod41efb52019-11-04 22:30:52 -05001368 ret = -ENOENT;
Al Viro9deed3e2020-01-17 08:45:08 -05001369 *jumped = need_mntput;
Al Viro84027522015-04-22 10:30:08 -04001370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Al Viro9deed3e2020-01-17 08:45:08 -05001373static inline int traverse_mounts(struct path *path, bool *jumped,
1374 int *count, unsigned lookup_flags)
1375{
1376 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1377
1378 /* fastpath */
1379 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1380 *jumped = false;
1381 if (unlikely(d_flags_negative(flags)))
1382 return -ENOENT;
1383 return 0;
1384 }
1385 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1386}
1387
David Howellscc53ce52011-01-14 18:45:26 +00001388int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 struct vfsmount *mounted;
1391
Al Viro1c755af2009-04-18 14:06:57 -04001392 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001394 dput(path->dentry);
1395 mntput(path->mnt);
1396 path->mnt = mounted;
1397 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 1;
1399 }
1400 return 0;
1401}
Al Viro4d359502014-03-14 12:20:17 -04001402EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
David Howells9875cf82011-01-14 18:45:21 +00001404/*
Al Viro9deed3e2020-01-17 08:45:08 -05001405 * Follow down to the covering mount currently visible to userspace. At each
1406 * point, the filesystem owning that dentry may be queried as to whether the
1407 * caller is permitted to proceed or not.
1408 */
1409int follow_down(struct path *path)
1410{
1411 struct vfsmount *mnt = path->mnt;
1412 bool jumped;
1413 int ret = traverse_mounts(path, &jumped, NULL, 0);
1414
1415 if (path->mnt != mnt)
1416 mntput(mnt);
1417 return ret;
1418}
1419EXPORT_SYMBOL(follow_down);
1420
1421/*
Al Viro287548e2011-05-27 06:50:06 -04001422 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1423 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001424 */
1425static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001426 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001427{
Al Viroea936ae2020-01-16 09:52:04 -05001428 struct dentry *dentry = path->dentry;
1429 unsigned int flags = dentry->d_flags;
1430
1431 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1432 return true;
1433
1434 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1435 return false;
1436
Ian Kent62a73752011-03-25 01:51:02 +08001437 for (;;) {
Ian Kent62a73752011-03-25 01:51:02 +08001438 /*
1439 * Don't forget we might have a non-mountpoint managed dentry
1440 * that wants to block transit.
1441 */
Al Viroea936ae2020-01-16 09:52:04 -05001442 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1443 int res = dentry->d_op->d_manage(path, true);
1444 if (res)
1445 return res == -EISDIR;
1446 flags = dentry->d_flags;
NeilBrownb8faf032014-08-04 17:06:29 +10001447 }
Ian Kent62a73752011-03-25 01:51:02 +08001448
Al Viroea936ae2020-01-16 09:52:04 -05001449 if (flags & DCACHE_MOUNTED) {
1450 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1451 if (mounted) {
1452 path->mnt = &mounted->mnt;
1453 dentry = path->dentry = mounted->mnt.mnt_root;
Al Virobcba1e72021-04-01 22:03:41 -04001454 nd->state |= ND_JUMPED;
Al Viroea936ae2020-01-16 09:52:04 -05001455 *seqp = read_seqcount_begin(&dentry->d_seq);
1456 *inode = dentry->d_inode;
1457 /*
1458 * We don't need to re-check ->d_seq after this
1459 * ->d_inode read - there will be an RCU delay
1460 * between mount hash removal and ->mnt_root
1461 * becoming unpinned.
1462 */
1463 flags = dentry->d_flags;
1464 continue;
1465 }
1466 if (read_seqretry(&mount_lock, nd->m_seq))
1467 return false;
1468 }
1469 return !(flags & DCACHE_NEED_AUTOMOUNT);
David Howells9875cf82011-01-14 18:45:21 +00001470 }
Al Viro287548e2011-05-27 06:50:06 -04001471}
1472
Al Virodb3c9ad2020-01-09 14:41:00 -05001473static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1474 struct path *path, struct inode **inode,
1475 unsigned int *seqp)
Al Virobd7c4b52020-01-08 20:37:23 -05001476{
Al Viro9deed3e2020-01-17 08:45:08 -05001477 bool jumped;
Al Virodb3c9ad2020-01-09 14:41:00 -05001478 int ret;
Al Virobd7c4b52020-01-08 20:37:23 -05001479
Al Virodb3c9ad2020-01-09 14:41:00 -05001480 path->mnt = nd->path.mnt;
1481 path->dentry = dentry;
Al Viroc1530072020-01-09 14:50:18 -05001482 if (nd->flags & LOOKUP_RCU) {
1483 unsigned int seq = *seqp;
1484 if (unlikely(!*inode))
1485 return -ENOENT;
1486 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viro9deed3e2020-01-17 08:45:08 -05001487 return 0;
Al Viroae66db42021-01-04 00:08:41 -05001488 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viroc1530072020-01-09 14:50:18 -05001489 return -ECHILD;
1490 // *path might've been clobbered by __follow_mount_rcu()
1491 path->mnt = nd->path.mnt;
1492 path->dentry = dentry;
1493 }
Al Viro9deed3e2020-01-17 08:45:08 -05001494 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1495 if (jumped) {
1496 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1497 ret = -EXDEV;
1498 else
Al Virobcba1e72021-04-01 22:03:41 -04001499 nd->state |= ND_JUMPED;
Al Viro9deed3e2020-01-17 08:45:08 -05001500 }
1501 if (unlikely(ret)) {
1502 dput(path->dentry);
1503 if (path->mnt != nd->path.mnt)
1504 mntput(path->mnt);
1505 } else {
Al Virobd7c4b52020-01-08 20:37:23 -05001506 *inode = d_backing_inode(path->dentry);
1507 *seqp = 0; /* out of RCU mode, so the value doesn't matter */
1508 }
1509 return ret;
1510}
1511
David Howells9875cf82011-01-14 18:45:21 +00001512/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001513 * This looks up the name in dcache and possibly revalidates the found dentry.
1514 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001515 */
Al Viroe3c13922016-03-06 14:03:27 -05001516static struct dentry *lookup_dcache(const struct qstr *name,
1517 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001518 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001519{
Al Viroa89f8332017-01-09 22:25:28 -05001520 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001521 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001522 int error = d_revalidate(dentry, flags);
1523 if (unlikely(error <= 0)) {
1524 if (!error)
1525 d_invalidate(dentry);
1526 dput(dentry);
1527 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001528 }
1529 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001530 return dentry;
1531}
1532
1533/*
Al Viroa03ece52018-03-08 11:00:45 -05001534 * Parent directory has inode locked exclusive. This is one
1535 * and only case when ->lookup() gets called on non in-lookup
1536 * dentries - as the matter of fact, this only gets called
1537 * when directory is guaranteed to have no in-lookup children
1538 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001539 */
Al Viroa03ece52018-03-08 11:00:45 -05001540static struct dentry *__lookup_hash(const struct qstr *name,
1541 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001542{
Al Viroa03ece52018-03-08 11:00:45 -05001543 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001544 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001545 struct inode *dir = base->d_inode;
1546
1547 if (dentry)
1548 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001549
1550 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001551 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001552 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001553
1554 dentry = d_alloc(base, name);
1555 if (unlikely(!dentry))
1556 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001557
Al Viro72bd8662012-06-10 17:17:17 -04001558 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001559 if (unlikely(old)) {
1560 dput(dentry);
1561 dentry = old;
1562 }
1563 return dentry;
1564}
1565
Al Viro20e34352020-01-09 14:58:31 -05001566static struct dentry *lookup_fast(struct nameidata *nd,
1567 struct inode **inode,
1568 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Nick Piggin31e6b012011-01-07 17:49:52 +11001570 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001571 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001572
Al Viro3cac2602009-08-13 18:27:43 +04001573 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001574 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001575 * of a false negative due to a concurrent rename, the caller is
1576 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001577 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001578 if (nd->flags & LOOKUP_RCU) {
1579 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001580 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001581 if (unlikely(!dentry)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001582 if (!try_to_unlazy(nd))
Al Viro20e34352020-01-09 14:58:31 -05001583 return ERR_PTR(-ECHILD);
1584 return NULL;
Al Viro5d0f49c2016-03-05 21:32:53 -05001585 }
Al Viro5a18fff2011-03-11 04:44:53 -05001586
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001587 /*
1588 * This sequence count validates that the inode matches
1589 * the dentry name information from lookup.
1590 */
David Howells63afdfc2015-05-06 15:59:00 +01001591 *inode = d_backing_inode(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001592 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Al Viro20e34352020-01-09 14:58:31 -05001593 return ERR_PTR(-ECHILD);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001594
1595 /*
1596 * This sequence count validates that the parent had no
1597 * changes while we did the lookup of the dentry above.
1598 *
1599 * The memory barrier in read_seqcount_begin of child is
1600 * enough, we can use __read_seqcount_retry here.
1601 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001602 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Al Viro20e34352020-01-09 14:58:31 -05001603 return ERR_PTR(-ECHILD);
Al Viro5a18fff2011-03-11 04:44:53 -05001604
Al Viro254cf582015-05-05 09:40:46 -04001605 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001606 status = d_revalidate(dentry, nd->flags);
Al Viroc1530072020-01-09 14:50:18 -05001607 if (likely(status > 0))
Al Viro20e34352020-01-09 14:58:31 -05001608 return dentry;
Al Viroae66db42021-01-04 00:08:41 -05001609 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viro20e34352020-01-09 14:58:31 -05001610 return ERR_PTR(-ECHILD);
Steven Rostedt (VMware)26ddb452020-12-09 17:09:28 -05001611 if (status == -ECHILD)
Al Viro209a7fb2017-01-09 01:35:39 -05001612 /* we'd been told to redo it in non-rcu mode */
1613 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001614 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001615 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001616 if (unlikely(!dentry))
Al Viro20e34352020-01-09 14:58:31 -05001617 return NULL;
Al Viroa89f8332017-01-09 22:25:28 -05001618 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001619 }
Al Viro5a18fff2011-03-11 04:44:53 -05001620 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001621 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001622 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001623 dput(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001624 return ERR_PTR(status);
Al Viro5a18fff2011-03-11 04:44:53 -05001625 }
Al Viro20e34352020-01-09 14:58:31 -05001626 return dentry;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001627}
1628
1629/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001630static struct dentry *__lookup_slow(const struct qstr *name,
1631 struct dentry *dir,
1632 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001633{
Al Viro88d83312018-04-06 16:43:47 -04001634 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001635 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001636 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001637
Al Viro19363862016-04-14 19:33:34 -04001638 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001639 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001640 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001641again:
Al Virod9171b92016-04-15 03:33:13 -04001642 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001643 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001644 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001645 if (unlikely(!d_in_lookup(dentry))) {
Al Viroc64cd6e2020-01-10 17:17:19 -05001646 int error = d_revalidate(dentry, flags);
1647 if (unlikely(error <= 0)) {
1648 if (!error) {
1649 d_invalidate(dentry);
Al Viro949a8522016-03-06 14:20:52 -05001650 dput(dentry);
Al Viroc64cd6e2020-01-10 17:17:19 -05001651 goto again;
Al Viro949a8522016-03-06 14:20:52 -05001652 }
Al Viroc64cd6e2020-01-10 17:17:19 -05001653 dput(dentry);
1654 dentry = ERR_PTR(error);
Al Viro949a8522016-03-06 14:20:52 -05001655 }
Al Viro94bdd652016-04-15 02:42:04 -04001656 } else {
1657 old = inode->i_op->lookup(inode, dentry, flags);
1658 d_lookup_done(dentry);
1659 if (unlikely(old)) {
1660 dput(dentry);
1661 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001662 }
1663 }
Al Viroe3c13922016-03-06 14:03:27 -05001664 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Al Viro88d83312018-04-06 16:43:47 -04001667static struct dentry *lookup_slow(const struct qstr *name,
1668 struct dentry *dir,
1669 unsigned int flags)
1670{
1671 struct inode *inode = dir->d_inode;
1672 struct dentry *res;
1673 inode_lock_shared(inode);
1674 res = __lookup_slow(name, dir, flags);
1675 inode_unlock_shared(inode);
1676 return res;
1677}
1678
Christian Braunerba73d982021-01-21 14:19:31 +01001679static inline int may_lookup(struct user_namespace *mnt_userns,
1680 struct nameidata *nd)
Al Viro52094c82011-02-21 21:34:47 -05001681{
1682 if (nd->flags & LOOKUP_RCU) {
Linus Torvalds7d6beb72021-02-23 13:39:45 -08001683 int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Jens Axboee36cffe2020-12-17 09:19:08 -07001684 if (err != -ECHILD || !try_to_unlazy(nd))
Al Viro52094c82011-02-21 21:34:47 -05001685 return err;
Al Viro52094c82011-02-21 21:34:47 -05001686 }
Christian Braunerba73d982021-01-21 14:19:31 +01001687 return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001688}
1689
Al Viro49055902020-03-03 11:22:34 -05001690static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1691{
Al Viro49055902020-03-03 11:22:34 -05001692 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1693 return -ELOOP;
Al Viro45425762020-03-03 11:25:31 -05001694
1695 if (likely(nd->depth != EMBEDDED_LEVELS))
1696 return 0;
1697 if (likely(nd->stack != nd->internal))
1698 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001699 if (likely(nd_alloc_stack(nd)))
Al Viro49055902020-03-03 11:22:34 -05001700 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001701
1702 if (nd->flags & LOOKUP_RCU) {
1703 // we need to grab link before we do unlazy. And we can't skip
1704 // unlazy even if we fail to grab the link - cleanup needs it
Al Viro49055902020-03-03 11:22:34 -05001705 bool grabbed_link = legitimize_path(nd, link, seq);
Al Viro60ef60c2020-03-03 11:43:55 -05001706
Jens Axboee36cffe2020-12-17 09:19:08 -07001707 if (!try_to_unlazy(nd) != 0 || !grabbed_link)
Al Viro60ef60c2020-03-03 11:43:55 -05001708 return -ECHILD;
1709
1710 if (nd_alloc_stack(nd))
1711 return 0;
Al Viro49055902020-03-03 11:22:34 -05001712 }
Al Viro60ef60c2020-03-03 11:43:55 -05001713 return -ENOMEM;
Al Viro49055902020-03-03 11:22:34 -05001714}
1715
Al Virob1a81972020-01-19 12:48:44 -05001716enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1717
Al Viro06708ad2020-01-14 14:26:57 -05001718static const char *pick_link(struct nameidata *nd, struct path *link,
Al Virob1a81972020-01-19 12:48:44 -05001719 struct inode *inode, unsigned seq, int flags)
Al Virod63ff282015-05-04 18:13:23 -04001720{
Al Viro1cf26652015-05-06 16:01:56 -04001721 struct saved *last;
Al Viroad6cc4c2020-01-14 14:41:39 -05001722 const char *res;
Al Viro49055902020-03-03 11:22:34 -05001723 int error = reserve_stack(nd, link, seq);
Al Viroad6cc4c2020-01-14 14:41:39 -05001724
Al Viro49055902020-03-03 11:22:34 -05001725 if (unlikely(error)) {
Al Viro84f0cd92020-03-03 10:14:30 -05001726 if (!(nd->flags & LOOKUP_RCU))
1727 path_put(link);
Al Viro49055902020-03-03 11:22:34 -05001728 return ERR_PTR(error);
Al Viro626de992015-05-04 18:26:59 -04001729 }
Al Viroab104922015-05-10 11:50:01 -04001730 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001731 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001732 clear_delayed_call(&last->done);
Al Viro0450b2d2015-05-08 13:23:53 -04001733 last->seq = seq;
Al Viroad6cc4c2020-01-14 14:41:39 -05001734
Al Virob1a81972020-01-19 12:48:44 -05001735 if (flags & WALK_TRAILING) {
Al Viroad6cc4c2020-01-14 14:41:39 -05001736 error = may_follow_link(nd, inode);
1737 if (unlikely(error))
1738 return ERR_PTR(error);
1739 }
1740
Mattias Nisslerdab741e02020-08-27 11:09:46 -06001741 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1742 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
Al Viroad6cc4c2020-01-14 14:41:39 -05001743 return ERR_PTR(-ELOOP);
1744
1745 if (!(nd->flags & LOOKUP_RCU)) {
1746 touch_atime(&last->link);
1747 cond_resched();
1748 } else if (atime_needs_update(&last->link, inode)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001749 if (!try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001750 return ERR_PTR(-ECHILD);
1751 touch_atime(&last->link);
1752 }
1753
1754 error = security_inode_follow_link(link->dentry, inode,
1755 nd->flags & LOOKUP_RCU);
1756 if (unlikely(error))
1757 return ERR_PTR(error);
1758
Al Viroad6cc4c2020-01-14 14:41:39 -05001759 res = READ_ONCE(inode->i_link);
1760 if (!res) {
1761 const char * (*get)(struct dentry *, struct inode *,
1762 struct delayed_call *);
1763 get = inode->i_op->get_link;
1764 if (nd->flags & LOOKUP_RCU) {
1765 res = get(NULL, inode, &last->done);
Jens Axboee36cffe2020-12-17 09:19:08 -07001766 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001767 res = get(link->dentry, inode, &last->done);
Al Viroad6cc4c2020-01-14 14:41:39 -05001768 } else {
1769 res = get(link->dentry, inode, &last->done);
1770 }
1771 if (!res)
1772 goto all_done;
1773 if (IS_ERR(res))
1774 return res;
1775 }
1776 if (*res == '/') {
1777 error = nd_jump_root(nd);
1778 if (unlikely(error))
1779 return ERR_PTR(error);
1780 while (unlikely(*++res == '/'))
1781 ;
1782 }
1783 if (*res)
1784 return res;
1785all_done: // pure jump
1786 put_link(nd);
1787 return NULL;
Al Virod63ff282015-05-04 18:13:23 -04001788}
1789
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001790/*
1791 * Do we need to follow links? We _really_ want to be able
1792 * to do this check without having to look at inode->i_op,
1793 * so we keep a cache of "no, this doesn't need follow_link"
1794 * for the common case.
1795 */
Al Virob0417d22020-01-14 13:34:20 -05001796static const char *step_into(struct nameidata *nd, int flags,
Al Virocbae4d12020-01-12 13:40:02 -05001797 struct dentry *dentry, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001798{
Al Virocbae4d12020-01-12 13:40:02 -05001799 struct path path;
1800 int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1801
1802 if (err < 0)
Al Virob0417d22020-01-14 13:34:20 -05001803 return ERR_PTR(err);
Al Virocbae4d12020-01-12 13:40:02 -05001804 if (likely(!d_is_symlink(path.dentry)) ||
Al Viro8c4efe22020-01-19 12:44:18 -05001805 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
Al Viroaca29032020-01-09 15:17:57 -05001806 (flags & WALK_NOFOLLOW)) {
Al Viro8f64fb12016-11-14 01:50:26 -05001807 /* not a symlink or should not follow */
Al Viroc99687a2020-03-03 10:56:17 -05001808 if (!(nd->flags & LOOKUP_RCU)) {
1809 dput(nd->path.dentry);
1810 if (nd->path.mnt != path.mnt)
1811 mntput(nd->path.mnt);
1812 }
1813 nd->path = path;
Al Viro8f64fb12016-11-14 01:50:26 -05001814 nd->inode = inode;
1815 nd->seq = seq;
Al Virob0417d22020-01-14 13:34:20 -05001816 return NULL;
Al Viro8f64fb12016-11-14 01:50:26 -05001817 }
Al Viroa7f77542016-02-27 19:31:01 -05001818 if (nd->flags & LOOKUP_RCU) {
Al Viro84f0cd92020-03-03 10:14:30 -05001819 /* make sure that d_is_symlink above matches inode */
Al Virocbae4d12020-01-12 13:40:02 -05001820 if (read_seqcount_retry(&path.dentry->d_seq, seq))
Al Virob0417d22020-01-14 13:34:20 -05001821 return ERR_PTR(-ECHILD);
Al Viro84f0cd92020-03-03 10:14:30 -05001822 } else {
1823 if (path.mnt == nd->path.mnt)
1824 mntget(path.mnt);
Al Viroa7f77542016-02-27 19:31:01 -05001825 }
Al Virob1a81972020-01-19 12:48:44 -05001826 return pick_link(nd, &path, inode, seq, flags);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001827}
1828
Al Viroc2df1962020-02-26 14:33:30 -05001829static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1830 struct inode **inodep,
1831 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001832{
Al Viro12487f32020-02-26 14:59:56 -05001833 struct dentry *parent, *old;
Al Viro957dd412020-02-26 01:40:04 -05001834
Al Viro12487f32020-02-26 14:59:56 -05001835 if (path_equal(&nd->path, &nd->root))
1836 goto in_root;
1837 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro7ef482f2020-02-26 17:50:13 -05001838 struct path path;
Al Viroefe772d2020-02-28 10:06:37 -05001839 unsigned seq;
Al Viro7ef482f2020-02-26 17:50:13 -05001840 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1841 &nd->root, &path, &seq))
1842 goto in_root;
Al Viroefe772d2020-02-28 10:06:37 -05001843 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1844 return ERR_PTR(-ECHILD);
1845 nd->path = path;
1846 nd->inode = path.dentry->d_inode;
1847 nd->seq = seq;
1848 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1849 return ERR_PTR(-ECHILD);
1850 /* we know that mountpoint was pinned */
Al Viro957dd412020-02-26 01:40:04 -05001851 }
Al Viro12487f32020-02-26 14:59:56 -05001852 old = nd->path.dentry;
1853 parent = old->d_parent;
1854 *inodep = parent->d_inode;
1855 *seqp = read_seqcount_begin(&parent->d_seq);
1856 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1857 return ERR_PTR(-ECHILD);
1858 if (unlikely(!path_connected(nd->path.mnt, parent)))
1859 return ERR_PTR(-ECHILD);
1860 return parent;
1861in_root:
Al Viroefe772d2020-02-28 10:06:37 -05001862 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1863 return ERR_PTR(-ECHILD);
Al Viroc2df1962020-02-26 14:33:30 -05001864 if (unlikely(nd->flags & LOOKUP_BENEATH))
1865 return ERR_PTR(-ECHILD);
1866 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001867}
1868
Al Viroc2df1962020-02-26 14:33:30 -05001869static struct dentry *follow_dotdot(struct nameidata *nd,
1870 struct inode **inodep,
1871 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001872{
Al Viro12487f32020-02-26 14:59:56 -05001873 struct dentry *parent;
1874
1875 if (path_equal(&nd->path, &nd->root))
1876 goto in_root;
1877 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro2aa38472020-02-26 19:19:05 -05001878 struct path path;
1879
1880 if (!choose_mountpoint(real_mount(nd->path.mnt),
1881 &nd->root, &path))
1882 goto in_root;
Al Viro165200d2020-02-28 10:17:52 -05001883 path_put(&nd->path);
1884 nd->path = path;
Al Viro2aa38472020-02-26 19:19:05 -05001885 nd->inode = path.dentry->d_inode;
Al Viro165200d2020-02-28 10:17:52 -05001886 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1887 return ERR_PTR(-EXDEV);
Al Viro957dd412020-02-26 01:40:04 -05001888 }
Al Viro12487f32020-02-26 14:59:56 -05001889 /* rare case of legitimate dget_parent()... */
1890 parent = dget_parent(nd->path.dentry);
1891 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1892 dput(parent);
1893 return ERR_PTR(-ENOENT);
1894 }
1895 *seqp = 0;
1896 *inodep = parent->d_inode;
1897 return parent;
1898
1899in_root:
Al Viroc2df1962020-02-26 14:33:30 -05001900 if (unlikely(nd->flags & LOOKUP_BENEATH))
1901 return ERR_PTR(-EXDEV);
1902 dget(nd->path.dentry);
1903 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001904}
1905
Al Viro7521f222020-02-26 12:22:58 -05001906static const char *handle_dots(struct nameidata *nd, int type)
Al Viro957dd412020-02-26 01:40:04 -05001907{
1908 if (type == LAST_DOTDOT) {
Al Viro7521f222020-02-26 12:22:58 -05001909 const char *error = NULL;
Al Viroc2df1962020-02-26 14:33:30 -05001910 struct dentry *parent;
1911 struct inode *inode;
1912 unsigned seq;
Al Viro957dd412020-02-26 01:40:04 -05001913
1914 if (!nd->root.mnt) {
Al Viro7521f222020-02-26 12:22:58 -05001915 error = ERR_PTR(set_root(nd));
Al Viro957dd412020-02-26 01:40:04 -05001916 if (error)
1917 return error;
1918 }
1919 if (nd->flags & LOOKUP_RCU)
Al Viroc2df1962020-02-26 14:33:30 -05001920 parent = follow_dotdot_rcu(nd, &inode, &seq);
Al Viro957dd412020-02-26 01:40:04 -05001921 else
Al Viroc2df1962020-02-26 14:33:30 -05001922 parent = follow_dotdot(nd, &inode, &seq);
1923 if (IS_ERR(parent))
1924 return ERR_CAST(parent);
1925 if (unlikely(!parent))
1926 error = step_into(nd, WALK_NOFOLLOW,
1927 nd->path.dentry, nd->inode, nd->seq);
1928 else
1929 error = step_into(nd, WALK_NOFOLLOW,
1930 parent, inode, seq);
1931 if (unlikely(error))
Al Viro957dd412020-02-26 01:40:04 -05001932 return error;
1933
1934 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1935 /*
1936 * If there was a racing rename or mount along our
1937 * path, then we can't be sure that ".." hasn't jumped
1938 * above nd->root (and so userspace should retry or use
1939 * some fallback).
1940 */
1941 smp_rmb();
1942 if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001943 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001944 if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001945 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001946 }
1947 }
Al Viro7521f222020-02-26 12:22:58 -05001948 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001949}
1950
Al Viro92d27012020-01-14 13:24:17 -05001951static const char *walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001952{
Al Virodb3c9ad2020-01-09 14:41:00 -05001953 struct dentry *dentry;
Al Viroce57dfc2011-03-13 19:58:58 -04001954 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001955 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001956 /*
1957 * "." and ".." are special - ".." especially so because it has
1958 * to be able to know about the current root directory and
1959 * parent relationships.
1960 */
Al Viro4693a542015-05-04 17:47:11 -04001961 if (unlikely(nd->last_type != LAST_NORM)) {
Al Viro1c4ff1a2016-11-14 01:39:36 -05001962 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001963 put_link(nd);
Al Viro7521f222020-02-26 12:22:58 -05001964 return handle_dots(nd, nd->last_type);
Al Viro4693a542015-05-04 17:47:11 -04001965 }
Al Viro20e34352020-01-09 14:58:31 -05001966 dentry = lookup_fast(nd, &inode, &seq);
1967 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001968 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001969 if (unlikely(!dentry)) {
Al Virodb3c9ad2020-01-09 14:41:00 -05001970 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1971 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001972 return ERR_CAST(dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001973 }
Al Viro56676ec2020-03-10 21:54:54 -04001974 if (!(flags & WALK_MORE) && nd->depth)
1975 put_link(nd);
Al Virob0417d22020-01-14 13:34:20 -05001976 return step_into(nd, flags, dentry, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001977}
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001980 * We can do the critical dentry name comparison and hashing
1981 * operations one word at a time, but we are limited to:
1982 *
1983 * - Architectures with fast unaligned word accesses. We could
1984 * do a "get_unaligned()" if this helps and is sufficiently
1985 * fast.
1986 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001987 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1988 * do not trap on the (extremely unlikely) case of a page
1989 * crossing operation.
1990 *
1991 * - Furthermore, we need an efficient 64-bit compile for the
1992 * 64-bit case in order to generate the "number of bytes in
1993 * the final mask". Again, that could be replaced with a
1994 * efficient population count instruction or similar.
1995 */
1996#ifdef CONFIG_DCACHE_WORD_ACCESS
1997
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001998#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001999
George Spelvin468a9422016-05-26 22:11:51 -04002000#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002001
George Spelvin468a9422016-05-26 22:11:51 -04002002/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
2003
2004#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04002005/*
2006 * Register pressure in the mixing function is an issue, particularly
2007 * on 32-bit x86, but almost any function requires one state value and
2008 * one temporary. Instead, use a function designed for two state values
2009 * and no temporaries.
2010 *
2011 * This function cannot create a collision in only two iterations, so
2012 * we have two iterations to achieve avalanche. In those two iterations,
2013 * we have six layers of mixing, which is enough to spread one bit's
2014 * influence out to 2^6 = 64 state bits.
2015 *
2016 * Rotate constants are scored by considering either 64 one-bit input
2017 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2018 * probability of that delta causing a change to each of the 128 output
2019 * bits, using a sample of random initial states.
2020 *
2021 * The Shannon entropy of the computed probabilities is then summed
2022 * to produce a score. Ideally, any input change has a 50% chance of
2023 * toggling any given output bit.
2024 *
2025 * Mixing scores (in bits) for (12,45):
2026 * Input delta: 1-bit 2-bit
2027 * 1 round: 713.3 42542.6
2028 * 2 rounds: 2753.7 140389.8
2029 * 3 rounds: 5954.1 233458.2
2030 * 4 rounds: 7862.6 256672.2
2031 * Perfect: 8192 258048
2032 * (64*128) (64*63/2 * 128)
2033 */
2034#define HASH_MIX(x, y, a) \
2035 ( x ^= (a), \
2036 y ^= x, x = rol64(x,12),\
2037 x += y, y = rol64(y,45),\
2038 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002039
George Spelvin0fed3ac2016-05-02 06:31:01 -04002040/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04002041 * Fold two longs into one 32-bit hash value. This must be fast, but
2042 * latency isn't quite as critical, as there is a fair bit of additional
2043 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04002044 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04002045static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002046{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002047 y ^= x * GOLDEN_RATIO_64;
2048 y *= GOLDEN_RATIO_64;
2049 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04002050}
2051
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002052#else /* 32-bit case */
2053
George Spelvin2a18da7a2016-05-23 07:43:58 -04002054/*
2055 * Mixing scores (in bits) for (7,20):
2056 * Input delta: 1-bit 2-bit
2057 * 1 round: 330.3 9201.6
2058 * 2 rounds: 1246.4 25475.4
2059 * 3 rounds: 1907.1 31295.1
2060 * 4 rounds: 2042.3 31718.6
2061 * Perfect: 2048 31744
2062 * (32*64) (32*31/2 * 64)
2063 */
2064#define HASH_MIX(x, y, a) \
2065 ( x ^= (a), \
2066 y ^= x, x = rol32(x, 7),\
2067 x += y, y = rol32(y,20),\
2068 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002069
George Spelvin2a18da7a2016-05-23 07:43:58 -04002070static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002071{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002072 /* Use arch-optimized multiply if one exists */
2073 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04002074}
2075
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002076#endif
2077
George Spelvin2a18da7a2016-05-23 07:43:58 -04002078/*
2079 * Return the hash of a string of known length. This is carfully
2080 * designed to match hash_name(), which is the more critical function.
2081 * In particular, we must end by hashing a final word containing 0..7
2082 * payload bytes, to match the way that hash_name() iterates until it
2083 * finds the delimiter after the name.
2084 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002085unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002086{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002087 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002088
2089 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002090 if (!len)
2091 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002092 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002093 if (len < sizeof(unsigned long))
2094 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04002095 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002096 name += sizeof(unsigned long);
2097 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002098 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04002099 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002100done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04002101 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002102}
2103EXPORT_SYMBOL(full_name_hash);
2104
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002105/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002106u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002107{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002108 unsigned long a = 0, x = 0, y = (unsigned long)salt;
2109 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002110 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2111
Linus Torvalds8387ff22016-06-10 07:51:30 -07002112 len = 0;
2113 goto inside;
2114
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002115 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002116 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002117 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002118inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002119 a = load_unaligned_zeropad(name+len);
2120 } while (!has_zero(a, &adata, &constants));
2121
2122 adata = prep_zero_mask(a, adata, &constants);
2123 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002124 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002125
George Spelvin2a18da7a2016-05-23 07:43:58 -04002126 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002127}
2128EXPORT_SYMBOL(hashlen_string);
2129
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002130/*
2131 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002132 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002133 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002134static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002135{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002136 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2137 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07002138 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002139
Linus Torvalds8387ff22016-06-10 07:51:30 -07002140 len = 0;
2141 goto inside;
2142
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002143 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002144 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002145 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002146inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002147 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002148 b = a ^ REPEAT_BYTE('/');
2149 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002150
Linus Torvalds36126f82012-05-26 10:43:17 -07002151 adata = prep_zero_mask(a, adata, &constants);
2152 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002153 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002154 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002155
George Spelvin2a18da7a2016-05-23 07:43:58 -04002156 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002157}
2158
George Spelvin2a18da7a2016-05-23 07:43:58 -04002159#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002160
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002161/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002162unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002163{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002164 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002165 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002166 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002167 return end_name_hash(hash);
2168}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002169EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002170
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002171/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002172u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002173{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002174 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002175 unsigned long len = 0, c;
2176
2177 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002178 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002179 len++;
2180 hash = partial_name_hash(c, hash);
2181 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002182 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002183 return hashlen_create(end_name_hash(hash), len);
2184}
George Spelvinf2a031b2016-05-29 01:26:41 -04002185EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002186
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002187/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002188 * We know there's a real path component here of at least
2189 * one character.
2190 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002191static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002192{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002193 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002194 unsigned long len = 0, c;
2195
2196 c = (unsigned char)*name;
2197 do {
2198 len++;
2199 hash = partial_name_hash(c, hash);
2200 c = (unsigned char)name[len];
2201 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002202 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002203}
2204
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002205#endif
2206
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002209 * This is the basic name resolution function, turning a pathname into
2210 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002212 * Returns 0 and nd will have valid dentry and mnt on success.
2213 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 */
Al Viro6de88d72009-08-09 01:41:57 +04002215static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Al Virod8d46112020-02-23 22:04:15 -05002217 int depth = 0; // depth <= nd->depth
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002219
Al Virob4c03532020-01-19 11:44:51 -05002220 nd->last_type = LAST_ROOT;
Al Viroc1088372020-03-05 15:48:44 -05002221 nd->flags |= LOOKUP_PARENT;
Al Viro9b5858e2018-07-09 16:33:23 -04002222 if (IS_ERR(name))
2223 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 while (*name=='/')
2225 name++;
Al Viro1a97d892020-09-19 17:55:58 +01002226 if (!*name) {
2227 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
Al Viro9e18f102015-04-18 20:44:34 -04002228 return 0;
Al Viro1a97d892020-09-19 17:55:58 +01002229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* At this point we know we have a real path component. */
2232 for(;;) {
Christian Brauner549c7292021-01-21 14:19:43 +01002233 struct user_namespace *mnt_userns;
Al Viro92d27012020-01-14 13:24:17 -05002234 const char *link;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002235 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002236 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Christian Brauner549c7292021-01-21 14:19:43 +01002238 mnt_userns = mnt_user_ns(nd->path.mnt);
2239 err = may_lookup(mnt_userns, nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002240 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds8387ff22016-06-10 07:51:30 -07002243 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Al Virofe479a52011-02-22 15:10:03 -05002245 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002246 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002247 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002248 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002249 type = LAST_DOTDOT;
Al Virobcba1e72021-04-01 22:03:41 -04002250 nd->state |= ND_JUMPED;
Al Viro16c2cd72011-02-22 15:50:10 -05002251 }
Al Virofe479a52011-02-22 15:10:03 -05002252 break;
2253 case 1:
2254 type = LAST_DOT;
2255 }
Al Viro5a202bc2011-03-08 14:17:44 -05002256 if (likely(type == LAST_NORM)) {
2257 struct dentry *parent = nd->path.dentry;
Al Virobcba1e72021-04-01 22:03:41 -04002258 nd->state &= ~ND_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002259 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002260 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002261 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002262 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002263 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002264 hash_len = this.hash_len;
2265 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002266 }
2267 }
Al Virofe479a52011-02-22 15:10:03 -05002268
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002269 nd->last.hash_len = hash_len;
2270 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002271 nd->last_type = type;
2272
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002273 name += hashlen_len(hash_len);
2274 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002275 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002276 /*
2277 * If it wasn't NUL, we know it was '/'. Skip that
2278 * slash, and continue until no more slashes.
2279 */
2280 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002281 name++;
2282 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002283 if (unlikely(!*name)) {
2284OK:
Al Virod8d46112020-02-23 22:04:15 -05002285 /* pathname or trailing symlink, done */
Al Viroc1088372020-03-05 15:48:44 -05002286 if (!depth) {
Christian Brauner549c7292021-01-21 14:19:43 +01002287 nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode);
Al Viro0f705952020-03-05 11:34:48 -05002288 nd->dir_mode = nd->inode->i_mode;
Al Viroc1088372020-03-05 15:48:44 -05002289 nd->flags &= ~LOOKUP_PARENT;
Al Viro8620c232015-05-04 08:58:35 -04002290 return 0;
Al Viroc1088372020-03-05 15:48:44 -05002291 }
Al Viro8620c232015-05-04 08:58:35 -04002292 /* last component of nested symlink */
Al Virod8d46112020-02-23 22:04:15 -05002293 name = nd->stack[--depth].name;
Al Viro8c4efe22020-01-19 12:44:18 -05002294 link = walk_component(nd, 0);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002295 } else {
2296 /* not the last component */
Al Viro8c4efe22020-01-19 12:44:18 -05002297 link = walk_component(nd, WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002298 }
Al Viro92d27012020-01-14 13:24:17 -05002299 if (unlikely(link)) {
2300 if (IS_ERR(link))
2301 return PTR_ERR(link);
2302 /* a symlink to follow */
Al Virod8d46112020-02-23 22:04:15 -05002303 nd->stack[depth++].name = name;
Al Viro92d27012020-01-14 13:24:17 -05002304 name = link;
2305 continue;
Nick Piggin31e6b012011-01-07 17:49:52 +11002306 }
Al Viro97242f92015-08-01 19:59:28 -04002307 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2308 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07002309 if (!try_to_unlazy(nd))
Al Viro97242f92015-08-01 19:59:28 -04002310 return -ECHILD;
2311 }
Al Viro3595e232015-05-09 16:54:45 -04002312 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Al Viroedc2b1d2018-07-09 16:27:23 -04002317/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002318static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Aleksa Sarai740a1672019-12-07 01:13:29 +11002320 int error;
Al Viroc8a53ee2015-05-12 18:43:07 -04002321 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Jens Axboe6c6ec2b2020-12-17 09:19:09 -07002323 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2324 if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
2325 return ERR_PTR(-EAGAIN);
2326
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002327 if (!*s)
2328 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002329 if (flags & LOOKUP_RCU)
2330 rcu_read_lock();
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002331
Al Virobcba1e72021-04-01 22:03:41 -04002332 nd->flags = flags;
2333 nd->state |= ND_JUMPED;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002334
2335 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2336 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2337 smp_rmb();
2338
Al Virobcba1e72021-04-01 22:03:41 -04002339 if (nd->state & ND_ROOT_PRESET) {
David Howellsb18825a2013-09-12 19:22:53 +01002340 struct dentry *root = nd->root.dentry;
2341 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002342 if (*s && unlikely(!d_can_lookup(root)))
2343 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002344 nd->path = nd->root;
2345 nd->inode = inode;
2346 if (flags & LOOKUP_RCU) {
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002347 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002348 nd->root_seq = nd->seq;
Al Viro5b6ca022011-03-09 23:04:47 -05002349 } else {
2350 path_get(&nd->path);
2351 }
Al Viro368ee9b2015-05-08 17:19:59 -04002352 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002353 }
2354
Al Viro2a737872009-04-07 11:49:53 -04002355 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002357 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2358 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
Aleksa Sarai740a1672019-12-07 01:13:29 +11002359 error = nd_jump_root(nd);
2360 if (unlikely(error))
2361 return ERR_PTR(error);
2362 return s;
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002363 }
2364
2365 /* Relative pathname -- get the starting-point it is relative to. */
2366 if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002367 if (flags & LOOKUP_RCU) {
2368 struct fs_struct *fs = current->fs;
2369 unsigned seq;
2370
Al Viroe41f7d42011-02-22 14:02:58 -05002371 do {
2372 seq = read_seqcount_begin(&fs->seq);
2373 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002374 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002375 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2376 } while (read_seqcount_retry(&fs->seq, seq));
2377 } else {
2378 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002379 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002380 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002381 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002382 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002383 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002384 struct dentry *dentry;
2385
Al Viro2903ff02012-08-28 12:52:22 -04002386 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002387 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002388
Al Viro2903ff02012-08-28 12:52:22 -04002389 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002390
Al Viroedc2b1d2018-07-09 16:27:23 -04002391 if (*s && unlikely(!d_can_lookup(dentry))) {
2392 fdput(f);
2393 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002394 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002395
Al Viro2903ff02012-08-28 12:52:22 -04002396 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002397 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002398 nd->inode = nd->path.dentry->d_inode;
2399 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002400 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002401 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002402 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002403 }
Al Viro34a26b92015-05-11 08:05:05 -04002404 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002406
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002407 /* For scoped-lookups we need to set the root to the dirfd as well. */
2408 if (flags & LOOKUP_IS_SCOPED) {
2409 nd->root = nd->path;
2410 if (flags & LOOKUP_RCU) {
2411 nd->root_seq = nd->seq;
2412 } else {
2413 path_get(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -04002414 nd->state |= ND_ROOT_GRABBED;
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002415 }
2416 }
2417 return s;
Al Viro9b4a9b12009-04-07 11:44:16 -04002418}
2419
Al Viro1ccac622020-01-14 10:13:40 -05002420static inline const char *lookup_last(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002421{
Al Virobd92d7f2011-03-14 19:54:59 -04002422 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2423 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2424
Al Viroc1088372020-03-05 15:48:44 -05002425 return walk_component(nd, WALK_TRAILING);
Al Virobd92d7f2011-03-14 19:54:59 -04002426}
2427
Al Viro4f757f32017-04-15 17:31:22 -04002428static int handle_lookup_down(struct nameidata *nd)
2429{
Al Viroc1530072020-01-09 14:50:18 -05002430 if (!(nd->flags & LOOKUP_RCU))
Al Virodb3c9ad2020-01-09 14:41:00 -05002431 dget(nd->path.dentry);
Al Virob0417d22020-01-14 13:34:20 -05002432 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2433 nd->path.dentry, nd->inode, nd->seq));
Al Viro4f757f32017-04-15 17:31:22 -04002434}
2435
Al Viro9b4a9b12009-04-07 11:44:16 -04002436/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002437static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002438{
Al Viroc8a53ee2015-05-12 18:43:07 -04002439 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002440 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002441
Al Viro9b5858e2018-07-09 16:33:23 -04002442 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002443 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002444 if (unlikely(err < 0))
2445 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002446 }
2447
Al Viro1ccac622020-01-14 10:13:40 -05002448 while (!(err = link_path_walk(s, nd)) &&
2449 (s = lookup_last(nd)) != NULL)
2450 ;
Al Viro4f0ed932021-04-06 19:46:51 -04002451 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2452 err = handle_lookup_down(nd);
Al Virobcba1e72021-04-01 22:03:41 -04002453 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please...
Al Viro4f0ed932021-04-06 19:46:51 -04002454 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002455 if (!err)
2456 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002457
Al Virodeb106c2015-05-08 18:05:21 -04002458 if (!err && nd->flags & LOOKUP_DIRECTORY)
2459 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002460 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002461 if (!err) {
2462 *path = nd->path;
2463 nd->path.mnt = NULL;
2464 nd->path.dentry = NULL;
2465 }
2466 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002467 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002468}
Nick Piggin31e6b012011-01-07 17:49:52 +11002469
Stephen Brennan794ebce2021-09-01 10:51:42 -07002470int filename_lookup(int dfd, struct filename *name, unsigned flags,
David Howells31d921c2018-11-01 23:07:24 +00002471 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002472{
Al Viro894bc8c2015-05-02 07:16:16 -04002473 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002474 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002475 if (IS_ERR(name))
2476 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002477 set_nameidata(&nd, dfd, name, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04002478 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002479 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002480 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002481 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002482 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002483
2484 if (likely(!retval))
Al Viro161aff12020-01-11 22:52:26 -05002485 audit_inode(name, path->dentry,
2486 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
Al Viro9883d182015-05-13 07:28:08 -04002487 restore_nameidata();
Dmitry Kadashev020250f2021-07-08 13:34:43 +07002488 return retval;
2489}
2490
Al Viro8bcb77f2015-05-08 16:59:20 -04002491/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002492static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002493 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002494{
Al Viroc8a53ee2015-05-12 18:43:07 -04002495 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002496 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002497 if (!err)
2498 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002499 if (!err) {
2500 *parent = nd->path;
2501 nd->path.mnt = NULL;
2502 nd->path.dentry = NULL;
2503 }
2504 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002505 return err;
2506}
2507
Stephen Brennan0766ec82021-09-01 10:51:41 -07002508/* Note: this does not consume "name" */
Al Viroc5f563f2021-09-07 15:57:42 -04002509static int filename_parentat(int dfd, struct filename *name,
Stephen Brennan0766ec82021-09-01 10:51:41 -07002510 unsigned int flags, struct path *parent,
2511 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002512{
2513 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002514 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002515
Al Viro5c31b6c2015-05-12 17:32:54 -04002516 if (IS_ERR(name))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002517 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002518 set_nameidata(&nd, dfd, name, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04002519 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002520 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002521 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002522 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002523 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002524 if (likely(!retval)) {
2525 *last = nd.last;
2526 *type = nd.last_type;
Al Viroc9b07ea2019-07-14 13:22:27 -04002527 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002528 }
Al Viro9883d182015-05-13 07:28:08 -04002529 restore_nameidata();
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002530 return retval;
2531}
2532
Al Viro79714f72012-06-15 03:01:42 +04002533/* does lookup, returns the object with parent locked */
Stephen Brennan0766ec82021-09-01 10:51:41 -07002534static struct dentry *__kern_path_locked(struct filename *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002535{
Al Viro5c31b6c2015-05-12 17:32:54 -04002536 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002537 struct qstr last;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002538 int type, error;
Paul Moore51689102015-01-22 00:00:03 -05002539
Al Viroc5f563f2021-09-07 15:57:42 -04002540 error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002541 if (error)
2542 return ERR_PTR(error);
Al Viro5c31b6c2015-05-12 17:32:54 -04002543 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002544 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002545 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002546 }
Al Viro59551022016-01-22 15:40:57 -05002547 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002548 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002549 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002550 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002551 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002552 }
Al Viro79714f72012-06-15 03:01:42 +04002553 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002554}
2555
Stephen Brennan0766ec82021-09-01 10:51:41 -07002556struct dentry *kern_path_locked(const char *name, struct path *path)
2557{
2558 struct filename *filename = getname_kernel(name);
2559 struct dentry *res = __kern_path_locked(filename, path);
2560
2561 putname(filename);
2562 return res;
2563}
2564
Al Virod1811462008-08-02 00:49:18 -04002565int kern_path(const char *name, unsigned int flags, struct path *path)
2566{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002567 struct filename *filename = getname_kernel(name);
2568 int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL);
2569
2570 putname(filename);
2571 return ret;
2572
Al Virod1811462008-08-02 00:49:18 -04002573}
Al Viro4d359502014-03-14 12:20:17 -04002574EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002575
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002576/**
2577 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2578 * @dentry: pointer to dentry of the base directory
2579 * @mnt: pointer to vfs mount of the base directory
2580 * @name: pointer to file name
2581 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002582 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002583 */
2584int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2585 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002586 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002587{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002588 struct filename *filename;
Al Viro9ad1aaa2015-05-12 16:44:39 -04002589 struct path root = {.mnt = mnt, .dentry = dentry};
Stephen Brennan794ebce2021-09-01 10:51:42 -07002590 int ret;
2591
2592 filename = getname_kernel(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002593 /* the first argument of filename_lookup() is ignored with root */
Stephen Brennan794ebce2021-09-01 10:51:42 -07002594 ret = filename_lookup(AT_FDCWD, filename, flags, path, &root);
2595 putname(filename);
2596 return ret;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002597}
Al Viro4d359502014-03-14 12:20:17 -04002598EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002599
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002600static int lookup_one_common(struct user_namespace *mnt_userns,
2601 const char *name, struct dentry *base, int len,
2602 struct qstr *this)
Al Viro3c95f0d2018-04-06 16:32:38 -04002603{
2604 this->name = name;
2605 this->len = len;
2606 this->hash = full_name_hash(base, name, len);
2607 if (!len)
2608 return -EACCES;
2609
2610 if (unlikely(name[0] == '.')) {
2611 if (len < 2 || (len == 2 && name[1] == '.'))
2612 return -EACCES;
2613 }
2614
2615 while (len--) {
2616 unsigned int c = *(const unsigned char *)name++;
2617 if (c == '/' || c == '\0')
2618 return -EACCES;
2619 }
2620 /*
2621 * See if the low-level filesystem might want
2622 * to use its own hash..
2623 */
2624 if (base->d_flags & DCACHE_OP_HASH) {
2625 int err = base->d_op->d_hash(base, this);
2626 if (err < 0)
2627 return err;
2628 }
2629
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002630 return inode_permission(mnt_userns, base->d_inode, MAY_EXEC);
Al Viro3c95f0d2018-04-06 16:32:38 -04002631}
2632
Christoph Hellwigeead1912007-10-16 23:25:38 -07002633/**
David Howells0da0b7f2018-06-15 15:19:22 +01002634 * try_lookup_one_len - filesystem helper to lookup single pathname component
2635 * @name: pathname component to lookup
2636 * @base: base directory to lookup from
2637 * @len: maximum length @len should be interpreted to
2638 *
2639 * Look up a dentry by name in the dcache, returning NULL if it does not
2640 * currently exist. The function does not try to create a dentry.
2641 *
2642 * Note that this routine is purely a helper for filesystem usage and should
2643 * not be called by generic code.
2644 *
2645 * The caller must hold base->i_mutex.
2646 */
2647struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2648{
2649 struct qstr this;
2650 int err;
2651
2652 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2653
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002654 err = lookup_one_common(&init_user_ns, name, base, len, &this);
David Howells0da0b7f2018-06-15 15:19:22 +01002655 if (err)
2656 return ERR_PTR(err);
2657
2658 return lookup_dcache(&this, base, 0);
2659}
2660EXPORT_SYMBOL(try_lookup_one_len);
2661
2662/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002663 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002664 * @name: pathname component to lookup
2665 * @base: base directory to lookup from
2666 * @len: maximum length @len should be interpreted to
2667 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002668 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002669 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002670 *
2671 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002672 */
James Morris057f6c02007-04-26 00:12:05 -07002673struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2674{
Al Viro8613a202018-04-06 16:45:33 -04002675 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002676 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002677 int err;
James Morris057f6c02007-04-26 00:12:05 -07002678
Al Viro59551022016-01-22 15:40:57 -05002679 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002680
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002681 err = lookup_one_common(&init_user_ns, name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002682 if (err)
2683 return ERR_PTR(err);
2684
Al Viro8613a202018-04-06 16:45:33 -04002685 dentry = lookup_dcache(&this, base, 0);
2686 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002687}
Al Viro4d359502014-03-14 12:20:17 -04002688EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002689
NeilBrownbbddca82016-01-07 16:08:20 -05002690/**
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002691 * lookup_one - filesystem helper to lookup single pathname component
2692 * @mnt_userns: user namespace of the mount the lookup is performed from
2693 * @name: pathname component to lookup
2694 * @base: base directory to lookup from
2695 * @len: maximum length @len should be interpreted to
2696 *
2697 * Note that this routine is purely a helper for filesystem usage and should
2698 * not be called by generic code.
2699 *
2700 * The caller must hold base->i_mutex.
2701 */
2702struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name,
2703 struct dentry *base, int len)
2704{
2705 struct dentry *dentry;
2706 struct qstr this;
2707 int err;
2708
2709 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2710
2711 err = lookup_one_common(mnt_userns, name, base, len, &this);
2712 if (err)
2713 return ERR_PTR(err);
2714
2715 dentry = lookup_dcache(&this, base, 0);
2716 return dentry ? dentry : __lookup_slow(&this, base, 0);
2717}
2718EXPORT_SYMBOL(lookup_one);
2719
2720/**
NeilBrownbbddca82016-01-07 16:08:20 -05002721 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2722 * @name: pathname component to lookup
2723 * @base: base directory to lookup from
2724 * @len: maximum length @len should be interpreted to
2725 *
2726 * Note that this routine is purely a helper for filesystem usage and should
2727 * not be called by generic code.
2728 *
2729 * Unlike lookup_one_len, it should be called without the parent
2730 * i_mutex held, and will take the i_mutex itself if necessary.
2731 */
2732struct dentry *lookup_one_len_unlocked(const char *name,
2733 struct dentry *base, int len)
2734{
2735 struct qstr this;
NeilBrownbbddca82016-01-07 16:08:20 -05002736 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002737 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002738
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002739 err = lookup_one_common(&init_user_ns, name, base, len, &this);
NeilBrownbbddca82016-01-07 16:08:20 -05002740 if (err)
2741 return ERR_PTR(err);
2742
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002743 ret = lookup_dcache(&this, base, 0);
2744 if (!ret)
2745 ret = lookup_slow(&this, base, 0);
2746 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002747}
2748EXPORT_SYMBOL(lookup_one_len_unlocked);
2749
Al Viro6c2d47982019-10-31 01:21:58 -04002750/*
2751 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2752 * on negatives. Returns known positive or ERR_PTR(); that's what
2753 * most of the users want. Note that pinned negative with unlocked parent
2754 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2755 * need to be very careful; pinned positives have ->d_inode stable, so
2756 * this one avoids such problems.
2757 */
2758struct dentry *lookup_positive_unlocked(const char *name,
2759 struct dentry *base, int len)
2760{
2761 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
Al Viro2fa6b1e2019-11-12 16:13:06 -05002762 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
Al Viro6c2d47982019-10-31 01:21:58 -04002763 dput(ret);
2764 ret = ERR_PTR(-ENOENT);
2765 }
2766 return ret;
2767}
2768EXPORT_SYMBOL(lookup_positive_unlocked);
2769
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002770#ifdef CONFIG_UNIX98_PTYS
2771int path_pts(struct path *path)
2772{
2773 /* Find something mounted on "pts" in the same directory as
2774 * the input path.
2775 */
Al Viroa6a7eb72020-03-11 13:05:03 -04002776 struct dentry *parent = dget_parent(path->dentry);
2777 struct dentry *child;
Al Viro19f60282020-02-26 20:09:37 -05002778 struct qstr this = QSTR_INIT("pts", 3);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002779
Al Viroa6a7eb72020-03-11 13:05:03 -04002780 if (unlikely(!path_connected(path->mnt, parent))) {
2781 dput(parent);
Al Viro63b27722020-02-24 16:01:19 -05002782 return -ENOENT;
Al Viroa6a7eb72020-03-11 13:05:03 -04002783 }
Al Viro63b27722020-02-24 16:01:19 -05002784 dput(path->dentry);
2785 path->dentry = parent;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002786 child = d_hash_and_lookup(parent, &this);
2787 if (!child)
2788 return -ENOENT;
2789
2790 path->dentry = child;
2791 dput(parent);
Al Viro19f60282020-02-26 20:09:37 -05002792 follow_down(path);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002793 return 0;
2794}
2795#endif
2796
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002797int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2798 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002800 struct filename *filename = getname_flags(name, flags, empty);
2801 int ret = filename_lookup(dfd, filename, flags, path, NULL);
2802
2803 putname(filename);
2804 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
Al Virob853a162015-05-13 09:12:02 -04002806EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002807
Christian Braunerba73d982021-01-21 14:19:31 +01002808int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
2809 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002811 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002812
Christian Braunerba73d982021-01-21 14:19:31 +01002813 if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002815 if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002817 return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002819EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821/*
2822 * Check whether we can remove a link victim from directory dir, check
2823 * whether the type of victim is right.
2824 * 1. We can't do it if dir is read-only (done in permission())
2825 * 2. We should have write and exec permissions on dir
2826 * 3. We can't remove anything from append-only dir
2827 * 4. We can't do anything with immutable dir (done in permission())
2828 * 5. If the sticky bit on dir is set we should either
2829 * a. be owner of dir, or
2830 * b. be owner of victim, or
2831 * c. have CAP_FOWNER capability
2832 * 6. If the victim is append-only or immutable we can't do antyhing with
2833 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002834 * 7. If the victim has an unknown uid or gid we can't change the inode.
2835 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2836 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2837 * 10. We can't remove a root or mountpoint.
2838 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 * nfs_async_unlink().
2840 */
Christian Braunerba73d982021-01-21 14:19:31 +01002841static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
2842 struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
David Howells63afdfc2015-05-06 15:59:00 +01002844 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int error;
2846
David Howellsb18825a2013-09-12 19:22:53 +01002847 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002849 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002852
2853 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01002854 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
2855 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002856 return -EOVERFLOW;
2857
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002858 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Christian Braunerba73d982021-01-21 14:19:31 +01002860 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (error)
2862 return error;
2863 if (IS_APPEND(dir))
2864 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002865
Christian Braunerba73d982021-01-21 14:19:31 +01002866 if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
2867 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
2868 HAS_UNMAPPED_ID(mnt_userns, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return -EPERM;
2870 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002871 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return -ENOTDIR;
2873 if (IS_ROOT(victim))
2874 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002875 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return -EISDIR;
2877 if (IS_DEADDIR(dir))
2878 return -ENOENT;
2879 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2880 return -EBUSY;
2881 return 0;
2882}
2883
2884/* Check whether we can create an object with dentry child in directory
2885 * dir.
2886 * 1. We can't do it if child already exists (open has special treatment for
2887 * this case, but since we are inlined it's OK)
2888 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002889 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2890 * 4. We should have write and exec permissions on dir
2891 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 */
Christian Braunerba73d982021-01-21 14:19:31 +01002893static inline int may_create(struct user_namespace *mnt_userns,
2894 struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Jeff Layton14e972b2013-05-08 10:25:58 -04002896 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (child->d_inode)
2898 return -EEXIST;
2899 if (IS_DEADDIR(dir))
2900 return -ENOENT;
Christian Brauner8e538912021-03-20 13:26:23 +01002901 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Eric W. Biederman036d5232016-07-01 12:52:06 -05002902 return -EOVERFLOW;
Christian Brauner8e538912021-03-20 13:26:23 +01002903
Christian Braunerba73d982021-01-21 14:19:31 +01002904 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907/*
2908 * p1 and p2 should be directories on the same fs.
2909 */
2910struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2911{
2912 struct dentry *p;
2913
2914 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002915 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return NULL;
2917 }
2918
Al Virofc640052016-04-10 01:33:30 -04002919 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002921 p = d_ancestor(p2, p1);
2922 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002923 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2924 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002925 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002928 p = d_ancestor(p1, p2);
2929 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002930 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2931 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002932 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934
Al Viro59551022016-01-22 15:40:57 -05002935 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2936 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return NULL;
2938}
Al Viro4d359502014-03-14 12:20:17 -04002939EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941void unlock_rename(struct dentry *p1, struct dentry *p2)
2942{
Al Viro59551022016-01-22 15:40:57 -05002943 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002945 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002946 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948}
Al Viro4d359502014-03-14 12:20:17 -04002949EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Christian Brauner6521f892021-01-21 14:19:33 +01002951/**
2952 * vfs_create - create new file
2953 * @mnt_userns: user namespace of the mount the inode was found from
2954 * @dir: inode of @dentry
2955 * @dentry: pointer to dentry of the base directory
2956 * @mode: mode of the new file
2957 * @want_excl: whether the file must not yet exist
2958 *
2959 * Create a new file.
2960 *
2961 * If the inode has been found through an idmapped mount the user namespace of
2962 * the vfsmount must be passed through @mnt_userns. This function will then take
2963 * care to map the inode according to @mnt_userns before checking permissions.
2964 * On non-idmapped mounts or if permission checking is to be performed on the
2965 * raw inode simply passs init_user_ns.
2966 */
2967int vfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2968 struct dentry *dentry, umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Christian Brauner6521f892021-01-21 14:19:33 +01002970 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (error)
2972 return error;
2973
Al Viroacfa4382008-12-04 10:06:33 -05002974 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return -EACCES; /* shouldn't it be ENOSYS? */
2976 mode &= S_IALLUGO;
2977 mode |= S_IFREG;
2978 error = security_inode_create(dir, dentry, mode);
2979 if (error)
2980 return error;
Christian Brauner549c7292021-01-21 14:19:43 +01002981 error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002982 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002983 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return error;
2985}
Al Viro4d359502014-03-14 12:20:17 -04002986EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Al Viro8e6c8482017-12-01 17:12:45 -05002988int vfs_mkobj(struct dentry *dentry, umode_t mode,
2989 int (*f)(struct dentry *, umode_t, void *),
2990 void *arg)
2991{
2992 struct inode *dir = dentry->d_parent->d_inode;
Christian Braunerba73d982021-01-21 14:19:31 +01002993 int error = may_create(&init_user_ns, dir, dentry);
Al Viro8e6c8482017-12-01 17:12:45 -05002994 if (error)
2995 return error;
2996
2997 mode &= S_IALLUGO;
2998 mode |= S_IFREG;
2999 error = security_inode_create(dir, dentry, mode);
3000 if (error)
3001 return error;
3002 error = f(dentry, mode, arg);
3003 if (!error)
3004 fsnotify_create(dir, dentry);
3005 return error;
3006}
3007EXPORT_SYMBOL(vfs_mkobj);
3008
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003009bool may_open_dev(const struct path *path)
3010{
3011 return !(path->mnt->mnt_flags & MNT_NODEV) &&
3012 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
3013}
3014
Christian Braunerba73d982021-01-21 14:19:31 +01003015static int may_open(struct user_namespace *mnt_userns, const struct path *path,
3016 int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02003018 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 struct inode *inode = dentry->d_inode;
3020 int error;
3021
3022 if (!inode)
3023 return -ENOENT;
3024
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003025 switch (inode->i_mode & S_IFMT) {
3026 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003028 case S_IFDIR:
Kees Cookfc4177b2020-08-14 17:30:14 -07003029 if (acc_mode & MAY_WRITE)
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003030 return -EISDIR;
Kees Cookfc4177b2020-08-14 17:30:14 -07003031 if (acc_mode & MAY_EXEC)
3032 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003033 break;
3034 case S_IFBLK:
3035 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003036 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return -EACCES;
Kees Cook633fb6a2020-08-11 18:36:26 -07003038 fallthrough;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003039 case S_IFIFO:
3040 case S_IFSOCK:
Kees Cook633fb6a2020-08-11 18:36:26 -07003041 if (acc_mode & MAY_EXEC)
3042 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003044 break;
Kees Cook0fd338b2020-08-11 18:36:30 -07003045 case S_IFREG:
3046 if ((acc_mode & MAY_EXEC) && path_noexec(path))
3047 return -EACCES;
3048 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08003049 }
Dave Hansenb41572e2007-10-16 23:31:14 -07003050
Christian Braunerba73d982021-01-21 14:19:31 +01003051 error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07003052 if (error)
3053 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05003054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 /*
3056 * An append-only file must be opened in append mode for writing.
3057 */
3058 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05003059 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05003060 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05003062 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
3064
3065 /* O_NOATIME can only be set by the owner or superuser */
Christian Braunerba73d982021-01-21 14:19:31 +01003066 if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
Al Viro7715b522009-12-16 03:54:00 -05003067 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04003069 return 0;
Al Viro7715b522009-12-16 03:54:00 -05003070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Christian Brauner549c7292021-01-21 14:19:43 +01003072static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05003073{
Al Virof0bb5aa2016-11-20 20:27:12 -05003074 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05003075 struct inode *inode = path->dentry->d_inode;
3076 int error = get_write_access(inode);
3077 if (error)
3078 return error;
Jeff Layton482e0002021-10-26 11:56:45 -04003079
Jeff Laytonf7e33bd2021-08-19 14:56:38 -04003080 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05003081 if (!error) {
Christian Brauner549c7292021-01-21 14:19:43 +01003082 error = do_truncate(mnt_userns, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05003083 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003084 filp);
Al Viro7715b522009-12-16 03:54:00 -05003085 }
3086 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003087 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Dave Hansend57999e2008-02-15 14:37:27 -08003090static inline int open_to_namei_flags(int flag)
3091{
Al Viro8a5e9292011-06-25 19:15:54 -04003092 if ((flag & O_ACCMODE) == 3)
3093 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003094 return flag;
3095}
3096
Christian Braunerba73d982021-01-21 14:19:31 +01003097static int may_o_create(struct user_namespace *mnt_userns,
3098 const struct path *dir, struct dentry *dentry,
3099 umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003100{
3101 int error = security_path_mknod(dir, dentry, mode, 0);
3102 if (error)
3103 return error;
3104
Christian Brauner8e538912021-03-20 13:26:23 +01003105 if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
Seth Forshee1328c722017-01-26 14:33:46 -06003106 return -EOVERFLOW;
3107
Christian Braunerba73d982021-01-21 14:19:31 +01003108 error = inode_permission(mnt_userns, dir->dentry->d_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01003109 MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003110 if (error)
3111 return error;
3112
3113 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3114}
3115
David Howells1acf0af2012-06-14 16:13:46 +01003116/*
3117 * Attempt to atomically look up, create and open a file from a negative
3118 * dentry.
3119 *
3120 * Returns 0 if successful. The file will have been created and attached to
3121 * @file by the filesystem calling finish_open().
3122 *
Al Viro00a07c12018-07-09 19:30:20 -04003123 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3124 * be set. The caller will need to perform the open themselves. @path will
3125 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003126 *
3127 * Returns an error code otherwise.
3128 */
Al Viro239eb982020-01-09 14:12:40 -05003129static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3130 struct file *file,
Al Viro239eb982020-01-09 14:12:40 -05003131 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003132{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003133 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003134 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003135 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003136
Miklos Szeredid18e9002012-06-05 15:10:17 +02003137 if (nd->flags & LOOKUP_DIRECTORY)
3138 open_flag |= O_DIRECTORY;
3139
Al Viro30d90492012-06-22 12:40:19 +04003140 file->f_path.dentry = DENTRY_NOT_SET;
3141 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003142 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003143 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003144 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003145 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003146 if (file->f_mode & FMODE_OPENED) {
Al Viro6fb968c2020-01-26 09:53:19 -05003147 if (unlikely(dentry != file->f_path.dentry)) {
3148 dput(dentry);
3149 dentry = dget(file->f_path.dentry);
3150 }
Al Viro64e1ac42018-07-09 19:17:52 -04003151 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003152 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003153 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003154 if (file->f_path.dentry) {
3155 dput(dentry);
3156 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003157 }
Al Viro239eb982020-01-09 14:12:40 -05003158 if (unlikely(d_is_negative(dentry)))
Al Viroa01e7182016-06-07 21:53:51 -04003159 error = -ENOENT;
Sage Weil62b2ce92012-08-15 13:30:12 -07003160 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003161 }
Al Viro239eb982020-01-09 14:12:40 -05003162 if (error) {
3163 dput(dentry);
3164 dentry = ERR_PTR(error);
3165 }
3166 return dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003167}
3168
Nick Piggin31e6b012011-01-07 17:49:52 +11003169/*
David Howells1acf0af2012-06-14 16:13:46 +01003170 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003171 *
Al Viro00a07c12018-07-09 19:30:20 -04003172 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003173 *
Al Viro00a07c12018-07-09 19:30:20 -04003174 * Returns 0 on success, that is, if
3175 * the file was successfully atomically created (if necessary) and opened, or
3176 * the file was not completely opened at this time, though lookups and
3177 * creations were performed.
3178 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3179 * In the latter case dentry returned in @path might be negative if O_CREAT
3180 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003181 *
Al Viro00a07c12018-07-09 19:30:20 -04003182 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003183 */
Al Viroda5ebf52020-01-09 14:25:14 -05003184static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3185 const struct open_flags *op,
3186 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003187{
Christian Brauner549c7292021-01-21 14:19:43 +01003188 struct user_namespace *mnt_userns;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003189 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003190 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003191 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003192 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003193 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003194 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003195 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003196
Al Viroce8644f2016-04-26 14:17:56 -04003197 if (unlikely(IS_DEADDIR(dir_inode)))
Al Viroda5ebf52020-01-09 14:25:14 -05003198 return ERR_PTR(-ENOENT);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003199
Al Viro73a09dd2018-06-08 13:22:02 -04003200 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003201 dentry = d_lookup(dir, &nd->last);
3202 for (;;) {
3203 if (!dentry) {
3204 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3205 if (IS_ERR(dentry))
Al Viroda5ebf52020-01-09 14:25:14 -05003206 return dentry;
Al Viro6fbd0712016-04-28 11:50:59 -04003207 }
3208 if (d_in_lookup(dentry))
3209 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003210
Al Viro6fbd0712016-04-28 11:50:59 -04003211 error = d_revalidate(dentry, nd->flags);
3212 if (likely(error > 0))
3213 break;
3214 if (error)
3215 goto out_dput;
3216 d_invalidate(dentry);
3217 dput(dentry);
3218 dentry = NULL;
3219 }
3220 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003221 /* Cached positive dentry: will open in f_op->open */
Al Viroda5ebf52020-01-09 14:25:14 -05003222 return dentry;
Al Viro6c51e512016-03-05 20:09:32 -05003223 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003224
Al Viro1643b432016-04-27 19:14:10 -04003225 /*
3226 * Checking write permission is tricky, bacuse we don't know if we are
3227 * going to actually need it: O_CREAT opens should work as long as the
3228 * file exists. But checking existence breaks atomicity. The trick is
3229 * to check access and if not granted clear O_CREAT from the flags.
3230 *
3231 * Another problem is returing the "right" error value (e.g. for an
3232 * O_EXCL open we want to return EEXIST not EROFS).
3233 */
Al Viro99a4a902020-03-12 14:07:27 -04003234 if (unlikely(!got_write))
3235 open_flag &= ~O_TRUNC;
Christian Brauner549c7292021-01-21 14:19:43 +01003236 mnt_userns = mnt_user_ns(nd->path.mnt);
Al Viro1643b432016-04-27 19:14:10 -04003237 if (open_flag & O_CREAT) {
Al Viro99a4a902020-03-12 14:07:27 -04003238 if (open_flag & O_EXCL)
3239 open_flag &= ~O_TRUNC;
Al Viro1643b432016-04-27 19:14:10 -04003240 if (!IS_POSIXACL(dir->d_inode))
3241 mode &= ~current_umask();
Al Viro99a4a902020-03-12 14:07:27 -04003242 if (likely(got_write))
Christian Brauner549c7292021-01-21 14:19:43 +01003243 create_error = may_o_create(mnt_userns, &nd->path,
Christian Braunerba73d982021-01-21 14:19:31 +01003244 dentry, mode);
Al Viro99a4a902020-03-12 14:07:27 -04003245 else
3246 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003247 }
Al Viro99a4a902020-03-12 14:07:27 -04003248 if (create_error)
3249 open_flag &= ~O_CREAT;
Al Viro1643b432016-04-27 19:14:10 -04003250 if (dir_inode->i_op->atomic_open) {
Al Virod489cf92020-03-11 08:07:53 -04003251 dentry = atomic_open(nd, dentry, file, open_flag, mode);
Al Viroda5ebf52020-01-09 14:25:14 -05003252 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3253 dentry = ERR_PTR(create_error);
3254 return dentry;
Al Viro1643b432016-04-27 19:14:10 -04003255 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003256
Al Viro6fbd0712016-04-28 11:50:59 -04003257 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003258 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3259 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003260 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003261 if (unlikely(res)) {
3262 if (IS_ERR(res)) {
3263 error = PTR_ERR(res);
3264 goto out_dput;
3265 }
3266 dput(dentry);
3267 dentry = res;
3268 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003269 }
3270
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003271 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003272 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003273 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003274 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003275 if (!dir_inode->i_op->create) {
3276 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003277 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003278 }
Christian Brauner549c7292021-01-21 14:19:43 +01003279
3280 error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry,
3281 mode, open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003282 if (error)
3283 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003284 }
Al Viro1643b432016-04-27 19:14:10 -04003285 if (unlikely(create_error) && !dentry->d_inode) {
3286 error = create_error;
3287 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003288 }
Al Viroda5ebf52020-01-09 14:25:14 -05003289 return dentry;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003290
3291out_dput:
3292 dput(dentry);
Al Viroda5ebf52020-01-09 14:25:14 -05003293 return ERR_PTR(error);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003294}
3295
Al Viroc981a482020-01-26 11:06:21 -05003296static const char *open_last_lookups(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003297 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003298{
Al Viroa1e28032009-12-24 02:12:06 -05003299 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003300 int open_flag = op->open_flag;
Al Viro64894cf2012-07-31 00:53:35 +04003301 bool got_write = false;
Al Viro254cf582015-05-05 09:40:46 -04003302 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003303 struct inode *inode;
Al Viroda5ebf52020-01-09 14:25:14 -05003304 struct dentry *dentry;
Al Virob0417d22020-01-14 13:34:20 -05003305 const char *res;
Al Virofb1cc552009-12-24 01:58:28 -05003306
Al Viroc3e380b2011-02-23 13:39:45 -05003307 nd->flags |= op->intent;
3308
Al Virobc77daa2013-06-06 09:12:33 -04003309 if (nd->last_type != LAST_NORM) {
Al Viro56676ec2020-03-10 21:54:54 -04003310 if (nd->depth)
3311 put_link(nd);
Al Viroff326a32020-03-10 10:19:24 -04003312 return handle_dots(nd, nd->last_type);
Al Viro1f36f772009-12-26 10:56:19 -05003313 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003314
Al Viroca344a892011-03-09 00:36:45 -05003315 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003316 if (nd->last.name[nd->last.len])
3317 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3318 /* we _can_ be in RCU mode here */
Al Viro20e34352020-01-09 14:58:31 -05003319 dentry = lookup_fast(nd, &inode, &seq);
3320 if (IS_ERR(dentry))
Al Viro1ccac622020-01-14 10:13:40 -05003321 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05003322 if (likely(dentry))
Miklos Szeredi71574862012-06-05 15:10:14 +02003323 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003324
Al Viro6583fe22016-03-05 18:14:03 -05003325 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003326 } else {
3327 /* create side of things */
Al Viro72287412020-03-10 10:09:26 -04003328 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003329 if (!try_to_unlazy(nd))
3330 return ERR_PTR(-ECHILD);
Al Viro72287412020-03-10 10:09:26 -04003331 }
Al Viroc9b07ea2019-07-14 13:22:27 -04003332 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003333 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003334 if (unlikely(nd->last.name[nd->last.len]))
Al Viro1ccac622020-01-14 10:13:40 -05003335 return ERR_PTR(-EISDIR);
Al Virofe2d35f2011-03-05 22:58:25 -05003336 }
3337
Al Viro9cf843e2016-04-28 19:35:16 -04003338 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003339 got_write = !mnt_want_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003340 /*
3341 * do _not_ fail yet - we might not need that or fail with
3342 * a different error; let lookup_open() decide; we'll be
3343 * dropping this one anyway.
3344 */
3345 }
Al Viro9cf843e2016-04-28 19:35:16 -04003346 if (open_flag & O_CREAT)
3347 inode_lock(dir->d_inode);
3348 else
3349 inode_lock_shared(dir->d_inode);
Al Viroda5ebf52020-01-09 14:25:14 -05003350 dentry = lookup_open(nd, file, op, got_write);
Al Virof7bb9592020-03-05 13:25:20 -05003351 if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3352 fsnotify_create(dir->d_inode, dentry);
Al Viro9cf843e2016-04-28 19:35:16 -04003353 if (open_flag & O_CREAT)
3354 inode_unlock(dir->d_inode);
3355 else
3356 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003357
Al Viroc981a482020-01-26 11:06:21 -05003358 if (got_write)
Al Viro59e96e62020-01-26 10:22:24 -05003359 mnt_drop_write(nd->path.mnt);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003360
Al Viro59e96e62020-01-26 10:22:24 -05003361 if (IS_ERR(dentry))
3362 return ERR_CAST(dentry);
3363
Al Viro973d4b72020-01-26 10:48:16 -05003364 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
Al Viroe73cabf2020-01-09 14:30:08 -05003365 dput(nd->path.dentry);
3366 nd->path.dentry = dentry;
Al Viroc981a482020-01-26 11:06:21 -05003367 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05003368 }
3369
Al Viro20e34352020-01-09 14:58:31 -05003370finish_lookup:
Al Viro56676ec2020-03-10 21:54:54 -04003371 if (nd->depth)
3372 put_link(nd);
Al Viro8c4efe22020-01-19 12:44:18 -05003373 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
Al Viroff326a32020-03-10 10:19:24 -04003374 if (unlikely(res))
Al Virob0417d22020-01-14 13:34:20 -05003375 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viroff326a32020-03-10 10:19:24 -04003376 return res;
Al Viroc981a482020-01-26 11:06:21 -05003377}
3378
3379/*
3380 * Handle the last step of open()
3381 */
Al Viroc5971b82020-03-05 11:41:29 -05003382static int do_open(struct nameidata *nd,
Al Viroc981a482020-01-26 11:06:21 -05003383 struct file *file, const struct open_flags *op)
3384{
Christian Brauner549c7292021-01-21 14:19:43 +01003385 struct user_namespace *mnt_userns;
Al Viroc981a482020-01-26 11:06:21 -05003386 int open_flag = op->open_flag;
3387 bool do_truncate;
3388 int acc_mode;
Al Viroc981a482020-01-26 11:06:21 -05003389 int error;
3390
Al Viroff326a32020-03-10 10:19:24 -04003391 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3392 error = complete_walk(nd);
3393 if (error)
3394 return error;
3395 }
Al Viro973d4b72020-01-26 10:48:16 -05003396 if (!(file->f_mode & FMODE_CREATED))
3397 audit_inode(nd->name, nd->path.dentry, 0);
Christian Brauner549c7292021-01-21 14:19:43 +01003398 mnt_userns = mnt_user_ns(nd->path.mnt);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003399 if (open_flag & O_CREAT) {
Al Virob94e0b32020-03-10 10:13:53 -04003400 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3401 return -EEXIST;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003402 if (d_is_dir(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003403 return -EISDIR;
Christian Brauner549c7292021-01-21 14:19:43 +01003404 error = may_create_in_sticky(mnt_userns, nd,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003405 d_backing_inode(nd->path.dentry));
3406 if (unlikely(error))
Al Viroc5971b82020-03-05 11:41:29 -05003407 return error;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003408 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003409 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003410 return -ENOTDIR;
Al Viro6c0d46c2011-03-09 00:59:59 -05003411
Al Viro8795e7d2020-01-26 10:38:17 -05003412 do_truncate = false;
3413 acc_mode = op->acc_mode;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003414 if (file->f_mode & FMODE_CREATED) {
3415 /* Don't check for write permission, don't truncate */
3416 open_flag &= ~O_TRUNC;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003417 acc_mode = 0;
Al Viro8795e7d2020-01-26 10:38:17 -05003418 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
Al Viro0f9d1a12011-03-09 00:13:14 -05003419 error = mnt_want_write(nd->path.mnt);
3420 if (error)
Al Viroc5971b82020-03-05 11:41:29 -05003421 return error;
Al Viro8795e7d2020-01-26 10:38:17 -05003422 do_truncate = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003423 }
Christian Brauner549c7292021-01-21 14:19:43 +01003424 error = may_open(mnt_userns, &nd->path, acc_mode, open_flag);
Al Viro8795e7d2020-01-26 10:38:17 -05003425 if (!error && !(file->f_mode & FMODE_OPENED))
Al Viro3ad56152020-01-26 10:06:13 -05003426 error = vfs_open(&nd->path, file);
Al Viro8795e7d2020-01-26 10:38:17 -05003427 if (!error)
3428 error = ima_file_check(file, op->acc_mode);
3429 if (!error && do_truncate)
Christian Brauner549c7292021-01-21 14:19:43 +01003430 error = handle_truncate(mnt_userns, file);
Al Viroc80567c2016-02-27 19:17:33 -05003431 if (unlikely(error > 0)) {
3432 WARN_ON(1);
3433 error = -EINVAL;
3434 }
Al Viro8795e7d2020-01-26 10:38:17 -05003435 if (do_truncate)
Al Viro0f9d1a12011-03-09 00:13:14 -05003436 mnt_drop_write(nd->path.mnt);
Al Viroc5971b82020-03-05 11:41:29 -05003437 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003438}
3439
Christian Brauner6521f892021-01-21 14:19:33 +01003440/**
3441 * vfs_tmpfile - create tmpfile
3442 * @mnt_userns: user namespace of the mount the inode was found from
3443 * @dentry: pointer to dentry of the base directory
3444 * @mode: mode of the new tmpfile
Randy Dunlap2111c3c2021-02-15 20:29:28 -08003445 * @open_flag: flags
Christian Brauner6521f892021-01-21 14:19:33 +01003446 *
3447 * Create a temporary file.
3448 *
3449 * If the inode has been found through an idmapped mount the user namespace of
3450 * the vfsmount must be passed through @mnt_userns. This function will then take
3451 * care to map the inode according to @mnt_userns before checking permissions.
3452 * On non-idmapped mounts or if permission checking is to be performed on the
3453 * raw inode simply passs init_user_ns.
3454 */
3455struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns,
3456 struct dentry *dentry, umode_t mode, int open_flag)
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003457{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003458 struct dentry *child = NULL;
3459 struct inode *dir = dentry->d_inode;
3460 struct inode *inode;
3461 int error;
3462
3463 /* we want directory to be writable */
Christian Brauner6521f892021-01-21 14:19:33 +01003464 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003465 if (error)
3466 goto out_err;
3467 error = -EOPNOTSUPP;
3468 if (!dir->i_op->tmpfile)
3469 goto out_err;
3470 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003471 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003472 if (unlikely(!child))
3473 goto out_err;
Christian Brauner549c7292021-01-21 14:19:43 +01003474 error = dir->i_op->tmpfile(mnt_userns, dir, child, mode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003475 if (error)
3476 goto out_err;
3477 error = -ENOENT;
3478 inode = child->d_inode;
3479 if (unlikely(!inode))
3480 goto out_err;
3481 if (!(open_flag & O_EXCL)) {
3482 spin_lock(&inode->i_lock);
3483 inode->i_state |= I_LINKABLE;
3484 spin_unlock(&inode->i_lock);
3485 }
Christian Braunera2d23292021-01-21 14:19:45 +01003486 ima_post_create_tmpfile(mnt_userns, inode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003487 return child;
3488
3489out_err:
3490 dput(child);
3491 return ERR_PTR(error);
3492}
3493EXPORT_SYMBOL(vfs_tmpfile);
3494
Al Viroc8a53ee2015-05-12 18:43:07 -04003495static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003496 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003497 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003498{
Christian Brauner6521f892021-01-21 14:19:33 +01003499 struct user_namespace *mnt_userns;
Al Viro625b6d12015-05-12 16:36:12 -04003500 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003501 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003502 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003503 if (unlikely(error))
3504 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003505 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003506 if (unlikely(error))
3507 goto out;
Christian Brauner6521f892021-01-21 14:19:33 +01003508 mnt_userns = mnt_user_ns(path.mnt);
3509 child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003510 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003511 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003512 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003513 dput(path.dentry);
3514 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003515 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003516 /* Don't check for other permissions, the inode was just created */
Christian Brauner549c7292021-01-21 14:19:43 +01003517 error = may_open(mnt_userns, &path, 0, op->open_flag);
Al Viro1e8f44f2020-03-11 17:22:19 -04003518 if (!error)
3519 error = vfs_open(&path, file);
Al Viro60545d02013-06-07 01:20:27 -04003520out2:
Al Viro625b6d12015-05-12 16:36:12 -04003521 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003522out:
Al Viro625b6d12015-05-12 16:36:12 -04003523 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003524 return error;
3525}
3526
Al Viro6ac08702016-04-26 00:02:50 -04003527static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3528{
3529 struct path path;
3530 int error = path_lookupat(nd, flags, &path);
3531 if (!error) {
3532 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003533 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003534 path_put(&path);
3535 }
3536 return error;
3537}
3538
Al Viroc8a53ee2015-05-12 18:43:07 -04003539static struct file *path_openat(struct nameidata *nd,
3540 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
Al Viro30d90492012-06-22 12:40:19 +04003542 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003543 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003544
Al Viroea73ea72018-07-11 15:00:04 -04003545 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003546 if (IS_ERR(file))
3547 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003548
Al Virobb458c62013-07-13 13:26:37 +04003549 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003550 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003551 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003552 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003553 } else {
3554 const char *s = path_init(nd, flags);
3555 while (!(error = link_path_walk(s, nd)) &&
Al Viroc5971b82020-03-05 11:41:29 -05003556 (s = open_last_lookups(nd, file, op)) != NULL)
Al Viro1ccac622020-01-14 10:13:40 -05003557 ;
Al Viroc5971b82020-03-05 11:41:29 -05003558 if (!error)
3559 error = do_open(nd, file, op);
Al Viro5f336e72018-07-09 16:38:06 -04003560 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003561 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003562 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003563 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003564 return file;
3565 WARN_ON(1);
3566 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003567 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003568 fput(file);
3569 if (error == -EOPENSTALE) {
3570 if (flags & LOOKUP_RCU)
3571 error = -ECHILD;
3572 else
3573 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003574 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003575 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Jeff Layton669abf42012-10-10 16:43:10 -04003578struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003579 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003580{
Al Viro9883d182015-05-13 07:28:08 -04003581 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003582 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003583 struct file *filp;
3584
Al Viro06422962021-04-01 22:28:03 -04003585 set_nameidata(&nd, dfd, pathname, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04003586 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003587 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003588 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003589 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003590 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003591 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003592 return filp;
3593}
3594
Al Viroffb37ca2021-04-01 19:00:57 -04003595struct file *do_file_open_root(const struct path *root,
Al Virof9652e12013-06-11 08:23:01 +04003596 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003597{
Al Viro9883d182015-05-13 07:28:08 -04003598 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003599 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003600 struct filename *filename;
Al Virobcba1e72021-04-01 22:03:41 -04003601 int flags = op->lookup_flags;
Al Viro73d049a2011-03-11 12:08:24 -05003602
Al Viroffb37ca2021-04-01 19:00:57 -04003603 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003604 return ERR_PTR(-ELOOP);
3605
Paul Moore51689102015-01-22 00:00:03 -05003606 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303607 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003608 return ERR_CAST(filename);
3609
Al Viro06422962021-04-01 22:28:03 -04003610 set_nameidata(&nd, -1, filename, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04003611 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003612 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003613 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003614 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003615 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003616 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003617 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003618 return file;
3619}
3620
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003621static struct dentry *filename_create(int dfd, struct filename *name,
3622 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003624 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003625 struct qstr last;
3626 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003627 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003628 int error;
3629 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3630
3631 /*
3632 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3633 * other flags passed in are ignored!
3634 */
3635 lookup_flags &= LOOKUP_REVAL;
3636
Al Viroc5f563f2021-09-07 15:57:42 -04003637 error = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07003638 if (error)
3639 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003641 /*
3642 * Yucky last component or no last component at all?
3643 * (foo/., foo/.., /////)
3644 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003645 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003646 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003647
Jan Karac30dabf2012-06-12 16:20:30 +02003648 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003649 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003650 /*
3651 * Do the final lookup.
3652 */
Al Viro391172c2015-05-09 11:19:16 -04003653 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003654 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003655 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003657 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003658
Al Viroa8104a92012-07-20 02:25:00 +04003659 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003660 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003661 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003662
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003663 /*
3664 * Special case - lookup gave negative, but... we had foo/bar/
3665 * From the vfs_mknod() POV we just have a negative dentry -
3666 * all is fine. Let's be bastards - you had / on the end, you've
3667 * been asking for (non-existent) directory. -ENOENT for you.
3668 */
Al Viro391172c2015-05-09 11:19:16 -04003669 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003670 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003671 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003672 }
Jan Karac30dabf2012-06-12 16:20:30 +02003673 if (unlikely(err2)) {
3674 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003675 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678fail:
Al Viroa8104a92012-07-20 02:25:00 +04003679 dput(dentry);
3680 dentry = ERR_PTR(error);
3681unlock:
Al Viro59551022016-01-22 15:40:57 -05003682 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003683 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003684 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003685out:
Al Viro391172c2015-05-09 11:19:16 -04003686 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 return dentry;
3688}
Al Virofa14a0b2015-01-22 02:16:49 -05003689
3690struct dentry *kern_path_create(int dfd, const char *pathname,
3691 struct path *path, unsigned int lookup_flags)
3692{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003693 struct filename *filename = getname_kernel(pathname);
3694 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3695
3696 putname(filename);
3697 return res;
Al Virofa14a0b2015-01-22 02:16:49 -05003698}
Al Virodae6ad82011-06-26 11:50:15 -04003699EXPORT_SYMBOL(kern_path_create);
3700
Al Viro921a1652012-07-20 01:15:31 +04003701void done_path_create(struct path *path, struct dentry *dentry)
3702{
3703 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003704 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003705 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003706 path_put(path);
3707}
3708EXPORT_SYMBOL(done_path_create);
3709
Al Viro520ae682015-05-13 07:00:28 -04003710inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003711 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003712{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003713 struct filename *filename = getname(pathname);
3714 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3715
3716 putname(filename);
3717 return res;
Al Virodae6ad82011-06-26 11:50:15 -04003718}
3719EXPORT_SYMBOL(user_path_create);
3720
Christian Brauner6521f892021-01-21 14:19:33 +01003721/**
3722 * vfs_mknod - create device node or file
3723 * @mnt_userns: user namespace of the mount the inode was found from
3724 * @dir: inode of @dentry
3725 * @dentry: pointer to dentry of the base directory
3726 * @mode: mode of the new device node or file
3727 * @dev: device number of device to create
3728 *
3729 * Create a device node or file.
3730 *
3731 * If the inode has been found through an idmapped mount the user namespace of
3732 * the vfsmount must be passed through @mnt_userns. This function will then take
3733 * care to map the inode according to @mnt_userns before checking permissions.
3734 * On non-idmapped mounts or if permission checking is to be performed on the
3735 * raw inode simply passs init_user_ns.
3736 */
3737int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
3738 struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003740 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
Christian Brauner6521f892021-01-21 14:19:33 +01003741 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 if (error)
3744 return error;
3745
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003746 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3747 !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return -EPERM;
3749
Al Viroacfa4382008-12-04 10:06:33 -05003750 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 return -EPERM;
3752
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003753 error = devcgroup_inode_mknod(mode, dev);
3754 if (error)
3755 return error;
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 error = security_inode_mknod(dir, dentry, mode, dev);
3758 if (error)
3759 return error;
3760
Christian Brauner549c7292021-01-21 14:19:43 +01003761 error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003762 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003763 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 return error;
3765}
Al Viro4d359502014-03-14 12:20:17 -04003766EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Al Virof69aac02011-07-26 04:31:40 -04003768static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003769{
3770 switch (mode & S_IFMT) {
3771 case S_IFREG:
3772 case S_IFCHR:
3773 case S_IFBLK:
3774 case S_IFIFO:
3775 case S_IFSOCK:
3776 case 0: /* zero mode translates to S_IFREG */
3777 return 0;
3778 case S_IFDIR:
3779 return -EPERM;
3780 default:
3781 return -EINVAL;
3782 }
3783}
3784
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003785static int do_mknodat(int dfd, struct filename *name, umode_t mode,
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003786 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
Christian Brauner6521f892021-01-21 14:19:33 +01003788 struct user_namespace *mnt_userns;
Al Viro2ad94ae2008-07-21 09:32:51 -04003789 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003790 struct path path;
3791 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003792 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Al Viro8e4bfca2012-07-20 01:17:26 +04003794 error = may_mknod(mode);
3795 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003796 goto out1;
Jeff Layton972567f2012-12-20 16:00:10 -05003797retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003798 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev77972512021-07-08 13:34:40 +07003799 error = PTR_ERR(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003800 if (IS_ERR(dentry))
Dmitry Kadashev77972512021-07-08 13:34:40 +07003801 goto out1;
Al Viro2ad94ae2008-07-21 09:32:51 -04003802
Al Virodae6ad82011-06-26 11:50:15 -04003803 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003804 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003805 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003806 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003807 goto out2;
Christian Brauner6521f892021-01-21 14:19:33 +01003808
3809 mnt_userns = mnt_user_ns(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003810 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 case 0: case S_IFREG:
Christian Brauner6521f892021-01-21 14:19:33 +01003812 error = vfs_create(mnt_userns, path.dentry->d_inode,
3813 dentry, mode, true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003814 if (!error)
Christian Braunera2d23292021-01-21 14:19:45 +01003815 ima_post_path_mknod(mnt_userns, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 break;
3817 case S_IFCHR: case S_IFBLK:
Christian Brauner6521f892021-01-21 14:19:33 +01003818 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3819 dentry, mode, new_decode_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 break;
3821 case S_IFIFO: case S_IFSOCK:
Christian Brauner6521f892021-01-21 14:19:33 +01003822 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3823 dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003826out2:
Al Viro921a1652012-07-20 01:15:31 +04003827 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003828 if (retry_estale(error, lookup_flags)) {
3829 lookup_flags |= LOOKUP_REVAL;
3830 goto retry;
3831 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003832out1:
3833 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return error;
3835}
3836
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003837SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3838 unsigned int, dev)
3839{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003840 return do_mknodat(dfd, getname(filename), mode, dev);
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003841}
3842
Al Viro8208a222011-07-25 17:32:17 -04003843SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003844{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003845 return do_mknodat(AT_FDCWD, getname(filename), mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003846}
3847
Christian Brauner6521f892021-01-21 14:19:33 +01003848/**
3849 * vfs_mkdir - create directory
3850 * @mnt_userns: user namespace of the mount the inode was found from
3851 * @dir: inode of @dentry
3852 * @dentry: pointer to dentry of the base directory
3853 * @mode: mode of the new directory
3854 *
3855 * Create a directory.
3856 *
3857 * If the inode has been found through an idmapped mount the user namespace of
3858 * the vfsmount must be passed through @mnt_userns. This function will then take
3859 * care to map the inode according to @mnt_userns before checking permissions.
3860 * On non-idmapped mounts or if permission checking is to be performed on the
3861 * raw inode simply passs init_user_ns.
3862 */
3863int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
3864 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
Christian Brauner6521f892021-01-21 14:19:33 +01003866 int error = may_create(mnt_userns, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003867 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
3869 if (error)
3870 return error;
3871
Al Viroacfa4382008-12-04 10:06:33 -05003872 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return -EPERM;
3874
3875 mode &= (S_IRWXUGO|S_ISVTX);
3876 error = security_inode_mkdir(dir, dentry, mode);
3877 if (error)
3878 return error;
3879
Al Viro8de52772012-02-06 12:45:27 -05003880 if (max_links && dir->i_nlink >= max_links)
3881 return -EMLINK;
3882
Christian Brauner549c7292021-01-21 14:19:43 +01003883 error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003884 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003885 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return error;
3887}
Al Viro4d359502014-03-14 12:20:17 -04003888EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003890int do_mkdirat(int dfd, struct filename *name, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Dave Hansen6902d922006-09-30 23:29:01 -07003892 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003893 struct path path;
3894 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003895 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003897retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003898 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003899 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003900 if (IS_ERR(dentry))
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003901 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003902
Al Virodae6ad82011-06-26 11:50:15 -04003903 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003904 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003905 error = security_path_mkdir(&path, dentry, mode);
Christian Brauner6521f892021-01-21 14:19:33 +01003906 if (!error) {
3907 struct user_namespace *mnt_userns;
3908 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01003909 error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry,
3910 mode);
Christian Brauner6521f892021-01-21 14:19:33 +01003911 }
Al Viro921a1652012-07-20 01:15:31 +04003912 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003913 if (retry_estale(error, lookup_flags)) {
3914 lookup_flags |= LOOKUP_REVAL;
3915 goto retry;
3916 }
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003917out_putname:
3918 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return error;
3920}
3921
Dominik Brodowski0101db72018-03-11 11:34:49 +01003922SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3923{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003924 return do_mkdirat(dfd, getname(pathname), mode);
Dominik Brodowski0101db72018-03-11 11:34:49 +01003925}
3926
Al Viroa218d0f2011-11-21 14:59:34 -05003927SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003928{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003929 return do_mkdirat(AT_FDCWD, getname(pathname), mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003930}
3931
Christian Brauner6521f892021-01-21 14:19:33 +01003932/**
3933 * vfs_rmdir - remove directory
3934 * @mnt_userns: user namespace of the mount the inode was found from
3935 * @dir: inode of @dentry
3936 * @dentry: pointer to dentry of the base directory
3937 *
3938 * Remove a directory.
3939 *
3940 * If the inode has been found through an idmapped mount the user namespace of
3941 * the vfsmount must be passed through @mnt_userns. This function will then take
3942 * care to map the inode according to @mnt_userns before checking permissions.
3943 * On non-idmapped mounts or if permission checking is to be performed on the
3944 * raw inode simply passs init_user_ns.
3945 */
3946int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
3947 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Christian Brauner6521f892021-01-21 14:19:33 +01003949 int error = may_delete(mnt_userns, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 if (error)
3952 return error;
3953
Al Viroacfa4382008-12-04 10:06:33 -05003954 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 return -EPERM;
3956
Al Viro1d2ef592011-09-14 18:55:41 +01003957 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003958 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Sage Weil912dbc12011-05-24 13:06:11 -07003960 error = -EBUSY;
David Howells1bd9c4e2021-11-18 08:58:08 +00003961 if (is_local_mountpoint(dentry) ||
3962 (dentry->d_inode->i_flags & S_KERNEL_FILE))
Sage Weil912dbc12011-05-24 13:06:11 -07003963 goto out;
3964
3965 error = security_inode_rmdir(dir, dentry);
3966 if (error)
3967 goto out;
3968
3969 error = dir->i_op->rmdir(dir, dentry);
3970 if (error)
3971 goto out;
3972
Al Viro87677122018-05-27 16:23:51 -04003973 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003974 dentry->d_inode->i_flags |= S_DEAD;
3975 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003976 detach_mounts(dentry);
Amir Goldstein116b9732019-05-26 17:34:02 +03003977 fsnotify_rmdir(dir, dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003978
3979out:
Al Viro59551022016-01-22 15:40:57 -05003980 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003981 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003982 if (!error)
3983 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return error;
3985}
Al Viro4d359502014-03-14 12:20:17 -04003986EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003988int do_rmdir(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Christian Brauner6521f892021-01-21 14:19:33 +01003990 struct user_namespace *mnt_userns;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07003991 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003993 struct path path;
3994 struct qstr last;
3995 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003996 unsigned int lookup_flags = 0;
3997retry:
Al Viroc5f563f2021-09-07 15:57:42 -04003998 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07003999 if (error)
4000 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Al Virof5beed72015-04-30 16:09:11 -04004002 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004003 case LAST_DOTDOT:
4004 error = -ENOTEMPTY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004005 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004006 case LAST_DOT:
4007 error = -EINVAL;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004008 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004009 case LAST_ROOT:
4010 error = -EBUSY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004011 goto exit2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004013
Al Virof5beed72015-04-30 16:09:11 -04004014 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004015 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004016 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004017
Al Viro59551022016-01-22 15:40:57 -05004018 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004019 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004021 if (IS_ERR(dentry))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004022 goto exit3;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004023 if (!dentry->d_inode) {
4024 error = -ENOENT;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004025 goto exit4;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004026 }
Al Virof5beed72015-04-30 16:09:11 -04004027 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004028 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004029 goto exit4;
Christian Brauner6521f892021-01-21 14:19:33 +01004030 mnt_userns = mnt_user_ns(path.mnt);
4031 error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004032exit4:
Dave Hansen6902d922006-09-30 23:29:01 -07004033 dput(dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004034exit3:
Al Viro59551022016-01-22 15:40:57 -05004035 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04004036 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004037exit2:
Al Virof5beed72015-04-30 16:09:11 -04004038 path_put(&path);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05004039 if (retry_estale(error, lookup_flags)) {
4040 lookup_flags |= LOOKUP_REVAL;
4041 goto retry;
4042 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004043exit1:
Al Viro24fb33d2020-08-12 05:15:18 +01004044 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 return error;
4046}
4047
Heiko Carstens3cdad422009-01-14 14:14:22 +01004048SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004049{
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004050 return do_rmdir(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004051}
4052
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004053/**
4054 * vfs_unlink - unlink a filesystem object
Christian Brauner6521f892021-01-21 14:19:33 +01004055 * @mnt_userns: user namespace of the mount the inode was found from
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004056 * @dir: parent directory
4057 * @dentry: victim
4058 * @delegated_inode: returns victim inode, if the inode is delegated.
4059 *
4060 * The caller must hold dir->i_mutex.
4061 *
4062 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4063 * return a reference to the inode in delegated_inode. The caller
4064 * should then break the delegation on that inode and retry. Because
4065 * breaking a delegation may take a long time, the caller should drop
4066 * dir->i_mutex before doing so.
4067 *
4068 * Alternatively, a caller may pass NULL for delegated_inode. This may
4069 * be appropriate for callers that expect the underlying filesystem not
4070 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004071 *
4072 * If the inode has been found through an idmapped mount the user namespace of
4073 * the vfsmount must be passed through @mnt_userns. This function will then take
4074 * care to map the inode according to @mnt_userns before checking permissions.
4075 * On non-idmapped mounts or if permission checking is to be performed on the
4076 * raw inode simply passs init_user_ns.
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004077 */
Christian Brauner6521f892021-01-21 14:19:33 +01004078int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
4079 struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004081 struct inode *target = dentry->d_inode;
Christian Brauner6521f892021-01-21 14:19:33 +01004082 int error = may_delete(mnt_userns, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
4084 if (error)
4085 return error;
4086
Al Viroacfa4382008-12-04 10:06:33 -05004087 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 return -EPERM;
4089
Al Viro59551022016-01-22 15:40:57 -05004090 inode_lock(target);
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004091 if (IS_SWAPFILE(target))
4092 error = -EPERM;
4093 else if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 error = -EBUSY;
4095 else {
4096 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004097 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004098 error = try_break_deleg(target, delegated_inode);
4099 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004102 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004103 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004104 detach_mounts(dentry);
Amir Goldstein116b9732019-05-26 17:34:02 +03004105 fsnotify_unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004106 }
Al Virobec10522010-03-03 14:12:08 -05004107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004109out:
Al Viro59551022016-01-22 15:40:57 -05004110 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
4112 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4113 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004114 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
Robert Love0eeca282005-07-12 17:06:03 -04004117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return error;
4119}
Al Viro4d359502014-03-14 12:20:17 -04004120EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122/*
4123 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004124 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 * writeout happening, and we don't want to prevent access to the directory
4126 * while waiting on the I/O.
4127 */
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004128int do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
Al Viro2ad94ae2008-07-21 09:32:51 -04004130 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004132 struct path path;
4133 struct qstr last;
4134 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004136 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004137 unsigned int lookup_flags = 0;
4138retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004139 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004140 if (error)
4141 goto exit1;
Al Viro2ad94ae2008-07-21 09:32:51 -04004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004144 if (type != LAST_NORM)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004145 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004146
Al Virof5beed72015-04-30 16:09:11 -04004147 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004148 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004149 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004150retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004151 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004152 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 error = PTR_ERR(dentry);
4154 if (!IS_ERR(dentry)) {
Christian Brauner6521f892021-01-21 14:19:33 +01004155 struct user_namespace *mnt_userns;
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004158 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004159 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004161 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004162 goto slashes;
4163 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004164 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004165 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004166 goto exit3;
Christian Brauner6521f892021-01-21 14:19:33 +01004167 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01004168 error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
4169 &delegated_inode);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004170exit3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 dput(dentry);
4172 }
Al Viro59551022016-01-22 15:40:57 -05004173 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 if (inode)
4175 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004176 inode = NULL;
4177 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004178 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004179 if (!error)
4180 goto retry_deleg;
4181 }
Al Virof5beed72015-04-30 16:09:11 -04004182 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004183exit2:
Al Virof5beed72015-04-30 16:09:11 -04004184 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004185 if (retry_estale(error, lookup_flags)) {
4186 lookup_flags |= LOOKUP_REVAL;
4187 inode = NULL;
4188 goto retry;
4189 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004190exit1:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004191 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return error;
4193
4194slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004195 if (d_is_negative(dentry))
4196 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004197 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004198 error = -EISDIR;
4199 else
4200 error = -ENOTDIR;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004201 goto exit3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004204SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004205{
4206 if ((flag & ~AT_REMOVEDIR) != 0)
4207 return -EINVAL;
4208
4209 if (flag & AT_REMOVEDIR)
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004210 return do_rmdir(dfd, getname(pathname));
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004211 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004212}
4213
Heiko Carstens3480b252009-01-14 14:14:16 +01004214SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004215{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004216 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004217}
4218
Christian Brauner6521f892021-01-21 14:19:33 +01004219/**
4220 * vfs_symlink - create symlink
4221 * @mnt_userns: user namespace of the mount the inode was found from
4222 * @dir: inode of @dentry
4223 * @dentry: pointer to dentry of the base directory
4224 * @oldname: name of the file to link to
4225 *
4226 * Create a symlink.
4227 *
4228 * If the inode has been found through an idmapped mount the user namespace of
4229 * the vfsmount must be passed through @mnt_userns. This function will then take
4230 * care to map the inode according to @mnt_userns before checking permissions.
4231 * On non-idmapped mounts or if permission checking is to be performed on the
4232 * raw inode simply passs init_user_ns.
4233 */
4234int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
4235 struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
Christian Brauner6521f892021-01-21 14:19:33 +01004237 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 if (error)
4240 return error;
4241
Al Viroacfa4382008-12-04 10:06:33 -05004242 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return -EPERM;
4244
4245 error = security_inode_symlink(dir, dentry, oldname);
4246 if (error)
4247 return error;
4248
Christian Brauner549c7292021-01-21 14:19:43 +01004249 error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004250 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004251 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 return error;
4253}
Al Viro4d359502014-03-14 12:20:17 -04004254EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07004256int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Al Viro2ad94ae2008-07-21 09:32:51 -04004258 int error;
Dave Hansen6902d922006-09-30 23:29:01 -07004259 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004260 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004261 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004263 if (IS_ERR(from)) {
4264 error = PTR_ERR(from);
4265 goto out_putnames;
4266 }
Jeff Laytonf46d3562012-12-11 12:10:08 -05004267retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004268 dentry = filename_create(newdfd, to, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004269 error = PTR_ERR(dentry);
4270 if (IS_ERR(dentry))
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004271 goto out_putnames;
Dave Hansen6902d922006-09-30 23:29:01 -07004272
Jeff Layton91a27b22012-10-10 15:25:28 -04004273 error = security_path_symlink(&path, dentry, from->name);
Christian Brauner6521f892021-01-21 14:19:33 +01004274 if (!error) {
4275 struct user_namespace *mnt_userns;
4276
4277 mnt_userns = mnt_user_ns(path.mnt);
4278 error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry,
4279 from->name);
4280 }
Al Viro921a1652012-07-20 01:15:31 +04004281 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004282 if (retry_estale(error, lookup_flags)) {
4283 lookup_flags |= LOOKUP_REVAL;
4284 goto retry;
4285 }
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004286out_putnames:
4287 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 putname(from);
4289 return error;
4290}
4291
Dominik Brodowskib724e842018-03-11 11:34:49 +01004292SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4293 int, newdfd, const char __user *, newname)
4294{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004295 return do_symlinkat(getname(oldname), newdfd, getname(newname));
Dominik Brodowskib724e842018-03-11 11:34:49 +01004296}
4297
Heiko Carstens3480b252009-01-14 14:14:16 +01004298SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004299{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004300 return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004301}
4302
J. Bruce Fields146a8592011-09-20 17:14:31 -04004303/**
4304 * vfs_link - create a new link
4305 * @old_dentry: object to be linked
Christian Brauner6521f892021-01-21 14:19:33 +01004306 * @mnt_userns: the user namespace of the mount
J. Bruce Fields146a8592011-09-20 17:14:31 -04004307 * @dir: new parent
4308 * @new_dentry: where to create the new link
4309 * @delegated_inode: returns inode needing a delegation break
4310 *
4311 * The caller must hold dir->i_mutex
4312 *
4313 * If vfs_link discovers a delegation on the to-be-linked file in need
4314 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4315 * inode in delegated_inode. The caller should then break the delegation
4316 * and retry. Because breaking a delegation may take a long time, the
4317 * caller should drop the i_mutex before doing so.
4318 *
4319 * Alternatively, a caller may pass NULL for delegated_inode. This may
4320 * be appropriate for callers that expect the underlying filesystem not
4321 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004322 *
4323 * If the inode has been found through an idmapped mount the user namespace of
4324 * the vfsmount must be passed through @mnt_userns. This function will then take
4325 * care to map the inode according to @mnt_userns before checking permissions.
4326 * On non-idmapped mounts or if permission checking is to be performed on the
4327 * raw inode simply passs init_user_ns.
J. Bruce Fields146a8592011-09-20 17:14:31 -04004328 */
Christian Brauner6521f892021-01-21 14:19:33 +01004329int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns,
4330 struct inode *dir, struct dentry *new_dentry,
4331 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004334 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 int error;
4336
4337 if (!inode)
4338 return -ENOENT;
4339
Christian Brauner6521f892021-01-21 14:19:33 +01004340 error = may_create(mnt_userns, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 if (error)
4342 return error;
4343
4344 if (dir->i_sb != inode->i_sb)
4345 return -EXDEV;
4346
4347 /*
4348 * A link to an append-only or immutable file cannot be created.
4349 */
4350 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4351 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004352 /*
4353 * Updating the link count will likely cause i_uid and i_gid to
4354 * be writen back improperly if their true value is unknown to
4355 * the vfs.
4356 */
Christian Brauner6521f892021-01-21 14:19:33 +01004357 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004358 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004359 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004361 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return -EPERM;
4363
4364 error = security_inode_link(old_dentry, dir, new_dentry);
4365 if (error)
4366 return error;
4367
Al Viro59551022016-01-22 15:40:57 -05004368 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304369 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004370 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304371 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004372 else if (max_links && inode->i_nlink >= max_links)
4373 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004374 else {
4375 error = try_break_deleg(inode, delegated_inode);
4376 if (!error)
4377 error = dir->i_op->link(old_dentry, dir, new_dentry);
4378 }
Al Virof4e0c302013-06-11 08:34:36 +04004379
4380 if (!error && (inode->i_state & I_LINKABLE)) {
4381 spin_lock(&inode->i_lock);
4382 inode->i_state &= ~I_LINKABLE;
4383 spin_unlock(&inode->i_lock);
4384 }
Al Viro59551022016-01-22 15:40:57 -05004385 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004386 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004387 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 return error;
4389}
Al Viro4d359502014-03-14 12:20:17 -04004390EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
4392/*
4393 * Hardlinks are often used in delicate situations. We avoid
4394 * security-related surprises by not following symlinks on the
4395 * newname. --KAB
4396 *
4397 * We don't follow them on the oldname either to be compatible
4398 * with linux 2.0, and to avoid hard-linking to directories
4399 * and other special files. --ADM
4400 */
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07004401int do_linkat(int olddfd, struct filename *old, int newdfd,
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004402 struct filename *new, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Christian Brauner6521f892021-01-21 14:19:33 +01004404 struct user_namespace *mnt_userns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004406 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004407 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304408 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004411 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
4412 error = -EINVAL;
4413 goto out_putnames;
4414 }
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304415 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004416 * To use null names we require CAP_DAC_READ_SEARCH
4417 * This ensures that not everyone will be able to create
4418 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304419 */
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004420 if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
4421 error = -ENOENT;
4422 goto out_putnames;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004423 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004424
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304425 if (flags & AT_SYMLINK_FOLLOW)
4426 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004427retry:
Stephen Brennan794ebce2021-09-01 10:51:42 -07004428 error = filename_lookup(olddfd, old, how, &old_path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 if (error)
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004430 goto out_putnames;
Al Viro2ad94ae2008-07-21 09:32:51 -04004431
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004432 new_dentry = filename_create(newdfd, new, &new_path,
Jeff Layton442e31c2012-12-20 16:15:38 -05004433 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004435 if (IS_ERR(new_dentry))
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004436 goto out_putpath;
Al Virodae6ad82011-06-26 11:50:15 -04004437
4438 error = -EXDEV;
4439 if (old_path.mnt != new_path.mnt)
4440 goto out_dput;
Christian Brauner549c7292021-01-21 14:19:43 +01004441 mnt_userns = mnt_user_ns(new_path.mnt);
4442 error = may_linkat(mnt_userns, &old_path);
Kees Cook800179c2012-07-25 17:29:07 -07004443 if (unlikely(error))
4444 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004445 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004446 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004447 goto out_dput;
Christian Brauner6521f892021-01-21 14:19:33 +01004448 error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
4449 new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004450out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004451 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004452 if (delegated_inode) {
4453 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004454 if (!error) {
4455 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004456 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004457 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004458 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004459 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004460 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004461 how |= LOOKUP_REVAL;
4462 goto retry;
4463 }
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004464out_putpath:
Al Viro2d8f3032008-07-22 09:59:21 -04004465 path_put(&old_path);
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004466out_putnames:
4467 putname(old);
4468 putname(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 return error;
4471}
4472
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004473SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4474 int, newdfd, const char __user *, newname, int, flags)
4475{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004476 return do_linkat(olddfd, getname_uflags(oldname, flags),
4477 newdfd, getname(newname), flags);
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004478}
4479
Heiko Carstens3480b252009-01-14 14:14:16 +01004480SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004481{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004482 return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004483}
4484
Miklos Szeredibc270272014-04-01 17:08:42 +02004485/**
4486 * vfs_rename - rename a filesystem object
Randy Dunlap2111c3c2021-02-15 20:29:28 -08004487 * @rd: pointer to &struct renamedata info
Miklos Szeredibc270272014-04-01 17:08:42 +02004488 *
4489 * The caller must hold multiple mutexes--see lock_rename()).
4490 *
4491 * If vfs_rename discovers a delegation in need of breaking at either
4492 * the source or destination, it will return -EWOULDBLOCK and return a
4493 * reference to the inode in delegated_inode. The caller should then
4494 * break the delegation and retry. Because breaking a delegation may
4495 * take a long time, the caller should drop all locks before doing
4496 * so.
4497 *
4498 * Alternatively, a caller may pass NULL for delegated_inode. This may
4499 * be appropriate for callers that expect the underlying filesystem not
4500 * to be NFS exported.
4501 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 * The worst of all namespace operations - renaming directory. "Perverted"
4503 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4504 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004505 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004506 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 * b) race potential - two innocent renames can create a loop together.
4508 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004509 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004511 * c) we have to lock _four_ objects - parents and victim (if it exists),
4512 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004513 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 * whether the target exists). Solution: try to be smart with locking
4515 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004516 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 * move will be locked. Thus we can rank directories by the tree
4518 * (ancestors first) and rank all non-directories after them.
4519 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004520 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 * HOWEVER, it relies on the assumption that any object with ->lookup()
4522 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4523 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004524 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004525 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004527 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 * locking].
4529 */
Christian Brauner9fe61452021-01-21 14:19:32 +01004530int vfs_rename(struct renamedata *rd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531{
4532 int error;
Christian Brauner9fe61452021-01-21 14:19:32 +01004533 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
4534 struct dentry *old_dentry = rd->old_dentry;
4535 struct dentry *new_dentry = rd->new_dentry;
4536 struct inode **delegated_inode = rd->delegated_inode;
4537 unsigned int flags = rd->flags;
Miklos Szeredibc270272014-04-01 17:08:42 +02004538 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004539 struct inode *source = old_dentry->d_inode;
4540 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004541 bool new_is_dir = false;
4542 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004543 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004545 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004546 return 0;
4547
Christian Brauner6521f892021-01-21 14:19:33 +01004548 error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (error)
4550 return error;
4551
Miklos Szeredida1ce062014-04-01 17:08:43 +02004552 if (!target) {
Christian Brauner6521f892021-01-21 14:19:33 +01004553 error = may_create(rd->new_mnt_userns, new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004554 } else {
4555 new_is_dir = d_is_dir(new_dentry);
4556
4557 if (!(flags & RENAME_EXCHANGE))
Christian Brauner6521f892021-01-21 14:19:33 +01004558 error = may_delete(rd->new_mnt_userns, new_dir,
4559 new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004560 else
Christian Brauner6521f892021-01-21 14:19:33 +01004561 error = may_delete(rd->new_mnt_userns, new_dir,
4562 new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (error)
4565 return error;
4566
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004567 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 return -EPERM;
4569
Miklos Szeredibc270272014-04-01 17:08:42 +02004570 /*
4571 * If we are going to change the parent - check write permissions,
4572 * we'll need to flip '..'.
4573 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004574 if (new_dir != old_dir) {
4575 if (is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004576 error = inode_permission(rd->old_mnt_userns, source,
Christian Brauner47291ba2021-01-21 14:19:24 +01004577 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004578 if (error)
4579 return error;
4580 }
4581 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004582 error = inode_permission(rd->new_mnt_userns, target,
Christian Brauner47291ba2021-01-21 14:19:24 +01004583 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004584 if (error)
4585 return error;
4586 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004587 }
Robert Love0eeca282005-07-12 17:06:03 -04004588
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004589 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4590 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004591 if (error)
4592 return error;
4593
Al Viro49d31c22017-07-07 14:51:19 -04004594 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004595 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004596 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004597 lock_two_nondirectories(source, target);
4598 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004599 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004600
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004601 error = -EPERM;
4602 if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target)))
4603 goto out;
4604
Miklos Szeredibc270272014-04-01 17:08:42 +02004605 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004606 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004607 goto out;
4608
Miklos Szeredida1ce062014-04-01 17:08:43 +02004609 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004610 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004611 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004612 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004613 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4614 old_dir->i_nlink >= max_links)
4615 goto out;
4616 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004617 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004618 error = try_break_deleg(source, delegated_inode);
4619 if (error)
4620 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004621 }
4622 if (target && !new_is_dir) {
4623 error = try_break_deleg(target, delegated_inode);
4624 if (error)
4625 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004626 }
Christian Brauner549c7292021-01-21 14:19:43 +01004627 error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry,
4628 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004629 if (error)
4630 goto out;
4631
Miklos Szeredida1ce062014-04-01 17:08:43 +02004632 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004633 if (is_dir) {
4634 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004635 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004636 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004637 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004638 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004639 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004640 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4641 if (!(flags & RENAME_EXCHANGE))
4642 d_move(old_dentry, new_dentry);
4643 else
4644 d_exchange(old_dentry, new_dentry);
4645 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004646out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004647 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004648 unlock_two_nondirectories(source, target);
4649 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004650 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004651 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004652 if (!error) {
Al Virof4ec3a32019-04-26 13:21:24 -04004653 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004654 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4655 if (flags & RENAME_EXCHANGE) {
Al Virof4ec3a32019-04-26 13:21:24 -04004656 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004657 new_is_dir, NULL, new_dentry);
4658 }
4659 }
Al Viro49d31c22017-07-07 14:51:19 -04004660 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 return error;
4663}
Al Viro4d359502014-03-14 12:20:17 -04004664EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Jens Axboee8866632020-09-26 17:20:17 -06004666int do_renameat2(int olddfd, struct filename *from, int newdfd,
4667 struct filename *to, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Christian Brauner9fe61452021-01-21 14:19:32 +01004669 struct renamedata rd;
Al Viro2ad94ae2008-07-21 09:32:51 -04004670 struct dentry *old_dentry, *new_dentry;
4671 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004672 struct path old_path, new_path;
4673 struct qstr old_last, new_last;
4674 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004675 struct inode *delegated_inode = NULL;
Al Virof5beed72015-04-30 16:09:11 -04004676 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004677 bool should_retry = false;
Jens Axboee8866632020-09-26 17:20:17 -06004678 int error = -EINVAL;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004679
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004680 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004681 goto put_names;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004682
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004683 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4684 (flags & RENAME_EXCHANGE))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004685 goto put_names;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004686
Al Virof5beed72015-04-30 16:09:11 -04004687 if (flags & RENAME_EXCHANGE)
4688 target_flags = 0;
4689
Jeff Laytonc6a94282012-12-11 12:10:10 -05004690retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004691 error = filename_parentat(olddfd, from, lookup_flags, &old_path,
4692 &old_last, &old_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004693 if (error)
4694 goto put_names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Al Viroc5f563f2021-09-07 15:57:42 -04004696 error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4697 &new_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004698 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 goto exit1;
4700
4701 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004702 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 goto exit2;
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004706 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 goto exit2;
4708
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004709 if (flags & RENAME_NOREPLACE)
4710 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004711 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 goto exit2;
4713
Al Virof5beed72015-04-30 16:09:11 -04004714 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004715 if (error)
4716 goto exit2;
4717
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004718retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004719 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Al Virof5beed72015-04-30 16:09:11 -04004721 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 error = PTR_ERR(old_dentry);
4723 if (IS_ERR(old_dentry))
4724 goto exit3;
4725 /* source must exist */
4726 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004727 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004729 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 error = PTR_ERR(new_dentry);
4731 if (IS_ERR(new_dentry))
4732 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004733 error = -EEXIST;
4734 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4735 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004736 if (flags & RENAME_EXCHANGE) {
4737 error = -ENOENT;
4738 if (d_is_negative(new_dentry))
4739 goto exit5;
4740
4741 if (!d_is_dir(new_dentry)) {
4742 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004743 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004744 goto exit5;
4745 }
4746 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004747 /* unless the source is a directory trailing slashes give -ENOTDIR */
4748 if (!d_is_dir(old_dentry)) {
4749 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004750 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004751 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004752 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004753 goto exit5;
4754 }
4755 /* source should not be ancestor of target */
4756 error = -EINVAL;
4757 if (old_dentry == trap)
4758 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004760 if (!(flags & RENAME_EXCHANGE))
4761 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 if (new_dentry == trap)
4763 goto exit5;
4764
Al Virof5beed72015-04-30 16:09:11 -04004765 error = security_path_rename(&old_path, old_dentry,
4766 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004767 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004768 goto exit5;
Christian Brauner9fe61452021-01-21 14:19:32 +01004769
4770 rd.old_dir = old_path.dentry->d_inode;
4771 rd.old_dentry = old_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004772 rd.old_mnt_userns = mnt_user_ns(old_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004773 rd.new_dir = new_path.dentry->d_inode;
4774 rd.new_dentry = new_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004775 rd.new_mnt_userns = mnt_user_ns(new_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004776 rd.delegated_inode = &delegated_inode;
4777 rd.flags = flags;
4778 error = vfs_rename(&rd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779exit5:
4780 dput(new_dentry);
4781exit4:
4782 dput(old_dentry);
4783exit3:
Al Virof5beed72015-04-30 16:09:11 -04004784 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004785 if (delegated_inode) {
4786 error = break_deleg_wait(&delegated_inode);
4787 if (!error)
4788 goto retry_deleg;
4789 }
Al Virof5beed72015-04-30 16:09:11 -04004790 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004792 if (retry_estale(error, lookup_flags))
4793 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004794 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795exit1:
Al Virof5beed72015-04-30 16:09:11 -04004796 path_put(&old_path);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004797 if (should_retry) {
4798 should_retry = false;
4799 lookup_flags |= LOOKUP_REVAL;
4800 goto retry;
4801 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004802put_names:
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004803 putname(from);
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004804 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 return error;
4806}
4807
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004808SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4809 int, newdfd, const char __user *, newname, unsigned int, flags)
4810{
Jens Axboee8866632020-09-26 17:20:17 -06004811 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4812 flags);
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004813}
4814
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004815SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4816 int, newdfd, const char __user *, newname)
4817{
Jens Axboee8866632020-09-26 17:20:17 -06004818 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4819 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004820}
4821
Heiko Carstensa26eab22009-01-14 14:14:17 +01004822SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004823{
Jens Axboee8866632020-09-26 17:20:17 -06004824 return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4825 getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004826}
4827
Al Viro5d826c82014-03-14 13:42:45 -04004828int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
Al Viro5d826c82014-03-14 13:42:45 -04004830 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (IS_ERR(link))
4832 goto out;
4833
4834 len = strlen(link);
4835 if (len > (unsigned) buflen)
4836 len = buflen;
4837 if (copy_to_user(buffer, link, len))
4838 len = -EFAULT;
4839out:
4840 return len;
4841}
4842
Miklos Szeredid60874c2016-10-04 14:40:45 +02004843/**
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004844 * vfs_readlink - copy symlink body into userspace buffer
4845 * @dentry: dentry on which to get symbolic link
4846 * @buffer: user memory pointer
4847 * @buflen: size of buffer
4848 *
4849 * Does not touch atime. That's up to the caller if necessary
4850 *
4851 * Does not call security hook.
4852 */
4853int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4854{
4855 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04004856 DEFINE_DELAYED_CALL(done);
4857 const char *link;
4858 int res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004859
Miklos Szeredi76fca902016-12-09 16:45:04 +01004860 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4861 if (unlikely(inode->i_op->readlink))
4862 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004863
Miklos Szeredi76fca902016-12-09 16:45:04 +01004864 if (!d_is_symlink(dentry))
4865 return -EINVAL;
4866
4867 spin_lock(&inode->i_lock);
4868 inode->i_opflags |= IOP_DEFAULT_READLINK;
4869 spin_unlock(&inode->i_lock);
4870 }
4871
Eric Biggers4c4f7c12019-04-10 13:21:14 -07004872 link = READ_ONCE(inode->i_link);
Al Virof2df5da2018-07-19 17:35:51 -04004873 if (!link) {
4874 link = inode->i_op->get_link(dentry, inode, &done);
4875 if (IS_ERR(link))
4876 return PTR_ERR(link);
4877 }
4878 res = readlink_copy(buffer, buflen, link);
4879 do_delayed_call(&done);
4880 return res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004881}
4882EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Miklos Szeredid60874c2016-10-04 14:40:45 +02004884/**
4885 * vfs_get_link - get symlink body
4886 * @dentry: dentry on which to get symbolic link
4887 * @done: caller needs to free returned data with this
4888 *
4889 * Calls security hook and i_op->get_link() on the supplied inode.
4890 *
4891 * It does not touch atime. That's up to the caller if necessary.
4892 *
4893 * Does not work on "special" symlinks like /proc/$$/fd/N
4894 */
4895const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4896{
4897 const char *res = ERR_PTR(-EINVAL);
4898 struct inode *inode = d_inode(dentry);
4899
4900 if (d_is_symlink(dentry)) {
4901 res = ERR_PTR(security_inode_readlink(dentry));
4902 if (!res)
4903 res = inode->i_op->get_link(dentry, inode, done);
4904 }
4905 return res;
4906}
4907EXPORT_SYMBOL(vfs_get_link);
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004910const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004911 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004913 char *kaddr;
4914 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004915 struct address_space *mapping = inode->i_mapping;
4916
Al Virod3883d42015-11-17 10:41:04 -05004917 if (!dentry) {
4918 page = find_get_page(mapping, 0);
4919 if (!page)
4920 return ERR_PTR(-ECHILD);
4921 if (!PageUptodate(page)) {
4922 put_page(page);
4923 return ERR_PTR(-ECHILD);
4924 }
4925 } else {
4926 page = read_mapping_page(mapping, 0, NULL);
4927 if (IS_ERR(page))
4928 return (char*)page;
4929 }
Al Virofceef392015-12-29 15:58:39 -05004930 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004931 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4932 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004933 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004934 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
Al Viro6b255392015-11-17 10:20:54 -05004937EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Al Virofceef392015-12-29 15:58:39 -05004939void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
Al Virofceef392015-12-29 15:58:39 -05004941 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
Al Viro4d359502014-03-14 12:20:17 -04004943EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
Al Viroaa80dea2015-11-16 18:26:34 -05004945int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4946{
Al Virofceef392015-12-29 15:58:39 -05004947 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004948 int res = readlink_copy(buffer, buflen,
4949 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004950 &done));
4951 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004952 return res;
4953}
4954EXPORT_SYMBOL(page_readlink);
4955
Nick Piggin54566b22009-01-04 12:00:53 -08004956/*
4957 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4958 */
4959int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960{
4961 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004962 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004963 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004964 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07004965 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08004966 if (nofs)
4967 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
NeilBrown7e53cac2006-03-25 03:07:57 -08004969retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004970 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004971 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004973 goto fail;
4974
Al Viro21fc61c2015-11-17 01:07:57 -05004975 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004976
4977 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4978 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 if (err < 0)
4980 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004981 if (err < len-1)
4982 goto retry;
4983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 mark_inode_dirty(inode);
4985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986fail:
4987 return err;
4988}
Al Viro4d359502014-03-14 12:20:17 -04004989EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004991int page_symlink(struct inode *inode, const char *symname, int len)
4992{
4993 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004994 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004995}
Al Viro4d359502014-03-14 12:20:17 -04004996EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004997
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004998const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05004999 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001EXPORT_SYMBOL(page_symlink_inode_operations);