Skip to content

Twoliter leaks temp directories containing the krane binary #546

@cbgbt

Description

@cbgbt

Problem

Twoliter leaks temporary directories containing the krane binary that is shipped with the tool

Cause

We use lazy_static! to construct a temporary directory where we write the krane binary.

lazy_static::lazy_static! {
    pub static ref KRANE: Krane = Krane::seal().unwrap();
}

#[derive(Debug)]
pub struct Krane {
    // Hold the file in memory to keep the fd open
    _tmp_dir: TempDir,
    path: PathBuf,
}

This static reference has little guarantee that the inner Drop implementation will be called on process exit, meaning that it's very easy for the tempfile to linger indefinitely.

Proposed Solution

We could refrain from putting krane in a tempfile, and always write it to a defined location (like ./tools). We could alternatively force the caller to tell us where to put it.

Complications

Overwriting a binary that another process is running is problematic (see this article about ETXTBSY.) This makes consistently writing to a known location problematic for concurrent twoliter invocations.

Resolution

We can avoid concurrent run/modify issues of the binary by:

  • Only writing krane under ./tools if there is a checksum mismatch
  • Writing krane to a tempfile under ./tools and then mv-ing it into place so that we get a new inode rather than replacing an existing inode. This means that running processes with a reference to the old inode will continue to work.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions