• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.9
/src/basic/chase.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/magic.h>
4
#include <sys/mount.h>
5
#include <unistd.h>
6

7
#include "alloc-util.h"
8
#include "chase.h"
9
#include "errno-util.h"
10
#include "fd-util.h"
11
#include "fileio.h"
12
#include "fs-util.h"
13
#include "glyph-util.h"
14
#include "log.h"
15
#include "path-util.h"
16
#include "stat-util.h"
17
#include "string-util.h"
18
#include "strv.h"
19
#include "user-util.h"
20

21
#define CHASE_NO_SHORTCUT_MASK                          \
22
        (CHASE_NONEXISTENT |                            \
23
         CHASE_NO_AUTOFS |                              \
24
         CHASE_TRIGGER_AUTOFS |                         \
25
         CHASE_SAFE |                                   \
26
         CHASE_STEP |                                   \
27
         CHASE_PROHIBIT_SYMLINKS |                      \
28
         CHASE_PARENT |                                 \
29
         CHASE_MKDIR_0755 |                             \
30
         CHASE_MUST_BE_DIRECTORY |                      \
31
         CHASE_MUST_BE_REGULAR |                        \
32
         CHASE_MUST_BE_SOCKET)
33

34
bool unsafe_transition(const struct stat *a, const struct stat *b) {
36,829✔
35
        /* Returns true if the transition from a to b is safe, i.e. that we never transition from unprivileged to
36
         * privileged files or directories. Why bother? So that unprivileged code can't symlink to privileged files
37
         * making us believe we read something safe even though it isn't safe in the specific context we open it in. */
38

39
        if (a->st_uid == 0) /* Transitioning from privileged to unprivileged is always fine */
36,829✔
40
                return false;
41

42
        return a->st_uid != b->st_uid; /* Otherwise we need to stay within the same UID */
631✔
43
}
44

45
static int log_unsafe_transition(int a, int b, const char *path, ChaseFlags flags) {
8✔
46
        _cleanup_free_ char *n1 = NULL, *n2 = NULL, *user_a = NULL, *user_b = NULL;
8✔
47
        struct stat st;
8✔
48

49
        if (!FLAGS_SET(flags, CHASE_WARN))
8✔
50
                return -ENOLINK;
51

52
        (void) fd_get_path(a, &n1);
5✔
53
        (void) fd_get_path(b, &n2);
5✔
54

55
        if (fstat(a, &st) == 0)
5✔
56
                user_a = uid_to_name(st.st_uid);
5✔
57
        if (fstat(b, &st) == 0)
5✔
58
                user_b = uid_to_name(st.st_uid);
5✔
59

60
        return log_warning_errno(SYNTHETIC_ERRNO(ENOLINK),
5✔
61
                                 "Detected unsafe path transition %s (owned by %s) %s %s (owned by %s) during canonicalization of %s.",
62
                                 strna(n1), strna(user_a), glyph(GLYPH_ARROW_RIGHT), strna(n2), strna(user_b), path);
63
}
64

65
static int log_autofs_mount_point(int fd, const char *path, ChaseFlags flags) {
×
66
        _cleanup_free_ char *n1 = NULL;
×
67

68
        if (!FLAGS_SET(flags, CHASE_WARN))
×
69
                return -EREMOTE;
70

71
        (void) fd_get_path(fd, &n1);
×
72

73
        return log_warning_errno(SYNTHETIC_ERRNO(EREMOTE),
×
74
                                 "Detected autofs mount point '%s' during canonicalization of '%s'.",
75
                                 strna(n1), path);
76
}
77

78
static int log_prohibited_symlink(int fd, ChaseFlags flags) {
7✔
79
        _cleanup_free_ char *n1 = NULL;
7✔
80

81
        assert(fd >= 0);
7✔
82

83
        if (!FLAGS_SET(flags, CHASE_WARN))
7✔
84
                return -EREMCHG;
85

86
        (void) fd_get_path(fd, &n1);
3✔
87

88
        return log_warning_errno(SYNTHETIC_ERRNO(EREMCHG),
3✔
89
                                 "Detected symlink where no symlink is allowed at '%s', refusing.",
90
                                 strna(n1));
91
}
92

93
static int openat_opath_with_automount(int dir_fd, const char *path, bool automount) {
19,625,492✔
94
        static bool can_open_tree = true;
19,625,492✔
95
        int r;
19,625,492✔
96

97
        /* Pin an inode via O_PATH semantics. Sounds pretty obvious to do this, right? You just do open()
98
         * with O_PATH, and there you go. But uh, it's not that easy. open() via O_PATH does not trigger
99
         * automounts, but we may want that when CHASE_TRIGGER_AUTOFS is set. But thankfully there's
100
         * a way out: the newer open_tree() call, when specified without OPEN_TREE_CLONE actually is fully
101
         * equivalent to open() with O_PATH – except for one thing: it triggers automounts.
102
         *
103
         * As it turns out some sandboxes prohibit open_tree(), and return EPERM or ENOSYS if we call it.
104
         * But since autofs does not work inside of mount namespace anyway, let's simply handle this
105
         * as gracefully as we can, and fall back to classic openat() if we see EPERM/ENOSYS. */
106

107
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
19,625,492✔
108
        assert(path);
19,625,492✔
109

110
        if (automount && can_open_tree) {
19,625,492✔
111
                r = RET_NERRNO(open_tree(dir_fd, path, AT_SYMLINK_NOFOLLOW|OPEN_TREE_CLOEXEC));
66,693✔
112
                if (r >= 0 || (r != -EPERM && !ERRNO_IS_NEG_NOT_SUPPORTED(r)))
2,896✔
113
                        return r;
66,693✔
114

115
                can_open_tree = false;
×
116
        }
117

118
        return RET_NERRNO(openat(dir_fd, path, O_PATH|O_NOFOLLOW|O_CLOEXEC));
19,558,799✔
119
}
120

121
static int chaseat_needs_absolute(int dir_fd, const char *path) {
3,424,649✔
122
        if (dir_fd < 0)
3,424,649✔
123
                return path_is_absolute(path);
838✔
124

125
        return dir_fd_is_root(dir_fd);
3,424,230✔
126
}
127

