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

systemd / systemd / 18765396043

23 Oct 2025 01:51PM UTC coverage: 72.284% (-0.01%) from 72.295%
18765396043

push

github

YHNdnzj
core: increment start limit counter only when we can start the unit

Otherwise, e.g. requesting to start a unit that is under stopping may
enter the failed state.

This makes
- rename .can_start() -> .test_startable(), and make it allow to return
  boolean and refuse to start units when it returns false,
- refuse earlier to start units that are in the deactivating state, so
  several redundant conditions in .start() can be dropped,
- move checks for unit states mapped to UNIT_ACTIVATING from .start() to
  .test_startable().

Fixes #39247.

13 of 15 new or added lines in 8 files covered. (86.67%)

6946 existing lines in 72 files now uncovered.

304970 of 421905 relevant lines covered (72.28%)

1105466.04 hits per line

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

87.22
/src/shared/machine-bind-user.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <grp.h>
4
#include <pwd.h>
5

6
#include "alloc-util.h"
7
#include "chase.h"
8
#include "fd-util.h"
9
#include "format-util.h"
10
#include "json-util.h"
11
#include "log.h"
12
#include "machine-bind-user.h"
13
#include "path-util.h"
14
#include "string-util.h"
15
#include "strv.h"
16
#include "user-util.h"
17
#include "userdb.h"
18

19
static int check_etc_passwd_collisions(
68✔
20
                const char *directory,
21
                const char *name,
22
                uid_t uid) {
23

24
        _cleanup_fclose_ FILE *f = NULL;
68✔
25
        int r;
68✔
26

27
        assert(name || uid_is_valid(uid));
68✔
28

29
        if (!directory)
68✔
30
                return 0;
31

32
        r = chase_and_fopen_unlocked("/etc/passwd", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
68✔
33
        if (r == -ENOENT)
68✔
34
                return 0; /* no user database? then no user, hence no collision */
35
        if (r < 0)
64✔
36
                return log_error_errno(r, "Failed to open /etc/passwd of machine: %m");
×
37

38
        for (;;) {
543✔
39
                struct passwd *pw;
607✔
40

41
                r = fgetpwent_sane(f, &pw);
607✔
42
                if (r < 0)
607✔
43
                        return log_error_errno(r, "Failed to iterate through /etc/passwd of machine: %m");
64✔
44
                if (r == 0) /* EOF */
607✔
45
                        return 0; /* no collision */
46

47
                if (name && streq_ptr(pw->pw_name, name))
544✔
48
                        return 1; /* name collision */
49
                if (uid_is_valid(uid) && pw->pw_uid == uid)
543✔
50
                        return 1; /* UID collision */
51
        }
52
}
53

54
static int check_etc_group_collisions(
67✔
55
                const char *directory,
56
                const char *name,
57
                gid_t gid) {
58

59
        _cleanup_fclose_ FILE *f = NULL;
67✔
60
        int r;
67✔
61

62
        assert(name || gid_is_valid(gid));
101✔
63

64
        if (!directory)
67✔
65
                return 0;
66

67
        r = chase_and_fopen_unlocked("/etc/group", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
67✔
68
        if (r == -ENOENT)
67✔
69
                return 0; /* no group database? then no group, hence no collision */
70
        if (r < 0)
67✔
71
                return log_error_errno(r, "Failed to open /etc/group of machine: %m");
×
72

73
        for (;;) {
1,452✔
74
                struct group *gr;
1,519✔
75

76
                r = fgetgrent_sane(f, &gr);
1,519✔
77
                if (r < 0)
1,519✔
78
                        return log_error_errno(r, "Failed to iterate through /etc/group of machine: %m");
67✔
79
                if (r == 0)
1,519✔
80
                        return 0; /* no collision */
81

82
                if (name && streq_ptr(gr->gr_name, name))
1,453✔
83
                        return 1; /* name collision */
84
                if (gid_is_valid(gid) && gr->gr_gid == gid)
1,452✔
85
                        return 1; /* gid collision */
86
        }
87
}
88

89
static int convert_user(
34✔
90
                const char *directory,
91
                UserRecord *u,
92
                GroupRecord *g,
93
                uid_t allocate_uid,
94
                const char *shell,
95
                bool shell_copy,
96
                const char *home_mount_directory,
97
                UserRecord **ret_converted_user,
98
                GroupRecord **ret_converted_group) {
99

100
        _cleanup_(group_record_unrefp) GroupRecord *converted_group = NULL;
34✔
UNCOV
101
        _cleanup_(user_record_unrefp) UserRecord *converted_user = NULL;
×
102
        _cleanup_free_ char *h = NULL;
34✔
103
        sd_json_variant *p, *hp = NULL, *ssh = NULL;
34✔
104
        int r;
34✔
105

106
        assert(u);
34✔
107
        assert(g);
34✔
108
        assert(user_record_gid(u) == g->gid);
34✔
109

110
        if (shell_copy)
34✔
111
                shell = u->shell;
6✔
112

113
        r = check_etc_passwd_collisions(directory, u->user_name, UID_INVALID);
34✔
114
        if (r < 0)
34✔
115
                return r;
116
        if (r > 0)
34✔
117
                return log_error_errno(SYNTHETIC_ERRNO(EBUSY),
1✔
118
                                       "Sorry, the user '%s' already exists in the machine.", u->user_name);
119

120
        r = check_etc_group_collisions(directory, g->group_name, GID_INVALID);
33✔
121
        if (r < 0)
33✔
122
                return r;
123
        if (r > 0)
33✔
124
                return log_error_errno(SYNTHETIC_ERRNO(EBUSY),
1✔
125
                                       "Sorry, the group '%s' already exists in the machine.", g->group_name);
126

127
        h = path_join(home_mount_directory, u->user_name);
32✔
128
        if (!h)
32✔
UNCOV
129
                return log_oom();
×
130

131
        /* Acquire the source hashed password array as-is, so that it retains the JSON_VARIANT_SENSITIVE flag */
132
        p = sd_json_variant_by_key(u->json, "privileged");
32✔
133
        if (p) {
32✔
134
                hp = sd_json_variant_by_key(p, "hashedPassword");
32✔
135
                ssh = sd_json_variant_by_key(p, "sshAuthorizedKeys");
32✔
136
        }
137

138
        r = user_record_build(
96✔
139
                        &converted_user,
140
                        SD_JSON_BUILD_OBJECT(
64✔
141
                                        SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(u->user_name)),
142
                                        SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(allocate_uid)),
143
                                        SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(allocate_uid)),
144
                                        SD_JSON_BUILD_PAIR_CONDITION(u->disposition >= 0, "disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(u->disposition))),
145
                                        SD_JSON_BUILD_PAIR("homeDirectory", SD_JSON_BUILD_STRING(h)),
146
                                        SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn")),
147
                                        JSON_BUILD_PAIR_STRING_NON_EMPTY("shell", shell),
148
                                        SD_JSON_BUILD_PAIR("privileged", SD_JSON_BUILD_OBJECT(
149
                                                                           SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(u->hashed_password), "hashedPassword", SD_JSON_BUILD_VARIANT(hp)),
150
                                                                           SD_JSON_BUILD_PAIR_CONDITION(!!ssh, "sshAuthorizedKeys", SD_JSON_BUILD_VARIANT(ssh))))));
