]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/namespace.h
pid1: create ro private tmp dirs when /tmp or /var/tmp is read-only
[thirdparty/systemd.git] / src / core / namespace.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
15ae422b
LP
3
4/***
96b2fb93 5 Copyright © 2016 Djalal Harouni
15ae422b
LP
6***/
7
bb0ff3fb 8typedef struct NamespaceInfo NamespaceInfo;
d2d6c096 9typedef struct BindMount BindMount;
2abd4e38 10typedef struct TemporaryFileSystem TemporaryFileSystem;
c575770b 11
15ae422b
LP
12#include <stdbool.h>
13
915e6d16 14#include "dissect-image.h"
56a13a49 15#include "fs-util.h"
417116f2 16#include "macro.h"
56a13a49 17#include "string-util.h"
417116f2 18
1b8689f9
LP
19typedef enum ProtectHome {
20 PROTECT_HOME_NO,
21 PROTECT_HOME_YES,
22 PROTECT_HOME_READ_ONLY,
e4da7d8c 23 PROTECT_HOME_TMPFS,
1b8689f9
LP
24 _PROTECT_HOME_MAX,
25 _PROTECT_HOME_INVALID = -1
26} ProtectHome;
27
6e2d7c4f
MS
28typedef enum NamespaceType {
29 NAMESPACE_MOUNT,
30 NAMESPACE_CGROUP,
31 NAMESPACE_UTS,
32 NAMESPACE_IPC,
33 NAMESPACE_USER,
34 NAMESPACE_PID,
35 NAMESPACE_NET,
36 _NAMESPACE_TYPE_MAX,
37 _NAMESPACE_TYPE_INVALID = -1,
38} NamespaceType;
39
1b8689f9
LP
40typedef enum ProtectSystem {
41 PROTECT_SYSTEM_NO,
42 PROTECT_SYSTEM_YES,
43 PROTECT_SYSTEM_FULL,
3f815163 44 PROTECT_SYSTEM_STRICT,
1b8689f9
LP
45 _PROTECT_SYSTEM_MAX,
46 _PROTECT_SYSTEM_INVALID = -1
47} ProtectSystem;
417116f2 48
bb0ff3fb 49struct NamespaceInfo {
af964954 50 bool ignore_protect_paths:1;
c575770b 51 bool private_dev:1;
228af36f 52 bool private_mounts:1;
c575770b
DH
53 bool protect_control_groups:1;
54 bool protect_kernel_tunables:1;
55 bool protect_kernel_modules:1;
94a7b275 56 bool protect_kernel_logs:1;
5d997827 57 bool mount_apivfs:1;
aecd5ac6 58 bool protect_hostname:1;
c575770b
DH
59};
60
d2d6c096
LP
61struct BindMount {
62 char *source;
63 char *destination;
64 bool read_only:1;
9ce4e4b0 65 bool nosuid:1;
d2d6c096
LP
66 bool recursive:1;
67 bool ignore_enoent:1;
68};
69
2abd4e38
YW
70struct TemporaryFileSystem {
71 char *path;
72 char *options;
73};
74
d2d6c096
LP
75int setup_namespace(
76 const char *root_directory,
915e6d16 77 const char *root_image,
bb0ff3fb 78 const NamespaceInfo *ns_info,
d2d6c096
LP
79 char **read_write_paths,
80 char **read_only_paths,
81 char **inaccessible_paths,
6c47cd7d 82 char **empty_directories,
d2d6c096 83 const BindMount *bind_mounts,
da6053d0 84 size_t n_bind_mounts,
2abd4e38 85 const TemporaryFileSystem *temporary_filesystems,
da6053d0 86 size_t n_temporary_filesystems,
d2d6c096
LP
87 const char *tmp_dir,
88 const char *var_tmp_dir,
91dd5f7c 89 const char *log_namespace,
d2d6c096
LP
90 ProtectHome protect_home,
91 ProtectSystem protect_system,
915e6d16 92 unsigned long mount_flags,
0389f4fa
LB
93 const void *root_hash,
94 size_t root_hash_size,
95 const char *root_hash_path,
d4d55b0d
LB
96 const void *root_hash_sig,
97 size_t root_hash_sig_size,
98 const char *root_hash_sig_path,
0389f4fa 99 const char *root_verity,
7cc5ef5f
ZJS
100 DissectImageFlags dissected_image_flags,
101 char **error_path);
d2d6c096 102
56a13a49
ZJS
103#define RUN_SYSTEMD_EMPTY "/run/systemd/empty"
104
105static inline void namespace_cleanup_tmpdir(char *p) {
106 PROTECT_ERRNO;
107 if (!streq_ptr(p, RUN_SYSTEMD_EMPTY))
108 (void) rmdir(p);
109 free(p);
110}
111DEFINE_TRIVIAL_CLEANUP_FUNC(char*, namespace_cleanup_tmpdir);
112
d2d6c096
LP
113int setup_tmp_dirs(
114 const char *id,
115 char **tmp_dir,
116 char **var_tmp_dir);
613b411c 117
2caa38e9
LP
118int setup_netns(const int netns_storage_socket[static 2]);
119int open_netns_path(const int netns_storage_socket[static 2], const char *path);
417116f2 120
1b8689f9
LP
121const char* protect_home_to_string(ProtectHome p) _const_;
122ProtectHome protect_home_from_string(const char *s) _pure_;
123
124const char* protect_system_to_string(ProtectSystem p) _const_;
125ProtectSystem protect_system_from_string(const char *s) _pure_;
d2d6c096 126
da6053d0
LP
127void bind_mount_free_many(BindMount *b, size_t n);
128int bind_mount_add(BindMount **b, size_t *n, const BindMount *item);
6e2d7c4f 129
da6053d0
LP
130void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n);
131int temporary_filesystem_add(TemporaryFileSystem **t, size_t *n,
2abd4e38
YW
132 const char *path, const char *options);
133
6e2d7c4f
MS
134const char* namespace_type_to_string(NamespaceType t) _const_;
135NamespaceType namespace_type_from_string(const char *s) _pure_;
136
137bool ns_type_supported(NamespaceType type);