128
int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int *ret_fd) {
3,433,358✔
129
        _cleanup_free_ char *buffer = NULL, *done = NULL;
3,433,358✔
130
        _cleanup_close_ int fd = -EBADF, root_fd = -EBADF;
6,866,716✔
131
        unsigned max_follow = CHASE_MAX; /* how many symlinks to follow before giving up and returning ELOOP */
3,433,358✔
132
        bool exists = true, append_trail_slash = false;
3,433,358✔
133
        struct stat st; /* stat obtained from fd */
3,433,358✔
134
        bool need_absolute = false; /* allocate early to avoid compiler warnings around goto */
3,433,358✔
135
        const char *todo;
3,433,358✔
136
        int r;
3,433,358✔
137

138
        assert(!FLAGS_SET(flags, CHASE_PREFIX_ROOT));
3,433,358✔
139
        assert(!FLAGS_SET(flags, CHASE_STEP|CHASE_EXTRACT_FILENAME));
3,433,358✔
140
        assert(!FLAGS_SET(flags, CHASE_NO_AUTOFS|CHASE_TRIGGER_AUTOFS));
3,433,358✔
141
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
3,433,358✔
142

143
        if (FLAGS_SET(flags, CHASE_STEP))
3,433,358✔
144
                assert(!ret_fd);
23,248✔
145

146
        if (isempty(path))
3,434,487✔
147
                path = ".";
148

149
        /* This function resolves symlinks of the path relative to the given directory file descriptor. If
150
         * CHASE_AT_RESOLVE_IN_ROOT is specified and a directory file descriptor is provided, symlinks
151
         * are resolved relative to the given directory file descriptor. Otherwise, they are resolved
152
         * relative to the root directory of the host.
153
         *
154
         * Note that when a positive directory file descriptor is provided and CHASE_AT_RESOLVE_IN_ROOT is
155
         * specified and we find an absolute symlink, it is resolved relative to given directory file
156
         * descriptor and not the root of the host. Also, when following relative symlinks, this functions
157
         * ensures they cannot be used to "escape" the given directory file descriptor. If a positive
158
         * directory file descriptor is provided, the "path" parameter is always interpreted relative to the
159
         * given directory file descriptor, even if it is absolute. If the given directory file descriptor is
160
         * AT_FDCWD and "path" is absolute, it is interpreted relative to the root directory of the host.
161
         *
162
         * When "dir_fd" points to a non-root directory and CHASE_AT_RESOLVE_IN_ROOT is set, this function
163
         * always returns a relative path in "ret_path", even if "path" is an absolute path, because openat()
164
         * like functions generally ignore the directory fd if they are provided with an absolute path. When
165
         * CHASE_AT_RESOLVE_IN_ROOT is not set, then this returns relative path to the specified file
166
         * descriptor if all resolved symlinks are relative, otherwise absolute path will be returned. When
167
         * "dir_fd" is AT_FDCWD and "path" is an absolute path, we return an absolute path in "ret_path"
168
         * because otherwise, if the caller passes the returned relative path to another openat() like
169
         * function, it would be resolved relative to the current working directory instead of to "/".
170
         *
171
         * Summary about the result path:
172
         * - "dir_fd" points to the root directory
173
         *    → result will be absolute
174
         * - "dir_fd" points to a non-root directory, and CHASE_AT_RESOLVE_IN_ROOT is set
175
         *    → relative
176
         * - "dir_fd" points to a non-root directory, and CHASE_AT_RESOLVE_IN_ROOT is not set
177
         *    → relative when all resolved symlinks are relative, otherwise absolute
178
         * - "dir_fd" is AT_FDCWD, and "path" is absolute
179
         *    → absolute
180
         * - "dir_fd" is AT_FDCWD, and "path" is relative
181
         *    → relative when all resolved symlinks are relative, otherwise absolute
182
         *
183
         * Algorithmically this operates on two path buffers: "done" are the components of the path we
184
         * already processed and resolved symlinks, "." and ".." of. "todo" are the components of the path we
185
         * still need to process. On each iteration, we move one component from "todo" to "done", processing
186
         * its special meaning each time. We always keep an O_PATH fd to the component we are currently
187
         * processing, thus keeping lookup races to a minimum.
188
         *
189
         * Suggested usage: whenever you want to canonicalize a path, use this function. Pass the absolute
190
         * path you got as-is: fully qualified and relative to your host's root. Optionally, specify the
191
         * "dir_fd" parameter to tell this function what to do when encountering a symlink with an absolute
192
         * path as directory: resolve it relative to the given directory file descriptor.
193
         *
194
         * There are five ways to invoke this function:
195
         *
196
         * 1. Without CHASE_STEP or ret_fd: in this case the path is resolved and the normalized path is
197
         *    returned in `ret_path`. The return value is < 0 on error. If CHASE_NONEXISTENT is also set, 0
198
         *    is returned if the file doesn't exist, > 0 otherwise. If CHASE_NONEXISTENT is not set, >= 0 is
199
         *    returned if the destination was found, -ENOENT if it wasn't.
200
         *
201
         * 2. With ret_fd: in this case the destination is opened after chasing it as O_PATH and this file
202
         *    descriptor is returned as return value. This is useful to open files relative to some root
203
         *    directory. Note that the returned O_PATH file descriptors must be converted into a regular one
204
         *    (using fd_reopen() or such) before it can be used for reading/writing. ret_fd may not be
205
         *    combined with CHASE_NONEXISTENT.
206
         *
207
         * 3. With CHASE_STEP: in this case only a single step of the normalization is executed, i.e. only
208
         *    the first symlink or ".." component of the path is resolved, and the resulting path is
209
         *    returned. This is useful if a caller wants to trace the path through the file system verbosely.
210
         *    Returns < 0 on error, > 0 if the path is fully normalized, and == 0 for each normalization
211
         *    step. This may be combined with CHASE_NONEXISTENT, in which case 1 is returned when a component
212
         *    is not found.
213
         *
214
         * 4. With CHASE_SAFE: in this case the path must not contain unsafe transitions, i.e. transitions
215
         *    from unprivileged to privileged files or directories. In such cases the return value is
216
         *    -ENOLINK. If CHASE_WARN is also set, a warning describing the unsafe transition is emitted.
217
         *    CHASE_WARN cannot be used in PID 1.
218
         *
219
         * 5. With CHASE_NO_AUTOFS: in this case if an autofs mount point is encountered, path normalization
220
         *    is aborted and -EREMOTE is returned. If CHASE_WARN is also set, a warning showing the path of
221
         *    the mount point is emitted. CHASE_WARN cannot be used in PID 1.
222
         */
223

224
        _cleanup_close_ int _dir_fd = -EBADF;
3,433,358✔
225
        r = dir_fd_is_root(dir_fd);
3,433,358✔
226
        if (r < 0)
110,823✔
227
                return r;
228
        if (r > 0) {
3,433,358✔
229

230
                /* Shortcut the common case where no root dir is specified, and no special flags are given to
231
                 * a regular open() */
232
                if (!ret_path &&
3,345,148✔
233
                    (flags & (CHASE_STEP|CHASE_NO_AUTOFS|CHASE_NONEXISTENT|CHASE_SAFE|CHASE_WARN|CHASE_PROHIBIT_SYMLINKS|CHASE_PARENT|CHASE_MKDIR_0755)) == 0) {
72,590✔
234
                        _cleanup_free_ char *slash_path = NULL;
19✔
235

236
                        if (!path_is_absolute(path)) {
8,689✔
237
                                slash_path = strjoin("/", path);
127✔
238
                                if (!slash_path)
127✔
239
                                        return -ENOMEM;
240
                        }
241

242
                        /* We use open_tree() rather than regular open() here, because it gives us direct
243
                         * control over automount behaviour, and otherwise is equivalent to open() with
244
                         * O_PATH */
245
                        fd = open_tree(-EBADF, slash_path ?: path, OPEN_TREE_CLOEXEC|(FLAGS_SET(flags, CHASE_TRIGGER_AUTOFS) ? 0 : AT_NO_AUTOMOUNT));
25,739✔
246
                        if (fd < 0)
8,689✔
247
                                return -errno;
19✔
248

249
                        if (fstat(fd, &st) < 0)
8,670✔
250
                                return -errno;
×
251

252
                        exists = true;
8,670✔
253
                        goto success;
8,670✔
254
                }
255

256
                _dir_fd = open("/", O_DIRECTORY|O_RDONLY|O_CLOEXEC);
3,336,459✔
257
                if (_dir_fd < 0)
3,336,459✔
258
                        return -errno;
×
259

260
                dir_fd = _dir_fd;
3,336,459✔
261
                flags &= ~CHASE_AT_RESOLVE_IN_ROOT;
3,336,459✔
262
        } else if (FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT)) {
88,210✔
263
                /* If we get AT_FDCWD or dir_fd points to "/", then we always resolve symlinks relative to
264
                 * the host's root. Hence, CHASE_AT_RESOLVE_IN_ROOT is meaningless. */
265

266
                r = dir_fd_is_root_or_cwd(dir_fd);
88,188✔
267
                if (r < 0)
88,188✔
268
                        return r;
269
                if (r > 0)
88,188✔
270
                        flags &= ~CHASE_AT_RESOLVE_IN_ROOT;
433✔
271
        }
