man/: ffix
[man-pages.git] / man / man7 / path_resolution.7
blob8cb369a0db9b5a33e42e28b55b6af8db788e134a
1 .\" Copyright (C) 2003 Andries Brouwer (aeb@cwi.nl)
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH path_resolution 7 (date) "Linux man-pages (unreleased)"
6 .SH NAME
7 path_resolution \- how a pathname is resolved to a file
8 .SH DESCRIPTION
9 Some UNIX/Linux system calls have as parameter one or more filenames.
10 A filename (or pathname) is resolved as follows.
11 .SS Step 1: start of the resolution process
12 If the pathname starts with the \[aq]/\[aq] character, the starting lookup
13 directory is the root directory of the calling process.
14 A process inherits its root directory from its parent.
15 Usually this will be the root directory of the file hierarchy.
16 A process may get a different root directory by use of the
17 .BR chroot (2)
18 system call, or may temporarily use a different root directory by using
19 .BR openat2 (2)
20 with the
21 .B RESOLVE_IN_ROOT
22 flag set.
24 A process may get an entirely private mount namespace in case
25 it\[em]or one of its ancestors\[em]was started by an invocation of the
26 .BR clone (2)
27 system call that had the
28 .B CLONE_NEWNS
29 flag set.
30 This handles the \[aq]/\[aq] part of the pathname.
32 If the pathname does not start with the \[aq]/\[aq] character, the starting
33 lookup directory of the resolution process is the current working directory of
34 the process \[em] or in the case of
35 .BR openat (2)-style
36 system calls, the
37 .I dfd
38 argument (or the current working directory if
39 .B AT_FDCWD
40 is passed as the
41 .I dfd
42 argument).
43 The current working directory is inherited from the parent, and can
44 be changed by use of the
45 .BR chdir (2)
46 system call.
48 Pathnames starting with a \[aq]/\[aq] character are called absolute pathnames.
49 Pathnames not starting with a \[aq]/\[aq] are called relative pathnames.
50 .SS Step 2: walk along the path
51 Set the current lookup directory to the starting lookup directory.
52 Now, for each nonfinal component of the pathname, where a component
53 is a substring delimited by \[aq]/\[aq] characters, this component is looked up
54 in the current lookup directory.
56 If the process does not have search permission on
57 the current lookup directory,
59 .B EACCES
60 error is returned ("Permission denied").
62 If the component is not found, an
63 .B ENOENT
64 error is returned
65 ("No such file or directory").
67 If the component is found, but is neither a directory nor a symbolic link,
69 .B ENOTDIR
70 error is returned ("Not a directory").
72 If the component is found and is a directory, we set the
73 current lookup directory to that directory, and go to the
74 next component.
76 If the component is found and is a symbolic link,
77 we first resolve this symbolic link
78 (with the current lookup directory
79 as starting lookup directory).
80 Upon error, that error is returned.
81 If the result is not a directory, an
82 .B ENOTDIR
83 error is returned.
84 If the resolution of the symbolic link is successful and returns a directory,
85 we set the current lookup directory to that directory, and go to
86 the next component.
87 Note that the resolution process here can involve recursion if the
88 prefix ('dirname') component of a pathname contains a filename
89 that is a symbolic link that resolves to a directory (where the
90 prefix component of that directory may contain a symbolic link, and so on).
91 In order to protect the kernel against stack overflow, and also
92 to protect against denial of service, there are limits on the
93 maximum recursion depth, and on the maximum number of symbolic links
94 followed.
96 .B ELOOP
97 error is returned when the maximum is
98 exceeded ("Too many levels of symbolic links").
101 .\" presently: max recursion depth during symlink resolution: 5
102 .\" max total number of symbolic links followed: 40
103 .\" _POSIX_SYMLOOP_MAX is 8
104 As currently implemented on Linux, the maximum number
105 .\" MAXSYMLINKS is 40
106 of symbolic links that will be followed while resolving a pathname is 40.
107 Before Linux 2.6.18, the limit on the recursion depth was 5.
108 Starting with Linux 2.6.18, this limit
109 .\" MAX_NESTED_LINKS
110 was raised to 8.
111 In Linux 4.2,
112 .\" commit 894bc8c4662ba9daceafe943a5ba0dd407da5cd3
113 the kernel's pathname-resolution code
114 was reworked to eliminate the use of recursion,
115 so that the only limit that remains is the maximum of 40
116 resolutions for the entire pathname.
118 The resolution of symbolic links during this stage can be blocked by using
119 .BR openat2 (2),
120 with the
121 .B RESOLVE_NO_SYMLINKS
122 flag set.
123 .SS Step 3: find the final entry
124 The lookup of the final component of the pathname goes just like
125 that of all other components, as described in the previous step,
126 with two differences: (i) the final component need not be a
127 directory (at least as far as the path resolution process is
128 concerned\[em]it may have to be a directory, or a nondirectory, because of
129 the requirements of the specific system call), and (ii) it
130 is not necessarily an error if the component is not found\[em]maybe
131 we are just creating it.
132 The details on the treatment
133 of the final entry are described in the manual pages of the specific
134 system calls.
135 .SS "\&. and .."
136 By convention, every directory has the entries "." and "..",
137 which refer to the directory itself and to its parent directory,
138 respectively.
140 The path resolution process will assume that these entries have
141 their conventional meanings, regardless of whether they are
142 actually present in the physical filesystem.
144 One cannot walk up past the root: "/.." is the same as "/".
145 .SS Mount points
146 After a
147 .I mount dev path
148 command, the pathname "path" refers to
149 the root of the filesystem hierarchy on the device "dev", and no
150 longer to whatever it referred to earlier.
152 One can walk out of a mounted filesystem: "path/.." refers to
153 the parent directory of "path",
154 outside of the filesystem hierarchy on "dev".
156 Traversal of mount points can be blocked by using
157 .BR openat2 (2),
158 with the
159 .B RESOLVE_NO_XDEV
160 flag set (though note that this also restricts bind mount traversal).
161 .SS Trailing slashes
162 If a pathname ends in a \[aq]/\[aq], that forces resolution of the preceding
163 component as in Step 2:
164 the component preceding the slash either exists and resolves to a directory
165 or it names a directory that is to be created
166 immediately after the pathname is resolved.
167 Otherwise, a trailing \[aq]/\[aq] is ignored.
168 .SS Final symbolic link
169 If the last component of a pathname is a symbolic link, then it
170 depends on the system call whether the file referred to will be
171 the symbolic link or the result of path resolution on its contents.
172 For example, the system call
173 .BR lstat (2)
174 will operate on the symbolic link,
175 while
176 .BR stat (2)
177 operates on the file pointed to by the symbolic link.
178 .SS Length limit
179 There is a maximum length for pathnames.
180 If the pathname (or some
181 intermediate pathname obtained while resolving symbolic links)
182 is too long, an
183 .B ENAMETOOLONG
184 error is returned ("Filename too long").
185 .SS Empty pathname
186 In the original UNIX, the empty pathname referred to the current directory.
187 Nowadays POSIX decrees that an empty pathname must not be resolved
188 successfully.
189 Linux returns
190 .B ENOENT
191 in this case.
192 .SS Permissions
193 The permission bits of a file consist of three groups of three bits; see
194 .BR chmod (1)
196 .BR stat (2).
197 The first group of three is used when the effective user ID of
198 the calling process equals the owner ID of the file.
199 The second group
200 of three is used when the group ID of the file either equals the
201 effective group ID of the calling process, or is one of the
202 supplementary group IDs of the calling process (as set by
203 .BR setgroups (2)).
204 When neither holds, the third group is used.
206 Of the three bits used, the first bit determines read permission,
207 the second write permission, and the last execute permission
208 in case of ordinary files, or search permission in case of directories.
210 Linux uses the fsuid instead of the effective user ID in permission checks.
211 Ordinarily the fsuid will equal the effective user ID, but the fsuid can be
212 changed by the system call
213 .BR setfsuid (2).
215 (Here "fsuid" stands for something like "filesystem user ID".
216 The concept was required for the implementation of a user space
217 NFS server at a time when processes could send a signal to a process
218 with the same effective user ID.
219 It is obsolete now.
220 Nobody should use
221 .BR setfsuid (2).)
223 Similarly, Linux uses the fsgid ("filesystem group ID")
224 instead of the effective group ID.
226 .BR setfsgid (2).
227 .\" FIXME . say something about filesystem mounted read-only ?
228 .SS Bypassing permission checks: superuser and capabilities
229 On a traditional UNIX system, the superuser
230 .RI ( root ,
231 user ID 0) is all-powerful, and bypasses all permissions restrictions
232 when accessing files.
233 .\" (but for exec at least one x bit must be set) -- AEB
234 .\" but there is variation across systems on this point: for
235 .\" example, HP-UX and Tru64 are as described by AEB.  However,
236 .\" on some implementations (e.g., Solaris, FreeBSD),
237 .\" access(X_OK) by superuser will report success, regardless
238 .\" of the file's execute permission bits. -- MTK (Oct 05)
240 On Linux, superuser privileges are divided into capabilities (see
241 .BR capabilities (7)).
242 Two capabilities are relevant for file permissions checks:
243 .B CAP_DAC_OVERRIDE
245 .BR CAP_DAC_READ_SEARCH .
246 (A process has these capabilities if its fsuid is 0.)
249 .B CAP_DAC_OVERRIDE
250 capability overrides all permission checking,
251 but grants execute permission only when at least one
252 of the file's three execute permission bits is set.
255 .B CAP_DAC_READ_SEARCH
256 capability grants read and search permission
257 on directories, and read permission on ordinary files.
258 .\" FIXME . say something about immutable files
259 .\" FIXME . say something about ACLs
260 .SH SEE ALSO
261 .BR readlink (2),
262 .BR capabilities (7),
263 .BR credentials (7),
264 .BR symlink (7)