The vulnerability is a double-free in the hivex crate. It occurs because both the Hive::close method and the Drop implementation for the Hive struct would free the same underlying raw handle. An explicit call to close() would free the handle, and then when the Hive object went out of scope, its drop() method would be invoked, freeing the same handle again.
This is exacerbated by the Hive::from_handle function, which was not marked as unsafe. This made it possible to create Hive objects from raw pointers in safe Rust code, potentially creating multiple Hive objects that point to the same handle. When these objects are dropped, a double-free or use-after-free would occur.
The patch addresses these issues by:
- Modifying
Hive::close to call std::mem::forget(self), which prevents the Drop implementation from running after close has already freed the handle.
- Marking
Hive::from_handle as unsafe, which forces callers to guarantee the safety of using the raw handle.
Therefore, the key functions involved in this vulnerability are Hive::close (which initiates the double-free sequence), Hive::from_handle (which allows for unsafe object creation), and Hive::drop (which performs the second free).