1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
5 #include "conf-parser.h"
6 #include "cpu-set-util.h"
7 #include "hostname-util.h"
8 #include "nspawn-network.h"
9 #include "nspawn-settings.h"
10 #include "parse-util.h"
11 #include "process-util.h"
12 #include "rlimit-util.h"
13 #include "socket-util.h"
14 #include "string-table.h"
15 #include "string-util.h"
17 #include "user-util.h"
20 Settings
*settings_new(void) {
28 .start_mode
= _START_MODE_INVALID
,
29 .personality
= PERSONALITY_INVALID
,
31 .resolv_conf
= _RESOLV_CONF_MODE_INVALID
,
32 .link_journal
= _LINK_JOURNAL_INVALID
,
33 .timezone
= _TIMEZONE_MODE_INVALID
,
35 .userns_mode
= _USER_NAMESPACE_MODE_INVALID
,
37 .uid_shift
= UID_INVALID
,
38 .uid_range
= UID_INVALID
,
40 .no_new_privileges
= -1,
43 .volatile_mode
= _VOLATILE_MODE_INVALID
,
45 .private_network
= -1,
48 .full_capabilities
= CAPABILITY_QUINTET_NULL
,
53 .console_mode
= _CONSOLE_MODE_INVALID
,
54 .console_width
= (unsigned) -1,
55 .console_height
= (unsigned) -1,
57 .clone_ns_flags
= (unsigned long) -1,
64 int settings_load(FILE *f
, const char *path
, Settings
**ret
) {
65 _cleanup_(settings_freep
) Settings
*s
= NULL
;
75 r
= config_parse(NULL
, path
, f
,
79 config_item_perf_lookup
, nspawn_gperf_lookup
,
85 /* Make sure that if userns_mode is set, userns_chown is set to something appropriate, and vice versa. Either
86 * both fields shall be initialized or neither. */
87 if (s
->userns_mode
== USER_NAMESPACE_PICK
)
88 s
->userns_chown
= true;
89 else if (s
->userns_mode
!= _USER_NAMESPACE_MODE_INVALID
&& s
->userns_chown
< 0)
90 s
->userns_chown
= false;
92 if (s
->userns_chown
>= 0 && s
->userns_mode
== _USER_NAMESPACE_MODE_INVALID
)
93 s
->userns_mode
= USER_NAMESPACE_NO
;
99 static void free_oci_hooks(OciHook
*h
, size_t n
) {
104 for (i
= 0; i
< n
; i
++) {
106 strv_free(h
[i
].args
);
113 void device_node_array_free(DeviceNode
*node
, size_t n
) {
116 for (i
= 0; i
< n
; i
++)
122 Settings
* settings_free(Settings
*s
) {
126 strv_free(s
->parameters
);
127 strv_free(s
->environment
);
129 free(s
->pivot_root_new
);
130 free(s
->pivot_root_old
);
131 free(s
->working_directory
);
132 strv_free(s
->syscall_allow_list
);
133 strv_free(s
->syscall_deny_list
);
134 rlimit_free_all(s
->rlimit
);
136 cpu_set_reset(&s
->cpu_set
);
138 strv_free(s
->network_interfaces
);
139 strv_free(s
->network_macvlan
);
140 strv_free(s
->network_ipvlan
);
141 strv_free(s
->network_veth_extra
);
142 free(s
->network_bridge
);
143 free(s
->network_zone
);
144 expose_port_free_all(s
->expose_ports
);
146 custom_mount_free_all(s
->custom_mounts
, s
->n_custom_mounts
);
151 free_oci_hooks(s
->oci_hooks_prestart
, s
->n_oci_hooks_prestart
);
152 free_oci_hooks(s
->oci_hooks_poststart
, s
->n_oci_hooks_poststart
);
153 free_oci_hooks(s
->oci_hooks_poststop
, s
->n_oci_hooks_poststop
);
156 sd_bus_message_unref(s
->properties
);
158 free(s
->supplementary_gids
);
159 device_node_array_free(s
->extra_nodes
, s
->n_extra_nodes
);
160 free(s
->network_namespace_path
);
162 strv_free(s
->sysctl
);
165 seccomp_release(s
->seccomp
);
171 bool settings_private_network(Settings
*s
) {
175 s
->private_network
> 0 ||
176 s
->network_veth
> 0 ||
179 s
->network_interfaces
||
180 s
->network_macvlan
||
182 s
->network_veth_extra
;
185 bool settings_network_veth(Settings
*s
) {
189 s
->network_veth
> 0 ||
194 int settings_allocate_properties(Settings
*s
) {
195 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
203 r
= sd_bus_default_system(&bus
);
207 r
= sd_bus_message_new(bus
, &s
->properties
, SD_BUS_MESSAGE_METHOD_CALL
);
214 DEFINE_CONFIG_PARSE_ENUM(config_parse_volatile_mode
, volatile_mode
, VolatileMode
, "Failed to parse volatile mode");
216 int config_parse_expose_port(
218 const char *filename
,
221 unsigned section_line
,
235 r
= expose_port_parse(&s
->expose_ports
, rvalue
);
237 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Duplicate port specification, ignoring: %s", rvalue
);
239 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse host port %s: %m", rvalue
);
244 int config_parse_capability(
246 const char *filename
,
249 unsigned section_line
,
256 uint64_t u
= 0, *result
= data
;
264 _cleanup_free_
char *word
= NULL
;
266 r
= extract_first_word(&rvalue
, &word
, NULL
, 0);
270 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to extract capability string, ignoring: %s", rvalue
);
276 if (streq(word
, "all"))
279 r
= capability_from_name(word
);
281 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse capability, ignoring: %s", word
);
285 u
|= UINT64_C(1) << r
;
296 int config_parse_pivot_root(
298 const char *filename
,
301 unsigned section_line
,
308 Settings
*settings
= data
;
315 r
= pivot_root_parse(&settings
->pivot_root_new
, &settings
->pivot_root_old
, rvalue
);
317 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid pivot root mount specification %s: %m", rvalue
);
322 int config_parse_bind(
324 const char *filename
,
327 unsigned section_line
,
334 Settings
*settings
= data
;
341 r
= bind_mount_parse(&settings
->custom_mounts
, &settings
->n_custom_mounts
, rvalue
, ltype
);
343 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid bind mount specification %s: %m", rvalue
);
348 int config_parse_tmpfs(
350 const char *filename
,
353 unsigned section_line
,
360 Settings
*settings
= data
;
367 r
= tmpfs_mount_parse(&settings
->custom_mounts
, &settings
->n_custom_mounts
, rvalue
);
369 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid temporary file system specification %s: %m", rvalue
);
374 int config_parse_inaccessible(
376 const char *filename
,
379 unsigned section_line
,
386 Settings
*settings
= data
;
393 r
= inaccessible_mount_parse(&settings
->custom_mounts
, &settings
->n_custom_mounts
, rvalue
);
395 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid inaccessible file system specification %s: %m", rvalue
);
400 int config_parse_overlay(
402 const char *filename
,
405 unsigned section_line
,
412 Settings
*settings
= data
;
419 r
= overlay_mount_parse(&settings
->custom_mounts
, &settings
->n_custom_mounts
, rvalue
, ltype
);
421 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid overlay file system specification %s, ignoring: %m", rvalue
);
426 int config_parse_veth_extra(
428 const char *filename
,
431 unsigned section_line
,
438 Settings
*settings
= data
;
445 r
= veth_extra_parse(&settings
->network_veth_extra
, rvalue
);
447 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid extra virtual Ethernet link specification %s: %m", rvalue
);
452 int config_parse_network_zone(
454 const char *filename
,
457 unsigned section_line
,
464 Settings
*settings
= data
;
465 _cleanup_free_
char *j
= NULL
;
471 j
= strjoin("vz-", rvalue
);
472 if (!ifname_valid(j
)) {
473 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid network zone name, ignoring: %s", rvalue
);
477 return free_and_replace(settings
->network_zone
, j
);
480 int config_parse_boot(
482 const char *filename
,
485 unsigned section_line
,
492 Settings
*settings
= data
;
499 r
= parse_boolean(rvalue
);
501 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse Boot= parameter %s, ignoring: %m", rvalue
);
506 if (settings
->start_mode
== START_PID2
)
509 settings
->start_mode
= START_BOOT
;
511 if (settings
->start_mode
== START_BOOT
)
514 if (settings
->start_mode
< 0)
515 settings
->start_mode
= START_PID1
;
521 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Conflicting Boot= or ProcessTwo= setting found. Ignoring.");
525 int config_parse_pid2(
527 const char *filename
,
530 unsigned section_line
,
537 Settings
*settings
= data
;
544 r
= parse_boolean(rvalue
);
546 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse ProcessTwo= parameter %s, ignoring: %m", rvalue
);
551 if (settings
->start_mode
== START_BOOT
)
554 settings
->start_mode
= START_PID2
;
556 if (settings
->start_mode
== START_PID2
)
559 if (settings
->start_mode
< 0)
560 settings
->start_mode
= START_PID1
;
566 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Conflicting Boot= or ProcessTwo= setting found. Ignoring.");
570 int config_parse_private_users(
572 const char *filename
,
575 unsigned section_line
,
582 Settings
*settings
= data
;
589 r
= parse_boolean(rvalue
);
591 /* no: User namespacing off */
592 settings
->userns_mode
= USER_NAMESPACE_NO
;
593 settings
->uid_shift
= UID_INVALID
;
594 settings
->uid_range
= UINT32_C(0x10000);
596 /* yes: User namespacing on, UID range is read from root dir */
597 settings
->userns_mode
= USER_NAMESPACE_FIXED
;
598 settings
->uid_shift
= UID_INVALID
;
599 settings
->uid_range
= UINT32_C(0x10000);
600 } else if (streq(rvalue
, "pick")) {
601 /* pick: User namespacing on, UID range is picked randomly */
602 settings
->userns_mode
= USER_NAMESPACE_PICK
;
603 settings
->uid_shift
= UID_INVALID
;
604 settings
->uid_range
= UINT32_C(0x10000);
606 const char *range
, *shift
;
609 /* anything else: User namespacing on, UID range is explicitly configured */
611 range
= strchr(rvalue
, ':');
613 shift
= strndupa(rvalue
, range
- rvalue
);
616 r
= safe_atou32(range
, &rn
);
617 if (r
< 0 || rn
<= 0) {
618 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "UID/GID range invalid, ignoring: %s", range
);
623 rn
= UINT32_C(0x10000);
626 r
= parse_uid(shift
, &sh
);
628 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "UID/GID shift invalid, ignoring: %s", range
);
632 settings
->userns_mode
= USER_NAMESPACE_FIXED
;
633 settings
->uid_shift
= sh
;
634 settings
->uid_range
= rn
;
640 int config_parse_syscall_filter(
642 const char *filename
,
645 unsigned section_line
,
652 Settings
*settings
= data
;
661 negative
= rvalue
[0] == '~';
662 items
= negative
? rvalue
+ 1 : rvalue
;
665 _cleanup_free_
char *word
= NULL
;
667 r
= extract_first_word(&items
, &word
, NULL
, 0);
673 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
674 "Failed to parse SystemCallFilter= parameter %s, ignoring: %m", rvalue
);
679 r
= strv_extend(&settings
->syscall_deny_list
, word
);
681 r
= strv_extend(&settings
->syscall_allow_list
, word
);
687 int config_parse_hostname(
689 const char *filename
,
692 unsigned section_line
,
704 if (!hostname_is_valid(rvalue
, 0)) {
705 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid hostname, ignoring: %s", rvalue
);
709 return free_and_strdup_warn(s
, empty_to_null(rvalue
));
712 int config_parse_oom_score_adjust(
714 const char *filename
,
717 unsigned section_line
,
724 Settings
*settings
= data
;
730 if (isempty(rvalue
)) {
731 settings
->oom_score_adjust_set
= false;
735 r
= parse_oom_score_adjust(rvalue
, &oa
);
737 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "OOM score adjust value out of range, ignoring: %s", rvalue
);
741 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse the OOM score adjust value, ignoring: %s", rvalue
);
745 settings
->oom_score_adjust
= oa
;
746 settings
->oom_score_adjust_set
= true;
751 int config_parse_cpu_affinity(
753 const char *filename
,
756 unsigned section_line
,
763 Settings
*settings
= data
;
768 return parse_cpu_set_extend(rvalue
, &settings
->cpu_set
, true, unit
, filename
, line
, lvalue
);
771 DEFINE_CONFIG_PARSE_ENUM(config_parse_resolv_conf
, resolv_conf_mode
, ResolvConfMode
, "Failed to parse resolv.conf mode");
773 static const char *const resolv_conf_mode_table
[_RESOLV_CONF_MODE_MAX
] = {
774 [RESOLV_CONF_OFF
] = "off",
775 [RESOLV_CONF_COPY_HOST
] = "copy-host",
776 [RESOLV_CONF_COPY_STATIC
] = "copy-static",
777 [RESOLV_CONF_COPY_UPLINK
] = "copy-uplink",
778 [RESOLV_CONF_COPY_STUB
] = "copy-stub",
779 [RESOLV_CONF_REPLACE_HOST
] = "replace-host",
780 [RESOLV_CONF_REPLACE_STATIC
] = "replace-static",
781 [RESOLV_CONF_REPLACE_UPLINK
] = "replace-uplink",
782 [RESOLV_CONF_REPLACE_STUB
] = "replace-stub",
783 [RESOLV_CONF_BIND_HOST
] = "bind-host",
784 [RESOLV_CONF_BIND_STATIC
] = "bind-static",
785 [RESOLV_CONF_BIND_UPLINK
] = "bind-uplink",
786 [RESOLV_CONF_BIND_STUB
] = "bind-stub",
787 [RESOLV_CONF_DELETE
] = "delete",
788 [RESOLV_CONF_AUTO
] = "auto",
791 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(resolv_conf_mode
, ResolvConfMode
, RESOLV_CONF_AUTO
);
793 int parse_link_journal(const char *s
, LinkJournal
*ret_mode
, bool *ret_try
) {
800 if (streq(s
, "auto")) {
801 *ret_mode
= LINK_AUTO
;
803 } else if (streq(s
, "guest")) {
804 *ret_mode
= LINK_GUEST
;
806 } else if (streq(s
, "host")) {
807 *ret_mode
= LINK_HOST
;
809 } else if (streq(s
, "try-guest")) {
810 *ret_mode
= LINK_GUEST
;
812 } else if (streq(s
, "try-host")) {
813 *ret_mode
= LINK_HOST
;
816 /* Also support boolean values, to make things less confusing. */
817 r
= parse_boolean(s
);
821 /* Let's consider "true" to be equivalent to "auto". */
822 *ret_mode
= r
? LINK_AUTO
: LINK_NO
;
829 int config_parse_link_journal(
831 const char *filename
,
834 unsigned section_line
,
841 Settings
*settings
= data
;
847 r
= parse_link_journal(rvalue
, &settings
->link_journal
, &settings
->link_journal_try
);
849 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse link journal mode, ignoring: %s", rvalue
);
854 DEFINE_CONFIG_PARSE_ENUM(config_parse_timezone
, timezone_mode
, TimezoneMode
, "Failed to parse timezone mode");
856 static const char *const timezone_mode_table
[_TIMEZONE_MODE_MAX
] = {
857 [TIMEZONE_OFF
] = "off",
858 [TIMEZONE_COPY
] = "copy",
859 [TIMEZONE_BIND
] = "bind",
860 [TIMEZONE_SYMLINK
] = "symlink",
861 [TIMEZONE_DELETE
] = "delete",
862 [TIMEZONE_AUTO
] = "auto",
865 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(timezone_mode
, TimezoneMode
, TIMEZONE_AUTO
);