Skip to content

Commit c28f3db

Browse files
committed
vmexec: Set perms on stdio
We don't chown the containers stdio today.
1 parent 958a83b commit c28f3db

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

vminitd/Sources/vmexec/ExecCommand.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ struct ExecCommand: ParsableCommand {
103103
try App.applyCloseExecOnFDs()
104104
try App.setRLimits(rlimits: process.rlimits)
105105

106-
// set uid, gid, and supplementary groups
106+
// Change stdio to be owned by the requested user.
107+
try App.fixStdioPerms(user: process.user)
108+
109+
// Set uid, gid, and supplementary groups
107110
try App.setPermissions(user: process.user)
108111

109112
if process.terminal {

vminitd/Sources/vmexec/RunCommand.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ struct RunCommand: ParsableCommand {
112112

113113
try App.setRLimits(rlimits: process.rlimits)
114114

115-
// set uid, gid, and supplementary groups
115+
// Change stdio to be owned by the requested user.
116+
try App.fixStdioPerms(user: process.user)
117+
118+
// Set uid, gid, and supplementary groups.
116119
try App.setPermissions(user: process.user)
117120

118121
if process.terminal {

vminitd/Sources/vmexec/vmexec.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ extension App {
106106
}
107107
}
108108

109+
static func fixStdioPerms(user: ContainerizationOCI.User) throws {
110+
for i in 0...2 {
111+
var fdStat = stat()
112+
try withUnsafeMutablePointer(to: &fdStat) { pointer in
113+
guard fstat(Int32(i), pointer) == 0 else {
114+
throw App.Errno(stage: "fstat(fd)")
115+
}
116+
}
117+
118+
let desired = uid_t(user.uid)
119+
if fdStat.st_uid != desired {
120+
guard fchown(Int32(i), desired, fdStat.st_gid) != -1 else {
121+
throw App.Errno(stage: "fchown(\(i))")
122+
}
123+
}
124+
}
125+
}
126+
109127
static func setRLimits(rlimits: [ContainerizationOCI.POSIXRlimit]) throws {
110128
for rl in rlimits {
111129
var limit = rlimit(rlim_cur: rl.soft, rlim_max: rl.hard)

0 commit comments

Comments
 (0)