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