1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <sched.h>
5 #include <stdio.h>
6 
7 #if HAVE_SECCOMP
8 #include <seccomp.h>
9 #endif
10 
11 #include "sd-bus.h"
12 #include "sd-id128.h"
13 
14 #include "capability-util.h"
15 #include "conf-parser.h"
16 #include "cpu-set-util.h"
17 #include "macro.h"
18 #include "missing_resource.h"
19 #include "nspawn-expose-ports.h"
20 #include "nspawn-mount.h"
21 #include "time-util.h"
22 
23 typedef enum StartMode {
24         START_PID1, /* Run parameters as command line as process 1 */
25         START_PID2, /* Use stub init process as PID 1, run parameters as command line as process 2 */
26         START_BOOT, /* Search for init system, pass arguments as parameters */
27         _START_MODE_MAX,
28         _START_MODE_INVALID = -EINVAL,
29 } StartMode;
30 
31 typedef enum UserNamespaceMode {
32         USER_NAMESPACE_NO,
33         USER_NAMESPACE_FIXED,
34         USER_NAMESPACE_PICK,
35         _USER_NAMESPACE_MODE_MAX,
36         _USER_NAMESPACE_MODE_INVALID = -EINVAL,
37 } UserNamespaceMode;
38 
39 typedef enum UserNamespaceOwnership {
40         USER_NAMESPACE_OWNERSHIP_OFF,
41         USER_NAMESPACE_OWNERSHIP_CHOWN,
42         USER_NAMESPACE_OWNERSHIP_MAP,
43         USER_NAMESPACE_OWNERSHIP_AUTO,
44         _USER_NAMESPACE_OWNERSHIP_MAX,
45         _USER_NAMESPACE_OWNERSHIP_INVALID = -1,
46 } UserNamespaceOwnership;
47 
48 typedef enum ResolvConfMode {
49         RESOLV_CONF_OFF,
50         RESOLV_CONF_COPY_HOST,     /* /etc/resolv.conf */
51         RESOLV_CONF_COPY_STATIC,   /* /usr/lib/systemd/resolv.conf */
52         RESOLV_CONF_COPY_UPLINK,   /* /run/systemd/resolve/resolv.conf */
53         RESOLV_CONF_COPY_STUB,     /* /run/systemd/resolve/stub-resolv.conf */
54         RESOLV_CONF_REPLACE_HOST,
55         RESOLV_CONF_REPLACE_STATIC,
56         RESOLV_CONF_REPLACE_UPLINK,
57         RESOLV_CONF_REPLACE_STUB,
58         RESOLV_CONF_BIND_HOST,
59         RESOLV_CONF_BIND_STATIC,
60         RESOLV_CONF_BIND_UPLINK,
61         RESOLV_CONF_BIND_STUB,
62         RESOLV_CONF_DELETE,
63         RESOLV_CONF_AUTO,
64         _RESOLV_CONF_MODE_MAX,
65         _RESOLV_CONF_MODE_INVALID = -EINVAL,
66 } ResolvConfMode;
67 
68 typedef enum LinkJournal {
69         LINK_NO,
70         LINK_AUTO,
71         LINK_HOST,
72         LINK_GUEST,
73         _LINK_JOURNAL_MAX,
74         _LINK_JOURNAL_INVALID = -EINVAL,
75 } LinkJournal;
76 
77 typedef enum TimezoneMode {
78         TIMEZONE_OFF,
79         TIMEZONE_COPY,
80         TIMEZONE_BIND,
81         TIMEZONE_SYMLINK,
82         TIMEZONE_DELETE,
83         TIMEZONE_AUTO,
84         _TIMEZONE_MODE_MAX,
85         _TIMEZONE_MODE_INVALID = -EINVAL,
86 } TimezoneMode;
87 
88 typedef enum ConsoleMode {
89         CONSOLE_INTERACTIVE,
90         CONSOLE_READ_ONLY,
91         CONSOLE_PASSIVE,
92         CONSOLE_PIPE,
93         _CONSOLE_MODE_MAX,
94         _CONSOLE_MODE_INVALID = -EINVAL,
95 } ConsoleMode;
96 
97 typedef enum SettingsMask {
98         SETTING_START_MODE        = UINT64_C(1) << 0,
99         SETTING_ENVIRONMENT       = UINT64_C(1) << 1,
100         SETTING_USER              = UINT64_C(1) << 2,
101         SETTING_CAPABILITY        = UINT64_C(1) << 3,
102         SETTING_KILL_SIGNAL       = UINT64_C(1) << 4,
103         SETTING_PERSONALITY       = UINT64_C(1) << 5,
104         SETTING_MACHINE_ID        = UINT64_C(1) << 6,
105         SETTING_NETWORK           = UINT64_C(1) << 7,
106         SETTING_EXPOSE_PORTS      = UINT64_C(1) << 8,
107         SETTING_READ_ONLY         = UINT64_C(1) << 9,
108         SETTING_VOLATILE_MODE     = UINT64_C(1) << 10,
109         SETTING_CUSTOM_MOUNTS     = UINT64_C(1) << 11,
110         SETTING_WORKING_DIRECTORY = UINT64_C(1) << 12,
111         SETTING_USERNS            = UINT64_C(1) << 13,
112         SETTING_NOTIFY_READY      = UINT64_C(1) << 14,
113         SETTING_PIVOT_ROOT        = UINT64_C(1) << 15,
114         SETTING_SYSCALL_FILTER    = UINT64_C(1) << 16,
115         SETTING_HOSTNAME          = UINT64_C(1) << 17,
116         SETTING_NO_NEW_PRIVILEGES = UINT64_C(1) << 18,
117         SETTING_OOM_SCORE_ADJUST  = UINT64_C(1) << 19,
118         SETTING_CPU_AFFINITY      = UINT64_C(1) << 20,
119         SETTING_RESOLV_CONF       = UINT64_C(1) << 21,
120         SETTING_LINK_JOURNAL      = UINT64_C(1) << 22,
121         SETTING_TIMEZONE          = UINT64_C(1) << 23,
122         SETTING_EPHEMERAL         = UINT64_C(1) << 24,
123         SETTING_SLICE             = UINT64_C(1) << 25,
124         SETTING_DIRECTORY         = UINT64_C(1) << 26,
125         SETTING_USE_CGNS          = UINT64_C(1) << 27,
126         SETTING_CLONE_NS_FLAGS    = UINT64_C(1) << 28,
127         SETTING_CONSOLE_MODE      = UINT64_C(1) << 29,
128         SETTING_CREDENTIALS       = UINT64_C(1) << 30,
129         SETTING_BIND_USER         = UINT64_C(1) << 31,
130         SETTING_SUPPRESS_SYNC     = UINT64_C(1) << 32,
131         SETTING_RLIMIT_FIRST      = UINT64_C(1) << 33, /* we define one bit per resource limit here */
132         SETTING_RLIMIT_LAST       = UINT64_C(1) << (33 + _RLIMIT_MAX - 1),
133         _SETTINGS_MASK_ALL        = (UINT64_C(1) << (33 + _RLIMIT_MAX)) -1,
134         _SETTING_FORCE_ENUM_WIDTH = UINT64_MAX
135 } SettingsMask;
136 
137 /* We want to use SETTING_RLIMIT_FIRST in shifts, so make sure it is really 64 bits
138  * when used in expressions. */
139 #define SETTING_RLIMIT_FIRST ((uint64_t) SETTING_RLIMIT_FIRST)
140 #define SETTING_RLIMIT_LAST ((uint64_t) SETTING_RLIMIT_LAST)
141 
142 assert_cc(sizeof(SettingsMask) == 8);
143 assert_cc(sizeof(SETTING_RLIMIT_FIRST) == 8);
144 assert_cc(sizeof(SETTING_RLIMIT_LAST) == 8);
145 
146 typedef struct DeviceNode {
147         char *path;
148         unsigned major;
149         unsigned minor;
150         mode_t mode;
151         uid_t uid;
152         gid_t gid;
153 } DeviceNode;
154 
155 typedef struct OciHook {
156         char *path;
157         char **args;
158         char **env;
159         usec_t timeout;
160 } OciHook;
161 
162 typedef struct Settings {
163         /* [Exec] */
164         StartMode start_mode;
165         int ephemeral;
166         char **parameters;
167         char **environment;
168         char *user;
169         uint64_t capability;
170         uint64_t drop_capability;
171         uint64_t ambient_capability;
172         int kill_signal;
173         unsigned long personality;
174         sd_id128_t machine_id;
175         char *working_directory;
176         char *pivot_root_new;
177         char *pivot_root_old;
178         UserNamespaceMode userns_mode;
179         uid_t uid_shift, uid_range;
180         int notify_ready;
181         char **syscall_allow_list;
182         char **syscall_deny_list;
183         struct rlimit *rlimit[_RLIMIT_MAX];
184         char *hostname;
185         int no_new_privileges;
186         int oom_score_adjust;
187         bool oom_score_adjust_set;
188         CPUSet cpu_set;
189         ResolvConfMode resolv_conf;
190         LinkJournal link_journal;
191         bool link_journal_try;
192         TimezoneMode timezone;
193         int suppress_sync;
194 
195         /* [Files] */
196         int read_only;
197         VolatileMode volatile_mode;
198         CustomMount *custom_mounts;
199         size_t n_custom_mounts;
200         UserNamespaceOwnership userns_ownership;
201         char **bind_user;
202 
203         /* [Network] */
204         int private_network;
205         int network_veth;
206         char *network_bridge;
207         char *network_zone;
208         char **network_interfaces;
209         char **network_macvlan;
210         char **network_ipvlan;
211         char **network_veth_extra;
212         ExposePort *expose_ports;
213 
214         /* Additional fields, that are specific to OCI runtime case */
215         char *bundle;
216         char *root;
217         OciHook *oci_hooks_prestart, *oci_hooks_poststart, *oci_hooks_poststop;
218         size_t n_oci_hooks_prestart, n_oci_hooks_poststart, n_oci_hooks_poststop;
219         char *slice;
220         sd_bus_message *properties;
221         CapabilityQuintet full_capabilities;
222         uid_t uid;
223         gid_t gid;
224         gid_t *supplementary_gids;
225         size_t n_supplementary_gids;
226         unsigned console_width, console_height;
227         ConsoleMode console_mode;
228         DeviceNode *extra_nodes;
229         size_t n_extra_nodes;
230         unsigned long clone_ns_flags;
231         char *network_namespace_path;
232         int use_cgns;
233         char **sysctl;
234 #if HAVE_SECCOMP
235         scmp_filter_ctx seccomp;
236 #endif
237 } Settings;
238 
239 Settings *settings_new(void);
240 int settings_load(FILE *f, const char *path, Settings **ret);
241 Settings* settings_free(Settings *s);
242 
243 bool settings_network_veth(Settings *s);
244 bool settings_private_network(Settings *s);
245 bool settings_network_configured(Settings *s);
246 
247 int settings_allocate_properties(Settings *s);
248 
249 DEFINE_TRIVIAL_CLEANUP_FUNC(Settings*, settings_free);
250 
251 const struct ConfigPerfItem* nspawn_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
252 
253 CONFIG_PARSER_PROTOTYPE(config_parse_capability);
254 CONFIG_PARSER_PROTOTYPE(config_parse_expose_port);
255 CONFIG_PARSER_PROTOTYPE(config_parse_volatile_mode);
256 CONFIG_PARSER_PROTOTYPE(config_parse_pivot_root);
257 CONFIG_PARSER_PROTOTYPE(config_parse_bind);
258 CONFIG_PARSER_PROTOTYPE(config_parse_tmpfs);
259 CONFIG_PARSER_PROTOTYPE(config_parse_overlay);
260 CONFIG_PARSER_PROTOTYPE(config_parse_inaccessible);
261 CONFIG_PARSER_PROTOTYPE(config_parse_veth_extra);
262 CONFIG_PARSER_PROTOTYPE(config_parse_network_zone);
263 CONFIG_PARSER_PROTOTYPE(config_parse_boot);
264 CONFIG_PARSER_PROTOTYPE(config_parse_pid2);
265 CONFIG_PARSER_PROTOTYPE(config_parse_private_users);
266 CONFIG_PARSER_PROTOTYPE(config_parse_syscall_filter);
267 CONFIG_PARSER_PROTOTYPE(config_parse_oom_score_adjust);
268 CONFIG_PARSER_PROTOTYPE(config_parse_cpu_affinity);
269 CONFIG_PARSER_PROTOTYPE(config_parse_resolv_conf);
270 CONFIG_PARSER_PROTOTYPE(config_parse_link_journal);
271 CONFIG_PARSER_PROTOTYPE(config_parse_timezone);
272 CONFIG_PARSER_PROTOTYPE(config_parse_userns_chown);
273 CONFIG_PARSER_PROTOTYPE(config_parse_userns_ownership);
274 CONFIG_PARSER_PROTOTYPE(config_parse_bind_user);
275 
276 const char *resolv_conf_mode_to_string(ResolvConfMode a) _const_;
277 ResolvConfMode resolv_conf_mode_from_string(const char *s) _pure_;
278 
279 const char *timezone_mode_to_string(TimezoneMode a) _const_;
280 TimezoneMode timezone_mode_from_string(const char *s) _pure_;
281 
282 const char *user_namespace_ownership_to_string(UserNamespaceOwnership a) _const_;
283 UserNamespaceOwnership user_namespace_ownership_from_string(const char *s) _pure_;
284 
285 int parse_link_journal(const char *s, LinkJournal *ret_mode, bool *ret_try);
286 
287 void device_node_array_free(DeviceNode *node, size_t n);
288