]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
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> | |
630d9c47 | 19 | #include <linux/export.h> |
44696908 | 20 | #include <linux/kernel.h> |
1da177e4 LT |
21 | #include <linux/slab.h> |
22 | #include <linux/fs.h> | |
23 | #include <linux/namei.h> | |
1da177e4 | 24 | #include <linux/pagemap.h> |
0eeca283 | 25 | #include <linux/fsnotify.h> |
1da177e4 LT |
26 | #include <linux/personality.h> |
27 | #include <linux/security.h> | |
6146f0d5 | 28 | #include <linux/ima.h> |
1da177e4 LT |
29 | #include <linux/syscalls.h> |
30 | #include <linux/mount.h> | |
31 | #include <linux/audit.h> | |
16f7e0fe | 32 | #include <linux/capability.h> |
834f2a4a | 33 | #include <linux/file.h> |
5590ff0d | 34 | #include <linux/fcntl.h> |
08ce5f16 | 35 | #include <linux/device_cgroup.h> |
5ad4e53b | 36 | #include <linux/fs_struct.h> |
e77819e5 | 37 | #include <linux/posix_acl.h> |
99d263d4 | 38 | #include <linux/hash.h> |
2a18da7a | 39 | #include <linux/bitops.h> |
aeaa4a79 | 40 | #include <linux/init_task.h> |
7c0f6ba6 | 41 | #include <linux/uaccess.h> |
1da177e4 | 42 | |
e81e3f4d | 43 | #include "internal.h" |
c7105365 | 44 | #include "mount.h" |
e81e3f4d | 45 | |
1da177e4 LT |
46 | /* [Feb-1997 T. Schoebel-Theuer] |
47 | * Fundamental changes in the pathname lookup mechanisms (namei) | |
48 | * were necessary because of omirr. The reason is that omirr needs | |
49 | * to know the _real_ pathname, not the user-supplied one, in case | |
50 | * of symlinks (and also when transname replacements occur). | |
51 | * | |
52 | * The new code replaces the old recursive symlink resolution with | |
53 | * an iterative one (in case of non-nested symlink chains). It does | |
54 | * this with calls to <fs>_follow_link(). | |
55 | * As a side effect, dir_namei(), _namei() and follow_link() are now | |
56 | * replaced with a single function lookup_dentry() that can handle all | |
57 | * the special cases of the former code. | |
58 | * | |
59 | * With the new dcache, the pathname is stored at each inode, at least as | |
60 | * long as the refcount of the inode is positive. As a side effect, the | |
61 | * size of the dcache depends on the inode cache and thus is dynamic. | |
62 | * | |
63 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink | |
64 | * resolution to correspond with current state of the code. | |
65 | * | |
66 | * Note that the symlink resolution is not *completely* iterative. | |
67 | * There is still a significant amount of tail- and mid- recursion in | |
68 | * the algorithm. Also, note that <fs>_readlink() is not used in | |
69 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() | |
70 | * may return different results than <fs>_follow_link(). Many virtual | |
71 | * filesystems (including /proc) exhibit this behavior. | |
72 | */ | |
73 | ||
74 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: | |
75 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL | |
76 | * and the name already exists in form of a symlink, try to create the new | |
77 | * name indicated by the symlink. The old code always complained that the | |
78 | * name already exists, due to not following the symlink even if its target | |
79 | * is nonexistent. The new semantics affects also mknod() and link() when | |
25985edc | 80 | * the name is a symlink pointing to a non-existent name. |
1da177e4 LT |
81 | * |
82 | * I don't know which semantics is the right one, since I have no access | |
83 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" | |
84 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the | |
85 | * "old" one. Personally, I think the new semantics is much more logical. | |
86 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing | |
87 | * file does succeed in both HP-UX and SunOs, but not in Solaris | |
88 | * and in the old Linux semantics. | |
89 | */ | |
90 | ||
91 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink | |
92 | * semantics. See the comments in "open_namei" and "do_link" below. | |
93 | * | |
94 | * [10-Sep-98 Alan Modra] Another symlink change. | |
95 | */ | |
96 | ||
97 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: | |
98 | * inside the path - always follow. | |
99 | * in the last component in creation/removal/renaming - never follow. | |
100 | * if LOOKUP_FOLLOW passed - follow. | |
101 | * if the pathname has trailing slashes - follow. | |
102 | * otherwise - don't follow. | |
103 | * (applied in that order). | |
104 | * | |
105 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT | |
106 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. | |
107 | * During the 2.4 we need to fix the userland stuff depending on it - | |
108 | * hopefully we will be able to get rid of that wart in 2.5. So far only | |
109 | * XEmacs seems to be relying on it... | |
110 | */ | |
111 | /* | |
112 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) | |
a11f3a05 | 113 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
1da177e4 LT |
114 | * any extra contention... |
115 | */ | |
116 | ||
117 | /* In order to reduce some races, while at the same time doing additional | |
118 | * checking and hopefully speeding things up, we copy filenames to the | |
119 | * kernel data space before using them.. | |
120 | * | |
121 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). | |
122 | * PATH_MAX includes the nul terminator --RR. | |
123 | */ | |
91a27b2a | 124 | |
fd2f7cb5 | 125 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
7950e385 | 126 | |
51f39a1f | 127 | struct filename * |
91a27b2a JL |
128 | getname_flags(const char __user *filename, int flags, int *empty) |
129 | { | |
94b5d262 | 130 | struct filename *result; |
7950e385 | 131 | char *kname; |
94b5d262 | 132 | int len; |
4043cde8 | 133 | |
7ac86265 JL |
134 | result = audit_reusename(filename); |
135 | if (result) | |
136 | return result; | |
137 | ||
7950e385 | 138 | result = __getname(); |
3f9f0aa6 | 139 | if (unlikely(!result)) |
4043cde8 EP |
140 | return ERR_PTR(-ENOMEM); |
141 | ||
7950e385 JL |
142 | /* |
143 | * First, try to embed the struct filename inside the names_cache | |
144 | * allocation | |
145 | */ | |
fd2f7cb5 | 146 | kname = (char *)result->iname; |
91a27b2a | 147 | result->name = kname; |
7950e385 | 148 | |
94b5d262 | 149 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
91a27b2a | 150 | if (unlikely(len < 0)) { |
94b5d262 AV |
151 | __putname(result); |
152 | return ERR_PTR(len); | |
91a27b2a | 153 | } |
3f9f0aa6 | 154 | |
7950e385 JL |
155 | /* |
156 | * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a | |
157 | * separate struct filename so we can dedicate the entire | |
158 | * names_cache allocation for the pathname, and re-do the copy from | |
159 | * userland. | |
160 | */ | |
94b5d262 | 161 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
fd2f7cb5 | 162 | const size_t size = offsetof(struct filename, iname[1]); |
7950e385 JL |
163 | kname = (char *)result; |
164 | ||
fd2f7cb5 AV |
165 | /* |
166 | * size is chosen that way we to guarantee that | |
167 | * result->iname[0] is within the same object and that | |
168 | * kname can't be equal to result->iname, no matter what. | |
169 | */ | |
170 | result = kzalloc(size, GFP_KERNEL); | |
94b5d262 AV |
171 | if (unlikely(!result)) { |
172 | __putname(kname); | |
173 | return ERR_PTR(-ENOMEM); | |
7950e385 JL |
174 | } |
175 | result->name = kname; | |
94b5d262 AV |
176 | len = strncpy_from_user(kname, filename, PATH_MAX); |
177 | if (unlikely(len < 0)) { | |
178 | __putname(kname); | |
179 | kfree(result); | |
180 | return ERR_PTR(len); | |
181 | } | |
182 | if (unlikely(len == PATH_MAX)) { | |
183 | __putname(kname); | |
184 | kfree(result); | |
185 | return ERR_PTR(-ENAMETOOLONG); | |
186 | } | |
7950e385 JL |
187 | } |
188 | ||
94b5d262 | 189 | result->refcnt = 1; |
3f9f0aa6 LT |
190 | /* The empty path is special. */ |
191 | if (unlikely(!len)) { | |
192 | if (empty) | |
4043cde8 | 193 | *empty = 1; |
94b5d262 AV |
194 | if (!(flags & LOOKUP_EMPTY)) { |
195 | putname(result); | |
196 | return ERR_PTR(-ENOENT); | |
197 | } | |
1da177e4 | 198 | } |
3f9f0aa6 | 199 | |
7950e385 | 200 | result->uptr = filename; |
c4ad8f98 | 201 | result->aname = NULL; |
7950e385 JL |
202 | audit_getname(result); |
203 | return result; | |
1da177e4 LT |
204 | } |
205 | ||
91a27b2a JL |
206 | struct filename * |
207 | getname(const char __user * filename) | |
f52e0c11 | 208 | { |
f7493e5d | 209 | return getname_flags(filename, 0, NULL); |
f52e0c11 AV |
210 | } |
211 | ||
c4ad8f98 LT |
212 | struct filename * |
213 | getname_kernel(const char * filename) | |
214 | { | |
215 | struct filename *result; | |
08518549 | 216 | int len = strlen(filename) + 1; |
c4ad8f98 LT |
217 | |
218 | result = __getname(); | |
219 | if (unlikely(!result)) | |
220 | return ERR_PTR(-ENOMEM); | |
221 | ||
08518549 | 222 | if (len <= EMBEDDED_NAME_MAX) { |
fd2f7cb5 | 223 | result->name = (char *)result->iname; |
08518549 | 224 | } else if (len <= PATH_MAX) { |
30ce4d19 | 225 | const size_t size = offsetof(struct filename, iname[1]); |
08518549 PM |
226 | struct filename *tmp; |
227 | ||
30ce4d19 | 228 | tmp = kmalloc(size, GFP_KERNEL); |
08518549 PM |
229 | if (unlikely(!tmp)) { |
230 | __putname(result); | |
231 | return ERR_PTR(-ENOMEM); | |
232 | } | |
233 | tmp->name = (char *)result; | |
08518549 PM |
234 | result = tmp; |
235 | } else { | |
236 | __putname(result); | |
237 | return ERR_PTR(-ENAMETOOLONG); | |
238 | } | |
239 | memcpy((char *)result->name, filename, len); | |
c4ad8f98 LT |
240 | result->uptr = NULL; |
241 | result->aname = NULL; | |
55422d0b | 242 | result->refcnt = 1; |
fd3522fd | 243 | audit_getname(result); |
c4ad8f98 | 244 | |
c4ad8f98 LT |
245 | return result; |
246 | } | |
247 | ||
91a27b2a | 248 | void putname(struct filename *name) |
1da177e4 | 249 | { |
91ef658f DK |
250 | if (IS_ERR_OR_NULL(name)) |
251 | return; | |
252 | ||
55422d0b PM |
253 | BUG_ON(name->refcnt <= 0); |
254 | ||
255 | if (--name->refcnt > 0) | |
256 | return; | |
257 | ||
fd2f7cb5 | 258 | if (name->name != name->iname) { |
55422d0b PM |
259 | __putname(name->name); |
260 | kfree(name); | |
261 | } else | |
262 | __putname(name); | |
1da177e4 | 263 | } |
1da177e4 | 264 | |
47291baa CB |
265 | /** |
266 | * check_acl - perform ACL permission checking | |
267 | * @mnt_userns: user namespace of the mount the inode was found from | |
268 | * @inode: inode to check permissions on | |
269 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) | |
270 | * | |
271 | * This function performs the ACL permission checking. Since this function | |
272 | * retrieve POSIX acls it needs to know whether it is called from a blocking or | |
273 | * non-blocking context and thus cares about the MAY_NOT_BLOCK bit. | |
274 | * | |
275 | * If the inode has been found through an idmapped mount the user namespace of | |
276 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
277 | * care to map the inode according to @mnt_userns before checking permissions. | |
278 | * On non-idmapped mounts or if permission checking is to be performed on the | |
279 | * raw inode simply passs init_user_ns. | |
280 | */ | |
281 | static int check_acl(struct user_namespace *mnt_userns, | |
282 | struct inode *inode, int mask) | |
e77819e5 | 283 | { |
84635d68 | 284 | #ifdef CONFIG_FS_POSIX_ACL |
e77819e5 LT |
285 | struct posix_acl *acl; |
286 | ||
e77819e5 | 287 | if (mask & MAY_NOT_BLOCK) { |
3567866b AV |
288 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
289 | if (!acl) | |
e77819e5 | 290 | return -EAGAIN; |
3567866b | 291 | /* no ->get_acl() calls in RCU mode... */ |
b8a7a3a6 | 292 | if (is_uncached_acl(acl)) |
3567866b | 293 | return -ECHILD; |
47291baa | 294 | return posix_acl_permission(mnt_userns, inode, acl, mask); |
e77819e5 LT |
295 | } |
296 | ||
2982baa2 CH |
297 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
298 | if (IS_ERR(acl)) | |
299 | return PTR_ERR(acl); | |
e77819e5 | 300 | if (acl) { |
47291baa | 301 | int error = posix_acl_permission(mnt_userns, inode, acl, mask); |
e77819e5 LT |
302 | posix_acl_release(acl); |
303 | return error; | |
304 | } | |
84635d68 | 305 | #endif |
e77819e5 LT |
306 | |
307 | return -EAGAIN; | |
308 | } | |
309 | ||
47291baa CB |
310 | /** |
311 | * acl_permission_check - perform basic UNIX permission checking | |
312 | * @mnt_userns: user namespace of the mount the inode was found from | |
313 | * @inode: inode to check permissions on | |
314 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) | |
315 | * | |
316 | * This function performs the basic UNIX permission checking. Since this | |
317 | * function may retrieve POSIX acls it needs to know whether it is called from a | |
318 | * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit. | |
5fc475b7 | 319 | * |
47291baa CB |
320 | * If the inode has been found through an idmapped mount the user namespace of |
321 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
322 | * care to map the inode according to @mnt_userns before checking permissions. | |
323 | * On non-idmapped mounts or if permission checking is to be performed on the | |
324 | * raw inode simply passs init_user_ns. | |
1da177e4 | 325 | */ |
47291baa CB |
326 | static int acl_permission_check(struct user_namespace *mnt_userns, |
327 | struct inode *inode, int mask) | |
1da177e4 | 328 | { |
26cf46be | 329 | unsigned int mode = inode->i_mode; |
47291baa | 330 | kuid_t i_uid; |
1da177e4 | 331 | |
5fc475b7 | 332 | /* Are we the owner? If so, ACL's don't matter */ |
47291baa CB |
333 | i_uid = i_uid_into_mnt(mnt_userns, inode); |
334 | if (likely(uid_eq(current_fsuid(), i_uid))) { | |
5fc475b7 | 335 | mask &= 7; |
1da177e4 | 336 | mode >>= 6; |
5fc475b7 LT |
337 | return (mask & ~mode) ? -EACCES : 0; |
338 | } | |
1da177e4 | 339 | |
5fc475b7 LT |
340 | /* Do we have ACL's? */ |
341 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { | |
47291baa | 342 | int error = check_acl(mnt_userns, inode, mask); |
5fc475b7 LT |
343 | if (error != -EAGAIN) |
344 | return error; | |
1da177e4 LT |
345 | } |
346 | ||
5fc475b7 LT |
347 | /* Only RWX matters for group/other mode bits */ |
348 | mask &= 7; | |
349 | ||
1da177e4 | 350 | /* |
5fc475b7 LT |
351 | * Are the group permissions different from |
352 | * the other permissions in the bits we care | |
353 | * about? Need to check group ownership if so. | |
1da177e4 | 354 | */ |
5fc475b7 | 355 | if (mask & (mode ^ (mode >> 3))) { |
47291baa CB |
356 | kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); |
357 | if (in_group_p(kgid)) | |
5fc475b7 LT |
358 | mode >>= 3; |
359 | } | |
360 | ||
361 | /* Bits in 'mode' clear that we require? */ | |
362 | return (mask & ~mode) ? -EACCES : 0; | |
5909ccaa LT |
363 | } |
364 | ||
365 | /** | |
b74c79e9 | 366 | * generic_permission - check for access rights on a Posix-like filesystem |
47291baa | 367 | * @mnt_userns: user namespace of the mount the inode was found from |
5909ccaa | 368 | * @inode: inode to check access rights for |
5fc475b7 LT |
369 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, |
370 | * %MAY_NOT_BLOCK ...) | |
5909ccaa LT |
371 | * |
372 | * Used to check for read/write/execute permissions on a file. | |
373 | * We use "fsuid" for this, letting us set arbitrary permissions | |
374 | * for filesystem access without changing the "normal" uids which | |
b74c79e9 NP |
375 | * are used for other things. |
376 | * | |
377 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk | |
378 | * request cannot be satisfied (eg. requires blocking or too much complexity). | |
379 | * It would then be called again in ref-walk mode. | |
47291baa CB |
380 | * |
381 | * If the inode has been found through an idmapped mount the user namespace of | |
382 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
383 | * care to map the inode according to @mnt_userns before checking permissions. | |
384 | * On non-idmapped mounts or if permission checking is to be performed on the | |
385 | * raw inode simply passs init_user_ns. | |
5909ccaa | 386 | */ |
47291baa CB |
387 | int generic_permission(struct user_namespace *mnt_userns, struct inode *inode, |
388 | int mask) | |
5909ccaa LT |
389 | { |
390 | int ret; | |
391 | ||
392 | /* | |
948409c7 | 393 | * Do the basic permission checks. |
5909ccaa | 394 | */ |
47291baa | 395 | ret = acl_permission_check(mnt_userns, inode, mask); |
5909ccaa LT |
396 | if (ret != -EACCES) |
397 | return ret; | |
1da177e4 | 398 | |
d594e7ec AV |
399 | if (S_ISDIR(inode->i_mode)) { |
400 | /* DACs are overridable for directories */ | |
d594e7ec | 401 | if (!(mask & MAY_WRITE)) |
47291baa | 402 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
23adbe12 | 403 | CAP_DAC_READ_SEARCH)) |
d594e7ec | 404 | return 0; |
47291baa | 405 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
0558c1bf | 406 | CAP_DAC_OVERRIDE)) |
1da177e4 | 407 | return 0; |
2a4c2242 SS |
408 | return -EACCES; |
409 | } | |
1da177e4 LT |
410 | |
411 | /* | |
412 | * Searching includes executable on directories, else just read. | |
413 | */ | |
7ea66001 | 414 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
d594e7ec | 415 | if (mask == MAY_READ) |
47291baa | 416 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
0558c1bf | 417 | CAP_DAC_READ_SEARCH)) |
1da177e4 | 418 | return 0; |
2a4c2242 SS |
419 | /* |
420 | * Read/write DACs are always overridable. | |
421 | * Executable DACs are overridable when there is | |
422 | * at least one exec bit set. | |
423 | */ | |
424 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) | |
47291baa | 425 | if (capable_wrt_inode_uidgid(mnt_userns, inode, |
0558c1bf | 426 | CAP_DAC_OVERRIDE)) |
2a4c2242 | 427 | return 0; |
1da177e4 LT |
428 | |
429 | return -EACCES; | |
430 | } | |
4d359507 | 431 | EXPORT_SYMBOL(generic_permission); |
1da177e4 | 432 | |
47291baa CB |
433 | /** |
434 | * do_inode_permission - UNIX permission checking | |
435 | * @mnt_userns: user namespace of the mount the inode was found from | |
436 | * @inode: inode to check permissions on | |
437 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) | |
438 | * | |
3ddcd056 LT |
439 | * We _really_ want to just do "generic_permission()" without |
440 | * even looking at the inode->i_op values. So we keep a cache | |
441 | * flag in inode->i_opflags, that says "this has not special | |
442 | * permission function, use the fast case". | |
443 | */ | |
47291baa CB |
444 | static inline int do_inode_permission(struct user_namespace *mnt_userns, |
445 | struct inode *inode, int mask) | |
3ddcd056 LT |
446 | { |
447 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { | |
448 | if (likely(inode->i_op->permission)) | |
549c7297 | 449 | return inode->i_op->permission(mnt_userns, inode, mask); |
3ddcd056 LT |
450 | |
451 | /* This gets set once for the inode lifetime */ | |
452 | spin_lock(&inode->i_lock); | |
453 | inode->i_opflags |= IOP_FASTPERM; | |
454 | spin_unlock(&inode->i_lock); | |
455 | } | |
47291baa | 456 | return generic_permission(mnt_userns, inode, mask); |
3ddcd056 LT |
457 | } |
458 | ||
0bdaea90 DH |
459 | /** |
460 | * sb_permission - Check superblock-level permissions | |
461 | * @sb: Superblock of inode to check permission on | |
55852635 | 462 | * @inode: Inode to check permission on |
0bdaea90 DH |
463 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
464 | * | |
465 | * Separate out file-system wide checks from inode-specific permission checks. | |
466 | */ | |
467 | static int sb_permission(struct super_block *sb, struct inode *inode, int mask) | |
468 | { | |
469 | if (unlikely(mask & MAY_WRITE)) { | |
470 | umode_t mode = inode->i_mode; | |
471 | ||
472 | /* Nobody gets write access to a read-only fs. */ | |
bc98a42c | 473 | if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
0bdaea90 DH |
474 | return -EROFS; |
475 | } | |
476 | return 0; | |
477 | } | |
478 | ||
479 | /** | |
480 | * inode_permission - Check for access rights to a given inode | |
47291baa CB |
481 | * @mnt_userns: User namespace of the mount the inode was found from |
482 | * @inode: Inode to check permission on | |
483 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
0bdaea90 DH |
484 | * |
485 | * Check for read/write/execute permissions on an inode. We use fs[ug]id for | |
486 | * this, letting us set arbitrary permissions for filesystem access without | |
487 | * changing the "normal" UIDs which are used for other things. | |
488 | * | |
489 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. | |
490 | */ | |
47291baa CB |
491 | int inode_permission(struct user_namespace *mnt_userns, |
492 | struct inode *inode, int mask) | |
0bdaea90 DH |
493 | { |
494 | int retval; | |
495 | ||
496 | retval = sb_permission(inode->i_sb, inode, mask); | |
497 | if (retval) | |
498 | return retval; | |
4bfd054a EB |
499 | |
500 | if (unlikely(mask & MAY_WRITE)) { | |
501 | /* | |
502 | * Nobody gets write access to an immutable file. | |
503 | */ | |
504 | if (IS_IMMUTABLE(inode)) | |
505 | return -EPERM; | |
506 | ||
507 | /* | |
508 | * Updating mtime will likely cause i_uid and i_gid to be | |
509 | * written back improperly if their true value is unknown | |
510 | * to the vfs. | |
511 | */ | |
ba73d987 | 512 | if (HAS_UNMAPPED_ID(mnt_userns, inode)) |
4bfd054a EB |
513 | return -EACCES; |
514 | } | |
515 | ||
47291baa | 516 | retval = do_inode_permission(mnt_userns, inode, mask); |
4bfd054a EB |
517 | if (retval) |
518 | return retval; | |
519 | ||
520 | retval = devcgroup_inode_permission(inode, mask); | |
521 | if (retval) | |
522 | return retval; | |
523 | ||
524 | return security_inode_permission(inode, mask); | |
0bdaea90 | 525 | } |
4d359507 | 526 | EXPORT_SYMBOL(inode_permission); |
0bdaea90 | 527 | |
5dd784d0 JB |
528 | /** |
529 | * path_get - get a reference to a path | |
530 | * @path: path to get the reference to | |
531 | * | |
532 | * Given a path increment the reference count to the dentry and the vfsmount. | |
533 | */ | |
dcf787f3 | 534 | void path_get(const struct path *path) |
5dd784d0 JB |
535 | { |
536 | mntget(path->mnt); | |
537 | dget(path->dentry); | |
538 | } | |
539 | EXPORT_SYMBOL(path_get); | |
540 | ||
1d957f9b JB |
541 | /** |
542 | * path_put - put a reference to a path | |
543 | * @path: path to put the reference to | |
544 | * | |
545 | * Given a path decrement the reference count to the dentry and the vfsmount. | |
546 | */ | |
dcf787f3 | 547 | void path_put(const struct path *path) |
1da177e4 | 548 | { |
1d957f9b JB |
549 | dput(path->dentry); |
550 | mntput(path->mnt); | |
1da177e4 | 551 | } |
1d957f9b | 552 | EXPORT_SYMBOL(path_put); |
1da177e4 | 553 | |
894bc8c4 | 554 | #define EMBEDDED_LEVELS 2 |
1f55a6ec AV |
555 | struct nameidata { |
556 | struct path path; | |
1cf2665b | 557 | struct qstr last; |
1f55a6ec AV |
558 | struct path root; |
559 | struct inode *inode; /* path.dentry.d_inode */ | |
bcba1e7d | 560 | unsigned int flags, state; |
ab87f9a5 | 561 | unsigned seq, m_seq, r_seq; |
1f55a6ec AV |
562 | int last_type; |
563 | unsigned depth; | |
756daf26 | 564 | int total_link_count; |
697fc6ca AV |
565 | struct saved { |
566 | struct path link; | |
fceef393 | 567 | struct delayed_call done; |
697fc6ca | 568 | const char *name; |
0450b2d1 | 569 | unsigned seq; |
894bc8c4 | 570 | } *stack, internal[EMBEDDED_LEVELS]; |
9883d185 AV |
571 | struct filename *name; |
572 | struct nameidata *saved; | |
573 | unsigned root_seq; | |
574 | int dfd; | |
0f705953 AV |
575 | kuid_t dir_uid; |
576 | umode_t dir_mode; | |
3859a271 | 577 | } __randomize_layout; |
1f55a6ec | 578 | |
bcba1e7d AV |
579 | #define ND_ROOT_PRESET 1 |
580 | #define ND_ROOT_GRABBED 2 | |
581 | #define ND_JUMPED 4 | |
582 | ||
06422964 | 583 | static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
894bc8c4 | 584 | { |
756daf26 N |
585 | struct nameidata *old = current->nameidata; |
586 | p->stack = p->internal; | |
7962c7d1 | 587 | p->depth = 0; |
c8a53ee5 AV |
588 | p->dfd = dfd; |
589 | p->name = name; | |
7d01ef75 AV |
590 | p->path.mnt = NULL; |
591 | p->path.dentry = NULL; | |
756daf26 | 592 | p->total_link_count = old ? old->total_link_count : 0; |
9883d185 | 593 | p->saved = old; |
756daf26 | 594 | current->nameidata = p; |
894bc8c4 AV |
595 | } |
596 | ||
06422964 AV |
597 | static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name, |
598 | const struct path *root) | |
599 | { | |
600 | __set_nameidata(p, dfd, name); | |
601 | p->state = 0; | |
602 | if (unlikely(root)) { | |
603 | p->state = ND_ROOT_PRESET; | |
604 | p->root = *root; | |
605 | } | |
606 | } | |
607 | ||
9883d185 | 608 | static void restore_nameidata(void) |
894bc8c4 | 609 | { |
9883d185 | 610 | struct nameidata *now = current->nameidata, *old = now->saved; |
756daf26 N |
611 | |
612 | current->nameidata = old; | |
613 | if (old) | |
614 | old->total_link_count = now->total_link_count; | |
e1a63bbc | 615 | if (now->stack != now->internal) |
756daf26 | 616 | kfree(now->stack); |
894bc8c4 AV |
617 | } |
618 | ||
60ef60c7 | 619 | static bool nd_alloc_stack(struct nameidata *nd) |
894bc8c4 | 620 | { |
bc40aee0 AV |
621 | struct saved *p; |
622 | ||
60ef60c7 AV |
623 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
624 | nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); | |
625 | if (unlikely(!p)) | |
626 | return false; | |
894bc8c4 AV |
627 | memcpy(p, nd->internal, sizeof(nd->internal)); |
628 | nd->stack = p; | |
60ef60c7 | 629 | return true; |
894bc8c4 AV |
630 | } |
631 | ||
397d425d | 632 | /** |
6b03f7ed | 633 | * path_connected - Verify that a dentry is below mnt.mnt_root |
397d425d EB |
634 | * |
635 | * Rename can sometimes move a file or directory outside of a bind | |
636 | * mount, path_connected allows those cases to be detected. | |
637 | */ | |
6b03f7ed | 638 | static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) |
397d425d | 639 | { |
95dd7758 | 640 | struct super_block *sb = mnt->mnt_sb; |
397d425d | 641 | |
402dd2cf CH |
642 | /* Bind mounts can have disconnected paths */ |
643 | if (mnt->mnt_root == sb->s_root) | |
397d425d EB |
644 | return true; |
645 | ||
6b03f7ed | 646 | return is_subdir(dentry, mnt->mnt_root); |
397d425d EB |
647 | } |
648 | ||
7973387a AV |
649 | static void drop_links(struct nameidata *nd) |
650 | { | |
651 | int i = nd->depth; | |
652 | while (i--) { | |
653 | struct saved *last = nd->stack + i; | |
fceef393 AV |
654 | do_delayed_call(&last->done); |
655 | clear_delayed_call(&last->done); | |
7973387a AV |
656 | } |
657 | } | |
658 | ||
659 | static void terminate_walk(struct nameidata *nd) | |
660 | { | |
661 | drop_links(nd); | |
662 | if (!(nd->flags & LOOKUP_RCU)) { | |
663 | int i; | |
664 | path_put(&nd->path); | |
665 | for (i = 0; i < nd->depth; i++) | |
666 | path_put(&nd->stack[i].link); | |
bcba1e7d | 667 | if (nd->state & ND_ROOT_GRABBED) { |
102b8af2 | 668 | path_put(&nd->root); |
bcba1e7d | 669 | nd->state &= ~ND_ROOT_GRABBED; |
102b8af2 | 670 | } |
7973387a AV |
671 | } else { |
672 | nd->flags &= ~LOOKUP_RCU; | |
7973387a AV |
673 | rcu_read_unlock(); |
674 | } | |
675 | nd->depth = 0; | |
7d01ef75 AV |
676 | nd->path.mnt = NULL; |
677 | nd->path.dentry = NULL; | |
7973387a AV |
678 | } |
679 | ||
680 | /* path_put is needed afterwards regardless of success or failure */ | |
2aa38470 | 681 | static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) |
7973387a | 682 | { |
2aa38470 | 683 | int res = __legitimize_mnt(path->mnt, mseq); |
7973387a AV |
684 | if (unlikely(res)) { |
685 | if (res > 0) | |
686 | path->mnt = NULL; | |
687 | path->dentry = NULL; | |
688 | return false; | |
689 | } | |
690 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { | |
691 | path->dentry = NULL; | |
692 | return false; | |
693 | } | |
694 | return !read_seqcount_retry(&path->dentry->d_seq, seq); | |
695 | } | |
696 | ||
2aa38470 AV |
697 | static inline bool legitimize_path(struct nameidata *nd, |
698 | struct path *path, unsigned seq) | |
699 | { | |
5bd73286 | 700 | return __legitimize_path(path, seq, nd->m_seq); |
2aa38470 AV |
701 | } |
702 | ||
7973387a AV |
703 | static bool legitimize_links(struct nameidata *nd) |
704 | { | |
705 | int i; | |
eacd9aa8 AV |
706 | if (unlikely(nd->flags & LOOKUP_CACHED)) { |
707 | drop_links(nd); | |
708 | nd->depth = 0; | |
709 | return false; | |
710 | } | |
7973387a AV |
711 | for (i = 0; i < nd->depth; i++) { |
712 | struct saved *last = nd->stack + i; | |
713 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { | |
714 | drop_links(nd); | |
715 | nd->depth = i + 1; | |
716 | return false; | |
717 | } | |
718 | } | |
719 | return true; | |
720 | } | |
721 | ||
ee594bff AV |
722 | static bool legitimize_root(struct nameidata *nd) |
723 | { | |
adb21d2b AS |
724 | /* |
725 | * For scoped-lookups (where nd->root has been zeroed), we need to | |
726 | * restart the whole lookup from scratch -- because set_root() is wrong | |
727 | * for these lookups (nd->dfd is the root, not the filesystem root). | |
728 | */ | |
729 | if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED)) | |
730 | return false; | |
731 | /* Nothing to do if nd->root is zero or is managed by the VFS user. */ | |
bcba1e7d | 732 | if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) |
ee594bff | 733 | return true; |
bcba1e7d | 734 | nd->state |= ND_ROOT_GRABBED; |
ee594bff AV |
735 | return legitimize_path(nd, &nd->root, nd->root_seq); |
736 | } | |
737 | ||
19660af7 | 738 | /* |
31e6b01f | 739 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
19660af7 AV |
740 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
741 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab | |
57e3715c | 742 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
19660af7 AV |
743 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
744 | * got stuck, so ref-walk may continue from there. If this is not successful | |
745 | * (eg. a seqcount has changed), then failure is returned and it's up to caller | |
746 | * to restart the path walk from the beginning in ref-walk mode. | |
31e6b01f | 747 | */ |
31e6b01f NP |
748 | |
749 | /** | |
e36cffed | 750 | * try_to_unlazy - try to switch to ref-walk mode. |
19660af7 | 751 | * @nd: nameidata pathwalk data |
e36cffed | 752 | * Returns: true on success, false on failure |
31e6b01f | 753 | * |
e36cffed | 754 | * try_to_unlazy attempts to legitimize the current nd->path and nd->root |
4675ac39 AV |
755 | * for ref-walk mode. |
756 | * Must be called from rcu-walk context. | |
e36cffed | 757 | * Nothing should touch nameidata between try_to_unlazy() failure and |
7973387a | 758 | * terminate_walk(). |
31e6b01f | 759 | */ |
e36cffed | 760 | static bool try_to_unlazy(struct nameidata *nd) |
31e6b01f | 761 | { |
31e6b01f NP |
762 | struct dentry *parent = nd->path.dentry; |
763 | ||
764 | BUG_ON(!(nd->flags & LOOKUP_RCU)); | |
e5c832d5 | 765 | |
4675ac39 AV |
766 | nd->flags &= ~LOOKUP_RCU; |
767 | if (unlikely(!legitimize_links(nd))) | |
4675ac39 | 768 | goto out1; |
84a2bd39 AV |
769 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
770 | goto out; | |
ee594bff AV |
771 | if (unlikely(!legitimize_root(nd))) |
772 | goto out; | |
4675ac39 AV |
773 | rcu_read_unlock(); |
774 | BUG_ON(nd->inode != parent->d_inode); | |
e36cffed | 775 | return true; |
4675ac39 | 776 | |
84a2bd39 | 777 | out1: |
4675ac39 AV |
778 | nd->path.mnt = NULL; |
779 | nd->path.dentry = NULL; | |
4675ac39 AV |
780 | out: |
781 | rcu_read_unlock(); | |
e36cffed | 782 | return false; |
4675ac39 AV |
783 | } |
784 | ||
785 | /** | |
ae66db45 | 786 | * try_to_unlazy_next - try to switch to ref-walk mode. |
4675ac39 | 787 | * @nd: nameidata pathwalk data |
ae66db45 AV |
788 | * @dentry: next dentry to step into |
789 | * @seq: seq number to check @dentry against | |
790 | * Returns: true on success, false on failure | |
4675ac39 | 791 | * |
ae66db45 AV |
792 | * Similar to to try_to_unlazy(), but here we have the next dentry already |
793 | * picked by rcu-walk and want to legitimize that in addition to the current | |
794 | * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context. | |
795 | * Nothing should touch nameidata between try_to_unlazy_next() failure and | |
4675ac39 AV |
796 | * terminate_walk(). |
797 | */ | |
ae66db45 | 798 | static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq) |
4675ac39 AV |
799 | { |
800 | BUG_ON(!(nd->flags & LOOKUP_RCU)); | |
801 | ||
e5c832d5 | 802 | nd->flags &= ~LOOKUP_RCU; |
7973387a AV |
803 | if (unlikely(!legitimize_links(nd))) |
804 | goto out2; | |
805 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) | |
806 | goto out2; | |
4675ac39 | 807 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
7973387a | 808 | goto out1; |
48a066e7 | 809 | |
15570086 | 810 | /* |
4675ac39 AV |
811 | * We need to move both the parent and the dentry from the RCU domain |
812 | * to be properly refcounted. And the sequence number in the dentry | |
813 | * validates *both* dentry counters, since we checked the sequence | |
814 | * number of the parent after we got the child sequence number. So we | |
815 | * know the parent must still be valid if the child sequence number is | |
15570086 | 816 | */ |
4675ac39 AV |
817 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
818 | goto out; | |
84a2bd39 AV |
819 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
820 | goto out_dput; | |
e5c832d5 LT |
821 | /* |
822 | * Sequence counts matched. Now make sure that the root is | |
823 | * still valid and get it if required. | |
824 | */ | |
84a2bd39 AV |
825 | if (unlikely(!legitimize_root(nd))) |
826 | goto out_dput; | |
8b61e74f | 827 | rcu_read_unlock(); |
ae66db45 | 828 | return true; |
19660af7 | 829 | |
7973387a AV |
830 | out2: |
831 | nd->path.mnt = NULL; | |
832 | out1: | |
833 | nd->path.dentry = NULL; | |
e5c832d5 | 834 | out: |
8b61e74f | 835 | rcu_read_unlock(); |
ae66db45 | 836 | return false; |
84a2bd39 AV |
837 | out_dput: |
838 | rcu_read_unlock(); | |
839 | dput(dentry); | |
ae66db45 | 840 | return false; |
31e6b01f NP |
841 | } |
842 | ||
4ce16ef3 | 843 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
34286d66 | 844 | { |
a89f8337 AV |
845 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
846 | return dentry->d_op->d_revalidate(dentry, flags); | |
847 | else | |
848 | return 1; | |
34286d66 NP |
849 | } |
850 | ||
9f1fafee AV |
851 | /** |
852 | * complete_walk - successful completion of path walk | |
853 | * @nd: pointer nameidata | |
39159de2 | 854 | * |
9f1fafee AV |
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. | |
39159de2 | 860 | */ |
9f1fafee | 861 | static int complete_walk(struct nameidata *nd) |
39159de2 | 862 | { |
16c2cd71 | 863 | struct dentry *dentry = nd->path.dentry; |
39159de2 | 864 | int status; |
39159de2 | 865 | |
9f1fafee | 866 | if (nd->flags & LOOKUP_RCU) { |
adb21d2b AS |
867 | /* |
868 | * We don't want to zero nd->root for scoped-lookups or | |
869 | * externally-managed nd->root. | |
870 | */ | |
bcba1e7d AV |
871 | if (!(nd->state & ND_ROOT_PRESET)) |
872 | if (!(nd->flags & LOOKUP_IS_SCOPED)) | |
873 | nd->root.mnt = NULL; | |
6c6ec2b0 | 874 | nd->flags &= ~LOOKUP_CACHED; |
e36cffed | 875 | if (!try_to_unlazy(nd)) |
9f1fafee | 876 | return -ECHILD; |
9f1fafee AV |
877 | } |
878 | ||
adb21d2b AS |
879 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
880 | /* | |
881 | * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't | |
882 | * ever step outside the root during lookup" and should already | |
883 | * be guaranteed by the rest of namei, we want to avoid a namei | |
884 | * BUG resulting in userspace being given a path that was not | |
885 | * scoped within the root at some point during the lookup. | |
886 | * | |
887 | * So, do a final sanity-check to make sure that in the | |
888 | * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) | |
889 | * we won't silently return an fd completely outside of the | |
890 | * requested root to userspace. | |
891 | * | |
892 | * Userspace could move the path outside the root after this | |
893 | * check, but as discussed elsewhere this is not a concern (the | |
894 | * resolved file was inside the root at some point). | |
895 | */ | |
896 | if (!path_is_under(&nd->path, &nd->root)) | |
897 | return -EXDEV; | |
898 | } | |
899 | ||
bcba1e7d | 900 | if (likely(!(nd->state & ND_JUMPED))) |
16c2cd71 AV |
901 | return 0; |
902 | ||
ecf3d1f1 | 903 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
39159de2 JL |
904 | return 0; |
905 | ||
ecf3d1f1 | 906 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
39159de2 JL |
907 | if (status > 0) |
908 | return 0; | |
909 | ||
16c2cd71 | 910 | if (!status) |
39159de2 | 911 | status = -ESTALE; |
16c2cd71 | 912 | |
39159de2 JL |
913 | return status; |
914 | } | |
915 | ||
740a1678 | 916 | static int set_root(struct nameidata *nd) |
31e6b01f | 917 | { |
7bd88377 | 918 | struct fs_struct *fs = current->fs; |
c28cc364 | 919 | |
adb21d2b AS |
920 | /* |
921 | * Jumping to the real root in a scoped-lookup is a BUG in namei, but we | |
922 | * still have to ensure it doesn't happen because it will cause a breakout | |
923 | * from the dirfd. | |
924 | */ | |
925 | if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) | |
926 | return -ENOTRECOVERABLE; | |
927 | ||
9e6697e2 AV |
928 | if (nd->flags & LOOKUP_RCU) { |
929 | unsigned seq; | |
930 | ||
931 | do { | |
932 | seq = read_seqcount_begin(&fs->seq); | |
933 | nd->root = fs->root; | |
934 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); | |
935 | } while (read_seqcount_retry(&fs->seq, seq)); | |
936 | } else { | |
937 | get_fs_root(fs, &nd->root); | |
bcba1e7d | 938 | nd->state |= ND_ROOT_GRABBED; |
9e6697e2 | 939 | } |
740a1678 | 940 | return 0; |
31e6b01f NP |
941 | } |
942 | ||
248fb5b9 AV |
943 | static int nd_jump_root(struct nameidata *nd) |
944 | { | |
adb21d2b AS |
945 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
946 | return -EXDEV; | |
72ba2929 AS |
947 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
948 | /* Absolute path arguments to path_init() are allowed. */ | |
949 | if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) | |
950 | return -EXDEV; | |
951 | } | |
740a1678 AS |
952 | if (!nd->root.mnt) { |
953 | int error = set_root(nd); | |
954 | if (error) | |
955 | return error; | |
956 | } | |
248fb5b9 AV |
957 | if (nd->flags & LOOKUP_RCU) { |
958 | struct dentry *d; | |
959 | nd->path = nd->root; | |
960 | d = nd->path.dentry; | |
961 | nd->inode = d->d_inode; | |
962 | nd->seq = nd->root_seq; | |
963 | if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) | |
964 | return -ECHILD; | |
965 | } else { | |
966 | path_put(&nd->path); | |
967 | nd->path = nd->root; | |
968 | path_get(&nd->path); | |
969 | nd->inode = nd->path.dentry->d_inode; | |
970 | } | |
bcba1e7d | 971 | nd->state |= ND_JUMPED; |
248fb5b9 AV |
972 | return 0; |
973 | } | |
974 | ||
b5fb63c1 | 975 | /* |
6b255391 | 976 | * Helper to directly jump to a known parsed path from ->get_link, |
b5fb63c1 CH |
977 | * caller must have taken a reference to path beforehand. |
978 | */ | |
1bc82070 | 979 | int nd_jump_link(struct path *path) |
b5fb63c1 | 980 | { |
4b99d499 | 981 | int error = -ELOOP; |
6e77137b | 982 | struct nameidata *nd = current->nameidata; |
b5fb63c1 | 983 | |
4b99d499 AS |
984 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
985 | goto err; | |
986 | ||
72ba2929 AS |
987 | error = -EXDEV; |
988 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { | |
989 | if (nd->path.mnt != path->mnt) | |
990 | goto err; | |
991 | } | |
adb21d2b AS |
992 | /* Not currently safe for scoped-lookups. */ |
993 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) | |
994 | goto err; | |
72ba2929 | 995 | |
4b99d499 | 996 | path_put(&nd->path); |
b5fb63c1 CH |
997 | nd->path = *path; |
998 | nd->inode = nd->path.dentry->d_inode; | |
bcba1e7d | 999 | nd->state |= ND_JUMPED; |
1bc82070 | 1000 | return 0; |
4b99d499 AS |
1001 | |
1002 | err: | |
1003 | path_put(path); | |
1004 | return error; | |
b5fb63c1 CH |
1005 | } |
1006 | ||
b9ff4429 | 1007 | static inline void put_link(struct nameidata *nd) |
574197e0 | 1008 | { |
21c3003d | 1009 | struct saved *last = nd->stack + --nd->depth; |
fceef393 | 1010 | do_delayed_call(&last->done); |
6548fae2 AV |
1011 | if (!(nd->flags & LOOKUP_RCU)) |
1012 | path_put(&last->link); | |
574197e0 AV |
1013 | } |
1014 | ||
561ec64a LT |
1015 | int sysctl_protected_symlinks __read_mostly = 0; |
1016 | int sysctl_protected_hardlinks __read_mostly = 0; | |
30aba665 SM |
1017 | int sysctl_protected_fifos __read_mostly; |
1018 | int sysctl_protected_regular __read_mostly; | |
800179c9 KC |
1019 | |
1020 | /** | |
1021 | * may_follow_link - Check symlink following for unsafe situations | |
55852635 | 1022 | * @nd: nameidata pathwalk data |
800179c9 KC |
1023 | * |
1024 | * In the case of the sysctl_protected_symlinks sysctl being enabled, | |
1025 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is | |
1026 | * in a sticky world-writable directory. This is to protect privileged | |
1027 | * processes from failing races against path names that may change out | |
1028 | * from under them by way of other users creating malicious symlinks. | |
1029 | * It will permit symlinks to be followed only when outside a sticky | |
1030 | * world-writable directory, or when the uid of the symlink and follower | |
1031 | * match, or when the directory owner matches the symlink's owner. | |
1032 | * | |
1033 | * Returns 0 if following the symlink is allowed, -ve on error. | |
1034 | */ | |
ad6cc4c3 | 1035 | static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) |
800179c9 | 1036 | { |
ba73d987 CB |
1037 | struct user_namespace *mnt_userns; |
1038 | kuid_t i_uid; | |
1039 | ||
800179c9 KC |
1040 | if (!sysctl_protected_symlinks) |
1041 | return 0; | |
1042 | ||
ba73d987 CB |
1043 | mnt_userns = mnt_user_ns(nd->path.mnt); |
1044 | i_uid = i_uid_into_mnt(mnt_userns, inode); | |
800179c9 | 1045 | /* Allowed if owner and follower match. */ |
ba73d987 | 1046 | if (uid_eq(current_cred()->fsuid, i_uid)) |
800179c9 KC |
1047 | return 0; |
1048 | ||
1049 | /* Allowed if parent directory not sticky and world-writable. */ | |
0f705953 | 1050 | if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
800179c9 KC |
1051 | return 0; |
1052 | ||
1053 | /* Allowed if parent directory and link owner match. */ | |
ba73d987 | 1054 | if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid)) |
800179c9 KC |
1055 | return 0; |
1056 | ||
31956502 AV |
1057 | if (nd->flags & LOOKUP_RCU) |
1058 | return -ECHILD; | |
1059 | ||
ea841baf | 1060 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
245d7369 | 1061 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
800179c9 KC |
1062 | return -EACCES; |
1063 | } | |
1064 | ||
1065 | /** | |
1066 | * safe_hardlink_source - Check for safe hardlink conditions | |
ba73d987 | 1067 | * @mnt_userns: user namespace of the mount the inode was found from |
800179c9 KC |
1068 | * @inode: the source inode to hardlink from |
1069 | * | |
1070 | * Return false if at least one of the following conditions: | |
1071 | * - inode is not a regular file | |
1072 | * - inode is setuid | |
1073 | * - inode is setgid and group-exec | |
1074 | * - access failure for read and write | |
1075 | * | |
1076 | * Otherwise returns true. | |
1077 | */ | |
ba73d987 CB |
1078 | static bool safe_hardlink_source(struct user_namespace *mnt_userns, |
1079 | struct inode *inode) | |
800179c9 KC |
1080 | { |
1081 | umode_t mode = inode->i_mode; | |
1082 | ||
1083 | /* Special files should not get pinned to the filesystem. */ | |
1084 | if (!S_ISREG(mode)) | |
1085 | return false; | |
1086 | ||
1087 | /* Setuid files should not get pinned to the filesystem. */ | |
1088 | if (mode & S_ISUID) | |
1089 | return false; | |
1090 | ||
1091 | /* Executable setgid files should not get pinned to the filesystem. */ | |
1092 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) | |
1093 | return false; | |
1094 | ||
1095 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ | |
ba73d987 | 1096 | if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE)) |
800179c9 KC |
1097 | return false; |
1098 | ||
1099 | return true; | |
1100 | } | |
1101 | ||
1102 | /** | |
1103 | * may_linkat - Check permissions for creating a hardlink | |
ba73d987 | 1104 | * @mnt_userns: user namespace of the mount the inode was found from |
800179c9 KC |
1105 | * @link: the source to hardlink from |
1106 | * | |
1107 | * Block hardlink when all of: | |
1108 | * - sysctl_protected_hardlinks enabled | |
1109 | * - fsuid does not match inode | |
1110 | * - hardlink source is unsafe (see safe_hardlink_source() above) | |
f2ca3796 | 1111 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
800179c9 | 1112 | * |
ba73d987 CB |
1113 | * If the inode has been found through an idmapped mount the user namespace of |
1114 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
1115 | * care to map the inode according to @mnt_userns before checking permissions. | |
1116 | * On non-idmapped mounts or if permission checking is to be performed on the | |
1117 | * raw inode simply passs init_user_ns. | |
1118 | * | |
800179c9 KC |
1119 | * Returns 0 if successful, -ve on error. |
1120 | */ | |
ba73d987 | 1121 | int may_linkat(struct user_namespace *mnt_userns, struct path *link) |
800179c9 | 1122 | { |
593d1ce8 EB |
1123 | struct inode *inode = link->dentry->d_inode; |
1124 | ||
1125 | /* Inode writeback is not safe when the uid or gid are invalid. */ | |
ba73d987 CB |
1126 | if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || |
1127 | !gid_valid(i_gid_into_mnt(mnt_userns, inode))) | |
593d1ce8 | 1128 | return -EOVERFLOW; |
800179c9 KC |
1129 | |
1130 | if (!sysctl_protected_hardlinks) | |
1131 | return 0; | |
1132 | ||
800179c9 KC |
1133 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
1134 | * otherwise, it must be a safe source. | |
1135 | */ | |
ba73d987 CB |
1136 | if (safe_hardlink_source(mnt_userns, inode) || |
1137 | inode_owner_or_capable(mnt_userns, inode)) | |
800179c9 KC |
1138 | return 0; |
1139 | ||
245d7369 | 1140 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
800179c9 KC |
1141 | return -EPERM; |
1142 | } | |
1143 | ||
30aba665 SM |
1144 | /** |
1145 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory | |
1146 | * should be allowed, or not, on files that already | |
1147 | * exist. | |
ba73d987 | 1148 | * @mnt_userns: user namespace of the mount the inode was found from |
2111c3c0 | 1149 | * @nd: nameidata pathwalk data |
30aba665 SM |
1150 | * @inode: the inode of the file to open |
1151 | * | |
1152 | * Block an O_CREAT open of a FIFO (or a regular file) when: | |
1153 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled | |
1154 | * - the file already exists | |
1155 | * - we are in a sticky directory | |
1156 | * - we don't own the file | |
1157 | * - the owner of the directory doesn't own the file | |
1158 | * - the directory is world writable | |
1159 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 | |
1160 | * the directory doesn't have to be world writable: being group writable will | |
1161 | * be enough. | |
1162 | * | |
ba73d987 CB |
1163 | * If the inode has been found through an idmapped mount the user namespace of |
1164 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
1165 | * care to map the inode according to @mnt_userns before checking permissions. | |
1166 | * On non-idmapped mounts or if permission checking is to be performed on the | |
1167 | * raw inode simply passs init_user_ns. | |
1168 | * | |
30aba665 SM |
1169 | * Returns 0 if the open is allowed, -ve on error. |
1170 | */ | |
ba73d987 CB |
1171 | static int may_create_in_sticky(struct user_namespace *mnt_userns, |
1172 | struct nameidata *nd, struct inode *const inode) | |
30aba665 | 1173 | { |
ba73d987 CB |
1174 | umode_t dir_mode = nd->dir_mode; |
1175 | kuid_t dir_uid = nd->dir_uid; | |
1176 | ||
30aba665 SM |
1177 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || |
1178 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || | |
d0cb5018 | 1179 | likely(!(dir_mode & S_ISVTX)) || |
ba73d987 CB |
1180 | uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) || |
1181 | uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) | |
30aba665 SM |
1182 | return 0; |
1183 | ||
d0cb5018 AV |
1184 | if (likely(dir_mode & 0002) || |
1185 | (dir_mode & 0020 && | |
30aba665 SM |
1186 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
1187 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { | |
245d7369 KC |
1188 | const char *operation = S_ISFIFO(inode->i_mode) ? |
1189 | "sticky_create_fifo" : | |
1190 | "sticky_create_regular"; | |
1191 | audit_log_path_denied(AUDIT_ANOM_CREAT, operation); | |
30aba665 SM |
1192 | return -EACCES; |
1193 | } | |
1194 | return 0; | |
1195 | } | |
1196 | ||
f015f126 DH |
1197 | /* |
1198 | * follow_up - Find the mountpoint of path's vfsmount | |
1199 | * | |
1200 | * Given a path, find the mountpoint of its source file system. | |
1201 | * Replace @path with the path of the mountpoint in the parent mount. | |
1202 | * Up is towards /. | |
1203 | * | |
1204 | * Return 1 if we went up a level and 0 if we were already at the | |
1205 | * root. | |
1206 | */ | |
bab77ebf | 1207 | int follow_up(struct path *path) |
1da177e4 | 1208 | { |
0714a533 AV |
1209 | struct mount *mnt = real_mount(path->mnt); |
1210 | struct mount *parent; | |
1da177e4 | 1211 | struct dentry *mountpoint; |
99b7db7b | 1212 | |
48a066e7 | 1213 | read_seqlock_excl(&mount_lock); |
0714a533 | 1214 | parent = mnt->mnt_parent; |
3c0a6163 | 1215 | if (parent == mnt) { |
48a066e7 | 1216 | read_sequnlock_excl(&mount_lock); |
1da177e4 LT |
1217 | return 0; |
1218 | } | |
0714a533 | 1219 | mntget(&parent->mnt); |
a73324da | 1220 | mountpoint = dget(mnt->mnt_mountpoint); |
48a066e7 | 1221 | read_sequnlock_excl(&mount_lock); |
bab77ebf AV |
1222 | dput(path->dentry); |
1223 | path->dentry = mountpoint; | |
1224 | mntput(path->mnt); | |
0714a533 | 1225 | path->mnt = &parent->mnt; |
1da177e4 LT |
1226 | return 1; |
1227 | } | |
4d359507 | 1228 | EXPORT_SYMBOL(follow_up); |
1da177e4 | 1229 | |
7ef482fa AV |
1230 | static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, |
1231 | struct path *path, unsigned *seqp) | |
1232 | { | |
1233 | while (mnt_has_parent(m)) { | |
1234 | struct dentry *mountpoint = m->mnt_mountpoint; | |
1235 | ||
1236 | m = m->mnt_parent; | |
1237 | if (unlikely(root->dentry == mountpoint && | |
1238 | root->mnt == &m->mnt)) | |
1239 | break; | |
1240 | if (mountpoint != m->mnt.mnt_root) { | |
1241 | path->mnt = &m->mnt; | |
1242 | path->dentry = mountpoint; | |
1243 | *seqp = read_seqcount_begin(&mountpoint->d_seq); | |
1244 | return true; | |
1245 | } | |
1246 | } | |
1247 | return false; | |
1248 | } | |
1249 | ||
2aa38470 AV |
1250 | static bool choose_mountpoint(struct mount *m, const struct path *root, |
1251 | struct path *path) | |
1252 | { | |
1253 | bool found; | |
1254 | ||
1255 | rcu_read_lock(); | |
1256 | while (1) { | |
1257 | unsigned seq, mseq = read_seqbegin(&mount_lock); | |
1258 | ||
1259 | found = choose_mountpoint_rcu(m, root, path, &seq); | |
1260 | if (unlikely(!found)) { | |
1261 | if (!read_seqretry(&mount_lock, mseq)) | |
1262 | break; | |
1263 | } else { | |
1264 | if (likely(__legitimize_path(path, seq, mseq))) | |
1265 | break; | |
1266 | rcu_read_unlock(); | |
1267 | path_put(path); | |
1268 | rcu_read_lock(); | |
1269 | } | |
1270 | } | |
1271 | rcu_read_unlock(); | |
1272 | return found; | |
1273 | } | |
1274 | ||
b5c84bf6 | 1275 | /* |
9875cf80 DH |
1276 | * Perform an automount |
1277 | * - return -EISDIR to tell follow_managed() to stop and return the path we | |
1278 | * were called with. | |
1da177e4 | 1279 | */ |
1c9f5e06 | 1280 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
31e6b01f | 1281 | { |
25e195aa | 1282 | struct dentry *dentry = path->dentry; |
9875cf80 | 1283 | |
0ec26fd0 MS |
1284 | /* We don't want to mount if someone's just doing a stat - |
1285 | * unless they're stat'ing a directory and appended a '/' to | |
1286 | * the name. | |
1287 | * | |
1288 | * We do, however, want to mount if someone wants to open or | |
1289 | * create a file of any type under the mountpoint, wants to | |
1290 | * traverse through the mountpoint or wants to open the | |
1291 | * mounted directory. Also, autofs may mark negative dentries | |
1292 | * as being automount points. These will need the attentions | |
1293 | * of the daemon to instantiate them before they can be used. | |
9875cf80 | 1294 | */ |
1c9f5e06 | 1295 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
5d38f049 | 1296 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
25e195aa | 1297 | dentry->d_inode) |
5d38f049 | 1298 | return -EISDIR; |
0ec26fd0 | 1299 | |
1c9f5e06 | 1300 | if (count && (*count)++ >= MAXSYMLINKS) |
9875cf80 DH |
1301 | return -ELOOP; |
1302 | ||
25e195aa | 1303 | return finish_automount(dentry->d_op->d_automount(path), path); |
463ffb2e AV |
1304 | } |
1305 | ||
9875cf80 | 1306 | /* |
9deed3eb AV |
1307 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
1308 | * dentries are pinned but not locked here, so negative dentry can go | |
1309 | * positive right under us. Use of smp_load_acquire() provides a barrier | |
1310 | * sufficient for ->d_inode and ->d_flags consistency. | |
9875cf80 | 1311 | */ |
9deed3eb AV |
1312 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
1313 | int *count, unsigned lookup_flags) | |
1da177e4 | 1314 | { |
9deed3eb | 1315 | struct vfsmount *mnt = path->mnt; |
9875cf80 | 1316 | bool need_mntput = false; |
8aef1884 | 1317 | int ret = 0; |
9875cf80 | 1318 | |
9deed3eb | 1319 | while (flags & DCACHE_MANAGED_DENTRY) { |
cc53ce53 DH |
1320 | /* Allow the filesystem to manage the transit without i_mutex |
1321 | * being held. */ | |
d41efb52 | 1322 | if (flags & DCACHE_MANAGE_TRANSIT) { |
fb5f51c7 | 1323 | ret = path->dentry->d_op->d_manage(path, false); |
508c8772 | 1324 | flags = smp_load_acquire(&path->dentry->d_flags); |
cc53ce53 | 1325 | if (ret < 0) |
8aef1884 | 1326 | break; |
cc53ce53 DH |
1327 | } |
1328 | ||
9deed3eb | 1329 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
9875cf80 | 1330 | struct vfsmount *mounted = lookup_mnt(path); |
9deed3eb | 1331 | if (mounted) { // ... in our namespace |
9875cf80 DH |
1332 | dput(path->dentry); |
1333 | if (need_mntput) | |
1334 | mntput(path->mnt); | |
1335 | path->mnt = mounted; | |
1336 | path->dentry = dget(mounted->mnt_root); | |
9deed3eb AV |
1337 | // here we know it's positive |
1338 | flags = path->dentry->d_flags; | |
9875cf80 DH |
1339 | need_mntput = true; |
1340 | continue; | |
1341 | } | |
9875cf80 DH |
1342 | } |
1343 | ||
9deed3eb AV |
1344 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
1345 | break; | |
9875cf80 | 1346 | |
9deed3eb AV |
1347 | // uncovered automount point |
1348 | ret = follow_automount(path, count, lookup_flags); | |
1349 | flags = smp_load_acquire(&path->dentry->d_flags); | |
1350 | if (ret < 0) | |
1351 | break; | |
1da177e4 | 1352 | } |
8aef1884 | 1353 | |
9deed3eb AV |
1354 | if (ret == -EISDIR) |
1355 | ret = 0; | |
1356 | // possible if you race with several mount --move | |
1357 | if (need_mntput && path->mnt == mnt) | |
1358 | mntput(path->mnt); | |
1359 | if (!ret && unlikely(d_flags_negative(flags))) | |
d41efb52 | 1360 | ret = -ENOENT; |
9deed3eb | 1361 | *jumped = need_mntput; |
8402752e | 1362 | return ret; |
1da177e4 LT |
1363 | } |
1364 | ||
9deed3eb AV |
1365 | static inline int traverse_mounts(struct path *path, bool *jumped, |
1366 | int *count, unsigned lookup_flags) | |
1367 | { | |
1368 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); | |
1369 | ||
1370 | /* fastpath */ | |
1371 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { | |
1372 | *jumped = false; | |
1373 | if (unlikely(d_flags_negative(flags))) | |
1374 | return -ENOENT; | |
1375 | return 0; | |
1376 | } | |
1377 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); | |
1378 | } | |
1379 | ||
cc53ce53 | 1380 | int follow_down_one(struct path *path) |
1da177e4 LT |
1381 | { |
1382 | struct vfsmount *mounted; | |
1383 | ||
1c755af4 | 1384 | mounted = lookup_mnt(path); |
1da177e4 | 1385 | if (mounted) { |
9393bd07 AV |
1386 | dput(path->dentry); |
1387 | mntput(path->mnt); | |
1388 | path->mnt = mounted; | |
1389 | path->dentry = dget(mounted->mnt_root); | |
1da177e4 LT |
1390 | return 1; |
1391 | } | |
1392 | return 0; | |
1393 | } | |
4d359507 | 1394 | EXPORT_SYMBOL(follow_down_one); |
1da177e4 | 1395 | |
9deed3eb AV |
1396 | /* |
1397 | * Follow down to the covering mount currently visible to userspace. At each | |
1398 | * point, the filesystem owning that dentry may be queried as to whether the | |
1399 | * caller is permitted to proceed or not. | |
1400 | */ | |
1401 | int follow_down(struct path *path) | |
1402 | { | |
1403 | struct vfsmount *mnt = path->mnt; | |
1404 | bool jumped; | |
1405 | int ret = traverse_mounts(path, &jumped, NULL, 0); | |
1406 | ||
1407 | if (path->mnt != mnt) | |
1408 | mntput(mnt); | |
1409 | return ret; | |
1410 | } | |
1411 | EXPORT_SYMBOL(follow_down); | |
1412 | ||
9875cf80 | 1413 | /* |
287548e4 AV |
1414 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
1415 | * we meet a managed dentry that would need blocking. | |
9875cf80 DH |
1416 | */ |
1417 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, | |
254cf582 | 1418 | struct inode **inode, unsigned *seqp) |
9875cf80 | 1419 | { |
ea936aeb AV |
1420 | struct dentry *dentry = path->dentry; |
1421 | unsigned int flags = dentry->d_flags; | |
1422 | ||
1423 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) | |
1424 | return true; | |
1425 | ||
1426 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
1427 | return false; | |
1428 | ||
62a7375e | 1429 | for (;;) { |
62a7375e IK |
1430 | /* |
1431 | * Don't forget we might have a non-mountpoint managed dentry | |
1432 | * that wants to block transit. | |
1433 | */ | |
ea936aeb AV |
1434 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
1435 | int res = dentry->d_op->d_manage(path, true); | |
1436 | if (res) | |
1437 | return res == -EISDIR; | |
1438 | flags = dentry->d_flags; | |
b8faf035 | 1439 | } |
62a7375e | 1440 | |
ea936aeb AV |
1441 | if (flags & DCACHE_MOUNTED) { |
1442 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); | |
1443 | if (mounted) { | |
1444 | path->mnt = &mounted->mnt; | |
1445 | dentry = path->dentry = mounted->mnt.mnt_root; | |
bcba1e7d | 1446 | nd->state |= ND_JUMPED; |
ea936aeb AV |
1447 | *seqp = read_seqcount_begin(&dentry->d_seq); |
1448 | *inode = dentry->d_inode; | |
1449 | /* | |
1450 | * We don't need to re-check ->d_seq after this | |
1451 | * ->d_inode read - there will be an RCU delay | |
1452 | * between mount hash removal and ->mnt_root | |
1453 | * becoming unpinned. | |
1454 | */ | |
1455 | flags = dentry->d_flags; | |
1456 | continue; | |
1457 | } | |
1458 | if (read_seqretry(&mount_lock, nd->m_seq)) | |
1459 | return false; | |
1460 | } | |
1461 | return !(flags & DCACHE_NEED_AUTOMOUNT); | |
9875cf80 | 1462 | } |
287548e4 AV |
1463 | } |
1464 | ||
db3c9ade AV |
1465 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
1466 | struct path *path, struct inode **inode, | |
1467 | unsigned int *seqp) | |
bd7c4b50 | 1468 | { |
9deed3eb | 1469 | bool jumped; |
db3c9ade | 1470 | int ret; |
bd7c4b50 | 1471 | |
db3c9ade AV |
1472 | path->mnt = nd->path.mnt; |
1473 | path->dentry = dentry; | |
c153007b AV |
1474 | if (nd->flags & LOOKUP_RCU) { |
1475 | unsigned int seq = *seqp; | |
1476 | if (unlikely(!*inode)) | |
1477 | return -ENOENT; | |
1478 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) | |
9deed3eb | 1479 | return 0; |
ae66db45 | 1480 | if (!try_to_unlazy_next(nd, dentry, seq)) |
c153007b AV |
1481 | return -ECHILD; |
1482 | // *path might've been clobbered by __follow_mount_rcu() | |
1483 | path->mnt = nd->path.mnt; | |
1484 | path->dentry = dentry; | |
1485 | } | |
9deed3eb AV |
1486 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
1487 | if (jumped) { | |
1488 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
1489 | ret = -EXDEV; | |
1490 | else | |
bcba1e7d | 1491 | nd->state |= ND_JUMPED; |
9deed3eb AV |
1492 | } |
1493 | if (unlikely(ret)) { | |
1494 | dput(path->dentry); | |
1495 | if (path->mnt != nd->path.mnt) | |
1496 | mntput(path->mnt); | |
1497 | } else { | |
bd7c4b50 AV |
1498 | *inode = d_backing_inode(path->dentry); |
1499 | *seqp = 0; /* out of RCU mode, so the value doesn't matter */ | |
1500 | } | |
1501 | return ret; | |
1502 | } | |
1503 | ||
baa03890 | 1504 | /* |
f4fdace9 OD |
1505 | * This looks up the name in dcache and possibly revalidates the found dentry. |
1506 | * NULL is returned if the dentry does not exist in the cache. | |
baa03890 | 1507 | */ |
e3c13928 AV |
1508 | static struct dentry *lookup_dcache(const struct qstr *name, |
1509 | struct dentry *dir, | |
6c51e513 | 1510 | unsigned int flags) |
baa03890 | 1511 | { |
a89f8337 | 1512 | struct dentry *dentry = d_lookup(dir, name); |
bad61189 | 1513 | if (dentry) { |
a89f8337 AV |
1514 | int error = d_revalidate(dentry, flags); |
1515 | if (unlikely(error <= 0)) { | |
1516 | if (!error) | |
1517 | d_invalidate(dentry); | |
1518 | dput(dentry); | |
1519 | return ERR_PTR(error); | |
bad61189 MS |
1520 | } |
1521 | } | |
baa03890 NP |
1522 | return dentry; |
1523 | } | |
1524 | ||
44396f4b | 1525 | /* |
a03ece5f AV |
1526 | * Parent directory has inode locked exclusive. This is one |
1527 | * and only case when ->lookup() gets called on non in-lookup | |
1528 | * dentries - as the matter of fact, this only gets called | |
1529 | * when directory is guaranteed to have no in-lookup children | |
1530 | * at all. | |
44396f4b | 1531 | */ |
e3c13928 | 1532 | static struct dentry *__lookup_hash(const struct qstr *name, |
72bd866a | 1533 | struct dentry *base, unsigned int flags) |
a3255546 | 1534 | { |
6c51e513 | 1535 | struct dentry *dentry = lookup_dcache(name, base, flags); |
a03ece5f AV |
1536 | struct dentry *old; |
1537 | struct inode *dir = base->d_inode; | |
a3255546 | 1538 | |
6c51e513 | 1539 | if (dentry) |
bad61189 | 1540 | return dentry; |
a3255546 | 1541 | |
a03ece5f AV |
1542 | /* Don't create child dentry for a dead directory. */ |
1543 | if (unlikely(IS_DEADDIR(dir))) | |
1544 | return ERR_PTR(-ENOENT); | |
1545 | ||
6c51e513 AV |
1546 | dentry = d_alloc(base, name); |
1547 | if (unlikely(!dentry)) | |
1548 | return ERR_PTR(-ENOMEM); | |
1549 | ||
a03ece5f AV |
1550 | old = dir->i_op->lookup(dir, dentry, flags); |
1551 | if (unlikely(old)) { | |
1552 | dput(dentry); | |
1553 | dentry = old; | |
1554 | } | |
1555 | return dentry; | |
a3255546 AV |
1556 | } |
1557 | ||
20e34357 AV |
1558 | static struct dentry *lookup_fast(struct nameidata *nd, |
1559 | struct inode **inode, | |
1560 | unsigned *seqp) | |
1da177e4 | 1561 | { |
31e6b01f | 1562 | struct dentry *dentry, *parent = nd->path.dentry; |
5a18fff2 | 1563 | int status = 1; |
9875cf80 | 1564 | |
b04f784e NP |
1565 | /* |
1566 | * Rename seqlock is not required here because in the off chance | |
5d0f49c1 AV |
1567 | * of a false negative due to a concurrent rename, the caller is |
1568 | * going to fall back to non-racy lookup. | |
b04f784e | 1569 | */ |
31e6b01f NP |
1570 | if (nd->flags & LOOKUP_RCU) { |
1571 | unsigned seq; | |
da53be12 | 1572 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
5d0f49c1 | 1573 | if (unlikely(!dentry)) { |
e36cffed | 1574 | if (!try_to_unlazy(nd)) |
20e34357 AV |
1575 | return ERR_PTR(-ECHILD); |
1576 | return NULL; | |
5d0f49c1 | 1577 | } |
5a18fff2 | 1578 | |
12f8ad4b LT |
1579 | /* |
1580 | * This sequence count validates that the inode matches | |
1581 | * the dentry name information from lookup. | |
1582 | */ | |
63afdfc7 | 1583 | *inode = d_backing_inode(dentry); |
5d0f49c1 | 1584 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
20e34357 | 1585 | return ERR_PTR(-ECHILD); |
12f8ad4b LT |
1586 | |
1587 | /* | |
1588 | * This sequence count validates that the parent had no | |
1589 | * changes while we did the lookup of the dentry above. | |
1590 | * | |
1591 | * The memory barrier in read_seqcount_begin of child is | |
1592 | * enough, we can use __read_seqcount_retry here. | |
1593 | */ | |
5d0f49c1 | 1594 | if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) |
20e34357 | 1595 | return ERR_PTR(-ECHILD); |
5a18fff2 | 1596 | |
254cf582 | 1597 | *seqp = seq; |
a89f8337 | 1598 | status = d_revalidate(dentry, nd->flags); |
c153007b | 1599 | if (likely(status > 0)) |
20e34357 | 1600 | return dentry; |
ae66db45 | 1601 | if (!try_to_unlazy_next(nd, dentry, seq)) |
20e34357 | 1602 | return ERR_PTR(-ECHILD); |
26ddb45e | 1603 | if (status == -ECHILD) |
209a7fb2 AV |
1604 | /* we'd been told to redo it in non-rcu mode */ |
1605 | status = d_revalidate(dentry, nd->flags); | |
5a18fff2 | 1606 | } else { |
e97cdc87 | 1607 | dentry = __d_lookup(parent, &nd->last); |
5d0f49c1 | 1608 | if (unlikely(!dentry)) |
20e34357 | 1609 | return NULL; |
a89f8337 | 1610 | status = d_revalidate(dentry, nd->flags); |
9875cf80 | 1611 | } |
5a18fff2 | 1612 | if (unlikely(status <= 0)) { |
e9742b53 | 1613 | if (!status) |
5d0f49c1 | 1614 | d_invalidate(dentry); |
5542aa2f | 1615 | dput(dentry); |
20e34357 | 1616 | return ERR_PTR(status); |
24643087 | 1617 | } |
20e34357 | 1618 | return dentry; |
697f514d MS |
1619 | } |
1620 | ||
1621 | /* Fast lookup failed, do it the slow way */ | |
88d8331a AV |
1622 | static struct dentry *__lookup_slow(const struct qstr *name, |
1623 | struct dentry *dir, | |
1624 | unsigned int flags) | |
697f514d | 1625 | { |
88d8331a | 1626 | struct dentry *dentry, *old; |
1936386e | 1627 | struct inode *inode = dir->d_inode; |
d9171b93 | 1628 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
1936386e | 1629 | |
1936386e | 1630 | /* Don't go there if it's already dead */ |
94bdd655 | 1631 | if (unlikely(IS_DEADDIR(inode))) |
88d8331a | 1632 | return ERR_PTR(-ENOENT); |
94bdd655 | 1633 | again: |
d9171b93 | 1634 | dentry = d_alloc_parallel(dir, name, &wq); |
94bdd655 | 1635 | if (IS_ERR(dentry)) |
88d8331a | 1636 | return dentry; |
94bdd655 | 1637 | if (unlikely(!d_in_lookup(dentry))) { |
c64cd6e3 AV |
1638 | int error = d_revalidate(dentry, flags); |
1639 | if (unlikely(error <= 0)) { | |
1640 | if (!error) { | |
1641 | d_invalidate(dentry); | |
949a852e | 1642 | dput(dentry); |
c64cd6e3 | 1643 | goto again; |
949a852e | 1644 | } |
c64cd6e3 AV |
1645 | dput(dentry); |
1646 | dentry = ERR_PTR(error); | |
949a852e | 1647 | } |
94bdd655 AV |
1648 | } else { |
1649 | old = inode->i_op->lookup(inode, dentry, flags); | |
1650 | d_lookup_done(dentry); | |
1651 | if (unlikely(old)) { | |
1652 | dput(dentry); | |
1653 | dentry = old; | |
949a852e AV |
1654 | } |
1655 | } | |
e3c13928 | 1656 | return dentry; |
1da177e4 LT |
1657 | } |
1658 | ||
88d8331a AV |
1659 | static struct dentry *lookup_slow(const struct qstr *name, |
1660 | struct dentry *dir, | |
1661 | unsigned int flags) | |
1662 | { | |
1663 | struct inode *inode = dir->d_inode; | |
1664 | struct dentry *res; | |
1665 | inode_lock_shared(inode); | |
1666 | res = __lookup_slow(name, dir, flags); | |
1667 | inode_unlock_shared(inode); | |
1668 | return res; | |
1669 | } | |
1670 | ||
ba73d987 CB |
1671 | static inline int may_lookup(struct user_namespace *mnt_userns, |
1672 | struct nameidata *nd) | |
52094c8a AV |
1673 | { |
1674 | if (nd->flags & LOOKUP_RCU) { | |
7d6beb71 | 1675 | int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
e36cffed | 1676 | if (err != -ECHILD || !try_to_unlazy(nd)) |
52094c8a | 1677 | return err; |
52094c8a | 1678 | } |
ba73d987 | 1679 | return inode_permission(mnt_userns, nd->inode, MAY_EXEC); |
52094c8a AV |
1680 | } |
1681 | ||
49055906 AV |
1682 | static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) |
1683 | { | |
49055906 AV |
1684 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) |
1685 | return -ELOOP; | |
4542576b AV |
1686 | |
1687 | if (likely(nd->depth != EMBEDDED_LEVELS)) | |
1688 | return 0; | |
1689 | if (likely(nd->stack != nd->internal)) | |
1690 | return 0; | |
60ef60c7 | 1691 | if (likely(nd_alloc_stack(nd))) |
49055906 | 1692 | return 0; |
60ef60c7 AV |
1693 | |
1694 | if (nd->flags & LOOKUP_RCU) { | |
1695 | // we need to grab link before we do unlazy. And we can't skip | |
1696 | // unlazy even if we fail to grab the link - cleanup needs it | |
49055906 | 1697 | bool grabbed_link = legitimize_path(nd, link, seq); |
60ef60c7 | 1698 | |
e36cffed | 1699 | if (!try_to_unlazy(nd) != 0 || !grabbed_link) |
60ef60c7 AV |
1700 | return -ECHILD; |
1701 | ||
1702 | if (nd_alloc_stack(nd)) | |
1703 | return 0; | |
49055906 | 1704 | } |
60ef60c7 | 1705 | return -ENOMEM; |
49055906 AV |
1706 | } |
1707 | ||
b1a81972 AV |
1708 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
1709 | ||
06708adb | 1710 | static const char *pick_link(struct nameidata *nd, struct path *link, |
b1a81972 | 1711 | struct inode *inode, unsigned seq, int flags) |
d63ff28f | 1712 | { |
1cf2665b | 1713 | struct saved *last; |
ad6cc4c3 | 1714 | const char *res; |
49055906 | 1715 | int error = reserve_stack(nd, link, seq); |
ad6cc4c3 | 1716 | |
626de996 | 1717 | if (unlikely(error)) { |
49055906 | 1718 | if (!(nd->flags & LOOKUP_RCU)) |
bc40aee0 | 1719 | path_put(link); |
49055906 | 1720 | return ERR_PTR(error); |
626de996 | 1721 | } |
ab104923 | 1722 | last = nd->stack + nd->depth++; |
1cf2665b | 1723 | last->link = *link; |
fceef393 | 1724 | clear_delayed_call(&last->done); |
0450b2d1 | 1725 | last->seq = seq; |
ad6cc4c3 | 1726 | |
b1a81972 | 1727 | if (flags & WALK_TRAILING) { |
ad6cc4c3 AV |
1728 | error = may_follow_link(nd, inode); |
1729 | if (unlikely(error)) | |
1730 | return ERR_PTR(error); | |
1731 | } | |
1732 | ||
dab741e0 MN |
1733 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || |
1734 | unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) | |
ad6cc4c3 AV |
1735 | return ERR_PTR(-ELOOP); |
1736 | ||
1737 | if (!(nd->flags & LOOKUP_RCU)) { | |
1738 | touch_atime(&last->link); | |
1739 | cond_resched(); | |
1740 | } else if (atime_needs_update(&last->link, inode)) { | |
e36cffed | 1741 | if (!try_to_unlazy(nd)) |
ad6cc4c3 AV |
1742 | return ERR_PTR(-ECHILD); |
1743 | touch_atime(&last->link); | |
1744 | } | |
1745 | ||
1746 | error = security_inode_follow_link(link->dentry, inode, | |
1747 | nd->flags & LOOKUP_RCU); | |
1748 | if (unlikely(error)) | |
1749 | return ERR_PTR(error); | |
1750 | ||
ad6cc4c3 AV |
1751 | res = READ_ONCE(inode->i_link); |
1752 | if (!res) { | |
1753 | const char * (*get)(struct dentry *, struct inode *, | |
1754 | struct delayed_call *); | |
1755 | get = inode->i_op->get_link; | |
1756 | if (nd->flags & LOOKUP_RCU) { | |
1757 | res = get(NULL, inode, &last->done); | |
e36cffed | 1758 | if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) |
ad6cc4c3 | 1759 | res = get(link->dentry, inode, &last->done); |
ad6cc4c3 AV |
1760 | } else { |
1761 | res = get(link->dentry, inode, &last->done); | |
1762 | } | |
1763 | if (!res) | |
1764 | goto all_done; | |
1765 | if (IS_ERR(res)) | |
1766 | return res; | |
1767 | } | |
1768 | if (*res == '/') { | |
1769 | error = nd_jump_root(nd); | |
1770 | if (unlikely(error)) | |
1771 | return ERR_PTR(error); | |
1772 | while (unlikely(*++res == '/')) | |
1773 | ; | |
1774 | } | |
1775 | if (*res) | |
1776 | return res; | |
1777 | all_done: // pure jump | |
1778 | put_link(nd); | |
1779 | return NULL; | |
d63ff28f AV |
1780 | } |
1781 | ||
3ddcd056 LT |
1782 | /* |
1783 | * Do we need to follow links? We _really_ want to be able | |
1784 | * to do this check without having to look at inode->i_op, | |
1785 | * so we keep a cache of "no, this doesn't need follow_link" | |
1786 | * for the common case. | |
1787 | */ | |
b0417d2c | 1788 | static const char *step_into(struct nameidata *nd, int flags, |
cbae4d12 | 1789 | struct dentry *dentry, struct inode *inode, unsigned seq) |
3ddcd056 | 1790 | { |
cbae4d12 AV |
1791 | struct path path; |
1792 | int err = handle_mounts(nd, dentry, &path, &inode, &seq); | |
1793 | ||
1794 | if (err < 0) | |
b0417d2c | 1795 | return ERR_PTR(err); |
cbae4d12 | 1796 | if (likely(!d_is_symlink(path.dentry)) || |
8c4efe22 | 1797 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
aca2903e | 1798 | (flags & WALK_NOFOLLOW)) { |
8f64fb1c | 1799 | /* not a symlink or should not follow */ |
c99687a0 AV |
1800 | if (!(nd->flags & LOOKUP_RCU)) { |
1801 | dput(nd->path.dentry); | |
1802 | if (nd->path.mnt != path.mnt) | |
1803 | mntput(nd->path.mnt); | |
1804 | } | |
1805 | nd->path = path; | |
8f64fb1c AV |
1806 | nd->inode = inode; |
1807 | nd->seq = seq; | |
b0417d2c | 1808 | return NULL; |
8f64fb1c | 1809 | } |
a7f77542 | 1810 | if (nd->flags & LOOKUP_RCU) { |
84f0cd9e | 1811 | /* make sure that d_is_symlink above matches inode */ |
cbae4d12 | 1812 | if (read_seqcount_retry(&path.dentry->d_seq, seq)) |
b0417d2c | 1813 | return ERR_PTR(-ECHILD); |
84f0cd9e AV |
1814 | } else { |
1815 | if (path.mnt == nd->path.mnt) | |
1816 | mntget(path.mnt); | |
a7f77542 | 1817 | } |
b1a81972 | 1818 | return pick_link(nd, &path, inode, seq, flags); |
3ddcd056 LT |
1819 | } |
1820 | ||
c2df1968 AV |
1821 | static struct dentry *follow_dotdot_rcu(struct nameidata *nd, |
1822 | struct inode **inodep, | |
1823 | unsigned *seqp) | |
957dd41d | 1824 | { |
12487f30 | 1825 | struct dentry *parent, *old; |
957dd41d | 1826 | |
12487f30 AV |
1827 | if (path_equal(&nd->path, &nd->root)) |
1828 | goto in_root; | |
1829 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { | |
7ef482fa | 1830 | struct path path; |
efe772d6 | 1831 | unsigned seq; |
7ef482fa AV |
1832 | if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), |
1833 | &nd->root, &path, &seq)) | |
1834 | goto in_root; | |
efe772d6 AV |
1835 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
1836 | return ERR_PTR(-ECHILD); | |
1837 | nd->path = path; | |
1838 | nd->inode = path.dentry->d_inode; | |
1839 | nd->seq = seq; | |
1840 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) | |
1841 | return ERR_PTR(-ECHILD); | |
1842 | /* we know that mountpoint was pinned */ | |
957dd41d | 1843 | } |
12487f30 AV |
1844 | old = nd->path.dentry; |
1845 | parent = old->d_parent; | |
1846 | *inodep = parent->d_inode; | |
1847 | *seqp = read_seqcount_begin(&parent->d_seq); | |
1848 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) | |
1849 | return ERR_PTR(-ECHILD); | |
1850 | if (unlikely(!path_connected(nd->path.mnt, parent))) | |
1851 | return ERR_PTR(-ECHILD); | |
1852 | return parent; | |
1853 | in_root: | |
efe772d6 AV |
1854 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
1855 | return ERR_PTR(-ECHILD); | |
c2df1968 AV |
1856 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
1857 | return ERR_PTR(-ECHILD); | |
1858 | return NULL; | |
957dd41d AV |
1859 | } |
1860 | ||
c2df1968 AV |
1861 | static struct dentry *follow_dotdot(struct nameidata *nd, |
1862 | struct inode **inodep, | |
1863 | unsigned *seqp) | |
957dd41d | 1864 | { |
12487f30 AV |
1865 | struct dentry *parent; |
1866 | ||
1867 | if (path_equal(&nd->path, &nd->root)) | |
1868 | goto in_root; | |
1869 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { | |
2aa38470 AV |
1870 | struct path path; |
1871 | ||
1872 | if (!choose_mountpoint(real_mount(nd->path.mnt), | |
1873 | &nd->root, &path)) | |
1874 | goto in_root; | |
165200d6 AV |
1875 | path_put(&nd->path); |
1876 | nd->path = path; | |
2aa38470 | 1877 | nd->inode = path.dentry->d_inode; |
165200d6 AV |
1878 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
1879 | return ERR_PTR(-EXDEV); | |
957dd41d | 1880 | } |
12487f30 AV |
1881 | /* rare case of legitimate dget_parent()... */ |
1882 | parent = dget_parent(nd->path.dentry); | |
1883 | if (unlikely(!path_connected(nd->path.mnt, parent))) { | |
1884 | dput(parent); | |
1885 | return ERR_PTR(-ENOENT); | |
1886 | } | |
1887 | *seqp = 0; | |
1888 | *inodep = parent->d_inode; | |
1889 | return parent; | |
1890 | ||
1891 | in_root: | |
c2df1968 AV |
1892 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
1893 | return ERR_PTR(-EXDEV); | |
1894 | dget(nd->path.dentry); | |
1895 | return NULL; | |
957dd41d AV |
1896 | } |
1897 | ||
7521f22b | 1898 | static const char *handle_dots(struct nameidata *nd, int type) |
957dd41d AV |
1899 | { |
1900 | if (type == LAST_DOTDOT) { | |
7521f22b | 1901 | const char *error = NULL; |
c2df1968 AV |
1902 | struct dentry *parent; |
1903 | struct inode *inode; | |
1904 | unsigned seq; | |
957dd41d AV |
1905 | |
1906 | if (!nd->root.mnt) { | |
7521f22b | 1907 | error = ERR_PTR(set_root(nd)); |
957dd41d AV |
1908 | if (error) |
1909 | return error; | |
1910 | } | |
1911 | if (nd->flags & LOOKUP_RCU) | |
c2df1968 | 1912 | parent = follow_dotdot_rcu(nd, &inode, &seq); |
957dd41d | 1913 | else |
c2df1968 AV |
1914 | parent = follow_dotdot(nd, &inode, &seq); |
1915 | if (IS_ERR(parent)) | |
1916 | return ERR_CAST(parent); | |
1917 | if (unlikely(!parent)) | |
1918 | error = step_into(nd, WALK_NOFOLLOW, | |
1919 | nd->path.dentry, nd->inode, nd->seq); | |
1920 | else | |
1921 | error = step_into(nd, WALK_NOFOLLOW, | |
1922 | parent, inode, seq); | |
1923 | if (unlikely(error)) | |
957dd41d AV |
1924 | return error; |
1925 | ||
1926 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { | |
1927 | /* | |
1928 | * If there was a racing rename or mount along our | |
1929 | * path, then we can't be sure that ".." hasn't jumped | |
1930 | * above nd->root (and so userspace should retry or use | |
1931 | * some fallback). | |
1932 | */ | |
1933 | smp_rmb(); | |
1934 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) | |
7521f22b | 1935 | return ERR_PTR(-EAGAIN); |
957dd41d | 1936 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
7521f22b | 1937 | return ERR_PTR(-EAGAIN); |
957dd41d AV |
1938 | } |
1939 | } | |
7521f22b | 1940 | return NULL; |
957dd41d AV |
1941 | } |
1942 | ||
92d27016 | 1943 | static const char *walk_component(struct nameidata *nd, int flags) |
ce57dfc1 | 1944 | { |
db3c9ade | 1945 | struct dentry *dentry; |
ce57dfc1 | 1946 | struct inode *inode; |
254cf582 | 1947 | unsigned seq; |
ce57dfc1 AV |
1948 | /* |
1949 | * "." and ".." are special - ".." especially so because it has | |
1950 | * to be able to know about the current root directory and | |
1951 | * parent relationships. | |
1952 | */ | |
4693a547 | 1953 | if (unlikely(nd->last_type != LAST_NORM)) { |
1c4ff1a8 | 1954 | if (!(flags & WALK_MORE) && nd->depth) |
4693a547 | 1955 | put_link(nd); |
7521f22b | 1956 | return handle_dots(nd, nd->last_type); |
4693a547 | 1957 | } |
20e34357 AV |
1958 | dentry = lookup_fast(nd, &inode, &seq); |
1959 | if (IS_ERR(dentry)) | |
92d27016 | 1960 | return ERR_CAST(dentry); |
20e34357 | 1961 | if (unlikely(!dentry)) { |
db3c9ade AV |
1962 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
1963 | if (IS_ERR(dentry)) | |
92d27016 | 1964 | return ERR_CAST(dentry); |
ce57dfc1 | 1965 | } |
56676ec3 AV |
1966 | if (!(flags & WALK_MORE) && nd->depth) |
1967 | put_link(nd); | |
b0417d2c | 1968 | return step_into(nd, flags, dentry, inode, seq); |
ce57dfc1 AV |
1969 | } |
1970 | ||
bfcfaa77 LT |
1971 | /* |
1972 | * We can do the critical dentry name comparison and hashing | |
1973 | * operations one word at a time, but we are limited to: | |
1974 | * | |
1975 | * - Architectures with fast unaligned word accesses. We could | |
1976 | * do a "get_unaligned()" if this helps and is sufficiently | |
1977 | * fast. | |
1978 | * | |
bfcfaa77 LT |
1979 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
1980 | * do not trap on the (extremely unlikely) case of a page | |
1981 | * crossing operation. | |
1982 | * | |
1983 | * - Furthermore, we need an efficient 64-bit compile for the | |
1984 | * 64-bit case in order to generate the "number of bytes in | |
1985 | * the final mask". Again, that could be replaced with a | |
1986 | * efficient population count instruction or similar. | |
1987 | */ | |
1988 | #ifdef CONFIG_DCACHE_WORD_ACCESS | |
1989 | ||
f68e556e | 1990 | #include <asm/word-at-a-time.h> |
bfcfaa77 | 1991 | |
468a9428 | 1992 | #ifdef HASH_MIX |
bfcfaa77 | 1993 | |
468a9428 | 1994 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
bfcfaa77 | 1995 | |
468a9428 | 1996 | #elif defined(CONFIG_64BIT) |
0fed3ac8 | 1997 | /* |
2a18da7a GS |
1998 | * Register pressure in the mixing function is an issue, particularly |
1999 | * on 32-bit x86, but almost any function requires one state value and | |
2000 | * one temporary. Instead, use a function designed for two state values | |
2001 | * and no temporaries. | |
2002 | * | |
2003 | * This function cannot create a collision in only two iterations, so | |
2004 | * we have two iterations to achieve avalanche. In those two iterations, | |
2005 | * we have six layers of mixing, which is enough to spread one bit's | |
2006 | * influence out to 2^6 = 64 state bits. | |
2007 | * | |
2008 | * Rotate constants are scored by considering either 64 one-bit input | |
2009 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the | |
2010 | * probability of that delta causing a change to each of the 128 output | |
2011 | * bits, using a sample of random initial states. | |
2012 | * | |
2013 | * The Shannon entropy of the computed probabilities is then summed | |
2014 | * to produce a score. Ideally, any input change has a 50% chance of | |
2015 | * toggling any given output bit. | |
2016 | * | |
2017 | * Mixing scores (in bits) for (12,45): | |
2018 | * Input delta: 1-bit 2-bit | |
2019 | * 1 round: 713.3 42542.6 | |
2020 | * 2 rounds: 2753.7 140389.8 | |
2021 | * 3 rounds: 5954.1 233458.2 | |
2022 | * 4 rounds: 7862.6 256672.2 | |
2023 | * Perfect: 8192 258048 | |
2024 | * (64*128) (64*63/2 * 128) | |
0fed3ac8 | 2025 | */ |
2a18da7a GS |
2026 | #define HASH_MIX(x, y, a) \ |
2027 | ( x ^= (a), \ | |
2028 | y ^= x, x = rol64(x,12),\ | |
2029 | x += y, y = rol64(y,45),\ | |
2030 | y *= 9 ) | |
bfcfaa77 | 2031 | |
0fed3ac8 | 2032 | /* |
2a18da7a GS |
2033 | * Fold two longs into one 32-bit hash value. This must be fast, but |
2034 | * latency isn't quite as critical, as there is a fair bit of additional | |
2035 | * work done before the hash value is used. | |
0fed3ac8 | 2036 | */ |
2a18da7a | 2037 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 2038 | { |
2a18da7a GS |
2039 | y ^= x * GOLDEN_RATIO_64; |
2040 | y *= GOLDEN_RATIO_64; | |
2041 | return y >> 32; | |
0fed3ac8 GS |
2042 | } |
2043 | ||
bfcfaa77 LT |
2044 | #else /* 32-bit case */ |
2045 | ||
2a18da7a GS |
2046 | /* |
2047 | * Mixing scores (in bits) for (7,20): | |
2048 | * Input delta: 1-bit 2-bit | |
2049 | * 1 round: 330.3 9201.6 | |
2050 | * 2 rounds: 1246.4 25475.4 | |
2051 | * 3 rounds: 1907.1 31295.1 | |
2052 | * 4 rounds: 2042.3 31718.6 | |
2053 | * Perfect: 2048 31744 | |
2054 | * (32*64) (32*31/2 * 64) | |
2055 | */ | |
2056 | #define HASH_MIX(x, y, a) \ | |
2057 | ( x ^= (a), \ | |
2058 | y ^= x, x = rol32(x, 7),\ | |
2059 | x += y, y = rol32(y,20),\ | |
2060 | y *= 9 ) | |
bfcfaa77 | 2061 | |
2a18da7a | 2062 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 2063 | { |
2a18da7a GS |
2064 | /* Use arch-optimized multiply if one exists */ |
2065 | return __hash_32(y ^ __hash_32(x)); | |
0fed3ac8 GS |
2066 | } |
2067 | ||
bfcfaa77 LT |
2068 | #endif |
2069 | ||
2a18da7a GS |
2070 | /* |
2071 | * Return the hash of a string of known length. This is carfully | |
2072 | * designed to match hash_name(), which is the more critical function. | |
2073 | * In particular, we must end by hashing a final word containing 0..7 | |
2074 | * payload bytes, to match the way that hash_name() iterates until it | |
2075 | * finds the delimiter after the name. | |
2076 | */ | |
8387ff25 | 2077 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
bfcfaa77 | 2078 | { |
8387ff25 | 2079 | unsigned long a, x = 0, y = (unsigned long)salt; |
bfcfaa77 LT |
2080 | |
2081 | for (;;) { | |
fcfd2fbf GS |
2082 | if (!len) |
2083 | goto done; | |
e419b4cc | 2084 | a = load_unaligned_zeropad(name); |
bfcfaa77 LT |
2085 | if (len < sizeof(unsigned long)) |
2086 | break; | |
2a18da7a | 2087 | HASH_MIX(x, y, a); |
bfcfaa77 LT |
2088 | name += sizeof(unsigned long); |
2089 | len -= sizeof(unsigned long); | |
bfcfaa77 | 2090 | } |
2a18da7a | 2091 | x ^= a & bytemask_from_count(len); |
bfcfaa77 | 2092 | done: |
2a18da7a | 2093 | return fold_hash(x, y); |
bfcfaa77 LT |
2094 | } |
2095 | EXPORT_SYMBOL(full_name_hash); | |
2096 | ||
fcfd2fbf | 2097 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 2098 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 2099 | { |
8387ff25 LT |
2100 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
2101 | unsigned long adata, mask, len; | |
fcfd2fbf GS |
2102 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
2103 | ||
8387ff25 LT |
2104 | len = 0; |
2105 | goto inside; | |
2106 | ||
fcfd2fbf | 2107 | do { |
2a18da7a | 2108 | HASH_MIX(x, y, a); |
fcfd2fbf | 2109 | len += sizeof(unsigned long); |
8387ff25 | 2110 | inside: |
fcfd2fbf GS |
2111 | a = load_unaligned_zeropad(name+len); |
2112 | } while (!has_zero(a, &adata, &constants)); | |
2113 | ||
2114 | adata = prep_zero_mask(a, adata, &constants); | |
2115 | mask = create_zero_mask(adata); | |
2a18da7a | 2116 | x ^= a & zero_bytemask(mask); |
fcfd2fbf | 2117 | |
2a18da7a | 2118 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
fcfd2fbf GS |
2119 | } |
2120 | EXPORT_SYMBOL(hashlen_string); | |
2121 | ||
bfcfaa77 LT |
2122 | /* |
2123 | * Calculate the length and hash of the path component, and | |
d6bb3e90 | 2124 | * return the "hash_len" as the result. |
bfcfaa77 | 2125 | */ |
8387ff25 | 2126 | static inline u64 hash_name(const void *salt, const char *name) |
bfcfaa77 | 2127 | { |
8387ff25 LT |
2128 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
2129 | unsigned long adata, bdata, mask, len; | |
36126f8f | 2130 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
bfcfaa77 | 2131 | |
8387ff25 LT |
2132 | len = 0; |
2133 | goto inside; | |
2134 | ||
bfcfaa77 | 2135 | do { |
2a18da7a | 2136 | HASH_MIX(x, y, a); |
bfcfaa77 | 2137 | len += sizeof(unsigned long); |
8387ff25 | 2138 | inside: |
e419b4cc | 2139 | a = load_unaligned_zeropad(name+len); |
36126f8f LT |
2140 | b = a ^ REPEAT_BYTE('/'); |
2141 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); | |
2142 | ||
2143 | adata = prep_zero_mask(a, adata, &constants); | |
2144 | bdata = prep_zero_mask(b, bdata, &constants); | |
36126f8f | 2145 | mask = create_zero_mask(adata | bdata); |
2a18da7a | 2146 | x ^= a & zero_bytemask(mask); |
36126f8f | 2147 | |
2a18da7a | 2148 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
bfcfaa77 LT |
2149 | } |
2150 | ||
2a18da7a | 2151 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
bfcfaa77 | 2152 | |
fcfd2fbf | 2153 | /* Return the hash of a string of known length */ |
8387ff25 | 2154 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
0145acc2 | 2155 | { |
8387ff25 | 2156 | unsigned long hash = init_name_hash(salt); |
0145acc2 | 2157 | while (len--) |
fcfd2fbf | 2158 | hash = partial_name_hash((unsigned char)*name++, hash); |
0145acc2 LT |
2159 | return end_name_hash(hash); |
2160 | } | |
ae942ae7 | 2161 | EXPORT_SYMBOL(full_name_hash); |
0145acc2 | 2162 | |
fcfd2fbf | 2163 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 2164 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 2165 | { |
8387ff25 | 2166 | unsigned long hash = init_name_hash(salt); |
fcfd2fbf GS |
2167 | unsigned long len = 0, c; |
2168 | ||
2169 | c = (unsigned char)*name; | |
e0ab7af9 | 2170 | while (c) { |
fcfd2fbf GS |
2171 | len++; |
2172 | hash = partial_name_hash(c, hash); | |
2173 | c = (unsigned char)name[len]; | |
e0ab7af9 | 2174 | } |
fcfd2fbf GS |
2175 | return hashlen_create(end_name_hash(hash), len); |
2176 | } | |
f2a031b6 | 2177 | EXPORT_SYMBOL(hashlen_string); |
fcfd2fbf | 2178 | |
200e9ef7 LT |
2179 | /* |
2180 | * We know there's a real path component here of at least | |
2181 | * one character. | |
2182 | */ | |
8387ff25 | 2183 | static inline u64 hash_name(const void *salt, const char *name) |
200e9ef7 | 2184 | { |
8387ff25 | 2185 | unsigned long hash = init_name_hash(salt); |
200e9ef7 LT |
2186 | unsigned long len = 0, c; |
2187 | ||
2188 | c = (unsigned char)*name; | |
2189 | do { | |
2190 | len++; | |
2191 | hash = partial_name_hash(c, hash); | |
2192 | c = (unsigned char)name[len]; | |
2193 | } while (c && c != '/'); | |
d6bb3e90 | 2194 | return hashlen_create(end_name_hash(hash), len); |
200e9ef7 LT |
2195 | } |
2196 | ||
bfcfaa77 LT |
2197 | #endif |
2198 | ||
1da177e4 LT |
2199 | /* |
2200 | * Name resolution. | |
ea3834d9 PM |
2201 | * This is the basic name resolution function, turning a pathname into |
2202 | * the final dentry. We expect 'base' to be positive and a directory. | |
1da177e4 | 2203 | * |
ea3834d9 PM |
2204 | * Returns 0 and nd will have valid dentry and mnt on success. |
2205 | * Returns error and drops reference to input namei data on failure. | |
1da177e4 | 2206 | */ |
6de88d72 | 2207 | static int link_path_walk(const char *name, struct nameidata *nd) |
1da177e4 | 2208 | { |
d8d4611a | 2209 | int depth = 0; // depth <= nd->depth |
1da177e4 | 2210 | int err; |
32cd7468 | 2211 | |
b4c03536 | 2212 | nd->last_type = LAST_ROOT; |
c108837e | 2213 | nd->flags |= LOOKUP_PARENT; |
9b5858e9 AV |
2214 | if (IS_ERR(name)) |
2215 | return PTR_ERR(name); | |
1da177e4 LT |
2216 | while (*name=='/') |
2217 | name++; | |
1a97d899 AV |
2218 | if (!*name) { |
2219 | nd->dir_mode = 0; // short-circuit the 'hardening' idiocy | |
9e18f10a | 2220 | return 0; |
1a97d899 | 2221 | } |
1da177e4 | 2222 | |
1da177e4 LT |
2223 | /* At this point we know we have a real path component. */ |
2224 | for(;;) { | |
549c7297 | 2225 | struct user_namespace *mnt_userns; |
92d27016 | 2226 | const char *link; |
d6bb3e90 | 2227 | u64 hash_len; |
fe479a58 | 2228 | int type; |
1da177e4 | 2229 | |
549c7297 CB |
2230 | mnt_userns = mnt_user_ns(nd->path.mnt); |
2231 | err = may_lookup(mnt_userns, nd); | |
2a18da7a | 2232 | if (err) |
3595e234 | 2233 | return err; |
1da177e4 | 2234 | |
8387ff25 | 2235 | hash_len = hash_name(nd->path.dentry, name); |
1da177e4 | 2236 | |
fe479a58 | 2237 | type = LAST_NORM; |
d6bb3e90 | 2238 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
fe479a58 | 2239 | case 2: |
200e9ef7 | 2240 | if (name[1] == '.') { |
fe479a58 | 2241 | type = LAST_DOTDOT; |
bcba1e7d | 2242 | nd->state |= ND_JUMPED; |
16c2cd71 | 2243 | } |
fe479a58 AV |
2244 | break; |
2245 | case 1: | |
2246 | type = LAST_DOT; | |
2247 | } | |
5a202bcd AV |
2248 | if (likely(type == LAST_NORM)) { |
2249 | struct dentry *parent = nd->path.dentry; | |
bcba1e7d | 2250 | nd->state &= ~ND_JUMPED; |
5a202bcd | 2251 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
a060dc50 | 2252 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
da53be12 | 2253 | err = parent->d_op->d_hash(parent, &this); |
5a202bcd | 2254 | if (err < 0) |
3595e234 | 2255 | return err; |
d6bb3e90 LT |
2256 | hash_len = this.hash_len; |
2257 | name = this.name; | |
5a202bcd AV |
2258 | } |
2259 | } | |
fe479a58 | 2260 | |
d6bb3e90 LT |
2261 | nd->last.hash_len = hash_len; |
2262 | nd->last.name = name; | |
5f4a6a69 AV |
2263 | nd->last_type = type; |
2264 | ||
d6bb3e90 LT |
2265 | name += hashlen_len(hash_len); |
2266 | if (!*name) | |
bdf6cbf1 | 2267 | goto OK; |
200e9ef7 LT |
2268 | /* |
2269 | * If it wasn't NUL, we know it was '/'. Skip that | |
2270 | * slash, and continue until no more slashes. | |
2271 | */ | |
2272 | do { | |
d6bb3e90 LT |
2273 | name++; |
2274 | } while (unlikely(*name == '/')); | |
8620c238 AV |
2275 | if (unlikely(!*name)) { |
2276 | OK: | |
d8d4611a | 2277 | /* pathname or trailing symlink, done */ |
c108837e | 2278 | if (!depth) { |
549c7297 | 2279 | nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode); |
0f705953 | 2280 | nd->dir_mode = nd->inode->i_mode; |
c108837e | 2281 | nd->flags &= ~LOOKUP_PARENT; |
8620c238 | 2282 | return 0; |
c108837e | 2283 | } |
8620c238 | 2284 | /* last component of nested symlink */ |
d8d4611a | 2285 | name = nd->stack[--depth].name; |
8c4efe22 | 2286 | link = walk_component(nd, 0); |
1c4ff1a8 AV |
2287 | } else { |
2288 | /* not the last component */ | |
8c4efe22 | 2289 | link = walk_component(nd, WALK_MORE); |
8620c238 | 2290 | } |
92d27016 AV |
2291 | if (unlikely(link)) { |
2292 | if (IS_ERR(link)) | |
2293 | return PTR_ERR(link); | |
2294 | /* a symlink to follow */ | |
d8d4611a | 2295 | nd->stack[depth++].name = name; |
92d27016 AV |
2296 | name = link; |
2297 | continue; | |
31e6b01f | 2298 | } |
97242f99 AV |
2299 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
2300 | if (nd->flags & LOOKUP_RCU) { | |
e36cffed | 2301 | if (!try_to_unlazy(nd)) |
97242f99 AV |
2302 | return -ECHILD; |
2303 | } | |
3595e234 | 2304 | return -ENOTDIR; |
97242f99 | 2305 | } |
1da177e4 | 2306 | } |
1da177e4 LT |
2307 | } |
2308 | ||
edc2b1da | 2309 | /* must be paired with terminate_walk() */ |
c8a53ee5 | 2310 | static const char *path_init(struct nameidata *nd, unsigned flags) |
31e6b01f | 2311 | { |
740a1678 | 2312 | int error; |
c8a53ee5 | 2313 | const char *s = nd->name->name; |
31e6b01f | 2314 | |
6c6ec2b0 JA |
2315 | /* LOOKUP_CACHED requires RCU, ask caller to retry */ |
2316 | if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) | |
2317 | return ERR_PTR(-EAGAIN); | |
2318 | ||
c0eb027e LT |
2319 | if (!*s) |
2320 | flags &= ~LOOKUP_RCU; | |
edc2b1da AV |
2321 | if (flags & LOOKUP_RCU) |
2322 | rcu_read_lock(); | |
c0eb027e | 2323 | |
bcba1e7d AV |
2324 | nd->flags = flags; |
2325 | nd->state |= ND_JUMPED; | |
ab87f9a5 AS |
2326 | |
2327 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); | |
2328 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); | |
2329 | smp_rmb(); | |
2330 | ||
bcba1e7d | 2331 | if (nd->state & ND_ROOT_PRESET) { |
b18825a7 DH |
2332 | struct dentry *root = nd->root.dentry; |
2333 | struct inode *inode = root->d_inode; | |
93893862 AV |
2334 | if (*s && unlikely(!d_can_lookup(root))) |
2335 | return ERR_PTR(-ENOTDIR); | |
5b6ca027 AV |
2336 | nd->path = nd->root; |
2337 | nd->inode = inode; | |
2338 | if (flags & LOOKUP_RCU) { | |
ab87f9a5 | 2339 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
8f47a016 | 2340 | nd->root_seq = nd->seq; |
5b6ca027 AV |
2341 | } else { |
2342 | path_get(&nd->path); | |
2343 | } | |
368ee9ba | 2344 | return s; |
5b6ca027 AV |
2345 | } |
2346 | ||
31e6b01f | 2347 | nd->root.mnt = NULL; |
31e6b01f | 2348 | |
8db52c7e AS |
2349 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
2350 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { | |
740a1678 AS |
2351 | error = nd_jump_root(nd); |
2352 | if (unlikely(error)) | |
2353 | return ERR_PTR(error); | |
2354 | return s; | |
8db52c7e AS |
2355 | } |
2356 | ||
2357 | /* Relative pathname -- get the starting-point it is relative to. */ | |
2358 | if (nd->dfd == AT_FDCWD) { | |
e41f7d4e AV |
2359 | if (flags & LOOKUP_RCU) { |
2360 | struct fs_struct *fs = current->fs; | |
2361 | unsigned seq; | |
31e6b01f | 2362 | |
e41f7d4e AV |
2363 | do { |
2364 | seq = read_seqcount_begin(&fs->seq); | |
2365 | nd->path = fs->pwd; | |
ef55d917 | 2366 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e AV |
2367 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
2368 | } while (read_seqcount_retry(&fs->seq, seq)); | |
2369 | } else { | |
2370 | get_fs_pwd(current->fs, &nd->path); | |
ef55d917 | 2371 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2372 | } |
31e6b01f | 2373 | } else { |
582aa64a | 2374 | /* Caller must check execute permissions on the starting path component */ |
c8a53ee5 | 2375 | struct fd f = fdget_raw(nd->dfd); |
31e6b01f NP |
2376 | struct dentry *dentry; |
2377 | ||
2903ff01 | 2378 | if (!f.file) |
368ee9ba | 2379 | return ERR_PTR(-EBADF); |
31e6b01f | 2380 | |
2903ff01 | 2381 | dentry = f.file->f_path.dentry; |
31e6b01f | 2382 | |
edc2b1da AV |
2383 | if (*s && unlikely(!d_can_lookup(dentry))) { |
2384 | fdput(f); | |
2385 | return ERR_PTR(-ENOTDIR); | |
f52e0c11 | 2386 | } |
31e6b01f | 2387 | |
2903ff01 | 2388 | nd->path = f.file->f_path; |
e41f7d4e | 2389 | if (flags & LOOKUP_RCU) { |
34a26b99 AV |
2390 | nd->inode = nd->path.dentry->d_inode; |
2391 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | |
e41f7d4e | 2392 | } else { |
2903ff01 | 2393 | path_get(&nd->path); |
34a26b99 | 2394 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2395 | } |
34a26b99 | 2396 | fdput(f); |
31e6b01f | 2397 | } |
8db52c7e | 2398 | |
adb21d2b AS |
2399 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
2400 | if (flags & LOOKUP_IS_SCOPED) { | |
2401 | nd->root = nd->path; | |
2402 | if (flags & LOOKUP_RCU) { | |
2403 | nd->root_seq = nd->seq; | |
2404 | } else { | |
2405 | path_get(&nd->root); | |
bcba1e7d | 2406 | nd->state |= ND_ROOT_GRABBED; |
adb21d2b AS |
2407 | } |
2408 | } | |
2409 | return s; | |
9b4a9b14 AV |
2410 | } |
2411 | ||
1ccac622 | 2412 | static inline const char *lookup_last(struct nameidata *nd) |
bd92d7fe AV |
2413 | { |
2414 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) | |
2415 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
2416 | ||
c108837e | 2417 | return walk_component(nd, WALK_TRAILING); |
bd92d7fe AV |
2418 | } |
2419 | ||
4f757f3c AV |
2420 | static int handle_lookup_down(struct nameidata *nd) |
2421 | { | |
c153007b | 2422 | if (!(nd->flags & LOOKUP_RCU)) |
db3c9ade | 2423 | dget(nd->path.dentry); |
b0417d2c AV |
2424 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
2425 | nd->path.dentry, nd->inode, nd->seq)); | |
4f757f3c AV |
2426 | } |
2427 | ||
9b4a9b14 | 2428 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2429 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
9b4a9b14 | 2430 | { |
c8a53ee5 | 2431 | const char *s = path_init(nd, flags); |
bd92d7fe | 2432 | int err; |
31e6b01f | 2433 | |
9b5858e9 | 2434 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
4f757f3c | 2435 | err = handle_lookup_down(nd); |
5f336e72 AV |
2436 | if (unlikely(err < 0)) |
2437 | s = ERR_PTR(err); | |
4f757f3c AV |
2438 | } |
2439 | ||
1ccac622 AV |
2440 | while (!(err = link_path_walk(s, nd)) && |
2441 | (s = lookup_last(nd)) != NULL) | |
2442 | ; | |
4f0ed93f AV |
2443 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
2444 | err = handle_lookup_down(nd); | |
bcba1e7d | 2445 | nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... |
4f0ed93f | 2446 | } |
9f1fafee AV |
2447 | if (!err) |
2448 | err = complete_walk(nd); | |
bd92d7fe | 2449 | |
deb106c6 AV |
2450 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
2451 | if (!d_can_lookup(nd->path.dentry)) | |
bd23a539 | 2452 | err = -ENOTDIR; |
625b6d10 AV |
2453 | if (!err) { |
2454 | *path = nd->path; | |
2455 | nd->path.mnt = NULL; | |
2456 | nd->path.dentry = NULL; | |
2457 | } | |
2458 | terminate_walk(nd); | |
bd92d7fe | 2459 | return err; |
ee0827cd | 2460 | } |
31e6b01f | 2461 | |
31d921c7 DH |
2462 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
2463 | struct path *path, struct path *root) | |
ee0827cd | 2464 | { |
894bc8c4 | 2465 | int retval; |
9883d185 | 2466 | struct nameidata nd; |
abc9f5be AV |
2467 | if (IS_ERR(name)) |
2468 | return PTR_ERR(name); | |
06422964 | 2469 | set_nameidata(&nd, dfd, name, root); |
c8a53ee5 | 2470 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
ee0827cd | 2471 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2472 | retval = path_lookupat(&nd, flags, path); |
ee0827cd | 2473 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2474 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
31e6b01f | 2475 | |
f78570dd | 2476 | if (likely(!retval)) |
161aff1d AV |
2477 | audit_inode(name, path->dentry, |
2478 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); | |
9883d185 | 2479 | restore_nameidata(); |
e4bd1c1a | 2480 | putname(name); |
170aa3d0 | 2481 | return retval; |
1da177e4 LT |
2482 | } |
2483 | ||
8bcb77fa | 2484 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2485 | static int path_parentat(struct nameidata *nd, unsigned flags, |
391172c4 | 2486 | struct path *parent) |
8bcb77fa | 2487 | { |
c8a53ee5 | 2488 | const char *s = path_init(nd, flags); |
9b5858e9 | 2489 | int err = link_path_walk(s, nd); |
8bcb77fa AV |
2490 | if (!err) |
2491 | err = complete_walk(nd); | |
391172c4 AV |
2492 | if (!err) { |
2493 | *parent = nd->path; | |
2494 | nd->path.mnt = NULL; | |
2495 | nd->path.dentry = NULL; | |
2496 | } | |
2497 | terminate_walk(nd); | |
8bcb77fa AV |
2498 | return err; |
2499 | } | |
2500 | ||
5c31b6ce | 2501 | static struct filename *filename_parentat(int dfd, struct filename *name, |
391172c4 AV |
2502 | unsigned int flags, struct path *parent, |
2503 | struct qstr *last, int *type) | |
8bcb77fa AV |
2504 | { |
2505 | int retval; | |
9883d185 | 2506 | struct nameidata nd; |
8bcb77fa | 2507 | |
5c31b6ce AV |
2508 | if (IS_ERR(name)) |
2509 | return name; | |
06422964 | 2510 | set_nameidata(&nd, dfd, name, NULL); |
c8a53ee5 | 2511 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
8bcb77fa | 2512 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2513 | retval = path_parentat(&nd, flags, parent); |
8bcb77fa | 2514 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2515 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
391172c4 AV |
2516 | if (likely(!retval)) { |
2517 | *last = nd.last; | |
2518 | *type = nd.last_type; | |
c9b07eab | 2519 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
5c31b6ce AV |
2520 | } else { |
2521 | putname(name); | |
2522 | name = ERR_PTR(retval); | |
391172c4 | 2523 | } |
9883d185 | 2524 | restore_nameidata(); |
5c31b6ce | 2525 | return name; |
8bcb77fa AV |
2526 | } |
2527 | ||
79714f72 AV |
2528 | /* does lookup, returns the object with parent locked */ |
2529 | struct dentry *kern_path_locked(const char *name, struct path *path) | |
5590ff0d | 2530 | { |
5c31b6ce AV |
2531 | struct filename *filename; |
2532 | struct dentry *d; | |
391172c4 AV |
2533 | struct qstr last; |
2534 | int type; | |
51689104 | 2535 | |
5c31b6ce AV |
2536 | filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, |
2537 | &last, &type); | |
51689104 PM |
2538 | if (IS_ERR(filename)) |
2539 | return ERR_CAST(filename); | |
5c31b6ce | 2540 | if (unlikely(type != LAST_NORM)) { |
391172c4 | 2541 | path_put(path); |
5c31b6ce AV |
2542 | putname(filename); |
2543 | return ERR_PTR(-EINVAL); | |
79714f72 | 2544 | } |
5955102c | 2545 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
391172c4 | 2546 | d = __lookup_hash(&last, path->dentry, 0); |
79714f72 | 2547 | if (IS_ERR(d)) { |
5955102c | 2548 | inode_unlock(path->dentry->d_inode); |
391172c4 | 2549 | path_put(path); |
79714f72 | 2550 | } |
51689104 | 2551 | putname(filename); |
79714f72 | 2552 | return d; |
5590ff0d UD |
2553 | } |
2554 | ||
d1811465 AV |
2555 | int kern_path(const char *name, unsigned int flags, struct path *path) |
2556 | { | |
abc9f5be AV |
2557 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2558 | flags, path, NULL); | |
d1811465 | 2559 | } |
4d359507 | 2560 | EXPORT_SYMBOL(kern_path); |
d1811465 | 2561 | |
16f18200 JJS |
2562 | /** |
2563 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair | |
2564 | * @dentry: pointer to dentry of the base directory | |
2565 | * @mnt: pointer to vfs mount of the base directory | |
2566 | * @name: pointer to file name | |
2567 | * @flags: lookup flags | |
e0a01249 | 2568 | * @path: pointer to struct path to fill |
16f18200 JJS |
2569 | */ |
2570 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, | |
2571 | const char *name, unsigned int flags, | |
e0a01249 | 2572 | struct path *path) |
16f18200 | 2573 | { |
9ad1aaa6 | 2574 | struct path root = {.mnt = mnt, .dentry = dentry}; |
9ad1aaa6 | 2575 | /* the first argument of filename_lookup() is ignored with root */ |
abc9f5be AV |
2576 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2577 | flags , path, &root); | |
16f18200 | 2578 | } |
4d359507 | 2579 | EXPORT_SYMBOL(vfs_path_lookup); |
16f18200 | 2580 | |
3c95f0dc AV |
2581 | static int lookup_one_len_common(const char *name, struct dentry *base, |
2582 | int len, struct qstr *this) | |
057f6c01 | 2583 | { |
3c95f0dc AV |
2584 | this->name = name; |
2585 | this->len = len; | |
2586 | this->hash = full_name_hash(base, name, len); | |
6a96ba54 | 2587 | if (!len) |
3c95f0dc | 2588 | return -EACCES; |
6a96ba54 | 2589 | |
21d8a15a AV |
2590 | if (unlikely(name[0] == '.')) { |
2591 | if (len < 2 || (len == 2 && name[1] == '.')) | |
3c95f0dc | 2592 | return -EACCES; |
21d8a15a AV |
2593 | } |
2594 | ||
6a96ba54 | 2595 | while (len--) { |
3c95f0dc | 2596 | unsigned int c = *(const unsigned char *)name++; |
6a96ba54 | 2597 | if (c == '/' || c == '\0') |
3c95f0dc | 2598 | return -EACCES; |
6a96ba54 | 2599 | } |
5a202bcd AV |
2600 | /* |
2601 | * See if the low-level filesystem might want | |
2602 | * to use its own hash.. | |
2603 | */ | |
2604 | if (base->d_flags & DCACHE_OP_HASH) { | |
3c95f0dc | 2605 | int err = base->d_op->d_hash(base, this); |
5a202bcd | 2606 | if (err < 0) |
3c95f0dc | 2607 | return err; |
5a202bcd | 2608 | } |
eead1911 | 2609 | |
47291baa | 2610 | return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC); |
3c95f0dc AV |
2611 | } |
2612 | ||
0da0b7fd DH |
2613 | /** |
2614 | * try_lookup_one_len - filesystem helper to lookup single pathname component | |
2615 | * @name: pathname component to lookup | |
2616 | * @base: base directory to lookup from | |
2617 | * @len: maximum length @len should be interpreted to | |
2618 | * | |
2619 | * Look up a dentry by name in the dcache, returning NULL if it does not | |
2620 | * currently exist. The function does not try to create a dentry. | |
2621 | * | |
2622 | * Note that this routine is purely a helper for filesystem usage and should | |
2623 | * not be called by generic code. | |
2624 | * | |
2625 | * The caller must hold base->i_mutex. | |
2626 | */ | |
2627 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) | |
2628 | { | |
2629 | struct qstr this; | |
2630 | int err; | |
2631 | ||
2632 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2633 | ||
2634 | err = lookup_one_len_common(name, base, len, &this); | |
2635 | if (err) | |
2636 | return ERR_PTR(err); | |
2637 | ||
2638 | return lookup_dcache(&this, base, 0); | |
2639 | } | |
2640 | EXPORT_SYMBOL(try_lookup_one_len); | |
2641 | ||
3c95f0dc AV |
2642 | /** |
2643 | * lookup_one_len - filesystem helper to lookup single pathname component | |
2644 | * @name: pathname component to lookup | |
2645 | * @base: base directory to lookup from | |
2646 | * @len: maximum length @len should be interpreted to | |
2647 | * | |
2648 | * Note that this routine is purely a helper for filesystem usage and should | |
2649 | * not be called by generic code. | |
2650 | * | |
2651 | * The caller must hold base->i_mutex. | |
2652 | */ | |
2653 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) | |
2654 | { | |
8613a209 | 2655 | struct dentry *dentry; |
3c95f0dc AV |
2656 | struct qstr this; |
2657 | int err; | |
2658 | ||
2659 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2660 | ||
2661 | err = lookup_one_len_common(name, base, len, &this); | |
cda309de MS |
2662 | if (err) |
2663 | return ERR_PTR(err); | |
2664 | ||
8613a209 AV |
2665 | dentry = lookup_dcache(&this, base, 0); |
2666 | return dentry ? dentry : __lookup_slow(&this, base, 0); | |
057f6c01 | 2667 | } |
4d359507 | 2668 | EXPORT_SYMBOL(lookup_one_len); |
057f6c01 | 2669 | |
bbddca8e N |
2670 | /** |
2671 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component | |
2672 | * @name: pathname component to lookup | |
2673 | * @base: base directory to lookup from | |
2674 | * @len: maximum length @len should be interpreted to | |
2675 | * | |
2676 | * Note that this routine is purely a helper for filesystem usage and should | |
2677 | * not be called by generic code. | |
2678 | * | |
2679 | * Unlike lookup_one_len, it should be called without the parent | |
2680 | * i_mutex held, and will take the i_mutex itself if necessary. | |
2681 | */ | |
2682 | struct dentry *lookup_one_len_unlocked(const char *name, | |
2683 | struct dentry *base, int len) | |
2684 | { | |
2685 | struct qstr this; | |
bbddca8e | 2686 | int err; |
20d00ee8 | 2687 | struct dentry *ret; |
bbddca8e | 2688 | |
3c95f0dc | 2689 | err = lookup_one_len_common(name, base, len, &this); |
bbddca8e N |
2690 | if (err) |
2691 | return ERR_PTR(err); | |
2692 | ||
20d00ee8 LT |
2693 | ret = lookup_dcache(&this, base, 0); |
2694 | if (!ret) | |
2695 | ret = lookup_slow(&this, base, 0); | |
2696 | return ret; | |
bbddca8e N |
2697 | } |
2698 | EXPORT_SYMBOL(lookup_one_len_unlocked); | |
2699 | ||
6c2d4798 AV |
2700 | /* |
2701 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) | |
2702 | * on negatives. Returns known positive or ERR_PTR(); that's what | |
2703 | * most of the users want. Note that pinned negative with unlocked parent | |
2704 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() | |
2705 | * need to be very careful; pinned positives have ->d_inode stable, so | |
2706 | * this one avoids such problems. | |
2707 | */ | |
2708 | struct dentry *lookup_positive_unlocked(const char *name, | |
2709 | struct dentry *base, int len) | |
2710 | { | |
2711 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); | |
2fa6b1e0 | 2712 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
6c2d4798 AV |
2713 | dput(ret); |
2714 | ret = ERR_PTR(-ENOENT); | |
2715 | } | |
2716 | return ret; | |
2717 | } | |
2718 | EXPORT_SYMBOL(lookup_positive_unlocked); | |
2719 | ||
eedf265a EB |
2720 | #ifdef CONFIG_UNIX98_PTYS |
2721 | int path_pts(struct path *path) | |
2722 | { | |
2723 | /* Find something mounted on "pts" in the same directory as | |
2724 | * the input path. | |
2725 | */ | |
a6a7eb76 AV |
2726 | struct dentry *parent = dget_parent(path->dentry); |
2727 | struct dentry *child; | |
19f6028a | 2728 | struct qstr this = QSTR_INIT("pts", 3); |
eedf265a | 2729 | |
a6a7eb76 AV |
2730 | if (unlikely(!path_connected(path->mnt, parent))) { |
2731 | dput(parent); | |
63b27720 | 2732 | return -ENOENT; |
a6a7eb76 | 2733 | } |
63b27720 AV |
2734 | dput(path->dentry); |
2735 | path->dentry = parent; | |
eedf265a EB |
2736 | child = d_hash_and_lookup(parent, &this); |
2737 | if (!child) | |
2738 | return -ENOENT; | |
2739 | ||
2740 | path->dentry = child; | |
2741 | dput(parent); | |
19f6028a | 2742 | follow_down(path); |
eedf265a EB |
2743 | return 0; |
2744 | } | |
2745 | #endif | |
2746 | ||
1fa1e7f6 AW |
2747 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
2748 | struct path *path, int *empty) | |
1da177e4 | 2749 | { |
abc9f5be AV |
2750 | return filename_lookup(dfd, getname_flags(name, flags, empty), |
2751 | flags, path, NULL); | |
1da177e4 | 2752 | } |
b853a161 | 2753 | EXPORT_SYMBOL(user_path_at_empty); |
1fa1e7f6 | 2754 | |
ba73d987 CB |
2755 | int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir, |
2756 | struct inode *inode) | |
1da177e4 | 2757 | { |
8e96e3b7 | 2758 | kuid_t fsuid = current_fsuid(); |
da9592ed | 2759 | |
ba73d987 | 2760 | if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid)) |
1da177e4 | 2761 | return 0; |
ba73d987 | 2762 | if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid)) |
1da177e4 | 2763 | return 0; |
ba73d987 | 2764 | return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER); |
1da177e4 | 2765 | } |
cbdf35bc | 2766 | EXPORT_SYMBOL(__check_sticky); |
1da177e4 LT |
2767 | |
2768 | /* | |
2769 | * Check whether we can remove a link victim from directory dir, check | |
2770 | * whether the type of victim is right. | |
2771 | * 1. We can't do it if dir is read-only (done in permission()) | |
2772 | * 2. We should have write and exec permissions on dir | |
2773 | * 3. We can't remove anything from append-only dir | |
2774 | * 4. We can't do anything with immutable dir (done in permission()) | |
2775 | * 5. If the sticky bit on dir is set we should either | |
2776 | * a. be owner of dir, or | |
2777 | * b. be owner of victim, or | |
2778 | * c. have CAP_FOWNER capability | |
2779 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
2780 | * links pointing to it. | |
0bd23d09 EB |
2781 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
2782 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
2783 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
2784 | * 10. We can't remove a root or mountpoint. | |
2785 | * 11. We don't allow removal of NFS sillyrenamed files; it's handled by | |
1da177e4 LT |
2786 | * nfs_async_unlink(). |
2787 | */ | |
ba73d987 CB |
2788 | static int may_delete(struct user_namespace *mnt_userns, struct inode *dir, |
2789 | struct dentry *victim, bool isdir) | |
1da177e4 | 2790 | { |
63afdfc7 | 2791 | struct inode *inode = d_backing_inode(victim); |
1da177e4 LT |
2792 | int error; |
2793 | ||
b18825a7 | 2794 | if (d_is_negative(victim)) |
1da177e4 | 2795 | return -ENOENT; |
b18825a7 | 2796 | BUG_ON(!inode); |
1da177e4 LT |
2797 | |
2798 | BUG_ON(victim->d_parent->d_inode != dir); | |
593d1ce8 EB |
2799 | |
2800 | /* Inode writeback is not safe when the uid or gid are invalid. */ | |
ba73d987 CB |
2801 | if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || |
2802 | !gid_valid(i_gid_into_mnt(mnt_userns, inode))) | |
593d1ce8 EB |
2803 | return -EOVERFLOW; |
2804 | ||
4fa6b5ec | 2805 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
1da177e4 | 2806 | |
ba73d987 | 2807 | error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2808 | if (error) |
2809 | return error; | |
2810 | if (IS_APPEND(dir)) | |
2811 | return -EPERM; | |
b18825a7 | 2812 | |
ba73d987 CB |
2813 | if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) || |
2814 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || | |
2815 | HAS_UNMAPPED_ID(mnt_userns, inode)) | |
1da177e4 LT |
2816 | return -EPERM; |
2817 | if (isdir) { | |
44b1d530 | 2818 | if (!d_is_dir(victim)) |
1da177e4 LT |
2819 | return -ENOTDIR; |
2820 | if (IS_ROOT(victim)) | |
2821 | return -EBUSY; | |
44b1d530 | 2822 | } else if (d_is_dir(victim)) |
1da177e4 LT |
2823 | return -EISDIR; |
2824 | if (IS_DEADDIR(dir)) | |
2825 | return -ENOENT; | |
2826 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
2827 | return -EBUSY; | |
2828 | return 0; | |
2829 | } | |
2830 | ||
2831 | /* Check whether we can create an object with dentry child in directory | |
2832 | * dir. | |
2833 | * 1. We can't do it if child already exists (open has special treatment for | |
2834 | * this case, but since we are inlined it's OK) | |
2835 | * 2. We can't do it if dir is read-only (done in permission()) | |
036d5236 EB |
2836 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
2837 | * 4. We should have write and exec permissions on dir | |
2838 | * 5. We can't do it if dir is immutable (done in permission()) | |
1da177e4 | 2839 | */ |
ba73d987 CB |
2840 | static inline int may_create(struct user_namespace *mnt_userns, |
2841 | struct inode *dir, struct dentry *child) | |
1da177e4 | 2842 | { |
14e972b4 | 2843 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
1da177e4 LT |
2844 | if (child->d_inode) |
2845 | return -EEXIST; | |
2846 | if (IS_DEADDIR(dir)) | |
2847 | return -ENOENT; | |
8e538913 | 2848 | if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) |
036d5236 | 2849 | return -EOVERFLOW; |
8e538913 | 2850 | |
ba73d987 | 2851 | return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2852 | } |
2853 | ||
1da177e4 LT |
2854 | /* |
2855 | * p1 and p2 should be directories on the same fs. | |
2856 | */ | |
2857 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) | |
2858 | { | |
2859 | struct dentry *p; | |
2860 | ||
2861 | if (p1 == p2) { | |
5955102c | 2862 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
1da177e4 LT |
2863 | return NULL; |
2864 | } | |
2865 | ||
fc64005c | 2866 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
1da177e4 | 2867 | |
e2761a11 OH |
2868 | p = d_ancestor(p2, p1); |
2869 | if (p) { | |
5955102c AV |
2870 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
2871 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); | |
e2761a11 | 2872 | return p; |
1da177e4 LT |
2873 | } |
2874 | ||
e2761a11 OH |
2875 | p = d_ancestor(p1, p2); |
2876 | if (p) { | |
5955102c AV |
2877 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
2878 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); | |
e2761a11 | 2879 | return p; |
1da177e4 LT |
2880 | } |
2881 | ||
5955102c AV |
2882 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
2883 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); | |
1da177e4 LT |
2884 | return NULL; |
2885 | } | |
4d359507 | 2886 | EXPORT_SYMBOL(lock_rename); |
1da177e4 LT |
2887 | |
2888 | void unlock_rename(struct dentry *p1, struct dentry *p2) | |
2889 | { | |
5955102c | 2890 | inode_unlock(p1->d_inode); |
1da177e4 | 2891 | if (p1 != p2) { |
5955102c | 2892 | inode_unlock(p2->d_inode); |
fc64005c | 2893 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
1da177e4 LT |
2894 | } |
2895 | } | |
4d359507 | 2896 | EXPORT_SYMBOL(unlock_rename); |
1da177e4 | 2897 | |
6521f891 CB |
2898 | /** |
2899 | * vfs_create - create new file | |
2900 | * @mnt_userns: user namespace of the mount the inode was found from | |
2901 | * @dir: inode of @dentry | |
2902 | * @dentry: pointer to dentry of the base directory | |
2903 | * @mode: mode of the new file | |
2904 | * @want_excl: whether the file must not yet exist | |
2905 | * | |
2906 | * Create a new file. | |
2907 | * | |
2908 | * If the inode has been found through an idmapped mount the user namespace of | |
2909 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
2910 | * care to map the inode according to @mnt_userns before checking permissions. | |
2911 | * On non-idmapped mounts or if permission checking is to be performed on the | |
2912 | * raw inode simply passs init_user_ns. | |
2913 | */ | |
2914 | int vfs_create(struct user_namespace *mnt_userns, struct inode *dir, | |
2915 | struct dentry *dentry, umode_t mode, bool want_excl) | |
1da177e4 | 2916 | { |
6521f891 | 2917 | int error = may_create(mnt_userns, dir, dentry); |
1da177e4 LT |
2918 | if (error) |
2919 | return error; | |
2920 | ||
acfa4380 | 2921 | if (!dir->i_op->create) |
1da177e4 LT |
2922 | return -EACCES; /* shouldn't it be ENOSYS? */ |
2923 | mode &= S_IALLUGO; | |
2924 | mode |= S_IFREG; | |
2925 | error = security_inode_create(dir, dentry, mode); | |
2926 | if (error) | |
2927 | return error; | |
549c7297 | 2928 | error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl); |
a74574aa | 2929 | if (!error) |
f38aa942 | 2930 | fsnotify_create(dir, dentry); |
1da177e4 LT |
2931 | return error; |
2932 | } | |
4d359507 | 2933 | EXPORT_SYMBOL(vfs_create); |
1da177e4 | 2934 | |
8e6c848e AV |
2935 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
2936 | int (*f)(struct dentry *, umode_t, void *), | |
2937 | void *arg) | |
2938 | { | |
2939 | struct inode *dir = dentry->d_parent->d_inode; | |
ba73d987 | 2940 | int error = may_create(&init_user_ns, dir, dentry); |
8e6c848e AV |
2941 | if (error) |
2942 | return error; | |
2943 | ||
2944 | mode &= S_IALLUGO; | |
2945 | mode |= S_IFREG; | |
2946 | error = security_inode_create(dir, dentry, mode); | |
2947 | if (error) | |
2948 | return error; | |
2949 | error = f(dentry, mode, arg); | |
2950 | if (!error) | |
2951 | fsnotify_create(dir, dentry); | |
2952 | return error; | |
2953 | } | |
2954 | EXPORT_SYMBOL(vfs_mkobj); | |
2955 | ||
a2982cc9 EB |
2956 | bool may_open_dev(const struct path *path) |
2957 | { | |
2958 | return !(path->mnt->mnt_flags & MNT_NODEV) && | |
2959 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); | |
2960 | } | |
2961 | ||
ba73d987 CB |
2962 | static int may_open(struct user_namespace *mnt_userns, const struct path *path, |
2963 | int acc_mode, int flag) | |
1da177e4 | 2964 | { |
3fb64190 | 2965 | struct dentry *dentry = path->dentry; |
1da177e4 LT |
2966 | struct inode *inode = dentry->d_inode; |
2967 | int error; | |
2968 | ||
2969 | if (!inode) | |
2970 | return -ENOENT; | |
2971 | ||
c8fe8f30 CH |
2972 | switch (inode->i_mode & S_IFMT) { |
2973 | case S_IFLNK: | |
1da177e4 | 2974 | return -ELOOP; |
c8fe8f30 | 2975 | case S_IFDIR: |
fc4177be | 2976 | if (acc_mode & MAY_WRITE) |
c8fe8f30 | 2977 | return -EISDIR; |
fc4177be KC |
2978 | if (acc_mode & MAY_EXEC) |
2979 | return -EACCES; | |
c8fe8f30 CH |
2980 | break; |
2981 | case S_IFBLK: | |
2982 | case S_IFCHR: | |
a2982cc9 | 2983 | if (!may_open_dev(path)) |
1da177e4 | 2984 | return -EACCES; |
633fb6ac | 2985 | fallthrough; |
c8fe8f30 CH |
2986 | case S_IFIFO: |
2987 | case S_IFSOCK: | |
633fb6ac KC |
2988 | if (acc_mode & MAY_EXEC) |
2989 | return -EACCES; | |
1da177e4 | 2990 | flag &= ~O_TRUNC; |
c8fe8f30 | 2991 | break; |
0fd338b2 KC |
2992 | case S_IFREG: |
2993 | if ((acc_mode & MAY_EXEC) && path_noexec(path)) | |
2994 | return -EACCES; | |
2995 | break; | |
4a3fd211 | 2996 | } |
b41572e9 | 2997 | |
ba73d987 | 2998 | error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode); |
b41572e9 DH |
2999 | if (error) |
3000 | return error; | |
6146f0d5 | 3001 | |
1da177e4 LT |
3002 | /* |
3003 | * An append-only file must be opened in append mode for writing. | |
3004 | */ | |
3005 | if (IS_APPEND(inode)) { | |
8737c930 | 3006 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
7715b521 | 3007 | return -EPERM; |
1da177e4 | 3008 | if (flag & O_TRUNC) |
7715b521 | 3009 | return -EPERM; |
1da177e4 LT |
3010 | } |
3011 | ||
3012 | /* O_NOATIME can only be set by the owner or superuser */ | |
ba73d987 | 3013 | if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode)) |
7715b521 | 3014 | return -EPERM; |
1da177e4 | 3015 | |
f3c7691e | 3016 | return 0; |
7715b521 | 3017 | } |
1da177e4 | 3018 | |
549c7297 | 3019 | static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp) |
7715b521 | 3020 | { |
f0bb5aaf | 3021 | const struct path *path = &filp->f_path; |
7715b521 AV |
3022 | struct inode *inode = path->dentry->d_inode; |
3023 | int error = get_write_access(inode); | |
3024 | if (error) | |
3025 | return error; | |
3026 | /* | |
3027 | * Refuse to truncate files with mandatory locks held on them. | |
3028 | */ | |
d7a06983 | 3029 | error = locks_verify_locked(filp); |
7715b521 | 3030 | if (!error) |
ea0d3ab2 | 3031 | error = security_path_truncate(path); |
7715b521 | 3032 | if (!error) { |
549c7297 | 3033 | error = do_truncate(mnt_userns, path->dentry, 0, |
7715b521 | 3034 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
e1181ee6 | 3035 | filp); |
7715b521 AV |
3036 | } |
3037 | put_write_access(inode); | |
acd0c935 | 3038 | return error; |
1da177e4 LT |
3039 | } |
3040 | ||
d57999e1 DH |
3041 | static inline int open_to_namei_flags(int flag) |
3042 | { | |
8a5e929d AV |
3043 | if ((flag & O_ACCMODE) == 3) |
3044 | flag--; | |
d57999e1 DH |
3045 | return flag; |
3046 | } | |
3047 | ||
ba73d987 CB |
3048 | static int may_o_create(struct user_namespace *mnt_userns, |
3049 | const struct path *dir, struct dentry *dentry, | |
3050 | umode_t mode) | |
d18e9008 MS |
3051 | { |
3052 | int error = security_path_mknod(dir, dentry, mode, 0); | |
3053 | if (error) | |
3054 | return error; | |
3055 | ||
8e538913 | 3056 | if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns)) |
1328c727 SF |
3057 | return -EOVERFLOW; |
3058 | ||
ba73d987 | 3059 | error = inode_permission(mnt_userns, dir->dentry->d_inode, |
47291baa | 3060 | MAY_WRITE | MAY_EXEC); |
d18e9008 MS |
3061 | if (error) |
3062 | return error; | |
3063 | ||
3064 | return security_inode_create(dir->dentry->d_inode, dentry, mode); | |
3065 | } | |
3066 | ||
1acf0af9 DH |
3067 | /* |
3068 | * Attempt to atomically look up, create and open a file from a negative | |
3069 | * dentry. | |
3070 | * | |
3071 | * Returns 0 if successful. The file will have been created and attached to | |
3072 | * @file by the filesystem calling finish_open(). | |
3073 | * | |
00a07c15 AV |
3074 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
3075 | * be set. The caller will need to perform the open themselves. @path will | |
3076 | * have been updated to point to the new dentry. This may be negative. | |
1acf0af9 DH |
3077 | * |
3078 | * Returns an error code otherwise. | |
3079 | */ | |
239eb983 AV |
3080 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
3081 | struct file *file, | |
239eb983 | 3082 | int open_flag, umode_t mode) |
d18e9008 | 3083 | { |
384f26e2 | 3084 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
d18e9008 | 3085 | struct inode *dir = nd->path.dentry->d_inode; |
d18e9008 | 3086 | int error; |
d18e9008 | 3087 | |
d18e9008 MS |
3088 | if (nd->flags & LOOKUP_DIRECTORY) |
3089 | open_flag |= O_DIRECTORY; | |
3090 | ||
30d90494 AV |
3091 | file->f_path.dentry = DENTRY_NOT_SET; |
3092 | file->f_path.mnt = nd->path.mnt; | |
0fb1ea09 | 3093 | error = dir->i_op->atomic_open(dir, dentry, file, |
44907d79 | 3094 | open_to_namei_flags(open_flag), mode); |
6fbd0714 | 3095 | d_lookup_done(dentry); |
384f26e2 | 3096 | if (!error) { |
64e1ac4d | 3097 | if (file->f_mode & FMODE_OPENED) { |
6fb968cd AV |
3098 | if (unlikely(dentry != file->f_path.dentry)) { |
3099 | dput(dentry); | |
3100 | dentry = dget(file->f_path.dentry); | |
3101 | } | |
64e1ac4d | 3102 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
2675a4eb | 3103 | error = -EIO; |
03da633a | 3104 | } else { |
384f26e2 AV |
3105 | if (file->f_path.dentry) { |
3106 | dput(dentry); | |
3107 | dentry = file->f_path.dentry; | |
03da633a | 3108 | } |
239eb983 | 3109 | if (unlikely(d_is_negative(dentry))) |
a01e718f | 3110 | error = -ENOENT; |
62b2ce96 | 3111 | } |
d18e9008 | 3112 | } |
239eb983 AV |
3113 | if (error) { |
3114 | dput(dentry); | |
3115 | dentry = ERR_PTR(error); | |
3116 | } | |
3117 | return dentry; | |
d18e9008 MS |
3118 | } |
3119 | ||
d58ffd35 | 3120 | /* |
1acf0af9 | 3121 | * Look up and maybe create and open the last component. |
d58ffd35 | 3122 | * |
00a07c15 | 3123 | * Must be called with parent locked (exclusive in O_CREAT case). |
1acf0af9 | 3124 | * |
00a07c15 AV |
3125 | * Returns 0 on success, that is, if |
3126 | * the file was successfully atomically created (if necessary) and opened, or | |
3127 | * the file was not completely opened at this time, though lookups and | |
3128 | * creations were performed. | |
3129 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. | |
3130 | * In the latter case dentry returned in @path might be negative if O_CREAT | |
3131 | * hadn't been specified. | |
1acf0af9 | 3132 | * |
00a07c15 | 3133 | * An error code is returned on failure. |
d58ffd35 | 3134 | */ |
da5ebf5a AV |
3135 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
3136 | const struct open_flags *op, | |
3137 | bool got_write) | |
d58ffd35 | 3138 | { |
549c7297 | 3139 | struct user_namespace *mnt_userns; |
d58ffd35 | 3140 | struct dentry *dir = nd->path.dentry; |
54ef4872 | 3141 | struct inode *dir_inode = dir->d_inode; |
1643b43f | 3142 | int open_flag = op->open_flag; |
d58ffd35 | 3143 | struct dentry *dentry; |
1643b43f | 3144 | int error, create_error = 0; |
1643b43f | 3145 | umode_t mode = op->mode; |
6fbd0714 | 3146 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
d58ffd35 | 3147 | |
ce8644fc | 3148 | if (unlikely(IS_DEADDIR(dir_inode))) |
da5ebf5a | 3149 | return ERR_PTR(-ENOENT); |
d58ffd35 | 3150 | |
73a09dd9 | 3151 | file->f_mode &= ~FMODE_CREATED; |
6fbd0714 AV |
3152 | dentry = d_lookup(dir, &nd->last); |
3153 | for (;;) { | |
3154 | if (!dentry) { | |
3155 | dentry = d_alloc_parallel(dir, &nd->last, &wq); | |
3156 | if (IS_ERR(dentry)) | |
da5ebf5a | 3157 | return dentry; |
6fbd0714 AV |
3158 | } |
3159 | if (d_in_lookup(dentry)) | |
3160 | break; | |
d58ffd35 | 3161 | |
6fbd0714 AV |
3162 | error = d_revalidate(dentry, nd->flags); |
3163 | if (likely(error > 0)) | |
3164 | break; | |
3165 | if (error) | |
3166 | goto out_dput; | |
3167 | d_invalidate(dentry); | |
3168 | dput(dentry); | |
3169 | dentry = NULL; | |
3170 | } | |
3171 | if (dentry->d_inode) { | |
6c51e513 | 3172 | /* Cached positive dentry: will open in f_op->open */ |
da5ebf5a | 3173 | return dentry; |
6c51e513 | 3174 | } |
d18e9008 | 3175 | |
1643b43f AV |
3176 | /* |
3177 | * Checking write permission is tricky, bacuse we don't know if we are | |
3178 | * going to actually need it: O_CREAT opens should work as long as the | |
3179 | * file exists. But checking existence breaks atomicity. The trick is | |
3180 | * to check access and if not granted clear O_CREAT from the flags. | |
3181 | * | |
3182 | * Another problem is returing the "right" error value (e.g. for an | |
3183 | * O_EXCL open we want to return EEXIST not EROFS). | |
3184 | */ | |
99a4a90c AV |
3185 | if (unlikely(!got_write)) |
3186 | open_flag &= ~O_TRUNC; | |
549c7297 | 3187 | mnt_userns = mnt_user_ns(nd->path.mnt); |
1643b43f | 3188 | if (open_flag & O_CREAT) { |
99a4a90c AV |
3189 | if (open_flag & O_EXCL) |
3190 | open_flag &= ~O_TRUNC; | |
1643b43f AV |
3191 | if (!IS_POSIXACL(dir->d_inode)) |
3192 | mode &= ~current_umask(); | |
99a4a90c | 3193 | if (likely(got_write)) |
549c7297 | 3194 | create_error = may_o_create(mnt_userns, &nd->path, |
ba73d987 | 3195 | dentry, mode); |
99a4a90c AV |
3196 | else |
3197 | create_error = -EROFS; | |
d18e9008 | 3198 | } |
99a4a90c AV |
3199 | if (create_error) |
3200 | open_flag &= ~O_CREAT; | |
6ac08709 | 3201 | if (dir_inode->i_op->atomic_open) { |
d489cf9a | 3202 | dentry = atomic_open(nd, dentry, file, open_flag, mode); |
da5ebf5a AV |
3203 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
3204 | dentry = ERR_PTR(create_error); | |
3205 | return dentry; | |
d18e9008 | 3206 | } |
54ef4872 | 3207 | |
6fbd0714 | 3208 | if (d_in_lookup(dentry)) { |
12fa5e24 AV |
3209 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
3210 | nd->flags); | |
6fbd0714 | 3211 | d_lookup_done(dentry); |
12fa5e24 AV |
3212 | if (unlikely(res)) { |
3213 | if (IS_ERR(res)) { | |
3214 | error = PTR_ERR(res); | |
3215 | goto out_dput; | |
3216 | } | |
3217 | dput(dentry); | |
3218 | dentry = res; | |
3219 | } | |
54ef4872 MS |
3220 | } |
3221 | ||
d58ffd35 | 3222 | /* Negative dentry, just create the file */ |
1643b43f | 3223 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
73a09dd9 | 3224 | file->f_mode |= FMODE_CREATED; |
ce8644fc | 3225 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
ce8644fc AV |
3226 | if (!dir_inode->i_op->create) { |
3227 | error = -EACCES; | |
d58ffd35 | 3228 | goto out_dput; |
ce8644fc | 3229 | } |
549c7297 CB |
3230 | |
3231 | error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry, | |
3232 | mode, open_flag & O_EXCL); | |
d58ffd35 MS |
3233 | if (error) |
3234 | goto out_dput; | |
3235 | } | |
1643b43f AV |
3236 | if (unlikely(create_error) && !dentry->d_inode) { |
3237 | error = create_error; | |
3238 | goto out_dput; | |
d58ffd35 | 3239 | } |
da5ebf5a | 3240 | return dentry; |
d58ffd35 MS |
3241 | |
3242 | out_dput: | |
3243 | dput(dentry); | |
da5ebf5a | 3244 | return ERR_PTR(error); |
d58ffd35 MS |
3245 | } |
3246 | ||
c981a482 | 3247 | static const char *open_last_lookups(struct nameidata *nd, |
3ec2eef1 | 3248 | struct file *file, const struct open_flags *op) |
fb1cc555 | 3249 | { |
a1e28038 | 3250 | struct dentry *dir = nd->path.dentry; |
ca344a89 | 3251 | int open_flag = op->open_flag; |
64894cf8 | 3252 | bool got_write = false; |
254cf582 | 3253 | unsigned seq; |
a1eb3315 | 3254 | struct inode *inode; |
da5ebf5a | 3255 | struct dentry *dentry; |
b0417d2c | 3256 | const char *res; |
1f36f774 | 3257 | |
c3e380b0 AV |
3258 | nd->flags |= op->intent; |
3259 | ||
bc77daa7 | 3260 | if (nd->last_type != LAST_NORM) { |
56676ec3 AV |
3261 | if (nd->depth) |
3262 | put_link(nd); | |
ff326a32 | 3263 | return handle_dots(nd, nd->last_type); |
1f36f774 | 3264 | } |
67ee3ad2 | 3265 | |
ca344a89 | 3266 | if (!(open_flag & O_CREAT)) { |
fe2d35ff AV |
3267 | if (nd->last.name[nd->last.len]) |
3268 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
3269 | /* we _can_ be in RCU mode here */ | |
20e34357 AV |
3270 | dentry = lookup_fast(nd, &inode, &seq); |
3271 | if (IS_ERR(dentry)) | |
1ccac622 | 3272 | return ERR_CAST(dentry); |
20e34357 | 3273 | if (likely(dentry)) |
71574865 MS |
3274 | goto finish_lookup; |
3275 | ||
6583fe22 | 3276 | BUG_ON(nd->flags & LOOKUP_RCU); |
b6183df7 MS |
3277 | } else { |
3278 | /* create side of things */ | |
72287417 | 3279 | if (nd->flags & LOOKUP_RCU) { |
e36cffed JA |
3280 | if (!try_to_unlazy(nd)) |
3281 | return ERR_PTR(-ECHILD); | |
72287417 | 3282 | } |
c9b07eab | 3283 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
b6183df7 | 3284 | /* trailing slashes? */ |
deb106c6 | 3285 | if (unlikely(nd->last.name[nd->last.len])) |
1ccac622 | 3286 | return ERR_PTR(-EISDIR); |
b6183df7 | 3287 | } |
a2c36b45 | 3288 | |
9cf843e3 | 3289 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
e36cffed | 3290 | got_write = !mnt_want_write(nd->path.mnt); |
64894cf8 AV |
3291 | /* |
3292 | * do _not_ fail yet - we might not need that or fail with | |
3293 | * a different error; let lookup_open() decide; we'll be | |
3294 | * dropping this one anyway. | |
3295 | */ | |
3296 | } | |
9cf843e3 AV |
3297 | if (open_flag & O_CREAT) |
3298 | inode_lock(dir->d_inode); | |
3299 | else | |
3300 | inode_lock_shared(dir->d_inode); | |
da5ebf5a | 3301 | dentry = lookup_open(nd, file, op, got_write); |
f7bb959d AV |
3302 | if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) |
3303 | fsnotify_create(dir->d_inode, dentry); | |
9cf843e3 AV |
3304 | if (open_flag & O_CREAT) |
3305 | inode_unlock(dir->d_inode); | |
3306 | else | |
3307 | inode_unlock_shared(dir->d_inode); | |
a1e28038 | 3308 | |
c981a482 | 3309 | if (got_write) |
59e96e65 | 3310 | mnt_drop_write(nd->path.mnt); |
d18e9008 | 3311 | |
59e96e65 AV |
3312 | if (IS_ERR(dentry)) |
3313 | return ERR_CAST(dentry); | |
3314 | ||
973d4b73 | 3315 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
e73cabff AV |
3316 | dput(nd->path.dentry); |
3317 | nd->path.dentry = dentry; | |
c981a482 | 3318 | return NULL; |
fb1cc555 AV |
3319 | } |
3320 | ||
20e34357 | 3321 | finish_lookup: |
56676ec3 AV |
3322 | if (nd->depth) |
3323 | put_link(nd); | |
8c4efe22 | 3324 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
ff326a32 | 3325 | if (unlikely(res)) |
b0417d2c | 3326 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
ff326a32 | 3327 | return res; |
c981a482 AV |
3328 | } |
3329 | ||
3330 | /* | |
3331 | * Handle the last step of open() | |
3332 | */ | |
c5971b8c | 3333 | static int do_open(struct nameidata *nd, |
c981a482 AV |
3334 | struct file *file, const struct open_flags *op) |
3335 | { | |
549c7297 | 3336 | struct user_namespace *mnt_userns; |
c981a482 AV |
3337 | int open_flag = op->open_flag; |
3338 | bool do_truncate; | |
3339 | int acc_mode; | |
c981a482 AV |
3340 | int error; |
3341 | ||
ff326a32 AV |
3342 | if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { |
3343 | error = complete_walk(nd); | |
3344 | if (error) | |
3345 | return error; | |
3346 | } | |
973d4b73 AV |
3347 | if (!(file->f_mode & FMODE_CREATED)) |
3348 | audit_inode(nd->name, nd->path.dentry, 0); | |
549c7297 | 3349 | mnt_userns = mnt_user_ns(nd->path.mnt); |
30aba665 | 3350 | if (open_flag & O_CREAT) { |
b94e0b32 AV |
3351 | if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) |
3352 | return -EEXIST; | |
30aba665 | 3353 | if (d_is_dir(nd->path.dentry)) |
c5971b8c | 3354 | return -EISDIR; |
549c7297 | 3355 | error = may_create_in_sticky(mnt_userns, nd, |
30aba665 SM |
3356 | d_backing_inode(nd->path.dentry)); |
3357 | if (unlikely(error)) | |
c5971b8c | 3358 | return error; |
30aba665 | 3359 | } |
44b1d530 | 3360 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
c5971b8c | 3361 | return -ENOTDIR; |
6c0d46c4 | 3362 | |
8795e7d4 AV |
3363 | do_truncate = false; |
3364 | acc_mode = op->acc_mode; | |
5a2d3edd AV |
3365 | if (file->f_mode & FMODE_CREATED) { |
3366 | /* Don't check for write permission, don't truncate */ | |
3367 | open_flag &= ~O_TRUNC; | |
5a2d3edd | 3368 | acc_mode = 0; |
8795e7d4 | 3369 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
0f9d1a10 AV |
3370 | error = mnt_want_write(nd->path.mnt); |
3371 | if (error) | |
c5971b8c | 3372 | return error; |
8795e7d4 | 3373 | do_truncate = true; |
0f9d1a10 | 3374 | } |
549c7297 | 3375 | error = may_open(mnt_userns, &nd->path, acc_mode, open_flag); |
8795e7d4 | 3376 | if (!error && !(file->f_mode & FMODE_OPENED)) |
3ad5615a | 3377 | error = vfs_open(&nd->path, file); |
8795e7d4 AV |
3378 | if (!error) |
3379 | error = ima_file_check(file, op->acc_mode); | |
3380 | if (!error && do_truncate) | |
549c7297 | 3381 | error = handle_truncate(mnt_userns, file); |
c80567c8 AV |
3382 | if (unlikely(error > 0)) { |
3383 | WARN_ON(1); | |
3384 | error = -EINVAL; | |
3385 | } | |
8795e7d4 | 3386 | if (do_truncate) |
0f9d1a10 | 3387 | mnt_drop_write(nd->path.mnt); |
c5971b8c | 3388 | return error; |
fb1cc555 AV |
3389 | } |
3390 | ||
6521f891 CB |
3391 | /** |
3392 | * vfs_tmpfile - create tmpfile | |
3393 | * @mnt_userns: user namespace of the mount the inode was found from | |
3394 | * @dentry: pointer to dentry of the base directory | |
3395 | * @mode: mode of the new tmpfile | |
2111c3c0 | 3396 | * @open_flag: flags |
6521f891 CB |
3397 | * |
3398 | * Create a temporary file. | |
3399 | * | |
3400 | * If the inode has been found through an idmapped mount the user namespace of | |
3401 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
3402 | * care to map the inode according to @mnt_userns before checking permissions. | |
3403 | * On non-idmapped mounts or if permission checking is to be performed on the | |
3404 | * raw inode simply passs init_user_ns. | |
3405 | */ | |
3406 | struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, | |
3407 | struct dentry *dentry, umode_t mode, int open_flag) | |
af7bd4dc | 3408 | { |
af7bd4dc AG |
3409 | struct dentry *child = NULL; |
3410 | struct inode *dir = dentry->d_inode; | |
3411 | struct inode *inode; | |
3412 | int error; | |
3413 | ||
3414 | /* we want directory to be writable */ | |
6521f891 | 3415 | error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
af7bd4dc AG |
3416 | if (error) |
3417 | goto out_err; | |
3418 | error = -EOPNOTSUPP; | |
3419 | if (!dir->i_op->tmpfile) | |
3420 | goto out_err; | |
3421 | error = -ENOMEM; | |
cdf01226 | 3422 | child = d_alloc(dentry, &slash_name); |
af7bd4dc AG |
3423 | if (unlikely(!child)) |
3424 | goto out_err; | |
549c7297 | 3425 | error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); |
af7bd4dc AG |
3426 | if (error) |
3427 | goto out_err; | |
3428 | error = -ENOENT; | |
3429 | inode = child->d_inode; | |
3430 | if (unlikely(!inode)) | |
3431 | goto out_err; | |
3432 | if (!(open_flag & O_EXCL)) { | |
3433 | spin_lock(&inode->i_lock); | |
3434 | inode->i_state |= I_LINKABLE; | |
3435 | spin_unlock(&inode->i_lock); | |
3436 | } | |
a2d2329e | 3437 | ima_post_create_tmpfile(mnt_userns, inode); |
af7bd4dc AG |
3438 | return child; |
3439 | ||
3440 | out_err: | |
3441 | dput(child); | |
3442 | return ERR_PTR(error); | |
3443 | } | |
3444 | EXPORT_SYMBOL(vfs_tmpfile); | |
3445 | ||
c8a53ee5 | 3446 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
60545d0d | 3447 | const struct open_flags *op, |
3ec2eef1 | 3448 | struct file *file) |
60545d0d | 3449 | { |
6521f891 | 3450 | struct user_namespace *mnt_userns; |
625b6d10 | 3451 | struct dentry *child; |
625b6d10 | 3452 | struct path path; |
c8a53ee5 | 3453 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
60545d0d AV |
3454 | if (unlikely(error)) |
3455 | return error; | |
625b6d10 | 3456 | error = mnt_want_write(path.mnt); |
60545d0d AV |
3457 | if (unlikely(error)) |
3458 | goto out; | |
6521f891 CB |
3459 | mnt_userns = mnt_user_ns(path.mnt); |
3460 | child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag); | |
af7bd4dc | 3461 | error = PTR_ERR(child); |
684e73be | 3462 | if (IS_ERR(child)) |
60545d0d | 3463 | goto out2; |
625b6d10 AV |
3464 | dput(path.dentry); |
3465 | path.dentry = child; | |
c8a53ee5 | 3466 | audit_inode(nd->name, child, 0); |
69a91c23 | 3467 | /* Don't check for other permissions, the inode was just created */ |
549c7297 | 3468 | error = may_open(mnt_userns, &path, 0, op->open_flag); |
1e8f44f1 AV |
3469 | if (!error) |
3470 | error = vfs_open(&path, file); | |
60545d0d | 3471 | out2: |
625b6d10 | 3472 | mnt_drop_write(path.mnt); |
60545d0d | 3473 | out: |
625b6d10 | 3474 | path_put(&path); |
60545d0d AV |
3475 | return error; |
3476 | } | |
3477 | ||
6ac08709 AV |
3478 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
3479 | { | |
3480 | struct path path; | |
3481 | int error = path_lookupat(nd, flags, &path); | |
3482 | if (!error) { | |
3483 | audit_inode(nd->name, path.dentry, 0); | |
ae2bb293 | 3484 | error = vfs_open(&path, file); |
6ac08709 AV |
3485 | path_put(&path); |
3486 | } | |
3487 | return error; | |
3488 | } | |
3489 | ||
c8a53ee5 AV |
3490 | static struct file *path_openat(struct nameidata *nd, |
3491 | const struct open_flags *op, unsigned flags) | |
1da177e4 | 3492 | { |
30d90494 | 3493 | struct file *file; |
13aab428 | 3494 | int error; |
31e6b01f | 3495 | |
ea73ea72 | 3496 | file = alloc_empty_file(op->open_flag, current_cred()); |
1afc99be AV |
3497 | if (IS_ERR(file)) |
3498 | return file; | |
31e6b01f | 3499 | |
bb458c64 | 3500 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
3ec2eef1 | 3501 | error = do_tmpfile(nd, flags, op, file); |
5f336e72 | 3502 | } else if (unlikely(file->f_flags & O_PATH)) { |
6ac08709 | 3503 | error = do_o_path(nd, flags, file); |
5f336e72 AV |
3504 | } else { |
3505 | const char *s = path_init(nd, flags); | |
3506 | while (!(error = link_path_walk(s, nd)) && | |
c5971b8c | 3507 | (s = open_last_lookups(nd, file, op)) != NULL) |
1ccac622 | 3508 | ; |
c5971b8c AV |
3509 | if (!error) |
3510 | error = do_open(nd, file, op); | |
5f336e72 | 3511 | terminate_walk(nd); |
806b681c | 3512 | } |
7c1c01ec | 3513 | if (likely(!error)) { |
aad888f8 | 3514 | if (likely(file->f_mode & FMODE_OPENED)) |
7c1c01ec AV |
3515 | return file; |
3516 | WARN_ON(1); | |
3517 | error = -EINVAL; | |
16b1c1cd | 3518 | } |
7c1c01ec AV |
3519 | fput(file); |
3520 | if (error == -EOPENSTALE) { | |
3521 | if (flags & LOOKUP_RCU) | |
3522 | error = -ECHILD; | |
3523 | else | |
3524 | error = -ESTALE; | |
2675a4eb | 3525 | } |
7c1c01ec | 3526 | return ERR_PTR(error); |
1da177e4 LT |
3527 | } |
3528 | ||
669abf4e | 3529 | struct file *do_filp_open(int dfd, struct filename *pathname, |
f9652e10 | 3530 | const struct open_flags *op) |
13aab428 | 3531 | { |
9883d185 | 3532 | struct nameidata nd; |
f9652e10 | 3533 | int flags = op->lookup_flags; |
13aab428 AV |
3534 | struct file *filp; |
3535 | ||
06422964 | 3536 | set_nameidata(&nd, dfd, pathname, NULL); |
c8a53ee5 | 3537 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
13aab428 | 3538 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3539 | filp = path_openat(&nd, op, flags); |
13aab428 | 3540 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3541 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3542 | restore_nameidata(); |
13aab428 AV |
3543 | return filp; |
3544 | } | |
3545 | ||
ffb37ca3 | 3546 | struct file *do_file_open_root(const struct path *root, |
f9652e10 | 3547 | const char *name, const struct open_flags *op) |
73d049a4 | 3548 | { |
9883d185 | 3549 | struct nameidata nd; |
73d049a4 | 3550 | struct file *file; |
51689104 | 3551 | struct filename *filename; |
bcba1e7d | 3552 | int flags = op->lookup_flags; |
73d049a4 | 3553 | |
ffb37ca3 | 3554 | if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) |
73d049a4 AV |
3555 | return ERR_PTR(-ELOOP); |
3556 | ||
51689104 | 3557 | filename = getname_kernel(name); |
a1c83681 | 3558 | if (IS_ERR(filename)) |
51689104 PM |
3559 | return ERR_CAST(filename); |
3560 | ||
06422964 | 3561 | set_nameidata(&nd, -1, filename, root); |
c8a53ee5 | 3562 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
73d049a4 | 3563 | if (unlikely(file == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3564 | file = path_openat(&nd, op, flags); |
73d049a4 | 3565 | if (unlikely(file == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3566 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3567 | restore_nameidata(); |
51689104 | 3568 | putname(filename); |
73d049a4 AV |
3569 | return file; |
3570 | } | |
3571 | ||
fa14a0b8 | 3572 | static struct dentry *filename_create(int dfd, struct filename *name, |
1ac12b4b | 3573 | struct path *path, unsigned int lookup_flags) |
1da177e4 | 3574 | { |
c663e5d8 | 3575 | struct dentry *dentry = ERR_PTR(-EEXIST); |
391172c4 AV |
3576 | struct qstr last; |
3577 | int type; | |
c30dabfe | 3578 | int err2; |
1ac12b4b JL |
3579 | int error; |
3580 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); | |
3581 | ||
3582 | /* | |
3583 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any | |
3584 | * other flags passed in are ignored! | |
3585 | */ | |
3586 | lookup_flags &= LOOKUP_REVAL; | |
3587 | ||
5c31b6ce AV |
3588 | name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
3589 | if (IS_ERR(name)) | |
3590 | return ERR_CAST(name); | |
1da177e4 | 3591 | |
c663e5d8 CH |
3592 | /* |
3593 | * Yucky last component or no last component at all? | |
3594 | * (foo/., foo/.., /////) | |
3595 | */ | |
5c31b6ce | 3596 | if (unlikely(type != LAST_NORM)) |
ed75e95d | 3597 | goto out; |
c663e5d8 | 3598 | |
c30dabfe | 3599 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
391172c4 | 3600 | err2 = mnt_want_write(path->mnt); |
c663e5d8 CH |
3601 | /* |
3602 | * Do the final lookup. | |
3603 | */ | |
391172c4 | 3604 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
5955102c | 3605 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
391172c4 | 3606 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); |
1da177e4 | 3607 | if (IS_ERR(dentry)) |
a8104a9f | 3608 | goto unlock; |
c663e5d8 | 3609 | |
a8104a9f | 3610 | error = -EEXIST; |
b18825a7 | 3611 | if (d_is_positive(dentry)) |
a8104a9f | 3612 | goto fail; |
b18825a7 | 3613 | |
c663e5d8 CH |
3614 | /* |
3615 | * Special case - lookup gave negative, but... we had foo/bar/ | |
3616 | * From the vfs_mknod() POV we just have a negative dentry - | |
3617 | * all is fine. Let's be bastards - you had / on the end, you've | |
3618 | * been asking for (non-existent) directory. -ENOENT for you. | |
3619 | */ | |
391172c4 | 3620 | if (unlikely(!is_dir && last.name[last.len])) { |
a8104a9f | 3621 | error = -ENOENT; |
ed75e95d | 3622 | goto fail; |
e9baf6e5 | 3623 | } |
c30dabfe JK |
3624 | if (unlikely(err2)) { |
3625 | error = err2; | |
a8104a9f | 3626 | goto fail; |
c30dabfe | 3627 | } |
181c37b6 | 3628 | putname(name); |
1da177e4 | 3629 | return dentry; |
1da177e4 | 3630 | fail: |
a8104a9f AV |
3631 | dput(dentry); |
3632 | dentry = ERR_PTR(error); | |
3633 | unlock: | |
5955102c | 3634 | inode_unlock(path->dentry->d_inode); |
c30dabfe | 3635 | if (!err2) |
391172c4 | 3636 | mnt_drop_write(path->mnt); |
ed75e95d | 3637 | out: |
391172c4 | 3638 | path_put(path); |
181c37b6 | 3639 | putname(name); |
1da177e4 LT |
3640 | return dentry; |
3641 | } | |
fa14a0b8 AV |
3642 | |
3643 | struct dentry *kern_path_create(int dfd, const char *pathname, | |
3644 | struct path *path, unsigned int lookup_flags) | |
3645 | { | |
181c37b6 AV |
3646 | return filename_create(dfd, getname_kernel(pathname), |
3647 | path, lookup_flags); | |
fa14a0b8 | 3648 | } |
dae6ad8f AV |
3649 | EXPORT_SYMBOL(kern_path_create); |
3650 | ||
921a1650 AV |
3651 | void done_path_create(struct path *path, struct dentry *dentry) |
3652 | { | |
3653 | dput(dentry); | |
5955102c | 3654 | inode_unlock(path->dentry->d_inode); |
a8104a9f | 3655 | mnt_drop_write(path->mnt); |
921a1650 AV |
3656 | path_put(path); |
3657 | } | |
3658 | EXPORT_SYMBOL(done_path_create); | |
3659 | ||
520ae687 | 3660 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
1ac12b4b | 3661 | struct path *path, unsigned int lookup_flags) |
dae6ad8f | 3662 | { |
181c37b6 | 3663 | return filename_create(dfd, getname(pathname), path, lookup_flags); |
dae6ad8f AV |
3664 | } |
3665 | EXPORT_SYMBOL(user_path_create); | |
3666 | ||
6521f891 CB |
3667 | /** |
3668 | * vfs_mknod - create device node or file | |
3669 | * @mnt_userns: user namespace of the mount the inode was found from | |
3670 | * @dir: inode of @dentry | |
3671 | * @dentry: pointer to dentry of the base directory | |
3672 | * @mode: mode of the new device node or file | |
3673 | * @dev: device number of device to create | |
3674 | * | |
3675 | * Create a device node or file. | |
3676 | * | |
3677 | * If the inode has been found through an idmapped mount the user namespace of | |
3678 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
3679 | * care to map the inode according to @mnt_userns before checking permissions. | |
3680 | * On non-idmapped mounts or if permission checking is to be performed on the | |
3681 | * raw inode simply passs init_user_ns. | |
3682 | */ | |
3683 | int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, | |
3684 | struct dentry *dentry, umode_t mode, dev_t dev) | |
1da177e4 | 3685 | { |
a3c751a5 | 3686 | bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; |
6521f891 | 3687 | int error = may_create(mnt_userns, dir, dentry); |
1da177e4 LT |
3688 | |
3689 | if (error) | |
3690 | return error; | |
3691 | ||
a3c751a5 MS |
3692 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && |
3693 | !capable(CAP_MKNOD)) | |
1da177e4 LT |
3694 | return -EPERM; |
3695 | ||
acfa4380 | 3696 | if (!dir->i_op->mknod) |
1da177e4 LT |
3697 | return -EPERM; |
3698 | ||
08ce5f16 SH |
3699 | error = devcgroup_inode_mknod(mode, dev); |
3700 | if (error) | |
3701 | return error; | |
3702 | ||
1da177e4 LT |
3703 | error = security_inode_mknod(dir, dentry, mode, dev); |
3704 | if (error) | |
3705 | return error; | |
3706 | ||
549c7297 | 3707 | error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev); |
a74574aa | 3708 | if (!error) |
f38aa942 | 3709 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3710 | return error; |
3711 | } | |
4d359507 | 3712 | EXPORT_SYMBOL(vfs_mknod); |
1da177e4 | 3713 | |
f69aac00 | 3714 | static int may_mknod(umode_t mode) |
463c3197 DH |
3715 | { |
3716 | switch (mode & S_IFMT) { | |
3717 | case S_IFREG: | |
3718 | case S_IFCHR: | |
3719 | case S_IFBLK: | |
3720 | case S_IFIFO: | |
3721 | case S_IFSOCK: | |
3722 | case 0: /* zero mode translates to S_IFREG */ | |
3723 | return 0; | |
3724 | case S_IFDIR: | |
3725 | return -EPERM; | |
3726 | default: | |
3727 | return -EINVAL; | |
3728 | } | |
3729 | } | |
3730 | ||
5fee64fc | 3731 | static long do_mknodat(int dfd, const char __user *filename, umode_t mode, |
87c4e192 | 3732 | unsigned int dev) |
1da177e4 | 3733 | { |
6521f891 | 3734 | struct user_namespace *mnt_userns; |
2ad94ae6 | 3735 | struct dentry *dentry; |
dae6ad8f AV |
3736 | struct path path; |
3737 | int error; | |
972567f1 | 3738 | unsigned int lookup_flags = 0; |
1da177e4 | 3739 | |
8e4bfca1 AV |
3740 | error = may_mknod(mode); |
3741 | if (error) | |
3742 | return error; | |
972567f1 JL |
3743 | retry: |
3744 | dentry = user_path_create(dfd, filename, &path, lookup_flags); | |
dae6ad8f AV |
3745 | if (IS_ERR(dentry)) |
3746 | return PTR_ERR(dentry); | |
2ad94ae6 | 3747 | |
dae6ad8f | 3748 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3749 | mode &= ~current_umask(); |
dae6ad8f | 3750 | error = security_path_mknod(&path, dentry, mode, dev); |
be6d3e56 | 3751 | if (error) |
a8104a9f | 3752 | goto out; |
6521f891 CB |
3753 | |
3754 | mnt_userns = mnt_user_ns(path.mnt); | |
463c3197 | 3755 | switch (mode & S_IFMT) { |
1da177e4 | 3756 | case 0: case S_IFREG: |
6521f891 CB |
3757 | error = vfs_create(mnt_userns, path.dentry->d_inode, |
3758 | dentry, mode, true); | |
05d1a717 | 3759 | if (!error) |
a2d2329e | 3760 | ima_post_path_mknod(mnt_userns, dentry); |
1da177e4 LT |
3761 | break; |
3762 | case S_IFCHR: case S_IFBLK: | |
6521f891 CB |
3763 | error = vfs_mknod(mnt_userns, path.dentry->d_inode, |
3764 | dentry, mode, new_decode_dev(dev)); | |
1da177e4 LT |
3765 | break; |
3766 | case S_IFIFO: case S_IFSOCK: | |
6521f891 CB |
3767 | error = vfs_mknod(mnt_userns, path.dentry->d_inode, |
3768 | dentry, mode, 0); | |
1da177e4 | 3769 | break; |
1da177e4 | 3770 | } |
a8104a9f | 3771 | out: |
921a1650 | 3772 | done_path_create(&path, dentry); |
972567f1 JL |
3773 | if (retry_estale(error, lookup_flags)) { |
3774 | lookup_flags |= LOOKUP_REVAL; | |
3775 | goto retry; | |
3776 | } | |
1da177e4 LT |
3777 | return error; |
3778 | } | |
3779 | ||
87c4e192 DB |
3780 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
3781 | unsigned int, dev) | |
3782 | { | |
3783 | return do_mknodat(dfd, filename, mode, dev); | |
3784 | } | |
3785 | ||
8208a22b | 3786 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
5590ff0d | 3787 | { |
87c4e192 | 3788 | return do_mknodat(AT_FDCWD, filename, mode, dev); |
5590ff0d UD |
3789 | } |
3790 | ||
6521f891 CB |
3791 | /** |
3792 | * vfs_mkdir - create directory | |
3793 | * @mnt_userns: user namespace of the mount the inode was found from | |
3794 | * @dir: inode of @dentry | |
3795 | * @dentry: pointer to dentry of the base directory | |
3796 | * @mode: mode of the new directory | |
3797 | * | |
3798 | * Create a directory. | |
3799 | * | |
3800 | * If the inode has been found through an idmapped mount the user namespace of | |
3801 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
3802 | * care to map the inode according to @mnt_userns before checking permissions. | |
3803 | * On non-idmapped mounts or if permission checking is to be performed on the | |
3804 | * raw inode simply passs init_user_ns. | |
3805 | */ | |
3806 | int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, | |
3807 | struct dentry *dentry, umode_t mode) | |
1da177e4 | 3808 | { |
6521f891 | 3809 | int error = may_create(mnt_userns, dir, dentry); |
8de52778 | 3810 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
3811 | |
3812 | if (error) | |
3813 | return error; | |
3814 | ||
acfa4380 | 3815 | if (!dir->i_op->mkdir) |
1da177e4 LT |
3816 | return -EPERM; |
3817 | ||
3818 | mode &= (S_IRWXUGO|S_ISVTX); | |
3819 | error = security_inode_mkdir(dir, dentry, mode); | |
3820 | if (error) | |
3821 | return error; | |
3822 | ||
8de52778 AV |
3823 | if (max_links && dir->i_nlink >= max_links) |
3824 | return -EMLINK; | |
3825 | ||
549c7297 | 3826 | error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode); |
a74574aa | 3827 | if (!error) |
f38aa942 | 3828 | fsnotify_mkdir(dir, dentry); |
1da177e4 LT |
3829 | return error; |
3830 | } | |
4d359507 | 3831 | EXPORT_SYMBOL(vfs_mkdir); |
1da177e4 | 3832 | |
83ff98c3 | 3833 | static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) |
1da177e4 | 3834 | { |
6902d925 | 3835 | struct dentry *dentry; |
dae6ad8f AV |
3836 | struct path path; |
3837 | int error; | |
b76d8b82 | 3838 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
1da177e4 | 3839 | |
b76d8b82 JL |
3840 | retry: |
3841 | dentry = user_path_create(dfd, pathname, &path, lookup_flags); | |
6902d925 | 3842 | if (IS_ERR(dentry)) |
dae6ad8f | 3843 | return PTR_ERR(dentry); |
1da177e4 | 3844 | |
dae6ad8f | 3845 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3846 | mode &= ~current_umask(); |
dae6ad8f | 3847 | error = security_path_mkdir(&path, dentry, mode); |
6521f891 CB |
3848 | if (!error) { |
3849 | struct user_namespace *mnt_userns; | |
3850 | mnt_userns = mnt_user_ns(path.mnt); | |
549c7297 CB |
3851 | error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry, |
3852 | mode); | |
6521f891 | 3853 | } |
921a1650 | 3854 | done_path_create(&path, dentry); |
b76d8b82 JL |
3855 | if (retry_estale(error, lookup_flags)) { |
3856 | lookup_flags |= LOOKUP_REVAL; | |
3857 | goto retry; | |
3858 | } | |
1da177e4 LT |
3859 | return error; |
3860 | } | |
3861 | ||
0101db7a DB |
3862 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
3863 | { | |
3864 | return do_mkdirat(dfd, pathname, mode); | |
3865 | } | |
3866 | ||
a218d0fd | 3867 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
5590ff0d | 3868 | { |
0101db7a | 3869 | return do_mkdirat(AT_FDCWD, pathname, mode); |
5590ff0d UD |
3870 | } |
3871 | ||
6521f891 CB |
3872 | /** |
3873 | * vfs_rmdir - remove directory | |
3874 | * @mnt_userns: user namespace of the mount the inode was found from | |
3875 | * @dir: inode of @dentry | |
3876 | * @dentry: pointer to dentry of the base directory | |
3877 | * | |
3878 | * Remove a directory. | |
3879 | * | |
3880 | * If the inode has been found through an idmapped mount the user namespace of | |
3881 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
3882 | * care to map the inode according to @mnt_userns before checking permissions. | |
3883 | * On non-idmapped mounts or if permission checking is to be performed on the | |
3884 | * raw inode simply passs init_user_ns. | |
3885 | */ | |
3886 | int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir, | |
3887 | struct dentry *dentry) | |
1da177e4 | 3888 | { |
6521f891 | 3889 | int error = may_delete(mnt_userns, dir, dentry, 1); |
1da177e4 LT |
3890 | |
3891 | if (error) | |
3892 | return error; | |
3893 | ||
acfa4380 | 3894 | if (!dir->i_op->rmdir) |
1da177e4 LT |
3895 | return -EPERM; |
3896 | ||
1d2ef590 | 3897 | dget(dentry); |
5955102c | 3898 | inode_lock(dentry->d_inode); |
912dbc15 SW |
3899 | |
3900 | error = -EBUSY; | |
7af1364f | 3901 | if (is_local_mountpoint(dentry)) |
912dbc15 SW |
3902 | goto out; |
3903 | ||
3904 | error = security_inode_rmdir(dir, dentry); | |
3905 | if (error) | |
3906 | goto out; | |
3907 | ||
3908 | error = dir->i_op->rmdir(dir, dentry); | |
3909 | if (error) | |
3910 | goto out; | |
3911 | ||
8767712f | 3912 | shrink_dcache_parent(dentry); |
912dbc15 SW |
3913 | dentry->d_inode->i_flags |= S_DEAD; |
3914 | dont_mount(dentry); | |
8ed936b5 | 3915 | detach_mounts(dentry); |
116b9731 | 3916 | fsnotify_rmdir(dir, dentry); |
912dbc15 SW |
3917 | |
3918 | out: | |
5955102c | 3919 | inode_unlock(dentry->d_inode); |
1d2ef590 | 3920 | dput(dentry); |
912dbc15 | 3921 | if (!error) |
1da177e4 | 3922 | d_delete(dentry); |
1da177e4 LT |
3923 | return error; |
3924 | } | |
4d359507 | 3925 | EXPORT_SYMBOL(vfs_rmdir); |
1da177e4 | 3926 | |
e24ab0ef | 3927 | long do_rmdir(int dfd, struct filename *name) |
1da177e4 | 3928 | { |
6521f891 | 3929 | struct user_namespace *mnt_userns; |
1da177e4 | 3930 | int error = 0; |
1da177e4 | 3931 | struct dentry *dentry; |
f5beed75 AV |
3932 | struct path path; |
3933 | struct qstr last; | |
3934 | int type; | |
c6ee9206 JL |
3935 | unsigned int lookup_flags = 0; |
3936 | retry: | |
e24ab0ef | 3937 | name = filename_parentat(dfd, name, lookup_flags, |
c1d4dd27 | 3938 | &path, &last, &type); |
91a27b2a JL |
3939 | if (IS_ERR(name)) |
3940 | return PTR_ERR(name); | |
1da177e4 | 3941 | |
f5beed75 | 3942 | switch (type) { |
0612d9fb OH |
3943 | case LAST_DOTDOT: |
3944 | error = -ENOTEMPTY; | |
3945 | goto exit1; | |
3946 | case LAST_DOT: | |
3947 | error = -EINVAL; | |
3948 | goto exit1; | |
3949 | case LAST_ROOT: | |
3950 | error = -EBUSY; | |
3951 | goto exit1; | |
1da177e4 | 3952 | } |
0612d9fb | 3953 | |
f5beed75 | 3954 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
3955 | if (error) |
3956 | goto exit1; | |
0612d9fb | 3957 | |
5955102c | 3958 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
f5beed75 | 3959 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
1da177e4 | 3960 | error = PTR_ERR(dentry); |
6902d925 DH |
3961 | if (IS_ERR(dentry)) |
3962 | goto exit2; | |
e6bc45d6 TT |
3963 | if (!dentry->d_inode) { |
3964 | error = -ENOENT; | |
3965 | goto exit3; | |
3966 | } | |
f5beed75 | 3967 | error = security_path_rmdir(&path, dentry); |
be6d3e56 | 3968 | if (error) |
c30dabfe | 3969 | goto exit3; |
6521f891 CB |
3970 | mnt_userns = mnt_user_ns(path.mnt); |
3971 | error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry); | |
0622753b | 3972 | exit3: |
6902d925 DH |
3973 | dput(dentry); |
3974 | exit2: | |
5955102c | 3975 | inode_unlock(path.dentry->d_inode); |
f5beed75 | 3976 | mnt_drop_write(path.mnt); |
1da177e4 | 3977 | exit1: |
f5beed75 | 3978 | path_put(&path); |
c6ee9206 JL |
3979 | if (retry_estale(error, lookup_flags)) { |
3980 | lookup_flags |= LOOKUP_REVAL; | |
3981 | goto retry; | |
3982 | } | |
24fb33d4 | 3983 | putname(name); |
1da177e4 LT |
3984 | return error; |
3985 | } | |
3986 | ||
3cdad428 | 3987 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
5590ff0d | 3988 | { |
e24ab0ef | 3989 | return do_rmdir(AT_FDCWD, getname(pathname)); |
5590ff0d UD |
3990 | } |
3991 | ||
b21996e3 BF |
3992 | /** |
3993 | * vfs_unlink - unlink a filesystem object | |
6521f891 | 3994 | * @mnt_userns: user namespace of the mount the inode was found from |
b21996e3 BF |
3995 | * @dir: parent directory |
3996 | * @dentry: victim | |
3997 | * @delegated_inode: returns victim inode, if the inode is delegated. | |
3998 | * | |
3999 | * The caller must hold dir->i_mutex. | |
4000 | * | |
4001 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and | |
4002 | * return a reference to the inode in delegated_inode. The caller | |
4003 | * should then break the delegation on that inode and retry. Because | |
4004 | * breaking a delegation may take a long time, the caller should drop | |
4005 | * dir->i_mutex before doing so. | |
4006 | * | |
4007 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4008 | * be appropriate for callers that expect the underlying filesystem not | |
4009 | * to be NFS exported. | |
6521f891 CB |
4010 | * |
4011 | * If the inode has been found through an idmapped mount the user namespace of | |
4012 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
4013 | * care to map the inode according to @mnt_userns before checking permissions. | |
4014 | * On non-idmapped mounts or if permission checking is to be performed on the | |
4015 | * raw inode simply passs init_user_ns. | |
b21996e3 | 4016 | */ |
6521f891 CB |
4017 | int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir, |
4018 | struct dentry *dentry, struct inode **delegated_inode) | |
1da177e4 | 4019 | { |
9accbb97 | 4020 | struct inode *target = dentry->d_inode; |
6521f891 | 4021 | int error = may_delete(mnt_userns, dir, dentry, 0); |
1da177e4 LT |
4022 | |
4023 | if (error) | |
4024 | return error; | |
4025 | ||
acfa4380 | 4026 | if (!dir->i_op->unlink) |
1da177e4 LT |
4027 | return -EPERM; |
4028 | ||
5955102c | 4029 | inode_lock(target); |
8ed936b5 | 4030 | if (is_local_mountpoint(dentry)) |
1da177e4 LT |
4031 | error = -EBUSY; |
4032 | else { | |
4033 | error = security_inode_unlink(dir, dentry); | |
bec1052e | 4034 | if (!error) { |
5a14696c BF |
4035 | error = try_break_deleg(target, delegated_inode); |
4036 | if (error) | |
b21996e3 | 4037 | goto out; |
1da177e4 | 4038 | error = dir->i_op->unlink(dir, dentry); |
8ed936b5 | 4039 | if (!error) { |
d83c49f3 | 4040 | dont_mount(dentry); |
8ed936b5 | 4041 | detach_mounts(dentry); |
116b9731 | 4042 | fsnotify_unlink(dir, dentry); |
8ed936b5 | 4043 | } |
bec1052e | 4044 | } |
1da177e4 | 4045 | } |
b21996e3 | 4046 | out: |
5955102c | 4047 | inode_unlock(target); |
1da177e4 LT |
4048 | |
4049 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ | |
4050 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { | |
9accbb97 | 4051 | fsnotify_link_count(target); |
e234f35c | 4052 | d_delete(dentry); |
1da177e4 | 4053 | } |
0eeca283 | 4054 | |
1da177e4 LT |
4055 | return error; |
4056 | } | |
4d359507 | 4057 | EXPORT_SYMBOL(vfs_unlink); |
1da177e4 LT |
4058 | |
4059 | /* | |
4060 | * Make sure that the actual truncation of the file will occur outside its | |
1b1dcc1b | 4061 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
1da177e4 LT |
4062 | * writeout happening, and we don't want to prevent access to the directory |
4063 | * while waiting on the I/O. | |
4064 | */ | |
da2f1362 | 4065 | long do_unlinkat(int dfd, struct filename *name) |
1da177e4 | 4066 | { |
2ad94ae6 | 4067 | int error; |
1da177e4 | 4068 | struct dentry *dentry; |
f5beed75 AV |
4069 | struct path path; |
4070 | struct qstr last; | |
4071 | int type; | |
1da177e4 | 4072 | struct inode *inode = NULL; |
b21996e3 | 4073 | struct inode *delegated_inode = NULL; |
5d18f813 JL |
4074 | unsigned int lookup_flags = 0; |
4075 | retry: | |
da2f1362 | 4076 | name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
91a27b2a JL |
4077 | if (IS_ERR(name)) |
4078 | return PTR_ERR(name); | |
2ad94ae6 | 4079 | |
1da177e4 | 4080 | error = -EISDIR; |
f5beed75 | 4081 | if (type != LAST_NORM) |
1da177e4 | 4082 | goto exit1; |
0612d9fb | 4083 | |
f5beed75 | 4084 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
4085 | if (error) |
4086 | goto exit1; | |
b21996e3 | 4087 | retry_deleg: |
5955102c | 4088 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
f5beed75 | 4089 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
1da177e4 LT |
4090 | error = PTR_ERR(dentry); |
4091 | if (!IS_ERR(dentry)) { | |
6521f891 CB |
4092 | struct user_namespace *mnt_userns; |
4093 | ||
1da177e4 | 4094 | /* Why not before? Because we want correct error value */ |
f5beed75 | 4095 | if (last.name[last.len]) |
50338b88 | 4096 | goto slashes; |
1da177e4 | 4097 | inode = dentry->d_inode; |
b18825a7 | 4098 | if (d_is_negative(dentry)) |
e6bc45d6 TT |
4099 | goto slashes; |
4100 | ihold(inode); | |
f5beed75 | 4101 | error = security_path_unlink(&path, dentry); |
be6d3e56 | 4102 | if (error) |
c30dabfe | 4103 | goto exit2; |
6521f891 | 4104 | mnt_userns = mnt_user_ns(path.mnt); |
549c7297 CB |
4105 | error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry, |
4106 | &delegated_inode); | |
c30dabfe | 4107 | exit2: |
1da177e4 LT |
4108 | dput(dentry); |
4109 | } | |
5955102c | 4110 | inode_unlock(path.dentry->d_inode); |
1da177e4 LT |
4111 | if (inode) |
4112 | iput(inode); /* truncate the inode here */ | |
b21996e3 BF |
4113 | inode = NULL; |
4114 | if (delegated_inode) { | |
5a14696c | 4115 | error = break_deleg_wait(&delegated_inode); |
b21996e3 BF |
4116 | if (!error) |
4117 | goto retry_deleg; | |
4118 | } | |
f5beed75 | 4119 | mnt_drop_write(path.mnt); |
1da177e4 | 4120 | exit1: |
f5beed75 | 4121 | path_put(&path); |
5d18f813 JL |
4122 | if (retry_estale(error, lookup_flags)) { |
4123 | lookup_flags |= LOOKUP_REVAL; | |
4124 | inode = NULL; | |
4125 | goto retry; | |
4126 | } | |
da2f1362 | 4127 | putname(name); |
1da177e4 LT |
4128 | return error; |
4129 | ||
4130 | slashes: | |
b18825a7 DH |
4131 | if (d_is_negative(dentry)) |
4132 | error = -ENOENT; | |
44b1d530 | 4133 | else if (d_is_dir(dentry)) |
b18825a7 DH |
4134 | error = -EISDIR; |
4135 | else | |
4136 | error = -ENOTDIR; | |
1da177e4 LT |
4137 | goto exit2; |
4138 | } | |
4139 | ||
2e4d0924 | 4140 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
5590ff0d UD |
4141 | { |
4142 | if ((flag & ~AT_REMOVEDIR) != 0) | |
4143 | return -EINVAL; | |
4144 | ||
4145 | if (flag & AT_REMOVEDIR) | |
e24ab0ef | 4146 | return do_rmdir(dfd, getname(pathname)); |
da2f1362 | 4147 | return do_unlinkat(dfd, getname(pathname)); |
5590ff0d UD |
4148 | } |
4149 | ||
3480b257 | 4150 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
5590ff0d | 4151 | { |
da2f1362 | 4152 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
5590ff0d UD |
4153 | } |
4154 | ||
6521f891 CB |
4155 | /** |
4156 | * vfs_symlink - create symlink | |
4157 | * @mnt_userns: user namespace of the mount the inode was found from | |
4158 | * @dir: inode of @dentry | |
4159 | * @dentry: pointer to dentry of the base directory | |
4160 | * @oldname: name of the file to link to | |
4161 | * | |
4162 | * Create a symlink. | |
4163 | * | |
4164 | * If the inode has been found through an idmapped mount the user namespace of | |
4165 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
4166 | * care to map the inode according to @mnt_userns before checking permissions. | |
4167 | * On non-idmapped mounts or if permission checking is to be performed on the | |
4168 | * raw inode simply passs init_user_ns. | |
4169 | */ | |
4170 | int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, | |
4171 | struct dentry *dentry, const char *oldname) | |
1da177e4 | 4172 | { |
6521f891 | 4173 | int error = may_create(mnt_userns, dir, dentry); |
1da177e4 LT |
4174 | |
4175 | if (error) | |
4176 | return error; | |
4177 | ||
acfa4380 | 4178 | if (!dir->i_op->symlink) |
1da177e4 LT |
4179 | return -EPERM; |
4180 | ||
4181 | error = security_inode_symlink(dir, dentry, oldname); | |
4182 | if (error) | |
4183 | return error; | |
4184 | ||
549c7297 | 4185 | error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname); |
a74574aa | 4186 | if (!error) |
f38aa942 | 4187 | fsnotify_create(dir, dentry); |
1da177e4 LT |
4188 | return error; |
4189 | } | |
4d359507 | 4190 | EXPORT_SYMBOL(vfs_symlink); |
1da177e4 | 4191 | |
cd3acb6a | 4192 | static long do_symlinkat(const char __user *oldname, int newdfd, |
b724e846 | 4193 | const char __user *newname) |
1da177e4 | 4194 | { |
2ad94ae6 | 4195 | int error; |
91a27b2a | 4196 | struct filename *from; |
6902d925 | 4197 | struct dentry *dentry; |
dae6ad8f | 4198 | struct path path; |
f46d3567 | 4199 | unsigned int lookup_flags = 0; |
1da177e4 LT |
4200 | |
4201 | from = getname(oldname); | |
2ad94ae6 | 4202 | if (IS_ERR(from)) |
1da177e4 | 4203 | return PTR_ERR(from); |
f46d3567 JL |
4204 | retry: |
4205 | dentry = user_path_create(newdfd, newname, &path, lookup_flags); | |
6902d925 DH |
4206 | error = PTR_ERR(dentry); |
4207 | if (IS_ERR(dentry)) | |
dae6ad8f | 4208 | goto out_putname; |
6902d925 | 4209 | |
91a27b2a | 4210 | error = security_path_symlink(&path, dentry, from->name); |
6521f891 CB |
4211 | if (!error) { |
4212 | struct user_namespace *mnt_userns; | |
4213 | ||
4214 | mnt_userns = mnt_user_ns(path.mnt); | |
4215 | error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry, | |
4216 | from->name); | |
4217 | } | |
921a1650 | 4218 | done_path_create(&path, dentry); |
f46d3567 JL |
4219 | if (retry_estale(error, lookup_flags)) { |
4220 | lookup_flags |= LOOKUP_REVAL; | |
4221 | goto retry; | |
4222 | } | |
6902d925 | 4223 | out_putname: |
1da177e4 LT |
4224 | putname(from); |
4225 | return error; | |
4226 | } | |
4227 | ||
b724e846 DB |
4228 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
4229 | int, newdfd, const char __user *, newname) | |
4230 | { | |
4231 | return do_symlinkat(oldname, newdfd, newname); | |
4232 | } | |
4233 | ||
3480b257 | 4234 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4235 | { |
b724e846 | 4236 | return do_symlinkat(oldname, AT_FDCWD, newname); |
5590ff0d UD |
4237 | } |
4238 | ||
146a8595 BF |
4239 | /** |
4240 | * vfs_link - create a new link | |
4241 | * @old_dentry: object to be linked | |
6521f891 | 4242 | * @mnt_userns: the user namespace of the mount |
146a8595 BF |
4243 | * @dir: new parent |
4244 | * @new_dentry: where to create the new link | |
4245 | * @delegated_inode: returns inode needing a delegation break | |
4246 | * | |
4247 | * The caller must hold dir->i_mutex | |
4248 | * | |
4249 | * If vfs_link discovers a delegation on the to-be-linked file in need | |
4250 | * of breaking, it will return -EWOULDBLOCK and return a reference to the | |
4251 | * inode in delegated_inode. The caller should then break the delegation | |
4252 | * and retry. Because breaking a delegation may take a long time, the | |
4253 | * caller should drop the i_mutex before doing so. | |
4254 | * | |
4255 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4256 | * be appropriate for callers that expect the underlying filesystem not | |
4257 | * to be NFS exported. | |
6521f891 CB |
4258 | * |
4259 | * If the inode has been found through an idmapped mount the user namespace of | |
4260 | * the vfsmount must be passed through @mnt_userns. This function will then take | |
4261 | * care to map the inode according to @mnt_userns before checking permissions. | |
4262 | * On non-idmapped mounts or if permission checking is to be performed on the | |
4263 | * raw inode simply passs init_user_ns. | |
146a8595 | 4264 | */ |
6521f891 CB |
4265 | int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns, |
4266 | struct inode *dir, struct dentry *new_dentry, | |
4267 | struct inode **delegated_inode) | |
1da177e4 LT |
4268 | { |
4269 | struct inode *inode = old_dentry->d_inode; | |
8de52778 | 4270 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
4271 | int error; |
4272 | ||
4273 | if (!inode) | |
4274 | return -ENOENT; | |
4275 | ||
6521f891 | 4276 | error = may_create(mnt_userns, dir, new_dentry); |
1da177e4 LT |
4277 | if (error) |
4278 | return error; | |
4279 | ||
4280 | if (dir->i_sb != inode->i_sb) | |
4281 | return -EXDEV; | |
4282 | ||
4283 | /* | |
4284 | * A link to an append-only or immutable file cannot be created. | |
4285 | */ | |
4286 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
4287 | return -EPERM; | |
0bd23d09 EB |
4288 | /* |
4289 | * Updating the link count will likely cause i_uid and i_gid to | |
4290 | * be writen back improperly if their true value is unknown to | |
4291 | * the vfs. | |
4292 | */ | |
6521f891 | 4293 | if (HAS_UNMAPPED_ID(mnt_userns, inode)) |
0bd23d09 | 4294 | return -EPERM; |
acfa4380 | 4295 | if (!dir->i_op->link) |
1da177e4 | 4296 | return -EPERM; |
7e79eedb | 4297 | if (S_ISDIR(inode->i_mode)) |
1da177e4 LT |
4298 | return -EPERM; |
4299 | ||
4300 | error = security_inode_link(old_dentry, dir, new_dentry); | |
4301 | if (error) | |
4302 | return error; | |
4303 | ||
5955102c | 4304 | inode_lock(inode); |
aae8a97d | 4305 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
f4e0c30c | 4306 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
aae8a97d | 4307 | error = -ENOENT; |
8de52778 AV |
4308 | else if (max_links && inode->i_nlink >= max_links) |
4309 | error = -EMLINK; | |
146a8595 BF |
4310 | else { |
4311 | error = try_break_deleg(inode, delegated_inode); | |
4312 | if (!error) | |
4313 | error = dir->i_op->link(old_dentry, dir, new_dentry); | |
4314 | } | |
f4e0c30c AV |
4315 | |
4316 | if (!error && (inode->i_state & I_LINKABLE)) { | |
4317 | spin_lock(&inode->i_lock); | |
4318 | inode->i_state &= ~I_LINKABLE; | |
4319 | spin_unlock(&inode->i_lock); | |
4320 | } | |
5955102c | 4321 | inode_unlock(inode); |
e31e14ec | 4322 | if (!error) |
7e79eedb | 4323 | fsnotify_link(dir, inode, new_dentry); |
1da177e4 LT |
4324 | return error; |
4325 | } | |
4d359507 | 4326 | EXPORT_SYMBOL(vfs_link); |
1da177e4 LT |
4327 | |
4328 | /* | |
4329 | * Hardlinks are often used in delicate situations. We avoid | |
4330 | * security-related surprises by not following symlinks on the | |
4331 | * newname. --KAB | |
4332 | * | |
4333 | * We don't follow them on the oldname either to be compatible | |
4334 | * with linux 2.0, and to avoid hard-linking to directories | |
4335 | * and other special files. --ADM | |
4336 | */ | |
812931d6 | 4337 | static int do_linkat(int olddfd, const char __user *oldname, int newdfd, |
46ea89eb | 4338 | const char __user *newname, int flags) |
1da177e4 | 4339 | { |
6521f891 | 4340 | struct user_namespace *mnt_userns; |
1da177e4 | 4341 | struct dentry *new_dentry; |
dae6ad8f | 4342 | struct path old_path, new_path; |
146a8595 | 4343 | struct inode *delegated_inode = NULL; |
11a7b371 | 4344 | int how = 0; |
1da177e4 | 4345 | int error; |
1da177e4 | 4346 | |
11a7b371 | 4347 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
c04030e1 | 4348 | return -EINVAL; |
11a7b371 | 4349 | /* |
f0cc6ffb LT |
4350 | * To use null names we require CAP_DAC_READ_SEARCH |
4351 | * This ensures that not everyone will be able to create | |
4352 | * handlink using the passed filedescriptor. | |
11a7b371 | 4353 | */ |
f0cc6ffb LT |
4354 | if (flags & AT_EMPTY_PATH) { |
4355 | if (!capable(CAP_DAC_READ_SEARCH)) | |
4356 | return -ENOENT; | |
11a7b371 | 4357 | how = LOOKUP_EMPTY; |
f0cc6ffb | 4358 | } |
11a7b371 AK |
4359 | |
4360 | if (flags & AT_SYMLINK_FOLLOW) | |
4361 | how |= LOOKUP_FOLLOW; | |
442e31ca | 4362 | retry: |
11a7b371 | 4363 | error = user_path_at(olddfd, oldname, how, &old_path); |
1da177e4 | 4364 | if (error) |
2ad94ae6 AV |
4365 | return error; |
4366 | ||
442e31ca JL |
4367 | new_dentry = user_path_create(newdfd, newname, &new_path, |
4368 | (how & LOOKUP_REVAL)); | |
1da177e4 | 4369 | error = PTR_ERR(new_dentry); |
6902d925 | 4370 | if (IS_ERR(new_dentry)) |
dae6ad8f AV |
4371 | goto out; |
4372 | ||
4373 | error = -EXDEV; | |
4374 | if (old_path.mnt != new_path.mnt) | |
4375 | goto out_dput; | |
549c7297 CB |
4376 | mnt_userns = mnt_user_ns(new_path.mnt); |
4377 | error = may_linkat(mnt_userns, &old_path); | |
800179c9 KC |
4378 | if (unlikely(error)) |
4379 | goto out_dput; | |
dae6ad8f | 4380 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
be6d3e56 | 4381 | if (error) |
a8104a9f | 4382 | goto out_dput; |
6521f891 CB |
4383 | error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode, |
4384 | new_dentry, &delegated_inode); | |
75c3f29d | 4385 | out_dput: |
921a1650 | 4386 | done_path_create(&new_path, new_dentry); |
146a8595 BF |
4387 | if (delegated_inode) { |
4388 | error = break_deleg_wait(&delegated_inode); | |
d22e6338 OD |
4389 | if (!error) { |
4390 | path_put(&old_path); | |
146a8595 | 4391 | goto retry; |
d22e6338 | 4392 | } |
146a8595 | 4393 | } |
442e31ca | 4394 | if (retry_estale(error, how)) { |
d22e6338 | 4395 | path_put(&old_path); |
442e31ca JL |
4396 | how |= LOOKUP_REVAL; |
4397 | goto retry; | |
4398 | } | |
1da177e4 | 4399 | out: |
2d8f3038 | 4400 | path_put(&old_path); |
1da177e4 LT |
4401 | |
4402 | return error; | |
4403 | } | |
4404 | ||
46ea89eb DB |
4405 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
4406 | int, newdfd, const char __user *, newname, int, flags) | |
4407 | { | |
4408 | return do_linkat(olddfd, oldname, newdfd, newname, flags); | |
4409 | } | |
4410 | ||
3480b257 | 4411 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4412 | { |
46ea89eb | 4413 | return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
4414 | } |
4415 | ||
bc27027a MS |
4416 | /** |
4417 | * vfs_rename - rename a filesystem object | |
2111c3c0 | 4418 | * @rd: pointer to &struct renamedata info |
bc27027a MS |
4419 | * |
4420 | * The caller must hold multiple mutexes--see lock_rename()). | |
4421 | * | |
4422 | * If vfs_rename discovers a delegation in need of breaking at either | |
4423 | * the source or destination, it will return -EWOULDBLOCK and return a | |
4424 | * reference to the inode in delegated_inode. The caller should then | |
4425 | * break the delegation and retry. Because breaking a delegation may | |
4426 | * take a long time, the caller should drop all locks before doing | |
4427 | * so. | |
4428 | * | |
4429 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4430 | * be appropriate for callers that expect the underlying filesystem not | |
4431 | * to be NFS exported. | |
4432 | * | |
1da177e4 LT |
4433 | * The worst of all namespace operations - renaming directory. "Perverted" |
4434 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | |
4435 | * Problems: | |
0117d427 | 4436 | * |
d03b29a2 | 4437 | * a) we can get into loop creation. |
1da177e4 LT |
4438 | * b) race potential - two innocent renames can create a loop together. |
4439 | * That's where 4.4 screws up. Current fix: serialization on | |
a11f3a05 | 4440 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
1da177e4 | 4441 | * story. |
6cedba89 BF |
4442 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
4443 | * and source (if it is not a directory). | |
1b1dcc1b | 4444 | * And that - after we got ->i_mutex on parents (until then we don't know |
1da177e4 LT |
4445 | * whether the target exists). Solution: try to be smart with locking |
4446 | * order for inodes. We rely on the fact that tree topology may change | |
a11f3a05 | 4447 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
1da177e4 LT |
4448 | * move will be locked. Thus we can rank directories by the tree |
4449 | * (ancestors first) and rank all non-directories after them. | |
4450 | * That works since everybody except rename does "lock parent, lookup, | |
a11f3a05 | 4451 | * lock child" and rename is under ->s_vfs_rename_mutex. |
1da177e4 LT |
4452 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
4453 | * has no more than 1 dentry. If "hybrid" objects will ever appear, | |
4454 | * we'd better make sure that there's no link(2) for them. | |
e4eaac06 | 4455 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
1b1dcc1b | 4456 | * we are removing the target. Solution: we will have to grab ->i_mutex |
1da177e4 | 4457 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
c41b20e7 | 4458 | * ->i_mutex on parents, which works but leads to some truly excessive |
1da177e4 LT |
4459 | * locking]. |
4460 | */ | |
9fe61450 | 4461 | int vfs_rename(struct renamedata *rd) |
1da177e4 | 4462 | { |
bc27027a | 4463 | int error; |
9fe61450 CB |
4464 | struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; |
4465 | struct dentry *old_dentry = rd->old_dentry; | |
4466 | struct dentry *new_dentry = rd->new_dentry; | |
4467 | struct inode **delegated_inode = rd->delegated_inode; | |
4468 | unsigned int flags = rd->flags; | |
bc27027a | 4469 | bool is_dir = d_is_dir(old_dentry); |
bc27027a | 4470 | struct inode *source = old_dentry->d_inode; |
9055cba7 | 4471 | struct inode *target = new_dentry->d_inode; |
da1ce067 MS |
4472 | bool new_is_dir = false; |
4473 | unsigned max_links = new_dir->i_sb->s_max_links; | |
49d31c2f | 4474 | struct name_snapshot old_name; |
bc27027a | 4475 | |
8d3e2936 | 4476 | if (source == target) |
bc27027a MS |
4477 | return 0; |
4478 | ||
6521f891 | 4479 | error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir); |
bc27027a MS |
4480 | if (error) |
4481 | return error; | |
4482 | ||
da1ce067 | 4483 | if (!target) { |
6521f891 | 4484 | error = may_create(rd->new_mnt_userns, new_dir, new_dentry); |
da1ce067 MS |
4485 | } else { |
4486 | new_is_dir = d_is_dir(new_dentry); | |
4487 | ||
4488 | if (!(flags & RENAME_EXCHANGE)) | |
6521f891 CB |
4489 | error = may_delete(rd->new_mnt_userns, new_dir, |
4490 | new_dentry, is_dir); | |
da1ce067 | 4491 | else |
6521f891 CB |
4492 | error = may_delete(rd->new_mnt_userns, new_dir, |
4493 | new_dentry, new_is_dir); | |
da1ce067 | 4494 | } |
bc27027a MS |
4495 | if (error) |
4496 | return error; | |
4497 | ||
2773bf00 | 4498 | if (!old_dir->i_op->rename) |
bc27027a | 4499 | return -EPERM; |
1da177e4 LT |
4500 | |
4501 | /* | |
4502 | * If we are going to change the parent - check write permissions, | |
4503 | * we'll need to flip '..'. | |
4504 | */ | |
da1ce067 MS |
4505 | if (new_dir != old_dir) { |
4506 | if (is_dir) { | |
6521f891 | 4507 | error = inode_permission(rd->old_mnt_userns, source, |
47291baa | 4508 | MAY_WRITE); |
da1ce067 MS |
4509 | if (error) |
4510 | return error; | |
4511 | } | |
4512 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { | |
6521f891 | 4513 | error = inode_permission(rd->new_mnt_userns, target, |
47291baa | 4514 | MAY_WRITE); |
da1ce067 MS |
4515 | if (error) |
4516 | return error; | |
4517 | } | |
1da177e4 LT |
4518 | } |
4519 | ||
0b3974eb MS |
4520 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
4521 | flags); | |
1da177e4 LT |
4522 | if (error) |
4523 | return error; | |
4524 | ||
49d31c2f | 4525 | take_dentry_name_snapshot(&old_name, old_dentry); |
1d2ef590 | 4526 | dget(new_dentry); |
da1ce067 | 4527 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4528 | lock_two_nondirectories(source, target); |
4529 | else if (target) | |
5955102c | 4530 | inode_lock(target); |
9055cba7 SW |
4531 | |
4532 | error = -EBUSY; | |
7af1364f | 4533 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
9055cba7 SW |
4534 | goto out; |
4535 | ||
da1ce067 | 4536 | if (max_links && new_dir != old_dir) { |
bc27027a | 4537 | error = -EMLINK; |
da1ce067 | 4538 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
bc27027a | 4539 | goto out; |
da1ce067 MS |
4540 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
4541 | old_dir->i_nlink >= max_links) | |
4542 | goto out; | |
4543 | } | |
da1ce067 | 4544 | if (!is_dir) { |
bc27027a | 4545 | error = try_break_deleg(source, delegated_inode); |
8e6d782c BF |
4546 | if (error) |
4547 | goto out; | |
da1ce067 MS |
4548 | } |
4549 | if (target && !new_is_dir) { | |
4550 | error = try_break_deleg(target, delegated_inode); | |
4551 | if (error) | |
4552 | goto out; | |
8e6d782c | 4553 | } |
549c7297 CB |
4554 | error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry, |
4555 | new_dir, new_dentry, flags); | |
51892bbb SW |
4556 | if (error) |
4557 | goto out; | |
4558 | ||
da1ce067 | 4559 | if (!(flags & RENAME_EXCHANGE) && target) { |
8767712f AV |
4560 | if (is_dir) { |
4561 | shrink_dcache_parent(new_dentry); | |
bc27027a | 4562 | target->i_flags |= S_DEAD; |
8767712f | 4563 | } |
51892bbb | 4564 | dont_mount(new_dentry); |
8ed936b5 | 4565 | detach_mounts(new_dentry); |
bc27027a | 4566 | } |
da1ce067 MS |
4567 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
4568 | if (!(flags & RENAME_EXCHANGE)) | |
4569 | d_move(old_dentry, new_dentry); | |
4570 | else | |
4571 | d_exchange(old_dentry, new_dentry); | |
4572 | } | |
51892bbb | 4573 | out: |
da1ce067 | 4574 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4575 | unlock_two_nondirectories(source, target); |
4576 | else if (target) | |
5955102c | 4577 | inode_unlock(target); |
1da177e4 | 4578 | dput(new_dentry); |
da1ce067 | 4579 | if (!error) { |
f4ec3a3d | 4580 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
da1ce067 MS |
4581 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
4582 | if (flags & RENAME_EXCHANGE) { | |
f4ec3a3d | 4583 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
da1ce067 MS |
4584 | new_is_dir, NULL, new_dentry); |
4585 | } | |
4586 | } | |
49d31c2f | 4587 | release_dentry_name_snapshot(&old_name); |
0eeca283 | 4588 | |
1da177e4 LT |
4589 | return error; |
4590 | } | |
4d359507 | 4591 | EXPORT_SYMBOL(vfs_rename); |
1da177e4 | 4592 | |
e886663c JA |
4593 | int do_renameat2(int olddfd, struct filename *from, int newdfd, |
4594 | struct filename *to, unsigned int flags) | |
1da177e4 | 4595 | { |
9fe61450 | 4596 | struct renamedata rd; |
2ad94ae6 AV |
4597 | struct dentry *old_dentry, *new_dentry; |
4598 | struct dentry *trap; | |
f5beed75 AV |
4599 | struct path old_path, new_path; |
4600 | struct qstr old_last, new_last; | |
4601 | int old_type, new_type; | |
8e6d782c | 4602 | struct inode *delegated_inode = NULL; |
f5beed75 | 4603 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
c6a94284 | 4604 | bool should_retry = false; |
e886663c | 4605 | int error = -EINVAL; |
520c8b16 | 4606 | |
0d7a8555 | 4607 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
e886663c | 4608 | goto put_both; |
da1ce067 | 4609 | |
0d7a8555 MS |
4610 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
4611 | (flags & RENAME_EXCHANGE)) | |
e886663c | 4612 | goto put_both; |
520c8b16 | 4613 | |
f5beed75 AV |
4614 | if (flags & RENAME_EXCHANGE) |
4615 | target_flags = 0; | |
4616 | ||
c6a94284 | 4617 | retry: |
e886663c JA |
4618 | from = filename_parentat(olddfd, from, lookup_flags, &old_path, |
4619 | &old_last, &old_type); | |
91a27b2a JL |
4620 | if (IS_ERR(from)) { |
4621 | error = PTR_ERR(from); | |
e886663c | 4622 | goto put_new; |
91a27b2a | 4623 | } |
1da177e4 | 4624 | |
e886663c JA |
4625 | to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, |
4626 | &new_type); | |
91a27b2a JL |
4627 | if (IS_ERR(to)) { |
4628 | error = PTR_ERR(to); | |
1da177e4 | 4629 | goto exit1; |
91a27b2a | 4630 | } |
1da177e4 LT |
4631 | |
4632 | error = -EXDEV; | |
f5beed75 | 4633 | if (old_path.mnt != new_path.mnt) |
1da177e4 LT |
4634 | goto exit2; |
4635 | ||
1da177e4 | 4636 | error = -EBUSY; |
f5beed75 | 4637 | if (old_type != LAST_NORM) |
1da177e4 LT |
4638 | goto exit2; |
4639 | ||
0a7c3937 MS |
4640 | if (flags & RENAME_NOREPLACE) |
4641 | error = -EEXIST; | |
f5beed75 | 4642 | if (new_type != LAST_NORM) |
1da177e4 LT |
4643 | goto exit2; |
4644 | ||
f5beed75 | 4645 | error = mnt_want_write(old_path.mnt); |
c30dabfe JK |
4646 | if (error) |
4647 | goto exit2; | |
4648 | ||
8e6d782c | 4649 | retry_deleg: |
f5beed75 | 4650 | trap = lock_rename(new_path.dentry, old_path.dentry); |
1da177e4 | 4651 | |
f5beed75 | 4652 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
1da177e4 LT |
4653 | error = PTR_ERR(old_dentry); |
4654 | if (IS_ERR(old_dentry)) | |
4655 | goto exit3; | |
4656 | /* source must exist */ | |
4657 | error = -ENOENT; | |
b18825a7 | 4658 | if (d_is_negative(old_dentry)) |
1da177e4 | 4659 | goto exit4; |
f5beed75 | 4660 | new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); |
0a7c3937 MS |
4661 | error = PTR_ERR(new_dentry); |
4662 | if (IS_ERR(new_dentry)) | |
4663 | goto exit4; | |
4664 | error = -EEXIST; | |
4665 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) | |
4666 | goto exit5; | |
da1ce067 MS |
4667 | if (flags & RENAME_EXCHANGE) { |
4668 | error = -ENOENT; | |
4669 | if (d_is_negative(new_dentry)) | |
4670 | goto exit5; | |
4671 | ||
4672 | if (!d_is_dir(new_dentry)) { | |
4673 | error = -ENOTDIR; | |
f5beed75 | 4674 | if (new_last.name[new_last.len]) |
da1ce067 MS |
4675 | goto exit5; |
4676 | } | |
4677 | } | |
1da177e4 | 4678 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
44b1d530 | 4679 | if (!d_is_dir(old_dentry)) { |
1da177e4 | 4680 | error = -ENOTDIR; |
f5beed75 | 4681 | if (old_last.name[old_last.len]) |
0a7c3937 | 4682 | goto exit5; |
f5beed75 | 4683 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
0a7c3937 | 4684 | goto exit5; |
1da177e4 LT |
4685 | } |
4686 | /* source should not be ancestor of target */ | |
4687 | error = -EINVAL; | |
4688 | if (old_dentry == trap) | |
0a7c3937 | 4689 | goto exit5; |
1da177e4 | 4690 | /* target should not be an ancestor of source */ |
da1ce067 MS |
4691 | if (!(flags & RENAME_EXCHANGE)) |
4692 | error = -ENOTEMPTY; | |
1da177e4 LT |
4693 | if (new_dentry == trap) |
4694 | goto exit5; | |
4695 | ||
f5beed75 AV |
4696 | error = security_path_rename(&old_path, old_dentry, |
4697 | &new_path, new_dentry, flags); | |
be6d3e56 | 4698 | if (error) |
c30dabfe | 4699 | goto exit5; |
9fe61450 CB |
4700 | |
4701 | rd.old_dir = old_path.dentry->d_inode; | |
4702 | rd.old_dentry = old_dentry; | |
6521f891 | 4703 | rd.old_mnt_userns = mnt_user_ns(old_path.mnt); |
9fe61450 CB |
4704 | rd.new_dir = new_path.dentry->d_inode; |
4705 | rd.new_dentry = new_dentry; | |
6521f891 | 4706 | rd.new_mnt_userns = mnt_user_ns(new_path.mnt); |
9fe61450 CB |
4707 | rd.delegated_inode = &delegated_inode; |
4708 | rd.flags = flags; | |
4709 | error = vfs_rename(&rd); | |
1da177e4 LT |
4710 | exit5: |
4711 | dput(new_dentry); | |
4712 | exit4: | |
4713 | dput(old_dentry); | |
4714 | exit3: | |
f5beed75 | 4715 | unlock_rename(new_path.dentry, old_path.dentry); |
8e6d782c BF |
4716 | if (delegated_inode) { |
4717 | error = break_deleg_wait(&delegated_inode); | |
4718 | if (!error) | |
4719 | goto retry_deleg; | |
4720 | } | |
f5beed75 | 4721 | mnt_drop_write(old_path.mnt); |
1da177e4 | 4722 | exit2: |
c6a94284 JL |
4723 | if (retry_estale(error, lookup_flags)) |
4724 | should_retry = true; | |
f5beed75 | 4725 | path_put(&new_path); |
1da177e4 | 4726 | exit1: |
f5beed75 | 4727 | path_put(&old_path); |
c6a94284 JL |
4728 | if (should_retry) { |
4729 | should_retry = false; | |
4730 | lookup_flags |= LOOKUP_REVAL; | |
4731 | goto retry; | |
4732 | } | |
e886663c | 4733 | put_both: |
91ef658f | 4734 | putname(from); |
e886663c | 4735 | put_new: |
91ef658f | 4736 | putname(to); |
1da177e4 LT |
4737 | return error; |
4738 | } | |
4739 | ||
ee81feb6 DB |
4740 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
4741 | int, newdfd, const char __user *, newname, unsigned int, flags) | |
4742 | { | |
e886663c JA |
4743 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
4744 | flags); | |
ee81feb6 DB |
4745 | } |
4746 | ||
520c8b16 MS |
4747 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
4748 | int, newdfd, const char __user *, newname) | |
4749 | { | |
e886663c JA |
4750 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
4751 | 0); | |
520c8b16 MS |
4752 | } |
4753 | ||
a26eab24 | 4754 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4755 | { |
e886663c JA |
4756 | return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, |
4757 | getname(newname), 0); | |
5590ff0d UD |
4758 | } |
4759 | ||
5d826c84 | 4760 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
1da177e4 | 4761 | { |
5d826c84 | 4762 | int len = PTR_ERR(link); |
1da177e4 LT |
4763 | if (IS_ERR(link)) |
4764 | goto out; | |
4765 | ||
4766 | len = strlen(link); | |
4767 | if (len > (unsigned) buflen) | |
4768 | len = buflen; | |
4769 | if (copy_to_user(buffer, link, len)) | |
4770 | len = -EFAULT; | |
4771 | out: | |
4772 | return len; | |
4773 | } | |
4774 | ||
fd4a0edf MS |
4775 | /** |
4776 | * vfs_readlink - copy symlink body into userspace buffer | |
4777 | * @dentry: dentry on which to get symbolic link | |
4778 | * @buffer: user memory pointer | |
4779 | * @buflen: size of buffer | |
4780 | * | |
4781 | * Does not touch atime. That's up to the caller if necessary | |
4782 | * | |
4783 | * Does not call security hook. | |
4784 | */ | |
4785 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
4786 | { | |
4787 | struct inode *inode = d_inode(dentry); | |
f2df5da6 AV |
4788 | DEFINE_DELAYED_CALL(done); |
4789 | const char *link; | |
4790 | int res; | |
fd4a0edf | 4791 | |
76fca90e MS |
4792 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
4793 | if (unlikely(inode->i_op->readlink)) | |
4794 | return inode->i_op->readlink(dentry, buffer, buflen); | |
4795 | ||
4796 | if (!d_is_symlink(dentry)) | |
4797 | return -EINVAL; | |
4798 | ||
4799 | spin_lock(&inode->i_lock); | |
4800 | inode->i_opflags |= IOP_DEFAULT_READLINK; | |
4801 | spin_unlock(&inode->i_lock); | |
4802 | } | |
fd4a0edf | 4803 | |
4c4f7c19 | 4804 | link = READ_ONCE(inode->i_link); |
f2df5da6 AV |
4805 | if (!link) { |
4806 | link = inode->i_op->get_link(dentry, inode, &done); | |
4807 | if (IS_ERR(link)) | |
4808 | return PTR_ERR(link); | |
4809 | } | |
4810 | res = readlink_copy(buffer, buflen, link); | |
4811 | do_delayed_call(&done); | |
4812 | return res; | |
fd4a0edf MS |
4813 | } |
4814 | EXPORT_SYMBOL(vfs_readlink); | |
1da177e4 | 4815 | |
d60874cd MS |
4816 | /** |
4817 | * vfs_get_link - get symlink body | |
4818 | * @dentry: dentry on which to get symbolic link | |
4819 | * @done: caller needs to free returned data with this | |
4820 | * | |
4821 | * Calls security hook and i_op->get_link() on the supplied inode. | |
4822 | * | |
4823 | * It does not touch atime. That's up to the caller if necessary. | |
4824 | * | |
4825 | * Does not work on "special" symlinks like /proc/$$/fd/N | |
4826 | */ | |
4827 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) | |
4828 | { | |
4829 | const char *res = ERR_PTR(-EINVAL); | |
4830 | struct inode *inode = d_inode(dentry); | |
4831 | ||
4832 | if (d_is_symlink(dentry)) { | |
4833 | res = ERR_PTR(security_inode_readlink(dentry)); | |
4834 | if (!res) | |
4835 | res = inode->i_op->get_link(dentry, inode, done); | |
4836 | } | |
4837 | return res; | |
4838 | } | |
4839 | EXPORT_SYMBOL(vfs_get_link); | |
4840 | ||
1da177e4 | 4841 | /* get the link contents into pagecache */ |
6b255391 | 4842 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
fceef393 | 4843 | struct delayed_call *callback) |
1da177e4 | 4844 | { |
ebd09abb DG |
4845 | char *kaddr; |
4846 | struct page *page; | |
6b255391 AV |
4847 | struct address_space *mapping = inode->i_mapping; |
4848 | ||
d3883d4f AV |
4849 | if (!dentry) { |
4850 | page = find_get_page(mapping, 0); | |
4851 | if (!page) | |
4852 | return ERR_PTR(-ECHILD); | |
4853 | if (!PageUptodate(page)) { | |
4854 | put_page(page); | |
4855 | return ERR_PTR(-ECHILD); | |
4856 | } | |
4857 | } else { | |
4858 | page = read_mapping_page(mapping, 0, NULL); | |
4859 | if (IS_ERR(page)) | |
4860 | return (char*)page; | |
4861 | } | |
fceef393 | 4862 | set_delayed_call(callback, page_put_link, page); |
21fc61c7 AV |
4863 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
4864 | kaddr = page_address(page); | |
6b255391 | 4865 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
ebd09abb | 4866 | return kaddr; |
1da177e4 LT |
4867 | } |
4868 | ||
6b255391 | 4869 | EXPORT_SYMBOL(page_get_link); |
1da177e4 | 4870 | |
fceef393 | 4871 | void page_put_link(void *arg) |
1da177e4 | 4872 | { |
fceef393 | 4873 | put_page(arg); |
1da177e4 | 4874 | } |
4d359507 | 4875 | EXPORT_SYMBOL(page_put_link); |
1da177e4 | 4876 | |
aa80deab AV |
4877 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
4878 | { | |
fceef393 | 4879 | DEFINE_DELAYED_CALL(done); |
6b255391 AV |
4880 | int res = readlink_copy(buffer, buflen, |
4881 | page_get_link(dentry, d_inode(dentry), | |
fceef393 AV |
4882 | &done)); |
4883 | do_delayed_call(&done); | |
aa80deab AV |
4884 | return res; |
4885 | } | |
4886 | EXPORT_SYMBOL(page_readlink); | |
4887 | ||
54566b2c NP |
4888 | /* |
4889 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS | |
4890 | */ | |
4891 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) | |
1da177e4 LT |
4892 | { |
4893 | struct address_space *mapping = inode->i_mapping; | |
0adb25d2 | 4894 | struct page *page; |
afddba49 | 4895 | void *fsdata; |
beb497ab | 4896 | int err; |
c718a975 | 4897 | unsigned int flags = 0; |
54566b2c NP |
4898 | if (nofs) |
4899 | flags |= AOP_FLAG_NOFS; | |
1da177e4 | 4900 | |
7e53cac4 | 4901 | retry: |
afddba49 | 4902 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
54566b2c | 4903 | flags, &page, &fsdata); |
1da177e4 | 4904 | if (err) |
afddba49 NP |
4905 | goto fail; |
4906 | ||
21fc61c7 | 4907 | memcpy(page_address(page), symname, len-1); |
afddba49 NP |
4908 | |
4909 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, | |
4910 | page, fsdata); | |
1da177e4 LT |
4911 | if (err < 0) |
4912 | goto fail; | |
afddba49 NP |
4913 | if (err < len-1) |
4914 | goto retry; | |
4915 | ||
1da177e4 LT |
4916 | mark_inode_dirty(inode); |
4917 | return 0; | |
1da177e4 LT |
4918 | fail: |
4919 | return err; | |
4920 | } | |
4d359507 | 4921 | EXPORT_SYMBOL(__page_symlink); |
1da177e4 | 4922 | |
0adb25d2 KK |
4923 | int page_symlink(struct inode *inode, const char *symname, int len) |
4924 | { | |
4925 | return __page_symlink(inode, symname, len, | |
c62d2555 | 4926 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
0adb25d2 | 4927 | } |
4d359507 | 4928 | EXPORT_SYMBOL(page_symlink); |
0adb25d2 | 4929 | |
92e1d5be | 4930 | const struct inode_operations page_symlink_inode_operations = { |
6b255391 | 4931 | .get_link = page_get_link, |
1da177e4 | 4932 | }; |
1da177e4 | 4933 | EXPORT_SYMBOL(page_symlink_inode_operations); |