-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(backends): copy links when the filesystem rejects symlinks (CIFS/SMB) (#10890) #10893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
localai-bot
wants to merge
4
commits into
master
Choose a base branch
from
fix/oci-symlink-cifs-10890
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+346
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
561056b
fix(backends): fall back to copying links when the filesystem rejects…
mudler 26a9d1d
fix(oci): check deferred Close in copyFilePreservingMode (errcheck)
mudler 0ca4ef2
fix(oci): reject path-traversal tar entries in the link-copy fallback
mudler 504751c
fix(oci): silence gosec on the validated link-copy file ops
mudler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package oci | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| // buildTar assembles an in-memory tar carrying a directory, a regular file and | ||
| // a relative symlink pointing at that file, mirroring the layout of a backend | ||
| // image (e.g. libcublas.so -> libcublas.so.12). | ||
| func buildTar() []byte { | ||
| var buf bytes.Buffer | ||
| tw := tar.NewWriter(&buf) | ||
|
|
||
| Expect(tw.WriteHeader(&tar.Header{ | ||
| Name: "lib/", | ||
| Typeflag: tar.TypeDir, | ||
| Mode: 0755, | ||
| })).To(Succeed()) | ||
|
|
||
| content := []byte("real library bytes") | ||
| Expect(tw.WriteHeader(&tar.Header{ | ||
| Name: "lib/libcublas.so.12", | ||
| Typeflag: tar.TypeReg, | ||
| Mode: 0644, | ||
| Size: int64(len(content)), | ||
| })).To(Succeed()) | ||
| _, err := tw.Write(content) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| Expect(tw.WriteHeader(&tar.Header{ | ||
| Name: "lib/libcublas.so", | ||
| Typeflag: tar.TypeSymlink, | ||
| Linkname: "libcublas.so.12", | ||
| Mode: 0777, | ||
| })).To(Succeed()) | ||
|
|
||
| Expect(tw.Close()).To(Succeed()) | ||
| return buf.Bytes() | ||
| } | ||
|
|
||
| var _ = Describe("Tar extraction fallback for link-less filesystems", func() { | ||
| Describe("isLinkUnsupportedError", func() { | ||
| It("recognises filesystem link-unsupported errors", func() { | ||
| Expect(isLinkUnsupportedError(syscall.ENOTSUP)).To(BeTrue()) | ||
| Expect(isLinkUnsupportedError(syscall.EOPNOTSUPP)).To(BeTrue()) | ||
| Expect(isLinkUnsupportedError(syscall.EPERM)).To(BeTrue()) | ||
| Expect(isLinkUnsupportedError(&os.LinkError{ | ||
| Op: "symlink", | ||
| Old: "libcublas.so.12", | ||
| New: "/backends/lib/libcublas.so", | ||
| Err: syscall.ENOTSUP, | ||
| })).To(BeTrue()) | ||
| }) | ||
|
|
||
| It("does not misclassify unrelated errors", func() { | ||
| Expect(isLinkUnsupportedError(os.ErrNotExist)).To(BeFalse()) | ||
| Expect(isLinkUnsupportedError(syscall.ENOSPC)).To(BeFalse()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("safeJoin", func() { | ||
| It("keeps entries inside the root", func() { | ||
| root := "/tmp/extract-root" | ||
| p, err := safeJoin(root, "lib/libcublas.so") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(p).To(Equal(filepath.Join(root, "lib/libcublas.so"))) | ||
| }) | ||
|
|
||
| It("rejects path traversal entries", func() { | ||
| _, err := safeJoin("/tmp/extract-root", "../../etc/passwd") | ||
| Expect(err).To(HaveOccurred()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("extractTarCopyingLinks", func() { | ||
| It("preserves symlinks when the filesystem supports them", func() { | ||
| dir := GinkgoT().TempDir() | ||
| Expect(extractTarCopyingLinks(bytes.NewReader(buildTar()), dir)).To(Succeed()) | ||
|
|
||
| linkPath := filepath.Join(dir, "lib", "libcublas.so") | ||
| fi, err := os.Lstat(linkPath) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(fi.Mode() & os.ModeSymlink).NotTo(BeZero()) | ||
|
|
||
| data, err := os.ReadFile(linkPath) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(string(data)).To(Equal("real library bytes")) | ||
| }) | ||
|
|
||
| It("copies the target when symlink creation is unsupported", func() { | ||
| // Simulate a CIFS/SMB mount: symlink() reports ENOTSUP. | ||
| origSymlink := symlink | ||
| symlink = func(string, string) error { return syscall.ENOTSUP } | ||
| DeferCleanup(func() { symlink = origSymlink }) | ||
|
|
||
| dir := GinkgoT().TempDir() | ||
| Expect(extractTarCopyingLinks(bytes.NewReader(buildTar()), dir)).To(Succeed()) | ||
|
|
||
| linkPath := filepath.Join(dir, "lib", "libcublas.so") | ||
| fi, err := os.Lstat(linkPath) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| // The entry must now be a real, regular file (a copy), not a symlink. | ||
| Expect(fi.Mode() & os.ModeSymlink).To(BeZero()) | ||
| Expect(fi.Mode().IsRegular()).To(BeTrue()) | ||
|
|
||
| data, err := os.ReadFile(linkPath) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(string(data)).To(Equal("real library bytes")) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.