{"id":"GHSA-8f24-6m29-wm2r","aliases":["GHSA-8f24-6m29-wm2r","RUSTSEC-2023-0078"],"related":[],"kind":"vulnerability","state":"published","title":"use-after-free in tracing","summary":"The implementation of the [`Instrumented::into_inner`] method in affected versions of this crate contains undefined behavior due to incorrect use of [`std::mem::forget`] The function creates `*const` pointers to `self`, calls [`mem::forget(self)`][`std::mem::forget`], and then moves values out of those pointers using [`std::ptr::read`].\n\n```rust\n// To manually destructure `Instrumented` without `Drop`, we\n// move it into a ManuallyDrop and use pointers to its fields\nlet span: *const Span = &this.span;\nlet inner: *const ManuallyDrop<T> = &this.inner;\nmem::forget(self);\n// SAFETY: Those pointers are valid for reads, because `Drop` didn't\n//         run, and properly aligned, because `Instrumented` isn't\n//         `#[repr(packed)]`.\nlet _span = unsafe { span.read() };\nlet inner = unsafe { inner.read() };\n```\n\nHowever, the [`mem::forget` documentation][`std::mem::forget`] states:\n\n> Any resources the value manages, such as heap memory or a file handle, will\n> linger forever in an unreachable state. **However, it does not guarantee that\n> pointers to this memory will remain valid.**\n\nThis means that these pointers are no longer valid. This could result in a stack use-after-free if LLVM chooses to reuse `self`'s stack slot for a rebinding after the call to [`std::mem::forget`].\n\nThis undefined behavior has not been observed to cause miscompilation as of Rust 1.73.0. However, any use of this method with the affected versions of `tracing` are unsound.\n\nThe flaw was corrected in commit [20a1762] ([PR #2765]) by replacing the use of [`std::mem::forget`] with `std::mem::ManuallyDrop`, ensuring that the stack slot is not reused and the pointers remain valid when they are read. The fix is\npublished in `tracing` [v0.1.40]. Affected versions have been yanked from crates.io.\n\nThanks to [Taylor Cramer] and [Manish Goregaokar] for finding and correcting\nthis issue!\n\n[`Instrumented::into_inner`]:\n    https://docs.rs/tracing/latest/tracing/instrument/struct.Instrumented.html#method.into_inner\n[`std::mem::forget`]: https://doc.rust-lang.org/std/mem/fn.forget.html\n[`std::ptr::read`]:\n    https://doc.rust-lang.org/std/primitive.pointer.html#method.read-1\n[20a1762]:\n    https://github.com/tokio-rs/tracing/commit/20a1762b3fd5f1fafead198fd18e469c68683721\n[PR #2765]: https://github.com/tokio-rs/tracing/pull/2765\n[v0.1.40]: https://crates.io/crates/tracing/0.1.40\n[Taylor Cramer]: https://github.com/cramertj\n[Manish Goregaokar]: https://github.com/manishearth","summaryFormat":"markdown","summarySource":"ghsa","descriptions":[{"lang":"en","text":"The implementation of the [`Instrumented::into_inner`] method in affected versions of this crate contains undefined behavior due to incorrect use of [`std::mem::forget`] The function creates `*const` pointers to `self`, calls [`mem::forget(self)`][`std::mem::forget`], and then moves values out of those pointers using [`std::ptr::read`].\n\n```rust\n// To manually destructure `Instrumented` without `Drop`, we\n// move it into a ManuallyDrop and use pointers to its fields\nlet span: *const Span = &this.span;\nlet inner: *const ManuallyDrop<T> = &this.inner;\nmem::forget(self);\n// SAFETY: Those pointers are valid for reads, because `Drop` didn't\n//         run, and properly aligned, because `Instrumented` isn't\n//         `#[repr(packed)]`.\nlet _span = unsafe { span.read() };\nlet inner = unsafe { inner.read() };\n```\n\nHowever, the [`mem::forget` documentation][`std::mem::forget`] states:\n\n> Any resources the value manages, such as heap memory or a file handle, will\n> linger forever in an unreachable state. **However, it does not guarantee that\n> pointers to this memory will remain valid.**\n\nThis means that these pointers are no longer valid. This could result in a stack use-after-free if LLVM chooses to reuse `self`'s stack slot for a rebinding after the call to [`std::mem::forget`].\n\nThis undefined behavior has not been observed to cause miscompilation as of Rust 1.73.0. However, any use of this method with the affected versions of `tracing` are unsound.\n\nThe flaw was corrected in commit [20a1762] ([PR #2765]) by replacing the use of [`std::mem::forget`] with `std::mem::ManuallyDrop`, ensuring that the stack slot is not reused and the pointers remain valid when they are read. The fix is\npublished in `tracing` [v0.1.40]. Affected versions have been yanked from crates.io.\n\nThanks to [Taylor Cramer] and [Manish Goregaokar] for finding and correcting\nthis issue!\n\n[`Instrumented::into_inner`]:\n    https://docs.rs/tracing/latest/tracing/instrument/struct.Instrumented.html#method.into_inner\n[`std::mem::forget`]: https://doc.rust-lang.org/std/mem/fn.forget.html\n[`std::ptr::read`]:\n    https://doc.rust-lang.org/std/primitive.pointer.html#method.read-1\n[20a1762]:\n    https://github.com/tokio-rs/tracing/commit/20a1762b3fd5f1fafead198fd18e469c68683721\n[PR #2765]: https://github.com/tokio-rs/tracing/pull/2765\n[v0.1.40]: https://crates.io/crates/tracing/0.1.40\n[Taylor Cramer]: https://github.com/cramertj\n[Manish Goregaokar]: https://github.com/manishearth","format":"markdown","source":"ghsa"}],"published":"2023-10-19T12:00:00.000Z","modified":"2024-02-10T16:26:48.362Z","severity":{"scorer":"GitHub","scheme":"rating","rating":"MEDIUM"},"severities":[{"scorer":"GitHub","scheme":"rating","rating":"MEDIUM"}],"cwes":["CWE-416"],"affected":[{"ranges":[{"type":"semver","introduced":"0.1.38","status":"affected","fixed":"0.1.40"}],"ecosystem":"crates.io","package":"tracing","purl":"pkg:cargo/tracing","source":"ghsa"},{"ranges":[{"type":"semver","introduced":"0.1.38-0","status":"affected","fixed":"0.1.40"}],"ecosystem":"crates.io","package":"tracing","purl":"pkg:cargo/tracing","source":"rustsec"}],"references":[{"url":"https://github.com/tokio-rs/tracing/pull/2765","tags":["web"],"sources":["ghsa","rustsec"]},{"url":"https://github.com/tokio-rs/tracing/commit/20a1762b3fd5f1fafead198fd18e469c68683721","tags":["web"],"sources":["ghsa"]},{"url":"https://github.com/tokio-rs/tracing","tags":["package"],"sources":["ghsa"]},{"url":"https://github.com/tokio-rs/tracing/releases/tag/tracing-0.1.40","tags":["web"],"sources":["ghsa"]},{"url":"https://rustsec.org/advisories/RUSTSEC-2023-0078.html","tags":["advisory","web"],"sources":["ghsa","rustsec"]},{"url":"https://crates.io/crates/tracing","tags":["package"],"sources":["rustsec"]}],"credits":[],"sources":[{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-8f24-6m29-wm2r","url":"https://github.com/advisories/GHSA-8f24-6m29-wm2r","licence":"CC-BY-4.0","modified":"2024-02-10T16:26:48.362Z"},{"source":"rustsec","name":"RustSec Advisory Database","id":"RUSTSEC-2023-0078","url":"https://rustsec.org/advisories/RUSTSEC-2023-0078.html","licence":"CC0-1.0","modified":"2024-02-10T16:26:48.362Z"}],"updatedAt":"2026-11-25T22:56:43.166Z"}