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

systemd / systemd / 13912360373

17 Mar 2025 10:34PM UTC coverage: 71.946% (+0.03%) from 71.915%
13912360373

push

github

web-flow
nsresourced,vmspawn: allow unpriv "tap" based networking in vmspawn (#36688)

This extends nsresourced to also allow delegation of a network tap
device (in addition to veth) to unpriv clients, with a strictly enforced
naming scheme.

also tightens security on a couple of things:

* enforces polkit on all nsresourced ops too (though by default still
everything is allowed)
* put a limit on delegated network devices
* forcibly clean up delegated network devices when the userns goes away

145 of 375 new or added lines in 14 files covered. (38.67%)

2324 existing lines in 47 files now uncovered.

296268 of 411794 relevant lines covered (71.95%)

711485.52 hits per line

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

0.0
/src/mountfsd/mountwork.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/mount.h>
4
#include <linux/loop.h>
5

6
#include "sd-daemon.h"
7
#include "sd-varlink.h"
8

9
#include "argv-util.h"
10
#include "bus-polkit.h"
11
#include "chase.h"
12
#include "discover-image.h"
13
#include "dissect-image.h"
14
#include "env-util.h"
15
#include "errno-util.h"
16
#include "fd-util.h"
17
#include "io-util.h"
18
#include "json-util.h"
19
#include "main-func.h"
20
#include "missing_syscall.h"
21
#include "namespace-util.h"
22
#include "nsresource.h"
23
#include "nulstr-util.h"
24
#include "os-util.h"
25
#include "process-util.h"
26
#include "stat-util.h"
27
#include "string-table.h"
28
#include "uid-classification.h"
29
#include "uid-range.h"
30
#include "user-util.h"
31
#include "varlink-io.systemd.MountFileSystem.h"
32
#include "varlink-util.h"
33

34
#define ITERATIONS_MAX 64U
35
#define RUNTIME_MAX_USEC (5 * USEC_PER_MINUTE)
36
#define PRESSURE_SLEEP_TIME_USEC (50 * USEC_PER_MSEC)
37
#define LISTEN_IDLE_USEC (90 * USEC_PER_SEC)
38

39
static const ImagePolicy image_policy_untrusted = {
40
        .n_policies = 2,
41
        .policies = {
42
                { PARTITION_ROOT,     PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT },
43
                { PARTITION_USR,      PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT },
44
        },
45
        .default_flags = PARTITION_POLICY_IGNORE,
46
};
47

48
static int json_dispatch_image_policy(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
×
49
        _cleanup_(image_policy_freep) ImagePolicy *q = NULL;
×
50
        ImagePolicy **p = ASSERT_PTR(userdata);
×
51
        int r;
×
52

53
        assert(p);
×
54

55
        if (sd_json_variant_is_null(variant)) {
×
56
                *p = image_policy_free(*p);
×
57
                return 0;
×
58
        }
59

60
        if (!sd_json_variant_is_string(variant))
×
61
                return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
×
62

63
        r = image_policy_from_string(sd_json_variant_string(variant), &q);
×
64
        if (r < 0)
×
65
                return json_log(variant, flags, r, "JSON field '%s' is not a valid image policy.", strna(name));
×
66

67
        image_policy_free(*p);
×
68
        *p = TAKE_PTR(q);
×
69
        return 0;
×
70
}
71

72
typedef struct MountImageParameters {
73
        unsigned image_fd_idx;
74
        unsigned userns_fd_idx;
75
        int read_only;
76
        int growfs;
77
        char *password;
78
        ImagePolicy *image_policy;
79
} MountImageParameters;
80

81
static void mount_image_parameters_done(MountImageParameters *p) {
×
82
        assert(p);
×
83

84
        p->password = erase_and_free(p->password);
×
85
        p->image_policy = image_policy_free(p->image_policy);
×
86
}
×
87

88
static int validate_image_fd(int fd, MountImageParameters *p) {
×
89
        int r, fl;
×
90

91
        assert(fd >= 0);
×
92
        assert(p);
×
93

94
        r = fd_verify_regular(fd);
×
95
        if (r < 0)
×
96
                return r;
97

98
        fl = fd_verify_safe_flags(fd);
×
99
        if (fl < 0)
×
100
                return log_debug_errno(fl, "Image file descriptor has unsafe flags set: %m");
×
101

102
        switch (fl & O_ACCMODE) {
×
103

104
        case O_RDONLY:
×
105
                p->read_only = true;
×
106
                break;
×
107

108
        case O_RDWR:
109
                break;
110

111
        default:
112
                return -EBADF;
113
        }
114

115
        return 0;
116
}
117

118
static int verify_trusted_image_fd_by_path(int fd) {
×
119
        _cleanup_free_ char *p = NULL;
×
120
        struct stat sta;
×
121
        int r;
×
122

123
        assert(fd >= 0);
×
124

125
        r = secure_getenv_bool("SYSTEMD_MOUNTFSD_TRUSTED_DIRECTORIES");
×
126
        if (r == -ENXIO)  {
×
127
                if (!DEFAULT_MOUNTFSD_TRUSTED_DIRECTORIES) {
×
128
                        log_debug("Trusted directory mechanism disabled at compile time.");
×
129
                        return false;
×
130
                }
131
        } else if (r < 0) {
×
132
                log_debug_errno(r, "Failed to parse $SYSTEMD_MOUNTFSD_TRUSTED_DIRECTORIES environment variable, not trusting any image.");
×
133
                return false;
×
134
        } else if (!r) {
×
135
                log_debug("Trusted directory mechanism disabled via $SYSTEMD_MOUNTFSD_TRUSTED_DIRECTORIES environment variable.");
×
136
                return false;
×
137
        }
138

139
        r = fd_get_path(fd, &p);
×
140
        if (r < 0)
×
141
                return log_debug_errno(r, "Failed to get path of passed image file descriptor: %m");
×
142
        if (fstat(fd, &sta) < 0)
×
143
                return log_debug_errno(errno, "Failed to stat() passed image file descriptor: %m");
×
144

145
        log_debug("Checking if image '%s' is in trusted directories.", p);
×
146

147
        for (ImageClass c = 0; c < _IMAGE_CLASS_MAX; c++)
×
148
                NULSTR_FOREACH(s, image_search_path[c]) {
×
149
                        _cleanup_close_ int dir_fd = -EBADF, inode_fd = -EBADF;
×
150
                        _cleanup_free_ char *q = NULL;
×
151
                        struct stat stb;
×
152
                        const char *e;
×
153

154
                        r = chase(s, NULL, CHASE_SAFE, &q, &dir_fd);
×
155
                        if (r == -ENOENT)
×
156
                                continue;
×
157
                        if (r < 0) {
×
158
                                log_warning_errno(r, "Failed to resolve search path '%s', ignoring: %m", s);
×
159
                                continue;
×
160
                        }
161

162
                        /* Check that the inode refers to a file immediately inside the image directory,
163
                         * i.e. not the image directory itself, and nothing further down the tree */
164
                        e = path_startswith(p, q);
×
165
                        if (isempty(e))
×
166
                                continue;
×
167

168
                        e += strspn(e, "/");
×
169
                        if (!filename_is_valid(e))
×
170
                                continue;
×
171

172
                        r = chaseat(dir_fd, e, CHASE_SAFE, NULL, &inode_fd);
×
173
                        if (r < 0)
×
174
                                return log_error_errno(r, "Couldn't verify that specified image '%s' is in search path '%s': %m", p, s);
×
175

176
                        if (fstat(inode_fd, &stb) < 0)
×
177
                                return log_error_errno(errno, "Failed to stat image file '%s/%s': %m", q, e);
×
178

179
                        if (stat_inode_same(&sta, &stb)) {
×
180
                                log_debug("Image '%s' is *in* trusted directories.", p);
×
181
                                return true; /* Yay */
×
182
                        }
183
                }
184

185
        log_debug("Image '%s' is *not* in trusted directories.", p);
×
186
        return false;
187
}
188

189
static int determine_image_policy(
×
190
                int image_fd,
191
                bool trusted,
192
                ImagePolicy *client_policy,
193
                ImagePolicy **ret) {
194

195
        _cleanup_(image_policy_freep) ImagePolicy *envvar_policy = NULL;
×
196
        const ImagePolicy *default_policy;
×
197
        const char *envvar, *e;
×
198
        int r;
×
199

200
        assert(image_fd >= 0);
×
201
        assert(ret);
×
202

203
        if (trusted) {
×
204
                envvar = "SYSTEMD_MOUNTFSD_IMAGE_POLICY_TRUSTED";
205
                default_policy = &image_policy_allow;
206
        } else {
207
                envvar = "SYSTEMD_MOUNTFSD_IMAGE_POLICY_UNTRUSTED";
×
208
                default_policy = &image_policy_untrusted;
×
209
        }
210

211
        e = secure_getenv(envvar);
×
212
        if (e) {
×
213
                r = image_policy_from_string(e, &envvar_policy);
×
214
                if (r < 0)
×
215
                        return log_error_errno(r, "Failed to parse image policy supplied via $%s: %m", envvar);
×
216

217
                default_policy = envvar_policy;
×
218
        }
219

220
        return image_policy_intersect(default_policy, client_policy, ret);
×
221
}
222

223
static int validate_userns(sd_varlink *link, int *userns_fd) {
×
224
        int r;
×
225

226
        assert(link);
×
227
        assert(userns_fd);
×
228

229
        if (*userns_fd < 0)
×
230
                return 0;
231

232
        r = fd_verify_safe_flags(*userns_fd);
×
233
        if (r < 0)
×
234
                return log_debug_errno(r, "User namespace file descriptor has unsafe flags set: %m");
×
235

236
        r = fd_is_namespace(*userns_fd, NAMESPACE_USER);
×
237
        if (r < 0)
×
238
                return r;
239
        if (r == 0)
×
240
                return sd_varlink_error_invalid_parameter_name(link, "userNamespaceFileDescriptor");
×
241

242
        /* Our own host user namespace? Then close the fd, and handle it as if none was specified. */
243
        r = is_our_namespace(*userns_fd, NAMESPACE_USER);
×
244
        if (r < 0)
×
245
                return log_debug_errno(r, "Failed to determine if user namespace provided by client is our own.");
×
246
        if (r > 0) {
×
247
                log_debug("User namespace provided by client is our own.");
×
248
                *userns_fd = safe_close(*userns_fd);
×
249
        }
250

251
        return 0;
252
}
253

254
static int vl_method_mount_image(
×
255
                sd_varlink *link,
256
                sd_json_variant *parameters,
257
                sd_varlink_method_flags_t flags,
258
                void *userdata) {
259

260
        static const sd_json_dispatch_field dispatch_table[] = {
×
261
                { "imageFileDescriptor",         SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,      offsetof(MountImageParameters, image_fd_idx),  SD_JSON_MANDATORY },
262
                { "userNamespaceFileDescriptor", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,      offsetof(MountImageParameters, userns_fd_idx), 0 },
263
                { "readOnly",                    SD_JSON_VARIANT_BOOLEAN,  sd_json_dispatch_tristate,  offsetof(MountImageParameters, read_only),     0 },
264
                { "growFileSystems",             SD_JSON_VARIANT_BOOLEAN,  sd_json_dispatch_tristate,  offsetof(MountImageParameters, growfs),        0 },
265
                { "password",                    SD_JSON_VARIANT_STRING,   sd_json_dispatch_string,    offsetof(MountImageParameters, password),      0 },
266
                { "imagePolicy",                 SD_JSON_VARIANT_STRING,   json_dispatch_image_policy, offsetof(MountImageParameters, image_policy),  0 },
267
                VARLINK_DISPATCH_POLKIT_FIELD,
268
                {}
269
        };
270

271
        _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT;
×
272
        _cleanup_(mount_image_parameters_done) MountImageParameters p = {
×
273
                .image_fd_idx = UINT_MAX,
274
                .userns_fd_idx = UINT_MAX,
275
                .read_only = -1,
276
                .growfs = -1,
277
        };
278
        _cleanup_(dissected_image_unrefp) DissectedImage *di = NULL;
×
279
        _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
×
280
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *aj = NULL;
×
281
        _cleanup_close_ int image_fd = -EBADF, userns_fd = -EBADF;
×
282
        _cleanup_(image_policy_freep) ImagePolicy *use_policy = NULL;
×
283
        Hashmap **polkit_registry = ASSERT_PTR(userdata);
×
284
        _cleanup_free_ char *ps = NULL;
×
285
        bool image_is_trusted = false;
×
286
        int r;
×
287

288
        assert(link);
×
289
        assert(parameters);
×
290

291
        sd_json_variant_sensitive(parameters); /* might contain passwords */
×
292

293
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
×
294
        if (r != 0)
×
295
                return r;
296

297
        if (p.image_fd_idx != UINT_MAX) {
×
298
                image_fd = sd_varlink_peek_dup_fd(link, p.image_fd_idx);
×
299
                if (image_fd < 0)
×
300
                        return log_debug_errno(image_fd, "Failed to peek image fd from client: %m");
×
301
        }
302

303
        if (p.userns_fd_idx != UINT_MAX) {
×
304
                userns_fd = sd_varlink_peek_dup_fd(link, p.userns_fd_idx);
×
305
                if (userns_fd < 0)
×
306
                        return log_debug_errno(userns_fd, "Failed to peek user namespace fd from client: %m");
×
307
        }
308

309
        r = validate_image_fd(image_fd, &p);
×
310
        if (r < 0)
×
311
                return r;
312

313
        r = validate_userns(link, &userns_fd);
×
314
        if (r != 0)
×
315
                return r;
316

317
        r = verify_trusted_image_fd_by_path(image_fd);
×
318
        if (r < 0)
×
319
                return r;
320
        image_is_trusted = r;
×
321

322
        const char *polkit_details[] = {
×
323
                "read_only", one_zero(p.read_only > 0),
×
324
                NULL,
325
        };
326

327
        const char *polkit_action, *polkit_untrusted_action;
×
328
        PolkitFlags polkit_flags;
×
329
        if (userns_fd < 0) {
×
330
                /* Mount into the host user namespace */
331
                polkit_action = "io.systemd.mount-file-system.mount-image";
332
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-image";
333
                polkit_flags = 0;
334
        } else {
335
                /* Mount into a private user namespace */
336
                polkit_action = "io.systemd.mount-file-system.mount-image-privately";
×
337
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-image-privately";
×
338

339
                /* If polkit is not around, let's allow mounting authenticated images by default */
340
                polkit_flags = POLKIT_DEFAULT_ALLOW;
×
341
        }
342

343
        /* Let's definitely acquire the regular action privilege, for mounting properly signed images */
344
        r = varlink_verify_polkit_async_full(
×
345
                        link,
346
                        /* bus= */ NULL,
347
                        polkit_action,
348
                        polkit_details,
349
                        /* good_user= */ UID_INVALID,
350
                        polkit_flags,
351
                        polkit_registry);
352
        if (r <= 0)
×
353
                return r;
354

355
        /* Generate the common dissection directory here. We are not going to use it, but the clients might,
356
         * and they likely are unprivileged, hence cannot create it themselves. Hence let's just create it
357
         * here, if it is missing. */
358
        r = get_common_dissect_directory(NULL);
×
359
        if (r < 0)
×
360
                return r;
361

362
        r = loop_device_make(
×
363
                        image_fd,
364
                        p.read_only == 0 ? O_RDONLY : O_RDWR,
×
365
                        0,
366
                        UINT64_MAX,
367
                        UINT32_MAX,
368
                        LO_FLAGS_PARTSCAN,
369
                        LOCK_EX,
370
                        &loop);
371
        if (r < 0)
×
372
                return r;
373

374
        DissectImageFlags dissect_flags =
×
375
                (p.read_only == 0 ? DISSECT_IMAGE_READ_ONLY : 0) |
×
376
                (p.growfs != 0 ? DISSECT_IMAGE_GROWFS : 0) |
×
377
                DISSECT_IMAGE_DISCARD_ANY |
378
                DISSECT_IMAGE_FSCK |
379
                DISSECT_IMAGE_ADD_PARTITION_DEVICES |
380
                DISSECT_IMAGE_PIN_PARTITION_DEVICES |
×
381
                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY;
382

383
        /* Let's see if we have acquired the privilege to mount untrusted images already */
384
        bool polkit_have_untrusted_action =
×
385
                varlink_has_polkit_action(link, polkit_untrusted_action, polkit_details, polkit_registry);
×
386

387
        for (;;) {
×
388
                use_policy = image_policy_free(use_policy);
×
389
                ps = mfree(ps);
×
390

391
                /* We use the image policy for trusted images if either the path is below a trusted
392
                 * directory, or if we have already acquired a PK authentication that tells us that untrusted
393
                 * images are OK */
394
                bool use_trusted_policy =
×
395
                        image_is_trusted ||
396
                        polkit_have_untrusted_action;
397

398
                r = determine_image_policy(
×
399
                                image_fd,
400
                                use_trusted_policy,
401
                                p.image_policy,
402
                                &use_policy);
403
                if (r < 0)
×
404
                        return r;
405

406
                r = image_policy_to_string(use_policy, /* simplify= */ true, &ps);
×
407
                if (r < 0)
×
408
                        return r;
409

410
                log_debug("Using image policy: %s", ps);
×
411

412
                r = dissect_loop_device(
×
413
                                loop,
414
                                &verity,
415
                                /* mount_options= */ NULL,
416
                                use_policy,
417
                                dissect_flags,
418
                                &di);
419
                if (r == -ENOPKG)
×
420
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.IncompatibleImage", NULL);
×
421
                if (r == -ENOTUNIQ)
×
422
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.MultipleRootPartitionsFound", NULL);
×
423
                if (r == -ENXIO)
×
424
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.RootPartitionNotFound", NULL);
×
425
                if (r == -ERFKILL) {
×
426
                        /* The image policy refused this, let's retry after trying to get PolicyKit */
427

428
                        if (!polkit_have_untrusted_action) {
×
429
                                log_debug("Denied by image policy. Trying a stronger polkit authentication before continuing.");
×
430
                                r = varlink_verify_polkit_async_full(
×
431
                                                link,
432
                                                /* bus= */ NULL,
433
                                                polkit_untrusted_action,
434
                                                polkit_details,
435
                                                /* good_user= */ UID_INVALID,
436
                                                /* flags= */ 0,                   /* NB: the image cannot be authenticated, hence unless PK is around to allow this anyway, fail! */
437
                                                polkit_registry);
438
                                if (r <= 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
×
439
                                        return r;
440
                                if (r > 0) {
×
441
                                        /* Try again, now that we know the client has enough privileges. */
442
                                        log_debug("Denied by image policy, retrying after polkit authentication.");
×
443
                                        polkit_have_untrusted_action = true;
×
444
                                        continue;
×
445
                                }
446
                        }
447

448
                        return sd_varlink_error(link, "io.systemd.MountFileSystem.DeniedByImagePolicy", NULL);
×
449
                }
450
                if (r < 0)
×
451
                        return r;
452

453
                /* Success */
454
                break;
×
455
        }
456

457
        r = dissected_image_load_verity_sig_partition(
×
458
                        di,
459
                        loop->fd,
×
460
                        &verity);
461
        if (r < 0)
×
462
                return r;
463

464
        r = dissected_image_decrypt(
×
465
                        di,
466
                        p.password,
×
467
                        &verity,
468
                        dissect_flags);
469
        if (r == -ENOKEY) /* new dm-verity userspace returns ENOKEY if the dm-verity signature key is not in
×
470
                           * key chain. That's great. */
471
                return sd_varlink_error(link, "io.systemd.MountFileSystem.KeyNotFound", NULL);
×
472
        if (r == -EBUSY) /* DM kernel subsystem is shit with returning useful errors hence we keep retrying
×
473
                          * under the assumption that some errors are transitional. Which the errors might
474
                          * not actually be. After all retries failed we return EBUSY. Let's turn that into a
475
                          * generic Verity error. It's not very helpful, could mean anything, but at least it
476
                          * gives client a clear idea that this has to do with Verity. */
477
                return sd_varlink_error(link, "io.systemd.MountFileSystem.VerityFailure", NULL);
×
478
        if (r < 0)
×
479
                return r;
480

481
        r = dissected_image_mount(
×
482
                        di,
483
                        /* where= */ NULL,
484
                        /* uid_shift= */ UID_INVALID,
485
                        /* uid_range= */ UID_INVALID,
486
                        userns_fd,
487
                        dissect_flags);
488
        if (r < 0)
×
489
                return r;
490

491
        for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) {
×
492
                DissectedPartition *pp = di->partitions + d;
×
493
                int fd_idx;
×
494

495
                if (!pp->found)
×
496
                        continue;
×
497

498
                if (pp->fsmount_fd < 0)
×
499
                        continue;
×
500

501
                if (userns_fd >= 0) {
×
502
                        r = nsresource_add_mount(userns_fd, pp->fsmount_fd);
×
503
                        if (r < 0)
×
504
                                return r;
×
505
                }
506

507
                fd_idx = sd_varlink_push_fd(link, pp->fsmount_fd);
×
508
                if (fd_idx < 0)
×
509
                        return fd_idx;
510

511
                TAKE_FD(pp->fsmount_fd);
×
512

513
                const char *m = partition_mountpoint_to_string(d);
×
514
                _cleanup_strv_free_ char **l = NULL;
×
515
                if (!isempty(m)) {
×
516
                        l = strv_split_nulstr(m);
×
517
                        if (!l)
×
518
                                return log_oom_debug();
×
519
                }
520

521
                r = sd_json_variant_append_arraybo(
×
522
                                &aj,
523
                                SD_JSON_BUILD_PAIR("designator", SD_JSON_BUILD_STRING(partition_designator_to_string(d))),
524
                                SD_JSON_BUILD_PAIR("writable", SD_JSON_BUILD_BOOLEAN(pp->rw)),
525
                                SD_JSON_BUILD_PAIR("growFileSystem", SD_JSON_BUILD_BOOLEAN(pp->growfs)),
526
                                SD_JSON_BUILD_PAIR_CONDITION(pp->partno > 0, "partitionNumber", SD_JSON_BUILD_INTEGER(pp->partno)),
527
                                SD_JSON_BUILD_PAIR_CONDITION(pp->architecture > 0, "architecture", SD_JSON_BUILD_STRING(architecture_to_string(pp->architecture))),
528
                                SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(pp->uuid), "partitionUuid", SD_JSON_BUILD_UUID(pp->uuid)),
529
                                SD_JSON_BUILD_PAIR("fileSystemType", SD_JSON_BUILD_STRING(dissected_partition_fstype(pp))),
530
                                SD_JSON_BUILD_PAIR_CONDITION(!!pp->label, "partitionLabel", SD_JSON_BUILD_STRING(pp->label)),
531
                                SD_JSON_BUILD_PAIR("size", SD_JSON_BUILD_INTEGER(pp->size)),
532
                                SD_JSON_BUILD_PAIR("offset", SD_JSON_BUILD_INTEGER(pp->offset)),
533
                                SD_JSON_BUILD_PAIR("mountFileDescriptor", SD_JSON_BUILD_INTEGER(fd_idx)),
534
                                JSON_BUILD_PAIR_STRV_NON_EMPTY("mountPoint", l));
535
                if (r < 0)
×
536
                        return r;
537
        }
538

539
        loop_device_relinquish(loop);
×
540

541
        return sd_varlink_replybo(
×
542
                        link,
543
                        SD_JSON_BUILD_PAIR("partitions", SD_JSON_BUILD_VARIANT(aj)),
544
                        SD_JSON_BUILD_PAIR("imagePolicy", SD_JSON_BUILD_STRING(ps)),
545
                        SD_JSON_BUILD_PAIR("imageSize", SD_JSON_BUILD_INTEGER(di->image_size)),
546
                        SD_JSON_BUILD_PAIR("sectorSize", SD_JSON_BUILD_INTEGER(di->sector_size)),
547
                        SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(di->image_uuid), "imageUuid", SD_JSON_BUILD_UUID(di->image_uuid)));
