Skip to content

Commit a64c647

Browse files
Merge pull request #5797 from dashea/dshea-1.27-cve-2024-9675
[release-1.27] Properly validate cache IDs and sources
2 parents 02fb249 + cd0e6c7 commit a64c647

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
# Changelog
44

5+
## v1.27.5 (2024-10-24)
6+
7+
Properly validate cache IDs and sources
8+
59
## v1.27.4 (2024-03-26)
610

711
[release-1.27] Bump Bump google.golang.org/protobuf to v1.33.0

changelog.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- Changelog for v1.27.5 (2024-10-24)
2+
* Properly validate cache IDs and sources
3+
14
- Changelog for v1.27.4 (2024-03-26)
25
* [release-1.27] Bump Bump google.golang.org/protobuf to v1.33.0
36
* [release-1.27] conformance tests: don't break on trailing zeroes

define/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const (
3030
Package = "buildah"
3131
// Version for the Package. Bump version in contrib/rpm/buildah.spec
3232
// too.
33-
Version = "1.27.4"
33+
Version = "1.27.5"
3434

3535
// DefaultRuntime if containers.conf fails.
3636
DefaultRuntime = "runc"

internal/parse/parse.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/containers/storage"
1919
"github.com/containers/storage/pkg/idtools"
2020
"github.com/containers/storage/pkg/lockfile"
21+
digest "github.com/opencontainers/go-digest"
2122
specs "github.com/opencontainers/runtime-spec/specs-go"
2223
)
2324

@@ -306,7 +307,11 @@ func GetCacheMount(args []string, store storage.Store, imageMountLabel string, a
306307
return newMount, lockedTargets, fmt.Errorf("no stage found with name %s", fromStage)
307308
}
308309
// path should be /contextDir/specified path
309-
newMount.Source = filepath.Join(mountPoint, filepath.Clean(string(filepath.Separator)+newMount.Source))
310+
evaluated, err := copier.Eval(mountPoint, string(filepath.Separator)+newMount.Source, copier.EvalOptions{})
311+
if err != nil {
312+
return newMount, nil, err
313+
}
314+
newMount.Source = evaluated
310315
} else {
311316
// we need to create cache on host if no image is being used
312317

@@ -323,9 +328,13 @@ func GetCacheMount(args []string, store storage.Store, imageMountLabel string, a
323328
}
324329

325330
if id != "" {
326-
newMount.Source = filepath.Join(cacheParent, filepath.Clean(id))
331+
// Don't let the user control where we place the directory.
332+
dirID := digest.FromString(id).Encoded()[:16]
333+
newMount.Source = filepath.Join(cacheParent, dirID)
327334
} else {
328-
newMount.Source = filepath.Join(cacheParent, filepath.Clean(newMount.Destination))
335+
// Don't let the user control where we place the directory.
336+
dirID := digest.FromString(newMount.Destination).Encoded()[:16]
337+
newMount.Source = filepath.Join(cacheParent, dirID)
329338
}
330339
idPair := idtools.IDPair{
331340
UID: uid,

tests/bud.bats

+41
Original file line numberDiff line numberDiff line change
@@ -5236,3 +5236,44 @@ _EOF
52365236
assert "$status" -eq 2 "exit code from ls"
52375237
expect_output --substring "No such file or directory"
52385238
}
5239+
5240+
@test "build-check-cve-2024-9675" {
5241+
_prefetch alpine
5242+
5243+
# SELinux can successfully block this exploit.
5244+
if ! which selinuxenabled > /dev/null 2> /dev/null ; then
5245+
searg=""
5246+
elif selinuxenabled ; then
5247+
searg="--security-opt=label=disable"
5248+
fi
5249+
5250+
touch ${TEST_SCRATCH_DIR}/file.txt
5251+
5252+
cat > ${TEST_SCRATCH_DIR}/Containerfile <<EOF
5253+
FROM alpine
5254+
RUN --mount=type=cache,id=../../../../../../../../../../../$TEST_SCRATCH_DIR,target=/var/tmp \
5255+
ls -l /var/tmp && cat /var/tmp/file.txt
5256+
EOF
5257+
5258+
run_buildah 1 build --no-cache $searg ${TEST_SCRATCH_DIR}
5259+
expect_output --substring "cat: can't open '/var/tmp/file.txt': No such file or directory"
5260+
5261+
cat > ${TEST_SCRATCH_DIR}/Containerfile <<EOF
5262+
FROM alpine
5263+
RUN --mount=type=cache,source=../../../../../../../../../../../$TEST_SCRATCH_DIR,target=/var/tmp \
5264+
ls -l /var/tmp && cat /var/tmp/file.txt
5265+
EOF
5266+
5267+
run_buildah 1 build --no-cache $searg ${TEST_SCRATCH_DIR}
5268+
expect_output --substring "cat: can't open '/var/tmp/file.txt': No such file or directory"
5269+
5270+
mkdir ${TEST_SCRATCH_DIR}/cve20249675
5271+
cat > ${TEST_SCRATCH_DIR}/cve20249675/Containerfile <<EOF
5272+
FROM alpine
5273+
RUN --mount=type=cache,from=testbuild,source=../,target=/var/tmp \
5274+
ls -l /var/tmp && cat /var/tmp/file.txt
5275+
EOF
5276+
5277+
run_buildah 1 build --no-cache $searg --build-context testbuild=${TEST_SCRATCH_DIR}/cve20249675/ ${TEST_SCRATCH_DIR}/cve20249675/
5278+
expect_output --substring "cat: can't open '/var/tmp/file.txt': No such file or directory"
5279+
}

0 commit comments

Comments
 (0)