| 1 |
//go:build !windows |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright The ORAS Authors. |
| 5 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
you may not use this file except in compliance with the License. |
| 7 |
You may obtain a copy of the License at |
| 8 |
|
| 9 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
Unless required by applicable law or agreed to in writing, software |
| 12 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
See the License for the specific language governing permissions and |
| 15 |
limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
package file |
| 19 |
|
| 20 |
import ( |
| 21 |
"archive/tar" |
| 22 |
"bytes" |
| 23 |
"compress/gzip" |
| 24 |
"context" |
| 25 |
"os" |
| 26 |
"path/filepath" |
| 27 |
"strings" |
| 28 |
"testing" |
| 29 |
|
| 30 |
"github.com/opencontainers/go-digest" |
| 31 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 32 |
"oras.land/oras-go/v2" |
| 33 |
) |
| 34 |
|
| 35 |
// Related issue: https://github.com/oras-project/oras-go/issues/402 |
| 36 |
func TestStore_Dir_ExtractSymlinkRel(t *testing.T) { |
| 37 |
// prepare test content |
| 38 |
tempDir := t.TempDir() |
| 39 |
dirName := "testdir" |
| 40 |
dirPath := filepath.Join(tempDir, dirName) |
| 41 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 42 |
t.Fatal("error calling Mkdir(), error =", err) |
| 43 |
} |
| 44 |
|
| 45 |
content := []byte("hello world") |
| 46 |
fileName := "test.txt" |
| 47 |
filePath := filepath.Join(dirPath, fileName) |
| 48 |
if err := os.WriteFile(filePath, content, 0444); err != nil { |
| 49 |
t.Fatal("error calling WriteFile(), error =", err) |
| 50 |
} |
| 51 |
// create symlink to a relative path |
| 52 |
symlinkName := "test_symlink" |
| 53 |
symlinkPath := filepath.Join(dirPath, symlinkName) |
| 54 |
if err := os.Symlink(fileName, symlinkPath); err != nil { |
| 55 |
t.Fatal("error calling Symlink(), error =", err) |
| 56 |
} |
| 57 |
|
| 58 |
src, err := New(tempDir) |
| 59 |
if err != nil { |
| 60 |
t.Fatal("Store.New() error =", err) |
| 61 |
} |
| 62 |
defer src.Close() |
| 63 |
ctx := context.Background() |
| 64 |
|
| 65 |
// add dir |
| 66 |
desc, err := src.Add(ctx, dirName, "", dirPath) |
| 67 |
if err != nil { |
| 68 |
t.Fatal("Store.Add() error =", err) |
| 69 |
} |
| 70 |
// pack a manifest |
| 71 |
opts := oras.PackManifestOptions{ |
| 72 |
Layers: []ocispec.Descriptor{desc}, |
| 73 |
} |
| 74 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "test/dir", opts) |
| 75 |
if err != nil { |
| 76 |
t.Fatal("oras.PackManifest() error =", err) |
| 77 |
} |
| 78 |
|
| 79 |
// copy to another file store created from an absolute root, to trigger extracting directory |
| 80 |
tempDir = t.TempDir() |
| 81 |
dstAbs, err := New(tempDir) |
| 82 |
if err != nil { |
| 83 |
t.Fatal("Store.New() error =", err) |
| 84 |
} |
| 85 |
defer dstAbs.Close() |
| 86 |
if err := oras.CopyGraph(ctx, src, dstAbs, manifestDesc, oras.DefaultCopyGraphOptions); err != nil { |
| 87 |
t.Fatal("oras.CopyGraph() error =", err) |
| 88 |
} |
| 89 |
|
| 90 |
// verify extracted symlink |
| 91 |
extractedSymlink := filepath.Join(tempDir, dirName, symlinkName) |
| 92 |
symlinkDst, err := os.Readlink(extractedSymlink) |
| 93 |
if err != nil { |
| 94 |
t.Fatal("failed to get symlink destination, error =", err) |
| 95 |
} |
| 96 |
if want := fileName; symlinkDst != want { |
| 97 |
t.Errorf("symlink destination = %v, want %v", symlinkDst, want) |
| 98 |
} |
| 99 |
got, err := os.ReadFile(extractedSymlink) |
| 100 |
if err != nil { |
| 101 |
t.Fatal("failed to read symlink file, error =", err) |
| 102 |
} |
| 103 |
if !bytes.Equal(got, content) { |
| 104 |
t.Errorf("symlink content = %v, want %v", got, content) |
| 105 |
} |
| 106 |
|
| 107 |
// copy to another file store created from a relative root, to trigger extracting directory |
| 108 |
tempDir = t.TempDir() |
| 109 |
t.Chdir(tempDir) |
| 110 |
dstRel, err := New(".") |
| 111 |
if err != nil { |
| 112 |
t.Fatal("Store.New() error =", err) |
| 113 |
} |
| 114 |
defer dstRel.Close() |
| 115 |
if err := oras.CopyGraph(ctx, src, dstRel, manifestDesc, oras.DefaultCopyGraphOptions); err != nil { |
| 116 |
t.Fatal("oras.CopyGraph() error =", err) |
| 117 |
} |
| 118 |
|
| 119 |
// verify extracted symlink |
| 120 |
extractedSymlink = filepath.Join(tempDir, dirName, symlinkName) |
| 121 |
symlinkDst, err = os.Readlink(extractedSymlink) |
| 122 |
if err != nil { |
| 123 |
t.Fatal("failed to get symlink destination, error =", err) |
| 124 |
} |
| 125 |
if want := fileName; symlinkDst != want { |
| 126 |
t.Errorf("symlink destination = %v, want %v", symlinkDst, want) |
| 127 |
} |
| 128 |
got, err = os.ReadFile(extractedSymlink) |
| 129 |
if err != nil { |
| 130 |
t.Fatal("failed to read symlink file, error =", err) |
| 131 |
} |
| 132 |
if !bytes.Equal(got, content) { |
| 133 |
t.Errorf("symlink content = %v, want %v", got, content) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// Related issue: https://github.com/oras-project/oras-go/issues/402 |
| 138 |
func TestStore_Dir_ExtractSymlinkAbs(t *testing.T) { |
| 139 |
// prepare test content |
| 140 |
tempDir, err := filepath.EvalSymlinks(t.TempDir()) |
| 141 |
if err != nil { |
| 142 |
t.Fatal("error calling filepath.EvalSymlinks(), error =", err) |
| 143 |
} |
| 144 |
dirName := "testdir" |
| 145 |
dirPath := filepath.Join(tempDir, dirName) |
| 146 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 147 |
t.Fatal("error calling Mkdir(), error =", err) |
| 148 |
} |
| 149 |
|
| 150 |
content := []byte("hello world") |
| 151 |
fileName := "test.txt" |
| 152 |
filePath := filepath.Join(dirPath, fileName) |
| 153 |
if err := os.WriteFile(filePath, content, 0444); err != nil { |
| 154 |
t.Fatal("error calling WriteFile(), error =", err) |
| 155 |
} |
| 156 |
// create symlink to an absolute path |
| 157 |
symlink := filepath.Join(dirPath, "test_symlink") |
| 158 |
if err := os.Symlink(filePath, symlink); err != nil { |
| 159 |
t.Fatal("error calling Symlink(), error =", err) |
| 160 |
} |
| 161 |
|
| 162 |
src, err := New(tempDir) |
| 163 |
if err != nil { |
| 164 |
t.Fatal("Store.New() error =", err) |
| 165 |
} |
| 166 |
defer src.Close() |
| 167 |
ctx := context.Background() |
| 168 |
|
| 169 |
// add dir |
| 170 |
desc, err := src.Add(ctx, dirName, "", dirPath) |
| 171 |
if err != nil { |
| 172 |
t.Fatal("Store.Add() error =", err) |
| 173 |
} |
| 174 |
// pack a manifest |
| 175 |
opts := oras.PackManifestOptions{ |
| 176 |
Layers: []ocispec.Descriptor{desc}, |
| 177 |
} |
| 178 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "test/dir", opts) |
| 179 |
if err != nil { |
| 180 |
t.Fatal("oras.PackManifest() error =", err) |
| 181 |
} |
| 182 |
|
| 183 |
// remove the original testing directory and create a new store using an absolute root |
| 184 |
if err := os.RemoveAll(dirPath); err != nil { |
| 185 |
t.Fatal("error calling RemoveAll(), error =", err) |
| 186 |
} |
| 187 |
dstAbs, err := New(tempDir) |
| 188 |
if err != nil { |
| 189 |
t.Fatal("Store.New() error =", err) |
| 190 |
} |
| 191 |
defer dstAbs.Close() |
| 192 |
if err := oras.CopyGraph(ctx, src, dstAbs, manifestDesc, oras.DefaultCopyGraphOptions); err != nil { |
| 193 |
t.Fatal("oras.CopyGraph() error =", err) |
| 194 |
} |
| 195 |
|
| 196 |
// verify extracted symlink |
| 197 |
symlinkDst, err := os.Readlink(symlink) |
| 198 |
if err != nil { |
| 199 |
t.Fatal("failed to get symlink destination, error =", err) |
| 200 |
} |
| 201 |
if want := filePath; symlinkDst != want { |
| 202 |
t.Errorf("symlink destination = %v, want %v", symlinkDst, want) |
| 203 |
} |
| 204 |
got, err := os.ReadFile(symlink) |
| 205 |
if err != nil { |
| 206 |
t.Fatal("failed to read symlink file, error =", err) |
| 207 |
} |
| 208 |
if !bytes.Equal(got, content) { |
| 209 |
t.Errorf("symlink content = %v, want %v", got, content) |
| 210 |
} |
| 211 |
|
| 212 |
// remove the original testing directory and create a new store using a relative path |
| 213 |
if err := os.RemoveAll(dirPath); err != nil { |
| 214 |
t.Fatal("error calling RemoveAll(), error =", err) |
| 215 |
} |
| 216 |
t.Chdir(tempDir) |
| 217 |
dstRel, err := New(".") |
| 218 |
if err != nil { |
| 219 |
t.Fatal("Store.New() error =", err) |
| 220 |
} |
| 221 |
defer dstRel.Close() |
| 222 |
if err := oras.CopyGraph(ctx, src, dstRel, manifestDesc, oras.DefaultCopyGraphOptions); err != nil { |
| 223 |
t.Fatal("oras.CopyGraph() error =", err) |
| 224 |
} |
| 225 |
|
| 226 |
// verify extracted symlink |
| 227 |
symlinkDst, err = os.Readlink(symlink) |
| 228 |
if err != nil { |
| 229 |
t.Fatal("failed to get symlink destination, error =", err) |
| 230 |
} |
| 231 |
if want := filePath; symlinkDst != want { |
| 232 |
t.Errorf("symlink destination = %v, want %v", symlinkDst, want) |
| 233 |
} |
| 234 |
got, err = os.ReadFile(symlink) |
| 235 |
if err != nil { |
| 236 |
t.Fatal("failed to read symlink file, error =", err) |
| 237 |
} |
| 238 |
if !bytes.Equal(got, content) { |
| 239 |
t.Errorf("symlink content = %v, want %v", got, content) |
| 240 |
} |
| 241 |
|
| 242 |
// copy to another file store created from an outside root, to trigger extracting directory |
| 243 |
tempDir = t.TempDir() |
| 244 |
dstOutside, err := New(tempDir) |
| 245 |
if err != nil { |
| 246 |
t.Fatal("Store.New() error =", err) |
| 247 |
} |
| 248 |
defer dstOutside.Close() |
| 249 |
if err := oras.CopyGraph(ctx, src, dstOutside, manifestDesc, oras.DefaultCopyGraphOptions); err == nil { |
| 250 |
t.Error("oras.CopyGraph() error = nil, wantErr ", true) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// Related issue: https://github.com/oras-project/oras-go/issues/865 |
| 255 |
func TestStore_Dir_OverwriteSymlinkRel(t *testing.T) { |
| 256 |
// prepare test content |
| 257 |
tempDir := t.TempDir() |
| 258 |
dirName := "testdir" |
| 259 |
dirPath := filepath.Join(tempDir, dirName) |
| 260 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 261 |
t.Fatal("error calling Mkdir(), error =", err) |
| 262 |
} |
| 263 |
|
| 264 |
content := []byte("hello world") |
| 265 |
fileName := "test.txt" |
| 266 |
filePath := filepath.Join(dirPath, fileName) |
| 267 |
if err := os.WriteFile(filePath, content, 0644); err != nil { |
| 268 |
t.Fatal("error calling WriteFile(), error =", err) |
| 269 |
} |
| 270 |
// create symlink to a relative path |
| 271 |
symlinkName := "test_symlink" |
| 272 |
symlinkPath := filepath.Join(dirPath, symlinkName) |
| 273 |
if err := os.Symlink(fileName, symlinkPath); err != nil { |
| 274 |
t.Fatal("error calling Symlink(), error =", err) |
| 275 |
} |
| 276 |
|
| 277 |
src, err := New(tempDir) |
| 278 |
if err != nil { |
| 279 |
t.Fatal("Store.New() error =", err) |
| 280 |
} |
| 281 |
defer src.Close() |
| 282 |
ctx := context.Background() |
| 283 |
|
| 284 |
// add dir |
| 285 |
desc, err := src.Add(ctx, dirName, "", dirPath) |
| 286 |
if err != nil { |
| 287 |
t.Fatal("Store.Add() error =", err) |
| 288 |
} |
| 289 |
// pack a manifest |
| 290 |
opts := oras.PackManifestOptions{ |
| 291 |
Layers: []ocispec.Descriptor{desc}, |
| 292 |
} |
| 293 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "test/dir", opts) |
| 294 |
if err != nil { |
| 295 |
t.Fatal("oras.PackManifest() error =", err) |
| 296 |
} |
| 297 |
|
| 298 |
// copy to another file store created from an absolute root, to trigger extracting directory |
| 299 |
tempDir = t.TempDir() |
| 300 |
dst, err := New(tempDir) |
| 301 |
if err != nil { |
| 302 |
t.Fatal("Store.New() error =", err) |
| 303 |
} |
| 304 |
defer dst.Close() |
| 305 |
if err := oras.CopyGraph(ctx, src, dst, manifestDesc, oras.DefaultCopyGraphOptions); err != nil { |
| 306 |
t.Fatal("oras.CopyGraph() error =", err) |
| 307 |
} |
| 308 |
|
| 309 |
// copy to another file store created from the same root again, to test overwriting symlink |
| 310 |
sameDst, err := New(tempDir) |
| 311 |
if err != nil { |
| 312 |
t.Fatal("Store.New() error =", err) |
| 313 |
} |
| 314 |
defer sameDst.Close() |
| 315 |
if err := oras.CopyGraph(ctx, src, sameDst, manifestDesc, oras.DefaultCopyGraphOptions); err != nil { |
| 316 |
t.Fatal("oras.CopyGraph() error =", err) |
| 317 |
} |
| 318 |
|
| 319 |
// verify extracted symlink |
| 320 |
extractedSymlink := filepath.Join(tempDir, dirName, symlinkName) |
| 321 |
symlinkDst, err := os.Readlink(extractedSymlink) |
| 322 |
if err != nil { |
| 323 |
t.Fatal("failed to get symlink destination, error =", err) |
| 324 |
} |
| 325 |
if want := fileName; symlinkDst != want { |
| 326 |
t.Errorf("symlink destination = %v, want %v", symlinkDst, want) |
| 327 |
} |
| 328 |
got, err := os.ReadFile(extractedSymlink) |
| 329 |
if err != nil { |
| 330 |
t.Fatal("failed to read symlink file, error =", err) |
| 331 |
} |
| 332 |
if !bytes.Equal(got, content) { |
| 333 |
t.Errorf("symlink content = %v, want %v", got, content) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// Related issue: https://github.com/oras-project/oras-go/issues/865 |
| 338 |
func TestStore_Dir_OverwriteSymlinkAbs(t *testing.T) { |
| 339 |
// prepare test content |
| 340 |
tempDir, err := filepath.EvalSymlinks(t.TempDir()) |
| 341 |
if err != nil { |
| 342 |
t.Fatal("error calling filepath.EvalSymlinks(), error =", err) |
| 343 |
} |
| 344 |
dirName := "testdir" |
| 345 |
dirPath := filepath.Join(tempDir, dirName) |
| 346 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 347 |
t.Fatal("error calling Mkdir(), error =", err) |
| 348 |
} |
| 349 |
|
| 350 |
content := []byte("hello world") |
| 351 |
fileName := "test.txt" |
| 352 |
filePath := filepath.Join(dirPath, fileName) |
| 353 |
if err := os.WriteFile(filePath, content, 0777); err != nil { |
| 354 |
t.Fatal("error calling WriteFile(), error =", err) |
| 355 |
} |
| 356 |
// create symlink to an absolute path |
| 357 |
symlink := filepath.Join(dirPath, "test_symlink") |
| 358 |
if err := os.Symlink(filePath, symlink); err != nil { |
| 359 |
t.Fatal("error calling Symlink(), error =", err) |
| 360 |
} |
| 361 |
|
| 362 |
src, err := New(tempDir) |
| 363 |
if err != nil { |
| 364 |
t.Fatal("Store.New() error =", err) |
| 365 |
} |
| 366 |
defer src.Close() |
| 367 |
ctx := context.Background() |
| 368 |
|
| 369 |
// add dir |
| 370 |
desc, err := src.Add(ctx, dirName, "", dirPath) |
| 371 |
if err != nil { |
| 372 |
t.Fatal("Store.Add() error =", err) |
| 373 |
} |
| 374 |
// pack a manifest |
| 375 |
opts := oras.PackManifestOptions{ |
| 376 |
Layers: []ocispec.Descriptor{desc}, |
| 377 |
} |
| 378 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "test/dir", opts) |
| 379 |
if err != nil { |
| 380 |
t.Fatal("oras.PackManifest() error =", err) |
| 381 |
} |
| 382 |
|
| 383 |
// create a new store from the same root, to test overwriting symlink |
| 384 |
dst, err := New(tempDir) |
| 385 |
if err != nil { |
| 386 |
t.Fatal("Store.New() error =", err) |
| 387 |
} |
| 388 |
defer dst.Close() |
| 389 |
if err := oras.CopyGraph(ctx, src, dst, manifestDesc, oras.DefaultCopyGraphOptions); err != nil { |
| 390 |
t.Fatal("oras.CopyGraph() error =", err) |
| 391 |
} |
| 392 |
|
| 393 |
// verify extracted symlink |
| 394 |
symlinkDst, err := os.Readlink(symlink) |
| 395 |
if err != nil { |
| 396 |
t.Fatal("failed to get symlink destination, error =", err) |
| 397 |
} |
| 398 |
if want := filePath; symlinkDst != want { |
| 399 |
t.Errorf("symlink destination = %v, want %v", symlinkDst, want) |
| 400 |
} |
| 401 |
got, err := os.ReadFile(symlink) |
| 402 |
if err != nil { |
| 403 |
t.Fatal("failed to read symlink file, error =", err) |
| 404 |
} |
| 405 |
if !bytes.Equal(got, content) { |
| 406 |
t.Errorf("symlink content = %v, want %v", got, content) |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
func TestStore_Dir_OverwriteSymlink_RemovalFailed(t *testing.T) { |
| 411 |
// prepare test content |
| 412 |
tempDir, err := filepath.EvalSymlinks(t.TempDir()) |
| 413 |
if err != nil { |
| 414 |
t.Fatal("error calling filepath.EvalSymlinks(), error =", err) |
| 415 |
} |
| 416 |
dirName := "testdir" |
| 417 |
dirPath := filepath.Join(tempDir, dirName) |
| 418 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 419 |
t.Fatal("error calling Mkdir(), error =", err) |
| 420 |
} |
| 421 |
|
| 422 |
content := []byte("hello world") |
| 423 |
fileName := "test.txt" |
| 424 |
filePath := filepath.Join(dirPath, fileName) |
| 425 |
if err := os.WriteFile(filePath, content, 0666); err != nil { |
| 426 |
t.Fatal("error calling WriteFile(), error =", err) |
| 427 |
} |
| 428 |
// create symlink to an absolute path |
| 429 |
symlink := filepath.Join(dirPath, "test_symlink") |
| 430 |
if err := os.Symlink(filePath, symlink); err != nil { |
| 431 |
t.Fatal("error calling Symlink(), error =", err) |
| 432 |
} |
| 433 |
// remove the link target |
| 434 |
if err := os.Remove(filePath); err != nil { |
| 435 |
t.Fatal("error calling Remove(), error =", err) |
| 436 |
} |
| 437 |
|
| 438 |
// new store based on the existing dir |
| 439 |
src, err := New(tempDir) |
| 440 |
if err != nil { |
| 441 |
t.Fatal("Store.New() error =", err) |
| 442 |
} |
| 443 |
defer src.Close() |
| 444 |
ctx := context.Background() |
| 445 |
|
| 446 |
// add dir |
| 447 |
desc, err := src.Add(ctx, dirName, "", dirPath) |
| 448 |
if err != nil { |
| 449 |
t.Fatal("Store.Add() error =", err) |
| 450 |
} |
| 451 |
// pack a manifest |
| 452 |
opts := oras.PackManifestOptions{ |
| 453 |
Layers: []ocispec.Descriptor{desc}, |
| 454 |
} |
| 455 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "test/dir", opts) |
| 456 |
if err != nil { |
| 457 |
t.Fatal("oras.PackManifest() error =", err) |
| 458 |
} |
| 459 |
|
| 460 |
// create a new store from the same root, to test overwriting symlink |
| 461 |
dst, err := New(tempDir) |
| 462 |
if err != nil { |
| 463 |
t.Fatal("Store.New() error =", err) |
| 464 |
} |
| 465 |
defer dst.Close() |
| 466 |
|
| 467 |
// disallow write on the directory so that the removal will fail |
| 468 |
if err := os.Chmod(dirPath, 0555); err != nil { |
| 469 |
t.Fatal("error calling Chmod(), error =", err) |
| 470 |
} |
| 471 |
if err := oras.CopyGraph(ctx, src, dst, manifestDesc, oras.DefaultCopyGraphOptions); err == nil { |
| 472 |
t.Error("oras.CopyGraph() error = nil, wantErr = true") |
| 473 |
} |
| 474 |
// chmod back so that the directory can be cleaned up |
| 475 |
if err := os.Chmod(dirPath, 0777); err != nil { |
| 476 |
t.Fatal("error calling Chmod(), error =", err) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
// TestStore_Dir_SymlinkChainEscape reproduces GHSA-m37j-52j7-pjw7: a malicious |
| 481 |
// unpack layer plants a chain of symlinks whose lexical target stays inside the |
| 482 |
// extraction root but whose kernel-resolved target is an absolute path outside |
| 483 |
// it, then writes a regular file with the same name to write through the link. |
| 484 |
// The write must be contained inside the store's working directory. |
| 485 |
func TestStore_Dir_SymlinkChainEscape(t *testing.T) { |
| 486 |
workDir := t.TempDir() |
| 487 |
// attacker's target lives outside the store working directory |
| 488 |
outsideDir := t.TempDir() |
| 489 |
outsidePath := filepath.Join(outsideDir, "PWNED") |
| 490 |
|
| 491 |
const title = "out" |
| 492 |
baseAbs := filepath.Join(workDir, title) |
| 493 |
depth := len(strings.Split(strings.Trim(filepath.ToSlash(baseAbs), "/"), "/")) |
| 494 |
|
| 495 |
// build the malicious tar.gz |
| 496 |
var buf bytes.Buffer |
| 497 |
gzw := gzip.NewWriter(&buf) |
| 498 |
tw := tar.NewWriter(gzw) |
| 499 |
|
| 500 |
dirs := make([]string, depth) |
| 501 |
for i := range dirs { |
| 502 |
dirs[i] = "d" |
| 503 |
} |
| 504 |
for i := 1; i <= depth; i++ { |
| 505 |
if err := tw.WriteHeader(&tar.Header{ |
| 506 |
Typeflag: tar.TypeDir, |
| 507 |
Name: title + "/" + strings.Join(dirs[:i], "/"), |
| 508 |
Mode: 0o755, |
| 509 |
}); err != nil { |
| 510 |
t.Fatal(err) |
| 511 |
} |
| 512 |
} |
| 513 |
// "up" symlink at the bottom, resolving back to baseAbs |
| 514 |
if err := tw.WriteHeader(&tar.Header{ |
| 515 |
Typeflag: tar.TypeSymlink, |
| 516 |
Name: title + "/" + strings.Join(dirs, "/") + "/up", |
| 517 |
Linkname: strings.Repeat("../", depth-1) + "..", |
| 518 |
Mode: 0o777, |
| 519 |
}); err != nil { |
| 520 |
t.Fatal(err) |
| 521 |
} |
| 522 |
// "escape" symlink whose lexical target stays in-bounds but whose |
| 523 |
// kernel-resolved target is outsidePath |
| 524 |
escapeTarget := strings.Join(dirs, "/") + "/up/" + strings.Repeat("../", depth-1) + ".." + outsidePath |
| 525 |
if err := tw.WriteHeader(&tar.Header{ |
| 526 |
Typeflag: tar.TypeSymlink, |
| 527 |
Name: title + "/escape", |
| 528 |
Linkname: escapeTarget, |
| 529 |
Mode: 0o777, |
| 530 |
}); err != nil { |
| 531 |
t.Fatal(err) |
| 532 |
} |
| 533 |
// regular file with the same name, written through the symlink |
| 534 |
payload := []byte("PWNED-BY-ORAS-TARSLIP") |
| 535 |
if err := tw.WriteHeader(&tar.Header{ |
| 536 |
Typeflag: tar.TypeReg, |
| 537 |
Name: title + "/escape", |
| 538 |
Mode: 0o644, |
| 539 |
Size: int64(len(payload)), |
| 540 |
}); err != nil { |
| 541 |
t.Fatal(err) |
| 542 |
} |
| 543 |
if _, err := tw.Write(payload); err != nil { |
| 544 |
t.Fatal(err) |
| 545 |
} |
| 546 |
if err := tw.Close(); err != nil { |
| 547 |
t.Fatal(err) |
| 548 |
} |
| 549 |
if err := gzw.Close(); err != nil { |
| 550 |
t.Fatal(err) |
| 551 |
} |
| 552 |
blob := buf.Bytes() |
| 553 |
|
| 554 |
desc := ocispec.Descriptor{ |
| 555 |
MediaType: ocispec.MediaTypeImageLayerGzip, |
| 556 |
Digest: digest.FromBytes(blob), |
| 557 |
Size: int64(len(blob)), |
| 558 |
Annotations: map[string]string{ |
| 559 |
ocispec.AnnotationTitle: title, |
| 560 |
AnnotationUnpack: "true", |
| 561 |
}, |
| 562 |
} |
| 563 |
|
| 564 |
s, err := New(workDir) |
| 565 |
if err != nil { |
| 566 |
t.Fatal("Store.New() error =", err) |
| 567 |
} |
| 568 |
defer s.Close() |
| 569 |
if s.AllowPathTraversalOnWrite { |
| 570 |
t.Fatal("expected AllowPathTraversalOnWrite to default to false") |
| 571 |
} |
| 572 |
|
| 573 |
// Push may succeed (write contained inside the store) or fail, but under no |
| 574 |
// circumstances may it write outside the working directory. |
| 575 |
_ = s.Push(context.Background(), desc, bytes.NewReader(blob)) |
| 576 |
|
| 577 |
if _, err := os.Stat(outsidePath); err == nil { |
| 578 |
t.Fatalf("path traversal: file written outside working dir at %s", outsidePath) |
| 579 |
} else if !os.IsNotExist(err) { |
| 580 |
t.Fatalf("unexpected error stat-ing %s: %v", outsidePath, err) |
| 581 |
} |
| 582 |
} |