151
        if (r < 0)
32✔
UNCOV
152
                return log_error_errno(r, "Failed to build machine user record: %m");
×
153

154
        r = group_record_build(
96✔
155
                        &converted_group,
156
                        SD_JSON_BUILD_OBJECT(
32✔
157
                                        SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(g->group_name)),
158
                                        SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(allocate_uid)),
159
                                        SD_JSON_BUILD_PAIR_CONDITION(g->disposition >= 0, "disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(g->disposition))),
160
                                        SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn"))));
161
        if (r < 0)
32✔
UNCOV
162
                return log_error_errno(r, "Failed to build machine group record: %m");
×
163

164
        *ret_converted_user = TAKE_PTR(converted_user);
32✔
165
        *ret_converted_group = TAKE_PTR(converted_group);
32✔
166

167
        return 0;
32✔
168
}
169

170
static int find_free_uid(const char *directory, uid_t *current_uid) {
34✔
171
        int r;
34✔
172

173
        assert(current_uid);
34✔
174

UNCOV
175
        for (;; (*current_uid)++) {
×
176
                if (*current_uid > MAP_UID_MAX)
34✔
UNCOV
177
                        return log_error_errno(
×
178
                                        SYNTHETIC_ERRNO(EBUSY),
179
                                        "No suitable available UID in range " UID_FMT "…" UID_FMT " in machine detected, can't map user.",
180
                                        MAP_UID_MIN, MAP_UID_MAX);
181

182
                r = check_etc_passwd_collisions(directory, NULL, *current_uid);
34✔
183
                if (r < 0)
34✔
184
                        return r;
185
                if (r > 0) /* already used */
34✔
UNCOV
186
                        continue;
×
187

188
                /* We want to use the UID also as GID, hence check for it in /etc/group too */
189
                r = check_etc_group_collisions(directory, NULL, (gid_t) *current_uid);
34✔
190
                if (r <= 0)
34✔
191
                        return r;
192
        }
193
}
194

195
MachineBindUserContext* machine_bind_user_context_free(MachineBindUserContext *c) {
13✔
196
        if (!c)
13✔
197
                return NULL;
198

199
        FOREACH_ARRAY(d, c->data, c->n_data) {
30✔
200
                user_record_unref(d->host_user);
17✔
201
                group_record_unref(d->host_group);
17✔
202
                user_record_unref(d->payload_user);
17✔
203
                group_record_unref(d->payload_group);
17✔
204
        }
205

206
        return mfree(c);
13✔
207
}
208

