Files
wehub-resource-sync e0e362d700
SDK Tests / changes (push) Successful in 2m29s
Real E2E Tests / changes (push) Successful in 2m29s
Deploy Docs Pages / build (push) Has been cancelled
Deploy Docs Pages / deploy (push) Has been cancelled
Real E2E Tests / JavaScript E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Python E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Java E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / C# E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Go E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Real E2E CI (push) Has been cancelled
SDK Tests / SDK CI (push) Has been cancelled
SDK Tests / CLI Tests (push) Has been cancelled
SDK Tests / Python SDK Quality (code-interpreter) (push) Has been cancelled
SDK Tests / Python SDK Quality (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (sandbox) (push) Has been cancelled
SDK Tests / CLI Quality (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Go SDK Quality And Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:33 +08:00

104 lines
3.0 KiB
Go

// 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
}