548
}
549

550
typedef enum MountMapMode {
551
        MOUNT_MAP_AUTO = 0,     /* determine automatically from image and caller */
552
        MOUNT_MAP_ROOT,         /* map caller's UID to root in namespace (map 1 UID only) */
553
        MOUNT_MAP_FOREIGN,      /* map foreign UID range to base in namespace (map 64K) */
554
        MOUNT_MAP_IDENTITY,     /* apply identity mapping (map 64K) */
555
        _MOUNT_MAP_MODE_MAX,
556
        _MOUNT_MAP_MODE_INVALID = -EINVAL,
557
} MountMapMode;
558

559
static const char *const mount_map_mode_table[_MOUNT_MAP_MODE_MAX] = {
560
        [MOUNT_MAP_AUTO]     = "auto",
561
        [MOUNT_MAP_ROOT]     = "root",
562
        [MOUNT_MAP_FOREIGN]  = "foreign",
563
        [MOUNT_MAP_IDENTITY] = "identity",
564
};
565

566
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(mount_map_mode, MountMapMode);
×
567

568
typedef struct MountDirectoryParameters {
569
        MountMapMode mode;
570
        unsigned directory_fd_idx;
571
        unsigned userns_fd_idx;
572
        int read_only;
573
} MountDirectoryParameters;
574

575
typedef enum DirectoryOwnership {
576
        DIRECTORY_IS_ROOT_PEER_OWNED,  /* This is returned if the directory is owned by the root user and the peer is root */
577
        DIRECTORY_IS_ROOT_OWNED,       /* This is returned if the directory is owned by the root user (and the peer user is not root) */
578
        DIRECTORY_IS_PEER_OWNED,       /* This is returned if the directory is owned by the peer user (who is not root) */
579
        DIRECTORY_IS_FOREIGN_OWNED,    /* This is returned if the directory is owned by the foreign UID range */
580
        DIRECTORY_IS_OTHERWISE_OWNED,  /* This is returned if the directory is owned by something else */
581
        _DIRECTORY_OWNERSHIP_MAX,
582
        _DIRECTORY_OWNERSHIP_ERRNO_MAX = -ERRNO_MAX, /* Guarantee the whole negative errno range fits */
583
} DirectoryOwnership;
584

585
static MountMapMode default_mount_map_mode(DirectoryOwnership ownership) {
×
586
        /* Derives a suitable mapping mode from the ownership of the base tree */
587

588
        switch (ownership) {
×
589
        case DIRECTORY_IS_PEER_OWNED:
590
                return MOUNT_MAP_ROOT;     /* Map the peer's UID to root in the container */
591

592
        case DIRECTORY_IS_FOREIGN_OWNED:
×
593
                return MOUNT_MAP_FOREIGN;  /* Map the foreign UID range to the container's UID range */
×
594

595
        case DIRECTORY_IS_ROOT_PEER_OWNED:
×
596
        case DIRECTORY_IS_ROOT_OWNED:
597
        case DIRECTORY_IS_OTHERWISE_OWNED:
598
                return MOUNT_MAP_IDENTITY; /* Don't map */
×
599

600
        default:
×
601
                return _MOUNT_MAP_MODE_INVALID;
×
602
        }
603
}
604