272

273
        if (!ret_path && ret_fd && (flags & (CHASE_AT_RESOLVE_IN_ROOT|CHASE_NO_SHORTCUT_MASK)) == 0) {
3,424,669✔
274
                /* Shortcut the ret_fd case if the caller isn't interested in the actual path and has no root
275
                 * set and doesn't care about any of the other special features we provide either. */
276
                r = openat(dir_fd, path, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0));
20✔
277
                if (r < 0)
20✔
278
                        return -errno;
×
279

280
                *ret_fd = r;
20✔
281
                return 0;
20✔
282
        }
283

284
        buffer = strdup(path);
3,424,649✔
285
        if (!buffer)
3,424,649✔
286
                return -ENOMEM;
287

288
        /* If we receive an absolute path together with AT_FDCWD, we need to return an absolute path, because
289
         * a relative path would be interpreted relative to the current working directory. Also, let's make
290
         * the result absolute when the file descriptor of the root directory is specified. */
291
        r = chaseat_needs_absolute(dir_fd, path);
3,424,649✔
292
        if (r < 0)
3,424,649✔
293
                return r;
294

295
        need_absolute = r;
3,424,649✔
296
        if (need_absolute) {
3,424,649✔
297
                done = strdup("/");
3,336,845✔
298
                if (!done)
3,336,845✔
299
                        return -ENOMEM;
300
        }
301

302
        /* If a positive directory file descriptor is provided, always resolve the given path relative to it,
303
         * regardless of whether it is absolute or not. If we get AT_FDCWD, follow regular openat()
304
         * semantics, if the path is relative, resolve against the current working directory. Otherwise,
305
         * resolve against root. */
306
        fd = openat(dir_fd, done ?: ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
3,512,453✔
307
        if (fd < 0)
3,424,649✔
308
                return -errno;
×
309

310
        if (fstat(fd, &st) < 0)
3,424,649✔
311
                return -errno;
×
312

313
        /* If we get AT_FDCWD, we always resolve symlinks relative to the host's root. Only if a positive
314
         * directory file descriptor is provided we will look at CHASE_AT_RESOLVE_IN_ROOT to determine
315
         * whether to resolve symlinks in it or not. */
316
        if (dir_fd >= 0 && FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT))
3,424,649✔
317
                root_fd = openat(dir_fd, ".", O_CLOEXEC|O_DIRECTORY|O_PATH);
87,755✔
318
        else
319
                root_fd = open("/", O_CLOEXEC|O_DIRECTORY|O_PATH);
3,336,894✔
320
        if (root_fd < 0)
3,424,649✔
321
                return -errno;
×
322

323
        if (ENDSWITH_SET(buffer, "/", "/.")) {
3,424,649✔
324
                flags |= CHASE_MUST_BE_DIRECTORY;
6,037✔
325
                if (FLAGS_SET(flags, CHASE_TRAIL_SLASH))
6,037✔
326
                        append_trail_slash = true;
8✔
327
        } else if (dot_or_dot_dot(buffer) || endswith(buffer, "/.."))
3,418,612✔
328
                flags |= CHASE_MUST_BE_DIRECTORY;
1,138✔
329

330
        if (FLAGS_SET(flags, CHASE_PARENT))
3,424,649✔
331
                flags |= CHASE_MUST_BE_DIRECTORY;
50,580✔
332

333
        /* If multiple flags are set now, fail immediately */
334
        if (FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY) + FLAGS_SET(flags, CHASE_MUST_BE_REGULAR) + FLAGS_SET(flags, CHASE_MUST_BE_SOCKET) > 1)
3,424,649✔
335
                return -EBADSLT;
336

337
        for (todo = buffer;;) {
3,424,649✔
338
                _cleanup_free_ char *first = NULL;
20,472,102✔
339
                _cleanup_close_ int child = -EBADF;
22,616,219✔
340
                struct stat st_child;
22,616,219✔
341
                const char *e;
22,616,219✔
342

343
                r = path_find_first_component(&todo, /* accept_dot_dot= */ true, &e);
22,616,219✔
344
                if (r < 0)
22,616,219✔
345
                        return r;
346
                if (r == 0) /* We reached the end. */
22,616,219✔
347
                        break;
348

349
                first = strndup(e, r);
20,536,304✔
350
                if (!first)
20,536,304✔
351
                        return -ENOMEM;
352

353
                /* Two dots? Then chop off the last bit of what we already found out. */
354
                if (streq(first, "..")) {
20,536,304✔
355
                        _cleanup_free_ char *parent = NULL;
910,812✔
356
                        _cleanup_close_ int fd_parent = -EBADF;
910,812✔
357
                        struct stat st_parent;
910,812✔
358

359
                        /* If we already are at the top, then going up will not change anything. This is
360
                         * in-line with how the kernel handles this. */
361
                        if (empty_or_root(done) && FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT)) {
910,812✔
362
                                if (FLAGS_SET(flags, CHASE_STEP))
124✔
363
                                        goto chased_one;
×
364
                                continue;
124✔
365
                        }
366

367
                        fd_parent = openat(fd, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH|O_DIRECTORY);
910,688✔
368
                        if (fd_parent < 0)
910,688✔
369
                                return -errno;
×
370

371
                        if (fstat(fd_parent, &st_parent) < 0)
910,688✔
372
                                return -errno;
×
373

374
                        /* If we opened the same directory, that _may_ indicate that we're at the host root
375
                         * directory. Let's confirm that in more detail with dir_fd_is_root(). And if so,
376
                         * going up won't change anything. */
377
                        if (stat_inode_same(&st_parent, &st)) {
910,688✔
378
                                r = dir_fd_is_root(fd);
42✔
379
                                if (r < 0)
42✔
380
                                        return r;
381
                                if (r > 0) {
42✔
382
                                        if (FLAGS_SET(flags, CHASE_STEP))
42✔
383
                                                goto chased_one;
×
384
                                        continue;
42✔
385
                                }
386
                        }
387

388
                        r = path_extract_directory(done, &parent);
910,646✔
389
                        if (r >= 0) {
910,646✔
390
                                assert(!need_absolute || path_is_absolute(parent));
909,321✔
391
                                free_and_replace(done, parent);
909,321✔
392
                        } else if (r == -EDESTADDRREQ) {
1,325✔
393
                                /* 'done' contains filename only (i.e. no slash). */
394
                                assert(!need_absolute);
1,325✔
395
                                done = mfree(done);
1,325✔
396
                        } else if (r == -EADDRNOTAVAIL) {
×
397
                                /* 'done' is "/". This branch should be already handled in the above. */
398
                                assert(!FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT));
×
399
                                assert_not_reached();
×
400
                        } else if (r == -EINVAL) {
×
401
                                /* 'done' is an empty string, ends with '..', or an invalid path. */
402
                                assert(!need_absolute);
×
403
                                assert(!FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT));
