Skip to content

Commit fd18588

Browse files
authored
Merge pull request #5009 from kolyshkin/defer-close-init
Close fds on error
2 parents 1f1ff4b + 93792e6 commit fd18588

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

libcontainer/init_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func startInitialization() (retErr error) {
175175
return fmt.Errorf("unable to convert _LIBCONTAINER_LOGPIPE: %w", err)
176176
}
177177
logPipe := os.NewFile(uintptr(logFd), "logpipe")
178+
defer logPipe.Close()
178179

179180
logrus.SetOutput(logPipe)
180181
logrus.SetFormatter(new(logrus.JSONFormatter))
@@ -190,6 +191,7 @@ func startInitialization() (retErr error) {
190191
return fmt.Errorf("unable to convert _LIBCONTAINER_FIFOFD: %w", err)
191192
}
192193
fifoFile = os.NewFile(uintptr(fifoFd), "initfifo")
194+
defer fifoFile.Close()
193195
}
194196

195197
var consoleSocket *os.File

libcontainer/mount_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func syscallMode(i fs.FileMode) (o uint32) {
250250
// process will need to do an old-fashioned mount(2) themselves.
251251
//
252252
// This helper is only intended to be used by goCreateMountSources.
253-
func mountFd(nsHandles *userns.Handles, m *configs.Mount) (*mountSource, error) {
253+
func mountFd(nsHandles *userns.Handles, m *configs.Mount) (_ *mountSource, retErr error) {
254254
if !m.IsBind() {
255255
return nil, errors.New("new mount api: only bind-mounts are supported")
256256
}
@@ -261,6 +261,11 @@ func mountFd(nsHandles *userns.Handles, m *configs.Mount) (*mountSource, error)
261261

262262
var mountFile *os.File
263263
var sourceType mountSourceType
264+
defer func() {
265+
if retErr != nil && mountFile != nil {
266+
mountFile.Close()
267+
}
268+
}()
264269

265270
// Ideally, we would use OPEN_TREE_CLONE for everything, because we can
266271
// be sure that the file descriptor cannot be used to escape outside of

libcontainer/process_linux.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type processComm struct {
6666
logPipeChild *os.File
6767
}
6868

69-
func newProcessComm() (*processComm, error) {
69+
func newProcessComm() (_ *processComm, retErr error) {
7070
var (
7171
comm processComm
7272
err error
@@ -75,10 +75,24 @@ func newProcessComm() (*processComm, error) {
7575
if err != nil {
7676
return nil, fmt.Errorf("unable to create init pipe: %w", err)
7777
}
78+
defer func() {
79+
if retErr != nil {
80+
comm.initSockParent.Close()
81+
comm.initSockChild.Close()
82+
}
83+
}()
84+
7885
comm.syncSockParent, comm.syncSockChild, err = newSyncSockpair("sync")
7986
if err != nil {
8087
return nil, fmt.Errorf("unable to create sync pipe: %w", err)
8188
}
89+
defer func() {
90+
if retErr != nil {
91+
comm.syncSockParent.Close()
92+
comm.syncSockChild.Close()
93+
}
94+
}()
95+
8296
comm.logPipeParent, comm.logPipeChild, err = os.Pipe()
8397
if err != nil {
8498
return nil, fmt.Errorf("unable to create log pipe: %w", err)

notify_socket.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,18 @@ func notifyHost(client *net.UnixConn, ready []byte, pid1 int) error {
175175
var errUnexpectedRead = errors.New("unexpected read from synchronization pipe")
176176

177177
// sdNotifyBarrier performs synchronization with systemd by means of the sd_notify_barrier protocol.
178-
func sdNotifyBarrier(client *net.UnixConn) error {
178+
func sdNotifyBarrier(client *net.UnixConn) (retErr error) {
179179
// Create a pipe for communicating with systemd daemon.
180180
pipeR, pipeW, err := os.Pipe()
181181
if err != nil {
182182
return err
183183
}
184+
defer func() {
185+
if retErr != nil {
186+
pipeW.Close()
187+
pipeR.Close()
188+
}
189+
}()
184190

185191
// Get the FD for the unix socket file to be able to use sendmsg.
186192
clientFd, err := client.File()

0 commit comments

Comments
 (0)