mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
refactor: Sandbox for separation of boundaries
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package platform
|
||||
|
||||
import "github.com/safedep/pmg/sandbox"
|
||||
|
||||
// NewSandbox creates a platform-specific sandbox instance for macOS.
|
||||
// Uses Seatbelt (sandbox-exec) for process isolation.
|
||||
func NewSandbox() (sandbox.Sandbox, error) {
|
||||
return newSeatbeltSandbox()
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package platform
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/safedep/pmg/sandbox"
|
||||
)
|
||||
|
||||
// NewSandbox creates a platform-specific sandbox instance for Linux.
|
||||
// TODO: Implement Bubblewrap or seccomp-bpf based sandbox.
|
||||
func NewSandbox() (sandbox.Sandbox, error) {
|
||||
return nil, errors.New("sandbox not yet implemented for Linux")
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build !darwin && !linux && !windows
|
||||
// +build !darwin,!linux,!windows
|
||||
|
||||
package platform
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/safedep/pmg/sandbox"
|
||||
)
|
||||
|
||||
// NewSandbox returns an error on unsupported platforms.
|
||||
func NewSandbox() (sandbox.Sandbox, error) {
|
||||
return nil, errors.New("sandbox not supported on this platform")
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package platform
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/safedep/pmg/sandbox"
|
||||
)
|
||||
|
||||
// NewSandbox creates a platform-specific sandbox instance for Windows.
|
||||
// TODO: Implement AppContainer or Job Objects based sandbox.
|
||||
func NewSandbox() (sandbox.Sandbox, error) {
|
||||
return nil, errors.New("sandbox not yet implemented for Windows")
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/sandbox"
|
||||
)
|
||||
|
||||
// seatbeltSandbox implements the Sandbox interface using macOS Seatbelt (sandbox-exec).
|
||||
type seatbeltSandbox struct {
|
||||
translator *policyTranslator
|
||||
}
|
||||
|
||||
// newSeatbeltSandbox creates a new Seatbelt sandbox instance.
|
||||
func newSeatbeltSandbox() (*seatbeltSandbox, error) {
|
||||
return &seatbeltSandbox{
|
||||
translator: newPolicyTranslator(),
|
||||
}, 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) {
|
||||
// Translate PMG policy to Seatbelt profile
|
||||
sbProfile, err := s.translator.translate(policy)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to translate sandbox policy: %w", err)
|
||||
}
|
||||
|
||||
// Write Seatbelt profile to temporary file
|
||||
tmpFile, err := os.CreateTemp("", "pmg-sandbox-*.sb")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temporary sandbox profile: %w", err)
|
||||
}
|
||||
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
if _, err := tmpFile.WriteString(sbProfile); err != nil {
|
||||
tmpFile.Close()
|
||||
return nil, fmt.Errorf("failed to write sandbox profile: %w", err)
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
log.Debugf("Seatbelt profile written to %s", tmpFile.Name())
|
||||
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",
|
||||
"-f", tmpFile.Name(),
|
||||
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 ExecutionResult indicating we only modified cmd, didn't execute it
|
||||
return sandbox.NewExecutionResult(false), nil
|
||||
}
|
||||
|
||||
// Name returns the name of this sandbox implementation.
|
||||
func (s *seatbeltSandbox) Name() string {
|
||||
return "seatbelt"
|
||||
}
|
||||
|
||||
// IsAvailable returns true if sandbox-exec is available on this system.
|
||||
func (s *seatbeltSandbox) IsAvailable() bool {
|
||||
_, err := exec.LookPath("sandbox-exec")
|
||||
return err == nil
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package platform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/sandbox/util"
|
||||
)
|
||||
|
||||
// policyTranslator translates PMG sandbox policies to Seatbelt Profile Language (.sb).
|
||||
type policyTranslator struct{}
|
||||
|
||||
// newPolicyTranslator creates a new policy translator.
|
||||
func newPolicyTranslator() *policyTranslator {
|
||||
return &policyTranslator{}
|
||||
}
|
||||
|
||||
// translate converts a PMG SandboxPolicy to Seatbelt Profile Language.
|
||||
func (t *policyTranslator) translate(policy *sandbox.SandboxPolicy) (string, error) {
|
||||
var sb strings.Builder
|
||||
|
||||
// Header
|
||||
sb.WriteString("(version 1)\n")
|
||||
sb.WriteString(fmt.Sprintf(";; PMG Sandbox Policy: %s\n", policy.Name))
|
||||
sb.WriteString(fmt.Sprintf(";; %s\n", policy.Description))
|
||||
sb.WriteString(";; Generated by PMG sandbox system\n\n")
|
||||
|
||||
// Default policy: deny by default for maximum security
|
||||
sb.WriteString("(deny default)\n\n")
|
||||
|
||||
// Allow basic system operations required for any process
|
||||
sb.WriteString(";; Basic system access\n")
|
||||
sb.WriteString("(allow process-fork)\n")
|
||||
sb.WriteString("(allow process-exec-interpreter)\n")
|
||||
sb.WriteString("(allow sysctl-read)\n")
|
||||
sb.WriteString("(allow mach-lookup)\n")
|
||||
sb.WriteString("(allow mach-register)\n")
|
||||
sb.WriteString("(allow ipc-posix-shm)\n")
|
||||
sb.WriteString("(allow signal)\n\n")
|
||||
|
||||
// Filesystem rules
|
||||
if err := t.translateFilesystem(policy, &sb); err != nil {
|
||||
return "", fmt.Errorf("failed to translate filesystem rules: %w", err)
|
||||
}
|
||||
|
||||
// Network rules
|
||||
if err := t.translateNetwork(policy, &sb); err != nil {
|
||||
return "", fmt.Errorf("failed to translate network rules: %w", err)
|
||||
}
|
||||
|
||||
// Process execution rules
|
||||
if err := t.translateProcess(policy, &sb); err != nil {
|
||||
return "", fmt.Errorf("failed to translate process rules: %w", err)
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
// translateFilesystem translates filesystem access rules.
|
||||
func (t *policyTranslator) translateFilesystem(policy *sandbox.SandboxPolicy, sb *strings.Builder) error {
|
||||
sb.WriteString(";; Filesystem access\n")
|
||||
|
||||
// Expand and add allow read rules
|
||||
for _, pattern := range policy.Filesystem.AllowRead {
|
||||
expanded, err := util.ExpandVariables(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to expand pattern %s: %w", pattern, err)
|
||||
}
|
||||
|
||||
// Handle glob patterns vs literal paths
|
||||
if util.ContainsGlob(expanded) {
|
||||
// For glob patterns, use subpath with the base directory
|
||||
baseDir := filepath.Dir(strings.TrimSuffix(expanded, "/**"))
|
||||
sb.WriteString(fmt.Sprintf("(allow file-read* (subpath \"%s\"))\n", baseDir))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("(allow file-read* (subpath \"%s\"))\n", expanded))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Expand and add allow write rules
|
||||
for _, pattern := range policy.Filesystem.AllowWrite {
|
||||
expanded, err := util.ExpandVariables(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to expand pattern %s: %w", pattern, err)
|
||||
}
|
||||
|
||||
if util.ContainsGlob(expanded) {
|
||||
baseDir := filepath.Dir(strings.TrimSuffix(expanded, "/**"))
|
||||
sb.WriteString(fmt.Sprintf("(allow file-write* (subpath \"%s\"))\n", baseDir))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("(allow file-write* (subpath \"%s\"))\n", expanded))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Deny rules have higher priority (applied after allow)
|
||||
// Note: Seatbelt evaluates rules in order, so denies after allows will override
|
||||
for _, pattern := range policy.Filesystem.DenyRead {
|
||||
expanded, err := util.ExpandVariables(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to expand pattern %s: %w", pattern, err)
|
||||
}
|
||||
|
||||
if util.ContainsGlob(expanded) {
|
||||
baseDir := filepath.Dir(strings.TrimSuffix(expanded, "/**"))
|
||||
sb.WriteString(fmt.Sprintf("(deny file-read* (subpath \"%s\"))\n", baseDir))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("(deny file-read* (subpath \"%s\"))\n", expanded))
|
||||
}
|
||||
}
|
||||
|
||||
for _, pattern := range policy.Filesystem.DenyWrite {
|
||||
expanded, err := util.ExpandVariables(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to expand pattern %s: %w", pattern, err)
|
||||
}
|
||||
|
||||
if util.ContainsGlob(expanded) {
|
||||
baseDir := filepath.Dir(strings.TrimSuffix(expanded, "/**"))
|
||||
sb.WriteString(fmt.Sprintf("(deny file-write* (subpath \"%s\"))\n", baseDir))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("(deny file-write* (subpath \"%s\"))\n", expanded))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// translateNetwork translates network access rules.
|
||||
func (t *policyTranslator) translateNetwork(policy *sandbox.SandboxPolicy, sb *strings.Builder) error {
|
||||
sb.WriteString(";; Network access\n")
|
||||
|
||||
// If there are allow outbound rules, allow network-outbound generally
|
||||
// (Seatbelt doesn't support fine-grained host:port filtering in all cases)
|
||||
// Note: This is a limitation of Seatbelt - for more fine-grained control,
|
||||
// consider using a network filtering solution or firewall rules
|
||||
if len(policy.Network.AllowOutbound) > 0 {
|
||||
sb.WriteString(";; Network outbound allowed to specific hosts\n")
|
||||
sb.WriteString(";; Note: Seatbelt has limited host-based filtering, consider using firewall rules for strict control\n")
|
||||
sb.WriteString("(allow network-outbound)\n")
|
||||
}
|
||||
|
||||
// If deny outbound includes "*:*", block all network
|
||||
for _, pattern := range policy.Network.DenyOutbound {
|
||||
if pattern == "*:*" {
|
||||
sb.WriteString(";; Network outbound denied\n")
|
||||
sb.WriteString("(deny network-outbound)\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// translateProcess translates process execution rules.
|
||||
func (t *policyTranslator) translateProcess(policy *sandbox.SandboxPolicy, sb *strings.Builder) error {
|
||||
sb.WriteString(";; Process execution\n")
|
||||
|
||||
// Add allow exec rules
|
||||
for _, exePath := range policy.Process.AllowExec {
|
||||
expanded, err := util.ExpandVariables(exePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to expand exec path %s: %w", exePath, err)
|
||||
}
|
||||
|
||||
if util.ContainsGlob(expanded) {
|
||||
// For glob patterns, use subpath to allow anything under that directory
|
||||
baseDir := filepath.Dir(strings.TrimSuffix(expanded, "/**"))
|
||||
sb.WriteString(fmt.Sprintf("(allow process-exec* (subpath \"%s\"))\n", baseDir))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("(allow process-exec* (literal \"%s\"))\n", expanded))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Add deny exec rules
|
||||
for _, exePath := range policy.Process.DenyExec {
|
||||
expanded, err := util.ExpandVariables(exePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to expand exec path %s: %w", exePath, err)
|
||||
}
|
||||
|
||||
if util.ContainsGlob(expanded) {
|
||||
baseDir := filepath.Dir(strings.TrimSuffix(expanded, "/**"))
|
||||
sb.WriteString(fmt.Sprintf("(deny process-exec* (subpath \"%s\"))\n", baseDir))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("(deny process-exec* (literal \"%s\"))\n", expanded))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user