-
Notifications
You must be signed in to change notification settings - Fork 2.3k
libct/exeseal: add annotation to choose runc binary protection mechanism #5290
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
captainmo1
wants to merge
2
commits into
opencontainers:main
Choose a base branch
from
captainmo1:5272-exeseal-annotation
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.
+139
−30
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
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
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,85 @@ | ||
| package exeseal | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| // AnnotationKey is the OCI annotation that selects how runc protects | ||
| // the host runc binary against tampering. See ValidateMode for the | ||
| // recognized values. | ||
| const AnnotationKey = "org.opencontainers.runc.clone-self-exe" | ||
|
|
||
| const ( | ||
| ModeUnset = "" | ||
| ModeIndependentDataCopy = "independent-data-copy" | ||
| ModeROSharedPage = "ro-shared-page" | ||
| ) | ||
|
|
||
| // ValidateMode reports whether value is a recognized annotation value. | ||
| // | ||
| // Recognized values: | ||
| // - "": annotation absent; use the default fallback chain. | ||
| // - "independent-data-copy": use the clone-binary path (memfd, with | ||
| // an internal fallback to a classic unlinked tmpfile on older | ||
| // kernels). Sealed overlayfs is not attempted. | ||
| // - "ro-shared-page": use sealed overlayfs only; fail | ||
| // container creation if it is not available. | ||
| func ValidateMode(value string) error { | ||
| switch value { | ||
| case ModeUnset, ModeIndependentDataCopy, ModeROSharedPage: | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("invalid %s value %q (want %q or %q)", | ||
| AnnotationKey, value, | ||
| ModeIndependentDataCopy, ModeROSharedPage) | ||
| } | ||
| } | ||
|
|
||
| // strategy produces a sealed /proc/self/exe handle by one specific | ||
| // mechanism. Returns the file on success, or an error on failure. | ||
| type strategy func(tmpDir string) (*os.File, error) | ||
|
|
||
| func overlayfsStrategy(tmpDir string) (*os.File, error) { | ||
| f, err := sealedOverlayfs("/proc/self/exe", tmpDir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| logrus.Debug("runc exeseal: using overlayfs for sealed /proc/self/exe") // used for tests | ||
| return f, nil | ||
| } | ||
|
|
||
| func cloneBinaryStrategy(tmpDir string) (*os.File, error) { | ||
| selfExe, err := os.Open("/proc/self/exe") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("opening current binary: %w", err) | ||
| } | ||
| defer selfExe.Close() | ||
|
|
||
| stat, err := selfExe.Stat() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("checking /proc/self/exe size: %w", err) | ||
| } | ||
| logrus.Debug("runc exeseal: using clone-binary path") // used for tests | ||
| return CloneBinary(selfExe, stat.Size(), "/proc/self/exe", tmpDir) | ||
| } | ||
|
|
||
| // strategiesFor returns the ordered list of strategies to try for a | ||
| // given annotation value. The first successful strategy wins; if all | ||
| // fail, the last error is returned to the caller. | ||
| func strategiesFor(mode string) []strategy { | ||
| switch mode { | ||
| case ModeROSharedPage: | ||
| return []strategy{overlayfsStrategy} | ||
| case ModeIndependentDataCopy: | ||
| return []strategy{cloneBinaryStrategy} | ||
| case ModeUnset: | ||
| // Historical default: overlayfs first, clone-binary fallback. | ||
| // The order may be reversed in a future release. | ||
| return []strategy{overlayfsStrategy, cloneBinaryStrategy} | ||
| default: | ||
| return nil | ||
| } | ||
| } |
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,26 @@ | ||
| package exeseal | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestValidateMode(t *testing.T) { | ||
| cases := []struct { | ||
| desc string | ||
| in string | ||
| wantErr bool | ||
| }{ | ||
| {"absent (empty string) is valid", "", false}, | ||
| {"recognized: independent-data-copy", "independent-data-copy", false}, | ||
| {"recognized: ro-shared-page", "ro-shared-page", false}, | ||
| {"unrecognized errors", "not-a-real-mode", true}, | ||
| {"case-sensitive", "INDEPENDENT-DATA-COPY", true}, | ||
| {"no whitespace trimming", " independent-data-copy ", true}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.desc, func(t *testing.T) { | ||
| err := ValidateMode(tc.in) | ||
| if (err != nil) != tc.wantErr { | ||
| t.Errorf("ValidateMode(%q) err=%v, wantErr=%v", tc.in, err, tc.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
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.
I think if this was explicitly set, we probably want to skip the
exeseal.IsSelfExeClonedcheck -- at the very least, someone asking forindependent-data-copydoesn't want to share for any reason, even if they usedmemfd-bind.