Skip to content

Commit ee5b574

Browse files
Merge pull request #6389 from nalind/mount-target-parent-perms-1.41
[release-1.41] Run: create parent directories of mount targets with mode 0755
2 parents 1e62000 + b7b5a91 commit ee5b574

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

run_common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,12 @@ func (b *Builder) createMountTargets(spec *specs.Spec) ([]copier.ConditionalRemo
21022102
// forced permissions
21032103
mode = &perms
21042104
}
2105+
if mode == nil && destination != cleanedDestination {
2106+
// parent directories default to 0o755, for
2107+
// the sake of commands running as UID != 0
2108+
perms := os.FileMode(0o755)
2109+
mode = &perms
2110+
}
21052111
targets.Paths = append(targets.Paths, copier.EnsurePath{
21062112
Path: destination,
21072113
Typeflag: typeFlag,

tests/bud.bats

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,8 @@ _EOF
12511251
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 stat / /var/tmp
12521252
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 test \`stat -c %u /var/tmp\` -eq 1000
12531253
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
1254+
RUN --mount=type=cache,id=${cacheid},target=/new-parent/var/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
1255+
RUN --mount=type=cache,id=${cacheid},target=/var/new-parent/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
12541256
EOF
12551257
run_buildah build $WITH_POLICY_JSON ${contextdir}
12561258

@@ -1262,6 +1264,8 @@ EOF
12621264
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 stat / /var/tmp
12631265
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 test \`stat -c %u /var/tmp\` -eq 1000
12641266
RUN --mount=type=cache,id=${cacheid},target=/var/tmp,uid=1000,gid=1000 touch /var/tmp/should-be-able-to-write
1267+
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
1268+
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
12651269
EOF
12661270
if test `id -u` -eq 0 ; then
12671271
run_buildah build --userns-uid-map 0:1:1023 --userns-gid-map 0:1:1023 $WITH_POLICY_JSON ${contextdir}
@@ -1270,6 +1274,55 @@ EOF
12701274
fi
12711275
}
12721276

1277+
@test "build-mount-cache-writeable-as-unprivileged-user" {
1278+
_prefetch busybox
1279+
local contextdir=${TEST_SCRATCH_DIR}/context
1280+
mkdir ${contextdir}
1281+
1282+
cat > ${contextdir}/Dockerfile << EOF
1283+
FROM busybox
1284+
USER 1000:1000
1285+
RUN --mount=type=cache,target=/usr/local/bin,id=/usr/local/bin/$$,uid=1000,gid=1000 touch /usr/local/bin/new-file
1286+
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
1287+
EOF
1288+
run_buildah build $WITH_POLICY_JSON ${contextdir}
1289+
}
1290+
1291+
@test "build-mount-bind-readable-as-unprivileged-user" {
1292+
_prefetch busybox
1293+
local contextdir=${TEST_SCRATCH_DIR}/context
1294+
mkdir ${contextdir}
1295+
1296+
cat > ${contextdir}/Dockerfile << EOF
1297+
FROM busybox
1298+
USER 1000:1000
1299+
RUN --mount=type=bind,target=/usr/local,from=busybox busybox ls /usr/local/bin/busybox
1300+
RUN --mount=type=bind,target=/var/not/already/there,from=busybox busybox ls /var/not/already/there/bin/busybox
1301+
EOF
1302+
run_buildah build $WITH_POLICY_JSON ${contextdir}
1303+
}
1304+
1305+
@test "build-mount-secret-readable-as-unprivileged-user" {
1306+
_prefetch busybox
1307+
local contextdir=${TEST_SCRATCH_DIR}/context
1308+
mkdir ${contextdir}
1309+
local secretfile=${TEST_SCRATCH_DIR}/secret.txt
1310+
1311+
echo -n hidingInPlainSight > ${secretfile}
1312+
cat > ${contextdir}/Dockerfile << EOF
1313+
FROM busybox
1314+
USER 1000:1000
1315+
RUN --mount=type=secret,id=theSecret,target=/var/not/already/there,uid=1000,gid=1000 wc -c /var/not/already/there
1316+
EOF
1317+
run_buildah build --secret id=theSecret,type=file,src=${secretfile} $WITH_POLICY_JSON ${contextdir}
1318+
cat > ${contextdir}/Dockerfile << EOF
1319+
FROM busybox
1320+
USER 1000:1000
1321+
RUN --mount=type=secret,id=theSecret,target=/top/var/tmp/there,uid=1000,gid=1000 wc -c /top/var/tmp/there
1322+
EOF
1323+
run_buildah build --secret id=theSecret,type=file,src=${secretfile} $WITH_POLICY_JSON ${contextdir}
1324+
}
1325+
12731326
@test "build test if supplemental groups has gid with --isolation chroot" {
12741327
test "${BUILDAH_ISOLATION}" != chroot || skip "BUILDAH_ISOLATION=${BUILDAH_ISOLATION} overrides --isolation"
12751328

tests/run.bats

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -981,14 +981,14 @@ _EOF
981981

982982
run_buildah from --quiet --pull=false $WITH_POLICY_JSON alpine
983983
cid=$output
984-
for mask in /proc/acpi /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 ls $mask
987-
expect_output "" "Directories should be empty"
984+
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"
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)