Skip to content

Commit baaaedd

Browse files
Merge pull request containers#6381 from nalind/mount-target-parent-perms
Run: create parent directories of mount targets with mode 0755
2 parents 73194da + 9cd4768 commit baaaedd

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

run_common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,12 @@ func (b *Builder) createMountTargets(spec *specs.Spec) ([]copier.ConditionalRemo
21212121
// forced permissions
21222122
mode = &perms
21232123
}
2124+
if mode == nil && destination != cleanedDestination {
2125+
// parent directories default to 0o755, for
2126+
// the sake of commands running as UID != 0
2127+
perms := os.FileMode(0o755)
2128+
mode = &perms
2129+
}
21242130
targets.Paths = append(targets.Paths, copier.EnsurePath{
21252131
Path: destination,
21262132
Typeflag: typeFlag,

tests/bud.bats

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,8 @@ _EOF
13611361
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 stat / /var/tmp
13621362
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 test \`stat -c %u /var/tmp\` -eq 1000
13631363
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
1364+
RUN --mount=type=cache,id=${cacheid},target=/new-parent/var/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
1365+
RUN --mount=type=cache,id=${cacheid},target=/var/new-parent/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
13641366
EOF
13651367
run_buildah build $WITH_POLICY_JSON ${contextdir}
13661368

@@ -1372,6 +1374,8 @@ EOF
13721374
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 stat / /var/tmp
13731375
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 test \`stat -c %u /var/tmp\` -eq 1000
13741376
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
1377+
RUN --mount=type=cache,id=${cacheid},target=/new/parent/var/tmp,uid=1000,gid=1000 touch /new/parent/var/tmp/should-be-able-to-write
1378+
RUN --mount=type=cache,id=${cacheid},target=/var/new/parent/tmp,uid=1000,gid=1000 touch /var/new/parent/tmp/should-be-able-to-write
13751379
EOF
13761380
if test `id -u` -eq 0 ; then
13771381
run_buildah build --userns-uid-map 0:1:1023 --userns-gid-map 0:1:1023 $WITH_POLICY_JSON ${contextdir}
@@ -1380,6 +1384,55 @@ EOF
13801384
fi
13811385
}
13821386

1387+
@test "build-mount-cache-writeable-as-unprivileged-user" {
1388+
_prefetch busybox
1389+
local contextdir=${TEST_SCRATCH_DIR}/context
1390+
mkdir ${contextdir}
1391+
1392+
cat > ${contextdir}/Dockerfile << EOF
1393+
FROM busybox
1394+
USER 1000:1000
1395+
RUN --mount=type=cache,target=/usr/local/bin,id=/usr/local/bin/$$,uid=1000,gid=1000 touch /usr/local/bin/new-file
1396+
RUN --mount=type=cache,target=/var/not/already/there,id=/var/not/already/there/$$,uid=1000,gid=1000 touch /var/not/already/there/new-file
1397+
EOF
1398+
run_buildah build $WITH_POLICY_JSON ${contextdir}
1399+
}
1400+
1401+
@test "build-mount-bind-readable-as-unprivileged-user" {
1402+
_prefetch busybox
1403+
local contextdir=${TEST_SCRATCH_DIR}/context
1404+
mkdir ${contextdir}
1405+
1406+
cat > ${contextdir}/Dockerfile << EOF
1407+
FROM busybox
1408+
USER 1000:1000
1409+
RUN --mount=type=bind,target=/usr/local,from=busybox busybox ls /usr/local/bin/busybox
1410+
RUN --mount=type=bind,target=/var/not/already/there,from=busybox busybox ls /var/not/already/there/bin/busybox
1411+
EOF
1412+
run_buildah build $WITH_POLICY_JSON ${contextdir}
1413+
}
1414+
1415+
@test "build-mount-secret-readable-as-unprivileged-user" {
1416+
_prefetch busybox
1417+
local contextdir=${TEST_SCRATCH_DIR}/context
1418+
mkdir ${contextdir}
1419+
local secretfile=${TEST_SCRATCH_DIR}/secret.txt
1420+
1421+
echo -n hidingInPlainSight > ${secretfile}
1422+
cat > ${contextdir}/Dockerfile << EOF
1423+
FROM busybox
1424+
USER 1000:1000
1425+
RUN --mount=type=secret,id=theSecret,target=/var/not/already/there,uid=1000,gid=1000 wc -c /var/not/already/there
1426+
EOF
1427+
run_buildah build --secret id=theSecret,type=file,src=${secretfile} $WITH_POLICY_JSON ${contextdir}
1428+
cat > ${contextdir}/Dockerfile << EOF
1429+
FROM busybox
1430+
USER 1000:1000
1431+
RUN --mount=type=secret,id=theSecret,target=/top/var/tmp/there,uid=1000,gid=1000 wc -c /top/var/tmp/there
1432+
EOF
1433+
run_buildah build --secret id=theSecret,type=file,src=${secretfile} $WITH_POLICY_JSON ${contextdir}
1434+
}
1435+
13831436
@test "build test if supplemental groups has gid with --isolation chroot" {
13841437
test "${BUILDAH_ISOLATION}" != chroot || skip "BUILDAH_ISOLATION=${BUILDAH_ISOLATION} overrides --isolation"
13851438

tests/run.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,13 +982,13 @@ _EOF
982982
run_buildah from --quiet --pull=false $WITH_POLICY_JSON alpine
983983
cid=$output
984984
for mask in /proc/acpi /proc/interrupts /proc/kcore /proc/keys /proc/latency_stats /proc/sched_debug /proc/scsi /proc/timer_list /proc/timer_stats /sys/devices/virtual/powercap /sys/firmware /sys/fs/selinux; do
985-
if test -d $mask; then
986-
run_buildah run $cid sh -c "echo $mask/*" # globbing will fail whether it's simply unreadable, or readable but empty
987-
expect_output "$mask/*" "Directories should be empty"
985+
if test -d $mask; then
986+
run_buildah run $cid sh -c "echo $mask/*" # globbing will fail whether it's simply unreadable, or readable but empty
987+
expect_output "$mask/*" "Directories should be empty"
988988
fi
989989
if test -f $mask; then
990-
run_buildah run $cid cat $mask
991-
expect_output "" "Directories should be empty"
990+
run_buildah run $cid cat $mask
991+
expect_output "" "Directories should be empty"
992992
fi
993993
done
994994
}

0 commit comments

Comments
 (0)