]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/execute.h
tree-wide: hook up image dissection policy logic everywhere
[thirdparty/systemd.git] / src / core / execute.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 typedef struct ExecStatus ExecStatus;
5 typedef struct ExecCommand ExecCommand;
6 typedef struct ExecContext ExecContext;
7 typedef struct ExecSharedRuntime ExecSharedRuntime;
8 typedef struct DynamicCreds DynamicCreds;
9 typedef struct ExecRuntime ExecRuntime;
10 typedef struct ExecParameters ExecParameters;
11 typedef struct Manager Manager;
12
13 #include <sched.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <sys/capability.h>
17
18 #include "cgroup-util.h"
19 #include "coredump-util.h"
20 #include "cpu-set-util.h"
21 #include "exec-util.h"
22 #include "fdset.h"
23 #include "list.h"
24 #include "missing_resource.h"
25 #include "namespace.h"
26 #include "nsflags.h"
27 #include "numa-util.h"
28 #include "open-file.h"
29 #include "path-util.h"
30 #include "set.h"
31 #include "time-util.h"
32
33 #define EXEC_STDIN_DATA_MAX (64U*1024U*1024U)
34
35 typedef enum ExecUtmpMode {
36 EXEC_UTMP_INIT,
37 EXEC_UTMP_LOGIN,
38 EXEC_UTMP_USER,
39 _EXEC_UTMP_MODE_MAX,
40 _EXEC_UTMP_MODE_INVALID = -EINVAL,
41 } ExecUtmpMode;
42
43 typedef enum ExecInput {
44 EXEC_INPUT_NULL,
45 EXEC_INPUT_TTY,
46 EXEC_INPUT_TTY_FORCE,
47 EXEC_INPUT_TTY_FAIL,
48 EXEC_INPUT_SOCKET,
49 EXEC_INPUT_NAMED_FD,
50 EXEC_INPUT_DATA,
51 EXEC_INPUT_FILE,
52 _EXEC_INPUT_MAX,
53 _EXEC_INPUT_INVALID = -EINVAL,
54 } ExecInput;
55
56 typedef enum ExecOutput {
57 EXEC_OUTPUT_INHERIT,
58 EXEC_OUTPUT_NULL,
59 EXEC_OUTPUT_TTY,
60 EXEC_OUTPUT_KMSG,
61 EXEC_OUTPUT_KMSG_AND_CONSOLE,
62 EXEC_OUTPUT_JOURNAL,
63 EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
64 EXEC_OUTPUT_SOCKET,
65 EXEC_OUTPUT_NAMED_FD,
66 EXEC_OUTPUT_FILE,
67 EXEC_OUTPUT_FILE_APPEND,
68 EXEC_OUTPUT_FILE_TRUNCATE,
69 _EXEC_OUTPUT_MAX,
70 _EXEC_OUTPUT_INVALID = -EINVAL,
71 } ExecOutput;
72
73 typedef enum ExecPreserveMode {
74 EXEC_PRESERVE_NO,
75 EXEC_PRESERVE_YES,
76 EXEC_PRESERVE_RESTART,
77 _EXEC_PRESERVE_MODE_MAX,
78 _EXEC_PRESERVE_MODE_INVALID = -EINVAL,
79 } ExecPreserveMode;
80
81 typedef enum ExecKeyringMode {
82 EXEC_KEYRING_INHERIT,
83 EXEC_KEYRING_PRIVATE,
84 EXEC_KEYRING_SHARED,
85 _EXEC_KEYRING_MODE_MAX,
86 _EXEC_KEYRING_MODE_INVALID = -EINVAL,
87 } ExecKeyringMode;
88
89 /* Contains start and exit information about an executed command. */
90 struct ExecStatus {
91 dual_timestamp start_timestamp;
92 dual_timestamp exit_timestamp;
93 pid_t pid;
94 int code; /* as in siginfo_t::si_code */
95 int status; /* as in siginfo_t::si_status */
96 };
97
98 /* Stores information about commands we execute. Covers both configuration settings as well as runtime data. */
99 struct ExecCommand {
100 char *path;
101 char **argv;
102 ExecStatus exec_status;
103 ExecCommandFlags flags;
104 LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
105 };
106
107 /* Encapsulates certain aspects of the runtime environment that is to be shared between multiple otherwise separate
108 * invocations of commands. Specifically, this allows sharing of /tmp and /var/tmp data as well as network namespaces
109 * between invocations of commands. This is a reference counted object, with one reference taken by each currently
110 * active command invocation that wants to share this runtime. */
111 struct ExecSharedRuntime {
112 unsigned n_ref;
113
114 Manager *manager;
115
116 char *id; /* Unit id of the owner */
117
118 char *tmp_dir;
119 char *var_tmp_dir;
120
121 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
122 * namespace. */
123 int netns_storage_socket[2];
124
125 /* Like netns_storage_socket, but the file descriptor is referring to the IPC namespace. */
126 int ipcns_storage_socket[2];
127 };
128
129 struct ExecRuntime {
130 ExecSharedRuntime *shared;
131 DynamicCreds *dynamic_creds;
132 };
133
134 typedef enum ExecDirectoryType {
135 EXEC_DIRECTORY_RUNTIME = 0,
136 EXEC_DIRECTORY_STATE,
137 EXEC_DIRECTORY_CACHE,
138 EXEC_DIRECTORY_LOGS,
139 EXEC_DIRECTORY_CONFIGURATION,
140 _EXEC_DIRECTORY_TYPE_MAX,
141 _EXEC_DIRECTORY_TYPE_INVALID = -EINVAL,
142 } ExecDirectoryType;
143
144 typedef struct ExecDirectoryItem {
145 char *path;
146 char **symlinks;
147 bool only_create;
148 } ExecDirectoryItem;
149
150 typedef struct ExecDirectory {
151 mode_t mode;
152 size_t n_items;
153 ExecDirectoryItem *items;
154 } ExecDirectory;
155
156 typedef enum ExecCleanMask {
157 /* In case you wonder why the bitmask below doesn't use "directory" in its name: we want to keep this
158 * generic so that .timer timestamp files can nicely be covered by this too, and similar. */
159 EXEC_CLEAN_RUNTIME = 1U << EXEC_DIRECTORY_RUNTIME,
160 EXEC_CLEAN_STATE = 1U << EXEC_DIRECTORY_STATE,
161 EXEC_CLEAN_CACHE = 1U << EXEC_DIRECTORY_CACHE,
162 EXEC_CLEAN_LOGS = 1U << EXEC_DIRECTORY_LOGS,
163 EXEC_CLEAN_CONFIGURATION = 1U << EXEC_DIRECTORY_CONFIGURATION,
164 EXEC_CLEAN_NONE = 0,
165 EXEC_CLEAN_ALL = (1U << _EXEC_DIRECTORY_TYPE_MAX) - 1,
166 _EXEC_CLEAN_MASK_INVALID = -EINVAL,
167 } ExecCleanMask;
168
169 /* A credential configured with LoadCredential= */
170 typedef struct ExecLoadCredential {
171 char *id, *path;
172 bool encrypted;
173 } ExecLoadCredential;
174
175 /* A credential configured with SetCredential= */
176 typedef struct ExecSetCredential {
177 char *id;
178 bool encrypted;
179 void *data;
180 size_t size;
181 } ExecSetCredential;
182
183 /* Encodes configuration parameters applied to invoked commands. Does not carry runtime data, but only configuration
184 * changes sourced from unit files and suchlike. ExecContext objects are usually embedded into Unit objects, and do not
185 * change after being loaded. */
186 struct ExecContext {
187 char **environment;
188 char **environment_files;
189 char **pass_environment;
190 char **unset_environment;
191
192 struct rlimit *rlimit[_RLIMIT_MAX];
193 char *working_directory, *root_directory, *root_image, *root_verity, *root_hash_path, *root_hash_sig_path;
194 void *root_hash, *root_hash_sig;
195 size_t root_hash_size, root_hash_sig_size;
196 LIST_HEAD(MountOptions, root_image_options);
197 bool working_directory_missing_ok:1;
198 bool working_directory_home:1;
199
200 bool oom_score_adjust_set:1;
201 bool coredump_filter_set:1;
202 bool nice_set:1;
203 bool ioprio_set:1;
204 bool cpu_sched_set:1;
205 bool mount_apivfs_set:1;
206
207 /* This is not exposed to the user but available internally. We need it to make sure that whenever we
208 * spawn /usr/bin/mount it is run in the same process group as us so that the autofs logic detects
209 * that it belongs to us and we don't enter a trigger loop. */
210 bool same_pgrp;
211
212 bool cpu_sched_reset_on_fork;
213 bool non_blocking;
214
215 mode_t umask;
216 int oom_score_adjust;
217 int nice;
218 int ioprio;
219 int cpu_sched_policy;
220 int cpu_sched_priority;
221 uint64_t coredump_filter;
222
223 CPUSet cpu_set;
224 NUMAPolicy numa_policy;
225 bool cpu_affinity_from_numa;
226
227 ExecInput std_input;
228 ExecOutput std_output;
229 ExecOutput std_error;
230 bool stdio_as_fds;
231 char *stdio_fdname[3];
232 char *stdio_file[3];
233
234 void *stdin_data;
235 size_t stdin_data_size;
236
237 nsec_t timer_slack_nsec;
238
239 char *tty_path;
240
241 bool tty_reset;
242 bool tty_vhangup;
243 bool tty_vt_disallocate;
244
245 unsigned tty_rows;
246 unsigned tty_cols;
247
248 bool ignore_sigpipe;
249
250 ExecKeyringMode keyring_mode;
251
252 /* Since resolving these names might involve socket
253 * connections and we don't want to deadlock ourselves these
254 * names are resolved on execution only and in the child
255 * process. */
256 char *user;
257 char *group;
258 char **supplementary_groups;
259
260 char *pam_name;
261
262 char *utmp_id;
263 ExecUtmpMode utmp_mode;
264
265 bool no_new_privileges;
266
267 bool selinux_context_ignore;
268 bool apparmor_profile_ignore;
269 bool smack_process_label_ignore;
270
271 char *selinux_context;
272 char *apparmor_profile;
273 char *smack_process_label;
274
275 char **read_write_paths, **read_only_paths, **inaccessible_paths, **exec_paths, **no_exec_paths;
276 char **exec_search_path;
277 unsigned long mount_propagation_flag;
278 BindMount *bind_mounts;
279 size_t n_bind_mounts;
280 TemporaryFileSystem *temporary_filesystems;
281 size_t n_temporary_filesystems;
282 MountImage *mount_images;
283 size_t n_mount_images;
284 MountImage *extension_images;
285 size_t n_extension_images;
286 char **extension_directories;
287
288 uint64_t capability_bounding_set;
289 uint64_t capability_ambient_set;
290 int secure_bits;
291
292 int syslog_priority;
293 bool syslog_level_prefix;
294 char *syslog_identifier;
295
296 struct iovec* log_extra_fields;
297 size_t n_log_extra_fields;
298 Set *log_filter_allowed_patterns;
299 Set *log_filter_denied_patterns;
300
301 usec_t log_ratelimit_interval_usec;
302 unsigned log_ratelimit_burst;
303
304 int log_level_max;
305
306 char *log_namespace;
307
308 ProtectProc protect_proc; /* hidepid= */
309 ProcSubset proc_subset; /* subset= */
310
311 int private_mounts;
312 bool private_tmp;
313 bool private_network;
314 bool private_devices;
315 bool private_users;
316 bool private_ipc;
317 bool protect_kernel_tunables;
318 bool protect_kernel_modules;
319 bool protect_kernel_logs;
320 bool protect_clock;
321 bool protect_control_groups;
322 ProtectSystem protect_system;
323 ProtectHome protect_home;
324 bool protect_hostname;
325 bool mount_apivfs;
326
327 bool dynamic_user;
328 bool remove_ipc;
329
330 bool memory_deny_write_execute;
331 bool restrict_realtime;
332 bool restrict_suid_sgid;
333
334 bool lock_personality;
335 unsigned long personality;
336
337 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
338
339 Set *restrict_filesystems;
340 bool restrict_filesystems_allow_list:1;
341
342 Hashmap *syscall_filter;
343 Set *syscall_archs;
344 int syscall_errno;
345 bool syscall_allow_list:1;
346
347 Hashmap *syscall_log;
348 bool syscall_log_allow_list:1; /* Log listed system calls */
349
350 bool address_families_allow_list:1;
351 Set *address_families;
352
353 char *network_namespace_path;
354 char *ipc_namespace_path;
355
356 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
357 ExecPreserveMode runtime_directory_preserve_mode;
358 usec_t timeout_clean_usec;
359
360 Hashmap *set_credentials; /* output id → ExecSetCredential */
361 Hashmap *load_credentials; /* output id → ExecLoadCredential */
362
363 ImagePolicy *root_image_policy, *mount_image_policy, *extension_image_policy;
364 };
365
366 static inline bool exec_context_restrict_namespaces_set(const ExecContext *c) {
367 assert(c);
368
369 return (c->restrict_namespaces & NAMESPACE_FLAGS_ALL) != NAMESPACE_FLAGS_ALL;
370 }
371
372 static inline bool exec_context_restrict_filesystems_set(const ExecContext *c) {
373 assert(c);
374
375 return c->restrict_filesystems_allow_list ||
376 !set_isempty(c->restrict_filesystems);
377 }
378
379 static inline bool exec_context_with_rootfs(const ExecContext *c) {
380 assert(c);
381
382 /* Checks if RootDirectory= or RootImage= are used */
383
384 return !empty_or_root(c->root_directory) || c->root_image;
385 }
386
387 typedef enum ExecFlags {
388 EXEC_APPLY_SANDBOXING = 1 << 0,
389 EXEC_APPLY_CHROOT = 1 << 1,
390 EXEC_APPLY_TTY_STDIN = 1 << 2,
391 EXEC_PASS_LOG_UNIT = 1 << 3, /* Whether to pass the unit name to the service's journal stream connection */
392 EXEC_CHOWN_DIRECTORIES = 1 << 4, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */
393 EXEC_NSS_DYNAMIC_BYPASS = 1 << 5, /* Set the SYSTEMD_NSS_DYNAMIC_BYPASS environment variable, to disable nss-systemd blocking on PID 1, for use by dbus-daemon */
394 EXEC_CGROUP_DELEGATE = 1 << 6,
395 EXEC_IS_CONTROL = 1 << 7,
396 EXEC_CONTROL_CGROUP = 1 << 8, /* Place the process not in the indicated cgroup but in a subcgroup '/.control', but only EXEC_CGROUP_DELEGATE and EXEC_IS_CONTROL is set, too */
397 EXEC_WRITE_CREDENTIALS = 1 << 9, /* Set up the credential store logic */
398
399 /* The following are not used by execute.c, but by consumers internally */
400 EXEC_PASS_FDS = 1 << 10,
401 EXEC_SETENV_RESULT = 1 << 11,
402 EXEC_SET_WATCHDOG = 1 << 12,
403 EXEC_SETENV_MONITOR_RESULT = 1 << 13, /* Pass exit status to OnFailure= and OnSuccess= dependencies. */
404 } ExecFlags;
405
406 /* Parameters for a specific invocation of a command. This structure is put together right before a command is
407 * executed. */
408 struct ExecParameters {
409 char **environment;
410
411 int *fds;
412 char **fd_names;
413 size_t n_socket_fds;
414 size_t n_storage_fds;
415
416 ExecFlags flags;
417 bool selinux_context_net:1;
418
419 CGroupMask cgroup_supported;
420 const char *cgroup_path;
421
422 char **prefix;
423 const char *received_credentials_directory;
424 const char *received_encrypted_credentials_directory;
425
426 const char *confirm_spawn;
427
428 usec_t watchdog_usec;
429
430 int *idle_pipe;
431
432 int stdin_fd;
433 int stdout_fd;
434 int stderr_fd;
435
436 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done */
437 int exec_fd;
438
439 const char *notify_socket;
440
441 LIST_HEAD(OpenFile, open_files);
442 };
443
444 #include "unit.h"
445 #include "dynamic-user.h"
446
447 int exec_spawn(Unit *unit,
448 ExecCommand *command,
449 const ExecContext *context,
450 const ExecParameters *exec_params,
451 ExecRuntime *runtime,
452 const CGroupContext *cgroup_context,
453 pid_t *ret);
454
455 void exec_command_done_array(ExecCommand *c, size_t n);
456 ExecCommand* exec_command_free_list(ExecCommand *c);
457 void exec_command_free_array(ExecCommand **c, size_t n);
458 void exec_command_reset_status_array(ExecCommand *c, size_t n);
459 void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
460 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
461 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
462 int exec_command_set(ExecCommand *c, const char *path, ...) _sentinel_;
463 int exec_command_append(ExecCommand *c, const char *path, ...) _sentinel_;
464
465 void exec_context_init(ExecContext *c);
466 void exec_context_done(ExecContext *c);
467 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
468
469 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root);
470 int exec_context_destroy_credentials(const ExecContext *c, const char *runtime_root, const char *unit);
471 int exec_context_destroy_mount_ns_dir(Unit *u);
472
473 const char* exec_context_fdname(const ExecContext *c, int fd_index);
474
475 bool exec_context_may_touch_console(const ExecContext *c);
476 bool exec_context_maintains_privileges(const ExecContext *c);
477 bool exec_context_has_encrypted_credentials(ExecContext *c);
478
479 int exec_context_get_effective_ioprio(const ExecContext *c);
480 bool exec_context_get_effective_mount_apivfs(const ExecContext *c);
481
482 void exec_context_free_log_extra_fields(ExecContext *c);
483
484 void exec_context_revert_tty(ExecContext *c);
485
486 int exec_context_get_clean_directories(ExecContext *c, char **prefix, ExecCleanMask mask, char ***ret);
487 int exec_context_get_clean_mask(ExecContext *c, ExecCleanMask *ret);
488
489 void exec_status_start(ExecStatus *s, pid_t pid);
490 void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status);
491 void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
492 void exec_status_reset(ExecStatus *s);
493
494 int exec_shared_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecSharedRuntime **ret);
495 ExecSharedRuntime *exec_shared_runtime_destroy(ExecSharedRuntime *r);
496 ExecSharedRuntime *exec_shared_runtime_unref(ExecSharedRuntime *r);
497 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSharedRuntime*, exec_shared_runtime_unref);
498
499 int exec_shared_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
500 int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
501 int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
502 void exec_shared_runtime_vacuum(Manager *m);
503
504 int exec_runtime_make(ExecSharedRuntime *shared, DynamicCreds *creds, ExecRuntime **ret);
505 ExecRuntime* exec_runtime_free(ExecRuntime *rt);
506 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecRuntime*, exec_runtime_free);
507 ExecRuntime* exec_runtime_destroy(ExecRuntime *rt);
508
509 void exec_params_clear(ExecParameters *p);
510
511 bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
512
513 ExecSetCredential *exec_set_credential_free(ExecSetCredential *sc);
514 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSetCredential*, exec_set_credential_free);
515
516 ExecLoadCredential *exec_load_credential_free(ExecLoadCredential *lc);
517 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecLoadCredential*, exec_load_credential_free);
518
519 void exec_directory_done(ExecDirectory *d);
520 int exec_directory_add(ExecDirectory *d, const char *path, const char *symlink);
521 void exec_directory_sort(ExecDirectory *d);
522
523 extern const struct hash_ops exec_set_credential_hash_ops;
524 extern const struct hash_ops exec_load_credential_hash_ops;
525
526 const char* exec_output_to_string(ExecOutput i) _const_;
527 ExecOutput exec_output_from_string(const char *s) _pure_;
528
529 const char* exec_input_to_string(ExecInput i) _const_;
530 ExecInput exec_input_from_string(const char *s) _pure_;
531
532 const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_;
533 ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_;
534
535 const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_;
536 ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_;
537
538 const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_;
539 ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_;
540
541 const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
542 ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;
543
544 const char* exec_directory_type_symlink_to_string(ExecDirectoryType i) _const_;
545 ExecDirectoryType exec_directory_type_symlink_from_string(const char *s) _pure_;
546
547 const char* exec_resource_type_to_string(ExecDirectoryType i) _const_;
548 ExecDirectoryType exec_resource_type_from_string(const char *s) _pure_;
549
550 bool exec_needs_mount_namespace(const ExecContext *context, const ExecParameters *params, const ExecRuntime *runtime);
551 bool exec_needs_network_namespace(const ExecContext *context);