From d48eee737e5bf0567184eed75420aeee31a46e7c Mon Sep 17 00:00:00 2001 From: Lucy Menon <168595099+syntactically@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:30:49 +0000 Subject: [PATCH] leakpocalypse in internal resource table API Using the reference produced by `ResourceEntry::borrow` soundly requires the guarantee that destructors of `LentResourceGuard`s are properly run, which is famously not provided in Rust. The relevant APIs are only used internally by the component bindgen macros, and are always used in the correct fashion, but the functions are `pub` in `hyperlight_common` to allow them to be used from generated code. This commit just makes the minimal change of marking `ResourceEntry::lend()` as unsafe and making it the caller's responsibility to ensure that `drop` is called properly. A more satisfying solution would be to switch the `lend` API to only lend the resource entry within the scope of a passed-in closure. Unfortunately, (a) this would require restructuring the component bindgen code significantly and (b) it is not entirely clear that it would be expressive enough to support upcoming async guest calls. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com> --- src/hyperlight_common/src/resource.rs | 20 ++++++++++++-------- src/hyperlight_component_util/src/hl.rs | 8 +++++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/hyperlight_common/src/resource.rs b/src/hyperlight_common/src/resource.rs index 8d6c438ae..0957f3199 100644 --- a/src/hyperlight_common/src/resource.rs +++ b/src/hyperlight_common/src/resource.rs @@ -36,13 +36,14 @@ use spin::{RwLock, RwLockReadGuard}; /// # Safety /// Informally: this only creates SharedRead references, so having a /// bunch of them going at once is fine. Safe Rust in the host can't -/// use any earlier borrows (potentially invalidating these) until -/// borrow passed into [`ResourceEntry::lend`] has expired. Because -/// that borrow outlives the [`LentResourceGuard`], it will not expire -/// until that destructor is called. That destructor ensures that (a) -/// there are no outstanding [`BorrowedResourceGuard`]s alive (since -/// they would be holding the read side of the [`RwLock`] if they -/// were), and that (b) the shared flag has been set to false, so +/// use any earlier borrows (potentially invalidating these) until the +/// borrow passed into [`ResourceEntry::lend`] has expired. +/// [`ResourceEntry::lend`] is marked `unsafe`, and its unsafe +/// contract requires the caller to ensure that its destructor is +/// called before that borrow expires. That destructor ensures that +/// (a) there are no outstanding [`BorrowedResourceGuard`]s alive +/// (since they would be holding the read side of the [`RwLock`] if +/// they were), and that (b) the shared flag has been set to false, so /// [`ResourceEntry::borrow`] will never create another borrow pub enum ResourceEntry { Empty, @@ -105,7 +106,10 @@ impl ResourceEntry { pub fn give(x: T) -> ResourceEntry { ResourceEntry::Owned(x) } - pub fn lend<'a>(x: &'a T) -> (LentResourceGuard<'a>, ResourceEntry) { + /// # Safety + /// The caller must ensure that the destructor of the returned + /// `LentResourceGuard` is run before its `'a` lifetime expires. + pub unsafe fn lend<'a>(x: &'a T) -> (LentResourceGuard<'a>, ResourceEntry) { let flag = Arc::new(RwLock::new(true)); ( LentResourceGuard { diff --git a/src/hyperlight_component_util/src/hl.rs b/src/hyperlight_component_util/src/hl.rs index 77719a7a1..921aa50b6 100644 --- a/src/hyperlight_component_util/src/hl.rs +++ b/src/hyperlight_component_util/src/hl.rs @@ -672,7 +672,13 @@ pub fn emit_hl_marshal_value(s: &mut State, id: Ident, vt: &Value) -> TokenStrea let rid = format_ident!("resource{}", vi); quote! { let i = rts.#rid.len(); - let (lrg, re) = ::hyperlight_common::resource::ResourceEntry::lend(#id); + let (lrg, re) = unsafe { + // # Safety + // The returned resource guard is placed in + // the `to_cleanup` list, which is + // unconditionally dropped at the end of this function + ::hyperlight_common::resource::ResourceEntry::lend(#id) + }; to_cleanup.push(Box::new(lrg)); rts.#rid.push_back(re); alloc::vec::Vec::from(u32::to_le_bytes(i as u32))