Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion vminitd/Sources/vmexec/ExecCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ struct ExecCommand: ParsableCommand {
try App.applyCloseExecOnFDs()
try App.setRLimits(rlimits: process.rlimits)

// set uid, gid, and supplementary groups
// 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)

if process.terminal {
Expand Down
5 changes: 4 additions & 1 deletion vminitd/Sources/vmexec/RunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ struct RunCommand: ParsableCommand {

try App.setRLimits(rlimits: process.rlimits)

// set uid, gid, and supplementary groups
// 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)

if process.terminal {
Expand Down
18 changes: 18 additions & 0 deletions vminitd/Sources/vmexec/vmexec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ extension App {
}
}

static func fixStdioPerms(user: ContainerizationOCI.User) throws {
for i in 0...2 {
var fdStat = stat()
try withUnsafeMutablePointer(to: &fdStat) { pointer in
guard fstat(Int32(i), pointer) == 0 else {
throw App.Errno(stage: "fstat(fd)")
}
}

let desired = uid_t(user.uid)
if fdStat.st_uid != desired {
guard fchown(Int32(i), desired, fdStat.st_gid) != -1 else {
throw App.Errno(stage: "fchown(\(i))")
}
}
}
}

static func setRLimits(rlimits: [ContainerizationOCI.POSIXRlimit]) throws {
for rl in rlimits {
var limit = rlimit(rlim_cur: rl.soft, rlim_max: rl.hard)
Expand Down