blob: 3f1829b3ab5b7cda8e2a48071efd6ac23cff4bac [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
Luis Chamberlain9c011be2022-01-21 22:13:13 -08001023static int sysctl_protected_symlinks __read_mostly;
1024static int sysctl_protected_hardlinks __read_mostly;
1025static int sysctl_protected_fifos __read_mostly;
1026static int sysctl_protected_regular __read_mostly;
1027
1028#ifdef CONFIG_SYSCTL
1029static struct ctl_table namei_sysctls[] = {
1030 {
1031 .procname = "protected_symlinks",
1032 .data = &sysctl_protected_symlinks,
1033 .maxlen = sizeof(int),
1034 .mode = 0600,
1035 .proc_handler = proc_dointvec_minmax,
1036 .extra1 = SYSCTL_ZERO,
1037 .extra2 = SYSCTL_ONE,
1038 },
1039 {
1040 .procname = "protected_hardlinks",
1041 .data = &sysctl_protected_hardlinks,
1042 .maxlen = sizeof(int),
1043 .mode = 0600,
1044 .proc_handler = proc_dointvec_minmax,
1045 .extra1 = SYSCTL_ZERO,
1046 .extra2 = SYSCTL_ONE,
1047 },
1048 {
1049 .procname = "protected_fifos",
1050 .data = &sysctl_protected_fifos,
1051 .maxlen = sizeof(int),
1052 .mode = 0600,
1053 .proc_handler = proc_dointvec_minmax,
1054 .extra1 = SYSCTL_ZERO,
1055 .extra2 = SYSCTL_TWO,
1056 },
1057 {
1058 .procname = "protected_regular",
1059 .data = &sysctl_protected_regular,
1060 .maxlen = sizeof(int),
1061 .mode = 0600,
1062 .proc_handler = proc_dointvec_minmax,
1063 .extra1 = SYSCTL_ZERO,
1064 .extra2 = SYSCTL_TWO,
1065 },
1066 { }
1067};
1068
1069static int __init init_fs_namei_sysctls(void)
1070{
1071 register_sysctl_init("fs", namei_sysctls);
1072 return 0;
1073}
1074fs_initcall(init_fs_namei_sysctls);
1075
1076#endif /* CONFIG_SYSCTL */
Kees Cook800179c2012-07-25 17:29:07 -07001077
1078/**
1079 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -07001080 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -07001081 *
1082 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1083 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1084 * in a sticky world-writable directory. This is to protect privileged
1085 * processes from failing races against path names that may change out
1086 * from under them by way of other users creating malicious symlinks.
1087 * It will permit symlinks to be followed only when outside a sticky
1088 * world-writable directory, or when the uid of the symlink and follower
1089 * match, or when the directory owner matches the symlink's owner.
1090 *
1091 * Returns 0 if following the symlink is allowed, -ve on error.
1092 */
Al Viroad6cc4c2020-01-14 14:41:39 -05001093static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001094{
Christian Braunerba73d982021-01-21 14:19:31 +01001095 struct user_namespace *mnt_userns;
1096 kuid_t i_uid;
1097
Kees Cook800179c2012-07-25 17:29:07 -07001098 if (!sysctl_protected_symlinks)
1099 return 0;
1100
Christian Braunerba73d982021-01-21 14:19:31 +01001101 mnt_userns = mnt_user_ns(nd->path.mnt);
1102 i_uid = i_uid_into_mnt(mnt_userns, inode);
Kees Cook800179c2012-07-25 17:29:07 -07001103 /* Allowed if owner and follower match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001104 if (uid_eq(current_cred()->fsuid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001105 return 0;
1106
1107 /* Allowed if parent directory not sticky and world-writable. */
Al Viro0f705952020-03-05 11:34:48 -05001108 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
Kees Cook800179c2012-07-25 17:29:07 -07001109 return 0;
1110
1111 /* Allowed if parent directory and link owner match. */
Christian Braunerba73d982021-01-21 14:19:31 +01001112 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001113 return 0;
1114
Al Viro319565022015-05-07 20:37:40 -04001115 if (nd->flags & LOOKUP_RCU)
1116 return -ECHILD;
1117
Richard Guy Briggsea841ba2018-03-21 04:42:21 -04001118 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Kees Cook245d7362019-10-02 16:41:58 -07001119 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
Kees Cook800179c2012-07-25 17:29:07 -07001120 return -EACCES;
1121}
1122
1123/**
1124 * safe_hardlink_source - Check for safe hardlink conditions
Christian Braunerba73d982021-01-21 14:19:31 +01001125 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001126 * @inode: the source inode to hardlink from
1127 *
1128 * Return false if at least one of the following conditions:
1129 * - inode is not a regular file
1130 * - inode is setuid
1131 * - inode is setgid and group-exec
1132 * - access failure for read and write
1133 *
1134 * Otherwise returns true.
1135 */
Christian Braunerba73d982021-01-21 14:19:31 +01001136static bool safe_hardlink_source(struct user_namespace *mnt_userns,
1137 struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001138{
1139 umode_t mode = inode->i_mode;
1140
1141 /* Special files should not get pinned to the filesystem. */
1142 if (!S_ISREG(mode))
1143 return false;
1144
1145 /* Setuid files should not get pinned to the filesystem. */
1146 if (mode & S_ISUID)
1147 return false;
1148
1149 /* Executable setgid files should not get pinned to the filesystem. */
1150 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1151 return false;
1152
1153 /* Hardlinking to unreadable or unwritable sources is dangerous. */
Christian Braunerba73d982021-01-21 14:19:31 +01001154 if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
Kees Cook800179c2012-07-25 17:29:07 -07001155 return false;
1156
1157 return true;
1158}
1159
1160/**
1161 * may_linkat - Check permissions for creating a hardlink
Christian Braunerba73d982021-01-21 14:19:31 +01001162 * @mnt_userns: user namespace of the mount the inode was found from
Kees Cook800179c2012-07-25 17:29:07 -07001163 * @link: the source to hardlink from
1164 *
1165 * Block hardlink when all of:
1166 * - sysctl_protected_hardlinks enabled
1167 * - fsuid does not match inode
1168 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001169 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -07001170 *
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 *
Kees Cook800179c2012-07-25 17:29:07 -07001177 * Returns 0 if successful, -ve on error.
1178 */
Christian Braunerba73d982021-01-21 14:19:31 +01001179int may_linkat(struct user_namespace *mnt_userns, struct path *link)
Kees Cook800179c2012-07-25 17:29:07 -07001180{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001181 struct inode *inode = link->dentry->d_inode;
1182
1183 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01001184 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
1185 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001186 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -07001187
1188 if (!sysctl_protected_hardlinks)
1189 return 0;
1190
Kees Cook800179c2012-07-25 17:29:07 -07001191 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1192 * otherwise, it must be a safe source.
1193 */
Christian Braunerba73d982021-01-21 14:19:31 +01001194 if (safe_hardlink_source(mnt_userns, inode) ||
1195 inode_owner_or_capable(mnt_userns, inode))
Kees Cook800179c2012-07-25 17:29:07 -07001196 return 0;
1197
Kees Cook245d7362019-10-02 16:41:58 -07001198 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001199 return -EPERM;
1200}
1201
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001202/**
1203 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1204 * should be allowed, or not, on files that already
1205 * exist.
Christian Braunerba73d982021-01-21 14:19:31 +01001206 * @mnt_userns: user namespace of the mount the inode was found from
Randy Dunlap2111c3c2021-02-15 20:29:28 -08001207 * @nd: nameidata pathwalk data
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001208 * @inode: the inode of the file to open
1209 *
1210 * Block an O_CREAT open of a FIFO (or a regular file) when:
1211 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1212 * - the file already exists
1213 * - we are in a sticky directory
1214 * - we don't own the file
1215 * - the owner of the directory doesn't own the file
1216 * - the directory is world writable
1217 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1218 * the directory doesn't have to be world writable: being group writable will
1219 * be enough.
1220 *
Christian Braunerba73d982021-01-21 14:19:31 +01001221 * If the inode has been found through an idmapped mount the user namespace of
1222 * the vfsmount must be passed through @mnt_userns. This function will then take
1223 * care to map the inode according to @mnt_userns before checking permissions.
1224 * On non-idmapped mounts or if permission checking is to be performed on the
1225 * raw inode simply passs init_user_ns.
1226 *
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001227 * Returns 0 if the open is allowed, -ve on error.
1228 */
Christian Braunerba73d982021-01-21 14:19:31 +01001229static int may_create_in_sticky(struct user_namespace *mnt_userns,
1230 struct nameidata *nd, struct inode *const inode)
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001231{
Christian Braunerba73d982021-01-21 14:19:31 +01001232 umode_t dir_mode = nd->dir_mode;
1233 kuid_t dir_uid = nd->dir_uid;
1234
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001235 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1236 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Virod0cb5012020-01-26 09:29:34 -05001237 likely(!(dir_mode & S_ISVTX)) ||
Christian Braunerba73d982021-01-21 14:19:31 +01001238 uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
1239 uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001240 return 0;
1241
Al Virod0cb5012020-01-26 09:29:34 -05001242 if (likely(dir_mode & 0002) ||
1243 (dir_mode & 0020 &&
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001244 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1245 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
Kees Cook245d7362019-10-02 16:41:58 -07001246 const char *operation = S_ISFIFO(inode->i_mode) ?
1247 "sticky_create_fifo" :
1248 "sticky_create_regular";
1249 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001250 return -EACCES;
1251 }
1252 return 0;
1253}
1254
David Howellsf015f1262012-06-25 12:55:28 +01001255/*
1256 * follow_up - Find the mountpoint of path's vfsmount
1257 *
1258 * Given a path, find the mountpoint of its source file system.
1259 * Replace @path with the path of the mountpoint in the parent mount.
1260 * Up is towards /.
1261 *
1262 * Return 1 if we went up a level and 0 if we were already at the
1263 * root.
1264 */
Al Virobab77eb2009-04-18 03:26:48 -04001265int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Al Viro0714a532011-11-24 22:19:58 -05001267 struct mount *mnt = real_mount(path->mnt);
1268 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001270
Al Viro48a066e2013-09-29 22:06:07 -04001271 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001272 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001273 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001274 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276 }
Al Viro0714a532011-11-24 22:19:58 -05001277 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001278 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001279 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001280 dput(path->dentry);
1281 path->dentry = mountpoint;
1282 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001283 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 1;
1285}
Al Viro4d359502014-03-14 12:20:17 -04001286EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Al Viro7ef482f2020-02-26 17:50:13 -05001288static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1289 struct path *path, unsigned *seqp)
1290{
1291 while (mnt_has_parent(m)) {
1292 struct dentry *mountpoint = m->mnt_mountpoint;
1293
1294 m = m->mnt_parent;
1295 if (unlikely(root->dentry == mountpoint &&
1296 root->mnt == &m->mnt))
1297 break;
1298 if (mountpoint != m->mnt.mnt_root) {
1299 path->mnt = &m->mnt;
1300 path->dentry = mountpoint;
1301 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1302 return true;
1303 }
1304 }
1305 return false;
1306}
1307
Al Viro2aa38472020-02-26 19:19:05 -05001308static bool choose_mountpoint(struct mount *m, const struct path *root,
1309 struct path *path)
1310{
1311 bool found;
1312
1313 rcu_read_lock();
1314 while (1) {
1315 unsigned seq, mseq = read_seqbegin(&mount_lock);
1316
1317 found = choose_mountpoint_rcu(m, root, path, &seq);
1318 if (unlikely(!found)) {
1319 if (!read_seqretry(&mount_lock, mseq))
1320 break;
1321 } else {
1322 if (likely(__legitimize_path(path, seq, mseq)))
1323 break;
1324 rcu_read_unlock();
1325 path_put(path);
1326 rcu_read_lock();
1327 }
1328 }
1329 rcu_read_unlock();
1330 return found;
1331}
1332
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001333/*
David Howells9875cf82011-01-14 18:45:21 +00001334 * Perform an automount
1335 * - return -EISDIR to tell follow_managed() to stop and return the path we
1336 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001338static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
Nick Piggin31e6b012011-01-07 17:49:52 +11001339{
Al Viro25e195a2020-01-11 11:27:46 -05001340 struct dentry *dentry = path->dentry;
Al Viro463ffb22005-06-06 13:36:05 -07001341
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001342 /* We don't want to mount if someone's just doing a stat -
1343 * unless they're stat'ing a directory and appended a '/' to
1344 * the name.
1345 *
1346 * We do, however, want to mount if someone wants to open or
1347 * create a file of any type under the mountpoint, wants to
1348 * traverse through the mountpoint or wants to open the
1349 * mounted directory. Also, autofs may mark negative dentries
1350 * as being automount points. These will need the attentions
1351 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001352 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001353 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001354 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Al Viro25e195a2020-01-11 11:27:46 -05001355 dentry->d_inode)
Ian Kent5d38f042017-11-29 16:11:26 -08001356 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001357
Al Viro1c9f5e02020-01-16 22:05:18 -05001358 if (count && (*count)++ >= MAXSYMLINKS)
David Howells9875cf82011-01-14 18:45:21 +00001359 return -ELOOP;
1360
Al Viro25e195a2020-01-11 11:27:46 -05001361 return finish_automount(dentry->d_op->d_automount(path), path);
David Howells9875cf82011-01-14 18:45:21 +00001362}
1363
1364/*
Al Viro9deed3e2020-01-17 08:45:08 -05001365 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1366 * dentries are pinned but not locked here, so negative dentry can go
1367 * positive right under us. Use of smp_load_acquire() provides a barrier
1368 * sufficient for ->d_inode and ->d_flags consistency.
David Howells9875cf82011-01-14 18:45:21 +00001369 */
Al Viro9deed3e2020-01-17 08:45:08 -05001370static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1371 int *count, unsigned lookup_flags)
David Howells9875cf82011-01-14 18:45:21 +00001372{
Al Viro9deed3e2020-01-17 08:45:08 -05001373 struct vfsmount *mnt = path->mnt;
David Howells9875cf82011-01-14 18:45:21 +00001374 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001375 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001376
Al Viro9deed3e2020-01-17 08:45:08 -05001377 while (flags & DCACHE_MANAGED_DENTRY) {
David Howellscc53ce52011-01-14 18:45:26 +00001378 /* Allow the filesystem to manage the transit without i_mutex
1379 * being held. */
Al Virod41efb52019-11-04 22:30:52 -05001380 if (flags & DCACHE_MANAGE_TRANSIT) {
Ian Kentfb5f51c2016-11-24 08:03:41 +11001381 ret = path->dentry->d_op->d_manage(path, false);
Al Viro508c8772020-01-14 22:09:57 -05001382 flags = smp_load_acquire(&path->dentry->d_flags);
David Howellscc53ce52011-01-14 18:45:26 +00001383 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001384 break;
David Howellscc53ce52011-01-14 18:45:26 +00001385 }
1386
Al Viro9deed3e2020-01-17 08:45:08 -05001387 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
David Howells9875cf82011-01-14 18:45:21 +00001388 struct vfsmount *mounted = lookup_mnt(path);
Al Viro9deed3e2020-01-17 08:45:08 -05001389 if (mounted) { // ... in our namespace
David Howells9875cf82011-01-14 18:45:21 +00001390 dput(path->dentry);
1391 if (need_mntput)
1392 mntput(path->mnt);
1393 path->mnt = mounted;
1394 path->dentry = dget(mounted->mnt_root);
Al Viro9deed3e2020-01-17 08:45:08 -05001395 // here we know it's positive
1396 flags = path->dentry->d_flags;
David Howells9875cf82011-01-14 18:45:21 +00001397 need_mntput = true;
1398 continue;
1399 }
David Howells9875cf82011-01-14 18:45:21 +00001400 }
1401
Al Viro9deed3e2020-01-17 08:45:08 -05001402 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1403 break;
David Howells9875cf82011-01-14 18:45:21 +00001404
Al Viro9deed3e2020-01-17 08:45:08 -05001405 // uncovered automount point
1406 ret = follow_automount(path, count, lookup_flags);
1407 flags = smp_load_acquire(&path->dentry->d_flags);
1408 if (ret < 0)
1409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Al Viro8aef1882011-06-16 15:10:06 +01001411
Al Viro9deed3e2020-01-17 08:45:08 -05001412 if (ret == -EISDIR)
1413 ret = 0;
1414 // possible if you race with several mount --move
1415 if (need_mntput && path->mnt == mnt)
1416 mntput(path->mnt);
1417 if (!ret && unlikely(d_flags_negative(flags)))
Al Virod41efb52019-11-04 22:30:52 -05001418 ret = -ENOENT;
Al Viro9deed3e2020-01-17 08:45:08 -05001419 *jumped = need_mntput;
Al Viro84027522015-04-22 10:30:08 -04001420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Al Viro9deed3e2020-01-17 08:45:08 -05001423static inline int traverse_mounts(struct path *path, bool *jumped,
1424 int *count, unsigned lookup_flags)
1425{
1426 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1427
1428 /* fastpath */
1429 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1430 *jumped = false;
1431 if (unlikely(d_flags_negative(flags)))
1432 return -ENOENT;
1433 return 0;
1434 }
1435 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1436}
1437
David Howellscc53ce52011-01-14 18:45:26 +00001438int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct vfsmount *mounted;
1441
Al Viro1c755af2009-04-18 14:06:57 -04001442 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001444 dput(path->dentry);
1445 mntput(path->mnt);
1446 path->mnt = mounted;
1447 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return 1;
1449 }
1450 return 0;
1451}
Al Viro4d359502014-03-14 12:20:17 -04001452EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
David Howells9875cf82011-01-14 18:45:21 +00001454/*
Al Viro9deed3e2020-01-17 08:45:08 -05001455 * Follow down to the covering mount currently visible to userspace. At each
1456 * point, the filesystem owning that dentry may be queried as to whether the
1457 * caller is permitted to proceed or not.
1458 */
1459int follow_down(struct path *path)
1460{
1461 struct vfsmount *mnt = path->mnt;
1462 bool jumped;
1463 int ret = traverse_mounts(path, &jumped, NULL, 0);
1464
1465 if (path->mnt != mnt)
1466 mntput(mnt);
1467 return ret;
1468}
1469EXPORT_SYMBOL(follow_down);
1470
1471/*
Al Viro287548e2011-05-27 06:50:06 -04001472 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1473 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001474 */
1475static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001476 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001477{
Al Viroea936ae2020-01-16 09:52:04 -05001478 struct dentry *dentry = path->dentry;
1479 unsigned int flags = dentry->d_flags;
1480
1481 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1482 return true;
1483
1484 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1485 return false;
1486
Ian Kent62a73752011-03-25 01:51:02 +08001487 for (;;) {
Ian Kent62a73752011-03-25 01:51:02 +08001488 /*
1489 * Don't forget we might have a non-mountpoint managed dentry
1490 * that wants to block transit.
1491 */
Al Viroea936ae2020-01-16 09:52:04 -05001492 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1493 int res = dentry->d_op->d_manage(path, true);
1494 if (res)
1495 return res == -EISDIR;
1496 flags = dentry->d_flags;
NeilBrownb8faf032014-08-04 17:06:29 +10001497 }
Ian Kent62a73752011-03-25 01:51:02 +08001498
Al Viroea936ae2020-01-16 09:52:04 -05001499 if (flags & DCACHE_MOUNTED) {
1500 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1501 if (mounted) {
1502 path->mnt = &mounted->mnt;
1503 dentry = path->dentry = mounted->mnt.mnt_root;
Al Virobcba1e72021-04-01 22:03:41 -04001504 nd->state |= ND_JUMPED;
Al Viroea936ae2020-01-16 09:52:04 -05001505 *seqp = read_seqcount_begin(&dentry->d_seq);
1506 *inode = dentry->d_inode;
1507 /*
1508 * We don't need to re-check ->d_seq after this
1509 * ->d_inode read - there will be an RCU delay
1510 * between mount hash removal and ->mnt_root
1511 * becoming unpinned.
1512 */
1513 flags = dentry->d_flags;
1514 continue;
1515 }
1516 if (read_seqretry(&mount_lock, nd->m_seq))
1517 return false;
1518 }
1519 return !(flags & DCACHE_NEED_AUTOMOUNT);
David Howells9875cf82011-01-14 18:45:21 +00001520 }
Al Viro287548e2011-05-27 06:50:06 -04001521}
1522
Al Virodb3c9ad2020-01-09 14:41:00 -05001523static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1524 struct path *path, struct inode **inode,
1525 unsigned int *seqp)
Al Virobd7c4b52020-01-08 20:37:23 -05001526{
Al Viro9deed3e2020-01-17 08:45:08 -05001527 bool jumped;
Al Virodb3c9ad2020-01-09 14:41:00 -05001528 int ret;
Al Virobd7c4b52020-01-08 20:37:23 -05001529
Al Virodb3c9ad2020-01-09 14:41:00 -05001530 path->mnt = nd->path.mnt;
1531 path->dentry = dentry;
Al Viroc1530072020-01-09 14:50:18 -05001532 if (nd->flags & LOOKUP_RCU) {
1533 unsigned int seq = *seqp;
1534 if (unlikely(!*inode))
1535 return -ENOENT;
1536 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viro9deed3e2020-01-17 08:45:08 -05001537 return 0;
Al Viroae66db42021-01-04 00:08:41 -05001538 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viroc1530072020-01-09 14:50:18 -05001539 return -ECHILD;
1540 // *path might've been clobbered by __follow_mount_rcu()
1541 path->mnt = nd->path.mnt;
1542 path->dentry = dentry;
1543 }
Al Viro9deed3e2020-01-17 08:45:08 -05001544 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1545 if (jumped) {
1546 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1547 ret = -EXDEV;
1548 else
Al Virobcba1e72021-04-01 22:03:41 -04001549 nd->state |= ND_JUMPED;
Al Viro9deed3e2020-01-17 08:45:08 -05001550 }
1551 if (unlikely(ret)) {
1552 dput(path->dentry);
1553 if (path->mnt != nd->path.mnt)
1554 mntput(path->mnt);
1555 } else {
Al Virobd7c4b52020-01-08 20:37:23 -05001556 *inode = d_backing_inode(path->dentry);
1557 *seqp = 0; /* out of RCU mode, so the value doesn't matter */
1558 }
1559 return ret;
1560}
1561
David Howells9875cf82011-01-14 18:45:21 +00001562/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001563 * This looks up the name in dcache and possibly revalidates the found dentry.
1564 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001565 */
Al Viroe3c13922016-03-06 14:03:27 -05001566static struct dentry *lookup_dcache(const struct qstr *name,
1567 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001568 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001569{
Al Viroa89f8332017-01-09 22:25:28 -05001570 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001571 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001572 int error = d_revalidate(dentry, flags);
1573 if (unlikely(error <= 0)) {
1574 if (!error)
1575 d_invalidate(dentry);
1576 dput(dentry);
1577 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001578 }
1579 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001580 return dentry;
1581}
1582
1583/*
Al Viroa03ece52018-03-08 11:00:45 -05001584 * Parent directory has inode locked exclusive. This is one
1585 * and only case when ->lookup() gets called on non in-lookup
1586 * dentries - as the matter of fact, this only gets called
1587 * when directory is guaranteed to have no in-lookup children
1588 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001589 */
Al Viroa03ece52018-03-08 11:00:45 -05001590static struct dentry *__lookup_hash(const struct qstr *name,
1591 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001592{
Al Viroa03ece52018-03-08 11:00:45 -05001593 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001594 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001595 struct inode *dir = base->d_inode;
1596
1597 if (dentry)
1598 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001599
1600 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001601 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001602 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001603
1604 dentry = d_alloc(base, name);
1605 if (unlikely(!dentry))
1606 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001607
Al Viro72bd8662012-06-10 17:17:17 -04001608 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001609 if (unlikely(old)) {
1610 dput(dentry);
1611 dentry = old;
1612 }
1613 return dentry;
1614}
1615
Al Viro20e34352020-01-09 14:58:31 -05001616static struct dentry *lookup_fast(struct nameidata *nd,
1617 struct inode **inode,
1618 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Nick Piggin31e6b012011-01-07 17:49:52 +11001620 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001621 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001622
Al Viro3cac2602009-08-13 18:27:43 +04001623 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001624 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001625 * of a false negative due to a concurrent rename, the caller is
1626 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001627 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001628 if (nd->flags & LOOKUP_RCU) {
1629 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001630 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001631 if (unlikely(!dentry)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001632 if (!try_to_unlazy(nd))
Al Viro20e34352020-01-09 14:58:31 -05001633 return ERR_PTR(-ECHILD);
1634 return NULL;
Al Viro5d0f49c2016-03-05 21:32:53 -05001635 }
Al Viro5a18fff2011-03-11 04:44:53 -05001636
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001637 /*
1638 * This sequence count validates that the inode matches
1639 * the dentry name information from lookup.
1640 */
David Howells63afdfc2015-05-06 15:59:00 +01001641 *inode = d_backing_inode(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001642 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Al Viro20e34352020-01-09 14:58:31 -05001643 return ERR_PTR(-ECHILD);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001644
1645 /*
1646 * This sequence count validates that the parent had no
1647 * changes while we did the lookup of the dentry above.
1648 *
1649 * The memory barrier in read_seqcount_begin of child is
1650 * enough, we can use __read_seqcount_retry here.
1651 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001652 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Al Viro20e34352020-01-09 14:58:31 -05001653 return ERR_PTR(-ECHILD);
Al Viro5a18fff2011-03-11 04:44:53 -05001654
Al Viro254cf582015-05-05 09:40:46 -04001655 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001656 status = d_revalidate(dentry, nd->flags);
Al Viroc1530072020-01-09 14:50:18 -05001657 if (likely(status > 0))
Al Viro20e34352020-01-09 14:58:31 -05001658 return dentry;
Al Viroae66db42021-01-04 00:08:41 -05001659 if (!try_to_unlazy_next(nd, dentry, seq))
Al Viro20e34352020-01-09 14:58:31 -05001660 return ERR_PTR(-ECHILD);
Steven Rostedt (VMware)26ddb452020-12-09 17:09:28 -05001661 if (status == -ECHILD)
Al Viro209a7fb2017-01-09 01:35:39 -05001662 /* we'd been told to redo it in non-rcu mode */
1663 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001664 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001665 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001666 if (unlikely(!dentry))
Al Viro20e34352020-01-09 14:58:31 -05001667 return NULL;
Al Viroa89f8332017-01-09 22:25:28 -05001668 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001669 }
Al Viro5a18fff2011-03-11 04:44:53 -05001670 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001671 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001672 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001673 dput(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001674 return ERR_PTR(status);
Al Viro5a18fff2011-03-11 04:44:53 -05001675 }
Al Viro20e34352020-01-09 14:58:31 -05001676 return dentry;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001677}
1678
1679/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001680static struct dentry *__lookup_slow(const struct qstr *name,
1681 struct dentry *dir,
1682 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001683{
Al Viro88d83312018-04-06 16:43:47 -04001684 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001685 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001686 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001687
Al Viro19363862016-04-14 19:33:34 -04001688 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001689 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001690 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001691again:
Al Virod9171b92016-04-15 03:33:13 -04001692 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001693 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001694 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001695 if (unlikely(!d_in_lookup(dentry))) {
Al Viroc64cd6e2020-01-10 17:17:19 -05001696 int error = d_revalidate(dentry, flags);
1697 if (unlikely(error <= 0)) {
1698 if (!error) {
1699 d_invalidate(dentry);
Al Viro949a8522016-03-06 14:20:52 -05001700 dput(dentry);
Al Viroc64cd6e2020-01-10 17:17:19 -05001701 goto again;
Al Viro949a8522016-03-06 14:20:52 -05001702 }
Al Viroc64cd6e2020-01-10 17:17:19 -05001703 dput(dentry);
1704 dentry = ERR_PTR(error);
Al Viro949a8522016-03-06 14:20:52 -05001705 }
Al Viro94bdd652016-04-15 02:42:04 -04001706 } else {
1707 old = inode->i_op->lookup(inode, dentry, flags);
1708 d_lookup_done(dentry);
1709 if (unlikely(old)) {
1710 dput(dentry);
1711 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001712 }
1713 }
Al Viroe3c13922016-03-06 14:03:27 -05001714 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Al Viro88d83312018-04-06 16:43:47 -04001717static struct dentry *lookup_slow(const struct qstr *name,
1718 struct dentry *dir,
1719 unsigned int flags)
1720{
1721 struct inode *inode = dir->d_inode;
1722 struct dentry *res;
1723 inode_lock_shared(inode);
1724 res = __lookup_slow(name, dir, flags);
1725 inode_unlock_shared(inode);
1726 return res;
1727}
1728
Christian Braunerba73d982021-01-21 14:19:31 +01001729static inline int may_lookup(struct user_namespace *mnt_userns,
1730 struct nameidata *nd)
Al Viro52094c82011-02-21 21:34:47 -05001731{
1732 if (nd->flags & LOOKUP_RCU) {
Linus Torvalds7d6beb72021-02-23 13:39:45 -08001733 int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Jens Axboee36cffe2020-12-17 09:19:08 -07001734 if (err != -ECHILD || !try_to_unlazy(nd))
Al Viro52094c82011-02-21 21:34:47 -05001735 return err;
Al Viro52094c82011-02-21 21:34:47 -05001736 }
Christian Braunerba73d982021-01-21 14:19:31 +01001737 return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001738}
1739
Al Viro49055902020-03-03 11:22:34 -05001740static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1741{
Al Viro49055902020-03-03 11:22:34 -05001742 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1743 return -ELOOP;
Al Viro45425762020-03-03 11:25:31 -05001744
1745 if (likely(nd->depth != EMBEDDED_LEVELS))
1746 return 0;
1747 if (likely(nd->stack != nd->internal))
1748 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001749 if (likely(nd_alloc_stack(nd)))
Al Viro49055902020-03-03 11:22:34 -05001750 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001751
1752 if (nd->flags & LOOKUP_RCU) {
1753 // we need to grab link before we do unlazy. And we can't skip
1754 // unlazy even if we fail to grab the link - cleanup needs it
Al Viro49055902020-03-03 11:22:34 -05001755 bool grabbed_link = legitimize_path(nd, link, seq);
Al Viro60ef60c2020-03-03 11:43:55 -05001756
Jens Axboee36cffe2020-12-17 09:19:08 -07001757 if (!try_to_unlazy(nd) != 0 || !grabbed_link)
Al Viro60ef60c2020-03-03 11:43:55 -05001758 return -ECHILD;
1759
1760 if (nd_alloc_stack(nd))
1761 return 0;
Al Viro49055902020-03-03 11:22:34 -05001762 }
Al Viro60ef60c2020-03-03 11:43:55 -05001763 return -ENOMEM;
Al Viro49055902020-03-03 11:22:34 -05001764}
1765
Al Virob1a81972020-01-19 12:48:44 -05001766enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1767
Al Viro06708ad2020-01-14 14:26:57 -05001768static const char *pick_link(struct nameidata *nd, struct path *link,
Al Virob1a81972020-01-19 12:48:44 -05001769 struct inode *inode, unsigned seq, int flags)
Al Virod63ff282015-05-04 18:13:23 -04001770{
Al Viro1cf26652015-05-06 16:01:56 -04001771 struct saved *last;
Al Viroad6cc4c2020-01-14 14:41:39 -05001772 const char *res;
Al Viro49055902020-03-03 11:22:34 -05001773 int error = reserve_stack(nd, link, seq);
Al Viroad6cc4c2020-01-14 14:41:39 -05001774
Al Viro49055902020-03-03 11:22:34 -05001775 if (unlikely(error)) {
Al Viro84f0cd92020-03-03 10:14:30 -05001776 if (!(nd->flags & LOOKUP_RCU))
1777 path_put(link);
Al Viro49055902020-03-03 11:22:34 -05001778 return ERR_PTR(error);
Al Viro626de992015-05-04 18:26:59 -04001779 }
Al Viroab104922015-05-10 11:50:01 -04001780 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001781 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001782 clear_delayed_call(&last->done);
Al Viro0450b2d2015-05-08 13:23:53 -04001783 last->seq = seq;
Al Viroad6cc4c2020-01-14 14:41:39 -05001784
Al Virob1a81972020-01-19 12:48:44 -05001785 if (flags & WALK_TRAILING) {
Al Viroad6cc4c2020-01-14 14:41:39 -05001786 error = may_follow_link(nd, inode);
1787 if (unlikely(error))
1788 return ERR_PTR(error);
1789 }
1790
Mattias Nisslerdab741e02020-08-27 11:09:46 -06001791 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1792 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
Al Viroad6cc4c2020-01-14 14:41:39 -05001793 return ERR_PTR(-ELOOP);
1794
1795 if (!(nd->flags & LOOKUP_RCU)) {
1796 touch_atime(&last->link);
1797 cond_resched();
1798 } else if (atime_needs_update(&last->link, inode)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07001799 if (!try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001800 return ERR_PTR(-ECHILD);
1801 touch_atime(&last->link);
1802 }
1803
1804 error = security_inode_follow_link(link->dentry, inode,
1805 nd->flags & LOOKUP_RCU);
1806 if (unlikely(error))
1807 return ERR_PTR(error);
1808
Al Viroad6cc4c2020-01-14 14:41:39 -05001809 res = READ_ONCE(inode->i_link);
1810 if (!res) {
1811 const char * (*get)(struct dentry *, struct inode *,
1812 struct delayed_call *);
1813 get = inode->i_op->get_link;
1814 if (nd->flags & LOOKUP_RCU) {
1815 res = get(NULL, inode, &last->done);
Jens Axboee36cffe2020-12-17 09:19:08 -07001816 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001817 res = get(link->dentry, inode, &last->done);
Al Viroad6cc4c2020-01-14 14:41:39 -05001818 } else {
1819 res = get(link->dentry, inode, &last->done);
1820 }
1821 if (!res)
1822 goto all_done;
1823 if (IS_ERR(res))
1824 return res;
1825 }
1826 if (*res == '/') {
1827 error = nd_jump_root(nd);
1828 if (unlikely(error))
1829 return ERR_PTR(error);
1830 while (unlikely(*++res == '/'))
1831 ;
1832 }
1833 if (*res)
1834 return res;
1835all_done: // pure jump
1836 put_link(nd);
1837 return NULL;
Al Virod63ff282015-05-04 18:13:23 -04001838}
1839
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001840/*
1841 * Do we need to follow links? We _really_ want to be able
1842 * to do this check without having to look at inode->i_op,
1843 * so we keep a cache of "no, this doesn't need follow_link"
1844 * for the common case.
1845 */
Al Virob0417d22020-01-14 13:34:20 -05001846static const char *step_into(struct nameidata *nd, int flags,
Al Virocbae4d12020-01-12 13:40:02 -05001847 struct dentry *dentry, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001848{
Al Virocbae4d12020-01-12 13:40:02 -05001849 struct path path;
1850 int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1851
1852 if (err < 0)
Al Virob0417d22020-01-14 13:34:20 -05001853 return ERR_PTR(err);
Al Virocbae4d12020-01-12 13:40:02 -05001854 if (likely(!d_is_symlink(path.dentry)) ||
Al Viro8c4efe22020-01-19 12:44:18 -05001855 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
Al Viroaca29032020-01-09 15:17:57 -05001856 (flags & WALK_NOFOLLOW)) {
Al Viro8f64fb12016-11-14 01:50:26 -05001857 /* not a symlink or should not follow */
Al Viroc99687a2020-03-03 10:56:17 -05001858 if (!(nd->flags & LOOKUP_RCU)) {
1859 dput(nd->path.dentry);
1860 if (nd->path.mnt != path.mnt)
1861 mntput(nd->path.mnt);
1862 }
1863 nd->path = path;
Al Viro8f64fb12016-11-14 01:50:26 -05001864 nd->inode = inode;
1865 nd->seq = seq;
Al Virob0417d22020-01-14 13:34:20 -05001866 return NULL;
Al Viro8f64fb12016-11-14 01:50:26 -05001867 }
Al Viroa7f77542016-02-27 19:31:01 -05001868 if (nd->flags & LOOKUP_RCU) {
Al Viro84f0cd92020-03-03 10:14:30 -05001869 /* make sure that d_is_symlink above matches inode */
Al Virocbae4d12020-01-12 13:40:02 -05001870 if (read_seqcount_retry(&path.dentry->d_seq, seq))
Al Virob0417d22020-01-14 13:34:20 -05001871 return ERR_PTR(-ECHILD);
Al Viro84f0cd92020-03-03 10:14:30 -05001872 } else {
1873 if (path.mnt == nd->path.mnt)
1874 mntget(path.mnt);
Al Viroa7f77542016-02-27 19:31:01 -05001875 }
Al Virob1a81972020-01-19 12:48:44 -05001876 return pick_link(nd, &path, inode, seq, flags);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001877}
1878
Al Viroc2df1962020-02-26 14:33:30 -05001879static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1880 struct inode **inodep,
1881 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001882{
Al Viro12487f32020-02-26 14:59:56 -05001883 struct dentry *parent, *old;
Al Viro957dd412020-02-26 01:40:04 -05001884
Al Viro12487f32020-02-26 14:59:56 -05001885 if (path_equal(&nd->path, &nd->root))
1886 goto in_root;
1887 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro7ef482f2020-02-26 17:50:13 -05001888 struct path path;
Al Viroefe772d2020-02-28 10:06:37 -05001889 unsigned seq;
Al Viro7ef482f2020-02-26 17:50:13 -05001890 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1891 &nd->root, &path, &seq))
1892 goto in_root;
Al Viroefe772d2020-02-28 10:06:37 -05001893 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1894 return ERR_PTR(-ECHILD);
1895 nd->path = path;
1896 nd->inode = path.dentry->d_inode;
1897 nd->seq = seq;
1898 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1899 return ERR_PTR(-ECHILD);
1900 /* we know that mountpoint was pinned */
Al Viro957dd412020-02-26 01:40:04 -05001901 }
Al Viro12487f32020-02-26 14:59:56 -05001902 old = nd->path.dentry;
1903 parent = old->d_parent;
1904 *inodep = parent->d_inode;
1905 *seqp = read_seqcount_begin(&parent->d_seq);
1906 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1907 return ERR_PTR(-ECHILD);
1908 if (unlikely(!path_connected(nd->path.mnt, parent)))
1909 return ERR_PTR(-ECHILD);
1910 return parent;
1911in_root:
Al Viroefe772d2020-02-28 10:06:37 -05001912 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1913 return ERR_PTR(-ECHILD);
Al Viroc2df1962020-02-26 14:33:30 -05001914 if (unlikely(nd->flags & LOOKUP_BENEATH))
1915 return ERR_PTR(-ECHILD);
1916 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001917}
1918
Al Viroc2df1962020-02-26 14:33:30 -05001919static struct dentry *follow_dotdot(struct nameidata *nd,
1920 struct inode **inodep,
1921 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001922{
Al Viro12487f32020-02-26 14:59:56 -05001923 struct dentry *parent;
1924
1925 if (path_equal(&nd->path, &nd->root))
1926 goto in_root;
1927 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro2aa38472020-02-26 19:19:05 -05001928 struct path path;
1929
1930 if (!choose_mountpoint(real_mount(nd->path.mnt),
1931 &nd->root, &path))
1932 goto in_root;
Al Viro165200d2020-02-28 10:17:52 -05001933 path_put(&nd->path);
1934 nd->path = path;
Al Viro2aa38472020-02-26 19:19:05 -05001935 nd->inode = path.dentry->d_inode;
Al Viro165200d2020-02-28 10:17:52 -05001936 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1937 return ERR_PTR(-EXDEV);
Al Viro957dd412020-02-26 01:40:04 -05001938 }
Al Viro12487f32020-02-26 14:59:56 -05001939 /* rare case of legitimate dget_parent()... */
1940 parent = dget_parent(nd->path.dentry);
1941 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1942 dput(parent);
1943 return ERR_PTR(-ENOENT);
1944 }
1945 *seqp = 0;
1946 *inodep = parent->d_inode;
1947 return parent;
1948
1949in_root:
Al Viroc2df1962020-02-26 14:33:30 -05001950 if (unlikely(nd->flags & LOOKUP_BENEATH))
1951 return ERR_PTR(-EXDEV);
1952 dget(nd->path.dentry);
1953 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001954}
1955
Al Viro7521f222020-02-26 12:22:58 -05001956static const char *handle_dots(struct nameidata *nd, int type)
Al Viro957dd412020-02-26 01:40:04 -05001957{
1958 if (type == LAST_DOTDOT) {
Al Viro7521f222020-02-26 12:22:58 -05001959 const char *error = NULL;
Al Viroc2df1962020-02-26 14:33:30 -05001960 struct dentry *parent;
1961 struct inode *inode;
1962 unsigned seq;
Al Viro957dd412020-02-26 01:40:04 -05001963
1964 if (!nd->root.mnt) {
Al Viro7521f222020-02-26 12:22:58 -05001965 error = ERR_PTR(set_root(nd));
Al Viro957dd412020-02-26 01:40:04 -05001966 if (error)
1967 return error;
1968 }
1969 if (nd->flags & LOOKUP_RCU)
Al Viroc2df1962020-02-26 14:33:30 -05001970 parent = follow_dotdot_rcu(nd, &inode, &seq);
Al Viro957dd412020-02-26 01:40:04 -05001971 else
Al Viroc2df1962020-02-26 14:33:30 -05001972 parent = follow_dotdot(nd, &inode, &seq);
1973 if (IS_ERR(parent))
1974 return ERR_CAST(parent);
1975 if (unlikely(!parent))
1976 error = step_into(nd, WALK_NOFOLLOW,
1977 nd->path.dentry, nd->inode, nd->seq);
1978 else
1979 error = step_into(nd, WALK_NOFOLLOW,
1980 parent, inode, seq);
1981 if (unlikely(error))
Al Viro957dd412020-02-26 01:40:04 -05001982 return error;
1983
1984 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1985 /*
1986 * If there was a racing rename or mount along our
1987 * path, then we can't be sure that ".." hasn't jumped
1988 * above nd->root (and so userspace should retry or use
1989 * some fallback).
1990 */
1991 smp_rmb();
1992 if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001993 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001994 if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001995 return ERR_PTR(-EAGAIN);
Al Viro957dd412020-02-26 01:40:04 -05001996 }
1997 }
Al Viro7521f222020-02-26 12:22:58 -05001998 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001999}
2000
Al Viro92d27012020-01-14 13:24:17 -05002001static const char *walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04002002{
Al Virodb3c9ad2020-01-09 14:41:00 -05002003 struct dentry *dentry;
Al Viroce57dfc2011-03-13 19:58:58 -04002004 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04002005 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04002006 /*
2007 * "." and ".." are special - ".." especially so because it has
2008 * to be able to know about the current root directory and
2009 * parent relationships.
2010 */
Al Viro4693a542015-05-04 17:47:11 -04002011 if (unlikely(nd->last_type != LAST_NORM)) {
Al Viro1c4ff1a2016-11-14 01:39:36 -05002012 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04002013 put_link(nd);
Al Viro7521f222020-02-26 12:22:58 -05002014 return handle_dots(nd, nd->last_type);
Al Viro4693a542015-05-04 17:47:11 -04002015 }
Al Viro20e34352020-01-09 14:58:31 -05002016 dentry = lookup_fast(nd, &inode, &seq);
2017 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05002018 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05002019 if (unlikely(!dentry)) {
Al Virodb3c9ad2020-01-09 14:41:00 -05002020 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
2021 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05002022 return ERR_CAST(dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04002023 }
Al Viro56676ec2020-03-10 21:54:54 -04002024 if (!(flags & WALK_MORE) && nd->depth)
2025 put_link(nd);
Al Virob0417d22020-01-14 13:34:20 -05002026 return step_into(nd, flags, dentry, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04002027}
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002030 * We can do the critical dentry name comparison and hashing
2031 * operations one word at a time, but we are limited to:
2032 *
2033 * - Architectures with fast unaligned word accesses. We could
2034 * do a "get_unaligned()" if this helps and is sufficiently
2035 * fast.
2036 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002037 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
2038 * do not trap on the (extremely unlikely) case of a page
2039 * crossing operation.
2040 *
2041 * - Furthermore, we need an efficient 64-bit compile for the
2042 * 64-bit case in order to generate the "number of bytes in
2043 * the final mask". Again, that could be replaced with a
2044 * efficient population count instruction or similar.
2045 */
2046#ifdef CONFIG_DCACHE_WORD_ACCESS
2047
Linus Torvaldsf68e5562012-04-06 13:54:56 -07002048#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002049
George Spelvin468a9422016-05-26 22:11:51 -04002050#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002051
George Spelvin468a9422016-05-26 22:11:51 -04002052/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
2053
2054#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04002055/*
2056 * Register pressure in the mixing function is an issue, particularly
2057 * on 32-bit x86, but almost any function requires one state value and
2058 * one temporary. Instead, use a function designed for two state values
2059 * and no temporaries.
2060 *
2061 * This function cannot create a collision in only two iterations, so
2062 * we have two iterations to achieve avalanche. In those two iterations,
2063 * we have six layers of mixing, which is enough to spread one bit's
2064 * influence out to 2^6 = 64 state bits.
2065 *
2066 * Rotate constants are scored by considering either 64 one-bit input
2067 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2068 * probability of that delta causing a change to each of the 128 output
2069 * bits, using a sample of random initial states.
2070 *
2071 * The Shannon entropy of the computed probabilities is then summed
2072 * to produce a score. Ideally, any input change has a 50% chance of
2073 * toggling any given output bit.
2074 *
2075 * Mixing scores (in bits) for (12,45):
2076 * Input delta: 1-bit 2-bit
2077 * 1 round: 713.3 42542.6
2078 * 2 rounds: 2753.7 140389.8
2079 * 3 rounds: 5954.1 233458.2
2080 * 4 rounds: 7862.6 256672.2
2081 * Perfect: 8192 258048
2082 * (64*128) (64*63/2 * 128)
2083 */
2084#define HASH_MIX(x, y, a) \
2085 ( x ^= (a), \
2086 y ^= x, x = rol64(x,12),\
2087 x += y, y = rol64(y,45),\
2088 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002089
George Spelvin0fed3ac2016-05-02 06:31:01 -04002090/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04002091 * Fold two longs into one 32-bit hash value. This must be fast, but
2092 * latency isn't quite as critical, as there is a fair bit of additional
2093 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04002094 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04002095static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002096{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002097 y ^= x * GOLDEN_RATIO_64;
2098 y *= GOLDEN_RATIO_64;
2099 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04002100}
2101
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002102#else /* 32-bit case */
2103
George Spelvin2a18da7a2016-05-23 07:43:58 -04002104/*
2105 * Mixing scores (in bits) for (7,20):
2106 * Input delta: 1-bit 2-bit
2107 * 1 round: 330.3 9201.6
2108 * 2 rounds: 1246.4 25475.4
2109 * 3 rounds: 1907.1 31295.1
2110 * 4 rounds: 2042.3 31718.6
2111 * Perfect: 2048 31744
2112 * (32*64) (32*31/2 * 64)
2113 */
2114#define HASH_MIX(x, y, a) \
2115 ( x ^= (a), \
2116 y ^= x, x = rol32(x, 7),\
2117 x += y, y = rol32(y,20),\
2118 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002119
George Spelvin2a18da7a2016-05-23 07:43:58 -04002120static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002121{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002122 /* Use arch-optimized multiply if one exists */
2123 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04002124}
2125
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002126#endif
2127
George Spelvin2a18da7a2016-05-23 07:43:58 -04002128/*
2129 * Return the hash of a string of known length. This is carfully
2130 * designed to match hash_name(), which is the more critical function.
2131 * In particular, we must end by hashing a final word containing 0..7
2132 * payload bytes, to match the way that hash_name() iterates until it
2133 * finds the delimiter after the name.
2134 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002135unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002136{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002137 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002138
2139 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002140 if (!len)
2141 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002142 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002143 if (len < sizeof(unsigned long))
2144 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04002145 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002146 name += sizeof(unsigned long);
2147 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002148 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04002149 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002150done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04002151 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002152}
2153EXPORT_SYMBOL(full_name_hash);
2154
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002155/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002156u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002157{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002158 unsigned long a = 0, x = 0, y = (unsigned long)salt;
2159 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002160 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2161
Linus Torvalds8387ff22016-06-10 07:51:30 -07002162 len = 0;
2163 goto inside;
2164
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002165 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002166 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002167 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002168inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002169 a = load_unaligned_zeropad(name+len);
2170 } while (!has_zero(a, &adata, &constants));
2171
2172 adata = prep_zero_mask(a, adata, &constants);
2173 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002174 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002175
George Spelvin2a18da7a2016-05-23 07:43:58 -04002176 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002177}
2178EXPORT_SYMBOL(hashlen_string);
2179
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002180/*
2181 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002182 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002183 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002184static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002185{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002186 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2187 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07002188 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002189
Linus Torvalds8387ff22016-06-10 07:51:30 -07002190 len = 0;
2191 goto inside;
2192
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002193 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002194 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002195 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002196inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002197 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002198 b = a ^ REPEAT_BYTE('/');
2199 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002200
Linus Torvalds36126f82012-05-26 10:43:17 -07002201 adata = prep_zero_mask(a, adata, &constants);
2202 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002203 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002204 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002205
George Spelvin2a18da7a2016-05-23 07:43:58 -04002206 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002207}
2208
George Spelvin2a18da7a2016-05-23 07:43:58 -04002209#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002210
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002211/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002212unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002213{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002214 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002215 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002216 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002217 return end_name_hash(hash);
2218}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002219EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002220
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002221/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002222u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002223{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002224 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002225 unsigned long len = 0, c;
2226
2227 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002228 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002229 len++;
2230 hash = partial_name_hash(c, hash);
2231 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002232 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002233 return hashlen_create(end_name_hash(hash), len);
2234}
George Spelvinf2a031b2016-05-29 01:26:41 -04002235EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002236
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002237/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002238 * We know there's a real path component here of at least
2239 * one character.
2240 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002241static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002242{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002243 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002244 unsigned long len = 0, c;
2245
2246 c = (unsigned char)*name;
2247 do {
2248 len++;
2249 hash = partial_name_hash(c, hash);
2250 c = (unsigned char)name[len];
2251 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002252 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002253}
2254
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002255#endif
2256
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002259 * This is the basic name resolution function, turning a pathname into
2260 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002262 * Returns 0 and nd will have valid dentry and mnt on success.
2263 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 */
Al Viro6de88d72009-08-09 01:41:57 +04002265static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Al Virod8d46112020-02-23 22:04:15 -05002267 int depth = 0; // depth <= nd->depth
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002269
Al Virob4c03532020-01-19 11:44:51 -05002270 nd->last_type = LAST_ROOT;
Al Viroc1088372020-03-05 15:48:44 -05002271 nd->flags |= LOOKUP_PARENT;
Al Viro9b5858e2018-07-09 16:33:23 -04002272 if (IS_ERR(name))
2273 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 while (*name=='/')
2275 name++;
Al Viro1a97d892020-09-19 17:55:58 +01002276 if (!*name) {
2277 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
Al Viro9e18f102015-04-18 20:44:34 -04002278 return 0;
Al Viro1a97d892020-09-19 17:55:58 +01002279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* At this point we know we have a real path component. */
2282 for(;;) {
Christian Brauner549c7292021-01-21 14:19:43 +01002283 struct user_namespace *mnt_userns;
Al Viro92d27012020-01-14 13:24:17 -05002284 const char *link;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002285 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002286 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Christian Brauner549c7292021-01-21 14:19:43 +01002288 mnt_userns = mnt_user_ns(nd->path.mnt);
2289 err = may_lookup(mnt_userns, nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002290 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Linus Torvalds8387ff22016-06-10 07:51:30 -07002293 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Al Virofe479a52011-02-22 15:10:03 -05002295 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002296 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002297 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002298 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002299 type = LAST_DOTDOT;
Al Virobcba1e72021-04-01 22:03:41 -04002300 nd->state |= ND_JUMPED;
Al Viro16c2cd72011-02-22 15:50:10 -05002301 }
Al Virofe479a52011-02-22 15:10:03 -05002302 break;
2303 case 1:
2304 type = LAST_DOT;
2305 }
Al Viro5a202bc2011-03-08 14:17:44 -05002306 if (likely(type == LAST_NORM)) {
2307 struct dentry *parent = nd->path.dentry;
Al Virobcba1e72021-04-01 22:03:41 -04002308 nd->state &= ~ND_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002309 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002310 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002311 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002312 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002313 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002314 hash_len = this.hash_len;
2315 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002316 }
2317 }
Al Virofe479a52011-02-22 15:10:03 -05002318
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002319 nd->last.hash_len = hash_len;
2320 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002321 nd->last_type = type;
2322
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002323 name += hashlen_len(hash_len);
2324 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002325 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002326 /*
2327 * If it wasn't NUL, we know it was '/'. Skip that
2328 * slash, and continue until no more slashes.
2329 */
2330 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002331 name++;
2332 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002333 if (unlikely(!*name)) {
2334OK:
Al Virod8d46112020-02-23 22:04:15 -05002335 /* pathname or trailing symlink, done */
Al Viroc1088372020-03-05 15:48:44 -05002336 if (!depth) {
Christian Brauner549c7292021-01-21 14:19:43 +01002337 nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode);
Al Viro0f705952020-03-05 11:34:48 -05002338 nd->dir_mode = nd->inode->i_mode;
Al Viroc1088372020-03-05 15:48:44 -05002339 nd->flags &= ~LOOKUP_PARENT;
Al Viro8620c232015-05-04 08:58:35 -04002340 return 0;
Al Viroc1088372020-03-05 15:48:44 -05002341 }
Al Viro8620c232015-05-04 08:58:35 -04002342 /* last component of nested symlink */
Al Virod8d46112020-02-23 22:04:15 -05002343 name = nd->stack[--depth].name;
Al Viro8c4efe22020-01-19 12:44:18 -05002344 link = walk_component(nd, 0);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002345 } else {
2346 /* not the last component */
Al Viro8c4efe22020-01-19 12:44:18 -05002347 link = walk_component(nd, WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002348 }
Al Viro92d27012020-01-14 13:24:17 -05002349 if (unlikely(link)) {
2350 if (IS_ERR(link))
2351 return PTR_ERR(link);
2352 /* a symlink to follow */
Al Virod8d46112020-02-23 22:04:15 -05002353 nd->stack[depth++].name = name;
Al Viro92d27012020-01-14 13:24:17 -05002354 name = link;
2355 continue;
Nick Piggin31e6b012011-01-07 17:49:52 +11002356 }
Al Viro97242f92015-08-01 19:59:28 -04002357 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2358 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07002359 if (!try_to_unlazy(nd))
Al Viro97242f92015-08-01 19:59:28 -04002360 return -ECHILD;
2361 }
Al Viro3595e232015-05-09 16:54:45 -04002362 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Al Viroedc2b1d2018-07-09 16:27:23 -04002367/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002368static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Aleksa Sarai740a1672019-12-07 01:13:29 +11002370 int error;
Al Viroc8a53ee2015-05-12 18:43:07 -04002371 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Jens Axboe6c6ec2b2020-12-17 09:19:09 -07002373 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2374 if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
2375 return ERR_PTR(-EAGAIN);
2376
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002377 if (!*s)
2378 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002379 if (flags & LOOKUP_RCU)
2380 rcu_read_lock();
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002381
Al Virobcba1e72021-04-01 22:03:41 -04002382 nd->flags = flags;
2383 nd->state |= ND_JUMPED;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002384
2385 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2386 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2387 smp_rmb();
2388
Al Virobcba1e72021-04-01 22:03:41 -04002389 if (nd->state & ND_ROOT_PRESET) {
David Howellsb18825a2013-09-12 19:22:53 +01002390 struct dentry *root = nd->root.dentry;
2391 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002392 if (*s && unlikely(!d_can_lookup(root)))
2393 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002394 nd->path = nd->root;
2395 nd->inode = inode;
2396 if (flags & LOOKUP_RCU) {
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002397 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002398 nd->root_seq = nd->seq;
Al Viro5b6ca022011-03-09 23:04:47 -05002399 } else {
2400 path_get(&nd->path);
2401 }
Al Viro368ee9b2015-05-08 17:19:59 -04002402 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002403 }
2404
Al Viro2a737872009-04-07 11:49:53 -04002405 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002407 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2408 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
Aleksa Sarai740a1672019-12-07 01:13:29 +11002409 error = nd_jump_root(nd);
2410 if (unlikely(error))
2411 return ERR_PTR(error);
2412 return s;
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002413 }
2414
2415 /* Relative pathname -- get the starting-point it is relative to. */
2416 if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002417 if (flags & LOOKUP_RCU) {
2418 struct fs_struct *fs = current->fs;
2419 unsigned seq;
2420
Al Viroe41f7d42011-02-22 14:02:58 -05002421 do {
2422 seq = read_seqcount_begin(&fs->seq);
2423 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002424 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002425 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2426 } while (read_seqcount_retry(&fs->seq, seq));
2427 } else {
2428 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002429 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002430 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002431 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002432 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002433 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002434 struct dentry *dentry;
2435
Al Viro2903ff02012-08-28 12:52:22 -04002436 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002437 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002438
Al Viro2903ff02012-08-28 12:52:22 -04002439 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002440
Al Viroedc2b1d2018-07-09 16:27:23 -04002441 if (*s && unlikely(!d_can_lookup(dentry))) {
2442 fdput(f);
2443 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002444 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002445
Al Viro2903ff02012-08-28 12:52:22 -04002446 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002447 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002448 nd->inode = nd->path.dentry->d_inode;
2449 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002450 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002451 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002452 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002453 }
Al Viro34a26b92015-05-11 08:05:05 -04002454 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002456
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002457 /* For scoped-lookups we need to set the root to the dirfd as well. */
2458 if (flags & LOOKUP_IS_SCOPED) {
2459 nd->root = nd->path;
2460 if (flags & LOOKUP_RCU) {
2461 nd->root_seq = nd->seq;
2462 } else {
2463 path_get(&nd->root);
Al Virobcba1e72021-04-01 22:03:41 -04002464 nd->state |= ND_ROOT_GRABBED;
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002465 }
2466 }
2467 return s;
Al Viro9b4a9b12009-04-07 11:44:16 -04002468}
2469
Al Viro1ccac622020-01-14 10:13:40 -05002470static inline const char *lookup_last(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002471{
Al Virobd92d7f2011-03-14 19:54:59 -04002472 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2473 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2474
Al Viroc1088372020-03-05 15:48:44 -05002475 return walk_component(nd, WALK_TRAILING);
Al Virobd92d7f2011-03-14 19:54:59 -04002476}
2477
Al Viro4f757f32017-04-15 17:31:22 -04002478static int handle_lookup_down(struct nameidata *nd)
2479{
Al Viroc1530072020-01-09 14:50:18 -05002480 if (!(nd->flags & LOOKUP_RCU))
Al Virodb3c9ad2020-01-09 14:41:00 -05002481 dget(nd->path.dentry);
Al Virob0417d22020-01-14 13:34:20 -05002482 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2483 nd->path.dentry, nd->inode, nd->seq));
Al Viro4f757f32017-04-15 17:31:22 -04002484}
2485
Al Viro9b4a9b12009-04-07 11:44:16 -04002486/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002487static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002488{
Al Viroc8a53ee2015-05-12 18:43:07 -04002489 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002490 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002491
Al Viro9b5858e2018-07-09 16:33:23 -04002492 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002493 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002494 if (unlikely(err < 0))
2495 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002496 }
2497
Al Viro1ccac622020-01-14 10:13:40 -05002498 while (!(err = link_path_walk(s, nd)) &&
2499 (s = lookup_last(nd)) != NULL)
2500 ;
Al Viro4f0ed932021-04-06 19:46:51 -04002501 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2502 err = handle_lookup_down(nd);
Al Virobcba1e72021-04-01 22:03:41 -04002503 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please...
Al Viro4f0ed932021-04-06 19:46:51 -04002504 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002505 if (!err)
2506 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002507
Al Virodeb106c2015-05-08 18:05:21 -04002508 if (!err && nd->flags & LOOKUP_DIRECTORY)
2509 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002510 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002511 if (!err) {
2512 *path = nd->path;
2513 nd->path.mnt = NULL;
2514 nd->path.dentry = NULL;
2515 }
2516 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002517 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002518}
Nick Piggin31e6b012011-01-07 17:49:52 +11002519
Stephen Brennan794ebce2021-09-01 10:51:42 -07002520int filename_lookup(int dfd, struct filename *name, unsigned flags,
David Howells31d921c2018-11-01 23:07:24 +00002521 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002522{
Al Viro894bc8c2015-05-02 07:16:16 -04002523 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002524 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002525 if (IS_ERR(name))
2526 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002527 set_nameidata(&nd, dfd, name, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04002528 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002529 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002530 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002531 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002532 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002533
2534 if (likely(!retval))
Al Viro161aff12020-01-11 22:52:26 -05002535 audit_inode(name, path->dentry,
2536 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
Al Viro9883d182015-05-13 07:28:08 -04002537 restore_nameidata();
Dmitry Kadashev020250f2021-07-08 13:34:43 +07002538 return retval;
2539}
2540
Al Viro8bcb77f2015-05-08 16:59:20 -04002541/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002542static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002543 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002544{
Al Viroc8a53ee2015-05-12 18:43:07 -04002545 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002546 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002547 if (!err)
2548 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002549 if (!err) {
2550 *parent = nd->path;
2551 nd->path.mnt = NULL;
2552 nd->path.dentry = NULL;
2553 }
2554 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002555 return err;
2556}
2557
Stephen Brennan0766ec82021-09-01 10:51:41 -07002558/* Note: this does not consume "name" */
Al Viroc5f563f2021-09-07 15:57:42 -04002559static int filename_parentat(int dfd, struct filename *name,
Stephen Brennan0766ec82021-09-01 10:51:41 -07002560 unsigned int flags, struct path *parent,
2561 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002562{
2563 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002564 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002565
Al Viro5c31b6c2015-05-12 17:32:54 -04002566 if (IS_ERR(name))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002567 return PTR_ERR(name);
Al Viro06422962021-04-01 22:28:03 -04002568 set_nameidata(&nd, dfd, name, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04002569 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002570 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002571 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002572 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002573 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002574 if (likely(!retval)) {
2575 *last = nd.last;
2576 *type = nd.last_type;
Al Viroc9b07ea2019-07-14 13:22:27 -04002577 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002578 }
Al Viro9883d182015-05-13 07:28:08 -04002579 restore_nameidata();
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002580 return retval;
2581}
2582
Al Viro79714f72012-06-15 03:01:42 +04002583/* does lookup, returns the object with parent locked */
Stephen Brennan0766ec82021-09-01 10:51:41 -07002584static struct dentry *__kern_path_locked(struct filename *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002585{
Al Viro5c31b6c2015-05-12 17:32:54 -04002586 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002587 struct qstr last;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002588 int type, error;
Paul Moore51689102015-01-22 00:00:03 -05002589
Al Viroc5f563f2021-09-07 15:57:42 -04002590 error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07002591 if (error)
2592 return ERR_PTR(error);
Al Viro5c31b6c2015-05-12 17:32:54 -04002593 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002594 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002595 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002596 }
Al Viro59551022016-01-22 15:40:57 -05002597 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002598 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002599 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002600 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002601 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002602 }
Al Viro79714f72012-06-15 03:01:42 +04002603 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002604}
2605
Stephen Brennan0766ec82021-09-01 10:51:41 -07002606struct dentry *kern_path_locked(const char *name, struct path *path)
2607{
2608 struct filename *filename = getname_kernel(name);
2609 struct dentry *res = __kern_path_locked(filename, path);
2610
2611 putname(filename);
2612 return res;
2613}
2614
Al Virod1811462008-08-02 00:49:18 -04002615int kern_path(const char *name, unsigned int flags, struct path *path)
2616{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002617 struct filename *filename = getname_kernel(name);
2618 int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL);
2619
2620 putname(filename);
2621 return ret;
2622
Al Virod1811462008-08-02 00:49:18 -04002623}
Al Viro4d359502014-03-14 12:20:17 -04002624EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002625
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002626/**
2627 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2628 * @dentry: pointer to dentry of the base directory
2629 * @mnt: pointer to vfs mount of the base directory
2630 * @name: pointer to file name
2631 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002632 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002633 */
2634int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2635 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002636 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002637{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002638 struct filename *filename;
Al Viro9ad1aaa2015-05-12 16:44:39 -04002639 struct path root = {.mnt = mnt, .dentry = dentry};
Stephen Brennan794ebce2021-09-01 10:51:42 -07002640 int ret;
2641
2642 filename = getname_kernel(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002643 /* the first argument of filename_lookup() is ignored with root */
Stephen Brennan794ebce2021-09-01 10:51:42 -07002644 ret = filename_lookup(AT_FDCWD, filename, flags, path, &root);
2645 putname(filename);
2646 return ret;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002647}
Al Viro4d359502014-03-14 12:20:17 -04002648EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002649
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002650static int lookup_one_common(struct user_namespace *mnt_userns,
2651 const char *name, struct dentry *base, int len,
2652 struct qstr *this)
Al Viro3c95f0d2018-04-06 16:32:38 -04002653{
2654 this->name = name;
2655 this->len = len;
2656 this->hash = full_name_hash(base, name, len);
2657 if (!len)
2658 return -EACCES;
2659
2660 if (unlikely(name[0] == '.')) {
2661 if (len < 2 || (len == 2 && name[1] == '.'))
2662 return -EACCES;
2663 }
2664
2665 while (len--) {
2666 unsigned int c = *(const unsigned char *)name++;
2667 if (c == '/' || c == '\0')
2668 return -EACCES;
2669 }
2670 /*
2671 * See if the low-level filesystem might want
2672 * to use its own hash..
2673 */
2674 if (base->d_flags & DCACHE_OP_HASH) {
2675 int err = base->d_op->d_hash(base, this);
2676 if (err < 0)
2677 return err;
2678 }
2679
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002680 return inode_permission(mnt_userns, base->d_inode, MAY_EXEC);
Al Viro3c95f0d2018-04-06 16:32:38 -04002681}
2682
Christoph Hellwigeead1912007-10-16 23:25:38 -07002683/**
David Howells0da0b7f2018-06-15 15:19:22 +01002684 * try_lookup_one_len - filesystem helper to lookup single pathname component
2685 * @name: pathname component to lookup
2686 * @base: base directory to lookup from
2687 * @len: maximum length @len should be interpreted to
2688 *
2689 * Look up a dentry by name in the dcache, returning NULL if it does not
2690 * currently exist. The function does not try to create a dentry.
2691 *
2692 * Note that this routine is purely a helper for filesystem usage and should
2693 * not be called by generic code.
2694 *
2695 * The caller must hold base->i_mutex.
2696 */
2697struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2698{
2699 struct qstr this;
2700 int err;
2701
2702 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2703
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002704 err = lookup_one_common(&init_user_ns, name, base, len, &this);
David Howells0da0b7f2018-06-15 15:19:22 +01002705 if (err)
2706 return ERR_PTR(err);
2707
2708 return lookup_dcache(&this, base, 0);
2709}
2710EXPORT_SYMBOL(try_lookup_one_len);
2711
2712/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002713 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002714 * @name: pathname component to lookup
2715 * @base: base directory to lookup from
2716 * @len: maximum length @len should be interpreted to
2717 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002718 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002719 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002720 *
2721 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002722 */
James Morris057f6c02007-04-26 00:12:05 -07002723struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2724{
Al Viro8613a202018-04-06 16:45:33 -04002725 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002726 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002727 int err;
James Morris057f6c02007-04-26 00:12:05 -07002728
Al Viro59551022016-01-22 15:40:57 -05002729 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002730
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002731 err = lookup_one_common(&init_user_ns, name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002732 if (err)
2733 return ERR_PTR(err);
2734
Al Viro8613a202018-04-06 16:45:33 -04002735 dentry = lookup_dcache(&this, base, 0);
2736 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002737}
Al Viro4d359502014-03-14 12:20:17 -04002738EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002739
NeilBrownbbddca82016-01-07 16:08:20 -05002740/**
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002741 * lookup_one - filesystem helper to lookup single pathname component
2742 * @mnt_userns: user namespace of the mount the lookup is performed from
2743 * @name: pathname component to lookup
2744 * @base: base directory to lookup from
2745 * @len: maximum length @len should be interpreted to
2746 *
2747 * Note that this routine is purely a helper for filesystem usage and should
2748 * not be called by generic code.
2749 *
2750 * The caller must hold base->i_mutex.
2751 */
2752struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name,
2753 struct dentry *base, int len)
2754{
2755 struct dentry *dentry;
2756 struct qstr this;
2757 int err;
2758
2759 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2760
2761 err = lookup_one_common(mnt_userns, name, base, len, &this);
2762 if (err)
2763 return ERR_PTR(err);
2764
2765 dentry = lookup_dcache(&this, base, 0);
2766 return dentry ? dentry : __lookup_slow(&this, base, 0);
2767}
2768EXPORT_SYMBOL(lookup_one);
2769
2770/**
NeilBrownbbddca82016-01-07 16:08:20 -05002771 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2772 * @name: pathname component to lookup
2773 * @base: base directory to lookup from
2774 * @len: maximum length @len should be interpreted to
2775 *
2776 * Note that this routine is purely a helper for filesystem usage and should
2777 * not be called by generic code.
2778 *
2779 * Unlike lookup_one_len, it should be called without the parent
2780 * i_mutex held, and will take the i_mutex itself if necessary.
2781 */
2782struct dentry *lookup_one_len_unlocked(const char *name,
2783 struct dentry *base, int len)
2784{
2785 struct qstr this;
NeilBrownbbddca82016-01-07 16:08:20 -05002786 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002787 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002788
Christian Braunerc2fd68b2021-07-27 12:48:40 +02002789 err = lookup_one_common(&init_user_ns, name, base, len, &this);
NeilBrownbbddca82016-01-07 16:08:20 -05002790 if (err)
2791 return ERR_PTR(err);
2792
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002793 ret = lookup_dcache(&this, base, 0);
2794 if (!ret)
2795 ret = lookup_slow(&this, base, 0);
2796 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002797}
2798EXPORT_SYMBOL(lookup_one_len_unlocked);
2799
Al Viro6c2d47982019-10-31 01:21:58 -04002800/*
2801 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2802 * on negatives. Returns known positive or ERR_PTR(); that's what
2803 * most of the users want. Note that pinned negative with unlocked parent
2804 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2805 * need to be very careful; pinned positives have ->d_inode stable, so
2806 * this one avoids such problems.
2807 */
2808struct dentry *lookup_positive_unlocked(const char *name,
2809 struct dentry *base, int len)
2810{
2811 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
Al Viro2fa6b1e2019-11-12 16:13:06 -05002812 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
Al Viro6c2d47982019-10-31 01:21:58 -04002813 dput(ret);
2814 ret = ERR_PTR(-ENOENT);
2815 }
2816 return ret;
2817}
2818EXPORT_SYMBOL(lookup_positive_unlocked);
2819
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002820#ifdef CONFIG_UNIX98_PTYS
2821int path_pts(struct path *path)
2822{
2823 /* Find something mounted on "pts" in the same directory as
2824 * the input path.
2825 */
Al Viroa6a7eb72020-03-11 13:05:03 -04002826 struct dentry *parent = dget_parent(path->dentry);
2827 struct dentry *child;
Al Viro19f60282020-02-26 20:09:37 -05002828 struct qstr this = QSTR_INIT("pts", 3);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002829
Al Viroa6a7eb72020-03-11 13:05:03 -04002830 if (unlikely(!path_connected(path->mnt, parent))) {
2831 dput(parent);
Al Viro63b27722020-02-24 16:01:19 -05002832 return -ENOENT;
Al Viroa6a7eb72020-03-11 13:05:03 -04002833 }
Al Viro63b27722020-02-24 16:01:19 -05002834 dput(path->dentry);
2835 path->dentry = parent;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002836 child = d_hash_and_lookup(parent, &this);
2837 if (!child)
2838 return -ENOENT;
2839
2840 path->dentry = child;
2841 dput(parent);
Al Viro19f60282020-02-26 20:09:37 -05002842 follow_down(path);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002843 return 0;
2844}
2845#endif
2846
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002847int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2848 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Stephen Brennan794ebce2021-09-01 10:51:42 -07002850 struct filename *filename = getname_flags(name, flags, empty);
2851 int ret = filename_lookup(dfd, filename, flags, path, NULL);
2852
2853 putname(filename);
2854 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
Al Virob853a162015-05-13 09:12:02 -04002856EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002857
Christian Braunerba73d982021-01-21 14:19:31 +01002858int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
2859 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002861 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002862
Christian Braunerba73d982021-01-21 14:19:31 +01002863 if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002865 if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return 0;
Christian Braunerba73d982021-01-21 14:19:31 +01002867 return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002869EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871/*
2872 * Check whether we can remove a link victim from directory dir, check
2873 * whether the type of victim is right.
2874 * 1. We can't do it if dir is read-only (done in permission())
2875 * 2. We should have write and exec permissions on dir
2876 * 3. We can't remove anything from append-only dir
2877 * 4. We can't do anything with immutable dir (done in permission())
2878 * 5. If the sticky bit on dir is set we should either
2879 * a. be owner of dir, or
2880 * b. be owner of victim, or
2881 * c. have CAP_FOWNER capability
2882 * 6. If the victim is append-only or immutable we can't do antyhing with
2883 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002884 * 7. If the victim has an unknown uid or gid we can't change the inode.
2885 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2886 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2887 * 10. We can't remove a root or mountpoint.
2888 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 * nfs_async_unlink().
2890 */
Christian Braunerba73d982021-01-21 14:19:31 +01002891static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
2892 struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
David Howells63afdfc2015-05-06 15:59:00 +01002894 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 int error;
2896
David Howellsb18825a2013-09-12 19:22:53 +01002897 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002899 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002902
2903 /* Inode writeback is not safe when the uid or gid are invalid. */
Christian Braunerba73d982021-01-21 14:19:31 +01002904 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
2905 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002906 return -EOVERFLOW;
2907
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002908 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Christian Braunerba73d982021-01-21 14:19:31 +01002910 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (error)
2912 return error;
2913 if (IS_APPEND(dir))
2914 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002915
Christian Braunerba73d982021-01-21 14:19:31 +01002916 if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
2917 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
2918 HAS_UNMAPPED_ID(mnt_userns, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return -EPERM;
2920 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002921 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return -ENOTDIR;
2923 if (IS_ROOT(victim))
2924 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002925 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return -EISDIR;
2927 if (IS_DEADDIR(dir))
2928 return -ENOENT;
2929 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2930 return -EBUSY;
2931 return 0;
2932}
2933
2934/* Check whether we can create an object with dentry child in directory
2935 * dir.
2936 * 1. We can't do it if child already exists (open has special treatment for
2937 * this case, but since we are inlined it's OK)
2938 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002939 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2940 * 4. We should have write and exec permissions on dir
2941 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 */
Christian Braunerba73d982021-01-21 14:19:31 +01002943static inline int may_create(struct user_namespace *mnt_userns,
2944 struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Jeff Layton14e972b2013-05-08 10:25:58 -04002946 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (child->d_inode)
2948 return -EEXIST;
2949 if (IS_DEADDIR(dir))
2950 return -ENOENT;
Christian Brauner8e538912021-03-20 13:26:23 +01002951 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Eric W. Biederman036d5232016-07-01 12:52:06 -05002952 return -EOVERFLOW;
Christian Brauner8e538912021-03-20 13:26:23 +01002953
Christian Braunerba73d982021-01-21 14:19:31 +01002954 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957/*
2958 * p1 and p2 should be directories on the same fs.
2959 */
2960struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2961{
2962 struct dentry *p;
2963
2964 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002965 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return NULL;
2967 }
2968
Al Virofc640052016-04-10 01:33:30 -04002969 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002971 p = d_ancestor(p2, p1);
2972 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002973 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2974 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002975 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
2977
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002978 p = d_ancestor(p1, p2);
2979 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002980 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2981 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002982 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984
Al Viro59551022016-01-22 15:40:57 -05002985 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2986 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return NULL;
2988}
Al Viro4d359502014-03-14 12:20:17 -04002989EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991void unlock_rename(struct dentry *p1, struct dentry *p2)
2992{
Al Viro59551022016-01-22 15:40:57 -05002993 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002995 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002996 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
2998}
Al Viro4d359502014-03-14 12:20:17 -04002999EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Christian Brauner6521f892021-01-21 14:19:33 +01003001/**
3002 * vfs_create - create new file
3003 * @mnt_userns: user namespace of the mount the inode was found from
3004 * @dir: inode of @dentry
3005 * @dentry: pointer to dentry of the base directory
3006 * @mode: mode of the new file
3007 * @want_excl: whether the file must not yet exist
3008 *
3009 * Create a new file.
3010 *
3011 * If the inode has been found through an idmapped mount the user namespace of
3012 * the vfsmount must be passed through @mnt_userns. This function will then take
3013 * care to map the inode according to @mnt_userns before checking permissions.
3014 * On non-idmapped mounts or if permission checking is to be performed on the
3015 * raw inode simply passs init_user_ns.
3016 */
3017int vfs_create(struct user_namespace *mnt_userns, struct inode *dir,
3018 struct dentry *dentry, umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Christian Brauner6521f892021-01-21 14:19:33 +01003020 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (error)
3022 return error;
3023
Al Viroacfa4382008-12-04 10:06:33 -05003024 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return -EACCES; /* shouldn't it be ENOSYS? */
3026 mode &= S_IALLUGO;
3027 mode |= S_IFREG;
3028 error = security_inode_create(dir, dentry, mode);
3029 if (error)
3030 return error;
Christian Brauner549c7292021-01-21 14:19:43 +01003031 error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003032 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003033 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return error;
3035}
Al Viro4d359502014-03-14 12:20:17 -04003036EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Al Viro8e6c8482017-12-01 17:12:45 -05003038int vfs_mkobj(struct dentry *dentry, umode_t mode,
3039 int (*f)(struct dentry *, umode_t, void *),
3040 void *arg)
3041{
3042 struct inode *dir = dentry->d_parent->d_inode;
Christian Braunerba73d982021-01-21 14:19:31 +01003043 int error = may_create(&init_user_ns, dir, dentry);
Al Viro8e6c8482017-12-01 17:12:45 -05003044 if (error)
3045 return error;
3046
3047 mode &= S_IALLUGO;
3048 mode |= S_IFREG;
3049 error = security_inode_create(dir, dentry, mode);
3050 if (error)
3051 return error;
3052 error = f(dentry, mode, arg);
3053 if (!error)
3054 fsnotify_create(dir, dentry);
3055 return error;
3056}
3057EXPORT_SYMBOL(vfs_mkobj);
3058
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003059bool may_open_dev(const struct path *path)
3060{
3061 return !(path->mnt->mnt_flags & MNT_NODEV) &&
3062 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
3063}
3064
Christian Braunerba73d982021-01-21 14:19:31 +01003065static int may_open(struct user_namespace *mnt_userns, const struct path *path,
3066 int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02003068 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 struct inode *inode = dentry->d_inode;
3070 int error;
3071
3072 if (!inode)
3073 return -ENOENT;
3074
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003075 switch (inode->i_mode & S_IFMT) {
3076 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003078 case S_IFDIR:
Kees Cookfc4177b2020-08-14 17:30:14 -07003079 if (acc_mode & MAY_WRITE)
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003080 return -EISDIR;
Kees Cookfc4177b2020-08-14 17:30:14 -07003081 if (acc_mode & MAY_EXEC)
3082 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003083 break;
3084 case S_IFBLK:
3085 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05003086 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return -EACCES;
Kees Cook633fb6a2020-08-11 18:36:26 -07003088 fallthrough;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003089 case S_IFIFO:
3090 case S_IFSOCK:
Kees Cook633fb6a2020-08-11 18:36:26 -07003091 if (acc_mode & MAY_EXEC)
3092 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003094 break;
Kees Cook0fd338b2020-08-11 18:36:30 -07003095 case S_IFREG:
3096 if ((acc_mode & MAY_EXEC) && path_noexec(path))
3097 return -EACCES;
3098 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08003099 }
Dave Hansenb41572e2007-10-16 23:31:14 -07003100
Christian Braunerba73d982021-01-21 14:19:31 +01003101 error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07003102 if (error)
3103 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 /*
3106 * An append-only file must be opened in append mode for writing.
3107 */
3108 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05003109 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05003110 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05003112 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
3114
3115 /* O_NOATIME can only be set by the owner or superuser */
Christian Braunerba73d982021-01-21 14:19:31 +01003116 if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
Al Viro7715b522009-12-16 03:54:00 -05003117 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04003119 return 0;
Al Viro7715b522009-12-16 03:54:00 -05003120}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Christian Brauner549c7292021-01-21 14:19:43 +01003122static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05003123{
Al Virof0bb5aa2016-11-20 20:27:12 -05003124 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05003125 struct inode *inode = path->dentry->d_inode;
3126 int error = get_write_access(inode);
3127 if (error)
3128 return error;
Jeff Layton482e0002021-10-26 11:56:45 -04003129
Jeff Laytonf7e33bd2021-08-19 14:56:38 -04003130 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05003131 if (!error) {
Christian Brauner549c7292021-01-21 14:19:43 +01003132 error = do_truncate(mnt_userns, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05003133 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003134 filp);
Al Viro7715b522009-12-16 03:54:00 -05003135 }
3136 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003137 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Dave Hansend57999e2008-02-15 14:37:27 -08003140static inline int open_to_namei_flags(int flag)
3141{
Al Viro8a5e9292011-06-25 19:15:54 -04003142 if ((flag & O_ACCMODE) == 3)
3143 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003144 return flag;
3145}
3146
Christian Braunerba73d982021-01-21 14:19:31 +01003147static int may_o_create(struct user_namespace *mnt_userns,
3148 const struct path *dir, struct dentry *dentry,
3149 umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003150{
3151 int error = security_path_mknod(dir, dentry, mode, 0);
3152 if (error)
3153 return error;
3154
Christian Brauner8e538912021-03-20 13:26:23 +01003155 if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
Seth Forshee1328c722017-01-26 14:33:46 -06003156 return -EOVERFLOW;
3157
Christian Braunerba73d982021-01-21 14:19:31 +01003158 error = inode_permission(mnt_userns, dir->dentry->d_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01003159 MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003160 if (error)
3161 return error;
3162
3163 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3164}
3165
David Howells1acf0af2012-06-14 16:13:46 +01003166/*
3167 * Attempt to atomically look up, create and open a file from a negative
3168 * dentry.
3169 *
3170 * Returns 0 if successful. The file will have been created and attached to
3171 * @file by the filesystem calling finish_open().
3172 *
Al Viro00a07c12018-07-09 19:30:20 -04003173 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3174 * be set. The caller will need to perform the open themselves. @path will
3175 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003176 *
3177 * Returns an error code otherwise.
3178 */
Al Viro239eb982020-01-09 14:12:40 -05003179static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3180 struct file *file,
Al Viro239eb982020-01-09 14:12:40 -05003181 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003182{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003183 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003184 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003185 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003186
Miklos Szeredid18e9002012-06-05 15:10:17 +02003187 if (nd->flags & LOOKUP_DIRECTORY)
3188 open_flag |= O_DIRECTORY;
3189
Al Viro30d90492012-06-22 12:40:19 +04003190 file->f_path.dentry = DENTRY_NOT_SET;
3191 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003192 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003193 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003194 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003195 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003196 if (file->f_mode & FMODE_OPENED) {
Al Viro6fb968c2020-01-26 09:53:19 -05003197 if (unlikely(dentry != file->f_path.dentry)) {
3198 dput(dentry);
3199 dentry = dget(file->f_path.dentry);
3200 }
Al Viro64e1ac42018-07-09 19:17:52 -04003201 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003202 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003203 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003204 if (file->f_path.dentry) {
3205 dput(dentry);
3206 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003207 }
Al Viro239eb982020-01-09 14:12:40 -05003208 if (unlikely(d_is_negative(dentry)))
Al Viroa01e7182016-06-07 21:53:51 -04003209 error = -ENOENT;
Sage Weil62b2ce92012-08-15 13:30:12 -07003210 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003211 }
Al Viro239eb982020-01-09 14:12:40 -05003212 if (error) {
3213 dput(dentry);
3214 dentry = ERR_PTR(error);
3215 }
3216 return dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003217}
3218
Nick Piggin31e6b012011-01-07 17:49:52 +11003219/*
David Howells1acf0af2012-06-14 16:13:46 +01003220 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003221 *
Al Viro00a07c12018-07-09 19:30:20 -04003222 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003223 *
Al Viro00a07c12018-07-09 19:30:20 -04003224 * Returns 0 on success, that is, if
3225 * the file was successfully atomically created (if necessary) and opened, or
3226 * the file was not completely opened at this time, though lookups and
3227 * creations were performed.
3228 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3229 * In the latter case dentry returned in @path might be negative if O_CREAT
3230 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003231 *
Al Viro00a07c12018-07-09 19:30:20 -04003232 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003233 */
Al Viroda5ebf52020-01-09 14:25:14 -05003234static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3235 const struct open_flags *op,
3236 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003237{
Christian Brauner549c7292021-01-21 14:19:43 +01003238 struct user_namespace *mnt_userns;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003239 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003240 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003241 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003242 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003243 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003244 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003245 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003246
Al Viroce8644f2016-04-26 14:17:56 -04003247 if (unlikely(IS_DEADDIR(dir_inode)))
Al Viroda5ebf52020-01-09 14:25:14 -05003248 return ERR_PTR(-ENOENT);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003249
Al Viro73a09dd2018-06-08 13:22:02 -04003250 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003251 dentry = d_lookup(dir, &nd->last);
3252 for (;;) {
3253 if (!dentry) {
3254 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3255 if (IS_ERR(dentry))
Al Viroda5ebf52020-01-09 14:25:14 -05003256 return dentry;
Al Viro6fbd0712016-04-28 11:50:59 -04003257 }
3258 if (d_in_lookup(dentry))
3259 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003260
Al Viro6fbd0712016-04-28 11:50:59 -04003261 error = d_revalidate(dentry, nd->flags);
3262 if (likely(error > 0))
3263 break;
3264 if (error)
3265 goto out_dput;
3266 d_invalidate(dentry);
3267 dput(dentry);
3268 dentry = NULL;
3269 }
3270 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003271 /* Cached positive dentry: will open in f_op->open */
Al Viroda5ebf52020-01-09 14:25:14 -05003272 return dentry;
Al Viro6c51e512016-03-05 20:09:32 -05003273 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003274
Al Viro1643b432016-04-27 19:14:10 -04003275 /*
3276 * Checking write permission is tricky, bacuse we don't know if we are
3277 * going to actually need it: O_CREAT opens should work as long as the
3278 * file exists. But checking existence breaks atomicity. The trick is
3279 * to check access and if not granted clear O_CREAT from the flags.
3280 *
3281 * Another problem is returing the "right" error value (e.g. for an
3282 * O_EXCL open we want to return EEXIST not EROFS).
3283 */
Al Viro99a4a902020-03-12 14:07:27 -04003284 if (unlikely(!got_write))
3285 open_flag &= ~O_TRUNC;
Christian Brauner549c7292021-01-21 14:19:43 +01003286 mnt_userns = mnt_user_ns(nd->path.mnt);
Al Viro1643b432016-04-27 19:14:10 -04003287 if (open_flag & O_CREAT) {
Al Viro99a4a902020-03-12 14:07:27 -04003288 if (open_flag & O_EXCL)
3289 open_flag &= ~O_TRUNC;
Al Viro1643b432016-04-27 19:14:10 -04003290 if (!IS_POSIXACL(dir->d_inode))
3291 mode &= ~current_umask();
Al Viro99a4a902020-03-12 14:07:27 -04003292 if (likely(got_write))
Christian Brauner549c7292021-01-21 14:19:43 +01003293 create_error = may_o_create(mnt_userns, &nd->path,
Christian Braunerba73d982021-01-21 14:19:31 +01003294 dentry, mode);
Al Viro99a4a902020-03-12 14:07:27 -04003295 else
3296 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003297 }
Al Viro99a4a902020-03-12 14:07:27 -04003298 if (create_error)
3299 open_flag &= ~O_CREAT;
Al Viro1643b432016-04-27 19:14:10 -04003300 if (dir_inode->i_op->atomic_open) {
Al Virod489cf92020-03-11 08:07:53 -04003301 dentry = atomic_open(nd, dentry, file, open_flag, mode);
Al Viroda5ebf52020-01-09 14:25:14 -05003302 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3303 dentry = ERR_PTR(create_error);
3304 return dentry;
Al Viro1643b432016-04-27 19:14:10 -04003305 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003306
Al Viro6fbd0712016-04-28 11:50:59 -04003307 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003308 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3309 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003310 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003311 if (unlikely(res)) {
3312 if (IS_ERR(res)) {
3313 error = PTR_ERR(res);
3314 goto out_dput;
3315 }
3316 dput(dentry);
3317 dentry = res;
3318 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003319 }
3320
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003321 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003322 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003323 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003324 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003325 if (!dir_inode->i_op->create) {
3326 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003327 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003328 }
Christian Brauner549c7292021-01-21 14:19:43 +01003329
3330 error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry,
3331 mode, open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003332 if (error)
3333 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003334 }
Al Viro1643b432016-04-27 19:14:10 -04003335 if (unlikely(create_error) && !dentry->d_inode) {
3336 error = create_error;
3337 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003338 }
Al Viroda5ebf52020-01-09 14:25:14 -05003339 return dentry;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003340
3341out_dput:
3342 dput(dentry);
Al Viroda5ebf52020-01-09 14:25:14 -05003343 return ERR_PTR(error);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003344}
3345
Al Viroc981a482020-01-26 11:06:21 -05003346static const char *open_last_lookups(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003347 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003348{
Al Viroa1e28032009-12-24 02:12:06 -05003349 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003350 int open_flag = op->open_flag;
Al Viro64894cf2012-07-31 00:53:35 +04003351 bool got_write = false;
Al Viro254cf582015-05-05 09:40:46 -04003352 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003353 struct inode *inode;
Al Viroda5ebf52020-01-09 14:25:14 -05003354 struct dentry *dentry;
Al Virob0417d22020-01-14 13:34:20 -05003355 const char *res;
Al Virofb1cc552009-12-24 01:58:28 -05003356
Al Viroc3e380b2011-02-23 13:39:45 -05003357 nd->flags |= op->intent;
3358
Al Virobc77daa2013-06-06 09:12:33 -04003359 if (nd->last_type != LAST_NORM) {
Al Viro56676ec2020-03-10 21:54:54 -04003360 if (nd->depth)
3361 put_link(nd);
Al Viroff326a32020-03-10 10:19:24 -04003362 return handle_dots(nd, nd->last_type);
Al Viro1f36f772009-12-26 10:56:19 -05003363 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003364
Al Viroca344a892011-03-09 00:36:45 -05003365 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003366 if (nd->last.name[nd->last.len])
3367 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3368 /* we _can_ be in RCU mode here */
Al Viro20e34352020-01-09 14:58:31 -05003369 dentry = lookup_fast(nd, &inode, &seq);
3370 if (IS_ERR(dentry))
Al Viro1ccac622020-01-14 10:13:40 -05003371 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05003372 if (likely(dentry))
Miklos Szeredi71574862012-06-05 15:10:14 +02003373 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003374
Al Viro6583fe22016-03-05 18:14:03 -05003375 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003376 } else {
3377 /* create side of things */
Al Viro72287412020-03-10 10:09:26 -04003378 if (nd->flags & LOOKUP_RCU) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003379 if (!try_to_unlazy(nd))
3380 return ERR_PTR(-ECHILD);
Al Viro72287412020-03-10 10:09:26 -04003381 }
Al Viroc9b07ea2019-07-14 13:22:27 -04003382 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003383 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003384 if (unlikely(nd->last.name[nd->last.len]))
Al Viro1ccac622020-01-14 10:13:40 -05003385 return ERR_PTR(-EISDIR);
Al Virofe2d35f2011-03-05 22:58:25 -05003386 }
3387
Al Viro9cf843e2016-04-28 19:35:16 -04003388 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Jens Axboee36cffe2020-12-17 09:19:08 -07003389 got_write = !mnt_want_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003390 /*
3391 * do _not_ fail yet - we might not need that or fail with
3392 * a different error; let lookup_open() decide; we'll be
3393 * dropping this one anyway.
3394 */
3395 }
Al Viro9cf843e2016-04-28 19:35:16 -04003396 if (open_flag & O_CREAT)
3397 inode_lock(dir->d_inode);
3398 else
3399 inode_lock_shared(dir->d_inode);
Al Viroda5ebf52020-01-09 14:25:14 -05003400 dentry = lookup_open(nd, file, op, got_write);
Al Virof7bb9592020-03-05 13:25:20 -05003401 if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3402 fsnotify_create(dir->d_inode, dentry);
Al Viro9cf843e2016-04-28 19:35:16 -04003403 if (open_flag & O_CREAT)
3404 inode_unlock(dir->d_inode);
3405 else
3406 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003407
Al Viroc981a482020-01-26 11:06:21 -05003408 if (got_write)
Al Viro59e96e62020-01-26 10:22:24 -05003409 mnt_drop_write(nd->path.mnt);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003410
Al Viro59e96e62020-01-26 10:22:24 -05003411 if (IS_ERR(dentry))
3412 return ERR_CAST(dentry);
3413
Al Viro973d4b72020-01-26 10:48:16 -05003414 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
Al Viroe73cabf2020-01-09 14:30:08 -05003415 dput(nd->path.dentry);
3416 nd->path.dentry = dentry;
Al Viroc981a482020-01-26 11:06:21 -05003417 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05003418 }
3419
Al Viro20e34352020-01-09 14:58:31 -05003420finish_lookup:
Al Viro56676ec2020-03-10 21:54:54 -04003421 if (nd->depth)
3422 put_link(nd);
Al Viro8c4efe22020-01-19 12:44:18 -05003423 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
Al Viroff326a32020-03-10 10:19:24 -04003424 if (unlikely(res))
Al Virob0417d22020-01-14 13:34:20 -05003425 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viroff326a32020-03-10 10:19:24 -04003426 return res;
Al Viroc981a482020-01-26 11:06:21 -05003427}
3428
3429/*
3430 * Handle the last step of open()
3431 */
Al Viroc5971b82020-03-05 11:41:29 -05003432static int do_open(struct nameidata *nd,
Al Viroc981a482020-01-26 11:06:21 -05003433 struct file *file, const struct open_flags *op)
3434{
Christian Brauner549c7292021-01-21 14:19:43 +01003435 struct user_namespace *mnt_userns;
Al Viroc981a482020-01-26 11:06:21 -05003436 int open_flag = op->open_flag;
3437 bool do_truncate;
3438 int acc_mode;
Al Viroc981a482020-01-26 11:06:21 -05003439 int error;
3440
Al Viroff326a32020-03-10 10:19:24 -04003441 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3442 error = complete_walk(nd);
3443 if (error)
3444 return error;
3445 }
Al Viro973d4b72020-01-26 10:48:16 -05003446 if (!(file->f_mode & FMODE_CREATED))
3447 audit_inode(nd->name, nd->path.dentry, 0);
Christian Brauner549c7292021-01-21 14:19:43 +01003448 mnt_userns = mnt_user_ns(nd->path.mnt);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003449 if (open_flag & O_CREAT) {
Al Virob94e0b32020-03-10 10:13:53 -04003450 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3451 return -EEXIST;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003452 if (d_is_dir(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003453 return -EISDIR;
Christian Brauner549c7292021-01-21 14:19:43 +01003454 error = may_create_in_sticky(mnt_userns, nd,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003455 d_backing_inode(nd->path.dentry));
3456 if (unlikely(error))
Al Viroc5971b82020-03-05 11:41:29 -05003457 return error;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003458 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003459 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003460 return -ENOTDIR;
Al Viro6c0d46c2011-03-09 00:59:59 -05003461
Al Viro8795e7d2020-01-26 10:38:17 -05003462 do_truncate = false;
3463 acc_mode = op->acc_mode;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003464 if (file->f_mode & FMODE_CREATED) {
3465 /* Don't check for write permission, don't truncate */
3466 open_flag &= ~O_TRUNC;
Al Viro5a2d3ed2020-01-26 10:32:22 -05003467 acc_mode = 0;
Al Viro8795e7d2020-01-26 10:38:17 -05003468 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
Al Viro0f9d1a12011-03-09 00:13:14 -05003469 error = mnt_want_write(nd->path.mnt);
3470 if (error)
Al Viroc5971b82020-03-05 11:41:29 -05003471 return error;
Al Viro8795e7d2020-01-26 10:38:17 -05003472 do_truncate = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003473 }
Christian Brauner549c7292021-01-21 14:19:43 +01003474 error = may_open(mnt_userns, &nd->path, acc_mode, open_flag);
Al Viro8795e7d2020-01-26 10:38:17 -05003475 if (!error && !(file->f_mode & FMODE_OPENED))
Al Viro3ad56152020-01-26 10:06:13 -05003476 error = vfs_open(&nd->path, file);
Al Viro8795e7d2020-01-26 10:38:17 -05003477 if (!error)
3478 error = ima_file_check(file, op->acc_mode);
3479 if (!error && do_truncate)
Christian Brauner549c7292021-01-21 14:19:43 +01003480 error = handle_truncate(mnt_userns, file);
Al Viroc80567c2016-02-27 19:17:33 -05003481 if (unlikely(error > 0)) {
3482 WARN_ON(1);
3483 error = -EINVAL;
3484 }
Al Viro8795e7d2020-01-26 10:38:17 -05003485 if (do_truncate)
Al Viro0f9d1a12011-03-09 00:13:14 -05003486 mnt_drop_write(nd->path.mnt);
Al Viroc5971b82020-03-05 11:41:29 -05003487 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003488}
3489
Christian Brauner6521f892021-01-21 14:19:33 +01003490/**
3491 * vfs_tmpfile - create tmpfile
3492 * @mnt_userns: user namespace of the mount the inode was found from
3493 * @dentry: pointer to dentry of the base directory
3494 * @mode: mode of the new tmpfile
Randy Dunlap2111c3c2021-02-15 20:29:28 -08003495 * @open_flag: flags
Christian Brauner6521f892021-01-21 14:19:33 +01003496 *
3497 * Create a temporary file.
3498 *
3499 * If the inode has been found through an idmapped mount the user namespace of
3500 * the vfsmount must be passed through @mnt_userns. This function will then take
3501 * care to map the inode according to @mnt_userns before checking permissions.
3502 * On non-idmapped mounts or if permission checking is to be performed on the
3503 * raw inode simply passs init_user_ns.
3504 */
3505struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns,
3506 struct dentry *dentry, umode_t mode, int open_flag)
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003507{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003508 struct dentry *child = NULL;
3509 struct inode *dir = dentry->d_inode;
3510 struct inode *inode;
3511 int error;
3512
3513 /* we want directory to be writable */
Christian Brauner6521f892021-01-21 14:19:33 +01003514 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003515 if (error)
3516 goto out_err;
3517 error = -EOPNOTSUPP;
3518 if (!dir->i_op->tmpfile)
3519 goto out_err;
3520 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003521 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003522 if (unlikely(!child))
3523 goto out_err;
Christian Brauner549c7292021-01-21 14:19:43 +01003524 error = dir->i_op->tmpfile(mnt_userns, dir, child, mode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003525 if (error)
3526 goto out_err;
3527 error = -ENOENT;
3528 inode = child->d_inode;
3529 if (unlikely(!inode))
3530 goto out_err;
3531 if (!(open_flag & O_EXCL)) {
3532 spin_lock(&inode->i_lock);
3533 inode->i_state |= I_LINKABLE;
3534 spin_unlock(&inode->i_lock);
3535 }
Christian Braunera2d23292021-01-21 14:19:45 +01003536 ima_post_create_tmpfile(mnt_userns, inode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003537 return child;
3538
3539out_err:
3540 dput(child);
3541 return ERR_PTR(error);
3542}
3543EXPORT_SYMBOL(vfs_tmpfile);
3544
Al Viroc8a53ee2015-05-12 18:43:07 -04003545static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003546 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003547 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003548{
Christian Brauner6521f892021-01-21 14:19:33 +01003549 struct user_namespace *mnt_userns;
Al Viro625b6d12015-05-12 16:36:12 -04003550 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003551 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003552 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003553 if (unlikely(error))
3554 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003555 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003556 if (unlikely(error))
3557 goto out;
Christian Brauner6521f892021-01-21 14:19:33 +01003558 mnt_userns = mnt_user_ns(path.mnt);
3559 child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003560 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003561 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003562 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003563 dput(path.dentry);
3564 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003565 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003566 /* Don't check for other permissions, the inode was just created */
Christian Brauner549c7292021-01-21 14:19:43 +01003567 error = may_open(mnt_userns, &path, 0, op->open_flag);
Al Viro1e8f44f2020-03-11 17:22:19 -04003568 if (!error)
3569 error = vfs_open(&path, file);
Al Viro60545d02013-06-07 01:20:27 -04003570out2:
Al Viro625b6d12015-05-12 16:36:12 -04003571 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003572out:
Al Viro625b6d12015-05-12 16:36:12 -04003573 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003574 return error;
3575}
3576
Al Viro6ac08702016-04-26 00:02:50 -04003577static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3578{
3579 struct path path;
3580 int error = path_lookupat(nd, flags, &path);
3581 if (!error) {
3582 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003583 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003584 path_put(&path);
3585 }
3586 return error;
3587}
3588
Al Viroc8a53ee2015-05-12 18:43:07 -04003589static struct file *path_openat(struct nameidata *nd,
3590 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Al Viro30d90492012-06-22 12:40:19 +04003592 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003593 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003594
Al Viroea73ea72018-07-11 15:00:04 -04003595 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003596 if (IS_ERR(file))
3597 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003598
Al Virobb458c62013-07-13 13:26:37 +04003599 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003600 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003601 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003602 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003603 } else {
3604 const char *s = path_init(nd, flags);
3605 while (!(error = link_path_walk(s, nd)) &&
Al Viroc5971b82020-03-05 11:41:29 -05003606 (s = open_last_lookups(nd, file, op)) != NULL)
Al Viro1ccac622020-01-14 10:13:40 -05003607 ;
Al Viroc5971b82020-03-05 11:41:29 -05003608 if (!error)
3609 error = do_open(nd, file, op);
Al Viro5f336e72018-07-09 16:38:06 -04003610 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003611 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003612 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003613 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003614 return file;
3615 WARN_ON(1);
3616 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003617 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003618 fput(file);
3619 if (error == -EOPENSTALE) {
3620 if (flags & LOOKUP_RCU)
3621 error = -ECHILD;
3622 else
3623 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003624 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003625 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
Jeff Layton669abf42012-10-10 16:43:10 -04003628struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003629 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003630{
Al Viro9883d182015-05-13 07:28:08 -04003631 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003632 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003633 struct file *filp;
3634
Al Viro06422962021-04-01 22:28:03 -04003635 set_nameidata(&nd, dfd, pathname, NULL);
Al Viroc8a53ee2015-05-12 18:43:07 -04003636 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003637 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003638 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003639 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003640 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003641 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003642 return filp;
3643}
3644
Al Viroffb37ca2021-04-01 19:00:57 -04003645struct file *do_file_open_root(const struct path *root,
Al Virof9652e12013-06-11 08:23:01 +04003646 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003647{
Al Viro9883d182015-05-13 07:28:08 -04003648 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003649 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003650 struct filename *filename;
Al Virobcba1e72021-04-01 22:03:41 -04003651 int flags = op->lookup_flags;
Al Viro73d049a2011-03-11 12:08:24 -05003652
Al Viroffb37ca2021-04-01 19:00:57 -04003653 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003654 return ERR_PTR(-ELOOP);
3655
Paul Moore51689102015-01-22 00:00:03 -05003656 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303657 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003658 return ERR_CAST(filename);
3659
Al Viro06422962021-04-01 22:28:03 -04003660 set_nameidata(&nd, -1, filename, root);
Al Viroc8a53ee2015-05-12 18:43:07 -04003661 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003662 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003663 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003664 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003665 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003666 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003667 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003668 return file;
3669}
3670
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003671static struct dentry *filename_create(int dfd, struct filename *name,
3672 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003674 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003675 struct qstr last;
3676 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003677 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003678 int error;
3679 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3680
3681 /*
3682 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3683 * other flags passed in are ignored!
3684 */
3685 lookup_flags &= LOOKUP_REVAL;
3686
Al Viroc5f563f2021-09-07 15:57:42 -04003687 error = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07003688 if (error)
3689 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003691 /*
3692 * Yucky last component or no last component at all?
3693 * (foo/., foo/.., /////)
3694 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003695 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003696 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003697
Jan Karac30dabf2012-06-12 16:20:30 +02003698 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003699 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003700 /*
3701 * Do the final lookup.
3702 */
Al Viro391172c2015-05-09 11:19:16 -04003703 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003704 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003705 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003707 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003708
Al Viroa8104a92012-07-20 02:25:00 +04003709 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003710 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003711 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003712
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003713 /*
3714 * Special case - lookup gave negative, but... we had foo/bar/
3715 * From the vfs_mknod() POV we just have a negative dentry -
3716 * all is fine. Let's be bastards - you had / on the end, you've
3717 * been asking for (non-existent) directory. -ENOENT for you.
3718 */
Al Viro391172c2015-05-09 11:19:16 -04003719 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003720 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003721 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003722 }
Jan Karac30dabf2012-06-12 16:20:30 +02003723 if (unlikely(err2)) {
3724 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003725 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728fail:
Al Viroa8104a92012-07-20 02:25:00 +04003729 dput(dentry);
3730 dentry = ERR_PTR(error);
3731unlock:
Al Viro59551022016-01-22 15:40:57 -05003732 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003733 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003734 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003735out:
Al Viro391172c2015-05-09 11:19:16 -04003736 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 return dentry;
3738}
Al Virofa14a0b2015-01-22 02:16:49 -05003739
3740struct dentry *kern_path_create(int dfd, const char *pathname,
3741 struct path *path, unsigned int lookup_flags)
3742{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003743 struct filename *filename = getname_kernel(pathname);
3744 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3745
3746 putname(filename);
3747 return res;
Al Virofa14a0b2015-01-22 02:16:49 -05003748}
Al Virodae6ad82011-06-26 11:50:15 -04003749EXPORT_SYMBOL(kern_path_create);
3750
Al Viro921a1652012-07-20 01:15:31 +04003751void done_path_create(struct path *path, struct dentry *dentry)
3752{
3753 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003754 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003755 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003756 path_put(path);
3757}
3758EXPORT_SYMBOL(done_path_create);
3759
Al Viro520ae682015-05-13 07:00:28 -04003760inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003761 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003762{
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003763 struct filename *filename = getname(pathname);
3764 struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
3765
3766 putname(filename);
3767 return res;
Al Virodae6ad82011-06-26 11:50:15 -04003768}
3769EXPORT_SYMBOL(user_path_create);
3770
Christian Brauner6521f892021-01-21 14:19:33 +01003771/**
3772 * vfs_mknod - create device node or file
3773 * @mnt_userns: user namespace of the mount the inode was found from
3774 * @dir: inode of @dentry
3775 * @dentry: pointer to dentry of the base directory
3776 * @mode: mode of the new device node or file
3777 * @dev: device number of device to create
3778 *
3779 * Create a device node or file.
3780 *
3781 * If the inode has been found through an idmapped mount the user namespace of
3782 * the vfsmount must be passed through @mnt_userns. This function will then take
3783 * care to map the inode according to @mnt_userns before checking permissions.
3784 * On non-idmapped mounts or if permission checking is to be performed on the
3785 * raw inode simply passs init_user_ns.
3786 */
3787int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
3788 struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003790 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
Christian Brauner6521f892021-01-21 14:19:33 +01003791 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 if (error)
3794 return error;
3795
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003796 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3797 !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return -EPERM;
3799
Al Viroacfa4382008-12-04 10:06:33 -05003800 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 return -EPERM;
3802
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003803 error = devcgroup_inode_mknod(mode, dev);
3804 if (error)
3805 return error;
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 error = security_inode_mknod(dir, dentry, mode, dev);
3808 if (error)
3809 return error;
3810
Christian Brauner549c7292021-01-21 14:19:43 +01003811 error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003812 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003813 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return error;
3815}
Al Viro4d359502014-03-14 12:20:17 -04003816EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Al Virof69aac02011-07-26 04:31:40 -04003818static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003819{
3820 switch (mode & S_IFMT) {
3821 case S_IFREG:
3822 case S_IFCHR:
3823 case S_IFBLK:
3824 case S_IFIFO:
3825 case S_IFSOCK:
3826 case 0: /* zero mode translates to S_IFREG */
3827 return 0;
3828 case S_IFDIR:
3829 return -EPERM;
3830 default:
3831 return -EINVAL;
3832 }
3833}
3834
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003835static int do_mknodat(int dfd, struct filename *name, umode_t mode,
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003836 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
Christian Brauner6521f892021-01-21 14:19:33 +01003838 struct user_namespace *mnt_userns;
Al Viro2ad94ae2008-07-21 09:32:51 -04003839 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003840 struct path path;
3841 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003842 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Al Viro8e4bfca2012-07-20 01:17:26 +04003844 error = may_mknod(mode);
3845 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003846 goto out1;
Jeff Layton972567f2012-12-20 16:00:10 -05003847retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003848 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev77972512021-07-08 13:34:40 +07003849 error = PTR_ERR(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003850 if (IS_ERR(dentry))
Dmitry Kadashev77972512021-07-08 13:34:40 +07003851 goto out1;
Al Viro2ad94ae2008-07-21 09:32:51 -04003852
Al Virodae6ad82011-06-26 11:50:15 -04003853 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003854 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003855 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003856 if (error)
Dmitry Kadashev77972512021-07-08 13:34:40 +07003857 goto out2;
Christian Brauner6521f892021-01-21 14:19:33 +01003858
3859 mnt_userns = mnt_user_ns(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003860 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 case 0: case S_IFREG:
Christian Brauner6521f892021-01-21 14:19:33 +01003862 error = vfs_create(mnt_userns, path.dentry->d_inode,
3863 dentry, mode, true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003864 if (!error)
Christian Braunera2d23292021-01-21 14:19:45 +01003865 ima_post_path_mknod(mnt_userns, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 break;
3867 case S_IFCHR: case S_IFBLK:
Christian Brauner6521f892021-01-21 14:19:33 +01003868 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3869 dentry, mode, new_decode_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 break;
3871 case S_IFIFO: case S_IFSOCK:
Christian Brauner6521f892021-01-21 14:19:33 +01003872 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3873 dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003876out2:
Al Viro921a1652012-07-20 01:15:31 +04003877 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003878 if (retry_estale(error, lookup_flags)) {
3879 lookup_flags |= LOOKUP_REVAL;
3880 goto retry;
3881 }
Dmitry Kadashev77972512021-07-08 13:34:40 +07003882out1:
3883 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return error;
3885}
3886
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003887SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3888 unsigned int, dev)
3889{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003890 return do_mknodat(dfd, getname(filename), mode, dev);
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003891}
3892
Al Viro8208a222011-07-25 17:32:17 -04003893SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003894{
Dmitry Kadashev77972512021-07-08 13:34:40 +07003895 return do_mknodat(AT_FDCWD, getname(filename), mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003896}
3897
Christian Brauner6521f892021-01-21 14:19:33 +01003898/**
3899 * vfs_mkdir - create directory
3900 * @mnt_userns: user namespace of the mount the inode was found from
3901 * @dir: inode of @dentry
3902 * @dentry: pointer to dentry of the base directory
3903 * @mode: mode of the new directory
3904 *
3905 * Create a directory.
3906 *
3907 * If the inode has been found through an idmapped mount the user namespace of
3908 * the vfsmount must be passed through @mnt_userns. This function will then take
3909 * care to map the inode according to @mnt_userns before checking permissions.
3910 * On non-idmapped mounts or if permission checking is to be performed on the
3911 * raw inode simply passs init_user_ns.
3912 */
3913int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
3914 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
Christian Brauner6521f892021-01-21 14:19:33 +01003916 int error = may_create(mnt_userns, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003917 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
3919 if (error)
3920 return error;
3921
Al Viroacfa4382008-12-04 10:06:33 -05003922 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return -EPERM;
3924
3925 mode &= (S_IRWXUGO|S_ISVTX);
3926 error = security_inode_mkdir(dir, dentry, mode);
3927 if (error)
3928 return error;
3929
Al Viro8de52772012-02-06 12:45:27 -05003930 if (max_links && dir->i_nlink >= max_links)
3931 return -EMLINK;
3932
Christian Brauner549c7292021-01-21 14:19:43 +01003933 error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003934 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003935 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 return error;
3937}
Al Viro4d359502014-03-14 12:20:17 -04003938EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07003940int do_mkdirat(int dfd, struct filename *name, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Dave Hansen6902d922006-09-30 23:29:01 -07003942 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003943 struct path path;
3944 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003945 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003947retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07003948 dentry = filename_create(dfd, name, &path, lookup_flags);
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003949 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003950 if (IS_ERR(dentry))
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003951 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003952
Al Virodae6ad82011-06-26 11:50:15 -04003953 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003954 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003955 error = security_path_mkdir(&path, dentry, mode);
Christian Brauner6521f892021-01-21 14:19:33 +01003956 if (!error) {
3957 struct user_namespace *mnt_userns;
3958 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01003959 error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry,
3960 mode);
Christian Brauner6521f892021-01-21 14:19:33 +01003961 }
Al Viro921a1652012-07-20 01:15:31 +04003962 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003963 if (retry_estale(error, lookup_flags)) {
3964 lookup_flags |= LOOKUP_REVAL;
3965 goto retry;
3966 }
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003967out_putname:
3968 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return error;
3970}
3971
Dominik Brodowski0101db72018-03-11 11:34:49 +01003972SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3973{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003974 return do_mkdirat(dfd, getname(pathname), mode);
Dominik Brodowski0101db72018-03-11 11:34:49 +01003975}
3976
Al Viroa218d0f2011-11-21 14:59:34 -05003977SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003978{
Dmitry Kadashev584d3222021-07-08 13:34:39 +07003979 return do_mkdirat(AT_FDCWD, getname(pathname), mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003980}
3981
Christian Brauner6521f892021-01-21 14:19:33 +01003982/**
3983 * vfs_rmdir - remove directory
3984 * @mnt_userns: user namespace of the mount the inode was found from
3985 * @dir: inode of @dentry
3986 * @dentry: pointer to dentry of the base directory
3987 *
3988 * Remove a directory.
3989 *
3990 * If the inode has been found through an idmapped mount the user namespace of
3991 * the vfsmount must be passed through @mnt_userns. This function will then take
3992 * care to map the inode according to @mnt_userns before checking permissions.
3993 * On non-idmapped mounts or if permission checking is to be performed on the
3994 * raw inode simply passs init_user_ns.
3995 */
3996int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
3997 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
Christian Brauner6521f892021-01-21 14:19:33 +01003999 int error = may_delete(mnt_userns, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001 if (error)
4002 return error;
4003
Al Viroacfa4382008-12-04 10:06:33 -05004004 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 return -EPERM;
4006
Al Viro1d2ef592011-09-14 18:55:41 +01004007 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05004008 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Sage Weil912dbc12011-05-24 13:06:11 -07004010 error = -EBUSY;
David Howells1bd9c4e2021-11-18 08:58:08 +00004011 if (is_local_mountpoint(dentry) ||
4012 (dentry->d_inode->i_flags & S_KERNEL_FILE))
Sage Weil912dbc12011-05-24 13:06:11 -07004013 goto out;
4014
4015 error = security_inode_rmdir(dir, dentry);
4016 if (error)
4017 goto out;
4018
4019 error = dir->i_op->rmdir(dir, dentry);
4020 if (error)
4021 goto out;
4022
Al Viro87677122018-05-27 16:23:51 -04004023 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004024 dentry->d_inode->i_flags |= S_DEAD;
4025 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004026 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004027
4028out:
Al Viro59551022016-01-22 15:40:57 -05004029 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01004030 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07004031 if (!error)
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004032 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 return error;
4034}
Al Viro4d359502014-03-14 12:20:17 -04004035EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004037int do_rmdir(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
Christian Brauner6521f892021-01-21 14:19:33 +01004039 struct user_namespace *mnt_userns;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004040 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004042 struct path path;
4043 struct qstr last;
4044 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05004045 unsigned int lookup_flags = 0;
4046retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004047 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004048 if (error)
4049 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Al Virof5beed72015-04-30 16:09:11 -04004051 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004052 case LAST_DOTDOT:
4053 error = -ENOTEMPTY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004054 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004055 case LAST_DOT:
4056 error = -EINVAL;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004057 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004058 case LAST_ROOT:
4059 error = -EBUSY;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004060 goto exit2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004062
Al Virof5beed72015-04-30 16:09:11 -04004063 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004064 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004065 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004066
Al Viro59551022016-01-22 15:40:57 -05004067 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004068 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004070 if (IS_ERR(dentry))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004071 goto exit3;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004072 if (!dentry->d_inode) {
4073 error = -ENOENT;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004074 goto exit4;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004075 }
Al Virof5beed72015-04-30 16:09:11 -04004076 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004077 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004078 goto exit4;
Christian Brauner6521f892021-01-21 14:19:33 +01004079 mnt_userns = mnt_user_ns(path.mnt);
4080 error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004081exit4:
Dave Hansen6902d922006-09-30 23:29:01 -07004082 dput(dentry);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004083exit3:
Al Viro59551022016-01-22 15:40:57 -05004084 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04004085 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004086exit2:
Al Virof5beed72015-04-30 16:09:11 -04004087 path_put(&path);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05004088 if (retry_estale(error, lookup_flags)) {
4089 lookup_flags |= LOOKUP_REVAL;
4090 goto retry;
4091 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004092exit1:
Al Viro24fb33d2020-08-12 05:15:18 +01004093 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 return error;
4095}
4096
Heiko Carstens3cdad422009-01-14 14:14:22 +01004097SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004098{
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004099 return do_rmdir(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004100}
4101
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004102/**
4103 * vfs_unlink - unlink a filesystem object
Christian Brauner6521f892021-01-21 14:19:33 +01004104 * @mnt_userns: user namespace of the mount the inode was found from
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004105 * @dir: parent directory
4106 * @dentry: victim
4107 * @delegated_inode: returns victim inode, if the inode is delegated.
4108 *
4109 * The caller must hold dir->i_mutex.
4110 *
4111 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4112 * return a reference to the inode in delegated_inode. The caller
4113 * should then break the delegation on that inode and retry. Because
4114 * breaking a delegation may take a long time, the caller should drop
4115 * dir->i_mutex before doing so.
4116 *
4117 * Alternatively, a caller may pass NULL for delegated_inode. This may
4118 * be appropriate for callers that expect the underlying filesystem not
4119 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004120 *
4121 * If the inode has been found through an idmapped mount the user namespace of
4122 * the vfsmount must be passed through @mnt_userns. This function will then take
4123 * care to map the inode according to @mnt_userns before checking permissions.
4124 * On non-idmapped mounts or if permission checking is to be performed on the
4125 * raw inode simply passs init_user_ns.
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004126 */
Christian Brauner6521f892021-01-21 14:19:33 +01004127int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
4128 struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004130 struct inode *target = dentry->d_inode;
Christian Brauner6521f892021-01-21 14:19:33 +01004131 int error = may_delete(mnt_userns, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
4133 if (error)
4134 return error;
4135
Al Viroacfa4382008-12-04 10:06:33 -05004136 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 return -EPERM;
4138
Al Viro59551022016-01-22 15:40:57 -05004139 inode_lock(target);
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004140 if (IS_SWAPFILE(target))
4141 error = -EPERM;
4142 else if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 error = -EBUSY;
4144 else {
4145 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004146 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004147 error = try_break_deleg(target, delegated_inode);
4148 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004151 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004152 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004153 detach_mounts(dentry);
4154 }
Al Virobec10522010-03-03 14:12:08 -05004155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004157out:
Al Viro59551022016-01-22 15:40:57 -05004158 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
4160 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004161 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
4162 fsnotify_unlink(dir, dentry);
4163 } else if (!error) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004164 fsnotify_link_count(target);
Amir Goldsteina37d9a12022-01-20 23:53:04 +02004165 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 }
Robert Love0eeca282005-07-12 17:06:03 -04004167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 return error;
4169}
Al Viro4d359502014-03-14 12:20:17 -04004170EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172/*
4173 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004174 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 * writeout happening, and we don't want to prevent access to the directory
4176 * while waiting on the I/O.
4177 */
Dmitry Kadashev45f30da2021-07-08 13:34:44 +07004178int do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
Al Viro2ad94ae2008-07-21 09:32:51 -04004180 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004182 struct path path;
4183 struct qstr last;
4184 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004186 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004187 unsigned int lookup_flags = 0;
4188retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004189 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004190 if (error)
4191 goto exit1;
Al Viro2ad94ae2008-07-21 09:32:51 -04004192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004194 if (type != LAST_NORM)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004195 goto exit2;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004196
Al Virof5beed72015-04-30 16:09:11 -04004197 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004198 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004199 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004200retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004201 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004202 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 error = PTR_ERR(dentry);
4204 if (!IS_ERR(dentry)) {
Christian Brauner6521f892021-01-21 14:19:33 +01004205 struct user_namespace *mnt_userns;
4206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004208 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004209 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004211 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004212 goto slashes;
4213 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004214 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004215 if (error)
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004216 goto exit3;
Christian Brauner6521f892021-01-21 14:19:33 +01004217 mnt_userns = mnt_user_ns(path.mnt);
Christian Brauner549c7292021-01-21 14:19:43 +01004218 error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
4219 &delegated_inode);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004220exit3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 dput(dentry);
4222 }
Al Viro59551022016-01-22 15:40:57 -05004223 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 if (inode)
4225 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004226 inode = NULL;
4227 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004228 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004229 if (!error)
4230 goto retry_deleg;
4231 }
Al Virof5beed72015-04-30 16:09:11 -04004232 mnt_drop_write(path.mnt);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004233exit2:
Al Virof5beed72015-04-30 16:09:11 -04004234 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004235 if (retry_estale(error, lookup_flags)) {
4236 lookup_flags |= LOOKUP_REVAL;
4237 inode = NULL;
4238 goto retry;
4239 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004240exit1:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004241 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return error;
4243
4244slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004245 if (d_is_negative(dentry))
4246 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004247 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004248 error = -EISDIR;
4249 else
4250 error = -ENOTDIR;
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004251 goto exit3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004254SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004255{
4256 if ((flag & ~AT_REMOVEDIR) != 0)
4257 return -EINVAL;
4258
4259 if (flag & AT_REMOVEDIR)
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004260 return do_rmdir(dfd, getname(pathname));
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004261 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004262}
4263
Heiko Carstens3480b252009-01-14 14:14:16 +01004264SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004265{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004266 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004267}
4268
Christian Brauner6521f892021-01-21 14:19:33 +01004269/**
4270 * vfs_symlink - create symlink
4271 * @mnt_userns: user namespace of the mount the inode was found from
4272 * @dir: inode of @dentry
4273 * @dentry: pointer to dentry of the base directory
4274 * @oldname: name of the file to link to
4275 *
4276 * Create a symlink.
4277 *
4278 * If the inode has been found through an idmapped mount the user namespace of
4279 * the vfsmount must be passed through @mnt_userns. This function will then take
4280 * care to map the inode according to @mnt_userns before checking permissions.
4281 * On non-idmapped mounts or if permission checking is to be performed on the
4282 * raw inode simply passs init_user_ns.
4283 */
4284int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
4285 struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Christian Brauner6521f892021-01-21 14:19:33 +01004287 int error = may_create(mnt_userns, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289 if (error)
4290 return error;
4291
Al Viroacfa4382008-12-04 10:06:33 -05004292 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return -EPERM;
4294
4295 error = security_inode_symlink(dir, dentry, oldname);
4296 if (error)
4297 return error;
4298
Christian Brauner549c7292021-01-21 14:19:43 +01004299 error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004300 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004301 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return error;
4303}
Al Viro4d359502014-03-14 12:20:17 -04004304EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07004306int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
Al Viro2ad94ae2008-07-21 09:32:51 -04004308 int error;
Dave Hansen6902d922006-09-30 23:29:01 -07004309 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004310 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004311 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004313 if (IS_ERR(from)) {
4314 error = PTR_ERR(from);
4315 goto out_putnames;
4316 }
Jeff Laytonf46d3562012-12-11 12:10:08 -05004317retry:
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004318 dentry = filename_create(newdfd, to, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004319 error = PTR_ERR(dentry);
4320 if (IS_ERR(dentry))
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004321 goto out_putnames;
Dave Hansen6902d922006-09-30 23:29:01 -07004322
Jeff Layton91a27b22012-10-10 15:25:28 -04004323 error = security_path_symlink(&path, dentry, from->name);
Christian Brauner6521f892021-01-21 14:19:33 +01004324 if (!error) {
4325 struct user_namespace *mnt_userns;
4326
4327 mnt_userns = mnt_user_ns(path.mnt);
4328 error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry,
4329 from->name);
4330 }
Al Viro921a1652012-07-20 01:15:31 +04004331 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004332 if (retry_estale(error, lookup_flags)) {
4333 lookup_flags |= LOOKUP_REVAL;
4334 goto retry;
4335 }
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004336out_putnames:
4337 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 putname(from);
4339 return error;
4340}
4341
Dominik Brodowskib724e842018-03-11 11:34:49 +01004342SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4343 int, newdfd, const char __user *, newname)
4344{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004345 return do_symlinkat(getname(oldname), newdfd, getname(newname));
Dominik Brodowskib724e842018-03-11 11:34:49 +01004346}
4347
Heiko Carstens3480b252009-01-14 14:14:16 +01004348SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004349{
Dmitry Kadashevda2d0ce2021-07-08 13:34:41 +07004350 return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004351}
4352
J. Bruce Fields146a8592011-09-20 17:14:31 -04004353/**
4354 * vfs_link - create a new link
4355 * @old_dentry: object to be linked
Christian Brauner6521f892021-01-21 14:19:33 +01004356 * @mnt_userns: the user namespace of the mount
J. Bruce Fields146a8592011-09-20 17:14:31 -04004357 * @dir: new parent
4358 * @new_dentry: where to create the new link
4359 * @delegated_inode: returns inode needing a delegation break
4360 *
4361 * The caller must hold dir->i_mutex
4362 *
4363 * If vfs_link discovers a delegation on the to-be-linked file in need
4364 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4365 * inode in delegated_inode. The caller should then break the delegation
4366 * and retry. Because breaking a delegation may take a long time, the
4367 * caller should drop the i_mutex before doing so.
4368 *
4369 * Alternatively, a caller may pass NULL for delegated_inode. This may
4370 * be appropriate for callers that expect the underlying filesystem not
4371 * to be NFS exported.
Christian Brauner6521f892021-01-21 14:19:33 +01004372 *
4373 * If the inode has been found through an idmapped mount the user namespace of
4374 * the vfsmount must be passed through @mnt_userns. This function will then take
4375 * care to map the inode according to @mnt_userns before checking permissions.
4376 * On non-idmapped mounts or if permission checking is to be performed on the
4377 * raw inode simply passs init_user_ns.
J. Bruce Fields146a8592011-09-20 17:14:31 -04004378 */
Christian Brauner6521f892021-01-21 14:19:33 +01004379int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns,
4380 struct inode *dir, struct dentry *new_dentry,
4381 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382{
4383 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004384 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 int error;
4386
4387 if (!inode)
4388 return -ENOENT;
4389
Christian Brauner6521f892021-01-21 14:19:33 +01004390 error = may_create(mnt_userns, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 if (error)
4392 return error;
4393
4394 if (dir->i_sb != inode->i_sb)
4395 return -EXDEV;
4396
4397 /*
4398 * A link to an append-only or immutable file cannot be created.
4399 */
4400 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4401 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004402 /*
4403 * Updating the link count will likely cause i_uid and i_gid to
4404 * be writen back improperly if their true value is unknown to
4405 * the vfs.
4406 */
Christian Brauner6521f892021-01-21 14:19:33 +01004407 if (HAS_UNMAPPED_ID(mnt_userns, inode))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004408 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004409 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004411 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 return -EPERM;
4413
4414 error = security_inode_link(old_dentry, dir, new_dentry);
4415 if (error)
4416 return error;
4417
Al Viro59551022016-01-22 15:40:57 -05004418 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304419 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004420 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304421 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004422 else if (max_links && inode->i_nlink >= max_links)
4423 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004424 else {
4425 error = try_break_deleg(inode, delegated_inode);
4426 if (!error)
4427 error = dir->i_op->link(old_dentry, dir, new_dentry);
4428 }
Al Virof4e0c302013-06-11 08:34:36 +04004429
4430 if (!error && (inode->i_state & I_LINKABLE)) {
4431 spin_lock(&inode->i_lock);
4432 inode->i_state &= ~I_LINKABLE;
4433 spin_unlock(&inode->i_lock);
4434 }
Al Viro59551022016-01-22 15:40:57 -05004435 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004436 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004437 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return error;
4439}
Al Viro4d359502014-03-14 12:20:17 -04004440EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442/*
4443 * Hardlinks are often used in delicate situations. We avoid
4444 * security-related surprises by not following symlinks on the
4445 * newname. --KAB
4446 *
4447 * We don't follow them on the oldname either to be compatible
4448 * with linux 2.0, and to avoid hard-linking to directories
4449 * and other special files. --ADM
4450 */
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07004451int do_linkat(int olddfd, struct filename *old, int newdfd,
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004452 struct filename *new, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Christian Brauner6521f892021-01-21 14:19:33 +01004454 struct user_namespace *mnt_userns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004456 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004457 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304458 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004461 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
4462 error = -EINVAL;
4463 goto out_putnames;
4464 }
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304465 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004466 * To use null names we require CAP_DAC_READ_SEARCH
4467 * This ensures that not everyone will be able to create
4468 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304469 */
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004470 if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
4471 error = -ENOENT;
4472 goto out_putnames;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004473 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004474
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304475 if (flags & AT_SYMLINK_FOLLOW)
4476 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004477retry:
Stephen Brennan794ebce2021-09-01 10:51:42 -07004478 error = filename_lookup(olddfd, old, how, &old_path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 if (error)
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004480 goto out_putnames;
Al Viro2ad94ae2008-07-21 09:32:51 -04004481
Stephen Brennanb4a4f212021-09-01 10:51:43 -07004482 new_dentry = filename_create(newdfd, new, &new_path,
Jeff Layton442e31c2012-12-20 16:15:38 -05004483 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004485 if (IS_ERR(new_dentry))
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004486 goto out_putpath;
Al Virodae6ad82011-06-26 11:50:15 -04004487
4488 error = -EXDEV;
4489 if (old_path.mnt != new_path.mnt)
4490 goto out_dput;
Christian Brauner549c7292021-01-21 14:19:43 +01004491 mnt_userns = mnt_user_ns(new_path.mnt);
4492 error = may_linkat(mnt_userns, &old_path);
Kees Cook800179c2012-07-25 17:29:07 -07004493 if (unlikely(error))
4494 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004495 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004496 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004497 goto out_dput;
Christian Brauner6521f892021-01-21 14:19:33 +01004498 error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
4499 new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004500out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004501 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004502 if (delegated_inode) {
4503 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004504 if (!error) {
4505 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004506 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004507 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004508 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004509 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004510 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004511 how |= LOOKUP_REVAL;
4512 goto retry;
4513 }
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004514out_putpath:
Al Viro2d8f3032008-07-22 09:59:21 -04004515 path_put(&old_path);
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004516out_putnames:
4517 putname(old);
4518 putname(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 return error;
4521}
4522
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004523SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4524 int, newdfd, const char __user *, newname, int, flags)
4525{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004526 return do_linkat(olddfd, getname_uflags(oldname, flags),
4527 newdfd, getname(newname), flags);
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004528}
4529
Heiko Carstens3480b252009-01-14 14:14:16 +01004530SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004531{
Dmitry Kadashev020250f2021-07-08 13:34:43 +07004532 return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004533}
4534
Miklos Szeredibc270272014-04-01 17:08:42 +02004535/**
4536 * vfs_rename - rename a filesystem object
Randy Dunlap2111c3c2021-02-15 20:29:28 -08004537 * @rd: pointer to &struct renamedata info
Miklos Szeredibc270272014-04-01 17:08:42 +02004538 *
4539 * The caller must hold multiple mutexes--see lock_rename()).
4540 *
4541 * If vfs_rename discovers a delegation in need of breaking at either
4542 * the source or destination, it will return -EWOULDBLOCK and return a
4543 * reference to the inode in delegated_inode. The caller should then
4544 * break the delegation and retry. Because breaking a delegation may
4545 * take a long time, the caller should drop all locks before doing
4546 * so.
4547 *
4548 * Alternatively, a caller may pass NULL for delegated_inode. This may
4549 * be appropriate for callers that expect the underlying filesystem not
4550 * to be NFS exported.
4551 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 * The worst of all namespace operations - renaming directory. "Perverted"
4553 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4554 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004555 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004556 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 * b) race potential - two innocent renames can create a loop together.
4558 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004559 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004561 * c) we have to lock _four_ objects - parents and victim (if it exists),
4562 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004563 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 * whether the target exists). Solution: try to be smart with locking
4565 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004566 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 * move will be locked. Thus we can rank directories by the tree
4568 * (ancestors first) and rank all non-directories after them.
4569 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004570 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 * HOWEVER, it relies on the assumption that any object with ->lookup()
4572 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4573 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004574 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004575 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004577 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 * locking].
4579 */
Christian Brauner9fe61452021-01-21 14:19:32 +01004580int vfs_rename(struct renamedata *rd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
4582 int error;
Christian Brauner9fe61452021-01-21 14:19:32 +01004583 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
4584 struct dentry *old_dentry = rd->old_dentry;
4585 struct dentry *new_dentry = rd->new_dentry;
4586 struct inode **delegated_inode = rd->delegated_inode;
4587 unsigned int flags = rd->flags;
Miklos Szeredibc270272014-04-01 17:08:42 +02004588 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004589 struct inode *source = old_dentry->d_inode;
4590 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004591 bool new_is_dir = false;
4592 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004593 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004595 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004596 return 0;
4597
Christian Brauner6521f892021-01-21 14:19:33 +01004598 error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 if (error)
4600 return error;
4601
Miklos Szeredida1ce062014-04-01 17:08:43 +02004602 if (!target) {
Christian Brauner6521f892021-01-21 14:19:33 +01004603 error = may_create(rd->new_mnt_userns, new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004604 } else {
4605 new_is_dir = d_is_dir(new_dentry);
4606
4607 if (!(flags & RENAME_EXCHANGE))
Christian Brauner6521f892021-01-21 14:19:33 +01004608 error = may_delete(rd->new_mnt_userns, new_dir,
4609 new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004610 else
Christian Brauner6521f892021-01-21 14:19:33 +01004611 error = may_delete(rd->new_mnt_userns, new_dir,
4612 new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 if (error)
4615 return error;
4616
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004617 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 return -EPERM;
4619
Miklos Szeredibc270272014-04-01 17:08:42 +02004620 /*
4621 * If we are going to change the parent - check write permissions,
4622 * we'll need to flip '..'.
4623 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004624 if (new_dir != old_dir) {
4625 if (is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004626 error = inode_permission(rd->old_mnt_userns, source,
Christian Brauner47291ba2021-01-21 14:19:24 +01004627 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004628 if (error)
4629 return error;
4630 }
4631 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Christian Brauner6521f892021-01-21 14:19:33 +01004632 error = inode_permission(rd->new_mnt_userns, target,
Christian Brauner47291ba2021-01-21 14:19:24 +01004633 MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004634 if (error)
4635 return error;
4636 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004637 }
Robert Love0eeca282005-07-12 17:06:03 -04004638
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004639 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4640 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004641 if (error)
4642 return error;
4643
Al Viro49d31c22017-07-07 14:51:19 -04004644 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004645 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004646 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004647 lock_two_nondirectories(source, target);
4648 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004649 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004650
Hugh Dickins51cc3a62021-09-02 14:53:57 -07004651 error = -EPERM;
4652 if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target)))
4653 goto out;
4654
Miklos Szeredibc270272014-04-01 17:08:42 +02004655 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004656 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004657 goto out;
4658
Miklos Szeredida1ce062014-04-01 17:08:43 +02004659 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004660 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004661 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004662 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004663 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4664 old_dir->i_nlink >= max_links)
4665 goto out;
4666 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004667 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004668 error = try_break_deleg(source, delegated_inode);
4669 if (error)
4670 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004671 }
4672 if (target && !new_is_dir) {
4673 error = try_break_deleg(target, delegated_inode);
4674 if (error)
4675 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004676 }
Christian Brauner549c7292021-01-21 14:19:43 +01004677 error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry,
4678 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004679 if (error)
4680 goto out;
4681
Miklos Szeredida1ce062014-04-01 17:08:43 +02004682 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004683 if (is_dir) {
4684 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004685 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004686 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004687 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004688 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004689 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004690 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4691 if (!(flags & RENAME_EXCHANGE))
4692 d_move(old_dentry, new_dentry);
4693 else
4694 d_exchange(old_dentry, new_dentry);
4695 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004696out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004697 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004698 unlock_two_nondirectories(source, target);
4699 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004700 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004701 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004702 if (!error) {
Al Virof4ec3a32019-04-26 13:21:24 -04004703 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004704 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4705 if (flags & RENAME_EXCHANGE) {
Al Virof4ec3a32019-04-26 13:21:24 -04004706 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004707 new_is_dir, NULL, new_dentry);
4708 }
4709 }
Al Viro49d31c22017-07-07 14:51:19 -04004710 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 return error;
4713}
Al Viro4d359502014-03-14 12:20:17 -04004714EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Jens Axboee8866632020-09-26 17:20:17 -06004716int do_renameat2(int olddfd, struct filename *from, int newdfd,
4717 struct filename *to, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
Christian Brauner9fe61452021-01-21 14:19:32 +01004719 struct renamedata rd;
Al Viro2ad94ae2008-07-21 09:32:51 -04004720 struct dentry *old_dentry, *new_dentry;
4721 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004722 struct path old_path, new_path;
4723 struct qstr old_last, new_last;
4724 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004725 struct inode *delegated_inode = NULL;
Al Virof5beed72015-04-30 16:09:11 -04004726 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004727 bool should_retry = false;
Jens Axboee8866632020-09-26 17:20:17 -06004728 int error = -EINVAL;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004729
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004730 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004731 goto put_names;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004732
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004733 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4734 (flags & RENAME_EXCHANGE))
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004735 goto put_names;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004736
Al Virof5beed72015-04-30 16:09:11 -04004737 if (flags & RENAME_EXCHANGE)
4738 target_flags = 0;
4739
Jeff Laytonc6a94282012-12-11 12:10:10 -05004740retry:
Al Viroc5f563f2021-09-07 15:57:42 -04004741 error = filename_parentat(olddfd, from, lookup_flags, &old_path,
4742 &old_last, &old_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004743 if (error)
4744 goto put_names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Al Viroc5f563f2021-09-07 15:57:42 -04004746 error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4747 &new_type);
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004748 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 goto exit1;
4750
4751 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004752 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 goto exit2;
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004756 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 goto exit2;
4758
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004759 if (flags & RENAME_NOREPLACE)
4760 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004761 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 goto exit2;
4763
Al Virof5beed72015-04-30 16:09:11 -04004764 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004765 if (error)
4766 goto exit2;
4767
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004768retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004769 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Al Virof5beed72015-04-30 16:09:11 -04004771 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 error = PTR_ERR(old_dentry);
4773 if (IS_ERR(old_dentry))
4774 goto exit3;
4775 /* source must exist */
4776 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004777 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004779 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 error = PTR_ERR(new_dentry);
4781 if (IS_ERR(new_dentry))
4782 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004783 error = -EEXIST;
4784 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4785 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004786 if (flags & RENAME_EXCHANGE) {
4787 error = -ENOENT;
4788 if (d_is_negative(new_dentry))
4789 goto exit5;
4790
4791 if (!d_is_dir(new_dentry)) {
4792 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004793 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004794 goto exit5;
4795 }
4796 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004797 /* unless the source is a directory trailing slashes give -ENOTDIR */
4798 if (!d_is_dir(old_dentry)) {
4799 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004800 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004801 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004802 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004803 goto exit5;
4804 }
4805 /* source should not be ancestor of target */
4806 error = -EINVAL;
4807 if (old_dentry == trap)
4808 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004810 if (!(flags & RENAME_EXCHANGE))
4811 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 if (new_dentry == trap)
4813 goto exit5;
4814
Al Virof5beed72015-04-30 16:09:11 -04004815 error = security_path_rename(&old_path, old_dentry,
4816 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004817 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004818 goto exit5;
Christian Brauner9fe61452021-01-21 14:19:32 +01004819
4820 rd.old_dir = old_path.dentry->d_inode;
4821 rd.old_dentry = old_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004822 rd.old_mnt_userns = mnt_user_ns(old_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004823 rd.new_dir = new_path.dentry->d_inode;
4824 rd.new_dentry = new_dentry;
Christian Brauner6521f892021-01-21 14:19:33 +01004825 rd.new_mnt_userns = mnt_user_ns(new_path.mnt);
Christian Brauner9fe61452021-01-21 14:19:32 +01004826 rd.delegated_inode = &delegated_inode;
4827 rd.flags = flags;
4828 error = vfs_rename(&rd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829exit5:
4830 dput(new_dentry);
4831exit4:
4832 dput(old_dentry);
4833exit3:
Al Virof5beed72015-04-30 16:09:11 -04004834 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004835 if (delegated_inode) {
4836 error = break_deleg_wait(&delegated_inode);
4837 if (!error)
4838 goto retry_deleg;
4839 }
Al Virof5beed72015-04-30 16:09:11 -04004840 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004842 if (retry_estale(error, lookup_flags))
4843 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004844 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845exit1:
Al Virof5beed72015-04-30 16:09:11 -04004846 path_put(&old_path);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004847 if (should_retry) {
4848 should_retry = false;
4849 lookup_flags |= LOOKUP_REVAL;
4850 goto retry;
4851 }
Dmitry Kadashev0ee50b42021-07-08 13:34:38 +07004852put_names:
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004853 putname(from);
Dmitry Kadashev91ef6582021-07-08 13:34:37 +07004854 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return error;
4856}
4857
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004858SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4859 int, newdfd, const char __user *, newname, unsigned int, flags)
4860{
Jens Axboee8866632020-09-26 17:20:17 -06004861 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4862 flags);
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004863}
4864
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004865SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4866 int, newdfd, const char __user *, newname)
4867{
Jens Axboee8866632020-09-26 17:20:17 -06004868 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4869 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004870}
4871
Heiko Carstensa26eab22009-01-14 14:14:17 +01004872SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004873{
Jens Axboee8866632020-09-26 17:20:17 -06004874 return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4875 getname(newname), 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004876}
4877
Al Viro5d826c82014-03-14 13:42:45 -04004878int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Al Viro5d826c82014-03-14 13:42:45 -04004880 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 if (IS_ERR(link))
4882 goto out;
4883
4884 len = strlen(link);
4885 if (len > (unsigned) buflen)
4886 len = buflen;
4887 if (copy_to_user(buffer, link, len))
4888 len = -EFAULT;
4889out:
4890 return len;
4891}
4892
Miklos Szeredid60874c2016-10-04 14:40:45 +02004893/**
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004894 * vfs_readlink - copy symlink body into userspace buffer
4895 * @dentry: dentry on which to get symbolic link
4896 * @buffer: user memory pointer
4897 * @buflen: size of buffer
4898 *
4899 * Does not touch atime. That's up to the caller if necessary
4900 *
4901 * Does not call security hook.
4902 */
4903int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4904{
4905 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04004906 DEFINE_DELAYED_CALL(done);
4907 const char *link;
4908 int res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004909
Miklos Szeredi76fca902016-12-09 16:45:04 +01004910 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4911 if (unlikely(inode->i_op->readlink))
4912 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004913
Miklos Szeredi76fca902016-12-09 16:45:04 +01004914 if (!d_is_symlink(dentry))
4915 return -EINVAL;
4916
4917 spin_lock(&inode->i_lock);
4918 inode->i_opflags |= IOP_DEFAULT_READLINK;
4919 spin_unlock(&inode->i_lock);
4920 }
4921
Eric Biggers4c4f7c12019-04-10 13:21:14 -07004922 link = READ_ONCE(inode->i_link);
Al Virof2df5da2018-07-19 17:35:51 -04004923 if (!link) {
4924 link = inode->i_op->get_link(dentry, inode, &done);
4925 if (IS_ERR(link))
4926 return PTR_ERR(link);
4927 }
4928 res = readlink_copy(buffer, buflen, link);
4929 do_delayed_call(&done);
4930 return res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004931}
4932EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Miklos Szeredid60874c2016-10-04 14:40:45 +02004934/**
4935 * vfs_get_link - get symlink body
4936 * @dentry: dentry on which to get symbolic link
4937 * @done: caller needs to free returned data with this
4938 *
4939 * Calls security hook and i_op->get_link() on the supplied inode.
4940 *
4941 * It does not touch atime. That's up to the caller if necessary.
4942 *
4943 * Does not work on "special" symlinks like /proc/$$/fd/N
4944 */
4945const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4946{
4947 const char *res = ERR_PTR(-EINVAL);
4948 struct inode *inode = d_inode(dentry);
4949
4950 if (d_is_symlink(dentry)) {
4951 res = ERR_PTR(security_inode_readlink(dentry));
4952 if (!res)
4953 res = inode->i_op->get_link(dentry, inode, done);
4954 }
4955 return res;
4956}
4957EXPORT_SYMBOL(vfs_get_link);
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004960const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004961 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004963 char *kaddr;
4964 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004965 struct address_space *mapping = inode->i_mapping;
4966
Al Virod3883d42015-11-17 10:41:04 -05004967 if (!dentry) {
4968 page = find_get_page(mapping, 0);
4969 if (!page)
4970 return ERR_PTR(-ECHILD);
4971 if (!PageUptodate(page)) {
4972 put_page(page);
4973 return ERR_PTR(-ECHILD);
4974 }
4975 } else {
4976 page = read_mapping_page(mapping, 0, NULL);
4977 if (IS_ERR(page))
4978 return (char*)page;
4979 }
Al Virofceef392015-12-29 15:58:39 -05004980 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004981 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4982 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004983 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004984 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
4986
Al Viro6b255392015-11-17 10:20:54 -05004987EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Al Virofceef392015-12-29 15:58:39 -05004989void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990{
Al Virofceef392015-12-29 15:58:39 -05004991 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
Al Viro4d359502014-03-14 12:20:17 -04004993EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Al Viroaa80dea2015-11-16 18:26:34 -05004995int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4996{
Al Virofceef392015-12-29 15:58:39 -05004997 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004998 int res = readlink_copy(buffer, buflen,
4999 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05005000 &done));
5001 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05005002 return res;
5003}
5004EXPORT_SYMBOL(page_readlink);
5005
Nick Piggin54566b22009-01-04 12:00:53 -08005006/*
5007 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
5008 */
5009int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
5011 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005012 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07005013 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08005014 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07005015 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08005016 if (nofs)
5017 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
NeilBrown7e53cac2006-03-25 03:07:57 -08005019retry:
Nick Pigginafddba42007-10-16 01:25:01 -07005020 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08005021 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07005023 goto fail;
5024
Al Viro21fc61c2015-11-17 01:07:57 -05005025 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07005026
5027 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
5028 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 if (err < 0)
5030 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07005031 if (err < len-1)
5032 goto retry;
5033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 mark_inode_dirty(inode);
5035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036fail:
5037 return err;
5038}
Al Viro4d359502014-03-14 12:20:17 -04005039EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005041int page_symlink(struct inode *inode, const char *symname, int len)
5042{
5043 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08005044 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005045}
Al Viro4d359502014-03-14 12:20:17 -04005046EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08005047
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005048const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05005049 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051EXPORT_SYMBOL(page_symlink_inode_operations);