mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* feat(sandbox): Seatbelt lockdown translation confines outbound to the PMG proxy Under network_via_proxy_only the Seatbelt profile now denies all network-outbound (with a target=direct violation marker) and allows only the loopback proxy port. SBPL is last-match-wins, so the broad deny is emitted first, specific allows after, and the allow_network_bind rules last — keeping loopback-to-loopback dev traffic working under lockdown. allow_direct_dns re-opens the /var/run/mDNSResponder unix socket that the deny otherwise covers. Replaces the temporary fail-closed rejection in the Seatbelt driver with the real translation; non-lockdown profiles translate byte-identically to before. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqMU5GNBbQvQct9nxek1VS * test(sandbox): assert deny marker presence before ordering comparison Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqMU5GNBbQvQct9nxek1VS * feat(sandbox): render lockdown profiles without a proxy as deny-only with runtime note pmg sandbox profile show renders profiles for debugging and must not fail on lockdown profiles. Without a running proxy the translator keeps the broad deny (rendered profile stays fail-closed, never looser than runtime) and documents the runtime-only proxy-port allow in an SBPL comment instead of fabricating a port. Execution is unaffected: the driver validates the proxy address before translating. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqMU5GNBbQvQct9nxek1VS --------- Co-authored-by: Claude <noreply@anthropic.com>
144 lines
3.9 KiB
Go
144 lines
3.9 KiB
Go
//go:build darwin
|
|
// +build darwin
|
|
|
|
package platform
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"time"
|
|
|
|
"github.com/safedep/dry/log"
|
|
"github.com/safedep/pmg/sandbox"
|
|
)
|
|
|
|
type seatbeltSandbox struct {
|
|
translator *seatbeltPolicyTranslator
|
|
tempProfilePath string
|
|
cleanupCompleted bool
|
|
policyName string
|
|
logTag string
|
|
startedAt time.Time
|
|
}
|
|
|
|
func newSeatbeltSandbox() (*seatbeltSandbox, error) {
|
|
return &seatbeltSandbox{
|
|
translator: newSeatbeltPolicyTranslator(),
|
|
}, nil
|
|
}
|
|
|
|
// Execute runs a command in the Seatbelt sandbox with the given policy.
|
|
// It translates the PMG policy to Seatbelt Profile Language (.sb) and wraps
|
|
// the command execution with sandbox-exec.
|
|
//
|
|
// This implementation modifies the cmd in place and does NOT execute it.
|
|
// Returns ExecutionResult with executed=false, indicating the caller must run cmd.Run().
|
|
func (s *seatbeltSandbox) Execute(ctx context.Context, cmd *exec.Cmd, policy *sandbox.SandboxPolicy, rt *sandbox.ExecutionContext) (*sandbox.ExecutionResult, error) {
|
|
if _, err := sandbox.ValidateNetworkLockdown(policy, rt); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sbProfile, err := s.translator.translate(policy, rt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to translate sandbox policy: %w", err)
|
|
}
|
|
|
|
// Log pattern referred in sandbox.md as debugging guidance
|
|
s.logTag = s.translator.LogTag()
|
|
s.policyName = policy.Name
|
|
s.startedAt = time.Now()
|
|
|
|
log.Debugf("MacOS Seatbelt sandbox log tag: %s", s.logTag)
|
|
|
|
tmpFile, err := os.CreateTemp("", "pmg-sandbox-*.sb")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create temporary sandbox profile: %w", err)
|
|
}
|
|
|
|
defer func() {
|
|
if err := tmpFile.Close(); err != nil {
|
|
log.Warnf("failed to close temporary sandbox profile: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Storing the path is required for cleanup in Close()
|
|
s.tempProfilePath = tmpFile.Name()
|
|
|
|
if _, err := tmpFile.WriteString(sbProfile); err != nil {
|
|
if err := os.Remove(s.tempProfilePath); err != nil {
|
|
log.Warnf("failed to remove temporary sandbox profile: %v", err)
|
|
}
|
|
|
|
s.tempProfilePath = ""
|
|
return nil, fmt.Errorf("failed to write sandbox profile: %w", err)
|
|
}
|
|
|
|
log.Debugf("Seatbelt profile written to %s", s.tempProfilePath)
|
|
|
|
debugLogPolicyContent(sbProfile)
|
|
|
|
// Modify command to run via sandbox-exec
|
|
originalPath := cmd.Path
|
|
originalArgs := cmd.Args
|
|
|
|
// sandbox-exec -f <profile> <command> <args...>
|
|
cmd.Path = "/usr/bin/sandbox-exec"
|
|
cmd.Args = []string{
|
|
"sandbox-exec",
|
|
"-f", s.tempProfilePath,
|
|
originalPath,
|
|
}
|
|
|
|
// Append original arguments (skip argv[0] which is the command itself)
|
|
if len(originalArgs) > 1 {
|
|
cmd.Args = append(cmd.Args, originalArgs[1:]...)
|
|
}
|
|
|
|
log.Debugf("Sandboxed command: %s %v", cmd.Path, cmd.Args)
|
|
|
|
return sandbox.NewExecutionResult(sandbox.WithExecutionResultSandbox(s)), nil
|
|
}
|
|
|
|
// Name returns the name of this sandbox implementation.
|
|
func (s *seatbeltSandbox) Name() sandbox.DriverName {
|
|
return sandbox.DriverSeatbelt
|
|
}
|
|
|
|
// IsAvailable returns true if sandbox-exec is available on this system.
|
|
func (s *seatbeltSandbox) IsAvailable() bool {
|
|
_, err := exec.LookPath("sandbox-exec")
|
|
return err == nil
|
|
}
|
|
|
|
// Close cleans up the temporary seatbelt profile file.
|
|
func (s *seatbeltSandbox) Close() error {
|
|
if s.cleanupCompleted || s.tempProfilePath == "" {
|
|
return nil
|
|
}
|
|
|
|
log.Debugf("Cleaning up seatbelt profile: %s", s.tempProfilePath)
|
|
|
|
err := os.Remove(s.tempProfilePath)
|
|
s.cleanupCompleted = true
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("failed to remove seatbelt profile %s: %w", s.tempProfilePath, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// debugLogPolicyContent logs the policy content when explicitly debugging is enabled.
|
|
func debugLogPolicyContent(content string) {
|
|
filePath := os.Getenv("PMG_SANDBOX_DEBUG_LOG_SEATBELT_POLICY_CONTENT")
|
|
if filePath == "" {
|
|
return
|
|
}
|
|
|
|
if err := os.WriteFile(filePath, []byte(content), 0600); err != nil {
|
|
log.Warnf("failed to write seatbelt policy content to %s: %v", filePath, err)
|
|
}
|
|
}
|