forked from bottlerocket-os/bottlerocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0002-v1-runtime-reduce-permissions-for-bundle-dir.patch
289 lines (282 loc) · 7.39 KB
/
0002-v1-runtime-reduce-permissions-for-bundle-dir.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
From 78204966b8ef86da7f62dbb4be20bd5d282fd2cc Mon Sep 17 00:00:00 2001
From: Samuel Karp <skarp@amazon.com>
Date: Tue, 21 Sep 2021 13:46:40 -0700
Subject: [PATCH 2/3] v1 runtime: reduce permissions for bundle dir
Bundle directory permissions should be 0700 by default. On Linux with
user namespaces enabled, the remapped root also needs access to the
bundle directory. In this case, the bundle directory is modified to
0710 and group ownership is changed to the remapped root group.
Port of the same change for the v2 runtime
Signed-off-by: Samuel Karp <skarp@amazon.com>
---
runtime/v1/linux/bundle.go | 56 ++++++++++-
runtime/v1/linux/bundle_test.go | 166 ++++++++++++++++++++++++++++++++
2 files changed, 221 insertions(+), 1 deletion(-)
create mode 100644 runtime/v1/linux/bundle_test.go
diff --git a/runtime/v1/linux/bundle.go b/runtime/v1/linux/bundle.go
index 9d0a6c447..48d81e8e0 100644
--- a/runtime/v1/linux/bundle.go
+++ b/runtime/v1/linux/bundle.go
@@ -21,6 +21,7 @@ package linux
import (
"context"
"crypto/sha256"
+ "encoding/json"
"fmt"
"io/ioutil"
"os"
@@ -30,6 +31,7 @@ import (
"github.com/containerd/containerd/runtime/linux/runctypes"
"github.com/containerd/containerd/runtime/v1/shim"
"github.com/containerd/containerd/runtime/v1/shim/client"
+ "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
@@ -48,7 +50,7 @@ func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
return nil, err
}
path = filepath.Join(path, id)
- if err := os.Mkdir(path, 0711); err != nil {
+ if err := os.Mkdir(path, 0700); err != nil {
return nil, err
}
defer func() {
@@ -56,6 +58,9 @@ func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
os.RemoveAll(path)
}
}()
+ if err := prepareBundleDirectoryPermissions(path, spec); err != nil {
+ return nil, err
+ }
workDir = filepath.Join(workDir, id)
if err := os.MkdirAll(workDir, 0711); err != nil {
return nil, err
@@ -77,6 +82,55 @@ func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
}, err
}
+// prepareBundleDirectoryPermissions prepares the permissions of the bundle
+// directory. When user namespaces are enabled, the permissions are modified
+// to allow the remapped root GID to access the bundle.
+func prepareBundleDirectoryPermissions(path string, spec []byte) error {
+ gid, err := remappedGID(spec)
+ if err != nil {
+ return err
+ }
+ if gid == 0 {
+ return nil
+ }
+ if err := os.Chown(path, -1, int(gid)); err != nil {
+ return err
+ }
+ return os.Chmod(path, 0710)
+}
+
+// ociSpecUserNS is a subset of specs.Spec used to reduce garbage during
+// unmarshal.
+type ociSpecUserNS struct {
+ Linux *linuxSpecUserNS
+}
+
+// linuxSpecUserNS is a subset of specs.Linux used to reduce garbage during
+// unmarshal.
+type linuxSpecUserNS struct {
+ GIDMappings []specs.LinuxIDMapping
+}
+
+// remappedGID reads the remapped GID 0 from the OCI spec, if it exists. If
+// there is no remapping, remappedGID returns 0. If the spec cannot be parsed,
+// remappedGID returns an error.
+func remappedGID(spec []byte) (uint32, error) {
+ var ociSpec ociSpecUserNS
+ err := json.Unmarshal(spec, &ociSpec)
+ if err != nil {
+ return 0, err
+ }
+ if ociSpec.Linux == nil || len(ociSpec.Linux.GIDMappings) == 0 {
+ return 0, nil
+ }
+ for _, mapping := range ociSpec.Linux.GIDMappings {
+ if mapping.ContainerID == 0 {
+ return mapping.HostID, nil
+ }
+ }
+ return 0, nil
+}
+
type bundle struct {
id string
path string
diff --git a/runtime/v1/linux/bundle_test.go b/runtime/v1/linux/bundle_test.go
new file mode 100644
index 000000000..adf39b4ec
--- /dev/null
+++ b/runtime/v1/linux/bundle_test.go
@@ -0,0 +1,166 @@
+//go:build linux
+// +build linux
+
+/*
+ Copyright The containerd Authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+package linux
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strconv"
+ "syscall"
+ "testing"
+
+ "github.com/containerd/containerd/oci"
+ "github.com/containerd/continuity/testutil"
+ "github.com/opencontainers/runtime-spec/specs-go"
+)
+
+func TestNewBundle(t *testing.T) {
+ testutil.RequiresRoot(t)
+ tests := []struct {
+ userns bool
+ }{{
+ userns: false,
+ }, {
+ userns: true,
+ }}
+ const usernsGID = 4200
+
+ for i, tc := range tests {
+ t.Run(strconv.Itoa(i), func(t *testing.T) {
+ dir, err := ioutil.TempDir("", "test-new-bundle")
+ if err != nil {
+ t.Fatal("failed to create test directory", err)
+ }
+ defer os.RemoveAll(dir)
+ work := filepath.Join(dir, "work")
+ state := filepath.Join(dir, "state")
+ id := fmt.Sprintf("new-bundle-%d", i)
+ spec := oci.Spec{}
+ if tc.userns {
+ spec.Linux = &specs.Linux{
+ GIDMappings: []specs.LinuxIDMapping{{ContainerID: 0, HostID: usernsGID}},
+ }
+ }
+ specBytes, err := json.Marshal(&spec)
+ if err != nil {
+ t.Fatal("failed to marshal spec", err)
+ }
+
+ b, err := newBundle(id, work, state, specBytes)
+ if err != nil {
+ t.Fatal("newBundle should succeed", err)
+ }
+ if b == nil {
+ t.Fatal("bundle should not be nil")
+ }
+
+ fi, err := os.Stat(b.path)
+ if err != nil {
+ t.Error("should be able to stat bundle path", err)
+ }
+ if tc.userns {
+ if fi.Mode() != os.ModeDir|0710 {
+ t.Error("bundle path should be a directory with perm 0710")
+ }
+ } else {
+ if fi.Mode() != os.ModeDir|0700 {
+ t.Error("bundle path should be a directory with perm 0700")
+ }
+ }
+ stat, ok := fi.Sys().(*syscall.Stat_t)
+ if !ok {
+ t.Fatal("should assert to *syscall.Stat_t")
+ }
+ expectedGID := uint32(0)
+ if tc.userns {
+ expectedGID = usernsGID
+ }
+ if stat.Gid != expectedGID {
+ t.Error("gid should match", expectedGID, stat.Gid)
+ }
+ })
+ }
+}
+
+func TestRemappedGID(t *testing.T) {
+ tests := []struct {
+ spec oci.Spec
+ gid uint32
+ }{{
+ // empty spec
+ spec: oci.Spec{},
+ gid: 0,
+ }, {
+ // empty Linux section
+ spec: oci.Spec{
+ Linux: &specs.Linux{},
+ },
+ gid: 0,
+ }, {
+ // empty ID mappings
+ spec: oci.Spec{
+ Linux: &specs.Linux{
+ GIDMappings: make([]specs.LinuxIDMapping, 0),
+ },
+ },
+ gid: 0,
+ }, {
+ // valid ID mapping
+ spec: oci.Spec{
+ Linux: &specs.Linux{
+ GIDMappings: []specs.LinuxIDMapping{{
+ ContainerID: 0,
+ HostID: 1000,
+ }},
+ },
+ },
+ gid: 1000,
+ }, {
+ // missing ID mapping
+ spec: oci.Spec{
+ Linux: &specs.Linux{
+ GIDMappings: []specs.LinuxIDMapping{{
+ ContainerID: 100,
+ HostID: 1000,
+ }},
+ },
+ },
+ gid: 0,
+ }}
+
+ for i, tc := range tests {
+ t.Run(strconv.Itoa(i), func(t *testing.T) {
+ s, err := json.Marshal(tc.spec)
+ if err != nil {
+ t.Fatal("failed to marshal spec", err)
+ }
+ gid, err := remappedGID(s)
+ if err != nil {
+ t.Error("should unmarshal successfully", err)
+ }
+ if gid != tc.gid {
+ t.Error("expected GID to match", tc.gid, gid)
+ }
+ })
+ }
+}
--
2.33.0