Skip to content

Commit 22fc573

Browse files
Merge pull request #3211 from rhatdan/overlay
Preserve ownership of lower directory when doing an overlay mount
2 parents 27f6ef5 + 2e1b71c commit 22fc573

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pkg/overlay/overlay.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88
"path/filepath"
99
"strings"
10+
"syscall"
1011

1112
"github.com/containers/storage/pkg/idtools"
1213
"github.com/containers/storage/pkg/system"
@@ -84,6 +85,12 @@ func mountHelper(contentDir, source, dest string, _, _ int, graphOptions []strin
8485
if err := os.Chmod(upperDir, st.Mode()); err != nil {
8586
return mount, err
8687
}
88+
if stat, ok := st.Sys().(*syscall.Stat_t); ok {
89+
if err := os.Chown(upperDir, int(stat.Uid), int(stat.Gid)); err != nil {
90+
return mount, err
91+
}
92+
}
93+
8794
overlayOptions = fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s,private", source, upperDir, workDir)
8895
}
8996

tests/overlay.bats

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ load helpers
3131
[ "$status" -ne 0 ]
3232
}
3333

34-
@test "overlay source permissions" {
34+
@test "overlay source permissions and owners" {
3535
if test \! -e /usr/bin/fuse-overlayfs -a "$BUILDAH_ISOLATION" = "rootless"; then
3636
skip "BUILDAH_ISOLATION = $BUILDAH_ISOLATION" and no /usr/bin/fuse-overlayfs present
3737
elif test "$STORAGE_DRIVER" = "vfs"; then
3838
skip "skipping overlay test because \$STORAGE_DRIVER = $STORAGE_DRIVER"
3939
fi
4040
image=alpine
4141
mkdir -m 770 ${TESTDIR}/lower
42-
permission=`stat -c %a ${TESTDIR}/lower`
42+
chown 1:1 ${TESTDIR}/lower
43+
permission=$(stat -c "%a %u %g" ${TESTDIR}/lower)
4344
run_buildah from --quiet -v ${TESTDIR}/lower:/tmp/test:O --quiet --signature-policy ${TESTSDIR}/policy.json $image
4445
cid=$output
4546

4647
# This should succeed
47-
run_buildah run $cid sh -c 'stat -c %a /tmp/test'
48-
expect_output $permission
48+
run_buildah run $cid sh -c 'stat -c "%a %u %g" /tmp/test'
49+
expect_output "$permission"
4950

5051
# Create and remove content in the overlay directory, should succeed
5152
touch ${TESTDIR}/lower/foo

0 commit comments

Comments
 (0)