Skip to content

Commit 5e8c704

Browse files
committed
Revert "[host/{mem,func},guest/host_functions,common/peb] Removed HostFunctionDefinitions memory region"
This reverts commit 1d938f2. Signed-off-by: danbugs <[email protected]>
1 parent 315ab5d commit 5e8c704

File tree

11 files changed

+366
-72
lines changed

11 files changed

+366
-72
lines changed

src/hyperlight_common/src/mem.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ pub struct HyperlightPEB {
4848
pub output_stack: GuestMemoryRegion,
4949
pub guest_heap: GuestMemoryRegion,
5050
pub guest_stack: GuestStack,
51+
pub host_function_definitions: GuestMemoryRegion,
5152
}

src/hyperlight_guest/src/guest_handle/host_comm.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
use alloc::format;
1818
use alloc::string::ToString;
1919
use alloc::vec::Vec;
20+
use core::slice::from_raw_parts;
2021

2122
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
2223
use hyperlight_common::flatbuffer_wrappers::function_types::{
@@ -25,6 +26,7 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
2526
use hyperlight_common::flatbuffer_wrappers::guest_error::{ErrorCode, GuestError};
2627
use hyperlight_common::flatbuffer_wrappers::guest_log_data::GuestLogData;
2728
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
29+
use hyperlight_common::flatbuffer_wrappers::host_function_details::HostFunctionDetails;
2830
use hyperlight_common::outb::OutBAction;
2931

3032
use super::handle::GuestHandle;
@@ -99,6 +101,21 @@ impl GuestHandle {
99101
self.get_host_return_value::<T>()
100102
}
101103

104+
pub fn get_host_function_details(&self) -> HostFunctionDetails {
105+
let peb_ptr = self.peb().unwrap();
106+
let host_function_details_buffer =
107+
unsafe { (*peb_ptr).host_function_definitions.ptr as *const u8 };
108+
let host_function_details_size =
109+
unsafe { (*peb_ptr).host_function_definitions.size as usize };
110+
111+
let host_function_details_slice: &[u8] =
112+
unsafe { from_raw_parts(host_function_details_buffer, host_function_details_size) };
113+
114+
host_function_details_slice
115+
.try_into()
116+
.expect("Failed to convert buffer to HostFunctionDetails")
117+
}
118+
102119
/// Write an error to the shared output data buffer.
103120
pub fn write_error(&self, error_code: ErrorCode, message: Option<&str>) {
104121
let guest_error: GuestError = GuestError::new(
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright 2024 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use alloc::format;
18+
use alloc::string::ToString;
19+
use alloc::vec::Vec;
20+
use core::slice::from_raw_parts;
21+
22+
use hyperlight_common::flatbuffer_wrappers::function_call::FunctionCall;
23+
use hyperlight_common::flatbuffer_wrappers::function_types::ParameterType;
24+
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
25+
use hyperlight_common::flatbuffer_wrappers::host_function_details::HostFunctionDetails;
26+
27+
use crate::error::{HyperlightGuestError, Result};
28+
use crate::P_PEB;
29+
30+
pub(crate) fn validate_host_function_call(function_call: &FunctionCall) -> Result<()> {
31+
// get host function details
32+
let host_function_details = get_host_function_details();
33+
34+
// check if there are any host functions
35+
if host_function_details.host_functions.is_none() {
36+
return Err(HyperlightGuestError::new(
37+
ErrorCode::GuestError,
38+
"No host functions found".to_string(),
39+
));
40+
}
41+
42+
// check if function w/ given name exists
43+
let host_function = if let Some(host_function) =
44+
host_function_details.find_by_function_name(&function_call.function_name)
45+
{
46+
host_function
47+
} else {
48+
return Err(HyperlightGuestError::new(
49+
ErrorCode::GuestError,
50+
format!(
51+
"Host Function Not Found: {}",
52+
function_call.function_name.clone()
53+
),
54+
));
55+
};
56+
57+
let function_call_fparameters = if let Some(parameters) = function_call.parameters.clone() {
58+
parameters
59+
} else {
60+
if host_function.parameter_types.is_some() {
61+
return Err(HyperlightGuestError::new(
62+
ErrorCode::GuestError,
63+
format!(
64+
"Incorrect parameter count for function: {}",
65+
function_call.function_name.clone()
66+
),
67+
));
68+
}
69+
70+
Vec::new() // if no parameters (and no mismatches), return empty vector
71+
};
72+
73+
let function_call_parameter_types = function_call_fparameters
74+
.iter()
75+
.map(|p| p.into())
76+
.collect::<Vec<ParameterType>>();
77+
78+
// Verify that the function call has the correct parameter types.
79+
host_function
80+
.verify_equal_parameter_types(&function_call_parameter_types)
81+
.map_err(|_| {
82+
HyperlightGuestError::new(
83+
ErrorCode::GuestError,
84+
format!(
85+
"Incorrect parameter type for function: {}",
86+
function_call.function_name.clone()
87+
),
88+
)
89+
})?;
90+
91+
Ok(())
92+
}
93+
94+
pub fn get_host_function_details() -> HostFunctionDetails {
95+
let peb_ptr = unsafe { P_PEB.unwrap() };
96+
97+
let host_function_details_buffer =
98+
unsafe { (*peb_ptr).hostFunctionDefinitions.fbHostFunctionDetails } as *const u8;
99+
let host_function_details_size =
100+
unsafe { (*peb_ptr).hostFunctionDefinitions.fbHostFunctionDetailsSize };
101+
102+
let host_function_details_slice: &[u8] = unsafe {
103+
from_raw_parts(
104+
host_function_details_buffer,
105+
host_function_details_size as usize,
106+
)
107+
};
108+
109+
host_function_details_slice
110+
.try_into()
111+
.expect("Failed to convert buffer to HostFunctionDetails")
112+
}

src/hyperlight_guest_bin/src/host_comm.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
2424
ParameterValue, ReturnType, ReturnValue,
2525
};
2626
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
27+
use hyperlight_common::flatbuffer_wrappers::host_function_details::HostFunctionDetails;
2728
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
2829
use hyperlight_guest::error::{HyperlightGuestError, Result};
2930

@@ -58,6 +59,12 @@ pub fn get_host_return_value<T: TryFrom<ReturnValue>>() -> Result<T> {
5859
handle.get_host_return_value::<T>()
5960
}
6061

62+
pub fn get_host_function_details() -> HostFunctionDetails {
63+
let handle = unsafe { GUEST_HANDLE };
64+
65+
handle.get_host_function_details()
66+
}
67+
6168
/// Print a message using the host's print function.
6269
///
6370
/// This function requires memory to be setup to be used. In particular, the

src/hyperlight_host/src/func/host_functions.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, Ret
2020

2121
use super::utils::for_each_tuple;
2222
use super::{ParameterTuple, ResultType, SupportedReturnType};
23+
use crate::sandbox::host_funcs::FunctionEntry;
2324
use crate::sandbox::{ExtraAllowedSyscall, UninitializedSandbox};
24-
use crate::{Result, log_then_return, new_error};
25+
use crate::{Result, new_error};
2526

2627
/// A sandbox on which (primitive) host functions can be registered
2728
///
@@ -52,7 +53,15 @@ impl Registerable for UninitializedSandbox {
5253
.host_funcs
5354
.try_lock()
5455
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
55-
(*hfs).register_host_function(name.to_string(), hf.into().into())
56+
57+
let entry = FunctionEntry {
58+
function: hf.into().into(),
59+
extra_allowed_syscalls: None,
60+
parameter_types: Args::TYPE,
61+
return_type: Output::TYPE,
62+
};
63+
64+
(*hfs).register_host_function(name.to_string(), entry, self.mgr.unwrap_mgr_mut())
5665
}
5766
#[cfg(all(feature = "seccomp", target_os = "linux"))]
5867
fn register_host_function_with_syscalls<Args: ParameterTuple, Output: SupportedReturnType>(
@@ -65,7 +74,15 @@ impl Registerable for UninitializedSandbox {
6574
.host_funcs
6675
.try_lock()
6776
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
68-
(*hfs).register_host_function_with_syscalls(name.to_string(), hf.into().into(), eas)
77+
78+
let entry = FunctionEntry {
79+
function: hf.into().into(),
80+
extra_allowed_syscalls: Some(eas),
81+
parameter_types: Args::TYPE,
82+
return_type: Output::TYPE,
83+
};
84+
85+
(*hfs).register_host_function(name.to_string(), entry, self.mgr.unwrap_mgr_mut())
6986
}
7087
}
7188

@@ -182,31 +199,18 @@ pub(crate) fn register_host_function<Args: ParameterTuple, Output: SupportedRetu
182199
) -> Result<()> {
183200
let func = func.into().into();
184201

185-
if let Some(_eas) = extra_allowed_syscalls {
186-
if cfg!(all(feature = "seccomp", target_os = "linux")) {
187-
// Register with extra allowed syscalls
188-
#[cfg(all(feature = "seccomp", target_os = "linux"))]
189-
{
190-
sandbox
191-
.host_funcs
192-
.try_lock()
193-
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
194-
.register_host_function_with_syscalls(name.to_string(), func, _eas)?;
195-
}
196-
} else {
197-
// Log and return an error
198-
log_then_return!(
199-
"Extra allowed syscalls are only supported on Linux with seccomp enabled"
200-
);
201-
}
202-
} else {
203-
// Register without extra allowed syscalls
204-
sandbox
205-
.host_funcs
206-
.try_lock()
207-
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
208-
.register_host_function(name.to_string(), func)?;
209-
}
202+
let entry = FunctionEntry {
203+
function: func,
204+
extra_allowed_syscalls: extra_allowed_syscalls.clone(),
205+
parameter_types: Args::TYPE,
206+
return_type: Output::TYPE,
207+
};
208+
209+
sandbox
210+
.host_funcs
211+
.try_lock()
212+
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
213+
.register_host_function(name.to_string(), entry, sandbox.mgr.unwrap_mgr_mut())?;
210214

211215
Ok(())
212216
}

0 commit comments

Comments
 (0)