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

systemd / systemd / 16082515961

04 Jul 2025 08:23PM UTC coverage: 72.095% (-0.1%) from 72.193%
16082515961

push

github

poettering
seccomp-util: allowlist open_tree() as part of @file-system

Now that we make use of open_tree() in places we previously used
openat() with O_PATH, it makes sense to move it from @mount to
@file-system. Without the OPEN_TREE_CLONE flag open_tree() is after all
unprivileged.

Note that open_tree_attr() I left in @mount, since it's purpose is
really to set mount options when cloning, and that's clearly a mount
related thing, not so much something you could use unpriv.

Follow-up for: c5de7b14a

This addresses an issue tracked down by Antonio Feijoo: since the commit
that started to use open_tree() various apps started to crash because
they used seccomp filters and sd-device started to use open_tree()
internally.

300842 of 417287 relevant lines covered (72.09%)

715300.57 hits per line

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

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

3
#include <unistd.h>
4

5
#include "alloc-util.h"
6
#include "creds-util.h"
7
#include "errno-util.h"
8
#include "fd-util.h"
9
#include "fileio.h"
10
#include "generator.h"
11
#include "install.h"
12
#include "log.h"
13
#include "parse-util.h"
14
#include "path-lookup.h"
15
#include "path-util.h"
16
#include "proc-cmdline.h"
17
#include "socket-netlink.h"
18
#include "socket-util.h"
19
#include "special.h"
20
#include "string-util.h"
21
#include "strv.h"
22
#include "virt.h"
23

24
/* A small generator binding potentially five or more SSH sockets:
25
 *
26
 *     1. Listen on AF_VSOCK port 22 if we run in a VM with AF_VSOCK enabled
27
 *     2. Listen on AF_UNIX socket /run/host/unix-export/ssh if we run in a container with /run/host/ support
28
 *     3. Listen on AF_UNIX socket /run/ssh-unix-local/socket (always)
29
 *     4. Listen on any socket specified via kernel command line option systemd.ssh_listen=
30
 *     5. Similar, but from system credential ssh.listen
31
 *
32
 * The first two provide a nice way for hosts to connect to containers and VMs they invoke via the usual SSH
33
 * logic, but without waiting for networking or suchlike. The third allows the same for local clients. */
34

35
static const char *arg_dest = NULL;
36
static bool arg_auto = true;
37
static char **arg_listen_extra = NULL;
38

39
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
×
40
        int r;
×
41

42
        assert(key);
×
43

44
        if (proc_cmdline_key_streq(key, "systemd.ssh_auto")) {
×
45
                r = value ? parse_boolean(value) : 1;
×
46
                if (r < 0)
×
47
                        log_warning_errno(r, "Failed to parse systemd.ssh_auto switch \"%s\", ignoring: %m", value);
×
48
                else
49
                        arg_auto = r;
×
50

51
        } else if (proc_cmdline_key_streq(key, "systemd.ssh_listen")) {
×
52

53
                if (proc_cmdline_value_missing(key, value))
×
54
                        return 0;
×
55

56
                SocketAddress sa;
×
57
                r = socket_address_parse(&sa, value);
×
58
                if (r < 0)
×
59
                        log_warning_errno(r, "Failed to parse systemd.ssh_listen= expression, ignoring: %s", value);
×
60
                else {
61
                        _cleanup_free_ char *s = NULL;
×
62
                        r = socket_address_print(&sa, &s);
×
63
                        if (r < 0)
×
64
                                return log_error_errno(r, "Failed to format socket address: %m");
×
65

66
                        if (strv_consume(&arg_listen_extra, TAKE_PTR(s)) < 0)
×
67
                                return log_oom();
×
68
                }
69
        }
70

71
        return 0;
72
}
73

74
static int make_sshd_template_unit(
×
75
                const char *dest,
76
                const char *template,
77
                const char *sshd_binary,
78
                const char *found_sshd_template_service,
79
                char **generated_sshd_template_unit) {
80

81
        int r;
×
82

83
        assert(dest);
×
84
        assert(template);
×
85
        assert(sshd_binary);
×
86
        assert(generated_sshd_template_unit);
×
87

88
        /* If the system has a suitable template already, symlink it to the name we want to reuse it */
89
        if (found_sshd_template_service)
×
90
                return generator_add_symlink(
×
91
                                dest,
92
                                template,
93
                                /* dep_type= */ NULL,
94
                                found_sshd_template_service);
95

96
        if (!*generated_sshd_template_unit) {
×
97
                _cleanup_fclose_ FILE *f = NULL;
×
98

99
                r = generator_open_unit_file_full(
×
100
                                dest,
101
                                /* source= */ NULL,
102
                                "sshd-generated@.service", /* Give this generated unit a generic name, since we want to use it for both AF_UNIX and AF_VSOCK */
103
                                &f,
104
                                generated_sshd_template_unit,
105
                                /* ret_temp_path= */ NULL);
106
                if (r < 0)
×
107
                        return r;
108

109
                fprintf(f,
×
110
                        "[Unit]\n"
111
                        "Description=OpenSSH Per-Connection Server Daemon\n"
112
                        "Documentation=man:systemd-ssh-generator(8) man:sshd(8)\n"
113
                        "\n"
114
                        "[Service]\n"
115
                        "ExecStart=-%s -i -o \"AuthorizedKeysFile ${CREDENTIALS_DIRECTORY}/ssh.ephemeral-authorized_keys-all .ssh/authorized_keys\"\n"
116
                        "StandardInput=socket\n"
117
                        "ImportCredential=ssh.ephemeral-authorized_keys-all\n",
118
                        sshd_binary);
119

120
                r = fflush_and_check(f);
×
121
                if (r < 0)
×
122
                        return log_error_errno(r, "Failed to write sshd template: %m");
×
123
        }
124

125
        return generator_add_symlink(
×
126
                        dest,
127
                        template,
128
                        /* dep_type= */ NULL,
129
                        *generated_sshd_template_unit);
130
}
131

132
static int write_socket_unit(
×
133
                const char *dest,
134
                const char *unit,
135
                const char *listen_stream,
136
                const char *comment,
137
                const char *extra,
138
                bool with_ssh_access_target_dependency) {
139

140
        int r;
×
141

142
        assert(dest);
×
143
        assert(unit);
×
144
        assert(listen_stream);
×
145
        assert(comment);
×
146

147
        _cleanup_fclose_ FILE *f = NULL;
×
148
        r = generator_open_unit_file(
×
149
                        dest,
150
                        /* source= */ NULL,
151
                        unit,
152
                        &f);
153
        if (r < 0)
×
154
                return r;
155

156
        fprintf(f,
×
157
                "[Unit]\n"
158
                "Description=OpenSSH Server Socket (systemd-ssh-generator, %s)\n"
159
                "Documentation=man:systemd-ssh-generator(8)\n",
160
                comment);
161

162
        /* When this is a remotely accessible socket let's mark this with a milestone: ssh-access.target */
163
        if (with_ssh_access_target_dependency)
×
164
                fputs("Wants=ssh-access.target\n"
×
165
                      "Before=ssh-access.target\n",
166
                      f);
167

168
        fprintf(f,
×
169
                "\n[Socket]\n"
170
                "ListenStream=%s\n"
171
                "Accept=yes\n"
172
                "PollLimitIntervalSec=30s\n"
173
                "PollLimitBurst=50\n",
174
                listen_stream);
175

176
        if (extra)
×
177
                fputs(extra, f);
×
178

179
        r = fflush_and_check(f);
×
180
        if (r < 0)
×
181
                return log_error_errno(r, "Failed to write %s SSH socket unit: %m", comment);
×
182

183
        r = generator_add_symlink(
×
184
                        dest,
185
                        SPECIAL_SOCKETS_TARGET,
186
                        "wants",
187
                        unit);
188
        if (r < 0)
×
189
                return r;
×
190

191
        return 0;
192
}
193

194
static int add_vsock_socket(
×
195
                const char *dest,
196
                const char *sshd_binary,
197
                const char *found_sshd_template_unit,
198
                char **generated_sshd_template_unit) {
199

200
        int r;
×
201

202
        assert(dest);
×
203
        assert(generated_sshd_template_unit);
×
204

205
        Virtualization v = detect_virtualization();
×
206
        if (v < 0)
×
207
                return log_error_errno(v, "Failed to detect if we run in a VM: %m");
×
208
        if (!VIRTUALIZATION_IS_VM(v)) {
×
209
                /* NB: if we are running in a container inside a VM, then we'll *not* do AF_VSOCK stuff */
210
                log_debug("Not running in a VM, not listening on AF_VSOCK.");
×
211
                return 0;
×
212
        }
213

214
        _cleanup_close_ int vsock_fd = socket(AF_VSOCK, SOCK_STREAM|SOCK_CLOEXEC, 0);
×
215
        if (vsock_fd < 0) {
×
216
                if (ERRNO_IS_NOT_SUPPORTED(errno)) {
×
217
                        log_debug("Not creating AF_VSOCK ssh listener, since AF_VSOCK is not available.");
×
218
                        return 0;
×
219
                }
220

221
                return log_error_errno(errno, "Unable to test if AF_VSOCK is available: %m");
×
222
        }
223

224
        vsock_fd = safe_close(vsock_fd);
×
225

226
        /* Determine the local CID so that we can log it to help users to connect to this VM */
227
        unsigned local_cid;
×
228
        r = vsock_get_local_cid(&local_cid);
×
229
        if (r < 0) {
×
230
                if (ERRNO_IS_DEVICE_ABSENT(r)) {
×
231
                        log_debug("Not creating AF_VSOCK ssh listener, since /dev/vsock is not available (even though AF_VSOCK is).");
×
232
                        return 0;
×
233
                }
234

235
                return log_error_errno(r, "Failed to query local AF_VSOCK CID: %m");
×
236
        }
237

238
        r = make_sshd_template_unit(
×
239
                        dest,
240
                        "sshd-vsock@.service",
241
                        sshd_binary,
242
                        found_sshd_template_unit,
243
                        generated_sshd_template_unit);
244
        if (r < 0)
×
245
                return r;
246

247
        r = write_socket_unit(
×
248
                        dest,
249
                        "sshd-vsock.socket",
250
                        "vsock::22",
251
                        "AF_VSOCK",
252
                        "ExecStartPost=-/usr/lib/systemd/systemd-ssh-issue --make-vsock\n"
253
                        "ExecStopPre=-/usr/lib/systemd/systemd-ssh-issue --rm-vsock\n",
254
                        /* with_ssh_access_target_dependency= */ true);
255
        if (r < 0)
×
256
                return r;
257

258
        log_debug("Binding SSH to AF_VSOCK vsock::22.\n"
×
259
                  "→ connect via 'ssh vsock/%u' from host", local_cid);
260
        return 0;
261
}
262

263
static int add_local_unix_socket(
×
264
                const char *dest,
265
                const char *sshd_binary,
266
                const char *found_sshd_template_unit,
267
                char **generated_sshd_template_unit) {
268

269
        int r;
×
270

271
        assert(dest);
×
272
        assert(sshd_binary);
×
273
        assert(generated_sshd_template_unit);
×
274

275
        r = make_sshd_template_unit(
×
276
                        dest,
277
                        "sshd-unix-local@.service",
278
                        sshd_binary,
279
                        found_sshd_template_unit,
280
                        generated_sshd_template_unit);
281
        if (r < 0)
×
282
                return r;
283

284
        r = write_socket_unit(
×
285
                        dest,
286
                        "sshd-unix-local.socket",
287
                        "/run/ssh-unix-local/socket",
288
                        "AF_UNIX Local",
289
                        /* extra= */ NULL,
290
                        /* with_ssh_access_target_dependency= */ false);
291
        if (r < 0)
×
292
                return r;
293

294
        log_debug("Binding SSH to AF_UNIX socket /run/ssh-unix-local/socket.\n"
×
295
                  "→ connect via 'ssh .host' locally");
296
        return 0;
297
}
298

