Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cordyceps): fix use-after-free in
List
iterators
The `cordyceps::List` `Iter` and `Cursor` types currently have incorrect `Item` types that can result in a UAF if used incorrectly. These iterators return `T::Handle` as their `Item` types...but a `Handle` *owns* an element, and dropping the handle can drop the element. If, for example, the element is `Box`, then dropping the `Handle` will deallocate the memory that the entry lives in. Similarly, dropping an `Arc` entry would perform an invalid reference count decrement. If an iterator over a list is created twice, and the list contains a handle type such as `Box<T>`, then the second iterator will cause a double free. More worryingly, accessing any entries from the list will cause a use-after-free. We missed this issue since 1. there weren't any tests for the iterators, and 2. all the tests currently use `&'a Entry` as the handle type, so dropping an entry doesn't free memory. This branch fixes the use-after-free by changing the `Iter` and `Cursor` types to return *references* to the element, rather than `Handle`s. We will add an additional `Drain` iterator that actually removes elements from the list and returns the `Handle` type, in a follow-up PR. I've added a test which fails against the current `main` branch. BREAKING CHANGE: This changes the type signature of the `list::Iter` and `list::Cursor` types.
- Loading branch information