Skip to content

Commit c7763c7

Browse files
authored
vmexec: Set perms on stdio (#174)
We don't chown the container's stdio today.
1 parent 059ff40 commit c7763c7

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
@@ -100,7 +100,10 @@ struct ExecCommand: ParsableCommand {
100100
try App.applyCloseExecOnFDs()
101101
try App.setRLimits(rlimits: process.rlimits)
102102

103-
// set uid, gid, and supplementary groups
103+
// Change stdio to be owned by the requested user.
104+
try App.fixStdioPerms(user: process.user)
105+
106+
// Set uid, gid, and supplementary groups
104107
try App.setPermissions(user: process.user)
105108

106109
if process.terminal {

vminitd/Sources/vmexec/RunCommand.swift

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

110110
try App.setRLimits(rlimits: process.rlimits)
111111

112-
// set uid, gid, and supplementary groups
112+
// Change stdio to be owned by the requested user.
113+
try App.fixStdioPerms(user: process.user)
114+
115+
// Set uid, gid, and supplementary groups.
113116
try App.setPermissions(user: process.user)
114117

115118
if process.terminal {

vminitd/Sources/vmexec/vmexec.swift

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

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

0 commit comments

Comments
 (0)