forked from bottlerocket-os/bottlerocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainerd-1.5-only-relabel-cri-managed-host-mounts.patch
231 lines (222 loc) · 8.67 KB
/
containerd-1.5-only-relabel-cri-managed-host-mounts.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
From a41213fedbbf6436e8c2b647e72b3c2fc33f53b7 Mon Sep 17 00:00:00 2001
From: Michael Crosby <michael@thepasture.io>
Date: Tue, 9 Nov 2021 19:45:40 +0000
Subject: [PATCH] only relabel cri managed host mounts
Co-authored-by: Samuel Karp <skarp@amazon.com>
Signed-off-by: Michael Crosby <michael@thepasture.io>
Signed-off-by: Samuel Karp <skarp@amazon.com>
(cherry picked from commit 9b0303913fcfa297f984d954c718541cf474107b)
Signed-off-by: Samuel Karp <skarp@amazon.com>
---
pkg/cri/opts/spec_linux.go | 24 ------
pkg/cri/server/container_create_linux.go | 23 +++---
pkg/cri/server/container_create_linux_test.go | 77 +++++++++++--------
3 files changed, 57 insertions(+), 67 deletions(-)
diff --git a/pkg/cri/opts/spec_linux.go b/pkg/cri/opts/spec_linux.go
index c5ec3dfdd..84c16b6f8 100644
--- a/pkg/cri/opts/spec_linux.go
+++ b/pkg/cri/opts/spec_linux.go
@@ -225,30 +225,6 @@ func WithMounts(osi osinterface.OS, config *runtime.ContainerConfig, extra []*ru
}
}
-const (
- etcHosts = "/etc/hosts"
- etcHostname = "/etc/hostname"
- resolvConfPath = "/etc/resolv.conf"
-)
-
-// WithRelabeledContainerMounts relabels the default container mounts for files in /etc
-func WithRelabeledContainerMounts(mountLabel string) oci.SpecOpts {
- return func(ctx context.Context, client oci.Client, _ *containers.Container, s *runtimespec.Spec) (err error) {
- if mountLabel == "" {
- return nil
- }
- for _, m := range s.Mounts {
- switch m.Destination {
- case etcHosts, etcHostname, resolvConfPath:
- if err := label.Relabel(m.Source, mountLabel, false); err != nil {
- return err
- }
- }
- }
- return nil
- }
-}
-
// Ensure mount point on which path is mounted, is shared.
func ensureShared(path string, lookupMount func(string) (mount.Info, error)) error {
mountInfo, err := lookupMount(path)
diff --git a/pkg/cri/server/container_create_linux.go b/pkg/cri/server/container_create_linux.go
index 1ad2947b8..4c857dff7 100644
--- a/pkg/cri/server/container_create_linux.go
+++ b/pkg/cri/server/container_create_linux.go
@@ -68,18 +68,20 @@ func (c *criService) containerMounts(sandboxID string, config *runtime.Container
hostpath := c.getSandboxHostname(sandboxID)
if _, err := c.os.Stat(hostpath); err == nil {
mounts = append(mounts, &runtime.Mount{
- ContainerPath: etcHostname,
- HostPath: hostpath,
- Readonly: securityContext.GetReadonlyRootfs(),
+ ContainerPath: etcHostname,
+ HostPath: hostpath,
+ Readonly: securityContext.GetReadonlyRootfs(),
+ SelinuxRelabel: true,
})
}
}
if !isInCRIMounts(etcHosts, config.GetMounts()) {
mounts = append(mounts, &runtime.Mount{
- ContainerPath: etcHosts,
- HostPath: c.getSandboxHosts(sandboxID),
- Readonly: securityContext.GetReadonlyRootfs(),
+ ContainerPath: etcHosts,
+ HostPath: c.getSandboxHosts(sandboxID),
+ Readonly: securityContext.GetReadonlyRootfs(),
+ SelinuxRelabel: true,
})
}
@@ -87,9 +89,10 @@ func (c *criService) containerMounts(sandboxID string, config *runtime.Container
// TODO: Need to figure out whether we should always mount it as read-only
if !isInCRIMounts(resolvConfPath, config.GetMounts()) {
mounts = append(mounts, &runtime.Mount{
- ContainerPath: resolvConfPath,
- HostPath: c.getResolvPath(sandboxID),
- Readonly: securityContext.GetReadonlyRootfs(),
+ ContainerPath: resolvConfPath,
+ HostPath: c.getResolvPath(sandboxID),
+ Readonly: securityContext.GetReadonlyRootfs(),
+ SelinuxRelabel: true,
})
}
@@ -192,7 +195,7 @@ func (c *criService) containerSpec(
}
}()
- specOpts = append(specOpts, customopts.WithMounts(c.os, config, extraMounts, mountLabel), customopts.WithRelabeledContainerMounts(mountLabel))
+ specOpts = append(specOpts, customopts.WithMounts(c.os, config, extraMounts, mountLabel))
if !c.config.DisableProcMount {
// Change the default masked/readonly paths to empty slices
diff --git a/pkg/cri/server/container_create_linux_test.go b/pkg/cri/server/container_create_linux_test.go
index 18d368344..881c8ecf7 100644
--- a/pkg/cri/server/container_create_linux_test.go
+++ b/pkg/cri/server/container_create_linux_test.go
@@ -450,19 +450,22 @@ func TestContainerMounts(t *testing.T) {
},
expectedMounts: []*runtime.Mount{
{
- ContainerPath: "/etc/hostname",
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"),
- Readonly: true,
+ ContainerPath: "/etc/hostname",
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"),
+ Readonly: true,
+ SelinuxRelabel: true,
},
{
- ContainerPath: "/etc/hosts",
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
- Readonly: true,
+ ContainerPath: "/etc/hosts",
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
+ Readonly: true,
+ SelinuxRelabel: true,
},
{
- ContainerPath: resolvConfPath,
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
- Readonly: true,
+ ContainerPath: resolvConfPath,
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
+ Readonly: true,
+ SelinuxRelabel: true,
},
{
ContainerPath: "/dev/shm",
@@ -476,19 +479,22 @@ func TestContainerMounts(t *testing.T) {
securityContext: &runtime.LinuxContainerSecurityContext{},
expectedMounts: []*runtime.Mount{
{
- ContainerPath: "/etc/hostname",
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"),
- Readonly: false,
+ ContainerPath: "/etc/hostname",
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
- ContainerPath: "/etc/hosts",
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
- Readonly: false,
+ ContainerPath: "/etc/hosts",
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
- ContainerPath: resolvConfPath,
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
- Readonly: false,
+ ContainerPath: resolvConfPath,
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
ContainerPath: "/dev/shm",
@@ -504,19 +510,22 @@ func TestContainerMounts(t *testing.T) {
},
expectedMounts: []*runtime.Mount{
{
- ContainerPath: "/etc/hostname",
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"),
- Readonly: false,
+ ContainerPath: "/etc/hostname",
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
- ContainerPath: "/etc/hosts",
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
- Readonly: false,
+ ContainerPath: "/etc/hosts",
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
- ContainerPath: resolvConfPath,
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
- Readonly: false,
+ ContainerPath: resolvConfPath,
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
ContainerPath: "/dev/shm",
@@ -555,14 +564,16 @@ func TestContainerMounts(t *testing.T) {
securityContext: &runtime.LinuxContainerSecurityContext{},
expectedMounts: []*runtime.Mount{
{
- ContainerPath: "/etc/hosts",
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
- Readonly: false,
+ ContainerPath: "/etc/hosts",
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
- ContainerPath: resolvConfPath,
- HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
- Readonly: false,
+ ContainerPath: resolvConfPath,
+ HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
+ Readonly: false,
+ SelinuxRelabel: true,
},
{
ContainerPath: "/dev/shm",
--
2.34.1