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

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

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

88.89
/src/shared/dev-setup.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <stdlib.h>
5
#include <unistd.h>
6

7
#include "alloc-util.h"
8
#include "dev-setup.h"
9
#include "fd-util.h"
10
#include "fs-util.h"
11
#include "label-util.h"
12
#include "lock-util.h"
13
#include "log.h"
14
#include "mkdir-label.h"
15
#include "nulstr-util.h"
16
#include "path-util.h"
17
#include "stat-util.h"
18
#include "terminal-util.h"
19
#include "umask-util.h"
20
#include "user-util.h"
21

22
int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
955✔
23
        static const char symlinks[] =
955✔
24
                "-/proc/kcore\0"     "/dev/core\0"
25
                "/proc/self/fd\0"    "/dev/fd\0"
26
                "/proc/self/fd/0\0"  "/dev/stdin\0"
27
                "/proc/self/fd/1\0"  "/dev/stdout\0"
28
                "/proc/self/fd/2\0"  "/dev/stderr\0";
29

30
        int r;
955✔
31

32
        NULSTR_FOREACH_PAIR(j, k, symlinks) {
10,505✔
33
                _cleanup_free_ char *link_name = NULL;
4,775✔
34
                const char *n;
4,775✔
35

36
                if (j[0] == '-') {
4,775✔
37
                        j++;
955✔
38

39
                        if (access(j, F_OK) < 0)
955✔
UNCOV
40
                                continue;
×
41
                }
42

43
                if (prefix) {
4,775✔
44
                        link_name = path_join(prefix, k);
4,475✔
45
                        if (!link_name)
4,475✔
UNCOV
46
                                return -ENOMEM;
×
47

48
                        n = link_name;
49
                } else
50
                        n = k;
51

52
                r = symlink_label(j, n);
4,775✔
53
                if (r < 0)
4,775✔
54
                        log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
240✔
55

56
                if (uid != UID_INVALID || gid != GID_INVALID)
4,775✔
57
                        if (lchown(n, uid, gid) < 0)
1,090✔
58
                                log_debug_errno(errno, "Failed to chown %s: %m", n);
4,775✔
59
        }
60

61
        return 0;
62
}
63

64
int make_inaccessible_nodes(
412✔
65
                const char *parent_dir,
66
                uid_t uid,
67
                gid_t gid) {
68

69
        static const mode_t table[] = {
412✔
70
                S_IFREG,
71
                S_IFDIR,
72
                S_IFIFO,
73
                S_IFSOCK,
74

75
                /* The following two are likely to fail if we lack the privs for it (for example in an userns
76
                 * environment, if CAP_SYS_MKNOD is missing, or if a device node policy prohibits creation of
77
                 * device nodes with a major/minor of 0). But that's entirely fine. Consumers of these files
78
                 * should implement falling back to use a different node then, for example
79
                 * <root>/inaccessible/sock, which is close enough in behaviour and semantics for most uses.
80
                 */
81
                S_IFCHR,
82
                S_IFBLK,
83

84
                /* NB: S_IFLNK is not listed here, as there is no such thing as an inaccessible symlink */
85
        };
86

87
        _cleanup_close_ int parent_fd = -EBADF, inaccessible_fd = -EBADF;
412✔
88
        int r;
412✔
89

90
        if (!parent_dir)
412✔
UNCOV
91
                parent_dir = "/run/systemd";
×
92

93
        BLOCK_WITH_UMASK(0000);
824✔
94

95
        parent_fd = open(parent_dir, O_DIRECTORY|O_CLOEXEC|O_PATH, 0);
412✔
96
        if (parent_fd < 0)
412✔
UNCOV
97
                return -errno;
×
98

99
        inaccessible_fd = open_mkdir_at_full(parent_fd, "inaccessible", O_CLOEXEC, XO_LABEL, 0755);
412✔
100
        if (inaccessible_fd < 0)
412✔
101
                return inaccessible_fd;
102

103
        /* Set up inaccessible (and empty) file nodes of all types. This are used to as mount sources for over-mounting
104
         * ("masking") file nodes that shall become inaccessible and empty for specific containers or services. We try
105
         * to lock down these nodes as much as we can, but otherwise try to match them as closely as possible with the
106
         * underlying file, i.e. in the best case we offer the same node type as the underlying node. */
107

108
        FOREACH_ELEMENT(m, table) {
2,884✔
109
                _cleanup_free_ char *path = NULL;
2,472✔
110
                mode_t inode_type = *m;
2,472✔
111
                const char *fn;
2,472✔
112

113
                fn = inode_type_to_string(inode_type);
2,472✔
114
                path = path_join(parent_dir, fn);
2,472✔
115
                if (!path)
2,472✔
UNCOV
116
                        return log_oom();
×
117

118
                if (S_ISDIR(inode_type))
2,472✔
119
                        r = mkdirat_label(inaccessible_fd, fn, 0000);
412✔
120
                else
121
                        r = mknodat_label(inaccessible_fd, fn, inode_type | 0000, makedev(0, 0));
2,060✔
122
                if (r == -EEXIST) {
2,472✔
123
                        if (fchmodat(inaccessible_fd, fn, 0000, AT_SYMLINK_NOFOLLOW) < 0)
64✔
UNCOV
124
                                log_debug_errno(errno, "Failed to adjust access mode of existing inode '%s', ignoring: %m", path);
×
125
                } else if (r < 0) {
2,408✔
126
                        log_debug_errno(r, "Failed to create '%s', ignoring: %m", path);
122✔
127
                        continue;
122✔
128
                }
129

130
                if (uid_is_valid(uid) || gid_is_valid(gid))
3,388✔
131
                        if (fchownat(inaccessible_fd, fn, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
1,312✔
132
                                log_debug_errno(errno, "Failed to chown '%s', ignoring: %m", path);
2,350✔
133
        }
134

135
        if (fchmod(inaccessible_fd, 0555) < 0)
412✔
136
                log_debug_errno(errno, "Failed to mark inaccessible directory read-only, ignoring: %m");
412✔
137

138
        return 0;
139
}
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