Files
pmg/sandbox/platform/seatbelt_darwin.go
T

116 lines
3.1 KiB
Go
Raw Normal View History

2026-01-08 13:40:22 +05:30
//go:build darwin
// +build darwin
package platform
2026-01-08 13:40:22 +05:30
import (
"context"
"fmt"
"os"
"os/exec"
"github.com/safedep/dry/log"
"github.com/safedep/pmg/sandbox"
2026-01-08 13:40:22 +05:30
)
type seatbeltSandbox struct {
2026-01-08 16:32:49 +05:30
translator *seatbeltPolicyTranslator
tempProfilePath string
cleanupCompleted bool
2026-01-08 13:40:22 +05:30
}
func newSeatbeltSandbox() (*seatbeltSandbox, error) {
return &seatbeltSandbox{
2026-01-08 16:32:49 +05:30
translator: newSeatbeltPolicyTranslator(),
2026-01-08 13:40:22 +05:30
}, 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) (*sandbox.ExecutionResult, error) {
sbProfile, err := s.translator.translate(policy)
2026-01-08 13:40:22 +05:30
if err != nil {
return nil, fmt.Errorf("failed to translate sandbox policy: %w", err)
2026-01-08 13:40:22 +05:30
}
tmpFile, err := os.CreateTemp("", "pmg-sandbox-*.sb")
if err != nil {
return nil, fmt.Errorf("failed to create temporary sandbox profile: %w", err)
2026-01-08 13:40:22 +05:30
}
2026-01-08 16:32:49 +05:30
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()
2026-01-08 14:30:33 +05:30
s.tempProfilePath = tmpFile.Name()
2026-01-08 13:40:22 +05:30
if _, err := tmpFile.WriteString(sbProfile); err != nil {
2026-01-08 16:32:49 +05:30
if err := os.Remove(s.tempProfilePath); err != nil {
log.Warnf("failed to remove temporary sandbox profile: %v", err)
}
2026-01-08 14:30:33 +05:30
s.tempProfilePath = ""
return nil, fmt.Errorf("failed to write sandbox profile: %w", err)
2026-01-08 13:40:22 +05:30
}
2026-01-08 14:30:33 +05:30
log.Debugf("Seatbelt profile written to %s", s.tempProfilePath)
2026-01-08 13:40:22 +05:30
log.Debugf("Seatbelt profile content:\n%s", 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",
2026-01-08 14:30:33 +05:30
"-f", s.tempProfilePath,
2026-01-08 13:40:22 +05:30
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)
2026-01-08 16:17:59 +05:30
return sandbox.NewExecutionResult(sandbox.WithExecutionResultSandbox(s)), nil
2026-01-08 13:40:22 +05:30
}
// Name returns the name of this sandbox implementation.
func (s *seatbeltSandbox) Name() string {
2026-01-08 13:40:22 +05:30
return "seatbelt"
}
// IsAvailable returns true if sandbox-exec is available on this system.
func (s *seatbeltSandbox) IsAvailable() bool {
2026-01-08 13:40:22 +05:30
_, err := exec.LookPath("sandbox-exec")
return err == nil
}
2026-01-08 14:30:33 +05:30
// 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
}