×
404

405
                                if (!path_is_valid(done))
×
406
                                        return -EINVAL;
407

408
                                /* If we're at the top of "dir_fd", start appending ".." to "done". */
409
                                if (!path_extend(&done, ".."))
×
410
                                        return -ENOMEM;
411
                        } else
412
                                return r;
413

414
                        if (FLAGS_SET(flags, CHASE_STEP))
910,646✔
415
                                goto chased_one;
6✔
416

417
                        if (FLAGS_SET(flags, CHASE_SAFE) &&
910,640✔
418
                            unsafe_transition(&st, &st_parent))
×
419
                                return log_unsafe_transition(fd, fd_parent, path, flags);
×
420

421
                        /* If the path ends on a "..", and CHASE_PARENT is specified then our current 'fd' is
422
                         * the child of the returned normalized path, not the parent as requested. To correct
423
                         * this we have to go *two* levels up. */
424
                        if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo)) {
910,640✔
425
                                _cleanup_close_ int fd_grandparent = -EBADF;
2✔
426
                                struct stat st_grandparent;
2✔
427

428
                                fd_grandparent = openat(fd_parent, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH|O_DIRECTORY);
2✔
429
                                if (fd_grandparent < 0)
2✔
430
                                        return -errno;
×
431

432
                                if (fstat(fd_grandparent, &st_grandparent) < 0)
2✔
433
                                        return -errno;
×
434

435
                                if (FLAGS_SET(flags, CHASE_SAFE) &&
2✔
436
                                    unsafe_transition(&st_parent, &st_grandparent))
×
437
                                        return log_unsafe_transition(fd_parent, fd_grandparent, path, flags);
×
438

439
                                st = st_grandparent;
2✔
440
                                close_and_replace(fd, fd_grandparent);
2✔
441
                                break;
2✔
442
                        }
443

444
                        /* update fd and stat */
445
                        st = st_parent;
910,638✔
446
                        close_and_replace(fd, fd_parent);
910,638✔
447
                        continue;
910,638✔
448
                }
449

450
                /* Otherwise let's pin it by file descriptor, via O_PATH. */
451
                child = r = openat_opath_with_automount(fd, first, /* automount= */ FLAGS_SET(flags, CHASE_TRIGGER_AUTOFS));
19,625,492✔
452
                if (r < 0) {
19,625,492✔
453
                        if (r != -ENOENT)
1,300,300✔
454
                                return r;
455

456
                        if (!isempty(todo) && !path_is_safe(todo)) /* Refuse parent/mkdir handling if suffix contains ".." or something weird */
1,746,071✔
457
                                return r;
458

459
                        if (FLAGS_SET(flags, CHASE_MKDIR_0755) && (!isempty(todo) || !(flags & (CHASE_PARENT|CHASE_NONEXISTENT)))) {
1,301,136✔
460
                                child = xopenat(fd,
400✔
461
                                                first,
462
                                                O_DIRECTORY|O_CREAT|O_EXCL|O_NOFOLLOW|O_PATH|O_CLOEXEC);
463
                                if (child < 0)
400✔
464
                                        return child;
465
                        } else if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo)) {
1,299,896✔
466
                                if (!path_extend(&done, first))
3,293✔
467
                                        return -ENOMEM;
468

469
                                break;
470
                        } else if (FLAGS_SET(flags, CHASE_NONEXISTENT)) {
1,296,603✔
471
                                if (!path_extend(&done, first, todo))
16,092✔
472
                                        return -ENOMEM;
473

474
                                exists = false;
475
                                break;
476
                        } else
477
                                return r;
478
                }
479

480
                /* ... and then check what it actually is. */
481
                if (fstat(child, &st_child) < 0)
18,325,592✔
482
                        return -errno;
×
483

484
                if (FLAGS_SET(flags, CHASE_SAFE) &&
18,362,416✔
485
                    unsafe_transition(&st, &st_child))
36,824✔
486
                        return log_unsafe_transition(fd, child, path, flags);
5✔
487

488
                if (FLAGS_SET(flags, CHASE_NO_AUTOFS) &&
18,325,587✔
489
                    fd_is_fs_type(child, AUTOFS_SUPER_MAGIC) > 0)
7,621,993✔
490
                        return log_autofs_mount_point(child, path, flags);
×
491

492
                if (S_ISLNK(st_child.st_mode) && !(FLAGS_SET(flags, CHASE_NOFOLLOW) && isempty(todo))) {
18,325,587✔
493
                        _cleanup_free_ char *destination = NULL;
12✔
494

495
                        if (FLAGS_SET(flags, CHASE_PROHIBIT_SYMLINKS))
416,527✔
496
                                return log_prohibited_symlink(child, flags);
7✔
497

498
                        /* This is a symlink, in this case read the destination. But let's make sure we
499
                         * don't follow symlinks without bounds. */
500
                        if (--max_follow <= 0)
416,520✔
501
                                return -ELOOP;
502

503
                        r = readlinkat_malloc(fd, first, &destination);
416,519✔
504
                        if (r < 0)
416,519✔
505
                                return r;
506
                        if (isempty(destination))
416,530✔
507
                                return -EINVAL;
508

509
                        if (path_is_absolute(destination)) {
416,518✔
510

511
                                /* An absolute destination. Start the loop from the beginning, but use the
512
                                 * root file descriptor as base. */
513

514
                                safe_close(fd);
8,495✔
515
                                fd = fd_reopen(root_fd, O_CLOEXEC|O_PATH|O_DIRECTORY);
8,495✔
516
                                if (fd < 0)
8,495✔
517
                                        return fd;
518

519
                                if (fstat(fd, &st) < 0)
8,495✔
520
                                        return -errno;
×
521

522
                                if (FLAGS_SET(flags, CHASE_SAFE) &&
8,500✔
523
                                    unsafe_transition(&st_child, &st))
5✔
524
                                        return log_unsafe_transition(child, fd, path, flags);
3✔
525

526
                                /* When CHASE_AT_RESOLVE_IN_ROOT is not set, now the chased path may be
527
                                 * outside of the specified dir_fd. Let's make the result absolute. */
528
                                if (!FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT))
8,492✔
529
                                        need_absolute = true;
530

531
                                r = free_and_strdup(&done, need_absolute ? "/" : NULL);
8,811✔
532
                                if (r < 0)
8,492✔
533
                                        return r;
534
                        }
535

536
                        /* Prefix what's left to do with what we just read, and start the loop again, but
537
                         * remain in the current directory. */
538
                        if (!path_extend(&destination, todo))
416,515✔
539
                                return -ENOMEM;
540

541
                        free_and_replace(buffer, destination);
416,515✔
542
                        todo = buffer;
416,515✔
543

544
                        if (FLAGS_SET(flags, CHASE_STEP))
416,515✔
545
                                goto chased_one;
8✔
546

547
                        continue;
416,507✔
548
                }
549

550
                /* If this is not a symlink, then let's just add the name we read to what we already verified. */
551
                if (!path_extend(&done, first))
17,909,060✔
552
                        return -ENOMEM;
553

554
                if (FLAGS_SET(flags, CHASE_PARENT) && isempty(todo))
20,053,163✔
555
                        break;
556

557
                /* And iterate again, but go one directory further down. */
558
                st = st_child;
17,864,259✔
559
                close_and_replace(fd, child);
17,864,259✔
560
        }
561

562
success:
2,144,103✔
563
        if (exists) {
2,152,773✔
564
                if (FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY)) {
2,136,681✔
565
                        r = stat_verify_directory(&st);
56,915✔
566
                        if (r < 0)
56,915✔
567
                                return r;
568
                }
569

570
                if (FLAGS_SET(flags, CHASE_MUST_BE_REGULAR)) {
2,136,681✔
571
                        r = stat_verify_regular(&st);
55✔
572
                        if (r < 0)
55✔
573
                                return r;
574
                }
575

576
                if (FLAGS_SET(flags, CHASE_MUST_BE_SOCKET)) {
2,136,681✔
577
                        r = stat_verify_socket(&st);
63✔
578
                        if (r < 0)
63✔
579
                                return r;
580
                }
581
        }
582

583
        if (ret_path) {
2,152,773✔
584
                if (FLAGS_SET(flags, CHASE_EXTRACT_FILENAME) && done) {
2,070,163✔
585
                        _cleanup_free_ char *f = NULL;
×
586

587
                        r = path_extract_filename(done, &f);
7,195✔
588
                        if (r < 0 && r != -EADDRNOTAVAIL)
7,195✔
589
                                return r;
×
590

591
                        /* If we get EADDRNOTAVAIL we clear done and it will get reinitialized by the next block. */
592
                        free_and_replace(done, f);
7,195✔
593
                }
594

595
                if (!done) {
2,070,163✔
596
                        assert(!need_absolute || FLAGS_SET(flags, CHASE_EXTRACT_FILENAME));
223✔
597
                        done = strdup(".");
223✔
598
                        if (!done)
223✔
599
                                return -ENOMEM;
600
                }
601

602
                if (append_trail_slash)
2,070,163✔
603
                        if (!strextend(&done, "/"))
8✔
604
                                return -ENOMEM;
605

606
                *ret_path = TAKE_PTR(done);
2,070,163✔
607
        }
608

609
        if (ret_fd) {
2,152,773✔
610
                if (exists) {
2,000,674✔
611
                        /* Return the O_PATH fd we currently are looking to the caller. It can translate it
612
                         * to a proper fd by opening /proc/self/fd/xyz. */
613
                        assert(fd >= 0);
1,999,855✔
614
                        *ret_fd = TAKE_FD(fd);
1,999,855✔
615
                } else
616
                        *ret_fd = -EBADF;
819✔
617
        }
618

619
        if (FLAGS_SET(flags, CHASE_STEP))
2,152,773✔
620
                return 1;
621

622
        return exists;
2,129,539✔
623

624
chased_one:
14✔
625
        if (ret_path) {
14✔
626
                const char *e;
14✔
627

628
                if (!done) {
14✔
629
                        assert(!need_absolute);
1✔
630
                        done = strdup(append_trail_slash ? "./" : ".");
1✔
631
                        if (!done)
1✔
632
                                return -ENOMEM;
×
633
                }
634

635
                /* todo may contain slashes at the beginning. */
636
                r = path_find_first_component(&todo, /* accept_dot_dot= */ true, &e);
14✔
637
                if (r < 0)
14✔
638
                        return r;
639
                if (r == 0)
14✔
640
                        *ret_path = TAKE_PTR(done);
×
641
                else {
642
                        char *c;
14✔
643

644
                        c = path_join(done, e);
14✔
645
                        if (!c)
14✔
646
                                return -ENOMEM;
647

648
                        *ret_path = c;
14✔
649
                }
650
        }
651

652
        return 0;
653
}
654

655
int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path, int *ret_fd) {
2,622,387✔
656
        _cleanup_free_ char *root_abs = NULL, *absolute = NULL, *p = NULL;
2,622,387✔
657
        _cleanup_close_ int fd = -EBADF, pfd = -EBADF;
5,244,774✔
658
        int r;
2,622,387✔
659

660
        assert(path);
2,622,387✔
661

662
        if (isempty(path))
5,244,773✔
663
                return -EINVAL;
664

665
        r = empty_or_root_harder_to_null(&root);
2,622,386✔
666
        if (r < 0)
2,622,386✔
667
                return r;
668

669
        /* A root directory of "/" or "" is identical to "/". */
670
        if (empty_or_root(root)) {
2,622,383✔
671
                root = "/";
2,574,922✔
672

673
                /* When the root directory is "/", we will drop CHASE_AT_RESOLVE_IN_ROOT in chaseat(),
674
                 * hence below is not necessary, but let's shortcut. */
675
                flags &= ~CHASE_AT_RESOLVE_IN_ROOT;
2,574,922✔
676

677
        } else {
678
                r = path_make_absolute_cwd(root, &root_abs);
47,461✔
679
                if (r < 0)
47,461✔
680
                        return r;
681

682
                /* Simplify the root directory, so that it has no duplicate slashes and nothing at the
683
                 * end. While we won't resolve the root path we still simplify it. */
684
                root = path_simplify(root_abs);
47,461✔
685

686
                assert(path_is_absolute(root));
47,461✔
687
                assert(!empty_or_root(root));
47,461✔
688

689
                if (FLAGS_SET(flags, CHASE_PREFIX_ROOT)) {
47,461✔
690
                        absolute = path_join(root, path);
14,527✔
691
                        if (!absolute)
14,527✔
692
                                return -ENOMEM;
693
                }
694

695
                flags |= CHASE_AT_RESOLVE_IN_ROOT;
47,461✔
696
        }
697

698
        if (!absolute) {
2,622,383✔
699
                r = path_make_absolute_cwd(path, &absolute);
2,607,856✔
700
                if (r < 0)
2,607,856✔
701
                        return r;
702
        }
703

704
        path = path_startswith(absolute, root);
2,622,383✔
705
        if (!path)
2,622,383✔
706
                return log_full_errno(FLAGS_SET(flags, CHASE_WARN) ? LOG_WARNING : LOG_DEBUG,
×
707
                                      SYNTHETIC_ERRNO(ECHRNG),
708
                                      "Specified path '%s' is outside of specified root directory '%s', refusing to resolve.",
709
                                      absolute, root);
710

711
        if (empty_or_root(root))
2,622,383✔
712
                fd = XAT_FDROOT;
713
        else {
714
                fd = open(root, O_CLOEXEC|O_DIRECTORY|O_PATH);
47,461✔
715
                if (fd < 0)
47,461✔
716
                        return -errno;
1✔
717
        }
718

719
        r = chaseat(fd, path, flags & ~CHASE_PREFIX_ROOT, ret_path ? &p : NULL, ret_fd ? &pfd : NULL);
3,153,344✔
720
        if (r < 0)
2,622,382✔
721
                return r;
722

723
        if (ret_path) {
1,911,484✔
724
                if (!FLAGS_SET(flags, CHASE_EXTRACT_FILENAME)) {
1,839,771✔
725

726
                        /* When "root" points to the root directory, the result of chaseat() is always
727
                         * absolute, hence it is not necessary to prefix with the root. When "root" points to
728
                         * a non-root directory, the result path is always normalized and relative, hence
729
                         * we can simply call path_join() and not necessary to call path_simplify().
730
                         * As a special case, chaseat() may return "." or "./", which are normalized too,
731
                         * but we need to drop "." before merging with root. */
732

733
                        if (empty_or_root(root))
1,834,229✔
734
                                assert(path_is_absolute(p));
1,803,900✔
735
                        else {
736
                                char *q;
30,329✔
737

738
                                assert(!path_is_absolute(p));
30,329✔
739

740
                                q = path_join(root, p + STR_IN_SET(p, ".", "./"));
30,329✔
741
                                if (!q)
30,329✔
742
                                        return -ENOMEM;
×
743

744
                                free_and_replace(p, q);
30,329✔
745
                        }
746
                }
747

748
                *ret_path = TAKE_PTR(p);
1,839,771✔
749
        }
750

751
        if (ret_fd)
1,911,484✔
752
                *ret_fd = TAKE_FD(pfd);
1,768,429✔
753

754
        return r;
755
}
756

757
int chaseat_prefix_root(const char *path, const char *root, char **ret) {
137,605✔
758
        char *q;
137,605✔
759
        int r;
137,605✔
760

761
        assert(path);
137,605✔
762
        assert(ret);
137,605✔
763

764
        /* This is mostly for prefixing the result of chaseat(). */
765

766
        if (!path_is_absolute(path)) {
137,605✔
767
                _cleanup_free_ char *root_abs = NULL;
5,492✔
768

769
                r = empty_or_root_harder_to_null(&root);
5,492✔
770
                if (r < 0 && r != -ENOENT)
5,492✔
771
                        return r;
772

773
                /* If the dir_fd points to the root directory, chaseat() always returns an absolute path. */
774
                if (empty_or_root(root))
5,492✔
775
                        return -EINVAL;
776

777
                r = path_make_absolute_cwd(root, &root_abs);
5,492✔
778
                if (r < 0)
5,492✔
779
                        return r;
780

781
                root = path_simplify(root_abs);
5,492✔
782

783
                q = path_join(root, path + (path[0] == '.' && IN_SET(path[1], '/', '\0')));
5,492✔
784
        } else
785
                q = strdup(path);
132,113✔
786
        if (!q)
137,605✔
787
                return -ENOMEM;
788

789
        *ret = q;
137,605✔
790
        return 0;
137,605✔
791
}
792

793
int chase_extract_filename(const char *path, const char *root, char **ret) {
2,046✔
794
        int r;
2,046✔
795

796
        /* This is similar to path_extract_filename(), but takes root directory.
797
         * The result should be consistent with chase() with CHASE_EXTRACT_FILENAME. */
798

799
        assert(path);
2,046✔
800
        assert(ret);
2,046✔
801

802
        if (isempty(path))
2,046✔
803
                return -EINVAL;
804

805
        if (!path_is_absolute(path))
4,092✔
806
                return -EINVAL;
807

808
        r = empty_or_root_harder_to_null(&root);
2,046✔
809
        if (r < 0 && r != -ENOENT)
2,046✔
810
                return r;
811

812
        if (!empty_or_root(root)) {
2,046✔
813
                _cleanup_free_ char *root_abs = NULL;
1,216✔
814

815
                r = path_make_absolute_cwd(root, &root_abs);
1,216✔
816
                if (r < 0)
1,216✔
817
                        return r;
818

819
                path = path_startswith(path, root_abs);
1,216✔
820
                if (!path)
1,216✔
821
                        return -EINVAL;
822
        }
823

824
        if (!isempty(path)) {
2,046✔
825
                r = path_extract_filename(path, ret);
1,887✔
826
                if (r != -EADDRNOTAVAIL)
1,887✔
827
                        return r;
828
        }
829

830
        return strdup_to(ret, ".");
166✔
831
}
832

833
int chase_and_open(
5,052✔
834
                const char *path,
835
                const char *root,
836
                ChaseFlags chase_flags,
837
                int open_flags,
838
                char **ret_path) {
839

840
        _cleanup_close_ int path_fd = -EBADF;
5,052✔
841
        _cleanup_free_ char *p = NULL, *fname = NULL;
5,052✔
842
        int r;
5,052✔
843

844
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
5,052✔
845

846
        XOpenFlags xopen_flags = 0;
5,052✔
847
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_DIRECTORY))
5,052✔
848
                open_flags |= O_DIRECTORY;
14✔
849
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_REGULAR))
5,052✔
850
                xopen_flags |= XO_REGULAR;
554✔
851

852
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
5,052✔
853
                /* Shortcut this call if none of the special features of this call are requested */
854
                return xopenat_full(AT_FDCWD, path,
2,476✔
855
                                    open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
2,476✔
856
                                    xopen_flags,
857
                                    MODE_INVALID);
858

859
        r = chase(path, root, (CHASE_PARENT|chase_flags)&~CHASE_MUST_BE_REGULAR, &p, &path_fd);
2,576✔
860
        if (r < 0)
2,576✔
861
                return r;
862
        assert(path_fd >= 0);
2,041✔
863

864
        if (!FLAGS_SET(chase_flags, CHASE_PARENT) &&
2,041✔
865
            !FLAGS_SET(chase_flags, CHASE_EXTRACT_FILENAME)) {
2,040✔
866
                r = chase_extract_filename(p, root, &fname);
2,040✔
867
                if (r < 0)
2,040✔
868
                        return r;
869
        }
870

871
        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, xopen_flags, MODE_INVALID);
2,042✔
872
        if (r < 0)
2,041✔
873
                return r;
874

875
        if (ret_path)
1,600✔
876
                *ret_path = TAKE_PTR(p);
258✔
877

878
        return r;
879
}
880

881
int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
142✔
882
        _cleanup_close_ int path_fd = -EBADF;
142✔
883
        _cleanup_free_ char *p = NULL;
142✔
884
        DIR *d;
