Sitelet https://github.com/integritee-network/worker/pull/293
Skip to content

Worker OCall Abstraction, PoC - #293

Merged
murerfel merged 1 commit into
masterfrom
feature/fm-worker-ocall-abstraction-poc
Jul 19, 2021
Merged

murerfel merged 1 commit into
masterfrom
feature/fm-worker-ocall-abstraction-poc

Conversation

@murerfel

@murerfel murerfel commented Jul 6, 2021 •

Copy link
Copy Markdown
Contributor

Proof-of-concept for the abstraction of the OCall API in the untrusted worker. This will be a first step for #287 .

The entire OCall bridge component is still a module of the worker for now. In a later stage, this will be extracted to a separate crate. Implementation&behavior can be injected using the Bridge struct, that internally uses global static state.

ocall_bridge_poc

@murerfel
murerfel requested a review from clangenb July 6, 2021 10:08
Comment thread worker/src/main.rs Outdated
Comment on lines +103 to +105
// initialize o-call bridge
OCallBridge::initialize(Arc::new(OCallBridgeComponentFactoryImpl {}));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here we initialize the component (i.e. global state) and inject the concrete implementation

@murerfel

murerfel commented Jul 6, 2021

Copy link
Copy Markdown
Contributor Author

A lot of the changes in the main file are just formatting changes, automatically applied by cargofmt upon saving