299
static int add_export_unix_socket(
×
300
                const char *dest,
301
                const char *sshd_binary,
302
                const char *found_sshd_template_unit,
303
                char **generated_sshd_template_unit) {
304

305
        int r;
×
306

307
        assert(dest);
×
308
        assert(sshd_binary);
×
309
        assert(generated_sshd_template_unit);
×
310

311
        Virtualization v = detect_container();
×
312
        if (v < 0)
×
313
                return log_error_errno(v, "Failed to detect if we run in a container: %m");
×
314
        if (v == VIRTUALIZATION_NONE) {
×
315
                log_debug("Not running in container, not listening on /run/host/unix-export/ssh");
×
316
                return 0;
×
317
        }
318

319
        if (access("/run/host/unix-export/", W_OK) < 0) {
×
320
                if (errno == ENOENT) {
×
321
                        log_debug("Container manager does not provide /run/host/unix-export/ mount, not binding AF_UNIX socket there.");
×
322
                        return 0;
×
323
                }
324
                if (errno == EROFS || ERRNO_IS_PRIVILEGE(errno)) {
×
325
                        log_debug("Container manager does not provide write access to /run/host/unix-export/, not binding AF_UNIX socket there.");
×
326
                        return 0;
×
327
                }
328

329
                return log_error_errno(errno, "Unable to check if /run/host/unix-export exists: %m");
×
330
        }
331

332
        r = make_sshd_template_unit(
×
333
                        dest,
334
                        "sshd-unix-export@.service",
335
                        sshd_binary,
336
                        found_sshd_template_unit,
337
                        generated_sshd_template_unit);
338
        if (r < 0)
×
339
                return r;
340

341
        r = write_socket_unit(
×
342
                        dest,
343
                        "sshd-unix-export.socket",
344
                        "/run/host/unix-export/ssh",
345
                        "AF_UNIX Export",
346
                        /* extra= */ NULL,
347
                        /* with_ssh_access_target_dependency= */ true);
348
        if (r < 0)
×
349
                return r;
350

351
        log_debug("Binding SSH to AF_UNIX socket /run/host/unix-export/ssh\n"
×
352
                  "→ connect via 'ssh unix/run/systemd/nspawn/unix-export/\?\?\?/ssh' from host");
353

354
        return 0;
355
}
356

357
static int add_extra_sockets(
×
358
                const char *dest,
359
                const char *sshd_binary,
360
                const char *found_sshd_template_unit,
361
                char **generated_sshd_template_unit) {
362

363
        unsigned n = 1;
×
364
        int r;
×
365

366
        assert(dest);
×
367
        assert(sshd_binary);
×
368
        assert(generated_sshd_template_unit);
×
369

370
        if (strv_isempty(arg_listen_extra))
×
371
                return 0;
372

373
        STRV_FOREACH(i, arg_listen_extra) {
×
374
                _cleanup_free_ char *service = NULL, *socket = NULL;
×
375

376
                if (n > 1) {
×
377
                        if (asprintf(&service, "sshd-extra-%u@.service", n) < 0)
×
378
                                return log_oom();
×
379

380
                        if (asprintf(&socket, "sshd-extra-%u.socket", n) < 0)
×
381
                                return log_oom();
×
382
                }
383

384
                r = make_sshd_template_unit(
×
385
                                dest,
386
                                service ?: "sshd-extra@.service",
×
387
                                sshd_binary,
388
                                found_sshd_template_unit,
389
                                generated_sshd_template_unit);
390
                if (r < 0)
×
391
                        return r;
392

393
                r = write_socket_unit(
×
394
                                dest,
395
                                socket ?: "sshd-extra.socket",
×
396
                                *i,
397
                                *i,
398
                                /* extra= */ NULL,
399
                                /* with_ssh_access_target_dependency= */ true);
400
                if (r < 0)
×
401
                        return r;
402

403
                log_debug("Binding SSH to socket %s.", *i);
×
404
                n++;
×
405
        }
406

407
        return 0;
408
}
409

