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> |
Matthew Wilcox (Oracle) | 2d87817 | 2022-02-22 09:43:12 -0500 | [diff] [blame] | 25 | #include <linux/sched/mm.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 26 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/personality.h> |
| 28 | #include <linux/security.h> |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 29 | #include <linux/ima.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/syscalls.h> |
| 31 | #include <linux/mount.h> |
| 32 | #include <linux/audit.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 33 | #include <linux/capability.h> |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 34 | #include <linux/file.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 35 | #include <linux/fcntl.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 36 | #include <linux/device_cgroup.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 37 | #include <linux/fs_struct.h> |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 38 | #include <linux/posix_acl.h> |
Linus Torvalds | 99d263d | 2014-09-13 11:30:10 -0700 | [diff] [blame] | 39 | #include <linux/hash.h> |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 40 | #include <linux/bitops.h> |
Eric W. Biederman | aeaa4a7 | 2016-07-23 11:20:44 -0500 | [diff] [blame] | 41 | #include <linux/init_task.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 42 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 44 | #include "internal.h" |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 45 | #include "mount.h" |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* [Feb-1997 T. Schoebel-Theuer] |
| 48 | * Fundamental changes in the pathname lookup mechanisms (namei) |
| 49 | * were necessary because of omirr. The reason is that omirr needs |
| 50 | * to know the _real_ pathname, not the user-supplied one, in case |
| 51 | * of symlinks (and also when transname replacements occur). |
| 52 | * |
| 53 | * The new code replaces the old recursive symlink resolution with |
| 54 | * an iterative one (in case of non-nested symlink chains). It does |
| 55 | * this with calls to <fs>_follow_link(). |
| 56 | * As a side effect, dir_namei(), _namei() and follow_link() are now |
| 57 | * replaced with a single function lookup_dentry() that can handle all |
| 58 | * the special cases of the former code. |
| 59 | * |
| 60 | * With the new dcache, the pathname is stored at each inode, at least as |
| 61 | * long as the refcount of the inode is positive. As a side effect, the |
| 62 | * size of the dcache depends on the inode cache and thus is dynamic. |
| 63 | * |
| 64 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink |
| 65 | * resolution to correspond with current state of the code. |
| 66 | * |
| 67 | * Note that the symlink resolution is not *completely* iterative. |
| 68 | * There is still a significant amount of tail- and mid- recursion in |
| 69 | * the algorithm. Also, note that <fs>_readlink() is not used in |
| 70 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() |
| 71 | * may return different results than <fs>_follow_link(). Many virtual |
| 72 | * filesystems (including /proc) exhibit this behavior. |
| 73 | */ |
| 74 | |
| 75 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: |
| 76 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL |
| 77 | * and the name already exists in form of a symlink, try to create the new |
| 78 | * name indicated by the symlink. The old code always complained that the |
| 79 | * name already exists, due to not following the symlink even if its target |
| 80 | * is nonexistent. The new semantics affects also mknod() and link() when |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 81 | * the name is a symlink pointing to a non-existent name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * |
| 83 | * I don't know which semantics is the right one, since I have no access |
| 84 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" |
| 85 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the |
| 86 | * "old" one. Personally, I think the new semantics is much more logical. |
| 87 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing |
| 88 | * file does succeed in both HP-UX and SunOs, but not in Solaris |
| 89 | * and in the old Linux semantics. |
| 90 | */ |
| 91 | |
| 92 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink |
| 93 | * semantics. See the comments in "open_namei" and "do_link" below. |
| 94 | * |
| 95 | * [10-Sep-98 Alan Modra] Another symlink change. |
| 96 | */ |
| 97 | |
| 98 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: |
| 99 | * inside the path - always follow. |
| 100 | * in the last component in creation/removal/renaming - never follow. |
| 101 | * if LOOKUP_FOLLOW passed - follow. |
| 102 | * if the pathname has trailing slashes - follow. |
| 103 | * otherwise - don't follow. |
| 104 | * (applied in that order). |
| 105 | * |
| 106 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT |
| 107 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. |
| 108 | * During the 2.4 we need to fix the userland stuff depending on it - |
| 109 | * hopefully we will be able to get rid of that wart in 2.5. So far only |
| 110 | * XEmacs seems to be relying on it... |
| 111 | */ |
| 112 | /* |
| 113 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 114 | * 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] | 115 | * any extra contention... |
| 116 | */ |
| 117 | |
| 118 | /* In order to reduce some races, while at the same time doing additional |
| 119 | * checking and hopefully speeding things up, we copy filenames to the |
| 120 | * kernel data space before using them.. |
| 121 | * |
| 122 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). |
| 123 | * PATH_MAX includes the nul terminator --RR. |
| 124 | */ |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 125 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 126 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 127 | |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 128 | struct filename * |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 129 | getname_flags(const char __user *filename, int flags, int *empty) |
| 130 | { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 131 | struct filename *result; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 132 | char *kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 133 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Jeff Layton | 7ac8626 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 135 | result = audit_reusename(filename); |
| 136 | if (result) |
| 137 | return result; |
| 138 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 139 | result = __getname(); |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 140 | if (unlikely(!result)) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 141 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 143 | /* |
| 144 | * First, try to embed the struct filename inside the names_cache |
| 145 | * allocation |
| 146 | */ |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 147 | kname = (char *)result->iname; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 148 | result->name = kname; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 149 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 150 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 151 | if (unlikely(len < 0)) { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 152 | __putname(result); |
| 153 | return ERR_PTR(len); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 154 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 155 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 156 | /* |
| 157 | * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a |
| 158 | * separate struct filename so we can dedicate the entire |
| 159 | * names_cache allocation for the pathname, and re-do the copy from |
| 160 | * userland. |
| 161 | */ |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 162 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 163 | const size_t size = offsetof(struct filename, iname[1]); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 164 | kname = (char *)result; |
| 165 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 166 | /* |
| 167 | * size is chosen that way we to guarantee that |
| 168 | * result->iname[0] is within the same object and that |
| 169 | * kname can't be equal to result->iname, no matter what. |
| 170 | */ |
| 171 | result = kzalloc(size, GFP_KERNEL); |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 172 | if (unlikely(!result)) { |
| 173 | __putname(kname); |
| 174 | return ERR_PTR(-ENOMEM); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 175 | } |
| 176 | result->name = kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 177 | len = strncpy_from_user(kname, filename, PATH_MAX); |
| 178 | if (unlikely(len < 0)) { |
| 179 | __putname(kname); |
| 180 | kfree(result); |
| 181 | return ERR_PTR(len); |
| 182 | } |
| 183 | if (unlikely(len == PATH_MAX)) { |
| 184 | __putname(kname); |
| 185 | kfree(result); |
| 186 | return ERR_PTR(-ENAMETOOLONG); |
| 187 | } |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 190 | result->refcnt = 1; |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 191 | /* The empty path is special. */ |
| 192 | if (unlikely(!len)) { |
| 193 | if (empty) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 194 | *empty = 1; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 195 | if (!(flags & LOOKUP_EMPTY)) { |
| 196 | putname(result); |
| 197 | return ERR_PTR(-ENOENT); |
| 198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 200 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 201 | result->uptr = filename; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 202 | result->aname = NULL; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 203 | audit_getname(result); |
| 204 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 207 | struct filename * |
Dmitry Kadashev | 8228e2c | 2021-07-08 13:34:42 +0700 | [diff] [blame] | 208 | getname_uflags(const char __user *filename, int uflags) |
| 209 | { |
| 210 | int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0; |
| 211 | |
| 212 | return getname_flags(filename, flags, NULL); |
| 213 | } |
| 214 | |
| 215 | struct filename * |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 216 | getname(const char __user * filename) |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 217 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 218 | return getname_flags(filename, 0, NULL); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 221 | struct filename * |
| 222 | getname_kernel(const char * filename) |
| 223 | { |
| 224 | struct filename *result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 225 | int len = strlen(filename) + 1; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 226 | |
| 227 | result = __getname(); |
| 228 | if (unlikely(!result)) |
| 229 | return ERR_PTR(-ENOMEM); |
| 230 | |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 231 | if (len <= EMBEDDED_NAME_MAX) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 232 | result->name = (char *)result->iname; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 233 | } else if (len <= PATH_MAX) { |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 234 | const size_t size = offsetof(struct filename, iname[1]); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 235 | struct filename *tmp; |
| 236 | |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 237 | tmp = kmalloc(size, GFP_KERNEL); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 238 | if (unlikely(!tmp)) { |
| 239 | __putname(result); |
| 240 | return ERR_PTR(-ENOMEM); |
| 241 | } |
| 242 | tmp->name = (char *)result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 243 | result = tmp; |
| 244 | } else { |
| 245 | __putname(result); |
| 246 | return ERR_PTR(-ENAMETOOLONG); |
| 247 | } |
| 248 | memcpy((char *)result->name, filename, len); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 249 | result->uptr = NULL; |
| 250 | result->aname = NULL; |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 251 | result->refcnt = 1; |
Paul Moore | fd3522f | 2015-01-22 00:00:10 -0500 | [diff] [blame] | 252 | audit_getname(result); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 253 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 254 | return result; |
| 255 | } |
| 256 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 257 | void putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Al Viro | ea47ab1 | 2021-09-07 16:14:05 -0400 | [diff] [blame] | 259 | if (IS_ERR(name)) |
Dmitry Kadashev | 91ef658 | 2021-07-08 13:34:37 +0700 | [diff] [blame] | 260 | return; |
| 261 | |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 262 | BUG_ON(name->refcnt <= 0); |
| 263 | |
| 264 | if (--name->refcnt > 0) |
| 265 | return; |
| 266 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 267 | if (name->name != name->iname) { |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 268 | __putname(name->name); |
| 269 | kfree(name); |
| 270 | } else |
| 271 | __putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 274 | /** |
| 275 | * check_acl - perform ACL permission checking |
| 276 | * @mnt_userns: user namespace of the mount the inode was found from |
| 277 | * @inode: inode to check permissions on |
| 278 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) |
| 279 | * |
| 280 | * This function performs the ACL permission checking. Since this function |
| 281 | * retrieve POSIX acls it needs to know whether it is called from a blocking or |
| 282 | * non-blocking context and thus cares about the MAY_NOT_BLOCK bit. |
| 283 | * |
| 284 | * If the inode has been found through an idmapped mount the user namespace of |
| 285 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 286 | * care to map the inode according to @mnt_userns before checking permissions. |
| 287 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 288 | * raw inode simply passs init_user_ns. |
| 289 | */ |
| 290 | static int check_acl(struct user_namespace *mnt_userns, |
| 291 | struct inode *inode, int mask) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 292 | { |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 293 | #ifdef CONFIG_FS_POSIX_ACL |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 294 | struct posix_acl *acl; |
| 295 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 296 | if (mask & MAY_NOT_BLOCK) { |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 297 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
| 298 | if (!acl) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 299 | return -EAGAIN; |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 300 | /* no ->get_acl() calls in RCU mode... */ |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 301 | if (is_uncached_acl(acl)) |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 302 | return -ECHILD; |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 303 | return posix_acl_permission(mnt_userns, inode, acl, mask); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 306 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
| 307 | if (IS_ERR(acl)) |
| 308 | return PTR_ERR(acl); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 309 | if (acl) { |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 310 | int error = posix_acl_permission(mnt_userns, inode, acl, mask); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 311 | posix_acl_release(acl); |
| 312 | return error; |
| 313 | } |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 314 | #endif |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 315 | |
| 316 | return -EAGAIN; |
| 317 | } |
| 318 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 319 | /** |
| 320 | * acl_permission_check - perform basic UNIX permission checking |
| 321 | * @mnt_userns: user namespace of the mount the inode was found from |
| 322 | * @inode: inode to check permissions on |
| 323 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 324 | * |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 325 | * This function performs the basic UNIX permission checking. Since this |
| 326 | * function may retrieve POSIX acls it needs to know whether it is called from a |
| 327 | * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit. |
| 328 | * |
| 329 | * If the inode has been found through an idmapped mount the user namespace of |
| 330 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 331 | * care to map the inode according to @mnt_userns before checking permissions. |
| 332 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 333 | * raw inode simply passs init_user_ns. |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 334 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 335 | static int acl_permission_check(struct user_namespace *mnt_userns, |
| 336 | struct inode *inode, int mask) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 337 | { |
Linus Torvalds | 26cf46b | 2011-05-13 11:51:01 -0700 | [diff] [blame] | 338 | unsigned int mode = inode->i_mode; |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 339 | kuid_t i_uid; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 340 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 341 | /* Are we the owner? If so, ACL's don't matter */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 342 | i_uid = i_uid_into_mnt(mnt_userns, inode); |
| 343 | if (likely(uid_eq(current_fsuid(), i_uid))) { |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 344 | mask &= 7; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 345 | mode >>= 6; |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 346 | return (mask & ~mode) ? -EACCES : 0; |
| 347 | } |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 348 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 349 | /* Do we have ACL's? */ |
| 350 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 351 | int error = check_acl(mnt_userns, inode, mask); |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 352 | if (error != -EAGAIN) |
| 353 | return error; |
| 354 | } |
| 355 | |
| 356 | /* Only RWX matters for group/other mode bits */ |
| 357 | mask &= 7; |
| 358 | |
| 359 | /* |
| 360 | * Are the group permissions different from |
| 361 | * the other permissions in the bits we care |
| 362 | * about? Need to check group ownership if so. |
| 363 | */ |
| 364 | if (mask & (mode ^ (mode >> 3))) { |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 365 | kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); |
| 366 | if (in_group_p(kgid)) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 367 | mode >>= 3; |
| 368 | } |
| 369 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 370 | /* Bits in 'mode' clear that we require? */ |
| 371 | return (mask & ~mode) ? -EACCES : 0; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | /** |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 375 | * generic_permission - check for access rights on a Posix-like filesystem |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 376 | * @mnt_userns: user namespace of the mount the inode was found from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | * @inode: inode to check access rights for |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 378 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, |
| 379 | * %MAY_NOT_BLOCK ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * |
| 381 | * Used to check for read/write/execute permissions on a file. |
| 382 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 383 | * for filesystem access without changing the "normal" uids which |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 384 | * are used for other things. |
| 385 | * |
| 386 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk |
| 387 | * request cannot be satisfied (eg. requires blocking or too much complexity). |
| 388 | * It would then be called again in ref-walk mode. |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 389 | * |
| 390 | * If the inode has been found through an idmapped mount the user namespace of |
| 391 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 392 | * care to map the inode according to @mnt_userns before checking permissions. |
| 393 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 394 | * raw inode simply passs init_user_ns. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 396 | int generic_permission(struct user_namespace *mnt_userns, struct inode *inode, |
| 397 | int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 399 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 402 | * Do the basic permission checks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 404 | ret = acl_permission_check(mnt_userns, inode, mask); |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 405 | if (ret != -EACCES) |
| 406 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 408 | if (S_ISDIR(inode->i_mode)) { |
| 409 | /* DACs are overridable for directories */ |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 410 | if (!(mask & MAY_WRITE)) |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 411 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 412 | CAP_DAC_READ_SEARCH)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 413 | return 0; |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 414 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Christian Brauner | 0558c1b | 2021-01-21 14:19:23 +0100 | [diff] [blame] | 415 | CAP_DAC_OVERRIDE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 417 | return -EACCES; |
| 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * Searching includes executable on directories, else just read. |
| 422 | */ |
Serge E. Hallyn | 7ea6600 | 2009-12-29 14:50:19 -0600 | [diff] [blame] | 423 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 424 | if (mask == MAY_READ) |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 425 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Christian Brauner | 0558c1b | 2021-01-21 14:19:23 +0100 | [diff] [blame] | 426 | CAP_DAC_READ_SEARCH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 428 | /* |
| 429 | * Read/write DACs are always overridable. |
| 430 | * Executable DACs are overridable when there is |
| 431 | * at least one exec bit set. |
| 432 | */ |
| 433 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 434 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
Christian Brauner | 0558c1b | 2021-01-21 14:19:23 +0100 | [diff] [blame] | 435 | CAP_DAC_OVERRIDE)) |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 436 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | return -EACCES; |
| 439 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 440 | EXPORT_SYMBOL(generic_permission); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 442 | /** |
| 443 | * do_inode_permission - UNIX permission checking |
| 444 | * @mnt_userns: user namespace of the mount the inode was found from |
| 445 | * @inode: inode to check permissions on |
| 446 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) |
| 447 | * |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 448 | * We _really_ want to just do "generic_permission()" without |
| 449 | * even looking at the inode->i_op values. So we keep a cache |
| 450 | * flag in inode->i_opflags, that says "this has not special |
| 451 | * permission function, use the fast case". |
| 452 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 453 | static inline int do_inode_permission(struct user_namespace *mnt_userns, |
| 454 | struct inode *inode, int mask) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 455 | { |
| 456 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { |
| 457 | if (likely(inode->i_op->permission)) |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 458 | return inode->i_op->permission(mnt_userns, inode, mask); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 459 | |
| 460 | /* This gets set once for the inode lifetime */ |
| 461 | spin_lock(&inode->i_lock); |
| 462 | inode->i_opflags |= IOP_FASTPERM; |
| 463 | spin_unlock(&inode->i_lock); |
| 464 | } |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 465 | return generic_permission(mnt_userns, inode, mask); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 468 | /** |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 469 | * sb_permission - Check superblock-level permissions |
| 470 | * @sb: Superblock of inode to check permission on |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 471 | * @inode: Inode to check permission on |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 472 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 473 | * |
| 474 | * Separate out file-system wide checks from inode-specific permission checks. |
| 475 | */ |
| 476 | static int sb_permission(struct super_block *sb, struct inode *inode, int mask) |
| 477 | { |
| 478 | if (unlikely(mask & MAY_WRITE)) { |
| 479 | umode_t mode = inode->i_mode; |
| 480 | |
| 481 | /* Nobody gets write access to a read-only fs. */ |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 482 | 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] | 483 | return -EROFS; |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * inode_permission - Check for access rights to a given inode |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 490 | * @mnt_userns: User namespace of the mount the inode was found from |
| 491 | * @inode: Inode to check permission on |
| 492 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 493 | * |
| 494 | * Check for read/write/execute permissions on an inode. We use fs[ug]id for |
| 495 | * this, letting us set arbitrary permissions for filesystem access without |
| 496 | * changing the "normal" UIDs which are used for other things. |
| 497 | * |
| 498 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. |
| 499 | */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 500 | int inode_permission(struct user_namespace *mnt_userns, |
| 501 | struct inode *inode, int mask) |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 502 | { |
| 503 | int retval; |
| 504 | |
| 505 | retval = sb_permission(inode->i_sb, inode, mask); |
| 506 | if (retval) |
| 507 | return retval; |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 508 | |
| 509 | if (unlikely(mask & MAY_WRITE)) { |
| 510 | /* |
| 511 | * Nobody gets write access to an immutable file. |
| 512 | */ |
| 513 | if (IS_IMMUTABLE(inode)) |
| 514 | return -EPERM; |
| 515 | |
| 516 | /* |
| 517 | * Updating mtime will likely cause i_uid and i_gid to be |
| 518 | * written back improperly if their true value is unknown |
| 519 | * to the vfs. |
| 520 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 521 | if (HAS_UNMAPPED_ID(mnt_userns, inode)) |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 522 | return -EACCES; |
| 523 | } |
| 524 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 525 | retval = do_inode_permission(mnt_userns, inode, mask); |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 526 | if (retval) |
| 527 | return retval; |
| 528 | |
| 529 | retval = devcgroup_inode_permission(inode, mask); |
| 530 | if (retval) |
| 531 | return retval; |
| 532 | |
| 533 | return security_inode_permission(inode, mask); |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 534 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 535 | EXPORT_SYMBOL(inode_permission); |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 536 | |
| 537 | /** |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 538 | * path_get - get a reference to a path |
| 539 | * @path: path to get the reference to |
| 540 | * |
| 541 | * Given a path increment the reference count to the dentry and the vfsmount. |
| 542 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 543 | void path_get(const struct path *path) |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 544 | { |
| 545 | mntget(path->mnt); |
| 546 | dget(path->dentry); |
| 547 | } |
| 548 | EXPORT_SYMBOL(path_get); |
| 549 | |
| 550 | /** |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 551 | * path_put - put a reference to a path |
| 552 | * @path: path to put the reference to |
| 553 | * |
| 554 | * Given a path decrement the reference count to the dentry and the vfsmount. |
| 555 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 556 | void path_put(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 558 | dput(path->dentry); |
| 559 | mntput(path->mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 561 | EXPORT_SYMBOL(path_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 563 | #define EMBEDDED_LEVELS 2 |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 564 | struct nameidata { |
| 565 | struct path path; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 566 | struct qstr last; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 567 | struct path root; |
| 568 | struct inode *inode; /* path.dentry.d_inode */ |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 569 | unsigned int flags, state; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 570 | unsigned seq, m_seq, r_seq; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 571 | int last_type; |
| 572 | unsigned depth; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 573 | int total_link_count; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 574 | struct saved { |
| 575 | struct path link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 576 | struct delayed_call done; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 577 | const char *name; |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 578 | unsigned seq; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 579 | } *stack, internal[EMBEDDED_LEVELS]; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 580 | struct filename *name; |
| 581 | struct nameidata *saved; |
| 582 | unsigned root_seq; |
| 583 | int dfd; |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 584 | kuid_t dir_uid; |
| 585 | umode_t dir_mode; |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 586 | } __randomize_layout; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 587 | |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 588 | #define ND_ROOT_PRESET 1 |
| 589 | #define ND_ROOT_GRABBED 2 |
| 590 | #define ND_JUMPED 4 |
| 591 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 592 | static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 593 | { |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 594 | struct nameidata *old = current->nameidata; |
| 595 | p->stack = p->internal; |
Al Viro | 7962c7d | 2021-04-03 16:49:44 -0400 | [diff] [blame] | 596 | p->depth = 0; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 597 | p->dfd = dfd; |
| 598 | p->name = name; |
Al Viro | 7d01ef75 | 2021-04-06 12:33:07 -0400 | [diff] [blame] | 599 | p->path.mnt = NULL; |
| 600 | p->path.dentry = NULL; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 601 | p->total_link_count = old ? old->total_link_count : 0; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 602 | p->saved = old; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 603 | current->nameidata = p; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 604 | } |
| 605 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 606 | static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name, |
| 607 | const struct path *root) |
| 608 | { |
| 609 | __set_nameidata(p, dfd, name); |
| 610 | p->state = 0; |
| 611 | if (unlikely(root)) { |
| 612 | p->state = ND_ROOT_PRESET; |
| 613 | p->root = *root; |
| 614 | } |
| 615 | } |
| 616 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 617 | static void restore_nameidata(void) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 618 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 619 | struct nameidata *now = current->nameidata, *old = now->saved; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 620 | |
| 621 | current->nameidata = old; |
| 622 | if (old) |
| 623 | old->total_link_count = now->total_link_count; |
Al Viro | e1a63bb | 2015-12-05 21:06:33 -0500 | [diff] [blame] | 624 | if (now->stack != now->internal) |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 625 | kfree(now->stack); |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 628 | static bool nd_alloc_stack(struct nameidata *nd) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 629 | { |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 630 | struct saved *p; |
| 631 | |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 632 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
| 633 | nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); |
| 634 | if (unlikely(!p)) |
| 635 | return false; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 636 | memcpy(p, nd->internal, sizeof(nd->internal)); |
| 637 | nd->stack = p; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 638 | return true; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 639 | } |
| 640 | |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 641 | /** |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 642 | * path_connected - Verify that a dentry is below mnt.mnt_root |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 643 | * |
| 644 | * Rename can sometimes move a file or directory outside of a bind |
| 645 | * mount, path_connected allows those cases to be detected. |
| 646 | */ |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 647 | static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 648 | { |
Eric W. Biederman | 95dd775 | 2018-03-14 18:20:29 -0500 | [diff] [blame] | 649 | struct super_block *sb = mnt->mnt_sb; |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 650 | |
Christoph Hellwig | 402dd2c | 2020-09-24 08:51:28 +0200 | [diff] [blame] | 651 | /* Bind mounts can have disconnected paths */ |
| 652 | if (mnt->mnt_root == sb->s_root) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 653 | return true; |
| 654 | |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 655 | return is_subdir(dentry, mnt->mnt_root); |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 656 | } |
| 657 | |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 658 | static void drop_links(struct nameidata *nd) |
| 659 | { |
| 660 | int i = nd->depth; |
| 661 | while (i--) { |
| 662 | struct saved *last = nd->stack + i; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 663 | do_delayed_call(&last->done); |
| 664 | clear_delayed_call(&last->done); |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | static void terminate_walk(struct nameidata *nd) |
| 669 | { |
| 670 | drop_links(nd); |
| 671 | if (!(nd->flags & LOOKUP_RCU)) { |
| 672 | int i; |
| 673 | path_put(&nd->path); |
| 674 | for (i = 0; i < nd->depth; i++) |
| 675 | path_put(&nd->stack[i].link); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 676 | if (nd->state & ND_ROOT_GRABBED) { |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 677 | path_put(&nd->root); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 678 | nd->state &= ~ND_ROOT_GRABBED; |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 679 | } |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 680 | } else { |
| 681 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 682 | rcu_read_unlock(); |
| 683 | } |
| 684 | nd->depth = 0; |
Al Viro | 7d01ef75 | 2021-04-06 12:33:07 -0400 | [diff] [blame] | 685 | nd->path.mnt = NULL; |
| 686 | nd->path.dentry = NULL; |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* path_put is needed afterwards regardless of success or failure */ |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 690 | static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 691 | { |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 692 | int res = __legitimize_mnt(path->mnt, mseq); |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 693 | if (unlikely(res)) { |
| 694 | if (res > 0) |
| 695 | path->mnt = NULL; |
| 696 | path->dentry = NULL; |
| 697 | return false; |
| 698 | } |
| 699 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { |
| 700 | path->dentry = NULL; |
| 701 | return false; |
| 702 | } |
| 703 | return !read_seqcount_retry(&path->dentry->d_seq, seq); |
| 704 | } |
| 705 | |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 706 | static inline bool legitimize_path(struct nameidata *nd, |
| 707 | struct path *path, unsigned seq) |
| 708 | { |
Al Viro | 5bd7328 | 2020-04-05 21:59:55 -0400 | [diff] [blame] | 709 | return __legitimize_path(path, seq, nd->m_seq); |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 712 | static bool legitimize_links(struct nameidata *nd) |
| 713 | { |
| 714 | int i; |
Al Viro | eacd9aa8 | 2021-02-15 12:03:23 -0500 | [diff] [blame] | 715 | if (unlikely(nd->flags & LOOKUP_CACHED)) { |
| 716 | drop_links(nd); |
| 717 | nd->depth = 0; |
| 718 | return false; |
| 719 | } |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 720 | for (i = 0; i < nd->depth; i++) { |
| 721 | struct saved *last = nd->stack + i; |
| 722 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { |
| 723 | drop_links(nd); |
| 724 | nd->depth = i + 1; |
| 725 | return false; |
| 726 | } |
| 727 | } |
| 728 | return true; |
| 729 | } |
| 730 | |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 731 | static bool legitimize_root(struct nameidata *nd) |
| 732 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 733 | /* 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] | 734 | if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 735 | return true; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 736 | nd->state |= ND_ROOT_GRABBED; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 737 | return legitimize_path(nd, &nd->root, nd->root_seq); |
| 738 | } |
| 739 | |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 740 | /* |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 741 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 742 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
| 743 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab |
Mike Marshall | 57e3715 | 2015-11-30 11:11:59 -0500 | [diff] [blame] | 744 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 745 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
| 746 | * got stuck, so ref-walk may continue from there. If this is not successful |
| 747 | * (eg. a seqcount has changed), then failure is returned and it's up to caller |
| 748 | * to restart the path walk from the beginning in ref-walk mode. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 749 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 750 | |
| 751 | /** |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 752 | * try_to_unlazy - try to switch to ref-walk mode. |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 753 | * @nd: nameidata pathwalk data |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 754 | * Returns: true on success, false on failure |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 755 | * |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 756 | * 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] | 757 | * for ref-walk mode. |
| 758 | * Must be called from rcu-walk context. |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 759 | * Nothing should touch nameidata between try_to_unlazy() failure and |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 760 | * terminate_walk(). |
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 | static bool try_to_unlazy(struct nameidata *nd) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 763 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 764 | struct dentry *parent = nd->path.dentry; |
| 765 | |
| 766 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 767 | |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 768 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 769 | if (unlikely(!legitimize_links(nd))) |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 770 | goto out1; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 771 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
| 772 | goto out; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 773 | if (unlikely(!legitimize_root(nd))) |
| 774 | goto out; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 775 | rcu_read_unlock(); |
| 776 | BUG_ON(nd->inode != parent->d_inode); |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 777 | return true; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 778 | |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 779 | out1: |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 780 | nd->path.mnt = NULL; |
| 781 | nd->path.dentry = NULL; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 782 | out: |
| 783 | rcu_read_unlock(); |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 784 | return false; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 788 | * try_to_unlazy_next - try to switch to ref-walk mode. |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 789 | * @nd: nameidata pathwalk data |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 790 | * @dentry: next dentry to step into |
| 791 | * @seq: seq number to check @dentry against |
| 792 | * Returns: true on success, false on failure |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 793 | * |
Tom Rix | 30476f7 | 2022-01-25 05:13:40 -0800 | [diff] [blame] | 794 | * Similar to try_to_unlazy(), but here we have the next dentry already |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 795 | * picked by rcu-walk and want to legitimize that in addition to the current |
| 796 | * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context. |
| 797 | * Nothing should touch nameidata between try_to_unlazy_next() failure and |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 798 | * terminate_walk(). |
| 799 | */ |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 800 | 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] | 801 | { |
| 802 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
| 803 | |
| 804 | nd->flags &= ~LOOKUP_RCU; |
| 805 | if (unlikely(!legitimize_links(nd))) |
| 806 | goto out2; |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 807 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) |
| 808 | goto out2; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 809 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 810 | goto out1; |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 811 | |
Linus Torvalds | 1557008 | 2013-09-02 11:38:06 -0700 | [diff] [blame] | 812 | /* |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 813 | * We need to move both the parent and the dentry from the RCU domain |
| 814 | * to be properly refcounted. And the sequence number in the dentry |
| 815 | * validates *both* dentry counters, since we checked the sequence |
| 816 | * number of the parent after we got the child sequence number. So we |
| 817 | * know the parent must still be valid if the child sequence number is |
Linus 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 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
| 820 | goto out; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 821 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
| 822 | goto out_dput; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 823 | /* |
| 824 | * Sequence counts matched. Now make sure that the root is |
| 825 | * still valid and get it if required. |
| 826 | */ |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 827 | if (unlikely(!legitimize_root(nd))) |
| 828 | goto out_dput; |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 829 | rcu_read_unlock(); |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 830 | return true; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 831 | |
Al Viro | 7973387a | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 832 | out2: |
| 833 | nd->path.mnt = NULL; |
| 834 | out1: |
| 835 | nd->path.dentry = NULL; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 836 | out: |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 837 | rcu_read_unlock(); |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 838 | return false; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 839 | out_dput: |
| 840 | rcu_read_unlock(); |
| 841 | dput(dentry); |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 842 | return false; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 843 | } |
| 844 | |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 845 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 846 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 847 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
| 848 | return dentry->d_op->d_revalidate(dentry, flags); |
| 849 | else |
| 850 | return 1; |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 851 | } |
| 852 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 853 | /** |
| 854 | * complete_walk - successful completion of path walk |
| 855 | * @nd: pointer nameidata |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 856 | * |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 857 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
| 858 | * Revalidate the final result, unless we'd already done that during |
| 859 | * the path walk or the filesystem doesn't ask for it. Return 0 on |
| 860 | * success, -error on failure. In case of failure caller does not |
| 861 | * need to drop nd->path. |
Jeff 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 | static int complete_walk(struct nameidata *nd) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 864 | { |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 865 | struct dentry *dentry = nd->path.dentry; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 866 | int status; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 867 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 868 | if (nd->flags & LOOKUP_RCU) { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 869 | /* |
| 870 | * We don't want to zero nd->root for scoped-lookups or |
| 871 | * externally-managed nd->root. |
| 872 | */ |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 873 | if (!(nd->state & ND_ROOT_PRESET)) |
| 874 | if (!(nd->flags & LOOKUP_IS_SCOPED)) |
| 875 | nd->root.mnt = NULL; |
Jens Axboe | 6c6ec2b | 2020-12-17 09:19:09 -0700 | [diff] [blame] | 876 | nd->flags &= ~LOOKUP_CACHED; |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 877 | if (!try_to_unlazy(nd)) |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 878 | return -ECHILD; |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 881 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 882 | /* |
| 883 | * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't |
| 884 | * ever step outside the root during lookup" and should already |
| 885 | * be guaranteed by the rest of namei, we want to avoid a namei |
| 886 | * BUG resulting in userspace being given a path that was not |
| 887 | * scoped within the root at some point during the lookup. |
| 888 | * |
| 889 | * So, do a final sanity-check to make sure that in the |
| 890 | * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) |
| 891 | * we won't silently return an fd completely outside of the |
| 892 | * requested root to userspace. |
| 893 | * |
| 894 | * Userspace could move the path outside the root after this |
| 895 | * check, but as discussed elsewhere this is not a concern (the |
| 896 | * resolved file was inside the root at some point). |
| 897 | */ |
| 898 | if (!path_is_under(&nd->path, &nd->root)) |
| 899 | return -EXDEV; |
| 900 | } |
| 901 | |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 902 | if (likely(!(nd->state & ND_JUMPED))) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 903 | return 0; |
| 904 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 905 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 906 | return 0; |
| 907 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 908 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 909 | if (status > 0) |
| 910 | return 0; |
| 911 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 912 | if (!status) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 913 | status = -ESTALE; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 914 | |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 915 | return status; |
| 916 | } |
| 917 | |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 918 | static int set_root(struct nameidata *nd) |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 919 | { |
Al Viro | 7bd8837 | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 920 | struct fs_struct *fs = current->fs; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 921 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 922 | /* |
| 923 | * Jumping to the real root in a scoped-lookup is a BUG in namei, but we |
| 924 | * still have to ensure it doesn't happen because it will cause a breakout |
| 925 | * from the dirfd. |
| 926 | */ |
| 927 | if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) |
| 928 | return -ENOTRECOVERABLE; |
| 929 | |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 930 | if (nd->flags & LOOKUP_RCU) { |
| 931 | unsigned seq; |
| 932 | |
| 933 | do { |
| 934 | seq = read_seqcount_begin(&fs->seq); |
| 935 | nd->root = fs->root; |
| 936 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); |
| 937 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 938 | } else { |
| 939 | get_fs_root(fs, &nd->root); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 940 | nd->state |= ND_ROOT_GRABBED; |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 941 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 942 | return 0; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 943 | } |
| 944 | |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 945 | static int nd_jump_root(struct nameidata *nd) |
| 946 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 947 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 948 | return -EXDEV; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 949 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 950 | /* Absolute path arguments to path_init() are allowed. */ |
| 951 | if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) |
| 952 | return -EXDEV; |
| 953 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 954 | if (!nd->root.mnt) { |
| 955 | int error = set_root(nd); |
| 956 | if (error) |
| 957 | return error; |
| 958 | } |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 959 | if (nd->flags & LOOKUP_RCU) { |
| 960 | struct dentry *d; |
| 961 | nd->path = nd->root; |
| 962 | d = nd->path.dentry; |
| 963 | nd->inode = d->d_inode; |
| 964 | nd->seq = nd->root_seq; |
| 965 | if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) |
| 966 | return -ECHILD; |
| 967 | } else { |
| 968 | path_put(&nd->path); |
| 969 | nd->path = nd->root; |
| 970 | path_get(&nd->path); |
| 971 | nd->inode = nd->path.dentry->d_inode; |
| 972 | } |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 973 | nd->state |= ND_JUMPED; |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 974 | return 0; |
| 975 | } |
| 976 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 977 | /* |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 978 | * Helper to directly jump to a known parsed path from ->get_link, |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 979 | * caller must have taken a reference to path beforehand. |
| 980 | */ |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 981 | int nd_jump_link(struct path *path) |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 982 | { |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 983 | int error = -ELOOP; |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 984 | struct nameidata *nd = current->nameidata; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 985 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 986 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
| 987 | goto err; |
| 988 | |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 989 | error = -EXDEV; |
| 990 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 991 | if (nd->path.mnt != path->mnt) |
| 992 | goto err; |
| 993 | } |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 994 | /* Not currently safe for scoped-lookups. */ |
| 995 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) |
| 996 | goto err; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 997 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 998 | path_put(&nd->path); |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 999 | nd->path = *path; |
| 1000 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 1001 | nd->state |= ND_JUMPED; |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 1002 | return 0; |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 1003 | |
| 1004 | err: |
| 1005 | path_put(path); |
| 1006 | return error; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
Al Viro | b9ff442 | 2015-05-02 20:19:23 -0400 | [diff] [blame] | 1009 | static inline void put_link(struct nameidata *nd) |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1010 | { |
Al Viro | 21c3003 | 2015-05-03 21:06:24 -0400 | [diff] [blame] | 1011 | struct saved *last = nd->stack + --nd->depth; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1012 | do_delayed_call(&last->done); |
Al Viro | 6548fae | 2015-05-07 20:32:22 -0400 | [diff] [blame] | 1013 | if (!(nd->flags & LOOKUP_RCU)) |
| 1014 | path_put(&last->link); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
Luis Chamberlain | 9c011be | 2022-01-21 22:13:13 -0800 | [diff] [blame] | 1017 | static int sysctl_protected_symlinks __read_mostly; |
| 1018 | static int sysctl_protected_hardlinks __read_mostly; |
| 1019 | static int sysctl_protected_fifos __read_mostly; |
| 1020 | static int sysctl_protected_regular __read_mostly; |
| 1021 | |
| 1022 | #ifdef CONFIG_SYSCTL |
| 1023 | static struct ctl_table namei_sysctls[] = { |
| 1024 | { |
| 1025 | .procname = "protected_symlinks", |
| 1026 | .data = &sysctl_protected_symlinks, |
| 1027 | .maxlen = sizeof(int), |
Julius Hemanth Pitti | c7031c1 | 2022-05-13 16:58:15 -0700 | [diff] [blame] | 1028 | .mode = 0644, |
Luis Chamberlain | 9c011be | 2022-01-21 22:13:13 -0800 | [diff] [blame] | 1029 | .proc_handler = proc_dointvec_minmax, |
| 1030 | .extra1 = SYSCTL_ZERO, |
| 1031 | .extra2 = SYSCTL_ONE, |
| 1032 | }, |
| 1033 | { |
| 1034 | .procname = "protected_hardlinks", |
| 1035 | .data = &sysctl_protected_hardlinks, |
| 1036 | .maxlen = sizeof(int), |
Julius Hemanth Pitti | c7031c1 | 2022-05-13 16:58:15 -0700 | [diff] [blame] | 1037 | .mode = 0644, |
Luis Chamberlain | 9c011be | 2022-01-21 22:13:13 -0800 | [diff] [blame] | 1038 | .proc_handler = proc_dointvec_minmax, |
| 1039 | .extra1 = SYSCTL_ZERO, |
| 1040 | .extra2 = SYSCTL_ONE, |
| 1041 | }, |
| 1042 | { |
| 1043 | .procname = "protected_fifos", |
| 1044 | .data = &sysctl_protected_fifos, |
| 1045 | .maxlen = sizeof(int), |
Julius Hemanth Pitti | c7031c1 | 2022-05-13 16:58:15 -0700 | [diff] [blame] | 1046 | .mode = 0644, |
Luis Chamberlain | 9c011be | 2022-01-21 22:13:13 -0800 | [diff] [blame] | 1047 | .proc_handler = proc_dointvec_minmax, |
| 1048 | .extra1 = SYSCTL_ZERO, |
| 1049 | .extra2 = SYSCTL_TWO, |
| 1050 | }, |
| 1051 | { |
| 1052 | .procname = "protected_regular", |
| 1053 | .data = &sysctl_protected_regular, |
| 1054 | .maxlen = sizeof(int), |
Julius Hemanth Pitti | c7031c1 | 2022-05-13 16:58:15 -0700 | [diff] [blame] | 1055 | .mode = 0644, |
Luis Chamberlain | 9c011be | 2022-01-21 22:13:13 -0800 | [diff] [blame] | 1056 | .proc_handler = proc_dointvec_minmax, |
| 1057 | .extra1 = SYSCTL_ZERO, |
| 1058 | .extra2 = SYSCTL_TWO, |
| 1059 | }, |
| 1060 | { } |
| 1061 | }; |
| 1062 | |
| 1063 | static int __init init_fs_namei_sysctls(void) |
| 1064 | { |
| 1065 | register_sysctl_init("fs", namei_sysctls); |
| 1066 | return 0; |
| 1067 | } |
| 1068 | fs_initcall(init_fs_namei_sysctls); |
| 1069 | |
| 1070 | #endif /* CONFIG_SYSCTL */ |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1071 | |
| 1072 | /** |
| 1073 | * may_follow_link - Check symlink following for unsafe situations |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 1074 | * @nd: nameidata pathwalk data |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1075 | * |
| 1076 | * In the case of the sysctl_protected_symlinks sysctl being enabled, |
| 1077 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is |
| 1078 | * in a sticky world-writable directory. This is to protect privileged |
| 1079 | * processes from failing races against path names that may change out |
| 1080 | * from under them by way of other users creating malicious symlinks. |
| 1081 | * It will permit symlinks to be followed only when outside a sticky |
| 1082 | * world-writable directory, or when the uid of the symlink and follower |
| 1083 | * match, or when the directory owner matches the symlink's owner. |
| 1084 | * |
| 1085 | * Returns 0 if following the symlink is allowed, -ve on error. |
| 1086 | */ |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1087 | 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] | 1088 | { |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1089 | struct user_namespace *mnt_userns; |
| 1090 | kuid_t i_uid; |
| 1091 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1092 | if (!sysctl_protected_symlinks) |
| 1093 | return 0; |
| 1094 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1095 | mnt_userns = mnt_user_ns(nd->path.mnt); |
| 1096 | i_uid = i_uid_into_mnt(mnt_userns, inode); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1097 | /* Allowed if owner and follower match. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1098 | if (uid_eq(current_cred()->fsuid, i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1099 | return 0; |
| 1100 | |
| 1101 | /* Allowed if parent directory not sticky and world-writable. */ |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 1102 | if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1103 | return 0; |
| 1104 | |
| 1105 | /* Allowed if parent directory and link owner match. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1106 | 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] | 1107 | return 0; |
| 1108 | |
Al Viro | 31956502 | 2015-05-07 20:37:40 -0400 | [diff] [blame] | 1109 | if (nd->flags & LOOKUP_RCU) |
| 1110 | return -ECHILD; |
| 1111 | |
Richard Guy Briggs | ea841ba | 2018-03-21 04:42:21 -0400 | [diff] [blame] | 1112 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1113 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1114 | return -EACCES; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * safe_hardlink_source - Check for safe hardlink conditions |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1119 | * @mnt_userns: user namespace of the mount the inode was found from |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1120 | * @inode: the source inode to hardlink from |
| 1121 | * |
| 1122 | * Return false if at least one of the following conditions: |
| 1123 | * - inode is not a regular file |
| 1124 | * - inode is setuid |
| 1125 | * - inode is setgid and group-exec |
| 1126 | * - access failure for read and write |
| 1127 | * |
| 1128 | * Otherwise returns true. |
| 1129 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1130 | static bool safe_hardlink_source(struct user_namespace *mnt_userns, |
| 1131 | struct inode *inode) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1132 | { |
| 1133 | umode_t mode = inode->i_mode; |
| 1134 | |
| 1135 | /* Special files should not get pinned to the filesystem. */ |
| 1136 | if (!S_ISREG(mode)) |
| 1137 | return false; |
| 1138 | |
| 1139 | /* Setuid files should not get pinned to the filesystem. */ |
| 1140 | if (mode & S_ISUID) |
| 1141 | return false; |
| 1142 | |
| 1143 | /* Executable setgid files should not get pinned to the filesystem. */ |
| 1144 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 1145 | return false; |
| 1146 | |
| 1147 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1148 | if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1149 | return false; |
| 1150 | |
| 1151 | return true; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * may_linkat - Check permissions for creating a hardlink |
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 |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1157 | * @link: the source to hardlink from |
| 1158 | * |
| 1159 | * Block hardlink when all of: |
| 1160 | * - sysctl_protected_hardlinks enabled |
| 1161 | * - fsuid does not match inode |
| 1162 | * - hardlink source is unsafe (see safe_hardlink_source() above) |
Dirk Steinmetz | f2ca379 | 2015-10-20 16:09:19 +0200 | [diff] [blame] | 1163 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1164 | * |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1165 | * If the inode has been found through an idmapped mount the user namespace of |
| 1166 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 1167 | * care to map the inode according to @mnt_userns before checking permissions. |
| 1168 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 1169 | * raw inode simply passs init_user_ns. |
| 1170 | * |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1171 | * Returns 0 if successful, -ve on error. |
| 1172 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1173 | int may_linkat(struct user_namespace *mnt_userns, struct path *link) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1174 | { |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 1175 | struct inode *inode = link->dentry->d_inode; |
| 1176 | |
| 1177 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1178 | if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || |
| 1179 | !gid_valid(i_gid_into_mnt(mnt_userns, inode))) |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 1180 | return -EOVERFLOW; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1181 | |
| 1182 | if (!sysctl_protected_hardlinks) |
| 1183 | return 0; |
| 1184 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1185 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
| 1186 | * otherwise, it must be a safe source. |
| 1187 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1188 | if (safe_hardlink_source(mnt_userns, inode) || |
| 1189 | inode_owner_or_capable(mnt_userns, inode)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1190 | return 0; |
| 1191 | |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1192 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1193 | return -EPERM; |
| 1194 | } |
| 1195 | |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1196 | /** |
| 1197 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory |
| 1198 | * should be allowed, or not, on files that already |
| 1199 | * exist. |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1200 | * @mnt_userns: user namespace of the mount the inode was found from |
Randy Dunlap | 2111c3c | 2021-02-15 20:29:28 -0800 | [diff] [blame] | 1201 | * @nd: nameidata pathwalk data |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1202 | * @inode: the inode of the file to open |
| 1203 | * |
| 1204 | * Block an O_CREAT open of a FIFO (or a regular file) when: |
| 1205 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled |
| 1206 | * - the file already exists |
| 1207 | * - we are in a sticky directory |
| 1208 | * - we don't own the file |
| 1209 | * - the owner of the directory doesn't own the file |
| 1210 | * - the directory is world writable |
| 1211 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 |
| 1212 | * the directory doesn't have to be world writable: being group writable will |
| 1213 | * be enough. |
| 1214 | * |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1215 | * If the inode has been found through an idmapped mount the user namespace of |
| 1216 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 1217 | * care to map the inode according to @mnt_userns before checking permissions. |
| 1218 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 1219 | * raw inode simply passs init_user_ns. |
| 1220 | * |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1221 | * Returns 0 if the open is allowed, -ve on error. |
| 1222 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1223 | static int may_create_in_sticky(struct user_namespace *mnt_userns, |
| 1224 | struct nameidata *nd, struct inode *const inode) |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1225 | { |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1226 | umode_t dir_mode = nd->dir_mode; |
| 1227 | kuid_t dir_uid = nd->dir_uid; |
| 1228 | |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1229 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || |
| 1230 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1231 | likely(!(dir_mode & S_ISVTX)) || |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1232 | uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) || |
| 1233 | uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1234 | return 0; |
| 1235 | |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1236 | if (likely(dir_mode & 0002) || |
| 1237 | (dir_mode & 0020 && |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1238 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
| 1239 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1240 | const char *operation = S_ISFIFO(inode->i_mode) ? |
| 1241 | "sticky_create_fifo" : |
| 1242 | "sticky_create_regular"; |
| 1243 | audit_log_path_denied(AUDIT_ANOM_CREAT, operation); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1244 | return -EACCES; |
| 1245 | } |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
David Howells | f015f126 | 2012-06-25 12:55:28 +0100 | [diff] [blame] | 1249 | /* |
| 1250 | * follow_up - Find the mountpoint of path's vfsmount |
| 1251 | * |
| 1252 | * Given a path, find the mountpoint of its source file system. |
| 1253 | * Replace @path with the path of the mountpoint in the parent mount. |
| 1254 | * Up is towards /. |
| 1255 | * |
| 1256 | * Return 1 if we went up a level and 0 if we were already at the |
| 1257 | * root. |
| 1258 | */ |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1259 | int follow_up(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1261 | struct mount *mnt = real_mount(path->mnt); |
| 1262 | struct mount *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | struct dentry *mountpoint; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1264 | |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1265 | read_seqlock_excl(&mount_lock); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1266 | parent = mnt->mnt_parent; |
Al Viro | 3c0a616 | 2012-07-18 17:32:50 +0400 | [diff] [blame] | 1267 | if (parent == mnt) { |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1268 | read_sequnlock_excl(&mount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | return 0; |
| 1270 | } |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1271 | mntget(&parent->mnt); |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 1272 | mountpoint = dget(mnt->mnt_mountpoint); |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1273 | read_sequnlock_excl(&mount_lock); |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1274 | dput(path->dentry); |
| 1275 | path->dentry = mountpoint; |
| 1276 | mntput(path->mnt); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1277 | path->mnt = &parent->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | return 1; |
| 1279 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1280 | EXPORT_SYMBOL(follow_up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1282 | static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, |
| 1283 | struct path *path, unsigned *seqp) |
| 1284 | { |
| 1285 | while (mnt_has_parent(m)) { |
| 1286 | struct dentry *mountpoint = m->mnt_mountpoint; |
| 1287 | |
| 1288 | m = m->mnt_parent; |
| 1289 | if (unlikely(root->dentry == mountpoint && |
| 1290 | root->mnt == &m->mnt)) |
| 1291 | break; |
| 1292 | if (mountpoint != m->mnt.mnt_root) { |
| 1293 | path->mnt = &m->mnt; |
| 1294 | path->dentry = mountpoint; |
| 1295 | *seqp = read_seqcount_begin(&mountpoint->d_seq); |
| 1296 | return true; |
| 1297 | } |
| 1298 | } |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1302 | static bool choose_mountpoint(struct mount *m, const struct path *root, |
| 1303 | struct path *path) |
| 1304 | { |
| 1305 | bool found; |
| 1306 | |
| 1307 | rcu_read_lock(); |
| 1308 | while (1) { |
| 1309 | unsigned seq, mseq = read_seqbegin(&mount_lock); |
| 1310 | |
| 1311 | found = choose_mountpoint_rcu(m, root, path, &seq); |
| 1312 | if (unlikely(!found)) { |
| 1313 | if (!read_seqretry(&mount_lock, mseq)) |
| 1314 | break; |
| 1315 | } else { |
| 1316 | if (likely(__legitimize_path(path, seq, mseq))) |
| 1317 | break; |
| 1318 | rcu_read_unlock(); |
| 1319 | path_put(path); |
| 1320 | rcu_read_lock(); |
| 1321 | } |
| 1322 | } |
| 1323 | rcu_read_unlock(); |
| 1324 | return found; |
| 1325 | } |
| 1326 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 1327 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1328 | * Perform an automount |
| 1329 | * - return -EISDIR to tell follow_managed() to stop and return the path we |
| 1330 | * were called with. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1332 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1333 | { |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1334 | struct dentry *dentry = path->dentry; |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 1335 | |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1336 | /* We don't want to mount if someone's just doing a stat - |
| 1337 | * unless they're stat'ing a directory and appended a '/' to |
| 1338 | * the name. |
| 1339 | * |
| 1340 | * We do, however, want to mount if someone wants to open or |
| 1341 | * create a file of any type under the mountpoint, wants to |
| 1342 | * traverse through the mountpoint or wants to open the |
| 1343 | * mounted directory. Also, autofs may mark negative dentries |
| 1344 | * as being automount points. These will need the attentions |
| 1345 | * of the daemon to instantiate them before they can be used. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1346 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1347 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1348 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1349 | dentry->d_inode) |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1350 | return -EISDIR; |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1351 | |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1352 | if (count && (*count)++ >= MAXSYMLINKS) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1353 | return -ELOOP; |
| 1354 | |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1355 | return finish_automount(dentry->d_op->d_automount(path), path); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1359 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
| 1360 | * dentries are pinned but not locked here, so negative dentry can go |
| 1361 | * positive right under us. Use of smp_load_acquire() provides a barrier |
| 1362 | * sufficient for ->d_inode and ->d_flags consistency. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1363 | */ |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1364 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
| 1365 | int *count, unsigned lookup_flags) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1366 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1367 | struct vfsmount *mnt = path->mnt; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1368 | bool need_mntput = false; |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1369 | int ret = 0; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1370 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1371 | while (flags & DCACHE_MANAGED_DENTRY) { |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1372 | /* Allow the filesystem to manage the transit without i_mutex |
| 1373 | * being held. */ |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1374 | if (flags & DCACHE_MANAGE_TRANSIT) { |
Ian Kent | fb5f51c | 2016-11-24 08:03:41 +1100 | [diff] [blame] | 1375 | ret = path->dentry->d_op->d_manage(path, false); |
Al Viro | 508c877 | 2020-01-14 22:09:57 -0500 | [diff] [blame] | 1376 | flags = smp_load_acquire(&path->dentry->d_flags); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1377 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1378 | break; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1381 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1382 | struct vfsmount *mounted = lookup_mnt(path); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1383 | if (mounted) { // ... in our namespace |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1384 | dput(path->dentry); |
| 1385 | if (need_mntput) |
| 1386 | mntput(path->mnt); |
| 1387 | path->mnt = mounted; |
| 1388 | path->dentry = dget(mounted->mnt_root); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1389 | // here we know it's positive |
| 1390 | flags = path->dentry->d_flags; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1391 | need_mntput = true; |
| 1392 | continue; |
| 1393 | } |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1396 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
| 1397 | break; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1398 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1399 | // uncovered automount point |
| 1400 | ret = follow_automount(path, count, lookup_flags); |
| 1401 | flags = smp_load_acquire(&path->dentry->d_flags); |
| 1402 | if (ret < 0) |
| 1403 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1405 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1406 | if (ret == -EISDIR) |
| 1407 | ret = 0; |
| 1408 | // possible if you race with several mount --move |
| 1409 | if (need_mntput && path->mnt == mnt) |
| 1410 | mntput(path->mnt); |
| 1411 | if (!ret && unlikely(d_flags_negative(flags))) |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1412 | ret = -ENOENT; |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1413 | *jumped = need_mntput; |
Al Viro | 8402752e | 2015-04-22 10:30:08 -0400 | [diff] [blame] | 1414 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1417 | static inline int traverse_mounts(struct path *path, bool *jumped, |
| 1418 | int *count, unsigned lookup_flags) |
| 1419 | { |
| 1420 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); |
| 1421 | |
| 1422 | /* fastpath */ |
| 1423 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { |
| 1424 | *jumped = false; |
| 1425 | if (unlikely(d_flags_negative(flags))) |
| 1426 | return -ENOENT; |
| 1427 | return 0; |
| 1428 | } |
| 1429 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); |
| 1430 | } |
| 1431 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1432 | int follow_down_one(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | { |
| 1434 | struct vfsmount *mounted; |
| 1435 | |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 1436 | mounted = lookup_mnt(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | if (mounted) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1438 | dput(path->dentry); |
| 1439 | mntput(path->mnt); |
| 1440 | path->mnt = mounted; |
| 1441 | path->dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | return 1; |
| 1443 | } |
| 1444 | return 0; |
| 1445 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1446 | EXPORT_SYMBOL(follow_down_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1448 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1449 | * Follow down to the covering mount currently visible to userspace. At each |
| 1450 | * point, the filesystem owning that dentry may be queried as to whether the |
| 1451 | * caller is permitted to proceed or not. |
| 1452 | */ |
| 1453 | int follow_down(struct path *path) |
| 1454 | { |
| 1455 | struct vfsmount *mnt = path->mnt; |
| 1456 | bool jumped; |
| 1457 | int ret = traverse_mounts(path, &jumped, NULL, 0); |
| 1458 | |
| 1459 | if (path->mnt != mnt) |
| 1460 | mntput(mnt); |
| 1461 | return ret; |
| 1462 | } |
| 1463 | EXPORT_SYMBOL(follow_down); |
| 1464 | |
| 1465 | /* |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1466 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
| 1467 | * we meet a managed dentry that would need blocking. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1468 | */ |
| 1469 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1470 | struct inode **inode, unsigned *seqp) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1471 | { |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1472 | struct dentry *dentry = path->dentry; |
| 1473 | unsigned int flags = dentry->d_flags; |
| 1474 | |
| 1475 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) |
| 1476 | return true; |
| 1477 | |
| 1478 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1479 | return false; |
| 1480 | |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1481 | for (;;) { |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1482 | /* |
| 1483 | * Don't forget we might have a non-mountpoint managed dentry |
| 1484 | * that wants to block transit. |
| 1485 | */ |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1486 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
| 1487 | int res = dentry->d_op->d_manage(path, true); |
| 1488 | if (res) |
| 1489 | return res == -EISDIR; |
| 1490 | flags = dentry->d_flags; |
NeilBrown | b8faf03 | 2014-08-04 17:06:29 +1000 | [diff] [blame] | 1491 | } |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1492 | |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1493 | if (flags & DCACHE_MOUNTED) { |
| 1494 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); |
| 1495 | if (mounted) { |
| 1496 | path->mnt = &mounted->mnt; |
| 1497 | dentry = path->dentry = mounted->mnt.mnt_root; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 1498 | nd->state |= ND_JUMPED; |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1499 | *seqp = read_seqcount_begin(&dentry->d_seq); |
| 1500 | *inode = dentry->d_inode; |
| 1501 | /* |
| 1502 | * We don't need to re-check ->d_seq after this |
| 1503 | * ->d_inode read - there will be an RCU delay |
| 1504 | * between mount hash removal and ->mnt_root |
| 1505 | * becoming unpinned. |
| 1506 | */ |
| 1507 | flags = dentry->d_flags; |
| 1508 | continue; |
| 1509 | } |
| 1510 | if (read_seqretry(&mount_lock, nd->m_seq)) |
| 1511 | return false; |
| 1512 | } |
| 1513 | return !(flags & DCACHE_NEED_AUTOMOUNT); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1514 | } |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1515 | } |
| 1516 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1517 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
| 1518 | struct path *path, struct inode **inode, |
| 1519 | unsigned int *seqp) |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1520 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1521 | bool jumped; |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1522 | int ret; |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1523 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1524 | path->mnt = nd->path.mnt; |
| 1525 | path->dentry = dentry; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1526 | if (nd->flags & LOOKUP_RCU) { |
| 1527 | unsigned int seq = *seqp; |
| 1528 | if (unlikely(!*inode)) |
| 1529 | return -ENOENT; |
| 1530 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1531 | return 0; |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 1532 | if (!try_to_unlazy_next(nd, dentry, seq)) |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1533 | return -ECHILD; |
| 1534 | // *path might've been clobbered by __follow_mount_rcu() |
| 1535 | path->mnt = nd->path.mnt; |
| 1536 | path->dentry = dentry; |
| 1537 | } |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1538 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
| 1539 | if (jumped) { |
| 1540 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1541 | ret = -EXDEV; |
| 1542 | else |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 1543 | nd->state |= ND_JUMPED; |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1544 | } |
| 1545 | if (unlikely(ret)) { |
| 1546 | dput(path->dentry); |
| 1547 | if (path->mnt != nd->path.mnt) |
| 1548 | mntput(path->mnt); |
| 1549 | } else { |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1550 | *inode = d_backing_inode(path->dentry); |
| 1551 | *seqp = 0; /* out of RCU mode, so the value doesn't matter */ |
| 1552 | } |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1556 | /* |
Oleg Drokin | f4fdace | 2016-07-07 22:04:04 -0400 | [diff] [blame] | 1557 | * This looks up the name in dcache and possibly revalidates the found dentry. |
| 1558 | * NULL is returned if the dentry does not exist in the cache. |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1559 | */ |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1560 | static struct dentry *lookup_dcache(const struct qstr *name, |
| 1561 | struct dentry *dir, |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 1562 | unsigned int flags) |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1563 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1564 | struct dentry *dentry = d_lookup(dir, name); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1565 | if (dentry) { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1566 | int error = d_revalidate(dentry, flags); |
| 1567 | if (unlikely(error <= 0)) { |
| 1568 | if (!error) |
| 1569 | d_invalidate(dentry); |
| 1570 | dput(dentry); |
| 1571 | return ERR_PTR(error); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1572 | } |
| 1573 | } |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1574 | return dentry; |
| 1575 | } |
| 1576 | |
| 1577 | /* |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1578 | * Parent directory has inode locked exclusive. This is one |
| 1579 | * and only case when ->lookup() gets called on non in-lookup |
| 1580 | * dentries - as the matter of fact, this only gets called |
| 1581 | * when directory is guaranteed to have no in-lookup children |
| 1582 | * at all. |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1583 | */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1584 | static struct dentry *__lookup_hash(const struct qstr *name, |
| 1585 | struct dentry *base, unsigned int flags) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1586 | { |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1587 | struct dentry *dentry = lookup_dcache(name, base, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1588 | struct dentry *old; |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1589 | struct inode *dir = base->d_inode; |
| 1590 | |
| 1591 | if (dentry) |
| 1592 | return dentry; |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1593 | |
| 1594 | /* Don't create child dentry for a dead directory. */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1595 | if (unlikely(IS_DEADDIR(dir))) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1596 | return ERR_PTR(-ENOENT); |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1597 | |
| 1598 | dentry = d_alloc(base, name); |
| 1599 | if (unlikely(!dentry)) |
| 1600 | return ERR_PTR(-ENOMEM); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1601 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1602 | old = dir->i_op->lookup(dir, dentry, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1603 | if (unlikely(old)) { |
| 1604 | dput(dentry); |
| 1605 | dentry = old; |
| 1606 | } |
| 1607 | return dentry; |
| 1608 | } |
| 1609 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1610 | static struct dentry *lookup_fast(struct nameidata *nd, |
| 1611 | struct inode **inode, |
| 1612 | unsigned *seqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1614 | struct dentry *dentry, *parent = nd->path.dentry; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1615 | int status = 1; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1616 | |
Al Viro | 3cac260 | 2009-08-13 18:27:43 +0400 | [diff] [blame] | 1617 | /* |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1618 | * Rename seqlock is not required here because in the off chance |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1619 | * of a false negative due to a concurrent rename, the caller is |
| 1620 | * going to fall back to non-racy lookup. |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1621 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1622 | if (nd->flags & LOOKUP_RCU) { |
| 1623 | unsigned seq; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 1624 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1625 | if (unlikely(!dentry)) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1626 | if (!try_to_unlazy(nd)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1627 | return ERR_PTR(-ECHILD); |
| 1628 | return NULL; |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1629 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1630 | |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1631 | /* |
| 1632 | * This sequence count validates that the inode matches |
| 1633 | * the dentry name information from lookup. |
| 1634 | */ |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 1635 | *inode = d_backing_inode(dentry); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1636 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1637 | return ERR_PTR(-ECHILD); |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1638 | |
| 1639 | /* |
| 1640 | * This sequence count validates that the parent had no |
| 1641 | * changes while we did the lookup of the dentry above. |
| 1642 | * |
| 1643 | * The memory barrier in read_seqcount_begin of child is |
| 1644 | * enough, we can use __read_seqcount_retry here. |
| 1645 | */ |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1646 | if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1647 | return ERR_PTR(-ECHILD); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1648 | |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1649 | *seqp = seq; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1650 | status = d_revalidate(dentry, nd->flags); |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1651 | if (likely(status > 0)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1652 | return dentry; |
Al Viro | ae66db4 | 2021-01-04 00:08:41 -0500 | [diff] [blame] | 1653 | if (!try_to_unlazy_next(nd, dentry, seq)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1654 | return ERR_PTR(-ECHILD); |
Steven Rostedt (VMware) | 26ddb45 | 2020-12-09 17:09:28 -0500 | [diff] [blame] | 1655 | if (status == -ECHILD) |
Al Viro | 209a7fb | 2017-01-09 01:35:39 -0500 | [diff] [blame] | 1656 | /* we'd been told to redo it in non-rcu mode */ |
| 1657 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1658 | } else { |
Al Viro | e97cdc8 | 2013-01-24 18:16:00 -0500 | [diff] [blame] | 1659 | dentry = __d_lookup(parent, &nd->last); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1660 | if (unlikely(!dentry)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1661 | return NULL; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1662 | status = d_revalidate(dentry, nd->flags); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1663 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1664 | if (unlikely(status <= 0)) { |
Al Viro | e9742b5 | 2016-03-05 22:04:59 -0500 | [diff] [blame] | 1665 | if (!status) |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1666 | d_invalidate(dentry); |
Eric W. Biederman | 5542aa2 | 2014-02-13 09:46:25 -0800 | [diff] [blame] | 1667 | dput(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1668 | return ERR_PTR(status); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1669 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1670 | return dentry; |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | /* Fast lookup failed, do it the slow way */ |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1674 | static struct dentry *__lookup_slow(const struct qstr *name, |
| 1675 | struct dentry *dir, |
| 1676 | unsigned int flags) |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1677 | { |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1678 | struct dentry *dentry, *old; |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1679 | struct inode *inode = dir->d_inode; |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1680 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1681 | |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1682 | /* Don't go there if it's already dead */ |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1683 | if (unlikely(IS_DEADDIR(inode))) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1684 | return ERR_PTR(-ENOENT); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1685 | again: |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1686 | dentry = d_alloc_parallel(dir, name, &wq); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1687 | if (IS_ERR(dentry)) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1688 | return dentry; |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1689 | if (unlikely(!d_in_lookup(dentry))) { |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1690 | int error = d_revalidate(dentry, flags); |
| 1691 | if (unlikely(error <= 0)) { |
| 1692 | if (!error) { |
| 1693 | d_invalidate(dentry); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1694 | dput(dentry); |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1695 | goto again; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1696 | } |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1697 | dput(dentry); |
| 1698 | dentry = ERR_PTR(error); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1699 | } |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1700 | } else { |
| 1701 | old = inode->i_op->lookup(inode, dentry, flags); |
| 1702 | d_lookup_done(dentry); |
| 1703 | if (unlikely(old)) { |
| 1704 | dput(dentry); |
| 1705 | dentry = old; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1706 | } |
| 1707 | } |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1708 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1711 | static struct dentry *lookup_slow(const struct qstr *name, |
| 1712 | struct dentry *dir, |
| 1713 | unsigned int flags) |
| 1714 | { |
| 1715 | struct inode *inode = dir->d_inode; |
| 1716 | struct dentry *res; |
| 1717 | inode_lock_shared(inode); |
| 1718 | res = __lookup_slow(name, dir, flags); |
| 1719 | inode_unlock_shared(inode); |
| 1720 | return res; |
| 1721 | } |
| 1722 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1723 | static inline int may_lookup(struct user_namespace *mnt_userns, |
| 1724 | struct nameidata *nd) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1725 | { |
| 1726 | if (nd->flags & LOOKUP_RCU) { |
Linus Torvalds | 7d6beb7 | 2021-02-23 13:39:45 -0800 | [diff] [blame] | 1727 | 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] | 1728 | if (err != -ECHILD || !try_to_unlazy(nd)) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1729 | return err; |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1730 | } |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 1731 | return inode_permission(mnt_userns, nd->inode, MAY_EXEC); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1732 | } |
| 1733 | |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1734 | static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) |
| 1735 | { |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1736 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) |
| 1737 | return -ELOOP; |
Al Viro | 4542576 | 2020-03-03 11:25:31 -0500 | [diff] [blame] | 1738 | |
| 1739 | if (likely(nd->depth != EMBEDDED_LEVELS)) |
| 1740 | return 0; |
| 1741 | if (likely(nd->stack != nd->internal)) |
| 1742 | return 0; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1743 | if (likely(nd_alloc_stack(nd))) |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1744 | return 0; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1745 | |
| 1746 | if (nd->flags & LOOKUP_RCU) { |
| 1747 | // we need to grab link before we do unlazy. And we can't skip |
| 1748 | // unlazy even if we fail to grab the link - cleanup needs it |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1749 | bool grabbed_link = legitimize_path(nd, link, seq); |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1750 | |
Al Viro | e5ca024 | 2022-01-07 12:20:37 -0500 | [diff] [blame] | 1751 | if (!try_to_unlazy(nd) || !grabbed_link) |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1752 | return -ECHILD; |
| 1753 | |
| 1754 | if (nd_alloc_stack(nd)) |
| 1755 | return 0; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1756 | } |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1757 | return -ENOMEM; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1758 | } |
| 1759 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1760 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
| 1761 | |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1762 | static const char *pick_link(struct nameidata *nd, struct path *link, |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1763 | struct inode *inode, unsigned seq, int flags) |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1764 | { |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1765 | struct saved *last; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1766 | const char *res; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1767 | int error = reserve_stack(nd, link, seq); |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1768 | |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1769 | if (unlikely(error)) { |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1770 | if (!(nd->flags & LOOKUP_RCU)) |
| 1771 | path_put(link); |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1772 | return ERR_PTR(error); |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1773 | } |
Al Viro | ab10492 | 2015-05-10 11:50:01 -0400 | [diff] [blame] | 1774 | last = nd->stack + nd->depth++; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1775 | last->link = *link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1776 | clear_delayed_call(&last->done); |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 1777 | last->seq = seq; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1778 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1779 | if (flags & WALK_TRAILING) { |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1780 | error = may_follow_link(nd, inode); |
| 1781 | if (unlikely(error)) |
| 1782 | return ERR_PTR(error); |
| 1783 | } |
| 1784 | |
Mattias Nissler | dab741e0 | 2020-08-27 11:09:46 -0600 | [diff] [blame] | 1785 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || |
| 1786 | unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1787 | return ERR_PTR(-ELOOP); |
| 1788 | |
| 1789 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1790 | touch_atime(&last->link); |
| 1791 | cond_resched(); |
| 1792 | } else if (atime_needs_update(&last->link, inode)) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1793 | if (!try_to_unlazy(nd)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1794 | return ERR_PTR(-ECHILD); |
| 1795 | touch_atime(&last->link); |
| 1796 | } |
| 1797 | |
| 1798 | error = security_inode_follow_link(link->dentry, inode, |
| 1799 | nd->flags & LOOKUP_RCU); |
| 1800 | if (unlikely(error)) |
| 1801 | return ERR_PTR(error); |
| 1802 | |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1803 | res = READ_ONCE(inode->i_link); |
| 1804 | if (!res) { |
| 1805 | const char * (*get)(struct dentry *, struct inode *, |
| 1806 | struct delayed_call *); |
| 1807 | get = inode->i_op->get_link; |
| 1808 | if (nd->flags & LOOKUP_RCU) { |
| 1809 | res = get(NULL, inode, &last->done); |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1810 | if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1811 | res = get(link->dentry, inode, &last->done); |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1812 | } else { |
| 1813 | res = get(link->dentry, inode, &last->done); |
| 1814 | } |
| 1815 | if (!res) |
| 1816 | goto all_done; |
| 1817 | if (IS_ERR(res)) |
| 1818 | return res; |
| 1819 | } |
| 1820 | if (*res == '/') { |
| 1821 | error = nd_jump_root(nd); |
| 1822 | if (unlikely(error)) |
| 1823 | return ERR_PTR(error); |
| 1824 | while (unlikely(*++res == '/')) |
| 1825 | ; |
| 1826 | } |
| 1827 | if (*res) |
| 1828 | return res; |
| 1829 | all_done: // pure jump |
| 1830 | put_link(nd); |
| 1831 | return NULL; |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1832 | } |
| 1833 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1834 | /* |
| 1835 | * Do we need to follow links? We _really_ want to be able |
| 1836 | * to do this check without having to look at inode->i_op, |
| 1837 | * so we keep a cache of "no, this doesn't need follow_link" |
| 1838 | * for the common case. |
| 1839 | */ |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1840 | static const char *step_into(struct nameidata *nd, int flags, |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1841 | struct dentry *dentry, struct inode *inode, unsigned seq) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1842 | { |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1843 | struct path path; |
| 1844 | int err = handle_mounts(nd, dentry, &path, &inode, &seq); |
| 1845 | |
| 1846 | if (err < 0) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1847 | return ERR_PTR(err); |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1848 | if (likely(!d_is_symlink(path.dentry)) || |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 1849 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
Al Viro | aca2903 | 2020-01-09 15:17:57 -0500 | [diff] [blame] | 1850 | (flags & WALK_NOFOLLOW)) { |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1851 | /* not a symlink or should not follow */ |
Al Viro | c99687a | 2020-03-03 10:56:17 -0500 | [diff] [blame] | 1852 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1853 | dput(nd->path.dentry); |
| 1854 | if (nd->path.mnt != path.mnt) |
| 1855 | mntput(nd->path.mnt); |
| 1856 | } |
| 1857 | nd->path = path; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1858 | nd->inode = inode; |
| 1859 | nd->seq = seq; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1860 | return NULL; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1861 | } |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1862 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1863 | /* make sure that d_is_symlink above matches inode */ |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1864 | if (read_seqcount_retry(&path.dentry->d_seq, seq)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1865 | return ERR_PTR(-ECHILD); |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1866 | } else { |
| 1867 | if (path.mnt == nd->path.mnt) |
| 1868 | mntget(path.mnt); |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1869 | } |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1870 | return pick_link(nd, &path, inode, seq, flags); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1873 | static struct dentry *follow_dotdot_rcu(struct nameidata *nd, |
| 1874 | struct inode **inodep, |
| 1875 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1876 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1877 | struct dentry *parent, *old; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1878 | |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1879 | if (path_equal(&nd->path, &nd->root)) |
| 1880 | goto in_root; |
| 1881 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1882 | struct path path; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1883 | unsigned seq; |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1884 | if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), |
| 1885 | &nd->root, &path, &seq)) |
| 1886 | goto in_root; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1887 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1888 | return ERR_PTR(-ECHILD); |
| 1889 | nd->path = path; |
| 1890 | nd->inode = path.dentry->d_inode; |
| 1891 | nd->seq = seq; |
| 1892 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1893 | return ERR_PTR(-ECHILD); |
| 1894 | /* we know that mountpoint was pinned */ |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1895 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1896 | old = nd->path.dentry; |
| 1897 | parent = old->d_parent; |
| 1898 | *inodep = parent->d_inode; |
| 1899 | *seqp = read_seqcount_begin(&parent->d_seq); |
| 1900 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) |
| 1901 | return ERR_PTR(-ECHILD); |
| 1902 | if (unlikely(!path_connected(nd->path.mnt, parent))) |
| 1903 | return ERR_PTR(-ECHILD); |
| 1904 | return parent; |
| 1905 | in_root: |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1906 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1907 | return ERR_PTR(-ECHILD); |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1908 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1909 | return ERR_PTR(-ECHILD); |
| 1910 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1911 | } |
| 1912 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1913 | static struct dentry *follow_dotdot(struct nameidata *nd, |
| 1914 | struct inode **inodep, |
| 1915 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1916 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1917 | struct dentry *parent; |
| 1918 | |
| 1919 | if (path_equal(&nd->path, &nd->root)) |
| 1920 | goto in_root; |
| 1921 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1922 | struct path path; |
| 1923 | |
| 1924 | if (!choose_mountpoint(real_mount(nd->path.mnt), |
| 1925 | &nd->root, &path)) |
| 1926 | goto in_root; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame] | 1927 | path_put(&nd->path); |
| 1928 | nd->path = path; |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1929 | nd->inode = path.dentry->d_inode; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame] | 1930 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1931 | return ERR_PTR(-EXDEV); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1932 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1933 | /* rare case of legitimate dget_parent()... */ |
| 1934 | parent = dget_parent(nd->path.dentry); |
| 1935 | if (unlikely(!path_connected(nd->path.mnt, parent))) { |
| 1936 | dput(parent); |
| 1937 | return ERR_PTR(-ENOENT); |
| 1938 | } |
| 1939 | *seqp = 0; |
| 1940 | *inodep = parent->d_inode; |
| 1941 | return parent; |
| 1942 | |
| 1943 | in_root: |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1944 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1945 | return ERR_PTR(-EXDEV); |
| 1946 | dget(nd->path.dentry); |
| 1947 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1948 | } |
| 1949 | |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1950 | static const char *handle_dots(struct nameidata *nd, int type) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1951 | { |
| 1952 | if (type == LAST_DOTDOT) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1953 | const char *error = NULL; |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1954 | struct dentry *parent; |
| 1955 | struct inode *inode; |
| 1956 | unsigned seq; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1957 | |
| 1958 | if (!nd->root.mnt) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1959 | error = ERR_PTR(set_root(nd)); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1960 | if (error) |
| 1961 | return error; |
| 1962 | } |
| 1963 | if (nd->flags & LOOKUP_RCU) |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1964 | parent = follow_dotdot_rcu(nd, &inode, &seq); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1965 | else |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1966 | parent = follow_dotdot(nd, &inode, &seq); |
| 1967 | if (IS_ERR(parent)) |
| 1968 | return ERR_CAST(parent); |
| 1969 | if (unlikely(!parent)) |
| 1970 | error = step_into(nd, WALK_NOFOLLOW, |
| 1971 | nd->path.dentry, nd->inode, nd->seq); |
| 1972 | else |
| 1973 | error = step_into(nd, WALK_NOFOLLOW, |
| 1974 | parent, inode, seq); |
| 1975 | if (unlikely(error)) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1976 | return error; |
| 1977 | |
| 1978 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 1979 | /* |
| 1980 | * If there was a racing rename or mount along our |
| 1981 | * path, then we can't be sure that ".." hasn't jumped |
| 1982 | * above nd->root (and so userspace should retry or use |
| 1983 | * some fallback). |
| 1984 | */ |
| 1985 | smp_rmb(); |
| 1986 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1987 | return ERR_PTR(-EAGAIN); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1988 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1989 | return ERR_PTR(-EAGAIN); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1990 | } |
| 1991 | } |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1992 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1993 | } |
| 1994 | |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1995 | static const char *walk_component(struct nameidata *nd, int flags) |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1996 | { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1997 | struct dentry *dentry; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1998 | struct inode *inode; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1999 | unsigned seq; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 2000 | /* |
| 2001 | * "." and ".." are special - ".." especially so because it has |
| 2002 | * to be able to know about the current root directory and |
| 2003 | * parent relationships. |
| 2004 | */ |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 2005 | if (unlikely(nd->last_type != LAST_NORM)) { |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 2006 | if (!(flags & WALK_MORE) && nd->depth) |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 2007 | put_link(nd); |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 2008 | return handle_dots(nd, nd->last_type); |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 2009 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 2010 | dentry = lookup_fast(nd, &inode, &seq); |
| 2011 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2012 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 2013 | if (unlikely(!dentry)) { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 2014 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
| 2015 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2016 | return ERR_CAST(dentry); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 2017 | } |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 2018 | if (!(flags & WALK_MORE) && nd->depth) |
| 2019 | put_link(nd); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 2020 | return step_into(nd, flags, dentry, inode, seq); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 2021 | } |
| 2022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | /* |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2024 | * We can do the critical dentry name comparison and hashing |
| 2025 | * operations one word at a time, but we are limited to: |
| 2026 | * |
| 2027 | * - Architectures with fast unaligned word accesses. We could |
| 2028 | * do a "get_unaligned()" if this helps and is sufficiently |
| 2029 | * fast. |
| 2030 | * |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2031 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
| 2032 | * do not trap on the (extremely unlikely) case of a page |
| 2033 | * crossing operation. |
| 2034 | * |
| 2035 | * - Furthermore, we need an efficient 64-bit compile for the |
| 2036 | * 64-bit case in order to generate the "number of bytes in |
| 2037 | * the final mask". Again, that could be replaced with a |
| 2038 | * efficient population count instruction or similar. |
| 2039 | */ |
| 2040 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
| 2041 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 2042 | #include <asm/word-at-a-time.h> |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2043 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 2044 | #ifdef HASH_MIX |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2045 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 2046 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
| 2047 | |
| 2048 | #elif defined(CONFIG_64BIT) |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2049 | /* |
| 2050 | * Register pressure in the mixing function is an issue, particularly |
| 2051 | * on 32-bit x86, but almost any function requires one state value and |
| 2052 | * one temporary. Instead, use a function designed for two state values |
| 2053 | * and no temporaries. |
| 2054 | * |
| 2055 | * This function cannot create a collision in only two iterations, so |
| 2056 | * we have two iterations to achieve avalanche. In those two iterations, |
| 2057 | * we have six layers of mixing, which is enough to spread one bit's |
| 2058 | * influence out to 2^6 = 64 state bits. |
| 2059 | * |
| 2060 | * Rotate constants are scored by considering either 64 one-bit input |
| 2061 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the |
| 2062 | * probability of that delta causing a change to each of the 128 output |
| 2063 | * bits, using a sample of random initial states. |
| 2064 | * |
| 2065 | * The Shannon entropy of the computed probabilities is then summed |
| 2066 | * to produce a score. Ideally, any input change has a 50% chance of |
| 2067 | * toggling any given output bit. |
| 2068 | * |
| 2069 | * Mixing scores (in bits) for (12,45): |
| 2070 | * Input delta: 1-bit 2-bit |
| 2071 | * 1 round: 713.3 42542.6 |
| 2072 | * 2 rounds: 2753.7 140389.8 |
| 2073 | * 3 rounds: 5954.1 233458.2 |
| 2074 | * 4 rounds: 7862.6 256672.2 |
| 2075 | * Perfect: 8192 258048 |
| 2076 | * (64*128) (64*63/2 * 128) |
| 2077 | */ |
| 2078 | #define HASH_MIX(x, y, a) \ |
| 2079 | ( x ^= (a), \ |
| 2080 | y ^= x, x = rol64(x,12),\ |
| 2081 | x += y, y = rol64(y,45),\ |
| 2082 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2083 | |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2084 | /* |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2085 | * Fold two longs into one 32-bit hash value. This must be fast, but |
| 2086 | * latency isn't quite as critical, as there is a fair bit of additional |
| 2087 | * work done before the hash value is used. |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2088 | */ |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2089 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2090 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2091 | y ^= x * GOLDEN_RATIO_64; |
| 2092 | y *= GOLDEN_RATIO_64; |
| 2093 | return y >> 32; |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2094 | } |
| 2095 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2096 | #else /* 32-bit case */ |
| 2097 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2098 | /* |
| 2099 | * Mixing scores (in bits) for (7,20): |
| 2100 | * Input delta: 1-bit 2-bit |
| 2101 | * 1 round: 330.3 9201.6 |
| 2102 | * 2 rounds: 1246.4 25475.4 |
| 2103 | * 3 rounds: 1907.1 31295.1 |
| 2104 | * 4 rounds: 2042.3 31718.6 |
| 2105 | * Perfect: 2048 31744 |
| 2106 | * (32*64) (32*31/2 * 64) |
| 2107 | */ |
| 2108 | #define HASH_MIX(x, y, a) \ |
| 2109 | ( x ^= (a), \ |
| 2110 | y ^= x, x = rol32(x, 7),\ |
| 2111 | x += y, y = rol32(y,20),\ |
| 2112 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2113 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2114 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2115 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2116 | /* Use arch-optimized multiply if one exists */ |
| 2117 | return __hash_32(y ^ __hash_32(x)); |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2118 | } |
| 2119 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2120 | #endif |
| 2121 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2122 | /* |
| 2123 | * Return the hash of a string of known length. This is carfully |
| 2124 | * designed to match hash_name(), which is the more critical function. |
| 2125 | * In particular, we must end by hashing a final word containing 0..7 |
| 2126 | * payload bytes, to match the way that hash_name() iterates until it |
| 2127 | * finds the delimiter after the name. |
| 2128 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2129 | 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] | 2130 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2131 | unsigned long a, x = 0, y = (unsigned long)salt; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2132 | |
| 2133 | for (;;) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2134 | if (!len) |
| 2135 | goto done; |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2136 | a = load_unaligned_zeropad(name); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2137 | if (len < sizeof(unsigned long)) |
| 2138 | break; |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2139 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2140 | name += sizeof(unsigned long); |
| 2141 | len -= sizeof(unsigned long); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2142 | } |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2143 | x ^= a & bytemask_from_count(len); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2144 | done: |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2145 | return fold_hash(x, y); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2146 | } |
| 2147 | EXPORT_SYMBOL(full_name_hash); |
| 2148 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2149 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2150 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2151 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2152 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
| 2153 | unsigned long adata, mask, len; |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2154 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
| 2155 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2156 | len = 0; |
| 2157 | goto inside; |
| 2158 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2159 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2160 | HASH_MIX(x, y, a); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2161 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2162 | inside: |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2163 | a = load_unaligned_zeropad(name+len); |
| 2164 | } while (!has_zero(a, &adata, &constants)); |
| 2165 | |
| 2166 | adata = prep_zero_mask(a, adata, &constants); |
| 2167 | mask = create_zero_mask(adata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2168 | x ^= a & zero_bytemask(mask); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2169 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2170 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2171 | } |
| 2172 | EXPORT_SYMBOL(hashlen_string); |
| 2173 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2174 | /* |
| 2175 | * Calculate the length and hash of the path component, and |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2176 | * return the "hash_len" as the result. |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2177 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2178 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2179 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2180 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
| 2181 | unsigned long adata, bdata, mask, len; |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2182 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2183 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2184 | len = 0; |
| 2185 | goto inside; |
| 2186 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2187 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2188 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2189 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2190 | inside: |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2191 | a = load_unaligned_zeropad(name+len); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2192 | b = a ^ REPEAT_BYTE('/'); |
| 2193 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2194 | |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2195 | adata = prep_zero_mask(a, adata, &constants); |
| 2196 | bdata = prep_zero_mask(b, bdata, &constants); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2197 | mask = create_zero_mask(adata | bdata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2198 | x ^= a & zero_bytemask(mask); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2199 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2200 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2201 | } |
| 2202 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2203 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2204 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2205 | /* Return the hash of a string of known length */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2206 | 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] | 2207 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2208 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2209 | while (len--) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2210 | hash = partial_name_hash((unsigned char)*name++, hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2211 | return end_name_hash(hash); |
| 2212 | } |
Linus Torvalds | ae942ae | 2012-03-02 19:40:57 -0800 | [diff] [blame] | 2213 | EXPORT_SYMBOL(full_name_hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2214 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2215 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2216 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2217 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2218 | unsigned long hash = init_name_hash(salt); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2219 | unsigned long len = 0, c; |
| 2220 | |
| 2221 | c = (unsigned char)*name; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2222 | while (c) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2223 | len++; |
| 2224 | hash = partial_name_hash(c, hash); |
| 2225 | c = (unsigned char)name[len]; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2226 | } |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2227 | return hashlen_create(end_name_hash(hash), len); |
| 2228 | } |
George Spelvin | f2a031b | 2016-05-29 01:26:41 -0400 | [diff] [blame] | 2229 | EXPORT_SYMBOL(hashlen_string); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2230 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 2231 | /* |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2232 | * We know there's a real path component here of at least |
| 2233 | * one character. |
| 2234 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2235 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2236 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2237 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2238 | unsigned long len = 0, c; |
| 2239 | |
| 2240 | c = (unsigned char)*name; |
| 2241 | do { |
| 2242 | len++; |
| 2243 | hash = partial_name_hash(c, hash); |
| 2244 | c = (unsigned char)name[len]; |
| 2245 | } while (c && c != '/'); |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2246 | return hashlen_create(end_name_hash(hash), len); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2247 | } |
| 2248 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2249 | #endif |
| 2250 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2251 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2253 | * This is the basic name resolution function, turning a pathname into |
| 2254 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2256 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 2257 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | */ |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 2259 | static int link_path_walk(const char *name, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | { |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2261 | int depth = 0; // depth <= nd->depth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | int err; |
Al Viro | 32cd746 | 2015-04-18 20:30:49 -0400 | [diff] [blame] | 2263 | |
Al Viro | b4c0353 | 2020-01-19 11:44:51 -0500 | [diff] [blame] | 2264 | nd->last_type = LAST_ROOT; |
Al Viro | c108837e | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2265 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2266 | if (IS_ERR(name)) |
| 2267 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | while (*name=='/') |
| 2269 | name++; |
Al Viro | 1a97d89 | 2020-09-19 17:55:58 +0100 | [diff] [blame] | 2270 | if (!*name) { |
| 2271 | nd->dir_mode = 0; // short-circuit the 'hardening' idiocy |
Al Viro | 9e18f10 | 2015-04-18 20:44:34 -0400 | [diff] [blame] | 2272 | return 0; |
Al Viro | 1a97d89 | 2020-09-19 17:55:58 +0100 | [diff] [blame] | 2273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | /* At this point we know we have a real path component. */ |
| 2276 | for(;;) { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2277 | struct user_namespace *mnt_userns; |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2278 | const char *link; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2279 | u64 hash_len; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2280 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2282 | mnt_userns = mnt_user_ns(nd->path.mnt); |
| 2283 | err = may_lookup(mnt_userns, nd); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2284 | if (err) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2285 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2287 | hash_len = hash_name(nd->path.dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2289 | type = LAST_NORM; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2290 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2291 | case 2: |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2292 | if (name[1] == '.') { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2293 | type = LAST_DOTDOT; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2294 | nd->state |= ND_JUMPED; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2295 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2296 | break; |
| 2297 | case 1: |
| 2298 | type = LAST_DOT; |
| 2299 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2300 | if (likely(type == LAST_NORM)) { |
| 2301 | struct dentry *parent = nd->path.dentry; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2302 | nd->state &= ~ND_JUMPED; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2303 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
James Hogan | a060dc5 | 2014-09-16 13:07:35 +0100 | [diff] [blame] | 2304 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 2305 | err = parent->d_op->d_hash(parent, &this); |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2306 | if (err < 0) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2307 | return err; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2308 | hash_len = this.hash_len; |
| 2309 | name = this.name; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2310 | } |
| 2311 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2312 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2313 | nd->last.hash_len = hash_len; |
| 2314 | nd->last.name = name; |
Al Viro | 5f4a6a6 | 2013-01-24 18:04:22 -0500 | [diff] [blame] | 2315 | nd->last_type = type; |
| 2316 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2317 | name += hashlen_len(hash_len); |
| 2318 | if (!*name) |
Al Viro | bdf6cbf | 2015-04-18 20:21:40 -0400 | [diff] [blame] | 2319 | goto OK; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2320 | /* |
| 2321 | * If it wasn't NUL, we know it was '/'. Skip that |
| 2322 | * slash, and continue until no more slashes. |
| 2323 | */ |
| 2324 | do { |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2325 | name++; |
| 2326 | } while (unlikely(*name == '/')); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2327 | if (unlikely(!*name)) { |
| 2328 | OK: |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2329 | /* pathname or trailing symlink, done */ |
Al Viro | c108837e | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2330 | if (!depth) { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2331 | nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode); |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 2332 | nd->dir_mode = nd->inode->i_mode; |
Al Viro | c108837e | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2333 | nd->flags &= ~LOOKUP_PARENT; |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2334 | return 0; |
Al Viro | c108837e | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2335 | } |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2336 | /* last component of nested symlink */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2337 | name = nd->stack[--depth].name; |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2338 | link = walk_component(nd, 0); |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 2339 | } else { |
| 2340 | /* not the last component */ |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2341 | link = walk_component(nd, WALK_MORE); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2342 | } |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2343 | if (unlikely(link)) { |
| 2344 | if (IS_ERR(link)) |
| 2345 | return PTR_ERR(link); |
| 2346 | /* a symlink to follow */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2347 | nd->stack[depth++].name = name; |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2348 | name = link; |
| 2349 | continue; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2350 | } |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2351 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
| 2352 | if (nd->flags & LOOKUP_RCU) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 2353 | if (!try_to_unlazy(nd)) |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2354 | return -ECHILD; |
| 2355 | } |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2356 | return -ENOTDIR; |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2361 | /* must be paired with terminate_walk() */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2362 | static const char *path_init(struct nameidata *nd, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2364 | int error; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2365 | const char *s = nd->name->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | |
Jens Axboe | 6c6ec2b | 2020-12-17 09:19:09 -0700 | [diff] [blame] | 2367 | /* LOOKUP_CACHED requires RCU, ask caller to retry */ |
| 2368 | if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) |
| 2369 | return ERR_PTR(-EAGAIN); |
| 2370 | |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2371 | if (!*s) |
| 2372 | flags &= ~LOOKUP_RCU; |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2373 | if (flags & LOOKUP_RCU) |
| 2374 | rcu_read_lock(); |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2375 | |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2376 | nd->flags = flags; |
| 2377 | nd->state |= ND_JUMPED; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2378 | |
| 2379 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); |
| 2380 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); |
| 2381 | smp_rmb(); |
| 2382 | |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2383 | if (nd->state & ND_ROOT_PRESET) { |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2384 | struct dentry *root = nd->root.dentry; |
| 2385 | struct inode *inode = root->d_inode; |
Al Viro | 9389386 | 2017-04-15 17:29:14 -0400 | [diff] [blame] | 2386 | if (*s && unlikely(!d_can_lookup(root))) |
| 2387 | return ERR_PTR(-ENOTDIR); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2388 | nd->path = nd->root; |
| 2389 | nd->inode = inode; |
| 2390 | if (flags & LOOKUP_RCU) { |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2391 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 8f47a016 | 2015-05-09 19:02:01 -0400 | [diff] [blame] | 2392 | nd->root_seq = nd->seq; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2393 | } else { |
| 2394 | path_get(&nd->path); |
| 2395 | } |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2396 | return s; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2397 | } |
| 2398 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 2399 | nd->root.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2401 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
| 2402 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2403 | error = nd_jump_root(nd); |
| 2404 | if (unlikely(error)) |
| 2405 | return ERR_PTR(error); |
| 2406 | return s; |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /* Relative pathname -- get the starting-point it is relative to. */ |
| 2410 | if (nd->dfd == AT_FDCWD) { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2411 | if (flags & LOOKUP_RCU) { |
| 2412 | struct fs_struct *fs = current->fs; |
| 2413 | unsigned seq; |
| 2414 | |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2415 | do { |
| 2416 | seq = read_seqcount_begin(&fs->seq); |
| 2417 | nd->path = fs->pwd; |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2418 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2419 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 2420 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 2421 | } else { |
| 2422 | get_fs_pwd(current->fs, &nd->path); |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2423 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2424 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2425 | } else { |
Jeff Layton | 582aa64 | 2012-12-11 08:56:16 -0500 | [diff] [blame] | 2426 | /* Caller must check execute permissions on the starting path component */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2427 | struct fd f = fdget_raw(nd->dfd); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2428 | struct dentry *dentry; |
| 2429 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2430 | if (!f.file) |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2431 | return ERR_PTR(-EBADF); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2432 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2433 | dentry = f.file->f_path.dentry; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2434 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2435 | if (*s && unlikely(!d_can_lookup(dentry))) { |
| 2436 | fdput(f); |
| 2437 | return ERR_PTR(-ENOTDIR); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 2438 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2439 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2440 | nd->path = f.file->f_path; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2441 | if (flags & LOOKUP_RCU) { |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2442 | nd->inode = nd->path.dentry->d_inode; |
| 2443 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2444 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2445 | path_get(&nd->path); |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2446 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2447 | } |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2448 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | } |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2450 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 2451 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
| 2452 | if (flags & LOOKUP_IS_SCOPED) { |
| 2453 | nd->root = nd->path; |
| 2454 | if (flags & LOOKUP_RCU) { |
| 2455 | nd->root_seq = nd->seq; |
| 2456 | } else { |
| 2457 | path_get(&nd->root); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2458 | nd->state |= ND_ROOT_GRABBED; |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | return s; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2462 | } |
| 2463 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2464 | static inline const char *lookup_last(struct nameidata *nd) |
Al Viro | 95fa25d | 2015-04-22 13:46:57 -0400 | [diff] [blame] | 2465 | { |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2466 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) |
| 2467 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 2468 | |
Al Viro | c108837e | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2469 | return walk_component(nd, WALK_TRAILING); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2470 | } |
| 2471 | |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2472 | static int handle_lookup_down(struct nameidata *nd) |
| 2473 | { |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 2474 | if (!(nd->flags & LOOKUP_RCU)) |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 2475 | dget(nd->path.dentry); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 2476 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
| 2477 | nd->path.dentry, nd->inode, nd->seq)); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2478 | } |
| 2479 | |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2480 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2481 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2482 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2483 | const char *s = path_init(nd, flags); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2484 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2485 | |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2486 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2487 | err = handle_lookup_down(nd); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 2488 | if (unlikely(err < 0)) |
| 2489 | s = ERR_PTR(err); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2490 | } |
| 2491 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2492 | while (!(err = link_path_walk(s, nd)) && |
| 2493 | (s = lookup_last(nd)) != NULL) |
| 2494 | ; |
Al Viro | 4f0ed93 | 2021-04-06 19:46:51 -0400 | [diff] [blame] | 2495 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
| 2496 | err = handle_lookup_down(nd); |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 2497 | nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... |
Al Viro | 4f0ed93 | 2021-04-06 19:46:51 -0400 | [diff] [blame] | 2498 | } |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2499 | if (!err) |
| 2500 | err = complete_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2501 | |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 2502 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
| 2503 | if (!d_can_lookup(nd->path.dentry)) |
Al Viro | bd23a53 | 2011-03-23 09:56:30 -0400 | [diff] [blame] | 2504 | err = -ENOTDIR; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 2505 | if (!err) { |
| 2506 | *path = nd->path; |
| 2507 | nd->path.mnt = NULL; |
| 2508 | nd->path.dentry = NULL; |
| 2509 | } |
| 2510 | terminate_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2511 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2512 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2513 | |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2514 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2515 | struct path *path, struct path *root) |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2516 | { |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 2517 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2518 | struct nameidata nd; |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2519 | if (IS_ERR(name)) |
| 2520 | return PTR_ERR(name); |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 2521 | set_nameidata(&nd, dfd, name, root); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2522 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2523 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2524 | retval = path_lookupat(&nd, flags, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2525 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2526 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2527 | |
| 2528 | if (likely(!retval)) |
Al Viro | 161aff1 | 2020-01-11 22:52:26 -0500 | [diff] [blame] | 2529 | audit_inode(name, path->dentry, |
| 2530 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2531 | restore_nameidata(); |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 2532 | return retval; |
| 2533 | } |
| 2534 | |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2535 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2536 | static int path_parentat(struct nameidata *nd, unsigned flags, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2537 | struct path *parent) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2538 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2539 | const char *s = path_init(nd, flags); |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2540 | int err = link_path_walk(s, nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2541 | if (!err) |
| 2542 | err = complete_walk(nd); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2543 | if (!err) { |
| 2544 | *parent = nd->path; |
| 2545 | nd->path.mnt = NULL; |
| 2546 | nd->path.dentry = NULL; |
| 2547 | } |
| 2548 | terminate_walk(nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2549 | return err; |
| 2550 | } |
| 2551 | |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2552 | /* Note: this does not consume "name" */ |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 2553 | static int filename_parentat(int dfd, struct filename *name, |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2554 | unsigned int flags, struct path *parent, |
| 2555 | struct qstr *last, int *type) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2556 | { |
| 2557 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2558 | struct nameidata nd; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2559 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2560 | if (IS_ERR(name)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2561 | return PTR_ERR(name); |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 2562 | set_nameidata(&nd, dfd, name, NULL); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2563 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2564 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2565 | retval = path_parentat(&nd, flags, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2566 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2567 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2568 | if (likely(!retval)) { |
| 2569 | *last = nd.last; |
| 2570 | *type = nd.last_type; |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 2571 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2572 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2573 | restore_nameidata(); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2574 | return retval; |
| 2575 | } |
| 2576 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2577 | /* does lookup, returns the object with parent locked */ |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2578 | static struct dentry *__kern_path_locked(struct filename *name, struct path *path) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2579 | { |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2580 | struct dentry *d; |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2581 | struct qstr last; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2582 | int type, error; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2583 | |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 2584 | error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 2585 | if (error) |
| 2586 | return ERR_PTR(error); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2587 | if (unlikely(type != LAST_NORM)) { |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2588 | path_put(path); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2589 | return ERR_PTR(-EINVAL); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2590 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2591 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2592 | d = __lookup_hash(&last, path->dentry, 0); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2593 | if (IS_ERR(d)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2594 | inode_unlock(path->dentry->d_inode); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2595 | path_put(path); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2596 | } |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2597 | return d; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2598 | } |
| 2599 | |
Stephen Brennan | 0766ec8 | 2021-09-01 10:51:41 -0700 | [diff] [blame] | 2600 | struct dentry *kern_path_locked(const char *name, struct path *path) |
| 2601 | { |
| 2602 | struct filename *filename = getname_kernel(name); |
| 2603 | struct dentry *res = __kern_path_locked(filename, path); |
| 2604 | |
| 2605 | putname(filename); |
| 2606 | return res; |
| 2607 | } |
| 2608 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2609 | int kern_path(const char *name, unsigned int flags, struct path *path) |
| 2610 | { |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2611 | struct filename *filename = getname_kernel(name); |
| 2612 | int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL); |
| 2613 | |
| 2614 | putname(filename); |
| 2615 | return ret; |
| 2616 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2617 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2618 | EXPORT_SYMBOL(kern_path); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2619 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2620 | /** |
| 2621 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 2622 | * @dentry: pointer to dentry of the base directory |
| 2623 | * @mnt: pointer to vfs mount of the base directory |
| 2624 | * @name: pointer to file name |
| 2625 | * @flags: lookup flags |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2626 | * @path: pointer to struct path to fill |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2627 | */ |
| 2628 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 2629 | const char *name, unsigned int flags, |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2630 | struct path *path) |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2631 | { |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2632 | struct filename *filename; |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2633 | struct path root = {.mnt = mnt, .dentry = dentry}; |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2634 | int ret; |
| 2635 | |
| 2636 | filename = getname_kernel(name); |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2637 | /* the first argument of filename_lookup() is ignored with root */ |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2638 | ret = filename_lookup(AT_FDCWD, filename, flags, path, &root); |
| 2639 | putname(filename); |
| 2640 | return ret; |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2641 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2642 | EXPORT_SYMBOL(vfs_path_lookup); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2643 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2644 | static int lookup_one_common(struct user_namespace *mnt_userns, |
| 2645 | const char *name, struct dentry *base, int len, |
| 2646 | struct qstr *this) |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2647 | { |
| 2648 | this->name = name; |
| 2649 | this->len = len; |
| 2650 | this->hash = full_name_hash(base, name, len); |
| 2651 | if (!len) |
| 2652 | return -EACCES; |
| 2653 | |
| 2654 | if (unlikely(name[0] == '.')) { |
| 2655 | if (len < 2 || (len == 2 && name[1] == '.')) |
| 2656 | return -EACCES; |
| 2657 | } |
| 2658 | |
| 2659 | while (len--) { |
| 2660 | unsigned int c = *(const unsigned char *)name++; |
| 2661 | if (c == '/' || c == '\0') |
| 2662 | return -EACCES; |
| 2663 | } |
| 2664 | /* |
| 2665 | * See if the low-level filesystem might want |
| 2666 | * to use its own hash.. |
| 2667 | */ |
| 2668 | if (base->d_flags & DCACHE_OP_HASH) { |
| 2669 | int err = base->d_op->d_hash(base, this); |
| 2670 | if (err < 0) |
| 2671 | return err; |
| 2672 | } |
| 2673 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2674 | return inode_permission(mnt_userns, base->d_inode, MAY_EXEC); |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2675 | } |
| 2676 | |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2677 | /** |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2678 | * try_lookup_one_len - filesystem helper to lookup single pathname component |
| 2679 | * @name: pathname component to lookup |
| 2680 | * @base: base directory to lookup from |
| 2681 | * @len: maximum length @len should be interpreted to |
| 2682 | * |
| 2683 | * Look up a dentry by name in the dcache, returning NULL if it does not |
| 2684 | * currently exist. The function does not try to create a dentry. |
| 2685 | * |
| 2686 | * Note that this routine is purely a helper for filesystem usage and should |
| 2687 | * not be called by generic code. |
| 2688 | * |
| 2689 | * The caller must hold base->i_mutex. |
| 2690 | */ |
| 2691 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) |
| 2692 | { |
| 2693 | struct qstr this; |
| 2694 | int err; |
| 2695 | |
| 2696 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
| 2697 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2698 | err = lookup_one_common(&init_user_ns, name, base, len, &this); |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2699 | if (err) |
| 2700 | return ERR_PTR(err); |
| 2701 | |
| 2702 | return lookup_dcache(&this, base, 0); |
| 2703 | } |
| 2704 | EXPORT_SYMBOL(try_lookup_one_len); |
| 2705 | |
| 2706 | /** |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2707 | * lookup_one_len - filesystem helper to lookup single pathname component |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2708 | * @name: pathname component to lookup |
| 2709 | * @base: base directory to lookup from |
| 2710 | * @len: maximum length @len should be interpreted to |
| 2711 | * |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2712 | * 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] | 2713 | * not be called by generic code. |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2714 | * |
| 2715 | * The caller must hold base->i_mutex. |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2716 | */ |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2717 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
| 2718 | { |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2719 | struct dentry *dentry; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2720 | struct qstr this; |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2721 | int err; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2722 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2723 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2724 | |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2725 | err = lookup_one_common(&init_user_ns, name, base, len, &this); |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2726 | if (err) |
| 2727 | return ERR_PTR(err); |
| 2728 | |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2729 | dentry = lookup_dcache(&this, base, 0); |
| 2730 | return dentry ? dentry : __lookup_slow(&this, base, 0); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2731 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2732 | EXPORT_SYMBOL(lookup_one_len); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2733 | |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2734 | /** |
Christian Brauner | c2fd68b | 2021-07-27 12:48:40 +0200 | [diff] [blame] | 2735 | * lookup_one - filesystem helper to lookup single pathname component |
| 2736 | * @mnt_userns: user namespace of the mount the lookup is performed from |
| 2737 | * @name: pathname component to lookup |
| 2738 | * @base: base directory to lookup from |
| 2739 | * @len: maximum length @len should be interpreted to |
| 2740 | * |
| 2741 | * Note that this routine is purely a helper for filesystem usage and should |
| 2742 | * not be called by generic code. |
| 2743 | * |
| 2744 | * The caller must hold base->i_mutex. |
| 2745 | */ |
| 2746 | struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name, |
| 2747 | struct dentry *base, int len) |
| 2748 | { |
| 2749 | struct dentry *dentry; |
| 2750 | struct qstr this; |
| 2751 | int err; |
| 2752 | |
| 2753 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
| 2754 | |
| 2755 | err = lookup_one_common(mnt_userns, name, base, len, &this); |
| 2756 | if (err) |
| 2757 | return ERR_PTR(err); |
| 2758 | |
| 2759 | dentry = lookup_dcache(&this, base, 0); |
| 2760 | return dentry ? dentry : __lookup_slow(&this, base, 0); |
| 2761 | } |
| 2762 | EXPORT_SYMBOL(lookup_one); |
| 2763 | |
| 2764 | /** |
Christian Brauner | 00675017e | 2022-04-04 12:51:40 +0200 | [diff] [blame] | 2765 | * lookup_one_unlocked - filesystem helper to lookup single pathname component |
| 2766 | * @mnt_userns: idmapping of the mount the lookup is performed from |
| 2767 | * @name: pathname component to lookup |
| 2768 | * @base: base directory to lookup from |
| 2769 | * @len: maximum length @len should be interpreted to |
| 2770 | * |
| 2771 | * Note that this routine is purely a helper for filesystem usage and should |
| 2772 | * not be called by generic code. |
| 2773 | * |
| 2774 | * Unlike lookup_one_len, it should be called without the parent |
| 2775 | * i_mutex held, and will take the i_mutex itself if necessary. |
| 2776 | */ |
| 2777 | struct dentry *lookup_one_unlocked(struct user_namespace *mnt_userns, |
| 2778 | const char *name, struct dentry *base, |
| 2779 | int len) |
| 2780 | { |
| 2781 | struct qstr this; |
| 2782 | int err; |
| 2783 | struct dentry *ret; |
| 2784 | |
| 2785 | err = lookup_one_common(mnt_userns, name, base, len, &this); |
| 2786 | if (err) |
| 2787 | return ERR_PTR(err); |
| 2788 | |
| 2789 | ret = lookup_dcache(&this, base, 0); |
| 2790 | if (!ret) |
| 2791 | ret = lookup_slow(&this, base, 0); |
| 2792 | return ret; |
| 2793 | } |
| 2794 | EXPORT_SYMBOL(lookup_one_unlocked); |
| 2795 | |
| 2796 | /** |
| 2797 | * lookup_one_positive_unlocked - filesystem helper to lookup single |
| 2798 | * pathname component |
| 2799 | * @mnt_userns: idmapping of the mount the lookup is performed from |
| 2800 | * @name: pathname component to lookup |
| 2801 | * @base: base directory to lookup from |
| 2802 | * @len: maximum length @len should be interpreted to |
| 2803 | * |
| 2804 | * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns |
| 2805 | * known positive or ERR_PTR(). This is what most of the users want. |
| 2806 | * |
| 2807 | * Note that pinned negative with unlocked parent _can_ become positive at any |
| 2808 | * time, so callers of lookup_one_unlocked() need to be very careful; pinned |
| 2809 | * positives have >d_inode stable, so this one avoids such problems. |
| 2810 | * |
| 2811 | * Note that this routine is purely a helper for filesystem usage and should |
| 2812 | * not be called by generic code. |
| 2813 | * |
| 2814 | * The helper should be called without i_mutex held. |
| 2815 | */ |
| 2816 | struct dentry *lookup_one_positive_unlocked(struct user_namespace *mnt_userns, |
| 2817 | const char *name, |
| 2818 | struct dentry *base, int len) |
| 2819 | { |
| 2820 | struct dentry *ret = lookup_one_unlocked(mnt_userns, name, base, len); |
| 2821 | |
| 2822 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
| 2823 | dput(ret); |
| 2824 | ret = ERR_PTR(-ENOENT); |
| 2825 | } |
| 2826 | return ret; |
| 2827 | } |
| 2828 | EXPORT_SYMBOL(lookup_one_positive_unlocked); |
| 2829 | |
| 2830 | /** |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2831 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component |
| 2832 | * @name: pathname component to lookup |
| 2833 | * @base: base directory to lookup from |
| 2834 | * @len: maximum length @len should be interpreted to |
| 2835 | * |
| 2836 | * Note that this routine is purely a helper for filesystem usage and should |
| 2837 | * not be called by generic code. |
| 2838 | * |
| 2839 | * Unlike lookup_one_len, it should be called without the parent |
| 2840 | * i_mutex held, and will take the i_mutex itself if necessary. |
| 2841 | */ |
| 2842 | struct dentry *lookup_one_len_unlocked(const char *name, |
| 2843 | struct dentry *base, int len) |
| 2844 | { |
Christian Brauner | 00675017e | 2022-04-04 12:51:40 +0200 | [diff] [blame] | 2845 | return lookup_one_unlocked(&init_user_ns, name, base, len); |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2846 | } |
| 2847 | EXPORT_SYMBOL(lookup_one_len_unlocked); |
| 2848 | |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2849 | /* |
| 2850 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) |
| 2851 | * on negatives. Returns known positive or ERR_PTR(); that's what |
| 2852 | * most of the users want. Note that pinned negative with unlocked parent |
| 2853 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() |
| 2854 | * need to be very careful; pinned positives have ->d_inode stable, so |
| 2855 | * this one avoids such problems. |
| 2856 | */ |
| 2857 | struct dentry *lookup_positive_unlocked(const char *name, |
| 2858 | struct dentry *base, int len) |
| 2859 | { |
Christian Brauner | 00675017e | 2022-04-04 12:51:40 +0200 | [diff] [blame] | 2860 | return lookup_one_positive_unlocked(&init_user_ns, name, base, len); |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2861 | } |
| 2862 | EXPORT_SYMBOL(lookup_positive_unlocked); |
| 2863 | |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2864 | #ifdef CONFIG_UNIX98_PTYS |
| 2865 | int path_pts(struct path *path) |
| 2866 | { |
| 2867 | /* Find something mounted on "pts" in the same directory as |
| 2868 | * the input path. |
| 2869 | */ |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2870 | struct dentry *parent = dget_parent(path->dentry); |
| 2871 | struct dentry *child; |
Al Viro | 19f6028 | 2020-02-26 20:09:37 -0500 | [diff] [blame] | 2872 | struct qstr this = QSTR_INIT("pts", 3); |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2873 | |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2874 | if (unlikely(!path_connected(path->mnt, parent))) { |
| 2875 | dput(parent); |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2876 | return -ENOENT; |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2877 | } |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2878 | dput(path->dentry); |
| 2879 | path->dentry = parent; |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2880 | child = d_hash_and_lookup(parent, &this); |
| 2881 | if (!child) |
| 2882 | return -ENOENT; |
| 2883 | |
| 2884 | path->dentry = child; |
| 2885 | dput(parent); |
Al Viro | 19f6028 | 2020-02-26 20:09:37 -0500 | [diff] [blame] | 2886 | follow_down(path); |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2887 | return 0; |
| 2888 | } |
| 2889 | #endif |
| 2890 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2891 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
| 2892 | struct path *path, int *empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | { |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 2894 | struct filename *filename = getname_flags(name, flags, empty); |
| 2895 | int ret = filename_lookup(dfd, filename, flags, path, NULL); |
| 2896 | |
| 2897 | putname(filename); |
| 2898 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | } |
Al Viro | b853a16 | 2015-05-13 09:12:02 -0400 | [diff] [blame] | 2900 | EXPORT_SYMBOL(user_path_at_empty); |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2901 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2902 | int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir, |
| 2903 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | { |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2905 | kuid_t fsuid = current_fsuid(); |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2906 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2907 | if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | return 0; |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2909 | if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | return 0; |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2911 | return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | } |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2913 | EXPORT_SYMBOL(__check_sticky); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | |
| 2915 | /* |
| 2916 | * Check whether we can remove a link victim from directory dir, check |
| 2917 | * whether the type of victim is right. |
| 2918 | * 1. We can't do it if dir is read-only (done in permission()) |
| 2919 | * 2. We should have write and exec permissions on dir |
| 2920 | * 3. We can't remove anything from append-only dir |
| 2921 | * 4. We can't do anything with immutable dir (done in permission()) |
| 2922 | * 5. If the sticky bit on dir is set we should either |
| 2923 | * a. be owner of dir, or |
| 2924 | * b. be owner of victim, or |
| 2925 | * c. have CAP_FOWNER capability |
| 2926 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 2927 | * links pointing to it. |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2928 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
| 2929 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 2930 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 2931 | * 10. We can't remove a root or mountpoint. |
| 2932 | * 11. We don't allow removal of NFS sillyrenamed files; it's handled by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | * nfs_async_unlink(). |
| 2934 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2935 | static int may_delete(struct user_namespace *mnt_userns, struct inode *dir, |
| 2936 | struct dentry *victim, bool isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | { |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 2938 | struct inode *inode = d_backing_inode(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | int error; |
| 2940 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2941 | if (d_is_negative(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | return -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2943 | BUG_ON(!inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | |
| 2945 | BUG_ON(victim->d_parent->d_inode != dir); |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 2946 | |
| 2947 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2948 | if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || |
| 2949 | !gid_valid(i_gid_into_mnt(mnt_userns, inode))) |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 2950 | return -EOVERFLOW; |
| 2951 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 2952 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2954 | error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | if (error) |
| 2956 | return error; |
| 2957 | if (IS_APPEND(dir)) |
| 2958 | return -EPERM; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2959 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2960 | if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) || |
| 2961 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || |
| 2962 | HAS_UNMAPPED_ID(mnt_userns, inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | return -EPERM; |
| 2964 | if (isdir) { |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2965 | if (!d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | return -ENOTDIR; |
| 2967 | if (IS_ROOT(victim)) |
| 2968 | return -EBUSY; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2969 | } else if (d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | return -EISDIR; |
| 2971 | if (IS_DEADDIR(dir)) |
| 2972 | return -ENOENT; |
| 2973 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 2974 | return -EBUSY; |
| 2975 | return 0; |
| 2976 | } |
| 2977 | |
| 2978 | /* Check whether we can create an object with dentry child in directory |
| 2979 | * dir. |
| 2980 | * 1. We can't do it if child already exists (open has special treatment for |
| 2981 | * this case, but since we are inlined it's OK) |
| 2982 | * 2. We can't do it if dir is read-only (done in permission()) |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2983 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
| 2984 | * 4. We should have write and exec permissions on dir |
| 2985 | * 5. We can't do it if dir is immutable (done in permission()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2987 | static inline int may_create(struct user_namespace *mnt_userns, |
| 2988 | struct inode *dir, struct dentry *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | { |
Jeff Layton | 14e972b | 2013-05-08 10:25:58 -0400 | [diff] [blame] | 2990 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | if (child->d_inode) |
| 2992 | return -EEXIST; |
| 2993 | if (IS_DEADDIR(dir)) |
| 2994 | return -ENOENT; |
Christian Brauner | 8e53891 | 2021-03-20 13:26:23 +0100 | [diff] [blame] | 2995 | if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2996 | return -EOVERFLOW; |
Christian Brauner | 8e53891 | 2021-03-20 13:26:23 +0100 | [diff] [blame] | 2997 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 2998 | return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | } |
| 3000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | /* |
| 3002 | * p1 and p2 should be directories on the same fs. |
| 3003 | */ |
| 3004 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 3005 | { |
| 3006 | struct dentry *p; |
| 3007 | |
| 3008 | if (p1 == p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3009 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | return NULL; |
| 3011 | } |
| 3012 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 3013 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 3015 | p = d_ancestor(p2, p1); |
| 3016 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3017 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
| 3018 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 3019 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | } |
| 3021 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 3022 | p = d_ancestor(p1, p2); |
| 3023 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3024 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 3025 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 3026 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3029 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 3030 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | return NULL; |
| 3032 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3033 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | |
| 3035 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 3036 | { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3037 | inode_unlock(p1->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | if (p1 != p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3039 | inode_unlock(p2->d_inode); |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 3040 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | } |
| 3042 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3043 | EXPORT_SYMBOL(unlock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3045 | /** |
| 3046 | * vfs_create - create new file |
| 3047 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3048 | * @dir: inode of @dentry |
| 3049 | * @dentry: pointer to dentry of the base directory |
| 3050 | * @mode: mode of the new file |
| 3051 | * @want_excl: whether the file must not yet exist |
| 3052 | * |
| 3053 | * Create a new file. |
| 3054 | * |
| 3055 | * If the inode has been found through an idmapped mount the user namespace of |
| 3056 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3057 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3058 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3059 | * raw inode simply passs init_user_ns. |
| 3060 | */ |
| 3061 | int vfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 3062 | struct dentry *dentry, umode_t mode, bool want_excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3064 | int error = may_create(mnt_userns, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | if (error) |
| 3066 | return error; |
| 3067 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3068 | if (!dir->i_op->create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 3070 | mode &= S_IALLUGO; |
| 3071 | mode |= S_IFREG; |
| 3072 | error = security_inode_create(dir, dentry, mode); |
| 3073 | if (error) |
| 3074 | return error; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3075 | error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3076 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3077 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | return error; |
| 3079 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3080 | EXPORT_SYMBOL(vfs_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 3082 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
| 3083 | int (*f)(struct dentry *, umode_t, void *), |
| 3084 | void *arg) |
| 3085 | { |
| 3086 | struct inode *dir = dentry->d_parent->d_inode; |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3087 | int error = may_create(&init_user_ns, dir, dentry); |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 3088 | if (error) |
| 3089 | return error; |
| 3090 | |
| 3091 | mode &= S_IALLUGO; |
| 3092 | mode |= S_IFREG; |
| 3093 | error = security_inode_create(dir, dentry, mode); |
| 3094 | if (error) |
| 3095 | return error; |
| 3096 | error = f(dentry, mode, arg); |
| 3097 | if (!error) |
| 3098 | fsnotify_create(dir, dentry); |
| 3099 | return error; |
| 3100 | } |
| 3101 | EXPORT_SYMBOL(vfs_mkobj); |
| 3102 | |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 3103 | bool may_open_dev(const struct path *path) |
| 3104 | { |
| 3105 | return !(path->mnt->mnt_flags & MNT_NODEV) && |
| 3106 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); |
| 3107 | } |
| 3108 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3109 | static int may_open(struct user_namespace *mnt_userns, const struct path *path, |
| 3110 | int acc_mode, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | { |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 3112 | struct dentry *dentry = path->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | struct inode *inode = dentry->d_inode; |
| 3114 | int error; |
| 3115 | |
| 3116 | if (!inode) |
| 3117 | return -ENOENT; |
| 3118 | |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3119 | switch (inode->i_mode & S_IFMT) { |
| 3120 | case S_IFLNK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | return -ELOOP; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3122 | case S_IFDIR: |
Kees Cook | fc4177b | 2020-08-14 17:30:14 -0700 | [diff] [blame] | 3123 | if (acc_mode & MAY_WRITE) |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3124 | return -EISDIR; |
Kees Cook | fc4177b | 2020-08-14 17:30:14 -0700 | [diff] [blame] | 3125 | if (acc_mode & MAY_EXEC) |
| 3126 | return -EACCES; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3127 | break; |
| 3128 | case S_IFBLK: |
| 3129 | case S_IFCHR: |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 3130 | if (!may_open_dev(path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | return -EACCES; |
Kees Cook | 633fb6a | 2020-08-11 18:36:26 -0700 | [diff] [blame] | 3132 | fallthrough; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3133 | case S_IFIFO: |
| 3134 | case S_IFSOCK: |
Kees Cook | 633fb6a | 2020-08-11 18:36:26 -0700 | [diff] [blame] | 3135 | if (acc_mode & MAY_EXEC) |
| 3136 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | flag &= ~O_TRUNC; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 3138 | break; |
Kees Cook | 0fd338b | 2020-08-11 18:36:30 -0700 | [diff] [blame] | 3139 | case S_IFREG: |
| 3140 | if ((acc_mode & MAY_EXEC) && path_noexec(path)) |
| 3141 | return -EACCES; |
| 3142 | break; |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 3143 | } |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 3144 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3145 | error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode); |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 3146 | if (error) |
| 3147 | return error; |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 3148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | /* |
| 3150 | * An append-only file must be opened in append mode for writing. |
| 3151 | */ |
| 3152 | if (IS_APPEND(inode)) { |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 3153 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3154 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | if (flag & O_TRUNC) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3156 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | /* O_NOATIME can only be set by the owner or superuser */ |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3160 | if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3161 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 3163 | return 0; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3166 | static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3167 | { |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 3168 | const struct path *path = &filp->f_path; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3169 | struct inode *inode = path->dentry->d_inode; |
| 3170 | int error = get_write_access(inode); |
| 3171 | if (error) |
| 3172 | return error; |
Jeff Layton | 482e000 | 2021-10-26 11:56:45 -0400 | [diff] [blame] | 3173 | |
Jeff Layton | f7e33bd | 2021-08-19 14:56:38 -0400 | [diff] [blame] | 3174 | error = security_path_truncate(path); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3175 | if (!error) { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3176 | error = do_truncate(mnt_userns, path->dentry, 0, |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3177 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 3178 | filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 3179 | } |
| 3180 | put_write_access(inode); |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 3181 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | } |
| 3183 | |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 3184 | static inline int open_to_namei_flags(int flag) |
| 3185 | { |
Al Viro | 8a5e929 | 2011-06-25 19:15:54 -0400 | [diff] [blame] | 3186 | if ((flag & O_ACCMODE) == 3) |
| 3187 | flag--; |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 3188 | return flag; |
| 3189 | } |
| 3190 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3191 | static int may_o_create(struct user_namespace *mnt_userns, |
| 3192 | const struct path *dir, struct dentry *dentry, |
| 3193 | umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3194 | { |
| 3195 | int error = security_path_mknod(dir, dentry, mode, 0); |
| 3196 | if (error) |
| 3197 | return error; |
| 3198 | |
Christian Brauner | 8e53891 | 2021-03-20 13:26:23 +0100 | [diff] [blame] | 3199 | if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns)) |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 3200 | return -EOVERFLOW; |
| 3201 | |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3202 | error = inode_permission(mnt_userns, dir->dentry->d_inode, |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 3203 | MAY_WRITE | MAY_EXEC); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3204 | if (error) |
| 3205 | return error; |
| 3206 | |
| 3207 | return security_inode_create(dir->dentry->d_inode, dentry, mode); |
| 3208 | } |
| 3209 | |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3210 | /* |
| 3211 | * Attempt to atomically look up, create and open a file from a negative |
| 3212 | * dentry. |
| 3213 | * |
| 3214 | * Returns 0 if successful. The file will have been created and attached to |
| 3215 | * @file by the filesystem calling finish_open(). |
| 3216 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3217 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
| 3218 | * be set. The caller will need to perform the open themselves. @path will |
| 3219 | * have been updated to point to the new dentry. This may be negative. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3220 | * |
| 3221 | * Returns an error code otherwise. |
| 3222 | */ |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3223 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
| 3224 | struct file *file, |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3225 | int open_flag, umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3226 | { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3227 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3228 | struct inode *dir = nd->path.dentry->d_inode; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3229 | int error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3230 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3231 | if (nd->flags & LOOKUP_DIRECTORY) |
| 3232 | open_flag |= O_DIRECTORY; |
| 3233 | |
Al Viro | 30d90494 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3234 | file->f_path.dentry = DENTRY_NOT_SET; |
| 3235 | file->f_path.mnt = nd->path.mnt; |
Al Viro | 0fb1ea0 | 2016-04-27 14:13:10 -0400 | [diff] [blame] | 3236 | error = dir->i_op->atomic_open(dir, dentry, file, |
Al Viro | 44907d7 | 2018-06-08 13:32:02 -0400 | [diff] [blame] | 3237 | open_to_namei_flags(open_flag), mode); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3238 | d_lookup_done(dentry); |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 3239 | if (!error) { |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 3240 | if (file->f_mode & FMODE_OPENED) { |
Al Viro | 6fb968c | 2020-01-26 09:53:19 -0500 | [diff] [blame] | 3241 | if (unlikely(dentry != file->f_path.dentry)) { |
| 3242 | dput(dentry); |
| 3243 | dentry = dget(file->f_path.dentry); |
| 3244 | } |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 3245 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3246 | error = -EIO; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 3247 | } else { |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 3248 | if (file->f_path.dentry) { |
| 3249 | dput(dentry); |
| 3250 | dentry = file->f_path.dentry; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 3251 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3252 | if (unlikely(d_is_negative(dentry))) |
Al Viro | a01e718 | 2016-06-07 21:53:51 -0400 | [diff] [blame] | 3253 | error = -ENOENT; |
Sage Weil | 62b2ce9 | 2012-08-15 13:30:12 -0700 | [diff] [blame] | 3254 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3255 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3256 | if (error) { |
| 3257 | dput(dentry); |
| 3258 | dentry = ERR_PTR(error); |
| 3259 | } |
| 3260 | return dentry; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3261 | } |
| 3262 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3263 | /* |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3264 | * Look up and maybe create and open the last component. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3265 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3266 | * Must be called with parent locked (exclusive in O_CREAT case). |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3267 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3268 | * Returns 0 on success, that is, if |
| 3269 | * the file was successfully atomically created (if necessary) and opened, or |
| 3270 | * the file was not completely opened at this time, though lookups and |
| 3271 | * creations were performed. |
| 3272 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. |
| 3273 | * In the latter case dentry returned in @path might be negative if O_CREAT |
| 3274 | * hadn't been specified. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3275 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3276 | * An error code is returned on failure. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3277 | */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3278 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
| 3279 | const struct open_flags *op, |
| 3280 | bool got_write) |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3281 | { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3282 | struct user_namespace *mnt_userns; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3283 | struct dentry *dir = nd->path.dentry; |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3284 | struct inode *dir_inode = dir->d_inode; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3285 | int open_flag = op->open_flag; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3286 | struct dentry *dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3287 | int error, create_error = 0; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3288 | umode_t mode = op->mode; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3289 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3290 | |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3291 | if (unlikely(IS_DEADDIR(dir_inode))) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3292 | return ERR_PTR(-ENOENT); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3293 | |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3294 | file->f_mode &= ~FMODE_CREATED; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3295 | dentry = d_lookup(dir, &nd->last); |
| 3296 | for (;;) { |
| 3297 | if (!dentry) { |
| 3298 | dentry = d_alloc_parallel(dir, &nd->last, &wq); |
| 3299 | if (IS_ERR(dentry)) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3300 | return dentry; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3301 | } |
| 3302 | if (d_in_lookup(dentry)) |
| 3303 | break; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3304 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3305 | error = d_revalidate(dentry, nd->flags); |
| 3306 | if (likely(error > 0)) |
| 3307 | break; |
| 3308 | if (error) |
| 3309 | goto out_dput; |
| 3310 | d_invalidate(dentry); |
| 3311 | dput(dentry); |
| 3312 | dentry = NULL; |
| 3313 | } |
| 3314 | if (dentry->d_inode) { |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3315 | /* Cached positive dentry: will open in f_op->open */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3316 | return dentry; |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3317 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3318 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3319 | /* |
| 3320 | * Checking write permission is tricky, bacuse we don't know if we are |
| 3321 | * going to actually need it: O_CREAT opens should work as long as the |
| 3322 | * file exists. But checking existence breaks atomicity. The trick is |
| 3323 | * to check access and if not granted clear O_CREAT from the flags. |
| 3324 | * |
| 3325 | * Another problem is returing the "right" error value (e.g. for an |
| 3326 | * O_EXCL open we want to return EEXIST not EROFS). |
| 3327 | */ |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3328 | if (unlikely(!got_write)) |
| 3329 | open_flag &= ~O_TRUNC; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3330 | mnt_userns = mnt_user_ns(nd->path.mnt); |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3331 | if (open_flag & O_CREAT) { |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3332 | if (open_flag & O_EXCL) |
| 3333 | open_flag &= ~O_TRUNC; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3334 | if (!IS_POSIXACL(dir->d_inode)) |
| 3335 | mode &= ~current_umask(); |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3336 | if (likely(got_write)) |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3337 | create_error = may_o_create(mnt_userns, &nd->path, |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 3338 | dentry, mode); |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3339 | else |
| 3340 | create_error = -EROFS; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3341 | } |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3342 | if (create_error) |
| 3343 | open_flag &= ~O_CREAT; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3344 | if (dir_inode->i_op->atomic_open) { |
Al Viro | d489cf9 | 2020-03-11 08:07:53 -0400 | [diff] [blame] | 3345 | dentry = atomic_open(nd, dentry, file, open_flag, mode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3346 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
| 3347 | dentry = ERR_PTR(create_error); |
| 3348 | return dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3349 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3350 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3351 | if (d_in_lookup(dentry)) { |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3352 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
| 3353 | nd->flags); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3354 | d_lookup_done(dentry); |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3355 | if (unlikely(res)) { |
| 3356 | if (IS_ERR(res)) { |
| 3357 | error = PTR_ERR(res); |
| 3358 | goto out_dput; |
| 3359 | } |
| 3360 | dput(dentry); |
| 3361 | dentry = res; |
| 3362 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3363 | } |
| 3364 | |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3365 | /* Negative dentry, just create the file */ |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3366 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3367 | file->f_mode |= FMODE_CREATED; |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3368 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3369 | if (!dir_inode->i_op->create) { |
| 3370 | error = -EACCES; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3371 | goto out_dput; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3372 | } |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3373 | |
| 3374 | error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry, |
| 3375 | mode, open_flag & O_EXCL); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3376 | if (error) |
| 3377 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3378 | } |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3379 | if (unlikely(create_error) && !dentry->d_inode) { |
| 3380 | error = create_error; |
| 3381 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3382 | } |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3383 | return dentry; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3384 | |
| 3385 | out_dput: |
| 3386 | dput(dentry); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3387 | return ERR_PTR(error); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3388 | } |
| 3389 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3390 | static const char *open_last_lookups(struct nameidata *nd, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3391 | struct file *file, const struct open_flags *op) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3392 | { |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3393 | struct dentry *dir = nd->path.dentry; |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3394 | int open_flag = op->open_flag; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3395 | bool got_write = false; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 3396 | unsigned seq; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3397 | struct inode *inode; |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3398 | struct dentry *dentry; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3399 | const char *res; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3400 | |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 3401 | nd->flags |= op->intent; |
| 3402 | |
Al Viro | bc77daa | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 3403 | if (nd->last_type != LAST_NORM) { |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3404 | if (nd->depth) |
| 3405 | put_link(nd); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3406 | return handle_dots(nd, nd->last_type); |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 3407 | } |
Al Viro | 67ee3ad | 2009-12-26 07:01:01 -0500 | [diff] [blame] | 3408 | |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3409 | if (!(open_flag & O_CREAT)) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3410 | if (nd->last.name[nd->last.len]) |
| 3411 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 3412 | /* we _can_ be in RCU mode here */ |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3413 | dentry = lookup_fast(nd, &inode, &seq); |
| 3414 | if (IS_ERR(dentry)) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3415 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3416 | if (likely(dentry)) |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 3417 | goto finish_lookup; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3418 | |
Al Viro | 6583fe2 | 2016-03-05 18:14:03 -0500 | [diff] [blame] | 3419 | BUG_ON(nd->flags & LOOKUP_RCU); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3420 | } else { |
| 3421 | /* create side of things */ |
Al Viro | 7228741 | 2020-03-10 10:09:26 -0400 | [diff] [blame] | 3422 | if (nd->flags & LOOKUP_RCU) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 3423 | if (!try_to_unlazy(nd)) |
| 3424 | return ERR_PTR(-ECHILD); |
Al Viro | 7228741 | 2020-03-10 10:09:26 -0400 | [diff] [blame] | 3425 | } |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 3426 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3427 | /* trailing slashes? */ |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 3428 | if (unlikely(nd->last.name[nd->last.len])) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3429 | return ERR_PTR(-EISDIR); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3430 | } |
| 3431 | |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3432 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
Jens Axboe | e36cffe | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 3433 | got_write = !mnt_want_write(nd->path.mnt); |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3434 | /* |
| 3435 | * do _not_ fail yet - we might not need that or fail with |
| 3436 | * a different error; let lookup_open() decide; we'll be |
| 3437 | * dropping this one anyway. |
| 3438 | */ |
| 3439 | } |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3440 | if (open_flag & O_CREAT) |
| 3441 | inode_lock(dir->d_inode); |
| 3442 | else |
| 3443 | inode_lock_shared(dir->d_inode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3444 | dentry = lookup_open(nd, file, op, got_write); |
Al Viro | f7bb959 | 2020-03-05 13:25:20 -0500 | [diff] [blame] | 3445 | if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) |
| 3446 | fsnotify_create(dir->d_inode, dentry); |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3447 | if (open_flag & O_CREAT) |
| 3448 | inode_unlock(dir->d_inode); |
| 3449 | else |
| 3450 | inode_unlock_shared(dir->d_inode); |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3451 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3452 | if (got_write) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3453 | mnt_drop_write(nd->path.mnt); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3454 | |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3455 | if (IS_ERR(dentry)) |
| 3456 | return ERR_CAST(dentry); |
| 3457 | |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3458 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
Al Viro | e73cabf | 2020-01-09 14:30:08 -0500 | [diff] [blame] | 3459 | dput(nd->path.dentry); |
| 3460 | nd->path.dentry = dentry; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3461 | return NULL; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3462 | } |
| 3463 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3464 | finish_lookup: |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3465 | if (nd->depth) |
| 3466 | put_link(nd); |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 3467 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3468 | if (unlikely(res)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3469 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3470 | return res; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | /* |
| 3474 | * Handle the last step of open() |
| 3475 | */ |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3476 | static int do_open(struct nameidata *nd, |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3477 | struct file *file, const struct open_flags *op) |
| 3478 | { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3479 | struct user_namespace *mnt_userns; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3480 | int open_flag = op->open_flag; |
| 3481 | bool do_truncate; |
| 3482 | int acc_mode; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3483 | int error; |
| 3484 | |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3485 | if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { |
| 3486 | error = complete_walk(nd); |
| 3487 | if (error) |
| 3488 | return error; |
| 3489 | } |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3490 | if (!(file->f_mode & FMODE_CREATED)) |
| 3491 | audit_inode(nd->name, nd->path.dentry, 0); |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3492 | mnt_userns = mnt_user_ns(nd->path.mnt); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3493 | if (open_flag & O_CREAT) { |
Al Viro | b94e0b3 | 2020-03-10 10:13:53 -0400 | [diff] [blame] | 3494 | if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) |
| 3495 | return -EEXIST; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3496 | if (d_is_dir(nd->path.dentry)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3497 | return -EISDIR; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3498 | error = may_create_in_sticky(mnt_userns, nd, |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3499 | d_backing_inode(nd->path.dentry)); |
| 3500 | if (unlikely(error)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3501 | return error; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3502 | } |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3503 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3504 | return -ENOTDIR; |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 3505 | |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3506 | do_truncate = false; |
| 3507 | acc_mode = op->acc_mode; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3508 | if (file->f_mode & FMODE_CREATED) { |
| 3509 | /* Don't check for write permission, don't truncate */ |
| 3510 | open_flag &= ~O_TRUNC; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3511 | acc_mode = 0; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3512 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3513 | error = mnt_want_write(nd->path.mnt); |
| 3514 | if (error) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3515 | return error; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3516 | do_truncate = true; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3517 | } |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3518 | error = may_open(mnt_userns, &nd->path, acc_mode, open_flag); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3519 | if (!error && !(file->f_mode & FMODE_OPENED)) |
Al Viro | 3ad5615 | 2020-01-26 10:06:13 -0500 | [diff] [blame] | 3520 | error = vfs_open(&nd->path, file); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3521 | if (!error) |
| 3522 | error = ima_file_check(file, op->acc_mode); |
| 3523 | if (!error && do_truncate) |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3524 | error = handle_truncate(mnt_userns, file); |
Al Viro | c80567c | 2016-02-27 19:17:33 -0500 | [diff] [blame] | 3525 | if (unlikely(error > 0)) { |
| 3526 | WARN_ON(1); |
| 3527 | error = -EINVAL; |
| 3528 | } |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3529 | if (do_truncate) |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3530 | mnt_drop_write(nd->path.mnt); |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3531 | return error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3532 | } |
| 3533 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3534 | /** |
| 3535 | * vfs_tmpfile - create tmpfile |
| 3536 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3537 | * @dentry: pointer to dentry of the base directory |
| 3538 | * @mode: mode of the new tmpfile |
Randy Dunlap | 2111c3c | 2021-02-15 20:29:28 -0800 | [diff] [blame] | 3539 | * @open_flag: flags |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3540 | * |
| 3541 | * Create a temporary file. |
| 3542 | * |
| 3543 | * If the inode has been found through an idmapped mount the user namespace of |
| 3544 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3545 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3546 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3547 | * raw inode simply passs init_user_ns. |
| 3548 | */ |
| 3549 | struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, |
| 3550 | struct dentry *dentry, umode_t mode, int open_flag) |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3551 | { |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3552 | struct dentry *child = NULL; |
| 3553 | struct inode *dir = dentry->d_inode; |
| 3554 | struct inode *inode; |
| 3555 | int error; |
| 3556 | |
| 3557 | /* we want directory to be writable */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3558 | error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3559 | if (error) |
| 3560 | goto out_err; |
| 3561 | error = -EOPNOTSUPP; |
| 3562 | if (!dir->i_op->tmpfile) |
| 3563 | goto out_err; |
| 3564 | error = -ENOMEM; |
David Howells | cdf0122 | 2017-07-04 17:25:22 +0100 | [diff] [blame] | 3565 | child = d_alloc(dentry, &slash_name); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3566 | if (unlikely(!child)) |
| 3567 | goto out_err; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3568 | error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3569 | if (error) |
| 3570 | goto out_err; |
| 3571 | error = -ENOENT; |
| 3572 | inode = child->d_inode; |
| 3573 | if (unlikely(!inode)) |
| 3574 | goto out_err; |
| 3575 | if (!(open_flag & O_EXCL)) { |
| 3576 | spin_lock(&inode->i_lock); |
| 3577 | inode->i_state |= I_LINKABLE; |
| 3578 | spin_unlock(&inode->i_lock); |
| 3579 | } |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 3580 | ima_post_create_tmpfile(mnt_userns, inode); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3581 | return child; |
| 3582 | |
| 3583 | out_err: |
| 3584 | dput(child); |
| 3585 | return ERR_PTR(error); |
| 3586 | } |
| 3587 | EXPORT_SYMBOL(vfs_tmpfile); |
| 3588 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3589 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3590 | const struct open_flags *op, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3591 | struct file *file) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3592 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3593 | struct user_namespace *mnt_userns; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3594 | struct dentry *child; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3595 | struct path path; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3596 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3597 | if (unlikely(error)) |
| 3598 | return error; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3599 | error = mnt_want_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3600 | if (unlikely(error)) |
| 3601 | goto out; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3602 | mnt_userns = mnt_user_ns(path.mnt); |
| 3603 | child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3604 | error = PTR_ERR(child); |
Hirofumi Nakagawa | 684e73b | 2017-09-26 03:21:26 +0900 | [diff] [blame] | 3605 | if (IS_ERR(child)) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3606 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3607 | dput(path.dentry); |
| 3608 | path.dentry = child; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3609 | audit_inode(nd->name, child, 0); |
Eric Rannaud | 69a91c2 | 2014-10-30 01:51:01 -0700 | [diff] [blame] | 3610 | /* Don't check for other permissions, the inode was just created */ |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3611 | error = may_open(mnt_userns, &path, 0, op->open_flag); |
Al Viro | 1e8f44f | 2020-03-11 17:22:19 -0400 | [diff] [blame] | 3612 | if (!error) |
| 3613 | error = vfs_open(&path, file); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3614 | out2: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3615 | mnt_drop_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3616 | out: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3617 | path_put(&path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3618 | return error; |
| 3619 | } |
| 3620 | |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3621 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
| 3622 | { |
| 3623 | struct path path; |
| 3624 | int error = path_lookupat(nd, flags, &path); |
| 3625 | if (!error) { |
| 3626 | audit_inode(nd->name, path.dentry, 0); |
Al Viro | ae2bb29 | 2018-07-10 13:22:28 -0400 | [diff] [blame] | 3627 | error = vfs_open(&path, file); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3628 | path_put(&path); |
| 3629 | } |
| 3630 | return error; |
| 3631 | } |
| 3632 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3633 | static struct file *path_openat(struct nameidata *nd, |
| 3634 | const struct open_flags *op, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | { |
Al Viro | 30d90494 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3636 | struct file *file; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3637 | int error; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3638 | |
Al Viro | ea73ea7 | 2018-07-11 15:00:04 -0400 | [diff] [blame] | 3639 | file = alloc_empty_file(op->open_flag, current_cred()); |
Al Viro | 1afc99b | 2013-02-14 20:41:04 -0500 | [diff] [blame] | 3640 | if (IS_ERR(file)) |
| 3641 | return file; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3642 | |
Al Viro | bb458c6 | 2013-07-13 13:26:37 +0400 | [diff] [blame] | 3643 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3644 | error = do_tmpfile(nd, flags, op, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3645 | } else if (unlikely(file->f_flags & O_PATH)) { |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3646 | error = do_o_path(nd, flags, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3647 | } else { |
| 3648 | const char *s = path_init(nd, flags); |
| 3649 | while (!(error = link_path_walk(s, nd)) && |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3650 | (s = open_last_lookups(nd, file, op)) != NULL) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3651 | ; |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3652 | if (!error) |
| 3653 | error = do_open(nd, file, op); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3654 | terminate_walk(nd); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3655 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3656 | if (likely(!error)) { |
Al Viro | aad888f | 2018-06-08 12:58:04 -0400 | [diff] [blame] | 3657 | if (likely(file->f_mode & FMODE_OPENED)) |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3658 | return file; |
| 3659 | WARN_ON(1); |
| 3660 | error = -EINVAL; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 3661 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3662 | fput(file); |
| 3663 | if (error == -EOPENSTALE) { |
| 3664 | if (flags & LOOKUP_RCU) |
| 3665 | error = -ECHILD; |
| 3666 | else |
| 3667 | error = -ESTALE; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3668 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3669 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | } |
| 3671 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3672 | struct file *do_filp_open(int dfd, struct filename *pathname, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3673 | const struct open_flags *op) |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3674 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3675 | struct nameidata nd; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3676 | int flags = op->lookup_flags; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3677 | struct file *filp; |
| 3678 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 3679 | set_nameidata(&nd, dfd, pathname, NULL); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3680 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3681 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3682 | filp = path_openat(&nd, op, flags); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3683 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3684 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3685 | restore_nameidata(); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3686 | return filp; |
| 3687 | } |
| 3688 | |
Al Viro | ffb37ca | 2021-04-01 19:00:57 -0400 | [diff] [blame] | 3689 | struct file *do_file_open_root(const struct path *root, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3690 | const char *name, const struct open_flags *op) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3691 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3692 | struct nameidata nd; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3693 | struct file *file; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3694 | struct filename *filename; |
Al Viro | bcba1e7 | 2021-04-01 22:03:41 -0400 | [diff] [blame] | 3695 | int flags = op->lookup_flags; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3696 | |
Al Viro | ffb37ca | 2021-04-01 19:00:57 -0400 | [diff] [blame] | 3697 | if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3698 | return ERR_PTR(-ELOOP); |
| 3699 | |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3700 | filename = getname_kernel(name); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 3701 | if (IS_ERR(filename)) |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3702 | return ERR_CAST(filename); |
| 3703 | |
Al Viro | 0642296 | 2021-04-01 22:28:03 -0400 | [diff] [blame] | 3704 | set_nameidata(&nd, -1, filename, root); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3705 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3706 | if (unlikely(file == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3707 | file = path_openat(&nd, op, flags); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3708 | if (unlikely(file == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3709 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3710 | restore_nameidata(); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3711 | putname(filename); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3712 | return file; |
| 3713 | } |
| 3714 | |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3715 | static struct dentry *filename_create(int dfd, struct filename *name, |
| 3716 | struct path *path, unsigned int lookup_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3718 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3719 | struct qstr last; |
NeilBrown | b3d4650 | 2022-04-14 13:57:35 +1000 | [diff] [blame] | 3720 | bool want_dir = lookup_flags & LOOKUP_DIRECTORY; |
| 3721 | unsigned int reval_flag = lookup_flags & LOOKUP_REVAL; |
| 3722 | unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL; |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3723 | int type; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3724 | int err2; |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3725 | int error; |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3726 | |
NeilBrown | b3d4650 | 2022-04-14 13:57:35 +1000 | [diff] [blame] | 3727 | error = filename_parentat(dfd, name, reval_flag, path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 3728 | if (error) |
| 3729 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3731 | /* |
| 3732 | * Yucky last component or no last component at all? |
| 3733 | * (foo/., foo/.., /////) |
| 3734 | */ |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3735 | if (unlikely(type != LAST_NORM)) |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3736 | goto out; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3737 | |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3738 | /* 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] | 3739 | err2 = mnt_want_write(path->mnt); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3740 | /* |
NeilBrown | b3d4650 | 2022-04-14 13:57:35 +1000 | [diff] [blame] | 3741 | * Do the final lookup. Suppress 'create' if there is a trailing |
| 3742 | * '/', and a directory wasn't requested. |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3743 | */ |
NeilBrown | b3d4650 | 2022-04-14 13:57:35 +1000 | [diff] [blame] | 3744 | if (last.name[last.len] && !want_dir) |
| 3745 | create_flags = 0; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3746 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
NeilBrown | b3d4650 | 2022-04-14 13:57:35 +1000 | [diff] [blame] | 3747 | dentry = __lookup_hash(&last, path->dentry, reval_flag | create_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | if (IS_ERR(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3749 | goto unlock; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3750 | |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3751 | error = -EEXIST; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3752 | if (d_is_positive(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3753 | goto fail; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3754 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3755 | /* |
| 3756 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 3757 | * From the vfs_mknod() POV we just have a negative dentry - |
| 3758 | * all is fine. Let's be bastards - you had / on the end, you've |
| 3759 | * been asking for (non-existent) directory. -ENOENT for you. |
| 3760 | */ |
NeilBrown | b3d4650 | 2022-04-14 13:57:35 +1000 | [diff] [blame] | 3761 | if (unlikely(!create_flags)) { |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3762 | error = -ENOENT; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3763 | goto fail; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3764 | } |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3765 | if (unlikely(err2)) { |
| 3766 | error = err2; |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3767 | goto fail; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | fail: |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3771 | dput(dentry); |
| 3772 | dentry = ERR_PTR(error); |
| 3773 | unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3774 | inode_unlock(path->dentry->d_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3775 | if (!err2) |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3776 | mnt_drop_write(path->mnt); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3777 | out: |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3778 | path_put(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | return dentry; |
| 3780 | } |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3781 | |
| 3782 | struct dentry *kern_path_create(int dfd, const char *pathname, |
| 3783 | struct path *path, unsigned int lookup_flags) |
| 3784 | { |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3785 | struct filename *filename = getname_kernel(pathname); |
| 3786 | struct dentry *res = filename_create(dfd, filename, path, lookup_flags); |
| 3787 | |
| 3788 | putname(filename); |
| 3789 | return res; |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3790 | } |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3791 | EXPORT_SYMBOL(kern_path_create); |
| 3792 | |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3793 | void done_path_create(struct path *path, struct dentry *dentry) |
| 3794 | { |
| 3795 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3796 | inode_unlock(path->dentry->d_inode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3797 | mnt_drop_write(path->mnt); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3798 | path_put(path); |
| 3799 | } |
| 3800 | EXPORT_SYMBOL(done_path_create); |
| 3801 | |
Al Viro | 520ae68 | 2015-05-13 07:00:28 -0400 | [diff] [blame] | 3802 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3803 | struct path *path, unsigned int lookup_flags) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3804 | { |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3805 | struct filename *filename = getname(pathname); |
| 3806 | struct dentry *res = filename_create(dfd, filename, path, lookup_flags); |
| 3807 | |
| 3808 | putname(filename); |
| 3809 | return res; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3810 | } |
| 3811 | EXPORT_SYMBOL(user_path_create); |
| 3812 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3813 | /** |
| 3814 | * vfs_mknod - create device node or file |
| 3815 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3816 | * @dir: inode of @dentry |
| 3817 | * @dentry: pointer to dentry of the base directory |
| 3818 | * @mode: mode of the new device node or file |
| 3819 | * @dev: device number of device to create |
| 3820 | * |
| 3821 | * Create a device node or file. |
| 3822 | * |
| 3823 | * If the inode has been found through an idmapped mount the user namespace of |
| 3824 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3825 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3826 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3827 | * raw inode simply passs init_user_ns. |
| 3828 | */ |
| 3829 | int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 3830 | struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | { |
Miklos Szeredi | a3c751a | 2020-05-14 16:44:23 +0200 | [diff] [blame] | 3832 | bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3833 | int error = may_create(mnt_userns, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | |
| 3835 | if (error) |
| 3836 | return error; |
| 3837 | |
Miklos Szeredi | a3c751a | 2020-05-14 16:44:23 +0200 | [diff] [blame] | 3838 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && |
| 3839 | !capable(CAP_MKNOD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | return -EPERM; |
| 3841 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3842 | if (!dir->i_op->mknod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | return -EPERM; |
| 3844 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3845 | error = devcgroup_inode_mknod(mode, dev); |
| 3846 | if (error) |
| 3847 | return error; |
| 3848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 3850 | if (error) |
| 3851 | return error; |
| 3852 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3853 | error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3854 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3855 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | return error; |
| 3857 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3858 | EXPORT_SYMBOL(vfs_mknod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | |
Al Viro | f69aac0 | 2011-07-26 04:31:40 -0400 | [diff] [blame] | 3860 | static int may_mknod(umode_t mode) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3861 | { |
| 3862 | switch (mode & S_IFMT) { |
| 3863 | case S_IFREG: |
| 3864 | case S_IFCHR: |
| 3865 | case S_IFBLK: |
| 3866 | case S_IFIFO: |
| 3867 | case S_IFSOCK: |
| 3868 | case 0: /* zero mode translates to S_IFREG */ |
| 3869 | return 0; |
| 3870 | case S_IFDIR: |
| 3871 | return -EPERM; |
| 3872 | default: |
| 3873 | return -EINVAL; |
| 3874 | } |
| 3875 | } |
| 3876 | |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 3877 | static int do_mknodat(int dfd, struct filename *name, umode_t mode, |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3878 | unsigned int dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3880 | struct user_namespace *mnt_userns; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3881 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3882 | struct path path; |
| 3883 | int error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3884 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | |
Al Viro | 8e4bfca | 2012-07-20 01:17:26 +0400 | [diff] [blame] | 3886 | error = may_mknod(mode); |
| 3887 | if (error) |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3888 | goto out1; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3889 | retry: |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3890 | dentry = filename_create(dfd, name, &path, lookup_flags); |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3891 | error = PTR_ERR(dentry); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3892 | if (IS_ERR(dentry)) |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3893 | goto out1; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3894 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3895 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3896 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3897 | error = security_path_mknod(&path, dentry, mode, dev); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3898 | if (error) |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3899 | goto out2; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3900 | |
| 3901 | mnt_userns = mnt_user_ns(path.mnt); |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3902 | switch (mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | case 0: case S_IFREG: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3904 | error = vfs_create(mnt_userns, path.dentry->d_inode, |
| 3905 | dentry, mode, true); |
Mimi Zohar | 05d1a71 | 2016-02-29 19:52:05 -0500 | [diff] [blame] | 3906 | if (!error) |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 3907 | ima_post_path_mknod(mnt_userns, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | break; |
| 3909 | case S_IFCHR: case S_IFBLK: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3910 | error = vfs_mknod(mnt_userns, path.dentry->d_inode, |
| 3911 | dentry, mode, new_decode_dev(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | break; |
| 3913 | case S_IFIFO: case S_IFSOCK: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3914 | error = vfs_mknod(mnt_userns, path.dentry->d_inode, |
| 3915 | dentry, mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | } |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3918 | out2: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3919 | done_path_create(&path, dentry); |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3920 | if (retry_estale(error, lookup_flags)) { |
| 3921 | lookup_flags |= LOOKUP_REVAL; |
| 3922 | goto retry; |
| 3923 | } |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3924 | out1: |
| 3925 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | return error; |
| 3927 | } |
| 3928 | |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3929 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
| 3930 | unsigned int, dev) |
| 3931 | { |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3932 | return do_mknodat(dfd, getname(filename), mode, dev); |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3933 | } |
| 3934 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3935 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3936 | { |
Dmitry Kadashev | 7797251 | 2021-07-08 13:34:40 +0700 | [diff] [blame] | 3937 | return do_mknodat(AT_FDCWD, getname(filename), mode, dev); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3938 | } |
| 3939 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3940 | /** |
| 3941 | * vfs_mkdir - create directory |
| 3942 | * @mnt_userns: user namespace of the mount the inode was found from |
| 3943 | * @dir: inode of @dentry |
| 3944 | * @dentry: pointer to dentry of the base directory |
| 3945 | * @mode: mode of the new directory |
| 3946 | * |
| 3947 | * Create a directory. |
| 3948 | * |
| 3949 | * If the inode has been found through an idmapped mount the user namespace of |
| 3950 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 3951 | * care to map the inode according to @mnt_userns before checking permissions. |
| 3952 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 3953 | * raw inode simply passs init_user_ns. |
| 3954 | */ |
| 3955 | int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 3956 | struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3958 | int error = may_create(mnt_userns, dir, dentry); |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3959 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | |
| 3961 | if (error) |
| 3962 | return error; |
| 3963 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3964 | if (!dir->i_op->mkdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | return -EPERM; |
| 3966 | |
| 3967 | mode &= (S_IRWXUGO|S_ISVTX); |
| 3968 | error = security_inode_mkdir(dir, dentry, mode); |
| 3969 | if (error) |
| 3970 | return error; |
| 3971 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3972 | if (max_links && dir->i_nlink >= max_links) |
| 3973 | return -EMLINK; |
| 3974 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 3975 | error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3976 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3977 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | return error; |
| 3979 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3980 | EXPORT_SYMBOL(vfs_mkdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 3982 | int do_mkdirat(int dfd, struct filename *name, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | { |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3984 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3985 | struct path path; |
| 3986 | int error; |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3987 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3989 | retry: |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 3990 | dentry = filename_create(dfd, name, &path, lookup_flags); |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 3991 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3992 | if (IS_ERR(dentry)) |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 3993 | goto out_putname; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3994 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3995 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3996 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3997 | error = security_path_mkdir(&path, dentry, mode); |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 3998 | if (!error) { |
| 3999 | struct user_namespace *mnt_userns; |
| 4000 | mnt_userns = mnt_user_ns(path.mnt); |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4001 | error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry, |
| 4002 | mode); |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4003 | } |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4004 | done_path_create(&path, dentry); |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 4005 | if (retry_estale(error, lookup_flags)) { |
| 4006 | lookup_flags |= LOOKUP_REVAL; |
| 4007 | goto retry; |
| 4008 | } |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 4009 | out_putname: |
| 4010 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | return error; |
| 4012 | } |
| 4013 | |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4014 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
| 4015 | { |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 4016 | return do_mkdirat(dfd, getname(pathname), mode); |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4017 | } |
| 4018 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 4019 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4020 | { |
Dmitry Kadashev | 584d322 | 2021-07-08 13:34:39 +0700 | [diff] [blame] | 4021 | return do_mkdirat(AT_FDCWD, getname(pathname), mode); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4022 | } |
| 4023 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4024 | /** |
| 4025 | * vfs_rmdir - remove directory |
| 4026 | * @mnt_userns: user namespace of the mount the inode was found from |
| 4027 | * @dir: inode of @dentry |
| 4028 | * @dentry: pointer to dentry of the base directory |
| 4029 | * |
| 4030 | * Remove a directory. |
| 4031 | * |
| 4032 | * If the inode has been found through an idmapped mount the user namespace of |
| 4033 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 4034 | * care to map the inode according to @mnt_userns before checking permissions. |
| 4035 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 4036 | * raw inode simply passs init_user_ns. |
| 4037 | */ |
| 4038 | int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 4039 | struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4041 | int error = may_delete(mnt_userns, dir, dentry, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | |
| 4043 | if (error) |
| 4044 | return error; |
| 4045 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4046 | if (!dir->i_op->rmdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | return -EPERM; |
| 4048 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 4049 | dget(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4050 | inode_lock(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 4052 | error = -EBUSY; |
David Howells | 1bd9c4e | 2021-11-18 08:58:08 +0000 | [diff] [blame] | 4053 | if (is_local_mountpoint(dentry) || |
| 4054 | (dentry->d_inode->i_flags & S_KERNEL_FILE)) |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 4055 | goto out; |
| 4056 | |
| 4057 | error = security_inode_rmdir(dir, dentry); |
| 4058 | if (error) |
| 4059 | goto out; |
| 4060 | |
| 4061 | error = dir->i_op->rmdir(dir, dentry); |
| 4062 | if (error) |
| 4063 | goto out; |
| 4064 | |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4065 | shrink_dcache_parent(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 4066 | dentry->d_inode->i_flags |= S_DEAD; |
| 4067 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4068 | detach_mounts(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 4069 | |
| 4070 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4071 | inode_unlock(dentry->d_inode); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 4072 | dput(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 4073 | if (!error) |
Amir Goldstein | a37d9a1 | 2022-01-20 23:53:04 +0200 | [diff] [blame] | 4074 | d_delete_notify(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | return error; |
| 4076 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4077 | EXPORT_SYMBOL(vfs_rmdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 4079 | int do_rmdir(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4081 | struct user_namespace *mnt_userns; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4082 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4084 | struct path path; |
| 4085 | struct qstr last; |
| 4086 | int type; |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 4087 | unsigned int lookup_flags = 0; |
| 4088 | retry: |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 4089 | error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4090 | if (error) |
| 4091 | goto exit1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4093 | switch (type) { |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4094 | case LAST_DOTDOT: |
| 4095 | error = -ENOTEMPTY; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4096 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4097 | case LAST_DOT: |
| 4098 | error = -EINVAL; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4099 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4100 | case LAST_ROOT: |
| 4101 | error = -EBUSY; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4102 | goto exit2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | } |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4104 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4105 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4106 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4107 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4108 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4109 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4110 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4112 | if (IS_ERR(dentry)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4113 | goto exit3; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 4114 | if (!dentry->d_inode) { |
| 4115 | error = -ENOENT; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4116 | goto exit4; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 4117 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4118 | error = security_path_rmdir(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4119 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4120 | goto exit4; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4121 | mnt_userns = mnt_user_ns(path.mnt); |
| 4122 | error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4123 | exit4: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4124 | dput(dentry); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4125 | exit3: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4126 | inode_unlock(path.dentry->d_inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4127 | mnt_drop_write(path.mnt); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4128 | exit2: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4129 | path_put(&path); |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 4130 | if (retry_estale(error, lookup_flags)) { |
| 4131 | lookup_flags |= LOOKUP_REVAL; |
| 4132 | goto retry; |
| 4133 | } |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4134 | exit1: |
Al Viro | 24fb33d | 2020-08-12 05:15:18 +0100 | [diff] [blame] | 4135 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | return error; |
| 4137 | } |
| 4138 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 4139 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4140 | { |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 4141 | return do_rmdir(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4142 | } |
| 4143 | |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4144 | /** |
| 4145 | * vfs_unlink - unlink a filesystem object |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4146 | * @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] | 4147 | * @dir: parent directory |
| 4148 | * @dentry: victim |
| 4149 | * @delegated_inode: returns victim inode, if the inode is delegated. |
| 4150 | * |
| 4151 | * The caller must hold dir->i_mutex. |
| 4152 | * |
| 4153 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and |
| 4154 | * return a reference to the inode in delegated_inode. The caller |
| 4155 | * should then break the delegation on that inode and retry. Because |
| 4156 | * breaking a delegation may take a long time, the caller should drop |
| 4157 | * dir->i_mutex before doing so. |
| 4158 | * |
| 4159 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4160 | * be appropriate for callers that expect the underlying filesystem not |
| 4161 | * to be NFS exported. |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4162 | * |
| 4163 | * If the inode has been found through an idmapped mount the user namespace of |
| 4164 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 4165 | * care to map the inode according to @mnt_userns before checking permissions. |
| 4166 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 4167 | * raw inode simply passs init_user_ns. |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4168 | */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4169 | int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 4170 | struct dentry *dentry, struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 4172 | struct inode *target = dentry->d_inode; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4173 | int error = may_delete(mnt_userns, dir, dentry, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | |
| 4175 | if (error) |
| 4176 | return error; |
| 4177 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4178 | if (!dir->i_op->unlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | return -EPERM; |
| 4180 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4181 | inode_lock(target); |
Hugh Dickins | 51cc3a6 | 2021-09-02 14:53:57 -0700 | [diff] [blame] | 4182 | if (IS_SWAPFILE(target)) |
| 4183 | error = -EPERM; |
| 4184 | else if (is_local_mountpoint(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | error = -EBUSY; |
| 4186 | else { |
| 4187 | error = security_inode_unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 4188 | if (!error) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 4189 | error = try_break_deleg(target, delegated_inode); |
| 4190 | if (error) |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4191 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | error = dir->i_op->unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4193 | if (!error) { |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 4194 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4195 | detach_mounts(dentry); |
| 4196 | } |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 4197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | } |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4199 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4200 | inode_unlock(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | |
| 4202 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
Amir Goldstein | a37d9a1 | 2022-01-20 23:53:04 +0200 | [diff] [blame] | 4203 | if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
| 4204 | fsnotify_unlink(dir, dentry); |
| 4205 | } else if (!error) { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 4206 | fsnotify_link_count(target); |
Amir Goldstein | a37d9a1 | 2022-01-20 23:53:04 +0200 | [diff] [blame] | 4207 | d_delete_notify(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | return error; |
| 4211 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4212 | EXPORT_SYMBOL(vfs_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | |
| 4214 | /* |
| 4215 | * 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] | 4216 | * 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] | 4217 | * writeout happening, and we don't want to prevent access to the directory |
| 4218 | * while waiting on the I/O. |
| 4219 | */ |
Dmitry Kadashev | 45f30da | 2021-07-08 13:34:44 +0700 | [diff] [blame] | 4220 | int do_unlinkat(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4222 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4224 | struct path path; |
| 4225 | struct qstr last; |
| 4226 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | struct inode *inode = NULL; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4228 | struct inode *delegated_inode = NULL; |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 4229 | unsigned int lookup_flags = 0; |
| 4230 | retry: |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 4231 | error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4232 | if (error) |
| 4233 | goto exit1; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | error = -EISDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4236 | if (type != LAST_NORM) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4237 | goto exit2; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4238 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4239 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4240 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4241 | goto exit2; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4242 | retry_deleg: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4243 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4244 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | error = PTR_ERR(dentry); |
| 4246 | if (!IS_ERR(dentry)) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4247 | struct user_namespace *mnt_userns; |
| 4248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | /* Why not before? Because we want correct error value */ |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4250 | if (last.name[last.len]) |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 4251 | goto slashes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | inode = dentry->d_inode; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4253 | if (d_is_negative(dentry)) |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 4254 | goto slashes; |
| 4255 | ihold(inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4256 | error = security_path_unlink(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4257 | if (error) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4258 | goto exit3; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4259 | mnt_userns = mnt_user_ns(path.mnt); |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4260 | error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry, |
| 4261 | &delegated_inode); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4262 | exit3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | dput(dentry); |
| 4264 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4265 | inode_unlock(path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | if (inode) |
| 4267 | iput(inode); /* truncate the inode here */ |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4268 | inode = NULL; |
| 4269 | if (delegated_inode) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 4270 | error = break_deleg_wait(&delegated_inode); |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 4271 | if (!error) |
| 4272 | goto retry_deleg; |
| 4273 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4274 | mnt_drop_write(path.mnt); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4275 | exit2: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4276 | path_put(&path); |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 4277 | if (retry_estale(error, lookup_flags)) { |
| 4278 | lookup_flags |= LOOKUP_REVAL; |
| 4279 | inode = NULL; |
| 4280 | goto retry; |
| 4281 | } |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4282 | exit1: |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4283 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | return error; |
| 4285 | |
| 4286 | slashes: |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4287 | if (d_is_negative(dentry)) |
| 4288 | error = -ENOENT; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 4289 | else if (d_is_dir(dentry)) |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4290 | error = -EISDIR; |
| 4291 | else |
| 4292 | error = -ENOTDIR; |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4293 | goto exit3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | } |
| 4295 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 4296 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4297 | { |
| 4298 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 4299 | return -EINVAL; |
| 4300 | |
| 4301 | if (flag & AT_REMOVEDIR) |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 4302 | return do_rmdir(dfd, getname(pathname)); |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4303 | return do_unlinkat(dfd, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4304 | } |
| 4305 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4306 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4307 | { |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4308 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4309 | } |
| 4310 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4311 | /** |
| 4312 | * vfs_symlink - create symlink |
| 4313 | * @mnt_userns: user namespace of the mount the inode was found from |
| 4314 | * @dir: inode of @dentry |
| 4315 | * @dentry: pointer to dentry of the base directory |
| 4316 | * @oldname: name of the file to link to |
| 4317 | * |
| 4318 | * Create a symlink. |
| 4319 | * |
| 4320 | * If the inode has been found through an idmapped mount the user namespace of |
| 4321 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 4322 | * care to map the inode according to @mnt_userns before checking permissions. |
| 4323 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 4324 | * raw inode simply passs init_user_ns. |
| 4325 | */ |
| 4326 | int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 4327 | struct dentry *dentry, const char *oldname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4329 | int error = may_create(mnt_userns, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | |
| 4331 | if (error) |
| 4332 | return error; |
| 4333 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4334 | if (!dir->i_op->symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | return -EPERM; |
| 4336 | |
| 4337 | error = security_inode_symlink(dir, dentry, oldname); |
| 4338 | if (error) |
| 4339 | return error; |
| 4340 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4341 | error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 4342 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 4343 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | return error; |
| 4345 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4346 | EXPORT_SYMBOL(vfs_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 4348 | int do_symlinkat(struct filename *from, int newdfd, struct filename *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4350 | int error; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4351 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4352 | struct path path; |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4353 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4355 | if (IS_ERR(from)) { |
| 4356 | error = PTR_ERR(from); |
| 4357 | goto out_putnames; |
| 4358 | } |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4359 | retry: |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 4360 | dentry = filename_create(newdfd, to, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4361 | error = PTR_ERR(dentry); |
| 4362 | if (IS_ERR(dentry)) |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4363 | goto out_putnames; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4364 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4365 | error = security_path_symlink(&path, dentry, from->name); |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4366 | if (!error) { |
| 4367 | struct user_namespace *mnt_userns; |
| 4368 | |
| 4369 | mnt_userns = mnt_user_ns(path.mnt); |
| 4370 | error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry, |
| 4371 | from->name); |
| 4372 | } |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4373 | done_path_create(&path, dentry); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4374 | if (retry_estale(error, lookup_flags)) { |
| 4375 | lookup_flags |= LOOKUP_REVAL; |
| 4376 | goto retry; |
| 4377 | } |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4378 | out_putnames: |
| 4379 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | putname(from); |
| 4381 | return error; |
| 4382 | } |
| 4383 | |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4384 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
| 4385 | int, newdfd, const char __user *, newname) |
| 4386 | { |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4387 | return do_symlinkat(getname(oldname), newdfd, getname(newname)); |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4388 | } |
| 4389 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4390 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4391 | { |
Dmitry Kadashev | da2d0ce | 2021-07-08 13:34:41 +0700 | [diff] [blame] | 4392 | return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4393 | } |
| 4394 | |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4395 | /** |
| 4396 | * vfs_link - create a new link |
| 4397 | * @old_dentry: object to be linked |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4398 | * @mnt_userns: the user namespace of the mount |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4399 | * @dir: new parent |
| 4400 | * @new_dentry: where to create the new link |
| 4401 | * @delegated_inode: returns inode needing a delegation break |
| 4402 | * |
| 4403 | * The caller must hold dir->i_mutex |
| 4404 | * |
| 4405 | * If vfs_link discovers a delegation on the to-be-linked file in need |
| 4406 | * of breaking, it will return -EWOULDBLOCK and return a reference to the |
| 4407 | * inode in delegated_inode. The caller should then break the delegation |
| 4408 | * and retry. Because breaking a delegation may take a long time, the |
| 4409 | * caller should drop the i_mutex before doing so. |
| 4410 | * |
| 4411 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4412 | * be appropriate for callers that expect the underlying filesystem not |
| 4413 | * to be NFS exported. |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4414 | * |
| 4415 | * If the inode has been found through an idmapped mount the user namespace of |
| 4416 | * the vfsmount must be passed through @mnt_userns. This function will then take |
| 4417 | * care to map the inode according to @mnt_userns before checking permissions. |
| 4418 | * On non-idmapped mounts or if permission checking is to be performed on the |
| 4419 | * raw inode simply passs init_user_ns. |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4420 | */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4421 | int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns, |
| 4422 | struct inode *dir, struct dentry *new_dentry, |
| 4423 | struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | { |
| 4425 | struct inode *inode = old_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4426 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | int error; |
| 4428 | |
| 4429 | if (!inode) |
| 4430 | return -ENOENT; |
| 4431 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4432 | error = may_create(mnt_userns, dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | if (error) |
| 4434 | return error; |
| 4435 | |
| 4436 | if (dir->i_sb != inode->i_sb) |
| 4437 | return -EXDEV; |
| 4438 | |
| 4439 | /* |
| 4440 | * A link to an append-only or immutable file cannot be created. |
| 4441 | */ |
| 4442 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4443 | return -EPERM; |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 4444 | /* |
| 4445 | * Updating the link count will likely cause i_uid and i_gid to |
| 4446 | * be writen back improperly if their true value is unknown to |
| 4447 | * the vfs. |
| 4448 | */ |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4449 | if (HAS_UNMAPPED_ID(mnt_userns, inode)) |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 4450 | return -EPERM; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4451 | if (!dir->i_op->link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | return -EPERM; |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4453 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | return -EPERM; |
| 4455 | |
| 4456 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 4457 | if (error) |
| 4458 | return error; |
| 4459 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4460 | inode_lock(inode); |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4461 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4462 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4463 | error = -ENOENT; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4464 | else if (max_links && inode->i_nlink >= max_links) |
| 4465 | error = -EMLINK; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4466 | else { |
| 4467 | error = try_break_deleg(inode, delegated_inode); |
| 4468 | if (!error) |
| 4469 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
| 4470 | } |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4471 | |
| 4472 | if (!error && (inode->i_state & I_LINKABLE)) { |
| 4473 | spin_lock(&inode->i_lock); |
| 4474 | inode->i_state &= ~I_LINKABLE; |
| 4475 | spin_unlock(&inode->i_lock); |
| 4476 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4477 | inode_unlock(inode); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 4478 | if (!error) |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4479 | fsnotify_link(dir, inode, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | return error; |
| 4481 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4482 | EXPORT_SYMBOL(vfs_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | |
| 4484 | /* |
| 4485 | * Hardlinks are often used in delicate situations. We avoid |
| 4486 | * security-related surprises by not following symlinks on the |
| 4487 | * newname. --KAB |
| 4488 | * |
| 4489 | * We don't follow them on the oldname either to be compatible |
| 4490 | * with linux 2.0, and to avoid hard-linking to directories |
| 4491 | * and other special files. --ADM |
| 4492 | */ |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 4493 | int do_linkat(int olddfd, struct filename *old, int newdfd, |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4494 | struct filename *new, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4496 | struct user_namespace *mnt_userns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | struct dentry *new_dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4498 | struct path old_path, new_path; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4499 | struct inode *delegated_inode = NULL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4500 | int how = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4503 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) { |
| 4504 | error = -EINVAL; |
| 4505 | goto out_putnames; |
| 4506 | } |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4507 | /* |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4508 | * To use null names we require CAP_DAC_READ_SEARCH |
| 4509 | * This ensures that not everyone will be able to create |
| 4510 | * handlink using the passed filedescriptor. |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4511 | */ |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4512 | if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) { |
| 4513 | error = -ENOENT; |
| 4514 | goto out_putnames; |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4515 | } |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4516 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4517 | if (flags & AT_SYMLINK_FOLLOW) |
| 4518 | how |= LOOKUP_FOLLOW; |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4519 | retry: |
Stephen Brennan | 794ebce | 2021-09-01 10:51:42 -0700 | [diff] [blame] | 4520 | error = filename_lookup(olddfd, old, how, &old_path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4521 | if (error) |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4522 | goto out_putnames; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4523 | |
Stephen Brennan | b4a4f21 | 2021-09-01 10:51:43 -0700 | [diff] [blame] | 4524 | new_dentry = filename_create(newdfd, new, &new_path, |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4525 | (how & LOOKUP_REVAL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4527 | if (IS_ERR(new_dentry)) |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4528 | goto out_putpath; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4529 | |
| 4530 | error = -EXDEV; |
| 4531 | if (old_path.mnt != new_path.mnt) |
| 4532 | goto out_dput; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4533 | mnt_userns = mnt_user_ns(new_path.mnt); |
| 4534 | error = may_linkat(mnt_userns, &old_path); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 4535 | if (unlikely(error)) |
| 4536 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4537 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4538 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 4539 | goto out_dput; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4540 | error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode, |
| 4541 | new_dentry, &delegated_inode); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 4542 | out_dput: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4543 | done_path_create(&new_path, new_dentry); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4544 | if (delegated_inode) { |
| 4545 | error = break_deleg_wait(&delegated_inode); |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4546 | if (!error) { |
| 4547 | path_put(&old_path); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4548 | goto retry; |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4549 | } |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4550 | } |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4551 | if (retry_estale(error, how)) { |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4552 | path_put(&old_path); |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4553 | how |= LOOKUP_REVAL; |
| 4554 | goto retry; |
| 4555 | } |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4556 | out_putpath: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 4557 | path_put(&old_path); |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4558 | out_putnames: |
| 4559 | putname(old); |
| 4560 | putname(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | |
| 4562 | return error; |
| 4563 | } |
| 4564 | |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4565 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
| 4566 | int, newdfd, const char __user *, newname, int, flags) |
| 4567 | { |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4568 | return do_linkat(olddfd, getname_uflags(oldname, flags), |
| 4569 | newdfd, getname(newname), flags); |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4570 | } |
| 4571 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4572 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4573 | { |
Dmitry Kadashev | 020250f | 2021-07-08 13:34:43 +0700 | [diff] [blame] | 4574 | return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4575 | } |
| 4576 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4577 | /** |
| 4578 | * vfs_rename - rename a filesystem object |
Randy Dunlap | 2111c3c | 2021-02-15 20:29:28 -0800 | [diff] [blame] | 4579 | * @rd: pointer to &struct renamedata info |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4580 | * |
| 4581 | * The caller must hold multiple mutexes--see lock_rename()). |
| 4582 | * |
| 4583 | * If vfs_rename discovers a delegation in need of breaking at either |
| 4584 | * the source or destination, it will return -EWOULDBLOCK and return a |
| 4585 | * reference to the inode in delegated_inode. The caller should then |
| 4586 | * break the delegation and retry. Because breaking a delegation may |
| 4587 | * take a long time, the caller should drop all locks before doing |
| 4588 | * so. |
| 4589 | * |
| 4590 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4591 | * be appropriate for callers that expect the underlying filesystem not |
| 4592 | * to be NFS exported. |
| 4593 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 4595 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 4596 | * Problems: |
Mauro Carvalho Chehab | 0117d42 | 2017-05-12 07:45:42 -0300 | [diff] [blame] | 4597 | * |
J. Bruce Fields | d03b29a2 | 2014-02-17 16:52:33 -0500 | [diff] [blame] | 4598 | * a) we can get into loop creation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | * b) race potential - two innocent renames can create a loop together. |
| 4600 | * That's where 4.4 screws up. Current fix: serialization on |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4601 | * 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] | 4602 | * story. |
J. Bruce Fields | 6cedba8 | 2012-03-05 11:40:41 -0500 | [diff] [blame] | 4603 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
| 4604 | * and source (if it is not a directory). |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4605 | * 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] | 4606 | * whether the target exists). Solution: try to be smart with locking |
| 4607 | * order for inodes. We rely on the fact that tree topology may change |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4608 | * 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] | 4609 | * move will be locked. Thus we can rank directories by the tree |
| 4610 | * (ancestors first) and rank all non-directories after them. |
| 4611 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4612 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 4614 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 4615 | * we'd better make sure that there's no link(2) for them. |
Sage Weil | e4eaac0 | 2011-05-24 13:06:07 -0700 | [diff] [blame] | 4616 | * 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] | 4617 | * 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] | 4618 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 4619 | * ->i_mutex on parents, which works but leads to some truly excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | * locking]. |
| 4621 | */ |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4622 | int vfs_rename(struct renamedata *rd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | { |
| 4624 | int error; |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4625 | struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; |
| 4626 | struct dentry *old_dentry = rd->old_dentry; |
| 4627 | struct dentry *new_dentry = rd->new_dentry; |
| 4628 | struct inode **delegated_inode = rd->delegated_inode; |
| 4629 | unsigned int flags = rd->flags; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4630 | bool is_dir = d_is_dir(old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4631 | struct inode *source = old_dentry->d_inode; |
| 4632 | struct inode *target = new_dentry->d_inode; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4633 | bool new_is_dir = false; |
| 4634 | unsigned max_links = new_dir->i_sb->s_max_links; |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4635 | struct name_snapshot old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | |
Miklos Szeredi | 8d3e293 | 2016-12-16 11:02:54 +0100 | [diff] [blame] | 4637 | if (source == target) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4638 | return 0; |
| 4639 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4640 | 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] | 4641 | if (error) |
| 4642 | return error; |
| 4643 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4644 | if (!target) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4645 | error = may_create(rd->new_mnt_userns, new_dir, new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4646 | } else { |
| 4647 | new_is_dir = d_is_dir(new_dentry); |
| 4648 | |
| 4649 | if (!(flags & RENAME_EXCHANGE)) |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4650 | error = may_delete(rd->new_mnt_userns, new_dir, |
| 4651 | new_dentry, is_dir); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4652 | else |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4653 | error = may_delete(rd->new_mnt_userns, new_dir, |
| 4654 | new_dentry, new_is_dir); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | if (error) |
| 4657 | return error; |
| 4658 | |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4659 | if (!old_dir->i_op->rename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | return -EPERM; |
| 4661 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4662 | /* |
| 4663 | * If we are going to change the parent - check write permissions, |
| 4664 | * we'll need to flip '..'. |
| 4665 | */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4666 | if (new_dir != old_dir) { |
| 4667 | if (is_dir) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4668 | error = inode_permission(rd->old_mnt_userns, source, |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 4669 | MAY_WRITE); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4670 | if (error) |
| 4671 | return error; |
| 4672 | } |
| 4673 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4674 | error = inode_permission(rd->new_mnt_userns, target, |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 4675 | MAY_WRITE); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4676 | if (error) |
| 4677 | return error; |
| 4678 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4679 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4680 | |
Miklos Szeredi | 0b3974e | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4681 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 4682 | flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4683 | if (error) |
| 4684 | return error; |
| 4685 | |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4686 | take_dentry_name_snapshot(&old_name, old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4687 | dget(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4688 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4689 | lock_two_nondirectories(source, target); |
| 4690 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4691 | inode_lock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4692 | |
Hugh Dickins | 51cc3a6 | 2021-09-02 14:53:57 -0700 | [diff] [blame] | 4693 | error = -EPERM; |
| 4694 | if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target))) |
| 4695 | goto out; |
| 4696 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4697 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 4698 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4699 | goto out; |
| 4700 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4701 | if (max_links && new_dir != old_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4702 | error = -EMLINK; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4703 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4704 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4705 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
| 4706 | old_dir->i_nlink >= max_links) |
| 4707 | goto out; |
| 4708 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4709 | if (!is_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4710 | error = try_break_deleg(source, delegated_inode); |
| 4711 | if (error) |
| 4712 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4713 | } |
| 4714 | if (target && !new_is_dir) { |
| 4715 | error = try_break_deleg(target, delegated_inode); |
| 4716 | if (error) |
| 4717 | goto out; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4718 | } |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 4719 | error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry, |
| 4720 | new_dir, new_dentry, flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4721 | if (error) |
| 4722 | goto out; |
| 4723 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4724 | if (!(flags & RENAME_EXCHANGE) && target) { |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4725 | if (is_dir) { |
| 4726 | shrink_dcache_parent(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4727 | target->i_flags |= S_DEAD; |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4728 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4729 | dont_mount(new_dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4730 | detach_mounts(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4731 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4732 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
| 4733 | if (!(flags & RENAME_EXCHANGE)) |
| 4734 | d_move(old_dentry, new_dentry); |
| 4735 | else |
| 4736 | d_exchange(old_dentry, new_dentry); |
| 4737 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4738 | out: |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4739 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4740 | unlock_two_nondirectories(source, target); |
| 4741 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4742 | inode_unlock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4743 | dput(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4744 | if (!error) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4745 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4746 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
| 4747 | if (flags & RENAME_EXCHANGE) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4748 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4749 | new_is_dir, NULL, new_dentry); |
| 4750 | } |
| 4751 | } |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4752 | release_dentry_name_snapshot(&old_name); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | return error; |
| 4755 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4756 | EXPORT_SYMBOL(vfs_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4758 | int do_renameat2(int olddfd, struct filename *from, int newdfd, |
| 4759 | struct filename *to, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | { |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4761 | struct renamedata rd; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4762 | struct dentry *old_dentry, *new_dentry; |
| 4763 | struct dentry *trap; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4764 | struct path old_path, new_path; |
| 4765 | struct qstr old_last, new_last; |
| 4766 | int old_type, new_type; |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4767 | struct inode *delegated_inode = NULL; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4768 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4769 | bool should_retry = false; |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4770 | int error = -EINVAL; |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4771 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4772 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4773 | goto put_names; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4774 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4775 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
| 4776 | (flags & RENAME_EXCHANGE)) |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4777 | goto put_names; |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4778 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4779 | if (flags & RENAME_EXCHANGE) |
| 4780 | target_flags = 0; |
| 4781 | |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4782 | retry: |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 4783 | error = filename_parentat(olddfd, from, lookup_flags, &old_path, |
| 4784 | &old_last, &old_type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4785 | if (error) |
| 4786 | goto put_names; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | |
Al Viro | c5f563f | 2021-09-07 15:57:42 -0400 | [diff] [blame] | 4788 | error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, |
| 4789 | &new_type); |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4790 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | goto exit1; |
| 4792 | |
| 4793 | error = -EXDEV; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4794 | if (old_path.mnt != new_path.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4795 | goto exit2; |
| 4796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | error = -EBUSY; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4798 | if (old_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | goto exit2; |
| 4800 | |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4801 | if (flags & RENAME_NOREPLACE) |
| 4802 | error = -EEXIST; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4803 | if (new_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | goto exit2; |
| 4805 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4806 | error = mnt_want_write(old_path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4807 | if (error) |
| 4808 | goto exit2; |
| 4809 | |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4810 | retry_deleg: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4811 | trap = lock_rename(new_path.dentry, old_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4813 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | error = PTR_ERR(old_dentry); |
| 4815 | if (IS_ERR(old_dentry)) |
| 4816 | goto exit3; |
| 4817 | /* source must exist */ |
| 4818 | error = -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4819 | if (d_is_negative(old_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | goto exit4; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4821 | 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] | 4822 | error = PTR_ERR(new_dentry); |
| 4823 | if (IS_ERR(new_dentry)) |
| 4824 | goto exit4; |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4825 | error = -EEXIST; |
| 4826 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) |
| 4827 | goto exit5; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4828 | if (flags & RENAME_EXCHANGE) { |
| 4829 | error = -ENOENT; |
| 4830 | if (d_is_negative(new_dentry)) |
| 4831 | goto exit5; |
| 4832 | |
| 4833 | if (!d_is_dir(new_dentry)) { |
| 4834 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4835 | if (new_last.name[new_last.len]) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4836 | goto exit5; |
| 4837 | } |
| 4838 | } |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4839 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 4840 | if (!d_is_dir(old_dentry)) { |
| 4841 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4842 | if (old_last.name[old_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4843 | goto exit5; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4844 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4845 | goto exit5; |
| 4846 | } |
| 4847 | /* source should not be ancestor of target */ |
| 4848 | error = -EINVAL; |
| 4849 | if (old_dentry == trap) |
| 4850 | goto exit5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | /* target should not be an ancestor of source */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4852 | if (!(flags & RENAME_EXCHANGE)) |
| 4853 | error = -ENOTEMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | if (new_dentry == trap) |
| 4855 | goto exit5; |
| 4856 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4857 | error = security_path_rename(&old_path, old_dentry, |
| 4858 | &new_path, new_dentry, flags); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4859 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4860 | goto exit5; |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4861 | |
| 4862 | rd.old_dir = old_path.dentry->d_inode; |
| 4863 | rd.old_dentry = old_dentry; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4864 | rd.old_mnt_userns = mnt_user_ns(old_path.mnt); |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4865 | rd.new_dir = new_path.dentry->d_inode; |
| 4866 | rd.new_dentry = new_dentry; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 4867 | rd.new_mnt_userns = mnt_user_ns(new_path.mnt); |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 4868 | rd.delegated_inode = &delegated_inode; |
| 4869 | rd.flags = flags; |
| 4870 | error = vfs_rename(&rd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | exit5: |
| 4872 | dput(new_dentry); |
| 4873 | exit4: |
| 4874 | dput(old_dentry); |
| 4875 | exit3: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4876 | unlock_rename(new_path.dentry, old_path.dentry); |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4877 | if (delegated_inode) { |
| 4878 | error = break_deleg_wait(&delegated_inode); |
| 4879 | if (!error) |
| 4880 | goto retry_deleg; |
| 4881 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4882 | mnt_drop_write(old_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | exit2: |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4884 | if (retry_estale(error, lookup_flags)) |
| 4885 | should_retry = true; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4886 | path_put(&new_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4888 | path_put(&old_path); |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4889 | if (should_retry) { |
| 4890 | should_retry = false; |
| 4891 | lookup_flags |= LOOKUP_REVAL; |
| 4892 | goto retry; |
| 4893 | } |
Dmitry Kadashev | 0ee50b4 | 2021-07-08 13:34:38 +0700 | [diff] [blame] | 4894 | put_names: |
Dmitry Kadashev | 91ef658 | 2021-07-08 13:34:37 +0700 | [diff] [blame] | 4895 | putname(from); |
Dmitry Kadashev | 91ef658 | 2021-07-08 13:34:37 +0700 | [diff] [blame] | 4896 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | return error; |
| 4898 | } |
| 4899 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4900 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
| 4901 | int, newdfd, const char __user *, newname, unsigned int, flags) |
| 4902 | { |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4903 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
| 4904 | flags); |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4905 | } |
| 4906 | |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4907 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
| 4908 | int, newdfd, const char __user *, newname) |
| 4909 | { |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4910 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
| 4911 | 0); |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4912 | } |
| 4913 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 4914 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4915 | { |
Jens Axboe | e886663 | 2020-09-26 17:20:17 -0600 | [diff] [blame] | 4916 | return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, |
| 4917 | getname(newname), 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4918 | } |
| 4919 | |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4920 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | { |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4922 | int len = PTR_ERR(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | if (IS_ERR(link)) |
| 4924 | goto out; |
| 4925 | |
| 4926 | len = strlen(link); |
| 4927 | if (len > (unsigned) buflen) |
| 4928 | len = buflen; |
| 4929 | if (copy_to_user(buffer, link, len)) |
| 4930 | len = -EFAULT; |
| 4931 | out: |
| 4932 | return len; |
| 4933 | } |
| 4934 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4935 | /** |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4936 | * vfs_readlink - copy symlink body into userspace buffer |
| 4937 | * @dentry: dentry on which to get symbolic link |
| 4938 | * @buffer: user memory pointer |
| 4939 | * @buflen: size of buffer |
| 4940 | * |
| 4941 | * Does not touch atime. That's up to the caller if necessary |
| 4942 | * |
| 4943 | * Does not call security hook. |
| 4944 | */ |
| 4945 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4946 | { |
| 4947 | struct inode *inode = d_inode(dentry); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4948 | DEFINE_DELAYED_CALL(done); |
| 4949 | const char *link; |
| 4950 | int res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4951 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4952 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
| 4953 | if (unlikely(inode->i_op->readlink)) |
| 4954 | return inode->i_op->readlink(dentry, buffer, buflen); |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4955 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4956 | if (!d_is_symlink(dentry)) |
| 4957 | return -EINVAL; |
| 4958 | |
| 4959 | spin_lock(&inode->i_lock); |
| 4960 | inode->i_opflags |= IOP_DEFAULT_READLINK; |
| 4961 | spin_unlock(&inode->i_lock); |
| 4962 | } |
| 4963 | |
Eric Biggers | 4c4f7c1 | 2019-04-10 13:21:14 -0700 | [diff] [blame] | 4964 | link = READ_ONCE(inode->i_link); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4965 | if (!link) { |
| 4966 | link = inode->i_op->get_link(dentry, inode, &done); |
| 4967 | if (IS_ERR(link)) |
| 4968 | return PTR_ERR(link); |
| 4969 | } |
| 4970 | res = readlink_copy(buffer, buflen, link); |
| 4971 | do_delayed_call(&done); |
| 4972 | return res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4973 | } |
| 4974 | EXPORT_SYMBOL(vfs_readlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4976 | /** |
| 4977 | * vfs_get_link - get symlink body |
| 4978 | * @dentry: dentry on which to get symbolic link |
| 4979 | * @done: caller needs to free returned data with this |
| 4980 | * |
| 4981 | * Calls security hook and i_op->get_link() on the supplied inode. |
| 4982 | * |
| 4983 | * It does not touch atime. That's up to the caller if necessary. |
| 4984 | * |
| 4985 | * Does not work on "special" symlinks like /proc/$$/fd/N |
| 4986 | */ |
| 4987 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) |
| 4988 | { |
| 4989 | const char *res = ERR_PTR(-EINVAL); |
| 4990 | struct inode *inode = d_inode(dentry); |
| 4991 | |
| 4992 | if (d_is_symlink(dentry)) { |
| 4993 | res = ERR_PTR(security_inode_readlink(dentry)); |
| 4994 | if (!res) |
| 4995 | res = inode->i_op->get_link(dentry, inode, done); |
| 4996 | } |
| 4997 | return res; |
| 4998 | } |
| 4999 | EXPORT_SYMBOL(vfs_get_link); |
| 5000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | /* get the link contents into pagecache */ |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 5002 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 5003 | struct delayed_call *callback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | { |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 5005 | char *kaddr; |
| 5006 | struct page *page; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 5007 | struct address_space *mapping = inode->i_mapping; |
| 5008 | |
Al Viro | d3883d4 | 2015-11-17 10:41:04 -0500 | [diff] [blame] | 5009 | if (!dentry) { |
| 5010 | page = find_get_page(mapping, 0); |
| 5011 | if (!page) |
| 5012 | return ERR_PTR(-ECHILD); |
| 5013 | if (!PageUptodate(page)) { |
| 5014 | put_page(page); |
| 5015 | return ERR_PTR(-ECHILD); |
| 5016 | } |
| 5017 | } else { |
| 5018 | page = read_mapping_page(mapping, 0, NULL); |
| 5019 | if (IS_ERR(page)) |
| 5020 | return (char*)page; |
| 5021 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 5022 | set_delayed_call(callback, page_put_link, page); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 5023 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
| 5024 | kaddr = page_address(page); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 5025 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 5026 | return kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | } |
| 5028 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 5029 | EXPORT_SYMBOL(page_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 5031 | void page_put_link(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 5033 | put_page(arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 5035 | EXPORT_SYMBOL(page_put_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 5037 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 5038 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 5039 | DEFINE_DELAYED_CALL(done); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 5040 | int res = readlink_copy(buffer, buflen, |
| 5041 | page_get_link(dentry, d_inode(dentry), |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 5042 | &done)); |
| 5043 | do_delayed_call(&done); |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 5044 | return res; |
| 5045 | } |
| 5046 | EXPORT_SYMBOL(page_readlink); |
| 5047 | |
Matthew Wilcox (Oracle) | 56f5746 | 2022-02-22 09:40:54 -0500 | [diff] [blame] | 5048 | int page_symlink(struct inode *inode, const char *symname, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5049 | { |
| 5050 | struct address_space *mapping = inode->i_mapping; |
Matthew Wilcox (Oracle) | 27a7791 | 2022-03-03 13:35:20 -0500 | [diff] [blame] | 5051 | const struct address_space_operations *aops = mapping->a_ops; |
Matthew Wilcox (Oracle) | 56f5746 | 2022-02-22 09:40:54 -0500 | [diff] [blame] | 5052 | bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 5053 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 5054 | void *fsdata; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 5055 | int err; |
Matthew Wilcox (Oracle) | 2d87817 | 2022-02-22 09:43:12 -0500 | [diff] [blame] | 5056 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 5058 | retry: |
Matthew Wilcox (Oracle) | 2d87817 | 2022-02-22 09:43:12 -0500 | [diff] [blame] | 5059 | if (nofs) |
| 5060 | flags = memalloc_nofs_save(); |
Matthew Wilcox (Oracle) | 27a7791 | 2022-03-03 13:35:20 -0500 | [diff] [blame] | 5061 | err = aops->write_begin(NULL, mapping, 0, len-1, &page, &fsdata); |
Matthew Wilcox (Oracle) | 2d87817 | 2022-02-22 09:43:12 -0500 | [diff] [blame] | 5062 | if (nofs) |
| 5063 | memalloc_nofs_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | if (err) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 5065 | goto fail; |
| 5066 | |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 5067 | memcpy(page_address(page), symname, len-1); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 5068 | |
Matthew Wilcox (Oracle) | 27a7791 | 2022-03-03 13:35:20 -0500 | [diff] [blame] | 5069 | err = aops->write_end(NULL, mapping, 0, len-1, len-1, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 5070 | page, fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | if (err < 0) |
| 5072 | goto fail; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 5073 | if (err < len-1) |
| 5074 | goto retry; |
| 5075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | mark_inode_dirty(inode); |
| 5077 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | fail: |
| 5079 | return err; |
| 5080 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 5081 | EXPORT_SYMBOL(page_symlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 5082 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 5083 | const struct inode_operations page_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 5084 | .get_link = page_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | EXPORT_SYMBOL(page_symlink_inode_operations); |