1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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
;
14 #include <sys/capability.h>
16 #include "cgroup-util.h"
17 #include "coredump-util.h"
18 #include "cpu-set-util.h"
19 #include "exec-util.h"
22 #include "missing_resource.h"
23 #include "namespace.h"
25 #include "numa-util.h"
26 #include "path-util.h"
27 #include "time-util.h"
29 #define EXEC_STDIN_DATA_MAX (64U*1024U*1024U)
31 typedef enum ExecUtmpMode
{
36 _EXEC_UTMP_MODE_INVALID
= -1
39 typedef enum ExecInput
{
49 _EXEC_INPUT_INVALID
= -1
52 typedef enum ExecOutput
{
57 EXEC_OUTPUT_KMSG_AND_CONSOLE
,
59 EXEC_OUTPUT_JOURNAL_AND_CONSOLE
,
63 EXEC_OUTPUT_FILE_APPEND
,
64 EXEC_OUTPUT_FILE_TRUNCATE
,
66 _EXEC_OUTPUT_INVALID
= -1
69 typedef enum ExecPreserveMode
{
72 EXEC_PRESERVE_RESTART
,
73 _EXEC_PRESERVE_MODE_MAX
,
74 _EXEC_PRESERVE_MODE_INVALID
= -1
77 typedef enum ExecKeyringMode
{
81 _EXEC_KEYRING_MODE_MAX
,
82 _EXEC_KEYRING_MODE_INVALID
= -1,
85 /* Contains start and exit information about an executed command. */
87 dual_timestamp start_timestamp
;
88 dual_timestamp exit_timestamp
;
90 int code
; /* as in siginfo_t::si_code */
91 int status
; /* as in siginfo_t::si_status */
94 /* Stores information about commands we execute. Covers both configuration settings as well as runtime data. */
98 ExecStatus exec_status
;
99 ExecCommandFlags flags
;
100 LIST_FIELDS(ExecCommand
, command
); /* useful for chaining commands */
103 /* Encapsulates certain aspects of the runtime environment that is to be shared between multiple otherwise separate
104 * invocations of commands. Specifically, this allows sharing of /tmp and /var/tmp data as well as network namespaces
105 * between invocations of commands. This is a reference counted object, with one reference taken by each currently
106 * active command invocation that wants to share this runtime. */
112 char *id
; /* Unit id of the owner */
117 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
119 int netns_storage_socket
[2];
122 typedef enum ExecDirectoryType
{
123 EXEC_DIRECTORY_RUNTIME
= 0,
124 EXEC_DIRECTORY_STATE
,
125 EXEC_DIRECTORY_CACHE
,
127 EXEC_DIRECTORY_CONFIGURATION
,
128 _EXEC_DIRECTORY_TYPE_MAX
,
129 _EXEC_DIRECTORY_TYPE_INVALID
= -1,
132 typedef struct ExecDirectory
{
137 typedef enum ExecCleanMask
{
138 /* In case you wonder why the bitmask below doesn't use "directory" in its name: we want to keep this
139 * generic so that .timer timestamp files can nicely be covered by this too, and similar. */
140 EXEC_CLEAN_RUNTIME
= 1U << EXEC_DIRECTORY_RUNTIME
,
141 EXEC_CLEAN_STATE
= 1U << EXEC_DIRECTORY_STATE
,
142 EXEC_CLEAN_CACHE
= 1U << EXEC_DIRECTORY_CACHE
,
143 EXEC_CLEAN_LOGS
= 1U << EXEC_DIRECTORY_LOGS
,
144 EXEC_CLEAN_CONFIGURATION
= 1U << EXEC_DIRECTORY_CONFIGURATION
,
146 EXEC_CLEAN_ALL
= (1U << _EXEC_DIRECTORY_TYPE_MAX
) - 1,
147 _EXEC_CLEAN_MASK_INVALID
= -1,
150 /* A credential configured with SetCredential= */
151 typedef struct ExecSetCredential
{
157 /* Encodes configuration parameters applied to invoked commands. Does not carry runtime data, but only configuration
158 * changes sourced from unit files and suchlike. ExecContext objects are usually embedded into Unit objects, and do not
159 * change after being loaded. */
162 char **environment_files
;
163 char **pass_environment
;
164 char **unset_environment
;
166 struct rlimit
*rlimit
[_RLIMIT_MAX
];
167 char *working_directory
, *root_directory
, *root_image
, *root_verity
, *root_hash_path
, *root_hash_sig_path
;
168 void *root_hash
, *root_hash_sig
;
169 size_t root_hash_size
, root_hash_sig_size
;
170 LIST_HEAD(MountOptions
, root_image_options
);
171 bool working_directory_missing_ok
:1;
172 bool working_directory_home
:1;
174 bool oom_score_adjust_set
:1;
175 bool coredump_filter_set
:1;
178 bool cpu_sched_set
:1;
179 bool mount_apivfs_set
:1;
181 /* This is not exposed to the user but available internally. We need it to make sure that whenever we
182 * spawn /usr/bin/mount it is run in the same process group as us so that the autofs logic detects
183 * that it belongs to us and we don't enter a trigger loop. */
186 bool cpu_sched_reset_on_fork
;
190 int oom_score_adjust
;
193 int cpu_sched_policy
;
194 int cpu_sched_priority
;
195 uint64_t coredump_filter
;
198 NUMAPolicy numa_policy
;
199 bool cpu_affinity_from_numa
;
202 ExecOutput std_output
;
203 ExecOutput std_error
;
205 char *stdio_fdname
[3];
209 size_t stdin_data_size
;
211 nsec_t timer_slack_nsec
;
217 bool tty_vt_disallocate
;
221 ExecKeyringMode keyring_mode
;
223 /* Since resolving these names might involve socket
224 * connections and we don't want to deadlock ourselves these
225 * names are resolved on execution only and in the child
229 char **supplementary_groups
;
234 ExecUtmpMode utmp_mode
;
236 bool no_new_privileges
;
238 bool selinux_context_ignore
;
239 bool apparmor_profile_ignore
;
240 bool smack_process_label_ignore
;
242 char *selinux_context
;
243 char *apparmor_profile
;
244 char *smack_process_label
;
246 char **read_write_paths
, **read_only_paths
, **inaccessible_paths
;
247 unsigned long mount_flags
;
248 BindMount
*bind_mounts
;
249 size_t n_bind_mounts
;
250 TemporaryFileSystem
*temporary_filesystems
;
251 size_t n_temporary_filesystems
;
252 MountImage
*mount_images
;
253 size_t n_mount_images
;
255 uint64_t capability_bounding_set
;
256 uint64_t capability_ambient_set
;
260 bool syslog_level_prefix
;
261 char *syslog_identifier
;
263 struct iovec
* log_extra_fields
;
264 size_t n_log_extra_fields
;
266 usec_t log_ratelimit_interval_usec
;
267 unsigned log_ratelimit_burst
;
273 ProtectProc protect_proc
; /* hidepid= */
274 ProcSubset proc_subset
; /* subset= */
277 bool private_network
;
278 bool private_devices
;
281 bool protect_kernel_tunables
;
282 bool protect_kernel_modules
;
283 bool protect_kernel_logs
;
285 bool protect_control_groups
;
286 ProtectSystem protect_system
;
287 ProtectHome protect_home
;
288 bool protect_hostname
;
294 bool memory_deny_write_execute
;
295 bool restrict_realtime
;
296 bool restrict_suid_sgid
;
298 bool lock_personality
;
299 unsigned long personality
;
301 unsigned long restrict_namespaces
; /* The CLONE_NEWxyz flags permitted to the unit's processes */
303 Hashmap
*syscall_filter
;
306 bool syscall_allow_list
:1;
308 Hashmap
*syscall_log
;
309 bool syscall_log_allow_list
:1; /* Log listed system calls */
311 bool address_families_allow_list
:1;
312 Set
*address_families
;
314 char *network_namespace_path
;
316 ExecDirectory directories
[_EXEC_DIRECTORY_TYPE_MAX
];
317 ExecPreserveMode runtime_directory_preserve_mode
;
318 usec_t timeout_clean_usec
;
320 Hashmap
*set_credentials
; /* output id → ExecSetCredential */
321 char **load_credentials
; /* pairs of output id, path/input id */
324 static inline bool exec_context_restrict_namespaces_set(const ExecContext
*c
) {
327 return (c
->restrict_namespaces
& NAMESPACE_FLAGS_ALL
) != NAMESPACE_FLAGS_ALL
;
330 static inline bool exec_context_with_rootfs(const ExecContext
*c
) {
333 /* Checks if RootDirectory= or RootImage= are used */
335 return !empty_or_root(c
->root_directory
) || c
->root_image
;
338 typedef enum ExecFlags
{
339 EXEC_APPLY_SANDBOXING
= 1 << 0,
340 EXEC_APPLY_CHROOT
= 1 << 1,
341 EXEC_APPLY_TTY_STDIN
= 1 << 2,
342 EXEC_PASS_LOG_UNIT
= 1 << 3, /* Whether to pass the unit name to the service's journal stream connection */
343 EXEC_CHOWN_DIRECTORIES
= 1 << 4, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */
344 EXEC_NSS_BYPASS_BUS
= 1 << 5, /* Set the SYSTEMD_NSS_BYPASS_BUS environment variable, to disable nss-systemd for dbus */
345 EXEC_CGROUP_DELEGATE
= 1 << 6,
346 EXEC_IS_CONTROL
= 1 << 7,
347 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 */
348 EXEC_WRITE_CREDENTIALS
= 1 << 9, /* Set up the credential store logic */
350 /* The following are not used by execute.c, but by consumers internally */
351 EXEC_PASS_FDS
= 1 << 10,
352 EXEC_SETENV_RESULT
= 1 << 11,
353 EXEC_SET_WATCHDOG
= 1 << 12,
356 /* Parameters for a specific invocation of a command. This structure is put together right before a command is
358 struct ExecParameters
{
364 size_t n_storage_fds
;
367 bool selinux_context_net
:1;
369 CGroupMask cgroup_supported
;
370 const char *cgroup_path
;
373 const char *received_credentials
;
375 const char *confirm_spawn
;
377 usec_t watchdog_usec
;
385 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done */
390 #include "dynamic-user.h"
392 int exec_spawn(Unit
*unit
,
393 ExecCommand
*command
,
394 const ExecContext
*context
,
395 const ExecParameters
*exec_params
,
396 ExecRuntime
*runtime
,
397 DynamicCreds
*dynamic_creds
,
400 void exec_command_done_array(ExecCommand
*c
, size_t n
);
401 ExecCommand
* exec_command_free_list(ExecCommand
*c
);
402 void exec_command_free_array(ExecCommand
**c
, size_t n
);
403 void exec_command_reset_status_array(ExecCommand
*c
, size_t n
);
404 void exec_command_reset_status_list_array(ExecCommand
**c
, size_t n
);
405 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
);
406 void exec_command_append_list(ExecCommand
**l
, ExecCommand
*e
);
407 int exec_command_set(ExecCommand
*c
, const char *path
, ...) _sentinel_
;
408 int exec_command_append(ExecCommand
*c
, const char *path
, ...) _sentinel_
;
410 void exec_context_init(ExecContext
*c
);
411 void exec_context_done(ExecContext
*c
);
412 void exec_context_dump(const ExecContext
*c
, FILE* f
, const char *prefix
);
414 int exec_context_destroy_runtime_directory(const ExecContext
*c
, const char *runtime_root
);
415 int exec_context_destroy_credentials(const ExecContext
*c
, const char *runtime_root
, const char *unit
);
417 const char* exec_context_fdname(const ExecContext
*c
, int fd_index
);
419 bool exec_context_may_touch_console(const ExecContext
*c
);
420 bool exec_context_maintains_privileges(const ExecContext
*c
);
422 int exec_context_get_effective_ioprio(const ExecContext
*c
);
423 bool exec_context_get_effective_mount_apivfs(const ExecContext
*c
);
425 void exec_context_free_log_extra_fields(ExecContext
*c
);
427 void exec_context_revert_tty(ExecContext
*c
);
429 int exec_context_get_clean_directories(ExecContext
*c
, char **prefix
, ExecCleanMask mask
, char ***ret
);
430 int exec_context_get_clean_mask(ExecContext
*c
, ExecCleanMask
*ret
);
432 void exec_status_start(ExecStatus
*s
, pid_t pid
);
433 void exec_status_exit(ExecStatus
*s
, const ExecContext
*context
, pid_t pid
, int code
, int status
);
434 void exec_status_dump(const ExecStatus
*s
, FILE *f
, const char *prefix
);
435 void exec_status_reset(ExecStatus
*s
);
437 int exec_runtime_acquire(Manager
*m
, const ExecContext
*c
, const char *name
, bool create
, ExecRuntime
**ret
);
438 ExecRuntime
*exec_runtime_unref(ExecRuntime
*r
, bool destroy
);
440 int exec_runtime_serialize(const Manager
*m
, FILE *f
, FDSet
*fds
);
441 int exec_runtime_deserialize_compat(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
);
442 int exec_runtime_deserialize_one(Manager
*m
, const char *value
, FDSet
*fds
);
443 void exec_runtime_vacuum(Manager
*m
);
445 void exec_params_clear(ExecParameters
*p
);
447 bool exec_context_get_cpu_affinity_from_numa(const ExecContext
*c
);
449 ExecSetCredential
*exec_set_credential_free(ExecSetCredential
*sc
);
450 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSetCredential
*, exec_set_credential_free
);
452 extern const struct hash_ops exec_set_credential_hash_ops
;
454 const char* exec_output_to_string(ExecOutput i
) _const_
;
455 ExecOutput
exec_output_from_string(const char *s
) _pure_
;
457 const char* exec_input_to_string(ExecInput i
) _const_
;
458 ExecInput
exec_input_from_string(const char *s
) _pure_
;
460 const char* exec_utmp_mode_to_string(ExecUtmpMode i
) _const_
;
461 ExecUtmpMode
exec_utmp_mode_from_string(const char *s
) _pure_
;
463 const char* exec_preserve_mode_to_string(ExecPreserveMode i
) _const_
;
464 ExecPreserveMode
exec_preserve_mode_from_string(const char *s
) _pure_
;
466 const char* exec_keyring_mode_to_string(ExecKeyringMode i
) _const_
;
467 ExecKeyringMode
exec_keyring_mode_from_string(const char *s
) _pure_
;
469 const char* exec_directory_type_to_string(ExecDirectoryType i
) _const_
;
470 ExecDirectoryType
exec_directory_type_from_string(const char *s
) _pure_
;
472 const char* exec_resource_type_to_string(ExecDirectoryType i
) _const_
;
473 ExecDirectoryType
exec_resource_type_from_string(const char *s
) _pure_
;