410
static int parse_credentials(void) {
×
411
        _cleanup_free_ char *b = NULL;
×
412
        size_t sz = 0;
×
413
        int r;
×
414

415
        r = read_credential_with_decryption("ssh.listen", (void**) &b, &sz);
×
416
        if (r <= 0)
×
417
                return r;
418

419
        _cleanup_fclose_ FILE *f = NULL;
×
420
        f = fmemopen_unlocked(b, sz, "r");
×
421
        if (!f)
×
422
                return log_oom();
×
423

424
        for (;;) {
×
425
                _cleanup_free_ char *item = NULL;
×
426

427
                r = read_stripped_line(f, LINE_MAX, &item);
×
428
                if (r == 0)
×
429
                        break;
430
                if (r < 0) {
×
431
                        log_error_errno(r, "Failed to parse credential 'ssh.listen': %m");
×
432
                        break;
433
                }
434

435
                if (startswith(item, "#"))
×
436
                        continue;
×
437

438
                SocketAddress sa;
×
439
                r = socket_address_parse(&sa, item);
×
440
                if (r < 0) {
×
441
                        log_warning_errno(r, "Failed to parse systemd.ssh_listen= expression, ignoring: %s", item);
×
442
                        continue;
×
443
                }
444

445
                _cleanup_free_ char *s = NULL;
×
446
                r = socket_address_print(&sa, &s);
×
447
                if (r < 0)
×
448
                        return log_error_errno(r, "Failed to format socket address: %m");
×
449

450
                if (strv_consume(&arg_listen_extra, TAKE_PTR(s)) < 0)
×
451
                        return log_oom();
×
452
        }
453

454
        return 0;
×
455
}
456

457
static int run(const char *dest, const char *dest_early, const char *dest_late) {
×
458
        int r;
×
459

460
        assert_se(arg_dest = dest);
×
461

462
        r = proc_cmdline_parse(parse_proc_cmdline_item, /* userdata= */ NULL, /* flags= */ 0);
×
463
        if (r < 0)
×
464
                log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
×
465

466
        (void) parse_credentials();
×
467

468
        strv_sort_uniq(arg_listen_extra);
×
469

470
        if (!arg_auto && strv_isempty(arg_listen_extra)) {
×
471
                log_debug("Disabling SSH generator logic, because it has been turned off explicitly.");
×
472
                return 0;
×
473
        }
474

475
        _cleanup_free_ char *sshd_binary = NULL;
×
476
        r = find_executable("sshd", &sshd_binary);
×
477
        if (r == -ENOENT) {
×
478
                log_debug("Disabling SSH generator logic, since sshd is not installed.");
×
479
                return 0;
×
480
        }
481
        if (r < 0)
×
482
                return log_error_errno(r, "Failed to determine if sshd is installed: %m");
×
483

484
        _cleanup_(lookup_paths_done) LookupPaths lp = {};
×
485
        r = lookup_paths_init_or_warn(&lp, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, /* root_dir= */ NULL);
×
486
        if (r < 0)
×
487
                return r;
488

489
        _cleanup_free_ char *found_sshd_template_unit = NULL;
×
490
        r = unit_file_exists_full(RUNTIME_SCOPE_SYSTEM, &lp, "sshd@.service", &found_sshd_template_unit);
×
491
        if (r < 0)
×
492
                return log_error_errno(r, "Unable to detect if sshd@.service exists: %m");
×
493

494
        _cleanup_free_ char *generated_sshd_template_unit = NULL;
×
495
        RET_GATHER(r, add_extra_sockets(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
496

497
        if (arg_auto) {
×
498
                RET_GATHER(r, add_vsock_socket(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
499
                RET_GATHER(r, add_local_unix_socket(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
500
                RET_GATHER(r, add_export_unix_socket(dest, sshd_binary, found_sshd_template_unit, &generated_sshd_template_unit));
×
501
        }
502

503
        return r;
×
504
}
505

506
DEFINE_MAIN_GENERATOR_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