605
static JSON_DISPATCH_ENUM_DEFINE(dispatch_mount_directory_mode, MountMapMode, mount_map_mode_from_string);
×
606

607
static DirectoryOwnership validate_directory_fd(int fd, uid_t peer_uid) {
×
608
        int r, fl;
×
609

610
        assert(fd >= 0);
×
611

612
        /* Checks if the specified directory fd looks sane. Returns a DirectoryOwnership that categorizes the
613
         * ownership situation in comparison to the peer's UID.
614
         *
615
         * Note one key difference to image validation (as implemented above): for regular files if the
616
         * client provided us with an open fd it implies the client has access, as well as what kind of
617
         * access (i.e. ro or rw). But for directories this doesn't work the same way, as directories are
618
         * always opened read-only only. Hence we use a different mechanism to validate access to them: we
619
         * check if the directory is owned by the peer UID or by the foreign UID range (in the latter case
620
         * one of the parent directories must be owned by the peer though). */
621

622
        struct stat st;
×
623
        if (fstat(fd, &st) < 0)
×
624
                return log_debug_errno(errno, "Failed to stat() directory fd: %m");
×
625

626
        r = stat_verify_directory(&st);
×
627
        if (r < 0)
×
628
                return r;
629

630
        fl = fd_verify_safe_flags_full(fd, O_DIRECTORY);
×
631
        if (fl < 0)
×
632
                return log_debug_errno(fl, "Directory file descriptor has unsafe flags set: %m");
×
633

634
        if (st.st_uid == 0) {
×
635
                if (peer_uid == 0) {
×
636
                        log_debug("Directory file descriptor points to root owned directory, who is also the peer.");
×
637
                        return DIRECTORY_IS_ROOT_PEER_OWNED;
×
638
                }
639
                log_debug("Directory file descriptor points to root owned directory.");
×
640
                return DIRECTORY_IS_ROOT_OWNED;
×
641
        }
642
        if (st.st_uid == peer_uid) {
×
643
                log_debug("Directory file descriptor points to peer owned directory.");
×
644
                return DIRECTORY_IS_PEER_OWNED;
×
645
        }
646

647
        /* For bind mounted directories we check if they are either owned by the client's UID, or by the
648
         * foreign UID set, but in that case the parent directory must be owned by the client's UID, or some
649
         * directory iteratively up the chain */
650

651
        _cleanup_close_ int parent_fd = -EBADF;
×
652
        unsigned n_level;
653
        for (n_level = 0; n_level < 16; n_level++) {
×
654
                /* Stop iteration if we find a directory up the tree that is neither owned by the user, nor is from the foreign UID range */
655
                if (!uid_is_foreign(st.st_uid) || !gid_is_foreign(st.st_gid)) {
×
656
                        log_debug("Directory file descriptor points to directory which itself or its parents is neither owned by foreign UID range nor by the user.");
×
657
                        return DIRECTORY_IS_OTHERWISE_OWNED;
×
658
                }
659

660
                /* If the peer is root, then it doesn't matter if we find a parent owned by root, let's shortcut things. */
661
                if (peer_uid == 0) {
×
662
                        log_debug("Directory file descriptor is owned by foreign UID range, and peer is root.");
×
663
                        return DIRECTORY_IS_FOREIGN_OWNED;
×
664
                }
665

666
                /* Go one level up */
667
                _cleanup_close_ int new_parent_fd = openat(fd, "..", O_DIRECTORY|O_PATH|O_CLOEXEC);
×
668
                if (new_parent_fd < 0)
×
669
                        return log_debug_errno(errno, "Failed to open parent directory of directory file descriptor: %m");
×
670

671
                struct stat new_st;
×
672
                if (fstat(new_parent_fd, &new_st) < 0)
×
673
                        return log_debug_errno(errno, "Failed to stat parent directory of directory file descriptor: %m");
×
674

675
                /* Safety check to see if we hit the root dir */
676
                if (stat_inode_same(&st, &new_st)) {
×
677
                        log_debug("Directory file descriptor is owned by foreign UID range, but didn't find parent directory that is owned by peer among ancestors.");
×
678
                        return DIRECTORY_IS_OTHERWISE_OWNED;
×
679
                }
680

681
                if (new_st.st_uid == peer_uid) { /* Parent inode is owned by the peer. That's good! Everything's fine. */
×
682
                        log_debug("Directory file descriptor is owned by foreign UID range, and ancestor is owned by peer.");
×
683
                        return DIRECTORY_IS_FOREIGN_OWNED;
×
684
                }
685

686
                close_and_replace(parent_fd, new_parent_fd);
×
687
                st = new_st;
×
688
        }
689

690
        log_debug("Failed to find peer owned parent directory after %u levels, refusing.", n_level);
×
691
        return DIRECTORY_IS_OTHERWISE_OWNED;
692
}
693

