Skip to content

Commit 49ff8c5

Browse files
committed
Fix file descriptor leaks in remote import, save, and checkpoint operations
Fix four file descriptor leaks: 1. tunnel/images.go Import: os.Open(opts.Source) never closed 2. tunnel/images.go Save: second os.Open for oci-dir/docker-dir never closed 3. bindings/checkpoint.go Restore: os.Open(importPath) never closed 4. container_internal_common.go: os.Create in checkpoint volume export loop not closed on five error paths These are the same class of bug fixed in #28723 and #28724. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
1 parent 6ef4d68 commit 49ff8c5

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

libpod/container_internal_common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,26 +1201,31 @@ func (c *Container) exportCheckpoint(options ContainerCheckpointOptions) error {
12011201

12021202
volume, err := c.runtime.GetVolume(v.Name)
12031203
if err != nil {
1204+
volumeTarFile.Close()
12041205
return err
12051206
}
12061207

12071208
mp, err := volume.MountPoint()
12081209
if err != nil {
1210+
volumeTarFile.Close()
12091211
return err
12101212
}
12111213
if mp == "" {
1214+
volumeTarFile.Close()
12121215
return fmt.Errorf("volume %s is not mounted, cannot export: %w", volume.Name(), define.ErrInternal)
12131216
}
12141217
input, err := chrootarchive.Tar(mp, &archive.TarOptions{
12151218
Compression: archive.Uncompressed,
12161219
IncludeSourceDir: true,
12171220
}, mp)
12181221
if err != nil {
1222+
volumeTarFile.Close()
12191223
return fmt.Errorf("reading volume directory %q: %w", v.Dest, err)
12201224
}
12211225

12221226
_, err = io.Copy(volumeTarFile, input)
12231227
if err != nil {
1228+
volumeTarFile.Close()
12241229
return err
12251230
}
12261231
volumeTarFile.Close()

pkg/bindings/containers/checkpoint.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*ty
8888
}
8989
if i != "" {
9090
params.Set("import", "true")
91-
r, err = os.Open(i)
91+
importFile, err := os.Open(i)
9292
if err != nil {
9393
return nil, err
9494
}
95+
defer importFile.Close()
96+
r = importFile
9597
// Hard-code the name since it will be ignored in any case.
9698
nameOrID = "import"
9799
}

pkg/domain/infra/tunnel/images.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ func (ir *ImageEngine) Import(_ context.Context, opts entities.ImageImportOption
267267
if err != nil {
268268
return nil, err
269269
}
270+
defer f.Close()
270271
}
271272
return images.Import(ir.ClientCtx, f, options)
272273
}
@@ -354,6 +355,7 @@ func (ir *ImageEngine) Save(_ context.Context, nameOrID string, tags []string, o
354355
if err != nil {
355356
return err
356357
}
358+
defer f.Close()
357359
info, err := os.Stat(opts.Output)
358360
switch {
359361
case err == nil:

0 commit comments

Comments
 (0)