-
Notifications
You must be signed in to change notification settings - Fork 25
fix: avoid CGO getgrgid_r segfault in static Linux binaries #433
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
nancysangani
wants to merge
1
commit into
modelpack:main
Choose a base branch
from
nancysangani:fix/segfault-static-binary-issue-285
base: main
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.
+38
−8
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -23,8 +23,39 @@ import ( | |
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "syscall" | ||
| ) | ||
|
|
||
| // buildTarHeader creates a tar header from FileInfo without using CGO-based | ||
| // os/user.LookupGroupId or os/user.LookupUserId. This avoids a segfault in | ||
| // statically linked CGO binaries caused by glibc NSS (getgrgid_r) being | ||
| // incompatible with static linking across different glibc versions. | ||
| // See: https://github.com/modelpack/modctl/issues/285 | ||
| func buildTarHeader(info os.FileInfo) (*tar.Header, error) { | ||
| header := &tar.Header{ | ||
| Name: info.Name(), | ||
| Size: info.Size(), | ||
| Mode: int64(info.Mode()), | ||
| ModTime: info.ModTime(), | ||
| } | ||
|
|
||
| // Set file type flag. | ||
| if info.IsDir() { | ||
| header.Typeflag = tar.TypeDir | ||
| } else { | ||
| header.Typeflag = tar.TypeReg | ||
| } | ||
|
|
||
| // Safely extract UID/GID from syscall.Stat_t without CGO user/group name lookup. | ||
| // We intentionally leave Uname/Gname empty to avoid os/user CGO calls entirely. | ||
| if stat, ok := info.Sys().(*syscall.Stat_t); ok { | ||
| header.Uid = int(stat.Uid) | ||
| header.Gid = int(stat.Gid) | ||
| } | ||
|
|
||
| return header, nil | ||
| } | ||
|
|
||
| // Tar creates a tar archive of the specified path (file or directory) | ||
| // and returns the content as a stream. For individual files, it preserves | ||
| // the directory structure relative to the working directory. | ||
|
|
@@ -56,7 +87,7 @@ func Tar(srcPath string, workDir string) (io.Reader, error) { | |
| return fmt.Errorf("failed to get relative path: %w", err) | ||
| } | ||
|
|
||
| header, err := tar.FileInfoHeader(info, "") | ||
| header, err := buildTarHeader(info) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if err != nil { | ||
| return fmt.Errorf("failed to create tar header: %w", err) | ||
| } | ||
|
|
@@ -95,14 +126,13 @@ func Tar(srcPath string, workDir string) (io.Reader, error) { | |
| } | ||
| defer file.Close() | ||
|
|
||
| header, err := tar.FileInfoHeader(info, "") | ||
| header, err := buildTarHeader(info) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if err != nil { | ||
| pw.CloseWithError(fmt.Errorf("failed to create tar header: %w", err)) | ||
| return | ||
| } | ||
|
|
||
| // Use relative path as the header name to preserve directory structure | ||
| // This keeps the directory structure as part of the file path in the tar. | ||
| // Use relative path as the header name to preserve directory structure. | ||
| relPath, err := filepath.Rel(workDir, srcPath) | ||
| if err != nil { | ||
| pw.CloseWithError(fmt.Errorf("failed to get relative path: %w", err)) | ||
|
|
@@ -189,9 +219,9 @@ func Untar(reader io.Reader, destPath string) error { | |
| } | ||
| file.Close() | ||
|
|
||
| // Set correct permissions for the directory. | ||
| // Set correct permissions for the file. | ||
| if err := os.Chmod(targetPath, os.FileMode(header.Mode)); err != nil { | ||
| return fmt.Errorf("failed to set directory permissions %s: %w", targetPath, err) | ||
| return fmt.Errorf("failed to set file permissions %s: %w", targetPath, err) | ||
| } | ||
| // Set modification time for the file. | ||
| if err := os.Chtimes(targetPath, header.ModTime, header.ModTime); err != nil { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
buildTarHeaderfunction is a good approach to avoid CGO issues. However, it has a couple of issues that represent a regression from usingtar.FileInfoHeader:info.Size()for a directory is system-dependent and typically non-zero, but tar headers for directories should have a size of 0.To address this and make the function more robust, I suggest modifying it to handle symlinks correctly and set sizes properly for different file types. This will require changing the function signature to accept the file path, which is needed to read a symlink's target. You will also need to update the call sites accordingly (see my other comments).