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