Skip to content

Commit b66dd64

Browse files
committed
Add Seccomp support
This change is rather large, but I think it's simpler to get in as one unit. It: - Adds a new ContainerizationSeccomp target/product that is a cBPF compiler specifically for seccomp. Its main use is to take in an OCI seccomp description and spit out a filter we can apply. - Adds a new friendly SeccompProfile API to Containerization to specify what filters you'd like applied. This will (as is the case for basically everything else) get translated to OCI behind the scenes. - Adds a small bit of logic in vmexec to apply the filters. And unit and integration tests for everything. Unit testing is interesting. I've added a small simulator so we actually have some semblance of testing outside of just integration tests and seeing if the syscall is blocked/returns whatever.
1 parent a59ed89 commit b66dd64

17 files changed

Lines changed: 2514 additions & 18 deletions

File tree

Package.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let package = Package(
3333
.library(name: "ContainerizationOS", targets: ["ContainerizationOS"]),
3434
.library(name: "ContainerizationExtras", targets: ["ContainerizationExtras"]),
3535
.library(name: "ContainerizationArchive", targets: ["ContainerizationArchive"]),
36+
.library(name: "ContainerizationSeccomp", targets: ["ContainerizationSeccomp"]),
3637
.executable(name: "cctl", targets: ["cctl"]),
3738
],
3839
dependencies: [
@@ -264,5 +265,18 @@ let package = Package(
264265
.target(
265266
name: "CShim"
266267
),
268+
.target(
269+
name: "ContainerizationSeccomp",
270+
dependencies: [
271+
"ContainerizationOCI"
272+
]
273+
),
274+
.testTarget(
275+
name: "ContainerizationSeccompTests",
276+
dependencies: [
277+
"ContainerizationSeccomp",
278+
"ContainerizationOCI",
279+
]
280+
),
267281
]
268282
)

Sources/Containerization/LinuxContainer.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public final class LinuxContainer: Container, Sendable {
6464
public var sockets: [UnixSocketConfiguration] = []
6565
/// The mounts for the container.
6666
public var mounts: [Mount] = LinuxContainer.defaultMounts()
67+
/// Seccomp profile for system call filtering.
68+
public var seccomp: SeccompProfile?
6769
/// The DNS configuration for the container.
6870
public var dns: DNS?
6971
/// The hosts to add to /etc/hosts for the container.
@@ -90,6 +92,7 @@ public final class LinuxContainer: Container, Sendable {
9092
interfaces: [any Interface] = [],
9193
sockets: [UnixSocketConfiguration] = [],
9294
mounts: [Mount] = LinuxContainer.defaultMounts(),
95+
seccomp: SeccompProfile? = nil,
9396
dns: DNS? = nil,
9497
hosts: Hosts? = nil,
9598
virtualization: Bool = false,
@@ -105,6 +108,7 @@ public final class LinuxContainer: Container, Sendable {
105108
self.interfaces = interfaces
106109
self.sockets = sockets
107110
self.mounts = mounts
111+
self.seccomp = seccomp
108112
self.dns = dns
109113
self.hosts = hosts
110114
self.virtualization = virtualization
@@ -356,6 +360,7 @@ public final class LinuxContainer: Container, Sendable {
356360

357361
// Linux toggles.
358362
spec.linux?.sysctl = config.sysctl
363+
spec.linux?.seccomp = config.seccomp?.toOCI(effectiveCapabilities: config.process.capabilities.effective)
359364

360365
// If the rootfs was requested as read-only, set it in the OCI spec.
361366
// We let the OCI runtime remount as ro, instead of doing it originally.

Sources/Containerization/LinuxPod.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public final class LinuxPod: Sendable {
7878
public var sysctl: [String: String] = [:]
7979
/// The mounts for the container.
8080
public var mounts: [Mount] = LinuxContainer.defaultMounts()
81+
/// Seccomp profile for system call filtering.
82+
public var seccomp: SeccompProfile?
8183
/// The Unix domain socket relays to setup for the container.
8284
public var sockets: [UnixSocketConfiguration] = []
8385
/// The DNS configuration for the container.
@@ -230,6 +232,7 @@ public final class LinuxPod: Sendable {
230232

231233
// Linux toggles
232234
spec.linux?.sysctl = config.sysctl
235+
spec.linux?.seccomp = config.seccomp?.toOCI(effectiveCapabilities: config.process.capabilities.effective)
233236

234237
// If the rootfs was requested as read-only, set it in the OCI spec.
235238
// We let the OCI runtime remount as ro, instead of doing it originally.

0 commit comments

Comments
 (0)