209
int machine_bind_user_prepare(
251✔
210
                const char *directory,
211
                char **bind_user,
212
                const char *bind_user_shell,
213
                bool bind_user_shell_copy,
214
                const char *bind_user_home_mount_directory,
215
                MachineBindUserContext **ret) {
216

217
        _cleanup_(machine_bind_user_context_freep) MachineBindUserContext *c = NULL;
251✔
218
        uid_t current_uid = MAP_UID_MIN;
251✔
219
        int r;
251✔
220

221
        assert(ret);
251✔
222

223
        /* This resolves the users specified in 'bind_user', generates a minimalized JSON user + group record
224
         * for it to stick in the machine, allocates a UID/GID for it, and updates the custom mount table,
225
         * to include an appropriate bind mount mapping.
226
         *
227
         * This extends the passed custom_mounts/n_custom_mounts with the home directories, and allocates a
228
         * new BindUserContext for the user records */
229

230
        if (strv_isempty(bind_user)) {
251✔
231
                *ret = NULL;
227✔
232
                return 0;
227✔
233
        }
234

235
        c = new0(MachineBindUserContext, 1);
24✔
236
        if (!c)
24✔
UNCOV
237
                return log_oom();
×
238

239
        STRV_FOREACH(n, bind_user) {
56✔
240
                _cleanup_(user_record_unrefp) UserRecord *u = NULL, *cu = NULL;
34✔
241
                _cleanup_(group_record_unrefp) GroupRecord *g = NULL, *cg = NULL;
34✔
242

243
                r = userdb_by_name(*n, /* match= */ NULL, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, &u);
34✔
244
                if (r < 0)
34✔
UNCOV
245
                        return log_error_errno(r, "Failed to resolve user '%s': %m", *n);
×
246

247
                /* For now, let's refuse mapping the root/nobody users explicitly. The records we generate
248
                 * are strictly additive, nss-systemd is typically placed last in /etc/nsswitch.conf. Thus
249
                 * even if we wanted, we couldn't override the root or nobody user records. Note we also
250
                 * check for name conflicts in /etc/passwd + /etc/group later on, which would usually filter
251
                 * out root/nobody too, hence these checks might appear redundant — but they actually are
252
                 * not, as we want to support environments where /etc/passwd and /etc/group are non-existent,
253
                 * and the user/group databases fully synthesized at runtime. Moreover, the name of the
254
                 * user/group name of the "nobody" account differs between distros, hence a check by numeric
255
                 * UID is safer. */
256
                if (user_record_is_root(u))
34✔
UNCOV
257
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Mapping 'root' user not supported, sorry.");
×
258

259
                if (user_record_is_nobody(u))
34✔
UNCOV
260
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Mapping 'nobody' user not supported, sorry.");
×
261

262
                if (!uid_is_valid(u->uid))
34✔
UNCOV
263
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot bind user with no UID, refusing.");
×
264

265
                r = groupdb_by_gid(user_record_gid(u), /* match= */ NULL, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, &g);
34✔
266
                if (r < 0)
34✔
UNCOV
267
                        return log_error_errno(r, "Failed to resolve group of user '%s': %m", u->user_name);
×
268

269
                /* We want to synthesize exactly one user + group from the host into the machine. This only
270
                 * makes sense if the user on the host has its own private group. We can't reasonably check
271
                 * this, so we just check of the name of user and group match.
272
                 *
273
                 * One of these days we might want to support users in a shared/common group too, but it's
274
                 * not clear to me how this would have to be mapped, precisely given that the common group
275
                 * probably already exists in the machine. */
276
                if (!streq(u->user_name, g->group_name))
34✔
UNCOV
277
                        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
×
278
                                               "Sorry, mapping users without private groups is currently not supported.");
279

280
                r = find_free_uid(directory, &current_uid);
34✔
281
                if (r < 0)
34✔
282
                        return r;
283

284
                r = convert_user(
34✔
285
                                directory,
286
                                u, g,
287
                                current_uid,
288
                                bind_user_shell,
289
                                bind_user_shell_copy,
290
                                bind_user_home_mount_directory,
291
                                &cu, &cg);
292
                if (r < 0)
34✔
293
                        return r;
294

295
                if (!GREEDY_REALLOC(c->data, c->n_data + 1))
32✔
UNCOV
296
                        return log_oom();
×
297

298
                c->data[c->n_data++] = (MachineBindUserData) {
32✔
299
                        .host_user = TAKE_PTR(u),
32✔
300
                        .host_group = TAKE_PTR(g),
32✔
301
                        .payload_user = TAKE_PTR(cu),
32✔
302
                        .payload_group = TAKE_PTR(cg),
32✔
303
                };
304

305
                current_uid++;
32✔
306
        }
307

308
        *ret = TAKE_PTR(c);
22✔
309
        return 1;
22✔
310
}
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