Skip to content

Commit

Permalink
CVE-2022-36113: avoid unpacking .cargo-ok from the crate
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtriplett authored and pietroalbini committed Sep 14, 2022
1 parent 22dd4bf commit 97b8091
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,13 @@ impl<'cfg> RegistrySource<'cfg> {
prefix
)
}
// Prevent unpacking the lockfile from the crate itself.
if entry_path
.file_name()
.map_or(false, |p| p == PACKAGE_SOURCE_LOCK)
{
continue;
}
// Unpacking failed
let mut result = entry.unpack_in(parent).map_err(anyhow::Error::from);
if cfg!(windows) && restricted_names::is_windows_reserved_path(&entry_path) {
Expand All @@ -654,16 +661,14 @@ impl<'cfg> RegistrySource<'cfg> {
.with_context(|| format!("failed to unpack entry at `{}`", entry_path.display()))?;
}

// The lock file is created after unpacking so we overwrite a lock file
// which may have been extracted from the package.
// Now that we've finished unpacking, create and write to the lock file to indicate that
// unpacking was successful.
let mut ok = OpenOptions::new()
.create(true)
.create_new(true)
.read(true)
.write(true)
.open(&path)
.with_context(|| format!("failed to open `{}`", path.display()))?;

// Write to the lock file to indicate that unpacking was successful.
write!(ok, "ok")?;

Ok(unpack_dir.to_path_buf())
Expand Down

0 comments on commit 97b8091

Please sign in to comment.