Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tn/unsafe handling #1114

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions enclave-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ fn sidechain_rpc_int(request: &str) -> Result<String> {
/// (parentchain components) have been initialized (because we need the parentchain
/// block import dispatcher).
#[no_mangle]
pub unsafe extern "C" fn init_enclave_sidechain_components() -> sgx_status_t {
pub extern "C" fn init_enclave_sidechain_components() -> sgx_status_t {
if let Err(e) = initialization::init_enclave_sidechain_components() {
error!("Failed to initialize sidechain components: {:?}", e);
return sgx_status_t::SGX_ERROR_UNEXPECTED
Expand Down Expand Up @@ -387,7 +387,7 @@ fn dispatch_parentchain_blocks_for_import<WorkerModeProvider: ProvideWorkerMode>
/// This trigger is only useful in combination with a `TriggeredDispatcher` and sidechain. In case no
/// sidechain and the `ImmediateDispatcher` are used, this function is obsolete.
#[no_mangle]
pub unsafe extern "C" fn trigger_parentchain_block_import() -> sgx_status_t {
pub extern "C" fn trigger_parentchain_block_import() -> sgx_status_t {
match internal_trigger_parentchain_block_import() {
Ok(()) => sgx_status_t::SGX_SUCCESS,
Err(e) => {
Expand Down
2 changes: 1 addition & 1 deletion enclave-runtime/src/tls_ra/tls_ra_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ where
}

#[no_mangle]
pub unsafe extern "C" fn run_state_provisioning_server(
pub extern "C" fn run_state_provisioning_server(
socket_fd: c_int,
sign_type: sgx_quote_sign_type_t,
skip_ra: c_int,
Expand Down
2 changes: 1 addition & 1 deletion enclave-runtime/src/top_pool_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use sp_runtime::{
use std::{sync::Arc, time::Instant, vec::Vec};

#[no_mangle]
pub unsafe extern "C" fn execute_trusted_calls() -> sgx_status_t {
pub extern "C" fn execute_trusted_calls() -> sgx_status_t {
if let Err(e) = execute_top_pool_trusted_calls_internal() {
return e.into()
}
Expand Down
2 changes: 1 addition & 1 deletion service/src/ocall_bridge/ffi/get_ias_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sgx_types::{c_int, sgx_status_t};
use std::sync::Arc;

#[no_mangle]
pub extern "C" fn ocall_get_ias_socket(ret_fd: *mut c_int) -> sgx_status_t {
pub unsafe extern "C" fn ocall_get_ias_socket(ret_fd: *mut c_int) -> sgx_status_t {
get_ias_socket(ret_fd, Bridge::get_ra_api()) // inject the RA API (global state)
}

Expand Down
45 changes: 20 additions & 25 deletions service/src/ocall_bridge/ffi/get_update_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,41 @@

*/

use crate::ocall_bridge::bridge_api::{Bridge, RemoteAttestationBridge};
use crate::ocall_bridge::bridge_api::{Bridge, OCallBridgeResult, RemoteAttestationBridge};
use log::*;
use sgx_types::{sgx_platform_info_t, sgx_status_t, sgx_update_info_bit_t};
use std::sync::Arc;

#[no_mangle]
pub extern "C" fn ocall_get_update_info(
pub unsafe extern "C" fn ocall_get_update_info(
p_platform_blob: *const sgx_platform_info_t,
enclave_trusted: i32,
p_update_info: *mut sgx_update_info_bit_t,
) -> sgx_status_t {
get_update_info(
p_platform_blob,
if p_platform_blob.is_null() || p_update_info.is_null() {
return sgx_status_t::SGX_ERROR_INVALID_PARAMETER
}
Comment on lines +30 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I think I like your proposal, and agree with it.

The only thing that I wonder; the teaclave examples don't check for null pointers in their sample code either, so I wonder if the overall construct with rust FFI and .edl files gives guarantees such that these function args can't be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah you are right regarding the teaclave example, but I don't see how they could guarantee that the arguments are never null. It can be a valid use-case to pass a null into a function so it cannot be enforced in general.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could imagine that the edger8r does enforce it. As the .edl files are strongly typed, the edger8r might enforce that a pointer to something does, in fact, always point to a valid object of said type. I am not sure about this though, but I found this very interesting reference to the edger8r.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only one place in the code samples where they check for null pointer on the v2.0.0-preview branch:
https://github.com/apache/incubator-teaclave-sgx-sdk/blob/v2.0.0-preview/samplecode/httpreq/enclave/src/lib.rs#L33:L38

(There are some checks on the current master branch as well)


I did not find anything related to explicit null checking in https://download.01.org/intel-sgx/latest/linux-latest/docs/Intel_SGX_Developer_Reference_Linux_2.18_Open_Source.pdf

I think it is worth to asking it on openenclave though just to have a confirmation.

let platform_blob = unsafe { *p_platform_blob };

let update_info_result = match get_update_info(
&platform_blob,
enclave_trusted,
p_update_info,
Bridge::get_ra_api(), // inject the RA API (global state)
)
) {
Ok(update_info_result) => update_info_result,
Err(e) => return e.into(),
};
unsafe {
*p_update_info = update_info_result;
}
sgx_status_t::SGX_SUCCESS
}

fn get_update_info(
p_platform_blob: *const sgx_platform_info_t,
platform_blob: &sgx_platform_info_t,
enclave_trusted: i32,
p_update_info: *mut sgx_update_info_bit_t,
ra_api: Arc<dyn RemoteAttestationBridge>,
) -> sgx_status_t {
) -> OCallBridgeResult<sgx_update_info_bit_t> {
debug!(" Entering ocall_get_update_info");

let platform_blob = unsafe { *p_platform_blob };

let update_info_result = match ra_api.get_update_info(platform_blob, enclave_trusted) {
Ok(r) => r,
Err(e) => {
error!("[-] Failed to get update info: {:?}", e);
return e.into()
},
};

unsafe {
*p_update_info = update_info_result;
}

sgx_status_t::SGX_SUCCESS
ra_api.get_update_info(*platform_blob, enclave_trusted)
}