-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpack.go
303 lines (274 loc) · 8.87 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
package main
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
)
type imageManifestItem struct {
Config string `json:"Config"`
RepoTags []string `json:"RepoTags"`
Layers []string `json:"Layers"`
}
type configurationData struct {
Hostname string `json:"Hostname"`
DomainName string `json:"Domainname"`
User string `json:"User"`
Environment []string `json:"Env"`
Command []string `json:"Cmd"`
WorkingDirectory string `json:"WorkingDir"`
Labels map[string]string `json:"Labels"`
}
type configurationTopLevel struct {
Architecture string `json:"architecture"`
Configuration configurationData `json:"config"`
Container string `json:"container"`
ContainerConfiguration configurationData `json:"container_config"`
Created time.Time `json:"created"`
DockerVersion string `json:"docker_version"`
}
func (imi imageManifestItem) Digest() string {
// The config file in an image contains the hash, but in some cases it
// has a .json extension, and in some cases it has a "hashtype:" prefix, so
// we need to strip those out.
basename := path.Base(imi.Config)
extension := path.Ext(basename)
body := strings.TrimSuffix(basename, extension)
parts := strings.Split(body, ":")
switch len(parts) {
case 1:
return body
case 2:
return parts[1]
default:
return imi.Config
}
}
func loadFileFromContainer(tarballPath string, filepath string, data interface{}) error {
file, err := os.Open(tarballPath)
if err != nil {
return fmt.Errorf("failed to open image for config: %w", err)
}
defer file.Close()
tarReader := tar.NewReader(file)
for {
header, err := tarReader.Next()
switch {
case err == io.EOF:
return err
case err != nil:
return fmt.Errorf("error reading next header: %w", err)
case header == nil:
continue
}
if header.Name != filepath {
continue
}
if header.Typeflag != tar.TypeReg {
return fmt.Errorf("expected manifest to be a regular file, but is %v", header.Typeflag)
}
return json.NewDecoder(tarReader).Decode(&data)
}
}
func unpackRootFS(tarballPath string, rootfsPath string) error {
imageManifest, err := loadImageManifest(tarballPath)
if err != nil {
if err != io.EOF {
return err
}
// if the error was io.EOF, we just didn't find the manifest, so
// assume we have a container image
return unpackContainer(tarballPath, rootfsPath)
}
// if we got here we have a docker image, so unpack that
return unpackImage(tarballPath, rootfsPath, imageManifest.Layers)
}
func getContainerConfiguration(tarballPath string) (configurationTopLevel, error) {
imageManifest, err := loadImageManifest(tarballPath)
if err != nil {
return configurationTopLevel{}, err
}
// after unpacking, try get the configuration for the image
var config configurationTopLevel
err = loadFileFromContainer(tarballPath, imageManifest.Config, &config)
return config, err
}
func loadImageManifest(tarballPath string) (imageManifestItem, error) {
var manifest []imageManifestItem
err := loadFileFromContainer(tarballPath, "manifest.json", &manifest)
if err != nil {
return imageManifestItem{}, err
}
if len(manifest) != 1 {
return imageManifestItem{}, fmt.Errorf("expected one item in manifest, got %d", len(manifest))
}
return manifest[0], nil
}
func expandTar(tarReader *tar.Reader, rootfsPath string, overlay bool) error {
for {
header, err := tarReader.Next()
switch {
case err == io.EOF:
return nil
case err != nil:
return fmt.Errorf("error reading next header: %w", err)
case header == nil:
continue
}
potentialTargetPath := filepath.Join(rootfsPath, header.Name)
cleanTargetPath := path.Clean(potentialTargetPath)
cleanRoot := path.Clean(rootfsPath)
if !strings.HasPrefix(cleanTargetPath, cleanRoot) {
return fmt.Errorf("suspicious path in tar that escapes target directory: %v", header.Name)
}
targetPath := cleanTargetPath
switch header.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(targetPath); err != nil {
if err := os.MkdirAll(targetPath, os.FileMode(header.Mode)); err != nil {
return fmt.Errorf("failed to create dir %v: %w", targetPath, err)
}
}
case tar.TypeReg:
basename := path.Base(header.Name)
if overlay && basename == ".wh..wh..opq" {
victimDirectory := path.Dir(targetPath)
if !strings.HasPrefix(victimDirectory, cleanRoot) {
return fmt.Errorf("attempt to remove children not in root: %v", header.Name)
}
victimFiles, err := os.ReadDir(victimDirectory)
if err != nil {
return fmt.Errorf("failed to index childern for overlay removal %v: %w", header.Name, err)
}
for _, victim := range victimFiles {
victimName := victim.Name()
if (victimName == ".") || (victimName == "..") {
continue
}
victimPath := path.Clean(path.Join(victimDirectory, victimName))
if !strings.HasPrefix(victimPath, cleanRoot) {
return fmt.Errorf("attempt to remove file not in root: %v", header.Name)
}
err = os.RemoveAll(victimPath)
if err != nil {
return fmt.Errorf("attempt to remove files failed %v: %w", victimName, err)
}
}
} else if overlay && strings.HasPrefix(basename, ".wh.") {
victimBasename := strings.TrimPrefix(basename, ".wh.")
victimDirectory := path.Dir(targetPath)
victimPath := path.Clean(path.Join(victimDirectory, victimBasename))
if !strings.HasPrefix(victimPath, cleanRoot) {
return fmt.Errorf("attempt to remove file not in root: %v", header.Name)
}
err = os.RemoveAll(victimPath)
if err != nil {
return fmt.Errorf("failed to remove overlay file %v: %w", header.Name, err)
}
} else {
f, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return fmt.Errorf("failed to create file %v: %w", targetPath, err)
}
if _, err := io.Copy(f, tarReader); err != nil {
return fmt.Errorf("failed to copy data for %v: %w", targetPath, err)
}
f.Close()
}
case tar.TypeSymlink:
if overlay {
err = os.Remove(targetPath)
if (err != nil) && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove file for symlink %v: %w", targetPath, err)
}
}
err = os.Symlink(header.Linkname, targetPath)
if err != nil {
return fmt.Errorf("failed to create symlink %v %v (overlay=%t): %w", header.Linkname, targetPath, overlay, err)
}
case tar.TypeLink:
if overlay {
err = os.Remove(targetPath)
if (err != nil) && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove file for symlink %v: %w", targetPath, err)
}
}
sourcePath := filepath.Join(rootfsPath, header.Linkname)
err = os.Link(sourcePath, targetPath)
if err != nil {
return fmt.Errorf("failed to create link %v %v: %w", header.Linkname, targetPath, err)
}
default:
fmt.Printf("Skipping %v of type %v\n", targetPath, header.Typeflag)
}
}
}
func unpackContainer(imgPath string, rootfsPath string) error {
file, err := os.Open(imgPath)
if err != nil {
return fmt.Errorf("failed to open image: %w", err)
}
defer file.Close()
tarReader := tar.NewReader(file)
return expandTar(tarReader, rootfsPath, false)
}
func unpackImage(imgPath string, rootfsPath string, layers []string) error {
file, err := os.Open(imgPath)
if err != nil {
return fmt.Errorf("failed to open image: %w", err)
}
defer file.Close()
// In my limited sample set it looks like the layers appear in order
// in the tarball, but that's not something I'd want to bet my life on always being
// the case, so this is thus rather pedantic
for _, layer := range layers {
offset, err := file.Seek(0, 0)
if err != nil {
return err
}
if offset != 0 {
return fmt.Errorf("Attemtped to move file reader to start, ended up at %d", offset)
}
tarReader := tar.NewReader(file)
for {
header, err := tarReader.Next()
switch {
case err == io.EOF:
return fmt.Errorf("failed to find layer %v in image", layer)
case err != nil:
return fmt.Errorf("error reading next header: %w", err)
case header == nil:
continue
}
if header.Name != layer {
continue
}
if header.Typeflag != tar.TypeReg {
return fmt.Errorf("expected layer %v to be a regular file, but is %v", layer, header.Typeflag)
}
extension := path.Ext(layer)
var layerTarReader *tar.Reader
if extension == ".gz" {
archiveReader, err := gzip.NewReader(tarReader)
if err != nil {
return fmt.Errorf("failed to read achive of layer %v: %w", layer, err)
}
layerTarReader = tar.NewReader(archiveReader)
} else {
layerTarReader = tar.NewReader(tarReader)
}
err = expandTar(layerTarReader, rootfsPath, true)
if err != nil {
return fmt.Errorf("failed to expand layer %v: %w", layer, err)
}
break
}
}
return nil
}