mirror of
https://github.com/apple/container.git
synced 2026-10-19 12:31:08 +00:00
`ExecCommand.execInNamespaces` entered a hardcoded `CLONE_NEWCGROUP | CLONE_NEWPID | CLONE_NEWUTS | CLONE_NEWNS`, while the init process gets its namespaces from the OCI spec via `RunCommand.setupNamespaces` — which includes `CLONE_NEWIPC` whenever the spec lists an `ipc` namespace. `LinuxContainer` (and `LinuxPod`) always declare one, so the init process runs in a private IPC namespace and every exec'd process stays in the guest's root one. The effect is that an exec cannot see SysV IPC segments or POSIX message queues created by the workload — and anything the exec creates leaks into the guest's root namespace, where it outlives the container. IPC-namespaced sysctls (`kernel.shm*`, `kernel.msg*`, `kernel.sem`, `fs.mqueue.*`) also read back as the guest default, because they resolve against the reading process's IPC namespace. That last one is what fails critest's "should support safe sysctls" and "should support unsafe sysctls", which set such a sysctl and read it back over ExecSync. Add `CLONE_NEWIPC` to the mask. `setns(2)` into a namespace the caller is already in is a no-op, so the mask is now a superset of everything `setupNamespaces` can unshare rather than a set that has to be kept in sync by hand. `CLONE_NEWUSER` stays out deliberately: nothing puts a container in a user namespace today, and entering one carries constraints that deserve their own change. The integration test asserts the general invariant rather than the specific sysctl: inside an exec, `/proc/self/ns/*` must equal `/proc/1/ns/*` for `ipc`, `uts`, `mnt`, `pid`, `cgroup`, and `net`. `kernel.shm_rmid_forced` is checked alongside it as the consumer-visible symptom. Signed-off-by: michael_crosby <michael_crosby@apple.com>
185 lines
7.2 KiB
Swift
185 lines
7.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
// Copyright © 2025-2026 Apple Inc. and the Containerization project authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import ArgumentParser
|
|
import ContainerizationOCI
|
|
import ContainerizationOS
|
|
import FoundationEssentials
|
|
import LCShim
|
|
import Logging
|
|
import SystemPackage
|
|
|
|
#if canImport(Musl)
|
|
import Musl
|
|
#elseif canImport(Glibc)
|
|
import Glibc
|
|
#endif
|
|
|
|
struct ExecCommand: ParsableCommand {
|
|
static let configuration = CommandConfiguration(
|
|
commandName: "exec",
|
|
abstract: "Exec in a container"
|
|
)
|
|
|
|
@Option(name: .long, help: "path to an OCI runtime spec process configuration")
|
|
var processPath: String
|
|
|
|
@Option(name: .long, help: "pid of the init process for the container")
|
|
var parentPid: Int
|
|
|
|
func run() throws {
|
|
do {
|
|
let src = URL(fileURLWithPath: processPath)
|
|
let processBytes = try Data(contentsOf: src)
|
|
let process = try JSONDecoder().decode(
|
|
ContainerizationOCI.Process.self,
|
|
from: processBytes
|
|
)
|
|
try execInNamespaces(process: process)
|
|
} catch {
|
|
App.writeError(error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
static func enterNS(pidFd: Int32, nsType: Int32) throws {
|
|
guard setns(pidFd, nsType) == 0 else {
|
|
throw App.Errno(stage: "setns(fd)")
|
|
}
|
|
}
|
|
|
|
private func execInNamespaces(process: ContainerizationOCI.Process) throws {
|
|
let syncPipe = FileDescriptor(rawValue: 3)
|
|
let ackPipe = FileDescriptor(rawValue: 4)
|
|
|
|
let pidFd = CZ_pidfd_open(Int32(parentPid), 0)
|
|
guard pidFd > 0 else {
|
|
throw App.Errno(stage: "pidfd_open(\(parentPid))")
|
|
}
|
|
// Join every namespace the container's init process could have been
|
|
// placed in by `RunCommand.setupNamespaces` (its `nsTypeToFlag` map).
|
|
// An exec that lands in a different namespace than the init process
|
|
// silently observes guest-root state instead of the container's — e.g.
|
|
// SysV IPC objects, POSIX message queues, and IPC-namespaced sysctls
|
|
// (`kernel.shm*`, `kernel.msg*`, `kernel.sem`, `fs.mqueue.*`) all read
|
|
// as the guest default without CLONE_NEWIPC here.
|
|
//
|
|
// setns(2) into a namespace the caller is already in is a no-op, so
|
|
// naming a flag the init process never unshared is harmless. Keeping
|
|
// this mask a superset of what `setupNamespaces` can unshare is what
|
|
// stops the two paths from drifting apart; add to it whenever that map
|
|
// grows.
|
|
//
|
|
// CLONE_NEWUSER is intentionally absent: nothing puts a container in a
|
|
// user namespace today, and entering one carries extra constraints
|
|
// (single-threaded caller, capabilities re-derived against the target
|
|
// namespace) that deserve their own change rather than riding along
|
|
// here.
|
|
try Self.enterNS(
|
|
pidFd: pidFd,
|
|
nsType: CLONE_NEWCGROUP | CLONE_NEWPID | CLONE_NEWUTS | CLONE_NEWNS | CLONE_NEWIPC
|
|
)
|
|
|
|
let processID = fork()
|
|
|
|
guard processID != -1 else {
|
|
try? syncPipe.close()
|
|
try? ackPipe.close()
|
|
|
|
throw App.Errno(stage: "fork")
|
|
}
|
|
|
|
if processID == 0 { // child
|
|
// Wait for the grandparent to tell us that they acked our pid.
|
|
var pidAckBuffer = [UInt8](repeating: 0, count: App.ackPid.count)
|
|
let pidAckBytesRead = try pidAckBuffer.withUnsafeMutableBytes { buffer in
|
|
try ackPipe.read(into: buffer)
|
|
}
|
|
guard pidAckBytesRead > 0 else {
|
|
throw App.Failure(message: "read ack pipe")
|
|
}
|
|
let pidAckStr = String(decoding: pidAckBuffer[..<pidAckBytesRead], as: UTF8.self)
|
|
|
|
guard pidAckStr == App.ackPid else {
|
|
throw App.Failure(message: "received invalid acknowledgement string: \(pidAckStr)")
|
|
}
|
|
|
|
guard setsid() != -1 else {
|
|
throw App.Errno(stage: "setsid()")
|
|
}
|
|
|
|
if process.terminal {
|
|
let pty = try Console()
|
|
try pty.configureStdIO()
|
|
var masterFD = pty.master
|
|
|
|
try withUnsafeBytes(of: &masterFD) { bytes in
|
|
_ = try syncPipe.write(bytes)
|
|
}
|
|
|
|
// Wait for the grandparent to tell us that they acked our console.
|
|
var consoleAckBuffer = [UInt8](repeating: 0, count: App.ackConsole.count)
|
|
let consoleAckBytesRead = try consoleAckBuffer.withUnsafeMutableBytes { buffer in
|
|
try ackPipe.read(into: buffer)
|
|
}
|
|
guard consoleAckBytesRead > 0 else {
|
|
throw App.Failure(message: "read ack pipe")
|
|
}
|
|
let consoleAckStr = String(decoding: consoleAckBuffer[..<consoleAckBytesRead], as: UTF8.self)
|
|
|
|
guard consoleAckStr == App.ackConsole else {
|
|
throw App.Failure(message: "received invalid acknowledgement string: \(consoleAckStr)")
|
|
}
|
|
|
|
guard ioctl(0, UInt(TIOCSCTTY), 0) != -1 else {
|
|
throw App.Errno(stage: "setctty(0)")
|
|
}
|
|
try pty.close()
|
|
}
|
|
|
|
// Apply O_CLOEXEC to all file descriptors except stdio.
|
|
// This ensures that all unwanted fds we may have accidentally
|
|
// inherited are marked close-on-exec so they stay out of the
|
|
// container.
|
|
try App.applyCloseExecOnFDs()
|
|
try App.setRLimits(rlimits: process.rlimits)
|
|
|
|
// Prepare capabilities (before user change)
|
|
let preparedCaps = try App.prepareCapabilities(capabilities: process.capabilities ?? ContainerizationOCI.LinuxCapabilities())
|
|
|
|
// Change stdio to be owned by the requested user.
|
|
try App.fixStdioPerms(user: process.user)
|
|
|
|
// Set uid, gid, and supplementary groups
|
|
try App.setPermissions(user: process.user)
|
|
|
|
// Finish capabilities (after user change)
|
|
try App.finishCapabilities(preparedCaps)
|
|
|
|
// Set no_new_privs if requested by the OCI spec.
|
|
try App.setNoNewPrivileges(process: process)
|
|
|
|
try App.exec(process: process, currentEnv: process.env)
|
|
} else { // parent process
|
|
// Send our child's pid to our parent before we exit.
|
|
var childPid = processID
|
|
try withUnsafeBytes(of: &childPid) { bytes in
|
|
_ = try syncPipe.write(bytes)
|
|
}
|
|
}
|
|
}
|
|
}
|