694
static int vl_method_mount_directory(
×
695
                sd_varlink *link,
696
                sd_json_variant *parameters,
697
                sd_varlink_method_flags_t flags,
698
                void *userdata) {
699

700
        static const sd_json_dispatch_field dispatch_table[] = {
×
701
                { "mode",                        SD_JSON_VARIANT_STRING,   dispatch_mount_directory_mode, offsetof(MountDirectoryParameters, mode),             0                 },
702
                { "directoryFileDescriptor",     SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,         offsetof(MountDirectoryParameters, directory_fd_idx), SD_JSON_MANDATORY },
703
                { "userNamespaceFileDescriptor", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint,         offsetof(MountDirectoryParameters, userns_fd_idx),    0                 },
704
                { "readOnly",                    SD_JSON_VARIANT_BOOLEAN,  sd_json_dispatch_tristate,     offsetof(MountDirectoryParameters, read_only),        0                 },
705
                VARLINK_DISPATCH_POLKIT_FIELD,
706
                {}
707
        };
708

709
        MountDirectoryParameters p = {
×
710
                .mode = MOUNT_MAP_AUTO,
711
                .directory_fd_idx = UINT_MAX,
712
                .userns_fd_idx = UINT_MAX,
713
                .read_only = -1,
714
        };
715
        _cleanup_close_ int directory_fd = -EBADF, userns_fd = -EBADF;
×
716
        Hashmap **polkit_registry = ASSERT_PTR(userdata);
×
717
        int r;
×
718

719
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
×
720
        if (r != 0)
×
721
                return r;
722

723
        if (p.directory_fd_idx == UINT_MAX)
×
724
                return sd_varlink_error_invalid_parameter_name(link, "directoryFileDescriptor");
×
725

726
        directory_fd = sd_varlink_peek_dup_fd(link, p.directory_fd_idx);
×
727
        if (directory_fd < 0)
×
728
                return log_debug_errno(directory_fd, "Failed to peek directory fd from client: %m");
×
729

730
        if (p.userns_fd_idx != UINT_MAX) {
×
731
                userns_fd = sd_varlink_peek_dup_fd(link, p.userns_fd_idx);
×
732
                if (userns_fd < 0)
×
733
                        return log_debug_errno(userns_fd, "Failed to peek user namespace fd from client: %m");
×
734
        }
735

736
        uid_t peer_uid;
×
737
        r = sd_varlink_get_peer_uid(link, &peer_uid);
×
738
        if (r < 0)
×
739
                return log_debug_errno(r, "Failed to get client UID: %m");
×
740

741
        DirectoryOwnership owned_by = validate_directory_fd(directory_fd, peer_uid);
×
742
        if (owned_by < 0)
×
743
                return owned_by;
744

745
        r = validate_userns(link, &userns_fd);
×
746
        if (r != 0)
×
747
                return r;
748

749
        /* If no mode is specified, pick sensible default */
750
        if (p.mode <= 0) {
×
751
                p.mode = default_mount_map_mode(owned_by);
×
752
                assert(p.mode > 0);
×
753
        }
754

755
        _cleanup_free_ char *directory_path = NULL;
×
756
        (void) fd_get_path(directory_fd, &directory_path);
×
757

758
        log_debug("Mounting '%s' with mapping mode: %s", strna(directory_path), mount_map_mode_to_string(p.mode));
×
759

760
        const char *polkit_details[] = {
×
761
                "read_only", one_zero(p.read_only > 0),
×
762
                "directory", strna(directory_path),
×
763
                NULL,
764
        };
765

766
        const char *polkit_action, *polkit_untrusted_action;
×
767
        PolkitFlags polkit_flags;
×
768
        if (userns_fd < 0) {
×
769
                /* Mount into the host user namespace */
770
                polkit_action = "io.systemd.mount-file-system.mount-directory";
771
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-directory";
772
                polkit_flags = 0;
773
        } else {
774
                /* Mount into a private user namespace */
775
                polkit_action = "io.systemd.mount-file-system.mount-directory-privately";
×
776
                polkit_untrusted_action = "io.systemd.mount-file-system.mount-untrusted-directory-privately";
×
777

778
                /* If polkit is not around, let's allow mounting authenticated images by default */
779
                polkit_flags = POLKIT_DEFAULT_ALLOW;
×
780
        }
781

782
        /* We consider a directory "trusted" if it is owned by the peer or the foreign UID range */
783
        bool trusted_directory = IN_SET(owned_by, DIRECTORY_IS_ROOT_PEER_OWNED, DIRECTORY_IS_PEER_OWNED, DIRECTORY_IS_FOREIGN_OWNED);
×
784

785
        /* Let's definitely acquire the regular action privilege, for mounting properly signed images */
786
        r = varlink_verify_polkit_async_full(
×
787
                        link,
788
                        /* bus= */ NULL,
789
                        trusted_directory ? polkit_action : polkit_untrusted_action,
790
                        polkit_details,
791
                        /* good_user= */ UID_INVALID,
792
                        trusted_directory ? polkit_flags : 0,
793
                        polkit_registry);
794
        if (r <= 0)
×
795
                return r;
796

797
        /* Generate the common dissection directory here. We are not going to use it, but the clients might,
798
         * and they likely are unprivileged, hence cannot create it themselves. Hence let's just create it
799
         * here, if it is missing. */
800
        r = get_common_dissect_directory(NULL);
×
801
        if (r < 0)
×
802
                return r;
803

804
        _cleanup_close_ int mount_fd = open_tree(directory_fd, "", OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH);
×
805
        if (mount_fd < 0)
×
806
                return log_debug_errno(errno, "Failed to issue open_tree() of provided directory '%s': %m", strna(directory_path));
×
807

808
        if (p.read_only > 0 && mount_setattr(
×
809
                            mount_fd, "", AT_EMPTY_PATH,
810
                            &(struct mount_attr) {
×
811
                                    .attr_set = MOUNT_ATTR_RDONLY,
812
                            }, MOUNT_ATTR_SIZE_VER0) < 0)
813
                return log_debug_errno(errno, "Failed to enable read-only mode: %m");
×
814

815
        if (p.mode != MOUNT_MAP_IDENTITY) {
×
816
                uid_t start;
×
817

818
                if (userns_fd >= 0) {
×
819
                        _cleanup_(uid_range_freep) UIDRange *uid_range_outside = NULL, *uid_range_inside = NULL, *gid_range_outside = NULL, *gid_range_inside = NULL;
×
820
                        r = uid_range_load_userns_by_fd(userns_fd, UID_RANGE_USERNS_OUTSIDE, &uid_range_outside);
×
821
                        if (r < 0)
×
822
                                return log_debug_errno(r, "Failed to load outside UID range of provided userns: %m");
×
823
                        r = uid_range_load_userns_by_fd(userns_fd, UID_RANGE_USERNS_INSIDE, &uid_range_inside);
×
824
                        if (r < 0)
×
825
                                return log_debug_errno(r, "Failed to load inside UID range of provided userns: %m");
×
826
                        r = uid_range_load_userns_by_fd(userns_fd, GID_RANGE_USERNS_OUTSIDE, &gid_range_outside);
×
827
                        if (r < 0)
×
828
                                return log_debug_errno(r, "Failed to load outside GID range of provided userns: %m");
×
829
                        r = uid_range_load_userns_by_fd(userns_fd, GID_RANGE_USERNS_INSIDE, &gid_range_inside);
×
830
                        if (r < 0)
×
831
                                return log_debug_errno(r, "Failed to load inside GID range of provided userns: %m");
×
832

833
                        /* Be very strict for now */
834
                        if (!uid_range_equal(uid_range_outside, gid_range_outside) ||
×
835
                            !uid_range_equal(uid_range_inside, gid_range_inside) ||
×
836
                            uid_range_outside->n_entries != 1 ||
×
837
                            uid_range_outside->entries[0].nr != 0x10000 ||
×
838
                            uid_range_inside->n_entries != 1 ||
×
839
                            uid_range_inside->entries[0].start != 0 ||
×
840
                            uid_range_inside->entries[0].nr != 0x10000)
×
841
                                return sd_varlink_error_invalid_parameter_name(link, "userNamespaceFileDescriptor");
×
842

843
                        start = uid_range_outside->entries[0].start;
×
844
                } else
845
                        start = 0;
846

847
                _cleanup_free_ char *new_uid_map = NULL;
×
848
                switch (p.mode) {
×
849
                case MOUNT_MAP_ROOT:
×
850
                        r = strextendf(&new_uid_map, UID_FMT " " UID_FMT " " UID_FMT,
×
851
                                       peer_uid, start, (uid_t) 1);
852
                        break;
853
                case MOUNT_MAP_FOREIGN:
×
854
                        r = strextendf(&new_uid_map, UID_FMT " " UID_FMT " " UID_FMT,
×
855
                                       (uid_t) FOREIGN_UID_MIN, start, (uid_t) 0x10000);
856
                        break;
857
                default:
×
858
                        assert_not_reached();
×
859
                }
860
                if (r < 0)
×
861
                        return r;
862

NEW
863
                _cleanup_close_ int idmap_userns_fd = userns_acquire(new_uid_map, new_uid_map, /* setgroups_deny= */ true);
×
864
                if (idmap_userns_fd < 0)
×
865
                        return log_debug_errno(idmap_userns_fd, "Failed to acquire user namespace for id mapping: %m");
×
866

867
                if (mount_setattr(mount_fd, "", AT_EMPTY_PATH,
×
868
                                  &(struct mount_attr) {
×
869
                                          .attr_set = MOUNT_ATTR_IDMAP,
870
                                          .userns_fd = idmap_userns_fd,
871
                                          .propagation = MS_PRIVATE,
872
                                  }, MOUNT_ATTR_SIZE_VER0) < 0)
873
                        return log_debug_errno(errno, "Failed to enable id mapping: %m");
×
874
        }
875

876
        if (userns_fd >= 0) {
×
877
                r = nsresource_add_mount(userns_fd, mount_fd);
×
878
                if (r < 0)
×
879
                        return r;
880
        }
881

882
        int fd_idx = sd_varlink_push_fd(link, mount_fd);
×
883
        if (fd_idx < 0)
×
884
                return fd_idx;
885

886
        TAKE_FD(mount_fd);
×
887

888
        return sd_varlink_replybo(
×
889
                        link,
890
                        SD_JSON_BUILD_PAIR("mountFileDescriptor", SD_JSON_BUILD_INTEGER(fd_idx)));
891
}
892

