Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/namei.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Some corrections by tytso. |
| 10 | */ |
| 11 | |
| 12 | /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname |
| 13 | * lookup logic. |
| 14 | */ |
| 15 | /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 19 | #include <linux/export.h> |
David S. Miller | 4469690 | 2012-05-23 20:12:50 -0700 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 25 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/personality.h> |
| 27 | #include <linux/security.h> |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 28 | #include <linux/ima.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/syscalls.h> |
| 30 | #include <linux/mount.h> |
| 31 | #include <linux/audit.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 32 | #include <linux/capability.h> |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 33 | #include <linux/file.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 34 | #include <linux/fcntl.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 35 | #include <linux/device_cgroup.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 36 | #include <linux/fs_struct.h> |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
Linus Torvalds | 99d263d | 2014-09-13 11:30:10 -0700 | [diff] [blame] | 38 | #include <linux/hash.h> |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 39 | #include <linux/bitops.h> |
Eric W. Biederman | aeaa4a7 | 2016-07-23 11:20:44 -0500 | [diff] [blame] | 40 | #include <linux/init_task.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 41 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 43 | #include "internal.h" |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 44 | #include "mount.h" |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 45 | |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 46 | #define CREATE_TRACE_POINTS |
| 47 | #include <trace/events/namei.h> |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* [Feb-1997 T. Schoebel-Theuer] |
| 50 | * Fundamental changes in the pathname lookup mechanisms (namei) |
| 51 | * were necessary because of omirr. The reason is that omirr needs |
| 52 | * to know the _real_ pathname, not the user-supplied one, in case |
| 53 | * of symlinks (and also when transname replacements occur). |
| 54 | * |
| 55 | * The new code replaces the old recursive symlink resolution with |
| 56 | * an iterative one (in case of non-nested symlink chains). It does |
| 57 | * this with calls to <fs>_follow_link(). |
| 58 | * As a side effect, dir_namei(), _namei() and follow_link() are now |
| 59 | * replaced with a single function lookup_dentry() that can handle all |
| 60 | * the special cases of the former code. |
| 61 | * |
| 62 | * With the new dcache, the pathname is stored at each inode, at least as |
| 63 | * long as the refcount of the inode is positive. As a side effect, the |
| 64 | * size of the dcache depends on the inode cache and thus is dynamic. |
| 65 | * |
| 66 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink |
| 67 | * resolution to correspond with current state of the code. |
| 68 | * |
| 69 | * Note that the symlink resolution is not *completely* iterative. |
| 70 | * There is still a significant amount of tail- and mid- recursion in |
| 71 | * the algorithm. Also, note that <fs>_readlink() is not used in |
| 72 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() |
| 73 | * may return different results than <fs>_follow_link(). Many virtual |
| 74 | * filesystems (including /proc) exhibit this behavior. |
| 75 | */ |
| 76 | |
| 77 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: |
| 78 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL |
| 79 | * and the name already exists in form of a symlink, try to create the new |
| 80 | * name indicated by the symlink. The old code always complained that the |
| 81 | * name already exists, due to not following the symlink even if its target |
| 82 | * is nonexistent. The new semantics affects also mknod() and link() when |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 83 | * the name is a symlink pointing to a non-existent name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * |
| 85 | * I don't know which semantics is the right one, since I have no access |
| 86 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" |
| 87 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the |
| 88 | * "old" one. Personally, I think the new semantics is much more logical. |
| 89 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing |
| 90 | * file does succeed in both HP-UX and SunOs, but not in Solaris |
| 91 | * and in the old Linux semantics. |
| 92 | */ |
| 93 | |
| 94 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink |
| 95 | * semantics. See the comments in "open_namei" and "do_link" below. |
| 96 | * |
| 97 | * [10-Sep-98 Alan Modra] Another symlink change. |
| 98 | */ |
| 99 | |
| 100 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: |
| 101 | * inside the path - always follow. |
| 102 | * in the last component in creation/removal/renaming - never follow. |
| 103 | * if LOOKUP_FOLLOW passed - follow. |
| 104 | * if the pathname has trailing slashes - follow. |
| 105 | * otherwise - don't follow. |
| 106 | * (applied in that order). |
| 107 | * |
| 108 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT |
| 109 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. |
| 110 | * During the 2.4 we need to fix the userland stuff depending on it - |
| 111 | * hopefully we will be able to get rid of that wart in 2.5. So far only |
| 112 | * XEmacs seems to be relying on it... |
| 113 | */ |
| 114 | /* |
| 115 | * [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] | 116 | * 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] | 117 | * any extra contention... |
| 118 | */ |
| 119 | |
| 120 | /* In order to reduce some races, while at the same time doing additional |
| 121 | * checking and hopefully speeding things up, we copy filenames to the |
| 122 | * kernel data space before using them.. |
| 123 | * |
| 124 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). |
| 125 | * PATH_MAX includes the nul terminator --RR. |
| 126 | */ |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 127 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 128 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 129 | |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 130 | struct filename * |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 131 | getname_flags(const char __user *filename, int flags, int *empty) |
| 132 | { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 133 | struct filename *result; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 134 | char *kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 135 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Jeff Layton | 7ac8626 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 137 | result = audit_reusename(filename); |
| 138 | if (result) |
| 139 | return result; |
| 140 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 141 | result = __getname(); |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 142 | if (unlikely(!result)) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 143 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 145 | /* |
| 146 | * First, try to embed the struct filename inside the names_cache |
| 147 | * allocation |
| 148 | */ |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 149 | kname = (char *)result->iname; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 150 | result->name = kname; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 151 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 152 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 153 | if (unlikely(len < 0)) { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 154 | __putname(result); |
| 155 | return ERR_PTR(len); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 156 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 157 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 158 | /* |
| 159 | * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a |
| 160 | * separate struct filename so we can dedicate the entire |
| 161 | * names_cache allocation for the pathname, and re-do the copy from |
| 162 | * userland. |
| 163 | */ |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 164 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 165 | const size_t size = offsetof(struct filename, iname[1]); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 166 | kname = (char *)result; |
| 167 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 168 | /* |
| 169 | * size is chosen that way we to guarantee that |
| 170 | * result->iname[0] is within the same object and that |
| 171 | * kname can't be equal to result->iname, no matter what. |
| 172 | */ |
| 173 | result = kzalloc(size, GFP_KERNEL); |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 174 | if (unlikely(!result)) { |
| 175 | __putname(kname); |
| 176 | return ERR_PTR(-ENOMEM); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 177 | } |
| 178 | result->name = kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 179 | len = strncpy_from_user(kname, filename, PATH_MAX); |
| 180 | if (unlikely(len < 0)) { |
| 181 | __putname(kname); |
| 182 | kfree(result); |
| 183 | return ERR_PTR(len); |
| 184 | } |
| 185 | if (unlikely(len == PATH_MAX)) { |
| 186 | __putname(kname); |
| 187 | kfree(result); |
| 188 | return ERR_PTR(-ENAMETOOLONG); |
| 189 | } |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 192 | result->refcnt = 1; |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 193 | /* The empty path is special. */ |
| 194 | if (unlikely(!len)) { |
| 195 | if (empty) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 196 | *empty = 1; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 197 | if (!(flags & LOOKUP_EMPTY)) { |
| 198 | putname(result); |
| 199 | return ERR_PTR(-ENOENT); |
| 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 202 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 203 | result->uptr = filename; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 204 | result->aname = NULL; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 205 | audit_getname(result); |
| 206 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 209 | struct filename * |
| 210 | getname(const char __user * filename) |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 211 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 212 | return getname_flags(filename, 0, NULL); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 215 | struct filename * |
| 216 | getname_kernel(const char * filename) |
| 217 | { |
| 218 | struct filename *result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 219 | int len = strlen(filename) + 1; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 220 | |
| 221 | result = __getname(); |
| 222 | if (unlikely(!result)) |
| 223 | return ERR_PTR(-ENOMEM); |
| 224 | |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 225 | if (len <= EMBEDDED_NAME_MAX) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 226 | result->name = (char *)result->iname; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 227 | } else if (len <= PATH_MAX) { |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 228 | const size_t size = offsetof(struct filename, iname[1]); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 229 | struct filename *tmp; |
| 230 | |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 231 | tmp = kmalloc(size, GFP_KERNEL); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 232 | if (unlikely(!tmp)) { |
| 233 | __putname(result); |
| 234 | return ERR_PTR(-ENOMEM); |
| 235 | } |
| 236 | tmp->name = (char *)result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 237 | result = tmp; |
| 238 | } else { |
| 239 | __putname(result); |
| 240 | return ERR_PTR(-ENAMETOOLONG); |
| 241 | } |
| 242 | memcpy((char *)result->name, filename, len); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 243 | result->uptr = NULL; |
| 244 | result->aname = NULL; |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 245 | result->refcnt = 1; |
Paul Moore | fd3522f | 2015-01-22 00:00:10 -0500 | [diff] [blame] | 246 | audit_getname(result); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 247 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 248 | return result; |
| 249 | } |
| 250 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 251 | void putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 253 | BUG_ON(name->refcnt <= 0); |
| 254 | |
| 255 | if (--name->refcnt > 0) |
| 256 | return; |
| 257 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 258 | if (name->name != name->iname) { |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 259 | __putname(name->name); |
| 260 | kfree(name); |
| 261 | } else |
| 262 | __putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 265 | static int check_acl(struct inode *inode, int mask) |
| 266 | { |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 267 | #ifdef CONFIG_FS_POSIX_ACL |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 268 | struct posix_acl *acl; |
| 269 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 270 | if (mask & MAY_NOT_BLOCK) { |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 271 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
| 272 | if (!acl) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 273 | return -EAGAIN; |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 274 | /* no ->get_acl() calls in RCU mode... */ |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 275 | if (is_uncached_acl(acl)) |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 276 | return -ECHILD; |
Linus Torvalds | 63d72b9 | 2020-06-07 12:19:06 -0700 | [diff] [blame] | 277 | return posix_acl_permission(inode, acl, mask); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 280 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
| 281 | if (IS_ERR(acl)) |
| 282 | return PTR_ERR(acl); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 283 | if (acl) { |
| 284 | int error = posix_acl_permission(inode, acl, mask); |
| 285 | posix_acl_release(acl); |
| 286 | return error; |
| 287 | } |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 288 | #endif |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 289 | |
| 290 | return -EAGAIN; |
| 291 | } |
| 292 | |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 293 | /* |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 294 | * This does the basic UNIX permission checking. |
| 295 | * |
| 296 | * Note that the POSIX ACL check cares about the MAY_NOT_BLOCK bit, |
| 297 | * for RCU walking. |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 298 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 299 | static int acl_permission_check(struct inode *inode, int mask) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 300 | { |
Linus Torvalds | 26cf46b | 2011-05-13 11:51:01 -0700 | [diff] [blame] | 301 | unsigned int mode = inode->i_mode; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 302 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 303 | /* Are we the owner? If so, ACL's don't matter */ |
| 304 | if (likely(uid_eq(current_fsuid(), inode->i_uid))) { |
| 305 | mask &= 7; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 306 | mode >>= 6; |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 307 | return (mask & ~mode) ? -EACCES : 0; |
| 308 | } |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 309 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 310 | /* Do we have ACL's? */ |
| 311 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
| 312 | int error = check_acl(inode, mask); |
| 313 | if (error != -EAGAIN) |
| 314 | return error; |
| 315 | } |
| 316 | |
| 317 | /* Only RWX matters for group/other mode bits */ |
| 318 | mask &= 7; |
| 319 | |
| 320 | /* |
| 321 | * Are the group permissions different from |
| 322 | * the other permissions in the bits we care |
| 323 | * about? Need to check group ownership if so. |
| 324 | */ |
| 325 | if (mask & (mode ^ (mode >> 3))) { |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 326 | if (in_group_p(inode->i_gid)) |
| 327 | mode >>= 3; |
| 328 | } |
| 329 | |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 330 | /* Bits in 'mode' clear that we require? */ |
| 331 | return (mask & ~mode) ? -EACCES : 0; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | /** |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 335 | * generic_permission - check for access rights on a Posix-like filesystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | * @inode: inode to check access rights for |
Linus Torvalds | 5fc475b | 2020-06-05 13:40:45 -0700 | [diff] [blame] | 337 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, |
| 338 | * %MAY_NOT_BLOCK ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | * |
| 340 | * Used to check for read/write/execute permissions on a file. |
| 341 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 342 | * for filesystem access without changing the "normal" uids which |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 343 | * are used for other things. |
| 344 | * |
| 345 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk |
| 346 | * request cannot be satisfied (eg. requires blocking or too much complexity). |
| 347 | * It would then be called again in ref-walk mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | */ |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 349 | int generic_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 351 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 354 | * Do the basic permission checks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 356 | ret = acl_permission_check(inode, mask); |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 357 | if (ret != -EACCES) |
| 358 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 360 | if (S_ISDIR(inode->i_mode)) { |
| 361 | /* DACs are overridable for directories */ |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 362 | if (!(mask & MAY_WRITE)) |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 363 | if (capable_wrt_inode_uidgid(inode, |
| 364 | CAP_DAC_READ_SEARCH)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 365 | return 0; |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 366 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 368 | return -EACCES; |
| 369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * Searching includes executable on directories, else just read. |
| 373 | */ |
Serge E. Hallyn | 7ea6600 | 2009-12-29 14:50:19 -0600 | [diff] [blame] | 374 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 375 | if (mask == MAY_READ) |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 376 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 378 | /* |
| 379 | * Read/write DACs are always overridable. |
| 380 | * Executable DACs are overridable when there is |
| 381 | * at least one exec bit set. |
| 382 | */ |
| 383 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) |
| 384 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE)) |
| 385 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | return -EACCES; |
| 388 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 389 | EXPORT_SYMBOL(generic_permission); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 391 | /* |
| 392 | * We _really_ want to just do "generic_permission()" without |
| 393 | * even looking at the inode->i_op values. So we keep a cache |
| 394 | * flag in inode->i_opflags, that says "this has not special |
| 395 | * permission function, use the fast case". |
| 396 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 397 | static inline int do_inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 398 | { |
| 399 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { |
| 400 | if (likely(inode->i_op->permission)) |
| 401 | return inode->i_op->permission(inode, mask); |
| 402 | |
| 403 | /* This gets set once for the inode lifetime */ |
| 404 | spin_lock(&inode->i_lock); |
| 405 | inode->i_opflags |= IOP_FASTPERM; |
| 406 | spin_unlock(&inode->i_lock); |
| 407 | } |
| 408 | return generic_permission(inode, mask); |
| 409 | } |
| 410 | |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 411 | /** |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 412 | * sb_permission - Check superblock-level permissions |
| 413 | * @sb: Superblock of inode to check permission on |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 414 | * @inode: Inode to check permission on |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 415 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 416 | * |
| 417 | * Separate out file-system wide checks from inode-specific permission checks. |
| 418 | */ |
| 419 | static int sb_permission(struct super_block *sb, struct inode *inode, int mask) |
| 420 | { |
| 421 | if (unlikely(mask & MAY_WRITE)) { |
| 422 | umode_t mode = inode->i_mode; |
| 423 | |
| 424 | /* Nobody gets write access to a read-only fs. */ |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 425 | 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] | 426 | return -EROFS; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /** |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 432 | * inode_permission - Check for access rights to a given inode |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 433 | * @inode: Inode to check permission on |
| 434 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 435 | * |
| 436 | * Check for read/write/execute permissions on an inode. We use fs[ug]id for |
| 437 | * this, letting us set arbitrary permissions for filesystem access without |
| 438 | * changing the "normal" UIDs which are used for other things. |
| 439 | * |
| 440 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. |
| 441 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 442 | int inode_permission(struct inode *inode, int mask) |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 443 | { |
| 444 | int retval; |
| 445 | |
| 446 | retval = sb_permission(inode->i_sb, inode, mask); |
| 447 | if (retval) |
| 448 | return retval; |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 449 | |
| 450 | if (unlikely(mask & MAY_WRITE)) { |
| 451 | /* |
| 452 | * Nobody gets write access to an immutable file. |
| 453 | */ |
| 454 | if (IS_IMMUTABLE(inode)) |
| 455 | return -EPERM; |
| 456 | |
| 457 | /* |
| 458 | * Updating mtime will likely cause i_uid and i_gid to be |
| 459 | * written back improperly if their true value is unknown |
| 460 | * to the vfs. |
| 461 | */ |
| 462 | if (HAS_UNMAPPED_ID(inode)) |
| 463 | return -EACCES; |
| 464 | } |
| 465 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 466 | retval = do_inode_permission(inode, mask); |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 467 | if (retval) |
| 468 | return retval; |
| 469 | |
| 470 | retval = devcgroup_inode_permission(inode, mask); |
| 471 | if (retval) |
| 472 | return retval; |
| 473 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 474 | return security_inode_permission(inode, mask); |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 475 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 476 | EXPORT_SYMBOL(inode_permission); |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 477 | |
| 478 | /** |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 479 | * path_get - get a reference to a path |
| 480 | * @path: path to get the reference to |
| 481 | * |
| 482 | * Given a path increment the reference count to the dentry and the vfsmount. |
| 483 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 484 | void path_get(const struct path *path) |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 485 | { |
| 486 | mntget(path->mnt); |
| 487 | dget(path->dentry); |
| 488 | } |
| 489 | EXPORT_SYMBOL(path_get); |
| 490 | |
| 491 | /** |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 492 | * path_put - put a reference to a path |
| 493 | * @path: path to put the reference to |
| 494 | * |
| 495 | * Given a path decrement the reference count to the dentry and the vfsmount. |
| 496 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 497 | void path_put(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 499 | dput(path->dentry); |
| 500 | mntput(path->mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 502 | EXPORT_SYMBOL(path_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 504 | #define EMBEDDED_LEVELS 2 |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 505 | struct nameidata { |
| 506 | struct path path; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 507 | struct qstr last; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 508 | struct path root; |
| 509 | struct inode *inode; /* path.dentry.d_inode */ |
| 510 | unsigned int flags; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 511 | unsigned seq, m_seq, r_seq; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 512 | int last_type; |
| 513 | unsigned depth; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 514 | int total_link_count; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 515 | struct saved { |
| 516 | struct path link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 517 | struct delayed_call done; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 518 | const char *name; |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 519 | unsigned seq; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 520 | } *stack, internal[EMBEDDED_LEVELS]; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 521 | struct filename *name; |
| 522 | struct nameidata *saved; |
| 523 | unsigned root_seq; |
| 524 | int dfd; |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 525 | kuid_t dir_uid; |
| 526 | umode_t dir_mode; |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 527 | } __randomize_layout; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 528 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 529 | static void set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 530 | { |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 531 | struct nameidata *old = current->nameidata; |
| 532 | p->stack = p->internal; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 533 | p->dfd = dfd; |
| 534 | p->name = name; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 535 | p->total_link_count = old ? old->total_link_count : 0; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 536 | p->saved = old; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 537 | current->nameidata = p; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 538 | } |
| 539 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 540 | static void restore_nameidata(void) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 541 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 542 | struct nameidata *now = current->nameidata, *old = now->saved; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 543 | |
| 544 | current->nameidata = old; |
| 545 | if (old) |
| 546 | old->total_link_count = now->total_link_count; |
Al Viro | e1a63bb | 2015-12-05 21:06:33 -0500 | [diff] [blame] | 547 | if (now->stack != now->internal) |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 548 | kfree(now->stack); |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 551 | static bool nd_alloc_stack(struct nameidata *nd) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 552 | { |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 553 | struct saved *p; |
| 554 | |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 555 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
| 556 | nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); |
| 557 | if (unlikely(!p)) |
| 558 | return false; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 559 | memcpy(p, nd->internal, sizeof(nd->internal)); |
| 560 | nd->stack = p; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 561 | return true; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 564 | /** |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 565 | * path_connected - Verify that a dentry is below mnt.mnt_root |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 566 | * |
| 567 | * Rename can sometimes move a file or directory outside of a bind |
| 568 | * mount, path_connected allows those cases to be detected. |
| 569 | */ |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 570 | static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 571 | { |
Eric W. Biederman | 95dd775 | 2018-03-14 18:20:29 -0500 | [diff] [blame] | 572 | struct super_block *sb = mnt->mnt_sb; |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 573 | |
Christoph Hellwig | 402dd2c | 2020-09-24 08:51:28 +0200 | [diff] [blame] | 574 | /* Bind mounts can have disconnected paths */ |
| 575 | if (mnt->mnt_root == sb->s_root) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 576 | return true; |
| 577 | |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 578 | return is_subdir(dentry, mnt->mnt_root); |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 581 | static void drop_links(struct nameidata *nd) |
| 582 | { |
| 583 | int i = nd->depth; |
| 584 | while (i--) { |
| 585 | struct saved *last = nd->stack + i; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 586 | do_delayed_call(&last->done); |
| 587 | clear_delayed_call(&last->done); |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | static void terminate_walk(struct nameidata *nd) |
| 592 | { |
| 593 | drop_links(nd); |
| 594 | if (!(nd->flags & LOOKUP_RCU)) { |
| 595 | int i; |
| 596 | path_put(&nd->path); |
| 597 | for (i = 0; i < nd->depth; i++) |
| 598 | path_put(&nd->stack[i].link); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 599 | if (nd->flags & LOOKUP_ROOT_GRABBED) { |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 600 | path_put(&nd->root); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 601 | nd->flags &= ~LOOKUP_ROOT_GRABBED; |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 602 | } |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 603 | } else { |
| 604 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 605 | rcu_read_unlock(); |
| 606 | } |
| 607 | nd->depth = 0; |
| 608 | } |
| 609 | |
| 610 | /* path_put is needed afterwards regardless of success or failure */ |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 611 | static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 612 | { |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 613 | int res = __legitimize_mnt(path->mnt, mseq); |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 614 | if (unlikely(res)) { |
| 615 | if (res > 0) |
| 616 | path->mnt = NULL; |
| 617 | path->dentry = NULL; |
| 618 | return false; |
| 619 | } |
| 620 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { |
| 621 | path->dentry = NULL; |
| 622 | return false; |
| 623 | } |
| 624 | return !read_seqcount_retry(&path->dentry->d_seq, seq); |
| 625 | } |
| 626 | |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 627 | static inline bool legitimize_path(struct nameidata *nd, |
| 628 | struct path *path, unsigned seq) |
| 629 | { |
Al Viro | 5bd7328 | 2020-04-05 21:59:55 -0400 | [diff] [blame] | 630 | return __legitimize_path(path, seq, nd->m_seq); |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 631 | } |
| 632 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 633 | static bool legitimize_links(struct nameidata *nd) |
| 634 | { |
| 635 | int i; |
| 636 | for (i = 0; i < nd->depth; i++) { |
| 637 | struct saved *last = nd->stack + i; |
| 638 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { |
| 639 | drop_links(nd); |
| 640 | nd->depth = i + 1; |
| 641 | return false; |
| 642 | } |
| 643 | } |
| 644 | return true; |
| 645 | } |
| 646 | |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 647 | static bool legitimize_root(struct nameidata *nd) |
| 648 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 649 | /* |
| 650 | * For scoped-lookups (where nd->root has been zeroed), we need to |
| 651 | * restart the whole lookup from scratch -- because set_root() is wrong |
| 652 | * for these lookups (nd->dfd is the root, not the filesystem root). |
| 653 | */ |
| 654 | if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED)) |
| 655 | return false; |
| 656 | /* Nothing to do if nd->root is zero or is managed by the VFS user. */ |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 657 | if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT)) |
| 658 | return true; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 659 | nd->flags |= LOOKUP_ROOT_GRABBED; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 660 | return legitimize_path(nd, &nd->root, nd->root_seq); |
| 661 | } |
| 662 | |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 663 | /* |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 664 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 665 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
| 666 | * 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] | 667 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 668 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
| 669 | * got stuck, so ref-walk may continue from there. If this is not successful |
| 670 | * (eg. a seqcount has changed), then failure is returned and it's up to caller |
| 671 | * to restart the path walk from the beginning in ref-walk mode. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 672 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 673 | |
| 674 | /** |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 675 | * try_to_unlazy - try to switch to ref-walk mode. |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 676 | * @nd: nameidata pathwalk data |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 677 | * Returns: true on success, false on failure |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 678 | * |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 679 | * 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] | 680 | * for ref-walk mode. |
| 681 | * Must be called from rcu-walk context. |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 682 | * Nothing should touch nameidata between try_to_unlazy() failure and |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 683 | * terminate_walk(). |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 684 | */ |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 685 | static bool try_to_unlazy(struct nameidata *nd) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 686 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 687 | struct dentry *parent = nd->path.dentry; |
| 688 | |
| 689 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 690 | |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 691 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 692 | if (unlikely(!legitimize_links(nd))) |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 693 | goto out1; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 694 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
| 695 | goto out; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 696 | if (unlikely(!legitimize_root(nd))) |
| 697 | goto out; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 698 | rcu_read_unlock(); |
| 699 | BUG_ON(nd->inode != parent->d_inode); |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 700 | return true; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 701 | |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 702 | out1: |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 703 | nd->path.mnt = NULL; |
| 704 | nd->path.dentry = NULL; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 705 | out: |
| 706 | rcu_read_unlock(); |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 707 | return false; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
| 711 | * unlazy_child - try to switch to ref-walk mode. |
| 712 | * @nd: nameidata pathwalk data |
| 713 | * @dentry: child of nd->path.dentry |
| 714 | * @seq: seq number to check dentry against |
| 715 | * Returns: 0 on success, -ECHILD on failure |
| 716 | * |
| 717 | * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry |
| 718 | * for ref-walk mode. @dentry must be a path found by a do_lookup call on |
| 719 | * @nd. Must be called from rcu-walk context. |
| 720 | * Nothing should touch nameidata between unlazy_child() failure and |
| 721 | * terminate_walk(). |
| 722 | */ |
| 723 | static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq) |
| 724 | { |
| 725 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
| 726 | |
| 727 | nd->flags &= ~LOOKUP_RCU; |
| 728 | if (unlikely(!legitimize_links(nd))) |
| 729 | goto out2; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 730 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) |
| 731 | goto out2; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 732 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 733 | goto out1; |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 734 | |
Linus Torvalds | 1557008 | 2013-09-02 11:38:06 -0700 | [diff] [blame] | 735 | /* |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 736 | * We need to move both the parent and the dentry from the RCU domain |
| 737 | * to be properly refcounted. And the sequence number in the dentry |
| 738 | * validates *both* dentry counters, since we checked the sequence |
| 739 | * number of the parent after we got the child sequence number. So we |
| 740 | * 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] | 741 | */ |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 742 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
| 743 | goto out; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 744 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
| 745 | goto out_dput; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 746 | /* |
| 747 | * Sequence counts matched. Now make sure that the root is |
| 748 | * still valid and get it if required. |
| 749 | */ |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 750 | if (unlikely(!legitimize_root(nd))) |
| 751 | goto out_dput; |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 752 | rcu_read_unlock(); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 753 | return 0; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 754 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 755 | out2: |
| 756 | nd->path.mnt = NULL; |
| 757 | out1: |
| 758 | nd->path.dentry = NULL; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 759 | out: |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 760 | rcu_read_unlock(); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 761 | return -ECHILD; |
| 762 | out_dput: |
| 763 | rcu_read_unlock(); |
| 764 | dput(dentry); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 765 | return -ECHILD; |
| 766 | } |
| 767 | |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 768 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 769 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 770 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
| 771 | return dentry->d_op->d_revalidate(dentry, flags); |
| 772 | else |
| 773 | return 1; |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 774 | } |
| 775 | |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 776 | #define INIT_PATH_SIZE 64 |
| 777 | |
| 778 | static void success_walk_trace(struct nameidata *nd) |
| 779 | { |
| 780 | struct path *pt = &nd->path; |
| 781 | struct inode *i = nd->inode; |
| 782 | char buf[INIT_PATH_SIZE], *try_buf; |
| 783 | int cur_path_size; |
| 784 | char *p; |
| 785 | |
| 786 | /* When eBPF/ tracepoint is disabled, keep overhead low. */ |
| 787 | if (!trace_inodepath_enabled()) |
| 788 | return; |
| 789 | |
| 790 | /* First try stack allocated buffer. */ |
| 791 | try_buf = buf; |
| 792 | cur_path_size = INIT_PATH_SIZE; |
| 793 | |
| 794 | while (cur_path_size <= PATH_MAX) { |
| 795 | /* Free previous heap allocation if we are now trying |
| 796 | * a second or later heap allocation. |
| 797 | */ |
| 798 | if (try_buf != buf) |
| 799 | kfree(try_buf); |
| 800 | |
| 801 | /* All but the first alloc are on the heap. */ |
| 802 | if (cur_path_size != INIT_PATH_SIZE) { |
| 803 | try_buf = kmalloc(cur_path_size, GFP_KERNEL); |
| 804 | if (!try_buf) { |
| 805 | try_buf = buf; |
| 806 | sprintf(try_buf, "error:buf_alloc_failed"); |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | p = d_path(pt, try_buf, cur_path_size); |
| 812 | |
| 813 | if (!IS_ERR(p)) { |
| 814 | char *end = mangle_path(try_buf, p, "\n"); |
| 815 | |
| 816 | if (end) { |
| 817 | try_buf[end - try_buf] = 0; |
| 818 | break; |
| 819 | } else { |
| 820 | /* On mangle errors, double path size |
| 821 | * till PATH_MAX. |
| 822 | */ |
| 823 | cur_path_size = cur_path_size << 1; |
| 824 | continue; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (PTR_ERR(p) == -ENAMETOOLONG) { |
| 829 | /* If d_path complains that name is too long, |
| 830 | * then double path size till PATH_MAX. |
| 831 | */ |
| 832 | cur_path_size = cur_path_size << 1; |
| 833 | continue; |
| 834 | } |
| 835 | |
| 836 | sprintf(try_buf, "error:d_path_failed_%lu", |
| 837 | -1 * PTR_ERR(p)); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | if (cur_path_size > PATH_MAX) |
| 842 | sprintf(try_buf, "error:d_path_name_too_long"); |
| 843 | |
| 844 | trace_inodepath(i, try_buf); |
| 845 | |
| 846 | if (try_buf != buf) |
| 847 | kfree(try_buf); |
| 848 | return; |
| 849 | } |
| 850 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 851 | /** |
| 852 | * complete_walk - successful completion of path walk |
| 853 | * @nd: pointer nameidata |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 854 | * |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 855 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
| 856 | * Revalidate the final result, unless we'd already done that during |
| 857 | * the path walk or the filesystem doesn't ask for it. Return 0 on |
| 858 | * success, -error on failure. In case of failure caller does not |
| 859 | * need to drop nd->path. |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 860 | */ |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 861 | static int complete_walk(struct nameidata *nd) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 862 | { |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 863 | struct dentry *dentry = nd->path.dentry; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 864 | int status; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 865 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 866 | if (nd->flags & LOOKUP_RCU) { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 867 | /* |
| 868 | * We don't want to zero nd->root for scoped-lookups or |
| 869 | * externally-managed nd->root. |
| 870 | */ |
| 871 | if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 872 | nd->root.mnt = NULL; |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 873 | if (!try_to_unlazy(nd)) |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 874 | return -ECHILD; |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 877 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 878 | /* |
| 879 | * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't |
| 880 | * ever step outside the root during lookup" and should already |
| 881 | * be guaranteed by the rest of namei, we want to avoid a namei |
| 882 | * BUG resulting in userspace being given a path that was not |
| 883 | * scoped within the root at some point during the lookup. |
| 884 | * |
| 885 | * So, do a final sanity-check to make sure that in the |
| 886 | * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) |
| 887 | * we won't silently return an fd completely outside of the |
| 888 | * requested root to userspace. |
| 889 | * |
| 890 | * Userspace could move the path outside the root after this |
| 891 | * check, but as discussed elsewhere this is not a concern (the |
| 892 | * resolved file was inside the root at some point). |
| 893 | */ |
| 894 | if (!path_is_under(&nd->path, &nd->root)) |
| 895 | return -EXDEV; |
| 896 | } |
| 897 | |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 898 | if (likely(!(nd->flags & LOOKUP_JUMPED))) { |
| 899 | success_walk_trace(nd); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 900 | return 0; |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 901 | } |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 902 | |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 903 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) { |
| 904 | success_walk_trace(nd); |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 905 | return 0; |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 906 | } |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 907 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 908 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 909 | if (status > 0) { |
| 910 | success_walk_trace(nd); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 911 | return 0; |
Joel Fernandes | 1304fea | 2019-03-14 17:32:39 -0400 | [diff] [blame] | 912 | } |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 913 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 914 | if (!status) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 915 | status = -ESTALE; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 916 | |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 917 | return status; |
| 918 | } |
| 919 | |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 920 | static int set_root(struct nameidata *nd) |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 921 | { |
Al Viro | 7bd8837 | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 922 | struct fs_struct *fs = current->fs; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 923 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 924 | /* |
| 925 | * Jumping to the real root in a scoped-lookup is a BUG in namei, but we |
| 926 | * still have to ensure it doesn't happen because it will cause a breakout |
| 927 | * from the dirfd. |
| 928 | */ |
| 929 | if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) |
| 930 | return -ENOTRECOVERABLE; |
| 931 | |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 932 | if (nd->flags & LOOKUP_RCU) { |
| 933 | unsigned seq; |
| 934 | |
| 935 | do { |
| 936 | seq = read_seqcount_begin(&fs->seq); |
| 937 | nd->root = fs->root; |
| 938 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); |
| 939 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 940 | } else { |
| 941 | get_fs_root(fs, &nd->root); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 942 | nd->flags |= LOOKUP_ROOT_GRABBED; |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 943 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 944 | return 0; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 945 | } |
| 946 | |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 947 | static int nd_jump_root(struct nameidata *nd) |
| 948 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 949 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 950 | return -EXDEV; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 951 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 952 | /* Absolute path arguments to path_init() are allowed. */ |
| 953 | if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) |
| 954 | return -EXDEV; |
| 955 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 956 | if (!nd->root.mnt) { |
| 957 | int error = set_root(nd); |
| 958 | if (error) |
| 959 | return error; |
| 960 | } |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 961 | if (nd->flags & LOOKUP_RCU) { |
| 962 | struct dentry *d; |
| 963 | nd->path = nd->root; |
| 964 | d = nd->path.dentry; |
| 965 | nd->inode = d->d_inode; |
| 966 | nd->seq = nd->root_seq; |
| 967 | if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) |
| 968 | return -ECHILD; |
| 969 | } else { |
| 970 | path_put(&nd->path); |
| 971 | nd->path = nd->root; |
| 972 | path_get(&nd->path); |
| 973 | nd->inode = nd->path.dentry->d_inode; |
| 974 | } |
| 975 | nd->flags |= LOOKUP_JUMPED; |
| 976 | return 0; |
| 977 | } |
| 978 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 979 | /* |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 980 | * Helper to directly jump to a known parsed path from ->get_link, |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 981 | * caller must have taken a reference to path beforehand. |
| 982 | */ |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 983 | int nd_jump_link(struct path *path) |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 984 | { |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 985 | int error = -ELOOP; |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 986 | struct nameidata *nd = current->nameidata; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 987 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 988 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
| 989 | goto err; |
| 990 | |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 991 | error = -EXDEV; |
| 992 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 993 | if (nd->path.mnt != path->mnt) |
| 994 | goto err; |
| 995 | } |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 996 | /* Not currently safe for scoped-lookups. */ |
| 997 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) |
| 998 | goto err; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 999 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 1000 | path_put(&nd->path); |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 1001 | nd->path = *path; |
| 1002 | nd->inode = nd->path.dentry->d_inode; |
| 1003 | nd->flags |= LOOKUP_JUMPED; |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 1004 | return 0; |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 1005 | |
| 1006 | err: |
| 1007 | path_put(path); |
| 1008 | return error; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 1009 | } |
| 1010 | |
Al Viro | b9ff442 | 2015-05-02 20:19:23 -0400 | [diff] [blame] | 1011 | static inline void put_link(struct nameidata *nd) |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1012 | { |
Al Viro | 21c3003 | 2015-05-03 21:06:24 -0400 | [diff] [blame] | 1013 | struct saved *last = nd->stack + --nd->depth; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1014 | do_delayed_call(&last->done); |
Al Viro | 6548fae | 2015-05-07 20:32:22 -0400 | [diff] [blame] | 1015 | if (!(nd->flags & LOOKUP_RCU)) |
| 1016 | path_put(&last->link); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
Linus Torvalds | 561ec64 | 2012-10-26 10:05:07 -0700 | [diff] [blame] | 1019 | int sysctl_protected_symlinks __read_mostly = 0; |
| 1020 | int sysctl_protected_hardlinks __read_mostly = 0; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1021 | int sysctl_protected_fifos __read_mostly; |
| 1022 | int sysctl_protected_regular __read_mostly; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1023 | |
| 1024 | /** |
| 1025 | * may_follow_link - Check symlink following for unsafe situations |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 1026 | * @nd: nameidata pathwalk data |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1027 | * |
| 1028 | * In the case of the sysctl_protected_symlinks sysctl being enabled, |
| 1029 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is |
| 1030 | * in a sticky world-writable directory. This is to protect privileged |
| 1031 | * processes from failing races against path names that may change out |
| 1032 | * from under them by way of other users creating malicious symlinks. |
| 1033 | * It will permit symlinks to be followed only when outside a sticky |
| 1034 | * world-writable directory, or when the uid of the symlink and follower |
| 1035 | * match, or when the directory owner matches the symlink's owner. |
| 1036 | * |
| 1037 | * Returns 0 if following the symlink is allowed, -ve on error. |
| 1038 | */ |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1039 | 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] | 1040 | { |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1041 | if (!sysctl_protected_symlinks) |
| 1042 | return 0; |
| 1043 | |
| 1044 | /* Allowed if owner and follower match. */ |
Eric W. Biederman | 81abe27 | 2012-08-03 09:38:08 -0700 | [diff] [blame] | 1045 | if (uid_eq(current_cred()->fsuid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1046 | return 0; |
| 1047 | |
| 1048 | /* Allowed if parent directory not sticky and world-writable. */ |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 1049 | if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1050 | return 0; |
| 1051 | |
| 1052 | /* Allowed if parent directory and link owner match. */ |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 1053 | if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1054 | return 0; |
| 1055 | |
Al Viro | 31956502 | 2015-05-07 20:37:40 -0400 | [diff] [blame] | 1056 | if (nd->flags & LOOKUP_RCU) |
| 1057 | return -ECHILD; |
| 1058 | |
Richard Guy Briggs | ea841ba | 2018-03-21 04:42:21 -0400 | [diff] [blame] | 1059 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1060 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1061 | return -EACCES; |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * safe_hardlink_source - Check for safe hardlink conditions |
| 1066 | * @inode: the source inode to hardlink from |
| 1067 | * |
| 1068 | * Return false if at least one of the following conditions: |
| 1069 | * - inode is not a regular file |
| 1070 | * - inode is setuid |
| 1071 | * - inode is setgid and group-exec |
| 1072 | * - access failure for read and write |
| 1073 | * |
| 1074 | * Otherwise returns true. |
| 1075 | */ |
| 1076 | static bool safe_hardlink_source(struct inode *inode) |
| 1077 | { |
| 1078 | umode_t mode = inode->i_mode; |
| 1079 | |
| 1080 | /* Special files should not get pinned to the filesystem. */ |
| 1081 | if (!S_ISREG(mode)) |
| 1082 | return false; |
| 1083 | |
| 1084 | /* Setuid files should not get pinned to the filesystem. */ |
| 1085 | if (mode & S_ISUID) |
| 1086 | return false; |
| 1087 | |
| 1088 | /* Executable setgid files should not get pinned to the filesystem. */ |
| 1089 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 1090 | return false; |
| 1091 | |
| 1092 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ |
| 1093 | if (inode_permission(inode, MAY_READ | MAY_WRITE)) |
| 1094 | return false; |
| 1095 | |
| 1096 | return true; |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * may_linkat - Check permissions for creating a hardlink |
| 1101 | * @link: the source to hardlink from |
| 1102 | * |
| 1103 | * Block hardlink when all of: |
| 1104 | * - sysctl_protected_hardlinks enabled |
| 1105 | * - fsuid does not match inode |
| 1106 | * - hardlink source is unsafe (see safe_hardlink_source() above) |
Dirk Steinmetz | f2ca379 | 2015-10-20 16:09:19 +0200 | [diff] [blame] | 1107 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1108 | * |
| 1109 | * Returns 0 if successful, -ve on error. |
| 1110 | */ |
Christoph Hellwig | 812931d | 2020-07-22 11:14:19 +0200 | [diff] [blame] | 1111 | int may_linkat(struct path *link) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1112 | { |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 1113 | struct inode *inode = link->dentry->d_inode; |
| 1114 | |
| 1115 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
| 1116 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) |
| 1117 | return -EOVERFLOW; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1118 | |
| 1119 | if (!sysctl_protected_hardlinks) |
| 1120 | return 0; |
| 1121 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1122 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
| 1123 | * otherwise, it must be a safe source. |
| 1124 | */ |
Kees Cook | cc658db | 2017-06-21 09:53:06 -0700 | [diff] [blame] | 1125 | if (safe_hardlink_source(inode) || inode_owner_or_capable(inode)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1126 | return 0; |
| 1127 | |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1128 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1129 | return -EPERM; |
| 1130 | } |
| 1131 | |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory |
| 1134 | * should be allowed, or not, on files that already |
| 1135 | * exist. |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1136 | * @dir_mode: mode bits of directory |
| 1137 | * @dir_uid: owner of directory |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1138 | * @inode: the inode of the file to open |
| 1139 | * |
| 1140 | * Block an O_CREAT open of a FIFO (or a regular file) when: |
| 1141 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled |
| 1142 | * - the file already exists |
| 1143 | * - we are in a sticky directory |
| 1144 | * - we don't own the file |
| 1145 | * - the owner of the directory doesn't own the file |
| 1146 | * - the directory is world writable |
| 1147 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 |
| 1148 | * the directory doesn't have to be world writable: being group writable will |
| 1149 | * be enough. |
| 1150 | * |
| 1151 | * Returns 0 if the open is allowed, -ve on error. |
| 1152 | */ |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1153 | static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid, |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1154 | struct inode * const inode) |
| 1155 | { |
| 1156 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || |
| 1157 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1158 | likely(!(dir_mode & S_ISVTX)) || |
| 1159 | uid_eq(inode->i_uid, dir_uid) || |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1160 | uid_eq(current_fsuid(), inode->i_uid)) |
| 1161 | return 0; |
| 1162 | |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1163 | if (likely(dir_mode & 0002) || |
| 1164 | (dir_mode & 0020 && |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1165 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
| 1166 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1167 | const char *operation = S_ISFIFO(inode->i_mode) ? |
| 1168 | "sticky_create_fifo" : |
| 1169 | "sticky_create_regular"; |
| 1170 | audit_log_path_denied(AUDIT_ANOM_CREAT, operation); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1171 | return -EACCES; |
| 1172 | } |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
David Howells | f015f126 | 2012-06-25 12:55:28 +0100 | [diff] [blame] | 1176 | /* |
| 1177 | * follow_up - Find the mountpoint of path's vfsmount |
| 1178 | * |
| 1179 | * Given a path, find the mountpoint of its source file system. |
| 1180 | * Replace @path with the path of the mountpoint in the parent mount. |
| 1181 | * Up is towards /. |
| 1182 | * |
| 1183 | * Return 1 if we went up a level and 0 if we were already at the |
| 1184 | * root. |
| 1185 | */ |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1186 | int follow_up(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1188 | struct mount *mnt = real_mount(path->mnt); |
| 1189 | struct mount *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | struct dentry *mountpoint; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1191 | |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1192 | read_seqlock_excl(&mount_lock); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1193 | parent = mnt->mnt_parent; |
Al Viro | 3c0a616 | 2012-07-18 17:32:50 +0400 | [diff] [blame] | 1194 | if (parent == mnt) { |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1195 | read_sequnlock_excl(&mount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | return 0; |
| 1197 | } |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1198 | mntget(&parent->mnt); |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 1199 | mountpoint = dget(mnt->mnt_mountpoint); |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1200 | read_sequnlock_excl(&mount_lock); |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1201 | dput(path->dentry); |
| 1202 | path->dentry = mountpoint; |
| 1203 | mntput(path->mnt); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1204 | path->mnt = &parent->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | return 1; |
| 1206 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1207 | EXPORT_SYMBOL(follow_up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1209 | static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, |
| 1210 | struct path *path, unsigned *seqp) |
| 1211 | { |
| 1212 | while (mnt_has_parent(m)) { |
| 1213 | struct dentry *mountpoint = m->mnt_mountpoint; |
| 1214 | |
| 1215 | m = m->mnt_parent; |
| 1216 | if (unlikely(root->dentry == mountpoint && |
| 1217 | root->mnt == &m->mnt)) |
| 1218 | break; |
| 1219 | if (mountpoint != m->mnt.mnt_root) { |
| 1220 | path->mnt = &m->mnt; |
| 1221 | path->dentry = mountpoint; |
| 1222 | *seqp = read_seqcount_begin(&mountpoint->d_seq); |
| 1223 | return true; |
| 1224 | } |
| 1225 | } |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1229 | static bool choose_mountpoint(struct mount *m, const struct path *root, |
| 1230 | struct path *path) |
| 1231 | { |
| 1232 | bool found; |
| 1233 | |
| 1234 | rcu_read_lock(); |
| 1235 | while (1) { |
| 1236 | unsigned seq, mseq = read_seqbegin(&mount_lock); |
| 1237 | |
| 1238 | found = choose_mountpoint_rcu(m, root, path, &seq); |
| 1239 | if (unlikely(!found)) { |
| 1240 | if (!read_seqretry(&mount_lock, mseq)) |
| 1241 | break; |
| 1242 | } else { |
| 1243 | if (likely(__legitimize_path(path, seq, mseq))) |
| 1244 | break; |
| 1245 | rcu_read_unlock(); |
| 1246 | path_put(path); |
| 1247 | rcu_read_lock(); |
| 1248 | } |
| 1249 | } |
| 1250 | rcu_read_unlock(); |
| 1251 | return found; |
| 1252 | } |
| 1253 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 1254 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1255 | * Perform an automount |
| 1256 | * - return -EISDIR to tell follow_managed() to stop and return the path we |
| 1257 | * were called with. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1259 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1260 | { |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1261 | struct dentry *dentry = path->dentry; |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 1262 | |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1263 | /* We don't want to mount if someone's just doing a stat - |
| 1264 | * unless they're stat'ing a directory and appended a '/' to |
| 1265 | * the name. |
| 1266 | * |
| 1267 | * We do, however, want to mount if someone wants to open or |
| 1268 | * create a file of any type under the mountpoint, wants to |
| 1269 | * traverse through the mountpoint or wants to open the |
| 1270 | * mounted directory. Also, autofs may mark negative dentries |
| 1271 | * as being automount points. These will need the attentions |
| 1272 | * of the daemon to instantiate them before they can be used. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1273 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1274 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1275 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1276 | dentry->d_inode) |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1277 | return -EISDIR; |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1278 | |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1279 | if (count && (*count)++ >= MAXSYMLINKS) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1280 | return -ELOOP; |
| 1281 | |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1282 | return finish_automount(dentry->d_op->d_automount(path), path); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1286 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
| 1287 | * dentries are pinned but not locked here, so negative dentry can go |
| 1288 | * positive right under us. Use of smp_load_acquire() provides a barrier |
| 1289 | * sufficient for ->d_inode and ->d_flags consistency. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1290 | */ |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1291 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
| 1292 | int *count, unsigned lookup_flags) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1293 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1294 | struct vfsmount *mnt = path->mnt; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1295 | bool need_mntput = false; |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1296 | int ret = 0; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1297 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1298 | while (flags & DCACHE_MANAGED_DENTRY) { |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1299 | /* Allow the filesystem to manage the transit without i_mutex |
| 1300 | * being held. */ |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1301 | if (flags & DCACHE_MANAGE_TRANSIT) { |
Ian Kent | fb5f51c | 2016-11-24 08:03:41 +1100 | [diff] [blame] | 1302 | ret = path->dentry->d_op->d_manage(path, false); |
Al Viro | 508c877 | 2020-01-14 22:09:57 -0500 | [diff] [blame] | 1303 | flags = smp_load_acquire(&path->dentry->d_flags); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1304 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1305 | break; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1308 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1309 | struct vfsmount *mounted = lookup_mnt(path); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1310 | if (mounted) { // ... in our namespace |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1311 | dput(path->dentry); |
| 1312 | if (need_mntput) |
| 1313 | mntput(path->mnt); |
| 1314 | path->mnt = mounted; |
| 1315 | path->dentry = dget(mounted->mnt_root); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1316 | // here we know it's positive |
| 1317 | flags = path->dentry->d_flags; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1318 | need_mntput = true; |
| 1319 | continue; |
| 1320 | } |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1323 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
| 1324 | break; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1325 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1326 | // uncovered automount point |
| 1327 | ret = follow_automount(path, count, lookup_flags); |
| 1328 | flags = smp_load_acquire(&path->dentry->d_flags); |
| 1329 | if (ret < 0) |
| 1330 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1332 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1333 | if (ret == -EISDIR) |
| 1334 | ret = 0; |
| 1335 | // possible if you race with several mount --move |
| 1336 | if (need_mntput && path->mnt == mnt) |
| 1337 | mntput(path->mnt); |
| 1338 | if (!ret && unlikely(d_flags_negative(flags))) |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1339 | ret = -ENOENT; |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1340 | *jumped = need_mntput; |
Al Viro | 8402752 | 2015-04-22 10:30:08 -0400 | [diff] [blame] | 1341 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1344 | static inline int traverse_mounts(struct path *path, bool *jumped, |
| 1345 | int *count, unsigned lookup_flags) |
| 1346 | { |
| 1347 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); |
| 1348 | |
| 1349 | /* fastpath */ |
| 1350 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { |
| 1351 | *jumped = false; |
| 1352 | if (unlikely(d_flags_negative(flags))) |
| 1353 | return -ENOENT; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); |
| 1357 | } |
| 1358 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1359 | int follow_down_one(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | { |
| 1361 | struct vfsmount *mounted; |
| 1362 | |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 1363 | mounted = lookup_mnt(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | if (mounted) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1365 | dput(path->dentry); |
| 1366 | mntput(path->mnt); |
| 1367 | path->mnt = mounted; |
| 1368 | path->dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | return 1; |
| 1370 | } |
| 1371 | return 0; |
| 1372 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1373 | EXPORT_SYMBOL(follow_down_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1375 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1376 | * Follow down to the covering mount currently visible to userspace. At each |
| 1377 | * point, the filesystem owning that dentry may be queried as to whether the |
| 1378 | * caller is permitted to proceed or not. |
| 1379 | */ |
| 1380 | int follow_down(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1382 | struct vfsmount *mnt = path->mnt; |
| 1383 | bool jumped; |
| 1384 | int ret = traverse_mounts(path, &jumped, NULL, 0); |
| 1385 | |
| 1386 | if (path->mnt != mnt) |
| 1387 | mntput(mnt); |
| 1388 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1390 | EXPORT_SYMBOL(follow_down); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1392 | /* |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1393 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
| 1394 | * we meet a managed dentry that would need blocking. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1395 | */ |
| 1396 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1397 | struct inode **inode, unsigned *seqp) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1398 | { |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1399 | struct dentry *dentry = path->dentry; |
| 1400 | unsigned int flags = dentry->d_flags; |
| 1401 | |
| 1402 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) |
| 1403 | return true; |
| 1404 | |
| 1405 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1406 | return false; |
| 1407 | |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1408 | for (;;) { |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1409 | /* |
| 1410 | * Don't forget we might have a non-mountpoint managed dentry |
| 1411 | * that wants to block transit. |
| 1412 | */ |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1413 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
| 1414 | int res = dentry->d_op->d_manage(path, true); |
| 1415 | if (res) |
| 1416 | return res == -EISDIR; |
| 1417 | flags = dentry->d_flags; |
NeilBrown | b8faf03 | 2014-08-04 17:06:29 +1000 | [diff] [blame] | 1418 | } |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1419 | |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1420 | if (flags & DCACHE_MOUNTED) { |
| 1421 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); |
| 1422 | if (mounted) { |
| 1423 | path->mnt = &mounted->mnt; |
| 1424 | dentry = path->dentry = mounted->mnt.mnt_root; |
| 1425 | nd->flags |= LOOKUP_JUMPED; |
| 1426 | *seqp = read_seqcount_begin(&dentry->d_seq); |
| 1427 | *inode = dentry->d_inode; |
| 1428 | /* |
| 1429 | * We don't need to re-check ->d_seq after this |
| 1430 | * ->d_inode read - there will be an RCU delay |
| 1431 | * between mount hash removal and ->mnt_root |
| 1432 | * becoming unpinned. |
| 1433 | */ |
| 1434 | flags = dentry->d_flags; |
| 1435 | continue; |
| 1436 | } |
| 1437 | if (read_seqretry(&mount_lock, nd->m_seq)) |
| 1438 | return false; |
| 1439 | } |
| 1440 | return !(flags & DCACHE_NEED_AUTOMOUNT); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1441 | } |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1442 | } |
| 1443 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1444 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
| 1445 | struct path *path, struct inode **inode, |
| 1446 | unsigned int *seqp) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1447 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1448 | bool jumped; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1449 | int ret; |
| 1450 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1451 | path->mnt = nd->path.mnt; |
| 1452 | path->dentry = dentry; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1453 | if (nd->flags & LOOKUP_RCU) { |
| 1454 | unsigned int seq = *seqp; |
| 1455 | if (unlikely(!*inode)) |
| 1456 | return -ENOENT; |
| 1457 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1458 | return 0; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1459 | if (unlazy_child(nd, dentry, seq)) |
| 1460 | return -ECHILD; |
| 1461 | // *path might've been clobbered by __follow_mount_rcu() |
| 1462 | path->mnt = nd->path.mnt; |
| 1463 | path->dentry = dentry; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1464 | } |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1465 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
| 1466 | if (jumped) { |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 1467 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1468 | ret = -EXDEV; |
| 1469 | else |
| 1470 | nd->flags |= LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | } |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1472 | if (unlikely(ret)) { |
| 1473 | dput(path->dentry); |
| 1474 | if (path->mnt != nd->path.mnt) |
| 1475 | mntput(path->mnt); |
| 1476 | } else { |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1477 | *inode = d_backing_inode(path->dentry); |
| 1478 | *seqp = 0; /* out of RCU mode, so the value doesn't matter */ |
| 1479 | } |
| 1480 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /* |
Oleg Drokin | f4fdace | 2016-07-07 22:04:04 -0400 | [diff] [blame] | 1484 | * This looks up the name in dcache and possibly revalidates the found dentry. |
| 1485 | * NULL is returned if the dentry does not exist in the cache. |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1486 | */ |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1487 | static struct dentry *lookup_dcache(const struct qstr *name, |
| 1488 | struct dentry *dir, |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 1489 | unsigned int flags) |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1490 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1491 | struct dentry *dentry = d_lookup(dir, name); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1492 | if (dentry) { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1493 | int error = d_revalidate(dentry, flags); |
| 1494 | if (unlikely(error <= 0)) { |
| 1495 | if (!error) |
| 1496 | d_invalidate(dentry); |
| 1497 | dput(dentry); |
| 1498 | return ERR_PTR(error); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1499 | } |
| 1500 | } |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1501 | return dentry; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1505 | * Parent directory has inode locked exclusive. This is one |
| 1506 | * and only case when ->lookup() gets called on non in-lookup |
| 1507 | * dentries - as the matter of fact, this only gets called |
| 1508 | * when directory is guaranteed to have no in-lookup children |
| 1509 | * at all. |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1510 | */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1511 | static struct dentry *__lookup_hash(const struct qstr *name, |
| 1512 | struct dentry *base, unsigned int flags) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1513 | { |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1514 | struct dentry *dentry = lookup_dcache(name, base, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1515 | struct dentry *old; |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1516 | struct inode *dir = base->d_inode; |
| 1517 | |
| 1518 | if (dentry) |
| 1519 | return dentry; |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1520 | |
| 1521 | /* Don't create child dentry for a dead directory. */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1522 | if (unlikely(IS_DEADDIR(dir))) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1523 | return ERR_PTR(-ENOENT); |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1524 | |
| 1525 | dentry = d_alloc(base, name); |
| 1526 | if (unlikely(!dentry)) |
| 1527 | return ERR_PTR(-ENOMEM); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1528 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1529 | old = dir->i_op->lookup(dir, dentry, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1530 | if (unlikely(old)) { |
| 1531 | dput(dentry); |
| 1532 | dentry = old; |
| 1533 | } |
| 1534 | return dentry; |
| 1535 | } |
| 1536 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1537 | static struct dentry *lookup_fast(struct nameidata *nd, |
| 1538 | struct inode **inode, |
| 1539 | unsigned *seqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1541 | struct dentry *dentry, *parent = nd->path.dentry; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1542 | int status = 1; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1543 | |
Al Viro | 3cac260 | 2009-08-13 18:27:43 +0400 | [diff] [blame] | 1544 | /* |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1545 | * Rename seqlock is not required here because in the off chance |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1546 | * of a false negative due to a concurrent rename, the caller is |
| 1547 | * going to fall back to non-racy lookup. |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1548 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1549 | if (nd->flags & LOOKUP_RCU) { |
| 1550 | unsigned seq; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 1551 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1552 | if (unlikely(!dentry)) { |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1553 | if (!try_to_unlazy(nd)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1554 | return ERR_PTR(-ECHILD); |
| 1555 | return NULL; |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1556 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1557 | |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1558 | /* |
| 1559 | * This sequence count validates that the inode matches |
| 1560 | * the dentry name information from lookup. |
| 1561 | */ |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 1562 | *inode = d_backing_inode(dentry); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1563 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1564 | return ERR_PTR(-ECHILD); |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1565 | |
| 1566 | /* |
| 1567 | * This sequence count validates that the parent had no |
| 1568 | * changes while we did the lookup of the dentry above. |
| 1569 | * |
| 1570 | * The memory barrier in read_seqcount_begin of child is |
| 1571 | * enough, we can use __read_seqcount_retry here. |
| 1572 | */ |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1573 | if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1574 | return ERR_PTR(-ECHILD); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1575 | |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1576 | *seqp = seq; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1577 | status = d_revalidate(dentry, nd->flags); |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1578 | if (likely(status > 0)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1579 | return dentry; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1580 | if (unlazy_child(nd, dentry, seq)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1581 | return ERR_PTR(-ECHILD); |
Al Viro | 209a7fb | 2017-01-09 01:35:39 -0500 | [diff] [blame] | 1582 | if (unlikely(status == -ECHILD)) |
| 1583 | /* we'd been told to redo it in non-rcu mode */ |
| 1584 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1585 | } else { |
Al Viro | e97cdc8 | 2013-01-24 18:16:00 -0500 | [diff] [blame] | 1586 | dentry = __d_lookup(parent, &nd->last); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1587 | if (unlikely(!dentry)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1588 | return NULL; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1589 | status = d_revalidate(dentry, nd->flags); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1590 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1591 | if (unlikely(status <= 0)) { |
Al Viro | e9742b5 | 2016-03-05 22:04:59 -0500 | [diff] [blame] | 1592 | if (!status) |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1593 | d_invalidate(dentry); |
Eric W. Biederman | 5542aa2 | 2014-02-13 09:46:25 -0800 | [diff] [blame] | 1594 | dput(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1595 | return ERR_PTR(status); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1596 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1597 | return dentry; |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /* Fast lookup failed, do it the slow way */ |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1601 | static struct dentry *__lookup_slow(const struct qstr *name, |
| 1602 | struct dentry *dir, |
| 1603 | unsigned int flags) |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1604 | { |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1605 | struct dentry *dentry, *old; |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1606 | struct inode *inode = dir->d_inode; |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1607 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1608 | |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1609 | /* Don't go there if it's already dead */ |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1610 | if (unlikely(IS_DEADDIR(inode))) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1611 | return ERR_PTR(-ENOENT); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1612 | again: |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1613 | dentry = d_alloc_parallel(dir, name, &wq); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1614 | if (IS_ERR(dentry)) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1615 | return dentry; |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1616 | if (unlikely(!d_in_lookup(dentry))) { |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1617 | int error = d_revalidate(dentry, flags); |
| 1618 | if (unlikely(error <= 0)) { |
| 1619 | if (!error) { |
| 1620 | d_invalidate(dentry); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1621 | dput(dentry); |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1622 | goto again; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1623 | } |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1624 | dput(dentry); |
| 1625 | dentry = ERR_PTR(error); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1626 | } |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1627 | } else { |
| 1628 | old = inode->i_op->lookup(inode, dentry, flags); |
| 1629 | d_lookup_done(dentry); |
| 1630 | if (unlikely(old)) { |
| 1631 | dput(dentry); |
| 1632 | dentry = old; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1633 | } |
| 1634 | } |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1635 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1638 | static struct dentry *lookup_slow(const struct qstr *name, |
| 1639 | struct dentry *dir, |
| 1640 | unsigned int flags) |
| 1641 | { |
| 1642 | struct inode *inode = dir->d_inode; |
| 1643 | struct dentry *res; |
| 1644 | inode_lock_shared(inode); |
| 1645 | res = __lookup_slow(name, dir, flags); |
| 1646 | inode_unlock_shared(inode); |
| 1647 | return res; |
| 1648 | } |
| 1649 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1650 | static inline int may_lookup(struct nameidata *nd) |
| 1651 | { |
| 1652 | if (nd->flags & LOOKUP_RCU) { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 1653 | int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1654 | if (err != -ECHILD || !try_to_unlazy(nd)) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1655 | return err; |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1656 | } |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 1657 | return inode_permission(nd->inode, MAY_EXEC); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1658 | } |
| 1659 | |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1660 | static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) |
| 1661 | { |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1662 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) |
| 1663 | return -ELOOP; |
Al Viro | 4542576 | 2020-03-03 11:25:31 -0500 | [diff] [blame] | 1664 | |
| 1665 | if (likely(nd->depth != EMBEDDED_LEVELS)) |
| 1666 | return 0; |
| 1667 | if (likely(nd->stack != nd->internal)) |
| 1668 | return 0; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1669 | if (likely(nd_alloc_stack(nd))) |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1670 | return 0; |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1671 | |
| 1672 | if (nd->flags & LOOKUP_RCU) { |
| 1673 | // we need to grab link before we do unlazy. And we can't skip |
| 1674 | // unlazy even if we fail to grab the link - cleanup needs it |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1675 | bool grabbed_link = legitimize_path(nd, link, seq); |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1676 | |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1677 | if (!try_to_unlazy(nd) != 0 || !grabbed_link) |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1678 | return -ECHILD; |
| 1679 | |
| 1680 | if (nd_alloc_stack(nd)) |
| 1681 | return 0; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1682 | } |
Al Viro | 60ef60c | 2020-03-03 11:43:55 -0500 | [diff] [blame] | 1683 | return -ENOMEM; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1684 | } |
| 1685 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1686 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
| 1687 | |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1688 | static const char *pick_link(struct nameidata *nd, struct path *link, |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1689 | struct inode *inode, unsigned seq, int flags) |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1690 | { |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1691 | struct saved *last; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1692 | const char *res; |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1693 | int error = reserve_stack(nd, link, seq); |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1694 | |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1695 | if (unlikely(error)) { |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1696 | if (!(nd->flags & LOOKUP_RCU)) |
| 1697 | path_put(link); |
Al Viro | 4905590 | 2020-03-03 11:22:34 -0500 | [diff] [blame] | 1698 | return ERR_PTR(error); |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1699 | } |
Al Viro | ab10492 | 2015-05-10 11:50:01 -0400 | [diff] [blame] | 1700 | last = nd->stack + nd->depth++; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1701 | last->link = *link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1702 | clear_delayed_call(&last->done); |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 1703 | last->seq = seq; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1704 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1705 | if (flags & WALK_TRAILING) { |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1706 | error = may_follow_link(nd, inode); |
| 1707 | if (unlikely(error)) |
| 1708 | return ERR_PTR(error); |
| 1709 | } |
| 1710 | |
Mattias Nissler | dab741e0 | 2020-08-27 11:09:46 -0600 | [diff] [blame] | 1711 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || |
| 1712 | unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1713 | return ERR_PTR(-ELOOP); |
| 1714 | |
| 1715 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1716 | touch_atime(&last->link); |
| 1717 | cond_resched(); |
| 1718 | } else if (atime_needs_update(&last->link, inode)) { |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1719 | if (!try_to_unlazy(nd)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1720 | return ERR_PTR(-ECHILD); |
| 1721 | touch_atime(&last->link); |
| 1722 | } |
| 1723 | |
| 1724 | error = security_inode_follow_link(link->dentry, inode, |
| 1725 | nd->flags & LOOKUP_RCU); |
| 1726 | if (unlikely(error)) |
| 1727 | return ERR_PTR(error); |
| 1728 | |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1729 | res = READ_ONCE(inode->i_link); |
| 1730 | if (!res) { |
| 1731 | const char * (*get)(struct dentry *, struct inode *, |
| 1732 | struct delayed_call *); |
| 1733 | get = inode->i_op->get_link; |
| 1734 | if (nd->flags & LOOKUP_RCU) { |
| 1735 | res = get(NULL, inode, &last->done); |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 1736 | if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1737 | res = get(link->dentry, inode, &last->done); |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1738 | } else { |
| 1739 | res = get(link->dentry, inode, &last->done); |
| 1740 | } |
| 1741 | if (!res) |
| 1742 | goto all_done; |
| 1743 | if (IS_ERR(res)) |
| 1744 | return res; |
| 1745 | } |
| 1746 | if (*res == '/') { |
| 1747 | error = nd_jump_root(nd); |
| 1748 | if (unlikely(error)) |
| 1749 | return ERR_PTR(error); |
| 1750 | while (unlikely(*++res == '/')) |
| 1751 | ; |
| 1752 | } |
| 1753 | if (*res) |
| 1754 | return res; |
| 1755 | all_done: // pure jump |
| 1756 | put_link(nd); |
| 1757 | return NULL; |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1758 | } |
| 1759 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1760 | /* |
| 1761 | * Do we need to follow links? We _really_ want to be able |
| 1762 | * to do this check without having to look at inode->i_op, |
| 1763 | * so we keep a cache of "no, this doesn't need follow_link" |
| 1764 | * for the common case. |
| 1765 | */ |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1766 | static const char *step_into(struct nameidata *nd, int flags, |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1767 | struct dentry *dentry, struct inode *inode, unsigned seq) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1768 | { |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1769 | struct path path; |
| 1770 | int err = handle_mounts(nd, dentry, &path, &inode, &seq); |
| 1771 | |
| 1772 | if (err < 0) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1773 | return ERR_PTR(err); |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1774 | if (likely(!d_is_symlink(path.dentry)) || |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 1775 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
Al Viro | aca2903 | 2020-01-09 15:17:57 -0500 | [diff] [blame] | 1776 | (flags & WALK_NOFOLLOW)) { |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1777 | /* not a symlink or should not follow */ |
Al Viro | c99687a | 2020-03-03 10:56:17 -0500 | [diff] [blame] | 1778 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1779 | dput(nd->path.dentry); |
| 1780 | if (nd->path.mnt != path.mnt) |
| 1781 | mntput(nd->path.mnt); |
| 1782 | } |
| 1783 | nd->path = path; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1784 | nd->inode = inode; |
| 1785 | nd->seq = seq; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1786 | return NULL; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1787 | } |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1788 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1789 | /* make sure that d_is_symlink above matches inode */ |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1790 | if (read_seqcount_retry(&path.dentry->d_seq, seq)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1791 | return ERR_PTR(-ECHILD); |
Al Viro | 84f0cd9 | 2020-03-03 10:14:30 -0500 | [diff] [blame] | 1792 | } else { |
| 1793 | if (path.mnt == nd->path.mnt) |
| 1794 | mntget(path.mnt); |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1795 | } |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1796 | return pick_link(nd, &path, inode, seq, flags); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1799 | static struct dentry *follow_dotdot_rcu(struct nameidata *nd, |
| 1800 | struct inode **inodep, |
| 1801 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1802 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1803 | struct dentry *parent, *old; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1804 | |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1805 | if (path_equal(&nd->path, &nd->root)) |
| 1806 | goto in_root; |
| 1807 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1808 | struct path path; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1809 | unsigned seq; |
Al Viro | 7ef482f | 2020-02-26 17:50:13 -0500 | [diff] [blame] | 1810 | if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), |
| 1811 | &nd->root, &path, &seq)) |
| 1812 | goto in_root; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1813 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1814 | return ERR_PTR(-ECHILD); |
| 1815 | nd->path = path; |
| 1816 | nd->inode = path.dentry->d_inode; |
| 1817 | nd->seq = seq; |
| 1818 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1819 | return ERR_PTR(-ECHILD); |
| 1820 | /* we know that mountpoint was pinned */ |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1821 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1822 | old = nd->path.dentry; |
| 1823 | parent = old->d_parent; |
| 1824 | *inodep = parent->d_inode; |
| 1825 | *seqp = read_seqcount_begin(&parent->d_seq); |
| 1826 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) |
| 1827 | return ERR_PTR(-ECHILD); |
| 1828 | if (unlikely(!path_connected(nd->path.mnt, parent))) |
| 1829 | return ERR_PTR(-ECHILD); |
| 1830 | return parent; |
| 1831 | in_root: |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1832 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1833 | return ERR_PTR(-ECHILD); |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1834 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1835 | return ERR_PTR(-ECHILD); |
| 1836 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1837 | } |
| 1838 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1839 | static struct dentry *follow_dotdot(struct nameidata *nd, |
| 1840 | struct inode **inodep, |
| 1841 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1842 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1843 | struct dentry *parent; |
| 1844 | |
| 1845 | if (path_equal(&nd->path, &nd->root)) |
| 1846 | goto in_root; |
| 1847 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1848 | struct path path; |
| 1849 | |
| 1850 | if (!choose_mountpoint(real_mount(nd->path.mnt), |
| 1851 | &nd->root, &path)) |
| 1852 | goto in_root; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame] | 1853 | path_put(&nd->path); |
| 1854 | nd->path = path; |
Al Viro | 2aa3847 | 2020-02-26 19:19:05 -0500 | [diff] [blame] | 1855 | nd->inode = path.dentry->d_inode; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame] | 1856 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1857 | return ERR_PTR(-EXDEV); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1858 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1859 | /* rare case of legitimate dget_parent()... */ |
| 1860 | parent = dget_parent(nd->path.dentry); |
| 1861 | if (unlikely(!path_connected(nd->path.mnt, parent))) { |
| 1862 | dput(parent); |
| 1863 | return ERR_PTR(-ENOENT); |
| 1864 | } |
| 1865 | *seqp = 0; |
| 1866 | *inodep = parent->d_inode; |
| 1867 | return parent; |
| 1868 | |
| 1869 | in_root: |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1870 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1871 | return ERR_PTR(-EXDEV); |
| 1872 | dget(nd->path.dentry); |
| 1873 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1874 | } |
| 1875 | |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1876 | static const char *handle_dots(struct nameidata *nd, int type) |
Al Viro | f0a9ba7 | 2015-05-04 07:59:30 -0400 | [diff] [blame] | 1877 | { |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1878 | if (type == LAST_DOTDOT) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1879 | const char *error = NULL; |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1880 | struct dentry *parent; |
| 1881 | struct inode *inode; |
| 1882 | unsigned seq; |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1883 | |
| 1884 | if (!nd->root.mnt) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1885 | error = ERR_PTR(set_root(nd)); |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1886 | if (error) |
| 1887 | return error; |
| 1888 | } |
| 1889 | if (nd->flags & LOOKUP_RCU) |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1890 | parent = follow_dotdot_rcu(nd, &inode, &seq); |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1891 | else |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1892 | parent = follow_dotdot(nd, &inode, &seq); |
| 1893 | if (IS_ERR(parent)) |
| 1894 | return ERR_CAST(parent); |
| 1895 | if (unlikely(!parent)) |
| 1896 | error = step_into(nd, WALK_NOFOLLOW, |
| 1897 | nd->path.dentry, nd->inode, nd->seq); |
| 1898 | else |
| 1899 | error = step_into(nd, WALK_NOFOLLOW, |
| 1900 | parent, inode, seq); |
| 1901 | if (unlikely(error)) |
Al Viro | 951361f | 2011-03-04 14:44:37 -0500 | [diff] [blame] | 1902 | return error; |
| 1903 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1904 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 1905 | /* |
| 1906 | * If there was a racing rename or mount along our |
| 1907 | * path, then we can't be sure that ".." hasn't jumped |
| 1908 | * above nd->root (and so userspace should retry or use |
| 1909 | * some fallback). |
| 1910 | */ |
| 1911 | smp_rmb(); |
| 1912 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1913 | return ERR_PTR(-EAGAIN); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1914 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1915 | return ERR_PTR(-EAGAIN); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1916 | } |
Al Viro | 766c4cb | 2015-05-07 19:24:57 -0400 | [diff] [blame] | 1917 | } |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1918 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1921 | static const char *walk_component(struct nameidata *nd, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1923 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | struct inode *inode; |
| 1925 | unsigned seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | /* |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1927 | * "." and ".." are special - ".." especially so because it has |
| 1928 | * to be able to know about the current root directory and |
| 1929 | * parent relationships. |
| 1930 | */ |
| 1931 | if (unlikely(nd->last_type != LAST_NORM)) { |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 1932 | if (!(flags & WALK_MORE) && nd->depth) |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1933 | put_link(nd); |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1934 | return handle_dots(nd, nd->last_type); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1935 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1936 | dentry = lookup_fast(nd, &inode, &seq); |
| 1937 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1938 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1939 | if (unlikely(!dentry)) { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1940 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
| 1941 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1942 | return ERR_CAST(dentry); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1943 | } |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 1944 | if (!(flags & WALK_MORE) && nd->depth) |
| 1945 | put_link(nd); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1946 | return step_into(nd, flags, dentry, inode, seq); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | /* |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1950 | * We can do the critical dentry name comparison and hashing |
| 1951 | * operations one word at a time, but we are limited to: |
| 1952 | * |
| 1953 | * - Architectures with fast unaligned word accesses. We could |
| 1954 | * do a "get_unaligned()" if this helps and is sufficiently |
| 1955 | * fast. |
| 1956 | * |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1957 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
| 1958 | * do not trap on the (extremely unlikely) case of a page |
| 1959 | * crossing operation. |
| 1960 | * |
| 1961 | * - Furthermore, we need an efficient 64-bit compile for the |
| 1962 | * 64-bit case in order to generate the "number of bytes in |
| 1963 | * the final mask". Again, that could be replaced with a |
| 1964 | * efficient population count instruction or similar. |
| 1965 | */ |
| 1966 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
| 1967 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1968 | #include <asm/word-at-a-time.h> |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1969 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 1970 | #ifdef HASH_MIX |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1971 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 1972 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
| 1973 | |
| 1974 | #elif defined(CONFIG_64BIT) |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1975 | /* |
| 1976 | * Register pressure in the mixing function is an issue, particularly |
| 1977 | * on 32-bit x86, but almost any function requires one state value and |
| 1978 | * one temporary. Instead, use a function designed for two state values |
| 1979 | * and no temporaries. |
| 1980 | * |
| 1981 | * This function cannot create a collision in only two iterations, so |
| 1982 | * we have two iterations to achieve avalanche. In those two iterations, |
| 1983 | * we have six layers of mixing, which is enough to spread one bit's |
| 1984 | * influence out to 2^6 = 64 state bits. |
| 1985 | * |
| 1986 | * Rotate constants are scored by considering either 64 one-bit input |
| 1987 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the |
| 1988 | * probability of that delta causing a change to each of the 128 output |
| 1989 | * bits, using a sample of random initial states. |
| 1990 | * |
| 1991 | * The Shannon entropy of the computed probabilities is then summed |
| 1992 | * to produce a score. Ideally, any input change has a 50% chance of |
| 1993 | * toggling any given output bit. |
| 1994 | * |
| 1995 | * Mixing scores (in bits) for (12,45): |
| 1996 | * Input delta: 1-bit 2-bit |
| 1997 | * 1 round: 713.3 42542.6 |
| 1998 | * 2 rounds: 2753.7 140389.8 |
| 1999 | * 3 rounds: 5954.1 233458.2 |
| 2000 | * 4 rounds: 7862.6 256672.2 |
| 2001 | * Perfect: 8192 258048 |
| 2002 | * (64*128) (64*63/2 * 128) |
| 2003 | */ |
| 2004 | #define HASH_MIX(x, y, a) \ |
| 2005 | ( x ^= (a), \ |
| 2006 | y ^= x, x = rol64(x,12),\ |
| 2007 | x += y, y = rol64(y,45),\ |
| 2008 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2009 | |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2010 | /* |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2011 | * Fold two longs into one 32-bit hash value. This must be fast, but |
| 2012 | * latency isn't quite as critical, as there is a fair bit of additional |
| 2013 | * work done before the hash value is used. |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2014 | */ |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2015 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2016 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2017 | y ^= x * GOLDEN_RATIO_64; |
| 2018 | y *= GOLDEN_RATIO_64; |
| 2019 | return y >> 32; |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2020 | } |
| 2021 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2022 | #else /* 32-bit case */ |
| 2023 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2024 | /* |
| 2025 | * Mixing scores (in bits) for (7,20): |
| 2026 | * Input delta: 1-bit 2-bit |
| 2027 | * 1 round: 330.3 9201.6 |
| 2028 | * 2 rounds: 1246.4 25475.4 |
| 2029 | * 3 rounds: 1907.1 31295.1 |
| 2030 | * 4 rounds: 2042.3 31718.6 |
| 2031 | * Perfect: 2048 31744 |
| 2032 | * (32*64) (32*31/2 * 64) |
| 2033 | */ |
| 2034 | #define HASH_MIX(x, y, a) \ |
| 2035 | ( x ^= (a), \ |
| 2036 | y ^= x, x = rol32(x, 7),\ |
| 2037 | x += y, y = rol32(y,20),\ |
| 2038 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2039 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2040 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2041 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2042 | /* Use arch-optimized multiply if one exists */ |
| 2043 | return __hash_32(y ^ __hash_32(x)); |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2046 | #endif |
| 2047 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2048 | /* |
| 2049 | * Return the hash of a string of known length. This is carfully |
| 2050 | * designed to match hash_name(), which is the more critical function. |
| 2051 | * In particular, we must end by hashing a final word containing 0..7 |
| 2052 | * payload bytes, to match the way that hash_name() iterates until it |
| 2053 | * finds the delimiter after the name. |
| 2054 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2055 | 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] | 2056 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2057 | unsigned long a, x = 0, y = (unsigned long)salt; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2058 | |
| 2059 | for (;;) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2060 | if (!len) |
| 2061 | goto done; |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2062 | a = load_unaligned_zeropad(name); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2063 | if (len < sizeof(unsigned long)) |
| 2064 | break; |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2065 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2066 | name += sizeof(unsigned long); |
| 2067 | len -= sizeof(unsigned long); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2068 | } |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2069 | x ^= a & bytemask_from_count(len); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2070 | done: |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2071 | return fold_hash(x, y); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2072 | } |
| 2073 | EXPORT_SYMBOL(full_name_hash); |
| 2074 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2075 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2076 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2077 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2078 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
| 2079 | unsigned long adata, mask, len; |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2080 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
| 2081 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2082 | len = 0; |
| 2083 | goto inside; |
| 2084 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2085 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2086 | HASH_MIX(x, y, a); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2087 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2088 | inside: |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2089 | a = load_unaligned_zeropad(name+len); |
| 2090 | } while (!has_zero(a, &adata, &constants)); |
| 2091 | |
| 2092 | adata = prep_zero_mask(a, adata, &constants); |
| 2093 | mask = create_zero_mask(adata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2094 | x ^= a & zero_bytemask(mask); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2095 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2096 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2097 | } |
| 2098 | EXPORT_SYMBOL(hashlen_string); |
| 2099 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2100 | /* |
| 2101 | * Calculate the length and hash of the path component, and |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2102 | * return the "hash_len" as the result. |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2103 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2104 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2105 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2106 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
| 2107 | unsigned long adata, bdata, mask, len; |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2108 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2109 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2110 | len = 0; |
| 2111 | goto inside; |
| 2112 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2113 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2114 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2115 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2116 | inside: |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2117 | a = load_unaligned_zeropad(name+len); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2118 | b = a ^ REPEAT_BYTE('/'); |
| 2119 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2120 | |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2121 | adata = prep_zero_mask(a, adata, &constants); |
| 2122 | bdata = prep_zero_mask(b, bdata, &constants); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2123 | mask = create_zero_mask(adata | bdata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2124 | x ^= a & zero_bytemask(mask); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2125 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2126 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2129 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2130 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2131 | /* Return the hash of a string of known length */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2132 | 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] | 2133 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2134 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2135 | while (len--) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2136 | hash = partial_name_hash((unsigned char)*name++, hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2137 | return end_name_hash(hash); |
| 2138 | } |
Linus Torvalds | ae942ae | 2012-03-02 19:40:57 -0800 | [diff] [blame] | 2139 | EXPORT_SYMBOL(full_name_hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2140 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2141 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2142 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2143 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2144 | unsigned long hash = init_name_hash(salt); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2145 | unsigned long len = 0, c; |
| 2146 | |
| 2147 | c = (unsigned char)*name; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2148 | while (c) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2149 | len++; |
| 2150 | hash = partial_name_hash(c, hash); |
| 2151 | c = (unsigned char)name[len]; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2152 | } |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2153 | return hashlen_create(end_name_hash(hash), len); |
| 2154 | } |
George Spelvin | f2a031b | 2016-05-29 01:26:41 -0400 | [diff] [blame] | 2155 | EXPORT_SYMBOL(hashlen_string); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2156 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 2157 | /* |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2158 | * We know there's a real path component here of at least |
| 2159 | * one character. |
| 2160 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2161 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2162 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2163 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2164 | unsigned long len = 0, c; |
| 2165 | |
| 2166 | c = (unsigned char)*name; |
| 2167 | do { |
| 2168 | len++; |
| 2169 | hash = partial_name_hash(c, hash); |
| 2170 | c = (unsigned char)name[len]; |
| 2171 | } while (c && c != '/'); |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2172 | return hashlen_create(end_name_hash(hash), len); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2175 | #endif |
| 2176 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2177 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2179 | * This is the basic name resolution function, turning a pathname into |
| 2180 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2182 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 2183 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | */ |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 2185 | static int link_path_walk(const char *name, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | { |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2187 | int depth = 0; // depth <= nd->depth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | int err; |
Al Viro | 32cd746 | 2015-04-18 20:30:49 -0400 | [diff] [blame] | 2189 | |
Al Viro | b4c0353 | 2020-01-19 11:44:51 -0500 | [diff] [blame] | 2190 | nd->last_type = LAST_ROOT; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2191 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2192 | if (IS_ERR(name)) |
| 2193 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | while (*name=='/') |
| 2195 | name++; |
| 2196 | if (!*name) |
Al Viro | 9e18f10 | 2015-04-18 20:44:34 -0400 | [diff] [blame] | 2197 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | /* At this point we know we have a real path component. */ |
| 2200 | for(;;) { |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2201 | const char *link; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2202 | u64 hash_len; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2203 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 2205 | err = may_lookup(nd); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2206 | if (err) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2207 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2209 | hash_len = hash_name(nd->path.dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2211 | type = LAST_NORM; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2212 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2213 | case 2: |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2214 | if (name[1] == '.') { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2215 | type = LAST_DOTDOT; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2216 | nd->flags |= LOOKUP_JUMPED; |
| 2217 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2218 | break; |
| 2219 | case 1: |
| 2220 | type = LAST_DOT; |
| 2221 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2222 | if (likely(type == LAST_NORM)) { |
| 2223 | struct dentry *parent = nd->path.dentry; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2224 | nd->flags &= ~LOOKUP_JUMPED; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2225 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
James Hogan | a060dc5 | 2014-09-16 13:07:35 +0100 | [diff] [blame] | 2226 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 2227 | err = parent->d_op->d_hash(parent, &this); |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2228 | if (err < 0) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2229 | return err; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2230 | hash_len = this.hash_len; |
| 2231 | name = this.name; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2232 | } |
| 2233 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2234 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2235 | nd->last.hash_len = hash_len; |
| 2236 | nd->last.name = name; |
Al Viro | 5f4a6a6 | 2013-01-24 18:04:22 -0500 | [diff] [blame] | 2237 | nd->last_type = type; |
| 2238 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2239 | name += hashlen_len(hash_len); |
| 2240 | if (!*name) |
Al Viro | bdf6cbf | 2015-04-18 20:21:40 -0400 | [diff] [blame] | 2241 | goto OK; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2242 | /* |
| 2243 | * If it wasn't NUL, we know it was '/'. Skip that |
| 2244 | * slash, and continue until no more slashes. |
| 2245 | */ |
| 2246 | do { |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2247 | name++; |
| 2248 | } while (unlikely(*name == '/')); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2249 | if (unlikely(!*name)) { |
| 2250 | OK: |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2251 | /* pathname or trailing symlink, done */ |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2252 | if (!depth) { |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 2253 | nd->dir_uid = nd->inode->i_uid; |
| 2254 | nd->dir_mode = nd->inode->i_mode; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2255 | nd->flags &= ~LOOKUP_PARENT; |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2256 | return 0; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2257 | } |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2258 | /* last component of nested symlink */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2259 | name = nd->stack[--depth].name; |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2260 | link = walk_component(nd, 0); |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 2261 | } else { |
| 2262 | /* not the last component */ |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2263 | link = walk_component(nd, WALK_MORE); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2264 | } |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2265 | if (unlikely(link)) { |
| 2266 | if (IS_ERR(link)) |
| 2267 | return PTR_ERR(link); |
| 2268 | /* a symlink to follow */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2269 | nd->stack[depth++].name = name; |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2270 | name = link; |
| 2271 | continue; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2272 | } |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2273 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
| 2274 | if (nd->flags & LOOKUP_RCU) { |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 2275 | if (!try_to_unlazy(nd)) |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2276 | return -ECHILD; |
| 2277 | } |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2278 | return -ENOTDIR; |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2283 | /* must be paired with terminate_walk() */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2284 | static const char *path_init(struct nameidata *nd, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2286 | int error; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2287 | const char *s = nd->name->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2289 | if (!*s) |
| 2290 | flags &= ~LOOKUP_RCU; |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2291 | if (flags & LOOKUP_RCU) |
| 2292 | rcu_read_lock(); |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2293 | |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2294 | nd->flags = flags | LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | nd->depth = 0; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2296 | |
| 2297 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); |
| 2298 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); |
| 2299 | smp_rmb(); |
| 2300 | |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2301 | if (flags & LOOKUP_ROOT) { |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2302 | struct dentry *root = nd->root.dentry; |
| 2303 | struct inode *inode = root->d_inode; |
Al Viro | 9389386 | 2017-04-15 17:29:14 -0400 | [diff] [blame] | 2304 | if (*s && unlikely(!d_can_lookup(root))) |
| 2305 | return ERR_PTR(-ENOTDIR); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2306 | nd->path = nd->root; |
| 2307 | nd->inode = inode; |
| 2308 | if (flags & LOOKUP_RCU) { |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2309 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 8f47a016 | 2015-05-09 19:02:01 -0400 | [diff] [blame] | 2310 | nd->root_seq = nd->seq; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2311 | } else { |
| 2312 | path_get(&nd->path); |
| 2313 | } |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2314 | return s; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2315 | } |
| 2316 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 2317 | nd->root.mnt = NULL; |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 2318 | nd->path.mnt = NULL; |
| 2319 | nd->path.dentry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2321 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
| 2322 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2323 | error = nd_jump_root(nd); |
| 2324 | if (unlikely(error)) |
| 2325 | return ERR_PTR(error); |
| 2326 | return s; |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | /* Relative pathname -- get the starting-point it is relative to. */ |
| 2330 | if (nd->dfd == AT_FDCWD) { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2331 | if (flags & LOOKUP_RCU) { |
| 2332 | struct fs_struct *fs = current->fs; |
| 2333 | unsigned seq; |
| 2334 | |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2335 | do { |
| 2336 | seq = read_seqcount_begin(&fs->seq); |
| 2337 | nd->path = fs->pwd; |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2338 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2339 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 2340 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 2341 | } else { |
| 2342 | get_fs_pwd(current->fs, &nd->path); |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2343 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2344 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2345 | } else { |
Jeff Layton | 582aa64 | 2012-12-11 08:56:16 -0500 | [diff] [blame] | 2346 | /* Caller must check execute permissions on the starting path component */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2347 | struct fd f = fdget_raw(nd->dfd); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2348 | struct dentry *dentry; |
| 2349 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2350 | if (!f.file) |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2351 | return ERR_PTR(-EBADF); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2352 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2353 | dentry = f.file->f_path.dentry; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2354 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2355 | if (*s && unlikely(!d_can_lookup(dentry))) { |
| 2356 | fdput(f); |
| 2357 | return ERR_PTR(-ENOTDIR); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 2358 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2359 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2360 | nd->path = f.file->f_path; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2361 | if (flags & LOOKUP_RCU) { |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2362 | nd->inode = nd->path.dentry->d_inode; |
| 2363 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2364 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2365 | path_get(&nd->path); |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2366 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2367 | } |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2368 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | } |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2370 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 2371 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
| 2372 | if (flags & LOOKUP_IS_SCOPED) { |
| 2373 | nd->root = nd->path; |
| 2374 | if (flags & LOOKUP_RCU) { |
| 2375 | nd->root_seq = nd->seq; |
| 2376 | } else { |
| 2377 | path_get(&nd->root); |
| 2378 | nd->flags |= LOOKUP_ROOT_GRABBED; |
| 2379 | } |
| 2380 | } |
| 2381 | return s; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2382 | } |
| 2383 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2384 | static inline const char *lookup_last(struct nameidata *nd) |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2385 | { |
| 2386 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) |
| 2387 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 2388 | |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2389 | return walk_component(nd, WALK_TRAILING); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2390 | } |
| 2391 | |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2392 | static int handle_lookup_down(struct nameidata *nd) |
| 2393 | { |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 2394 | if (!(nd->flags & LOOKUP_RCU)) |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 2395 | dget(nd->path.dentry); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 2396 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
| 2397 | nd->path.dentry, nd->inode, nd->seq)); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2398 | } |
| 2399 | |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2400 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2401 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2402 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2403 | const char *s = path_init(nd, flags); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2404 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2405 | |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2406 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2407 | err = handle_lookup_down(nd); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 2408 | if (unlikely(err < 0)) |
| 2409 | s = ERR_PTR(err); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2410 | } |
| 2411 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2412 | while (!(err = link_path_walk(s, nd)) && |
| 2413 | (s = lookup_last(nd)) != NULL) |
| 2414 | ; |
Al Viro | 4390813 | 2021-04-06 19:46:51 -0400 | [diff] [blame] | 2415 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
| 2416 | err = handle_lookup_down(nd); |
| 2417 | nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please... |
| 2418 | } |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2419 | if (!err) |
| 2420 | err = complete_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2421 | |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 2422 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
| 2423 | if (!d_can_lookup(nd->path.dentry)) |
Al Viro | bd23a53 | 2011-03-23 09:56:30 -0400 | [diff] [blame] | 2424 | err = -ENOTDIR; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 2425 | if (!err) { |
| 2426 | *path = nd->path; |
| 2427 | nd->path.mnt = NULL; |
| 2428 | nd->path.dentry = NULL; |
| 2429 | } |
| 2430 | terminate_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2431 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2432 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2433 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2434 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
| 2435 | struct path *path, struct path *root) |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2436 | { |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 2437 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2438 | struct nameidata nd; |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2439 | if (IS_ERR(name)) |
| 2440 | return PTR_ERR(name); |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2441 | if (unlikely(root)) { |
| 2442 | nd.root = *root; |
| 2443 | flags |= LOOKUP_ROOT; |
| 2444 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2445 | set_nameidata(&nd, dfd, name); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2446 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2447 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2448 | retval = path_lookupat(&nd, flags, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2449 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2450 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2451 | |
| 2452 | if (likely(!retval)) |
Al Viro | 161aff1 | 2020-01-11 22:52:26 -0500 | [diff] [blame] | 2453 | audit_inode(name, path->dentry, |
| 2454 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2455 | restore_nameidata(); |
Al Viro | e4bd1c1 | 2015-05-12 16:40:39 -0400 | [diff] [blame] | 2456 | putname(name); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2457 | return retval; |
| 2458 | } |
| 2459 | |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2460 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2461 | static int path_parentat(struct nameidata *nd, unsigned flags, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2462 | struct path *parent) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2463 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2464 | const char *s = path_init(nd, flags); |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2465 | int err = link_path_walk(s, nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2466 | if (!err) |
| 2467 | err = complete_walk(nd); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2468 | if (!err) { |
| 2469 | *parent = nd->path; |
| 2470 | nd->path.mnt = NULL; |
| 2471 | nd->path.dentry = NULL; |
| 2472 | } |
| 2473 | terminate_walk(nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2474 | return err; |
| 2475 | } |
| 2476 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2477 | static struct filename *filename_parentat(int dfd, struct filename *name, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2478 | unsigned int flags, struct path *parent, |
| 2479 | struct qstr *last, int *type) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2480 | { |
| 2481 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2482 | struct nameidata nd; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2483 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2484 | if (IS_ERR(name)) |
| 2485 | return name; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2486 | set_nameidata(&nd, dfd, name); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2487 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2488 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2489 | retval = path_parentat(&nd, flags, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2490 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2491 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2492 | if (likely(!retval)) { |
| 2493 | *last = nd.last; |
| 2494 | *type = nd.last_type; |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 2495 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2496 | } else { |
| 2497 | putname(name); |
| 2498 | name = ERR_PTR(retval); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2499 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2500 | restore_nameidata(); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2501 | return name; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2502 | } |
| 2503 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2504 | /* does lookup, returns the object with parent locked */ |
| 2505 | struct dentry *kern_path_locked(const char *name, struct path *path) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2506 | { |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2507 | struct filename *filename; |
| 2508 | struct dentry *d; |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2509 | struct qstr last; |
| 2510 | int type; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2511 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2512 | filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, |
| 2513 | &last, &type); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2514 | if (IS_ERR(filename)) |
| 2515 | return ERR_CAST(filename); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2516 | if (unlikely(type != LAST_NORM)) { |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2517 | path_put(path); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2518 | putname(filename); |
| 2519 | return ERR_PTR(-EINVAL); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2520 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2521 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2522 | d = __lookup_hash(&last, path->dentry, 0); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2523 | if (IS_ERR(d)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2524 | inode_unlock(path->dentry->d_inode); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2525 | path_put(path); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2526 | } |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2527 | putname(filename); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2528 | return d; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2529 | } |
| 2530 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2531 | int kern_path(const char *name, unsigned int flags, struct path *path) |
| 2532 | { |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2533 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
| 2534 | flags, path, NULL); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2535 | } |
Greg Kroah-Hartman | 91be423 | 2022-03-07 20:45:25 +0100 | [diff] [blame] | 2536 | EXPORT_SYMBOL(kern_path); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2537 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2538 | /** |
| 2539 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 2540 | * @dentry: pointer to dentry of the base directory |
| 2541 | * @mnt: pointer to vfs mount of the base directory |
| 2542 | * @name: pointer to file name |
| 2543 | * @flags: lookup flags |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2544 | * @path: pointer to struct path to fill |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2545 | */ |
| 2546 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 2547 | const char *name, unsigned int flags, |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2548 | struct path *path) |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2549 | { |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2550 | struct path root = {.mnt = mnt, .dentry = dentry}; |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2551 | /* the first argument of filename_lookup() is ignored with root */ |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2552 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
| 2553 | flags , path, &root); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2554 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2555 | EXPORT_SYMBOL(vfs_path_lookup); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2556 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2557 | static int lookup_one_len_common(const char *name, struct dentry *base, |
| 2558 | int len, struct qstr *this) |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2559 | { |
| 2560 | this->name = name; |
| 2561 | this->len = len; |
| 2562 | this->hash = full_name_hash(base, name, len); |
| 2563 | if (!len) |
| 2564 | return -EACCES; |
| 2565 | |
| 2566 | if (unlikely(name[0] == '.')) { |
| 2567 | if (len < 2 || (len == 2 && name[1] == '.')) |
| 2568 | return -EACCES; |
| 2569 | } |
| 2570 | |
| 2571 | while (len--) { |
| 2572 | unsigned int c = *(const unsigned char *)name++; |
| 2573 | if (c == '/' || c == '\0') |
| 2574 | return -EACCES; |
| 2575 | } |
| 2576 | /* |
| 2577 | * See if the low-level filesystem might want |
| 2578 | * to use its own hash.. |
| 2579 | */ |
| 2580 | if (base->d_flags & DCACHE_OP_HASH) { |
| 2581 | int err = base->d_op->d_hash(base, this); |
| 2582 | if (err < 0) |
| 2583 | return err; |
| 2584 | } |
| 2585 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2586 | return inode_permission(base->d_inode, MAY_EXEC); |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2587 | } |
| 2588 | |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2589 | /** |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2590 | * try_lookup_one_len - filesystem helper to lookup single pathname component |
| 2591 | * @name: pathname component to lookup |
| 2592 | * @base: base directory to lookup from |
| 2593 | * @len: maximum length @len should be interpreted to |
| 2594 | * |
| 2595 | * Look up a dentry by name in the dcache, returning NULL if it does not |
| 2596 | * currently exist. The function does not try to create a dentry. |
| 2597 | * |
| 2598 | * Note that this routine is purely a helper for filesystem usage and should |
| 2599 | * not be called by generic code. |
| 2600 | * |
| 2601 | * The caller must hold base->i_mutex. |
| 2602 | */ |
| 2603 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) |
| 2604 | { |
| 2605 | struct qstr this; |
| 2606 | int err; |
| 2607 | |
| 2608 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
| 2609 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2610 | err = lookup_one_len_common(name, base, len, &this); |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2611 | if (err) |
| 2612 | return ERR_PTR(err); |
| 2613 | |
| 2614 | return lookup_dcache(&this, base, 0); |
| 2615 | } |
| 2616 | EXPORT_SYMBOL(try_lookup_one_len); |
| 2617 | |
| 2618 | /** |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2619 | * lookup_one_len - filesystem helper to lookup single pathname component |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2620 | * @name: pathname component to lookup |
| 2621 | * @base: base directory to lookup from |
| 2622 | * @len: maximum length @len should be interpreted to |
| 2623 | * |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2624 | * 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] | 2625 | * not be called by generic code. |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2626 | * |
| 2627 | * The caller must hold base->i_mutex. |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2628 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2629 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2630 | { |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2631 | struct dentry *dentry; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2632 | struct qstr this; |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2633 | int err; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2634 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2635 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2636 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2637 | err = lookup_one_len_common(name, base, len, &this); |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2638 | if (err) |
| 2639 | return ERR_PTR(err); |
| 2640 | |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2641 | dentry = lookup_dcache(&this, base, 0); |
| 2642 | return dentry ? dentry : __lookup_slow(&this, base, 0); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2643 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2644 | EXPORT_SYMBOL(lookup_one_len); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2645 | |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2646 | /** |
| 2647 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component |
| 2648 | * @name: pathname component to lookup |
| 2649 | * @base: base directory to lookup from |
| 2650 | * @len: maximum length @len should be interpreted to |
| 2651 | * |
| 2652 | * Note that this routine is purely a helper for filesystem usage and should |
| 2653 | * not be called by generic code. |
| 2654 | * |
| 2655 | * Unlike lookup_one_len, it should be called without the parent |
| 2656 | * i_mutex held, and will take the i_mutex itself if necessary. |
| 2657 | */ |
| 2658 | struct dentry *lookup_one_len_unlocked(const char *name, |
| 2659 | struct dentry *base, int len) |
| 2660 | { |
| 2661 | struct qstr this; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2662 | int err; |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2663 | struct dentry *ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2664 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2665 | err = lookup_one_len_common(name, base, len, &this); |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2666 | if (err) |
| 2667 | return ERR_PTR(err); |
| 2668 | |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2669 | ret = lookup_dcache(&this, base, 0); |
| 2670 | if (!ret) |
| 2671 | ret = lookup_slow(&this, base, 0); |
| 2672 | return ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2673 | } |
| 2674 | EXPORT_SYMBOL(lookup_one_len_unlocked); |
| 2675 | |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2676 | /* |
| 2677 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) |
| 2678 | * on negatives. Returns known positive or ERR_PTR(); that's what |
| 2679 | * most of the users want. Note that pinned negative with unlocked parent |
| 2680 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() |
| 2681 | * need to be very careful; pinned positives have ->d_inode stable, so |
| 2682 | * this one avoids such problems. |
| 2683 | */ |
| 2684 | struct dentry *lookup_positive_unlocked(const char *name, |
| 2685 | struct dentry *base, int len) |
| 2686 | { |
| 2687 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); |
Al Viro | 2fa6b1e | 2019-11-12 16:13:06 -0500 | [diff] [blame] | 2688 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2689 | dput(ret); |
| 2690 | ret = ERR_PTR(-ENOENT); |
| 2691 | } |
| 2692 | return ret; |
| 2693 | } |
| 2694 | EXPORT_SYMBOL(lookup_positive_unlocked); |
| 2695 | |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2696 | #ifdef CONFIG_UNIX98_PTYS |
| 2697 | int path_pts(struct path *path) |
| 2698 | { |
| 2699 | /* Find something mounted on "pts" in the same directory as |
| 2700 | * the input path. |
| 2701 | */ |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2702 | struct dentry *parent = dget_parent(path->dentry); |
| 2703 | struct dentry *child; |
Al Viro | 19f6028 | 2020-02-26 20:09:37 -0500 | [diff] [blame] | 2704 | struct qstr this = QSTR_INIT("pts", 3); |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2705 | |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2706 | if (unlikely(!path_connected(path->mnt, parent))) { |
| 2707 | dput(parent); |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2708 | return -ENOENT; |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2709 | } |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2710 | dput(path->dentry); |
| 2711 | path->dentry = parent; |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2712 | child = d_hash_and_lookup(parent, &this); |
| 2713 | if (!child) |
| 2714 | return -ENOENT; |
| 2715 | |
| 2716 | path->dentry = child; |
| 2717 | dput(parent); |
Al Viro | 19f6028 | 2020-02-26 20:09:37 -0500 | [diff] [blame] | 2718 | follow_down(path); |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2719 | return 0; |
| 2720 | } |
| 2721 | #endif |
| 2722 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2723 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
| 2724 | struct path *path, int *empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | { |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2726 | return filename_lookup(dfd, getname_flags(name, flags, empty), |
| 2727 | flags, path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | } |
Al Viro | b853a16 | 2015-05-13 09:12:02 -0400 | [diff] [blame] | 2729 | EXPORT_SYMBOL(user_path_at_empty); |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2730 | |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2731 | int __check_sticky(struct inode *dir, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | { |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2733 | kuid_t fsuid = current_fsuid(); |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2734 | |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2735 | if (uid_eq(inode->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | return 0; |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2737 | if (uid_eq(dir->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | return 0; |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 2739 | return !capable_wrt_inode_uidgid(inode, CAP_FOWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | } |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2741 | EXPORT_SYMBOL(__check_sticky); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | |
| 2743 | /* |
| 2744 | * Check whether we can remove a link victim from directory dir, check |
| 2745 | * whether the type of victim is right. |
| 2746 | * 1. We can't do it if dir is read-only (done in permission()) |
| 2747 | * 2. We should have write and exec permissions on dir |
| 2748 | * 3. We can't remove anything from append-only dir |
| 2749 | * 4. We can't do anything with immutable dir (done in permission()) |
| 2750 | * 5. If the sticky bit on dir is set we should either |
| 2751 | * a. be owner of dir, or |
| 2752 | * b. be owner of victim, or |
| 2753 | * c. have CAP_FOWNER capability |
| 2754 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 2755 | * links pointing to it. |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2756 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
| 2757 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 2758 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 2759 | * 10. We can't remove a root or mountpoint. |
| 2760 | * 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] | 2761 | * nfs_async_unlink(). |
| 2762 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2763 | static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | { |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 2765 | struct inode *inode = d_backing_inode(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | int error; |
| 2767 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2768 | if (d_is_negative(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | return -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2770 | BUG_ON(!inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | |
| 2772 | BUG_ON(victim->d_parent->d_inode != dir); |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 2773 | |
| 2774 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
| 2775 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) |
| 2776 | return -EOVERFLOW; |
| 2777 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 2778 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2780 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | if (error) |
| 2782 | return error; |
| 2783 | if (IS_APPEND(dir)) |
| 2784 | return -EPERM; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2785 | |
| 2786 | if (check_sticky(dir, inode) || IS_APPEND(inode) || |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2787 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | return -EPERM; |
| 2789 | if (isdir) { |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2790 | if (!d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | return -ENOTDIR; |
| 2792 | if (IS_ROOT(victim)) |
| 2793 | return -EBUSY; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2794 | } else if (d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | return -EISDIR; |
| 2796 | if (IS_DEADDIR(dir)) |
| 2797 | return -ENOENT; |
| 2798 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 2799 | return -EBUSY; |
| 2800 | return 0; |
| 2801 | } |
| 2802 | |
| 2803 | /* Check whether we can create an object with dentry child in directory |
| 2804 | * dir. |
| 2805 | * 1. We can't do it if child already exists (open has special treatment for |
| 2806 | * this case, but since we are inlined it's OK) |
| 2807 | * 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] | 2808 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
| 2809 | * 4. We should have write and exec permissions on dir |
| 2810 | * 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] | 2811 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2812 | static inline int may_create(struct inode *dir, struct dentry *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | { |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2814 | struct user_namespace *s_user_ns; |
Jeff Layton | 14e972b | 2013-05-08 10:25:58 -0400 | [diff] [blame] | 2815 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | if (child->d_inode) |
| 2817 | return -EEXIST; |
| 2818 | if (IS_DEADDIR(dir)) |
| 2819 | return -ENOENT; |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2820 | s_user_ns = dir->i_sb->s_user_ns; |
| 2821 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || |
| 2822 | !kgid_has_mapping(s_user_ns, current_fsgid())) |
| 2823 | return -EOVERFLOW; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2824 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | /* |
| 2828 | * p1 and p2 should be directories on the same fs. |
| 2829 | */ |
| 2830 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 2831 | { |
| 2832 | struct dentry *p; |
| 2833 | |
| 2834 | if (p1 == p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2835 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | return NULL; |
| 2837 | } |
| 2838 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2839 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2841 | p = d_ancestor(p2, p1); |
| 2842 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2843 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
| 2844 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2845 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2848 | p = d_ancestor(p1, p2); |
| 2849 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2850 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2851 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2852 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2855 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2856 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | return NULL; |
| 2858 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2859 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | |
| 2861 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 2862 | { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2863 | inode_unlock(p1->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | if (p1 != p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2865 | inode_unlock(p2->d_inode); |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2866 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | } |
| 2868 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2869 | EXPORT_SYMBOL(unlock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2871 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 2872 | bool want_excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2874 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | if (error) |
| 2876 | return error; |
| 2877 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 2878 | if (!dir->i_op->create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 2880 | mode &= S_IALLUGO; |
| 2881 | mode |= S_IFREG; |
| 2882 | error = security_inode_create(dir, dentry, mode); |
| 2883 | if (error) |
| 2884 | return error; |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2885 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 2886 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2887 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | return error; |
| 2889 | } |
Greg Kroah-Hartman | d483eed | 2020-07-02 12:51:03 +0200 | [diff] [blame] | 2890 | EXPORT_SYMBOL_NS(vfs_create, ANDROID_GKI_VFS_EXPORT_ONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2892 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 2893 | int (*f)(struct dentry *, umode_t, void *), |
| 2894 | void *arg) |
| 2895 | { |
| 2896 | struct inode *dir = dentry->d_parent->d_inode; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2897 | int error = may_create(dir, dentry); |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 2898 | if (error) |
| 2899 | return error; |
| 2900 | |
| 2901 | mode &= S_IALLUGO; |
| 2902 | mode |= S_IFREG; |
| 2903 | error = security_inode_create(dir, dentry, mode); |
| 2904 | if (error) |
| 2905 | return error; |
| 2906 | error = f(dentry, mode, arg); |
| 2907 | if (!error) |
| 2908 | fsnotify_create(dir, dentry); |
| 2909 | return error; |
| 2910 | } |
| 2911 | EXPORT_SYMBOL(vfs_mkobj); |
| 2912 | |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 2913 | bool may_open_dev(const struct path *path) |
| 2914 | { |
| 2915 | return !(path->mnt->mnt_flags & MNT_NODEV) && |
| 2916 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); |
| 2917 | } |
| 2918 | |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 2919 | static int may_open(const struct path *path, int acc_mode, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | { |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2921 | struct dentry *dentry = path->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | struct inode *inode = dentry->d_inode; |
| 2923 | int error; |
| 2924 | |
| 2925 | if (!inode) |
| 2926 | return -ENOENT; |
| 2927 | |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2928 | switch (inode->i_mode & S_IFMT) { |
| 2929 | case S_IFLNK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | return -ELOOP; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2931 | case S_IFDIR: |
Kees Cook | 901e02c | 2020-08-13 16:17:22 -0700 | [diff] [blame] | 2932 | if (acc_mode & MAY_WRITE) |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2933 | return -EISDIR; |
Kees Cook | 901e02c | 2020-08-13 16:17:22 -0700 | [diff] [blame] | 2934 | if (acc_mode & MAY_EXEC) |
| 2935 | return -EACCES; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2936 | break; |
| 2937 | case S_IFBLK: |
| 2938 | case S_IFCHR: |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 2939 | if (!may_open_dev(path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | return -EACCES; |
Greg Kroah-Hartman | af494fd | 2020-08-14 09:11:29 +0200 | [diff] [blame] | 2941 | fallthrough; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2942 | case S_IFIFO: |
| 2943 | case S_IFSOCK: |
Greg Kroah-Hartman | af494fd | 2020-08-14 09:11:29 +0200 | [diff] [blame] | 2944 | if (acc_mode & MAY_EXEC) |
| 2945 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | flag &= ~O_TRUNC; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2947 | break; |
Kees Cook | 0fd338b | 2020-08-11 18:36:30 -0700 | [diff] [blame] | 2948 | case S_IFREG: |
| 2949 | if ((acc_mode & MAY_EXEC) && path_noexec(path)) |
| 2950 | return -EACCES; |
| 2951 | break; |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 2952 | } |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2953 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 2954 | error = inode_permission(inode, MAY_OPEN | acc_mode); |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2955 | if (error) |
| 2956 | return error; |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 2957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | /* |
| 2959 | * An append-only file must be opened in append mode for writing. |
| 2960 | */ |
| 2961 | if (IS_APPEND(inode)) { |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 2962 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2963 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | if (flag & O_TRUNC) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2965 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | /* O_NOATIME can only be set by the owner or superuser */ |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 2969 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2970 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 2972 | return 0; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2975 | static int handle_truncate(struct file *filp) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2976 | { |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 2977 | const struct path *path = &filp->f_path; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2978 | struct inode *inode = path->dentry->d_inode; |
| 2979 | int error = get_write_access(inode); |
| 2980 | if (error) |
| 2981 | return error; |
| 2982 | /* |
| 2983 | * Refuse to truncate files with mandatory locks held on them. |
| 2984 | */ |
Jeff Layton | d7a0698 | 2014-03-10 09:54:15 -0400 | [diff] [blame] | 2985 | error = locks_verify_locked(filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2986 | if (!error) |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 2987 | error = security_path_truncate(path); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2988 | if (!error) { |
Greg Kroah-Hartman | 584c547 | 2020-05-28 16:46:54 +0200 | [diff] [blame] | 2989 | error = do_truncate(path->dentry, 0, |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2990 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2991 | filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2992 | } |
| 2993 | put_write_access(inode); |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 2994 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | } |
| 2996 | |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2997 | static inline int open_to_namei_flags(int flag) |
| 2998 | { |
Al Viro | 8a5e929 | 2011-06-25 19:15:54 -0400 | [diff] [blame] | 2999 | if ((flag & O_ACCMODE) == 3) |
| 3000 | flag--; |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 3001 | return flag; |
| 3002 | } |
| 3003 | |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 3004 | static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3005 | { |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 3006 | struct user_namespace *s_user_ns; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3007 | int error = security_path_mknod(dir, dentry, mode, 0); |
| 3008 | if (error) |
| 3009 | return error; |
| 3010 | |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 3011 | s_user_ns = dir->dentry->d_sb->s_user_ns; |
| 3012 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || |
| 3013 | !kgid_has_mapping(s_user_ns, current_fsgid())) |
| 3014 | return -EOVERFLOW; |
| 3015 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3016 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3017 | if (error) |
| 3018 | return error; |
| 3019 | |
| 3020 | return security_inode_create(dir->dentry->d_inode, dentry, mode); |
| 3021 | } |
| 3022 | |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3023 | /* |
| 3024 | * Attempt to atomically look up, create and open a file from a negative |
| 3025 | * dentry. |
| 3026 | * |
| 3027 | * Returns 0 if successful. The file will have been created and attached to |
| 3028 | * @file by the filesystem calling finish_open(). |
| 3029 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3030 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
| 3031 | * be set. The caller will need to perform the open themselves. @path will |
| 3032 | * 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] | 3033 | * |
| 3034 | * Returns an error code otherwise. |
| 3035 | */ |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3036 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
| 3037 | struct file *file, |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3038 | int open_flag, umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3039 | { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3040 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3041 | struct inode *dir = nd->path.dentry->d_inode; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3042 | int error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3043 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3044 | if (nd->flags & LOOKUP_DIRECTORY) |
| 3045 | open_flag |= O_DIRECTORY; |
| 3046 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3047 | file->f_path.dentry = DENTRY_NOT_SET; |
| 3048 | file->f_path.mnt = nd->path.mnt; |
Al Viro | 0fb1ea0 | 2016-04-27 14:13:10 -0400 | [diff] [blame] | 3049 | error = dir->i_op->atomic_open(dir, dentry, file, |
Al Viro | 44907d7 | 2018-06-08 13:32:02 -0400 | [diff] [blame] | 3050 | open_to_namei_flags(open_flag), mode); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3051 | d_lookup_done(dentry); |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 3052 | if (!error) { |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 3053 | if (file->f_mode & FMODE_OPENED) { |
Al Viro | 6fb968c | 2020-01-26 09:53:19 -0500 | [diff] [blame] | 3054 | if (unlikely(dentry != file->f_path.dentry)) { |
| 3055 | dput(dentry); |
| 3056 | dentry = dget(file->f_path.dentry); |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 3057 | } |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 3058 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3059 | error = -EIO; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 3060 | } else { |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 3061 | if (file->f_path.dentry) { |
| 3062 | dput(dentry); |
| 3063 | dentry = file->f_path.dentry; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 3064 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3065 | if (unlikely(d_is_negative(dentry))) |
Al Viro | a01e718 | 2016-06-07 21:53:51 -0400 | [diff] [blame] | 3066 | error = -ENOENT; |
Sage Weil | 62b2ce9 | 2012-08-15 13:30:12 -0700 | [diff] [blame] | 3067 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3068 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3069 | if (error) { |
| 3070 | dput(dentry); |
| 3071 | dentry = ERR_PTR(error); |
| 3072 | } |
| 3073 | return dentry; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3074 | } |
| 3075 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3076 | /* |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3077 | * Look up and maybe create and open the last component. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3078 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3079 | * Must be called with parent locked (exclusive in O_CREAT case). |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3080 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3081 | * Returns 0 on success, that is, if |
| 3082 | * the file was successfully atomically created (if necessary) and opened, or |
| 3083 | * the file was not completely opened at this time, though lookups and |
| 3084 | * creations were performed. |
| 3085 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. |
| 3086 | * In the latter case dentry returned in @path might be negative if O_CREAT |
| 3087 | * hadn't been specified. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 3088 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3089 | * An error code is returned on failure. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3090 | */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3091 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
| 3092 | const struct open_flags *op, |
| 3093 | bool got_write) |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3094 | { |
| 3095 | struct dentry *dir = nd->path.dentry; |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3096 | struct inode *dir_inode = dir->d_inode; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3097 | int open_flag = op->open_flag; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3098 | struct dentry *dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3099 | int error, create_error = 0; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3100 | umode_t mode = op->mode; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3101 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3102 | |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3103 | if (unlikely(IS_DEADDIR(dir_inode))) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3104 | return ERR_PTR(-ENOENT); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3105 | |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3106 | file->f_mode &= ~FMODE_CREATED; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3107 | dentry = d_lookup(dir, &nd->last); |
| 3108 | for (;;) { |
| 3109 | if (!dentry) { |
| 3110 | dentry = d_alloc_parallel(dir, &nd->last, &wq); |
| 3111 | if (IS_ERR(dentry)) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3112 | return dentry; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3113 | } |
| 3114 | if (d_in_lookup(dentry)) |
| 3115 | break; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3116 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3117 | error = d_revalidate(dentry, nd->flags); |
| 3118 | if (likely(error > 0)) |
| 3119 | break; |
| 3120 | if (error) |
| 3121 | goto out_dput; |
| 3122 | d_invalidate(dentry); |
| 3123 | dput(dentry); |
| 3124 | dentry = NULL; |
| 3125 | } |
| 3126 | if (dentry->d_inode) { |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3127 | /* Cached positive dentry: will open in f_op->open */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3128 | return dentry; |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3129 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3130 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3131 | /* |
| 3132 | * Checking write permission is tricky, bacuse we don't know if we are |
| 3133 | * going to actually need it: O_CREAT opens should work as long as the |
| 3134 | * file exists. But checking existence breaks atomicity. The trick is |
| 3135 | * to check access and if not granted clear O_CREAT from the flags. |
| 3136 | * |
| 3137 | * Another problem is returing the "right" error value (e.g. for an |
| 3138 | * O_EXCL open we want to return EEXIST not EROFS). |
| 3139 | */ |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3140 | if (unlikely(!got_write)) |
| 3141 | open_flag &= ~O_TRUNC; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3142 | if (open_flag & O_CREAT) { |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3143 | if (open_flag & O_EXCL) |
| 3144 | open_flag &= ~O_TRUNC; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3145 | if (!IS_POSIXACL(dir->d_inode)) |
| 3146 | mode &= ~current_umask(); |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3147 | if (likely(got_write)) |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3148 | create_error = may_o_create(&nd->path, dentry, mode); |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3149 | else |
| 3150 | create_error = -EROFS; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3151 | } |
Al Viro | 99a4a90 | 2020-03-12 14:07:27 -0400 | [diff] [blame] | 3152 | if (create_error) |
| 3153 | open_flag &= ~O_CREAT; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3154 | if (dir_inode->i_op->atomic_open) { |
Al Viro | d489cf9 | 2020-03-11 08:07:53 -0400 | [diff] [blame] | 3155 | dentry = atomic_open(nd, dentry, file, open_flag, mode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3156 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
| 3157 | dentry = ERR_PTR(create_error); |
| 3158 | return dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3159 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3160 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3161 | if (d_in_lookup(dentry)) { |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3162 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
| 3163 | nd->flags); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3164 | d_lookup_done(dentry); |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3165 | if (unlikely(res)) { |
| 3166 | if (IS_ERR(res)) { |
| 3167 | error = PTR_ERR(res); |
| 3168 | goto out_dput; |
| 3169 | } |
| 3170 | dput(dentry); |
| 3171 | dentry = res; |
| 3172 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3173 | } |
| 3174 | |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3175 | /* Negative dentry, just create the file */ |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3176 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3177 | file->f_mode |= FMODE_CREATED; |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3178 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3179 | if (!dir_inode->i_op->create) { |
| 3180 | error = -EACCES; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3181 | goto out_dput; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3182 | } |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3183 | error = dir_inode->i_op->create(dir_inode, dentry, mode, |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3184 | open_flag & O_EXCL); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3185 | if (error) |
| 3186 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3187 | } |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3188 | if (unlikely(create_error) && !dentry->d_inode) { |
| 3189 | error = create_error; |
| 3190 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3191 | } |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3192 | return dentry; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3193 | |
| 3194 | out_dput: |
| 3195 | dput(dentry); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3196 | return ERR_PTR(error); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3197 | } |
| 3198 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3199 | static const char *open_last_lookups(struct nameidata *nd, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3200 | struct file *file, const struct open_flags *op) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3201 | { |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3202 | struct dentry *dir = nd->path.dentry; |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3203 | int open_flag = op->open_flag; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3204 | bool got_write = false; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 3205 | unsigned seq; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3206 | struct inode *inode; |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3207 | struct dentry *dentry; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3208 | const char *res; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3209 | |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 3210 | nd->flags |= op->intent; |
| 3211 | |
Al Viro | bc77daa | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 3212 | if (nd->last_type != LAST_NORM) { |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3213 | if (nd->depth) |
| 3214 | put_link(nd); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3215 | return handle_dots(nd, nd->last_type); |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 3216 | } |
Al Viro | 67ee3ad | 2009-12-26 07:01:01 -0500 | [diff] [blame] | 3217 | |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3218 | if (!(open_flag & O_CREAT)) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3219 | if (nd->last.name[nd->last.len]) |
| 3220 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 3221 | /* we _can_ be in RCU mode here */ |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3222 | dentry = lookup_fast(nd, &inode, &seq); |
| 3223 | if (IS_ERR(dentry)) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3224 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3225 | if (likely(dentry)) |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 3226 | goto finish_lookup; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3227 | |
Al Viro | 6583fe2 | 2016-03-05 18:14:03 -0500 | [diff] [blame] | 3228 | BUG_ON(nd->flags & LOOKUP_RCU); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3229 | } else { |
| 3230 | /* create side of things */ |
Al Viro | 7228741 | 2020-03-10 10:09:26 -0400 | [diff] [blame] | 3231 | if (nd->flags & LOOKUP_RCU) { |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 3232 | if (!try_to_unlazy(nd)) |
| 3233 | return ERR_PTR(-ECHILD); |
Al Viro | 7228741 | 2020-03-10 10:09:26 -0400 | [diff] [blame] | 3234 | } |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 3235 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3236 | /* trailing slashes? */ |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 3237 | if (unlikely(nd->last.name[nd->last.len])) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3238 | return ERR_PTR(-EISDIR); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3239 | } |
| 3240 | |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3241 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
Jens Axboe | 01fd84a | 2020-12-17 09:19:08 -0700 | [diff] [blame] | 3242 | got_write = !mnt_want_write(nd->path.mnt); |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3243 | /* |
| 3244 | * do _not_ fail yet - we might not need that or fail with |
| 3245 | * a different error; let lookup_open() decide; we'll be |
| 3246 | * dropping this one anyway. |
| 3247 | */ |
| 3248 | } |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3249 | if (open_flag & O_CREAT) |
| 3250 | inode_lock(dir->d_inode); |
| 3251 | else |
| 3252 | inode_lock_shared(dir->d_inode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3253 | dentry = lookup_open(nd, file, op, got_write); |
Al Viro | f7bb959 | 2020-03-05 13:25:20 -0500 | [diff] [blame] | 3254 | if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) |
| 3255 | fsnotify_create(dir->d_inode, dentry); |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3256 | if (open_flag & O_CREAT) |
| 3257 | inode_unlock(dir->d_inode); |
| 3258 | else |
| 3259 | inode_unlock_shared(dir->d_inode); |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3260 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3261 | if (got_write) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3262 | mnt_drop_write(nd->path.mnt); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3263 | |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3264 | if (IS_ERR(dentry)) |
| 3265 | return ERR_CAST(dentry); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3266 | |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3267 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
Al Viro | e73cabf | 2020-01-09 14:30:08 -0500 | [diff] [blame] | 3268 | dput(nd->path.dentry); |
| 3269 | nd->path.dentry = dentry; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3270 | return NULL; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3271 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3272 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3273 | finish_lookup: |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3274 | if (nd->depth) |
| 3275 | put_link(nd); |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 3276 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3277 | if (unlikely(res)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3278 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3279 | return res; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | /* |
| 3283 | * Handle the last step of open() |
| 3284 | */ |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3285 | static int do_open(struct nameidata *nd, |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3286 | struct file *file, const struct open_flags *op) |
| 3287 | { |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3288 | int open_flag = op->open_flag; |
| 3289 | bool do_truncate; |
| 3290 | int acc_mode; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3291 | int error; |
| 3292 | |
Al Viro | ff326a3 | 2020-03-10 10:19:24 -0400 | [diff] [blame] | 3293 | if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { |
| 3294 | error = complete_walk(nd); |
| 3295 | if (error) |
| 3296 | return error; |
| 3297 | } |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3298 | if (!(file->f_mode & FMODE_CREATED)) |
| 3299 | audit_inode(nd->name, nd->path.dentry, 0); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3300 | if (open_flag & O_CREAT) { |
Al Viro | b94e0b3 | 2020-03-10 10:13:53 -0400 | [diff] [blame] | 3301 | if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) |
| 3302 | return -EEXIST; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3303 | if (d_is_dir(nd->path.dentry)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3304 | return -EISDIR; |
Al Viro | 0f70595 | 2020-03-05 11:34:48 -0500 | [diff] [blame] | 3305 | error = may_create_in_sticky(nd->dir_mode, nd->dir_uid, |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3306 | d_backing_inode(nd->path.dentry)); |
| 3307 | if (unlikely(error)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3308 | return error; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3309 | } |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3310 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3311 | return -ENOTDIR; |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 3312 | |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3313 | do_truncate = false; |
| 3314 | acc_mode = op->acc_mode; |
Al Viro | 648fa86 | 2009-12-24 01:26:48 -0500 | [diff] [blame] | 3315 | if (file->f_mode & FMODE_CREATED) { |
| 3316 | /* Don't check for write permission, don't truncate */ |
| 3317 | open_flag &= ~O_TRUNC; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3318 | acc_mode = 0; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3319 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3320 | error = mnt_want_write(nd->path.mnt); |
| 3321 | if (error) |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3322 | return error; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3323 | do_truncate = true; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3324 | } |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3325 | error = may_open(&nd->path, acc_mode, open_flag); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3326 | if (!error && !(file->f_mode & FMODE_OPENED)) |
Al Viro | 3ad5615 | 2020-01-26 10:06:13 -0500 | [diff] [blame] | 3327 | error = vfs_open(&nd->path, file); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3328 | if (!error) |
| 3329 | error = ima_file_check(file, op->acc_mode); |
| 3330 | if (!error && do_truncate) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3331 | error = handle_truncate(file); |
Al Viro | c80567c | 2016-02-27 19:17:33 -0500 | [diff] [blame] | 3332 | if (unlikely(error > 0)) { |
| 3333 | WARN_ON(1); |
| 3334 | error = -EINVAL; |
| 3335 | } |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3336 | if (do_truncate) |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3337 | mnt_drop_write(nd->path.mnt); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3338 | return error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3339 | } |
| 3340 | |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3341 | struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag) |
| 3342 | { |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3343 | struct dentry *child = NULL; |
| 3344 | struct inode *dir = dentry->d_inode; |
| 3345 | struct inode *inode; |
| 3346 | int error; |
| 3347 | |
| 3348 | /* we want directory to be writable */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3349 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3350 | if (error) |
| 3351 | goto out_err; |
| 3352 | error = -EOPNOTSUPP; |
| 3353 | if (!dir->i_op->tmpfile) |
| 3354 | goto out_err; |
| 3355 | error = -ENOMEM; |
David Howells | cdf0122 | 2017-07-04 17:25:22 +0100 | [diff] [blame] | 3356 | child = d_alloc(dentry, &slash_name); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3357 | if (unlikely(!child)) |
| 3358 | goto out_err; |
| 3359 | error = dir->i_op->tmpfile(dir, child, mode); |
| 3360 | if (error) |
| 3361 | goto out_err; |
| 3362 | error = -ENOENT; |
| 3363 | inode = child->d_inode; |
| 3364 | if (unlikely(!inode)) |
| 3365 | goto out_err; |
| 3366 | if (!(open_flag & O_EXCL)) { |
| 3367 | spin_lock(&inode->i_lock); |
| 3368 | inode->i_state |= I_LINKABLE; |
| 3369 | spin_unlock(&inode->i_lock); |
| 3370 | } |
Mimi Zohar | fdb2410 | 2019-01-22 14:06:49 -0600 | [diff] [blame] | 3371 | ima_post_create_tmpfile(inode); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3372 | return child; |
| 3373 | |
| 3374 | out_err: |
| 3375 | dput(child); |
| 3376 | return ERR_PTR(error); |
| 3377 | } |
| 3378 | EXPORT_SYMBOL(vfs_tmpfile); |
| 3379 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3380 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3381 | const struct open_flags *op, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3382 | struct file *file) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3383 | { |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3384 | struct dentry *child; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3385 | struct path path; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3386 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3387 | if (unlikely(error)) |
| 3388 | return error; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3389 | error = mnt_want_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3390 | if (unlikely(error)) |
| 3391 | goto out; |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3392 | child = vfs_tmpfile(path.dentry, op->mode, op->open_flag); |
| 3393 | error = PTR_ERR(child); |
Hirofumi Nakagawa | 684e73b | 2017-09-26 03:21:26 +0900 | [diff] [blame] | 3394 | if (IS_ERR(child)) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3395 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3396 | dput(path.dentry); |
| 3397 | path.dentry = child; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3398 | audit_inode(nd->name, child, 0); |
Eric Rannaud | 69a91c2 | 2014-10-30 01:51:01 -0700 | [diff] [blame] | 3399 | /* Don't check for other permissions, the inode was just created */ |
Al Viro | 62fb4a1 | 2015-12-26 22:33:24 -0500 | [diff] [blame] | 3400 | error = may_open(&path, 0, op->open_flag); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3401 | if (error) |
| 3402 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3403 | file->f_path.mnt = path.mnt; |
Al Viro | be12af3 | 2018-06-08 11:44:56 -0400 | [diff] [blame] | 3404 | error = finish_open(file, child, NULL); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3405 | out2: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3406 | mnt_drop_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3407 | out: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3408 | path_put(&path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3409 | return error; |
| 3410 | } |
| 3411 | |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3412 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
| 3413 | { |
| 3414 | struct path path; |
| 3415 | int error = path_lookupat(nd, flags, &path); |
| 3416 | if (!error) { |
| 3417 | audit_inode(nd->name, path.dentry, 0); |
Al Viro | ae2bb29 | 2018-07-10 13:22:28 -0400 | [diff] [blame] | 3418 | error = vfs_open(&path, file); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3419 | path_put(&path); |
| 3420 | } |
| 3421 | return error; |
| 3422 | } |
| 3423 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3424 | static struct file *path_openat(struct nameidata *nd, |
| 3425 | const struct open_flags *op, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | { |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3427 | struct file *file; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3428 | int error; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3429 | |
Al Viro | ea73ea7 | 2018-07-11 15:00:04 -0400 | [diff] [blame] | 3430 | file = alloc_empty_file(op->open_flag, current_cred()); |
Al Viro | 1afc99b | 2013-02-14 20:41:04 -0500 | [diff] [blame] | 3431 | if (IS_ERR(file)) |
| 3432 | return file; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3433 | |
Al Viro | bb458c6 | 2013-07-13 13:26:37 +0400 | [diff] [blame] | 3434 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3435 | error = do_tmpfile(nd, flags, op, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3436 | } else if (unlikely(file->f_flags & O_PATH)) { |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3437 | error = do_o_path(nd, flags, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3438 | } else { |
| 3439 | const char *s = path_init(nd, flags); |
| 3440 | while (!(error = link_path_walk(s, nd)) && |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3441 | (s = open_last_lookups(nd, file, op)) != NULL) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3442 | ; |
Al Viro | c5971b8 | 2020-03-05 11:41:29 -0500 | [diff] [blame] | 3443 | if (!error) |
| 3444 | error = do_open(nd, file, op); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3445 | terminate_walk(nd); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3446 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3447 | if (likely(!error)) { |
Al Viro | aad888f | 2018-06-08 12:58:04 -0400 | [diff] [blame] | 3448 | if (likely(file->f_mode & FMODE_OPENED)) |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3449 | return file; |
| 3450 | WARN_ON(1); |
| 3451 | error = -EINVAL; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 3452 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3453 | fput(file); |
| 3454 | if (error == -EOPENSTALE) { |
| 3455 | if (flags & LOOKUP_RCU) |
| 3456 | error = -ECHILD; |
| 3457 | else |
| 3458 | error = -ESTALE; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3459 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3460 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | } |
| 3462 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3463 | struct file *do_filp_open(int dfd, struct filename *pathname, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3464 | const struct open_flags *op) |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3465 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3466 | struct nameidata nd; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3467 | int flags = op->lookup_flags; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3468 | struct file *filp; |
| 3469 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3470 | set_nameidata(&nd, dfd, pathname); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3471 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3472 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3473 | filp = path_openat(&nd, op, flags); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3474 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3475 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3476 | restore_nameidata(); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3477 | return filp; |
| 3478 | } |
| 3479 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3480 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3481 | const char *name, const struct open_flags *op) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3482 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3483 | struct nameidata nd; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3484 | struct file *file; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3485 | struct filename *filename; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3486 | int flags = op->lookup_flags | LOOKUP_ROOT; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3487 | |
| 3488 | nd.root.mnt = mnt; |
| 3489 | nd.root.dentry = dentry; |
| 3490 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3491 | if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3492 | return ERR_PTR(-ELOOP); |
| 3493 | |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3494 | filename = getname_kernel(name); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 3495 | if (IS_ERR(filename)) |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3496 | return ERR_CAST(filename); |
| 3497 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3498 | set_nameidata(&nd, -1, filename); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3499 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3500 | if (unlikely(file == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3501 | file = path_openat(&nd, op, flags); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3502 | if (unlikely(file == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3503 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3504 | restore_nameidata(); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3505 | putname(filename); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3506 | return file; |
| 3507 | } |
| 3508 | |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3509 | static struct dentry *filename_create(int dfd, struct filename *name, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3510 | struct path *path, unsigned int lookup_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3512 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3513 | struct qstr last; |
| 3514 | int type; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3515 | int err2; |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3516 | int error; |
| 3517 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); |
| 3518 | |
| 3519 | /* |
| 3520 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any |
| 3521 | * other flags passed in are ignored! |
| 3522 | */ |
| 3523 | lookup_flags &= LOOKUP_REVAL; |
| 3524 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3525 | name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
| 3526 | if (IS_ERR(name)) |
| 3527 | return ERR_CAST(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3529 | /* |
| 3530 | * Yucky last component or no last component at all? |
| 3531 | * (foo/., foo/.., /////) |
| 3532 | */ |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3533 | if (unlikely(type != LAST_NORM)) |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3534 | goto out; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3535 | |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3536 | /* 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] | 3537 | err2 = mnt_want_write(path->mnt); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3538 | /* |
| 3539 | * Do the final lookup. |
| 3540 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3541 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3542 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3543 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | if (IS_ERR(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3545 | goto unlock; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3546 | |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3547 | error = -EEXIST; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3548 | if (d_is_positive(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3549 | goto fail; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3550 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3551 | /* |
| 3552 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 3553 | * From the vfs_mknod() POV we just have a negative dentry - |
| 3554 | * all is fine. Let's be bastards - you had / on the end, you've |
| 3555 | * been asking for (non-existent) directory. -ENOENT for you. |
| 3556 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3557 | if (unlikely(!is_dir && last.name[last.len])) { |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3558 | error = -ENOENT; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3559 | goto fail; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3560 | } |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3561 | if (unlikely(err2)) { |
| 3562 | error = err2; |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3563 | goto fail; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3564 | } |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3565 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | fail: |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3568 | dput(dentry); |
| 3569 | dentry = ERR_PTR(error); |
| 3570 | unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3571 | inode_unlock(path->dentry->d_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3572 | if (!err2) |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3573 | mnt_drop_write(path->mnt); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3574 | out: |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3575 | path_put(path); |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3576 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | return dentry; |
| 3578 | } |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3579 | |
| 3580 | struct dentry *kern_path_create(int dfd, const char *pathname, |
| 3581 | struct path *path, unsigned int lookup_flags) |
| 3582 | { |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3583 | return filename_create(dfd, getname_kernel(pathname), |
| 3584 | path, lookup_flags); |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3585 | } |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3586 | EXPORT_SYMBOL(kern_path_create); |
| 3587 | |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3588 | void done_path_create(struct path *path, struct dentry *dentry) |
| 3589 | { |
| 3590 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3591 | inode_unlock(path->dentry->d_inode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3592 | mnt_drop_write(path->mnt); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3593 | path_put(path); |
| 3594 | } |
| 3595 | EXPORT_SYMBOL(done_path_create); |
| 3596 | |
Al Viro | 520ae68 | 2015-05-13 07:00:28 -0400 | [diff] [blame] | 3597 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3598 | struct path *path, unsigned int lookup_flags) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3599 | { |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3600 | return filename_create(dfd, getname(pathname), path, lookup_flags); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3601 | } |
| 3602 | EXPORT_SYMBOL(user_path_create); |
| 3603 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3604 | int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | { |
Miklos Szeredi | a3c751a | 2020-05-14 16:44:23 +0200 | [diff] [blame] | 3606 | bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3607 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3608 | |
| 3609 | if (error) |
| 3610 | return error; |
| 3611 | |
Miklos Szeredi | a3c751a | 2020-05-14 16:44:23 +0200 | [diff] [blame] | 3612 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && |
| 3613 | !capable(CAP_MKNOD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | return -EPERM; |
| 3615 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3616 | if (!dir->i_op->mknod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | return -EPERM; |
| 3618 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3619 | error = devcgroup_inode_mknod(mode, dev); |
| 3620 | if (error) |
| 3621 | return error; |
| 3622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 3624 | if (error) |
| 3625 | return error; |
| 3626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3628 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3629 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | return error; |
| 3631 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3632 | EXPORT_SYMBOL(vfs_mknod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | |
Al Viro | f69aac0 | 2011-07-26 04:31:40 -0400 | [diff] [blame] | 3634 | static int may_mknod(umode_t mode) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3635 | { |
| 3636 | switch (mode & S_IFMT) { |
| 3637 | case S_IFREG: |
| 3638 | case S_IFCHR: |
| 3639 | case S_IFBLK: |
| 3640 | case S_IFIFO: |
| 3641 | case S_IFSOCK: |
| 3642 | case 0: /* zero mode translates to S_IFREG */ |
| 3643 | return 0; |
| 3644 | case S_IFDIR: |
| 3645 | return -EPERM; |
| 3646 | default: |
| 3647 | return -EINVAL; |
| 3648 | } |
| 3649 | } |
| 3650 | |
Christoph Hellwig | 5fee64f | 2020-07-22 11:41:20 +0200 | [diff] [blame] | 3651 | static long do_mknodat(int dfd, const char __user *filename, umode_t mode, |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3652 | unsigned int dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3654 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3655 | struct path path; |
| 3656 | int error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3657 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | |
Al Viro | 8e4bfca | 2012-07-20 01:17:26 +0400 | [diff] [blame] | 3659 | error = may_mknod(mode); |
| 3660 | if (error) |
| 3661 | return error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3662 | retry: |
| 3663 | dentry = user_path_create(dfd, filename, &path, lookup_flags); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3664 | if (IS_ERR(dentry)) |
| 3665 | return PTR_ERR(dentry); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3666 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3667 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3668 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3669 | error = security_path_mknod(&path, dentry, mode, dev); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3670 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3671 | goto out; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3672 | switch (mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | case 0: case S_IFREG: |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3674 | error = vfs_create(path.dentry->d_inode,dentry,mode,true); |
Mimi Zohar | 05d1a71 | 2016-02-29 19:52:05 -0500 | [diff] [blame] | 3675 | if (!error) |
| 3676 | ima_post_path_mknod(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | break; |
| 3678 | case S_IFCHR: case S_IFBLK: |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3679 | error = vfs_mknod(path.dentry->d_inode,dentry,mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | new_decode_dev(dev)); |
| 3681 | break; |
| 3682 | case S_IFIFO: case S_IFSOCK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3683 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | } |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3686 | out: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3687 | done_path_create(&path, dentry); |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3688 | if (retry_estale(error, lookup_flags)) { |
| 3689 | lookup_flags |= LOOKUP_REVAL; |
| 3690 | goto retry; |
| 3691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3692 | return error; |
| 3693 | } |
| 3694 | |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3695 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
| 3696 | unsigned int, dev) |
| 3697 | { |
| 3698 | return do_mknodat(dfd, filename, mode, dev); |
| 3699 | } |
| 3700 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3701 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3702 | { |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3703 | return do_mknodat(AT_FDCWD, filename, mode, dev); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3704 | } |
| 3705 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3706 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3708 | int error = may_create(dir, dentry); |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3709 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | |
| 3711 | if (error) |
| 3712 | return error; |
| 3713 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3714 | if (!dir->i_op->mkdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | return -EPERM; |
| 3716 | |
| 3717 | mode &= (S_IRWXUGO|S_ISVTX); |
| 3718 | error = security_inode_mkdir(dir, dentry, mode); |
| 3719 | if (error) |
| 3720 | return error; |
| 3721 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3722 | if (max_links && dir->i_nlink >= max_links) |
| 3723 | return -EMLINK; |
| 3724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | error = dir->i_op->mkdir(dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3726 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3727 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | return error; |
| 3729 | } |
Greg Kroah-Hartman | d483eed | 2020-07-02 12:51:03 +0200 | [diff] [blame] | 3730 | EXPORT_SYMBOL_NS(vfs_mkdir, ANDROID_GKI_VFS_EXPORT_ONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | |
Christoph Hellwig | 83ff98c | 2020-07-22 11:14:59 +0200 | [diff] [blame] | 3732 | static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | { |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3734 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3735 | struct path path; |
| 3736 | int error; |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3737 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3739 | retry: |
| 3740 | dentry = user_path_create(dfd, pathname, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3741 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3742 | return PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3743 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3744 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3745 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3746 | error = security_path_mkdir(&path, dentry, mode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3747 | if (!error) |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3748 | error = vfs_mkdir(path.dentry->d_inode, dentry, mode); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3749 | done_path_create(&path, dentry); |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3750 | if (retry_estale(error, lookup_flags)) { |
| 3751 | lookup_flags |= LOOKUP_REVAL; |
| 3752 | goto retry; |
| 3753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | return error; |
| 3755 | } |
| 3756 | |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3757 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
| 3758 | { |
| 3759 | return do_mkdirat(dfd, pathname, mode); |
| 3760 | } |
| 3761 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3762 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3763 | { |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3764 | return do_mkdirat(AT_FDCWD, pathname, mode); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3765 | } |
| 3766 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3767 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3769 | int error = may_delete(dir, dentry, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | |
| 3771 | if (error) |
| 3772 | return error; |
| 3773 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3774 | if (!dir->i_op->rmdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | return -EPERM; |
| 3776 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3777 | dget(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3778 | inode_lock(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3780 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 3781 | if (is_local_mountpoint(dentry)) |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3782 | goto out; |
| 3783 | |
| 3784 | error = security_inode_rmdir(dir, dentry); |
| 3785 | if (error) |
| 3786 | goto out; |
| 3787 | |
| 3788 | error = dir->i_op->rmdir(dir, dentry); |
| 3789 | if (error) |
| 3790 | goto out; |
| 3791 | |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 3792 | shrink_dcache_parent(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3793 | dentry->d_inode->i_flags |= S_DEAD; |
| 3794 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3795 | detach_mounts(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3796 | |
| 3797 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3798 | inode_unlock(dentry->d_inode); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3799 | dput(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3800 | if (!error) |
Amir Goldstein | 0b4e824 | 2022-01-20 23:53:04 +0200 | [diff] [blame] | 3801 | d_delete_notify(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | return error; |
| 3803 | } |
Greg Kroah-Hartman | d483eed | 2020-07-02 12:51:03 +0200 | [diff] [blame] | 3804 | EXPORT_SYMBOL_NS(vfs_rmdir, ANDROID_GKI_VFS_EXPORT_ONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 3806 | long do_rmdir(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | { |
| 3808 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3810 | struct path path; |
| 3811 | struct qstr last; |
| 3812 | int type; |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 3813 | unsigned int lookup_flags = 0; |
| 3814 | retry: |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 3815 | name = filename_parentat(dfd, name, lookup_flags, |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 3816 | &path, &last, &type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3817 | if (IS_ERR(name)) |
| 3818 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3820 | switch (type) { |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3821 | case LAST_DOTDOT: |
| 3822 | error = -ENOTEMPTY; |
| 3823 | goto exit1; |
| 3824 | case LAST_DOT: |
| 3825 | error = -EINVAL; |
| 3826 | goto exit1; |
| 3827 | case LAST_ROOT: |
| 3828 | error = -EBUSY; |
| 3829 | goto exit1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | } |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3831 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3832 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3833 | if (error) |
| 3834 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3835 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3836 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3837 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3839 | if (IS_ERR(dentry)) |
| 3840 | goto exit2; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3841 | if (!dentry->d_inode) { |
| 3842 | error = -ENOENT; |
| 3843 | goto exit3; |
| 3844 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3845 | error = security_path_rmdir(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3846 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3847 | goto exit3; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3848 | error = vfs_rmdir(path.dentry->d_inode, dentry); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 3849 | exit3: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3850 | dput(dentry); |
| 3851 | exit2: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3852 | inode_unlock(path.dentry->d_inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3853 | mnt_drop_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3855 | path_put(&path); |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 3856 | if (retry_estale(error, lookup_flags)) { |
| 3857 | lookup_flags |= LOOKUP_REVAL; |
| 3858 | goto retry; |
| 3859 | } |
Al Viro | 24fb33d | 2020-08-12 05:15:18 +0100 | [diff] [blame] | 3860 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | return error; |
| 3862 | } |
| 3863 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 3864 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3865 | { |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 3866 | return do_rmdir(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3867 | } |
| 3868 | |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3869 | /** |
| 3870 | * vfs_unlink - unlink a filesystem object |
| 3871 | * @dir: parent directory |
| 3872 | * @dentry: victim |
| 3873 | * @delegated_inode: returns victim inode, if the inode is delegated. |
| 3874 | * |
| 3875 | * The caller must hold dir->i_mutex. |
| 3876 | * |
| 3877 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and |
| 3878 | * return a reference to the inode in delegated_inode. The caller |
| 3879 | * should then break the delegation on that inode and retry. Because |
| 3880 | * breaking a delegation may take a long time, the caller should drop |
| 3881 | * dir->i_mutex before doing so. |
| 3882 | * |
| 3883 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 3884 | * be appropriate for callers that expect the underlying filesystem not |
| 3885 | * to be NFS exported. |
| 3886 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3887 | int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 3889 | struct inode *target = dentry->d_inode; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3890 | int error = may_delete(dir, dentry, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | |
| 3892 | if (error) |
| 3893 | return error; |
| 3894 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3895 | if (!dir->i_op->unlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | return -EPERM; |
| 3897 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3898 | inode_lock(target); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3899 | if (is_local_mountpoint(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | error = -EBUSY; |
| 3901 | else { |
| 3902 | error = security_inode_unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3903 | if (!error) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 3904 | error = try_break_deleg(target, delegated_inode); |
| 3905 | if (error) |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3906 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | error = dir->i_op->unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3908 | if (!error) { |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 3909 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3910 | detach_mounts(dentry); |
| 3911 | } |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | } |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3914 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3915 | inode_unlock(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | |
| 3917 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
Amir Goldstein | 0b4e824 | 2022-01-20 23:53:04 +0200 | [diff] [blame] | 3918 | if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
| 3919 | fsnotify_unlink(dir, dentry); |
| 3920 | } else if (!error) { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 3921 | fsnotify_link_count(target); |
Amir Goldstein | 0b4e824 | 2022-01-20 23:53:04 +0200 | [diff] [blame] | 3922 | d_delete_notify(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | return error; |
| 3926 | } |
Greg Kroah-Hartman | d483eed | 2020-07-02 12:51:03 +0200 | [diff] [blame] | 3927 | EXPORT_SYMBOL_NS(vfs_unlink, ANDROID_GKI_VFS_EXPORT_ONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | |
| 3929 | /* |
| 3930 | * 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] | 3931 | * 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] | 3932 | * writeout happening, and we don't want to prevent access to the directory |
| 3933 | * while waiting on the I/O. |
| 3934 | */ |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3935 | long do_unlinkat(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3937 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3939 | struct path path; |
| 3940 | struct qstr last; |
| 3941 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | struct inode *inode = NULL; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3943 | struct inode *delegated_inode = NULL; |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 3944 | unsigned int lookup_flags = 0; |
| 3945 | retry: |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3946 | name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3947 | if (IS_ERR(name)) |
| 3948 | return PTR_ERR(name); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | error = -EISDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3951 | if (type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3953 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3954 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3955 | if (error) |
| 3956 | goto exit1; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3957 | retry_deleg: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3958 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3959 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | error = PTR_ERR(dentry); |
| 3961 | if (!IS_ERR(dentry)) { |
| 3962 | /* Why not before? Because we want correct error value */ |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3963 | if (last.name[last.len]) |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 3964 | goto slashes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | inode = dentry->d_inode; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3966 | if (d_is_negative(dentry)) |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3967 | goto slashes; |
| 3968 | ihold(inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3969 | error = security_path_unlink(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3970 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3971 | goto exit2; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 3972 | error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3973 | exit2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | dput(dentry); |
| 3975 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3976 | inode_unlock(path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | if (inode) |
| 3978 | iput(inode); /* truncate the inode here */ |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3979 | inode = NULL; |
| 3980 | if (delegated_inode) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 3981 | error = break_deleg_wait(&delegated_inode); |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3982 | if (!error) |
| 3983 | goto retry_deleg; |
| 3984 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3985 | mnt_drop_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3987 | path_put(&path); |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 3988 | if (retry_estale(error, lookup_flags)) { |
| 3989 | lookup_flags |= LOOKUP_REVAL; |
| 3990 | inode = NULL; |
| 3991 | goto retry; |
| 3992 | } |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3993 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | return error; |
| 3995 | |
| 3996 | slashes: |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3997 | if (d_is_negative(dentry)) |
| 3998 | error = -ENOENT; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3999 | else if (d_is_dir(dentry)) |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4000 | error = -EISDIR; |
| 4001 | else |
| 4002 | error = -ENOTDIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | goto exit2; |
| 4004 | } |
| 4005 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 4006 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4007 | { |
| 4008 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 4009 | return -EINVAL; |
| 4010 | |
| 4011 | if (flag & AT_REMOVEDIR) |
Christoph Hellwig | e24ab0e | 2020-07-21 10:48:15 +0200 | [diff] [blame] | 4012 | return do_rmdir(dfd, getname(pathname)); |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4013 | return do_unlinkat(dfd, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4014 | } |
| 4015 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4016 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4017 | { |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 4018 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4019 | } |
| 4020 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4021 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4023 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | |
| 4025 | if (error) |
| 4026 | return error; |
| 4027 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4028 | if (!dir->i_op->symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | return -EPERM; |
| 4030 | |
| 4031 | error = security_inode_symlink(dir, dentry, oldname); |
| 4032 | if (error) |
| 4033 | return error; |
| 4034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4035 | error = dir->i_op->symlink(dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 4036 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 4037 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | return error; |
| 4039 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4040 | EXPORT_SYMBOL(vfs_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | |
Christoph Hellwig | cd3acb6 | 2020-07-22 11:14:36 +0200 | [diff] [blame] | 4042 | static long do_symlinkat(const char __user *oldname, int newdfd, |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4043 | const char __user *newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4045 | int error; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4046 | struct filename *from; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4047 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4048 | struct path path; |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4049 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | |
| 4051 | from = getname(oldname); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4052 | if (IS_ERR(from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | return PTR_ERR(from); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4054 | retry: |
| 4055 | dentry = user_path_create(newdfd, newname, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4056 | error = PTR_ERR(dentry); |
| 4057 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4058 | goto out_putname; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4059 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4060 | error = security_path_symlink(&path, dentry, from->name); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 4061 | if (!error) |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4062 | error = vfs_symlink(path.dentry->d_inode, dentry, from->name); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4063 | done_path_create(&path, dentry); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4064 | if (retry_estale(error, lookup_flags)) { |
| 4065 | lookup_flags |= LOOKUP_REVAL; |
| 4066 | goto retry; |
| 4067 | } |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4068 | out_putname: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | putname(from); |
| 4070 | return error; |
| 4071 | } |
| 4072 | |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4073 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
| 4074 | int, newdfd, const char __user *, newname) |
| 4075 | { |
| 4076 | return do_symlinkat(oldname, newdfd, newname); |
| 4077 | } |
| 4078 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4079 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4080 | { |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4081 | return do_symlinkat(oldname, AT_FDCWD, newname); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4082 | } |
| 4083 | |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4084 | /** |
| 4085 | * vfs_link - create a new link |
| 4086 | * @old_dentry: object to be linked |
| 4087 | * @dir: new parent |
| 4088 | * @new_dentry: where to create the new link |
| 4089 | * @delegated_inode: returns inode needing a delegation break |
| 4090 | * |
| 4091 | * The caller must hold dir->i_mutex |
| 4092 | * |
| 4093 | * If vfs_link discovers a delegation on the to-be-linked file in need |
| 4094 | * of breaking, it will return -EWOULDBLOCK and return a reference to the |
| 4095 | * inode in delegated_inode. The caller should then break the delegation |
| 4096 | * and retry. Because breaking a delegation may take a long time, the |
| 4097 | * caller should drop the i_mutex before doing so. |
| 4098 | * |
| 4099 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4100 | * be appropriate for callers that expect the underlying filesystem not |
| 4101 | * to be NFS exported. |
| 4102 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4103 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | { |
| 4105 | struct inode *inode = old_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4106 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | int error; |
| 4108 | |
| 4109 | if (!inode) |
| 4110 | return -ENOENT; |
| 4111 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4112 | error = may_create(dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | if (error) |
| 4114 | return error; |
| 4115 | |
| 4116 | if (dir->i_sb != inode->i_sb) |
| 4117 | return -EXDEV; |
| 4118 | |
| 4119 | /* |
| 4120 | * A link to an append-only or immutable file cannot be created. |
| 4121 | */ |
| 4122 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4123 | return -EPERM; |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 4124 | /* |
| 4125 | * Updating the link count will likely cause i_uid and i_gid to |
| 4126 | * be writen back improperly if their true value is unknown to |
| 4127 | * the vfs. |
| 4128 | */ |
| 4129 | if (HAS_UNMAPPED_ID(inode)) |
| 4130 | return -EPERM; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4131 | if (!dir->i_op->link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | return -EPERM; |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4133 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | return -EPERM; |
| 4135 | |
| 4136 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 4137 | if (error) |
| 4138 | return error; |
| 4139 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4140 | inode_lock(inode); |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4141 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4142 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4143 | error = -ENOENT; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4144 | else if (max_links && inode->i_nlink >= max_links) |
| 4145 | error = -EMLINK; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4146 | else { |
| 4147 | error = try_break_deleg(inode, delegated_inode); |
| 4148 | if (!error) |
| 4149 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
| 4150 | } |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4151 | |
| 4152 | if (!error && (inode->i_state & I_LINKABLE)) { |
| 4153 | spin_lock(&inode->i_lock); |
| 4154 | inode->i_state &= ~I_LINKABLE; |
| 4155 | spin_unlock(&inode->i_lock); |
| 4156 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4157 | inode_unlock(inode); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 4158 | if (!error) |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4159 | fsnotify_link(dir, inode, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | return error; |
| 4161 | } |
Greg Kroah-Hartman | d483eed | 2020-07-02 12:51:03 +0200 | [diff] [blame] | 4162 | EXPORT_SYMBOL_NS(vfs_link, ANDROID_GKI_VFS_EXPORT_ONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | |
| 4164 | /* |
| 4165 | * Hardlinks are often used in delicate situations. We avoid |
| 4166 | * security-related surprises by not following symlinks on the |
| 4167 | * newname. --KAB |
| 4168 | * |
| 4169 | * We don't follow them on the oldname either to be compatible |
| 4170 | * with linux 2.0, and to avoid hard-linking to directories |
| 4171 | * and other special files. --ADM |
| 4172 | */ |
Christoph Hellwig | 812931d | 2020-07-22 11:14:19 +0200 | [diff] [blame] | 4173 | static int do_linkat(int olddfd, const char __user *oldname, int newdfd, |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4174 | const char __user *newname, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | { |
| 4176 | struct dentry *new_dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4177 | struct path old_path, new_path; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4178 | struct inode *delegated_inode = NULL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4179 | int how = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4182 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4183 | return -EINVAL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4184 | /* |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4185 | * To use null names we require CAP_DAC_READ_SEARCH |
| 4186 | * This ensures that not everyone will be able to create |
| 4187 | * handlink using the passed filedescriptor. |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4188 | */ |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4189 | if (flags & AT_EMPTY_PATH) { |
| 4190 | if (!capable(CAP_DAC_READ_SEARCH)) |
| 4191 | return -ENOENT; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4192 | how = LOOKUP_EMPTY; |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4193 | } |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4194 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4195 | if (flags & AT_SYMLINK_FOLLOW) |
| 4196 | how |= LOOKUP_FOLLOW; |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4197 | retry: |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4198 | error = user_path_at(olddfd, oldname, how, &old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | if (error) |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4200 | return error; |
| 4201 | |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4202 | new_dentry = user_path_create(newdfd, newname, &new_path, |
| 4203 | (how & LOOKUP_REVAL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4205 | if (IS_ERR(new_dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4206 | goto out; |
| 4207 | |
| 4208 | error = -EXDEV; |
| 4209 | if (old_path.mnt != new_path.mnt) |
| 4210 | goto out_dput; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 4211 | error = may_linkat(&old_path); |
| 4212 | if (unlikely(error)) |
| 4213 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4214 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4215 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 4216 | goto out_dput; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4217 | error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 4218 | out_dput: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4219 | done_path_create(&new_path, new_dentry); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4220 | if (delegated_inode) { |
| 4221 | error = break_deleg_wait(&delegated_inode); |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4222 | if (!error) { |
| 4223 | path_put(&old_path); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4224 | goto retry; |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4225 | } |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4226 | } |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4227 | if (retry_estale(error, how)) { |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4228 | path_put(&old_path); |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4229 | how |= LOOKUP_REVAL; |
| 4230 | goto retry; |
| 4231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 4233 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | |
| 4235 | return error; |
| 4236 | } |
| 4237 | |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4238 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
| 4239 | int, newdfd, const char __user *, newname, int, flags) |
| 4240 | { |
| 4241 | return do_linkat(olddfd, oldname, newdfd, newname, flags); |
| 4242 | } |
| 4243 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4244 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4245 | { |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4246 | return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4247 | } |
| 4248 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4249 | /** |
| 4250 | * vfs_rename - rename a filesystem object |
| 4251 | * @old_dir: parent of source |
| 4252 | * @old_dentry: source |
| 4253 | * @new_dir: parent of destination |
| 4254 | * @new_dentry: destination |
| 4255 | * @delegated_inode: returns an inode needing a delegation break |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4256 | * @flags: rename flags |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4257 | * |
| 4258 | * The caller must hold multiple mutexes--see lock_rename()). |
| 4259 | * |
| 4260 | * If vfs_rename discovers a delegation in need of breaking at either |
| 4261 | * the source or destination, it will return -EWOULDBLOCK and return a |
| 4262 | * reference to the inode in delegated_inode. The caller should then |
| 4263 | * break the delegation and retry. Because breaking a delegation may |
| 4264 | * take a long time, the caller should drop all locks before doing |
| 4265 | * so. |
| 4266 | * |
| 4267 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4268 | * be appropriate for callers that expect the underlying filesystem not |
| 4269 | * to be NFS exported. |
| 4270 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 4272 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 4273 | * Problems: |
Mauro Carvalho Chehab | 0117d42 | 2017-05-12 07:45:42 -0300 | [diff] [blame] | 4274 | * |
J. Bruce Fields | d03b29a | 2014-02-17 16:52:33 -0500 | [diff] [blame] | 4275 | * a) we can get into loop creation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | * b) race potential - two innocent renames can create a loop together. |
| 4277 | * 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] | 4278 | * 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] | 4279 | * story. |
J. Bruce Fields | 6cedba8 | 2012-03-05 11:40:41 -0500 | [diff] [blame] | 4280 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
| 4281 | * and source (if it is not a directory). |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4282 | * 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] | 4283 | * whether the target exists). Solution: try to be smart with locking |
| 4284 | * 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] | 4285 | * 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] | 4286 | * move will be locked. Thus we can rank directories by the tree |
| 4287 | * (ancestors first) and rank all non-directories after them. |
| 4288 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4289 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 4291 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 4292 | * 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] | 4293 | * 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] | 4294 | * 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] | 4295 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 4296 | * ->i_mutex on parents, which works but leads to some truly excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | * locking]. |
| 4298 | */ |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4299 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4300 | struct inode *new_dir, struct dentry *new_dentry, |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4301 | struct inode **delegated_inode, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | { |
| 4303 | int error; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4304 | bool is_dir = d_is_dir(old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4305 | struct inode *source = old_dentry->d_inode; |
| 4306 | struct inode *target = new_dentry->d_inode; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4307 | bool new_is_dir = false; |
| 4308 | unsigned max_links = new_dir->i_sb->s_max_links; |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4309 | struct name_snapshot old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | |
Miklos Szeredi | 8d3e293 | 2016-12-16 11:02:54 +0100 | [diff] [blame] | 4311 | if (source == target) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4312 | return 0; |
| 4313 | |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4314 | error = may_delete(old_dir, old_dentry, is_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | if (error) |
| 4316 | return error; |
| 4317 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4318 | if (!target) { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4319 | error = may_create(new_dir, new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4320 | } else { |
| 4321 | new_is_dir = d_is_dir(new_dentry); |
| 4322 | |
| 4323 | if (!(flags & RENAME_EXCHANGE)) |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4324 | error = may_delete(new_dir, new_dentry, is_dir); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4325 | else |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4326 | error = may_delete(new_dir, new_dentry, new_is_dir); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | if (error) |
| 4329 | return error; |
| 4330 | |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4331 | if (!old_dir->i_op->rename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | return -EPERM; |
| 4333 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4334 | /* |
| 4335 | * If we are going to change the parent - check write permissions, |
| 4336 | * we'll need to flip '..'. |
| 4337 | */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4338 | if (new_dir != old_dir) { |
| 4339 | if (is_dir) { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4340 | error = inode_permission(source, MAY_WRITE); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4341 | if (error) |
| 4342 | return error; |
| 4343 | } |
| 4344 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4345 | error = inode_permission(target, MAY_WRITE); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4346 | if (error) |
| 4347 | return error; |
| 4348 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4349 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4350 | |
Miklos Szeredi | 0b3974e | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4351 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 4352 | flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4353 | if (error) |
| 4354 | return error; |
| 4355 | |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4356 | take_dentry_name_snapshot(&old_name, old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4357 | dget(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4358 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4359 | lock_two_nondirectories(source, target); |
| 4360 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4361 | inode_lock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4362 | |
| 4363 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 4364 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4365 | goto out; |
| 4366 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4367 | if (max_links && new_dir != old_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4368 | error = -EMLINK; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4369 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4370 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4371 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
| 4372 | old_dir->i_nlink >= max_links) |
| 4373 | goto out; |
| 4374 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4375 | if (!is_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4376 | error = try_break_deleg(source, delegated_inode); |
| 4377 | if (error) |
| 4378 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4379 | } |
| 4380 | if (target && !new_is_dir) { |
| 4381 | error = try_break_deleg(target, delegated_inode); |
| 4382 | if (error) |
| 4383 | goto out; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4384 | } |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4385 | error = old_dir->i_op->rename(old_dir, old_dentry, |
Miklos Szeredi | 18fc84d | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4386 | new_dir, new_dentry, flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4387 | if (error) |
| 4388 | goto out; |
| 4389 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4390 | if (!(flags & RENAME_EXCHANGE) && target) { |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4391 | if (is_dir) { |
| 4392 | shrink_dcache_parent(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4393 | target->i_flags |= S_DEAD; |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4394 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4395 | dont_mount(new_dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4396 | detach_mounts(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4397 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4398 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
| 4399 | if (!(flags & RENAME_EXCHANGE)) |
| 4400 | d_move(old_dentry, new_dentry); |
| 4401 | else |
| 4402 | d_exchange(old_dentry, new_dentry); |
| 4403 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4404 | out: |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4405 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4406 | unlock_two_nondirectories(source, target); |
| 4407 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4408 | inode_unlock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4409 | dput(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4410 | if (!error) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4411 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4412 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
| 4413 | if (flags & RENAME_EXCHANGE) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4414 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4415 | new_is_dir, NULL, new_dentry); |
| 4416 | } |
| 4417 | } |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4418 | release_dentry_name_snapshot(&old_name); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | return error; |
| 4421 | } |
Greg Kroah-Hartman | d483eed | 2020-07-02 12:51:03 +0200 | [diff] [blame] | 4422 | EXPORT_SYMBOL_NS(vfs_rename, ANDROID_GKI_VFS_EXPORT_ONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4424 | static int do_renameat2(int olddfd, const char __user *oldname, int newdfd, |
| 4425 | const char __user *newname, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4427 | struct dentry *old_dentry, *new_dentry; |
| 4428 | struct dentry *trap; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4429 | struct path old_path, new_path; |
| 4430 | struct qstr old_last, new_last; |
| 4431 | int old_type, new_type; |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4432 | struct inode *delegated_inode = NULL; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4433 | struct filename *from; |
| 4434 | struct filename *to; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4435 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4436 | bool should_retry = false; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4437 | int error; |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4438 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4439 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4440 | return -EINVAL; |
| 4441 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4442 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
| 4443 | (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4444 | return -EINVAL; |
| 4445 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4446 | if (flags & RENAME_EXCHANGE) |
| 4447 | target_flags = 0; |
| 4448 | |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4449 | retry: |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 4450 | from = filename_parentat(olddfd, getname(oldname), lookup_flags, |
| 4451 | &old_path, &old_last, &old_type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4452 | if (IS_ERR(from)) { |
| 4453 | error = PTR_ERR(from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | goto exit; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 4457 | to = filename_parentat(newdfd, getname(newname), lookup_flags, |
| 4458 | &new_path, &new_last, &new_type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4459 | if (IS_ERR(to)) { |
| 4460 | error = PTR_ERR(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | goto exit1; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | |
| 4464 | error = -EXDEV; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4465 | if (old_path.mnt != new_path.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | goto exit2; |
| 4467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | error = -EBUSY; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4469 | if (old_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | goto exit2; |
| 4471 | |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4472 | if (flags & RENAME_NOREPLACE) |
| 4473 | error = -EEXIST; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4474 | if (new_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | goto exit2; |
| 4476 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4477 | error = mnt_want_write(old_path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4478 | if (error) |
| 4479 | goto exit2; |
| 4480 | |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4481 | retry_deleg: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4482 | trap = lock_rename(new_path.dentry, old_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4484 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | error = PTR_ERR(old_dentry); |
| 4486 | if (IS_ERR(old_dentry)) |
| 4487 | goto exit3; |
| 4488 | /* source must exist */ |
| 4489 | error = -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4490 | if (d_is_negative(old_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | goto exit4; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4492 | 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] | 4493 | error = PTR_ERR(new_dentry); |
| 4494 | if (IS_ERR(new_dentry)) |
| 4495 | goto exit4; |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4496 | error = -EEXIST; |
| 4497 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) |
| 4498 | goto exit5; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4499 | if (flags & RENAME_EXCHANGE) { |
| 4500 | error = -ENOENT; |
| 4501 | if (d_is_negative(new_dentry)) |
| 4502 | goto exit5; |
| 4503 | |
| 4504 | if (!d_is_dir(new_dentry)) { |
| 4505 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4506 | if (new_last.name[new_last.len]) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4507 | goto exit5; |
| 4508 | } |
| 4509 | } |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4510 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 4511 | if (!d_is_dir(old_dentry)) { |
| 4512 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4513 | if (old_last.name[old_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4514 | goto exit5; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4515 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4516 | goto exit5; |
| 4517 | } |
| 4518 | /* source should not be ancestor of target */ |
| 4519 | error = -EINVAL; |
| 4520 | if (old_dentry == trap) |
| 4521 | goto exit5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | /* target should not be an ancestor of source */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4523 | if (!(flags & RENAME_EXCHANGE)) |
| 4524 | error = -ENOTEMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | if (new_dentry == trap) |
| 4526 | goto exit5; |
| 4527 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4528 | error = security_path_rename(&old_path, old_dentry, |
| 4529 | &new_path, new_dentry, flags); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4530 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4531 | goto exit5; |
Greg Kroah-Hartman | c3271fe | 2020-05-28 16:44:45 +0200 | [diff] [blame] | 4532 | error = vfs_rename(old_path.dentry->d_inode, old_dentry, |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4533 | new_path.dentry->d_inode, new_dentry, |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4534 | &delegated_inode, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | exit5: |
| 4536 | dput(new_dentry); |
| 4537 | exit4: |
| 4538 | dput(old_dentry); |
| 4539 | exit3: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4540 | unlock_rename(new_path.dentry, old_path.dentry); |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4541 | if (delegated_inode) { |
| 4542 | error = break_deleg_wait(&delegated_inode); |
| 4543 | if (!error) |
| 4544 | goto retry_deleg; |
| 4545 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4546 | mnt_drop_write(old_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | exit2: |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4548 | if (retry_estale(error, lookup_flags)) |
| 4549 | should_retry = true; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4550 | path_put(&new_path); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4551 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4553 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | putname(from); |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4555 | if (should_retry) { |
| 4556 | should_retry = false; |
| 4557 | lookup_flags |= LOOKUP_REVAL; |
| 4558 | goto retry; |
| 4559 | } |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4560 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | return error; |
| 4562 | } |
| 4563 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4564 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
| 4565 | int, newdfd, const char __user *, newname, unsigned int, flags) |
| 4566 | { |
| 4567 | return do_renameat2(olddfd, oldname, newdfd, newname, flags); |
| 4568 | } |
| 4569 | |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4570 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
| 4571 | int, newdfd, const char __user *, newname) |
| 4572 | { |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4573 | return do_renameat2(olddfd, oldname, newdfd, newname, 0); |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4574 | } |
| 4575 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 4576 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4577 | { |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4578 | return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4579 | } |
| 4580 | |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4581 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | { |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4583 | int len = PTR_ERR(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | if (IS_ERR(link)) |
| 4585 | goto out; |
| 4586 | |
| 4587 | len = strlen(link); |
| 4588 | if (len > (unsigned) buflen) |
| 4589 | len = buflen; |
| 4590 | if (copy_to_user(buffer, link, len)) |
| 4591 | len = -EFAULT; |
| 4592 | out: |
| 4593 | return len; |
| 4594 | } |
| 4595 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4596 | /** |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4597 | * vfs_readlink - copy symlink body into userspace buffer |
| 4598 | * @dentry: dentry on which to get symbolic link |
| 4599 | * @buffer: user memory pointer |
| 4600 | * @buflen: size of buffer |
| 4601 | * |
| 4602 | * Does not touch atime. That's up to the caller if necessary |
| 4603 | * |
| 4604 | * Does not call security hook. |
| 4605 | */ |
| 4606 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4607 | { |
| 4608 | struct inode *inode = d_inode(dentry); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4609 | DEFINE_DELAYED_CALL(done); |
| 4610 | const char *link; |
| 4611 | int res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4612 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4613 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
| 4614 | if (unlikely(inode->i_op->readlink)) |
| 4615 | return inode->i_op->readlink(dentry, buffer, buflen); |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4616 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4617 | if (!d_is_symlink(dentry)) |
| 4618 | return -EINVAL; |
| 4619 | |
| 4620 | spin_lock(&inode->i_lock); |
| 4621 | inode->i_opflags |= IOP_DEFAULT_READLINK; |
| 4622 | spin_unlock(&inode->i_lock); |
| 4623 | } |
| 4624 | |
Eric Biggers | 4c4f7c1 | 2019-04-10 13:21:14 -0700 | [diff] [blame] | 4625 | link = READ_ONCE(inode->i_link); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4626 | if (!link) { |
| 4627 | link = inode->i_op->get_link(dentry, inode, &done); |
| 4628 | if (IS_ERR(link)) |
| 4629 | return PTR_ERR(link); |
| 4630 | } |
| 4631 | res = readlink_copy(buffer, buflen, link); |
| 4632 | do_delayed_call(&done); |
| 4633 | return res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4634 | } |
| 4635 | EXPORT_SYMBOL(vfs_readlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4637 | /** |
| 4638 | * vfs_get_link - get symlink body |
| 4639 | * @dentry: dentry on which to get symbolic link |
| 4640 | * @done: caller needs to free returned data with this |
| 4641 | * |
| 4642 | * Calls security hook and i_op->get_link() on the supplied inode. |
| 4643 | * |
| 4644 | * It does not touch atime. That's up to the caller if necessary. |
| 4645 | * |
| 4646 | * Does not work on "special" symlinks like /proc/$$/fd/N |
| 4647 | */ |
| 4648 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) |
| 4649 | { |
| 4650 | const char *res = ERR_PTR(-EINVAL); |
| 4651 | struct inode *inode = d_inode(dentry); |
| 4652 | |
| 4653 | if (d_is_symlink(dentry)) { |
| 4654 | res = ERR_PTR(security_inode_readlink(dentry)); |
| 4655 | if (!res) |
| 4656 | res = inode->i_op->get_link(dentry, inode, done); |
| 4657 | } |
| 4658 | return res; |
| 4659 | } |
| 4660 | EXPORT_SYMBOL(vfs_get_link); |
| 4661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | /* get the link contents into pagecache */ |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4663 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4664 | struct delayed_call *callback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | { |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4666 | char *kaddr; |
| 4667 | struct page *page; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4668 | struct address_space *mapping = inode->i_mapping; |
| 4669 | |
Al Viro | d3883d4 | 2015-11-17 10:41:04 -0500 | [diff] [blame] | 4670 | if (!dentry) { |
| 4671 | page = find_get_page(mapping, 0); |
| 4672 | if (!page) |
| 4673 | return ERR_PTR(-ECHILD); |
| 4674 | if (!PageUptodate(page)) { |
| 4675 | put_page(page); |
| 4676 | return ERR_PTR(-ECHILD); |
| 4677 | } |
| 4678 | } else { |
| 4679 | page = read_mapping_page(mapping, 0, NULL); |
| 4680 | if (IS_ERR(page)) |
| 4681 | return (char*)page; |
| 4682 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4683 | set_delayed_call(callback, page_put_link, page); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4684 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
| 4685 | kaddr = page_address(page); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4686 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4687 | return kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | } |
| 4689 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4690 | EXPORT_SYMBOL(page_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4692 | void page_put_link(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4694 | put_page(arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4696 | EXPORT_SYMBOL(page_put_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4698 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4699 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4700 | DEFINE_DELAYED_CALL(done); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4701 | int res = readlink_copy(buffer, buflen, |
| 4702 | page_get_link(dentry, d_inode(dentry), |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4703 | &done)); |
| 4704 | do_delayed_call(&done); |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4705 | return res; |
| 4706 | } |
| 4707 | EXPORT_SYMBOL(page_readlink); |
| 4708 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4709 | /* |
| 4710 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS |
| 4711 | */ |
| 4712 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | { |
| 4714 | struct address_space *mapping = inode->i_mapping; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4715 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4716 | void *fsdata; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 4717 | int err; |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 4718 | unsigned int flags = 0; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4719 | if (nofs) |
| 4720 | flags |= AOP_FLAG_NOFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 4722 | retry: |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4723 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4724 | flags, &page, &fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | if (err) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4726 | goto fail; |
| 4727 | |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4728 | memcpy(page_address(page), symname, len-1); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4729 | |
| 4730 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, |
| 4731 | page, fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | if (err < 0) |
| 4733 | goto fail; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4734 | if (err < len-1) |
| 4735 | goto retry; |
| 4736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | mark_inode_dirty(inode); |
| 4738 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | fail: |
| 4740 | return err; |
| 4741 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4742 | EXPORT_SYMBOL(__page_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4744 | int page_symlink(struct inode *inode, const char *symname, int len) |
| 4745 | { |
| 4746 | return __page_symlink(inode, symname, len, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 4747 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4748 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4749 | EXPORT_SYMBOL(page_symlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4750 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 4751 | const struct inode_operations page_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4752 | .get_link = page_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | EXPORT_SYMBOL(page_symlink_inode_operations); |