diff --git a/integration/dockerfiles-with-context/issue-2075/Dockerfile b/integration/dockerfiles-with-context/issue-2075/Dockerfile new file mode 100644 index 000000000..03d61853c --- /dev/null +++ b/integration/dockerfiles-with-context/issue-2075/Dockerfile @@ -0,0 +1,28 @@ +# Copyright 2022 Google, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM docker.io/debian:bullseye-slim as base +FROM base as build +COPY ["top1", "/tmp/top1"] +RUN \ + set -eu; \ + cp /tmp/top1 /usr/local/bin/top1; \ + chown root:root /usr/local/bin/top1; \ + chmod u=rxs,go=rx /usr/local/bin/top1; \ + ls -lh /usr/local/bin/top1 +FROM base as final +COPY --from=build ["/usr/local/bin/top1", "/usr/local/bin/"] +RUN [ -u /usr/local/bin/top1 ] +LABEL \ + description="Testing setuid behavior in Kaniko" diff --git a/integration/dockerfiles-with-context/issue-2075/top1 b/integration/dockerfiles-with-context/issue-2075/top1 new file mode 100755 index 000000000..5d1f4f536 Binary files /dev/null and b/integration/dockerfiles-with-context/issue-2075/top1 differ diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 347774b14..b21653da2 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -892,7 +892,11 @@ func getSymlink(path string) error { func CopyFileOrSymlink(src string, destDir string, root string) error { destFile := filepath.Join(destDir, src) src = filepath.Join(root, src) - if fi, _ := os.Lstat(src); IsSymlink(fi) { + fi, err := os.Lstat(src) + if err != nil { + return errors.Wrap(err, "getting file info") + } + if IsSymlink(fi) { link, err := os.Readlink(src) if err != nil { return errors.Wrap(err, "copying file or symlink") @@ -902,14 +906,15 @@ func CopyFileOrSymlink(src string, destDir string, root string) error { } return os.Symlink(link, destFile) } - err := otiai10Cpy.Copy(src, destFile) - if err != nil { + if err := otiai10Cpy.Copy(src, destFile); err != nil { return errors.Wrap(err, "copying file") } - err = CopyOwnership(src, destDir, root) - if err != nil { + if err := CopyOwnership(src, destDir, root); err != nil { return errors.Wrap(err, "copying ownership") } + if err := os.Chmod(destFile, fi.Mode()); err != nil { + return errors.Wrap(err, "copying file mode") + } return nil }