142✔
885
        int r;
142✔
886

887
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_MUST_BE_REGULAR|CHASE_MUST_BE_SOCKET)));
142✔
888
        assert(ret_dir);
142✔
889

890
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0) {
142✔
891
                /* Shortcut this call if none of the special features of this call are requested */
892
                d = opendir(path);
×
893
                if (!d)
×
894
                        return -errno;
×
895

896
                *ret_dir = d;
×
897
                return 0;
×
898
        }
899

900
        r = chase(path, root, chase_flags|CHASE_MUST_BE_DIRECTORY, ret_path ? &p : NULL, &path_fd);
189✔
901
        if (r < 0)
142✔
902
                return r;
903
        assert(path_fd >= 0);
126✔
904

905
        d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0);
126✔
906
        if (!d)
126✔
907
                return -errno;
×
908

909
        if (ret_path)
126✔
910
                *ret_path = TAKE_PTR(p);
79✔
911

912
        *ret_dir = d;
126✔
913
        return 0;
126✔
914
}
915

916
int chase_and_stat(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
559,734✔
917
        _cleanup_close_ int path_fd = -EBADF;
559,734✔
918
        _cleanup_free_ char *p = NULL;
559,734✔
919
        int r;
559,734✔
920

921
        assert(path);
559,734✔
922
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
559,734✔
923
        assert(ret_stat);
559,734✔
924

925
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
559,734✔
926
                /* Shortcut this call if none of the special features of this call are requested */
927
                return RET_NERRNO(fstatat(AT_FDCWD, path, ret_stat,
322,368✔
928
                                          FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
322,306✔
929

930
        r = chase(path, root, chase_flags, ret_path ? &p : NULL, &path_fd);
237,708✔
931
        if (r < 0)
237,428✔
932
                return r;
933
        assert(path_fd >= 0);
151,236✔
934

935
        if (fstat(path_fd, ret_stat) < 0)
151,236✔
936
                return -errno;
×
937

938
        if (ret_path)
151,236✔
939
                *ret_path = TAKE_PTR(p);
151,167✔
940

941
        return 0;
942
}
943

944
int chase_and_access(const char *path, const char *root, ChaseFlags chase_flags, int access_mode, char **ret_path) {
6,872✔
945
        _cleanup_close_ int path_fd = -EBADF;
6,872✔
946
        _cleanup_free_ char *p = NULL;
6,872✔
947
        int r;
6,872✔
948

949
        assert(path);
6,872✔
950
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
6,872✔
951

952
        if (empty_or_root(root) && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
6,872✔
953
                /* Shortcut this call if none of the special features of this call are requested */
954
                return RET_NERRNO(faccessat(AT_FDCWD, path, access_mode,
13,289✔
955
                                            FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
6,646✔
956

957
        r = chase(path, root, chase_flags, ret_path ? &p : NULL, &path_fd);
442✔
958
        if (r < 0)
226✔
959
                return r;
960
        assert(path_fd >= 0);
33✔
961

962
        r = access_fd(path_fd, access_mode);
33✔
963
        if (r < 0)
33✔
964
                return r;
965

966
        if (ret_path)
33✔
967
                *ret_path = TAKE_PTR(p);
10✔
968

969
        return 0;
970
}
971

972
int chase_and_fopen_unlocked(
396✔
973
                const char *path,
974
                const char *root,
975
                ChaseFlags chase_flags,
976
                const char *open_flags,
977
                char **ret_path,
978
                FILE **ret_file) {
979

980
        _cleanup_free_ char *final_path = NULL;
396✔
981
        _cleanup_close_ int fd = -EBADF;
396✔
982
        int mode_flags, r;
396✔
983

984
        assert(path);
396✔
985
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT|CHASE_MUST_BE_DIRECTORY|CHASE_MUST_BE_SOCKET)));
396✔
986
        assert(open_flags);
396✔
987
        assert(ret_file);
396✔
988

989
        mode_flags = fopen_mode_to_flags(open_flags);
396✔
990
        if (mode_flags < 0)
396✔
991
                return mode_flags;
992

993
        fd = chase_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
531✔
994
        if (fd < 0)
396✔
995
                return fd;
996

997
        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
238✔
998
        if (r < 0)
238✔
999
                return r;
1000

1001
        if (ret_path)
238✔
1002
                *ret_path = TAKE_PTR(final_path);
107✔
1003

1004
        return 0;
1005
}
1006

1007
int chase_and_unlink(const char *path, const char *root, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
1✔
1008
        _cleanup_free_ char *p = NULL, *fname = NULL;
1✔
1009
        _cleanup_close_ int fd = -EBADF;
1✔
1010
        int r;
1✔
1011

1012
        assert(path);
1✔
1013
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
1✔
1014

1015
        fd = chase_and_open(path, root, chase_flags|CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &p);
1✔
1016
        if (fd < 0)
1✔
1017
                return fd;
1018

1019
        r = path_extract_filename(p, &fname);
1✔
1020
        if (r < 0)
1✔
1021
                return r;
1022

1023
        if (unlinkat(fd, fname, unlink_flags) < 0)
1✔
1024
                return -errno;
×
1025

1026
        if (ret_path)
1✔
1027
                *ret_path = TAKE_PTR(p);
1✔
1028

1029
        return 0;
1030
}
1031

1032
int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_flags, char **ret_filename) {
162✔
1033
        int pfd, r;
162✔
1034

1035
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
162✔
1036

1037
        r = chase(path, root, CHASE_PARENT|CHASE_EXTRACT_FILENAME|chase_flags, ret_filename, &pfd);
162✔
1038
        if (r < 0)
162✔
1039
                return r;
162✔
1040

1041
        return pfd;
161✔
1042
}
1043

1044
int chase_and_openat(
40,720✔
1045
                int dir_fd,
1046
                const char *path,
1047
                ChaseFlags chase_flags,
1048
                int open_flags,
1049
                char **ret_path) {
1050

1051
        _cleanup_close_ int path_fd = -EBADF;
40,720✔
1052
        _cleanup_free_ char *p = NULL, *fname = NULL;
40,720✔
1053
        int r;
40,720✔
1054

1055
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_MUST_BE_SOCKET)));
40,720✔
1056

1057
        XOpenFlags xopen_flags = 0;
40,720✔
1058
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_DIRECTORY))
40,720✔
1059
                open_flags |= O_DIRECTORY;
138✔
1060
        if (FLAGS_SET(chase_flags, CHASE_MUST_BE_REGULAR))
40,720✔
1061
                xopen_flags |= XO_REGULAR;
39,312✔
1062

1063
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
40,720✔
1064
                /* Shortcut this call if none of the special features of this call are requested */
1065
                return xopenat_full(dir_fd, path,
×
1066
                                    open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
×
1067
                                    xopen_flags,
1068
                                    MODE_INVALID);
1069

1070
        r = chaseat(dir_fd, path, (chase_flags|CHASE_PARENT)&~CHASE_MUST_BE_REGULAR, &p, &path_fd);
40,720✔
1071
        if (r < 0)
40,720✔
1072
                return r;
1073

