Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 19 | #include <linux/export.h> |
David S. Miller | 4469690 | 2012-05-23 20:12:50 -0700 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 25 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/personality.h> |
| 27 | #include <linux/security.h> |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 28 | #include <linux/ima.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/syscalls.h> |
| 30 | #include <linux/mount.h> |
| 31 | #include <linux/audit.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 32 | #include <linux/capability.h> |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 33 | #include <linux/file.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 34 | #include <linux/fcntl.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 35 | #include <linux/device_cgroup.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 36 | #include <linux/fs_struct.h> |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
Linus Torvalds | 99d263d | 2014-09-13 11:30:10 -0700 | [diff] [blame] | 38 | #include <linux/hash.h> |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 39 | #include <linux/bitops.h> |
Eric W. Biederman | aeaa4a7 | 2016-07-23 11:20:44 -0500 | [diff] [blame] | 40 | #include <linux/init_task.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 41 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 43 | #include "internal.h" |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 44 | #include "mount.h" |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* [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 Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 80 | * the name is a symlink pointing to a non-existent name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
| 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 Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 113 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * 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 Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 124 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 125 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 126 | |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 127 | struct filename * |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 128 | getname_flags(const char __user *filename, int flags, int *empty) |
| 129 | { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 130 | struct filename *result; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 131 | char *kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 132 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Jeff Layton | 7ac8626 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 134 | result = audit_reusename(filename); |
| 135 | if (result) |
| 136 | return result; |
| 137 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 138 | result = __getname(); |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 139 | if (unlikely(!result)) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 140 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 142 | /* |
| 143 | * First, try to embed the struct filename inside the names_cache |
| 144 | * allocation |
| 145 | */ |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 146 | kname = (char *)result->iname; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 147 | result->name = kname; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 148 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 149 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 150 | if (unlikely(len < 0)) { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 151 | __putname(result); |
| 152 | return ERR_PTR(len); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 153 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 154 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 155 | /* |
| 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 Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 161 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 162 | const size_t size = offsetof(struct filename, iname[1]); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 163 | kname = (char *)result; |
| 164 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 165 | /* |
| 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 Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 171 | if (unlikely(!result)) { |
| 172 | __putname(kname); |
| 173 | return ERR_PTR(-ENOMEM); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 174 | } |
| 175 | result->name = kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 176 | 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 Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 189 | result->refcnt = 1; |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 190 | /* The empty path is special. */ |
| 191 | if (unlikely(!len)) { |
| 192 | if (empty) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 193 | *empty = 1; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 194 | if (!(flags & LOOKUP_EMPTY)) { |
| 195 | putname(result); |
| 196 | return ERR_PTR(-ENOENT); |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 199 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 200 | result->uptr = filename; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 201 | result->aname = NULL; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 202 | audit_getname(result); |
| 203 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 206 | struct filename * |
Dmitry Kadashev | 8228e2c | 2021-07-08 13:34:42 +0700 | [diff] [blame] | 207 | getname_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 | |
| 214 | struct filename * |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 215 | getname(const char __user * filename) |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 216 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 217 | return getname_flags(filename, 0, NULL); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 220 | struct filename * |
| 221 | getname_kernel(const char * filename) |
| 222 | { |
| 223 | struct filename *result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 224 | int len = strlen(filename) + 1; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 225 | |
| 226 | result = __getname(); |
| 227 | if (unlikely(!result)) |
| 228 | return ERR_PTR(-ENOMEM); |
| 229 | |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 230 | if (len <= EMBEDDED_NAME_MAX) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 231 | result->name = (char *)result->iname; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 232 | } else if (len <= PATH_MAX) { |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 233 | const size_t size = offsetof(struct filename, iname[1]); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 234 | struct filename *tmp; |
| 235 | |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 236 | tmp = kmalloc(size, GFP_KERNEL); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 237 | if (unlikely(!tmp)) { |
| 238 | __putname(result); |
| 239 | return ERR_PTR(-ENOMEM); |
| 240 | } |
| 241 | tmp->name = (char *)result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 242 | result = tmp; |
| 243 | } else { |
| 244 | __putname(result); |
| 245 | return ERR_PTR(-ENAMETOOLONG); |
| 246 | } |
| 247 | memcpy((char *)result->name, filename, len); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 248 | result->uptr = NULL; |
| 249 | result->aname = NULL; |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 250 | result->refcnt = 1; |
Paul Moore | fd3522f | 2015-01-22 00:00:10 -0500 | [diff] [blame] | 251 | audit_getname(result); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 252 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 253 | return result; |
| 254 | } |
| 255 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 256 | void putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Al Viro | ea47ab1 | 2021-09-07 16:14:05 -0400 | [diff] [blame] | 258 | if (IS_ERR(name)) |
Dmitry Kadashev | 91ef658 | 2021-07-08 13:34:37 +0700 | [diff] [blame] | 259 | return; |
| 260 | |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 261 | BUG_ON(name->refcnt <= 0); |
| 262 | |
| 263 | if (--name->refcnt > 0) |
| 264 | return; |
| 265 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 266 | if (name->name != name->iname) { |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 267 | __putname(name->name); |
| 268 | kfree(name); |
| 269 | } else |
| 270 | __putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 273 | /** |
| 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 | */ |
| 289 | static int check_acl(struct user_namespace *mnt_userns, |
| 290 | struct inode *inode, int mask) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 291 | { |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 292 | #ifdef CONFIG_FS_POSIX_ACL |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 293 | struct posix_acl *acl; |
| 294 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 295 | if (mask & MAY_NOT_BLOCK) { |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 296 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
| 297 | if (!acl) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 298 | return -EAGAIN; |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 299 | /* no ->get_acl() calls in RCU mode... */ |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 300 | if (is_uncached_acl(acl)) |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 301 | return -ECHILD; |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 302 | return posix_acl_permission(mnt_userns, inode, acl, mask); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 305 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
| 306 | if (IS_ERR(acl)) |
| 307 | return PTR_ERR(acl); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 308 | if (acl) { |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 309 | int error = posix_acl_permission(mnt_userns, inode, acl, mask); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 310 | posix_acl_release(acl); |
| 311 | return error; |
| 312 | } |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 313 | #endif |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 314 | |
| 315 | return -EAGAIN; |
| 316 | } |
| 317 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 318 | /** |
| 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 Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 323 | * |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 324 | * 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 Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 333 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 334 | static int acl_permission_check(struct user_namespace *mnt_userns, |
| 335 | struct inode *inode, int mask) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 336 | { |
Linus Torvalds | 26cf46b | 2011-05-13 11:51:01 -0700 | [diff] [blame] | 337 | unsigned int mode = inode->i_mode; |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 338 | kuid_t i_uid; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 339 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 340 | /* Are we the owner? If so, ACL's don't matter */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 341 | i_uid = i_uid_into_mnt(mnt_userns, inode); |
| 342 | if (likely(uid_eq(current_fsuid(), i_uid))) { |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 343 | mask &= 7; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 344 | mode >>= 6; |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 345 | return (mask & ~mode) ? -EACCES : 0; |
| 346 | } |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 347 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 348 | /* Do we have ACL's? */ |
| 349 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 350 | int error = check_acl(mnt_userns, inode, mask); |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 351 | 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 Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 364 | kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); |
| 365 | if (in_group_p(kgid)) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 366 | mode >>= 3; |
| 367 | } |
| 368 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 369 | /* Bits in 'mode' clear that we require? */ |
| 370 | return (mask & ~mode) ? -EACCES : 0; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | /** |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 374 | * generic_permission - check for access rights on a Posix-like filesystem |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 375 | * @mnt_userns: user namespace of the mount the inode was found from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | * @inode: inode to check access rights for |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 377 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, |
| 378 | * %MAY_NOT_BLOCK ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | * |
| 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 Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 383 | * 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 Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 388 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 395 | int generic_permission(struct user_namespace *mnt_userns, struct inode *inode, |
| 396 | int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 398 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 401 | * Do the basic permission checks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 403 | ret = acl_permission_check(mnt_userns, inode, mask); |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 404 | if (ret != -EACCES) |
| 405 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 407 | if (S_ISDIR(inode->i_mode)) { |
| 408 | /* DACs are overridable for directories */ |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 409 | if (!(mask & MAY_WRITE)) |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 410 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 411 | CAP_DAC_READ_SEARCH)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 412 | return 0; |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 413 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Christian Brauner | 0558c1b | 2021-01-21 14:19:23 +0100 | [diff] [blame] | 414 | CAP_DAC_OVERRIDE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 416 | return -EACCES; |
| 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | /* |
| 420 | * Searching includes executable on directories, else just read. |
| 421 | */ |
Serge E. Hallyn | 7ea6600 | 2009-12-29 14:50:19 -0600 | [diff] [blame] | 422 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 423 | if (mask == MAY_READ) |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 424 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Christian Brauner | 0558c1b | 2021-01-21 14:19:23 +0100 | [diff] [blame] | 425 | CAP_DAC_READ_SEARCH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 427 | /* |
| 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 Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 433 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Christian Brauner | 0558c1b | 2021-01-21 14:19:23 +0100 | [diff] [blame] | 434 | CAP_DAC_OVERRIDE)) |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 435 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | return -EACCES; |
| 438 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 439 | EXPORT_SYMBOL(generic_permission); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 441 | /** |
| 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 Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 447 | * 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 Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 452 | static inline int do_inode_permission(struct user_namespace *mnt_userns, |
| 453 | struct inode *inode, int mask) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 454 | { |
| 455 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { |
| 456 | if (likely(inode->i_op->permission)) |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 457 | return inode->i_op->permission(mnt_userns, inode, mask); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 458 | |
| 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 Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 464 | return generic_permission(mnt_userns, inode, mask); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 467 | /** |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 468 | * sb_permission - Check superblock-level permissions |
| 469 | * @sb: Superblock of inode to check permission on |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 470 | * @inode: Inode to check permission on |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 471 | * @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 | */ |
| 475 | static 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 Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 481 | if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 482 | return -EROFS; |
| 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * inode_permission - Check for access rights to a given inode |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 489 | * @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 Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 492 | * |
| 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 Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 499 | int inode_permission(struct user_namespace *mnt_userns, |
| 500 | struct inode *inode, int mask) |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 501 | { |
| 502 | int retval; |
| 503 | |
| 504 | retval = sb_permission(inode->i_sb, inode, mask); |
| 505 | if (retval) |
| 506 | return retval; |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 507 | |
| 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 Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 520 | if (HAS_UNMAPPED_ID(mnt_userns, inode)) |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 521 | return -EACCES; |
| 522 | } |
| 523 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 524 | retval = do_inode_permission(mnt_userns, inode, mask); |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 525 | 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 Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 533 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 534 | EXPORT_SYMBOL(inode_permission); |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 535 | |
| 536 | /** |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 537 | * 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 Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 542 | void path_get(const struct path *path) |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 543 | { |
| 544 | mntget(path->mnt); |
| 545 | dget(path->dentry); |
| 546 | } |
| 547 | EXPORT_SYMBOL(path_get); |
| 548 | |
| 549 | /** |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 550 | * 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 Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 555 | void path_put(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 557 | dput(path->dentry); |
| 558 | mntput(path->mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 560 | EXPORT_SYMBOL(path_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 562 | #define EMBEDDED_LEVELS 2 |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 563 | struct nameidata { |
| 564 | struct path path; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 565 | struct qstr last; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 566 | struct path root; |
| 567 | struct inode *inode; /* path.dentry.d_inode */ |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 568 | unsigned int flags, state; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 569 | unsigned seq, m_seq, r_seq; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 570 | int last_type; |
| 571 | unsigned depth; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 572 | int total_link_count; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 573 | struct saved { |
| 574 | struct path link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 575 | struct delayed_call done; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 576 | const char *name; |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 577 | unsigned seq; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 578 | } *stack, internal[EMBEDDED_LEVELS]; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 579 | struct filename *name; |
| 580 | struct nameidata *saved; |
| 581 | unsigned root_seq; |
| 582 | int dfd; |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 583 | kuid_t dir_uid; |
| 584 | umode_t dir_mode; |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 585 | } __randomize_layout; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 586 | |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 587 | #define ND_ROOT_PRESET 1 |
| 588 | #define ND_ROOT_GRABBED 2 |
| 589 | #define ND_JUMPED 4 |
| 590 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 591 | static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 592 | { |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 593 | struct nameidata *old = current->nameidata; |
| 594 | p->stack = p->internal; |
Al Viro | 7962c7d | 2021-04-03 16:49:44 -0400 | [diff] [blame] | 595 | p->depth = 0; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 596 | p->dfd = dfd; |
| 597 | p->name = name; |
Al Viro | 7d01ef7 | 2021-04-06 12:33:07 -0400 | [diff] [blame] | 598 | p->path.mnt = NULL; |
| 599 | p->path.dentry = NULL; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 600 | p->total_link_count = old ? old->total_link_count : 0; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 601 | p->saved = old; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 602 | current->nameidata = p; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 605 | static 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 Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 616 | static void restore_nameidata(void) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 617 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 618 | struct nameidata *now = current->nameidata, *old = now->saved; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 619 | |
| 620 | current->nameidata = old; |
| 621 | if (old) |
| 622 | old->total_link_count = now->total_link_count; |
Al Viro | e1a63bb | 2015-12-05 21:06:33 -0500 | [diff] [blame] | 623 | if (now->stack != now->internal) |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 624 | kfree(now->stack); |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 625 | } |
| 626 | |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 627 | static bool nd_alloc_stack(struct nameidata *nd) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 628 | { |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 629 | struct saved *p; |
| 630 | |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 631 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
| 632 | nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); |
| 633 | if (unlikely(!p)) |
| 634 | return false; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 635 | memcpy(p, nd->internal, sizeof(nd->internal)); |
| 636 | nd->stack = p; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 637 | return true; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 638 | } |
| 639 | |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 640 | /** |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 641 | * path_connected - Verify that a dentry is below mnt.mnt_root |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 642 | * |
| 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 Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 646 | static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 647 | { |
Eric W. Biederman | 95dd775 | 2018-03-14 18:20:29 -0500 | [diff] [blame] | 648 | struct super_block *sb = mnt->mnt_sb; |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 649 | |
Christoph Hellwig | 402dd2c | 2020-09-24 08:51:28 +0200 | [diff] [blame] | 650 | /* Bind mounts can have disconnected paths */ |
| 651 | if (mnt->mnt_root == sb->s_root) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 652 | return true; |
| 653 | |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 654 | return is_subdir(dentry, mnt->mnt_root); |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 655 | } |
| 656 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 657 | static void drop_links(struct nameidata *nd) |
| 658 | { |
| 659 | int i = nd->depth; |
| 660 | while (i--) { |
| 661 | struct saved *last = nd->stack + i; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 662 | do_delayed_call(&last->done); |
| 663 | clear_delayed_call(&last->done); |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | static 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 Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 675 | if (nd->state & ND_ROOT_GRABBED) { |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 676 | path_put(&nd->root); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 677 | nd->state &= ~ND_ROOT_GRABBED; |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 678 | } |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 679 | } else { |
| 680 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 681 | rcu_read_unlock(); |
| 682 | } |
| 683 | nd->depth = 0; |
Al Viro | 7d01ef7 | 2021-04-06 12:33:07 -0400 | [diff] [blame] | 684 | nd->path.mnt = NULL; |
| 685 | nd->path.dentry = NULL; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* path_put is needed afterwards regardless of success or failure */ |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 689 | static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 690 | { |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 691 | int res = __legitimize_mnt(path->mnt, mseq); |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 692 | 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 Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 705 | static inline bool legitimize_path(struct nameidata *nd, |
| 706 | struct path *path, unsigned seq) |
| 707 | { |
Al Viro | 5bd7328 | 2020-04-05 21:59:55 -0400 | [diff] [blame] | 708 | return __legitimize_path(path, seq, nd->m_seq); |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 709 | } |
| 710 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 711 | static bool legitimize_links(struct nameidata *nd) |
| 712 | { |
| 713 | int i; |
Al Viro | eacd9aa8 | 2021-02-15 12:03:23 -0500 | [diff] [blame] | 714 | if (unlikely(nd->flags & LOOKUP_CACHED)) { |
| 715 | drop_links(nd); |
| 716 | nd->depth = 0; |
| 717 | return false; |
| 718 | } |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 719 | 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 Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 730 | static bool legitimize_root(struct nameidata *nd) |
| 731 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 732 | /* |
| 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 Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 740 | if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 741 | return true; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 742 | nd->state |= ND_ROOT_GRABBED; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 743 | return legitimize_path(nd, &nd->root, nd->root_seq); |
| 744 | } |
| 745 | |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 746 | /* |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 747 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 748 | * 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 Marshall | 57e3715 | 2015-11-30 11:11:59 -0500 | [diff] [blame] | 750 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 751 | * 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 Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 755 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 756 | |
| 757 | /** |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 758 | * try_to_unlazy - try to switch to ref-walk mode. |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 759 | * @nd: nameidata pathwalk data |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 760 | * Returns: true on success, false on failure |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 761 | * |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 762 | * try_to_unlazy attempts to legitimize the current nd->path and nd->root |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 763 | * for ref-walk mode. |
| 764 | * Must be called from rcu-walk context. |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 765 | * Nothing should touch nameidata between try_to_unlazy() failure and |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 766 | * terminate_walk(). |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 767 | */ |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 768 | static bool try_to_unlazy(struct nameidata *nd) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 769 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 770 | struct dentry *parent = nd->path.dentry; |
| 771 | |
| 772 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 773 | |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 774 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 775 | if (unlikely(!legitimize_links(nd))) |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 776 | goto out1; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 777 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
| 778 | goto out; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 779 | if (unlikely(!legitimize_root(nd))) |
| 780 | goto out; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 781 | rcu_read_unlock(); |
| 782 | BUG_ON(nd->inode != parent->d_inode); |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 783 | return true; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 784 | |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 785 | out1: |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 786 | nd->path.mnt = NULL; |
| 787 | nd->path.dentry = NULL; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 788 | out: |
| 789 | rcu_read_unlock(); |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 790 | return false; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 794 | * try_to_unlazy_next - try to switch to ref-walk mode. |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 795 | * @nd: nameidata pathwalk data |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 796 | * @dentry: next dentry to step into |
| 797 | * @seq: seq number to check @dentry against |
| 798 | * Returns: true on success, false on failure |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 799 | * |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 800 | * 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 Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 804 | * terminate_walk(). |
| 805 | */ |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 806 | static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq) |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 807 | { |
| 808 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
| 809 | |
| 810 | nd->flags &= ~LOOKUP_RCU; |
| 811 | if (unlikely(!legitimize_links(nd))) |
| 812 | goto out2; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 813 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) |
| 814 | goto out2; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 815 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 816 | goto out1; |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 817 | |
Linus Torvalds | 1557008 | 2013-09-02 11:38:06 -0700 | [diff] [blame] | 818 | /* |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 819 | * 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 Torvalds | 1557008 | 2013-09-02 11:38:06 -0700 | [diff] [blame] | 824 | */ |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 825 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
| 826 | goto out; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 827 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
| 828 | goto out_dput; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 829 | /* |
| 830 | * Sequence counts matched. Now make sure that the root is |
| 831 | * still valid and get it if required. |
| 832 | */ |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 833 | if (unlikely(!legitimize_root(nd))) |
| 834 | goto out_dput; |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 835 | rcu_read_unlock(); |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 836 | return true; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 837 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 838 | out2: |
| 839 | nd->path.mnt = NULL; |
| 840 | out1: |
| 841 | nd->path.dentry = NULL; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 842 | out: |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 843 | rcu_read_unlock(); |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 844 | return false; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 845 | out_dput: |
| 846 | rcu_read_unlock(); |
| 847 | dput(dentry); |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 848 | return false; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 849 | } |
| 850 | |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 851 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 852 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 853 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
| 854 | return dentry->d_op->d_revalidate(dentry, flags); |
| 855 | else |
| 856 | return 1; |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 857 | } |
| 858 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 859 | /** |
| 860 | * complete_walk - successful completion of path walk |
| 861 | * @nd: pointer nameidata |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 862 | * |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 863 | * 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 Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 868 | */ |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 869 | static int complete_walk(struct nameidata *nd) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 870 | { |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 871 | struct dentry *dentry = nd->path.dentry; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 872 | int status; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 873 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 874 | if (nd->flags & LOOKUP_RCU) { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 875 | /* |
| 876 | * We don't want to zero nd->root for scoped-lookups or |
| 877 | * externally-managed nd->root. |
| 878 | */ |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 879 | if (!(nd->state & ND_ROOT_PRESET)) |
| 880 | if (!(nd->flags & LOOKUP_IS_SCOPED)) |
| 881 | nd->root.mnt = NULL; |
Jens Axboe | 6c6ec2b | 2020-12-17 09:19:09 -0700 | [diff] [blame] | 882 | nd->flags &= ~LOOKUP_CACHED; |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 883 | if (!try_to_unlazy(nd)) |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 884 | return -ECHILD; |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 885 | } |
| 886 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 887 | 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 Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 908 | if (likely(!(nd->state & ND_JUMPED))) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 909 | return 0; |
| 910 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 911 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 912 | return 0; |
| 913 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 914 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 915 | if (status > 0) |
| 916 | return 0; |
| 917 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 918 | if (!status) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 919 | status = -ESTALE; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 920 | |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 921 | return status; |
| 922 | } |
| 923 | |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 924 | static int set_root(struct nameidata *nd) |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 925 | { |
Al Viro | 7bd8837 | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 926 | struct fs_struct *fs = current->fs; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 927 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 928 | /* |
| 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 Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 936 | 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 Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 946 | nd->state |= ND_ROOT_GRABBED; |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 947 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 948 | return 0; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 949 | } |
| 950 | |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 951 | static int nd_jump_root(struct nameidata *nd) |
| 952 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 953 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 954 | return -EXDEV; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 955 | 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 Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 960 | if (!nd->root.mnt) { |
| 961 | int error = set_root(nd); |
| 962 | if (error) |
| 963 | return error; |
| 964 | } |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 965 | 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 Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 979 | nd->state |= ND_JUMPED; |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 980 | return 0; |
| 981 | } |
| 982 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 983 | /* |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 984 | * Helper to directly jump to a known parsed path from ->get_link, |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 985 | * caller must have taken a reference to path beforehand. |
| 986 | */ |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 987 | int nd_jump_link(struct path *path) |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 988 | { |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 989 | int error = -ELOOP; |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 990 | struct nameidata *nd = current->nameidata; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 991 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 992 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
| 993 | goto err; |
| 994 | |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 995 | error = -EXDEV; |
| 996 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 997 | if (nd->path.mnt != path->mnt) |
| 998 | goto err; |
| 999 | } |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 1000 | /* Not currently safe for scoped-lookups. */ |
| 1001 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) |
| 1002 | goto err; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 1003 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 1004 | path_put(&nd->path); |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 1005 | nd->path = *path; |
| 1006 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 1007 | nd->state |= ND_JUMPED; |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 1008 | return 0; |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 1009 | |
| 1010 | err: |
| 1011 | path_put(path); |
| 1012 | return error; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 1013 | } |
| 1014 | |
Al Viro | b9ff442 | 2015-05-02 20:19:23 -0400 | [diff] [blame] | 1015 | static inline void put_link(struct nameidata *nd) |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1016 | { |
Al Viro | 21c3003 | 2015-05-03 21:06:24 -0400 | [diff] [blame] | 1017 | struct saved *last = nd->stack + --nd->depth; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1018 | do_delayed_call(&last->done); |
Al Viro | 6548fae | 2015-05-07 20:32:22 -0400 | [diff] [blame] | 1019 | if (!(nd->flags & LOOKUP_RCU)) |
| 1020 | path_put(&last->link); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
Linus Torvalds | 561ec64 | 2012-10-26 10:05:07 -0700 | [diff] [blame] | 1023 | int sysctl_protected_symlinks __read_mostly = 0; |
| 1024 | int sysctl_protected_hardlinks __read_mostly = 0; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1025 | int sysctl_protected_fifos __read_mostly; |
| 1026 | int sysctl_protected_regular __read_mostly; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1027 | |
| 1028 | /** |
| 1029 | * may_follow_link - Check symlink following for unsafe situations |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 1030 | * @nd: nameidata pathwalk data |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1031 | * |
| 1032 | * In the case of the sysctl_protected_symlinks sysctl being enabled, |
| 1033 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is |
| 1034 | * in a sticky world-writable directory. This is to protect privileged |
| 1035 | * processes from failing races against path names that may change out |
| 1036 | * from under them by way of other users creating malicious symlinks. |
| 1037 | * It will permit symlinks to be followed only when outside a sticky |
| 1038 | * world-writable directory, or when the uid of the symlink and follower |
| 1039 | * match, or when the directory owner matches the symlink's owner. |
| 1040 | * |
| 1041 | * Returns 0 if following the symlink is allowed, -ve on error. |
| 1042 | */ |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1043 | static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1044 | { |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1045 | struct user_namespace *mnt_userns; |
| 1046 | kuid_t i_uid; |
| 1047 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1048 | if (!sysctl_protected_symlinks) |
| 1049 | return 0; |
| 1050 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1051 | mnt_userns = mnt_user_ns(nd->path.mnt); |
| 1052 | i_uid = i_uid_into_mnt(mnt_userns, inode); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1053 | /* Allowed if owner and follower match. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1054 | if (uid_eq(current_cred()->fsuid, i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1055 | return 0; |
| 1056 | |
| 1057 | /* Allowed if parent directory not sticky and world-writable. */ |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 1058 | if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1059 | return 0; |
| 1060 | |
| 1061 | /* Allowed if parent directory and link owner match. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1062 | if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1063 | return 0; |
| 1064 | |
Al Viro | 31956502 | 2015-05-07 20:37:40 -0400 | [diff] [blame] | 1065 | if (nd->flags & LOOKUP_RCU) |
| 1066 | return -ECHILD; |
| 1067 | |
Richard Guy Briggs | ea841ba | 2018-03-21 04:42:21 -0400 | [diff] [blame] | 1068 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1069 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1070 | return -EACCES; |
| 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * safe_hardlink_source - Check for safe hardlink conditions |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1075 | * @mnt_userns: user namespace of the mount the inode was found from |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1076 | * @inode: the source inode to hardlink from |
| 1077 | * |
| 1078 | * Return false if at least one of the following conditions: |
| 1079 | * - inode is not a regular file |
| 1080 | * - inode is setuid |
| 1081 | * - inode is setgid and group-exec |
| 1082 | * - access failure for read and write |
| 1083 | * |
| 1084 | * Otherwise returns true. |
| 1085 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1086 | static bool safe_hardlink_source(struct user_namespace *mnt_userns, |
| 1087 | struct inode *inode) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1088 | { |
| 1089 | umode_t mode = inode->i_mode; |
| 1090 | |
| 1091 | /* Special files should not get pinned to the filesystem. */ |
| 1092 | if (!S_ISREG(mode)) |
| 1093 | return false; |
| 1094 | |
| 1095 | /* Setuid files should not get pinned to the filesystem. */ |
| 1096 | if (mode & S_ISUID) |
| 1097 | return false; |
| 1098 | |
| 1099 | /* Executable setgid files should not get pinned to the filesystem. */ |
| 1100 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 1101 | return false; |
| 1102 | |
| 1103 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1104 | if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1105 | return false; |
| 1106 | |
| 1107 | return true; |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * may_linkat - Check permissions for creating a hardlink |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1112 | * @mnt_userns: user namespace of the mount the inode was found from |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1113 | * @link: the source to hardlink from |
| 1114 | * |
| 1115 | * Block hardlink when all of: |
| 1116 | * - sysctl_protected_hardlinks enabled |
| 1117 | * - fsuid does not match inode |
| 1118 | * - hardlink source is unsafe (see safe_hardlink_source() above) |
Dirk Steinmetz | f2ca379 | 2015-10-20 16:09:19 +0200 | [diff] [blame] | 1119 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1120 | * |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1121 | * If the inode has been found through an idmapped mount the user namespace of |
| 1122 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 1123 | * care to map the inode according to @mnt_userns before checking permissions. |
| 1124 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 1125 | * raw inode simply passs init_user_ns. |
| 1126 | * |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1127 | * Returns 0 if successful, -ve on error. |
| 1128 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1129 | int may_linkat(struct user_namespace *mnt_userns, struct path *link) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1130 | { |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 1131 | struct inode *inode = link->dentry->d_inode; |
| 1132 | |
| 1133 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1134 | if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || |
| 1135 | !gid_valid(i_gid_into_mnt(mnt_userns, inode))) |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 1136 | return -EOVERFLOW; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (!sysctl_protected_hardlinks) |
| 1139 | return 0; |
| 1140 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1141 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
| 1142 | * otherwise, it must be a safe source. |
| 1143 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1144 | if (safe_hardlink_source(mnt_userns, inode) || |
| 1145 | inode_owner_or_capable(mnt_userns, inode)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1146 | return 0; |
| 1147 | |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1148 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1149 | return -EPERM; |
| 1150 | } |
| 1151 | |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1152 | /** |
| 1153 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory |
| 1154 | * should be allowed, or not, on files that already |
| 1155 | * exist. |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1156 | * @mnt_userns: user namespace of the mount the inode was found from |
Randy Dunlap | 2111c3c | 2021-02-15 20:29:28 -0800 | [diff] [blame] | 1157 | * @nd: nameidata pathwalk data |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1158 | * @inode: the inode of the file to open |
| 1159 | * |
| 1160 | * Block an O_CREAT open of a FIFO (or a regular file) when: |
| 1161 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled |
| 1162 | * - the file already exists |
| 1163 | * - we are in a sticky directory |
| 1164 | * - we don't own the file |
| 1165 | * - the owner of the directory doesn't own the file |
| 1166 | * - the directory is world writable |
| 1167 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 |
| 1168 | * the directory doesn't have to be world writable: being group writable will |
| 1169 | * be enough. |
| 1170 | * |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1171 | * If the inode has been found through an idmapped mount the user namespace of |
| 1172 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 1173 | * care to map the inode according to @mnt_userns before checking permissions. |
| 1174 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 1175 | * raw inode simply passs init_user_ns. |
| 1176 | * |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1177 | * Returns 0 if the open is allowed, -ve on error. |
| 1178 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1179 | static int may_create_in_sticky(struct user_namespace *mnt_userns, |
| 1180 | struct nameidata *nd, struct inode *const inode) |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1181 | { |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1182 | umode_t dir_mode = nd->dir_mode; |
| 1183 | kuid_t dir_uid = nd->dir_uid; |
| 1184 | |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1185 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || |
| 1186 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1187 | likely(!(dir_mode & S_ISVTX)) || |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1188 | uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) || |
| 1189 | uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1190 | return 0; |
| 1191 | |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1192 | if (likely(dir_mode & 0002) || |
| 1193 | (dir_mode & 0020 && |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1194 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
| 1195 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1196 | const char *operation = S_ISFIFO(inode->i_mode) ? |
| 1197 | "sticky_create_fifo" : |
| 1198 | "sticky_create_regular"; |
| 1199 | audit_log_path_denied(AUDIT_ANOM_CREAT, operation); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1200 | return -EACCES; |
| 1201 | } |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
David Howells | f015f126 | 2012-06-25 12:55:28 +0100 | [diff] [blame] | 1205 | /* |
| 1206 | * follow_up - Find the mountpoint of path's vfsmount |
| 1207 | * |
| 1208 | * Given a path, find the mountpoint of its source file system. |
| 1209 | * Replace @path with the path of the mountpoint in the parent mount. |
| 1210 | * Up is towards /. |
| 1211 | * |
| 1212 | * Return 1 if we went up a level and 0 if we were already at the |
| 1213 | * root. |
| 1214 | */ |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1215 | int follow_up(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1217 | struct mount *mnt = real_mount(path->mnt); |
| 1218 | struct mount *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | struct dentry *mountpoint; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1220 | |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1221 | read_seqlock_excl(&mount_lock); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1222 | parent = mnt->mnt_parent; |
Al Viro | 3c0a616 | 2012-07-18 17:32:50 +0400 | [diff] [blame] | 1223 | if (parent == mnt) { |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1224 | read_sequnlock_excl(&mount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | return 0; |
| 1226 | } |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1227 | mntget(&parent->mnt); |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 1228 | mountpoint = dget(mnt->mnt_mountpoint); |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1229 | read_sequnlock_excl(&mount_lock); |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1230 | dput(path->dentry); |
| 1231 | path->dentry = mountpoint; |
| 1232 | mntput(path->mnt); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1233 | path->mnt = &parent->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | return 1; |
| 1235 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1236 | EXPORT_SYMBOL(follow_up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1238 | static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, |
| 1239 | struct path *path, unsigned *seqp) |
| 1240 | { |
| 1241 | while (mnt_has_parent(m)) { |
| 1242 | struct dentry *mountpoint = m->mnt_mountpoint; |
| 1243 | |
| 1244 | m = m->mnt_parent; |
| 1245 | if (unlikely(root->dentry == mountpoint && |
| 1246 | root->mnt == &m->mnt)) |
| 1247 | break; |
| 1248 | if (mountpoint != m->mnt.mnt_root) { |
| 1249 | path->mnt = &m->mnt; |
| 1250 | path->dentry = mountpoint; |
| 1251 | *seqp = read_seqcount_begin(&mountpoint->d_seq); |
| 1252 | return true; |
| 1253 | } |
| 1254 | } |
| 1255 | return false; |
| 1256 | } |
| 1257 | |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1258 | static bool choose_mountpoint(struct mount *m, const struct path *root, |
| 1259 | struct path *path) |
| 1260 | { |
| 1261 | bool found; |
| 1262 | |
| 1263 | rcu_read_lock(); |
| 1264 | while (1) { |
| 1265 | unsigned seq, mseq = read_seqbegin(&mount_lock); |
| 1266 | |
| 1267 | found = choose_mountpoint_rcu(m, root, path, &seq); |
| 1268 | if (unlikely(!found)) { |
| 1269 | if (!read_seqretry(&mount_lock, mseq)) |
| 1270 | break; |
| 1271 | } else { |
| 1272 | if (likely(__legitimize_path(path, seq, mseq))) |
| 1273 | break; |
| 1274 | rcu_read_unlock(); |
| 1275 | path_put(path); |
| 1276 | rcu_read_lock(); |
| 1277 | } |
| 1278 | } |
| 1279 | rcu_read_unlock(); |
| 1280 | return found; |
| 1281 | } |
| 1282 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 1283 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1284 | * Perform an automount |
| 1285 | * - return -EISDIR to tell follow_managed() to stop and return the path we |
| 1286 | * were called with. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1288 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1289 | { |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1290 | struct dentry *dentry = path->dentry; |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 1291 | |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1292 | /* We don't want to mount if someone's just doing a stat - |
| 1293 | * unless they're stat'ing a directory and appended a '/' to |
| 1294 | * the name. |
| 1295 | * |
| 1296 | * We do, however, want to mount if someone wants to open or |
| 1297 | * create a file of any type under the mountpoint, wants to |
| 1298 | * traverse through the mountpoint or wants to open the |
| 1299 | * mounted directory. Also, autofs may mark negative dentries |
| 1300 | * as being automount points. These will need the attentions |
| 1301 | * of the daemon to instantiate them before they can be used. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1302 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1303 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1304 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1305 | dentry->d_inode) |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1306 | return -EISDIR; |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1307 | |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1308 | if (count && (*count)++ >= MAXSYMLINKS) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1309 | return -ELOOP; |
| 1310 | |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1311 | return finish_automount(dentry->d_op->d_automount(path), path); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1315 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
| 1316 | * dentries are pinned but not locked here, so negative dentry can go |
| 1317 | * positive right under us. Use of smp_load_acquire() provides a barrier |
| 1318 | * sufficient for ->d_inode and ->d_flags consistency. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1319 | */ |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1320 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
| 1321 | int *count, unsigned lookup_flags) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1322 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1323 | struct vfsmount *mnt = path->mnt; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1324 | bool need_mntput = false; |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1325 | int ret = 0; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1326 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1327 | while (flags & DCACHE_MANAGED_DENTRY) { |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1328 | /* Allow the filesystem to manage the transit without i_mutex |
| 1329 | * being held. */ |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1330 | if (flags & DCACHE_MANAGE_TRANSIT) { |
Ian Kent | fb5f51c | 2016-11-24 08:03:41 +1100 | [diff] [blame] | 1331 | ret = path->dentry->d_op->d_manage(path, false); |
Al Viro | 508c877 | 2020-01-14 22:09:57 -0500 | [diff] [blame] | 1332 | flags = smp_load_acquire(&path->dentry->d_flags); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1333 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1334 | break; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1337 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1338 | struct vfsmount *mounted = lookup_mnt(path); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1339 | if (mounted) { // ... in our namespace |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1340 | dput(path->dentry); |
| 1341 | if (need_mntput) |
| 1342 | mntput(path->mnt); |
| 1343 | path->mnt = mounted; |
| 1344 | path->dentry = dget(mounted->mnt_root); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1345 | // here we know it's positive |
| 1346 | flags = path->dentry->d_flags; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1347 | need_mntput = true; |
| 1348 | continue; |
| 1349 | } |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1352 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
| 1353 | break; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1354 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1355 | // uncovered automount point |
| 1356 | ret = follow_automount(path, count, lookup_flags); |
| 1357 | flags = smp_load_acquire(&path->dentry->d_flags); |
| 1358 | if (ret < 0) |
| 1359 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | } |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1361 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1362 | if (ret == -EISDIR) |
| 1363 | ret = 0; |
| 1364 | // possible if you race with several mount --move |
| 1365 | if (need_mntput && path->mnt == mnt) |
| 1366 | mntput(path->mnt); |
| 1367 | if (!ret && unlikely(d_flags_negative(flags))) |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1368 | ret = -ENOENT; |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1369 | *jumped = need_mntput; |
Al Viro | 8402752 | 2015-04-22 10:30:08 -0400 | [diff] [blame] | 1370 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1373 | static inline int traverse_mounts(struct path *path, bool *jumped, |
| 1374 | int *count, unsigned lookup_flags) |
| 1375 | { |
| 1376 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); |
| 1377 | |
| 1378 | /* fastpath */ |
| 1379 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { |
| 1380 | *jumped = false; |
| 1381 | if (unlikely(d_flags_negative(flags))) |
| 1382 | return -ENOENT; |
| 1383 | return 0; |
| 1384 | } |
| 1385 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); |
| 1386 | } |
| 1387 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1388 | int follow_down_one(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | { |
| 1390 | struct vfsmount *mounted; |
| 1391 | |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 1392 | mounted = lookup_mnt(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | if (mounted) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1394 | dput(path->dentry); |
| 1395 | mntput(path->mnt); |
| 1396 | path->mnt = mounted; |
| 1397 | path->dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | return 1; |
| 1399 | } |
| 1400 | return 0; |
| 1401 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1402 | EXPORT_SYMBOL(follow_down_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1404 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1405 | * Follow down to the covering mount currently visible to userspace. At each |
| 1406 | * point, the filesystem owning that dentry may be queried as to whether the |
| 1407 | * caller is permitted to proceed or not. |
| 1408 | */ |
| 1409 | int follow_down(struct path *path) |
| 1410 | { |
| 1411 | struct vfsmount *mnt = path->mnt; |
| 1412 | bool jumped; |
| 1413 | int ret = traverse_mounts(path, &jumped, NULL, 0); |
| 1414 | |
| 1415 | if (path->mnt != mnt) |
| 1416 | mntput(mnt); |
| 1417 | return ret; |
| 1418 | } |
| 1419 | EXPORT_SYMBOL(follow_down); |
| 1420 | |
| 1421 | /* |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1422 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
| 1423 | * we meet a managed dentry that would need blocking. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1424 | */ |
| 1425 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1426 | struct inode **inode, unsigned *seqp) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1427 | { |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1428 | struct dentry *dentry = path->dentry; |
| 1429 | unsigned int flags = dentry->d_flags; |
| 1430 | |
| 1431 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) |
| 1432 | return true; |
| 1433 | |
| 1434 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1435 | return false; |
| 1436 | |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1437 | for (;;) { |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1438 | /* |
| 1439 | * Don't forget we might have a non-mountpoint managed dentry |
| 1440 | * that wants to block transit. |
| 1441 | */ |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1442 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
| 1443 | int res = dentry->d_op->d_manage(path, true); |
| 1444 | if (res) |
| 1445 | return res == -EISDIR; |
| 1446 | flags = dentry->d_flags; |
NeilBrown | b8faf03 | 2014-08-04 17:06:29 +1000 | [diff] [blame] | 1447 | } |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1448 | |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1449 | if (flags & DCACHE_MOUNTED) { |
| 1450 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); |
| 1451 | if (mounted) { |
| 1452 | path->mnt = &mounted->mnt; |
| 1453 | dentry = path->dentry = mounted->mnt.mnt_root; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 1454 | nd->state |= ND_JUMPED; |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1455 | *seqp = read_seqcount_begin(&dentry->d_seq); |
| 1456 | *inode = dentry->d_inode; |
| 1457 | /* |
| 1458 | * We don't need to re-check ->d_seq after this |
| 1459 | * ->d_inode read - there will be an RCU delay |
| 1460 | * between mount hash removal and ->mnt_root |
| 1461 | * becoming unpinned. |
| 1462 | */ |
| 1463 | flags = dentry->d_flags; |
| 1464 | continue; |
| 1465 | } |
| 1466 | if (read_seqretry(&mount_lock, nd->m_seq)) |
| 1467 | return false; |
| 1468 | } |
| 1469 | return !(flags & DCACHE_NEED_AUTOMOUNT); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1470 | } |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1471 | } |
| 1472 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1473 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
| 1474 | struct path *path, struct inode **inode, |
| 1475 | unsigned int *seqp) |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1476 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1477 | bool jumped; |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1478 | int ret; |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1479 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1480 | path->mnt = nd->path.mnt; |
| 1481 | path->dentry = dentry; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1482 | if (nd->flags & LOOKUP_RCU) { |
| 1483 | unsigned int seq = *seqp; |
| 1484 | if (unlikely(!*inode)) |
| 1485 | return -ENOENT; |
| 1486 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1487 | return 0; |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 1488 | if (!try_to_unlazy_next(nd, dentry, seq)) |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1489 | return -ECHILD; |
| 1490 | // *path might've been clobbered by __follow_mount_rcu() |
| 1491 | path->mnt = nd->path.mnt; |
| 1492 | path->dentry = dentry; |
| 1493 | } |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1494 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
| 1495 | if (jumped) { |
| 1496 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1497 | ret = -EXDEV; |
| 1498 | else |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 1499 | nd->state |= ND_JUMPED; |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1500 | } |
| 1501 | if (unlikely(ret)) { |
| 1502 | dput(path->dentry); |
| 1503 | if (path->mnt != nd->path.mnt) |
| 1504 | mntput(path->mnt); |
| 1505 | } else { |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1506 | *inode = d_backing_inode(path->dentry); |
| 1507 | *seqp = 0; /* out of RCU mode, so the value doesn't matter */ |
| 1508 | } |
| 1509 | return ret; |
| 1510 | } |
| 1511 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1512 | /* |
Oleg Drokin | f4fdace | 2016-07-07 22:04:04 -0400 | [diff] [blame] | 1513 | * This looks up the name in dcache and possibly revalidates the found dentry. |
| 1514 | * NULL is returned if the dentry does not exist in the cache. |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1515 | */ |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1516 | static struct dentry *lookup_dcache(const struct qstr *name, |
| 1517 | struct dentry *dir, |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 1518 | unsigned int flags) |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1519 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1520 | struct dentry *dentry = d_lookup(dir, name); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1521 | if (dentry) { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1522 | int error = d_revalidate(dentry, flags); |
| 1523 | if (unlikely(error <= 0)) { |
| 1524 | if (!error) |
| 1525 | d_invalidate(dentry); |
| 1526 | dput(dentry); |
| 1527 | return ERR_PTR(error); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1528 | } |
| 1529 | } |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1530 | return dentry; |
| 1531 | } |
| 1532 | |
| 1533 | /* |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1534 | * Parent directory has inode locked exclusive. This is one |
| 1535 | * and only case when ->lookup() gets called on non in-lookup |
| 1536 | * dentries - as the matter of fact, this only gets called |
| 1537 | * when directory is guaranteed to have no in-lookup children |
| 1538 | * at all. |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1539 | */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1540 | static struct dentry *__lookup_hash(const struct qstr *name, |
| 1541 | struct dentry *base, unsigned int flags) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1542 | { |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1543 | struct dentry *dentry = lookup_dcache(name, base, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1544 | struct dentry *old; |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1545 | struct inode *dir = base->d_inode; |
| 1546 | |
| 1547 | if (dentry) |
| 1548 | return dentry; |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1549 | |
| 1550 | /* Don't create child dentry for a dead directory. */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1551 | if (unlikely(IS_DEADDIR(dir))) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1552 | return ERR_PTR(-ENOENT); |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1553 | |
| 1554 | dentry = d_alloc(base, name); |
| 1555 | if (unlikely(!dentry)) |
| 1556 | return ERR_PTR(-ENOMEM); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1557 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1558 | old = dir->i_op->lookup(dir, dentry, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1559 | if (unlikely(old)) { |
| 1560 | dput(dentry); |
| 1561 | dentry = old; |
| 1562 | } |
| 1563 | return dentry; |
| 1564 | } |
| 1565 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1566 | static struct dentry *lookup_fast(struct nameidata *nd, |
| 1567 | struct inode **inode, |
| 1568 | unsigned *seqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1570 | struct dentry *dentry, *parent = nd->path.dentry; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1571 | int status = 1; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1572 | |
Al Viro | 3cac260 | 2009-08-13 18:27:43 +0400 | [diff] [blame] | 1573 | /* |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1574 | * Rename seqlock is not required here because in the off chance |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1575 | * of a false negative due to a concurrent rename, the caller is |
| 1576 | * going to fall back to non-racy lookup. |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1577 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1578 | if (nd->flags & LOOKUP_RCU) { |
| 1579 | unsigned seq; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 1580 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1581 | if (unlikely(!dentry)) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1582 | if (!try_to_unlazy(nd)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1583 | return ERR_PTR(-ECHILD); |
| 1584 | return NULL; |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1585 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1586 | |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1587 | /* |
| 1588 | * This sequence count validates that the inode matches |
| 1589 | * the dentry name information from lookup. |
| 1590 | */ |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 1591 | *inode = d_backing_inode(dentry); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1592 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1593 | return ERR_PTR(-ECHILD); |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1594 | |
| 1595 | /* |
| 1596 | * This sequence count validates that the parent had no |
| 1597 | * changes while we did the lookup of the dentry above. |
| 1598 | * |
| 1599 | * The memory barrier in read_seqcount_begin of child is |
| 1600 | * enough, we can use __read_seqcount_retry here. |
| 1601 | */ |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1602 | if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1603 | return ERR_PTR(-ECHILD); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1604 | |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1605 | *seqp = seq; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1606 | status = d_revalidate(dentry, nd->flags); |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1607 | if (likely(status > 0)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1608 | return dentry; |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 1609 | if (!try_to_unlazy_next(nd, dentry, seq)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1610 | return ERR_PTR(-ECHILD); |
Steven Rostedt (VMware) | 26ddb45 | 2020-12-09 17:09:28 -0500 | [diff] [blame] | 1611 | if (status == -ECHILD) |
Al Viro | 209a7fb | 2017-01-09 01:35:39 -0500 | [diff] [blame] | 1612 | /* we'd been told to redo it in non-rcu mode */ |
| 1613 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1614 | } else { |
Al Viro | e97cdc8 | 2013-01-24 18:16:00 -0500 | [diff] [blame] | 1615 | dentry = __d_lookup(parent, &nd->last); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1616 | if (unlikely(!dentry)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1617 | return NULL; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1618 | status = d_revalidate(dentry, nd->flags); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1619 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1620 | if (unlikely(status <= 0)) { |
Al Viro | e9742b5 | 2016-03-05 22:04:59 -0500 | [diff] [blame] | 1621 | if (!status) |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1622 | d_invalidate(dentry); |
Eric W. Biederman | 5542aa2 | 2014-02-13 09:46:25 -0800 | [diff] [blame] | 1623 | dput(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1624 | return ERR_PTR(status); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1625 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1626 | return dentry; |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* Fast lookup failed, do it the slow way */ |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1630 | static struct dentry *__lookup_slow(const struct qstr *name, |
| 1631 | struct dentry *dir, |
| 1632 | unsigned int flags) |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1633 | { |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1634 | struct dentry *dentry, *old; |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1635 | struct inode *inode = dir->d_inode; |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1636 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1637 | |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1638 | /* Don't go there if it's already dead */ |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1639 | if (unlikely(IS_DEADDIR(inode))) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1640 | return ERR_PTR(-ENOENT); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1641 | again: |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1642 | dentry = d_alloc_parallel(dir, name, &wq); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1643 | if (IS_ERR(dentry)) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1644 | return dentry; |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1645 | if (unlikely(!d_in_lookup(dentry))) { |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1646 | int error = d_revalidate(dentry, flags); |
| 1647 | if (unlikely(error <= 0)) { |
| 1648 | if (!error) { |
| 1649 | d_invalidate(dentry); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1650 | dput(dentry); |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1651 | goto again; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1652 | } |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1653 | dput(dentry); |
| 1654 | dentry = ERR_PTR(error); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1655 | } |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1656 | } else { |
| 1657 | old = inode->i_op->lookup(inode, dentry, flags); |
| 1658 | d_lookup_done(dentry); |
| 1659 | if (unlikely(old)) { |
| 1660 | dput(dentry); |
| 1661 | dentry = old; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1662 | } |
| 1663 | } |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1664 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1667 | static struct dentry *lookup_slow(const struct qstr *name, |
| 1668 | struct dentry *dir, |
| 1669 | unsigned int flags) |
| 1670 | { |
| 1671 | struct inode *inode = dir->d_inode; |
| 1672 | struct dentry *res; |
| 1673 | inode_lock_shared(inode); |
| 1674 | res = __lookup_slow(name, dir, flags); |
| 1675 | inode_unlock_shared(inode); |
| 1676 | return res; |
| 1677 | } |
| 1678 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1679 | static inline int may_lookup(struct user_namespace *mnt_userns, |
| 1680 | struct nameidata *nd) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1681 | { |
| 1682 | if (nd->flags & LOOKUP_RCU) { |
Linus Torvalds | 7d6beb7 | 2021-02-23 13:39:45 -0800 | [diff] [blame] | 1683 | int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1684 | if (err != -ECHILD || !try_to_unlazy(nd)) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1685 | return err; |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1686 | } |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1687 | return inode_permission(mnt_userns, nd->inode, MAY_EXEC); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1688 | } |
| 1689 | |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1690 | static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) |
| 1691 | { |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1692 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) |
| 1693 | return -ELOOP; |
Al Viro | 4542576 | 2020-03-03 11:25:31 -0500 | [diff] [blame] | 1694 | |
| 1695 | if (likely(nd->depth != EMBEDDED_LEVELS)) |
| 1696 | return 0; |
| 1697 | if (likely(nd->stack != nd->internal)) |
| 1698 | return 0; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1699 | if (likely(nd_alloc_stack(nd))) |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1700 | return 0; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1701 | |
| 1702 | if (nd->flags & LOOKUP_RCU) { |
| 1703 | // we need to grab link before we do unlazy. And we can't skip |
| 1704 | // unlazy even if we fail to grab the link - cleanup needs it |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1705 | bool grabbed_link = legitimize_path(nd, link, seq); |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1706 | |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1707 | if (!try_to_unlazy(nd) != 0 || !grabbed_link) |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1708 | return -ECHILD; |
| 1709 | |
| 1710 | if (nd_alloc_stack(nd)) |
| 1711 | return 0; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1712 | } |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1713 | return -ENOMEM; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1714 | } |
| 1715 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1716 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
| 1717 | |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1718 | static const char *pick_link(struct nameidata *nd, struct path *link, |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1719 | struct inode *inode, unsigned seq, int flags) |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1720 | { |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1721 | struct saved *last; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1722 | const char *res; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1723 | int error = reserve_stack(nd, link, seq); |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1724 | |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1725 | if (unlikely(error)) { |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1726 | if (!(nd->flags & LOOKUP_RCU)) |
| 1727 | path_put(link); |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1728 | return ERR_PTR(error); |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1729 | } |
Al Viro | ab10492 | 2015-05-10 11:50:01 -0400 | [diff] [blame] | 1730 | last = nd->stack + nd->depth++; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1731 | last->link = *link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1732 | clear_delayed_call(&last->done); |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 1733 | last->seq = seq; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1734 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1735 | if (flags & WALK_TRAILING) { |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1736 | error = may_follow_link(nd, inode); |
| 1737 | if (unlikely(error)) |
| 1738 | return ERR_PTR(error); |
| 1739 | } |
| 1740 | |
Mattias Nissler | dab741e0 | 2020-08-27 11:09:46 -0600 | [diff] [blame] | 1741 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || |
| 1742 | unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1743 | return ERR_PTR(-ELOOP); |
| 1744 | |
| 1745 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1746 | touch_atime(&last->link); |
| 1747 | cond_resched(); |
| 1748 | } else if (atime_needs_update(&last->link, inode)) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1749 | if (!try_to_unlazy(nd)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1750 | return ERR_PTR(-ECHILD); |
| 1751 | touch_atime(&last->link); |
| 1752 | } |
| 1753 | |
| 1754 | error = security_inode_follow_link(link->dentry, inode, |
| 1755 | nd->flags & LOOKUP_RCU); |
| 1756 | if (unlikely(error)) |
| 1757 | return ERR_PTR(error); |
| 1758 | |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1759 | res = READ_ONCE(inode->i_link); |
| 1760 | if (!res) { |
| 1761 | const char * (*get)(struct dentry *, struct inode *, |
| 1762 | struct delayed_call *); |
| 1763 | get = inode->i_op->get_link; |
| 1764 | if (nd->flags & LOOKUP_RCU) { |
| 1765 | res = get(NULL, inode, &last->done); |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1766 | if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1767 | res = get(link->dentry, inode, &last->done); |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1768 | } else { |
| 1769 | res = get(link->dentry, inode, &last->done); |
| 1770 | } |
| 1771 | if (!res) |
| 1772 | goto all_done; |
| 1773 | if (IS_ERR(res)) |
| 1774 | return res; |
| 1775 | } |
| 1776 | if (*res == '/') { |
| 1777 | error = nd_jump_root(nd); |
| 1778 | if (unlikely(error)) |
| 1779 | return ERR_PTR(error); |
| 1780 | while (unlikely(*++res == '/')) |
| 1781 | ; |
| 1782 | } |
| 1783 | if (*res) |
| 1784 | return res; |
| 1785 | all_done: // pure jump |
| 1786 | put_link(nd); |
| 1787 | return NULL; |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1788 | } |
| 1789 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1790 | /* |
| 1791 | * Do we need to follow links? We _really_ want to be able |
| 1792 | * to do this check without having to look at inode->i_op, |
| 1793 | * so we keep a cache of "no, this doesn't need follow_link" |
| 1794 | * for the common case. |
| 1795 | */ |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1796 | static const char *step_into(struct nameidata *nd, int flags, |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1797 | struct dentry *dentry, struct inode *inode, unsigned seq) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1798 | { |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1799 | struct path path; |
| 1800 | int err = handle_mounts(nd, dentry, &path, &inode, &seq); |
| 1801 | |
| 1802 | if (err < 0) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1803 | return ERR_PTR(err); |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1804 | if (likely(!d_is_symlink(path.dentry)) || |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 1805 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
Al Viro | aca2903 | 2020-01-09 15:17:57 -0500 | [diff] [blame] | 1806 | (flags & WALK_NOFOLLOW)) { |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1807 | /* not a symlink or should not follow */ |
Al Viro | c99687a | 2020-03-03 10:56:17 -0500 | [diff] [blame] | 1808 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1809 | dput(nd->path.dentry); |
| 1810 | if (nd->path.mnt != path.mnt) |
| 1811 | mntput(nd->path.mnt); |
| 1812 | } |
| 1813 | nd->path = path; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1814 | nd->inode = inode; |
| 1815 | nd->seq = seq; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1816 | return NULL; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1817 | } |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1818 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1819 | /* make sure that d_is_symlink above matches inode */ |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1820 | if (read_seqcount_retry(&path.dentry->d_seq, seq)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1821 | return ERR_PTR(-ECHILD); |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1822 | } else { |
| 1823 | if (path.mnt == nd->path.mnt) |
| 1824 | mntget(path.mnt); |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1825 | } |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1826 | return pick_link(nd, &path, inode, seq, flags); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1829 | static struct dentry *follow_dotdot_rcu(struct nameidata *nd, |
| 1830 | struct inode **inodep, |
| 1831 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1832 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1833 | struct dentry *parent, *old; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1834 | |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1835 | if (path_equal(&nd->path, &nd->root)) |
| 1836 | goto in_root; |
| 1837 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1838 | struct path path; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1839 | unsigned seq; |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1840 | if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), |
| 1841 | &nd->root, &path, &seq)) |
| 1842 | goto in_root; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1843 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1844 | return ERR_PTR(-ECHILD); |
| 1845 | nd->path = path; |
| 1846 | nd->inode = path.dentry->d_inode; |
| 1847 | nd->seq = seq; |
| 1848 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1849 | return ERR_PTR(-ECHILD); |
| 1850 | /* we know that mountpoint was pinned */ |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1851 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1852 | old = nd->path.dentry; |
| 1853 | parent = old->d_parent; |
| 1854 | *inodep = parent->d_inode; |
| 1855 | *seqp = read_seqcount_begin(&parent->d_seq); |
| 1856 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) |
| 1857 | return ERR_PTR(-ECHILD); |
| 1858 | if (unlikely(!path_connected(nd->path.mnt, parent))) |
| 1859 | return ERR_PTR(-ECHILD); |
| 1860 | return parent; |
| 1861 | in_root: |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1862 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1863 | return ERR_PTR(-ECHILD); |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1864 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1865 | return ERR_PTR(-ECHILD); |
| 1866 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1867 | } |
| 1868 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1869 | static struct dentry *follow_dotdot(struct nameidata *nd, |
| 1870 | struct inode **inodep, |
| 1871 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1872 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1873 | struct dentry *parent; |
| 1874 | |
| 1875 | if (path_equal(&nd->path, &nd->root)) |
| 1876 | goto in_root; |
| 1877 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1878 | struct path path; |
| 1879 | |
| 1880 | if (!choose_mountpoint(real_mount(nd->path.mnt), |
| 1881 | &nd->root, &path)) |
| 1882 | goto in_root; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame] | 1883 | path_put(&nd->path); |
| 1884 | nd->path = path; |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1885 | nd->inode = path.dentry->d_inode; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame] | 1886 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1887 | return ERR_PTR(-EXDEV); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1888 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1889 | /* rare case of legitimate dget_parent()... */ |
| 1890 | parent = dget_parent(nd->path.dentry); |
| 1891 | if (unlikely(!path_connected(nd->path.mnt, parent))) { |
| 1892 | dput(parent); |
| 1893 | return ERR_PTR(-ENOENT); |
| 1894 | } |
| 1895 | *seqp = 0; |
| 1896 | *inodep = parent->d_inode; |
| 1897 | return parent; |
| 1898 | |
| 1899 | in_root: |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1900 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1901 | return ERR_PTR(-EXDEV); |
| 1902 | dget(nd->path.dentry); |
| 1903 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1904 | } |
| 1905 | |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1906 | static const char *handle_dots(struct nameidata *nd, int type) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1907 | { |
| 1908 | if (type == LAST_DOTDOT) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1909 | const char *error = NULL; |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1910 | struct dentry *parent; |
| 1911 | struct inode *inode; |
| 1912 | unsigned seq; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1913 | |
| 1914 | if (!nd->root.mnt) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1915 | error = ERR_PTR(set_root(nd)); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1916 | if (error) |
| 1917 | return error; |
| 1918 | } |
| 1919 | if (nd->flags & LOOKUP_RCU) |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1920 | parent = follow_dotdot_rcu(nd, &inode, &seq); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1921 | else |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1922 | parent = follow_dotdot(nd, &inode, &seq); |
| 1923 | if (IS_ERR(parent)) |
| 1924 | return ERR_CAST(parent); |
| 1925 | if (unlikely(!parent)) |
| 1926 | error = step_into(nd, WALK_NOFOLLOW, |
| 1927 | nd->path.dentry, nd->inode, nd->seq); |
| 1928 | else |
| 1929 | error = step_into(nd, WALK_NOFOLLOW, |
| 1930 | parent, inode, seq); |
| 1931 | if (unlikely(error)) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1932 | return error; |
| 1933 | |
| 1934 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 1935 | /* |
| 1936 | * If there was a racing rename or mount along our |
| 1937 | * path, then we can't be sure that ".." hasn't jumped |
| 1938 | * above nd->root (and so userspace should retry or use |
| 1939 | * some fallback). |
| 1940 | */ |
| 1941 | smp_rmb(); |
| 1942 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1943 | return ERR_PTR(-EAGAIN); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1944 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1945 | return ERR_PTR(-EAGAIN); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1946 | } |
| 1947 | } |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1948 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1949 | } |
| 1950 | |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1951 | static const char *walk_component(struct nameidata *nd, int flags) |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1952 | { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1953 | struct dentry *dentry; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1954 | struct inode *inode; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1955 | unsigned seq; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1956 | /* |
| 1957 | * "." and ".." are special - ".." especially so because it has |
| 1958 | * to be able to know about the current root directory and |
| 1959 | * parent relationships. |
| 1960 | */ |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1961 | if (unlikely(nd->last_type != LAST_NORM)) { |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 1962 | if (!(flags & WALK_MORE) && nd->depth) |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1963 | put_link(nd); |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1964 | return handle_dots(nd, nd->last_type); |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1965 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1966 | dentry = lookup_fast(nd, &inode, &seq); |
| 1967 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1968 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1969 | if (unlikely(!dentry)) { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1970 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
| 1971 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1972 | return ERR_CAST(dentry); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1973 | } |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 1974 | if (!(flags & WALK_MORE) && nd->depth) |
| 1975 | put_link(nd); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1976 | return step_into(nd, flags, dentry, inode, seq); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1977 | } |
| 1978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | /* |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1980 | * We can do the critical dentry name comparison and hashing |
| 1981 | * operations one word at a time, but we are limited to: |
| 1982 | * |
| 1983 | * - Architectures with fast unaligned word accesses. We could |
| 1984 | * do a "get_unaligned()" if this helps and is sufficiently |
| 1985 | * fast. |
| 1986 | * |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1987 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
| 1988 | * do not trap on the (extremely unlikely) case of a page |
| 1989 | * crossing operation. |
| 1990 | * |
| 1991 | * - Furthermore, we need an efficient 64-bit compile for the |
| 1992 | * 64-bit case in order to generate the "number of bytes in |
| 1993 | * the final mask". Again, that could be replaced with a |
| 1994 | * efficient population count instruction or similar. |
| 1995 | */ |
| 1996 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
| 1997 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1998 | #include <asm/word-at-a-time.h> |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1999 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 2000 | #ifdef HASH_MIX |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2001 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 2002 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
| 2003 | |
| 2004 | #elif defined(CONFIG_64BIT) |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2005 | /* |
| 2006 | * Register pressure in the mixing function is an issue, particularly |
| 2007 | * on 32-bit x86, but almost any function requires one state value and |
| 2008 | * one temporary. Instead, use a function designed for two state values |
| 2009 | * and no temporaries. |
| 2010 | * |
| 2011 | * This function cannot create a collision in only two iterations, so |
| 2012 | * we have two iterations to achieve avalanche. In those two iterations, |
| 2013 | * we have six layers of mixing, which is enough to spread one bit's |
| 2014 | * influence out to 2^6 = 64 state bits. |
| 2015 | * |
| 2016 | * Rotate constants are scored by considering either 64 one-bit input |
| 2017 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the |
| 2018 | * probability of that delta causing a change to each of the 128 output |
| 2019 | * bits, using a sample of random initial states. |
| 2020 | * |
| 2021 | * The Shannon entropy of the computed probabilities is then summed |
| 2022 | * to produce a score. Ideally, any input change has a 50% chance of |
| 2023 | * toggling any given output bit. |
| 2024 | * |
| 2025 | * Mixing scores (in bits) for (12,45): |
| 2026 | * Input delta: 1-bit 2-bit |
| 2027 | * 1 round: 713.3 42542.6 |
| 2028 | * 2 rounds: 2753.7 140389.8 |
| 2029 | * 3 rounds: 5954.1 233458.2 |
| 2030 | * 4 rounds: 7862.6 256672.2 |
| 2031 | * Perfect: 8192 258048 |
| 2032 | * (64*128) (64*63/2 * 128) |
| 2033 | */ |
| 2034 | #define HASH_MIX(x, y, a) \ |
| 2035 | ( x ^= (a), \ |
| 2036 | y ^= x, x = rol64(x,12),\ |
| 2037 | x += y, y = rol64(y,45),\ |
| 2038 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2039 | |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2040 | /* |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2041 | * Fold two longs into one 32-bit hash value. This must be fast, but |
| 2042 | * latency isn't quite as critical, as there is a fair bit of additional |
| 2043 | * work done before the hash value is used. |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2044 | */ |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2045 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2046 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2047 | y ^= x * GOLDEN_RATIO_64; |
| 2048 | y *= GOLDEN_RATIO_64; |
| 2049 | return y >> 32; |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2050 | } |
| 2051 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2052 | #else /* 32-bit case */ |
| 2053 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2054 | /* |
| 2055 | * Mixing scores (in bits) for (7,20): |
| 2056 | * Input delta: 1-bit 2-bit |
| 2057 | * 1 round: 330.3 9201.6 |
| 2058 | * 2 rounds: 1246.4 25475.4 |
| 2059 | * 3 rounds: 1907.1 31295.1 |
| 2060 | * 4 rounds: 2042.3 31718.6 |
| 2061 | * Perfect: 2048 31744 |
| 2062 | * (32*64) (32*31/2 * 64) |
| 2063 | */ |
| 2064 | #define HASH_MIX(x, y, a) \ |
| 2065 | ( x ^= (a), \ |
| 2066 | y ^= x, x = rol32(x, 7),\ |
| 2067 | x += y, y = rol32(y,20),\ |
| 2068 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2069 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2070 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2071 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2072 | /* Use arch-optimized multiply if one exists */ |
| 2073 | return __hash_32(y ^ __hash_32(x)); |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2074 | } |
| 2075 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2076 | #endif |
| 2077 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2078 | /* |
| 2079 | * Return the hash of a string of known length. This is carfully |
| 2080 | * designed to match hash_name(), which is the more critical function. |
| 2081 | * In particular, we must end by hashing a final word containing 0..7 |
| 2082 | * payload bytes, to match the way that hash_name() iterates until it |
| 2083 | * finds the delimiter after the name. |
| 2084 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2085 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2086 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2087 | unsigned long a, x = 0, y = (unsigned long)salt; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2088 | |
| 2089 | for (;;) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2090 | if (!len) |
| 2091 | goto done; |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2092 | a = load_unaligned_zeropad(name); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2093 | if (len < sizeof(unsigned long)) |
| 2094 | break; |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2095 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2096 | name += sizeof(unsigned long); |
| 2097 | len -= sizeof(unsigned long); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2098 | } |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2099 | x ^= a & bytemask_from_count(len); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2100 | done: |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2101 | return fold_hash(x, y); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2102 | } |
| 2103 | EXPORT_SYMBOL(full_name_hash); |
| 2104 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2105 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2106 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2107 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2108 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
| 2109 | unsigned long adata, mask, len; |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2110 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
| 2111 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2112 | len = 0; |
| 2113 | goto inside; |
| 2114 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2115 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2116 | HASH_MIX(x, y, a); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2117 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2118 | inside: |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2119 | a = load_unaligned_zeropad(name+len); |
| 2120 | } while (!has_zero(a, &adata, &constants)); |
| 2121 | |
| 2122 | adata = prep_zero_mask(a, adata, &constants); |
| 2123 | mask = create_zero_mask(adata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2124 | x ^= a & zero_bytemask(mask); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2125 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2126 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2127 | } |
| 2128 | EXPORT_SYMBOL(hashlen_string); |
| 2129 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2130 | /* |
| 2131 | * Calculate the length and hash of the path component, and |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2132 | * return the "hash_len" as the result. |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2133 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2134 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2135 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2136 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
| 2137 | unsigned long adata, bdata, mask, len; |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2138 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2139 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2140 | len = 0; |
| 2141 | goto inside; |
| 2142 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2143 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2144 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2145 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2146 | inside: |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2147 | a = load_unaligned_zeropad(name+len); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2148 | b = a ^ REPEAT_BYTE('/'); |
| 2149 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2150 | |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2151 | adata = prep_zero_mask(a, adata, &constants); |
| 2152 | bdata = prep_zero_mask(b, bdata, &constants); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2153 | mask = create_zero_mask(adata | bdata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2154 | x ^= a & zero_bytemask(mask); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2155 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2156 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2157 | } |
| 2158 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2159 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2160 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2161 | /* Return the hash of a string of known length */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2162 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2163 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2164 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2165 | while (len--) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2166 | hash = partial_name_hash((unsigned char)*name++, hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2167 | return end_name_hash(hash); |
| 2168 | } |
Linus Torvalds | ae942ae | 2012-03-02 19:40:57 -0800 | [diff] [blame] | 2169 | EXPORT_SYMBOL(full_name_hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2170 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2171 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2172 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2173 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2174 | unsigned long hash = init_name_hash(salt); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2175 | unsigned long len = 0, c; |
| 2176 | |
| 2177 | c = (unsigned char)*name; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2178 | while (c) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2179 | len++; |
| 2180 | hash = partial_name_hash(c, hash); |
| 2181 | c = (unsigned char)name[len]; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2182 | } |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2183 | return hashlen_create(end_name_hash(hash), len); |
| 2184 | } |
George Spelvin | f2a031b | 2016-05-29 01:26:41 -0400 | [diff] [blame] | 2185 | EXPORT_SYMBOL(hashlen_string); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2186 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 2187 | /* |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2188 | * We know there's a real path component here of at least |
| 2189 | * one character. |
| 2190 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2191 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2192 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2193 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2194 | unsigned long len = 0, c; |
| 2195 | |
| 2196 | c = (unsigned char)*name; |
| 2197 | do { |
| 2198 | len++; |
| 2199 | hash = partial_name_hash(c, hash); |
| 2200 | c = (unsigned char)name[len]; |
| 2201 | } while (c && c != '/'); |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2202 | return hashlen_create(end_name_hash(hash), len); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2203 | } |
| 2204 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2205 | #endif |
| 2206 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2207 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2209 | * This is the basic name resolution function, turning a pathname into |
| 2210 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2212 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 2213 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | */ |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 2215 | static int link_path_walk(const char *name, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | { |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2217 | int depth = 0; // depth <= nd->depth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | int err; |
Al Viro | 32cd746 | 2015-04-18 20:30:49 -0400 | [diff] [blame] | 2219 | |
Al Viro | b4c0353 | 2020-01-19 11:44:51 -0500 | [diff] [blame] | 2220 | nd->last_type = LAST_ROOT; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2221 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2222 | if (IS_ERR(name)) |
| 2223 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | while (*name=='/') |
| 2225 | name++; |
Al Viro | 1a97d89 | 2020-09-19 17:55:58 +0100 | [diff] [blame] | 2226 | if (!*name) { |
| 2227 | nd->dir_mode = 0; // short-circuit the 'hardening' idiocy |
Al Viro | 9e18f10 | 2015-04-18 20:44:34 -0400 | [diff] [blame] | 2228 | return 0; |
Al Viro | 1a97d89 | 2020-09-19 17:55:58 +0100 | [diff] [blame] | 2229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | /* At this point we know we have a real path component. */ |
| 2232 | for(;;) { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2233 | struct user_namespace *mnt_userns; |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2234 | const char *link; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2235 | u64 hash_len; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2236 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2238 | mnt_userns = mnt_user_ns(nd->path.mnt); |
| 2239 | err = may_lookup(mnt_userns, nd); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2240 | if (err) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2241 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2243 | hash_len = hash_name(nd->path.dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2245 | type = LAST_NORM; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2246 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2247 | case 2: |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2248 | if (name[1] == '.') { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2249 | type = LAST_DOTDOT; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2250 | nd->state |= ND_JUMPED; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2251 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2252 | break; |
| 2253 | case 1: |
| 2254 | type = LAST_DOT; |
| 2255 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2256 | if (likely(type == LAST_NORM)) { |
| 2257 | struct dentry *parent = nd->path.dentry; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2258 | nd->state &= ~ND_JUMPED; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2259 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
James Hogan | a060dc5 | 2014-09-16 13:07:35 +0100 | [diff] [blame] | 2260 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 2261 | err = parent->d_op->d_hash(parent, &this); |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2262 | if (err < 0) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2263 | return err; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2264 | hash_len = this.hash_len; |
| 2265 | name = this.name; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2266 | } |
| 2267 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2268 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2269 | nd->last.hash_len = hash_len; |
| 2270 | nd->last.name = name; |
Al Viro | 5f4a6a6 | 2013-01-24 18:04:22 -0500 | [diff] [blame] | 2271 | nd->last_type = type; |
| 2272 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2273 | name += hashlen_len(hash_len); |
| 2274 | if (!*name) |
Al Viro | bdf6cbf | 2015-04-18 20:21:40 -0400 | [diff] [blame] | 2275 | goto OK; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2276 | /* |
| 2277 | * If it wasn't NUL, we know it was '/'. Skip that |
| 2278 | * slash, and continue until no more slashes. |
| 2279 | */ |
| 2280 | do { |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2281 | name++; |
| 2282 | } while (unlikely(*name == '/')); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2283 | if (unlikely(!*name)) { |
| 2284 | OK: |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2285 | /* pathname or trailing symlink, done */ |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2286 | if (!depth) { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2287 | nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode); |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 2288 | nd->dir_mode = nd->inode->i_mode; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2289 | nd->flags &= ~LOOKUP_PARENT; |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2290 | return 0; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2291 | } |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2292 | /* last component of nested symlink */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2293 | name = nd->stack[--depth].name; |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2294 | link = walk_component(nd, 0); |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 2295 | } else { |
| 2296 | /* not the last component */ |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2297 | link = walk_component(nd, WALK_MORE); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2298 | } |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2299 | if (unlikely(link)) { |
| 2300 | if (IS_ERR(link)) |
| 2301 | return PTR_ERR(link); |
| 2302 | /* a symlink to follow */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2303 | nd->stack[depth++].name = name; |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2304 | name = link; |
| 2305 | continue; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2306 | } |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2307 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
| 2308 | if (nd->flags & LOOKUP_RCU) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 2309 | if (!try_to_unlazy(nd)) |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2310 | return -ECHILD; |
| 2311 | } |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2312 | return -ENOTDIR; |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | } |
| 2316 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2317 | /* must be paired with terminate_walk() */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2318 | static const char *path_init(struct nameidata *nd, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2320 | int error; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2321 | const char *s = nd->name->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | |
Jens Axboe | 6c6ec2b | 2020-12-17 09:19:09 -0700 | [diff] [blame] | 2323 | /* LOOKUP_CACHED requires RCU, ask caller to retry */ |
| 2324 | if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) |
| 2325 | return ERR_PTR(-EAGAIN); |
| 2326 | |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2327 | if (!*s) |
| 2328 | flags &= ~LOOKUP_RCU; |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2329 | if (flags & LOOKUP_RCU) |
| 2330 | rcu_read_lock(); |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2331 | |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2332 | nd->flags = flags; |
| 2333 | nd->state |= ND_JUMPED; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2334 | |
| 2335 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); |
| 2336 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); |
| 2337 | smp_rmb(); |
| 2338 | |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2339 | if (nd->state & ND_ROOT_PRESET) { |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2340 | struct dentry *root = nd->root.dentry; |
| 2341 | struct inode *inode = root->d_inode; |
Al Viro | 9389386 | 2017-04-15 17:29:14 -0400 | [diff] [blame] | 2342 | if (*s && unlikely(!d_can_lookup(root))) |
| 2343 | return ERR_PTR(-ENOTDIR); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2344 | nd->path = nd->root; |
| 2345 | nd->inode = inode; |
| 2346 | if (flags & LOOKUP_RCU) { |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2347 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 8f47a016 | 2015-05-09 19:02:01 -0400 | [diff] [blame] | 2348 | nd->root_seq = nd->seq; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2349 | } else { |
| 2350 | path_get(&nd->path); |
| 2351 | } |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2352 | return s; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2353 | } |
| 2354 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 2355 | nd->root.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2357 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
| 2358 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2359 | error = nd_jump_root(nd); |
| 2360 | if (unlikely(error)) |
| 2361 | return ERR_PTR(error); |
| 2362 | return s; |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | /* Relative pathname -- get the starting-point it is relative to. */ |
| 2366 | if (nd->dfd == AT_FDCWD) { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2367 | if (flags & LOOKUP_RCU) { |
| 2368 | struct fs_struct *fs = current->fs; |
| 2369 | unsigned seq; |
| 2370 | |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2371 | do { |
| 2372 | seq = read_seqcount_begin(&fs->seq); |
| 2373 | nd->path = fs->pwd; |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2374 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2375 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 2376 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 2377 | } else { |
| 2378 | get_fs_pwd(current->fs, &nd->path); |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2379 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2380 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2381 | } else { |
Jeff Layton | 582aa64 | 2012-12-11 08:56:16 -0500 | [diff] [blame] | 2382 | /* Caller must check execute permissions on the starting path component */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2383 | struct fd f = fdget_raw(nd->dfd); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2384 | struct dentry *dentry; |
| 2385 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2386 | if (!f.file) |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2387 | return ERR_PTR(-EBADF); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2388 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2389 | dentry = f.file->f_path.dentry; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2390 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2391 | if (*s && unlikely(!d_can_lookup(dentry))) { |
| 2392 | fdput(f); |
| 2393 | return ERR_PTR(-ENOTDIR); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 2394 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2395 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2396 | nd->path = f.file->f_path; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2397 | if (flags & LOOKUP_RCU) { |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2398 | nd->inode = nd->path.dentry->d_inode; |
| 2399 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2400 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2401 | path_get(&nd->path); |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2402 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2403 | } |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2404 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | } |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2406 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 2407 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
| 2408 | if (flags & LOOKUP_IS_SCOPED) { |
| 2409 | nd->root = nd->path; |
| 2410 | if (flags & LOOKUP_RCU) { |
| 2411 | nd->root_seq = nd->seq; |
| 2412 | } else { |
| 2413 | path_get(&nd->root); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2414 | nd->state |= ND_ROOT_GRABBED; |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 2415 | } |
| 2416 | } |
| 2417 | return s; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2418 | } |
| 2419 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2420 | static inline const char *lookup_last(struct nameidata *nd) |
Al Viro | 95fa25d | 2015-04-22 13:46:57 -0400 | [diff] [blame] | 2421 | { |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2422 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) |
| 2423 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 2424 | |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2425 | return walk_component(nd, WALK_TRAILING); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2426 | } |
| 2427 | |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2428 | static int handle_lookup_down(struct nameidata *nd) |
| 2429 | { |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 2430 | if (!(nd->flags & LOOKUP_RCU)) |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 2431 | dget(nd->path.dentry); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 2432 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
| 2433 | nd->path.dentry, nd->inode, nd->seq)); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2434 | } |
| 2435 | |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2436 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2437 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2438 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2439 | const char *s = path_init(nd, flags); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2440 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2441 | |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2442 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2443 | err = handle_lookup_down(nd); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 2444 | if (unlikely(err < 0)) |
| 2445 | s = ERR_PTR(err); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2446 | } |
| 2447 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2448 | while (!(err = link_path_walk(s, nd)) && |
| 2449 | (s = lookup_last(nd)) != NULL) |
| 2450 | ; |
Al Viro | 4f0ed93 | 2021-04-06 19:46:51 -0400 | [diff] [blame] | 2451 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
| 2452 | err = handle_lookup_down(nd); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2453 | nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... |
Al Viro | 4f0ed93 | 2021-04-06 19:46:51 -0400 | [diff] [blame] | 2454 | } |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2455 | if (!err) |
| 2456 | err = complete_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2457 | |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 2458 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
| 2459 | if (!d_can_lookup(nd->path.dentry)) |
Al Viro | bd23a53 | 2011-03-23 09:56:30 -0400 | [diff] [blame] | 2460 | err = -ENOTDIR; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 2461 | if (!err) { |
| 2462 | *path = nd->path; |
| 2463 | nd->path.mnt = NULL; |
| 2464 | nd->path.dentry = NULL; |
| 2465 | } |
| 2466 | terminate_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2467 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2468 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2469 | |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2470 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2471 | struct path *path, struct path *root) |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2472 | { |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 2473 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2474 | struct nameidata nd; |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2475 | if (IS_ERR(name)) |
| 2476 | return PTR_ERR(name); |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 2477 | set_nameidata(&nd, dfd, name, root); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2478 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2479 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2480 | retval = path_lookupat(&nd, flags, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2481 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2482 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2483 | |
| 2484 | if (likely(!retval)) |
Al Viro | 161aff1 | 2020-01-11 22:52:26 -0500 | [diff] [blame] | 2485 | audit_inode(name, path->dentry, |
| 2486 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2487 | restore_nameidata(); |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 2488 | return retval; |
| 2489 | } |
| 2490 | |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2491 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2492 | static int path_parentat(struct nameidata *nd, unsigned flags, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2493 | struct path *parent) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2494 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2495 | const char *s = path_init(nd, flags); |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2496 | int err = link_path_walk(s, nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2497 | if (!err) |
| 2498 | err = complete_walk(nd); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2499 | if (!err) { |
| 2500 | *parent = nd->path; |
| 2501 | nd->path.mnt = NULL; |
| 2502 | nd->path.dentry = NULL; |
| 2503 | } |
| 2504 | terminate_walk(nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2505 | return err; |
| 2506 | } |
| 2507 | |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2508 | /* Note: this does not consume "name" */ |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 2509 | static int filename_parentat(int dfd, struct filename *name, |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2510 | unsigned int flags, struct path *parent, |
| 2511 | struct qstr *last, int *type) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2512 | { |
| 2513 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2514 | struct nameidata nd; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2515 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2516 | if (IS_ERR(name)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2517 | return PTR_ERR(name); |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 2518 | set_nameidata(&nd, dfd, name, NULL); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2519 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2520 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2521 | retval = path_parentat(&nd, flags, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2522 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2523 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2524 | if (likely(!retval)) { |
| 2525 | *last = nd.last; |
| 2526 | *type = nd.last_type; |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 2527 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2528 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2529 | restore_nameidata(); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2530 | return retval; |
| 2531 | } |
| 2532 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2533 | /* does lookup, returns the object with parent locked */ |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2534 | static struct dentry *__kern_path_locked(struct filename *name, struct path *path) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2535 | { |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2536 | struct dentry *d; |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2537 | struct qstr last; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2538 | int type, error; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2539 | |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 2540 | error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2541 | if (error) |
| 2542 | return ERR_PTR(error); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2543 | if (unlikely(type != LAST_NORM)) { |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2544 | path_put(path); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2545 | return ERR_PTR(-EINVAL); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2546 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2547 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2548 | d = __lookup_hash(&last, path->dentry, 0); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2549 | if (IS_ERR(d)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2550 | inode_unlock(path->dentry->d_inode); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2551 | path_put(path); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2552 | } |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2553 | return d; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2554 | } |
| 2555 | |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2556 | struct dentry *kern_path_locked(const char *name, struct path *path) |
| 2557 | { |
| 2558 | struct filename *filename = getname_kernel(name); |
| 2559 | struct dentry *res = __kern_path_locked(filename, path); |
| 2560 | |
| 2561 | putname(filename); |
| 2562 | return res; |
| 2563 | } |
| 2564 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2565 | int kern_path(const char *name, unsigned int flags, struct path *path) |
| 2566 | { |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2567 | struct filename *filename = getname_kernel(name); |
| 2568 | int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL); |
| 2569 | |
| 2570 | putname(filename); |
| 2571 | return ret; |
| 2572 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2573 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2574 | EXPORT_SYMBOL(kern_path); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2575 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2576 | /** |
| 2577 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 2578 | * @dentry: pointer to dentry of the base directory |
| 2579 | * @mnt: pointer to vfs mount of the base directory |
| 2580 | * @name: pointer to file name |
| 2581 | * @flags: lookup flags |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2582 | * @path: pointer to struct path to fill |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2583 | */ |
| 2584 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 2585 | const char *name, unsigned int flags, |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2586 | struct path *path) |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2587 | { |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2588 | struct filename *filename; |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2589 | struct path root = {.mnt = mnt, .dentry = dentry}; |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2590 | int ret; |
| 2591 | |
| 2592 | filename = getname_kernel(name); |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2593 | /* the first argument of filename_lookup() is ignored with root */ |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2594 | ret = filename_lookup(AT_FDCWD, filename, flags, path, &root); |
| 2595 | putname(filename); |
| 2596 | return ret; |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2597 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2598 | EXPORT_SYMBOL(vfs_path_lookup); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2599 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2600 | static int lookup_one_common(struct user_namespace *mnt_userns, |
| 2601 | const char *name, struct dentry *base, int len, |
| 2602 | struct qstr *this) |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2603 | { |
| 2604 | this->name = name; |
| 2605 | this->len = len; |
| 2606 | this->hash = full_name_hash(base, name, len); |
| 2607 | if (!len) |
| 2608 | return -EACCES; |
| 2609 | |
| 2610 | if (unlikely(name[0] == '.')) { |
| 2611 | if (len < 2 || (len == 2 && name[1] == '.')) |
| 2612 | return -EACCES; |
| 2613 | } |
| 2614 | |
| 2615 | while (len--) { |
| 2616 | unsigned int c = *(const unsigned char *)name++; |
| 2617 | if (c == '/' || c == '\0') |
| 2618 | return -EACCES; |
| 2619 | } |
| 2620 | /* |
| 2621 | * See if the low-level filesystem might want |
| 2622 | * to use its own hash.. |
| 2623 | */ |
| 2624 | if (base->d_flags & DCACHE_OP_HASH) { |
| 2625 | int err = base->d_op->d_hash(base, this); |
| 2626 | if (err < 0) |
| 2627 | return err; |
| 2628 | } |
| 2629 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2630 | return inode_permission(mnt_userns, base->d_inode, MAY_EXEC); |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2631 | } |
| 2632 | |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2633 | /** |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2634 | * try_lookup_one_len - filesystem helper to lookup single pathname component |
| 2635 | * @name: pathname component to lookup |
| 2636 | * @base: base directory to lookup from |
| 2637 | * @len: maximum length @len should be interpreted to |
| 2638 | * |
| 2639 | * Look up a dentry by name in the dcache, returning NULL if it does not |
| 2640 | * currently exist. The function does not try to create a dentry. |
| 2641 | * |
| 2642 | * Note that this routine is purely a helper for filesystem usage and should |
| 2643 | * not be called by generic code. |
| 2644 | * |
| 2645 | * The caller must hold base->i_mutex. |
| 2646 | */ |
| 2647 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) |
| 2648 | { |
| 2649 | struct qstr this; |
| 2650 | int err; |
| 2651 | |
| 2652 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
| 2653 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2654 | err = lookup_one_common(&init_user_ns, name, base, len, &this); |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2655 | if (err) |
| 2656 | return ERR_PTR(err); |
| 2657 | |
| 2658 | return lookup_dcache(&this, base, 0); |
| 2659 | } |
| 2660 | EXPORT_SYMBOL(try_lookup_one_len); |
| 2661 | |
| 2662 | /** |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2663 | * lookup_one_len - filesystem helper to lookup single pathname component |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2664 | * @name: pathname component to lookup |
| 2665 | * @base: base directory to lookup from |
| 2666 | * @len: maximum length @len should be interpreted to |
| 2667 | * |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2668 | * Note that this routine is purely a helper for filesystem usage and should |
Al Viro | 9e7543e | 2015-02-23 02:49:48 -0500 | [diff] [blame] | 2669 | * not be called by generic code. |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2670 | * |
| 2671 | * The caller must hold base->i_mutex. |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2672 | */ |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2673 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
| 2674 | { |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2675 | struct dentry *dentry; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2676 | struct qstr this; |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2677 | int err; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2678 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2679 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2680 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2681 | err = lookup_one_common(&init_user_ns, name, base, len, &this); |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2682 | if (err) |
| 2683 | return ERR_PTR(err); |
| 2684 | |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2685 | dentry = lookup_dcache(&this, base, 0); |
| 2686 | return dentry ? dentry : __lookup_slow(&this, base, 0); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2687 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2688 | EXPORT_SYMBOL(lookup_one_len); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2689 | |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2690 | /** |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2691 | * lookup_one - filesystem helper to lookup single pathname component |
| 2692 | * @mnt_userns: user namespace of the mount the lookup is performed from |
| 2693 | * @name: pathname component to lookup |
| 2694 | * @base: base directory to lookup from |
| 2695 | * @len: maximum length @len should be interpreted to |
| 2696 | * |
| 2697 | * Note that this routine is purely a helper for filesystem usage and should |
| 2698 | * not be called by generic code. |
| 2699 | * |
| 2700 | * The caller must hold base->i_mutex. |
| 2701 | */ |
| 2702 | struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name, |
| 2703 | struct dentry *base, int len) |
| 2704 | { |
| 2705 | struct dentry *dentry; |
| 2706 | struct qstr this; |
| 2707 | int err; |
| 2708 | |
| 2709 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
| 2710 | |
| 2711 | err = lookup_one_common(mnt_userns, name, base, len, &this); |
| 2712 | if (err) |
| 2713 | return ERR_PTR(err); |
| 2714 | |
| 2715 | dentry = lookup_dcache(&this, base, 0); |
| 2716 | return dentry ? dentry : __lookup_slow(&this, base, 0); |
| 2717 | } |
| 2718 | EXPORT_SYMBOL(lookup_one); |
| 2719 | |
| 2720 | /** |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2721 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component |
| 2722 | * @name: pathname component to lookup |
| 2723 | * @base: base directory to lookup from |
| 2724 | * @len: maximum length @len should be interpreted to |
| 2725 | * |
| 2726 | * Note that this routine is purely a helper for filesystem usage and should |
| 2727 | * not be called by generic code. |
| 2728 | * |
| 2729 | * Unlike lookup_one_len, it should be called without the parent |
| 2730 | * i_mutex held, and will take the i_mutex itself if necessary. |
| 2731 | */ |
| 2732 | struct dentry *lookup_one_len_unlocked(const char *name, |
| 2733 | struct dentry *base, int len) |
| 2734 | { |
| 2735 | struct qstr this; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2736 | int err; |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2737 | struct dentry *ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2738 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2739 | err = lookup_one_common(&init_user_ns, name, base, len, &this); |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2740 | if (err) |
| 2741 | return ERR_PTR(err); |
| 2742 | |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2743 | ret = lookup_dcache(&this, base, 0); |
| 2744 | if (!ret) |
| 2745 | ret = lookup_slow(&this, base, 0); |
| 2746 | return ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2747 | } |
| 2748 | EXPORT_SYMBOL(lookup_one_len_unlocked); |
| 2749 | |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2750 | /* |
| 2751 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) |
| 2752 | * on negatives. Returns known positive or ERR_PTR(); that's what |
| 2753 | * most of the users want. Note that pinned negative with unlocked parent |
| 2754 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() |
| 2755 | * need to be very careful; pinned positives have ->d_inode stable, so |
| 2756 | * this one avoids such problems. |
| 2757 | */ |
| 2758 | struct dentry *lookup_positive_unlocked(const char *name, |
| 2759 | struct dentry *base, int len) |
| 2760 | { |
| 2761 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); |
Al Viro | 2fa6b1e | 2019-11-12 16:13:06 -0500 | [diff] [blame] | 2762 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2763 | dput(ret); |
| 2764 | ret = ERR_PTR(-ENOENT); |
| 2765 | } |
| 2766 | return ret; |
| 2767 | } |
| 2768 | EXPORT_SYMBOL(lookup_positive_unlocked); |
| 2769 | |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2770 | #ifdef CONFIG_UNIX98_PTYS |
| 2771 | int path_pts(struct path *path) |
| 2772 | { |
| 2773 | /* Find something mounted on "pts" in the same directory as |
| 2774 | * the input path. |
| 2775 | */ |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2776 | struct dentry *parent = dget_parent(path->dentry); |
| 2777 | struct dentry *child; |
Al Viro | 19f6028 | 2020-02-26 20:09:37 -0500 | [diff] [blame] | 2778 | struct qstr this = QSTR_INIT("pts", 3); |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2779 | |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2780 | if (unlikely(!path_connected(path->mnt, parent))) { |
| 2781 | dput(parent); |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2782 | return -ENOENT; |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2783 | } |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2784 | dput(path->dentry); |
| 2785 | path->dentry = parent; |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2786 | child = d_hash_and_lookup(parent, &this); |
| 2787 | if (!child) |
| 2788 | return -ENOENT; |
| 2789 | |
| 2790 | path->dentry = child; |
| 2791 | dput(parent); |
Al Viro | 19f6028 | 2020-02-26 20:09:37 -0500 | [diff] [blame] | 2792 | follow_down(path); |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2793 | return 0; |
| 2794 | } |
| 2795 | #endif |
| 2796 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2797 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
| 2798 | struct path *path, int *empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | { |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2800 | struct filename *filename = getname_flags(name, flags, empty); |
| 2801 | int ret = filename_lookup(dfd, filename, flags, path, NULL); |
| 2802 | |
| 2803 | putname(filename); |
| 2804 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | } |
Al Viro | b853a16 | 2015-05-13 09:12:02 -0400 | [diff] [blame] | 2806 | EXPORT_SYMBOL(user_path_at_empty); |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2807 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2808 | int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir, |
| 2809 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | { |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2811 | kuid_t fsuid = current_fsuid(); |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2812 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2813 | if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | return 0; |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2815 | if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | return 0; |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2817 | return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | } |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2819 | EXPORT_SYMBOL(__check_sticky); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | |
| 2821 | /* |
| 2822 | * Check whether we can remove a link victim from directory dir, check |
| 2823 | * whether the type of victim is right. |
| 2824 | * 1. We can't do it if dir is read-only (done in permission()) |
| 2825 | * 2. We should have write and exec permissions on dir |
| 2826 | * 3. We can't remove anything from append-only dir |
| 2827 | * 4. We can't do anything with immutable dir (done in permission()) |
| 2828 | * 5. If the sticky bit on dir is set we should either |
| 2829 | * a. be owner of dir, or |
| 2830 | * b. be owner of victim, or |
| 2831 | * c. have CAP_FOWNER capability |
| 2832 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 2833 | * links pointing to it. |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2834 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
| 2835 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 2836 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 2837 | * 10. We can't remove a root or mountpoint. |
| 2838 | * 11. We don't allow removal of NFS sillyrenamed files; it's handled by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | * nfs_async_unlink(). |
| 2840 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2841 | static int may_delete(struct user_namespace *mnt_userns, struct inode *dir, |
| 2842 | struct dentry *victim, bool isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | { |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 2844 | struct inode *inode = d_backing_inode(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | int error; |
| 2846 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2847 | if (d_is_negative(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | return -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2849 | BUG_ON(!inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | |
| 2851 | BUG_ON(victim->d_parent->d_inode != dir); |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 2852 | |
| 2853 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2854 | if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || |
| 2855 | !gid_valid(i_gid_into_mnt(mnt_userns, inode))) |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 2856 | return -EOVERFLOW; |
| 2857 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 2858 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2860 | error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | if (error) |
| 2862 | return error; |
| 2863 | if (IS_APPEND(dir)) |
| 2864 | return -EPERM; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2865 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2866 | if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) || |
| 2867 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || |
| 2868 | HAS_UNMAPPED_ID(mnt_userns, inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | return -EPERM; |
| 2870 | if (isdir) { |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2871 | if (!d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | return -ENOTDIR; |
| 2873 | if (IS_ROOT(victim)) |
| 2874 | return -EBUSY; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2875 | } else if (d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | return -EISDIR; |
| 2877 | if (IS_DEADDIR(dir)) |
| 2878 | return -ENOENT; |
| 2879 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 2880 | return -EBUSY; |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
| 2884 | /* Check whether we can create an object with dentry child in directory |
| 2885 | * dir. |
| 2886 | * 1. We can't do it if child already exists (open has special treatment for |
| 2887 | * this case, but since we are inlined it's OK) |
| 2888 | * 2. We can't do it if dir is read-only (done in permission()) |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2889 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
| 2890 | * 4. We should have write and exec permissions on dir |
| 2891 | * 5. We can't do it if dir is immutable (done in permission()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2893 | static inline int may_create(struct user_namespace *mnt_userns, |
| 2894 | struct inode *dir, struct dentry *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | { |
Jeff Layton | 14e972b | 2013-05-08 10:25:58 -0400 | [diff] [blame] | 2896 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | if (child->d_inode) |
| 2898 | return -EEXIST; |
| 2899 | if (IS_DEADDIR(dir)) |
| 2900 | return -ENOENT; |
Christian Brauner | 8e53891 | 2021-03-20 13:26:23 +0100 | [diff] [blame] | 2901 | if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2902 | return -EOVERFLOW; |
Christian Brauner | 8e53891 | 2021-03-20 13:26:23 +0100 | [diff] [blame] | 2903 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2904 | return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | } |
| 2906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | /* |
| 2908 | * p1 and p2 should be directories on the same fs. |
| 2909 | */ |
| 2910 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 2911 | { |
| 2912 | struct dentry *p; |
| 2913 | |
| 2914 | if (p1 == p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2915 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | return NULL; |
| 2917 | } |
| 2918 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2919 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2921 | p = d_ancestor(p2, p1); |
| 2922 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2923 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
| 2924 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2925 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | } |
| 2927 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2928 | p = d_ancestor(p1, p2); |
| 2929 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2930 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2931 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2932 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2935 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2936 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | return NULL; |
| 2938 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2939 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | |
| 2941 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 2942 | { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2943 | inode_unlock(p1->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | if (p1 != p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2945 | inode_unlock(p2->d_inode); |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2946 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | } |
| 2948 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2949 | EXPORT_SYMBOL(unlock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 2951 | /** |
| 2952 | * vfs_create - create new file |
| 2953 | * @mnt_userns: user namespace of the mount the inode was found from |
| 2954 | * @dir: inode of @dentry |
| 2955 | * @dentry: pointer to dentry of the base directory |
| 2956 | * @mode: mode of the new file |
| 2957 | * @want_excl: whether the file must not yet exist |
| 2958 | * |
| 2959 | * Create a new file. |
| 2960 | * |
| 2961 | * If the inode has been found through an idmapped mount the user namespace of |
| 2962 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 2963 | * care to map the inode according to @mnt_userns before checking permissions. |
| 2964 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 2965 | * raw inode simply passs init_user_ns. |
| 2966 | */ |
| 2967 | int vfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 2968 | struct dentry *dentry, umode_t mode, bool want_excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 2970 | int error = may_create(mnt_userns, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | if (error) |
| 2972 | return error; |
| 2973 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 2974 | if (!dir->i_op->create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 2976 | mode &= S_IALLUGO; |
| 2977 | mode |= S_IFREG; |
| 2978 | error = security_inode_create(dir, dentry, mode); |
| 2979 | if (error) |
| 2980 | return error; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2981 | error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 2982 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2983 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | return error; |
| 2985 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2986 | EXPORT_SYMBOL(vfs_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 2988 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
| 2989 | int (*f)(struct dentry *, umode_t, void *), |
| 2990 | void *arg) |
| 2991 | { |
| 2992 | struct inode *dir = dentry->d_parent->d_inode; |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2993 | int error = may_create(&init_user_ns, dir, dentry); |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 2994 | if (error) |
| 2995 | return error; |
| 2996 | |
| 2997 | mode &= S_IALLUGO; |
| 2998 | mode |= S_IFREG; |
| 2999 | error = security_inode_create(dir, dentry, mode); |
| 3000 | if (error) |
| 3001 | return error; |
| 3002 | error = f(dentry, mode, arg); |
| 3003 | if (!error) |
| 3004 | fsnotify_create(dir, dentry); |
| 3005 | return error; |
| 3006 | } |
| 3007 | EXPORT_SYMBOL(vfs_mkobj); |
| 3008 | |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 3009 | bool may_open_dev(const struct path *path) |
| 3010 | { |
| 3011 | return !(path->mnt->mnt_flags & MNT_NODEV) && |
| 3012 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); |
| 3013 | } |
| 3014 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3015 | static int may_open(struct user_namespace *mnt_userns, const struct path *path, |
| 3016 | int acc_mode, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | { |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 3018 | struct dentry *dentry = path->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | struct inode *inode = dentry->d_inode; |
| 3020 | int error; |
| 3021 | |
| 3022 | if (!inode) |
| 3023 | return -ENOENT; |
| 3024 | |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3025 | switch (inode->i_mode & S_IFMT) { |
| 3026 | case S_IFLNK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | return -ELOOP; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3028 | case S_IFDIR: |
Kees Cook | fc4177b | 2020-08-14 17:30:14 -0700 | [diff] [blame] | 3029 | if (acc_mode & MAY_WRITE) |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3030 | return -EISDIR; |
Kees Cook | fc4177b | 2020-08-14 17:30:14 -0700 | [diff] [blame] | 3031 | if (acc_mode & MAY_EXEC) |
| 3032 | return -EACCES; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3033 | break; |
| 3034 | case S_IFBLK: |
| 3035 | case S_IFCHR: |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 3036 | if (!may_open_dev(path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | return -EACCES; |
Kees Cook | 633fb6a | 2020-08-11 18:36:26 -0700 | [diff] [blame] | 3038 | fallthrough; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3039 | case S_IFIFO: |
| 3040 | case S_IFSOCK: |
Kees Cook | 633fb6a | 2020-08-11 18:36:26 -0700 | [diff] [blame] | 3041 | if (acc_mode & MAY_EXEC) |
| 3042 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | flag &= ~O_TRUNC; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3044 | break; |
Kees Cook | 0fd338b | 2020-08-11 18:36:30 -0700 | [diff] [blame] | 3045 | case S_IFREG: |
| 3046 | if ((acc_mode & MAY_EXEC) && path_noexec(path)) |
| 3047 | return -EACCES; |
| 3048 | break; |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 3049 | } |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 3050 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3051 | error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode); |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 3052 | if (error) |
| 3053 | return error; |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 3054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | /* |
| 3056 | * An append-only file must be opened in append mode for writing. |
| 3057 | */ |
| 3058 | if (IS_APPEND(inode)) { |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 3059 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3060 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | if (flag & O_TRUNC) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3062 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | /* O_NOATIME can only be set by the owner or superuser */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3066 | if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3067 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 3069 | return 0; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3072 | static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3073 | { |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 3074 | const struct path *path = &filp->f_path; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3075 | struct inode *inode = path->dentry->d_inode; |
| 3076 | int error = get_write_access(inode); |
| 3077 | if (error) |
| 3078 | return error; |
Jeff Layton | 482e000 | 2021-10-26 11:56:45 -0400 | [diff] [blame] | 3079 | |
Jeff Layton | f7e33bd | 2021-08-19 14:56:38 -0400 | [diff] [blame] | 3080 | error = security_path_truncate(path); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3081 | if (!error) { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3082 | error = do_truncate(mnt_userns, path->dentry, 0, |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3083 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 3084 | filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3085 | } |
| 3086 | put_write_access(inode); |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 3087 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | } |
| 3089 | |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 3090 | static inline int open_to_namei_flags(int flag) |
| 3091 | { |
Al Viro | 8a5e929 | 2011-06-25 19:15:54 -0400 | [diff] [blame] | 3092 | if ((flag & O_ACCMODE) == 3) |
| 3093 | flag--; |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 3094 | return flag; |
| 3095 | } |
| 3096 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3097 | static int may_o_create(struct user_namespace *mnt_userns, |
| 3098 | const struct path *dir, struct dentry *dentry, |
| 3099 | umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3100 | { |
| 3101 | int error = security_path_mknod(dir, dentry, mode, 0); |
| 3102 | if (error) |
| 3103 | return error; |
| 3104 | |
Christian Brauner | 8e53891 | 2021-03-20 13:26:23 +0100 | [diff] [blame] | 3105 | if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns)) |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 3106 | return -EOVERFLOW; |
| 3107 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3108 | error = inode_permission(mnt_userns, dir->dentry->d_inode, |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 3109 | MAY_WRITE | MAY_EXEC); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3110 | if (error) |
| 3111 | return error; |
| 3112 | |
| 3113 | return security_inode_create(dir->dentry->d_inode, dentry, mode); |
| 3114 | } |
| 3115 | |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3116 | /* |
| 3117 | * Attempt to atomically look up, create and open a file from a negative |
| 3118 | * dentry. |
| 3119 | * |
| 3120 | * Returns 0 if successful. The file will have been created and attached to |
| 3121 | * @file by the filesystem calling finish_open(). |
| 3122 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3123 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
| 3124 | * be set. The caller will need to perform the open themselves. @path will |
| 3125 | * have been updated to point to the new dentry. This may be negative. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3126 | * |
| 3127 | * Returns an error code otherwise. |
| 3128 | */ |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3129 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
| 3130 | struct file *file, |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3131 | int open_flag, umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3132 | { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3133 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3134 | struct inode *dir = nd->path.dentry->d_inode; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3135 | int error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3136 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3137 | if (nd->flags & LOOKUP_DIRECTORY) |
| 3138 | open_flag |= O_DIRECTORY; |
| 3139 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3140 | file->f_path.dentry = DENTRY_NOT_SET; |
| 3141 | file->f_path.mnt = nd->path.mnt; |
Al Viro | 0fb1ea0 | 2016-04-27 14:13:10 -0400 | [diff] [blame] | 3142 | error = dir->i_op->atomic_open(dir, dentry, file, |
Al Viro | 44907d7 | 2018-06-08 13:32:02 -0400 | [diff] [blame] | 3143 | open_to_namei_flags(open_flag), mode); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3144 | d_lookup_done(dentry); |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 3145 | if (!error) { |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 3146 | if (file->f_mode & FMODE_OPENED) { |
Al Viro | 6fb968c | 2020-01-26 09:53:19 -0500 | [diff] [blame] | 3147 | if (unlikely(dentry != file->f_path.dentry)) { |
| 3148 | dput(dentry); |
| 3149 | dentry = dget(file->f_path.dentry); |
| 3150 | } |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 3151 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3152 | error = -EIO; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 3153 | } else { |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 3154 | if (file->f_path.dentry) { |
| 3155 | dput(dentry); |
| 3156 | dentry = file->f_path.dentry; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 3157 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3158 | if (unlikely(d_is_negative(dentry))) |
Al Viro | a01e718 | 2016-06-07 21:53:51 -0400 | [diff] [blame] | 3159 | error = -ENOENT; |
Sage Weil | 62b2ce9 | 2012-08-15 13:30:12 -0700 | [diff] [blame] | 3160 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3161 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3162 | if (error) { |
| 3163 | dput(dentry); |
| 3164 | dentry = ERR_PTR(error); |
| 3165 | } |
| 3166 | return dentry; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3167 | } |
| 3168 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3169 | /* |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3170 | * Look up and maybe create and open the last component. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3171 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3172 | * Must be called with parent locked (exclusive in O_CREAT case). |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3173 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3174 | * Returns 0 on success, that is, if |
| 3175 | * the file was successfully atomically created (if necessary) and opened, or |
| 3176 | * the file was not completely opened at this time, though lookups and |
| 3177 | * creations were performed. |
| 3178 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. |
| 3179 | * In the latter case dentry returned in @path might be negative if O_CREAT |
| 3180 | * hadn't been specified. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3181 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3182 | * An error code is returned on failure. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3183 | */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3184 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
| 3185 | const struct open_flags *op, |
| 3186 | bool got_write) |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3187 | { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3188 | struct user_namespace *mnt_userns; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3189 | struct dentry *dir = nd->path.dentry; |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3190 | struct inode *dir_inode = dir->d_inode; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3191 | int open_flag = op->open_flag; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3192 | struct dentry *dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3193 | int error, create_error = 0; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3194 | umode_t mode = op->mode; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3195 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3196 | |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3197 | if (unlikely(IS_DEADDIR(dir_inode))) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3198 | return ERR_PTR(-ENOENT); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3199 | |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3200 | file->f_mode &= ~FMODE_CREATED; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3201 | dentry = d_lookup(dir, &nd->last); |
| 3202 | for (;;) { |
| 3203 | if (!dentry) { |
| 3204 | dentry = d_alloc_parallel(dir, &nd->last, &wq); |
| 3205 | if (IS_ERR(dentry)) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3206 | return dentry; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3207 | } |
| 3208 | if (d_in_lookup(dentry)) |
| 3209 | break; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3210 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3211 | error = d_revalidate(dentry, nd->flags); |
| 3212 | if (likely(error > 0)) |
| 3213 | break; |
| 3214 | if (error) |
| 3215 | goto out_dput; |
| 3216 | d_invalidate(dentry); |
| 3217 | dput(dentry); |
| 3218 | dentry = NULL; |
| 3219 | } |
| 3220 | if (dentry->d_inode) { |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3221 | /* Cached positive dentry: will open in f_op->open */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3222 | return dentry; |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3223 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3224 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3225 | /* |
| 3226 | * Checking write permission is tricky, bacuse we don't know if we are |
| 3227 | * going to actually need it: O_CREAT opens should work as long as the |
| 3228 | * file exists. But checking existence breaks atomicity. The trick is |
| 3229 | * to check access and if not granted clear O_CREAT from the flags. |
| 3230 | * |
| 3231 | * Another problem is returing the "right" error value (e.g. for an |
| 3232 | * O_EXCL open we want to return EEXIST not EROFS). |
| 3233 | */ |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3234 | if (unlikely(!got_write)) |
| 3235 | open_flag &= ~O_TRUNC; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3236 | mnt_userns = mnt_user_ns(nd->path.mnt); |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3237 | if (open_flag & O_CREAT) { |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3238 | if (open_flag & O_EXCL) |
| 3239 | open_flag &= ~O_TRUNC; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3240 | if (!IS_POSIXACL(dir->d_inode)) |
| 3241 | mode &= ~current_umask(); |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3242 | if (likely(got_write)) |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3243 | create_error = may_o_create(mnt_userns, &nd->path, |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3244 | dentry, mode); |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3245 | else |
| 3246 | create_error = -EROFS; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3247 | } |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3248 | if (create_error) |
| 3249 | open_flag &= ~O_CREAT; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3250 | if (dir_inode->i_op->atomic_open) { |
Al Viro | d489cf9 | 2020-03-11 08:07:53 -0400 | [diff] [blame] | 3251 | dentry = atomic_open(nd, dentry, file, open_flag, mode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3252 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
| 3253 | dentry = ERR_PTR(create_error); |
| 3254 | return dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3255 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3256 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3257 | if (d_in_lookup(dentry)) { |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3258 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
| 3259 | nd->flags); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3260 | d_lookup_done(dentry); |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3261 | if (unlikely(res)) { |
| 3262 | if (IS_ERR(res)) { |
| 3263 | error = PTR_ERR(res); |
| 3264 | goto out_dput; |
| 3265 | } |
| 3266 | dput(dentry); |
| 3267 | dentry = res; |
| 3268 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3271 | /* Negative dentry, just create the file */ |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3272 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3273 | file->f_mode |= FMODE_CREATED; |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3274 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3275 | if (!dir_inode->i_op->create) { |
| 3276 | error = -EACCES; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3277 | goto out_dput; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3278 | } |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3279 | |
| 3280 | error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry, |
| 3281 | mode, open_flag & O_EXCL); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3282 | if (error) |
| 3283 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3284 | } |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3285 | if (unlikely(create_error) && !dentry->d_inode) { |
| 3286 | error = create_error; |
| 3287 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3288 | } |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3289 | return dentry; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3290 | |
| 3291 | out_dput: |
| 3292 | dput(dentry); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3293 | return ERR_PTR(error); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3294 | } |
| 3295 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3296 | static const char *open_last_lookups(struct nameidata *nd, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3297 | struct file *file, const struct open_flags *op) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3298 | { |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3299 | struct dentry *dir = nd->path.dentry; |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3300 | int open_flag = op->open_flag; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3301 | bool got_write = false; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 3302 | unsigned seq; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3303 | struct inode *inode; |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3304 | struct dentry *dentry; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3305 | const char *res; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3306 | |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 3307 | nd->flags |= op->intent; |
| 3308 | |
Al Viro | bc77daa | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 3309 | if (nd->last_type != LAST_NORM) { |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3310 | if (nd->depth) |
| 3311 | put_link(nd); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3312 | return handle_dots(nd, nd->last_type); |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 3313 | } |
Al Viro | 67ee3ad | 2009-12-26 07:01:01 -0500 | [diff] [blame] | 3314 | |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3315 | if (!(open_flag & O_CREAT)) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3316 | if (nd->last.name[nd->last.len]) |
| 3317 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 3318 | /* we _can_ be in RCU mode here */ |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3319 | dentry = lookup_fast(nd, &inode, &seq); |
| 3320 | if (IS_ERR(dentry)) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3321 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3322 | if (likely(dentry)) |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 3323 | goto finish_lookup; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3324 | |
Al Viro | 6583fe2 | 2016-03-05 18:14:03 -0500 | [diff] [blame] | 3325 | BUG_ON(nd->flags & LOOKUP_RCU); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3326 | } else { |
| 3327 | /* create side of things */ |
Al Viro | 7228741 | 2020-03-10 10:09:26 -0400 | [diff] [blame] | 3328 | if (nd->flags & LOOKUP_RCU) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 3329 | if (!try_to_unlazy(nd)) |
| 3330 | return ERR_PTR(-ECHILD); |
Al Viro | 7228741 | 2020-03-10 10:09:26 -0400 | [diff] [blame] | 3331 | } |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 3332 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3333 | /* trailing slashes? */ |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 3334 | if (unlikely(nd->last.name[nd->last.len])) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3335 | return ERR_PTR(-EISDIR); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3336 | } |
| 3337 | |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3338 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 3339 | got_write = !mnt_want_write(nd->path.mnt); |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3340 | /* |
| 3341 | * do _not_ fail yet - we might not need that or fail with |
| 3342 | * a different error; let lookup_open() decide; we'll be |
| 3343 | * dropping this one anyway. |
| 3344 | */ |
| 3345 | } |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3346 | if (open_flag & O_CREAT) |
| 3347 | inode_lock(dir->d_inode); |
| 3348 | else |
| 3349 | inode_lock_shared(dir->d_inode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3350 | dentry = lookup_open(nd, file, op, got_write); |
Al Viro | f7bb959 | 2020-03-05 13:25:20 -0500 | [diff] [blame] | 3351 | if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) |
| 3352 | fsnotify_create(dir->d_inode, dentry); |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3353 | if (open_flag & O_CREAT) |
| 3354 | inode_unlock(dir->d_inode); |
| 3355 | else |
| 3356 | inode_unlock_shared(dir->d_inode); |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3357 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3358 | if (got_write) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3359 | mnt_drop_write(nd->path.mnt); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3360 | |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3361 | if (IS_ERR(dentry)) |
| 3362 | return ERR_CAST(dentry); |
| 3363 | |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3364 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
Al Viro | e73cabf | 2020-01-09 14:30:08 -0500 | [diff] [blame] | 3365 | dput(nd->path.dentry); |
| 3366 | nd->path.dentry = dentry; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3367 | return NULL; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3368 | } |
| 3369 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3370 | finish_lookup: |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3371 | if (nd->depth) |
| 3372 | put_link(nd); |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 3373 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3374 | if (unlikely(res)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3375 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3376 | return res; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | * Handle the last step of open() |
| 3381 | */ |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3382 | static int do_open(struct nameidata *nd, |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3383 | struct file *file, const struct open_flags *op) |
| 3384 | { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3385 | struct user_namespace *mnt_userns; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3386 | int open_flag = op->open_flag; |
| 3387 | bool do_truncate; |
| 3388 | int acc_mode; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3389 | int error; |
| 3390 | |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3391 | if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { |
| 3392 | error = complete_walk(nd); |
| 3393 | if (error) |
| 3394 | return error; |
| 3395 | } |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3396 | if (!(file->f_mode & FMODE_CREATED)) |
| 3397 | audit_inode(nd->name, nd->path.dentry, 0); |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3398 | mnt_userns = mnt_user_ns(nd->path.mnt); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3399 | if (open_flag & O_CREAT) { |
Al Viro | b94e0b3 | 2020-03-10 10:13:53 -0400 | [diff] [blame] | 3400 | if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) |
| 3401 | return -EEXIST; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3402 | if (d_is_dir(nd->path.dentry)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3403 | return -EISDIR; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3404 | error = may_create_in_sticky(mnt_userns, nd, |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3405 | d_backing_inode(nd->path.dentry)); |
| 3406 | if (unlikely(error)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3407 | return error; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3408 | } |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3409 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3410 | return -ENOTDIR; |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 3411 | |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3412 | do_truncate = false; |
| 3413 | acc_mode = op->acc_mode; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3414 | if (file->f_mode & FMODE_CREATED) { |
| 3415 | /* Don't check for write permission, don't truncate */ |
| 3416 | open_flag &= ~O_TRUNC; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3417 | acc_mode = 0; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3418 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3419 | error = mnt_want_write(nd->path.mnt); |
| 3420 | if (error) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3421 | return error; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3422 | do_truncate = true; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3423 | } |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3424 | error = may_open(mnt_userns, &nd->path, acc_mode, open_flag); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3425 | if (!error && !(file->f_mode & FMODE_OPENED)) |
Al Viro | 3ad5615 | 2020-01-26 10:06:13 -0500 | [diff] [blame] | 3426 | error = vfs_open(&nd->path, file); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3427 | if (!error) |
| 3428 | error = ima_file_check(file, op->acc_mode); |
| 3429 | if (!error && do_truncate) |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3430 | error = handle_truncate(mnt_userns, file); |
Al Viro | c80567c | 2016-02-27 19:17:33 -0500 | [diff] [blame] | 3431 | if (unlikely(error > 0)) { |
| 3432 | WARN_ON(1); |
| 3433 | error = -EINVAL; |
| 3434 | } |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3435 | if (do_truncate) |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3436 | mnt_drop_write(nd->path.mnt); |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3437 | return error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3438 | } |
| 3439 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3440 | /** |
| 3441 | * vfs_tmpfile - create tmpfile |
| 3442 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3443 | * @dentry: pointer to dentry of the base directory |
| 3444 | * @mode: mode of the new tmpfile |
Randy Dunlap | 2111c3c | 2021-02-15 20:29:28 -0800 | [diff] [blame] | 3445 | * @open_flag: flags |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3446 | * |
| 3447 | * Create a temporary file. |
| 3448 | * |
| 3449 | * If the inode has been found through an idmapped mount the user namespace of |
| 3450 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3451 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3452 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3453 | * raw inode simply passs init_user_ns. |
| 3454 | */ |
| 3455 | struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, |
| 3456 | struct dentry *dentry, umode_t mode, int open_flag) |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3457 | { |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3458 | struct dentry *child = NULL; |
| 3459 | struct inode *dir = dentry->d_inode; |
| 3460 | struct inode *inode; |
| 3461 | int error; |
| 3462 | |
| 3463 | /* we want directory to be writable */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3464 | error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3465 | if (error) |
| 3466 | goto out_err; |
| 3467 | error = -EOPNOTSUPP; |
| 3468 | if (!dir->i_op->tmpfile) |
| 3469 | goto out_err; |
| 3470 | error = -ENOMEM; |
David Howells | cdf0122 | 2017-07-04 17:25:22 +0100 | [diff] [blame] | 3471 | child = d_alloc(dentry, &slash_name); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3472 | if (unlikely(!child)) |
| 3473 | goto out_err; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3474 | error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3475 | if (error) |
| 3476 | goto out_err; |
| 3477 | error = -ENOENT; |
| 3478 | inode = child->d_inode; |
| 3479 | if (unlikely(!inode)) |
| 3480 | goto out_err; |
| 3481 | if (!(open_flag & O_EXCL)) { |
| 3482 | spin_lock(&inode->i_lock); |
| 3483 | inode->i_state |= I_LINKABLE; |
| 3484 | spin_unlock(&inode->i_lock); |
| 3485 | } |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 3486 | ima_post_create_tmpfile(mnt_userns, inode); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3487 | return child; |
| 3488 | |
| 3489 | out_err: |
| 3490 | dput(child); |
| 3491 | return ERR_PTR(error); |
| 3492 | } |
| 3493 | EXPORT_SYMBOL(vfs_tmpfile); |
| 3494 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3495 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3496 | const struct open_flags *op, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3497 | struct file *file) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3498 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3499 | struct user_namespace *mnt_userns; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3500 | struct dentry *child; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3501 | struct path path; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3502 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3503 | if (unlikely(error)) |
| 3504 | return error; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3505 | error = mnt_want_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3506 | if (unlikely(error)) |
| 3507 | goto out; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3508 | mnt_userns = mnt_user_ns(path.mnt); |
| 3509 | child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3510 | error = PTR_ERR(child); |
Hirofumi Nakagawa | 684e73b | 2017-09-26 03:21:26 +0900 | [diff] [blame] | 3511 | if (IS_ERR(child)) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3512 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3513 | dput(path.dentry); |
| 3514 | path.dentry = child; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3515 | audit_inode(nd->name, child, 0); |
Eric Rannaud | 69a91c2 | 2014-10-30 01:51:01 -0700 | [diff] [blame] | 3516 | /* Don't check for other permissions, the inode was just created */ |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3517 | error = may_open(mnt_userns, &path, 0, op->open_flag); |
Al Viro | 1e8f44f | 2020-03-11 17:22:19 -0400 | [diff] [blame] | 3518 | if (!error) |
| 3519 | error = vfs_open(&path, file); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3520 | out2: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3521 | mnt_drop_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3522 | out: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3523 | path_put(&path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3524 | return error; |
| 3525 | } |
| 3526 | |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3527 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
| 3528 | { |
| 3529 | struct path path; |
| 3530 | int error = path_lookupat(nd, flags, &path); |
| 3531 | if (!error) { |
| 3532 | audit_inode(nd->name, path.dentry, 0); |
Al Viro | ae2bb29 | 2018-07-10 13:22:28 -0400 | [diff] [blame] | 3533 | error = vfs_open(&path, file); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3534 | path_put(&path); |
| 3535 | } |
| 3536 | return error; |
| 3537 | } |
| 3538 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3539 | static struct file *path_openat(struct nameidata *nd, |
| 3540 | const struct open_flags *op, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | { |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3542 | struct file *file; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3543 | int error; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3544 | |
Al Viro | ea73ea7 | 2018-07-11 15:00:04 -0400 | [diff] [blame] | 3545 | file = alloc_empty_file(op->open_flag, current_cred()); |
Al Viro | 1afc99b | 2013-02-14 20:41:04 -0500 | [diff] [blame] | 3546 | if (IS_ERR(file)) |
| 3547 | return file; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3548 | |
Al Viro | bb458c6 | 2013-07-13 13:26:37 +0400 | [diff] [blame] | 3549 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3550 | error = do_tmpfile(nd, flags, op, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3551 | } else if (unlikely(file->f_flags & O_PATH)) { |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3552 | error = do_o_path(nd, flags, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3553 | } else { |
| 3554 | const char *s = path_init(nd, flags); |
| 3555 | while (!(error = link_path_walk(s, nd)) && |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3556 | (s = open_last_lookups(nd, file, op)) != NULL) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3557 | ; |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3558 | if (!error) |
| 3559 | error = do_open(nd, file, op); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3560 | terminate_walk(nd); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3561 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3562 | if (likely(!error)) { |
Al Viro | aad888f | 2018-06-08 12:58:04 -0400 | [diff] [blame] | 3563 | if (likely(file->f_mode & FMODE_OPENED)) |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3564 | return file; |
| 3565 | WARN_ON(1); |
| 3566 | error = -EINVAL; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 3567 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3568 | fput(file); |
| 3569 | if (error == -EOPENSTALE) { |
| 3570 | if (flags & LOOKUP_RCU) |
| 3571 | error = -ECHILD; |
| 3572 | else |
| 3573 | error = -ESTALE; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3574 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3575 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | } |
| 3577 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3578 | struct file *do_filp_open(int dfd, struct filename *pathname, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3579 | const struct open_flags *op) |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3580 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3581 | struct nameidata nd; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3582 | int flags = op->lookup_flags; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3583 | struct file *filp; |
| 3584 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 3585 | set_nameidata(&nd, dfd, pathname, NULL); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3586 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3587 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3588 | filp = path_openat(&nd, op, flags); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3589 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3590 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3591 | restore_nameidata(); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3592 | return filp; |
| 3593 | } |
| 3594 | |
Al Viro | ffb37ca | 2021-04-01 19:00:57 -0400 | [diff] [blame] | 3595 | struct file *do_file_open_root(const struct path *root, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3596 | const char *name, const struct open_flags *op) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3597 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3598 | struct nameidata nd; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3599 | struct file *file; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3600 | struct filename *filename; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 3601 | int flags = op->lookup_flags; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3602 | |
Al Viro | ffb37ca | 2021-04-01 19:00:57 -0400 | [diff] [blame] | 3603 | if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3604 | return ERR_PTR(-ELOOP); |
| 3605 | |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3606 | filename = getname_kernel(name); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 3607 | if (IS_ERR(filename)) |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3608 | return ERR_CAST(filename); |
| 3609 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 3610 | set_nameidata(&nd, -1, filename, root); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3611 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3612 | if (unlikely(file == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3613 | file = path_openat(&nd, op, flags); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3614 | if (unlikely(file == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3615 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3616 | restore_nameidata(); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3617 | putname(filename); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3618 | return file; |
| 3619 | } |
| 3620 | |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3621 | static struct dentry *filename_create(int dfd, struct filename *name, |
| 3622 | struct path *path, unsigned int lookup_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3624 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3625 | struct qstr last; |
| 3626 | int type; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3627 | int err2; |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3628 | int error; |
| 3629 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); |
| 3630 | |
| 3631 | /* |
| 3632 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any |
| 3633 | * other flags passed in are ignored! |
| 3634 | */ |
| 3635 | lookup_flags &= LOOKUP_REVAL; |
| 3636 | |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 3637 | error = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 3638 | if (error) |
| 3639 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3641 | /* |
| 3642 | * Yucky last component or no last component at all? |
| 3643 | * (foo/., foo/.., /////) |
| 3644 | */ |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3645 | if (unlikely(type != LAST_NORM)) |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3646 | goto out; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3647 | |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3648 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3649 | err2 = mnt_want_write(path->mnt); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3650 | /* |
| 3651 | * Do the final lookup. |
| 3652 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3653 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3654 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3655 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | if (IS_ERR(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3657 | goto unlock; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3658 | |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3659 | error = -EEXIST; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3660 | if (d_is_positive(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3661 | goto fail; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3662 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3663 | /* |
| 3664 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 3665 | * From the vfs_mknod() POV we just have a negative dentry - |
| 3666 | * all is fine. Let's be bastards - you had / on the end, you've |
| 3667 | * been asking for (non-existent) directory. -ENOENT for you. |
| 3668 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3669 | if (unlikely(!is_dir && last.name[last.len])) { |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3670 | error = -ENOENT; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3671 | goto fail; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3672 | } |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3673 | if (unlikely(err2)) { |
| 3674 | error = err2; |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3675 | goto fail; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | fail: |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3679 | dput(dentry); |
| 3680 | dentry = ERR_PTR(error); |
| 3681 | unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3682 | inode_unlock(path->dentry->d_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3683 | if (!err2) |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3684 | mnt_drop_write(path->mnt); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3685 | out: |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3686 | path_put(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | return dentry; |
| 3688 | } |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3689 | |
| 3690 | struct dentry *kern_path_create(int dfd, const char *pathname, |
| 3691 | struct path *path, unsigned int lookup_flags) |
| 3692 | { |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3693 | struct filename *filename = getname_kernel(pathname); |
| 3694 | struct dentry *res = filename_create(dfd, filename, path, lookup_flags); |
| 3695 | |
| 3696 | putname(filename); |
| 3697 | return res; |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3698 | } |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3699 | EXPORT_SYMBOL(kern_path_create); |
| 3700 | |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3701 | void done_path_create(struct path *path, struct dentry *dentry) |
| 3702 | { |
| 3703 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3704 | inode_unlock(path->dentry->d_inode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3705 | mnt_drop_write(path->mnt); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3706 | path_put(path); |
| 3707 | } |
| 3708 | EXPORT_SYMBOL(done_path_create); |
| 3709 | |
Al Viro | 520ae68 | 2015-05-13 07:00:28 -0400 | [diff] [blame] | 3710 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3711 | struct path *path, unsigned int lookup_flags) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3712 | { |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3713 | struct filename *filename = getname(pathname); |
| 3714 | struct dentry *res = filename_create(dfd, filename, path, lookup_flags); |
| 3715 | |
| 3716 | putname(filename); |
| 3717 | return res; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3718 | } |
| 3719 | EXPORT_SYMBOL(user_path_create); |
| 3720 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3721 | /** |
| 3722 | * vfs_mknod - create device node or file |
| 3723 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3724 | * @dir: inode of @dentry |
| 3725 | * @dentry: pointer to dentry of the base directory |
| 3726 | * @mode: mode of the new device node or file |
| 3727 | * @dev: device number of device to create |
| 3728 | * |
| 3729 | * Create a device node or file. |
| 3730 | * |
| 3731 | * If the inode has been found through an idmapped mount the user namespace of |
| 3732 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3733 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3734 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3735 | * raw inode simply passs init_user_ns. |
| 3736 | */ |
| 3737 | int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 3738 | struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | { |
Miklos Szeredi | a3c751a | 2020-05-14 16:44:23 +0200 | [diff] [blame] | 3740 | bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3741 | int error = may_create(mnt_userns, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | |
| 3743 | if (error) |
| 3744 | return error; |
| 3745 | |
Miklos Szeredi | a3c751a | 2020-05-14 16:44:23 +0200 | [diff] [blame] | 3746 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && |
| 3747 | !capable(CAP_MKNOD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | return -EPERM; |
| 3749 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3750 | if (!dir->i_op->mknod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | return -EPERM; |
| 3752 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3753 | error = devcgroup_inode_mknod(mode, dev); |
| 3754 | if (error) |
| 3755 | return error; |
| 3756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 3758 | if (error) |
| 3759 | return error; |
| 3760 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3761 | error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3762 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3763 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | return error; |
| 3765 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3766 | EXPORT_SYMBOL(vfs_mknod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | |
Al Viro | f69aac0 | 2011-07-26 04:31:40 -0400 | [diff] [blame] | 3768 | static int may_mknod(umode_t mode) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3769 | { |
| 3770 | switch (mode & S_IFMT) { |
| 3771 | case S_IFREG: |
| 3772 | case S_IFCHR: |
| 3773 | case S_IFBLK: |
| 3774 | case S_IFIFO: |
| 3775 | case S_IFSOCK: |
| 3776 | case 0: /* zero mode translates to S_IFREG */ |
| 3777 | return 0; |
| 3778 | case S_IFDIR: |
| 3779 | return -EPERM; |
| 3780 | default: |
| 3781 | return -EINVAL; |
| 3782 | } |
| 3783 | } |
| 3784 | |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 3785 | static int do_mknodat(int dfd, struct filename *name, umode_t mode, |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3786 | unsigned int dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3788 | struct user_namespace *mnt_userns; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3789 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3790 | struct path path; |
| 3791 | int error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3792 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | |
Al Viro | 8e4bfca | 2012-07-20 01:17:26 +0400 | [diff] [blame] | 3794 | error = may_mknod(mode); |
| 3795 | if (error) |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3796 | goto out1; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3797 | retry: |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3798 | dentry = filename_create(dfd, name, &path, lookup_flags); |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3799 | error = PTR_ERR(dentry); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3800 | if (IS_ERR(dentry)) |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3801 | goto out1; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3802 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3803 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3804 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3805 | error = security_path_mknod(&path, dentry, mode, dev); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3806 | if (error) |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3807 | goto out2; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3808 | |
| 3809 | mnt_userns = mnt_user_ns(path.mnt); |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3810 | switch (mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | case 0: case S_IFREG: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3812 | error = vfs_create(mnt_userns, path.dentry->d_inode, |
| 3813 | dentry, mode, true); |
Mimi Zohar | 05d1a71 | 2016-02-29 19:52:05 -0500 | [diff] [blame] | 3814 | if (!error) |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 3815 | ima_post_path_mknod(mnt_userns, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | break; |
| 3817 | case S_IFCHR: case S_IFBLK: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3818 | error = vfs_mknod(mnt_userns, path.dentry->d_inode, |
| 3819 | dentry, mode, new_decode_dev(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | break; |
| 3821 | case S_IFIFO: case S_IFSOCK: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3822 | error = vfs_mknod(mnt_userns, path.dentry->d_inode, |
| 3823 | dentry, mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | } |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3826 | out2: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3827 | done_path_create(&path, dentry); |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3828 | if (retry_estale(error, lookup_flags)) { |
| 3829 | lookup_flags |= LOOKUP_REVAL; |
| 3830 | goto retry; |
| 3831 | } |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3832 | out1: |
| 3833 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | return error; |
| 3835 | } |
| 3836 | |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3837 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
| 3838 | unsigned int, dev) |
| 3839 | { |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3840 | return do_mknodat(dfd, getname(filename), mode, dev); |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3841 | } |
| 3842 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3843 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3844 | { |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3845 | return do_mknodat(AT_FDCWD, getname(filename), mode, dev); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3846 | } |
| 3847 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3848 | /** |
| 3849 | * vfs_mkdir - create directory |
| 3850 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3851 | * @dir: inode of @dentry |
| 3852 | * @dentry: pointer to dentry of the base directory |
| 3853 | * @mode: mode of the new directory |
| 3854 | * |
| 3855 | * Create a directory. |
| 3856 | * |
| 3857 | * If the inode has been found through an idmapped mount the user namespace of |
| 3858 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3859 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3860 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3861 | * raw inode simply passs init_user_ns. |
| 3862 | */ |
| 3863 | int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 3864 | struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3866 | int error = may_create(mnt_userns, dir, dentry); |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3867 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | |
| 3869 | if (error) |
| 3870 | return error; |
| 3871 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3872 | if (!dir->i_op->mkdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | return -EPERM; |
| 3874 | |
| 3875 | mode &= (S_IRWXUGO|S_ISVTX); |
| 3876 | error = security_inode_mkdir(dir, dentry, mode); |
| 3877 | if (error) |
| 3878 | return error; |
| 3879 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3880 | if (max_links && dir->i_nlink >= max_links) |
| 3881 | return -EMLINK; |
| 3882 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3883 | error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3884 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3885 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | return error; |
| 3887 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3888 | EXPORT_SYMBOL(vfs_mkdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 3890 | int do_mkdirat(int dfd, struct filename *name, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | { |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3892 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3893 | struct path path; |
| 3894 | int error; |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3895 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3897 | retry: |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3898 | dentry = filename_create(dfd, name, &path, lookup_flags); |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 3899 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3900 | if (IS_ERR(dentry)) |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 3901 | goto out_putname; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3902 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3903 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3904 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3905 | error = security_path_mkdir(&path, dentry, mode); |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3906 | if (!error) { |
| 3907 | struct user_namespace *mnt_userns; |
| 3908 | mnt_userns = mnt_user_ns(path.mnt); |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3909 | error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry, |
| 3910 | mode); |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3911 | } |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3912 | done_path_create(&path, dentry); |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3913 | if (retry_estale(error, lookup_flags)) { |
| 3914 | lookup_flags |= LOOKUP_REVAL; |
| 3915 | goto retry; |
| 3916 | } |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 3917 | out_putname: |
| 3918 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | return error; |
| 3920 | } |
| 3921 | |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3922 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
| 3923 | { |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 3924 | return do_mkdirat(dfd, getname(pathname), mode); |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3925 | } |
| 3926 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3927 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3928 | { |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 3929 | return do_mkdirat(AT_FDCWD, getname(pathname), mode); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3930 | } |
| 3931 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3932 | /** |
| 3933 | * vfs_rmdir - remove directory |
| 3934 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3935 | * @dir: inode of @dentry |
| 3936 | * @dentry: pointer to dentry of the base directory |
| 3937 | * |
| 3938 | * Remove a directory. |
| 3939 | * |
| 3940 | * If the inode has been found through an idmapped mount the user namespace of |
| 3941 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3942 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3943 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3944 | * raw inode simply passs init_user_ns. |
| 3945 | */ |
| 3946 | int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 3947 | struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3949 | int error = may_delete(mnt_userns, dir, dentry, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | |
| 3951 | if (error) |
| 3952 | return error; |
| 3953 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3954 | if (!dir->i_op->rmdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | return -EPERM; |
| 3956 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3957 | dget(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3958 | inode_lock(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3960 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 3961 | if (is_local_mountpoint(dentry)) |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3962 | goto out; |
| 3963 | |
| 3964 | error = security_inode_rmdir(dir, dentry); |
| 3965 | if (error) |
| 3966 | goto out; |
| 3967 | |
| 3968 | error = dir->i_op->rmdir(dir, dentry); |
| 3969 | if (error) |
| 3970 | goto out; |
| 3971 | |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 3972 | shrink_dcache_parent(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3973 | dentry->d_inode->i_flags |= S_DEAD; |
| 3974 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3975 | detach_mounts(dentry); |
Amir Goldstein | 116b973 | 2019-05-26 17:34:02 +0300 | [diff] [blame] | 3976 | fsnotify_rmdir(dir, dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3977 | |
| 3978 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3979 | inode_unlock(dentry->d_inode); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3980 | dput(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3981 | if (!error) |
| 3982 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | return error; |
| 3984 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3985 | EXPORT_SYMBOL(vfs_rmdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 3987 | int do_rmdir(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3989 | struct user_namespace *mnt_userns; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 3990 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3992 | struct path path; |
| 3993 | struct qstr last; |
| 3994 | int type; |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 3995 | unsigned int lookup_flags = 0; |
| 3996 | retry: |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 3997 | error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 3998 | if (error) |
| 3999 | goto exit1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4001 | switch (type) { |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4002 | case LAST_DOTDOT: |
| 4003 | error = -ENOTEMPTY; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4004 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4005 | case LAST_DOT: |
| 4006 | error = -EINVAL; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4007 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4008 | case LAST_ROOT: |
| 4009 | error = -EBUSY; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4010 | goto exit2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | } |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4012 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4013 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4014 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4015 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4016 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4017 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4018 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4020 | if (IS_ERR(dentry)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4021 | goto exit3; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 4022 | if (!dentry->d_inode) { |
| 4023 | error = -ENOENT; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4024 | goto exit4; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 4025 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4026 | error = security_path_rmdir(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4027 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4028 | goto exit4; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4029 | mnt_userns = mnt_user_ns(path.mnt); |
| 4030 | error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4031 | exit4: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4032 | dput(dentry); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4033 | exit3: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4034 | inode_unlock(path.dentry->d_inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4035 | mnt_drop_write(path.mnt); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4036 | exit2: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4037 | path_put(&path); |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 4038 | if (retry_estale(error, lookup_flags)) { |
| 4039 | lookup_flags |= LOOKUP_REVAL; |
| 4040 | goto retry; |
| 4041 | } |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4042 | exit1: |
Al Viro | 24fb33d | 2020-08-12 05:15:18 +0100 | [diff] [blame] | 4043 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | return error; |
| 4045 | } |
| 4046 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 4047 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4048 | { |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 4049 | return do_rmdir(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4050 | } |
| 4051 | |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4052 | /** |
| 4053 | * vfs_unlink - unlink a filesystem object |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4054 | * @mnt_userns: user namespace of the mount the inode was found from |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4055 | * @dir: parent directory |
| 4056 | * @dentry: victim |
| 4057 | * @delegated_inode: returns victim inode, if the inode is delegated. |
| 4058 | * |
| 4059 | * The caller must hold dir->i_mutex. |
| 4060 | * |
| 4061 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and |
| 4062 | * return a reference to the inode in delegated_inode. The caller |
| 4063 | * should then break the delegation on that inode and retry. Because |
| 4064 | * breaking a delegation may take a long time, the caller should drop |
| 4065 | * dir->i_mutex before doing so. |
| 4066 | * |
| 4067 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4068 | * be appropriate for callers that expect the underlying filesystem not |
| 4069 | * to be NFS exported. |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4070 | * |
| 4071 | * If the inode has been found through an idmapped mount the user namespace of |
| 4072 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 4073 | * care to map the inode according to @mnt_userns before checking permissions. |
| 4074 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 4075 | * raw inode simply passs init_user_ns. |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4076 | */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4077 | int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 4078 | struct dentry *dentry, struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 4080 | struct inode *target = dentry->d_inode; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4081 | int error = may_delete(mnt_userns, dir, dentry, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | |
| 4083 | if (error) |
| 4084 | return error; |
| 4085 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4086 | if (!dir->i_op->unlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4087 | return -EPERM; |
| 4088 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4089 | inode_lock(target); |
Hugh Dickins | 51cc3a6 | 2021-09-02 14:53:57 -0700 | [diff] [blame] | 4090 | if (IS_SWAPFILE(target)) |
| 4091 | error = -EPERM; |
| 4092 | else if (is_local_mountpoint(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | error = -EBUSY; |
| 4094 | else { |
| 4095 | error = security_inode_unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 4096 | if (!error) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 4097 | error = try_break_deleg(target, delegated_inode); |
| 4098 | if (error) |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4099 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | error = dir->i_op->unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4101 | if (!error) { |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 4102 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4103 | detach_mounts(dentry); |
Amir Goldstein | 116b973 | 2019-05-26 17:34:02 +0300 | [diff] [blame] | 4104 | fsnotify_unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4105 | } |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 4106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | } |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4108 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4109 | inode_unlock(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | |
| 4111 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
| 4112 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 4113 | fsnotify_link_count(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | return error; |
| 4118 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4119 | EXPORT_SYMBOL(vfs_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | |
| 4121 | /* |
| 4122 | * Make sure that the actual truncation of the file will occur outside its |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4123 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | * writeout happening, and we don't want to prevent access to the directory |
| 4125 | * while waiting on the I/O. |
| 4126 | */ |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 4127 | int do_unlinkat(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4129 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4131 | struct path path; |
| 4132 | struct qstr last; |
| 4133 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | struct inode *inode = NULL; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4135 | struct inode *delegated_inode = NULL; |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 4136 | unsigned int lookup_flags = 0; |
| 4137 | retry: |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 4138 | error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4139 | if (error) |
| 4140 | goto exit1; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | error = -EISDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4143 | if (type != LAST_NORM) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4144 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4145 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4146 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4147 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4148 | goto exit2; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4149 | retry_deleg: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4150 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4151 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | error = PTR_ERR(dentry); |
| 4153 | if (!IS_ERR(dentry)) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4154 | struct user_namespace *mnt_userns; |
| 4155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | /* Why not before? Because we want correct error value */ |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4157 | if (last.name[last.len]) |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 4158 | goto slashes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | inode = dentry->d_inode; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4160 | if (d_is_negative(dentry)) |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 4161 | goto slashes; |
| 4162 | ihold(inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4163 | error = security_path_unlink(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4164 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4165 | goto exit3; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4166 | mnt_userns = mnt_user_ns(path.mnt); |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4167 | error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry, |
| 4168 | &delegated_inode); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4169 | exit3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | dput(dentry); |
| 4171 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4172 | inode_unlock(path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | if (inode) |
| 4174 | iput(inode); /* truncate the inode here */ |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4175 | inode = NULL; |
| 4176 | if (delegated_inode) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 4177 | error = break_deleg_wait(&delegated_inode); |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4178 | if (!error) |
| 4179 | goto retry_deleg; |
| 4180 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4181 | mnt_drop_write(path.mnt); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4182 | exit2: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4183 | path_put(&path); |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 4184 | if (retry_estale(error, lookup_flags)) { |
| 4185 | lookup_flags |= LOOKUP_REVAL; |
| 4186 | inode = NULL; |
| 4187 | goto retry; |
| 4188 | } |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4189 | exit1: |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4190 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | return error; |
| 4192 | |
| 4193 | slashes: |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4194 | if (d_is_negative(dentry)) |
| 4195 | error = -ENOENT; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 4196 | else if (d_is_dir(dentry)) |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4197 | error = -EISDIR; |
| 4198 | else |
| 4199 | error = -ENOTDIR; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4200 | goto exit3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | } |
| 4202 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 4203 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4204 | { |
| 4205 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 4206 | return -EINVAL; |
| 4207 | |
| 4208 | if (flag & AT_REMOVEDIR) |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 4209 | return do_rmdir(dfd, getname(pathname)); |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4210 | return do_unlinkat(dfd, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4211 | } |
| 4212 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4213 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4214 | { |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4215 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4216 | } |
| 4217 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4218 | /** |
| 4219 | * vfs_symlink - create symlink |
| 4220 | * @mnt_userns: user namespace of the mount the inode was found from |
| 4221 | * @dir: inode of @dentry |
| 4222 | * @dentry: pointer to dentry of the base directory |
| 4223 | * @oldname: name of the file to link to |
| 4224 | * |
| 4225 | * Create a symlink. |
| 4226 | * |
| 4227 | * If the inode has been found through an idmapped mount the user namespace of |
| 4228 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 4229 | * care to map the inode according to @mnt_userns before checking permissions. |
| 4230 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 4231 | * raw inode simply passs init_user_ns. |
| 4232 | */ |
| 4233 | int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 4234 | struct dentry *dentry, const char *oldname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4236 | int error = may_create(mnt_userns, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | |
| 4238 | if (error) |
| 4239 | return error; |
| 4240 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4241 | if (!dir->i_op->symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | return -EPERM; |
| 4243 | |
| 4244 | error = security_inode_symlink(dir, dentry, oldname); |
| 4245 | if (error) |
| 4246 | return error; |
| 4247 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4248 | error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 4249 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 4250 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | return error; |
| 4252 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4253 | EXPORT_SYMBOL(vfs_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 4255 | int do_symlinkat(struct filename *from, int newdfd, struct filename *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4257 | int error; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4258 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4259 | struct path path; |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4260 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4262 | if (IS_ERR(from)) { |
| 4263 | error = PTR_ERR(from); |
| 4264 | goto out_putnames; |
| 4265 | } |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4266 | retry: |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 4267 | dentry = filename_create(newdfd, to, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4268 | error = PTR_ERR(dentry); |
| 4269 | if (IS_ERR(dentry)) |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4270 | goto out_putnames; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4271 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4272 | error = security_path_symlink(&path, dentry, from->name); |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4273 | if (!error) { |
| 4274 | struct user_namespace *mnt_userns; |
| 4275 | |
| 4276 | mnt_userns = mnt_user_ns(path.mnt); |
| 4277 | error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry, |
| 4278 | from->name); |
| 4279 | } |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4280 | done_path_create(&path, dentry); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4281 | if (retry_estale(error, lookup_flags)) { |
| 4282 | lookup_flags |= LOOKUP_REVAL; |
| 4283 | goto retry; |
| 4284 | } |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4285 | out_putnames: |
| 4286 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | putname(from); |
| 4288 | return error; |
| 4289 | } |
| 4290 | |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4291 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
| 4292 | int, newdfd, const char __user *, newname) |
| 4293 | { |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4294 | return do_symlinkat(getname(oldname), newdfd, getname(newname)); |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4295 | } |
| 4296 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4297 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4298 | { |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4299 | return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4300 | } |
| 4301 | |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4302 | /** |
| 4303 | * vfs_link - create a new link |
| 4304 | * @old_dentry: object to be linked |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4305 | * @mnt_userns: the user namespace of the mount |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4306 | * @dir: new parent |
| 4307 | * @new_dentry: where to create the new link |
| 4308 | * @delegated_inode: returns inode needing a delegation break |
| 4309 | * |
| 4310 | * The caller must hold dir->i_mutex |
| 4311 | * |
| 4312 | * If vfs_link discovers a delegation on the to-be-linked file in need |
| 4313 | * of breaking, it will return -EWOULDBLOCK and return a reference to the |
| 4314 | * inode in delegated_inode. The caller should then break the delegation |
| 4315 | * and retry. Because breaking a delegation may take a long time, the |
| 4316 | * caller should drop the i_mutex before doing so. |
| 4317 | * |
| 4318 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4319 | * be appropriate for callers that expect the underlying filesystem not |
| 4320 | * to be NFS exported. |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4321 | * |
| 4322 | * If the inode has been found through an idmapped mount the user namespace of |
| 4323 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 4324 | * care to map the inode according to @mnt_userns before checking permissions. |
| 4325 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 4326 | * raw inode simply passs init_user_ns. |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4327 | */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4328 | int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns, |
| 4329 | struct inode *dir, struct dentry *new_dentry, |
| 4330 | struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | { |
| 4332 | struct inode *inode = old_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4333 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | int error; |
| 4335 | |
| 4336 | if (!inode) |
| 4337 | return -ENOENT; |
| 4338 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4339 | error = may_create(mnt_userns, dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | if (error) |
| 4341 | return error; |
| 4342 | |
| 4343 | if (dir->i_sb != inode->i_sb) |
| 4344 | return -EXDEV; |
| 4345 | |
| 4346 | /* |
| 4347 | * A link to an append-only or immutable file cannot be created. |
| 4348 | */ |
| 4349 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4350 | return -EPERM; |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 4351 | /* |
| 4352 | * Updating the link count will likely cause i_uid and i_gid to |
| 4353 | * be writen back improperly if their true value is unknown to |
| 4354 | * the vfs. |
| 4355 | */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4356 | if (HAS_UNMAPPED_ID(mnt_userns, inode)) |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 4357 | return -EPERM; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4358 | if (!dir->i_op->link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | return -EPERM; |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4360 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | return -EPERM; |
| 4362 | |
| 4363 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 4364 | if (error) |
| 4365 | return error; |
| 4366 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4367 | inode_lock(inode); |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4368 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4369 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4370 | error = -ENOENT; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4371 | else if (max_links && inode->i_nlink >= max_links) |
| 4372 | error = -EMLINK; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4373 | else { |
| 4374 | error = try_break_deleg(inode, delegated_inode); |
| 4375 | if (!error) |
| 4376 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
| 4377 | } |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4378 | |
| 4379 | if (!error && (inode->i_state & I_LINKABLE)) { |
| 4380 | spin_lock(&inode->i_lock); |
| 4381 | inode->i_state &= ~I_LINKABLE; |
| 4382 | spin_unlock(&inode->i_lock); |
| 4383 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4384 | inode_unlock(inode); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 4385 | if (!error) |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4386 | fsnotify_link(dir, inode, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | return error; |
| 4388 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4389 | EXPORT_SYMBOL(vfs_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | |
| 4391 | /* |
| 4392 | * Hardlinks are often used in delicate situations. We avoid |
| 4393 | * security-related surprises by not following symlinks on the |
| 4394 | * newname. --KAB |
| 4395 | * |
| 4396 | * We don't follow them on the oldname either to be compatible |
| 4397 | * with linux 2.0, and to avoid hard-linking to directories |
| 4398 | * and other special files. --ADM |
| 4399 | */ |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 4400 | int do_linkat(int olddfd, struct filename *old, int newdfd, |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4401 | struct filename *new, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4403 | struct user_namespace *mnt_userns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | struct dentry *new_dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4405 | struct path old_path, new_path; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4406 | struct inode *delegated_inode = NULL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4407 | int how = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4410 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) { |
| 4411 | error = -EINVAL; |
| 4412 | goto out_putnames; |
| 4413 | } |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4414 | /* |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4415 | * To use null names we require CAP_DAC_READ_SEARCH |
| 4416 | * This ensures that not everyone will be able to create |
| 4417 | * handlink using the passed filedescriptor. |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4418 | */ |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4419 | if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) { |
| 4420 | error = -ENOENT; |
| 4421 | goto out_putnames; |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4422 | } |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4423 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4424 | if (flags & AT_SYMLINK_FOLLOW) |
| 4425 | how |= LOOKUP_FOLLOW; |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4426 | retry: |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 4427 | error = filename_lookup(olddfd, old, how, &old_path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | if (error) |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4429 | goto out_putnames; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4430 | |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 4431 | new_dentry = filename_create(newdfd, new, &new_path, |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4432 | (how & LOOKUP_REVAL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4434 | if (IS_ERR(new_dentry)) |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4435 | goto out_putpath; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4436 | |
| 4437 | error = -EXDEV; |
| 4438 | if (old_path.mnt != new_path.mnt) |
| 4439 | goto out_dput; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4440 | mnt_userns = mnt_user_ns(new_path.mnt); |
| 4441 | error = may_linkat(mnt_userns, &old_path); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 4442 | if (unlikely(error)) |
| 4443 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4444 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4445 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 4446 | goto out_dput; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4447 | error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode, |
| 4448 | new_dentry, &delegated_inode); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 4449 | out_dput: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4450 | done_path_create(&new_path, new_dentry); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4451 | if (delegated_inode) { |
| 4452 | error = break_deleg_wait(&delegated_inode); |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4453 | if (!error) { |
| 4454 | path_put(&old_path); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4455 | goto retry; |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4456 | } |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4457 | } |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4458 | if (retry_estale(error, how)) { |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4459 | path_put(&old_path); |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4460 | how |= LOOKUP_REVAL; |
| 4461 | goto retry; |
| 4462 | } |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4463 | out_putpath: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 4464 | path_put(&old_path); |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4465 | out_putnames: |
| 4466 | putname(old); |
| 4467 | putname(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | |
| 4469 | return error; |
| 4470 | } |
| 4471 | |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4472 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
| 4473 | int, newdfd, const char __user *, newname, int, flags) |
| 4474 | { |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4475 | return do_linkat(olddfd, getname_uflags(oldname, flags), |
| 4476 | newdfd, getname(newname), flags); |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4477 | } |
| 4478 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4479 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4480 | { |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4481 | return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4482 | } |
| 4483 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4484 | /** |
| 4485 | * vfs_rename - rename a filesystem object |
Randy Dunlap | 2111c3c | 2021-02-15 20:29:28 -0800 | [diff] [blame] | 4486 | * @rd: pointer to &struct renamedata info |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4487 | * |
| 4488 | * The caller must hold multiple mutexes--see lock_rename()). |
| 4489 | * |
| 4490 | * If vfs_rename discovers a delegation in need of breaking at either |
| 4491 | * the source or destination, it will return -EWOULDBLOCK and return a |
| 4492 | * reference to the inode in delegated_inode. The caller should then |
| 4493 | * break the delegation and retry. Because breaking a delegation may |
| 4494 | * take a long time, the caller should drop all locks before doing |
| 4495 | * so. |
| 4496 | * |
| 4497 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4498 | * be appropriate for callers that expect the underlying filesystem not |
| 4499 | * to be NFS exported. |
| 4500 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 4502 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 4503 | * Problems: |
Mauro Carvalho Chehab | 0117d42 | 2017-05-12 07:45:42 -0300 | [diff] [blame] | 4504 | * |
J. Bruce Fields | d03b29a | 2014-02-17 16:52:33 -0500 | [diff] [blame] | 4505 | * a) we can get into loop creation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | * b) race potential - two innocent renames can create a loop together. |
| 4507 | * That's where 4.4 screws up. Current fix: serialization on |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4508 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | * story. |
J. Bruce Fields | 6cedba8 | 2012-03-05 11:40:41 -0500 | [diff] [blame] | 4510 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
| 4511 | * and source (if it is not a directory). |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4512 | * And that - after we got ->i_mutex on parents (until then we don't know |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | * whether the target exists). Solution: try to be smart with locking |
| 4514 | * order for inodes. We rely on the fact that tree topology may change |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4515 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | * move will be locked. Thus we can rank directories by the tree |
| 4517 | * (ancestors first) and rank all non-directories after them. |
| 4518 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4519 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 4521 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 4522 | * we'd better make sure that there's no link(2) for them. |
Sage Weil | e4eaac0 | 2011-05-24 13:06:07 -0700 | [diff] [blame] | 4523 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4524 | * we are removing the target. Solution: we will have to grab ->i_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 4526 | * ->i_mutex on parents, which works but leads to some truly excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | * locking]. |
| 4528 | */ |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4529 | int vfs_rename(struct renamedata *rd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | { |
| 4531 | int error; |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4532 | struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; |
| 4533 | struct dentry *old_dentry = rd->old_dentry; |
| 4534 | struct dentry *new_dentry = rd->new_dentry; |
| 4535 | struct inode **delegated_inode = rd->delegated_inode; |
| 4536 | unsigned int flags = rd->flags; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4537 | bool is_dir = d_is_dir(old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4538 | struct inode *source = old_dentry->d_inode; |
| 4539 | struct inode *target = new_dentry->d_inode; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4540 | bool new_is_dir = false; |
| 4541 | unsigned max_links = new_dir->i_sb->s_max_links; |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4542 | struct name_snapshot old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | |
Miklos Szeredi | 8d3e293 | 2016-12-16 11:02:54 +0100 | [diff] [blame] | 4544 | if (source == target) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4545 | return 0; |
| 4546 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4547 | error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | if (error) |
| 4549 | return error; |
| 4550 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4551 | if (!target) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4552 | error = may_create(rd->new_mnt_userns, new_dir, new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4553 | } else { |
| 4554 | new_is_dir = d_is_dir(new_dentry); |
| 4555 | |
| 4556 | if (!(flags & RENAME_EXCHANGE)) |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4557 | error = may_delete(rd->new_mnt_userns, new_dir, |
| 4558 | new_dentry, is_dir); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4559 | else |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4560 | error = may_delete(rd->new_mnt_userns, new_dir, |
| 4561 | new_dentry, new_is_dir); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | if (error) |
| 4564 | return error; |
| 4565 | |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4566 | if (!old_dir->i_op->rename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | return -EPERM; |
| 4568 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4569 | /* |
| 4570 | * If we are going to change the parent - check write permissions, |
| 4571 | * we'll need to flip '..'. |
| 4572 | */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4573 | if (new_dir != old_dir) { |
| 4574 | if (is_dir) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4575 | error = inode_permission(rd->old_mnt_userns, source, |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 4576 | MAY_WRITE); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4577 | if (error) |
| 4578 | return error; |
| 4579 | } |
| 4580 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4581 | error = inode_permission(rd->new_mnt_userns, target, |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 4582 | MAY_WRITE); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4583 | if (error) |
| 4584 | return error; |
| 4585 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4586 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4587 | |
Miklos Szeredi | 0b3974e | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4588 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 4589 | flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4590 | if (error) |
| 4591 | return error; |
| 4592 | |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4593 | take_dentry_name_snapshot(&old_name, old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4594 | dget(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4595 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4596 | lock_two_nondirectories(source, target); |
| 4597 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4598 | inode_lock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4599 | |
Hugh Dickins | 51cc3a6 | 2021-09-02 14:53:57 -0700 | [diff] [blame] | 4600 | error = -EPERM; |
| 4601 | if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target))) |
| 4602 | goto out; |
| 4603 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4604 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 4605 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4606 | goto out; |
| 4607 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4608 | if (max_links && new_dir != old_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4609 | error = -EMLINK; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4610 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4611 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4612 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
| 4613 | old_dir->i_nlink >= max_links) |
| 4614 | goto out; |
| 4615 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4616 | if (!is_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4617 | error = try_break_deleg(source, delegated_inode); |
| 4618 | if (error) |
| 4619 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4620 | } |
| 4621 | if (target && !new_is_dir) { |
| 4622 | error = try_break_deleg(target, delegated_inode); |
| 4623 | if (error) |
| 4624 | goto out; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4625 | } |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4626 | error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry, |
| 4627 | new_dir, new_dentry, flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4628 | if (error) |
| 4629 | goto out; |
| 4630 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4631 | if (!(flags & RENAME_EXCHANGE) && target) { |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4632 | if (is_dir) { |
| 4633 | shrink_dcache_parent(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4634 | target->i_flags |= S_DEAD; |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4635 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4636 | dont_mount(new_dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4637 | detach_mounts(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4638 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4639 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
| 4640 | if (!(flags & RENAME_EXCHANGE)) |
| 4641 | d_move(old_dentry, new_dentry); |
| 4642 | else |
| 4643 | d_exchange(old_dentry, new_dentry); |
| 4644 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4645 | out: |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4646 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4647 | unlock_two_nondirectories(source, target); |
| 4648 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4649 | inode_unlock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4650 | dput(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4651 | if (!error) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4652 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4653 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
| 4654 | if (flags & RENAME_EXCHANGE) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4655 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4656 | new_is_dir, NULL, new_dentry); |
| 4657 | } |
| 4658 | } |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4659 | release_dentry_name_snapshot(&old_name); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | return error; |
| 4662 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4663 | EXPORT_SYMBOL(vfs_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4665 | int do_renameat2(int olddfd, struct filename *from, int newdfd, |
| 4666 | struct filename *to, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | { |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4668 | struct renamedata rd; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4669 | struct dentry *old_dentry, *new_dentry; |
| 4670 | struct dentry *trap; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4671 | struct path old_path, new_path; |
| 4672 | struct qstr old_last, new_last; |
| 4673 | int old_type, new_type; |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4674 | struct inode *delegated_inode = NULL; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4675 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4676 | bool should_retry = false; |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4677 | int error = -EINVAL; |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4678 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4679 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4680 | goto put_names; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4681 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4682 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
| 4683 | (flags & RENAME_EXCHANGE)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4684 | goto put_names; |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4685 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4686 | if (flags & RENAME_EXCHANGE) |
| 4687 | target_flags = 0; |
| 4688 | |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4689 | retry: |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 4690 | error = filename_parentat(olddfd, from, lookup_flags, &old_path, |
| 4691 | &old_last, &old_type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4692 | if (error) |
| 4693 | goto put_names; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 4695 | error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, |
| 4696 | &new_type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4697 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | goto exit1; |
| 4699 | |
| 4700 | error = -EXDEV; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4701 | if (old_path.mnt != new_path.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | goto exit2; |
| 4703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | error = -EBUSY; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4705 | if (old_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | goto exit2; |
| 4707 | |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4708 | if (flags & RENAME_NOREPLACE) |
| 4709 | error = -EEXIST; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4710 | if (new_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | goto exit2; |
| 4712 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4713 | error = mnt_want_write(old_path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4714 | if (error) |
| 4715 | goto exit2; |
| 4716 | |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4717 | retry_deleg: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4718 | trap = lock_rename(new_path.dentry, old_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4720 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | error = PTR_ERR(old_dentry); |
| 4722 | if (IS_ERR(old_dentry)) |
| 4723 | goto exit3; |
| 4724 | /* source must exist */ |
| 4725 | error = -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4726 | if (d_is_negative(old_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | goto exit4; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4728 | new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | error = PTR_ERR(new_dentry); |
| 4730 | if (IS_ERR(new_dentry)) |
| 4731 | goto exit4; |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4732 | error = -EEXIST; |
| 4733 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) |
| 4734 | goto exit5; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4735 | if (flags & RENAME_EXCHANGE) { |
| 4736 | error = -ENOENT; |
| 4737 | if (d_is_negative(new_dentry)) |
| 4738 | goto exit5; |
| 4739 | |
| 4740 | if (!d_is_dir(new_dentry)) { |
| 4741 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4742 | if (new_last.name[new_last.len]) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4743 | goto exit5; |
| 4744 | } |
| 4745 | } |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4746 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 4747 | if (!d_is_dir(old_dentry)) { |
| 4748 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4749 | if (old_last.name[old_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4750 | goto exit5; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4751 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4752 | goto exit5; |
| 4753 | } |
| 4754 | /* source should not be ancestor of target */ |
| 4755 | error = -EINVAL; |
| 4756 | if (old_dentry == trap) |
| 4757 | goto exit5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | /* target should not be an ancestor of source */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4759 | if (!(flags & RENAME_EXCHANGE)) |
| 4760 | error = -ENOTEMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | if (new_dentry == trap) |
| 4762 | goto exit5; |
| 4763 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4764 | error = security_path_rename(&old_path, old_dentry, |
| 4765 | &new_path, new_dentry, flags); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4766 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4767 | goto exit5; |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4768 | |
| 4769 | rd.old_dir = old_path.dentry->d_inode; |
| 4770 | rd.old_dentry = old_dentry; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4771 | rd.old_mnt_userns = mnt_user_ns(old_path.mnt); |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4772 | rd.new_dir = new_path.dentry->d_inode; |
| 4773 | rd.new_dentry = new_dentry; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4774 | rd.new_mnt_userns = mnt_user_ns(new_path.mnt); |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4775 | rd.delegated_inode = &delegated_inode; |
| 4776 | rd.flags = flags; |
| 4777 | error = vfs_rename(&rd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | exit5: |
| 4779 | dput(new_dentry); |
| 4780 | exit4: |
| 4781 | dput(old_dentry); |
| 4782 | exit3: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4783 | unlock_rename(new_path.dentry, old_path.dentry); |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4784 | if (delegated_inode) { |
| 4785 | error = break_deleg_wait(&delegated_inode); |
| 4786 | if (!error) |
| 4787 | goto retry_deleg; |
| 4788 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4789 | mnt_drop_write(old_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | exit2: |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4791 | if (retry_estale(error, lookup_flags)) |
| 4792 | should_retry = true; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4793 | path_put(&new_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4795 | path_put(&old_path); |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4796 | if (should_retry) { |
| 4797 | should_retry = false; |
| 4798 | lookup_flags |= LOOKUP_REVAL; |
| 4799 | goto retry; |
| 4800 | } |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4801 | put_names: |
Dmitry Kadashev | 91ef658 | 2021-07-08 13:34:37 +0700 | [diff] [blame] | 4802 | putname(from); |
Dmitry Kadashev | 91ef658 | 2021-07-08 13:34:37 +0700 | [diff] [blame] | 4803 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | return error; |
| 4805 | } |
| 4806 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4807 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
| 4808 | int, newdfd, const char __user *, newname, unsigned int, flags) |
| 4809 | { |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4810 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
| 4811 | flags); |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4812 | } |
| 4813 | |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4814 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
| 4815 | int, newdfd, const char __user *, newname) |
| 4816 | { |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4817 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
| 4818 | 0); |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4819 | } |
| 4820 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 4821 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4822 | { |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4823 | return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, |
| 4824 | getname(newname), 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4825 | } |
| 4826 | |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4827 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | { |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4829 | int len = PTR_ERR(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | if (IS_ERR(link)) |
| 4831 | goto out; |
| 4832 | |
| 4833 | len = strlen(link); |
| 4834 | if (len > (unsigned) buflen) |
| 4835 | len = buflen; |
| 4836 | if (copy_to_user(buffer, link, len)) |
| 4837 | len = -EFAULT; |
| 4838 | out: |
| 4839 | return len; |
| 4840 | } |
| 4841 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4842 | /** |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4843 | * vfs_readlink - copy symlink body into userspace buffer |
| 4844 | * @dentry: dentry on which to get symbolic link |
| 4845 | * @buffer: user memory pointer |
| 4846 | * @buflen: size of buffer |
| 4847 | * |
| 4848 | * Does not touch atime. That's up to the caller if necessary |
| 4849 | * |
| 4850 | * Does not call security hook. |
| 4851 | */ |
| 4852 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4853 | { |
| 4854 | struct inode *inode = d_inode(dentry); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4855 | DEFINE_DELAYED_CALL(done); |
| 4856 | const char *link; |
| 4857 | int res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4858 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4859 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
| 4860 | if (unlikely(inode->i_op->readlink)) |
| 4861 | return inode->i_op->readlink(dentry, buffer, buflen); |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4862 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4863 | if (!d_is_symlink(dentry)) |
| 4864 | return -EINVAL; |
| 4865 | |
| 4866 | spin_lock(&inode->i_lock); |
| 4867 | inode->i_opflags |= IOP_DEFAULT_READLINK; |
| 4868 | spin_unlock(&inode->i_lock); |
| 4869 | } |
| 4870 | |
Eric Biggers | 4c4f7c1 | 2019-04-10 13:21:14 -0700 | [diff] [blame] | 4871 | link = READ_ONCE(inode->i_link); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4872 | if (!link) { |
| 4873 | link = inode->i_op->get_link(dentry, inode, &done); |
| 4874 | if (IS_ERR(link)) |
| 4875 | return PTR_ERR(link); |
| 4876 | } |
| 4877 | res = readlink_copy(buffer, buflen, link); |
| 4878 | do_delayed_call(&done); |
| 4879 | return res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4880 | } |
| 4881 | EXPORT_SYMBOL(vfs_readlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4883 | /** |
| 4884 | * vfs_get_link - get symlink body |
| 4885 | * @dentry: dentry on which to get symbolic link |
| 4886 | * @done: caller needs to free returned data with this |
| 4887 | * |
| 4888 | * Calls security hook and i_op->get_link() on the supplied inode. |
| 4889 | * |
| 4890 | * It does not touch atime. That's up to the caller if necessary. |
| 4891 | * |
| 4892 | * Does not work on "special" symlinks like /proc/$$/fd/N |
| 4893 | */ |
| 4894 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) |
| 4895 | { |
| 4896 | const char *res = ERR_PTR(-EINVAL); |
| 4897 | struct inode *inode = d_inode(dentry); |
| 4898 | |
| 4899 | if (d_is_symlink(dentry)) { |
| 4900 | res = ERR_PTR(security_inode_readlink(dentry)); |
| 4901 | if (!res) |
| 4902 | res = inode->i_op->get_link(dentry, inode, done); |
| 4903 | } |
| 4904 | return res; |
| 4905 | } |
| 4906 | EXPORT_SYMBOL(vfs_get_link); |
| 4907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | /* get the link contents into pagecache */ |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4909 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4910 | struct delayed_call *callback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | { |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4912 | char *kaddr; |
| 4913 | struct page *page; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4914 | struct address_space *mapping = inode->i_mapping; |
| 4915 | |
Al Viro | d3883d4 | 2015-11-17 10:41:04 -0500 | [diff] [blame] | 4916 | if (!dentry) { |
| 4917 | page = find_get_page(mapping, 0); |
| 4918 | if (!page) |
| 4919 | return ERR_PTR(-ECHILD); |
| 4920 | if (!PageUptodate(page)) { |
| 4921 | put_page(page); |
| 4922 | return ERR_PTR(-ECHILD); |
| 4923 | } |
| 4924 | } else { |
| 4925 | page = read_mapping_page(mapping, 0, NULL); |
| 4926 | if (IS_ERR(page)) |
| 4927 | return (char*)page; |
| 4928 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4929 | set_delayed_call(callback, page_put_link, page); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4930 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
| 4931 | kaddr = page_address(page); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4932 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4933 | return kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | } |
| 4935 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4936 | EXPORT_SYMBOL(page_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4938 | void page_put_link(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4940 | put_page(arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4942 | EXPORT_SYMBOL(page_put_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4944 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4945 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4946 | DEFINE_DELAYED_CALL(done); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4947 | int res = readlink_copy(buffer, buflen, |
| 4948 | page_get_link(dentry, d_inode(dentry), |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4949 | &done)); |
| 4950 | do_delayed_call(&done); |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4951 | return res; |
| 4952 | } |
| 4953 | EXPORT_SYMBOL(page_readlink); |
| 4954 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4955 | /* |
| 4956 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS |
| 4957 | */ |
| 4958 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | { |
| 4960 | struct address_space *mapping = inode->i_mapping; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4961 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4962 | void *fsdata; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 4963 | int err; |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 4964 | unsigned int flags = 0; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4965 | if (nofs) |
| 4966 | flags |= AOP_FLAG_NOFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 4968 | retry: |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4969 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4970 | flags, &page, &fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | if (err) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4972 | goto fail; |
| 4973 | |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4974 | memcpy(page_address(page), symname, len-1); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4975 | |
| 4976 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, |
| 4977 | page, fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | if (err < 0) |
| 4979 | goto fail; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4980 | if (err < len-1) |
| 4981 | goto retry; |
| 4982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | mark_inode_dirty(inode); |
| 4984 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | fail: |
| 4986 | return err; |
| 4987 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4988 | EXPORT_SYMBOL(__page_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4990 | int page_symlink(struct inode *inode, const char *symname, int len) |
| 4991 | { |
| 4992 | return __page_symlink(inode, symname, len, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 4993 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4994 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4995 | EXPORT_SYMBOL(page_symlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4996 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 4997 | const struct inode_operations page_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4998 | .get_link = page_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | EXPORT_SYMBOL(page_symlink_inode_operations); |