Comment on lines +26 to +31
pub struct RemoteAttestationOCallImpl {
// TODO as a member here we need the e-call API trait, so we can use it instead of making the e-call directly
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the actual implementation of the remote attestation o-calls. This was extracted from the c-functions and turned into interfaces that only use Rust types. Translating from and to C-API types should be the responsibility of the FFI functions.


pub struct Bridge {}

impl Bridge {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The Bridge is the static/global interface to inject concrete implementations (or rather the factories for them) - this is done at startup of the worker. On the other side, it is used by the o-call FFI to retrieve the state and forward calls to their respective implementation.

@haerdib haerdib Jul 7, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there any reason you're not putting such comments directly into the code? I think (atleast for me) it would be really helpful to have the option to look up such comments also later on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good input. This is also helpful for fellow sdk users.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, indeed, there probably should be more comments, especially in the future for all the public facing traits and functions.

I've neglected them in part because of laziness, but also because comments tend to go out of sync with the code VERY fast, and then they're more harmful than anything else. And here we're dealing with the beginning of some substantial refactoring, where everything is very volatile.. so far my excuse. Once we reach a more stable state, we should definitely do more documentation inside the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My latest commit added some of the comments you (rightfully) requested

Comment thread worker/src/ocall_bridge/bridge_api.rs
Comment thread worker/src/ocall_bridge/bridge_api.rs
Comment thread worker/src/ocall_bridge/ffi/get_ias_socket.rs Outdated
Comment on lines +68 to +74
let revocation_list: Vec<u8> =
unsafe { slice::from_raw_parts(p_sigrl, sigrl_len as usize).to_vec() };

let report = unsafe { *p_report };
let spid = unsafe { *p_spid };
let quote_nonce = unsafe { *p_nonce };

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Translate parameters from C-API types to Rust types

Comment on lines +77 to +91
let quote = get_quote_result.2;

if quote.len() as u32 > maxlen {
return sgx_status_t::SGX_ERROR_FAAS_BUFFER_TOO_SHORT;
}

let quote_slice = unsafe { slice::from_raw_parts_mut(p_quote, quote.len()) };
quote_slice.clone_from_slice(quote.as_slice());

unsafe {
*p_qe_report = get_quote_result.1;
*p_quote_len = quote.len() as u32;
};

get_quote_result.0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Translate results back to the C-API

Comment on lines +31 to +52
fn init_quote(&self) -> (sgx_status_t, sgx_target_info_t, sgx_epid_group_id_t) {
// TODO this translation to unsafe C-API should be moved to the EnclaveApi / ECall API
let mut ti: sgx_target_info_t = sgx_target_info_t::default();
let mut eg: sgx_epid_group_id_t = sgx_epid_group_id_t::default();

unsafe {
let ret_status = sgx_init_quote(
&mut ti as *mut sgx_target_info_t,
&mut eg as *mut sgx_epid_group_id_t,
);
(ret_status, ti, eg)
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

3 of the 4 methods for remote attestation basically just call back right into the enclave, using an e-call. Once we have the e-call API for this, these implementations should be moved to that API, making the implementation here very simple.

Comment on lines +29 to +39
impl OCallBridgeComponentFactory for OCallBridgeComponentFactoryImpl {
fn get_ra_api(&self) -> Arc<dyn RemoteAttestationOCall> {
Arc::new(RemoteAttestationOCallImpl {})
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Component factory, i.e. root for all ocall components. Once we're filling this up with more components for all the other ocalls, its usefulness will become apparent.

Comment on lines +55 to +80
#[test]
fn init_quote_sets_results() {
let mut ra_ocall_api_mock = MockRemoteAttestationOCall::new();
ra_ocall_api_mock
.expect_init_quote()
.times(1)
.returning(|| (sgx_status_t::SGX_SUCCESS, dummy_target_info(), [8u8; 4]));

let mut ti: sgx_target_info_t = sgx_target_info_t::default();
let mut eg: sgx_epid_group_id_t = sgx_epid_group_id_t::default();

let ret_status = sgx_init_quote(
&mut ti as *mut sgx_target_info_t,
&mut eg as *mut sgx_epid_group_id_t,
Arc::new(ra_ocall_api_mock),
);

assert_eq!(ret_status, sgx_status_t::SGX_SUCCESS);
assert_eq!(eg, [8u8; 4]);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Another simple test using the mocking framework to mock the RemoteAttestationOCall trait.

@murerfel murerfel self-assigned this Jul 6, 2021

@clangenb clangenb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks in general very good. In general, I don't see any design remarks that could stand in the way of our refactoring goal.

My remarks are only details and rather rust related.

Comment thread worker/src/ocall_bridge/attestation_ocall_impl.rs Outdated
Comment thread worker/src/ocall_bridge/attestation_ocall_impl.rs Outdated
Comment thread worker/src/ocall_bridge/attestation_ocall_impl.rs
Comment thread worker/src/ocall_bridge/bridge_api.rs
Comment thread worker/src/ocall_bridge/bridge_api.rs
Comment thread worker/src/ocall_bridge/component_factory.rs Outdated
Comment thread worker/src/main.rs
Comment thread worker/src/ocall_bridge/ffi/get_quote.rs Outdated
@murerfel
murerfel marked this pull request as ready for review July 7, 2021 06:18
Comment thread worker/Cargo.toml
@murerfel
murerfel requested a review from clangenb July 7, 2021 07:06
@murerfel
murerfel force-pushed the feature/fm-worker-ocall-abstraction-poc branch from e47ae3d to bab40fe Compare July 7, 2021 07:10

@clangenb clangenb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cool. I like the new errors! My two comments are only a little nitpicking. :) Will approve anyhow.

Comment thread worker/src/ocall_bridge/attestation_ocall_impl.rs
Comment thread worker/src/ocall_bridge/attestation_ocall_impl.rs
@murerfel
murerfel requested a review from haerdib July 7, 2021 07:48

@haerdib haerdib left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, though I haven't really looked at the overall structure. Leaving this task to @clangenb. :)
I'd really appreciate some more documentation though - especially when it gets as abstract as the bridge struct some reasoning comments (what is it used for, what's the original reasoning behind it...) would be really helpful.

}
}

fn lookup_ipv4(host: &str, port: u16) -> Result<SocketAddr, String> {

@haerdib haerdib Jul 7, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just as an information: This function, respective generally the transformation from a string and / or port to a SocketAdress is used for most ws applications, there's also one within the enclave

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, we should make sure then to share this code in an external crate, shared by both 👍

@murerfel
murerfel requested a review from brenzi July 7, 2021 13:42

@brenzi brenzi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

it is unfortunate that this PR is mixed with cargo fmt changes. This makes reviewing painful. Actually I thought that we enforce fmt in CI anyway, but maybe that was dropped when moving to GH actions? We should enfore fmt in CI if we don't do so yet (excluding sgx_runtime, because parity doesn't use fmt)

I'm not yet sure that "bridge" is a good name. In blockchain, a bridge is usually something that allows cross-blockchain interaction. But, actually, we're bridging layer one with layer two somehow, so I'd leave it

@murerfel

Copy link
Copy Markdown
Contributor Author

I wouldn't mind renaming 'bridge' to something else, especially when that name is already reserved for another concept in the domain. It will never again be as simple as now to do the renaming.. What are the alternatives?
OCall-

  • Gateway
  • Link
  • Facade
    ... any suggestions are welcome 😄

@brenzi

brenzi commented Jul 15, 2021 •

Copy link
Copy Markdown
Collaborator

@mullefel You mentioned that you need to rebase before merging? there seem to be no conflicts, but let me know. I'd be ready to merge as soon as the Jenkins tests have passed

Because we envision to implement an "integritee-bridge" in the future, I think we really should rename the "OcallBridge".
Gateway and Link are ambiguous again. "Facade" could work. What do you think about "Tunnel"?

@murerfel

murerfel commented Jul 19, 2021 •

Copy link
Copy Markdown
Contributor Author

I find 'Tunnel' an interesting suggestion - it's not something that I've found commonly used yet, but I think it could be a good fit in our case 👍 I will open an issue to re-name the bridges to 'tunnels'. I think it's less work to merge the PRs now and do the re-naming in a separate PR (instead of having to rebase a lot of conflicts because of the renaming)

Edit: created issue #316 for this

@murerfel
murerfel force-pushed the feature/fm-worker-ocall-abstraction-poc branch from 9c163f5 to 2ac8961 Compare July 19, 2021 07:51
@murerfel
murerfel force-pushed the feature/fm-worker-ocall-abstraction-poc branch from 2ac8961 to 6065d03 Compare July 19, 2021 08:28
@murerfel
murerfel merged commit 2918dde into master Jul 19, 2021
@murerfel
murerfel deleted the feature/fm-worker-ocall-abstraction-poc branch July 19, 2021 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants