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