-
Notifications
You must be signed in to change notification settings - Fork 679
feat(execd): add EXECD_CLONE3_COMPAT seccomp-based clone3 fallback #518
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
Pangjiping
wants to merge
2
commits into
alibaba:main
Choose a base branch
from
Pangjiping:fix/execd-clone3
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.
+297
−1
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
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,27 @@ | ||
| // Copyright 2026 Alibaba Group Holding Ltd. | ||
| // | ||
| // 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. | ||
|
|
||
| // Package clone3compat installs an optional seccomp rule so clone3(2) returns ENOSYS and | ||
| // libc / the Go runtime fall back to clone(2), following the same idea as | ||
| // https://github.com/AkihiroSuda/clone3-workaround . | ||
| // | ||
| // Enable on Linux via environment when starting execd: | ||
| // | ||
| // EXECD_CLONE3_COMPAT=1 — install the filter at process start (after Go runtime init). | ||
| // EXECD_CLONE3_COMPAT=reexec — install the filter then re-exec the same binary so all | ||
| // package init code runs with the filter already active | ||
| // (closest to wrapping with the external clone3-workaround binary). | ||
| // | ||
| // Disabled when unset or empty, or set to 0, false, off, no. | ||
| package clone3compat |
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,103 @@ | ||
| // Copyright 2026 Alibaba Group Holding Ltd. | ||
| // | ||
| // 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. | ||
|
|
||
| //go:build linux | ||
|
|
||
| package clone3compat | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "syscall" | ||
|
|
||
| seccomp "github.com/elastic/go-seccomp-bpf" | ||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| const ( | ||
| envCompat = "EXECD_CLONE3_COMPAT" | ||
| envApplied = "_EXECD_CLONE3_COMPAT_APPLIED" | ||
| ) | ||
|
|
||
| // MaybeApply optionally installs a seccomp rule so clone3 returns ENOSYS, matching the | ||
| // behavior of https://github.com/AkihiroSuda/clone3-workaround . | ||
| // It returns true if this process is running with that compatibility active (including | ||
| // a post-reexec process that inherited the seccomp filter). | ||
| func MaybeApply() bool { | ||
| mode := strings.ToLower(strings.TrimSpace(os.Getenv(envCompat))) | ||
| switch mode { | ||
| case "", "0", "false", "off", "no": | ||
| return false | ||
| case "1", "true", "yes", "on": | ||
| if err := loadClone3EnosysFilter(); err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "execd: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| return true | ||
| case "reexec": | ||
| if os.Getenv(envApplied) == "1" { | ||
| return true | ||
| } | ||
| if err := loadClone3EnosysFilter(); err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "execd: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| if err := os.Setenv(envApplied, "1"); err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "execd: clone3 compat: set %s: %v\n", envApplied, err) | ||
| os.Exit(1) | ||
| } | ||
| exe, err := os.Readlink("/proc/self/exe") | ||
| if err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "execd: clone3 compat: readlink /proc/self/exe: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| exe = strings.TrimSuffix(exe, " (deleted)") | ||
| if err := unix.Exec(exe, os.Args, os.Environ()); err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "execd: clone3 compat: exec: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| panic("unreachable") // Exec replaces this process. | ||
| default: | ||
| _, _ = fmt.Fprintf(os.Stderr, "execd: invalid %s=%q (use 1, true, or reexec)\n", envCompat, os.Getenv(envCompat)) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| func loadClone3EnosysFilter() error { | ||
| if !seccomp.Supported() { | ||
| return errors.New("clone3 compat: seccomp is not available on this kernel") | ||
| } | ||
| f := seccomp.Filter{ | ||
| NoNewPrivs: true, | ||
| Flag: seccomp.FilterFlagTSync, | ||
| Policy: seccomp.Policy{ | ||
| DefaultAction: seccomp.ActionAllow, | ||
| Syscalls: []seccomp.SyscallGroup{ | ||
| { | ||
| Names: []string{"clone3"}, | ||
| // Not plain ActionErrno: assembler defaults errno to EPERM. | ||
| Action: seccomp.ActionErrno | seccomp.Action(syscall.ENOSYS), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| if err := seccomp.LoadFilter(f); err != nil { | ||
| return fmt.Errorf("clone3 compat: %w", err) | ||
| } | ||
| 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,20 @@ | ||
| // Copyright 2026 Alibaba Group Holding Ltd. | ||
| // | ||
| // 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. | ||
|
|
||
| //go:build !linux | ||
|
|
||
| package clone3compat | ||
|
|
||
| // MaybeApply is a no-op on non-Linux platforms. | ||
| func MaybeApply() bool { return false } |
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
Oops, something went wrong.
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.