This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
forked from opencontainers/umoci
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunpack.go
347 lines (312 loc) · 12.2 KB
/
unpack.go
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016-2019 SUSE LLC.
*
* 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 layer
import (
"archive/tar"
// Import is necessary for go-digest.
_ "crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/apex/log"
gzip "github.com/klauspost/pgzip"
"github.com/openSUSE/umoci/oci/cas"
"github.com/openSUSE/umoci/oci/casext"
iconv "github.com/openSUSE/umoci/oci/config/convert"
"github.com/openSUSE/umoci/pkg/fseval"
"github.com/openSUSE/umoci/pkg/idtools"
"github.com/openSUSE/umoci/pkg/system"
"github.com/opencontainers/go-digest"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// UnpackLayer unpacks the tar stream representing an OCI layer at the given
// root. It ensures that the state of the root is as close as possible to the
// state used to create the layer. If an error is returned, the state of root
// is undefined (unpacking is not guaranteed to be atomic).
func UnpackLayer(root string, layer io.Reader, opt *MapOptions) error {
var mapOptions MapOptions
if opt != nil {
mapOptions = *opt
}
te := NewTarExtractor(mapOptions)
tr := tar.NewReader(layer)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return errors.Wrap(err, "read next entry")
}
if err := te.UnpackEntry(root, hdr, tr); err != nil {
return errors.Wrapf(err, "unpack entry: %s", hdr.Name)
}
}
return nil
}
// RootfsName is the name of the rootfs directory inside the bundle path when
// generated.
const RootfsName = "rootfs"
// isLayerType returns if the given MediaType is the media type of an image
// layer blob. This includes both distributable and non-distributable images.
func isLayerType(mediaType string) bool {
return mediaType == ispec.MediaTypeImageLayer || mediaType == ispec.MediaTypeImageLayerNonDistributable ||
mediaType == ispec.MediaTypeImageLayerGzip || mediaType == ispec.MediaTypeImageLayerNonDistributableGzip
}
func needsGunzip(mediaType string) bool {
return mediaType == ispec.MediaTypeImageLayerGzip || mediaType == ispec.MediaTypeImageLayerNonDistributableGzip
}
// UnpackManifest extracts all of the layers in the given manifest, as well as
// generating a runtime bundle and configuration. The rootfs is extracted to
// <bundle>/<layer.RootfsName>.
//
// FIXME: This interface is ugly.
func UnpackManifest(ctx context.Context, engine cas.Engine, bundle string, manifest ispec.Manifest, opt *MapOptions) (err error) {
// Create the bundle directory. We only error out if config.json or rootfs/
// already exists, because we cannot be sure that the user intended us to
// extract over an existing bundle.
if err := os.MkdirAll(bundle, 0755); err != nil {
return errors.Wrap(err, "mkdir bundle")
}
// We change the mode of the bundle directory to 0700. A user can easily
// change this after-the-fact, but we do this explicitly to avoid cases
// where an unprivileged user could recurse into an otherwise unsafe image
// (giving them potential root access through setuid binaries for example).
if err := os.Chmod(bundle, 0700); err != nil {
return errors.Wrap(err, "chmod bundle 0700")
}
configPath := filepath.Join(bundle, "config.json")
rootfsPath := filepath.Join(bundle, RootfsName)
if _, err := os.Lstat(configPath); !os.IsNotExist(err) {
if err == nil {
err = fmt.Errorf("config.json already exists")
}
return errors.Wrap(err, "bundle path empty")
}
defer func() {
if err != nil {
fsEval := fseval.DefaultFsEval
if opt != nil && opt.Rootless {
fsEval = fseval.RootlessFsEval
}
// It's too late to care about errors.
// #nosec G104
_ = fsEval.RemoveAll(rootfsPath)
}
}()
if _, err := os.Lstat(rootfsPath); !os.IsNotExist(err) {
if err == nil {
err = fmt.Errorf("%s already exists", rootfsPath)
}
return err
}
log.Infof("unpack rootfs: %s", rootfsPath)
if err := UnpackRootfs(ctx, engine, rootfsPath, manifest, opt); err != nil {
return errors.Wrap(err, "unpack rootfs")
}
// Generate a runtime configuration file from ispec.Image.
configFile, err := os.Create(configPath)
if err != nil {
return errors.Wrap(err, "open config.json")
}
defer configFile.Close()
if err := UnpackRuntimeJSON(ctx, engine, configFile, rootfsPath, manifest, opt); err != nil {
return errors.Wrap(err, "unpack config.json")
}
return nil
}
// UnpackRootfs extracts all of the layers in the given manifest.
// Some verification is done during image extraction.
func UnpackRootfs(ctx context.Context, engine cas.Engine, rootfsPath string, manifest ispec.Manifest, opt *MapOptions) (err error) {
engineExt := casext.NewEngine(engine)
if err := os.Mkdir(rootfsPath, 0755); err != nil && !os.IsExist(err) {
return errors.Wrap(err, "mkdir rootfs")
}
// In order to avoid having a broken rootfs in the case of an error, we
// remove the rootfs. In the case of rootless this is particularly
// important (`rm -rf` won't work on most distro rootfs's).
defer func() {
if err != nil {
fsEval := fseval.DefaultFsEval
if opt != nil && opt.Rootless {
fsEval = fseval.RootlessFsEval
}
// It's too late to care about errors.
// #nosec G104
_ = fsEval.RemoveAll(rootfsPath)
}
}()
// Make sure that the owner is correct.
rootUID, err := idtools.ToHost(0, opt.UIDMappings)
if err != nil {
return errors.Wrap(err, "ensure rootuid has mapping")
}
rootGID, err := idtools.ToHost(0, opt.GIDMappings)
if err != nil {
return errors.Wrap(err, "ensure rootgid has mapping")
}
if err := os.Lchown(rootfsPath, rootUID, rootGID); err != nil {
return errors.Wrap(err, "chown rootfs")
}
// Currently, many different images in the wild don't specify what the
// atime/mtime of the root directory is. This is a huge pain because it
// means that we can't ensure consistent unpacking. In order to get around
// this, we first set the mtime of the root directory to the Unix epoch
// (which is as good of an arbitrary choice as any).
epoch := time.Unix(0, 0)
if err := system.Lutimes(rootfsPath, epoch, epoch); err != nil {
return errors.Wrap(err, "set initial root time")
}
// In order to verify the DiffIDs as we extract layers, we have to get the
// .Config blob first. But we can't extract it (generate the runtime
// config) until after we have the full rootfs generated.
configBlob, err := engineExt.FromDescriptor(ctx, manifest.Config)
if err != nil {
return errors.Wrap(err, "get config blob")
}
defer configBlob.Close()
if configBlob.Descriptor.MediaType != ispec.MediaTypeImageConfig {
return errors.Errorf("unpack rootfs: config blob is not correct mediatype %s: %s", ispec.MediaTypeImageConfig, configBlob.Descriptor.MediaType)
}
config, ok := configBlob.Data.(ispec.Image)
if !ok {
// Should _never_ be reached.
return errors.Errorf("[internal error] unknown config blob type: %s", configBlob.Descriptor.MediaType)
}
// We can't understand non-layer images.
if config.RootFS.Type != "layers" {
return errors.Errorf("unpack rootfs: config: unsupported rootfs.type: %s", config.RootFS.Type)
}
// Layer extraction.
for idx, layerDescriptor := range manifest.Layers {
layerDiffID := config.RootFS.DiffIDs[idx]
log.Infof("unpack layer: %s", layerDescriptor.Digest)
layerBlob, err := engineExt.FromDescriptor(ctx, layerDescriptor)
if err != nil {
return errors.Wrap(err, "get layer blob")
}
defer layerBlob.Close()
if !isLayerType(layerBlob.Descriptor.MediaType) {
return errors.Errorf("unpack rootfs: layer %s: blob is not correct mediatype: %s", layerBlob.Descriptor.Digest, layerBlob.Descriptor.MediaType)
}
layerData, ok := layerBlob.Data.(io.ReadCloser)
if !ok {
// Should _never_ be reached.
return errors.Errorf("[internal error] layerBlob was not an io.ReadCloser")
}
layerRaw := layerData
if needsGunzip(layerBlob.Descriptor.MediaType) {
// We have to extract a gzip'd version of the above layer. Also note
// that we have to check the DiffID we're extracting (which is the
// sha256 sum of the *uncompressed* layer).
layerRaw, err = gzip.NewReader(layerData)
if err != nil {
return errors.Wrap(err, "create gzip reader")
}
}
layerDigester := digest.SHA256.Digester()
layer := io.TeeReader(layerRaw, layerDigester.Hash())
if err := UnpackLayer(rootfsPath, layer, opt); err != nil {
return errors.Wrap(err, "unpack layer")
}
// Different tar implementations can have different levels of redundant
// padding and other similar weird behaviours. While on paper they are
// all entirely valid archives, Go's tar.Reader implementation doesn't
// guarantee that the entire stream will be consumed (which can result
// in the later diff_id check failing because the digester didn't get
// the whole uncompressed stream). Just blindly consume anything left
// in the layer.
if _, err = io.Copy(ioutil.Discard, layer); err != nil {
return errors.Wrap(err, "discard trailing archive bits")
}
if err := layerData.Close(); err != nil {
return errors.Wrap(err, "close layer data")
}
layerDigest := layerDigester.Digest()
if layerDigest != layerDiffID {
return errors.Errorf("unpack manifest: layer %s: diffid mismatch: got %s expected %s", layerDescriptor.Digest, layerDigest, layerDiffID)
}
}
return nil
}
// UnpackRuntimeJSON converts a given manifest's configuration to a runtime
// configuration and writes it to the given writer. If rootfs is specified, it
// is sourced during the configuration generation (for conversion of
// Config.User and other similar jobs -- which will error out if the user could
// not be parsed). If rootfs is not specified (is an empty string) then all
// conversions that require sourcing the rootfs will be set to their default
// values.
//
// XXX: I don't like this API. It has way too many arguments.
func UnpackRuntimeJSON(ctx context.Context, engine cas.Engine, configFile io.Writer, rootfs string, manifest ispec.Manifest, opt *MapOptions) error {
engineExt := casext.NewEngine(engine)
var mapOptions MapOptions
if opt != nil {
mapOptions = *opt
}
// In order to verify the DiffIDs as we extract layers, we have to get the
// .Config blob first. But we can't extract it (generate the runtime
// config) until after we have the full rootfs generated.
configBlob, err := engineExt.FromDescriptor(ctx, manifest.Config)
if err != nil {
return errors.Wrap(err, "get config blob")
}
defer configBlob.Close()
if configBlob.Descriptor.MediaType != ispec.MediaTypeImageConfig {
return errors.Errorf("unpack manifest: config blob is not correct mediatype %s: %s", ispec.MediaTypeImageConfig, configBlob.Descriptor.MediaType)
}
config, ok := configBlob.Data.(ispec.Image)
if !ok {
// Should _never_ be reached.
return errors.Errorf("[internal error] unknown config blob type: %s", configBlob.Descriptor.MediaType)
}
spec, err := iconv.ToRuntimeSpec(rootfs, config)
if err != nil {
return errors.Wrap(err, "generate config.json")
}
// Add UIDMapping / GIDMapping options.
if len(mapOptions.UIDMappings) > 0 || len(mapOptions.GIDMappings) > 0 {
var namespaces []rspec.LinuxNamespace
for _, ns := range spec.Linux.Namespaces {
if ns.Type == "user" {
continue
}
namespaces = append(namespaces, ns)
}
spec.Linux.Namespaces = append(namespaces, rspec.LinuxNamespace{
Type: "user",
})
}
spec.Linux.UIDMappings = mapOptions.UIDMappings
spec.Linux.GIDMappings = mapOptions.GIDMappings
if mapOptions.Rootless {
if err := iconv.ToRootless(&spec); err != nil {
return errors.Wrap(err, "convert spec to rootless")
}
}
// Save the config.json.
enc := json.NewEncoder(configFile)
enc.SetIndent("", "\t")
return errors.Wrap(enc.Encode(spec), "write config.json")
}