|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 - 2022 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | + */ |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import TSCBasic |
| 13 | +import TSCUtility |
| 14 | + |
| 15 | +/// A sandbox profile representing the desired restrictions. The implementation can vary between platforms. |
| 16 | +public struct SandboxProfile { |
| 17 | + /// Whether to allow outbound and inbound network access. |
| 18 | + public var allowNetwork: Bool |
| 19 | + |
| 20 | + /// Whether to allow writing to system-defined temporary directories. Overridden by any rules in `pathRules`. |
| 21 | + public var allowWritingToTemporaryDirectories: Bool |
| 22 | + |
| 23 | + /// An ordered list of path rules, where the last rule to cover a particular path "wins". These will be resolved |
| 24 | + /// to absolute paths at the time the profile is applied. They are applied after any of the implicit directories |
| 25 | + /// referenced by other sandbox profile settings. |
| 26 | + public var pathRules: [PathWritabilityRule] |
| 27 | + |
| 28 | + /// Represents a readonly/writable rule for a path and everything under it. |
| 29 | + public enum PathWritabilityRule: Equatable { |
| 30 | + case readonly(AbsolutePath) |
| 31 | + case writable(AbsolutePath) |
| 32 | + } |
| 33 | + |
| 34 | + /// Configures a SandboxProfile for blocking network access and writing to the file system except to specifically |
| 35 | + /// permitted locations. |
| 36 | + public init( |
| 37 | + allowNetwork: Bool = false, |
| 38 | + allowWritingToTemporaryDirectories: Bool = false, |
| 39 | + pathRules: [PathWritabilityRule] = [] |
| 40 | + ) { |
| 41 | + self.allowNetwork = allowNetwork |
| 42 | + self.allowWritingToTemporaryDirectories = allowWritingToTemporaryDirectories |
| 43 | + self.pathRules = pathRules |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +extension SandboxProfile { |
| 48 | + /// Applies the sandbox profile to the given command line (if the platform supports it), and returns the modified |
| 49 | + /// command line. On platforms that don't support sandboxing, the unmodified command line is returned. |
| 50 | + public func apply(to command: [String]) -> [String] { |
| 51 | + #if os(macOS) |
| 52 | + return ["/usr/bin/sandbox-exec", "-p", self.generateMacOSSandboxProfileString()] + command |
| 53 | + #else |
| 54 | + // rdar://40235432, rdar://75636874 tracks implementing sandboxes for other platforms. |
| 55 | + return command |
| 56 | + #endif |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// MARK: - macOS |
| 61 | + |
| 62 | +#if os(macOS) |
| 63 | +fileprivate extension SandboxProfile { |
| 64 | + /// Private function that generates a Darwin sandbox profile suitable for passing to `sandbox-exec(1)`. |
| 65 | + func generateMacOSSandboxProfileString() -> String { |
| 66 | + var contents = "(version 1)\n" |
| 67 | + |
| 68 | + // Deny everything by default. |
| 69 | + contents += "(deny default)\n" |
| 70 | + |
| 71 | + // Import the system sandbox profile. |
| 72 | + contents += "(import \"system.sb\")\n" |
| 73 | + |
| 74 | + // Allow reading any file that isn't protected by TCC or permissions (ideally we'd only allow a specific set |
| 75 | + // of readable locations, and can maybe tighten this in the future). |
| 76 | + contents += "(allow file-read*)\n" |
| 77 | + |
| 78 | + // Allow operations on subprocesses. |
| 79 | + contents += "(allow process*)\n" |
| 80 | + |
| 81 | + // Optionally allow network access (inbound and outbound). |
| 82 | + if allowNetwork { |
| 83 | + contents += "(system-network)\n" |
| 84 | + contents += "(allow network*)\n" |
| 85 | + } |
| 86 | + |
| 87 | + // Optionally allow writing to the platform-specific temporary directories. |
| 88 | + if allowWritingToTemporaryDirectories { |
| 89 | + contents += "(allow file-write*\n" |
| 90 | + for path in temporaryDirectories { |
| 91 | + contents += " (subpath \(resolveSymlinksAndQuotePath(path)))\n" |
| 92 | + } |
| 93 | + contents += ")\n" |
| 94 | + } |
| 95 | + |
| 96 | + // Apply customized rules for specific file system locations. Everything is readonly by default, so we just |
| 97 | + // either allow or deny writing, in order. Later rules have precedence over earlier rules. |
| 98 | + for rule in pathRules { |
| 99 | + switch rule { |
| 100 | + case .readonly(let path): |
| 101 | + contents += "(deny file-write* (subpath \(resolveSymlinksAndQuotePath(path))))\n" |
| 102 | + case .writable(let path): |
| 103 | + contents += "(allow file-write* (subpath \(resolveSymlinksAndQuotePath(path))))\n" |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return contents |
| 108 | + } |
| 109 | + |
| 110 | + /// Private helper function that returns the temporary directories on macOS. |
| 111 | + var temporaryDirectories: [AbsolutePath] { |
| 112 | + return [AbsolutePath("/tmp"), AbsolutePath(NSTemporaryDirectory())] |
| 113 | + } |
| 114 | + |
| 115 | + /// Private helper function that resolves an AbsolutePath and returns it as a string quoted for use as a subpath |
| 116 | + /// in a .sb sandbox profile. |
| 117 | + func resolveSymlinksAndQuotePath(_ path: AbsolutePath) -> String { |
| 118 | + return "\"" + resolveSymlinks(path).pathString |
| 119 | + .replacingOccurrences(of: "\\", with: "\\\\") |
| 120 | + .replacingOccurrences(of: "\"", with: "\\\"") |
| 121 | + + "\"" |
| 122 | + } |
| 123 | +} |
| 124 | +#endif |
0 commit comments