GHSA-969w-q74q-9j8v

Suggest an improvement
Source
https://github.com/advisories/GHSA-969w-q74q-9j8v
Import Source
https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2022/12/GHSA-969w-q74q-9j8v/GHSA-969w-q74q-9j8v.json
JSON Data
https://api.osv.dev/v1/vulns/GHSA-969w-q74q-9j8v
Aliases
Published
2022-12-08T16:00:29Z
Modified
2023-11-08T04:17:44.447792Z
Summary
Unsound API in `secp256k1` allows use-after-free and invalid deallocation from safe code
Details

Because of incorrect bounds on method Secp256k1::preallocated_gen_new it was possible to cause use-after-free from safe consumer code. It was also possible to "free" memory not allocated by the appropriate allocator.

The method takes a place for storing the context as a mutable reference and returns context containing that reference. Because the code internally uses unsafe and the bounds were incorrect it was possible to create a context that outlived the passed reference (e.g. 'static). Because the context can alternatively carry heap-allocated pointer freed on drop it was possible to "deallocate" a pointer that wasn't returned from appropriate allocator. The code decides whether to free the memory based on type parameter but because of missing bound it was possible to construct the context with invalid parameter.

You are unaffected if you either

  • don't call Secp256k1::preallocated_gen_new
  • manually checked that your usage of the method is sound
  • upgraded to the patched version of secp256k1 (recommended)

The patched version uses correct bounds which means it is API-breaking. This effectively means adopting the policy of Rust lang itself allowing API-breaking changes to fix soundness bugs. Note however that valid straigthforward usage of the code will continue to compile. Only unsound code or code that propagates the bound in custom generics will fail to compile. If the code is sound fixing the bounds should be sufficient to make the code compile.

See the GitHub issue for example "exploit" code and further discussion.

Database specific
{
    "nvd_published_at": null,
    "cwe_ids": [],
    "severity": "MODERATE",
    "github_reviewed": true,
    "github_reviewed_at": "2022-12-08T16:00:29Z"
}
References

Affected packages

crates.io / secp256k1

Package

Affected ranges

Type
SEMVER
Events
Introduced
0.14.0
Fixed
0.22.2

Ecosystem specific

{
    "affected_functions": [
        "secp256k1::Secp256k1::preallocated_gen_new"
    ]
}

crates.io / secp256k1

Package

Affected ranges

Type
SEMVER
Events
Introduced
0.23.0
Fixed
0.23.5

Ecosystem specific

{
    "affected_functions": [
        "secp256k1::Secp256k1::preallocated_gen_new"
    ]
}

crates.io / secp256k1

Package

Affected ranges

Type
SEMVER
Events
Introduced
0.24.0
Fixed
0.24.2

Ecosystem specific

{
    "affected_functions": [
        "secp256k1::Secp256k1::preallocated_gen_new"
    ]
}