893
static int process_connection(sd_varlink_server *server, int _fd) {
×
894
        _cleanup_close_ int fd = TAKE_FD(_fd); /* always take possession */
×
895
        _cleanup_(sd_varlink_close_unrefp) sd_varlink *vl = NULL;
×
896
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
897
        int r;
×
898

899
        r = sd_event_new(&event);
×
900
        if (r < 0)
×
901
                return r;
902

903
        r = sd_varlink_server_attach_event(server, event, 0);
×
904
        if (r < 0)
×
905
                return log_error_errno(r, "Failed to attach Varlink server to event loop: %m");
×
906

907
        r = sd_varlink_server_add_connection(server, fd, &vl);
×
908
        if (r < 0)
×
909
                return log_error_errno(r, "Failed to add connection: %m");
×
910

911
        TAKE_FD(fd);
×
912
        vl = sd_varlink_ref(vl);
×
913

914
        r = sd_event_loop(event);
×
915
        if (r < 0)
×
916
                return log_error_errno(r, "Failed to run event loop: %m");
×
917

918
        r = sd_varlink_server_detach_event(server);
×
919
        if (r < 0)
×
920
                return log_error_errno(r, "Failed to detach Varlink server from event loop: %m");
×
921

922
        return 0;
923
}
924

925
static int run(int argc, char *argv[]) {
×
926
        usec_t start_time, listen_idle_usec, last_busy_usec = USEC_INFINITY;
×
927
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *server = NULL;
×
928
        _cleanup_hashmap_free_ Hashmap *polkit_registry = NULL;
×
929
        _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
×
930
        unsigned n_iterations = 0;
×
931
        int m, listen_fd, r;
×
932

933
        log_setup();
×
934

935
        m = sd_listen_fds(false);
×
936
        if (m < 0)
×
937
                return log_error_errno(m, "Failed to determine number of listening fds: %m");
×
938
        if (m == 0)
×
939
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No socket to listen on received.");
×
940
        if (m > 1)
×
941
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Worker can only listen on a single socket at a time.");
×
942

943
        listen_fd = SD_LISTEN_FDS_START;
×
944

945
        r = fd_nonblock(listen_fd, false);
×
946
        if (r < 0)
×
947
                return log_error_errno(r, "Failed to turn off non-blocking mode for listening socket: %m");
×
948

949
        r = varlink_server_new(&server,
×
950
                               SD_VARLINK_SERVER_INHERIT_USERDATA|
951
                               SD_VARLINK_SERVER_ALLOW_FD_PASSING_INPUT|SD_VARLINK_SERVER_ALLOW_FD_PASSING_OUTPUT,
952
                               &polkit_registry);
953
        if (r < 0)
×
954
                return log_error_errno(r, "Failed to allocate server: %m");
×
955

956
        r = sd_varlink_server_add_interface(server, &vl_interface_io_systemd_MountFileSystem);
×
957
        if (r < 0)
×
958
                return log_error_errno(r, "Failed to add MountFileSystem interface to varlink server: %m");
×
959

960
        r = sd_varlink_server_bind_method_many(
×
961
                        server,
962
                        "io.systemd.MountFileSystem.MountImage",     vl_method_mount_image,
963
                        "io.systemd.MountFileSystem.MountDirectory", vl_method_mount_directory);
964
        if (r < 0)
×
965
                return log_error_errno(r, "Failed to bind methods: %m");
×
966

967
        r = sd_varlink_server_set_exit_on_idle(server, true);
×
968
        if (r < 0)
×
969
                return log_error_errno(r, "Failed to enable exit-on-idle mode: %m");
×
970

971
        r = getenv_bool("MOUNTFS_FIXED_WORKER");
×
972
        if (r < 0)
×
973
                return log_error_errno(r, "Failed to parse MOUNTFSD_FIXED_WORKER: %m");
×
974
        listen_idle_usec = r ? USEC_INFINITY : LISTEN_IDLE_USEC;
×
975

976
        r = pidref_set_parent(&parent);
×
977
        if (r < 0)
×
978
                return log_error_errno(r, "Failed to acquire pidfd of parent process: %m");
×
979

980
        start_time = now(CLOCK_MONOTONIC);
×
981

982
        for (;;) {
×
983
                _cleanup_close_ int fd = -EBADF;
×
984
                usec_t n;
×
985

986
                /* Exit the worker in regular intervals, to flush out all memory use */
987
                if (n_iterations++ > ITERATIONS_MAX) {
×
988
                        log_debug("Exiting worker, processed %u iterations, that's enough.", n_iterations);
×
989
                        break;
990
                }
991

992
                n = now(CLOCK_MONOTONIC);
×
993
                if (n >= usec_add(start_time, RUNTIME_MAX_USEC)) {
×
994
                        log_debug("Exiting worker, ran for %s, that's enough.",
×
995
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, start_time), 0));
996
                        break;
×
997
                }
998

999
                if (last_busy_usec == USEC_INFINITY)
×
1000
                        last_busy_usec = n;
1001
                else if (listen_idle_usec != USEC_INFINITY && n >= usec_add(last_busy_usec, listen_idle_usec)) {
×
1002
                        log_debug("Exiting worker, been idle for %s.",
×
1003
                                  FORMAT_TIMESPAN(usec_sub_unsigned(n, last_busy_usec), 0));
1004
                        break;
×
1005
                }
1006

1007
                (void) rename_process("systemd-mountwork: waiting...");
×
1008
                fd = RET_NERRNO(accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC));
×
1009
                (void) rename_process("systemd-mountwork: processing...");
×
1010

1011
                if (fd == -EAGAIN)
×
1012
                        continue; /* The listening socket has SO_RECVTIMEO set, hence a timeout is expected
×
1013
                                   * after a while, let's check if it's time to exit though. */
1014
                if (fd == -EINTR)
×
1015
                        continue; /* Might be that somebody attached via strace, let's just continue in that
×
1016
                                   * case */
1017
                if (fd < 0)
×
1018
                        return log_error_errno(fd, "Failed to accept() from listening socket: %m");
×
1019

1020
                if (now(CLOCK_MONOTONIC) <= usec_add(n, PRESSURE_SLEEP_TIME_USEC)) {
×
1021
                        /* We only slept a very short time? If so, let's see if there are more sockets
1022
                         * pending, and if so, let's ask our parent for more workers */
1023

1024
                        r = fd_wait_for_event(listen_fd, POLLIN, 0);
×
1025
                        if (r < 0)
×
1026
                                return log_error_errno(r, "Failed to test for POLLIN on listening socket: %m");
×
1027

1028
                        if (FLAGS_SET(r, POLLIN)) {
×
1029
                                r = pidref_kill(&parent, SIGUSR2);
×
1030
                                if (r == -ESRCH)
×
1031
                                        return log_error_errno(r, "Parent already died?");
×
1032
                                if (r < 0)
×
1033
                                        return log_error_errno(r, "Failed to send SIGUSR2 signal to parent. %m");
×
1034
                        }
1035
                }
1036

1037
                (void) process_connection(server, TAKE_FD(fd));
×
1038
                last_busy_usec = USEC_INFINITY;
×
1039
        }
1040

1041
        return 0;
1042
}
1043

1044
DEFINE_MAIN_FUNCTION(run);
×
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