1074
        if (!FLAGS_SET(chase_flags, CHASE_PARENT)) {
38,941✔
1075
                r = path_extract_filename(p, &fname);
37,904✔
1076
                if (r < 0 && r != -EADDRNOTAVAIL)
37,904✔
1077
                        return r;
1078
        }
1079

1080
        r = xopenat_full(
116,823✔
1081
                        path_fd,
1082
                        strempty(fname),
39,978✔
1083
                        open_flags|O_NOFOLLOW,
1084
                        xopen_flags,
1085
                        MODE_INVALID);
1086
        if (r < 0)
38,941✔
1087
                return r;
1088

1089
        if (ret_path)
37,712✔
1090
                *ret_path = TAKE_PTR(p);
1,165✔
1091

1092
        return r;
1093
}
1094

1095
int chase_and_opendirat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
616,748✔
1096
        _cleanup_close_ int path_fd = -EBADF;
616,748✔
1097
        _cleanup_free_ char *p = NULL;
616,748✔
1098
        DIR *d;
616,748✔
1099
        int r;
616,748✔
1100

1101
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_MUST_BE_REGULAR|CHASE_MUST_BE_SOCKET)));
616,748✔
1102
        assert(ret_dir);
616,748✔
1103

1104
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0) {
616,748✔
1105
                /* Shortcut this call if none of the special features of this call are requested */
1106
                d = opendir(path);
×
1107
                if (!d)
×
1108
                        return -errno;
×
1109

1110
                *ret_dir = d;
×
1111
                return 0;
×
1112
        }
1113

1114
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
616,872✔
1115
        if (r < 0)
616,748✔
1116
                return r;
1117
        assert(path_fd >= 0);
51,905✔
1118

1119
        d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0);
51,905✔
1120
        if (!d)
51,905✔
1121
                return -errno;
×
1122

1123
        if (ret_path)
51,905✔
1124
                *ret_path = TAKE_PTR(p);
51,781✔
1125

1126
        *ret_dir = d;
51,905✔
1127
        return 0;
51,905✔
1128
}
1129

1130
int chase_and_statat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
1✔
1131
        _cleanup_close_ int path_fd = -EBADF;
1✔
1132
        _cleanup_free_ char *p = NULL;
1✔
1133
        int r;
1✔
1134

1135
        assert(path);
1✔
1136
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
1✔
1137
        assert(ret_stat);
1✔
1138

1139
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
1✔
1140
                /* Shortcut this call if none of the special features of this call are requested */
1141
                return RET_NERRNO(fstatat(AT_FDCWD, path, ret_stat,
×
1142
                                          FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
×
1143

1144
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
1✔
1145
        if (r < 0)
1✔
1146
                return r;
1147
        assert(path_fd >= 0);
1✔
1148

1149
        if (fstat(path_fd, ret_stat) < 0)
1✔
1150
                return -errno;
×
1151

1152
        if (ret_path)
1✔
1153
                *ret_path = TAKE_PTR(p);
1✔
1154

1155
        return 0;
1156
}
1157

1158
int chase_and_accessat(int dir_fd, const char *path, ChaseFlags chase_flags, int access_mode, char **ret_path) {
14✔
1159
        _cleanup_close_ int path_fd = -EBADF;
14✔
1160
        _cleanup_free_ char *p = NULL;
14✔
1161
        int r;
14✔
1162

1163
        assert(path);
14✔
1164
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
14✔
1165

1166
        if (dir_fd == AT_FDCWD && !ret_path && (chase_flags & CHASE_NO_SHORTCUT_MASK) == 0)
14✔
1167
                /* Shortcut this call if none of the special features of this call are requested */
1168
                return RET_NERRNO(faccessat(AT_FDCWD, path, access_mode,
×
1169
                                            FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0));
×
1170

1171
        r = chaseat(dir_fd, path, chase_flags, ret_path ? &p : NULL, &path_fd);
27✔
1172
        if (r < 0)
14✔
1173
                return r;
1174
        assert(path_fd >= 0);
14✔
1175

1176
        r = access_fd(path_fd, access_mode);
14✔
1177
        if (r < 0)
14✔
1178
                return r;
1179

1180
        if (ret_path)
14✔
1181
                *ret_path = TAKE_PTR(p);
1✔
1182

1183
        return 0;
1184
}
1185

1186
int chase_and_fopenat_unlocked(
39,183✔
1187
                int dir_fd,
1188
                const char *path,
1189
                ChaseFlags chase_flags,
1190
                const char *open_flags,
1191
                char **ret_path,
1192
                FILE **ret_file) {
1193

1194
        _cleanup_free_ char *final_path = NULL;
39,183✔
1195
        _cleanup_close_ int fd = -EBADF;
39,183✔
1196
        int mode_flags, r;
39,183✔
1197

1198
        assert(path);
39,183✔
1199
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
39,183✔
1200
        assert(open_flags);
39,183✔
1201
        assert(ret_file);
39,183✔
1202

1203
        mode_flags = fopen_mode_to_flags(open_flags);
39,183✔
1204
        if (mode_flags < 0)
39,183✔
1205
                return mode_flags;
1206

1207
        fd = chase_and_openat(dir_fd, path, chase_flags, mode_flags, ret_path ? &final_path : NULL);
78,365✔
1208
        if (fd < 0)
39,183✔
1209
                return fd;
1210

1211
        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
36,414✔
1212
        if (r < 0)
36,414✔
1213
                return r;
1214

1215
        if (ret_path)
36,414✔
1216
                *ret_path = TAKE_PTR(final_path);
1✔
1217

1218
        return 0;
1219
}
1220

1221
int chase_and_unlinkat(int dir_fd, const char *path, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
272✔
1222
        _cleanup_free_ char *p = NULL, *fname = NULL;
272✔
1223
        _cleanup_close_ int fd = -EBADF;
272✔
1224
        int r;
272✔
1225

1226
        assert(path);
272✔
1227
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP|CHASE_PARENT)));
272✔
1228

1229
        fd = chase_and_openat(dir_fd, path, chase_flags|CHASE_PARENT|CHASE_NOFOLLOW, O_PATH|O_DIRECTORY|O_CLOEXEC, &p);
272✔
1230
        if (fd < 0)
272✔
1231
                return fd;
1232

1233
        r = path_extract_filename(p, &fname);
226✔
1234
        if (r < 0)
226✔
1235
                return r;
1236

1237
        if (unlinkat(fd, fname, unlink_flags) < 0)
226✔
1238
                return -errno;
106✔
1239

1240
        if (ret_path)
120✔
1241
                *ret_path = TAKE_PTR(p);
1✔
1242

1243
        return 0;
1244
}
1245

1246
int chase_and_open_parent_at(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_filename) {
983✔
1247
        int pfd, r;
983✔
1248

1249
        assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
983✔
1250

1251
        r = chaseat(dir_fd, path, CHASE_PARENT|CHASE_EXTRACT_FILENAME|chase_flags, ret_filename, &pfd);
983✔
1252
        if (r < 0)
983✔
1253
                return r;
983✔
1254

1255
        return pfd;
983✔
1256
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc